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