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.

1084 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. ((uint16_t*)name##_buffer_ptr)++;\
  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. /**
  524. * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
  525. * if MSB not set it is negative
  526. * @param n length in bits
  527. * @author BERO
  528. */
  529. static inline int get_xbits(GetBitContext *s, int n){
  530. register int tmp;
  531. register int32_t cache;
  532. OPEN_READER(re, s)
  533. UPDATE_CACHE(re, s)
  534. cache = GET_CACHE(re,s);
  535. if ((int32_t)cache<0) { //MSB=1
  536. tmp = NEG_USR32(cache,n);
  537. } else {
  538. // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
  539. // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
  540. tmp = - NEG_USR32(~cache,n);
  541. }
  542. LAST_SKIP_BITS(re, s, n)
  543. CLOSE_READER(re, s)
  544. return tmp;
  545. }
  546. static inline int get_sbits(GetBitContext *s, int n){
  547. register int tmp;
  548. OPEN_READER(re, s)
  549. UPDATE_CACHE(re, s)
  550. tmp= SHOW_SBITS(re, s, n);
  551. LAST_SKIP_BITS(re, s, n)
  552. CLOSE_READER(re, s)
  553. return tmp;
  554. }
  555. /**
  556. * reads 0-17 bits.
  557. * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
  558. */
  559. static inline unsigned int get_bits(GetBitContext *s, int n){
  560. register int tmp;
  561. OPEN_READER(re, s)
  562. UPDATE_CACHE(re, s)
  563. tmp= SHOW_UBITS(re, s, n);
  564. LAST_SKIP_BITS(re, s, n)
  565. CLOSE_READER(re, s)
  566. return tmp;
  567. }
  568. unsigned int get_bits_long(GetBitContext *s, int n);
  569. /**
  570. * shows 0-17 bits.
  571. * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
  572. */
  573. static inline unsigned int show_bits(GetBitContext *s, int n){
  574. register int tmp;
  575. OPEN_READER(re, s)
  576. UPDATE_CACHE(re, s)
  577. tmp= SHOW_UBITS(re, s, n);
  578. // CLOSE_READER(re, s)
  579. return tmp;
  580. }
  581. unsigned int show_bits_long(GetBitContext *s, int n);
  582. static inline void skip_bits(GetBitContext *s, int n){
  583. //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
  584. OPEN_READER(re, s)
  585. UPDATE_CACHE(re, s)
  586. LAST_SKIP_BITS(re, s, n)
  587. CLOSE_READER(re, s)
  588. }
  589. static inline unsigned int get_bits1(GetBitContext *s){
  590. #ifdef ALT_BITSTREAM_READER
  591. int index= s->index;
  592. uint8_t result= s->buffer[ index>>3 ];
  593. result<<= (index&0x07);
  594. result>>= 8 - 1;
  595. index++;
  596. s->index= index;
  597. return result;
  598. #else
  599. return get_bits(s, 1);
  600. #endif
  601. }
  602. static inline unsigned int show_bits1(GetBitContext *s){
  603. return show_bits(s, 1);
  604. }
  605. static inline void skip_bits1(GetBitContext *s){
  606. skip_bits(s, 1);
  607. }
  608. void init_get_bits(GetBitContext *s,
  609. const uint8_t *buffer, int buffer_size);
  610. int check_marker(GetBitContext *s, const char *msg);
  611. void align_get_bits(GetBitContext *s);
  612. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  613. const void *bits, int bits_wrap, int bits_size,
  614. const void *codes, int codes_wrap, int codes_size);
  615. void free_vlc(VLC *vlc);
  616. /**
  617. *
  618. * if the vlc code is invalid and max_depth=1 than no bits will be removed
  619. * if the vlc code is invalid and max_depth>1 than the number of bits removed
  620. * is undefined
  621. */
  622. #define GET_VLC(code, name, gb, table, bits, max_depth)\
  623. {\
  624. int n, index, nb_bits;\
  625. \
  626. index= SHOW_UBITS(name, gb, bits);\
  627. code = table[index][0];\
  628. n = table[index][1];\
  629. \
  630. if(max_depth > 1 && n < 0){\
  631. LAST_SKIP_BITS(name, gb, bits)\
  632. UPDATE_CACHE(name, gb)\
  633. \
  634. nb_bits = -n;\
  635. \
  636. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  637. code = table[index][0];\
  638. n = table[index][1];\
  639. if(max_depth > 2 && n < 0){\
  640. LAST_SKIP_BITS(name, gb, nb_bits)\
  641. UPDATE_CACHE(name, gb)\
  642. \
  643. nb_bits = -n;\
  644. \
  645. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  646. code = table[index][0];\
  647. n = table[index][1];\
  648. }\
  649. }\
  650. SKIP_BITS(name, gb, n)\
  651. }
  652. #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
  653. {\
  654. int n, index, nb_bits;\
  655. \
  656. index= SHOW_UBITS(name, gb, bits);\
  657. level = table[index].level;\
  658. n = table[index].len;\
  659. \
  660. if(max_depth > 1 && n < 0){\
  661. LAST_SKIP_BITS(name, gb, bits)\
  662. UPDATE_CACHE(name, gb)\
  663. \
  664. nb_bits = -n;\
  665. \
  666. index= SHOW_UBITS(name, gb, nb_bits) + level;\
  667. level = table[index].level;\
  668. n = table[index].len;\
  669. }\
  670. run= table[index].run;\
  671. SKIP_BITS(name, gb, n)\
  672. }
  673. // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
  674. static inline int get_vlc(GetBitContext *s, VLC *vlc)
  675. {
  676. int code;
  677. VLC_TYPE (*table)[2]= vlc->table;
  678. OPEN_READER(re, s)
  679. UPDATE_CACHE(re, s)
  680. GET_VLC(code, re, s, table, vlc->bits, 3)
  681. CLOSE_READER(re, s)
  682. return code;
  683. }
  684. /**
  685. * parses a vlc code, faster then get_vlc()
  686. * @param bits is the number of bits which will be read at once, must be
  687. * identical to nb_bits in init_vlc()
  688. * @param max_depth is the number of times bits bits must be readed to completly
  689. * read the longest vlc code
  690. * = (max_vlc_length + bits - 1) / bits
  691. */
  692. static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  693. int bits, int max_depth)
  694. {
  695. int code;
  696. OPEN_READER(re, s)
  697. UPDATE_CACHE(re, s)
  698. GET_VLC(code, re, s, table, bits, max_depth)
  699. CLOSE_READER(re, s)
  700. return code;
  701. }
  702. //#define TRACE
  703. #ifdef TRACE
  704. static inline void print_bin(int bits, int n){
  705. int i;
  706. for(i=n-1; i>=0; i--){
  707. printf("%d", (bits>>i)&1);
  708. }
  709. for(i=n; i<24; i++)
  710. printf(" ");
  711. }
  712. static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
  713. int r= get_bits(s, n);
  714. print_bin(r, n);
  715. printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
  716. return r;
  717. }
  718. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
  719. int show= show_bits(s, 24);
  720. int pos= get_bits_count(s);
  721. int r= get_vlc2(s, table, bits, max_depth);
  722. int len= get_bits_count(s) - pos;
  723. int bits2= show>>(24-len);
  724. print_bin(bits2, len);
  725. printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
  726. return r;
  727. }
  728. #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  729. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  730. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  731. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  732. #define tprintf printf
  733. #else //TRACE
  734. #define tprintf(_arg...) {}
  735. #endif
  736. /* define it to include statistics code (useful only for optimizing
  737. codec efficiency */
  738. //#define STATS
  739. #ifdef STATS
  740. enum {
  741. ST_UNKNOWN,
  742. ST_DC,
  743. ST_INTRA_AC,
  744. ST_INTER_AC,
  745. ST_INTRA_MB,
  746. ST_INTER_MB,
  747. ST_MV,
  748. ST_NB,
  749. };
  750. extern int st_current_index;
  751. extern unsigned int st_bit_counts[ST_NB];
  752. extern unsigned int st_out_bit_counts[ST_NB];
  753. void print_stats(void);
  754. #endif
  755. /* misc math functions */
  756. extern const uint8_t ff_log2_tab[256];
  757. static inline int av_log2(unsigned int v)
  758. {
  759. int n;
  760. n = 0;
  761. if (v & 0xffff0000) {
  762. v >>= 16;
  763. n += 16;
  764. }
  765. if (v & 0xff00) {
  766. v >>= 8;
  767. n += 8;
  768. }
  769. n += ff_log2_tab[v];
  770. return n;
  771. }
  772. static inline int av_log2_16bit(unsigned int v)
  773. {
  774. int n;
  775. n = 0;
  776. if (v & 0xff00) {
  777. v >>= 8;
  778. n += 8;
  779. }
  780. n += ff_log2_tab[v];
  781. return n;
  782. }
  783. /* median of 3 */
  784. static inline int mid_pred(int a, int b, int c)
  785. {
  786. int vmin, vmax;
  787. vmax = vmin = a;
  788. if (b < vmin)
  789. vmin = b;
  790. else
  791. vmax = b;
  792. if (c < vmin)
  793. vmin = c;
  794. else if (c > vmax)
  795. vmax = c;
  796. return a + b + c - vmin - vmax;
  797. }
  798. static inline int clip(int a, int amin, int amax)
  799. {
  800. if (a < amin)
  801. return amin;
  802. else if (a > amax)
  803. return amax;
  804. else
  805. return a;
  806. }
  807. /* math */
  808. extern const uint8_t ff_sqrt_tab[128];
  809. int64_t ff_gcd(int64_t a, int64_t b);
  810. static inline int ff_sqrt(int a)
  811. {
  812. int ret=0;
  813. int s;
  814. int ret_sq=0;
  815. if(a<128) return ff_sqrt_tab[a];
  816. for(s=15; s>=0; s--){
  817. int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
  818. if(b<=a){
  819. ret_sq=b;
  820. ret+= 1<<s;
  821. }
  822. }
  823. return ret;
  824. }
  825. /**
  826. * converts fourcc string to int
  827. */
  828. static inline int ff_get_fourcc(const char *s){
  829. assert( strlen(s)==4 );
  830. return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
  831. }
  832. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  833. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  834. void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max);
  835. #ifdef ARCH_X86
  836. #define MASK_ABS(mask, level)\
  837. asm volatile(\
  838. "cdq \n\t"\
  839. "xorl %1, %0 \n\t"\
  840. "subl %1, %0 \n\t"\
  841. : "+a" (level), "=&d" (mask)\
  842. );
  843. #else
  844. #define MASK_ABS(mask, level)\
  845. mask= level>>31;\
  846. level= (level^mask)-mask;
  847. #endif
  848. #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
  849. #define COPY3_IF_LT(x,y,a,b,c,d)\
  850. asm volatile (\
  851. "cmpl %0, %3 \n\t"\
  852. "cmovl %3, %0 \n\t"\
  853. "cmovl %4, %1 \n\t"\
  854. "cmovl %5, %2 \n\t"\
  855. : "+r" (x), "+r" (a), "+r" (c)\
  856. : "r" (y), "r" (b), "r" (d)\
  857. );
  858. #else
  859. #define COPY3_IF_LT(x,y,a,b,c,d)\
  860. if((y)<(x)){\
  861. (x)=(y);\
  862. (a)=(b);\
  863. (c)=(d);\
  864. }
  865. #endif
  866. #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
  867. /* avoid usage of various functions */
  868. #define malloc please_use_av_malloc
  869. #define free please_use_av_free
  870. #define realloc please_use_av_realloc
  871. #define CHECKED_ALLOCZ(p, size)\
  872. {\
  873. p= av_mallocz(size);\
  874. if(p==NULL){\
  875. perror("malloc");\
  876. goto fail;\
  877. }\
  878. }
  879. #endif /* HAVE_AV_CONFIG_H */
  880. #endif /* COMMON_H */