summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/codecs/lib/SOURCES1
-rw-r--r--apps/codecs/lib/ffmpeg_bitstream.c321
-rw-r--r--apps/codecs/lib/ffmpeg_get_bits.h (renamed from apps/codecs/lib/ffmpeg_bitstream.h)421
-rw-r--r--apps/codecs/libatrac/SOURCES2
-rw-r--r--apps/codecs/libatrac/atrac3.h2
-rw-r--r--apps/codecs/libcook/SOURCES2
-rw-r--r--apps/codecs/libcook/cook.h2
-rw-r--r--apps/codecs/libffmpegFLAC/SOURCES1
-rw-r--r--apps/codecs/libffmpegFLAC/bitstream.c62
-rw-r--r--apps/codecs/libffmpegFLAC/bitstream.h278
-rw-r--r--apps/codecs/libffmpegFLAC/shndec.c11
11 files changed, 315 insertions, 788 deletions
diff --git a/apps/codecs/lib/SOURCES b/apps/codecs/lib/SOURCES
index 49e744721d..f9b2447efe 100644
--- a/apps/codecs/lib/SOURCES
+++ b/apps/codecs/lib/SOURCES
@@ -1,6 +1,7 @@
1#if CONFIG_CODEC == SWCODEC /* software codec platforms */ 1#if CONFIG_CODEC == SWCODEC /* software codec platforms */
2codeclib.c 2codeclib.c
3fixedpoint.c 3fixedpoint.c
4ffmpeg_bitstream.c
4 5
5/* OLD MDCT */ 6/* OLD MDCT */
6/* (when all other codecs are remediated this can be remoed) */ 7/* (when all other codecs are remediated this can be remoed) */
diff --git a/apps/codecs/lib/ffmpeg_bitstream.c b/apps/codecs/lib/ffmpeg_bitstream.c
index 88e3cbfe3a..c879661c47 100644
--- a/apps/codecs/lib/ffmpeg_bitstream.c
+++ b/apps/codecs/lib/ffmpeg_bitstream.c
@@ -2,6 +2,7 @@
2 * Common bit i/o utils 2 * Common bit i/o utils
3 * Copyright (c) 2000, 2001 Fabrice Bellard 3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> 4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 * Copyright (c) 2010 Loren Merritt
5 * 6 *
6 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at> 7 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
7 * 8 *
@@ -22,7 +23,16 @@
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */ 24 */
24 25
25#include "ffmpeg_bitstream.h" 26/**
27 * @file
28 * bitstream api.
29 */
30
31//#include "avcodec.h"
32#include "ffmpeg_get_bits.h"
33//#include "put_bits.h"
34
35#define av_log(...)
26 36
27#ifdef ROCKBOX 37#ifdef ROCKBOX
28#undef DEBUGF 38#undef DEBUGF
@@ -36,27 +46,47 @@ const uint8_t ff_log2_run[32]={
36 8, 9,10,11,12,13,14,15 46 8, 9,10,11,12,13,14,15
37}; 47};
38 48
39/** 49#if 0 // unused in rockbox
40 * Same as av_mallocz_static(), but does a realloc. 50void align_put_bits(PutBitContext *s)
41 * 51{
42 * @param[in] ptr The block of memory to reallocate. 52#ifdef ALT_BITSTREAM_WRITER
43 * @param[in] size The requested size. 53 put_bits(s,( - s->index) & 7,0);
44 * @return Block of memory of requested size. 54#else
45 * @deprecated. Code which uses ff_realloc_static is broken/misdesigned 55 put_bits(s,s->bit_left & 7,0);
46 * and should correctly use static arrays 56#endif
47 */ 57}
48
49 58
50void ff_put_string(PutBitContext * pbc, const char *s, int put_zero) 59void ff_put_string(PutBitContext *pb, const char *string, int terminate_string)
51{ 60{
52 while(*s){ 61 while(*string){
53 put_bits(pbc, 8, *s); 62 put_bits(pb, 8, *string);
54 s++; 63 string++;
55 } 64 }
56 if(put_zero) 65 if(terminate_string)
57 put_bits(pbc, 8, 0); 66 put_bits(pb, 8, 0);
58} 67}
59 68
69void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
70{
71 int words= length>>4;
72 int bits= length&15;
73 int i;
74
75 if(length==0) return;
76
77 if(CONFIG_SMALL || words < 16 || put_bits_count(pb)&7){
78 for(i=0; i<words; i++) put_bits(pb, 16, AV_RB16(src + 2*i));
79 }else{
80 for(i=0; put_bits_count(pb)&31; i++)
81 put_bits(pb, 8, src[i]);
82 flush_put_bits(pb);
83 memcpy(put_bits_ptr(pb), src+i, 2*words-i);
84 skip_put_bytes(pb, 2*words-i);
85 }
86
87 put_bits(pb, bits, AV_RB16(src + 2*words)>>(16-bits));
88}
89#endif
60/* VLC decoding */ 90/* VLC decoding */
61 91
62//#define DEBUG_VLC 92//#define DEBUG_VLC
@@ -84,118 +114,143 @@ static int alloc_table(VLC *vlc, int size, int use_static)
84 index = vlc->table_size; 114 index = vlc->table_size;
85 vlc->table_size += size; 115 vlc->table_size += size;
86 if (vlc->table_size > vlc->table_allocated) { 116 if (vlc->table_size > vlc->table_allocated) {
87 if(use_static>1){ 117 if(use_static)
118 {
88 DEBUGF("init_vlc() used with too little memory : table_size > allocated_memory\n"); 119 DEBUGF("init_vlc() used with too little memory : table_size > allocated_memory\n");
120 return -1;
89 } 121 }
90 122// abort(); //cant do anything, init_vlc() is used with too little memory
123// vlc->table_allocated += (1 << vlc->bits);
124// vlc->table = av_realloc(vlc->table,
125// sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
91 if (!vlc->table) 126 if (!vlc->table)
92 return -1; 127 return -1;
93 } 128 }
94 return index; 129 return index;
95} 130}
96 131
97static int build_table(VLC *vlc, int table_nb_bits, 132/*
98 int nb_codes, 133static av_always_inline uint32_t bitswap_32(uint32_t x) {
99 const void *bits, int bits_wrap, int bits_size, 134 return av_reverse[x&0xFF]<<24
100 const void *codes, int codes_wrap, int codes_size, 135 | av_reverse[(x>>8)&0xFF]<<16
101 const void *symbols, int symbols_wrap, int symbols_size, 136 | av_reverse[(x>>16)&0xFF]<<8
102 uint32_t code_prefix, int n_prefix, int flags) 137 | av_reverse[x>>24];
103{ 138}
104 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2, symbol; 139*/
140
141typedef struct {
142 uint8_t bits;
143 uint16_t symbol;
144 /** codeword, with the first bit-to-be-read in the msb
145 * (even if intended for a little-endian bitstream reader) */
105 uint32_t code; 146 uint32_t code;
147} VLCcode;
148
149static int compare_vlcspec(const void *a, const void *b)
150{
151 const VLCcode *sa=a, *sb=b;
152 return (sa->code >> 1) - (sb->code >> 1);
153}
154
155/**
156 * Build VLC decoding tables suitable for use with get_vlc().
157 *
158 * @param vlc the context to be initted
159 *
160 * @param table_nb_bits max length of vlc codes to store directly in this table
161 * (Longer codes are delegated to subtables.)
162 *
163 * @param nb_codes number of elements in codes[]
164 *
165 * @param codes descriptions of the vlc codes
166 * These must be ordered such that codes going into the same subtable are contiguous.
167 * Sorting by VLCcode.code is sufficient, though not necessary.
168 */
169static int build_table(VLC *vlc, int table_nb_bits, int nb_codes,
170 VLCcode *codes, int flags)
171{
172 int table_size, table_index, index, symbol, subtable_bits;
173 int i, j, k, n, nb, inc;
174 uint32_t code, code_prefix;
106 VLC_TYPE (*table)[2]; 175 VLC_TYPE (*table)[2];
107 176
108 table_size = 1 << table_nb_bits; 177 table_size = 1 << table_nb_bits;
109 table_index = alloc_table(vlc, table_size, flags & (INIT_VLC_USE_STATIC|INIT_VLC_USE_NEW_STATIC)); 178 table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_NEW_STATIC);
110#ifdef DEBUG_VLC 179#ifdef DEBUG_VLC
111 DEBUGF("new table index=%d size=%d code_prefix=%x n=%d\n", 180 av_log(NULL,AV_LOG_DEBUG,"new table index=%d size=%d\n",
112 table_index, table_size, code_prefix, n_prefix); 181 table_index, table_size);
113#endif 182#endif
114 if (table_index < 0) 183 if (table_index < 0)
115 return -1; 184 return -1;
116 table = &vlc->table[table_index]; 185 table = &vlc->table[table_index];
117 186
118 for(i=0;i<table_size;i++) { 187 for (i = 0; i < table_size; i++) {
119 table[i][1] = 0; //bits 188 table[i][1] = 0; //bits
120 table[i][0] = -1; //codes 189 table[i][0] = -1; //codes
121 } 190 }
122 191
123 /* first pass: map codes and compute auxillary table sizes */ 192 /* first pass: map codes and compute auxillary table sizes */
124 for(i=0;i<nb_codes;i++) { 193 for (i = 0; i < nb_codes; i++) {
125 GET_DATA(n, bits, i, bits_wrap, bits_size); 194 n = codes[i].bits;
126 GET_DATA(code, codes, i, codes_wrap, codes_size); 195 code = codes[i].code;
127 /* we accept tables with holes */ 196 symbol = codes[i].symbol;
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 197#if defined(DEBUG_VLC) && 0
135 DEBUGF("i=%d n=%d code=0x%x\n", i, n, code); 198 av_log(NULL,AV_LOG_DEBUG,"i=%d n=%d code=0x%x\n", i, n, code);
136#endif 199#endif
137 /* if code matches the prefix, it is in the table */ 200 if (n <= table_nb_bits) {
138 n -= n_prefix; 201 /* no need to add another table */
139 if(flags & INIT_VLC_LE) 202 j = code >> (32 - table_nb_bits);
140 code_prefix2= code & (n_prefix>=32 ? (int)0xffffffff : (1 << n_prefix)-1); 203 nb = 1 << (table_nb_bits - n);
141 else 204 inc = 1;
142 code_prefix2= code >> n; 205/* if (flags & INIT_VLC_LE) {
143 if (n > 0 && code_prefix2 == (int)code_prefix) { 206 j = bitswap_32(code);
144 if (n <= table_nb_bits) { 207 inc = 1 << n;
145 /* no need to add another table */ 208 } */
146 j = (code << (table_nb_bits - n)) & (table_size - 1); 209 for (k = 0; k < nb; k++) {
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 210#ifdef DEBUG_VLC
152 DEBUGF("%4x: code=%d n=%d\n", 211 av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
153 j, i, n); 212 j, i, n);
154#endif 213#endif
155 if (table[j][1] /*bits*/ != 0) { 214 if (table[j][1] /*bits*/ != 0) {
156 DEBUGF("incorrect codes\n"); 215 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
157 return -1; 216 return -1;
158 }
159 table[j][1] = n; //bits
160 table[j][0] = symbol;
161 j++;
162 } 217 }
163 } else { 218 table[j][1] = n; //bits
164 n -= table_nb_bits; 219 table[j][0] = symbol;
165 j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1); 220 j += inc;
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 } 221 }
176 } 222 } else {
177 } 223 /* fill auxiliary table recursively */
178 224 n -= table_nb_bits;
179 /* second pass : fill auxillary tables recursively */ 225 code_prefix = code >> (32 - table_nb_bits);
180 for(i=0;i<table_size;i++) { 226 subtable_bits = n;
181 n = table[i][1]; //bits 227 codes[i].bits = n;
182 if (n < 0) { 228 codes[i].code = code << table_nb_bits;
183 n = -n; 229 for (k = i+1; k < nb_codes; k++) {
184 if (n > table_nb_bits) { 230 n = codes[k].bits - table_nb_bits;
185 n = table_nb_bits; 231 if (n <= 0)
186 table[i][1] = -n; //bits 232 break;
233 code = codes[k].code;
234 if (code >> (32 - table_nb_bits) != code_prefix)
235 break;
236 codes[k].bits = n;
237 codes[k].code = code << table_nb_bits;
238 subtable_bits = FFMAX(subtable_bits, n);
187 } 239 }
188 index = build_table(vlc, n, nb_codes, 240 subtable_bits = FFMIN(subtable_bits, table_nb_bits);
189 bits, bits_wrap, bits_size, 241 j = /*(flags & INIT_VLC_LE) ? bitswap_32(code_prefix) >> (32 - table_nb_bits) :*/ code_prefix;
190 codes, codes_wrap, codes_size, 242 table[j][1] = -subtable_bits;
191 symbols, symbols_wrap, symbols_size, 243#ifdef DEBUG_VLC
192 (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i), 244 av_log(NULL,AV_LOG_DEBUG,"%4x: n=%d (subtable)\n",
193 n_prefix + table_nb_bits, flags); 245 j, codes[i].bits + table_nb_bits);
246#endif
247 index = build_table(vlc, subtable_bits, k-i, codes+i, flags);
194 if (index < 0) 248 if (index < 0)
195 return -1; 249 return -1;
196 /* note: realloc has been done, so reload tables */ 250 /* note: realloc has been done, so reload tables */
197 table = &vlc->table[table_index]; 251 table = &vlc->table[table_index];
198 table[i][0] = index; //code 252 table[j][0] = index; //code
253 i = k-1;
199 } 254 }
200 } 255 }
201 return table_index; 256 return table_index;
@@ -228,49 +283,83 @@ static int build_table(VLC *vlc, int table_nb_bits,
228 'use_static' should be set to 1 for tables, which should be freed 283 '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. 284 with av_free_static(), 0 if free_vlc() will be used.
230*/ 285*/
286
287/* Rockbox: support for INIT_VLC_LE is currently disabled since none of our
288 codecs use it, there's a LUT based bit reverse function for this commented
289 out above (bitswap_32) and an inline asm version in libtremor/codebook.c
290 if we ever want this */
231int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, 291int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
232 const void *bits, int bits_wrap, int bits_size, 292 const void *bits, int bits_wrap, int bits_size,
233 const void *codes, int codes_wrap, int codes_size, 293 const void *codes, int codes_wrap, int codes_size,
234 const void *symbols, int symbols_wrap, int symbols_size, 294 const void *symbols, int symbols_wrap, int symbols_size,
235 int flags) 295 int flags)
236{ 296{
297 VLCcode buf[nb_codes+1]; /* worst case from cook seems to be nb_codes == 607
298 which would make this about 4.8k... */
299 int i, j, ret;
300
237 vlc->bits = nb_bits; 301 vlc->bits = nb_bits;
238 if(flags & INIT_VLC_USE_NEW_STATIC){ 302 if(flags & INIT_VLC_USE_NEW_STATIC){
239 if(vlc->table_size && vlc->table_size == vlc->table_allocated){ 303 if(vlc->table_size && vlc->table_size == vlc->table_allocated){
240 return 0; 304 return 0;
241 }else if(vlc->table_size){ 305 }else if(vlc->table_size){
242 return -1; // fatal error, we are called on a partially initialized table 306 DEBUGF("fatal error, we are called on a partially initialized table\n");
307 return -1;
308// abort(); // fatal error, we are called on a partially initialized table
243 } 309 }
244 }else if(!(flags & INIT_VLC_USE_STATIC)) { 310 }else {
245 vlc->table = NULL; 311 vlc->table = NULL;
246 vlc->table_allocated = 0; 312 vlc->table_allocated = 0;
247 vlc->table_size = 0; 313 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 } 314 }
254 315
255#ifdef DEBUG_VLC 316#ifdef DEBUG_VLC
256 DEBUGF("build table nb_codes=%d\n", nb_codes); 317 av_log(NULL,AV_LOG_DEBUG,"build table nb_codes=%d\n", nb_codes);
257#endif 318#endif
258 319
259 if (build_table(vlc, nb_bits, nb_codes, 320// buf = av_malloc((nb_codes+1)*sizeof(VLCcode));
260 bits, bits_wrap, bits_size, 321
261 codes, codes_wrap, codes_size, 322// assert(symbols_size <= 2 || !symbols);
262 symbols, symbols_wrap, symbols_size, 323 j = 0;
263 0, 0, flags) < 0) { 324#define COPY(condition)\
264 //free(&vlc->table); 325 for (i = 0; i < nb_codes; i++) {\
326 GET_DATA(buf[j].bits, bits, i, bits_wrap, bits_size);\
327 if (!(condition))\
328 continue;\
329 GET_DATA(buf[j].code, codes, i, codes_wrap, codes_size);\
330/* if (flags & INIT_VLC_LE)*/\
331/* buf[j].code = bitswap_32(buf[j].code);*/\
332/* else*/\
333 buf[j].code <<= 32 - buf[j].bits;\
334 if (symbols)\
335 GET_DATA(buf[j].symbol, symbols, i, symbols_wrap, symbols_size)\
336 else\
337 buf[j].symbol = i;\
338 j++;\
339 }
340 COPY(buf[j].bits > nb_bits);
341 // qsort is the slowest part of init_vlc, and could probably be improved or avoided
342 qsort(buf, j, sizeof(VLCcode), compare_vlcspec);
343 COPY(buf[j].bits && buf[j].bits <= nb_bits);
344 nb_codes = j;
345
346 ret = build_table(vlc, nb_bits, nb_codes, buf, flags);
347
348// av_free(buf);
349 if (ret < 0) {
350// av_freep(&vlc->table);
265 return -1; 351 return -1;
266 } 352 }
267 /* Changed the following condition to be true if table_size > table_allocated. * 353 if((flags & INIT_VLC_USE_NEW_STATIC) && vlc->table_size != vlc->table_allocated) {
268 * This would be more sensible for static tables since we want warnings for * 354 av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, vlc->table_allocated);
269 * memory shortages only. */ 355 }
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; 356 return 0;
275} 357}
276 358
359/* not used in rockbox
360void free_vlc(VLC *vlc)
361{
362 av_freep(&vlc->table);
363}
364*/
365
diff --git a/apps/codecs/lib/ffmpeg_bitstream.h b/apps/codecs/lib/ffmpeg_get_bits.h
index f0105a4afe..1a461e9e25 100644
--- a/apps/codecs/lib/ffmpeg_bitstream.h
+++ b/apps/codecs/lib/ffmpeg_get_bits.h
@@ -18,140 +18,93 @@
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */ 19 */
20 20
21#ifndef BITSTREAM_H 21/**
22#define BITSTREAM_H 22 * @file
23 * bitstream reader API header.
24 */
23 25
24#include <inttypes.h> 26#ifndef AVCODEC_GET_BITS_H
27#define AVCODEC_GET_BITS_H
28
29#include <stdint.h>
25#include <stdlib.h> 30#include <stdlib.h>
26//#include <assert.h> 31//#include <assert.h>
27#include <string.h> 32//#include "libavutil/bswap.h"
28#include <stdio.h> 33//#include "libavutil/common.h"
29#include "ffmpeg_bswap.h" 34//#include "libavutil/intreadwrite.h"
35//#include "libavutil/log.h"
36//#include "mathops.h"
37
38#include "codecs.h"
39
40/* rockbox' optimised inline functions */
41#define bswap_16(x) swap16(x)
42#define bswap_32(x) swap32(x)
43
44#ifdef ROCKBOX_BIG_ENDIAN
45#define be2me_16(x) (x)
46#define be2me_32(x) (x)
47#define le2me_16(x) bswap_16(x)
48#define le2me_32(x) bswap_32(x)
49#else
50#define be2me_16(x) bswap_16(x)
51#define be2me_32(x) bswap_32(x)
52#define le2me_16(x) (x)
53#define le2me_32(x) (x)
54#endif
55
56#define av_const __attribute__((const))
57#define av_always_inline inline __attribute__((always_inline))
30 58
31/* The following 2 defines are taken from libavutil/intreadwrite.h */ 59/* Coldfire cpu's support unaligned long reads */
60#ifdef CPU_COLDFIRE
61#define AV_RB32(x) (*(const uint32_t*)(x))
62#else
63/* The following define is taken from libavutil/intreadwrite.h */
32#define AV_RB32(x) ((((const uint8_t*)(x))[0] << 24) | \ 64#define AV_RB32(x) ((((const uint8_t*)(x))[0] << 24) | \
33 (((const uint8_t*)(x))[1] << 16) | \ 65 (((const uint8_t*)(x))[1] << 16) | \
34 (((const uint8_t*)(x))[2] << 8) | \ 66 (((const uint8_t*)(x))[2] << 8) | \
35 ((const uint8_t*)(x))[3]) 67 ((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 68#endif
69/* The following is taken from mathops.h */
57 70
58extern const uint8_t ff_reverse[256]; 71#ifndef sign_extend
59 72static inline av_const int sign_extend(int val, unsigned bits)
60#if defined(ARCH_X86) 73{
61// avoid +32 for shift optimization (gcc should do that ...) 74 return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
62static 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}
69static 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} 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 76#endif
80 77
81/* bit output */ 78#ifndef NEG_SSR32
79# define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
80#endif
82 81
83/* buf and buf_end must be present and used by every alternative writer. */ 82#ifndef NEG_USR32
84typedef struct PutBitContext { 83# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
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 84#endif
93 int size_in_bits;
94} PutBitContext;
95 85
96static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size) 86/* these 2 are from libavutil/common.h */
97{
98 if(buffer_size < 0) {
99 buffer_size = 0;
100 buffer = NULL;
101 }
102 87
103 s->size_in_bits= 8*buffer_size; 88#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
104 s->buf = buffer; 89#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
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 90
117/* return the number of bits output */ 91#if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
118static inline int put_bits_count(PutBitContext *s) 92# define ALT_BITSTREAM_READER
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 93#endif
125}
126 94
127/* pad the end of the output stream with zeros */ 95/*
128static inline void flush_put_bits(PutBitContext *s) 96#if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
129{ 97# if ARCH_ARM && !HAVE_FAST_UNALIGNED
130#ifdef ALT_BITSTREAM_WRITER 98# define A32_BITSTREAM_READER
131 align_put_bits(s); 99# else
132#else 100*/
133#ifndef BITSTREAM_WRITER_LE 101# define ALT_BITSTREAM_READER
134 s->bit_buf<<= s->bit_left; 102/*
135#endif 103//#define LIBMPEG2_BITSTREAM_READER
136 while (s->bit_left < 32) { 104//#define A32_BITSTREAM_READER
137 /* XXX: should test end of buffer */ 105# endif
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 106#endif
150} 107*/
151
152void align_put_bits(PutBitContext *s);
153void ff_put_string(PutBitContext * pbc, const char *s, int put_zero);
154void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
155 108
156/* bit input */ 109/* bit input */
157/* buffer, buffer_end and size_in_bits must be present and used by every reader */ 110/* buffer, buffer_end and size_in_bits must be present and used by every reader */
@@ -186,179 +139,6 @@ typedef struct RL_VLC_ELEM {
186 uint8_t run; 139 uint8_t run;
187} RL_VLC_ELEM; 140} RL_VLC_ELEM;
188 141
189#ifndef ALT_BITSTREAM_WRITER
190static 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
244static 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
309static 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
317static 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 */
330static 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 */
345static 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 */
358static 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: 142/* Bitstream reader API docs:
363name 143name
364 arbitrary name which is used as prefix for the internal variables 144 arbitrary name which is used as prefix for the internal variables
@@ -399,7 +179,7 @@ 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 179 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
400 180
401LAST_SKIP_BITS(name, gb, num) 181LAST_SKIP_BITS(name, gb, num)
402 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER 182 is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
403 183
404for examples see get_bits, show_bits, skip_bits, get_vlc 184for examples see get_bits, show_bits, skip_bits, get_vlc
405*/ 185*/
@@ -408,7 +188,7 @@ for examples see get_bits, show_bits, skip_bits, get_vlc
408# define MIN_CACHE_BITS 25 188# define MIN_CACHE_BITS 25
409 189
410# define OPEN_READER(name, gb)\ 190# define OPEN_READER(name, gb)\
411 int name##_index= (gb)->index;\ 191 unsigned int name##_index= (gb)->index;\
412 int name##_cache= 0;\ 192 int name##_cache= 0;\
413 193
414# define CLOSE_READER(name, gb)\ 194# define CLOSE_READER(name, gb)\
@@ -443,10 +223,10 @@ for examples see get_bits, show_bits, skip_bits, get_vlc
443 223
444# ifdef ALT_BITSTREAM_READER_LE 224# ifdef ALT_BITSTREAM_READER_LE
445# define SHOW_UBITS(name, gb, num)\ 225# define SHOW_UBITS(name, gb, num)\
446 ((name##_cache) & (NEG_USR32(0xffffffff,num))) 226 zero_extend(name##_cache, num)
447 227
448# define SHOW_SBITS(name, gb, num)\ 228# define SHOW_SBITS(name, gb, num)\
449 NEG_SSR32((name##_cache)<<(32-(num)), num) 229 sign_extend(name##_cache, num)
450# else 230# else
451# define SHOW_UBITS(name, gb, num)\ 231# define SHOW_UBITS(name, gb, num)\
452 NEG_USR32(name##_cache, num) 232 NEG_USR32(name##_cache, num)
@@ -458,7 +238,7 @@ for examples see get_bits, show_bits, skip_bits, get_vlc
458# define GET_CACHE(name, gb)\ 238# define GET_CACHE(name, gb)\
459 ((uint32_t)name##_cache) 239 ((uint32_t)name##_cache)
460 240
461static inline int get_bits_count(GetBitContext *s){ 241static inline int get_bits_count(const GetBitContext *s){
462 return s->index; 242 return s->index;
463} 243}
464 244
@@ -512,7 +292,7 @@ static inline void skip_bits_long(GetBitContext *s, int n){
512# define GET_CACHE(name, gb)\ 292# define GET_CACHE(name, gb)\
513 ((uint32_t)name##_cache) 293 ((uint32_t)name##_cache)
514 294
515static inline int get_bits_count(GetBitContext *s){ 295static inline int get_bits_count(const GetBitContext *s){
516 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count; 296 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
517} 297}
518 298
@@ -544,7 +324,7 @@ static inline void skip_bits_long(GetBitContext *s, int n){
544 324
545# define UPDATE_CACHE(name, gb)\ 325# define UPDATE_CACHE(name, gb)\
546 if(name##_bit_count > 0){\ 326 if(name##_bit_count > 0){\
547 const uint32_t next= be2me_32( *name##_buffer_ptr );\ 327 const uint32_t next= av_be2ne32( *name##_buffer_ptr );\
548 name##_cache0 |= NEG_USR32(next,name##_bit_count);\ 328 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
549 name##_cache1 |= next<<name##_bit_count;\ 329 name##_cache1 |= next<<name##_bit_count;\
550 name##_buffer_ptr++;\ 330 name##_buffer_ptr++;\
@@ -587,7 +367,7 @@ static inline void skip_bits_long(GetBitContext *s, int n){
587# define GET_CACHE(name, gb)\ 367# define GET_CACHE(name, gb)\
588 (name##_cache0) 368 (name##_cache0)
589 369
590static inline int get_bits_count(GetBitContext *s){ 370static inline int get_bits_count(const GetBitContext *s){
591 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count; 371 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
592} 372}
593 373
@@ -596,7 +376,7 @@ static inline void skip_bits_long(GetBitContext *s, int n){
596 re_bit_count += n; 376 re_bit_count += n;
597 re_buffer_ptr += re_bit_count>>5; 377 re_buffer_ptr += re_bit_count>>5;
598 re_bit_count &= 31; 378 re_bit_count &= 31;
599 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count; 379 re_cache0 = av_be2ne32( re_buffer_ptr[-1] ) << re_bit_count;
600 re_cache1 = 0; 380 re_cache1 = 0;
601 UPDATE_CACHE(re, s) 381 UPDATE_CACHE(re, s)
602 CLOSE_READER(re, s) 382 CLOSE_READER(re, s)
@@ -669,7 +449,7 @@ static inline void skip_bits(GetBitContext *s, int n){
669 449
670static inline unsigned int get_bits1(GetBitContext *s){ 450static inline unsigned int get_bits1(GetBitContext *s){
671#ifdef ALT_BITSTREAM_READER 451#ifdef ALT_BITSTREAM_READER
672 int index= s->index; 452 unsigned int index= s->index;
673 uint8_t result= s->buffer[ index>>3 ]; 453 uint8_t result= s->buffer[ index>>3 ];
674#ifdef ALT_BITSTREAM_READER_LE 454#ifdef ALT_BITSTREAM_READER_LE
675 result>>= (index&0x07); 455 result>>= (index&0x07);
@@ -699,7 +479,7 @@ static inline void skip_bits1(GetBitContext *s){
699 * reads 0-32 bits. 479 * reads 0-32 bits.
700 */ 480 */
701static inline unsigned int get_bits_long(GetBitContext *s, int n){ 481static inline unsigned int get_bits_long(GetBitContext *s, int n){
702 if(n<=17) return get_bits(s, n); 482 if(n<=MIN_CACHE_BITS) return get_bits(s, n);
703 else{ 483 else{
704#ifdef ALT_BITSTREAM_READER_LE 484#ifdef ALT_BITSTREAM_READER_LE
705 int ret= get_bits(s, 16); 485 int ret= get_bits(s, 16);
@@ -711,42 +491,43 @@ static inline unsigned int get_bits_long(GetBitContext *s, int n){
711 } 491 }
712} 492}
713 493
714#if 0
715/** 494/**
716 * reads 0-32 bits as a signed integer. 495 * reads 0-32 bits as a signed integer.
717 */ 496 */
718static inline int get_sbits_long(GetBitContext *s, int n) { 497static inline int get_sbits_long(GetBitContext *s, int n) {
719 return sign_extend(get_bits_long(s, n), n); 498 return sign_extend(get_bits_long(s, n), n);
720} 499}
721#endif
722 500
723/** 501/**
724 * shows 0-32 bits. 502 * shows 0-32 bits.
725 */ 503 */
726static inline unsigned int show_bits_long(GetBitContext *s, int n){ 504static inline unsigned int show_bits_long(GetBitContext *s, int n){
727 if(n<=17) return show_bits(s, n); 505 if(n<=MIN_CACHE_BITS) return show_bits(s, n);
728 else{ 506 else{
729 GetBitContext gb= *s; 507 GetBitContext gb= *s;
730 return get_bits_long(&gb, n); 508 return get_bits_long(&gb, n);
731 } 509 }
732} 510}
733 511
734#if 0 512/* not used
735static inline int check_marker(GetBitContext *s, const char *msg) 513static inline int check_marker(GetBitContext *s, const char *msg)
736{ 514{
737 int bit= get_bits1(s); 515 int bit= get_bits1(s);
738 if(!bit) 516 if(!bit)
739 printf("Marker bit missing %s\n", msg); 517 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
740 518
741 return bit; 519 return bit;
742} 520}
743#endif 521*/
744 522
745/** 523/**
746 * init GetBitContext. 524 * init GetBitContext.
747 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits 525 * @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 526 * 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 527 * @param bit_size the size of the buffer in bits
528 *
529 * While GetBitContext stores the buffer size, for performance reasons you are
530 * responsible for checking for the buffer end yourself (take advantage of the padding)!
750 */ 531 */
751static inline void init_get_bits(GetBitContext *s, 532static inline void init_get_bits(GetBitContext *s,
752 const uint8_t *buffer, int bit_size) 533 const uint8_t *buffer, int bit_size)
@@ -793,7 +574,6 @@ int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
793 const void *codes, int codes_wrap, int codes_size, 574 const void *codes, int codes_wrap, int codes_size,
794 const void *symbols, int symbols_wrap, int symbols_size, 575 const void *symbols, int symbols_wrap, int symbols_size,
795 int flags); 576 int flags);
796#define INIT_VLC_USE_STATIC 1 ///< VERY strongly deprecated and forbidden
797#define INIT_VLC_LE 2 577#define INIT_VLC_LE 2
798#define INIT_VLC_USE_NEW_STATIC 4 578#define INIT_VLC_USE_NEW_STATIC 4
799void free_vlc(VLC *vlc); 579void free_vlc(VLC *vlc);
@@ -809,13 +589,14 @@ void free_vlc(VLC *vlc);
809 589
810/** 590/**
811 * 591 *
812 * if the vlc code is invalid and max_depth=1 than no bits will be removed 592 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
813 * if the vlc code is invalid and max_depth>1 than the number of bits removed 593 * If the vlc code is invalid and max_depth>1, then the number of bits removed
814 * is undefined 594 * is undefined.
815 */ 595 */
816#define GET_VLC(code, name, gb, table, bits, max_depth)\ 596#define GET_VLC(code, name, gb, table, bits, max_depth)\
817{\ 597{\
818 int n, index, nb_bits;\ 598 int n, nb_bits;\
599 unsigned int index;\
819\ 600\
820 index= SHOW_UBITS(name, gb, bits);\ 601 index= SHOW_UBITS(name, gb, bits);\
821 code = table[index][0];\ 602 code = table[index][0];\
@@ -846,7 +627,8 @@ void free_vlc(VLC *vlc);
846 627
847#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\ 628#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
848{\ 629{\
849 int n, index, nb_bits;\ 630 int n, nb_bits;\
631 unsigned int index;\
850\ 632\
851 index= SHOW_UBITS(name, gb, bits);\ 633 index= SHOW_UBITS(name, gb, bits);\
852 level = table[index].level;\ 634 level = table[index].level;\
@@ -877,7 +659,7 @@ void free_vlc(VLC *vlc);
877 * read the longest vlc code 659 * read the longest vlc code
878 * = (max_vlc_length + bits - 1) / bits 660 * = (max_vlc_length + bits - 1) / bits
879 */ 661 */
880static inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2], 662static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
881 int bits, int max_depth) 663 int bits, int max_depth)
882{ 664{
883 int code; 665 int code;
@@ -898,17 +680,17 @@ static inline void print_bin(int bits, int n){
898 int i; 680 int i;
899 681
900 for(i=n-1; i>=0; i--){ 682 for(i=n-1; i>=0; i--){
901 printf("%d", (bits>>i)&1); 683 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
902 } 684 }
903 for(i=n; i<24; i++) 685 for(i=n; i<24; i++)
904 printf(" "); 686 av_log(NULL, AV_LOG_DEBUG, " ");
905} 687}
906 688
907static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){ 689static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
908 int r= get_bits(s, n); 690 int r= get_bits(s, n);
909 691
910 print_bin(r, n); 692 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); 693 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);
912 return r; 694 return r;
913} 695}
914static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){ 696static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
@@ -920,7 +702,7 @@ static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits
920 702
921 print_bin(bits2, len); 703 print_bin(bits2, len);
922 704
923 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line); 705 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
924 return r; 706 return r;
925} 707}
926static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){ 708static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
@@ -928,7 +710,7 @@ static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const cha
928 int r= get_xbits(s, n); 710 int r= get_xbits(s, n);
929 711
930 print_bin(show, n); 712 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); 713 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);
932 return r; 714 return r;
933} 715}
934 716
@@ -938,7 +720,7 @@ static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const cha
938#define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__) 720#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__) 721#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
940 722
941#define tprintf(p, ...) printf 723#define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
942 724
943#else //TRACE 725#else //TRACE
944#define tprintf(p, ...) {} 726#define tprintf(p, ...) {}
@@ -960,4 +742,9 @@ static inline int decode210(GetBitContext *gb){
960 return 2 - get_bits1(gb); 742 return 2 - get_bits1(gb);
961} 743}
962 744
963#endif /* BITSTREAM_H */ 745static inline int get_bits_left(GetBitContext *gb)
746{
747 return gb->size_in_bits - get_bits_count(gb);
748}
749
750#endif /* AVCODEC_GET_BITS_H */
diff --git a/apps/codecs/libatrac/SOURCES b/apps/codecs/libatrac/SOURCES
index 5a033c1732..3eaf4c9c3b 100644
--- a/apps/codecs/libatrac/SOURCES
+++ b/apps/codecs/libatrac/SOURCES
@@ -2,4 +2,4 @@ atrac3.c
2#if defined(CPU_ARM) 2#if defined(CPU_ARM)
3atrac3_arm.S 3atrac3_arm.S
4#endif 4#endif
5../lib/ffmpeg_bitstream.c 5
diff --git a/apps/codecs/libatrac/atrac3.h b/apps/codecs/libatrac/atrac3.h
index d3fdc5056a..74dd992e1b 100644
--- a/apps/codecs/libatrac/atrac3.h
+++ b/apps/codecs/libatrac/atrac3.h
@@ -19,7 +19,7 @@
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21 21
22#include "ffmpeg_bitstream.h" 22#include "ffmpeg_get_bits.h"
23#include "../librm/rm.h" 23#include "../librm/rm.h"
24#include "codeclib.h" 24#include "codeclib.h"
25 25
diff --git a/apps/codecs/libcook/SOURCES b/apps/codecs/libcook/SOURCES
index d093173aea..b656fdd2f7 100644
--- a/apps/codecs/libcook/SOURCES
+++ b/apps/codecs/libcook/SOURCES
@@ -1,2 +1,2 @@
1cook.c 1cook.c
2../lib/ffmpeg_bitstream.c 2
diff --git a/apps/codecs/libcook/cook.h b/apps/codecs/libcook/cook.h
index 93abf9a52d..51d6fd34f2 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 "ffmpeg_bitstream.h" 26#include "ffmpeg_get_bits.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/libffmpegFLAC/SOURCES b/apps/codecs/libffmpegFLAC/SOURCES
index deed19bcec..63094b30a6 100644
--- a/apps/codecs/libffmpegFLAC/SOURCES
+++ b/apps/codecs/libffmpegFLAC/SOURCES
@@ -1,4 +1,3 @@
1bitstream.c
2decoder.c 1decoder.c
3shndec.c 2shndec.c
4#if defined(CPU_COLDFIRE) 3#if defined(CPU_COLDFIRE)
diff --git a/apps/codecs/libffmpegFLAC/bitstream.c b/apps/codecs/libffmpegFLAC/bitstream.c
deleted file mode 100644
index e53ec0fd46..0000000000
--- a/apps/codecs/libffmpegFLAC/bitstream.c
+++ /dev/null
@@ -1,62 +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 library 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 of the License, or (at your option) any later version.
10 *
11 * This library 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 this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
21 */
22
23/**
24 * @file bitstream.c
25 * bitstream api.
26 */
27
28#include <stdio.h>
29#include "bitstream.h"
30
31/* bit input functions */
32
33/**
34 * reads 0-32 bits.
35 */
36unsigned int get_bits_long(GetBitContext *s, int n){
37 if(n<=17) return get_bits(s, n);
38 else{
39 int ret= get_bits(s, 16) << (n-16);
40 return ret | get_bits(s, n-16);
41 }
42}
43
44/**
45 * shows 0-32 bits.
46 */
47unsigned int show_bits_long(GetBitContext *s, int n){
48 if(n<=17) return show_bits(s, n);
49 else{
50 GetBitContext gb= *s;
51 int ret= get_bits_long(s, n);
52 *s= gb;
53 return ret;
54 }
55}
56
57void align_get_bits(GetBitContext *s)
58{
59 int n= (-get_bits_count(s)) & 7;
60 if(n) skip_bits(s, n);
61}
62
diff --git a/apps/codecs/libffmpegFLAC/bitstream.h b/apps/codecs/libffmpegFLAC/bitstream.h
index 9a8c548d95..1333b9f4b8 100644
--- a/apps/codecs/libffmpegFLAC/bitstream.h
+++ b/apps/codecs/libffmpegFLAC/bitstream.h
@@ -7,6 +7,7 @@
7#define BITSTREAM_H 7#define BITSTREAM_H
8 8
9#include <inttypes.h> 9#include <inttypes.h>
10#include "ffmpeg_get_bits.h"
10 11
11#ifndef BUILD_STANDALONE 12#ifndef BUILD_STANDALONE
12 #include <config.h> 13 #include <config.h>
@@ -61,281 +62,4 @@
61 } 62 }
62#endif 63#endif
63 64
64/* FLAC files are big-endian */
65#define ALT_BITSTREAM_READER_BE
66
67#define NEG_SSR32(a,s) (((int32_t)(a))>>(32-(s)))
68#define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
69
70/* bit input */
71/* buffer, buffer_end and size_in_bits must be present and used by every reader */
72typedef struct GetBitContext {
73 const uint8_t *buffer, *buffer_end;
74 int index;
75 int size_in_bits;
76} GetBitContext;
77
78#define VLC_TYPE int16_t
79
80typedef struct VLC {
81 int bits;
82 VLC_TYPE (*table)[2]; ///< code, bits
83 int table_size, table_allocated;
84} VLC;
85
86typedef struct RL_VLC_ELEM {
87 int16_t level;
88 int8_t len;
89 uint8_t run;
90} RL_VLC_ELEM;
91
92#if defined(ARCH_SPARC) || defined(ARCH_ARMV4L)
93#define UNALIGNED_STORES_ARE_BAD
94#endif
95
96/* used to avoid missaligned exceptions on some archs (alpha, ...) */
97#if defined(ARCH_X86) || defined(ARCH_X86_64)
98# define unaligned32(a) (*(const uint32_t*)(a))
99#else
100# ifdef __GNUC__
101static inline uint32_t unaligned32(const void *v) {
102 struct Unaligned {
103 uint32_t i;
104 } __attribute__((packed));
105
106 return ((const struct Unaligned *) v)->i;
107}
108# elif defined(__DECC)
109static inline uint32_t unaligned32(const void *v) {
110 return *(const __unaligned uint32_t *) v;
111}
112# else
113static inline uint32_t unaligned32(const void *v) {
114 return *(const uint32_t *) v;
115}
116# endif
117#endif //!ARCH_X86
118
119
120/* Bitstream reader API docs:
121name
122 abritary name which is used as prefix for the internal variables
123
124gb
125 getbitcontext
126
127OPEN_READER(name, gb)
128 loads gb into local variables
129
130CLOSE_READER(name, gb)
131 stores local vars in gb
132
133UPDATE_CACHE(name, gb)
134 refills the internal cache from the bitstream
135 after this call at least MIN_CACHE_BITS will be available,
136
137GET_CACHE(name, gb)
138 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
139
140SHOW_UBITS(name, gb, num)
141 will return the next num bits
142
143SHOW_SBITS(name, gb, num)
144 will return the next num bits and do sign extension
145
146SKIP_BITS(name, gb, num)
147 will skip over the next num bits
148 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
149
150SKIP_CACHE(name, gb, num)
151 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
152
153SKIP_COUNTER(name, gb, num)
154 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
155
156LAST_SKIP_CACHE(name, gb, num)
157 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
158
159LAST_SKIP_BITS(name, gb, num)
160 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
161
162for examples see get_bits, show_bits, skip_bits, get_vlc
163*/
164
165static inline int unaligned32_be(const void *v)
166{
167#ifdef CONFIG_ALIGN
168 const uint8_t *p=v;
169 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
170#else
171 return betoh32( unaligned32(v)); //original
172#endif
173}
174
175static inline int unaligned32_le(const void *v)
176{
177#ifdef CONFIG_ALIGN
178 const uint8_t *p=v;
179 return (((p[3]<<8) | p[2])<<16) | (p[1]<<8) | (p[0]);
180#else
181 return letoh32( unaligned32(v)); //original
182#endif
183}
184
185# define MIN_CACHE_BITS 25
186
187# define OPEN_READER(name, gb)\
188 int name##_index= (gb)->index;\
189 int name##_cache= 0;\
190
191# define CLOSE_READER(name, gb)\
192 (gb)->index= name##_index;\
193
194# ifdef ALT_BITSTREAM_READER_LE
195# define UPDATE_CACHE(name, gb)\
196 name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
197
198# define SKIP_CACHE(name, gb, num)\
199 name##_cache >>= (num);
200# else
201# define UPDATE_CACHE(name, gb)\
202 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
203
204# define SKIP_CACHE(name, gb, num)\
205 name##_cache <<= (num);
206# endif
207
208// FIXME name?
209# define SKIP_COUNTER(name, gb, num)\
210 name##_index += (num);\
211
212# define SKIP_BITS(name, gb, num)\
213 {\
214 SKIP_CACHE(name, gb, num)\
215 SKIP_COUNTER(name, gb, num)\
216 }\
217
218# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
219# define LAST_SKIP_CACHE(name, gb, num) ;
220
221# ifdef ALT_BITSTREAM_READER_LE
222# define SHOW_UBITS(name, gb, num)\
223 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
224# else
225# define SHOW_UBITS(name, gb, num)\
226 NEG_USR32(name##_cache, num)
227# endif
228
229# define SHOW_SBITS(name, gb, num)\
230 NEG_SSR32(name##_cache, num)
231
232# define GET_CACHE(name, gb)\
233 ((uint32_t)name##_cache)
234
235static inline int get_bits_count(GetBitContext *s){
236 return s->index;
237}
238
239static inline int get_sbits(GetBitContext *s, int n){
240 register int tmp;
241 OPEN_READER(re, s)
242 UPDATE_CACHE(re, s)
243 tmp= SHOW_SBITS(re, s, n);
244 LAST_SKIP_BITS(re, s, n)
245 CLOSE_READER(re, s)
246 return tmp;
247}
248
249/**
250 * reads 0-17 bits.
251 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
252 */
253static inline unsigned int get_bits(GetBitContext *s, int n){
254 register int tmp;
255 OPEN_READER(re, s)
256 UPDATE_CACHE(re, s)
257 tmp= SHOW_UBITS(re, s, n);
258 LAST_SKIP_BITS(re, s, n)
259 CLOSE_READER(re, s)
260 return tmp;
261}
262
263unsigned int get_bits_long(GetBitContext *s, int n) ICODE_ATTR_FLAC;
264
265/**
266 * shows 0-17 bits.
267 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
268 */
269static inline unsigned int show_bits(GetBitContext *s, int n){
270 register int tmp;
271 OPEN_READER(re, s)
272 UPDATE_CACHE(re, s)
273 tmp= SHOW_UBITS(re, s, n);
274// CLOSE_READER(re, s)
275 return tmp;
276}
277
278unsigned int show_bits_long(GetBitContext *s, int n) ICODE_ATTR_FLAC;
279
280static inline void skip_bits(GetBitContext *s, int n){
281 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
282 OPEN_READER(re, s)
283 UPDATE_CACHE(re, s)
284 LAST_SKIP_BITS(re, s, n)
285 CLOSE_READER(re, s)
286}
287
288static inline unsigned int get_bits1(GetBitContext *s){
289 int index= s->index;
290 uint8_t result= s->buffer[ index>>3 ];
291#ifdef ALT_BITSTREAM_READER_LE
292 result>>= (index&0x07);
293 result&= 1;
294#else
295 result<<= (index&0x07);
296 result>>= 8 - 1;
297#endif
298 index++;
299 s->index= index;
300
301 return result;
302}
303
304static inline unsigned int show_bits1(GetBitContext *s){
305 return show_bits(s, 1);
306}
307
308static inline void skip_bits1(GetBitContext *s){
309 skip_bits(s, 1);
310}
311
312/**
313 * init GetBitContext.
314 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
315 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
316 * @param bit_size the size of the buffer in bits
317 */
318static inline void init_get_bits(GetBitContext *s,
319 const uint8_t *buffer, int bit_size)
320{
321 int buffer_size= (bit_size+7)>>3;
322 if(buffer_size < 0 || bit_size < 0) {
323 buffer_size = bit_size = 0;
324 buffer = 0;
325 }
326
327 s->buffer= buffer;
328 s->size_in_bits= bit_size;
329 s->buffer_end= buffer + buffer_size;
330 s->index=0;
331 {
332 OPEN_READER(re, s)
333 UPDATE_CACHE(re, s)
334 UPDATE_CACHE(re, s)
335 CLOSE_READER(re, s)
336 }
337}
338
339void align_get_bits(GetBitContext *s) ICODE_ATTR_FLAC;
340
341#endif /* BITSTREAM_H */ 65#endif /* BITSTREAM_H */
diff --git a/apps/codecs/libffmpegFLAC/shndec.c b/apps/codecs/libffmpegFLAC/shndec.c
index fb11f77bfa..b107b356d7 100644
--- a/apps/codecs/libffmpegFLAC/shndec.c
+++ b/apps/codecs/libffmpegFLAC/shndec.c
@@ -54,22 +54,11 @@
54#define VERBATIM_BYTE_SIZE 8 54#define VERBATIM_BYTE_SIZE 8
55#define CANONICAL_HEADER_SIZE 44 55#define CANONICAL_HEADER_SIZE 44
56 56
57#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
58#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
59#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24)) 57#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
60 58
61#define get_le16(gb) bswap_16(get_bits_long(gb, 16)) 59#define get_le16(gb) bswap_16(get_bits_long(gb, 16))
62#define get_le32(gb) bswap_32(get_bits_long(gb, 32)) 60#define get_le32(gb) bswap_32(get_bits_long(gb, 32))
63 61
64static uint32_t bswap_32(uint32_t x){
65 x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
66 return (x>>16) | (x<<16);
67}
68
69static uint16_t bswap_16(uint16_t x){
70 return (x>>8) | (x<<8);
71}
72
73/* converts fourcc string to int */ 62/* converts fourcc string to int */
74static int ff_get_fourcc(const char *s){ 63static int ff_get_fourcc(const char *s){
75 //assert( strlen(s)==4 ); 64 //assert( strlen(s)==4 );