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.

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