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.

1141 lines
27KB

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