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.

968 lines
23KB

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