annotate src/ffmpeg/libavcodec/bitstream.h @ 811:3afe5701df41 trunk

[svn] - remove h261/h263/h264 stuff
author nenolod
date Mon, 12 Mar 2007 11:38:51 -0700
parents e8776388b02a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1 /*
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
4 * This file is part of FFmpeg.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
5 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
6 * FFmpeg is free software; you can redistribute it and/or
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
8 * License as published by the Free Software Foundation; either
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
9 * version 2.1 of the License, or (at your option) any later version.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
10 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
11 * FFmpeg is distributed in the hope that it will be useful,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
14 * Lesser General Public License for more details.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
15 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
17 * License along with FFmpeg; if not, write to the Free Software
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
19 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
20
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
21 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
22 * @file bitstream.h
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
23 * bitstream api header.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
24 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
25
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
26 #ifndef BITSTREAM_H
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
27 #define BITSTREAM_H
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
28
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
29 #include "log.h"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
30
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
31 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
32 #define ALT_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
33 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
34
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
35 //#define ALT_BITSTREAM_WRITER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
36 //#define ALIGNED_BITSTREAM_WRITER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
37 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
38 # ifdef ARCH_ARMV4L
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
39 # define A32_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
40 # else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
41 #define ALT_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
42 //#define LIBMPEG2_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
43 //#define A32_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
44 # endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
45 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
46 #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
47
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
48 extern const uint8_t ff_reverse[256];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
49
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
50 #if defined(ARCH_X86) || defined(ARCH_X86_64)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
51 // avoid +32 for shift optimization (gcc should do that ...)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
52 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
53 asm ("sarl %1, %0\n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
54 : "+r" (a)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
55 : "ic" ((uint8_t)(-s))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
56 );
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
57 return a;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
58 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
59 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
60 asm ("shrl %1, %0\n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
61 : "+r" (a)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
62 : "ic" ((uint8_t)(-s))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
63 );
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
64 return a;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
65 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
66 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
67 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
68 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
69 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
70
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
71 /* bit output */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
72
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
73 /* buf and buf_end must be present and used by every alternative writer. */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
74 typedef struct PutBitContext {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
75 #ifdef ALT_BITSTREAM_WRITER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
76 uint8_t *buf, *buf_end;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
77 int index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
78 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
79 uint32_t bit_buf;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
80 int bit_left;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
81 uint8_t *buf, *buf_ptr, *buf_end;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
82 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
83 } PutBitContext;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
84
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
85 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
86 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
87 if(buffer_size < 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
88 buffer_size = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
89 buffer = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
90 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
91
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
92 s->buf = buffer;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
93 s->buf_end = s->buf + buffer_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
94 #ifdef ALT_BITSTREAM_WRITER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
95 s->index=0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
96 ((uint32_t*)(s->buf))[0]=0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
97 // memset(buffer, 0, buffer_size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
98 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
99 s->buf_ptr = s->buf;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
100 s->bit_left=32;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
101 s->bit_buf=0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
102 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
103 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
104
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
105 /* return the number of bits output */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
106 static inline int put_bits_count(PutBitContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
107 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
108 #ifdef ALT_BITSTREAM_WRITER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
109 return s->index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
110 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
111 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
112 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
113 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
114
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
115 /* pad the end of the output stream with zeros */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
116 static inline void flush_put_bits(PutBitContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
117 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
118 #ifdef ALT_BITSTREAM_WRITER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
119 align_put_bits(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
120 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
121 s->bit_buf<<= s->bit_left;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
122 while (s->bit_left < 32) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
123 /* XXX: should test end of buffer */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
124 *s->buf_ptr++=s->bit_buf >> 24;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
125 s->bit_buf<<=8;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
126 s->bit_left+=8;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
127 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
128 s->bit_left=32;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
129 s->bit_buf=0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
130 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
131 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
132
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
133 void align_put_bits(PutBitContext *s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
134 void ff_put_string(PutBitContext * pbc, char *s, int put_zero);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
135
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
136 /* bit input */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
137 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
138 typedef struct GetBitContext {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
139 const uint8_t *buffer, *buffer_end;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
140 #ifdef ALT_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
141 int index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
142 #elif defined LIBMPEG2_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
143 uint8_t *buffer_ptr;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
144 uint32_t cache;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
145 int bit_count;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
146 #elif defined A32_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
147 uint32_t *buffer_ptr;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
148 uint32_t cache0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
149 uint32_t cache1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
150 int bit_count;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
151 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
152 int size_in_bits;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
153 } GetBitContext;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
154
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
155 #define VLC_TYPE int16_t
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
156
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
157 typedef struct VLC {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
158 int bits;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
159 VLC_TYPE (*table)[2]; ///< code, bits
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
160 int table_size, table_allocated;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
161 } VLC;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
162
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
163 typedef struct RL_VLC_ELEM {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
164 int16_t level;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
165 int8_t len;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
166 uint8_t run;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
167 } RL_VLC_ELEM;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
168
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
169 #if defined(ARCH_SPARC) || defined(ARCH_ARMV4L) || defined(ARCH_MIPS)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
170 #define UNALIGNED_STORES_ARE_BAD
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
171 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
172
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
173 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
174 #if defined(ARCH_X86) || defined(ARCH_X86_64)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
175 # define unaligned16(a) (*(const uint16_t*)(a))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
176 # define unaligned32(a) (*(const uint32_t*)(a))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
177 # define unaligned64(a) (*(const uint64_t*)(a))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
178 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
179 # ifdef __GNUC__
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
180 # define unaligned(x) \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
181 static inline uint##x##_t unaligned##x(const void *v) { \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
182 struct Unaligned { \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
183 uint##x##_t i; \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
184 } __attribute__((packed)); \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
185 \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
186 return ((const struct Unaligned *) v)->i; \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
187 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
188 # elif defined(__DECC)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
189 # define unaligned(x) \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
190 static inline uint##x##_t unaligned##x##(const void *v) { \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
191 return *(const __unaligned uint##x##_t *) v; \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
192 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
193 # else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
194 # define unaligned(x) \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
195 static inline uint##x##_t unaligned##x##(const void *v) { \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
196 return *(const uint##x##_t *) v; \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
197 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
198 # endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
199 unaligned(16)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
200 unaligned(32)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
201 unaligned(64)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
202 #undef unaligned
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
203 #endif /* defined(ARCH_X86) || defined(ARCH_X86_64) */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
204
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
205 #ifndef ALT_BITSTREAM_WRITER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
206 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
207 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
208 unsigned int bit_buf;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
209 int bit_left;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
210
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
211 // printf("put_bits=%d %x\n", n, value);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
212 assert(n == 32 || value < (1U << n));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
213
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
214 bit_buf = s->bit_buf;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
215 bit_left = s->bit_left;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
216
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
217 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
218 /* XXX: optimize */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
219 if (n < bit_left) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
220 bit_buf = (bit_buf<<n) | value;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
221 bit_left-=n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
222 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
223 bit_buf<<=bit_left;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
224 bit_buf |= value >> (n - bit_left);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
225 #ifdef UNALIGNED_STORES_ARE_BAD
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
226 if (3 & (intptr_t) s->buf_ptr) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
227 s->buf_ptr[0] = bit_buf >> 24;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
228 s->buf_ptr[1] = bit_buf >> 16;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
229 s->buf_ptr[2] = bit_buf >> 8;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
230 s->buf_ptr[3] = bit_buf ;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
231 } else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
232 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
233 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
234 //printf("bitbuf = %08x\n", bit_buf);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
235 s->buf_ptr+=4;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
236 bit_left+=32 - n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
237 bit_buf = value;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
238 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
239
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
240 s->bit_buf = bit_buf;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
241 s->bit_left = bit_left;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
242 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
243 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
244
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
245
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
246 #ifdef ALT_BITSTREAM_WRITER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
247 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
248 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
249 # ifdef ALIGNED_BITSTREAM_WRITER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
250 # if defined(ARCH_X86) || defined(ARCH_X86_64)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
251 asm volatile(
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
252 "movl %0, %%ecx \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
253 "xorl %%eax, %%eax \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
254 "shrdl %%cl, %1, %%eax \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
255 "shrl %%cl, %1 \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
256 "movl %0, %%ecx \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
257 "shrl $3, %%ecx \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
258 "andl $0xFFFFFFFC, %%ecx \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
259 "bswapl %1 \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
260 "orl %1, (%2, %%ecx) \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
261 "bswapl %%eax \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
262 "addl %3, %0 \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
263 "movl %%eax, 4(%2, %%ecx) \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
264 : "=&r" (s->index), "=&r" (value)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
265 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
266 : "%eax", "%ecx"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
267 );
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
268 # else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
269 int index= s->index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
270 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
271
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
272 value<<= 32-n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
273
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
274 ptr[0] |= be2me_32(value>>(index&31));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
275 ptr[1] = be2me_32(value<<(32-(index&31)));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
276 //if(n>24) printf("%d %d\n", n, value);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
277 index+= n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
278 s->index= index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
279 # endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
280 # else //ALIGNED_BITSTREAM_WRITER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
281 # if defined(ARCH_X86) || defined(ARCH_X86_64)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
282 asm volatile(
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
283 "movl $7, %%ecx \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
284 "andl %0, %%ecx \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
285 "addl %3, %%ecx \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
286 "negl %%ecx \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
287 "shll %%cl, %1 \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
288 "bswapl %1 \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
289 "movl %0, %%ecx \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
290 "shrl $3, %%ecx \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
291 "orl %1, (%%ecx, %2) \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
292 "addl %3, %0 \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
293 "movl $0, 4(%%ecx, %2) \n\t"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
294 : "=&r" (s->index), "=&r" (value)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
295 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
296 : "%ecx"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
297 );
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
298 # else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
299 int index= s->index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
300 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
301
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
302 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
303 ptr[1] = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
304 //if(n>24) printf("%d %d\n", n, value);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
305 index+= n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
306 s->index= index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
307 # endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
308 # endif //!ALIGNED_BITSTREAM_WRITER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
309 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
310 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
311
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
312
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
313 static inline uint8_t* pbBufPtr(PutBitContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
314 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
315 #ifdef ALT_BITSTREAM_WRITER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
316 return s->buf + (s->index>>3);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
317 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
318 return s->buf_ptr;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
319 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
320 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
321
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
322 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
323 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
324 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
325 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
326 static inline void skip_put_bytes(PutBitContext *s, int n){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
327 assert((put_bits_count(s)&7)==0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
328 #ifdef ALT_BITSTREAM_WRITER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
329 FIXME may need some cleaning of the buffer
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
330 s->index += n<<3;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
331 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
332 assert(s->bit_left==32);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
333 s->buf_ptr += n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
334 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
335 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
336
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
337 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
338 * skips the given number of bits.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
339 * must only be used if the actual values in the bitstream dont matter
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
340 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
341 static inline void skip_put_bits(PutBitContext *s, int n){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
342 #ifdef ALT_BITSTREAM_WRITER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
343 s->index += n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
344 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
345 s->bit_left -= n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
346 s->buf_ptr-= s->bit_left>>5;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
347 s->bit_left &= 31;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
348 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
349 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
350
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
351 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
352 * Changes the end of the buffer.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
353 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
354 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
355 s->buf_end= s->buf + size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
356 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
357
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
358 /* Bitstream reader API docs:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
359 name
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
360 abritary name which is used as prefix for the internal variables
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
361
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
362 gb
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
363 getbitcontext
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
364
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
365 OPEN_READER(name, gb)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
366 loads gb into local variables
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
367
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
368 CLOSE_READER(name, gb)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
369 stores local vars in gb
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
370
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
371 UPDATE_CACHE(name, gb)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
372 refills the internal cache from the bitstream
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
373 after this call at least MIN_CACHE_BITS will be available,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
374
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
375 GET_CACHE(name, gb)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
376 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
377
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
378 SHOW_UBITS(name, gb, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
379 will return the next num bits
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
380
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
381 SHOW_SBITS(name, gb, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
382 will return the next num bits and do sign extension
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
383
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
384 SKIP_BITS(name, gb, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
385 will skip over the next num bits
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
386 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
387
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
388 SKIP_CACHE(name, gb, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
389 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
390
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
391 SKIP_COUNTER(name, gb, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
392 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
393
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
394 LAST_SKIP_CACHE(name, gb, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
395 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
396
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
397 LAST_SKIP_BITS(name, gb, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
398 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
399
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
400 for examples see get_bits, show_bits, skip_bits, get_vlc
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
401 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
402
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
403 static inline int unaligned32_be(const void *v)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
404 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
405 #ifdef CONFIG_ALIGN
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
406 const uint8_t *p=v;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
407 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
408 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
409 return be2me_32( unaligned32(v)); //original
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
410 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
411 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
412
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
413 static inline int unaligned32_le(const void *v)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
414 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
415 #ifdef CONFIG_ALIGN
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
416 const uint8_t *p=v;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
417 return (((p[3]<<8) | p[2])<<16) | (p[1]<<8) | (p[0]);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
418 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
419 return le2me_32( unaligned32(v)); //original
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
420 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
421 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
422
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
423 #ifdef ALT_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
424 # define MIN_CACHE_BITS 25
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
425
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
426 # define OPEN_READER(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
427 int name##_index= (gb)->index;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
428 int name##_cache= 0;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
429
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
430 # define CLOSE_READER(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
431 (gb)->index= name##_index;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
432
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
433 # ifdef ALT_BITSTREAM_READER_LE
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
434 # define UPDATE_CACHE(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
435 name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
436
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
437 # define SKIP_CACHE(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
438 name##_cache >>= (num);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
439 # else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
440 # define UPDATE_CACHE(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
441 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
442
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
443 # define SKIP_CACHE(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
444 name##_cache <<= (num);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
445 # endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
446
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
447 // FIXME name?
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
448 # define SKIP_COUNTER(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
449 name##_index += (num);\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
450
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
451 # define SKIP_BITS(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
452 {\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
453 SKIP_CACHE(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
454 SKIP_COUNTER(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
455 }\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
456
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
457 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
458 # define LAST_SKIP_CACHE(name, gb, num) ;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
459
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
460 # ifdef ALT_BITSTREAM_READER_LE
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
461 # define SHOW_UBITS(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
462 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
463
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
464 # define SHOW_SBITS(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
465 NEG_SSR32((name##_cache)<<(32-(num)), num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
466 # else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
467 # define SHOW_UBITS(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
468 NEG_USR32(name##_cache, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
469
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
470 # define SHOW_SBITS(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
471 NEG_SSR32(name##_cache, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
472 # endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
473
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
474 # define GET_CACHE(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
475 ((uint32_t)name##_cache)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
476
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
477 static inline int get_bits_count(GetBitContext *s){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
478 return s->index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
479 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
480
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
481 static inline void skip_bits_long(GetBitContext *s, int n){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
482 s->index += n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
483 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
484
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
485 #elif defined LIBMPEG2_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
486 //libmpeg2 like reader
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
487
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
488 # define MIN_CACHE_BITS 17
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
489
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
490 # define OPEN_READER(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
491 int name##_bit_count=(gb)->bit_count;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
492 int name##_cache= (gb)->cache;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
493 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
494
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
495 # define CLOSE_READER(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
496 (gb)->bit_count= name##_bit_count;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
497 (gb)->cache= name##_cache;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
498 (gb)->buffer_ptr= name##_buffer_ptr;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
499
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
500 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
501
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
502 # define UPDATE_CACHE(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
503 if(name##_bit_count >= 0){\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
504 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
505 name##_buffer_ptr += 2;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
506 name##_bit_count-= 16;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
507 }\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
508
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
509 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
510
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
511 # define UPDATE_CACHE(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
512 if(name##_bit_count >= 0){\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
513 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
514 name##_buffer_ptr+=2;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
515 name##_bit_count-= 16;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
516 }\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
517
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
518 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
519
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
520 # define SKIP_CACHE(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
521 name##_cache <<= (num);\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
522
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
523 # define SKIP_COUNTER(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
524 name##_bit_count += (num);\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
525
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
526 # define SKIP_BITS(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
527 {\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
528 SKIP_CACHE(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
529 SKIP_COUNTER(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
530 }\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
531
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
532 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
533 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
534
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
535 # define SHOW_UBITS(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
536 NEG_USR32(name##_cache, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
537
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
538 # define SHOW_SBITS(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
539 NEG_SSR32(name##_cache, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
540
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
541 # define GET_CACHE(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
542 ((uint32_t)name##_cache)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
543
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
544 static inline int get_bits_count(GetBitContext *s){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
545 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
546 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
547
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
548 static inline void skip_bits_long(GetBitContext *s, int n){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
549 OPEN_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
550 re_bit_count += n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
551 re_buffer_ptr += 2*(re_bit_count>>4);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
552 re_bit_count &= 15;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
553 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
554 UPDATE_CACHE(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
555 CLOSE_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
556 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
557
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
558 #elif defined A32_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
559
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
560 # define MIN_CACHE_BITS 32
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
561
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
562 # define OPEN_READER(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
563 int name##_bit_count=(gb)->bit_count;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
564 uint32_t name##_cache0= (gb)->cache0;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
565 uint32_t name##_cache1= (gb)->cache1;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
566 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
567
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
568 # define CLOSE_READER(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
569 (gb)->bit_count= name##_bit_count;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
570 (gb)->cache0= name##_cache0;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
571 (gb)->cache1= name##_cache1;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
572 (gb)->buffer_ptr= name##_buffer_ptr;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
573
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
574 # define UPDATE_CACHE(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
575 if(name##_bit_count > 0){\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
576 const uint32_t next= be2me_32( *name##_buffer_ptr );\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
577 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
578 name##_cache1 |= next<<name##_bit_count;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
579 name##_buffer_ptr++;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
580 name##_bit_count-= 32;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
581 }\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
582
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
583 #if defined(ARCH_X86) || defined(ARCH_X86_64)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
584 # define SKIP_CACHE(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
585 asm(\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
586 "shldl %2, %1, %0 \n\t"\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
587 "shll %2, %1 \n\t"\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
588 : "+r" (name##_cache0), "+r" (name##_cache1)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
589 : "Ic" ((uint8_t)(num))\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
590 );
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
591 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
592 # define SKIP_CACHE(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
593 name##_cache0 <<= (num);\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
594 name##_cache0 |= NEG_USR32(name##_cache1,num);\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
595 name##_cache1 <<= (num);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
596 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
597
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
598 # define SKIP_COUNTER(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
599 name##_bit_count += (num);\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
600
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
601 # define SKIP_BITS(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
602 {\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
603 SKIP_CACHE(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
604 SKIP_COUNTER(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
605 }\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
606
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
607 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
608 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
609
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
610 # define SHOW_UBITS(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
611 NEG_USR32(name##_cache0, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
612
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
613 # define SHOW_SBITS(name, gb, num)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
614 NEG_SSR32(name##_cache0, num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
615
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
616 # define GET_CACHE(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
617 (name##_cache0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
618
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
619 static inline int get_bits_count(GetBitContext *s){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
620 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
621 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
622
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
623 static inline void skip_bits_long(GetBitContext *s, int n){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
624 OPEN_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
625 re_bit_count += n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
626 re_buffer_ptr += re_bit_count>>5;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
627 re_bit_count &= 31;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
628 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
629 re_cache1 = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
630 UPDATE_CACHE(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
631 CLOSE_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
632 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
633
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
634 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
635
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
636 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
637 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
638 * if MSB not set it is negative
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
639 * @param n length in bits
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
640 * @author BERO
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
641 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
642 static inline int get_xbits(GetBitContext *s, int n){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
643 register int sign;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
644 register int32_t cache;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
645 OPEN_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
646 UPDATE_CACHE(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
647 cache = GET_CACHE(re,s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
648 sign=(~cache)>>31;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
649 LAST_SKIP_BITS(re, s, n)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
650 CLOSE_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
651 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
652 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
653
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
654 static inline int get_sbits(GetBitContext *s, int n){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
655 register int tmp;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
656 OPEN_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
657 UPDATE_CACHE(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
658 tmp= SHOW_SBITS(re, s, n);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
659 LAST_SKIP_BITS(re, s, n)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
660 CLOSE_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
661 return tmp;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
662 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
663
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
664 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
665 * reads 0-17 bits.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
666 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
667 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
668 static inline unsigned int get_bits(GetBitContext *s, int n){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
669 register int tmp;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
670 OPEN_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
671 UPDATE_CACHE(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
672 tmp= SHOW_UBITS(re, s, n);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
673 LAST_SKIP_BITS(re, s, n)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
674 CLOSE_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
675 return tmp;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
676 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
677
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
678 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
679 * shows 0-17 bits.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
680 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
681 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
682 static inline unsigned int show_bits(GetBitContext *s, int n){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
683 register int tmp;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
684 OPEN_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
685 UPDATE_CACHE(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
686 tmp= SHOW_UBITS(re, s, n);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
687 // CLOSE_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
688 return tmp;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
689 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
690
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
691 static inline void skip_bits(GetBitContext *s, int n){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
692 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
693 OPEN_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
694 UPDATE_CACHE(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
695 LAST_SKIP_BITS(re, s, n)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
696 CLOSE_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
697 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
698
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
699 static inline unsigned int get_bits1(GetBitContext *s){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
700 #ifdef ALT_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
701 int index= s->index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
702 uint8_t result= s->buffer[ index>>3 ];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
703 #ifdef ALT_BITSTREAM_READER_LE
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
704 result>>= (index&0x07);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
705 result&= 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
706 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
707 result<<= (index&0x07);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
708 result>>= 8 - 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
709 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
710 index++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
711 s->index= index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
712
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
713 return result;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
714 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
715 return get_bits(s, 1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
716 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
717 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
718
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
719 static inline unsigned int show_bits1(GetBitContext *s){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
720 return show_bits(s, 1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
721 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
722
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
723 static inline void skip_bits1(GetBitContext *s){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
724 skip_bits(s, 1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
725 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
726
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
727 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
728 * reads 0-32 bits.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
729 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
730 static inline unsigned int get_bits_long(GetBitContext *s, int n){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
731 if(n<=17) return get_bits(s, n);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
732 else{
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
733 #ifdef ALT_BITSTREAM_READER_LE
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
734 int ret= get_bits(s, 16);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
735 return ret | (get_bits(s, n-16) << 16);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
736 #else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
737 int ret= get_bits(s, 16) << (n-16);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
738 return ret | get_bits(s, n-16);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
739 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
740 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
741 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
742
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
743 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
744 * shows 0-32 bits.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
745 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
746 static inline unsigned int show_bits_long(GetBitContext *s, int n){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
747 if(n<=17) return show_bits(s, n);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
748 else{
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
749 GetBitContext gb= *s;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
750 int ret= get_bits_long(s, n);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
751 *s= gb;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
752 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
753 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
754 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
755
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
756 static inline int check_marker(GetBitContext *s, const char *msg)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
757 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
758 int bit= get_bits1(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
759 if(!bit)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
760 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
761
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
762 return bit;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
763 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
764
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
765 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
766 * init GetBitContext.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
767 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
768 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
769 * @param bit_size the size of the buffer in bits
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
770 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
771 static inline void init_get_bits(GetBitContext *s,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
772 const uint8_t *buffer, int bit_size)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
773 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
774 int buffer_size= (bit_size+7)>>3;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
775 if(buffer_size < 0 || bit_size < 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
776 buffer_size = bit_size = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
777 buffer = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
778 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
779
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
780 s->buffer= buffer;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
781 s->size_in_bits= bit_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
782 s->buffer_end= buffer + buffer_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
783 #ifdef ALT_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
784 s->index=0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
785 #elif defined LIBMPEG2_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
786 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
787 s->bit_count = 16 + 8*((intptr_t)buffer&1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
788 skip_bits_long(s, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
789 #elif defined A32_BITSTREAM_READER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
790 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
791 s->bit_count = 32 + 8*((intptr_t)buffer&3);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
792 skip_bits_long(s, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
793 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
794 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
795
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
796 static inline void align_get_bits(GetBitContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
797 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
798 int n= (-get_bits_count(s)) & 7;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
799 if(n) skip_bits(s, n);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
800 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
801
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
802 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
803 const void *bits, int bits_wrap, int bits_size,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
804 const void *codes, int codes_wrap, int codes_size,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
805 int flags);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
806 #define INIT_VLC_USE_STATIC 1
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
807 #define INIT_VLC_LE 2
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
808 void free_vlc(VLC *vlc);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
809
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
810 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
811 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
812 * if the vlc code is invalid and max_depth=1 than no bits will be removed
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
813 * if the vlc code is invalid and max_depth>1 than the number of bits removed
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
814 * is undefined
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
815 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
816 #define GET_VLC(code, name, gb, table, bits, max_depth)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
817 {\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
818 int n, index, nb_bits;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
819 \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
820 index= SHOW_UBITS(name, gb, bits);\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
821 code = table[index][0];\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
822 n = table[index][1];\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
823 \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
824 if(max_depth > 1 && n < 0){\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
825 LAST_SKIP_BITS(name, gb, bits)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
826 UPDATE_CACHE(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
827 \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
828 nb_bits = -n;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
829 \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
830 index= SHOW_UBITS(name, gb, nb_bits) + code;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
831 code = table[index][0];\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
832 n = table[index][1];\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
833 if(max_depth > 2 && n < 0){\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
834 LAST_SKIP_BITS(name, gb, nb_bits)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
835 UPDATE_CACHE(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
836 \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
837 nb_bits = -n;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
838 \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
839 index= SHOW_UBITS(name, gb, nb_bits) + code;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
840 code = table[index][0];\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
841 n = table[index][1];\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
842 }\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
843 }\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
844 SKIP_BITS(name, gb, n)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
845 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
846
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
847 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
848 {\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
849 int n, index, nb_bits;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
850 \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
851 index= SHOW_UBITS(name, gb, bits);\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
852 level = table[index].level;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
853 n = table[index].len;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
854 \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
855 if(max_depth > 1 && n < 0){\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
856 SKIP_BITS(name, gb, bits)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
857 if(need_update){\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
858 UPDATE_CACHE(name, gb)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
859 }\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
860 \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
861 nb_bits = -n;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
862 \
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
863 index= SHOW_UBITS(name, gb, nb_bits) + level;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
864 level = table[index].level;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
865 n = table[index].len;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
866 }\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
867 run= table[index].run;\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
868 SKIP_BITS(name, gb, n)\
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
869 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
870
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
871
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
872 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
873 * parses a vlc code, faster then get_vlc()
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
874 * @param bits is the number of bits which will be read at once, must be
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
875 * identical to nb_bits in init_vlc()
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
876 * @param max_depth is the number of times bits bits must be readed to completly
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
877 * read the longest vlc code
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
878 * = (max_vlc_length + bits - 1) / bits
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
879 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
880 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
881 int bits, int max_depth)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
882 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
883 int code;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
884
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
885 OPEN_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
886 UPDATE_CACHE(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
887
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
888 GET_VLC(code, re, s, table, bits, max_depth)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
889
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
890 CLOSE_READER(re, s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
891 return code;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
892 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
893
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
894 //#define TRACE
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
895
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
896 #ifdef TRACE
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
897 static inline void print_bin(int bits, int n){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
898 int i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
899
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
900 for(i=n-1; i>=0; i--){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
901 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
902 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
903 for(i=n; i<24; i++)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
904 av_log(NULL, AV_LOG_DEBUG, " ");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
905 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
906
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
907 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
908 int r= get_bits(s, n);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
909
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
910 print_bin(r, n);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
911 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
912 return r;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
913 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
914 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
915 int show= show_bits(s, 24);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
916 int pos= get_bits_count(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
917 int r= get_vlc2(s, table, bits, max_depth);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
918 int len= get_bits_count(s) - pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
919 int bits2= show>>(24-len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
920
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
921 print_bin(bits2, len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
922
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
923 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
924 return r;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
925 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
926 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
927 int show= show_bits(s, n);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
928 int r= get_xbits(s, n);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
929
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
930 print_bin(show, n);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
931 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
932 return r;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
933 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
934
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
935 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
936 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
937 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
938 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
939 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
940
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
941 #define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
942
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
943 #else //TRACE
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
944 #define tprintf(...) {}
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
945 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
946
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
947 static inline int decode012(GetBitContext *gb){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
948 int n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
949 n = get_bits1(gb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
950 if (n == 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
951 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
952 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
953 return get_bits1(gb) + 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
954 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
955
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
956 #endif /* BITSTREAM_H */