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.

1208 lines
29KB

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