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.

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