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.

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