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.

1202 lines
29KB

  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. #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
  16. #ifndef M_PI
  17. #define M_PI 3.14159265358979323846
  18. #endif
  19. #ifdef HAVE_AV_CONFIG_H
  20. /* only include the following when compiling package */
  21. # include "config.h"
  22. # include <stdlib.h>
  23. # include <stdio.h>
  24. # include <string.h>
  25. # include <ctype.h>
  26. # ifndef __BEOS__
  27. # include <errno.h>
  28. # else
  29. # include "berrno.h"
  30. # endif
  31. # include <math.h>
  32. # ifndef ENODATA
  33. # define ENODATA 61
  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_DOUBLE(name, help, field, minv, maxv, defval) \
  42. { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
  43. #define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
  44. { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
  45. #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
  46. { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
  47. #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
  48. { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
  49. #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
  50. { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
  51. #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
  52. #define AVOPTION_END() AVOPTION_SUB(NULL)
  53. struct AVOption;
  54. #ifdef HAVE_MMX
  55. extern const struct AVOption avoptions_common[3 + 5];
  56. #else
  57. extern const struct AVOption avoptions_common[3];
  58. #endif
  59. extern const struct AVOption avoptions_workaround_bug[11];
  60. #endif /* HAVE_AV_CONFIG_H */
  61. /* Suppress restrict if it was not defined in config.h. */
  62. #ifndef restrict
  63. # define restrict
  64. #endif
  65. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  66. # define always_inline __attribute__((always_inline)) inline
  67. #else
  68. # define always_inline inline
  69. #endif
  70. #ifndef EMULATE_INTTYPES
  71. # include <inttypes.h>
  72. #else
  73. typedef signed char int8_t;
  74. typedef signed short int16_t;
  75. typedef signed int int32_t;
  76. typedef unsigned char uint8_t;
  77. typedef unsigned short uint16_t;
  78. typedef unsigned int uint32_t;
  79. # ifdef CONFIG_WIN32
  80. typedef signed __int64 int64_t;
  81. typedef unsigned __int64 uint64_t;
  82. # else /* other OS */
  83. typedef signed long long int64_t;
  84. typedef unsigned long long uint64_t;
  85. # endif /* other OS */
  86. #endif /* HAVE_INTTYPES_H */
  87. #ifndef INT64_MAX
  88. #define INT64_MAX 9223372036854775807LL
  89. #endif
  90. #ifdef EMULATE_FAST_INT
  91. /* note that we don't emulate 64bit ints */
  92. typedef signed char int_fast8_t;
  93. typedef signed int int_fast16_t;
  94. typedef signed int int_fast32_t;
  95. typedef unsigned char uint_fast8_t;
  96. typedef unsigned int uint_fast16_t;
  97. typedef unsigned int uint_fast32_t;
  98. #endif
  99. #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
  100. static inline float floorf(float f) {
  101. return floor(f);
  102. }
  103. #endif
  104. #ifdef CONFIG_WIN32
  105. /* windows */
  106. # ifndef __MINGW32__
  107. # define int64_t_C(c) (c ## i64)
  108. # define uint64_t_C(c) (c ## i64)
  109. # ifdef HAVE_AV_CONFIG_H
  110. # define inline __inline
  111. # endif
  112. # else
  113. # define int64_t_C(c) (c ## LL)
  114. # define uint64_t_C(c) (c ## ULL)
  115. # endif /* __MINGW32__ */
  116. # ifdef HAVE_AV_CONFIG_H
  117. # ifdef _DEBUG
  118. # define DEBUG
  119. # endif
  120. # define snprintf _snprintf
  121. # define vsnprintf _vsnprintf
  122. # endif
  123. /* CONFIG_WIN32 end */
  124. #elif defined (CONFIG_OS2)
  125. /* OS/2 EMX */
  126. #ifndef int64_t_C
  127. #define int64_t_C(c) (c ## LL)
  128. #define uint64_t_C(c) (c ## ULL)
  129. #endif
  130. #ifdef HAVE_AV_CONFIG_H
  131. #ifdef USE_FASTMEMCPY
  132. #include "fastmemcpy.h"
  133. #endif
  134. #include <float.h>
  135. #endif /* HAVE_AV_CONFIG_H */
  136. /* CONFIG_OS2 end */
  137. #else
  138. /* unix */
  139. #ifndef int64_t_C
  140. #define int64_t_C(c) (c ## LL)
  141. #define uint64_t_C(c) (c ## ULL)
  142. #endif
  143. #ifdef HAVE_AV_CONFIG_H
  144. # ifdef USE_FASTMEMCPY
  145. # include "fastmemcpy.h"
  146. # endif
  147. # endif /* HAVE_AV_CONFIG_H */
  148. #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
  149. #ifdef HAVE_AV_CONFIG_H
  150. # include "bswap.h"
  151. # if defined(__MINGW32__) || defined(__CYGWIN__) || \
  152. defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
  153. # define MANGLE(a) "_" #a
  154. # else
  155. # define MANGLE(a) #a
  156. # endif
  157. /* debug stuff */
  158. # ifndef DEBUG
  159. # define NDEBUG
  160. # endif
  161. # include <assert.h>
  162. /* dprintf macros */
  163. # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
  164. inline void dprintf(const char* fmt,...) {}
  165. # else
  166. # ifdef DEBUG
  167. # define dprintf(fmt,...) printf(fmt, __VA_ARGS__)
  168. # else
  169. # define dprintf(fmt,...)
  170. # endif
  171. # endif /* !CONFIG_WIN32 */
  172. # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  173. //rounded divison & shift
  174. #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
  175. /* assume b>0 */
  176. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  177. #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
  178. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  179. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  180. extern const uint32_t inverse[256];
  181. #ifdef ARCH_X86
  182. # define FASTDIV(a,b) \
  183. ({\
  184. int ret,dmy;\
  185. asm volatile(\
  186. "mull %3"\
  187. :"=d"(ret),"=a"(dmy)\
  188. :"1"(a),"g"(inverse[b])\
  189. );\
  190. ret;\
  191. })
  192. #elif defined(CONFIG_FASTDIV)
  193. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
  194. #else
  195. # define FASTDIV(a,b) ((a)/(b))
  196. #endif
  197. #ifdef ARCH_X86
  198. // avoid +32 for shift optimization (gcc should do that ...)
  199. static inline int32_t NEG_SSR32( int32_t a, int8_t s){
  200. asm ("sarl %1, %0\n\t"
  201. : "+r" (a)
  202. : "ic" ((uint8_t)(-s))
  203. );
  204. return a;
  205. }
  206. static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
  207. asm ("shrl %1, %0\n\t"
  208. : "+r" (a)
  209. : "ic" ((uint8_t)(-s))
  210. );
  211. return a;
  212. }
  213. #else
  214. # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
  215. # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
  216. #endif
  217. /* bit output */
  218. struct PutBitContext;
  219. typedef void (*WriteDataFunc)(void *, uint8_t *, int);
  220. typedef struct PutBitContext {
  221. #ifdef ALT_BITSTREAM_WRITER
  222. uint8_t *buf, *buf_end;
  223. int index;
  224. #else
  225. uint32_t bit_buf;
  226. int bit_left;
  227. uint8_t *buf, *buf_ptr, *buf_end;
  228. #endif
  229. } PutBitContext;
  230. void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size);
  231. int put_bits_count(PutBitContext *s);
  232. void align_put_bits(PutBitContext *s);
  233. void flush_put_bits(PutBitContext *s);
  234. void put_string(PutBitContext * pbc, char *s, int put_zero);
  235. /* bit input */
  236. typedef struct GetBitContext {
  237. const uint8_t *buffer, *buffer_end;
  238. #ifdef ALT_BITSTREAM_READER
  239. int index;
  240. #elif defined LIBMPEG2_BITSTREAM_READER
  241. uint8_t *buffer_ptr;
  242. uint32_t cache;
  243. int bit_count;
  244. #elif defined A32_BITSTREAM_READER
  245. uint32_t *buffer_ptr;
  246. uint32_t cache0;
  247. uint32_t cache1;
  248. int bit_count;
  249. #endif
  250. int size_in_bits;
  251. } GetBitContext;
  252. static inline int get_bits_count(GetBitContext *s);
  253. #define VLC_TYPE int16_t
  254. typedef struct VLC {
  255. int bits;
  256. VLC_TYPE (*table)[2]; ///< code, bits
  257. int table_size, table_allocated;
  258. } VLC;
  259. typedef struct RL_VLC_ELEM {
  260. int16_t level;
  261. int8_t len;
  262. uint8_t run;
  263. } RL_VLC_ELEM;
  264. #ifdef ARCH_SPARC64
  265. #define UNALIGNED_STORES_ARE_BAD
  266. #endif
  267. /* used to avoid missaligned exceptions on some archs (alpha, ...) */
  268. #ifdef ARCH_X86
  269. # define unaligned32(a) (*(uint32_t*)(a))
  270. #else
  271. # ifdef __GNUC__
  272. static inline uint32_t unaligned32(const void *v) {
  273. struct Unaligned {
  274. uint32_t i;
  275. } __attribute__((packed));
  276. return ((const struct Unaligned *) v)->i;
  277. }
  278. # elif defined(__DECC)
  279. static inline uint32_t unaligned32(const void *v) {
  280. return *(const __unaligned uint32_t *) v;
  281. }
  282. # else
  283. static inline uint32_t unaligned32(const void *v) {
  284. return *(const uint32_t *) v;
  285. }
  286. # endif
  287. #endif //!ARCH_X86
  288. #ifndef ALT_BITSTREAM_WRITER
  289. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  290. {
  291. unsigned int bit_buf;
  292. int bit_left;
  293. #ifdef STATS
  294. st_out_bit_counts[st_current_index] += n;
  295. #endif
  296. // printf("put_bits=%d %x\n", n, value);
  297. assert(n == 32 || value < (1U << n));
  298. bit_buf = s->bit_buf;
  299. bit_left = s->bit_left;
  300. // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
  301. /* XXX: optimize */
  302. if (n < bit_left) {
  303. bit_buf = (bit_buf<<n) | value;
  304. bit_left-=n;
  305. } else {
  306. bit_buf<<=bit_left;
  307. bit_buf |= value >> (n - bit_left);
  308. #ifdef UNALIGNED_STORES_ARE_BAD
  309. if (3 & (int) s->buf_ptr) {
  310. s->buf_ptr[0] = bit_buf >> 24;
  311. s->buf_ptr[1] = bit_buf >> 16;
  312. s->buf_ptr[2] = bit_buf >> 8;
  313. s->buf_ptr[3] = bit_buf ;
  314. } else
  315. #endif
  316. *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
  317. //printf("bitbuf = %08x\n", bit_buf);
  318. s->buf_ptr+=4;
  319. bit_left+=32 - n;
  320. bit_buf = value;
  321. }
  322. s->bit_buf = bit_buf;
  323. s->bit_left = bit_left;
  324. }
  325. #endif
  326. #ifdef ALT_BITSTREAM_WRITER
  327. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  328. {
  329. # ifdef ALIGNED_BITSTREAM_WRITER
  330. # ifdef ARCH_X86
  331. asm volatile(
  332. "movl %0, %%ecx \n\t"
  333. "xorl %%eax, %%eax \n\t"
  334. "shrdl %%cl, %1, %%eax \n\t"
  335. "shrl %%cl, %1 \n\t"
  336. "movl %0, %%ecx \n\t"
  337. "shrl $3, %%ecx \n\t"
  338. "andl $0xFFFFFFFC, %%ecx \n\t"
  339. "bswapl %1 \n\t"
  340. "orl %1, (%2, %%ecx) \n\t"
  341. "bswapl %%eax \n\t"
  342. "addl %3, %0 \n\t"
  343. "movl %%eax, 4(%2, %%ecx) \n\t"
  344. : "=&r" (s->index), "=&r" (value)
  345. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
  346. : "%eax", "%ecx"
  347. );
  348. # else
  349. int index= s->index;
  350. uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
  351. value<<= 32-n;
  352. ptr[0] |= be2me_32(value>>(index&31));
  353. ptr[1] = be2me_32(value<<(32-(index&31)));
  354. //if(n>24) printf("%d %d\n", n, value);
  355. index+= n;
  356. s->index= index;
  357. # endif
  358. # else //ALIGNED_BITSTREAM_WRITER
  359. # ifdef ARCH_X86
  360. asm volatile(
  361. "movl $7, %%ecx \n\t"
  362. "andl %0, %%ecx \n\t"
  363. "addl %3, %%ecx \n\t"
  364. "negl %%ecx \n\t"
  365. "shll %%cl, %1 \n\t"
  366. "bswapl %1 \n\t"
  367. "movl %0, %%ecx \n\t"
  368. "shrl $3, %%ecx \n\t"
  369. "orl %1, (%%ecx, %2) \n\t"
  370. "addl %3, %0 \n\t"
  371. "movl $0, 4(%%ecx, %2) \n\t"
  372. : "=&r" (s->index), "=&r" (value)
  373. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
  374. : "%ecx"
  375. );
  376. # else
  377. int index= s->index;
  378. uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  379. ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
  380. ptr[1] = 0;
  381. //if(n>24) printf("%d %d\n", n, value);
  382. index+= n;
  383. s->index= index;
  384. # endif
  385. # endif //!ALIGNED_BITSTREAM_WRITER
  386. }
  387. #endif
  388. static inline uint8_t* pbBufPtr(PutBitContext *s)
  389. {
  390. #ifdef ALT_BITSTREAM_WRITER
  391. return s->buf + (s->index>>3);
  392. #else
  393. return s->buf_ptr;
  394. #endif
  395. }
  396. /**
  397. *
  398. * PutBitContext must be flushed & aligned to a byte boundary before calling this.
  399. */
  400. static inline void skip_put_bytes(PutBitContext *s, int n){
  401. assert((put_bits_count(s)&7)==0);
  402. #ifdef ALT_BITSTREAM_WRITER
  403. FIXME may need some cleaning of the buffer
  404. s->index += n<<3;
  405. #else
  406. assert(s->bit_left==32);
  407. s->buf_ptr += n;
  408. #endif
  409. }
  410. /**
  411. * Changes the end of the buffer.
  412. */
  413. static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
  414. s->buf_end= s->buf + size;
  415. }
  416. /* Bitstream reader API docs:
  417. name
  418. abritary name which is used as prefix for the internal variables
  419. gb
  420. getbitcontext
  421. OPEN_READER(name, gb)
  422. loads gb into local variables
  423. CLOSE_READER(name, gb)
  424. stores local vars in gb
  425. UPDATE_CACHE(name, gb)
  426. refills the internal cache from the bitstream
  427. after this call at least MIN_CACHE_BITS will be available,
  428. GET_CACHE(name, gb)
  429. will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
  430. SHOW_UBITS(name, gb, num)
  431. will return the nest num bits
  432. SHOW_SBITS(name, gb, num)
  433. will return the nest num bits and do sign extension
  434. SKIP_BITS(name, gb, num)
  435. will skip over the next num bits
  436. note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
  437. SKIP_CACHE(name, gb, num)
  438. will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
  439. SKIP_COUNTER(name, gb, num)
  440. will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
  441. LAST_SKIP_CACHE(name, gb, num)
  442. will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
  443. LAST_SKIP_BITS(name, gb, num)
  444. is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
  445. for examples see get_bits, show_bits, skip_bits, get_vlc
  446. */
  447. static inline int unaligned32_be(const void *v)
  448. {
  449. #ifdef CONFIG_ALIGN
  450. const uint8_t *p=v;
  451. return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
  452. #else
  453. return be2me_32( unaligned32(v)); //original
  454. #endif
  455. }
  456. #ifdef ALT_BITSTREAM_READER
  457. # define MIN_CACHE_BITS 25
  458. # define OPEN_READER(name, gb)\
  459. int name##_index= (gb)->index;\
  460. int name##_cache= 0;\
  461. # define CLOSE_READER(name, gb)\
  462. (gb)->index= name##_index;\
  463. # define UPDATE_CACHE(name, gb)\
  464. name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
  465. # define SKIP_CACHE(name, gb, num)\
  466. name##_cache <<= (num);\
  467. // FIXME name?
  468. # define SKIP_COUNTER(name, gb, num)\
  469. name##_index += (num);\
  470. # define SKIP_BITS(name, gb, num)\
  471. {\
  472. SKIP_CACHE(name, gb, num)\
  473. SKIP_COUNTER(name, gb, num)\
  474. }\
  475. # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  476. # define LAST_SKIP_CACHE(name, gb, num) ;
  477. # define SHOW_UBITS(name, gb, num)\
  478. NEG_USR32(name##_cache, num)
  479. # define SHOW_SBITS(name, gb, num)\
  480. NEG_SSR32(name##_cache, num)
  481. # define GET_CACHE(name, gb)\
  482. ((uint32_t)name##_cache)
  483. static inline int get_bits_count(GetBitContext *s){
  484. return s->index;
  485. }
  486. #elif defined LIBMPEG2_BITSTREAM_READER
  487. //libmpeg2 like reader
  488. # define MIN_CACHE_BITS 17
  489. # define OPEN_READER(name, gb)\
  490. int name##_bit_count=(gb)->bit_count;\
  491. int name##_cache= (gb)->cache;\
  492. uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  493. # define CLOSE_READER(name, gb)\
  494. (gb)->bit_count= name##_bit_count;\
  495. (gb)->cache= name##_cache;\
  496. (gb)->buffer_ptr= name##_buffer_ptr;\
  497. #ifdef LIBMPEG2_BITSTREAM_READER_HACK
  498. # define UPDATE_CACHE(name, gb)\
  499. if(name##_bit_count >= 0){\
  500. name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
  501. ((uint16_t*)name##_buffer_ptr)++;\
  502. name##_bit_count-= 16;\
  503. }\
  504. #else
  505. # define UPDATE_CACHE(name, gb)\
  506. if(name##_bit_count >= 0){\
  507. name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
  508. name##_buffer_ptr+=2;\
  509. name##_bit_count-= 16;\
  510. }\
  511. #endif
  512. # define SKIP_CACHE(name, gb, num)\
  513. name##_cache <<= (num);\
  514. # define SKIP_COUNTER(name, gb, num)\
  515. name##_bit_count += (num);\
  516. # define SKIP_BITS(name, gb, num)\
  517. {\
  518. SKIP_CACHE(name, gb, num)\
  519. SKIP_COUNTER(name, gb, num)\
  520. }\
  521. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  522. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  523. # define SHOW_UBITS(name, gb, num)\
  524. NEG_USR32(name##_cache, num)
  525. # define SHOW_SBITS(name, gb, num)\
  526. NEG_SSR32(name##_cache, num)
  527. # define GET_CACHE(name, gb)\
  528. ((uint32_t)name##_cache)
  529. static inline int get_bits_count(GetBitContext *s){
  530. return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
  531. }
  532. #elif defined A32_BITSTREAM_READER
  533. # define MIN_CACHE_BITS 32
  534. # define OPEN_READER(name, gb)\
  535. int name##_bit_count=(gb)->bit_count;\
  536. uint32_t name##_cache0= (gb)->cache0;\
  537. uint32_t name##_cache1= (gb)->cache1;\
  538. uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  539. # define CLOSE_READER(name, gb)\
  540. (gb)->bit_count= name##_bit_count;\
  541. (gb)->cache0= name##_cache0;\
  542. (gb)->cache1= name##_cache1;\
  543. (gb)->buffer_ptr= name##_buffer_ptr;\
  544. # define UPDATE_CACHE(name, gb)\
  545. if(name##_bit_count > 0){\
  546. const uint32_t next= be2me_32( *name##_buffer_ptr );\
  547. name##_cache0 |= NEG_USR32(next,name##_bit_count);\
  548. name##_cache1 |= next<<name##_bit_count;\
  549. name##_buffer_ptr++;\
  550. name##_bit_count-= 32;\
  551. }\
  552. #ifdef ARCH_X86
  553. # define SKIP_CACHE(name, gb, num)\
  554. asm(\
  555. "shldl %2, %1, %0 \n\t"\
  556. "shll %2, %1 \n\t"\
  557. : "+r" (name##_cache0), "+r" (name##_cache1)\
  558. : "Ic" ((uint8_t)num)\
  559. );
  560. #else
  561. # define SKIP_CACHE(name, gb, num)\
  562. name##_cache0 <<= (num);\
  563. name##_cache0 |= NEG_USR32(name##_cache1,num);\
  564. name##_cache1 <<= (num);
  565. #endif
  566. # define SKIP_COUNTER(name, gb, num)\
  567. name##_bit_count += (num);\
  568. # define SKIP_BITS(name, gb, num)\
  569. {\
  570. SKIP_CACHE(name, gb, num)\
  571. SKIP_COUNTER(name, gb, num)\
  572. }\
  573. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  574. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  575. # define SHOW_UBITS(name, gb, num)\
  576. NEG_USR32(name##_cache0, num)
  577. # define SHOW_SBITS(name, gb, num)\
  578. NEG_SSR32(name##_cache0, num)
  579. # define GET_CACHE(name, gb)\
  580. (name##_cache0)
  581. static inline int get_bits_count(GetBitContext *s){
  582. return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
  583. }
  584. #endif
  585. /**
  586. * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
  587. * if MSB not set it is negative
  588. * @param n length in bits
  589. * @author BERO
  590. */
  591. static inline int get_xbits(GetBitContext *s, int n){
  592. register int tmp;
  593. register int32_t cache;
  594. OPEN_READER(re, s)
  595. UPDATE_CACHE(re, s)
  596. cache = GET_CACHE(re,s);
  597. if ((int32_t)cache<0) { //MSB=1
  598. tmp = NEG_USR32(cache,n);
  599. } else {
  600. // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
  601. // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
  602. tmp = - NEG_USR32(~cache,n);
  603. }
  604. LAST_SKIP_BITS(re, s, n)
  605. CLOSE_READER(re, s)
  606. return tmp;
  607. }
  608. static inline int get_sbits(GetBitContext *s, int n){
  609. register int tmp;
  610. OPEN_READER(re, s)
  611. UPDATE_CACHE(re, s)
  612. tmp= SHOW_SBITS(re, s, n);
  613. LAST_SKIP_BITS(re, s, n)
  614. CLOSE_READER(re, s)
  615. return tmp;
  616. }
  617. /**
  618. * reads 0-17 bits.
  619. * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
  620. */
  621. static inline unsigned int get_bits(GetBitContext *s, int n){
  622. register int tmp;
  623. OPEN_READER(re, s)
  624. UPDATE_CACHE(re, s)
  625. tmp= SHOW_UBITS(re, s, n);
  626. LAST_SKIP_BITS(re, s, n)
  627. CLOSE_READER(re, s)
  628. return tmp;
  629. }
  630. unsigned int get_bits_long(GetBitContext *s, int n);
  631. /**
  632. * shows 0-17 bits.
  633. * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
  634. */
  635. static inline unsigned int show_bits(GetBitContext *s, int n){
  636. register int tmp;
  637. OPEN_READER(re, s)
  638. UPDATE_CACHE(re, s)
  639. tmp= SHOW_UBITS(re, s, n);
  640. // CLOSE_READER(re, s)
  641. return tmp;
  642. }
  643. unsigned int show_bits_long(GetBitContext *s, int n);
  644. static inline void skip_bits(GetBitContext *s, int n){
  645. //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
  646. OPEN_READER(re, s)
  647. UPDATE_CACHE(re, s)
  648. LAST_SKIP_BITS(re, s, n)
  649. CLOSE_READER(re, s)
  650. }
  651. static inline unsigned int get_bits1(GetBitContext *s){
  652. #ifdef ALT_BITSTREAM_READER
  653. int index= s->index;
  654. uint8_t result= s->buffer[ index>>3 ];
  655. result<<= (index&0x07);
  656. result>>= 8 - 1;
  657. index++;
  658. s->index= index;
  659. return result;
  660. #else
  661. return get_bits(s, 1);
  662. #endif
  663. }
  664. static inline unsigned int show_bits1(GetBitContext *s){
  665. return show_bits(s, 1);
  666. }
  667. static inline void skip_bits1(GetBitContext *s){
  668. skip_bits(s, 1);
  669. }
  670. void init_get_bits(GetBitContext *s,
  671. const uint8_t *buffer, int buffer_size);
  672. int check_marker(GetBitContext *s, const char *msg);
  673. void align_get_bits(GetBitContext *s);
  674. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  675. const void *bits, int bits_wrap, int bits_size,
  676. const void *codes, int codes_wrap, int codes_size);
  677. void free_vlc(VLC *vlc);
  678. /**
  679. *
  680. * if the vlc code is invalid and max_depth=1 than no bits will be removed
  681. * if the vlc code is invalid and max_depth>1 than the number of bits removed
  682. * is undefined
  683. */
  684. #define GET_VLC(code, name, gb, table, bits, max_depth)\
  685. {\
  686. int n, index, nb_bits;\
  687. \
  688. index= SHOW_UBITS(name, gb, bits);\
  689. code = table[index][0];\
  690. n = table[index][1];\
  691. \
  692. if(max_depth > 1 && n < 0){\
  693. LAST_SKIP_BITS(name, gb, bits)\
  694. UPDATE_CACHE(name, gb)\
  695. \
  696. nb_bits = -n;\
  697. \
  698. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  699. code = table[index][0];\
  700. n = table[index][1];\
  701. if(max_depth > 2 && n < 0){\
  702. LAST_SKIP_BITS(name, gb, nb_bits)\
  703. UPDATE_CACHE(name, gb)\
  704. \
  705. nb_bits = -n;\
  706. \
  707. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  708. code = table[index][0];\
  709. n = table[index][1];\
  710. }\
  711. }\
  712. SKIP_BITS(name, gb, n)\
  713. }
  714. #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
  715. {\
  716. int n, index, nb_bits;\
  717. \
  718. index= SHOW_UBITS(name, gb, bits);\
  719. level = table[index].level;\
  720. n = table[index].len;\
  721. \
  722. if(max_depth > 1 && n < 0){\
  723. LAST_SKIP_BITS(name, gb, bits)\
  724. UPDATE_CACHE(name, gb)\
  725. \
  726. nb_bits = -n;\
  727. \
  728. index= SHOW_UBITS(name, gb, nb_bits) + level;\
  729. level = table[index].level;\
  730. n = table[index].len;\
  731. }\
  732. run= table[index].run;\
  733. SKIP_BITS(name, gb, n)\
  734. }
  735. // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
  736. static inline int get_vlc(GetBitContext *s, VLC *vlc)
  737. {
  738. int code;
  739. VLC_TYPE (*table)[2]= vlc->table;
  740. OPEN_READER(re, s)
  741. UPDATE_CACHE(re, s)
  742. GET_VLC(code, re, s, table, vlc->bits, 3)
  743. CLOSE_READER(re, s)
  744. return code;
  745. }
  746. /**
  747. * parses a vlc code, faster then get_vlc()
  748. * @param bits is the number of bits which will be read at once, must be
  749. * identical to nb_bits in init_vlc()
  750. * @param max_depth is the number of times bits bits must be readed to completly
  751. * read the longest vlc code
  752. * = (max_vlc_length + bits - 1) / bits
  753. */
  754. static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  755. int bits, int max_depth)
  756. {
  757. int code;
  758. OPEN_READER(re, s)
  759. UPDATE_CACHE(re, s)
  760. GET_VLC(code, re, s, table, bits, max_depth)
  761. CLOSE_READER(re, s)
  762. return code;
  763. }
  764. //#define TRACE
  765. #ifdef TRACE
  766. static inline void print_bin(int bits, int n){
  767. int i;
  768. for(i=n-1; i>=0; i--){
  769. printf("%d", (bits>>i)&1);
  770. }
  771. for(i=n; i<24; i++)
  772. printf(" ");
  773. }
  774. static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
  775. int r= get_bits(s, n);
  776. print_bin(r, n);
  777. printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
  778. return r;
  779. }
  780. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
  781. int show= show_bits(s, 24);
  782. int pos= get_bits_count(s);
  783. int r= get_vlc2(s, table, bits, max_depth);
  784. int len= get_bits_count(s) - pos;
  785. int bits2= show>>(24-len);
  786. print_bin(bits2, len);
  787. printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
  788. return r;
  789. }
  790. static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
  791. int show= show_bits(s, n);
  792. int r= get_xbits(s, n);
  793. print_bin(show, n);
  794. printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
  795. return r;
  796. }
  797. #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  798. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  799. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  800. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  801. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  802. #define tprintf printf
  803. #else //TRACE
  804. #define tprintf(...) {}
  805. #endif
  806. /* define it to include statistics code (useful only for optimizing
  807. codec efficiency */
  808. //#define STATS
  809. #ifdef STATS
  810. enum {
  811. ST_UNKNOWN,
  812. ST_DC,
  813. ST_INTRA_AC,
  814. ST_INTER_AC,
  815. ST_INTRA_MB,
  816. ST_INTER_MB,
  817. ST_MV,
  818. ST_NB,
  819. };
  820. extern int st_current_index;
  821. extern unsigned int st_bit_counts[ST_NB];
  822. extern unsigned int st_out_bit_counts[ST_NB];
  823. void print_stats(void);
  824. #endif
  825. /* misc math functions */
  826. extern const uint8_t ff_log2_tab[256];
  827. static inline int av_log2(unsigned int v)
  828. {
  829. int n;
  830. n = 0;
  831. if (v & 0xffff0000) {
  832. v >>= 16;
  833. n += 16;
  834. }
  835. if (v & 0xff00) {
  836. v >>= 8;
  837. n += 8;
  838. }
  839. n += ff_log2_tab[v];
  840. return n;
  841. }
  842. static inline int av_log2_16bit(unsigned int v)
  843. {
  844. int n;
  845. n = 0;
  846. if (v & 0xff00) {
  847. v >>= 8;
  848. n += 8;
  849. }
  850. n += ff_log2_tab[v];
  851. return n;
  852. }
  853. /* median of 3 */
  854. static inline int mid_pred(int a, int b, int c)
  855. {
  856. #if 0
  857. int t= (a-b)&((a-b)>>31);
  858. a-=t;
  859. b+=t;
  860. b-= (b-c)&((b-c)>>31);
  861. b+= (a-b)&((a-b)>>31);
  862. return b;
  863. #else
  864. if(a>b){
  865. if(c>b){
  866. if(c>a) b=a;
  867. else b=c;
  868. }
  869. }else{
  870. if(b>c){
  871. if(c>a) b=c;
  872. else b=a;
  873. }
  874. }
  875. return b;
  876. #endif
  877. }
  878. static inline int clip(int a, int amin, int amax)
  879. {
  880. if (a < amin)
  881. return amin;
  882. else if (a > amax)
  883. return amax;
  884. else
  885. return a;
  886. }
  887. /* math */
  888. extern const uint8_t ff_sqrt_tab[128];
  889. int64_t ff_gcd(int64_t a, int64_t b);
  890. static inline int ff_sqrt(int a)
  891. {
  892. int ret=0;
  893. int s;
  894. int ret_sq=0;
  895. if(a<128) return ff_sqrt_tab[a];
  896. for(s=15; s>=0; s--){
  897. int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
  898. if(b<=a){
  899. ret_sq=b;
  900. ret+= 1<<s;
  901. }
  902. }
  903. return ret;
  904. }
  905. /**
  906. * converts fourcc string to int
  907. */
  908. static inline int ff_get_fourcc(const char *s){
  909. assert( strlen(s)==4 );
  910. return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
  911. }
  912. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  913. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  914. #ifdef ARCH_X86
  915. #define MASK_ABS(mask, level)\
  916. asm volatile(\
  917. "cdq \n\t"\
  918. "xorl %1, %0 \n\t"\
  919. "subl %1, %0 \n\t"\
  920. : "+a" (level), "=&d" (mask)\
  921. );
  922. #else
  923. #define MASK_ABS(mask, level)\
  924. mask= level>>31;\
  925. level= (level^mask)-mask;
  926. #endif
  927. #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
  928. #define COPY3_IF_LT(x,y,a,b,c,d)\
  929. asm volatile (\
  930. "cmpl %0, %3 \n\t"\
  931. "cmovl %3, %0 \n\t"\
  932. "cmovl %4, %1 \n\t"\
  933. "cmovl %5, %2 \n\t"\
  934. : "+r" (x), "+r" (a), "+r" (c)\
  935. : "r" (y), "r" (b), "r" (d)\
  936. );
  937. #else
  938. #define COPY3_IF_LT(x,y,a,b,c,d)\
  939. if((y)<(x)){\
  940. (x)=(y);\
  941. (a)=(b);\
  942. (c)=(d);\
  943. }
  944. #endif
  945. #ifdef ARCH_X86
  946. static inline long long rdtsc()
  947. {
  948. long long l;
  949. asm volatile( "rdtsc\n\t"
  950. : "=A" (l)
  951. );
  952. return l;
  953. }
  954. #define START_TIMER \
  955. uint64_t tend;\
  956. uint64_t tstart= rdtsc();\
  957. #define STOP_TIMER(id) \
  958. tend= rdtsc();\
  959. {\
  960. static uint64_t tsum=0;\
  961. static int tcount=0;\
  962. static int tskip_count=0;\
  963. if(tcount<2 || tend - tstart < 8*tsum/tcount){\
  964. tsum+= tend - tstart;\
  965. tcount++;\
  966. }else\
  967. tskip_count++;\
  968. if(256*256*256*64%(tcount+tskip_count)==0){\
  969. av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
  970. }\
  971. }
  972. #endif
  973. #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
  974. /* avoid usage of various functions */
  975. #define malloc please_use_av_malloc
  976. #define free please_use_av_free
  977. #define realloc please_use_av_realloc
  978. #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
  979. #define printf please_use_av_log
  980. #define fprintf please_use_av_log
  981. #endif
  982. #define CHECKED_ALLOCZ(p, size)\
  983. {\
  984. p= av_mallocz(size);\
  985. if(p==NULL && (size)!=0){\
  986. perror("malloc");\
  987. goto fail;\
  988. }\
  989. }
  990. #endif /* HAVE_AV_CONFIG_H */
  991. #endif /* COMMON_H */