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.

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