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.

945 lines
21KB

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