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.

1311 lines
31KB

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