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.

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