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.

1069 lines
26KB

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