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.

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