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.

937 lines
22KB

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