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.

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