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.

890 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. #ifndef MAX
  124. # define MAX(a,b) ((a) > (b) ? (a) : (b))
  125. #endif
  126. #ifndef MIN
  127. # define MIN(a,b) ((a) > (b) ? (b) : (a))
  128. #endif
  129. #ifdef ARCH_X86
  130. // avoid +32 for shift optimization (gcc should do that ...)
  131. static inline int32_t NEG_SSR32( int32_t a, int8_t s){
  132. asm ("sarl %1, %0\n\t"
  133. : "+r" (a)
  134. : "ic" ((uint8_t)(-s))
  135. );
  136. return a;
  137. }
  138. static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
  139. asm ("shrl %1, %0\n\t"
  140. : "+r" (a)
  141. : "ic" ((uint8_t)(-s))
  142. );
  143. return a;
  144. }
  145. #else
  146. # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
  147. # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
  148. #endif
  149. /* bit output */
  150. struct PutBitContext;
  151. typedef void (*WriteDataFunc)(void *, UINT8 *, int);
  152. typedef struct PutBitContext {
  153. #ifdef ALT_BITSTREAM_WRITER
  154. UINT8 *buf, *buf_end;
  155. int index;
  156. #else
  157. UINT32 bit_buf;
  158. int bit_left;
  159. UINT8 *buf, *buf_ptr, *buf_end;
  160. #endif
  161. INT64 data_out_size; /* in bytes */
  162. } PutBitContext;
  163. void init_put_bits(PutBitContext *s,
  164. UINT8 *buffer, int buffer_size,
  165. void *opaque,
  166. void (*write_data)(void *, UINT8 *, int));
  167. INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
  168. void align_put_bits(PutBitContext *s);
  169. void flush_put_bits(PutBitContext *s);
  170. void put_string(PutBitContext * pbc, char *s);
  171. /* bit input */
  172. typedef struct GetBitContext {
  173. UINT8 *buffer, *buffer_end;
  174. #ifdef ALT_BITSTREAM_READER
  175. int index;
  176. #elif defined LIBMPEG2_BITSTREAM_READER
  177. UINT8 *buffer_ptr;
  178. UINT32 cache;
  179. int bit_count;
  180. #elif defined A32_BITSTREAM_READER
  181. UINT32 *buffer_ptr;
  182. UINT32 cache0;
  183. UINT32 cache1;
  184. int bit_count;
  185. #endif
  186. int size;
  187. } GetBitContext;
  188. static inline int get_bits_count(GetBitContext *s);
  189. #define VLC_TYPE INT16
  190. typedef struct VLC {
  191. int bits;
  192. VLC_TYPE (*table)[2]; // code, bits
  193. int table_size, table_allocated;
  194. } VLC;
  195. typedef struct RL_VLC_ELEM {
  196. int16_t level;
  197. int8_t len;
  198. uint8_t run;
  199. } RL_VLC_ELEM;
  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. *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
  242. //printf("bitbuf = %08x\n", bit_buf);
  243. s->buf_ptr+=4;
  244. bit_left+=32 - n;
  245. bit_buf = value;
  246. }
  247. s->bit_buf = bit_buf;
  248. s->bit_left = bit_left;
  249. }
  250. #endif
  251. #ifdef ALT_BITSTREAM_WRITER
  252. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  253. {
  254. # ifdef ALIGNED_BITSTREAM_WRITER
  255. # ifdef ARCH_X86
  256. asm volatile(
  257. "movl %0, %%ecx \n\t"
  258. "xorl %%eax, %%eax \n\t"
  259. "shrdl %%cl, %1, %%eax \n\t"
  260. "shrl %%cl, %1 \n\t"
  261. "movl %0, %%ecx \n\t"
  262. "shrl $3, %%ecx \n\t"
  263. "andl $0xFFFFFFFC, %%ecx \n\t"
  264. "bswapl %1 \n\t"
  265. "orl %1, (%2, %%ecx) \n\t"
  266. "bswapl %%eax \n\t"
  267. "addl %3, %0 \n\t"
  268. "movl %%eax, 4(%2, %%ecx) \n\t"
  269. : "=&r" (s->index), "=&r" (value)
  270. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
  271. : "%eax", "%ecx"
  272. );
  273. # else
  274. int index= s->index;
  275. uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
  276. value<<= 32-n;
  277. ptr[0] |= be2me_32(value>>(index&31));
  278. ptr[1] = be2me_32(value<<(32-(index&31)));
  279. //if(n>24) printf("%d %d\n", n, value);
  280. index+= n;
  281. s->index= index;
  282. # endif
  283. # else //ALIGNED_BITSTREAM_WRITER
  284. # ifdef ARCH_X86
  285. asm volatile(
  286. "movl $7, %%ecx \n\t"
  287. "andl %0, %%ecx \n\t"
  288. "addl %3, %%ecx \n\t"
  289. "negl %%ecx \n\t"
  290. "shll %%cl, %1 \n\t"
  291. "bswapl %1 \n\t"
  292. "movl %0, %%ecx \n\t"
  293. "shrl $3, %%ecx \n\t"
  294. "orl %1, (%%ecx, %2) \n\t"
  295. "addl %3, %0 \n\t"
  296. "movl $0, 4(%%ecx, %2) \n\t"
  297. : "=&r" (s->index), "=&r" (value)
  298. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
  299. : "%ecx"
  300. );
  301. # else
  302. int index= s->index;
  303. uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  304. ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
  305. ptr[1] = 0;
  306. //if(n>24) printf("%d %d\n", n, value);
  307. index+= n;
  308. s->index= index;
  309. # endif
  310. # endif //!ALIGNED_BITSTREAM_WRITER
  311. }
  312. #endif
  313. static inline uint8_t* pbBufPtr(PutBitContext *s)
  314. {
  315. #ifdef ALT_BITSTREAM_WRITER
  316. return s->buf + (s->index>>3);
  317. #else
  318. return s->buf_ptr;
  319. #endif
  320. }
  321. /* Bitstream reader API docs:
  322. name
  323. abritary name which is used as prefix for the internal variables
  324. gb
  325. getbitcontext
  326. OPEN_READER(name, gb)
  327. loads gb into local variables
  328. CLOSE_READER(name, gb)
  329. stores local vars in gb
  330. UPDATE_CACHE(name, gb)
  331. refills the internal cache from the bitstream
  332. after this call at least MIN_CACHE_BITS will be available,
  333. GET_CACHE(name, gb)
  334. will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
  335. SHOW_UBITS(name, gb, num)
  336. will return the nest num bits
  337. SHOW_SBITS(name, gb, num)
  338. will return the nest num bits and do sign extension
  339. SKIP_BITS(name, gb, num)
  340. will skip over the next num bits
  341. note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
  342. SKIP_CACHE(name, gb, num)
  343. will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
  344. SKIP_COUNTER(name, gb, num)
  345. will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
  346. LAST_SKIP_CACHE(name, gb, num)
  347. will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
  348. LAST_SKIP_BITS(name, gb, num)
  349. is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
  350. for examples see get_bits, show_bits, skip_bits, get_vlc
  351. */
  352. #ifdef ALT_BITSTREAM_READER
  353. # define MIN_CACHE_BITS 25
  354. # define OPEN_READER(name, gb)\
  355. int name##_index= (gb)->index;\
  356. int name##_cache= 0;\
  357. # define CLOSE_READER(name, gb)\
  358. (gb)->index= name##_index;\
  359. # define UPDATE_CACHE(name, gb)\
  360. name##_cache= be2me_32( unaligned32( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) ) << (name##_index&0x07);\
  361. # define SKIP_CACHE(name, gb, num)\
  362. name##_cache <<= (num);\
  363. // FIXME name?
  364. # define SKIP_COUNTER(name, gb, num)\
  365. name##_index += (num);\
  366. # define SKIP_BITS(name, gb, num)\
  367. {\
  368. SKIP_CACHE(name, gb, num)\
  369. SKIP_COUNTER(name, gb, num)\
  370. }\
  371. # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  372. # define LAST_SKIP_CACHE(name, gb, num) ;
  373. # define SHOW_UBITS(name, gb, num)\
  374. NEG_USR32(name##_cache, num)
  375. # define SHOW_SBITS(name, gb, num)\
  376. NEG_SSR32(name##_cache, num)
  377. # define GET_CACHE(name, gb)\
  378. ((uint32_t)name##_cache)
  379. static inline int get_bits_count(GetBitContext *s){
  380. return s->index;
  381. }
  382. #elif defined LIBMPEG2_BITSTREAM_READER
  383. //libmpeg2 like reader
  384. # define MIN_CACHE_BITS 16
  385. # define OPEN_READER(name, gb)\
  386. int name##_bit_count=(gb)->bit_count;\
  387. int name##_cache= (gb)->cache;\
  388. uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  389. # define CLOSE_READER(name, gb)\
  390. (gb)->bit_count= name##_bit_count;\
  391. (gb)->cache= name##_cache;\
  392. (gb)->buffer_ptr= name##_buffer_ptr;\
  393. # define UPDATE_CACHE(name, gb)\
  394. if(name##_bit_count > 0){\
  395. name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
  396. name##_buffer_ptr+=2;\
  397. name##_bit_count-= 16;\
  398. }\
  399. # define SKIP_CACHE(name, gb, num)\
  400. name##_cache <<= (num);\
  401. # define SKIP_COUNTER(name, gb, num)\
  402. name##_bit_count += (num);\
  403. # define SKIP_BITS(name, gb, num)\
  404. {\
  405. SKIP_CACHE(name, gb, num)\
  406. SKIP_COUNTER(name, gb, num)\
  407. }\
  408. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  409. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  410. # define SHOW_UBITS(name, gb, num)\
  411. NEG_USR32(name##_cache, num)
  412. # define SHOW_SBITS(name, gb, num)\
  413. NEG_SSR32(name##_cache, num)
  414. # define GET_CACHE(name, gb)\
  415. ((uint32_t)name##_cache)
  416. static inline int get_bits_count(GetBitContext *s){
  417. return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
  418. }
  419. #elif defined A32_BITSTREAM_READER
  420. # define MIN_CACHE_BITS 32
  421. # define OPEN_READER(name, gb)\
  422. int name##_bit_count=(gb)->bit_count;\
  423. uint32_t name##_cache0= (gb)->cache0;\
  424. uint32_t name##_cache1= (gb)->cache1;\
  425. uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  426. # define CLOSE_READER(name, gb)\
  427. (gb)->bit_count= name##_bit_count;\
  428. (gb)->cache0= name##_cache0;\
  429. (gb)->cache1= name##_cache1;\
  430. (gb)->buffer_ptr= name##_buffer_ptr;\
  431. # define UPDATE_CACHE(name, gb)\
  432. if(name##_bit_count > 0){\
  433. const uint32_t next= be2me_32( *name##_buffer_ptr );\
  434. name##_cache0 |= NEG_USR32(next,name##_bit_count);\
  435. name##_cache1 |= next<<name##_bit_count;\
  436. name##_buffer_ptr++;\
  437. name##_bit_count-= 32;\
  438. }\
  439. #ifdef ARCH_X86
  440. # define SKIP_CACHE(name, gb, num)\
  441. asm(\
  442. "shldl %2, %1, %0 \n\t"\
  443. "shll %2, %1 \n\t"\
  444. : "+r" (name##_cache0), "+r" (name##_cache1)\
  445. : "Ic" ((uint8_t)num)\
  446. );
  447. #else
  448. # define SKIP_CACHE(name, gb, num)\
  449. name##_cache0 <<= (num);\
  450. name##_cache0 |= NEG_USR32(name##_cache1,num);\
  451. name##_cache1 <<= (num);
  452. #endif
  453. # define SKIP_COUNTER(name, gb, num)\
  454. name##_bit_count += (num);\
  455. # define SKIP_BITS(name, gb, num)\
  456. {\
  457. SKIP_CACHE(name, gb, num)\
  458. SKIP_COUNTER(name, gb, num)\
  459. }\
  460. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  461. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  462. # define SHOW_UBITS(name, gb, num)\
  463. NEG_USR32(name##_cache0, num)
  464. # define SHOW_SBITS(name, gb, num)\
  465. NEG_SSR32(name##_cache0, num)
  466. # define GET_CACHE(name, gb)\
  467. (name##_cache0)
  468. static inline int get_bits_count(GetBitContext *s){
  469. return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
  470. }
  471. #endif
  472. static inline unsigned int get_bits(GetBitContext *s, int n){
  473. register int tmp;
  474. OPEN_READER(re, s)
  475. UPDATE_CACHE(re, s)
  476. tmp= SHOW_UBITS(re, s, n);
  477. LAST_SKIP_BITS(re, s, n)
  478. CLOSE_READER(re, s)
  479. return tmp;
  480. }
  481. static inline unsigned int show_bits(GetBitContext *s, int n){
  482. register int tmp;
  483. OPEN_READER(re, s)
  484. UPDATE_CACHE(re, s)
  485. tmp= SHOW_UBITS(re, s, n);
  486. // CLOSE_READER(re, s)
  487. return tmp;
  488. }
  489. static inline void skip_bits(GetBitContext *s, int n){
  490. //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
  491. OPEN_READER(re, s)
  492. UPDATE_CACHE(re, s)
  493. LAST_SKIP_BITS(re, s, n)
  494. CLOSE_READER(re, s)
  495. }
  496. static inline unsigned int get_bits1(GetBitContext *s){
  497. #ifdef ALT_BITSTREAM_READER
  498. int index= s->index;
  499. uint8_t result= s->buffer[ index>>3 ];
  500. result<<= (index&0x07);
  501. result>>= 8 - 1;
  502. index++;
  503. s->index= index;
  504. return result;
  505. #else
  506. return get_bits(s, 1);
  507. #endif
  508. }
  509. static inline unsigned int show_bits1(GetBitContext *s){
  510. return show_bits(s, 1);
  511. }
  512. static inline void skip_bits1(GetBitContext *s){
  513. skip_bits(s, 1);
  514. }
  515. void init_get_bits(GetBitContext *s,
  516. UINT8 *buffer, int buffer_size);
  517. int check_marker(GetBitContext *s, char *msg);
  518. void align_get_bits(GetBitContext *s);
  519. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  520. const void *bits, int bits_wrap, int bits_size,
  521. const void *codes, int codes_wrap, int codes_size);
  522. void free_vlc(VLC *vlc);
  523. #define GET_VLC(code, name, gb, table, bits, max_depth)\
  524. {\
  525. int n, index, nb_bits;\
  526. \
  527. index= SHOW_UBITS(name, gb, bits);\
  528. code = table[index][0];\
  529. n = table[index][1];\
  530. \
  531. if(max_depth > 1 && n < 0){\
  532. LAST_SKIP_BITS(name, gb, bits)\
  533. UPDATE_CACHE(name, gb)\
  534. \
  535. nb_bits = -n;\
  536. \
  537. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  538. code = table[index][0];\
  539. n = table[index][1];\
  540. if(max_depth > 2 && n < 0){\
  541. LAST_SKIP_BITS(name, gb, nb_bits)\
  542. UPDATE_CACHE(name, gb)\
  543. \
  544. nb_bits = -n;\
  545. \
  546. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  547. code = table[index][0];\
  548. n = table[index][1];\
  549. }\
  550. }\
  551. SKIP_BITS(name, gb, n)\
  552. }
  553. #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
  554. {\
  555. int n, index, nb_bits;\
  556. \
  557. index= SHOW_UBITS(name, gb, bits);\
  558. level = table[index].level;\
  559. n = table[index].len;\
  560. \
  561. if(max_depth > 1 && n < 0){\
  562. LAST_SKIP_BITS(name, gb, bits)\
  563. UPDATE_CACHE(name, gb)\
  564. \
  565. nb_bits = -n;\
  566. \
  567. index= SHOW_UBITS(name, gb, nb_bits) + level;\
  568. level = table[index].level;\
  569. n = table[index].len;\
  570. }\
  571. run= table[index].run;\
  572. SKIP_BITS(name, gb, n)\
  573. }
  574. // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
  575. static inline int get_vlc(GetBitContext *s, VLC *vlc)
  576. {
  577. int code;
  578. VLC_TYPE (*table)[2]= vlc->table;
  579. OPEN_READER(re, s)
  580. UPDATE_CACHE(re, s)
  581. GET_VLC(code, re, s, table, vlc->bits, 3)
  582. CLOSE_READER(re, s)
  583. return code;
  584. }
  585. static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  586. int bits, int max_depth)
  587. {
  588. int code;
  589. OPEN_READER(re, s)
  590. UPDATE_CACHE(re, s)
  591. GET_VLC(code, re, s, table, bits, max_depth)
  592. CLOSE_READER(re, s)
  593. return code;
  594. }
  595. /* define it to include statistics code (useful only for optimizing
  596. codec efficiency */
  597. //#define STATS
  598. #ifdef STATS
  599. enum {
  600. ST_UNKNOWN,
  601. ST_DC,
  602. ST_INTRA_AC,
  603. ST_INTER_AC,
  604. ST_INTRA_MB,
  605. ST_INTER_MB,
  606. ST_MV,
  607. ST_NB,
  608. };
  609. extern int st_current_index;
  610. extern unsigned int st_bit_counts[ST_NB];
  611. extern unsigned int st_out_bit_counts[ST_NB];
  612. void print_stats(void);
  613. #endif
  614. /* misc math functions */
  615. static inline int av_log2(unsigned int v)
  616. {
  617. int n;
  618. n = 0;
  619. if (v & 0xffff0000) {
  620. v >>= 16;
  621. n += 16;
  622. }
  623. if (v & 0xff00) {
  624. v >>= 8;
  625. n += 8;
  626. }
  627. if (v & 0xf0) {
  628. v >>= 4;
  629. n += 4;
  630. }
  631. if (v & 0xc) {
  632. v >>= 2;
  633. n += 2;
  634. }
  635. if (v & 0x2) {
  636. n++;
  637. }
  638. return n;
  639. }
  640. /* median of 3 */
  641. static inline int mid_pred(int a, int b, int c)
  642. {
  643. int vmin, vmax;
  644. vmax = vmin = a;
  645. if (b < vmin)
  646. vmin = b;
  647. else
  648. vmax = b;
  649. if (c < vmin)
  650. vmin = c;
  651. else if (c > vmax)
  652. vmax = c;
  653. return a + b + c - vmin - vmax;
  654. }
  655. static inline int clip(int a, int amin, int amax)
  656. {
  657. if (a < amin)
  658. return amin;
  659. else if (a > amax)
  660. return amax;
  661. else
  662. return a;
  663. }
  664. /* math */
  665. extern const UINT8 ff_sqrt_tab[128];
  666. int ff_gcd(int a, int b);
  667. static inline int ff_sqrt(int a)
  668. {
  669. int ret=0;
  670. int s;
  671. int ret_sq=0;
  672. if(a<128) return ff_sqrt_tab[a];
  673. for(s=15; s>=0; s--){
  674. int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
  675. if(b<=a){
  676. ret_sq=b;
  677. ret+= 1<<s;
  678. }
  679. }
  680. return ret;
  681. }
  682. /**
  683. * converts fourcc string to int
  684. */
  685. static inline int ff_get_fourcc(char *s){
  686. assert( strlen(s)==4 );
  687. return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
  688. }
  689. #ifdef ARCH_X86
  690. #define MASK_ABS(mask, level)\
  691. asm volatile(\
  692. "cdq \n\t"\
  693. "xorl %1, %0 \n\t"\
  694. "subl %1, %0 \n\t"\
  695. : "+a" (level), "=&d" (mask)\
  696. );
  697. #else
  698. #define MASK_ABS(mask, level)\
  699. mask= level>>31;\
  700. level= (level^mask)-mask;
  701. #endif
  702. #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
  703. #define COPY3_IF_LT(x,y,a,b,c,d)\
  704. asm volatile (\
  705. "cmpl %0, %3 \n\t"\
  706. "cmovl %3, %0 \n\t"\
  707. "cmovl %4, %1 \n\t"\
  708. "cmovl %5, %2 \n\t"\
  709. : "+r" (x), "+r" (a), "+r" (c)\
  710. : "r" (y), "r" (b), "r" (d)\
  711. );
  712. #else
  713. #define COPY3_IF_LT(x,y,a,b,c,d)\
  714. if((y)<(x)){\
  715. (x)=(y);\
  716. (a)=(b);\
  717. (c)=(d);\
  718. }
  719. #endif
  720. #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
  721. #endif /* HAVE_AV_CONFIG_H */
  722. #endif /* COMMON_H */