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.

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