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.

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