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.

1014 lines
24KB

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