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.

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