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.

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