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.

950 lines
22KB

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