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.

955 lines
22KB

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