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.

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