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.

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