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.

1156 lines
28KB

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