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.

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