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.

947 lines
22KB

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