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.

562 lines
12KB

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