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.

1299 lines
31KB

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