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.

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