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.

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