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.

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