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.

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