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.

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