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.

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