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