Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ ab35de18

History | View | Annotate | Download (31.4 KB)

1 983e3246 Michael Niedermayer
/**
2
 * @file common.h
3
 * common internal api header.
4
 */
5
6 de6d9b64 Fabrice Bellard
#ifndef COMMON_H
7
#define COMMON_H
8
9 4bdd9157 Nick Kurshev
#if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
10 9b59c92f Michael Niedermayer
#    define CONFIG_WIN32
11 1a565432 Fabrice Bellard
#endif
12
13 17592475 Michael Niedermayer
//#define ALT_BITSTREAM_WRITER
14 4f0f8bfc Michael Niedermayer
//#define ALIGNED_BITSTREAM_WRITER
15 3530320d Falk Hüffner
16 8db1a1dd Michael Niedermayer
#define ALT_BITSTREAM_READER
17
//#define LIBMPEG2_BITSTREAM_READER
18
//#define A32_BITSTREAM_READER
19 d8e00c09 BERO
#define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
20 8db1a1dd Michael Niedermayer
21 e0c53ac8 Alex Beregszaszi
#ifndef M_PI
22
#define M_PI    3.14159265358979323846
23
#endif
24
25 420b073b Fabrice Bellard
#ifdef HAVE_AV_CONFIG_H
26 1a565432 Fabrice Bellard
/* only include the following when compiling package */
27 9b59c92f Michael Niedermayer
#    include "config.h"
28
29
#    include <stdlib.h>
30
#    include <stdio.h>
31
#    include <string.h>
32 56c4a184 Fabrice Bellard
#    include <ctype.h>
33 9ff18a70 Michael Niedermayer
#    include <limits.h>
34 9b59c92f Michael Niedermayer
#    ifndef __BEOS__
35
#        include <errno.h>
36
#    else
37
#        include "berrno.h"
38
#    endif
39
#    include <math.h>
40
41
#    ifndef ENODATA
42
#        define ENODATA  61
43
#    endif
44 1a565432 Fabrice Bellard
45 fe1b62fb Zdenek Kabelac
#include <stddef.h>
46
#ifndef offsetof
47
# define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
48
#endif
49
50
#define AVOPTION_CODEC_BOOL(name, help, field) \
51
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
52 5d4ce457 Zdenek Kabelac
#define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
53
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
54 fe1b62fb Zdenek Kabelac
#define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
55
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
56
#define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
57
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
58
#define AVOPTION_CODEC_STRING(name, help, field, str, val) \
59
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
60
#define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
61
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
62 bec89a84 Zdenek Kabelac
#define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
63 fe1b62fb Zdenek Kabelac
#define AVOPTION_END() AVOPTION_SUB(NULL)
64
65 a77146ab Zdenek Kabelac
struct AVOption;
66
#ifdef HAVE_MMX
67
extern const struct AVOption avoptions_common[3 + 5];
68
#else
69
extern const struct AVOption avoptions_common[3];
70
#endif
71
extern const struct AVOption avoptions_workaround_bug[11];
72
73 44f27b3a Fabrice Bellard
#endif /* HAVE_AV_CONFIG_H */
74 1a565432 Fabrice Bellard
75 02da51ec Falk Hüffner
/* Suppress restrict if it was not defined in config.h.  */
76
#ifndef restrict
77 9b59c92f Michael Niedermayer
#    define restrict
78 02da51ec Falk Hüffner
#endif
79
80 f4ae934a Alex Beregszaszi
#ifndef always_inline
81 d200cab6 Falk Hüffner
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
82 9b59c92f Michael Niedermayer
#    define always_inline __attribute__((always_inline)) inline
83 d200cab6 Falk Hüffner
#else
84 9b59c92f Michael Niedermayer
#    define always_inline inline
85 d200cab6 Falk Hüffner
#endif
86 f4ae934a Alex Beregszaszi
#endif
87 d200cab6 Falk Hüffner
88 f4ae934a Alex Beregszaszi
#ifndef attribute_used
89 5c0513bd Dmitry Baryshkov
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
90
#    define attribute_used __attribute__((used))
91
#else
92
#    define attribute_used
93
#endif
94 f4ae934a Alex Beregszaszi
#endif
95 5c0513bd Dmitry Baryshkov
96 d2a9bddd Michael Niedermayer
#ifndef EMULATE_INTTYPES
97 7f965c1c Chris Flerackers
#   include <inttypes.h>
98
#else
99
    typedef signed char  int8_t;
100
    typedef signed short int16_t;
101
    typedef signed int   int32_t;
102
    typedef unsigned char  uint8_t;
103
    typedef unsigned short uint16_t;
104
    typedef unsigned int   uint32_t;
105
106
#   ifdef CONFIG_WIN32
107
        typedef signed __int64   int64_t;
108
        typedef unsigned __int64 uint64_t;
109
#   else /* other OS */
110
        typedef signed long long   int64_t;
111
        typedef unsigned long long uint64_t;
112
#   endif /* other OS */
113
#endif /* HAVE_INTTYPES_H */
114
115 9ff5f175 Michael Niedermayer
#ifndef INT64_MAX
116 2a24fe4c Gael Chardon
#define INT64_MAX int64_t_C(9223372036854775807)
117 9ff5f175 Michael Niedermayer
#endif
118
119 caa50878 Michael Niedermayer
#ifndef UINT64_MAX
120
#define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
121
#endif
122
123 19d053c5 Roman Shaposhnik
#ifdef EMULATE_FAST_INT
124
/* note that we don't emulate 64bit ints */
125
typedef signed char int_fast8_t;
126
typedef signed int  int_fast16_t;
127
typedef signed int  int_fast32_t;
128
typedef unsigned char uint_fast8_t;
129
typedef unsigned int  uint_fast16_t;
130
typedef unsigned int  uint_fast32_t;
131
#endif
132
133 0ff93477 Michael Niedermayer
#ifndef INT_BIT
134 9ff18a70 Michael Niedermayer
#    if INT_MAX != 2147483647
135 0ff93477 Michael Niedermayer
#        define INT_BIT 64
136
#    else
137
#        define INT_BIT 32
138
#    endif
139
#endif
140
141 19d053c5 Roman Shaposhnik
#if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
142
static inline float floorf(float f) { 
143
    return floor(f); 
144
}
145
#endif
146
147 1a565432 Fabrice Bellard
#ifdef CONFIG_WIN32
148
149
/* windows */
150
151 5c668f46 Sascha Sommer
#    if !defined(__MINGW32__) && !defined(__CYGWIN__)
152 0c1a9eda Zdenek Kabelac
#        define int64_t_C(c)     (c ## i64)
153
#        define uint64_t_C(c)    (c ## i64)
154 1a565432 Fabrice Bellard
155 49e17f84 Michael Niedermayer
#    ifdef HAVE_AV_CONFIG_H
156
#            define inline __inline
157
#    endif
158 1a565432 Fabrice Bellard
159 9b59c92f Michael Niedermayer
#    else
160 0c1a9eda Zdenek Kabelac
#        define int64_t_C(c)     (c ## LL)
161
#        define uint64_t_C(c)    (c ## ULL)
162 9b59c92f Michael Niedermayer
#    endif /* __MINGW32__ */
163 a74127c0 Fabrice Bellard
164 49e17f84 Michael Niedermayer
#    ifdef HAVE_AV_CONFIG_H
165
#        ifdef _DEBUG
166
#            define DEBUG
167
#        endif
168 de6d9b64 Fabrice Bellard
169 49e17f84 Michael Niedermayer
#        define snprintf _snprintf
170
#        define vsnprintf _vsnprintf
171
#    endif
172 1a565432 Fabrice Bellard
173 f3ec2d46 Slavik Gnatenko
/* CONFIG_WIN32 end */
174
#elif defined (CONFIG_OS2)
175
/* OS/2 EMX */
176
177 0c1a9eda Zdenek Kabelac
#ifndef int64_t_C
178
#define int64_t_C(c)     (c ## LL)
179
#define uint64_t_C(c)    (c ## ULL)
180 f3ec2d46 Slavik Gnatenko
#endif
181
182 f5fe9d5f Fabrice Bellard
#ifdef HAVE_AV_CONFIG_H
183
184 f3ec2d46 Slavik Gnatenko
#ifdef USE_FASTMEMCPY
185
#include "fastmemcpy.h"
186
#endif
187
188
#include <float.h>
189
190
#endif /* HAVE_AV_CONFIG_H */
191
192
/* CONFIG_OS2 end */
193
#else
194 1a565432 Fabrice Bellard
195
/* unix */
196
197 f5fe9d5f Fabrice Bellard
#ifndef int64_t_C
198
#define int64_t_C(c)     (c ## LL)
199
#define uint64_t_C(c)    (c ## ULL)
200
#endif
201
202
#ifdef HAVE_AV_CONFIG_H
203 1a565432 Fabrice Bellard
204 9b59c92f Michael Niedermayer
#        ifdef USE_FASTMEMCPY
205
#            include "fastmemcpy.h"
206
#        endif
207
#    endif /* HAVE_AV_CONFIG_H */
208 a74127c0 Fabrice Bellard
209 f3ec2d46 Slavik Gnatenko
#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
210 a74127c0 Fabrice Bellard
211
#ifdef HAVE_AV_CONFIG_H
212
213 9b59c92f Michael Niedermayer
#    include "bswap.h"
214 96707bb7 Fabrice Bellard
215 9b59c92f Michael Niedermayer
#    if defined(__MINGW32__) || defined(__CYGWIN__) || \
216 814b648c Michael Niedermayer
        defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
217 9b59c92f Michael Niedermayer
#        define MANGLE(a) "_" #a
218
#    else
219
#        define MANGLE(a) #a
220
#    endif
221 6a3d7e36 Fabrice Bellard
222 d503f970 Fabrice Bellard
/* debug stuff */
223
224 9b59c92f Michael Niedermayer
#    ifndef DEBUG
225
#        define NDEBUG
226
#    endif
227
#    include <assert.h>
228 1a565432 Fabrice Bellard
229 a74127c0 Fabrice Bellard
/* dprintf macros */
230 2a86d50b Sascha Sommer
#    if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
231 a74127c0 Fabrice Bellard
232
inline void dprintf(const char* fmt,...) {}
233
234 9b59c92f Michael Niedermayer
#    else
235 a74127c0 Fabrice Bellard
236 9b59c92f Michael Niedermayer
#        ifdef DEBUG
237 365e75f8 Michael Niedermayer
#            define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
238 9b59c92f Michael Niedermayer
#        else
239 dc7cb06a Dan Christiansen
#            define dprintf(fmt,...)
240 9b59c92f Michael Niedermayer
#        endif
241 1a565432 Fabrice Bellard
242 9b59c92f Michael Niedermayer
#    endif /* !CONFIG_WIN32 */
243 1a565432 Fabrice Bellard
244 9b879566 Michel Bardiaux
#    define av_abort()      do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
245 935cdf09 Philip Gladstone
246 073b013d Michael Niedermayer
//rounded divison & shift
247 10f3005f Michael Niedermayer
#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
248 d7e9533a Michael Niedermayer
/* assume b>0 */
249
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
250 32cd20de Michael Niedermayer
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
251 75460b0c Michael Niedermayer
252 b8a78f41 Michael Niedermayer
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
253
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
254 d7e9533a Michael Niedermayer
255 d4961b35 BERO
extern const uint32_t inverse[256];
256
257
#ifdef ARCH_X86
258
#    define FASTDIV(a,b) \
259
    ({\
260
        int ret,dmy;\
261
        asm volatile(\
262
            "mull %3"\
263
            :"=d"(ret),"=a"(dmy)\
264
            :"1"(a),"g"(inverse[b])\
265
            );\
266
        ret;\
267
    })
268
#elif defined(CONFIG_FASTDIV)
269
#    define FASTDIV(a,b)   ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
270
#else
271
#    define FASTDIV(a,b)   ((a)/(b))
272
#endif
273
 
274 ef5e74bb Michael Niedermayer
#ifdef ARCH_X86
275
// avoid +32 for shift optimization (gcc should do that ...)
276
static inline  int32_t NEG_SSR32( int32_t a, int8_t s){
277
    asm ("sarl %1, %0\n\t"
278
         : "+r" (a)
279
         : "ic" ((uint8_t)(-s))
280
    );
281
    return a;
282
}
283
static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
284
    asm ("shrl %1, %0\n\t"
285
         : "+r" (a)
286
         : "ic" ((uint8_t)(-s))
287
    );
288
    return a;
289
}
290 8db1a1dd Michael Niedermayer
#else
291 9b59c92f Michael Niedermayer
#    define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
292
#    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
293 8db1a1dd Michael Niedermayer
#endif
294
295 de6d9b64 Fabrice Bellard
/* bit output */
296
297 58254257 Roman Shaposhnik
/* buf and buf_end must be present and used by every alternative writer. */
298 de6d9b64 Fabrice Bellard
typedef struct PutBitContext {
299 17592475 Michael Niedermayer
#ifdef ALT_BITSTREAM_WRITER
300 0c1a9eda Zdenek Kabelac
    uint8_t *buf, *buf_end;
301 17592475 Michael Niedermayer
    int index;
302
#else
303 0c1a9eda Zdenek Kabelac
    uint32_t bit_buf;
304 d8cf5aea Michael Niedermayer
    int bit_left;
305 0c1a9eda Zdenek Kabelac
    uint8_t *buf, *buf_ptr, *buf_end;
306 17592475 Michael Niedermayer
#endif
307 de6d9b64 Fabrice Bellard
} PutBitContext;
308
309 e7a18d5d Roman Shaposhnik
static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
310
{
311
    s->buf = buffer;
312
    s->buf_end = s->buf + buffer_size;
313
#ifdef ALT_BITSTREAM_WRITER
314
    s->index=0;
315
    ((uint32_t*)(s->buf))[0]=0;
316
//    memset(buffer, 0, buffer_size);
317
#else
318
    s->buf_ptr = s->buf;
319
    s->bit_left=32;
320
    s->bit_buf=0;
321
#endif
322
}
323
324
/* return the number of bits output */
325
static inline int put_bits_count(PutBitContext *s)
326
{
327
#ifdef ALT_BITSTREAM_WRITER
328
    return s->index;
329
#else
330
    return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
331
#endif
332
}
333
334
/* pad the end of the output stream with zeros */
335
static inline void flush_put_bits(PutBitContext *s)
336
{
337
#ifdef ALT_BITSTREAM_WRITER
338
    align_put_bits(s);
339
#else
340
    s->bit_buf<<= s->bit_left;
341
    while (s->bit_left < 32) {
342
        /* XXX: should test end of buffer */
343
        *s->buf_ptr++=s->bit_buf >> 24;
344
        s->bit_buf<<=8;
345
        s->bit_left+=8;
346
    }
347
    s->bit_left=32;
348
    s->bit_buf=0;
349
#endif
350
}
351 17592475 Michael Niedermayer
352 de6d9b64 Fabrice Bellard
void align_put_bits(PutBitContext *s);
353 99683a30 Michael Niedermayer
void put_string(PutBitContext * pbc, char *s, int put_zero);
354 de6d9b64 Fabrice Bellard
355
/* bit input */
356 58254257 Roman Shaposhnik
/* buffer, buffer_end and size_in_bits must be present and used by every reader */
357 de6d9b64 Fabrice Bellard
typedef struct GetBitContext {
358 ae39132d Zdenek Kabelac
    const uint8_t *buffer, *buffer_end;
359 27a3e2c5 Michael Niedermayer
#ifdef ALT_BITSTREAM_READER
360
    int index;
361 8db1a1dd Michael Niedermayer
#elif defined LIBMPEG2_BITSTREAM_READER
362 0c1a9eda Zdenek Kabelac
    uint8_t *buffer_ptr;
363
    uint32_t cache;
364 8db1a1dd Michael Niedermayer
    int bit_count;
365
#elif defined A32_BITSTREAM_READER
366 0c1a9eda Zdenek Kabelac
    uint32_t *buffer_ptr;
367
    uint32_t cache0;
368
    uint32_t cache1;
369 8db1a1dd Michael Niedermayer
    int bit_count;
370 27a3e2c5 Michael Niedermayer
#endif
371 68f593b4 Michael Niedermayer
    int size_in_bits;
372 de6d9b64 Fabrice Bellard
} GetBitContext;
373
374 0c1a9eda Zdenek Kabelac
#define VLC_TYPE int16_t
375 8db1a1dd Michael Niedermayer
376 de6d9b64 Fabrice Bellard
typedef struct VLC {
377
    int bits;
378 64863965 Michael Niedermayer
    VLC_TYPE (*table)[2]; ///< code, bits
379 de6d9b64 Fabrice Bellard
    int table_size, table_allocated;
380
} VLC;
381
382 45a82edb Michael Niedermayer
typedef struct RL_VLC_ELEM {
383
    int16_t level;
384
    int8_t len;
385
    uint8_t run;
386
} RL_VLC_ELEM;
387
388 bb476ff3 James Morrison
#ifdef ARCH_SPARC
389 35fedfc3 Philip Gladstone
#define UNALIGNED_STORES_ARE_BAD
390
#endif
391
392 3587d501 Michael Niedermayer
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
393
#ifdef ARCH_X86
394 0c1a9eda Zdenek Kabelac
#    define unaligned32(a) (*(uint32_t*)(a))
395 3587d501 Michael Niedermayer
#else
396 9b59c92f Michael Niedermayer
#    ifdef __GNUC__
397 3587d501 Michael Niedermayer
static inline uint32_t unaligned32(const void *v) {
398
    struct Unaligned {
399
        uint32_t i;
400
    } __attribute__((packed));
401
402
    return ((const struct Unaligned *) v)->i;
403
}
404 9b59c92f Michael Niedermayer
#    elif defined(__DECC)
405 3587d501 Michael Niedermayer
static inline uint32_t unaligned32(const void *v) {
406
    return *(const __unaligned uint32_t *) v;
407
}
408 9b59c92f Michael Niedermayer
#    else
409 3587d501 Michael Niedermayer
static inline uint32_t unaligned32(const void *v) {
410
    return *(const uint32_t *) v;
411
}
412 9b59c92f Michael Niedermayer
#    endif
413 3587d501 Michael Niedermayer
#endif //!ARCH_X86
414
415 d8cf5aea Michael Niedermayer
#ifndef ALT_BITSTREAM_WRITER
416
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
417
{
418
    unsigned int bit_buf;
419
    int bit_left;
420
421
#ifdef STATS
422
    st_out_bit_counts[st_current_index] += n;
423
#endif
424
    //    printf("put_bits=%d %x\n", n, value);
425
    assert(n == 32 || value < (1U << n));
426 fcb609f4 Michael Niedermayer
    
427 d8cf5aea Michael Niedermayer
    bit_buf = s->bit_buf;
428
    bit_left = s->bit_left;
429
430
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
431
    /* XXX: optimize */
432
    if (n < bit_left) {
433
        bit_buf = (bit_buf<<n) | value;
434
        bit_left-=n;
435
    } else {
436
        bit_buf<<=bit_left;
437
        bit_buf |= value >> (n - bit_left);
438 35fedfc3 Philip Gladstone
#ifdef UNALIGNED_STORES_ARE_BAD
439 bb476ff3 James Morrison
        if (3 & (intptr_t) s->buf_ptr) {
440 35fedfc3 Philip Gladstone
            s->buf_ptr[0] = bit_buf >> 24;
441
            s->buf_ptr[1] = bit_buf >> 16;
442
            s->buf_ptr[2] = bit_buf >>  8;
443
            s->buf_ptr[3] = bit_buf      ;
444
        } else
445
#endif
446 0c1a9eda Zdenek Kabelac
        *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
447 d8cf5aea Michael Niedermayer
        //printf("bitbuf = %08x\n", bit_buf);
448
        s->buf_ptr+=4;
449
        bit_left+=32 - n;
450
        bit_buf = value;
451
    }
452
453
    s->bit_buf = bit_buf;
454
    s->bit_left = bit_left;
455
}
456
#endif
457
458
459 17592475 Michael Niedermayer
#ifdef ALT_BITSTREAM_WRITER
460 4f0f8bfc Michael Niedermayer
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
461 17592475 Michael Niedermayer
{
462 9b59c92f Michael Niedermayer
#    ifdef ALIGNED_BITSTREAM_WRITER
463
#        ifdef ARCH_X86
464 4f0f8bfc Michael Niedermayer
    asm volatile(
465
        "movl %0, %%ecx                        \n\t"
466
        "xorl %%eax, %%eax                \n\t"
467
        "shrdl %%cl, %1, %%eax                \n\t"
468
        "shrl %%cl, %1                        \n\t"
469
        "movl %0, %%ecx                        \n\t"
470
        "shrl $3, %%ecx                        \n\t"
471
        "andl $0xFFFFFFFC, %%ecx        \n\t"
472
        "bswapl %1                        \n\t"
473
        "orl %1, (%2, %%ecx)                \n\t"
474
        "bswapl %%eax                        \n\t"
475
        "addl %3, %0                        \n\t"
476
        "movl %%eax, 4(%2, %%ecx)        \n\t"
477
        : "=&r" (s->index), "=&r" (value)
478
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
479
        : "%eax", "%ecx"
480
    );
481 9b59c92f Michael Niedermayer
#        else
482 4f0f8bfc Michael Niedermayer
    int index= s->index;
483
    uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
484
    
485
    value<<= 32-n; 
486
    
487
    ptr[0] |= be2me_32(value>>(index&31));
488
    ptr[1]  = be2me_32(value<<(32-(index&31)));
489
//if(n>24) printf("%d %d\n", n, value);
490
    index+= n;
491
    s->index= index;
492 9b59c92f Michael Niedermayer
#        endif
493
#    else //ALIGNED_BITSTREAM_WRITER
494
#        ifdef ARCH_X86
495 17592475 Michael Niedermayer
    asm volatile(
496
        "movl $7, %%ecx                        \n\t"
497
        "andl %0, %%ecx                        \n\t"
498
        "addl %3, %%ecx                        \n\t"
499
        "negl %%ecx                        \n\t"
500
        "shll %%cl, %1                        \n\t"
501
        "bswapl %1                        \n\t"
502
        "movl %0, %%ecx                        \n\t"
503
        "shrl $3, %%ecx                        \n\t"
504
        "orl %1, (%%ecx, %2)                \n\t"
505
        "addl %3, %0                        \n\t"
506
        "movl $0, 4(%%ecx, %2)                \n\t"
507
        : "=&r" (s->index), "=&r" (value)
508
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
509
        : "%ecx"
510
    );
511 9b59c92f Michael Niedermayer
#        else
512 17592475 Michael Niedermayer
    int index= s->index;
513
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
514
    
515
    ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
516
    ptr[1] = 0;
517
//if(n>24) printf("%d %d\n", n, value);
518
    index+= n;
519
    s->index= index;
520 9b59c92f Michael Niedermayer
#        endif
521
#    endif //!ALIGNED_BITSTREAM_WRITER
522 17592475 Michael Niedermayer
}
523
#endif
524
525
526
static inline uint8_t* pbBufPtr(PutBitContext *s)
527
{
528
#ifdef ALT_BITSTREAM_WRITER
529
        return s->buf + (s->index>>3);
530
#else
531
        return s->buf_ptr;
532
#endif
533
}
534
535 9c3d33d6 Michael Niedermayer
/**
536
 *
537
 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
538
 */
539
static inline void skip_put_bytes(PutBitContext *s, int n){
540
        assert((put_bits_count(s)&7)==0);
541
#ifdef ALT_BITSTREAM_WRITER
542
        FIXME may need some cleaning of the buffer
543
        s->index += n<<3;
544
#else
545
        assert(s->bit_left==32);
546
        s->buf_ptr += n;
547
#endif    
548
}
549
550
/**
551
 * Changes the end of the buffer.
552
 */
553
static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
554
    s->buf_end= s->buf + size;
555
}
556
557 8db1a1dd Michael Niedermayer
/* Bitstream reader API docs:
558
name
559
    abritary name which is used as prefix for the internal variables
560 de6d9b64 Fabrice Bellard

561 8db1a1dd Michael Niedermayer
gb
562
    getbitcontext
563

564
OPEN_READER(name, gb)
565
    loads gb into local variables
566

567
CLOSE_READER(name, gb)
568
    stores local vars in gb
569

570
UPDATE_CACHE(name, gb)
571
    refills the internal cache from the bitstream
572
    after this call at least MIN_CACHE_BITS will be available,
573

574
GET_CACHE(name, gb)
575
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
576

577
SHOW_UBITS(name, gb, num)
578
    will return the nest num bits
579

580
SHOW_SBITS(name, gb, num)
581
    will return the nest num bits and do sign extension
582

583
SKIP_BITS(name, gb, num)
584
    will skip over the next num bits
585
    note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
586

587
SKIP_CACHE(name, gb, num)
588
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
589

590
SKIP_COUNTER(name, gb, num)
591
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
592

593
LAST_SKIP_CACHE(name, gb, num)
594
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
595

596
LAST_SKIP_BITS(name, gb, num)
597
    is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
598

599
for examples see get_bits, show_bits, skip_bits, get_vlc
600
*/
601 2931ecb9 Arpi
602 d8e00c09 BERO
static inline int unaligned32_be(const void *v)
603
{
604
#ifdef CONFIG_ALIGN
605
        const uint8_t *p=v;
606
        return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
607
#else
608
        return be2me_32( unaligned32(v)); //original
609
#endif
610
}
611
612 27a3e2c5 Michael Niedermayer
#ifdef ALT_BITSTREAM_READER
613 8db1a1dd Michael Niedermayer
#   define MIN_CACHE_BITS 25
614 27a3e2c5 Michael Niedermayer
615 8db1a1dd Michael Niedermayer
#   define OPEN_READER(name, gb)\
616
        int name##_index= (gb)->index;\
617
        int name##_cache= 0;\
618
619
#   define CLOSE_READER(name, gb)\
620
        (gb)->index= name##_index;\
621
622
#   define UPDATE_CACHE(name, gb)\
623 d8e00c09 BERO
        name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
624 8db1a1dd Michael Niedermayer
625
#   define SKIP_CACHE(name, gb, num)\
626
        name##_cache <<= (num);\
627
628
// FIXME name?
629
#   define SKIP_COUNTER(name, gb, num)\
630
        name##_index += (num);\
631
632
#   define SKIP_BITS(name, gb, num)\
633
        {\
634
            SKIP_CACHE(name, gb, num)\
635
            SKIP_COUNTER(name, gb, num)\
636
        }\
637
638
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
639
#   define LAST_SKIP_CACHE(name, gb, num) ;
640
641
#   define SHOW_UBITS(name, gb, num)\
642 ef5e74bb Michael Niedermayer
        NEG_USR32(name##_cache, num)
643 8db1a1dd Michael Niedermayer
644
#   define SHOW_SBITS(name, gb, num)\
645 ef5e74bb Michael Niedermayer
        NEG_SSR32(name##_cache, num)
646 8db1a1dd Michael Niedermayer
647
#   define GET_CACHE(name, gb)\
648
        ((uint32_t)name##_cache)
649
650
static inline int get_bits_count(GetBitContext *s){
651
    return s->index;
652 2931ecb9 Arpi
}
653 8db1a1dd Michael Niedermayer
#elif defined LIBMPEG2_BITSTREAM_READER
654
//libmpeg2 like reader
655
656 3bb10888 Michael Niedermayer
#   define MIN_CACHE_BITS 17
657 8db1a1dd Michael Niedermayer
658
#   define OPEN_READER(name, gb)\
659
        int name##_bit_count=(gb)->bit_count;\
660
        int name##_cache= (gb)->cache;\
661
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
662
663
#   define CLOSE_READER(name, gb)\
664
        (gb)->bit_count= name##_bit_count;\
665
        (gb)->cache= name##_cache;\
666
        (gb)->buffer_ptr= name##_buffer_ptr;\
667
668 d8e00c09 BERO
#ifdef LIBMPEG2_BITSTREAM_READER_HACK
669
670
#   define UPDATE_CACHE(name, gb)\
671
    if(name##_bit_count >= 0){\
672 17fb5fd3 Michael Niedermayer
        name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
673
        ((uint16_t*)name##_buffer_ptr)++;\
674 d8e00c09 BERO
        name##_bit_count-= 16;\
675
    }\
676
677
#else
678
679 8db1a1dd Michael Niedermayer
#   define UPDATE_CACHE(name, gb)\
680 3bb10888 Michael Niedermayer
    if(name##_bit_count >= 0){\
681 8db1a1dd Michael Niedermayer
        name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
682
        name##_buffer_ptr+=2;\
683
        name##_bit_count-= 16;\
684
    }\
685 2931ecb9 Arpi
686 d8e00c09 BERO
#endif
687
688 8db1a1dd Michael Niedermayer
#   define SKIP_CACHE(name, gb, num)\
689
        name##_cache <<= (num);\
690
691
#   define SKIP_COUNTER(name, gb, num)\
692
        name##_bit_count += (num);\
693
694
#   define SKIP_BITS(name, gb, num)\
695
        {\
696
            SKIP_CACHE(name, gb, num)\
697
            SKIP_COUNTER(name, gb, num)\
698
        }\
699
700
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
701
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
702
703
#   define SHOW_UBITS(name, gb, num)\
704 ef5e74bb Michael Niedermayer
        NEG_USR32(name##_cache, num)
705 8db1a1dd Michael Niedermayer
706
#   define SHOW_SBITS(name, gb, num)\
707 ef5e74bb Michael Niedermayer
        NEG_SSR32(name##_cache, num)
708 8db1a1dd Michael Niedermayer
709
#   define GET_CACHE(name, gb)\
710
        ((uint32_t)name##_cache)
711
712
static inline int get_bits_count(GetBitContext *s){
713
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
714 612476ef Arpi
}
715
716 8db1a1dd Michael Niedermayer
#elif defined A32_BITSTREAM_READER
717
718
#   define MIN_CACHE_BITS 32
719
720
#   define OPEN_READER(name, gb)\
721
        int name##_bit_count=(gb)->bit_count;\
722
        uint32_t name##_cache0= (gb)->cache0;\
723
        uint32_t name##_cache1= (gb)->cache1;\
724
        uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
725
726
#   define CLOSE_READER(name, gb)\
727
        (gb)->bit_count= name##_bit_count;\
728
        (gb)->cache0= name##_cache0;\
729
        (gb)->cache1= name##_cache1;\
730
        (gb)->buffer_ptr= name##_buffer_ptr;\
731
732
#   define UPDATE_CACHE(name, gb)\
733
    if(name##_bit_count > 0){\
734
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
735 ef5e74bb Michael Niedermayer
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
736 8db1a1dd Michael Niedermayer
        name##_cache1 |= next<<name##_bit_count;\
737
        name##_buffer_ptr++;\
738
        name##_bit_count-= 32;\
739
    }\
740
741 3587d501 Michael Niedermayer
#ifdef ARCH_X86
742 8db1a1dd Michael Niedermayer
#   define SKIP_CACHE(name, gb, num)\
743
        asm(\
744
            "shldl %2, %1, %0                \n\t"\
745
            "shll %2, %1                \n\t"\
746
            : "+r" (name##_cache0), "+r" (name##_cache1)\
747
            : "Ic" ((uint8_t)num)\
748
           );
749 3587d501 Michael Niedermayer
#else
750 8db1a1dd Michael Niedermayer
#   define SKIP_CACHE(name, gb, num)\
751
        name##_cache0 <<= (num);\
752 ef5e74bb Michael Niedermayer
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
753 8db1a1dd Michael Niedermayer
        name##_cache1 <<= (num);
754 3587d501 Michael Niedermayer
#endif
755 27a3e2c5 Michael Niedermayer
756 8db1a1dd Michael Niedermayer
#   define SKIP_COUNTER(name, gb, num)\
757
        name##_bit_count += (num);\
758
759
#   define SKIP_BITS(name, gb, num)\
760
        {\
761
            SKIP_CACHE(name, gb, num)\
762
            SKIP_COUNTER(name, gb, num)\
763
        }\
764
765
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
766
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
767
768
#   define SHOW_UBITS(name, gb, num)\
769 ef5e74bb Michael Niedermayer
        NEG_USR32(name##_cache0, num)
770 8db1a1dd Michael Niedermayer
771
#   define SHOW_SBITS(name, gb, num)\
772 ef5e74bb Michael Niedermayer
        NEG_SSR32(name##_cache0, num)
773 8db1a1dd Michael Niedermayer
774
#   define GET_CACHE(name, gb)\
775
        (name##_cache0)
776
777
static inline int get_bits_count(GetBitContext *s){
778
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
779 4949028f Juanjo
}
780
781 49092244 Michael Niedermayer
#endif
782
783 17fb5fd3 Michael Niedermayer
/**
784
 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
785
 * if MSB not set it is negative 
786
 * @param n length in bits
787
 * @author BERO  
788
 */
789 d8e00c09 BERO
static inline int get_xbits(GetBitContext *s, int n){
790
    register int tmp;
791
    register int32_t cache;
792
    OPEN_READER(re, s)
793
    UPDATE_CACHE(re, s)
794
    cache = GET_CACHE(re,s);
795
    if ((int32_t)cache<0) { //MSB=1
796
        tmp = NEG_USR32(cache,n);
797
    } else {
798
    //   tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
799
    //   tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
800
        tmp = - NEG_USR32(~cache,n);
801
    }
802
    LAST_SKIP_BITS(re, s, n)
803
    CLOSE_READER(re, s)
804
    return tmp;
805
}
806
807
static inline int get_sbits(GetBitContext *s, int n){
808
    register int tmp;
809
    OPEN_READER(re, s)
810
    UPDATE_CACHE(re, s)
811
    tmp= SHOW_SBITS(re, s, n);
812
    LAST_SKIP_BITS(re, s, n)
813
    CLOSE_READER(re, s)
814
    return tmp;
815
}
816
817 17fb5fd3 Michael Niedermayer
/**
818
 * reads 0-17 bits.
819
 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
820
 */
821 8db1a1dd Michael Niedermayer
static inline unsigned int get_bits(GetBitContext *s, int n){
822
    register int tmp;
823
    OPEN_READER(re, s)
824
    UPDATE_CACHE(re, s)
825
    tmp= SHOW_UBITS(re, s, n);
826
    LAST_SKIP_BITS(re, s, n)
827
    CLOSE_READER(re, s)
828
    return tmp;
829
}
830 49092244 Michael Niedermayer
831 17fb5fd3 Michael Niedermayer
unsigned int get_bits_long(GetBitContext *s, int n);
832
833
/**
834
 * shows 0-17 bits.
835
 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
836
 */
837 8db1a1dd Michael Niedermayer
static inline unsigned int show_bits(GetBitContext *s, int n){
838
    register int tmp;
839
    OPEN_READER(re, s)
840
    UPDATE_CACHE(re, s)
841
    tmp= SHOW_UBITS(re, s, n);
842
//    CLOSE_READER(re, s)
843
    return tmp;
844 49092244 Michael Niedermayer
}
845
846 17fb5fd3 Michael Niedermayer
unsigned int show_bits_long(GetBitContext *s, int n);
847
848 612476ef Arpi
static inline void skip_bits(GetBitContext *s, int n){
849 8db1a1dd Michael Niedermayer
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
850
    OPEN_READER(re, s)
851
    UPDATE_CACHE(re, s)
852
    LAST_SKIP_BITS(re, s, n)
853
    CLOSE_READER(re, s)
854
}
855
856
static inline unsigned int get_bits1(GetBitContext *s){
857 27a3e2c5 Michael Niedermayer
#ifdef ALT_BITSTREAM_READER
858 8db1a1dd Michael Niedermayer
    int index= s->index;
859
    uint8_t result= s->buffer[ index>>3 ];
860
    result<<= (index&0x07);
861
    result>>= 8 - 1;
862
    index++;
863
    s->index= index;
864 fcb609f4 Michael Niedermayer
865 8db1a1dd Michael Niedermayer
    return result;
866 27a3e2c5 Michael Niedermayer
#else
867 8db1a1dd Michael Niedermayer
    return get_bits(s, 1);
868 27a3e2c5 Michael Niedermayer
#endif
869 612476ef Arpi
}
870
871 8db1a1dd Michael Niedermayer
static inline unsigned int show_bits1(GetBitContext *s){
872
    return show_bits(s, 1);
873 612476ef Arpi
}
874
875 8db1a1dd Michael Niedermayer
static inline void skip_bits1(GetBitContext *s){
876
    skip_bits(s, 1);
877 9dbf9389 Fabrice Bellard
}
878 612476ef Arpi
879 e7a18d5d Roman Shaposhnik
/**
880
 * init GetBitContext.
881
 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
882
 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
883
 * @param bit_size the size of the buffer in bits
884
 */
885
static inline void init_get_bits(GetBitContext *s,
886
                   const uint8_t *buffer, int bit_size)
887
{
888
    const int buffer_size= (bit_size+7)>>3;
889
890
    s->buffer= buffer;
891
    s->size_in_bits= bit_size;
892
    s->buffer_end= buffer + buffer_size;
893
#ifdef ALT_BITSTREAM_READER
894
    s->index=0;
895
#elif defined LIBMPEG2_BITSTREAM_READER
896
#ifdef LIBMPEG2_BITSTREAM_READER_HACK
897
  if ((int)buffer&1) {
898
     /* word alignment */
899
    s->cache = (*buffer++)<<24;
900
    s->buffer_ptr = buffer;
901
    s->bit_count = 16-8;
902
  } else
903
#endif
904
  {
905
    s->buffer_ptr = buffer;
906
    s->bit_count = 16;
907
    s->cache = 0;
908
  }
909
#elif defined A32_BITSTREAM_READER
910
    s->buffer_ptr = (uint32_t*)buffer;
911
    s->bit_count = 32;
912
    s->cache0 = 0;
913
    s->cache1 = 0;
914
#endif
915
    {
916
        OPEN_READER(re, s)
917
        UPDATE_CACHE(re, s)
918
        UPDATE_CACHE(re, s)
919
        CLOSE_READER(re, s)
920
    }
921
#ifdef A32_BITSTREAM_READER
922
    s->cache1 = 0;
923
#endif
924
}
925
926 18f77016 Zdenek Kabelac
int check_marker(GetBitContext *s, const char *msg);
927 de6d9b64 Fabrice Bellard
void align_get_bits(GetBitContext *s);
928
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
929
             const void *bits, int bits_wrap, int bits_size,
930
             const void *codes, int codes_wrap, int codes_size);
931
void free_vlc(VLC *vlc);
932
933 68f593b4 Michael Niedermayer
/**
934
 *
935
 * if the vlc code is invalid and max_depth=1 than no bits will be removed
936
 * if the vlc code is invalid and max_depth>1 than the number of bits removed
937
 * is undefined
938
 */
939 cceabc86 Michael Niedermayer
#define GET_VLC(code, name, gb, table, bits, max_depth)\
940
{\
941
    int n, index, nb_bits;\
942
\
943
    index= SHOW_UBITS(name, gb, bits);\
944
    code = table[index][0];\
945
    n    = table[index][1];\
946
\
947
    if(max_depth > 1 && n < 0){\
948
        LAST_SKIP_BITS(name, gb, bits)\
949
        UPDATE_CACHE(name, gb)\
950
\
951
        nb_bits = -n;\
952
\
953 6300c80a Michael Niedermayer
        index= SHOW_UBITS(name, gb, nb_bits) + code;\
954 cceabc86 Michael Niedermayer
        code = table[index][0];\
955
        n    = table[index][1];\
956
        if(max_depth > 2 && n < 0){\
957
            LAST_SKIP_BITS(name, gb, nb_bits)\
958
            UPDATE_CACHE(name, gb)\
959
\
960
            nb_bits = -n;\
961
\
962 6300c80a Michael Niedermayer
            index= SHOW_UBITS(name, gb, nb_bits) + code;\
963 cceabc86 Michael Niedermayer
            code = table[index][0];\
964
            n    = table[index][1];\
965
        }\
966
    }\
967
    SKIP_BITS(name, gb, n)\
968
}
969
970 45a82edb Michael Niedermayer
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
971
{\
972
    int n, index, nb_bits;\
973
\
974
    index= SHOW_UBITS(name, gb, bits);\
975
    level = table[index].level;\
976
    n     = table[index].len;\
977
\
978
    if(max_depth > 1 && n < 0){\
979
        LAST_SKIP_BITS(name, gb, bits)\
980
        UPDATE_CACHE(name, gb)\
981
\
982
        nb_bits = -n;\
983
\
984
        index= SHOW_UBITS(name, gb, nb_bits) + level;\
985
        level = table[index].level;\
986
        n     = table[index].len;\
987
    }\
988
    run= table[index].run;\
989
    SKIP_BITS(name, gb, n)\
990
}
991
992 08dce7b0 Michael Niedermayer
// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
993 3587d501 Michael Niedermayer
static inline int get_vlc(GetBitContext *s, VLC *vlc)
994
{
995 cceabc86 Michael Niedermayer
    int code;
996
    VLC_TYPE (*table)[2]= vlc->table;
997
    
998 8db1a1dd Michael Niedermayer
    OPEN_READER(re, s)
999
    UPDATE_CACHE(re, s)
1000 3587d501 Michael Niedermayer
1001 cceabc86 Michael Niedermayer
    GET_VLC(code, re, s, table, vlc->bits, 3)    
1002 8db1a1dd Michael Niedermayer
1003
    CLOSE_READER(re, s)
1004 3587d501 Michael Niedermayer
    return code;
1005 08dce7b0 Michael Niedermayer
}
1006
1007 d4c48e48 Michael Niedermayer
/**
1008
 * parses a vlc code, faster then get_vlc()
1009
 * @param bits is the number of bits which will be read at once, must be 
1010
 *             identical to nb_bits in init_vlc()
1011
 * @param max_depth is the number of times bits bits must be readed to completly
1012
 *                  read the longest vlc code 
1013
 *                  = (max_vlc_length + bits - 1) / bits
1014
 */
1015 d200cab6 Falk Hüffner
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
1016
                                  int bits, int max_depth)
1017 08dce7b0 Michael Niedermayer
{
1018
    int code;
1019
    
1020
    OPEN_READER(re, s)
1021
    UPDATE_CACHE(re, s)
1022
1023
    GET_VLC(code, re, s, table, bits, max_depth)
1024
1025
    CLOSE_READER(re, s)
1026
    return code;
1027 3587d501 Michael Niedermayer
}
1028
1029 64338956 Michael Niedermayer
//#define TRACE
1030
1031
#ifdef TRACE
1032
1033
static inline void print_bin(int bits, int n){
1034
    int i;
1035
    
1036
    for(i=n-1; i>=0; i--){
1037
        printf("%d", (bits>>i)&1);
1038
    }
1039
    for(i=n; i<24; i++)
1040
        printf(" ");
1041
}
1042
1043
static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
1044
    int r= get_bits(s, n);
1045
    
1046
    print_bin(r, n);
1047
    printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
1048
    return r;
1049
}
1050
static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
1051
    int show= show_bits(s, 24);
1052
    int pos= get_bits_count(s);
1053
    int r= get_vlc2(s, table, bits, max_depth);
1054
    int len= get_bits_count(s) - pos;
1055
    int bits2= show>>(24-len);
1056
    
1057
    print_bin(bits2, len);
1058
    
1059
    printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
1060
    return r;
1061
}
1062 3d2e8cce Michael Niedermayer
static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
1063
    int show= show_bits(s, n);
1064
    int r= get_xbits(s, n);
1065
    
1066
    print_bin(show, n);
1067
    printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
1068
    return r;
1069
}
1070 64338956 Michael Niedermayer
1071
#define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1072
#define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1073 3d2e8cce Michael Niedermayer
#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1074 3b0da3f9 Michael Niedermayer
#define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1075 64338956 Michael Niedermayer
#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1076
1077 365e75f8 Michael Niedermayer
#define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
1078 95c26348 Michael Niedermayer
1079
#else //TRACE
1080 310ffb8f Michael Niedermayer
#define tprintf(...) {}
1081 95c26348 Michael Niedermayer
#endif
1082 45a82edb Michael Niedermayer
1083 de6d9b64 Fabrice Bellard
/* define it to include statistics code (useful only for optimizing
1084
   codec efficiency */
1085
//#define STATS
1086
1087
#ifdef STATS
1088
1089
enum {
1090
    ST_UNKNOWN,
1091
    ST_DC,
1092
    ST_INTRA_AC,
1093
    ST_INTER_AC,
1094
    ST_INTRA_MB,
1095
    ST_INTER_MB,
1096
    ST_MV,
1097
    ST_NB,
1098
};
1099
1100
extern int st_current_index;
1101
extern unsigned int st_bit_counts[ST_NB];
1102
extern unsigned int st_out_bit_counts[ST_NB];
1103
1104
void print_stats(void);
1105
#endif
1106
1107
/* misc math functions */
1108 c81f0349 Michael Niedermayer
extern const uint8_t ff_log2_tab[256];
1109 de6d9b64 Fabrice Bellard
1110 a822a479 Nick Kurshev
static inline int av_log2(unsigned int v)
1111 de6d9b64 Fabrice Bellard
{
1112
    int n;
1113
1114
    n = 0;
1115
    if (v & 0xffff0000) {
1116
        v >>= 16;
1117
        n += 16;
1118
    }
1119
    if (v & 0xff00) {
1120
        v >>= 8;
1121
        n += 8;
1122
    }
1123 c81f0349 Michael Niedermayer
    n += ff_log2_tab[v];
1124
1125
    return n;
1126
}
1127
1128
static inline int av_log2_16bit(unsigned int v)
1129
{
1130
    int n;
1131
1132
    n = 0;
1133
    if (v & 0xff00) {
1134
        v >>= 8;
1135
        n += 8;
1136 de6d9b64 Fabrice Bellard
    }
1137 c81f0349 Michael Niedermayer
    n += ff_log2_tab[v];
1138
1139 de6d9b64 Fabrice Bellard
    return n;
1140
}
1141
1142 45870f57 Michael Niedermayer
/* median of 3 */
1143
static inline int mid_pred(int a, int b, int c)
1144
{
1145 7a62e94a Michael Niedermayer
#if 0
1146
    int t= (a-b)&((a-b)>>31);
1147
    a-=t;
1148
    b+=t;
1149
    b-= (b-c)&((b-c)>>31);
1150
    b+= (a-b)&((a-b)>>31);
1151

1152
    return b;
1153
#else
1154
    if(a>b){
1155
        if(c>b){
1156
            if(c>a) b=a;
1157
            else    b=c;
1158
        }
1159
    }else{
1160
        if(b>c){
1161
            if(c>a) b=c;
1162
            else    b=a;
1163
        }
1164
    }
1165
    return b;
1166
#endif
1167 45870f57 Michael Niedermayer
}
1168
1169 91029be7 Michael Niedermayer
static inline int clip(int a, int amin, int amax)
1170
{
1171
    if (a < amin)
1172
        return amin;
1173
    else if (a > amax)
1174
        return amax;
1175
    else
1176
        return a;
1177
}
1178
1179 3ebc7e04 Michael Niedermayer
static inline int clip_uint8(int a)
1180
{
1181
    if (a&(~255)) return (-a)>>31;
1182
    else          return a;
1183
}
1184
1185 9dbcbd92 Michael Niedermayer
/* math */
1186 c81f0349 Michael Niedermayer
extern const uint8_t ff_sqrt_tab[128];
1187 f36db5df Michael Niedermayer
1188 14bea432 Michael Niedermayer
int64_t ff_gcd(int64_t a, int64_t b);
1189 9dbcbd92 Michael Niedermayer
1190 7fd08ac1 Michael Niedermayer
static inline int ff_sqrt(int a)
1191
{
1192
    int ret=0;
1193
    int s;
1194
    int ret_sq=0;
1195 f36db5df Michael Niedermayer
    
1196
    if(a<128) return ff_sqrt_tab[a];
1197
    
1198 7fd08ac1 Michael Niedermayer
    for(s=15; s>=0; s--){
1199
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
1200
        if(b<=a){
1201
            ret_sq=b;
1202
            ret+= 1<<s;
1203
        }
1204
    }
1205
    return ret;
1206
}
1207 202ef8b8 Michael Niedermayer
1208
/**
1209
 * converts fourcc string to int
1210
 */
1211 6a85ec8d Zdenek Kabelac
static inline int ff_get_fourcc(const char *s){
1212 202ef8b8 Michael Niedermayer
    assert( strlen(s)==4 );
1213 966df5b6 Mike Melanson
1214 202ef8b8 Michael Niedermayer
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
1215
}
1216
1217 e8750b00 Fred Rothganger
#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1218
#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1219
1220
1221 2ad1516a Michael Niedermayer
#ifdef ARCH_X86
1222
#define MASK_ABS(mask, level)\
1223
            asm volatile(\
1224
                "cdq                        \n\t"\
1225
                "xorl %1, %0                \n\t"\
1226
                "subl %1, %0                \n\t"\
1227
                : "+a" (level), "=&d" (mask)\
1228
            );
1229
#else
1230
#define MASK_ABS(mask, level)\
1231
            mask= level>>31;\
1232
            level= (level^mask)-mask;
1233
#endif
1234
1235
1236 7fd08ac1 Michael Niedermayer
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1237
#define COPY3_IF_LT(x,y,a,b,c,d)\
1238
asm volatile (\
1239
    "cmpl %0, %3        \n\t"\
1240
    "cmovl %3, %0        \n\t"\
1241
    "cmovl %4, %1        \n\t"\
1242
    "cmovl %5, %2        \n\t"\
1243
    : "+r" (x), "+r" (a), "+r" (c)\
1244
    : "r" (y), "r" (b), "r" (d)\
1245
);
1246
#else
1247
#define COPY3_IF_LT(x,y,a,b,c,d)\
1248
if((y)<(x)){\
1249
     (x)=(y);\
1250
     (a)=(b);\
1251
     (c)=(d);\
1252
}
1253
#endif
1254
1255 b534c7f9 Michael Niedermayer
#ifdef ARCH_X86
1256
static inline long long rdtsc()
1257
{
1258
        long long l;
1259
        asm volatile(        "rdtsc\n\t"
1260
                : "=A" (l)
1261
        );
1262
        return l;
1263
}
1264
1265
#define START_TIMER \
1266
uint64_t tend;\
1267
uint64_t tstart= rdtsc();\
1268
1269
#define STOP_TIMER(id) \
1270
tend= rdtsc();\
1271 d705e4a6 Michael Niedermayer
{\
1272
  static uint64_t tsum=0;\
1273
  static int tcount=0;\
1274
  static int tskip_count=0;\
1275
  if(tcount<2 || tend - tstart < 8*tsum/tcount){\
1276
      tsum+= tend - tstart;\
1277
      tcount++;\
1278
  }else\
1279
      tskip_count++;\
1280
  if(256*256*256*64%(tcount+tskip_count)==0){\
1281
      av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
1282
  }\
1283 b534c7f9 Michael Niedermayer
}
1284
#endif
1285
1286 a69b930c Alex Beregszaszi
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1287
1288 8e1e6f31 Fabrice Bellard
/* avoid usage of various functions */
1289
#define malloc please_use_av_malloc
1290
#define free please_use_av_free
1291
#define realloc please_use_av_realloc
1292 7d1bbcd4 Michael Niedermayer
#define time time_is_forbidden_due_to_security_issues
1293
#define rand rand_is_forbidden_due_to_state_trashing
1294
#define srand srand_is_forbidden_due_to_state_trashing
1295 d705e4a6 Michael Niedermayer
#if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
1296
#define printf please_use_av_log
1297
#define fprintf please_use_av_log
1298
#endif
1299 8e1e6f31 Fabrice Bellard
1300 7bc9090a Michael Niedermayer
#define CHECKED_ALLOCZ(p, size)\
1301
{\
1302
    p= av_mallocz(size);\
1303 dc939fd5 Michael Niedermayer
    if(p==NULL && (size)!=0){\
1304 7bc9090a Michael Niedermayer
        perror("malloc");\
1305
        goto fail;\
1306
    }\
1307
}
1308
1309 96707bb7 Fabrice Bellard
#endif /* HAVE_AV_CONFIG_H */
1310
1311
#endif /* COMMON_H */