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.

1141 lines
27KB

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