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.

1337 lines
32KB

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