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.

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