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.

588 lines
13KB

  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_READER
  9. //#define ALIGNED_BITSTREAM
  10. #define FAST_GET_FIRST_VLC
  11. #ifdef HAVE_AV_CONFIG_H
  12. /* only include the following when compiling package */
  13. #include "../config.h"
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <errno.h>
  18. #ifndef ENODATA
  19. #define ENODATA 61
  20. #endif
  21. #endif
  22. #ifdef CONFIG_WIN32
  23. /* windows */
  24. typedef unsigned short UINT16;
  25. typedef signed short INT16;
  26. typedef unsigned char UINT8;
  27. typedef unsigned int UINT32;
  28. typedef unsigned __int64 UINT64;
  29. typedef signed char INT8;
  30. typedef signed int INT32;
  31. typedef signed __int64 INT64;
  32. typedef UINT8 uint8_t;
  33. typedef INT8 int8_t;
  34. typedef UINT16 uint16_t;
  35. typedef INT16 int16_t;
  36. typedef UINT32 uint32_t;
  37. typedef INT32 int32_t;
  38. #ifndef __MINGW32__
  39. #define INT64_C(c) (c ## i64)
  40. #define UINT64_C(c) (c ## i64)
  41. #define inline __inline
  42. /*
  43. Disable warning messages:
  44. warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
  45. warning C4305: 'argument' : truncation from 'const double' to 'float'
  46. */
  47. #pragma warning( disable : 4244 )
  48. #pragma warning( disable : 4305 )
  49. #else
  50. #define INT64_C(c) (c ## LL)
  51. #define UINT64_C(c) (c ## ULL)
  52. #endif /* __MINGW32__ */
  53. #define M_PI 3.14159265358979323846
  54. #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
  55. #ifdef _DEBUG
  56. #define DEBUG
  57. #endif
  58. // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
  59. #define bswap_32(x) \
  60. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  61. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  62. #define be2me_32(x) bswap_32(x)
  63. #define snprintf _snprintf
  64. #ifndef __MINGW32__
  65. /* no config.h with VC */
  66. #define CONFIG_ENCODERS 1
  67. #define CONFIG_DECODERS 1
  68. #define CONFIG_AC3 1
  69. #endif
  70. #else
  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. #ifdef __FreeBSD__
  86. #include <sys/param.h>
  87. #endif
  88. #ifndef INT64_C
  89. #define INT64_C(c) (c ## LL)
  90. #define UINT64_C(c) (c ## ULL)
  91. #endif
  92. #include "../bswap.h"
  93. #ifdef USE_FASTMEMCPY
  94. #include "fastmemcpy.h"
  95. #endif
  96. #endif /* HAVE_AV_CONFIG_H */
  97. #endif /* !CONFIG_WIN32 */
  98. /* debug stuff */
  99. #ifdef HAVE_AV_CONFIG_H
  100. #ifndef DEBUG
  101. #define NDEBUG
  102. #endif
  103. #include <assert.h>
  104. /* dprintf macros */
  105. #if defined(CONFIG_WIN32) && !defined(__MINGW32__)
  106. inline void dprintf(const char* fmt,...) {}
  107. #else
  108. #ifdef DEBUG
  109. #define dprintf(fmt,args...) printf(fmt, ## args)
  110. #else
  111. #define dprintf(fmt,args...)
  112. #endif
  113. #endif /* !CONFIG_WIN32 */
  114. #endif /* HAVE_AV_CONFIG_H */
  115. /* bit output */
  116. struct PutBitContext;
  117. typedef void (*WriteDataFunc)(void *, UINT8 *, int);
  118. typedef struct PutBitContext {
  119. UINT32 bit_buf;
  120. int bit_cnt;
  121. UINT8 *buf, *buf_ptr, *buf_end;
  122. INT64 data_out_size; /* in bytes */
  123. void *opaque;
  124. WriteDataFunc write_data;
  125. } PutBitContext;
  126. void init_put_bits(PutBitContext *s,
  127. UINT8 *buffer, int buffer_size,
  128. void *opaque,
  129. void (*write_data)(void *, UINT8 *, int));
  130. void put_bits(PutBitContext *s, int n, unsigned int value);
  131. INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
  132. void align_put_bits(PutBitContext *s);
  133. void flush_put_bits(PutBitContext *s);
  134. /* jpeg specific put_bits */
  135. void jput_bits(PutBitContext *s, int n, unsigned int value);
  136. void jflush_put_bits(PutBitContext *s);
  137. /* bit input */
  138. typedef struct GetBitContext {
  139. #ifdef ALT_BITSTREAM_READER
  140. int index;
  141. UINT8 *buffer;
  142. #else
  143. UINT32 bit_buf;
  144. int bit_cnt;
  145. UINT8 *buf, *buf_ptr, *buf_end;
  146. #endif
  147. } GetBitContext;
  148. typedef struct VLC {
  149. int bits;
  150. INT16 *table_codes;
  151. INT8 *table_bits;
  152. int table_size, table_allocated;
  153. } VLC;
  154. /* used to avoid missaligned exceptions on some archs (alpha, ...) */
  155. #ifdef ARCH_X86
  156. #define unaligned32(a) (*(UINT32*)(a))
  157. #else
  158. #ifdef __GNUC__
  159. static inline uint32_t unaligned32(const void *v) {
  160. struct Unaligned {
  161. uint32_t i;
  162. } __attribute__((packed));
  163. return ((const struct Unaligned *) v)->i;
  164. }
  165. #elif defined(__DECC)
  166. static inline uint32_t unaligned32(const void *v) {
  167. return *(const __unaligned uint32_t *) v;
  168. }
  169. #else
  170. static inline uint32_t unaligned32(const void *v) {
  171. return *(const uint32_t *) v;
  172. }
  173. #endif
  174. #endif //!ARCH_X86
  175. void init_get_bits(GetBitContext *s,
  176. UINT8 *buffer, int buffer_size);
  177. #ifndef ALT_BITSTREAM_READER
  178. unsigned int get_bits_long(GetBitContext *s, int n);
  179. unsigned int show_bits_long(GetBitContext *s, int n);
  180. #endif
  181. static inline unsigned int get_bits(GetBitContext *s, int n){
  182. #ifdef ALT_BITSTREAM_READER
  183. #ifdef ALIGNED_BITSTREAM
  184. int index= s->index;
  185. uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  186. uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  187. #ifdef ARCH_X86
  188. asm ("shldl %%cl, %2, %0\n\t"
  189. : "=r" (result1)
  190. : "0" (result1), "r" (result2), "c" (index));
  191. #else
  192. result1<<= (index&0x1F);
  193. result2= (result2>>1) >> (31-(index&0x1F));
  194. result1|= result2;
  195. #endif
  196. result1>>= 32 - n;
  197. index+= n;
  198. s->index= index;
  199. return result1;
  200. #else //ALIGNED_BITSTREAM
  201. int index= s->index;
  202. uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
  203. result<<= (index&0x07);
  204. result>>= 32 - n;
  205. index+= n;
  206. s->index= index;
  207. return result;
  208. #endif //!ALIGNED_BITSTREAM
  209. #else //ALT_BITSTREAM_READER
  210. if(s->bit_cnt>=n){
  211. /* most common case here */
  212. unsigned int val = s->bit_buf >> (32 - n);
  213. s->bit_buf <<= n;
  214. s->bit_cnt -= n;
  215. #ifdef STATS
  216. st_bit_counts[st_current_index] += n;
  217. #endif
  218. return val;
  219. }
  220. return get_bits_long(s,n);
  221. #endif //!ALT_BITSTREAM_READER
  222. }
  223. static inline unsigned int get_bits1(GetBitContext *s){
  224. #ifdef ALT_BITSTREAM_READER
  225. int index= s->index;
  226. uint8_t result= s->buffer[ index>>3 ];
  227. result<<= (index&0x07);
  228. result>>= 8 - 1;
  229. index++;
  230. s->index= index;
  231. return result;
  232. #else
  233. if(s->bit_cnt>0){
  234. /* most common case here */
  235. unsigned int val = s->bit_buf >> 31;
  236. s->bit_buf <<= 1;
  237. s->bit_cnt--;
  238. #ifdef STATS
  239. st_bit_counts[st_current_index]++;
  240. #endif
  241. return val;
  242. }
  243. return get_bits_long(s,1);
  244. #endif
  245. }
  246. /* This function is identical to get_bits(), the only */
  247. /* diference is that it doesn't touch the buffer */
  248. /* it is usefull to see the buffer. */
  249. static inline unsigned int show_bits(GetBitContext *s, int n)
  250. {
  251. #ifdef ALT_BITSTREAM_READER
  252. #ifdef ALIGNED_BITSTREAM
  253. int index= s->index;
  254. uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  255. uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  256. #ifdef ARCH_X86
  257. asm ("shldl %%cl, %2, %0\n\t"
  258. : "=r" (result1)
  259. : "0" (result1), "r" (result2), "c" (index));
  260. #else
  261. result1<<= (index&0x1F);
  262. result2= (result2>>1) >> (31-(index&0x1F));
  263. result1|= result2;
  264. #endif
  265. result1>>= 32 - n;
  266. return result1;
  267. #else //ALIGNED_BITSTREAM
  268. int index= s->index;
  269. uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
  270. result<<= (index&0x07);
  271. result>>= 32 - n;
  272. return result;
  273. #endif //!ALIGNED_BITSTREAM
  274. #else //ALT_BITSTREAM_READER
  275. if(s->bit_cnt>=n) {
  276. /* most common case here */
  277. unsigned int val = s->bit_buf >> (32 - n);
  278. return val;
  279. }
  280. return show_bits_long(s,n);
  281. #endif //!ALT_BITSTREAM_READER
  282. }
  283. static inline void skip_bits(GetBitContext *s, int n){
  284. #ifdef ALT_BITSTREAM_READER
  285. s->index+= n;
  286. #else
  287. if(s->bit_cnt>=n){
  288. /* most common case here */
  289. s->bit_buf <<= n;
  290. s->bit_cnt -= n;
  291. #ifdef STATS
  292. st_bit_counts[st_current_index] += n;
  293. #endif
  294. } else {
  295. get_bits_long(s,n);
  296. }
  297. #endif
  298. }
  299. static inline void skip_bits1(GetBitContext *s){
  300. #ifdef ALT_BITSTREAM_READER
  301. s->index++;
  302. #else
  303. if(s->bit_cnt>0){
  304. /* most common case here */
  305. s->bit_buf <<= 1;
  306. s->bit_cnt--;
  307. #ifdef STATS
  308. st_bit_counts[st_current_index]++;
  309. #endif
  310. } else {
  311. get_bits_long(s,1);
  312. }
  313. #endif
  314. }
  315. static inline int get_bits_count(GetBitContext *s)
  316. {
  317. #ifdef ALT_BITSTREAM_READER
  318. return s->index;
  319. #else
  320. return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
  321. #endif
  322. }
  323. void align_get_bits(GetBitContext *s);
  324. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  325. const void *bits, int bits_wrap, int bits_size,
  326. const void *codes, int codes_wrap, int codes_size);
  327. void free_vlc(VLC *vlc);
  328. #ifdef ALT_BITSTREAM_READER
  329. #ifdef ALIGNED_BITSTREAM
  330. #ifdef ARCH_X86
  331. #define SHOW_BITS(s, val, n) \
  332. val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
  333. {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
  334. asm ("shldl %%cl, %2, %0\n\t"\
  335. : "=r" (val)\
  336. : "0" (val), "r" (result2), "c" (bit_cnt));\
  337. ((uint32_t)val)>>= 32 - n;}
  338. #else //ARCH_X86
  339. #define SHOW_BITS(s, val, n) \
  340. val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
  341. {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
  342. val<<= (bit_cnt&0x1F);\
  343. result2= (result2>>1) >> (31-(bit_cnt&0x1F));\
  344. val|= result2;\
  345. ((uint32_t)val)>>= 32 - n;}
  346. #endif //!ARCH_X86
  347. #else //ALIGNED_BITSTREAM
  348. #define SHOW_BITS(s, val, n) \
  349. val= be2me_32( unaligned32( ((uint8_t *)(s)->buffer)+(bit_cnt>>3) ) );\
  350. val<<= (bit_cnt&0x07);\
  351. ((uint32_t)val)>>= 32 - n;
  352. #endif // !ALIGNED_BITSTREAM
  353. #define FLUSH_BITS(n) bit_cnt+=n;
  354. #define SAVE_BITS(s) bit_cnt= (s)->index;
  355. #define RESTORE_BITS(s) (s)->index= bit_cnt;
  356. #else
  357. /* macro to go faster */
  358. /* n must be <= 24 */
  359. /* XXX: optimize buffer end test */
  360. #define SHOW_BITS(s, val, n)\
  361. {\
  362. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  363. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  364. bit_cnt += 8;\
  365. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  366. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  367. bit_cnt += 8;\
  368. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  369. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  370. bit_cnt += 8;\
  371. }\
  372. }\
  373. }\
  374. val = bit_buf >> (32 - n);\
  375. }
  376. /* SHOW_BITS with n1 >= n must be been done before */
  377. #define FLUSH_BITS(n)\
  378. {\
  379. bit_buf <<= n;\
  380. bit_cnt -= n;\
  381. }
  382. #define SAVE_BITS(s) \
  383. {\
  384. bit_cnt = (s)->bit_cnt;\
  385. bit_buf = (s)->bit_buf;\
  386. buf_ptr = (s)->buf_ptr;\
  387. }
  388. #define RESTORE_BITS(s) \
  389. {\
  390. (s)->buf_ptr = buf_ptr;\
  391. (s)->bit_buf = bit_buf;\
  392. (s)->bit_cnt = bit_cnt;\
  393. }
  394. #endif // !ALT_BITSTREAM_READER
  395. static inline int get_vlc(GetBitContext *s, VLC *vlc)
  396. {
  397. int code, n, nb_bits, index;
  398. INT16 *table_codes;
  399. INT8 *table_bits;
  400. int bit_cnt;
  401. #ifndef ALT_BITSTREAM_READER
  402. UINT32 bit_buf;
  403. UINT8 *buf_ptr;
  404. #endif
  405. SAVE_BITS(s);
  406. nb_bits = vlc->bits;
  407. table_codes = vlc->table_codes;
  408. table_bits = vlc->table_bits;
  409. #ifdef FAST_GET_FIRST_VLC
  410. SHOW_BITS(s, index, nb_bits);
  411. code = table_codes[index];
  412. n = table_bits[index];
  413. if (n > 0) {
  414. /* most common case (90%)*/
  415. FLUSH_BITS(n);
  416. RESTORE_BITS(s);
  417. return code;
  418. } else if (n == 0) {
  419. return -1;
  420. } else {
  421. FLUSH_BITS(nb_bits);
  422. nb_bits = -n;
  423. table_codes = vlc->table_codes + code;
  424. table_bits = vlc->table_bits + code;
  425. }
  426. #endif
  427. for(;;) {
  428. SHOW_BITS(s, index, nb_bits);
  429. code = table_codes[index];
  430. n = table_bits[index];
  431. if (n > 0) {
  432. /* most common case */
  433. FLUSH_BITS(n);
  434. #ifdef STATS
  435. st_bit_counts[st_current_index] += n;
  436. #endif
  437. break;
  438. } else if (n == 0) {
  439. return -1;
  440. } else {
  441. FLUSH_BITS(nb_bits);
  442. #ifdef STATS
  443. st_bit_counts[st_current_index] += nb_bits;
  444. #endif
  445. nb_bits = -n;
  446. table_codes = vlc->table_codes + code;
  447. table_bits = vlc->table_bits + code;
  448. }
  449. }
  450. RESTORE_BITS(s);
  451. return code;
  452. }
  453. /* define it to include statistics code (useful only for optimizing
  454. codec efficiency */
  455. //#define STATS
  456. #ifdef STATS
  457. enum {
  458. ST_UNKNOWN,
  459. ST_DC,
  460. ST_INTRA_AC,
  461. ST_INTER_AC,
  462. ST_INTRA_MB,
  463. ST_INTER_MB,
  464. ST_MV,
  465. ST_NB,
  466. };
  467. extern int st_current_index;
  468. extern unsigned int st_bit_counts[ST_NB];
  469. extern unsigned int st_out_bit_counts[ST_NB];
  470. void print_stats(void);
  471. #endif
  472. /* misc math functions */
  473. static inline int av_log2(unsigned int v)
  474. {
  475. int n;
  476. n = 0;
  477. if (v & 0xffff0000) {
  478. v >>= 16;
  479. n += 16;
  480. }
  481. if (v & 0xff00) {
  482. v >>= 8;
  483. n += 8;
  484. }
  485. if (v & 0xf0) {
  486. v >>= 4;
  487. n += 4;
  488. }
  489. if (v & 0xc) {
  490. v >>= 2;
  491. n += 2;
  492. }
  493. if (v & 0x2) {
  494. n++;
  495. }
  496. return n;
  497. }
  498. /* memory */
  499. void *av_mallocz(int size);
  500. #endif