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.

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