summaryrefslogtreecommitdiff
path: root/apps/codecs
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs')
-rw-r--r--apps/codecs/libwma/SOURCES2
-rw-r--r--apps/codecs/libwma/bitstream.c (renamed from apps/codecs/libwma/common.c)198
-rw-r--r--apps/codecs/libwma/bitstream.h989
-rw-r--r--apps/codecs/libwma/common.h408
-rw-r--r--apps/codecs/libwma/dsputil.h84
-rw-r--r--apps/codecs/libwma/wmadata.h29
-rw-r--r--apps/codecs/libwma/wmadec.h2
-rw-r--r--apps/codecs/libwma/wmadeci.c62
-rw-r--r--apps/codecs/libwma/wmafixed.c1
9 files changed, 1143 insertions, 632 deletions
diff --git a/apps/codecs/libwma/SOURCES b/apps/codecs/libwma/SOURCES
index 65166464fd..43b4831787 100644
--- a/apps/codecs/libwma/SOURCES
+++ b/apps/codecs/libwma/SOURCES
@@ -1,3 +1,3 @@
1wmadeci.c 1wmadeci.c
2common.c
3wmafixed.c 2wmafixed.c
3bitstream.c \ No newline at end of file
diff --git a/apps/codecs/libwma/common.c b/apps/codecs/libwma/bitstream.c
index b4577a4c74..12cf0518ee 100644
--- a/apps/codecs/libwma/common.c
+++ b/apps/codecs/libwma/bitstream.c
@@ -3,29 +3,43 @@
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 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public 9 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. 11 * version 2.1 of the License, or (at your option) any later version.
10 * 12 *
11 * This library is distributed in the hope that it will be useful, 13 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details. 16 * Lesser General Public License for more details.
15 * 17 *
16 * You should have received a copy of the GNU Lesser General Public 18 * 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 19 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * 21 *
20 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at> 22 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
21 */ 23 */
22 24
23/** 25/**
24 * @file common.c 26 * @file bitstream.c
25 * common internal api. 27 * bitstream api.
28 */
29
30#include "bitstream.h"
31
32/**
33 * Same as av_mallocz_static(), but does a realloc.
34 *
35 * @param[in] ptr The block of memory to reallocate.
36 * @param[in] size The requested size.
37 * @return Block of memory of requested size.
38 * @deprecated. Code which uses ff_realloc_static is broken/missdesigned
39 * and should correctly use static arrays
26 */ 40 */
41attribute_deprecated void *ff_realloc_static(void *ptr, unsigned int size);
27 42
28#include "common.h"
29 43
30const uint8_t ff_sqrt_tab[128]={ 44const uint8_t ff_sqrt_tab[128]={
31 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 45 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
@@ -45,64 +59,24 @@ const uint8_t ff_log2_tab[256]={
45 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 59 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
46}; 60};
47 61
48/**
49 * init GetBitContext.
50 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
51 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
52 * @param bit_size the size of the buffer in bits
53 */
54void init_get_bits(GetBitContext *s,
55 const uint8_t *buffer, int bit_size)
56{
57 const int buffer_size= (bit_size+7)>>3;
58
59 s->buffer= buffer;
60 s->size_in_bits= bit_size;
61 s->buffer_end= buffer + buffer_size;
62 s->index=0;
63 {
64 OPEN_READER(re, s)
65 UPDATE_CACHE(re, s)
66 UPDATE_CACHE(re, s)
67 CLOSE_READER(re, s)
68 }
69}
70
71/**
72 * reads 0-32 bits.
73 */
74unsigned int get_bits_long(GetBitContext *s, int n){
75 if(n<=17) return get_bits(s, n);
76 else{
77 int ret= get_bits(s, 16) << (n-16);
78 return ret | get_bits(s, n-16);
79 }
80}
81
82/**
83 * shows 0-32 bits.
84 */
85unsigned int show_bits_long(GetBitContext *s, int n){
86 if(n<=17) return show_bits(s, n);
87 else{
88 GetBitContext gb= *s;
89 int ret= get_bits_long(s, n);
90 *s= gb;
91 return ret;
92 }
93}
94 62
95void align_get_bits(GetBitContext *s) 63void align_put_bits(PutBitContext *s)
96{ 64{
97 int n= (-get_bits_count(s)) & 7; 65#ifdef ALT_BITSTREAM_WRITER
98 if(n) skip_bits(s, n); 66 put_bits(s,( - s->index) & 7,0);
67#else
68 put_bits(s,s->bit_left & 7,0);
69#endif
99} 70}
100 71
101int check_marker(GetBitContext *s, const char *msg) 72void ff_put_string(PutBitContext * pbc, char *s, int put_zero)
102{ 73{
103 (void)msg; 74 while(*s){
104 int bit= get_bits1(s); 75 put_bits(pbc, 8, *s);
105 return bit; 76 s++;
77 }
78 if(put_zero)
79 put_bits(pbc, 8, 0);
106} 80}
107 81
108/* VLC decoding */ 82/* VLC decoding */
@@ -125,6 +99,7 @@ int check_marker(GetBitContext *s, const char *msg)
125 }\ 99 }\
126} 100}
127 101
102
128static int alloc_table(VLC *vlc, int size) 103static int alloc_table(VLC *vlc, int size)
129{ 104{
130 int index; 105 int index;
@@ -139,59 +114,74 @@ static int alloc_table(VLC *vlc, int size)
139} 114}
140 115
141static int build_table(VLC *vlc, int table_nb_bits, 116static int build_table(VLC *vlc, int table_nb_bits,
142 int nb_codes, 117 int nb_codes,
143 const void *bits, int bits_wrap, int bits_size, 118 const void *bits, int bits_wrap, int bits_size,
144 const void *codes, int codes_wrap, int codes_size, 119 const void *codes, int codes_wrap, int codes_size,
145 uint32_t code_prefix, int n_prefix) 120 uint32_t code_prefix, int n_prefix)
146{ 121{
147 int i, j, k, n, table_size, table_index, nb, n1, index; 122 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2;
148 uint32_t code; 123 uint32_t code;
124 int flags = 0;
149 VLC_TYPE (*table)[2]; 125 VLC_TYPE (*table)[2];
150 126
151 table_size = 1 << table_nb_bits; 127 table_size = 1 << table_nb_bits;
152 table_index = alloc_table(vlc, table_size); 128 table_index = alloc_table(vlc, table_size);
129#ifdef DEBUG_VLC
130 printf("new table index=%d size=%d code_prefix=%x n=%d\n",
131 table_index, table_size, code_prefix, n_prefix);
132#endif
153 if (table_index < 0) 133 if (table_index < 0)
154 return -1; 134 return -1;
155 table = &vlc->table[table_index]; 135 table = &vlc->table[table_index];
156 136
157 for(i=0;i<table_size;i++) 137 for(i=0;i<table_size;i++) {
158 {
159 table[i][1] = 0; //bits 138 table[i][1] = 0; //bits
160 table[i][0] = -1; //codes 139 table[i][0] = -1; //codes
161 } 140 }
162 141
163 /* first pass: map codes and compute auxillary table sizes */ 142 /* first pass: map codes and compute auxillary table sizes */
164 for(i=0;i<nb_codes;i++) 143 for(i=0;i<nb_codes;i++) {
165 {
166 GET_DATA(n, bits, i, bits_wrap, bits_size); 144 GET_DATA(n, bits, i, bits_wrap, bits_size);
167 GET_DATA(code, codes, i, codes_wrap, codes_size); 145 GET_DATA(code, codes, i, codes_wrap, codes_size);
168 /* we accept tables with holes */ 146 /* we accept tables with holes */
169 if (n <= 0) 147 if (n <= 0)
170 continue; 148 continue;
149#if defined(DEBUG_VLC) && 0
150 printf("i=%d n=%d code=0x%x\n", i, n, code);
151#endif
171 /* if code matches the prefix, it is in the table */ 152 /* if code matches the prefix, it is in the table */
172 n -= n_prefix; 153 n -= n_prefix;
173 if (n > 0 && (code >> n) == code_prefix) 154 if(flags & INIT_VLC_LE)
174 { 155 code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1);
175 if (n <= table_nb_bits) 156 else
176 { 157 code_prefix2= code >> n;
158 if (n > 0 && code_prefix2 == code_prefix) {
159 if (n <= table_nb_bits) {
177 /* no need to add another table */ 160 /* no need to add another table */
178 j = (code << (table_nb_bits - n)) & (table_size - 1); 161 j = (code << (table_nb_bits - n)) & (table_size - 1);
179 nb = 1 << (table_nb_bits - n); 162 nb = 1 << (table_nb_bits - n);
180 for(k=0;k<nb;k++) 163 for(k=0;k<nb;k++) {
181 { 164 if(flags & INIT_VLC_LE)
182 if (table[j][1] /*bits*/ != 0) 165 j = (code >> n_prefix) + (k<<n);
183 { 166#ifdef DEBUG_VLC
184 // PJJ exit(-1); 167 av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
168 j, i, n);
169#endif
170 if (table[j][1] /*bits*/ != 0) {
171 // av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
172 return -1;
185 } 173 }
186 table[j][1] = n; //bits 174 table[j][1] = n; //bits
187 table[j][0] = i; //code 175 table[j][0] = i; //code
188 j++; 176 j++;
189 } 177 }
190 } 178 } else {
191 else
192 {
193 n -= table_nb_bits; 179 n -= table_nb_bits;
194 j = (code >> n) & ((1 << table_nb_bits) - 1); 180 j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
181#ifdef DEBUG_VLC
182 printf("%4x: n=%d (subtable)\n",
183 j, n);
184#endif
195 /* compute table size */ 185 /* compute table size */
196 n1 = -table[j][1]; //bits 186 n1 = -table[j][1]; //bits
197 if (n > n1) 187 if (n > n1)
@@ -202,21 +192,18 @@ static int build_table(VLC *vlc, int table_nb_bits,
202 } 192 }
203 193
204 /* second pass : fill auxillary tables recursively */ 194 /* second pass : fill auxillary tables recursively */
205 for(i=0;i<table_size;i++) 195 for(i=0;i<table_size;i++) {
206 {
207 n = table[i][1]; //bits 196 n = table[i][1]; //bits
208 if (n < 0) 197 if (n < 0) {
209 {
210 n = -n; 198 n = -n;
211 if (n > table_nb_bits) 199 if (n > table_nb_bits) {
212 {
213 n = table_nb_bits; 200 n = table_nb_bits;
214 table[i][1] = -n; //bits 201 table[i][1] = -n; //bits
215 } 202 }
216 index = build_table(vlc, n, nb_codes, 203 index = build_table(vlc, n, nb_codes,
217 bits, bits_wrap, bits_size, 204 bits, bits_wrap, bits_size,
218 codes, codes_wrap, codes_size, 205 codes, codes_wrap, codes_size,
219 (code_prefix << table_nb_bits) | i, 206 (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
220 n_prefix + table_nb_bits); 207 n_prefix + table_nb_bits);
221 if (index < 0) 208 if (index < 0)
222 return -1; 209 return -1;
@@ -228,6 +215,7 @@ static int build_table(VLC *vlc, int table_nb_bits,
228 return table_index; 215 return table_index;
229} 216}
230 217
218
231/* Build VLC decoding tables suitable for use with get_vlc(). 219/* Build VLC decoding tables suitable for use with get_vlc().
232 220
233 'nb_bits' set thee decoding table size (2^nb_bits) entries. The 221 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
@@ -248,21 +236,39 @@ static int build_table(VLC *vlc, int table_nb_bits,
248 236
249 'wrap' and 'size' allows to use any memory configuration and types 237 'wrap' and 'size' allows to use any memory configuration and types
250 (byte/word/long) to store the 'bits' and 'codes' tables. 238 (byte/word/long) to store the 'bits' and 'codes' tables.
239
240 'use_static' should be set to 1 for tables, which should be freed
241 with av_free_static(), 0 if free_vlc() will be used.
251*/ 242*/
252int init_vlc(VLC *vlc, int nb_bits, int nb_codes, 243int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
253 const void *bits, int bits_wrap, int bits_size, 244 const void *bits, int bits_wrap, int bits_size,
254 const void *codes, int codes_wrap, int codes_size) 245 const void *codes, int codes_wrap, int codes_size,
246 int flags)
255{ 247{
248
256 vlc->bits = nb_bits; 249 vlc->bits = nb_bits;
257 vlc->table_size = 0; 250 vlc->table_size = 0;
251
252#ifdef DEBUG_VLC
253 printf("build table nb_codes=%d\n", nb_codes);
254#endif
258 255
259 if (build_table(vlc, nb_bits, nb_codes, 256 if (build_table(vlc, nb_bits, nb_codes,
260 bits, bits_wrap, bits_size, 257 bits, bits_wrap, bits_size,
261 codes, codes_wrap, codes_size, 258 codes, codes_wrap, codes_size,
262 0, 0) < 0) 259 0, 0) < 0) {
263 { 260 //av_free(vlc->table);
264
265 return -1; 261 return -1;
266 } 262 }
267 return 0; 263 /* return flags to block gcc warning while allowing us to keep
264 * consistent with ffmpeg's function parameters
265 */
266 return flags;
267}
268
269/*
270void free_vlc(VLC *vlc)
271{
272 av_free(vlc->table);
268} 273}
274*/
diff --git a/apps/codecs/libwma/bitstream.h b/apps/codecs/libwma/bitstream.h
new file mode 100644
index 0000000000..c02845e09b
--- /dev/null
+++ b/apps/codecs/libwma/bitstream.h
@@ -0,0 +1,989 @@
1/*
2 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file bitstream.h
23 * bitstream api header.
24 */
25
26#ifndef BITSTREAM_H
27#define BITSTREAM_H
28
29#define av_always_inline inline
30#define attribute_deprecated
31
32#include <inttypes.h>
33#include "ffmpeg_config.h"
34#include <stdlib.h>
35
36#include "bswap.h"
37
38extern const uint8_t ff_log2_tab[256];
39
40
41/*misc utility functions added to make it compile */
42static inline int av_log2(unsigned int v)
43{
44 int n;
45
46 n = 0;
47 if (v & 0xffff0000) {
48 v >>= 16;
49 n += 16;
50 }
51 if (v & 0xff00) {
52 v >>= 8;
53 n += 8;
54 }
55 n += ff_log2_tab[v];
56
57 return n;
58}
59
60
61//#include "log.h"
62
63#if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
64#define ALT_BITSTREAM_READER
65#endif
66
67//#define ALT_BITSTREAM_WRITER
68//#define ALIGNED_BITSTREAM_WRITER
69#if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
70# ifdef ARCH_ARMV4L
71# define A32_BITSTREAM_READER
72# else
73#define ALT_BITSTREAM_READER
74//#define LIBMPEG2_BITSTREAM_READER
75//#define A32_BITSTREAM_READER
76# endif
77#endif
78#define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
79
80extern const uint8_t ff_reverse[256];
81
82#if defined(ARCH_X86)
83// avoid +32 for shift optimization (gcc should do that ...)
84static inline int32_t NEG_SSR32( int32_t a, int8_t s){
85 asm ("sarl %1, %0\n\t"
86 : "+r" (a)
87 : "ic" ((uint8_t)(-s))
88 );
89 return a;
90}
91static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
92 asm ("shrl %1, %0\n\t"
93 : "+r" (a)
94 : "ic" ((uint8_t)(-s))
95 );
96 return a;
97}
98#else
99# define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
100# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
101#endif
102
103/* bit output */
104
105/* buf and buf_end must be present and used by every alternative writer. */
106typedef struct PutBitContext {
107#ifdef ALT_BITSTREAM_WRITER
108 uint8_t *buf, *buf_end;
109 int index;
110#else
111 uint32_t bit_buf;
112 int bit_left;
113 uint8_t *buf, *buf_ptr, *buf_end;
114#endif
115} PutBitContext;
116
117static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
118{
119 if(buffer_size < 0) {
120 buffer_size = 0;
121 buffer = NULL;
122 }
123
124 s->buf = buffer;
125 s->buf_end = s->buf + buffer_size;
126#ifdef ALT_BITSTREAM_WRITER
127 s->index=0;
128 ((uint32_t*)(s->buf))[0]=0;
129// memset(buffer, 0, buffer_size);
130#else
131 s->buf_ptr = s->buf;
132 s->bit_left=32;
133 s->bit_buf=0;
134#endif
135}
136
137/* return the number of bits output */
138static inline int put_bits_count(PutBitContext *s)
139{
140#ifdef ALT_BITSTREAM_WRITER
141 return s->index;
142#else
143 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
144#endif
145}
146
147/* pad the end of the output stream with zeros */
148static inline void flush_put_bits(PutBitContext *s)
149{
150#ifdef ALT_BITSTREAM_WRITER
151 align_put_bits(s);
152#else
153 s->bit_buf<<= s->bit_left;
154 while (s->bit_left < 32) {
155 /* XXX: should test end of buffer */
156 *s->buf_ptr++=s->bit_buf >> 24;
157 s->bit_buf<<=8;
158 s->bit_left+=8;
159 }
160 s->bit_left=32;
161 s->bit_buf=0;
162#endif
163}
164
165void align_put_bits(PutBitContext *s);
166void ff_put_string(PutBitContext * pbc, char *s, int put_zero);
167
168/* bit input */
169/* buffer, buffer_end and size_in_bits must be present and used by every reader */
170typedef struct GetBitContext {
171 const uint8_t *buffer, *buffer_end;
172#ifdef ALT_BITSTREAM_READER
173 int index;
174#elif defined LIBMPEG2_BITSTREAM_READER
175 uint8_t *buffer_ptr;
176 uint32_t cache;
177 int bit_count;
178#elif defined A32_BITSTREAM_READER
179 uint32_t *buffer_ptr;
180 uint32_t cache0;
181 uint32_t cache1;
182 int bit_count;
183#endif
184 int size_in_bits;
185} GetBitContext;
186
187#define VLC_TYPE int16_t
188
189typedef struct VLC {
190 int bits;
191 VLC_TYPE (*table)[2]; ///< code, bits
192 int table_size, table_allocated;
193} VLC;
194
195typedef struct RL_VLC_ELEM {
196 int16_t level;
197 int8_t len;
198 uint8_t run;
199} RL_VLC_ELEM;
200
201#if defined(ARCH_SPARC) || defined(ARCH_ARMV4L) || defined(ARCH_MIPS) || defined(ARCH_BFIN)
202#define UNALIGNED_STORES_ARE_BAD
203#endif
204
205/* used to avoid missaligned exceptions on some archs (alpha, ...) */
206#if defined(ARCH_X86)
207# define unaligned16(a) (*(const uint16_t*)(a))
208# define unaligned32(a) (*(const uint32_t*)(a))
209# define unaligned64(a) (*(const uint64_t*)(a))
210#else
211# ifdef __GNUC__
212# define unaligned(x) \
213static inline uint##x##_t unaligned##x(const void *v) { \
214 struct Unaligned { \
215 uint##x##_t i; \
216 } __attribute__((packed)); \
217 \
218 return ((const struct Unaligned *) v)->i; \
219}
220# elif defined(__DECC)
221# define unaligned(x) \
222static inline uint##x##_t unaligned##x(const void *v) { \
223 return *(const __unaligned uint##x##_t *) v; \
224}
225# else
226# define unaligned(x) \
227static inline uint##x##_t unaligned##x(const void *v) { \
228 return *(const uint##x##_t *) v; \
229}
230# endif
231unaligned(16)
232unaligned(32)
233unaligned(64)
234#undef unaligned
235#endif /* defined(ARCH_X86) */
236
237#ifndef ALT_BITSTREAM_WRITER
238static inline void put_bits(PutBitContext *s, int n, unsigned int value)
239{
240 unsigned int bit_buf;
241 int bit_left;
242
243 // printf("put_bits=%d %x\n", n, value);
244 // assert(n == 32 || value < (1U << n));
245
246 bit_buf = s->bit_buf;
247 bit_left = s->bit_left;
248
249 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
250 /* XXX: optimize */
251 if (n < bit_left) {
252 bit_buf = (bit_buf<<n) | value;
253 bit_left-=n;
254 } else {
255 bit_buf<<=bit_left;
256 bit_buf |= value >> (n - bit_left);
257#ifdef UNALIGNED_STORES_ARE_BAD
258 if (3 & (intptr_t) s->buf_ptr) {
259 s->buf_ptr[0] = bit_buf >> 24;
260 s->buf_ptr[1] = bit_buf >> 16;
261 s->buf_ptr[2] = bit_buf >> 8;
262 s->buf_ptr[3] = bit_buf ;
263 } else
264#endif
265 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
266 //printf("bitbuf = %08x\n", bit_buf);
267 s->buf_ptr+=4;
268 bit_left+=32 - n;
269 bit_buf = value;
270 }
271
272 s->bit_buf = bit_buf;
273 s->bit_left = bit_left;
274}
275#endif
276
277
278#ifdef ALT_BITSTREAM_WRITER
279static inline void put_bits(PutBitContext *s, int n, unsigned int value)
280{
281# ifdef ALIGNED_BITSTREAM_WRITER
282# if defined(ARCH_X86)
283 asm volatile(
284 "movl %0, %%ecx \n\t"
285 "xorl %%eax, %%eax \n\t"
286 "shrdl %%cl, %1, %%eax \n\t"
287 "shrl %%cl, %1 \n\t"
288 "movl %0, %%ecx \n\t"
289 "shrl $3, %%ecx \n\t"
290 "andl $0xFFFFFFFC, %%ecx \n\t"
291 "bswapl %1 \n\t"
292 "orl %1, (%2, %%ecx) \n\t"
293 "bswapl %%eax \n\t"
294 "addl %3, %0 \n\t"
295 "movl %%eax, 4(%2, %%ecx) \n\t"
296 : "=&r" (s->index), "=&r" (value)
297 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
298 : "%eax", "%ecx"
299 );
300# else
301 int index= s->index;
302 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
303
304 value<<= 32-n;
305
306 ptr[0] |= be2me_32(value>>(index&31));
307 ptr[1] = be2me_32(value<<(32-(index&31)));
308//if(n>24) printf("%d %d\n", n, value);
309 index+= n;
310 s->index= index;
311# endif
312# else //ALIGNED_BITSTREAM_WRITER
313# if defined(ARCH_X86)
314 asm volatile(
315 "movl $7, %%ecx \n\t"
316 "andl %0, %%ecx \n\t"
317 "addl %3, %%ecx \n\t"
318 "negl %%ecx \n\t"
319 "shll %%cl, %1 \n\t"
320 "bswapl %1 \n\t"
321 "movl %0, %%ecx \n\t"
322 "shrl $3, %%ecx \n\t"
323 "orl %1, (%%ecx, %2) \n\t"
324 "addl %3, %0 \n\t"
325 "movl $0, 4(%%ecx, %2) \n\t"
326 : "=&r" (s->index), "=&r" (value)
327 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
328 : "%ecx"
329 );
330# else
331 int index= s->index;
332 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
333
334 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
335 ptr[1] = 0;
336//if(n>24) printf("%d %d\n", n, value);
337 index+= n;
338 s->index= index;
339# endif
340# endif //!ALIGNED_BITSTREAM_WRITER
341}
342#endif
343
344
345static inline uint8_t* pbBufPtr(PutBitContext *s)
346{
347#ifdef ALT_BITSTREAM_WRITER
348 return s->buf + (s->index>>3);
349#else
350 return s->buf_ptr;
351#endif
352}
353
354/**
355 *
356 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
357 */
358static inline void skip_put_bytes(PutBitContext *s, int n){
359 // assert((put_bits_count(s)&7)==0);
360#ifdef ALT_BITSTREAM_WRITER
361 FIXME may need some cleaning of the buffer
362 s->index += n<<3;
363#else
364 // assert(s->bit_left==32);
365 s->buf_ptr += n;
366#endif
367}
368
369/**
370 * skips the given number of bits.
371 * must only be used if the actual values in the bitstream dont matter
372 */
373static inline void skip_put_bits(PutBitContext *s, int n){
374#ifdef ALT_BITSTREAM_WRITER
375 s->index += n;
376#else
377 s->bit_left -= n;
378 s->buf_ptr-= s->bit_left>>5;
379 s->bit_left &= 31;
380#endif
381}
382
383/**
384 * Changes the end of the buffer.
385 */
386static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
387 s->buf_end= s->buf + size;
388}
389
390/* Bitstream reader API docs:
391name
392 abritary name which is used as prefix for the internal variables
393
394gb
395 getbitcontext
396
397OPEN_READER(name, gb)
398 loads gb into local variables
399
400CLOSE_READER(name, gb)
401 stores local vars in gb
402
403UPDATE_CACHE(name, gb)
404 refills the internal cache from the bitstream
405 after this call at least MIN_CACHE_BITS will be available,
406
407GET_CACHE(name, gb)
408 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
409
410SHOW_UBITS(name, gb, num)
411 will return the next num bits
412
413SHOW_SBITS(name, gb, num)
414 will return the next num bits and do sign extension
415
416SKIP_BITS(name, gb, num)
417 will skip over the next num bits
418 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
419
420SKIP_CACHE(name, gb, num)
421 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
422
423SKIP_COUNTER(name, gb, num)
424 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
425
426LAST_SKIP_CACHE(name, gb, num)
427 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
428
429LAST_SKIP_BITS(name, gb, num)
430 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
431
432for examples see get_bits, show_bits, skip_bits, get_vlc
433*/
434
435static inline int unaligned32_be(const void *v)
436{
437#ifdef CONFIG_ALIGN
438 const uint8_t *p=v;
439 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
440#else
441 return be2me_32( unaligned32(v)); //original
442#endif
443}
444
445static inline int unaligned32_le(const void *v)
446{
447#ifdef CONFIG_ALIGN
448 const uint8_t *p=v;
449 return (((p[3]<<8) | p[2])<<16) | (p[1]<<8) | (p[0]);
450#else
451 return le2me_32( unaligned32(v)); //original
452#endif
453}
454
455#ifdef ALT_BITSTREAM_READER
456# define MIN_CACHE_BITS 25
457
458# define OPEN_READER(name, gb)\
459 int name##_index= (gb)->index;\
460 int name##_cache= 0;\
461
462# define CLOSE_READER(name, gb)\
463 (gb)->index= name##_index;\
464
465# ifdef ALT_BITSTREAM_READER_LE
466# define UPDATE_CACHE(name, gb)\
467 name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
468
469# define SKIP_CACHE(name, gb, num)\
470 name##_cache >>= (num);
471# else
472# define UPDATE_CACHE(name, gb)\
473 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
474
475# define SKIP_CACHE(name, gb, num)\
476 name##_cache <<= (num);
477# endif
478
479// FIXME name?
480# define SKIP_COUNTER(name, gb, num)\
481 name##_index += (num);\
482
483# define SKIP_BITS(name, gb, num)\
484 {\
485 SKIP_CACHE(name, gb, num)\
486 SKIP_COUNTER(name, gb, num)\
487 }\
488
489# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
490# define LAST_SKIP_CACHE(name, gb, num) ;
491
492# ifdef ALT_BITSTREAM_READER_LE
493# define SHOW_UBITS(name, gb, num)\
494 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
495
496# define SHOW_SBITS(name, gb, num)\
497 NEG_SSR32((name##_cache)<<(32-(num)), num)
498# else
499# define SHOW_UBITS(name, gb, num)\
500 NEG_USR32(name##_cache, num)
501
502# define SHOW_SBITS(name, gb, num)\
503 NEG_SSR32(name##_cache, num)
504# endif
505
506# define GET_CACHE(name, gb)\
507 ((uint32_t)name##_cache)
508
509static inline int get_bits_count(GetBitContext *s){
510 return s->index;
511}
512
513static inline void skip_bits_long(GetBitContext *s, int n){
514 s->index += n;
515}
516
517#elif defined LIBMPEG2_BITSTREAM_READER
518//libmpeg2 like reader
519
520# define MIN_CACHE_BITS 17
521
522# define OPEN_READER(name, gb)\
523 int name##_bit_count=(gb)->bit_count;\
524 int name##_cache= (gb)->cache;\
525 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
526
527# define CLOSE_READER(name, gb)\
528 (gb)->bit_count= name##_bit_count;\
529 (gb)->cache= name##_cache;\
530 (gb)->buffer_ptr= name##_buffer_ptr;\
531
532#ifdef LIBMPEG2_BITSTREAM_READER_HACK
533
534# define UPDATE_CACHE(name, gb)\
535 if(name##_bit_count >= 0){\
536 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
537 name##_buffer_ptr += 2;\
538 name##_bit_count-= 16;\
539 }\
540
541#else
542
543# define UPDATE_CACHE(name, gb)\
544 if(name##_bit_count >= 0){\
545 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
546 name##_buffer_ptr+=2;\
547 name##_bit_count-= 16;\
548 }\
549
550#endif
551
552# define SKIP_CACHE(name, gb, num)\
553 name##_cache <<= (num);\
554
555# define SKIP_COUNTER(name, gb, num)\
556 name##_bit_count += (num);\
557
558# define SKIP_BITS(name, gb, num)\
559 {\
560 SKIP_CACHE(name, gb, num)\
561 SKIP_COUNTER(name, gb, num)\
562 }\
563
564# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
565# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
566
567# define SHOW_UBITS(name, gb, num)\
568 NEG_USR32(name##_cache, num)
569
570# define SHOW_SBITS(name, gb, num)\
571 NEG_SSR32(name##_cache, num)
572
573# define GET_CACHE(name, gb)\
574 ((uint32_t)name##_cache)
575
576static inline int get_bits_count(GetBitContext *s){
577 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
578}
579
580static inline void skip_bits_long(GetBitContext *s, int n){
581 OPEN_READER(re, s)
582 re_bit_count += n;
583 re_buffer_ptr += 2*(re_bit_count>>4);
584 re_bit_count &= 15;
585 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
586 UPDATE_CACHE(re, s)
587 CLOSE_READER(re, s)
588}
589
590#elif defined A32_BITSTREAM_READER
591
592# define MIN_CACHE_BITS 32
593
594# define OPEN_READER(name, gb)\
595 int name##_bit_count=(gb)->bit_count;\
596 uint32_t name##_cache0= (gb)->cache0;\
597 uint32_t name##_cache1= (gb)->cache1;\
598 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
599
600# define CLOSE_READER(name, gb)\
601 (gb)->bit_count= name##_bit_count;\
602 (gb)->cache0= name##_cache0;\
603 (gb)->cache1= name##_cache1;\
604 (gb)->buffer_ptr= name##_buffer_ptr;\
605
606# define UPDATE_CACHE(name, gb)\
607 if(name##_bit_count > 0){\
608 const uint32_t next= be2me_32( *name##_buffer_ptr );\
609 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
610 name##_cache1 |= next<<name##_bit_count;\
611 name##_buffer_ptr++;\
612 name##_bit_count-= 32;\
613 }\
614
615#if defined(ARCH_X86)
616# define SKIP_CACHE(name, gb, num)\
617 asm(\
618 "shldl %2, %1, %0 \n\t"\
619 "shll %2, %1 \n\t"\
620 : "+r" (name##_cache0), "+r" (name##_cache1)\
621 : "Ic" ((uint8_t)(num))\
622 );
623#else
624# define SKIP_CACHE(name, gb, num)\
625 name##_cache0 <<= (num);\
626 name##_cache0 |= NEG_USR32(name##_cache1,num);\
627 name##_cache1 <<= (num);
628#endif
629
630# define SKIP_COUNTER(name, gb, num)\
631 name##_bit_count += (num);\
632
633# define SKIP_BITS(name, gb, num)\
634 {\
635 SKIP_CACHE(name, gb, num)\
636 SKIP_COUNTER(name, gb, num)\
637 }\
638
639# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
640# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
641
642# define SHOW_UBITS(name, gb, num)\
643 NEG_USR32(name##_cache0, num)
644
645# define SHOW_SBITS(name, gb, num)\
646 NEG_SSR32(name##_cache0, num)
647
648# define GET_CACHE(name, gb)\
649 (name##_cache0)
650
651static inline int get_bits_count(GetBitContext *s){
652 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
653}
654
655static inline void skip_bits_long(GetBitContext *s, int n){
656 OPEN_READER(re, s)
657 re_bit_count += n;
658 re_buffer_ptr += re_bit_count>>5;
659 re_bit_count &= 31;
660 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
661 re_cache1 = 0;
662 UPDATE_CACHE(re, s)
663 CLOSE_READER(re, s)
664}
665
666#endif
667
668/**
669 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
670 * if MSB not set it is negative
671 * @param n length in bits
672 * @author BERO
673 */
674static inline int get_xbits(GetBitContext *s, int n){
675 register int sign;
676 register int32_t cache;
677 OPEN_READER(re, s)
678 UPDATE_CACHE(re, s)
679 cache = GET_CACHE(re,s);
680 sign=(~cache)>>31;
681 LAST_SKIP_BITS(re, s, n)
682 CLOSE_READER(re, s)
683 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
684}
685
686static inline int get_sbits(GetBitContext *s, int n){
687 register int tmp;
688 OPEN_READER(re, s)
689 UPDATE_CACHE(re, s)
690 tmp= SHOW_SBITS(re, s, n);
691 LAST_SKIP_BITS(re, s, n)
692 CLOSE_READER(re, s)
693 return tmp;
694}
695
696/**
697 * reads 1-17 bits.
698 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
699 */
700static inline unsigned int get_bits(GetBitContext *s, int n){
701 register int tmp;
702 OPEN_READER(re, s)
703 UPDATE_CACHE(re, s)
704 tmp= SHOW_UBITS(re, s, n);
705 LAST_SKIP_BITS(re, s, n)
706 CLOSE_READER(re, s)
707 return tmp;
708}
709
710/**
711 * shows 1-17 bits.
712 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
713 */
714static inline unsigned int show_bits(GetBitContext *s, int n){
715 register int tmp;
716 OPEN_READER(re, s)
717 UPDATE_CACHE(re, s)
718 tmp= SHOW_UBITS(re, s, n);
719// CLOSE_READER(re, s)
720 return tmp;
721}
722
723static inline void skip_bits(GetBitContext *s, int n){
724 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
725 OPEN_READER(re, s)
726 UPDATE_CACHE(re, s)
727 LAST_SKIP_BITS(re, s, n)
728 CLOSE_READER(re, s)
729}
730
731static inline unsigned int get_bits1(GetBitContext *s){
732#ifdef ALT_BITSTREAM_READER
733 int index= s->index;
734 uint8_t result= s->buffer[ index>>3 ];
735#ifdef ALT_BITSTREAM_READER_LE
736 result>>= (index&0x07);
737 result&= 1;
738#else
739 result<<= (index&0x07);
740 result>>= 8 - 1;
741#endif
742 index++;
743 s->index= index;
744
745 return result;
746#else
747 return get_bits(s, 1);
748#endif
749}
750
751static inline unsigned int show_bits1(GetBitContext *s){
752 return show_bits(s, 1);
753}
754
755static inline void skip_bits1(GetBitContext *s){
756 skip_bits(s, 1);
757}
758
759/**
760 * reads 0-32 bits.
761 */
762static inline unsigned int get_bits_long(GetBitContext *s, int n){
763 if(n<=17) return get_bits(s, n);
764 else{
765#ifdef ALT_BITSTREAM_READER_LE
766 int ret= get_bits(s, 16);
767 return ret | (get_bits(s, n-16) << 16);
768#else
769 int ret= get_bits(s, 16) << (n-16);
770 return ret | get_bits(s, n-16);
771#endif
772 }
773}
774
775/**
776 * shows 0-32 bits.
777 */
778static inline unsigned int show_bits_long(GetBitContext *s, int n){
779 if(n<=17) return show_bits(s, n);
780 else{
781 GetBitContext gb= *s;
782 int ret= get_bits_long(s, n);
783 *s= gb;
784 return ret;
785 }
786}
787
788/*
789static inline int check_marker(GetBitContext *s, const char *msg)
790{
791 int bit= get_bits1(s);
792 if(!bit)
793 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
794
795 return bit;
796}
797*/
798/**
799 * init GetBitContext.
800 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
801 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
802 * @param bit_size the size of the buffer in bits
803 */
804static inline void init_get_bits(GetBitContext *s,
805 const uint8_t *buffer, int bit_size)
806{
807 int buffer_size= (bit_size+7)>>3;
808 if(buffer_size < 0 || bit_size < 0) {
809 buffer_size = bit_size = 0;
810 buffer = NULL;
811 }
812
813 s->buffer= buffer;
814 s->size_in_bits= bit_size;
815 s->buffer_end= buffer + buffer_size;
816#ifdef ALT_BITSTREAM_READER
817 s->index=0;
818#elif defined LIBMPEG2_BITSTREAM_READER
819 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
820 s->bit_count = 16 + 8*((intptr_t)buffer&1);
821 skip_bits_long(s, 0);
822#elif defined A32_BITSTREAM_READER
823 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
824 s->bit_count = 32 + 8*((intptr_t)buffer&3);
825 skip_bits_long(s, 0);
826#endif
827}
828
829static inline void align_get_bits(GetBitContext *s)
830{
831 int n= (-get_bits_count(s)) & 7;
832 if(n) skip_bits(s, n);
833}
834
835int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
836 const void *bits, int bits_wrap, int bits_size,
837 const void *codes, int codes_wrap, int codes_size,
838 int flags);
839#define INIT_VLC_USE_STATIC 1
840#define INIT_VLC_LE 2
841void free_vlc(VLC *vlc);
842
843/**
844 *
845 * if the vlc code is invalid and max_depth=1 than no bits will be removed
846 * if the vlc code is invalid and max_depth>1 than the number of bits removed
847 * is undefined
848 */
849#define GET_VLC(code, name, gb, table, bits, max_depth)\
850{\
851 int n, index, nb_bits;\
852\
853 index= SHOW_UBITS(name, gb, bits);\
854 code = table[index][0];\
855 n = table[index][1];\
856\
857 if(max_depth > 1 && n < 0){\
858 LAST_SKIP_BITS(name, gb, bits)\
859 UPDATE_CACHE(name, gb)\
860\
861 nb_bits = -n;\
862\
863 index= SHOW_UBITS(name, gb, nb_bits) + code;\
864 code = table[index][0];\
865 n = table[index][1];\
866 if(max_depth > 2 && n < 0){\
867 LAST_SKIP_BITS(name, gb, nb_bits)\
868 UPDATE_CACHE(name, gb)\
869\
870 nb_bits = -n;\
871\
872 index= SHOW_UBITS(name, gb, nb_bits) + code;\
873 code = table[index][0];\
874 n = table[index][1];\
875 }\
876 }\
877 SKIP_BITS(name, gb, n)\
878}
879
880#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
881{\
882 int n, index, nb_bits;\
883\
884 index= SHOW_UBITS(name, gb, bits);\
885 level = table[index].level;\
886 n = table[index].len;\
887\
888 if(max_depth > 1 && n < 0){\
889 SKIP_BITS(name, gb, bits)\
890 if(need_update){\
891 UPDATE_CACHE(name, gb)\
892 }\
893\
894 nb_bits = -n;\
895\
896 index= SHOW_UBITS(name, gb, nb_bits) + level;\
897 level = table[index].level;\
898 n = table[index].len;\
899 }\
900 run= table[index].run;\
901 SKIP_BITS(name, gb, n)\
902}
903
904
905/**
906 * parses a vlc code, faster then get_vlc()
907 * @param bits is the number of bits which will be read at once, must be
908 * identical to nb_bits in init_vlc()
909 * @param max_depth is the number of times bits bits must be read to completely
910 * read the longest vlc code
911 * = (max_vlc_length + bits - 1) / bits
912 */
913static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
914 int bits, int max_depth)
915{
916 int code;
917
918 OPEN_READER(re, s)
919 UPDATE_CACHE(re, s)
920
921 GET_VLC(code, re, s, table, bits, max_depth)
922
923 CLOSE_READER(re, s)
924 return code;
925}
926
927//#define TRACE
928
929#ifdef TRACE
930static inline void print_bin(int bits, int n){
931 int i;
932
933 for(i=n-1; i>=0; i--){
934 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
935 }
936 for(i=n; i<24; i++)
937 av_log(NULL, AV_LOG_DEBUG, " ");
938}
939
940static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
941 int r= get_bits(s, n);
942
943 print_bin(r, n);
944 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);
945 return r;
946}
947static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
948 int show= show_bits(s, 24);
949 int pos= get_bits_count(s);
950 int r= get_vlc2(s, table, bits, max_depth);
951 int len= get_bits_count(s) - pos;
952 int bits2= show>>(24-len);
953
954 print_bin(bits2, len);
955
956 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
957 return r;
958}
959static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
960 int show= show_bits(s, n);
961 int r= get_xbits(s, n);
962
963 print_bin(show, n);
964 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);
965 return r;
966}
967
968#define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
969#define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
970#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
971#define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
972#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
973
974#define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
975
976#else //TRACE
977#define tprintf(p, ...) {}
978#endif
979
980static inline int decode012(GetBitContext *gb){
981 int n;
982 n = get_bits1(gb);
983 if (n == 0)
984 return 0;
985 else
986 return get_bits1(gb) + 1;
987}
988
989#endif /* BITSTREAM_H */
diff --git a/apps/codecs/libwma/common.h b/apps/codecs/libwma/common.h
deleted file mode 100644
index 0fbc0d16e6..0000000000
--- a/apps/codecs/libwma/common.h
+++ /dev/null
@@ -1,408 +0,0 @@
1/*
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file common.h
23 * common internal api header.
24 */
25
26#ifndef COMMON_H
27#define COMMON_H
28
29#include "ffmpeg_config.h"
30#include <inttypes.h>
31
32#define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
33#define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
34
35/* bit input */
36
37typedef struct GetBitContext {
38 const uint8_t *buffer, *buffer_end;
39 int index;
40 int size_in_bits;
41} GetBitContext;
42
43static inline int get_bits_count(GetBitContext *s);
44
45#define VLC_TYPE int16_t
46
47typedef struct VLC {
48 int bits;
49 VLC_TYPE (*table)[2]; ///< code, bits
50 int table_size, table_allocated;
51} VLC;
52
53/* used to avoid missaligned exceptions on some archs (alpha, ...) */
54static inline uint32_t unaligned32(const void *v) {
55 struct Unaligned {
56 uint32_t i;
57 } __attribute__((packed));
58
59 return ((const struct Unaligned *) v)->i;
60}
61
62
63/* Bitstream reader API docs:
64name
65 abritary name which is used as prefix for the internal variables
66
67gb
68 getbitcontext
69
70OPEN_READER(name, gb)
71 loads gb into local variables
72
73CLOSE_READER(name, gb)
74 stores local vars in gb
75
76UPDATE_CACHE(name, gb)
77 refills the internal cache from the bitstream
78 after this call at least MIN_CACHE_BITS will be available,
79
80GET_CACHE(name, gb)
81 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
82
83SHOW_UBITS(name, gb, num)
84 will return the nest num bits
85
86SHOW_SBITS(name, gb, num)
87 will return the nest num bits and do sign extension
88
89SKIP_BITS(name, gb, num)
90 will skip over the next num bits
91 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
92
93SKIP_CACHE(name, gb, num)
94 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
95
96SKIP_COUNTER(name, gb, num)
97 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
98
99LAST_SKIP_CACHE(name, gb, num)
100 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
101
102LAST_SKIP_BITS(name, gb, num)
103 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
104
105for examples see get_bits, show_bits, skip_bits, get_vlc
106*/
107
108static inline int unaligned32_be(const void *v)
109{
110#ifdef CONFIG_ALIGN
111 const uint8_t *p=v;
112 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
113#else
114 return be2me_32( unaligned32(v)); //original
115#endif
116}
117
118#define MIN_CACHE_BITS 25
119
120#define OPEN_READER(name, gb)\
121 int name##_index= (gb)->index;\
122 int name##_cache= 0;\
123
124#define CLOSE_READER(name, gb)\
125 (gb)->index= name##_index;\
126
127#define UPDATE_CACHE(name, gb)\
128 name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
129
130#define SKIP_CACHE(name, gb, num)\
131 name##_cache <<= (num);\
132
133// FIXME name?
134#define SKIP_COUNTER(name, gb, num)\
135 name##_index += (num);\
136
137#define SKIP_BITS(name, gb, num)\
138 {\
139 SKIP_CACHE(name, gb, num)\
140 SKIP_COUNTER(name, gb, num)\
141 }\
142
143#define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
144#define LAST_SKIP_CACHE(name, gb, num) ;
145
146#define SHOW_UBITS(name, gb, num)\
147 NEG_USR32(name##_cache, num)
148
149#define SHOW_SBITS(name, gb, num)\
150 NEG_SSR32(name##_cache, num)
151
152#define GET_CACHE(name, gb)\
153 ((uint32_t)name##_cache)
154
155static inline int get_bits_count(GetBitContext *s){
156 return s->index;
157}
158
159/**
160 * reads 0-17 bits.
161 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
162 */
163static inline unsigned int get_bits(GetBitContext *s, int n){
164 register int tmp;
165 OPEN_READER(re, s)
166 UPDATE_CACHE(re, s)
167 tmp= SHOW_UBITS(re, s, n);
168 LAST_SKIP_BITS(re, s, n)
169 CLOSE_READER(re, s)
170 return tmp;
171}
172
173unsigned int get_bits_long(GetBitContext *s, int n);
174
175/**
176 * shows 0-17 bits.
177 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
178 */
179static inline unsigned int show_bits(GetBitContext *s, int n){
180 register int tmp;
181 OPEN_READER(re, s)
182 UPDATE_CACHE(re, s)
183 tmp= SHOW_UBITS(re, s, n);
184// CLOSE_READER(re, s)
185 return tmp;
186}
187
188unsigned int show_bits_long(GetBitContext *s, int n);
189
190static inline void skip_bits(GetBitContext *s, int n){
191 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
192 OPEN_READER(re, s)
193 UPDATE_CACHE(re, s)
194 LAST_SKIP_BITS(re, s, n)
195 CLOSE_READER(re, s)
196}
197
198static inline unsigned int get_bits1(GetBitContext *s){
199 int index= s->index;
200 uint8_t result= s->buffer[ index>>3 ];
201 result<<= (index&0x07);
202 result>>= 8 - 1;
203 index++;
204 s->index= index;
205
206 return result;
207}
208
209static inline unsigned int show_bits1(GetBitContext *s){
210 return show_bits(s, 1);
211}
212
213static inline void skip_bits1(GetBitContext *s){
214 skip_bits(s, 1);
215}
216
217void init_get_bits(GetBitContext *s,
218 const uint8_t *buffer, int buffer_size);
219
220int check_marker(GetBitContext *s, const char *msg);
221void align_get_bits(GetBitContext *s);
222int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
223 const void *bits, int bits_wrap, int bits_size,
224 const void *codes, int codes_wrap, int codes_size);
225
226#define GET_VLC(code, name, gb, table, bits, max_depth)\
227{\
228 int n, index, nb_bits;\
229\
230 index= SHOW_UBITS(name, gb, bits);\
231 code = table[index][0];\
232 n = table[index][1];\
233\
234 if(max_depth > 1 && n < 0){\
235 LAST_SKIP_BITS(name, gb, bits)\
236 UPDATE_CACHE(name, gb)\
237\
238 nb_bits = -n;\
239\
240 index= SHOW_UBITS(name, gb, nb_bits) + code;\
241 code = table[index][0];\
242 n = table[index][1];\
243 if(max_depth > 2 && n < 0){\
244 LAST_SKIP_BITS(name, gb, nb_bits)\
245 UPDATE_CACHE(name, gb)\
246\
247 nb_bits = -n;\
248\
249 index= SHOW_UBITS(name, gb, nb_bits) + code;\
250 code = table[index][0];\
251 n = table[index][1];\
252 }\
253 }\
254 SKIP_BITS(name, gb, n)\
255}
256
257
258// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
259static inline int get_vlc(GetBitContext *s, VLC *vlc)
260{
261 int code;
262 VLC_TYPE (*table)[2]= vlc->table;
263
264 OPEN_READER(re, s)
265 UPDATE_CACHE(re, s)
266
267 GET_VLC(code, re, s, table, vlc->bits, 3)
268
269 CLOSE_READER(re, s)
270 return code;
271}
272
273
274
275
276//#define TRACE
277
278#ifdef TRACE
279
280static inline void print_bin(int bits, int n){
281 int i;
282
283 for(i=n-1; i>=0; i--){
284 printf("%d", (bits>>i)&1);
285 }
286 for(i=n; i<24; i++)
287 printf(" ");
288}
289
290static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
291 int r= get_bits(s, n);
292
293 print_bin(r, n);
294 printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
295 return r;
296}
297static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
298 int show= show_bits(s, 24);
299 int pos= get_bits_count(s);
300 int r= get_vlc2(s, table, bits, max_depth);
301 int len= get_bits_count(s) - pos;
302 int bits2= show>>(24-len);
303
304 print_bin(bits2, len);
305
306 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
307 return r;
308}
309static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
310 int show= show_bits(s, n);
311 int r= get_xbits(s, n);
312
313 print_bin(show, n);
314 printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
315 return r;
316}
317
318#define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
319#define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
320#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
321#define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
322#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
323
324#define tprintf printf
325
326#else //TRACE
327#define tprintf(_arg...) {}
328#endif
329
330/* define it to include statistics code (useful only for optimizing
331 codec efficiency */
332//#define STATS
333
334#ifdef STATS
335
336enum {
337 ST_UNKNOWN,
338 ST_DC,
339 ST_INTRA_AC,
340 ST_INTER_AC,
341 ST_INTRA_MB,
342 ST_INTER_MB,
343 ST_MV,
344 ST_NB,
345};
346
347extern int st_current_index;
348extern unsigned int st_bit_counts[ST_NB];
349extern unsigned int st_out_bit_counts[ST_NB];
350
351void print_stats(void);
352#endif
353
354/* misc math functions */
355extern const uint8_t ff_log2_tab[256];
356
357static inline int av_log2(unsigned int v)
358{
359 int n;
360
361 n = 0;
362 if (v & 0xffff0000) {
363 v >>= 16;
364 n += 16;
365 }
366 if (v & 0xff00) {
367 v >>= 8;
368 n += 8;
369 }
370 n += ff_log2_tab[v];
371
372 return n;
373}
374
375static inline int clip(int a, int amin, int amax)
376{
377 if (a < amin)
378 return amin;
379 else if (a > amax)
380 return amax;
381 else
382 return a;
383}
384
385/* math */
386extern const uint8_t ff_sqrt_tab[128];
387
388int64_t ff_gcd(int64_t a, int64_t b);
389
390static inline int ff_sqrt(int a)
391{
392 int ret=0;
393 int s;
394 int ret_sq=0;
395
396 if(a<128) return ff_sqrt_tab[a];
397
398 for(s=15; s>=0; s--){
399 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
400 if(b<=a){
401 ret_sq=b;
402 ret+= 1<<s;
403 }
404 }
405 return ret;
406}
407
408#endif /* COMMON_H */
diff --git a/apps/codecs/libwma/dsputil.h b/apps/codecs/libwma/dsputil.h
deleted file mode 100644
index 794af1e285..0000000000
--- a/apps/codecs/libwma/dsputil.h
+++ /dev/null
@@ -1,84 +0,0 @@
1/*
2 * DSP utils
3 * Copyright (c) 2000, 2001, 2002 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
21/**
22 * @file dsputil.h
23 * DSP utils.
24 * note, many functions in here may use MMX which trashes the FPU state, it is
25 * absolutely necessary to call emms_c() between dsp & float/double code
26 */
27
28#ifndef DSPUTIL_H
29#define DSPUTIL_H
30
31#include "common.h"
32
33void dsputil_static_init(void);
34
35/* FFT computation */
36
37/* NOTE: soon integer code will be added, so you must use the
38 FFTSample type */
39typedef float FFTSample;
40
41typedef struct FFTComplex {
42 FFTSample re, im;
43} FFTComplex;
44
45typedef struct FFTContext {
46 int nbits;
47 int inverse;
48 uint16_t *revtab;
49 FFTComplex *exptab;
50 FFTComplex *exptab1; /* only used by SSE code */
51 void (*fft_calc)(struct FFTContext *s, FFTComplex *z);
52} FFTContext;
53
54int fft_inits(FFTContext *s, int nbits, int inverse);
55void fft_permute(FFTContext *s, FFTComplex *z);
56void fft_calc_c(FFTContext *s, FFTComplex *z);
57void fft_calc_sse(FFTContext *s, FFTComplex *z);
58void fft_calc_altivec(FFTContext *s, FFTComplex *z);
59
60static inline void fft_calc(FFTContext *s, FFTComplex *z)
61{
62 s->fft_calc(s, z);
63}
64void fft_end(FFTContext *s);
65
66/* MDCT computation */
67
68typedef struct MDCTContext {
69 int n; /* size of MDCT (i.e. number of input data * 2) */
70 int nbits; /* n = 2^nbits */
71 /* pre/post rotation tables */
72 FFTSample *tcos;
73 FFTSample *tsin;
74 FFTContext fft;
75} MDCTContext;
76
77int ff_mdct_init(MDCTContext *s, int nbits, int inverse);
78void ff_imdct_calc(MDCTContext *s, FFTSample *output,
79 const FFTSample *input, FFTSample *tmp);
80void ff_mdct_calc(MDCTContext *s, FFTSample *out,
81 const FFTSample *input, FFTSample *tmp);
82void ff_mdct_end(MDCTContext *s);
83
84#endif
diff --git a/apps/codecs/libwma/wmadata.h b/apps/codecs/libwma/wmadata.h
index 5abf49442b..d88a855261 100644
--- a/apps/codecs/libwma/wmadata.h
+++ b/apps/codecs/libwma/wmadata.h
@@ -66,24 +66,17 @@ static const uint8_t hgain_huffbits[37] = {
66 13, 10, 13, 13, 13, 66 13, 10, 13, 13, 13,
67}; 67};
68 68
69static const float lsp_codebook[NB_LSP_COEFS][16] = { 69static const fixed32 lsp_codebook[NB_LSP_COEFS][16] = {
70 { 1.98732877, 1.97944528, 1.97179088, 1.96260549, 1.95038374, 1.93336114, 1.90719232, 1.86191415, }, 70{0x1fcc2,0x1fabd,0x1f8c7,0x1f66d,0x1f34c,0x1eef1,0x1e83e,0x1dca6,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0},
71 { 1.97260000, 1.96083160, 1.94982586, 1.93806164, 1.92516608, 1.91010199, 1.89232331, 1.87149812, 71{0x1f8fc,0x1f5f9,0x1f328,0x1f025,0x1ecd8,0x1e8fc,0x1e46f,0x1df1b,0x1d87c,0x1d047,0x1c6b5,0x1bb8f,0x1add8,0x19c0e,0x18220,0x154ca},
72 1.84564818, 1.81358067, 1.77620070, 1.73265264, 1.67907855, 1.60959081, 1.50829650, 1.33120330, }, 72{0x1e6ae,0x1dd65,0x1d58e,0x1cd3b,0x1c439,0x1ba69,0x1af5e,0x1a32c,0x195c4,0x18498,0x16fd2,0x156ea,0x13de4,0x11f63,0xf7ae,0xbd90},
73 { 1.90109110, 1.86482426, 1.83419671, 1.80168452, 1.76650116, 1.72816320, 1.68502700, 1.63738256, 73{0x1c4fa,0x1ada0,0x19976,0x1891d,0x17986,0x1697f,0x15858,0x145fd,0x1316b,0x11900,0xfcfa,0xdf55,0xbe63,0x9902,0x6e83,0x2e05},
74 1.58501580, 1.51795181, 1.43679906, 1.33950585, 1.24176208, 1.12260729, 0.96749668, 0.74048265, }, 74{0x16f2d,0x15205,0x135f3,0x11b14,0x10170,0xe743,0xcdec,0xb504,0x9ab2,0x7f86,0x6296,0x4565,0x24e2,0x90,0xffffd52f,0xffffa172},
75 { 1.76943864, 1.67822463, 1.59946365, 1.53560582, 1.47470796, 1.41210167, 1.34509536, 1.27339507, 75{0xffbc,0xd786,0xb521,0x943e,0x7876,0x5ea3,0x44ad,0x2bf0,0x1274,0xfffff829,0xfffe9981,0xffffbfab,0xffffa0bb,0xffff7d3f,0xffff59e3,0xffff3269},
76 1.19303814, 1.09765169, 0.98818722, 0.87239446, 0.74369172, 0.59768184, 0.43168630, 0.17977021, }, 76{0x43e1,0x102a,0xffffe94a,0xffffc9fa,0xffffb076,0xffff9a6b,0xffff871c,0xffff7555,0xffff62b4,0xffff4f81,0xffff3bf4,0xffff25f7,0xffff0c0f,0xfffeef53,0xfffecb7e,0xfffe9fb3},
77 { 1.43428349, 1.32038354, 1.21074086, 1.10577988, 1.00561746, 0.90335924, 0.80437489, 0.70709671, 77{0xffff75ea,0xffff4325,0xffff1da2,0xfffefd23,0xfffeddb9,0xfffebb51,0xfffe945f,0xfffe6131,0xfffee5fe,0xfffed5ba,0xfffec442,0xfffeb224,0xfffe9f95,0xfffe880e,0xfffe6c7a,0xfffe54c1},
78 0.60427395, 0.49814048, 0.38509539, 0.27106800, 0.14407416, 0.00219910, -0.16725141, -0.36936085, }, 78{0xffff9d2e,0xffff709e,0xffff5489,0xffff3d5e,0xffff295b,0xffff1761,0xffff06a2,0xfffef68a,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0},
79 { 0.99895687, 0.84188166, 0.70753739, 0.57906595, 0.47055563, 0.36966965, 0.26826648, 0.17163380, 79{0xfffe7045,0xfffe572f,0xfffe45ea,0xfffe38af,0xfffe2d8f,0xfffe2347,0xfffe18df,0xfffe0d42,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}
80 0.07208392, -0.03062936, -1.40037388, -0.25128968, -0.37213937, -0.51075646, -0.64887512, -0.80308031, },
81 { 0.26515280, 0.06313551, -0.08872080, -0.21103548, -0.31069678, -0.39680323, -0.47223474, -0.54167135,
82 -0.61444740, -0.68943343, -0.76580211, -0.85170082, -0.95289061, -1.06514703, -1.20510707, -1.37617746, },
83 { -0.53940301, -0.73770929, -0.88424876, -1.01117930, -1.13389091, -1.26830073, -1.42041987, -1.62033919,
84 -1.10158808, -1.16512566, -1.23337128, -1.30414401, -1.37663312, -1.46853845, -1.57625798, -1.66893638, },
85 { -0.38601997, -0.56009350, -0.66978483, -0.76028471, -0.83846064, -0.90868087, -0.97408881, -1.03694962, },
86 { -1.56144989, -1.65944032, -1.72689685, -1.77857740, -1.82203011, -1.86220079, -1.90283983, -1.94820479, },
87}; 80};
88 81
89static const uint32_t scale_huffcodes[121] = { 82static const uint32_t scale_huffcodes[121] = {
diff --git a/apps/codecs/libwma/wmadec.h b/apps/codecs/libwma/wmadec.h
index 59bf4cdf75..5280d9bbdc 100644
--- a/apps/codecs/libwma/wmadec.h
+++ b/apps/codecs/libwma/wmadec.h
@@ -21,7 +21,7 @@
21#define _WMADEC_H 21#define _WMADEC_H
22 22
23#include "asf.h" 23#include "asf.h"
24#include "common.h" /* For GetBitContext */ 24#include "bitstream.h" /* For GetBitContext */
25//#include "dsputil.h" /* For MDCTContext */ 25//#include "dsputil.h" /* For MDCTContext */
26 26
27 27
diff --git a/apps/codecs/libwma/wmadeci.c b/apps/codecs/libwma/wmadeci.c
index 668aac7294..4b16acd038 100644
--- a/apps/codecs/libwma/wmadeci.c
+++ b/apps/codecs/libwma/wmadeci.c
@@ -27,6 +27,18 @@
27#include "asf.h" 27#include "asf.h"
28#include "wmadec.h" 28#include "wmadec.h"
29#include "wmafixed.c" 29#include "wmafixed.c"
30#include "bitstream.h"
31
32
33#define VLCBITS 9
34#define VLCMAX ((22+VLCBITS-1)/VLCBITS)
35
36#define EXPVLCBITS 9
37#define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
38
39#define HGAINVLCBITS 9
40#define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
41
30 42
31#ifdef CPU_ARM 43#ifdef CPU_ARM
32static inline 44static inline
@@ -500,13 +512,11 @@ static void init_coef_vlc(VLC *vlc,
500 int i, l, j, level; 512 int i, l, j, level;
501 513
502 514
503 init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4); 515 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
504 516
505 run_table = runtabarray[tab]; 517 run_table = runtabarray[tab];
506 //run_table = av_malloc(n * sizeof(uint16_t)); //max n should be 1336
507
508 level_table= levtabarray[tab]; 518 level_table= levtabarray[tab];
509 //level_table = av_malloc(n * sizeof(uint16_t)); 519
510 p = levels_table; 520 p = levels_table;
511 i = 2; 521 i = 2;
512 level = 1; 522 level = 1;
@@ -629,7 +639,6 @@ int wma_decode_init(WMADecodeContext* s, asf_waveformatex_t *wfx)
629 fixed64 tmpi = fixdiv64(tim,itofix64(8)); 639 fixed64 tmpi = fixdiv64(tim,itofix64(8));
630 s->byte_offset_bits = av_log2(fixtoi64(tmpi+0x8000)) + 2; 640 s->byte_offset_bits = av_log2(fixtoi64(tmpi+0x8000)) + 2;
631 641
632
633 /* compute high frequency value and choose if noise coding should 642 /* compute high frequency value and choose if noise coding should
634 be activated */ 643 be activated */
635 bps1 = bps; 644 bps1 = bps;
@@ -898,18 +907,21 @@ int wma_decode_init(WMADecodeContext* s, asf_waveformatex_t *wfx)
898 } 907 }
899 } 908 }
900 909
901 init_vlc(&s->hgain_vlc, 9, sizeof(hgain_huffbits), 910
911 init_vlc(&s->hgain_vlc, 9, sizeof(hgain_huffbits),
902 hgain_huffbits, 1, 1, 912 hgain_huffbits, 1, 1,
903 hgain_huffcodes, 2, 2); 913 hgain_huffcodes, 2, 2, 0);
904 } 914 }
905 915
906 if (s->use_exp_vlc) 916 if (s->use_exp_vlc)
907 { 917 {
918
908 s->exp_vlc.table = vlcbuf3; 919 s->exp_vlc.table = vlcbuf3;
909 s->exp_vlc.table_allocated = 1536; 920 s->exp_vlc.table_allocated = 1536;
910 init_vlc(&s->exp_vlc, 9, sizeof(scale_huffbits), 921
911 scale_huffbits, 1, 1, 922 init_vlc(&s->exp_vlc, 9, sizeof(scale_huffbits),
912 scale_huffcodes, 4, 4); 923 scale_huffbits, 1, 1,
924 scale_huffcodes, 4, 4, 0);
913 } 925 }
914 else 926 else
915 { 927 {
@@ -934,6 +946,7 @@ int wma_decode_init(WMADecodeContext* s, asf_waveformatex_t *wfx)
934 s->coef_vlc[1].table = vlcbuf2; 946 s->coef_vlc[1].table = vlcbuf2;
935 s->coef_vlc[1].table_allocated = 14336/4; 947 s->coef_vlc[1].table_allocated = 14336/4;
936 948
949
937 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], 950 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
938 &coef_vlcs[coef_vlc_table * 2], 0); 951 &coef_vlcs[coef_vlc_table * 2], 0);
939 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], 952 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
@@ -1084,7 +1097,7 @@ static int decode_exp_vlc(WMADecodeContext *s, int ch)
1084 1097
1085 while (q < q_end) 1098 while (q < q_end)
1086 { 1099 {
1087 code = get_vlc(&s->gb, &s->exp_vlc); 1100 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
1088 if (code < 0) 1101 if (code < 0)
1089 { 1102 {
1090 return -1; 1103 return -1;
@@ -1120,7 +1133,8 @@ static int wma_decode_block(WMADecodeContext *s)
1120 fixed32 mdct_norm; 1133 fixed32 mdct_norm;
1121 1134
1122// printf("***decode_block: %d:%d (%d)\n", s->frame_count - 1, s->block_num, s->block_len); 1135// printf("***decode_block: %d:%d (%d)\n", s->frame_count - 1, s->block_num, s->block_len);
1123 /* compute current block length */ 1136
1137 /* compute current block length */
1124 if (s->use_variable_block_len) 1138 if (s->use_variable_block_len)
1125 { 1139 {
1126 n = av_log2(s->nb_block_sizes - 1) + 1; 1140 n = av_log2(s->nb_block_sizes - 1) + 1;
@@ -1151,7 +1165,7 @@ static int wma_decode_block(WMADecodeContext *s)
1151 1165
1152 1166
1153 1167
1154 LOGF("v was %d", v); 1168
1155 if (v >= s->nb_block_sizes) 1169 if (v >= s->nb_block_sizes)
1156 { 1170 {
1157 // rb->splash(HZ*4, "v was %d", v); //5, 7 1171 // rb->splash(HZ*4, "v was %d", v); //5, 7
@@ -1228,7 +1242,6 @@ static int wma_decode_block(WMADecodeContext *s)
1228 nb_coefs[ch] = n; 1242 nb_coefs[ch] = n;
1229 } 1243 }
1230 /* complex coding */ 1244 /* complex coding */
1231
1232 if (s->use_noise_coding) 1245 if (s->use_noise_coding)
1233 { 1246 {
1234 1247
@@ -1266,7 +1279,8 @@ static int wma_decode_block(WMADecodeContext *s)
1266 } 1279 }
1267 else 1280 else
1268 { 1281 {
1269 code = get_vlc(&s->gb, &s->hgain_vlc); 1282 //code = get_vlc(&s->gb, &s->hgain_vlc);
1283 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
1270 if (code < 0) 1284 if (code < 0)
1271 { 1285 {
1272 return -6; 1286 return -6;
@@ -1328,7 +1342,8 @@ static int wma_decode_block(WMADecodeContext *s)
1328 1342
1329 for(;;) 1343 for(;;)
1330 { 1344 {
1331 code = get_vlc(&s->gb, coef_vlc); 1345 code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX);
1346 //code = get_vlc(&s->gb, coef_vlc);
1332 if (code < 0) 1347 if (code < 0)
1333 { 1348 {
1334 return -8; 1349 return -8;
@@ -1627,7 +1642,7 @@ static int wma_decode_frame(WMADecodeContext *s, int16_t *samples)
1627 ret = wma_decode_block(s); 1642 ret = wma_decode_block(s);
1628 if (ret < 0) 1643 if (ret < 0)
1629 { 1644 {
1630 LOGF("wma_decode_block: %d",ret); 1645
1631 //rb->splash(HZ*4, "wma_decode_block failed with ret %d", ret); 1646 //rb->splash(HZ*4, "wma_decode_block failed with ret %d", ret);
1632 return -1; 1647 return -1;
1633 } 1648 }
@@ -1676,8 +1691,8 @@ static int wma_decode_frame(WMADecodeContext *s, int16_t *samples)
1676/* Initialise the superframe decoding */ 1691/* Initialise the superframe decoding */
1677 1692
1678int wma_decode_superframe_init(WMADecodeContext* s, 1693int wma_decode_superframe_init(WMADecodeContext* s,
1679 uint8_t *buf, /*input*/ 1694 uint8_t *buf, /*input*/
1680 int buf_size) 1695 int buf_size)
1681{ 1696{
1682 if (buf_size==0) 1697 if (buf_size==0)
1683 { 1698 {
@@ -1771,12 +1786,12 @@ int wma_decode_superframe_frame(WMADecodeContext* s,
1771 1786
1772 /* If we haven't decoded a frame yet, do it now */ 1787 /* If we haven't decoded a frame yet, do it now */
1773 if (!done) 1788 if (!done)
1774 {
1775 if (wma_decode_frame(s, samples) < 0)
1776 { 1789 {
1777 goto fail; 1790 if (wma_decode_frame(s, samples) < 0)
1791 {
1792 goto fail;
1793 }
1778 } 1794 }
1779 }
1780 1795
1781 s->current_frame++; 1796 s->current_frame++;
1782 1797
@@ -1802,3 +1817,4 @@ fail:
1802 s->last_superframe_len = 0; 1817 s->last_superframe_len = 0;
1803 return -1; 1818 return -1;
1804} 1819}
1820
diff --git a/apps/codecs/libwma/wmafixed.c b/apps/codecs/libwma/wmafixed.c
index 7b08689282..b53a1da607 100644
--- a/apps/codecs/libwma/wmafixed.c
+++ b/apps/codecs/libwma/wmafixed.c
@@ -1,4 +1,3 @@
1//#include "asf.h"
2#include "wmadec.h" 1#include "wmadec.h"
3#include "wmafixed.h" 2#include "wmafixed.h"
4#include <codecs.h> 3#include <codecs.h>