~ubuntu-branches/ubuntu/quantal/vice/quantal

« back to all changes in this revision

Viewing changes to src/gfxoutputdrv/ffmpeg/common.h

  • Committer: Bazaar Package Importer
  • Author(s): Zed Pobre
  • Date: 2005-03-27 13:06:04 UTC
  • mfrom: (4 hoary)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20050327130604-zodmv0i60dbbmcik
Tags: 1.16-3
Apply patch from Andreas Jochens <aj@andaco.de> to correct building on
AMD64 and GCC 4.x (closes: #300936)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file common.h
 
3
 * common internal api header.
 
4
 */
 
5
 
 
6
#ifndef COMMON_H
 
7
#define COMMON_H
 
8
 
 
9
#if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
 
10
#    define CONFIG_WIN32
 
11
#endif
 
12
 
 
13
//#define ALT_BITSTREAM_WRITER
 
14
//#define ALIGNED_BITSTREAM_WRITER
 
15
 
 
16
#define ALT_BITSTREAM_READER
 
17
//#define LIBMPEG2_BITSTREAM_READER
 
18
//#define A32_BITSTREAM_READER
 
19
#define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
 
20
 
 
21
#ifdef HAVE_AV_CONFIG_H
 
22
/* only include the following when compiling package */
 
23
#    include "config.h"
 
24
 
 
25
#    include <stdlib.h>
 
26
#    include <stdio.h>
 
27
#    include <string.h>
 
28
#    include <ctype.h>
 
29
#    ifndef __BEOS__
 
30
#        include <errno.h>
 
31
#    else
 
32
#        include "berrno.h"
 
33
#    endif
 
34
#    include <math.h>
 
35
 
 
36
#    ifndef ENODATA
 
37
#        define ENODATA  61
 
38
#    endif
 
39
 
 
40
#ifndef M_PI
 
41
#define M_PI    3.14159265358979323846
 
42
#endif
 
43
 
 
44
#include <stddef.h>
 
45
#ifndef offsetof
 
46
# define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
 
47
#endif
 
48
 
 
49
#define AVOPTION_CODEC_BOOL(name, help, field) \
 
50
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
 
51
#define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
 
52
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
 
53
#define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
 
54
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
 
55
#define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
 
56
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
 
57
#define AVOPTION_CODEC_STRING(name, help, field, str, val) \
 
58
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
 
59
#define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
 
60
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
 
61
#define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
 
62
#define AVOPTION_END() AVOPTION_SUB(NULL)
 
63
 
 
64
struct AVOption;
 
65
#ifdef HAVE_MMX
 
66
extern const struct AVOption avoptions_common[3 + 5];
 
67
#else
 
68
extern const struct AVOption avoptions_common[3];
 
69
#endif
 
70
extern const struct AVOption avoptions_workaround_bug[11];
 
71
 
 
72
#endif /* HAVE_AV_CONFIG_H */
 
73
 
 
74
/* Suppress restrict if it was not defined in config.h.  */
 
75
#ifndef restrict
 
76
#    define restrict
 
77
#endif
 
78
 
 
79
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
 
80
#    define always_inline __attribute__((always_inline)) inline
 
81
#else
 
82
#    define always_inline inline
 
83
#endif
 
84
 
 
85
#ifdef CONFIG_WIN32
 
86
 
 
87
/* windows */
 
88
 
 
89
typedef unsigned short uint16_t;
 
90
typedef signed short int16_t;
 
91
typedef unsigned char uint8_t;
 
92
typedef unsigned int uint32_t;
 
93
typedef unsigned __int64 uint64_t;
 
94
typedef signed char int8_t;
 
95
typedef signed int int32_t;
 
96
typedef signed __int64 int64_t;
 
97
 
 
98
#    ifndef __MINGW32__
 
99
#        define int64_t_C(c)     (c ## i64)
 
100
#        define uint64_t_C(c)    (c ## i64)
 
101
 
 
102
#        define inline __inline
 
103
 
 
104
#    else
 
105
#        define int64_t_C(c)     (c ## LL)
 
106
#        define uint64_t_C(c)    (c ## ULL)
 
107
#    endif /* __MINGW32__ */
 
108
 
 
109
#    ifdef _DEBUG
 
110
#        define DEBUG
 
111
#    endif
 
112
 
 
113
#    define snprintf _snprintf
 
114
#    define vsnprintf _vsnprintf
 
115
 
 
116
/* CONFIG_WIN32 end */
 
117
#elif defined (CONFIG_OS2)
 
118
/* OS/2 EMX */
 
119
 
 
120
#include <inttypes.h>
 
121
 
 
122
#ifndef int64_t_C
 
123
#define int64_t_C(c)     (c ## LL)
 
124
#define uint64_t_C(c)    (c ## ULL)
 
125
#endif
 
126
 
 
127
#ifdef HAVE_AV_CONFIG_H
 
128
 
 
129
#ifdef USE_FASTMEMCPY
 
130
#include "fastmemcpy.h"
 
131
#endif
 
132
 
 
133
#include <float.h>
 
134
 
 
135
#endif /* HAVE_AV_CONFIG_H */
 
136
 
 
137
/* CONFIG_OS2 end */
 
138
#else
 
139
 
 
140
/* unix */
 
141
 
 
142
#include <inttypes.h>
 
143
 
 
144
#ifndef int64_t_C
 
145
#define int64_t_C(c)     (c ## LL)
 
146
#define uint64_t_C(c)    (c ## ULL)
 
147
#endif
 
148
 
 
149
#ifdef HAVE_AV_CONFIG_H
 
150
 
 
151
#        ifdef USE_FASTMEMCPY
 
152
#            include "fastmemcpy.h"
 
153
#        endif
 
154
#    endif /* HAVE_AV_CONFIG_H */
 
155
 
 
156
#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
 
157
 
 
158
#ifdef HAVE_AV_CONFIG_H
 
159
 
 
160
#    include "bswap.h"
 
161
 
 
162
#    if defined(__MINGW32__) || defined(__CYGWIN__) || \
 
163
        defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
 
164
#        define MANGLE(a) "_" #a
 
165
#    else
 
166
#        define MANGLE(a) #a
 
167
#    endif
 
168
 
 
169
/* debug stuff */
 
170
 
 
171
#    ifndef DEBUG
 
172
#        define NDEBUG
 
173
#    endif
 
174
#    include <assert.h>
 
175
 
 
176
/* dprintf macros */
 
177
#    if defined(CONFIG_WIN32) && !defined(__MINGW32__)
 
178
 
 
179
inline void dprintf(const char* fmt,...) {}
 
180
 
 
181
#    else
 
182
 
 
183
#        ifdef DEBUG
 
184
#            define dprintf(fmt,args...) printf(fmt, ## args)
 
185
#        else
 
186
#            define dprintf(fmt,args...)
 
187
#        endif
 
188
 
 
189
#    endif /* !CONFIG_WIN32 */
 
190
 
 
191
#    define av_abort()      do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
 
192
 
 
193
//rounded divison & shift
 
194
#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
 
195
/* assume b>0 */
 
196
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
 
197
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
 
198
 
 
199
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
 
200
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
 
201
 
 
202
extern const uint32_t inverse[256];
 
203
 
 
204
#ifdef ARCH_X86
 
205
#    define FASTDIV(a,b) \
 
206
    ({\
 
207
        int ret,dmy;\
 
208
        asm volatile(\
 
209
            "mull %3"\
 
210
            :"=d"(ret),"=a"(dmy)\
 
211
            :"1"(a),"g"(inverse[b])\
 
212
            );\
 
213
        ret;\
 
214
    })
 
215
#elif defined(CONFIG_FASTDIV)
 
216
#    define FASTDIV(a,b)   ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
 
217
#else
 
218
#    define FASTDIV(a,b)   ((a)/(b))
 
219
#endif
 
220
 
 
221
#ifdef ARCH_X86
 
222
// avoid +32 for shift optimization (gcc should do that ...)
 
223
static inline  int32_t NEG_SSR32( int32_t a, int8_t s){
 
224
    asm ("sarl %1, %0\n\t"
 
225
         : "+r" (a)
 
226
         : "ic" ((uint8_t)(-s))
 
227
    );
 
228
    return a;
 
229
}
 
230
static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
 
231
    asm ("shrl %1, %0\n\t"
 
232
         : "+r" (a)
 
233
         : "ic" ((uint8_t)(-s))
 
234
    );
 
235
    return a;
 
236
}
 
237
#else
 
238
#    define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
 
239
#    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
 
240
#endif
 
241
 
 
242
/* bit output */
 
243
 
 
244
struct PutBitContext;
 
245
 
 
246
typedef void (*WriteDataFunc)(void *, uint8_t *, int);
 
247
 
 
248
typedef struct PutBitContext {
 
249
#ifdef ALT_BITSTREAM_WRITER
 
250
    uint8_t *buf, *buf_end;
 
251
    int index;
 
252
#else
 
253
    uint32_t bit_buf;
 
254
    int bit_left;
 
255
    uint8_t *buf, *buf_ptr, *buf_end;
 
256
#endif
 
257
    int64_t data_out_size; /* in bytes */
 
258
} PutBitContext;
 
259
 
 
260
void init_put_bits(PutBitContext *s, 
 
261
                   uint8_t *buffer, int buffer_size,
 
262
                   void *opaque,
 
263
                   void (*write_data)(void *, uint8_t *, int));
 
264
 
 
265
int64_t get_bit_count(PutBitContext *s); /* XXX: change function name */
 
266
void align_put_bits(PutBitContext *s);
 
267
void flush_put_bits(PutBitContext *s);
 
268
void put_string(PutBitContext * pbc, char *s);
 
269
 
 
270
/* bit input */
 
271
 
 
272
typedef struct GetBitContext {
 
273
    const uint8_t *buffer, *buffer_end;
 
274
#ifdef ALT_BITSTREAM_READER
 
275
    int index;
 
276
#elif defined LIBMPEG2_BITSTREAM_READER
 
277
    uint8_t *buffer_ptr;
 
278
    uint32_t cache;
 
279
    int bit_count;
 
280
#elif defined A32_BITSTREAM_READER
 
281
    uint32_t *buffer_ptr;
 
282
    uint32_t cache0;
 
283
    uint32_t cache1;
 
284
    int bit_count;
 
285
#endif
 
286
    int size_in_bits;
 
287
} GetBitContext;
 
288
 
 
289
static inline int get_bits_count(GetBitContext *s);
 
290
 
 
291
#define VLC_TYPE int16_t
 
292
 
 
293
typedef struct VLC {
 
294
    int bits;
 
295
    VLC_TYPE (*table)[2]; ///< code, bits
 
296
    int table_size, table_allocated;
 
297
} VLC;
 
298
 
 
299
typedef struct RL_VLC_ELEM {
 
300
    int16_t level;
 
301
    int8_t len;
 
302
    uint8_t run;
 
303
} RL_VLC_ELEM;
 
304
 
 
305
#ifdef ARCH_SPARC64
 
306
#define UNALIGNED_STORES_ARE_BAD
 
307
#endif
 
308
 
 
309
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
 
310
#ifdef ARCH_X86
 
311
#    define unaligned32(a) (*(uint32_t*)(a))
 
312
#else
 
313
#    ifdef __GNUC__
 
314
static inline uint32_t unaligned32(const void *v) {
 
315
    struct Unaligned {
 
316
        uint32_t i;
 
317
    } __attribute__((packed));
 
318
 
 
319
    return ((const struct Unaligned *) v)->i;
 
320
}
 
321
#    elif defined(__DECC)
 
322
static inline uint32_t unaligned32(const void *v) {
 
323
    return *(const __unaligned uint32_t *) v;
 
324
}
 
325
#    else
 
326
static inline uint32_t unaligned32(const void *v) {
 
327
    return *(const uint32_t *) v;
 
328
}
 
329
#    endif
 
330
#endif //!ARCH_X86
 
331
 
 
332
#ifndef ALT_BITSTREAM_WRITER
 
333
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
 
334
{
 
335
    unsigned int bit_buf;
 
336
    int bit_left;
 
337
 
 
338
#ifdef STATS
 
339
    st_out_bit_counts[st_current_index] += n;
 
340
#endif
 
341
    //    printf("put_bits=%d %x\n", n, value);
 
342
    assert(n == 32 || value < (1U << n));
 
343
    
 
344
    bit_buf = s->bit_buf;
 
345
    bit_left = s->bit_left;
 
346
 
 
347
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
 
348
    /* XXX: optimize */
 
349
    if (n < bit_left) {
 
350
        bit_buf = (bit_buf<<n) | value;
 
351
        bit_left-=n;
 
352
    } else {
 
353
        bit_buf<<=bit_left;
 
354
        bit_buf |= value >> (n - bit_left);
 
355
#ifdef UNALIGNED_STORES_ARE_BAD
 
356
        if (3 & (int) s->buf_ptr) {
 
357
            s->buf_ptr[0] = bit_buf >> 24;
 
358
            s->buf_ptr[1] = bit_buf >> 16;
 
359
            s->buf_ptr[2] = bit_buf >>  8;
 
360
            s->buf_ptr[3] = bit_buf      ;
 
361
        } else
 
362
#endif
 
363
        *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
 
364
        //printf("bitbuf = %08x\n", bit_buf);
 
365
        s->buf_ptr+=4;
 
366
        bit_left+=32 - n;
 
367
        bit_buf = value;
 
368
    }
 
369
 
 
370
    s->bit_buf = bit_buf;
 
371
    s->bit_left = bit_left;
 
372
}
 
373
#endif
 
374
 
 
375
 
 
376
#ifdef ALT_BITSTREAM_WRITER
 
377
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
 
378
{
 
379
#    ifdef ALIGNED_BITSTREAM_WRITER
 
380
#        ifdef ARCH_X86
 
381
    asm volatile(
 
382
        "movl %0, %%ecx                 \n\t"
 
383
        "xorl %%eax, %%eax              \n\t"
 
384
        "shrdl %%cl, %1, %%eax          \n\t"
 
385
        "shrl %%cl, %1                  \n\t"
 
386
        "movl %0, %%ecx                 \n\t"
 
387
        "shrl $3, %%ecx                 \n\t"
 
388
        "andl $0xFFFFFFFC, %%ecx        \n\t"
 
389
        "bswapl %1                      \n\t"
 
390
        "orl %1, (%2, %%ecx)            \n\t"
 
391
        "bswapl %%eax                   \n\t"
 
392
        "addl %3, %0                    \n\t"
 
393
        "movl %%eax, 4(%2, %%ecx)       \n\t"
 
394
        : "=&r" (s->index), "=&r" (value)
 
395
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
 
396
        : "%eax", "%ecx"
 
397
    );
 
398
#        else
 
399
    int index= s->index;
 
400
    uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
 
401
    
 
402
    value<<= 32-n; 
 
403
    
 
404
    ptr[0] |= be2me_32(value>>(index&31));
 
405
    ptr[1]  = be2me_32(value<<(32-(index&31)));
 
406
//if(n>24) printf("%d %d\n", n, value);
 
407
    index+= n;
 
408
    s->index= index;
 
409
#        endif
 
410
#    else //ALIGNED_BITSTREAM_WRITER
 
411
#        ifdef ARCH_X86
 
412
    asm volatile(
 
413
        "movl $7, %%ecx                 \n\t"
 
414
        "andl %0, %%ecx                 \n\t"
 
415
        "addl %3, %%ecx                 \n\t"
 
416
        "negl %%ecx                     \n\t"
 
417
        "shll %%cl, %1                  \n\t"
 
418
        "bswapl %1                      \n\t"
 
419
        "movl %0, %%ecx                 \n\t"
 
420
        "shrl $3, %%ecx                 \n\t"
 
421
        "orl %1, (%%ecx, %2)            \n\t"
 
422
        "addl %3, %0                    \n\t"
 
423
        "movl $0, 4(%%ecx, %2)          \n\t"
 
424
        : "=&r" (s->index), "=&r" (value)
 
425
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
 
426
        : "%ecx"
 
427
    );
 
428
#        else
 
429
    int index= s->index;
 
430
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
 
431
    
 
432
    ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
 
433
    ptr[1] = 0;
 
434
//if(n>24) printf("%d %d\n", n, value);
 
435
    index+= n;
 
436
    s->index= index;
 
437
#        endif
 
438
#    endif //!ALIGNED_BITSTREAM_WRITER
 
439
}
 
440
#endif
 
441
 
 
442
 
 
443
static inline uint8_t* pbBufPtr(PutBitContext *s)
 
444
{
 
445
#ifdef ALT_BITSTREAM_WRITER
 
446
        return s->buf + (s->index>>3);
 
447
#else
 
448
        return s->buf_ptr;
 
449
#endif
 
450
}
 
451
 
 
452
/* Bitstream reader API docs:
 
453
name
 
454
    abritary name which is used as prefix for the internal variables
 
455
 
 
456
gb
 
457
    getbitcontext
 
458
 
 
459
OPEN_READER(name, gb)
 
460
    loads gb into local variables
 
461
 
 
462
CLOSE_READER(name, gb)
 
463
    stores local vars in gb
 
464
 
 
465
UPDATE_CACHE(name, gb)
 
466
    refills the internal cache from the bitstream
 
467
    after this call at least MIN_CACHE_BITS will be available,
 
468
 
 
469
GET_CACHE(name, gb)
 
470
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
 
471
 
 
472
SHOW_UBITS(name, gb, num)
 
473
    will return the nest num bits
 
474
 
 
475
SHOW_SBITS(name, gb, num)
 
476
    will return the nest num bits and do sign extension
 
477
 
 
478
SKIP_BITS(name, gb, num)
 
479
    will skip over the next num bits
 
480
    note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
 
481
 
 
482
SKIP_CACHE(name, gb, num)
 
483
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
 
484
 
 
485
SKIP_COUNTER(name, gb, num)
 
486
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
 
487
 
 
488
LAST_SKIP_CACHE(name, gb, num)
 
489
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
 
490
 
 
491
LAST_SKIP_BITS(name, gb, num)
 
492
    is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
 
493
 
 
494
for examples see get_bits, show_bits, skip_bits, get_vlc
 
495
*/
 
496
 
 
497
static inline int unaligned32_be(const void *v)
 
498
{
 
499
#ifdef CONFIG_ALIGN
 
500
        const uint8_t *p=v;
 
501
        return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
 
502
#else
 
503
        return be2me_32( unaligned32(v)); //original
 
504
#endif
 
505
}
 
506
 
 
507
#ifdef ALT_BITSTREAM_READER
 
508
#   define MIN_CACHE_BITS 25
 
509
 
 
510
#   define OPEN_READER(name, gb)\
 
511
        int name##_index= (gb)->index;\
 
512
        int name##_cache= 0;\
 
513
 
 
514
#   define CLOSE_READER(name, gb)\
 
515
        (gb)->index= name##_index;\
 
516
 
 
517
#   define UPDATE_CACHE(name, gb)\
 
518
        name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
 
519
 
 
520
#   define SKIP_CACHE(name, gb, num)\
 
521
        name##_cache <<= (num);\
 
522
 
 
523
// FIXME name?
 
524
#   define SKIP_COUNTER(name, gb, num)\
 
525
        name##_index += (num);\
 
526
 
 
527
#   define SKIP_BITS(name, gb, num)\
 
528
        {\
 
529
            SKIP_CACHE(name, gb, num)\
 
530
            SKIP_COUNTER(name, gb, num)\
 
531
        }\
 
532
 
 
533
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
 
534
#   define LAST_SKIP_CACHE(name, gb, num) ;
 
535
 
 
536
#   define SHOW_UBITS(name, gb, num)\
 
537
        NEG_USR32(name##_cache, num)
 
538
 
 
539
#   define SHOW_SBITS(name, gb, num)\
 
540
        NEG_SSR32(name##_cache, num)
 
541
 
 
542
#   define GET_CACHE(name, gb)\
 
543
        ((uint32_t)name##_cache)
 
544
 
 
545
static inline int get_bits_count(GetBitContext *s){
 
546
    return s->index;
 
547
}
 
548
#elif defined LIBMPEG2_BITSTREAM_READER
 
549
//libmpeg2 like reader
 
550
 
 
551
#   define MIN_CACHE_BITS 17
 
552
 
 
553
#   define OPEN_READER(name, gb)\
 
554
        int name##_bit_count=(gb)->bit_count;\
 
555
        int name##_cache= (gb)->cache;\
 
556
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
 
557
 
 
558
#   define CLOSE_READER(name, gb)\
 
559
        (gb)->bit_count= name##_bit_count;\
 
560
        (gb)->cache= name##_cache;\
 
561
        (gb)->buffer_ptr= name##_buffer_ptr;\
 
562
 
 
563
#ifdef LIBMPEG2_BITSTREAM_READER_HACK
 
564
 
 
565
#   define UPDATE_CACHE(name, gb)\
 
566
    if(name##_bit_count >= 0){\
 
567
        name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
 
568
        ((uint16_t*)name##_buffer_ptr)++;\
 
569
        name##_bit_count-= 16;\
 
570
    }\
 
571
 
 
572
#else
 
573
 
 
574
#   define UPDATE_CACHE(name, gb)\
 
575
    if(name##_bit_count >= 0){\
 
576
        name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
 
577
        name##_buffer_ptr+=2;\
 
578
        name##_bit_count-= 16;\
 
579
    }\
 
580
 
 
581
#endif
 
582
 
 
583
#   define SKIP_CACHE(name, gb, num)\
 
584
        name##_cache <<= (num);\
 
585
 
 
586
#   define SKIP_COUNTER(name, gb, num)\
 
587
        name##_bit_count += (num);\
 
588
 
 
589
#   define SKIP_BITS(name, gb, num)\
 
590
        {\
 
591
            SKIP_CACHE(name, gb, num)\
 
592
            SKIP_COUNTER(name, gb, num)\
 
593
        }\
 
594
 
 
595
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
 
596
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
 
597
 
 
598
#   define SHOW_UBITS(name, gb, num)\
 
599
        NEG_USR32(name##_cache, num)
 
600
 
 
601
#   define SHOW_SBITS(name, gb, num)\
 
602
        NEG_SSR32(name##_cache, num)
 
603
 
 
604
#   define GET_CACHE(name, gb)\
 
605
        ((uint32_t)name##_cache)
 
606
 
 
607
static inline int get_bits_count(GetBitContext *s){
 
608
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
 
609
}
 
610
 
 
611
#elif defined A32_BITSTREAM_READER
 
612
 
 
613
#   define MIN_CACHE_BITS 32
 
614
 
 
615
#   define OPEN_READER(name, gb)\
 
616
        int name##_bit_count=(gb)->bit_count;\
 
617
        uint32_t name##_cache0= (gb)->cache0;\
 
618
        uint32_t name##_cache1= (gb)->cache1;\
 
619
        uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
 
620
 
 
621
#   define CLOSE_READER(name, gb)\
 
622
        (gb)->bit_count= name##_bit_count;\
 
623
        (gb)->cache0= name##_cache0;\
 
624
        (gb)->cache1= name##_cache1;\
 
625
        (gb)->buffer_ptr= name##_buffer_ptr;\
 
626
 
 
627
#   define UPDATE_CACHE(name, gb)\
 
628
    if(name##_bit_count > 0){\
 
629
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
 
630
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
 
631
        name##_cache1 |= next<<name##_bit_count;\
 
632
        name##_buffer_ptr++;\
 
633
        name##_bit_count-= 32;\
 
634
    }\
 
635
 
 
636
#ifdef ARCH_X86
 
637
#   define SKIP_CACHE(name, gb, num)\
 
638
        asm(\
 
639
            "shldl %2, %1, %0           \n\t"\
 
640
            "shll %2, %1                \n\t"\
 
641
            : "+r" (name##_cache0), "+r" (name##_cache1)\
 
642
            : "Ic" ((uint8_t)num)\
 
643
           );
 
644
#else
 
645
#   define SKIP_CACHE(name, gb, num)\
 
646
        name##_cache0 <<= (num);\
 
647
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
 
648
        name##_cache1 <<= (num);
 
649
#endif
 
650
 
 
651
#   define SKIP_COUNTER(name, gb, num)\
 
652
        name##_bit_count += (num);\
 
653
 
 
654
#   define SKIP_BITS(name, gb, num)\
 
655
        {\
 
656
            SKIP_CACHE(name, gb, num)\
 
657
            SKIP_COUNTER(name, gb, num)\
 
658
        }\
 
659
 
 
660
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
 
661
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
 
662
 
 
663
#   define SHOW_UBITS(name, gb, num)\
 
664
        NEG_USR32(name##_cache0, num)
 
665
 
 
666
#   define SHOW_SBITS(name, gb, num)\
 
667
        NEG_SSR32(name##_cache0, num)
 
668
 
 
669
#   define GET_CACHE(name, gb)\
 
670
        (name##_cache0)
 
671
 
 
672
static inline int get_bits_count(GetBitContext *s){
 
673
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
 
674
}
 
675
 
 
676
#endif
 
677
 
 
678
/**
 
679
 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
 
680
 * if MSB not set it is negative 
 
681
 * @param n length in bits
 
682
 * @author BERO  
 
683
 */
 
684
static inline int get_xbits(GetBitContext *s, int n){
 
685
    register int tmp;
 
686
    register int32_t cache;
 
687
    OPEN_READER(re, s)
 
688
    UPDATE_CACHE(re, s)
 
689
    cache = GET_CACHE(re,s);
 
690
    if ((int32_t)cache<0) { //MSB=1
 
691
        tmp = NEG_USR32(cache,n);
 
692
    } else {
 
693
    //   tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
 
694
    //   tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
 
695
        tmp = - NEG_USR32(~cache,n);
 
696
    }
 
697
    LAST_SKIP_BITS(re, s, n)
 
698
    CLOSE_READER(re, s)
 
699
    return tmp;
 
700
}
 
701
 
 
702
static inline int get_sbits(GetBitContext *s, int n){
 
703
    register int tmp;
 
704
    OPEN_READER(re, s)
 
705
    UPDATE_CACHE(re, s)
 
706
    tmp= SHOW_SBITS(re, s, n);
 
707
    LAST_SKIP_BITS(re, s, n)
 
708
    CLOSE_READER(re, s)
 
709
    return tmp;
 
710
}
 
711
 
 
712
/**
 
713
 * reads 0-17 bits.
 
714
 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
 
715
 */
 
716
static inline unsigned int get_bits(GetBitContext *s, int n){
 
717
    register int tmp;
 
718
    OPEN_READER(re, s)
 
719
    UPDATE_CACHE(re, s)
 
720
    tmp= SHOW_UBITS(re, s, n);
 
721
    LAST_SKIP_BITS(re, s, n)
 
722
    CLOSE_READER(re, s)
 
723
    return tmp;
 
724
}
 
725
 
 
726
unsigned int get_bits_long(GetBitContext *s, int n);
 
727
 
 
728
/**
 
729
 * shows 0-17 bits.
 
730
 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
 
731
 */
 
732
static inline unsigned int show_bits(GetBitContext *s, int n){
 
733
    register int tmp;
 
734
    OPEN_READER(re, s)
 
735
    UPDATE_CACHE(re, s)
 
736
    tmp= SHOW_UBITS(re, s, n);
 
737
//    CLOSE_READER(re, s)
 
738
    return tmp;
 
739
}
 
740
 
 
741
unsigned int show_bits_long(GetBitContext *s, int n);
 
742
 
 
743
static inline void skip_bits(GetBitContext *s, int n){
 
744
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
 
745
    OPEN_READER(re, s)
 
746
    UPDATE_CACHE(re, s)
 
747
    LAST_SKIP_BITS(re, s, n)
 
748
    CLOSE_READER(re, s)
 
749
}
 
750
 
 
751
static inline unsigned int get_bits1(GetBitContext *s){
 
752
#ifdef ALT_BITSTREAM_READER
 
753
    int index= s->index;
 
754
    uint8_t result= s->buffer[ index>>3 ];
 
755
    result<<= (index&0x07);
 
756
    result>>= 8 - 1;
 
757
    index++;
 
758
    s->index= index;
 
759
 
 
760
    return result;
 
761
#else
 
762
    return get_bits(s, 1);
 
763
#endif
 
764
}
 
765
 
 
766
static inline unsigned int show_bits1(GetBitContext *s){
 
767
    return show_bits(s, 1);
 
768
}
 
769
 
 
770
static inline void skip_bits1(GetBitContext *s){
 
771
    skip_bits(s, 1);
 
772
}
 
773
 
 
774
void init_get_bits(GetBitContext *s,
 
775
                   const uint8_t *buffer, int buffer_size);
 
776
 
 
777
int check_marker(GetBitContext *s, const char *msg);
 
778
void align_get_bits(GetBitContext *s);
 
779
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
 
780
             const void *bits, int bits_wrap, int bits_size,
 
781
             const void *codes, int codes_wrap, int codes_size);
 
782
void free_vlc(VLC *vlc);
 
783
 
 
784
/**
 
785
 *
 
786
 * if the vlc code is invalid and max_depth=1 than no bits will be removed
 
787
 * if the vlc code is invalid and max_depth>1 than the number of bits removed
 
788
 * is undefined
 
789
 */
 
790
#define GET_VLC(code, name, gb, table, bits, max_depth)\
 
791
{\
 
792
    int n, index, nb_bits;\
 
793
\
 
794
    index= SHOW_UBITS(name, gb, bits);\
 
795
    code = table[index][0];\
 
796
    n    = table[index][1];\
 
797
\
 
798
    if(max_depth > 1 && n < 0){\
 
799
        LAST_SKIP_BITS(name, gb, bits)\
 
800
        UPDATE_CACHE(name, gb)\
 
801
\
 
802
        nb_bits = -n;\
 
803
\
 
804
        index= SHOW_UBITS(name, gb, nb_bits) + code;\
 
805
        code = table[index][0];\
 
806
        n    = table[index][1];\
 
807
        if(max_depth > 2 && n < 0){\
 
808
            LAST_SKIP_BITS(name, gb, nb_bits)\
 
809
            UPDATE_CACHE(name, gb)\
 
810
\
 
811
            nb_bits = -n;\
 
812
\
 
813
            index= SHOW_UBITS(name, gb, nb_bits) + code;\
 
814
            code = table[index][0];\
 
815
            n    = table[index][1];\
 
816
        }\
 
817
    }\
 
818
    SKIP_BITS(name, gb, n)\
 
819
}
 
820
 
 
821
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
 
822
{\
 
823
    int n, index, nb_bits;\
 
824
\
 
825
    index= SHOW_UBITS(name, gb, bits);\
 
826
    level = table[index].level;\
 
827
    n     = table[index].len;\
 
828
\
 
829
    if(max_depth > 1 && n < 0){\
 
830
        LAST_SKIP_BITS(name, gb, bits)\
 
831
        UPDATE_CACHE(name, gb)\
 
832
\
 
833
        nb_bits = -n;\
 
834
\
 
835
        index= SHOW_UBITS(name, gb, nb_bits) + level;\
 
836
        level = table[index].level;\
 
837
        n     = table[index].len;\
 
838
    }\
 
839
    run= table[index].run;\
 
840
    SKIP_BITS(name, gb, n)\
 
841
}
 
842
 
 
843
// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
 
844
static inline int get_vlc(GetBitContext *s, VLC *vlc)
 
845
{
 
846
    int code;
 
847
    VLC_TYPE (*table)[2]= vlc->table;
 
848
    
 
849
    OPEN_READER(re, s)
 
850
    UPDATE_CACHE(re, s)
 
851
 
 
852
    GET_VLC(code, re, s, table, vlc->bits, 3)    
 
853
 
 
854
    CLOSE_READER(re, s)
 
855
    return code;
 
856
}
 
857
 
 
858
/**
 
859
 * parses a vlc code, faster then get_vlc()
 
860
 * @param bits is the number of bits which will be read at once, must be 
 
861
 *             identical to nb_bits in init_vlc()
 
862
 * @param max_depth is the number of times bits bits must be readed to completly
 
863
 *                  read the longest vlc code 
 
864
 *                  = (max_vlc_length + bits - 1) / bits
 
865
 */
 
866
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
 
867
                                  int bits, int max_depth)
 
868
{
 
869
    int code;
 
870
    
 
871
    OPEN_READER(re, s)
 
872
    UPDATE_CACHE(re, s)
 
873
 
 
874
    GET_VLC(code, re, s, table, bits, max_depth)
 
875
 
 
876
    CLOSE_READER(re, s)
 
877
    return code;
 
878
}
 
879
 
 
880
//#define TRACE
 
881
 
 
882
#ifdef TRACE
 
883
 
 
884
static inline void print_bin(int bits, int n){
 
885
    int i;
 
886
    
 
887
    for(i=n-1; i>=0; i--){
 
888
        printf("%d", (bits>>i)&1);
 
889
    }
 
890
    for(i=n; i<24; i++)
 
891
        printf(" ");
 
892
}
 
893
 
 
894
static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
 
895
    int r= get_bits(s, n);
 
896
    
 
897
    print_bin(r, n);
 
898
    printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
 
899
    return r;
 
900
}
 
901
static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
 
902
    int show= show_bits(s, 24);
 
903
    int pos= get_bits_count(s);
 
904
    int r= get_vlc2(s, table, bits, max_depth);
 
905
    int len= get_bits_count(s) - pos;
 
906
    int bits2= show>>(24-len);
 
907
    
 
908
    print_bin(bits2, len);
 
909
    
 
910
    printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
 
911
    return r;
 
912
}
 
913
static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
 
914
    int show= show_bits(s, n);
 
915
    int r= get_xbits(s, n);
 
916
    
 
917
    print_bin(show, n);
 
918
    printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
 
919
    return r;
 
920
}
 
921
 
 
922
#define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
 
923
#define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
 
924
#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
 
925
#define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
 
926
#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
 
927
 
 
928
#define tprintf printf
 
929
 
 
930
#else //TRACE
 
931
#define tprintf(_arg...) {}
 
932
#endif
 
933
 
 
934
/* define it to include statistics code (useful only for optimizing
 
935
   codec efficiency */
 
936
//#define STATS
 
937
 
 
938
#ifdef STATS
 
939
 
 
940
enum {
 
941
    ST_UNKNOWN,
 
942
    ST_DC,
 
943
    ST_INTRA_AC,
 
944
    ST_INTER_AC,
 
945
    ST_INTRA_MB,
 
946
    ST_INTER_MB,
 
947
    ST_MV,
 
948
    ST_NB,
 
949
};
 
950
 
 
951
extern int st_current_index;
 
952
extern unsigned int st_bit_counts[ST_NB];
 
953
extern unsigned int st_out_bit_counts[ST_NB];
 
954
 
 
955
void print_stats(void);
 
956
#endif
 
957
 
 
958
/* misc math functions */
 
959
extern const uint8_t ff_log2_tab[256];
 
960
 
 
961
static inline int av_log2(unsigned int v)
 
962
{
 
963
    int n;
 
964
 
 
965
    n = 0;
 
966
    if (v & 0xffff0000) {
 
967
        v >>= 16;
 
968
        n += 16;
 
969
    }
 
970
    if (v & 0xff00) {
 
971
        v >>= 8;
 
972
        n += 8;
 
973
    }
 
974
    n += ff_log2_tab[v];
 
975
 
 
976
    return n;
 
977
}
 
978
 
 
979
static inline int av_log2_16bit(unsigned int v)
 
980
{
 
981
    int n;
 
982
 
 
983
    n = 0;
 
984
    if (v & 0xff00) {
 
985
        v >>= 8;
 
986
        n += 8;
 
987
    }
 
988
    n += ff_log2_tab[v];
 
989
 
 
990
    return n;
 
991
}
 
992
 
 
993
 
 
994
/* median of 3 */
 
995
static inline int mid_pred(int a, int b, int c)
 
996
{
 
997
    int vmin, vmax;
 
998
    vmax = vmin = a;
 
999
    if (b < vmin)
 
1000
        vmin = b;
 
1001
    else
 
1002
        vmax = b;
 
1003
 
 
1004
    if (c < vmin)
 
1005
        vmin = c;
 
1006
    else if (c > vmax)
 
1007
        vmax = c;
 
1008
 
 
1009
    return a + b + c - vmin - vmax;
 
1010
}
 
1011
 
 
1012
static inline int clip(int a, int amin, int amax)
 
1013
{
 
1014
    if (a < amin)
 
1015
        return amin;
 
1016
    else if (a > amax)
 
1017
        return amax;
 
1018
    else
 
1019
        return a;
 
1020
}
 
1021
 
 
1022
/* math */
 
1023
extern const uint8_t ff_sqrt_tab[128];
 
1024
 
 
1025
int64_t ff_gcd(int64_t a, int64_t b);
 
1026
 
 
1027
static inline int ff_sqrt(int a)
 
1028
{
 
1029
    int ret=0;
 
1030
    int s;
 
1031
    int ret_sq=0;
 
1032
    
 
1033
    if(a<128) return ff_sqrt_tab[a];
 
1034
    
 
1035
    for(s=15; s>=0; s--){
 
1036
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
 
1037
        if(b<=a){
 
1038
            ret_sq=b;
 
1039
            ret+= 1<<s;
 
1040
        }
 
1041
    }
 
1042
    return ret;
 
1043
}
 
1044
 
 
1045
/**
 
1046
 * converts fourcc string to int
 
1047
 */
 
1048
static inline int ff_get_fourcc(const char *s){
 
1049
    assert( strlen(s)==4 );
 
1050
 
 
1051
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
 
1052
}
 
1053
 
 
1054
#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
 
1055
#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
 
1056
 
 
1057
 
 
1058
void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max);
 
1059
 
 
1060
 
 
1061
#ifdef ARCH_X86
 
1062
#define MASK_ABS(mask, level)\
 
1063
            asm volatile(\
 
1064
                "cdq                    \n\t"\
 
1065
                "xorl %1, %0            \n\t"\
 
1066
                "subl %1, %0            \n\t"\
 
1067
                : "+a" (level), "=&d" (mask)\
 
1068
            );
 
1069
#else
 
1070
#define MASK_ABS(mask, level)\
 
1071
            mask= level>>31;\
 
1072
            level= (level^mask)-mask;
 
1073
#endif
 
1074
 
 
1075
 
 
1076
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
 
1077
#define COPY3_IF_LT(x,y,a,b,c,d)\
 
1078
asm volatile (\
 
1079
    "cmpl %0, %3        \n\t"\
 
1080
    "cmovl %3, %0       \n\t"\
 
1081
    "cmovl %4, %1       \n\t"\
 
1082
    "cmovl %5, %2       \n\t"\
 
1083
    : "+r" (x), "+r" (a), "+r" (c)\
 
1084
    : "r" (y), "r" (b), "r" (d)\
 
1085
);
 
1086
#else
 
1087
#define COPY3_IF_LT(x,y,a,b,c,d)\
 
1088
if((y)<(x)){\
 
1089
     (x)=(y);\
 
1090
     (a)=(b);\
 
1091
     (c)=(d);\
 
1092
}
 
1093
#endif
 
1094
 
 
1095
#ifdef ARCH_X86
 
1096
static inline long long rdtsc()
 
1097
{
 
1098
        long long l;
 
1099
        asm volatile(   "rdtsc\n\t"
 
1100
                : "=A" (l)
 
1101
        );
 
1102
        return l;
 
1103
}
 
1104
 
 
1105
#define START_TIMER \
 
1106
static uint64_t tsum=0;\
 
1107
static int tcount=0;\
 
1108
static int tskip_count=0;\
 
1109
uint64_t tend;\
 
1110
uint64_t tstart= rdtsc();\
 
1111
 
 
1112
#define STOP_TIMER(id) \
 
1113
tend= rdtsc();\
 
1114
if(tcount<2 || tend - tstart < 4*tsum/tcount){\
 
1115
    tsum+= tend - tstart;\
 
1116
    tcount++;\
 
1117
}else\
 
1118
    tskip_count++;\
 
1119
if(256*256*256*64%(tcount+tskip_count)==0){\
 
1120
    fprintf(stderr, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
 
1121
}
 
1122
#endif
 
1123
 
 
1124
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
 
1125
 
 
1126
/* avoid usage of various functions */
 
1127
#define malloc please_use_av_malloc
 
1128
#define free please_use_av_free
 
1129
#define realloc please_use_av_realloc
 
1130
 
 
1131
#define CHECKED_ALLOCZ(p, size)\
 
1132
{\
 
1133
    p= av_mallocz(size);\
 
1134
    if(p==NULL && (size)!=0){\
 
1135
        perror("malloc");\
 
1136
        goto fail;\
 
1137
    }\
 
1138
}
 
1139
 
 
1140
#endif /* HAVE_AV_CONFIG_H */
 
1141
 
 
1142
#endif /* COMMON_H */