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.

940 lines
21KB

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