You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

693 lines
15KB

  1. #ifndef COMMON_H
  2. #define COMMON_H
  3. #define FFMPEG_VERSION_INT 0x000406
  4. #define FFMPEG_VERSION "0.4.6"
  5. #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
  6. #define CONFIG_WIN32
  7. #endif
  8. //#define ALT_BITSTREAM_WRITER
  9. //#define ALT_BITSTREAM_READER
  10. //#define ALIGNED_BITSTREAM
  11. #define FAST_GET_FIRST_VLC
  12. #ifdef HAVE_AV_CONFIG_H
  13. /* only include the following when compiling package */
  14. #include "../config.h"
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #ifndef ENODATA
  20. #define ENODATA 61
  21. #endif
  22. #endif
  23. #ifdef CONFIG_WIN32
  24. /* windows */
  25. typedef unsigned short UINT16;
  26. typedef signed short INT16;
  27. typedef unsigned char UINT8;
  28. typedef unsigned int UINT32;
  29. typedef unsigned __int64 UINT64;
  30. typedef signed char INT8;
  31. typedef signed int INT32;
  32. typedef signed __int64 INT64;
  33. typedef UINT8 uint8_t;
  34. typedef INT8 int8_t;
  35. typedef UINT16 uint16_t;
  36. typedef INT16 int16_t;
  37. typedef UINT32 uint32_t;
  38. typedef INT32 int32_t;
  39. #ifndef __MINGW32__
  40. #define INT64_C(c) (c ## i64)
  41. #define UINT64_C(c) (c ## i64)
  42. #define inline __inline
  43. /*
  44. Disable warning messages:
  45. warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
  46. warning C4305: 'argument' : truncation from 'const double' to 'float'
  47. */
  48. #pragma warning( disable : 4244 )
  49. #pragma warning( disable : 4305 )
  50. #else
  51. #define INT64_C(c) (c ## LL)
  52. #define UINT64_C(c) (c ## ULL)
  53. #endif /* __MINGW32__ */
  54. #define M_PI 3.14159265358979323846
  55. #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
  56. #ifdef _DEBUG
  57. #define DEBUG
  58. #endif
  59. // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
  60. #define bswap_32(x) \
  61. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  62. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  63. #define be2me_32(x) bswap_32(x)
  64. #define snprintf _snprintf
  65. #ifndef __MINGW32__
  66. /* no config.h with VC */
  67. #define CONFIG_ENCODERS 1
  68. #define CONFIG_DECODERS 1
  69. #define CONFIG_AC3 1
  70. #endif
  71. #else
  72. /* unix */
  73. #include <inttypes.h>
  74. #ifndef __WINE_WINDEF16_H
  75. /* workaround for typedef conflict in MPlayer (wine typedefs) */
  76. typedef unsigned short UINT16;
  77. typedef signed short INT16;
  78. #endif
  79. typedef unsigned char UINT8;
  80. typedef unsigned int UINT32;
  81. typedef unsigned long long UINT64;
  82. typedef signed char INT8;
  83. typedef signed int INT32;
  84. typedef signed long long INT64;
  85. #ifdef HAVE_AV_CONFIG_H
  86. #ifdef __FreeBSD__
  87. #include <sys/param.h>
  88. #endif
  89. #ifndef INT64_C
  90. #define INT64_C(c) (c ## LL)
  91. #define UINT64_C(c) (c ## ULL)
  92. #endif
  93. #include "../bswap.h"
  94. #ifdef USE_FASTMEMCPY
  95. #include "fastmemcpy.h"
  96. #endif
  97. #endif /* HAVE_AV_CONFIG_H */
  98. #endif /* !CONFIG_WIN32 */
  99. /* debug stuff */
  100. #ifdef HAVE_AV_CONFIG_H
  101. #ifndef DEBUG
  102. #define NDEBUG
  103. #endif
  104. #include <assert.h>
  105. /* dprintf macros */
  106. #if defined(CONFIG_WIN32) && !defined(__MINGW32__)
  107. inline void dprintf(const char* fmt,...) {}
  108. #else
  109. #ifdef DEBUG
  110. #define dprintf(fmt,args...) printf(fmt, ## args)
  111. #else
  112. #define dprintf(fmt,args...)
  113. #endif
  114. #endif /* !CONFIG_WIN32 */
  115. #endif /* HAVE_AV_CONFIG_H */
  116. /* bit output */
  117. struct PutBitContext;
  118. typedef void (*WriteDataFunc)(void *, UINT8 *, int);
  119. typedef struct PutBitContext {
  120. #ifdef ALT_BITSTREAM_WRITER
  121. UINT8 *buf, *buf_end;
  122. int index;
  123. #else
  124. UINT32 bit_buf;
  125. int bit_cnt;
  126. UINT8 *buf, *buf_ptr, *buf_end;
  127. void *opaque;
  128. WriteDataFunc write_data;
  129. #endif
  130. INT64 data_out_size; /* in bytes */
  131. } PutBitContext;
  132. void init_put_bits(PutBitContext *s,
  133. UINT8 *buffer, int buffer_size,
  134. void *opaque,
  135. void (*write_data)(void *, UINT8 *, int));
  136. #ifndef ALT_BITSTREAM_WRITER
  137. void put_bits(PutBitContext *s, int n, unsigned int value);
  138. #endif
  139. INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
  140. void align_put_bits(PutBitContext *s);
  141. void flush_put_bits(PutBitContext *s);
  142. /* jpeg specific put_bits */
  143. #ifndef ALT_BITSTREAM_WRITER
  144. void jput_bits(PutBitContext *s, int n, unsigned int value);
  145. #endif
  146. void jflush_put_bits(PutBitContext *s);
  147. /* bit input */
  148. typedef struct GetBitContext {
  149. #ifdef ALT_BITSTREAM_READER
  150. int index;
  151. UINT8 *buffer;
  152. #else
  153. UINT32 bit_buf;
  154. int bit_cnt;
  155. UINT8 *buf, *buf_ptr, *buf_end;
  156. #endif
  157. } GetBitContext;
  158. typedef struct VLC {
  159. int bits;
  160. INT16 *table_codes;
  161. INT8 *table_bits;
  162. int table_size, table_allocated;
  163. } VLC;
  164. /* used to avoid missaligned exceptions on some archs (alpha, ...) */
  165. #ifdef ARCH_X86
  166. #define unaligned32(a) (*(UINT32*)(a))
  167. #else
  168. #ifdef __GNUC__
  169. static inline uint32_t unaligned32(const void *v) {
  170. struct Unaligned {
  171. uint32_t i;
  172. } __attribute__((packed));
  173. return ((const struct Unaligned *) v)->i;
  174. }
  175. #elif defined(__DECC)
  176. static inline uint32_t unaligned32(const void *v) {
  177. return *(const __unaligned uint32_t *) v;
  178. }
  179. #else
  180. static inline uint32_t unaligned32(const void *v) {
  181. return *(const uint32_t *) v;
  182. }
  183. #endif
  184. #endif //!ARCH_X86
  185. #ifdef ALT_BITSTREAM_WRITER
  186. static inline void put_bits(PutBitContext *s, int n, int value)
  187. {
  188. #ifdef ARCH_X86
  189. asm volatile(
  190. "movl $7, %%ecx \n\t"
  191. "andl %0, %%ecx \n\t"
  192. "addl %3, %%ecx \n\t"
  193. "negl %%ecx \n\t"
  194. "shll %%cl, %1 \n\t"
  195. "bswapl %1 \n\t"
  196. "movl %0, %%ecx \n\t"
  197. "shrl $3, %%ecx \n\t"
  198. "orl %1, (%%ecx, %2) \n\t"
  199. "addl %3, %0 \n\t"
  200. "movl $0, 4(%%ecx, %2) \n\t"
  201. : "=&r" (s->index), "=&r" (value)
  202. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
  203. : "%ecx"
  204. );
  205. #else
  206. int index= s->index;
  207. uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  208. ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
  209. ptr[1] = 0;
  210. //if(n>24) printf("%d %d\n", n, value);
  211. index+= n;
  212. s->index= index;
  213. #endif
  214. }
  215. #endif
  216. #ifdef ALT_BITSTREAM_WRITER
  217. static inline void jput_bits(PutBitContext *s, int n, int value)
  218. {
  219. int index= s->index;
  220. uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  221. int v= ptr[0];
  222. //if(n>24) printf("%d %d\n", n, value);
  223. v |= be2me_32(value<<(32-n-(index&7) ));
  224. if(((v+0x01010101)^0xFFFFFFFF)&v&0x80808080)
  225. {
  226. /* handle idiotic (m)jpeg escapes */
  227. uint8_t *bPtr= (uint8_t*)ptr;
  228. int numChecked= ((index+n)>>3) - (index>>3);
  229. v= be2me_32(v);
  230. *(bPtr++)= v>>24;
  231. if((v&0xFF000000)==0xFF000000 && numChecked>0){
  232. *(bPtr++)= 0x00;
  233. index+=8;
  234. }
  235. *(bPtr++)= (v>>16)&0xFF;
  236. if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
  237. *(bPtr++)= 0x00;
  238. index+=8;
  239. }
  240. *(bPtr++)= (v>>8)&0xFF;
  241. if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
  242. *(bPtr++)= 0x00;
  243. index+=8;
  244. }
  245. *(bPtr++)= v&0xFF;
  246. if((v&0x000000FF)==0x000000FF && numChecked>3){
  247. *(bPtr++)= 0x00;
  248. index+=8;
  249. }
  250. *((uint32_t*)bPtr)= 0;
  251. }
  252. else
  253. {
  254. ptr[0] = v;
  255. ptr[1] = 0;
  256. }
  257. index+= n;
  258. s->index= index;
  259. }
  260. #endif
  261. static inline uint8_t* pbBufPtr(PutBitContext *s)
  262. {
  263. #ifdef ALT_BITSTREAM_WRITER
  264. return s->buf + (s->index>>3);
  265. #else
  266. return s->buf_ptr;
  267. #endif
  268. }
  269. void init_get_bits(GetBitContext *s,
  270. UINT8 *buffer, int buffer_size);
  271. #ifndef ALT_BITSTREAM_READER
  272. unsigned int get_bits_long(GetBitContext *s, int n);
  273. unsigned int show_bits_long(GetBitContext *s, int n);
  274. #endif
  275. static inline unsigned int get_bits(GetBitContext *s, int n){
  276. #ifdef ALT_BITSTREAM_READER
  277. #ifdef ALIGNED_BITSTREAM
  278. int index= s->index;
  279. uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  280. uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  281. #ifdef ARCH_X86
  282. asm ("shldl %%cl, %2, %0\n\t"
  283. : "=r" (result1)
  284. : "0" (result1), "r" (result2), "c" (index));
  285. #else
  286. result1<<= (index&0x1F);
  287. result2= (result2>>1) >> (31-(index&0x1F));
  288. result1|= result2;
  289. #endif
  290. result1>>= 32 - n;
  291. index+= n;
  292. s->index= index;
  293. return result1;
  294. #else //ALIGNED_BITSTREAM
  295. int index= s->index;
  296. uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
  297. result<<= (index&0x07);
  298. result>>= 32 - n;
  299. index+= n;
  300. s->index= index;
  301. return result;
  302. #endif //!ALIGNED_BITSTREAM
  303. #else //ALT_BITSTREAM_READER
  304. if(s->bit_cnt>=n){
  305. /* most common case here */
  306. unsigned int val = s->bit_buf >> (32 - n);
  307. s->bit_buf <<= n;
  308. s->bit_cnt -= n;
  309. #ifdef STATS
  310. st_bit_counts[st_current_index] += n;
  311. #endif
  312. return val;
  313. }
  314. return get_bits_long(s,n);
  315. #endif //!ALT_BITSTREAM_READER
  316. }
  317. static inline unsigned int get_bits1(GetBitContext *s){
  318. #ifdef ALT_BITSTREAM_READER
  319. int index= s->index;
  320. uint8_t result= s->buffer[ index>>3 ];
  321. result<<= (index&0x07);
  322. result>>= 8 - 1;
  323. index++;
  324. s->index= index;
  325. return result;
  326. #else
  327. if(s->bit_cnt>0){
  328. /* most common case here */
  329. unsigned int val = s->bit_buf >> 31;
  330. s->bit_buf <<= 1;
  331. s->bit_cnt--;
  332. #ifdef STATS
  333. st_bit_counts[st_current_index]++;
  334. #endif
  335. return val;
  336. }
  337. return get_bits_long(s,1);
  338. #endif
  339. }
  340. /* This function is identical to get_bits(), the only */
  341. /* diference is that it doesn't touch the buffer */
  342. /* it is usefull to see the buffer. */
  343. static inline unsigned int show_bits(GetBitContext *s, int n)
  344. {
  345. #ifdef ALT_BITSTREAM_READER
  346. #ifdef ALIGNED_BITSTREAM
  347. int index= s->index;
  348. uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  349. uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  350. #ifdef ARCH_X86
  351. asm ("shldl %%cl, %2, %0\n\t"
  352. : "=r" (result1)
  353. : "0" (result1), "r" (result2), "c" (index));
  354. #else
  355. result1<<= (index&0x1F);
  356. result2= (result2>>1) >> (31-(index&0x1F));
  357. result1|= result2;
  358. #endif
  359. result1>>= 32 - n;
  360. return result1;
  361. #else //ALIGNED_BITSTREAM
  362. int index= s->index;
  363. uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
  364. result<<= (index&0x07);
  365. result>>= 32 - n;
  366. return result;
  367. #endif //!ALIGNED_BITSTREAM
  368. #else //ALT_BITSTREAM_READER
  369. if(s->bit_cnt>=n) {
  370. /* most common case here */
  371. unsigned int val = s->bit_buf >> (32 - n);
  372. return val;
  373. }
  374. return show_bits_long(s,n);
  375. #endif //!ALT_BITSTREAM_READER
  376. }
  377. static inline void skip_bits(GetBitContext *s, int n){
  378. #ifdef ALT_BITSTREAM_READER
  379. s->index+= n;
  380. #else
  381. if(s->bit_cnt>=n){
  382. /* most common case here */
  383. s->bit_buf <<= n;
  384. s->bit_cnt -= n;
  385. #ifdef STATS
  386. st_bit_counts[st_current_index] += n;
  387. #endif
  388. } else {
  389. get_bits_long(s,n);
  390. }
  391. #endif
  392. }
  393. static inline void skip_bits1(GetBitContext *s){
  394. #ifdef ALT_BITSTREAM_READER
  395. s->index++;
  396. #else
  397. if(s->bit_cnt>0){
  398. /* most common case here */
  399. s->bit_buf <<= 1;
  400. s->bit_cnt--;
  401. #ifdef STATS
  402. st_bit_counts[st_current_index]++;
  403. #endif
  404. } else {
  405. get_bits_long(s,1);
  406. }
  407. #endif
  408. }
  409. static inline int get_bits_count(GetBitContext *s)
  410. {
  411. #ifdef ALT_BITSTREAM_READER
  412. return s->index;
  413. #else
  414. return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
  415. #endif
  416. }
  417. void align_get_bits(GetBitContext *s);
  418. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  419. const void *bits, int bits_wrap, int bits_size,
  420. const void *codes, int codes_wrap, int codes_size);
  421. void free_vlc(VLC *vlc);
  422. #ifdef ALT_BITSTREAM_READER
  423. #ifdef ALIGNED_BITSTREAM
  424. #ifdef ARCH_X86
  425. #define SHOW_BITS(s, val, n) \
  426. val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
  427. {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
  428. asm ("shldl %%cl, %2, %0\n\t"\
  429. : "=r" (val)\
  430. : "0" (val), "r" (result2), "c" (bit_cnt));\
  431. ((uint32_t)val)>>= 32 - n;}
  432. #else //ARCH_X86
  433. #define SHOW_BITS(s, val, n) \
  434. val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
  435. {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
  436. val<<= (bit_cnt&0x1F);\
  437. result2= (result2>>1) >> (31-(bit_cnt&0x1F));\
  438. val|= result2;\
  439. ((uint32_t)val)>>= 32 - n;}
  440. #endif //!ARCH_X86
  441. #else //ALIGNED_BITSTREAM
  442. #define SHOW_BITS(s, val, n) \
  443. val= be2me_32( unaligned32( ((uint8_t *)(s)->buffer)+(bit_cnt>>3) ) );\
  444. val<<= (bit_cnt&0x07);\
  445. ((uint32_t)val)>>= 32 - n;
  446. #endif // !ALIGNED_BITSTREAM
  447. #define FLUSH_BITS(n) bit_cnt+=n;
  448. #define SAVE_BITS(s) bit_cnt= (s)->index;
  449. #define RESTORE_BITS(s) (s)->index= bit_cnt;
  450. #else
  451. /* macro to go faster */
  452. /* n must be <= 24 */
  453. /* XXX: optimize buffer end test */
  454. #define SHOW_BITS(s, val, n)\
  455. {\
  456. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  457. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  458. bit_cnt += 8;\
  459. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  460. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  461. bit_cnt += 8;\
  462. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  463. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  464. bit_cnt += 8;\
  465. }\
  466. }\
  467. }\
  468. val = bit_buf >> (32 - n);\
  469. }
  470. /* SHOW_BITS with n1 >= n must be been done before */
  471. #define FLUSH_BITS(n)\
  472. {\
  473. bit_buf <<= n;\
  474. bit_cnt -= n;\
  475. }
  476. #define SAVE_BITS(s) \
  477. {\
  478. bit_cnt = (s)->bit_cnt;\
  479. bit_buf = (s)->bit_buf;\
  480. buf_ptr = (s)->buf_ptr;\
  481. }
  482. #define RESTORE_BITS(s) \
  483. {\
  484. (s)->buf_ptr = buf_ptr;\
  485. (s)->bit_buf = bit_buf;\
  486. (s)->bit_cnt = bit_cnt;\
  487. }
  488. #endif // !ALT_BITSTREAM_READER
  489. static inline int get_vlc(GetBitContext *s, VLC *vlc)
  490. {
  491. int code, n, nb_bits, index;
  492. INT16 *table_codes;
  493. INT8 *table_bits;
  494. int bit_cnt;
  495. #ifndef ALT_BITSTREAM_READER
  496. UINT32 bit_buf;
  497. UINT8 *buf_ptr;
  498. #endif
  499. SAVE_BITS(s);
  500. nb_bits = vlc->bits;
  501. table_codes = vlc->table_codes;
  502. table_bits = vlc->table_bits;
  503. #ifdef FAST_GET_FIRST_VLC
  504. SHOW_BITS(s, index, nb_bits);
  505. code = table_codes[index];
  506. n = table_bits[index];
  507. if (n > 0) {
  508. /* most common case (90%)*/
  509. FLUSH_BITS(n);
  510. RESTORE_BITS(s);
  511. return code;
  512. } else if (n == 0) {
  513. return -1;
  514. } else {
  515. FLUSH_BITS(nb_bits);
  516. nb_bits = -n;
  517. table_codes = vlc->table_codes + code;
  518. table_bits = vlc->table_bits + code;
  519. }
  520. #endif
  521. for(;;) {
  522. SHOW_BITS(s, index, nb_bits);
  523. code = table_codes[index];
  524. n = table_bits[index];
  525. if (n > 0) {
  526. /* most common case */
  527. FLUSH_BITS(n);
  528. #ifdef STATS
  529. st_bit_counts[st_current_index] += n;
  530. #endif
  531. break;
  532. } else if (n == 0) {
  533. return -1;
  534. } else {
  535. FLUSH_BITS(nb_bits);
  536. #ifdef STATS
  537. st_bit_counts[st_current_index] += nb_bits;
  538. #endif
  539. nb_bits = -n;
  540. table_codes = vlc->table_codes + code;
  541. table_bits = vlc->table_bits + code;
  542. }
  543. }
  544. RESTORE_BITS(s);
  545. return code;
  546. }
  547. /* define it to include statistics code (useful only for optimizing
  548. codec efficiency */
  549. //#define STATS
  550. #ifdef STATS
  551. enum {
  552. ST_UNKNOWN,
  553. ST_DC,
  554. ST_INTRA_AC,
  555. ST_INTER_AC,
  556. ST_INTRA_MB,
  557. ST_INTER_MB,
  558. ST_MV,
  559. ST_NB,
  560. };
  561. extern int st_current_index;
  562. extern unsigned int st_bit_counts[ST_NB];
  563. extern unsigned int st_out_bit_counts[ST_NB];
  564. void print_stats(void);
  565. #endif
  566. /* misc math functions */
  567. static inline int av_log2(unsigned int v)
  568. {
  569. int n;
  570. n = 0;
  571. if (v & 0xffff0000) {
  572. v >>= 16;
  573. n += 16;
  574. }
  575. if (v & 0xff00) {
  576. v >>= 8;
  577. n += 8;
  578. }
  579. if (v & 0xf0) {
  580. v >>= 4;
  581. n += 4;
  582. }
  583. if (v & 0xc) {
  584. v >>= 2;
  585. n += 2;
  586. }
  587. if (v & 0x2) {
  588. n++;
  589. }
  590. return n;
  591. }
  592. /* memory */
  593. void *av_mallocz(int size);
  594. #endif