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.

1157 lines
28KB

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