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.

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