You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

905 lines
20KB

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