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.

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