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.

905 lines
20KB

  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 ALIGNED_BITSTREAM_WRITER
  10. //#define ALT_BITSTREAM_READER
  11. //#define ALIGNED_BITSTREAM
  12. #define FAST_GET_FIRST_VLC
  13. //#define DUMP_STREAM // only works with the ALT_BITSTREAM_READER
  14. #ifdef HAVE_AV_CONFIG_H
  15. /* only include the following when compiling package */
  16. #include "config.h"
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <math.h>
  22. #ifndef ENODATA
  23. #define ENODATA 61
  24. #endif
  25. #endif /* HAVE_AV_CONFIG_H */
  26. #ifdef CONFIG_WIN32
  27. /* windows */
  28. typedef unsigned short UINT16;
  29. typedef signed short INT16;
  30. typedef unsigned char UINT8;
  31. typedef unsigned int UINT32;
  32. typedef unsigned __int64 UINT64;
  33. typedef signed char INT8;
  34. typedef signed int INT32;
  35. typedef signed __int64 INT64;
  36. typedef UINT8 uint8_t;
  37. typedef INT8 int8_t;
  38. typedef UINT16 uint16_t;
  39. typedef INT16 int16_t;
  40. typedef UINT32 uint32_t;
  41. typedef INT32 int32_t;
  42. typedef UINT64 uint64_t;
  43. typedef INT64 int64_t;
  44. #ifndef __MINGW32__
  45. #define INT64_C(c) (c ## i64)
  46. #define UINT64_C(c) (c ## i64)
  47. #define inline __inline
  48. #else
  49. #define INT64_C(c) (c ## LL)
  50. #define UINT64_C(c) (c ## ULL)
  51. #endif /* __MINGW32__ */
  52. #define M_PI 3.14159265358979323846
  53. #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
  54. #ifdef _DEBUG
  55. #define DEBUG
  56. #endif
  57. #define snprintf _snprintf
  58. #ifdef HAVE_AV_CONFIG_H
  59. #define MANGLE(a) "_" #a
  60. #endif /* HAVE_AV_CONFIG_H */
  61. #else /* CONFIG_WIN32 */
  62. /* unix */
  63. #include <inttypes.h>
  64. #ifndef __WINE_WINDEF16_H
  65. /* workaround for typedef conflict in MPlayer (wine typedefs) */
  66. typedef unsigned short UINT16;
  67. typedef signed short INT16;
  68. #endif
  69. typedef unsigned char UINT8;
  70. typedef unsigned int UINT32;
  71. typedef unsigned long long UINT64;
  72. typedef signed char INT8;
  73. typedef signed int INT32;
  74. typedef signed long long INT64;
  75. #ifdef HAVE_AV_CONFIG_H
  76. #ifdef __FreeBSD__
  77. #include <sys/param.h>
  78. #endif
  79. #ifndef INT64_C
  80. #define INT64_C(c) (c ## LL)
  81. #define UINT64_C(c) (c ## ULL)
  82. #endif
  83. #ifdef USE_FASTMEMCPY
  84. #include "fastmemcpy.h"
  85. #endif
  86. #define MANGLE(a) #a
  87. #endif /* HAVE_AV_CONFIG_H */
  88. #endif /* !CONFIG_WIN32 */
  89. #ifdef HAVE_AV_CONFIG_H
  90. #include "bswap.h"
  91. /* debug stuff */
  92. #ifndef DEBUG
  93. #define NDEBUG
  94. #endif
  95. #include <assert.h>
  96. /* dprintf macros */
  97. #if defined(CONFIG_WIN32) && !defined(__MINGW32__)
  98. inline void dprintf(const char* fmt,...) {}
  99. #else
  100. #ifdef DEBUG
  101. #define dprintf(fmt,args...) printf(fmt, ## args)
  102. #else
  103. #define dprintf(fmt,args...)
  104. #endif
  105. #endif /* !CONFIG_WIN32 */
  106. #endif /* HAVE_AV_CONFIG_H */
  107. #define av_abort() do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  108. /* assume b>0 */
  109. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  110. #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
  111. /* bit output */
  112. struct PutBitContext;
  113. typedef void (*WriteDataFunc)(void *, UINT8 *, int);
  114. typedef struct PutBitContext {
  115. #ifdef ALT_BITSTREAM_WRITER
  116. UINT8 *buf, *buf_end;
  117. int index;
  118. #else
  119. UINT32 bit_buf;
  120. int bit_left;
  121. UINT8 *buf, *buf_ptr, *buf_end;
  122. #endif
  123. INT64 data_out_size; /* in bytes */
  124. } PutBitContext;
  125. void init_put_bits(PutBitContext *s,
  126. UINT8 *buffer, int buffer_size,
  127. void *opaque,
  128. void (*write_data)(void *, UINT8 *, int));
  129. INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
  130. void align_put_bits(PutBitContext *s);
  131. void flush_put_bits(PutBitContext *s);
  132. void put_string(PutBitContext * pbc, char *s);
  133. /* jpeg specific put_bits */
  134. void jflush_put_bits(PutBitContext *s);
  135. /* bit input */
  136. typedef struct GetBitContext {
  137. #ifdef ALT_BITSTREAM_READER
  138. int index;
  139. UINT8 *buffer;
  140. #else
  141. UINT32 bit_buf;
  142. int bit_cnt;
  143. UINT8 *buf, *buf_ptr, *buf_end;
  144. #endif
  145. int size;
  146. } GetBitContext;
  147. static inline int get_bits_count(GetBitContext *s);
  148. typedef struct VLC {
  149. int bits;
  150. INT16 *table_codes;
  151. INT8 *table_bits;
  152. int table_size, table_allocated;
  153. } VLC;
  154. /* used to avoid missaligned exceptions on some archs (alpha, ...) */
  155. #ifdef ARCH_X86
  156. #define unaligned32(a) (*(UINT32*)(a))
  157. #else
  158. #ifdef __GNUC__
  159. static inline uint32_t unaligned32(const void *v) {
  160. struct Unaligned {
  161. uint32_t i;
  162. } __attribute__((packed));
  163. return ((const struct Unaligned *) v)->i;
  164. }
  165. #elif defined(__DECC)
  166. static inline uint32_t unaligned32(const void *v) {
  167. return *(const __unaligned uint32_t *) v;
  168. }
  169. #else
  170. static inline uint32_t unaligned32(const void *v) {
  171. return *(const uint32_t *) v;
  172. }
  173. #endif
  174. #endif //!ARCH_X86
  175. #ifndef ALT_BITSTREAM_WRITER
  176. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  177. {
  178. unsigned int bit_buf;
  179. int bit_left;
  180. #ifdef STATS
  181. st_out_bit_counts[st_current_index] += n;
  182. #endif
  183. // printf("put_bits=%d %x\n", n, value);
  184. assert(n == 32 || value < (1U << n));
  185. bit_buf = s->bit_buf;
  186. bit_left = s->bit_left;
  187. // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
  188. /* XXX: optimize */
  189. if (n < bit_left) {
  190. bit_buf = (bit_buf<<n) | value;
  191. bit_left-=n;
  192. } else {
  193. bit_buf<<=bit_left;
  194. bit_buf |= value >> (n - bit_left);
  195. *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
  196. //printf("bitbuf = %08x\n", bit_buf);
  197. s->buf_ptr+=4;
  198. bit_left+=32 - n;
  199. bit_buf = value;
  200. }
  201. s->bit_buf = bit_buf;
  202. s->bit_left = bit_left;
  203. }
  204. #endif
  205. #ifdef ALT_BITSTREAM_WRITER
  206. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  207. {
  208. #ifdef ALIGNED_BITSTREAM_WRITER
  209. #ifdef ARCH_X86
  210. asm volatile(
  211. "movl %0, %%ecx \n\t"
  212. "xorl %%eax, %%eax \n\t"
  213. "shrdl %%cl, %1, %%eax \n\t"
  214. "shrl %%cl, %1 \n\t"
  215. "movl %0, %%ecx \n\t"
  216. "shrl $3, %%ecx \n\t"
  217. "andl $0xFFFFFFFC, %%ecx \n\t"
  218. "bswapl %1 \n\t"
  219. "orl %1, (%2, %%ecx) \n\t"
  220. "bswapl %%eax \n\t"
  221. "addl %3, %0 \n\t"
  222. "movl %%eax, 4(%2, %%ecx) \n\t"
  223. : "=&r" (s->index), "=&r" (value)
  224. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
  225. : "%eax", "%ecx"
  226. );
  227. #else
  228. int index= s->index;
  229. uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
  230. value<<= 32-n;
  231. ptr[0] |= be2me_32(value>>(index&31));
  232. ptr[1] = be2me_32(value<<(32-(index&31)));
  233. //if(n>24) printf("%d %d\n", n, value);
  234. index+= n;
  235. s->index= index;
  236. #endif
  237. #else //ALIGNED_BITSTREAM_WRITER
  238. #ifdef ARCH_X86
  239. asm volatile(
  240. "movl $7, %%ecx \n\t"
  241. "andl %0, %%ecx \n\t"
  242. "addl %3, %%ecx \n\t"
  243. "negl %%ecx \n\t"
  244. "shll %%cl, %1 \n\t"
  245. "bswapl %1 \n\t"
  246. "movl %0, %%ecx \n\t"
  247. "shrl $3, %%ecx \n\t"
  248. "orl %1, (%%ecx, %2) \n\t"
  249. "addl %3, %0 \n\t"
  250. "movl $0, 4(%%ecx, %2) \n\t"
  251. : "=&r" (s->index), "=&r" (value)
  252. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
  253. : "%ecx"
  254. );
  255. #else
  256. int index= s->index;
  257. uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  258. ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
  259. ptr[1] = 0;
  260. //if(n>24) printf("%d %d\n", n, value);
  261. index+= n;
  262. s->index= index;
  263. #endif
  264. #endif //!ALIGNED_BITSTREAM_WRITER
  265. }
  266. #endif
  267. #ifndef ALT_BITSTREAM_WRITER
  268. /* for jpeg : escape 0xff with 0x00 after it */
  269. static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
  270. {
  271. unsigned int bit_buf, b;
  272. int bit_left, i;
  273. assert(n == 32 || value < (1U << n));
  274. bit_buf = s->bit_buf;
  275. bit_left = s->bit_left;
  276. //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
  277. /* XXX: optimize */
  278. if (n < bit_left) {
  279. bit_buf = (bit_buf<<n) | value;
  280. bit_left-=n;
  281. } else {
  282. bit_buf<<=bit_left;
  283. bit_buf |= value >> (n - bit_left);
  284. /* handle escape */
  285. for(i=0;i<4;i++) {
  286. b = (bit_buf >> 24);
  287. *(s->buf_ptr++) = b;
  288. if (b == 0xff)
  289. *(s->buf_ptr++) = 0;
  290. bit_buf <<= 8;
  291. }
  292. bit_left+= 32 - n;
  293. bit_buf = value;
  294. }
  295. s->bit_buf = bit_buf;
  296. s->bit_left = bit_left;
  297. }
  298. #endif
  299. #ifdef ALT_BITSTREAM_WRITER
  300. static inline void jput_bits(PutBitContext *s, int n, int value)
  301. {
  302. int index= s->index;
  303. uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  304. int v= ptr[0];
  305. //if(n>24) printf("%d %d\n", n, value);
  306. v |= be2me_32(value<<(32-n-(index&7) ));
  307. if(((v+0x01010101)^0xFFFFFFFF)&v&0x80808080)
  308. {
  309. /* handle idiotic (m)jpeg escapes */
  310. uint8_t *bPtr= (uint8_t*)ptr;
  311. int numChecked= ((index+n)>>3) - (index>>3);
  312. v= be2me_32(v);
  313. *(bPtr++)= v>>24;
  314. if((v&0xFF000000)==0xFF000000 && numChecked>0){
  315. *(bPtr++)= 0x00;
  316. index+=8;
  317. }
  318. *(bPtr++)= (v>>16)&0xFF;
  319. if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
  320. *(bPtr++)= 0x00;
  321. index+=8;
  322. }
  323. *(bPtr++)= (v>>8)&0xFF;
  324. if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
  325. *(bPtr++)= 0x00;
  326. index+=8;
  327. }
  328. *(bPtr++)= v&0xFF;
  329. if((v&0x000000FF)==0x000000FF && numChecked>3){
  330. *(bPtr++)= 0x00;
  331. index+=8;
  332. }
  333. *((uint32_t*)bPtr)= 0;
  334. }
  335. else
  336. {
  337. ptr[0] = v;
  338. ptr[1] = 0;
  339. }
  340. index+= n;
  341. s->index= index;
  342. }
  343. #endif
  344. static inline uint8_t* pbBufPtr(PutBitContext *s)
  345. {
  346. #ifdef ALT_BITSTREAM_WRITER
  347. return s->buf + (s->index>>3);
  348. #else
  349. return s->buf_ptr;
  350. #endif
  351. }
  352. void init_get_bits(GetBitContext *s,
  353. UINT8 *buffer, int buffer_size);
  354. #ifndef ALT_BITSTREAM_READER
  355. unsigned int get_bits_long(GetBitContext *s, int n);
  356. unsigned int show_bits_long(GetBitContext *s, int n);
  357. #endif
  358. static inline unsigned int get_bits(GetBitContext *s, int n){
  359. #ifdef ALT_BITSTREAM_READER
  360. #ifdef ALIGNED_BITSTREAM
  361. int index= s->index;
  362. uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  363. uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  364. #ifdef ARCH_X86
  365. asm ("shldl %%cl, %2, %0\n\t"
  366. : "=r" (result1)
  367. : "0" (result1), "r" (result2), "c" (index));
  368. #else
  369. result1<<= (index&0x1F);
  370. result2= (result2>>1) >> (31-(index&0x1F));
  371. result1|= result2;
  372. #endif
  373. result1>>= 32 - n;
  374. index+= n;
  375. s->index= index;
  376. return result1;
  377. #else //ALIGNED_BITSTREAM
  378. int index= s->index;
  379. uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
  380. result<<= (index&0x07);
  381. result>>= 32 - n;
  382. index+= n;
  383. s->index= index;
  384. #ifdef DUMP_STREAM
  385. while(n){
  386. printf("%d", (result>>(n-1))&1);
  387. n--;
  388. }
  389. printf(" ");
  390. #endif
  391. return result;
  392. #endif //!ALIGNED_BITSTREAM
  393. #else //ALT_BITSTREAM_READER
  394. if(s->bit_cnt>=n){
  395. /* most common case here */
  396. unsigned int val = s->bit_buf >> (32 - n);
  397. s->bit_buf <<= n;
  398. s->bit_cnt -= n;
  399. #ifdef STATS
  400. st_bit_counts[st_current_index] += n;
  401. #endif
  402. return val;
  403. }
  404. return get_bits_long(s,n);
  405. #endif //!ALT_BITSTREAM_READER
  406. }
  407. static inline unsigned int get_bits1(GetBitContext *s){
  408. #ifdef ALT_BITSTREAM_READER
  409. int index= s->index;
  410. uint8_t result= s->buffer[ index>>3 ];
  411. result<<= (index&0x07);
  412. result>>= 8 - 1;
  413. index++;
  414. s->index= index;
  415. #ifdef DUMP_STREAM
  416. printf("%d ", result);
  417. #endif
  418. return result;
  419. #else
  420. if(s->bit_cnt>0){
  421. /* most common case here */
  422. unsigned int val = s->bit_buf >> 31;
  423. s->bit_buf <<= 1;
  424. s->bit_cnt--;
  425. #ifdef STATS
  426. st_bit_counts[st_current_index]++;
  427. #endif
  428. return val;
  429. }
  430. return get_bits_long(s,1);
  431. #endif
  432. }
  433. /* This function is identical to get_bits(), the only */
  434. /* diference is that it doesn't touch the buffer */
  435. /* it is usefull to see the buffer. */
  436. static inline unsigned int show_bits(GetBitContext *s, int n)
  437. {
  438. #ifdef ALT_BITSTREAM_READER
  439. #ifdef ALIGNED_BITSTREAM
  440. int index= s->index;
  441. uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  442. uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  443. #ifdef ARCH_X86
  444. asm ("shldl %%cl, %2, %0\n\t"
  445. : "=r" (result1)
  446. : "0" (result1), "r" (result2), "c" (index));
  447. #else
  448. result1<<= (index&0x1F);
  449. result2= (result2>>1) >> (31-(index&0x1F));
  450. result1|= result2;
  451. #endif
  452. result1>>= 32 - n;
  453. return result1;
  454. #else //ALIGNED_BITSTREAM
  455. int index= s->index;
  456. uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
  457. result<<= (index&0x07);
  458. result>>= 32 - n;
  459. return result;
  460. #endif //!ALIGNED_BITSTREAM
  461. #else //ALT_BITSTREAM_READER
  462. if(s->bit_cnt>=n) {
  463. /* most common case here */
  464. unsigned int val = s->bit_buf >> (32 - n);
  465. return val;
  466. }
  467. return show_bits_long(s,n);
  468. #endif //!ALT_BITSTREAM_READER
  469. }
  470. static inline int show_aligned_bits(GetBitContext *s, int offset, int n)
  471. {
  472. #ifdef ALT_BITSTREAM_READER
  473. #ifdef ALIGNED_BITSTREAM
  474. int index= (s->index + offset + 7)&(~7);
  475. uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  476. uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  477. #ifdef ARCH_X86
  478. asm ("shldl %%cl, %2, %0\n\t"
  479. : "=r" (result1)
  480. : "0" (result1), "r" (result2), "c" (index));
  481. #else
  482. result1<<= (index&0x1F);
  483. result2= (result2>>1) >> (31-(index&0x1F));
  484. result1|= result2;
  485. #endif
  486. result1>>= 32 - n;
  487. return result1;
  488. #else //ALIGNED_BITSTREAM
  489. int index= (s->index + offset + 7)>>3;
  490. uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+index ) );
  491. result>>= 32 - n;
  492. return result;
  493. #endif //!ALIGNED_BITSTREAM
  494. #else //ALT_BITSTREAM_READER
  495. int index= (get_bits_count(s) + offset + 7)>>3;
  496. uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buf)+index ) );
  497. result>>= 32 - n;
  498. //printf(" %X %X %d \n", (int)(((uint8_t *)s->buf)+index ), (int)s->buf_ptr, s->bit_cnt);
  499. return result;
  500. #endif //!ALT_BITSTREAM_READER
  501. }
  502. static inline void skip_bits(GetBitContext *s, int n){
  503. #ifdef ALT_BITSTREAM_READER
  504. s->index+= n;
  505. #ifdef DUMP_STREAM
  506. {
  507. int result;
  508. s->index-= n;
  509. result= get_bits(s, n);
  510. }
  511. #endif
  512. #else
  513. if(s->bit_cnt>=n){
  514. /* most common case here */
  515. s->bit_buf <<= n;
  516. s->bit_cnt -= n;
  517. #ifdef STATS
  518. st_bit_counts[st_current_index] += n;
  519. #endif
  520. } else {
  521. get_bits_long(s,n);
  522. }
  523. #endif
  524. }
  525. static inline void skip_bits1(GetBitContext *s){
  526. #ifdef ALT_BITSTREAM_READER
  527. s->index++;
  528. #ifdef DUMP_STREAM
  529. s->index--;
  530. printf("%d ", get_bits1(s));
  531. #endif
  532. #else
  533. if(s->bit_cnt>0){
  534. /* most common case here */
  535. s->bit_buf <<= 1;
  536. s->bit_cnt--;
  537. #ifdef STATS
  538. st_bit_counts[st_current_index]++;
  539. #endif
  540. } else {
  541. get_bits_long(s,1);
  542. }
  543. #endif
  544. }
  545. static inline int get_bits_count(GetBitContext *s)
  546. {
  547. #ifdef ALT_BITSTREAM_READER
  548. return s->index;
  549. #else
  550. return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
  551. #endif
  552. }
  553. int check_marker(GetBitContext *s, char *msg);
  554. void align_get_bits(GetBitContext *s);
  555. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  556. const void *bits, int bits_wrap, int bits_size,
  557. const void *codes, int codes_wrap, int codes_size);
  558. void free_vlc(VLC *vlc);
  559. #ifdef ALT_BITSTREAM_READER
  560. #ifdef ALIGNED_BITSTREAM
  561. #ifdef ARCH_X86
  562. #define SHOW_BITS(s, val, n) \
  563. val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
  564. {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
  565. asm ("shldl %%cl, %2, %0\n\t"\
  566. : "=r" (val)\
  567. : "0" (val), "r" (result2), "c" (bit_cnt));\
  568. ((uint32_t)val)>>= 32 - n;}
  569. #else //ARCH_X86
  570. #define SHOW_BITS(s, val, n) \
  571. val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
  572. {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
  573. val<<= (bit_cnt&0x1F);\
  574. result2= (result2>>1) >> (31-(bit_cnt&0x1F));\
  575. val|= result2;\
  576. ((uint32_t)val)>>= 32 - n;}
  577. #endif //!ARCH_X86
  578. #else //ALIGNED_BITSTREAM
  579. #define SHOW_BITS(s, val, n) \
  580. val= be2me_32( unaligned32( ((uint8_t *)(s)->buffer)+(bit_cnt>>3) ) );\
  581. val<<= (bit_cnt&0x07);\
  582. ((uint32_t)val)>>= 32 - n;
  583. #endif // !ALIGNED_BITSTREAM
  584. #define FLUSH_BITS(n) bit_cnt+=n;
  585. #define SAVE_BITS(s) bit_cnt= (s)->index;
  586. #define RESTORE_BITS(s) (s)->index= bit_cnt;
  587. #else
  588. /* macro to go faster */
  589. /* n must be <= 24 */
  590. /* XXX: optimize buffer end test */
  591. #define SHOW_BITS(s, val, n)\
  592. {\
  593. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  594. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  595. bit_cnt += 8;\
  596. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  597. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  598. bit_cnt += 8;\
  599. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  600. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  601. bit_cnt += 8;\
  602. }\
  603. }\
  604. }\
  605. val = bit_buf >> (32 - n);\
  606. }
  607. /* SHOW_BITS with n1 >= n must be been done before */
  608. #define FLUSH_BITS(n)\
  609. {\
  610. bit_buf <<= n;\
  611. bit_cnt -= n;\
  612. }
  613. #define SAVE_BITS(s) \
  614. {\
  615. bit_cnt = (s)->bit_cnt;\
  616. bit_buf = (s)->bit_buf;\
  617. buf_ptr = (s)->buf_ptr;\
  618. }
  619. #define RESTORE_BITS(s) \
  620. {\
  621. (s)->buf_ptr = buf_ptr;\
  622. (s)->bit_buf = bit_buf;\
  623. (s)->bit_cnt = bit_cnt;\
  624. }
  625. #endif // !ALT_BITSTREAM_READER
  626. static inline int get_vlc(GetBitContext *s, VLC *vlc)
  627. {
  628. int code, n, nb_bits, index;
  629. INT16 *table_codes;
  630. INT8 *table_bits;
  631. int bit_cnt;
  632. #ifndef ALT_BITSTREAM_READER
  633. UINT32 bit_buf;
  634. UINT8 *buf_ptr;
  635. #endif
  636. SAVE_BITS(s);
  637. nb_bits = vlc->bits;
  638. table_codes = vlc->table_codes;
  639. table_bits = vlc->table_bits;
  640. #ifdef FAST_GET_FIRST_VLC
  641. SHOW_BITS(s, index, nb_bits);
  642. code = table_codes[index];
  643. n = table_bits[index];
  644. if (n > 0) {
  645. /* most common case (90%)*/
  646. FLUSH_BITS(n);
  647. #ifdef DUMP_STREAM
  648. {
  649. int n= bit_cnt - s->index;
  650. skip_bits(s, n);
  651. RESTORE_BITS(s);
  652. }
  653. #endif
  654. RESTORE_BITS(s);
  655. return code;
  656. } else if (n == 0) {
  657. return -1;
  658. } else {
  659. FLUSH_BITS(nb_bits);
  660. nb_bits = -n;
  661. table_codes = vlc->table_codes + code;
  662. table_bits = vlc->table_bits + code;
  663. }
  664. #endif
  665. for(;;) {
  666. SHOW_BITS(s, index, nb_bits);
  667. code = table_codes[index];
  668. n = table_bits[index];
  669. if (n > 0) {
  670. /* most common case */
  671. FLUSH_BITS(n);
  672. #ifdef STATS
  673. st_bit_counts[st_current_index] += n;
  674. #endif
  675. break;
  676. } else if (n == 0) {
  677. return -1;
  678. } else {
  679. FLUSH_BITS(nb_bits);
  680. #ifdef STATS
  681. st_bit_counts[st_current_index] += nb_bits;
  682. #endif
  683. nb_bits = -n;
  684. table_codes = vlc->table_codes + code;
  685. table_bits = vlc->table_bits + code;
  686. }
  687. }
  688. #ifdef DUMP_STREAM
  689. {
  690. int n= bit_cnt - s->index;
  691. skip_bits(s, n);
  692. RESTORE_BITS(s);
  693. }
  694. #endif
  695. RESTORE_BITS(s);
  696. return code;
  697. }
  698. /* define it to include statistics code (useful only for optimizing
  699. codec efficiency */
  700. //#define STATS
  701. #ifdef STATS
  702. enum {
  703. ST_UNKNOWN,
  704. ST_DC,
  705. ST_INTRA_AC,
  706. ST_INTER_AC,
  707. ST_INTRA_MB,
  708. ST_INTER_MB,
  709. ST_MV,
  710. ST_NB,
  711. };
  712. extern int st_current_index;
  713. extern unsigned int st_bit_counts[ST_NB];
  714. extern unsigned int st_out_bit_counts[ST_NB];
  715. void print_stats(void);
  716. #endif
  717. /* misc math functions */
  718. static inline int av_log2(unsigned int v)
  719. {
  720. int n;
  721. n = 0;
  722. if (v & 0xffff0000) {
  723. v >>= 16;
  724. n += 16;
  725. }
  726. if (v & 0xff00) {
  727. v >>= 8;
  728. n += 8;
  729. }
  730. if (v & 0xf0) {
  731. v >>= 4;
  732. n += 4;
  733. }
  734. if (v & 0xc) {
  735. v >>= 2;
  736. n += 2;
  737. }
  738. if (v & 0x2) {
  739. n++;
  740. }
  741. return n;
  742. }
  743. /* median of 3 */
  744. static inline int mid_pred(int a, int b, int c)
  745. {
  746. int vmin, vmax;
  747. vmax = vmin = a;
  748. if (b < vmin)
  749. vmin = b;
  750. else
  751. vmax = b;
  752. if (c < vmin)
  753. vmin = c;
  754. else if (c > vmax)
  755. vmax = c;
  756. return a + b + c - vmin - vmax;
  757. }
  758. static inline int clip(int a, int amin, int amax)
  759. {
  760. if (a < amin)
  761. return amin;
  762. else if (a > amax)
  763. return amax;
  764. else
  765. return a;
  766. }
  767. /* memory */
  768. void *av_malloc(int size);
  769. void *av_mallocz(int size);
  770. void av_free(void *ptr);
  771. void __av_freep(void **ptr);
  772. #define av_freep(p) __av_freep((void **)(p))
  773. /* math */
  774. int ff_gcd(int a, int b);
  775. #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
  776. #endif