1 /*
2 * Musepack SV7 decoder
3 * Copyright (c) 2006 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * MPEG Audio Layer 1/2 -like codec with frames of 1152 samples
25 * divided into 32 subbands.
26 */
27
36
39
41
43 {
44 0, 512, 1024, 1536, 2052, 2564, 3076, 3588, 4100, 4612, 5124,
45 5636, 6164, 6676, 7224
46 };
47
48
50 {
51 int i, j;
55 static int vlc_initialized = 0;
56
60 static VLC_TYPE quant_tables[7224][2];
61
62 /* Musepack SV7 is always stereo */
66 }
67
70 return -1;
71 }
79
85 return -1;
86 }
93
96
97 if(vlc_initialized) return 0;
99 scfi_vlc.
table = scfi_table;
105 return -1;
106 }
107 dscf_vlc.
table = dscf_table;
113 return -1;
114 }
115 hdr_vlc.
table = hdr_table;
121 return -1;
122 }
124 for(j = 0; j < 2; j++){
126 quant_vlc[i][j].
table_allocated = quant_offsets[i*2 + j + 1] - quant_offsets[i*2 + j];
131 return -1;
132 }
133 }
134 }
135 vlc_initialized = 1;
136
137 return 0;
138 }
139
140 /**
141 * Fill samples for given subband
142 */
144 {
146 switch(idx){
147 case -1:
150 }
151 break;
152 case 1:
154 for(i = 0; i < SAMPLES_PER_BAND/3; i++){
159 }
160 break;
161 case 2:
163 for(i = 0; i < SAMPLES_PER_BAND/2; i++){
167 }
168 break;
169 case 3: case 4: case 5: case 6: case 7:
173 break;
174 case 8: case 9: case 10: case 11: case 12:
175 case 13: case 14: case 15: case 16: case 17:
176 t = (1 << (idx - 2)) - 1;
179 break;
180 default: // case 0 and -2..-17
181 return;
182 }
183 }
184
186 {
188 if (t == 8)
191 }
192
194 int *got_frame_ptr,
AVPacket *avpkt)
195 {
198 int buf_size;
201 int i, ch;
204 int off,
ret, last_frame, skip;
205 int bits_used, bits_avail;
206
207 memset(bands, 0,
sizeof(*bands) * (c->
maxbands + 1));
208
209 buf_size = avpkt->
size & ~3;
210 if (buf_size <= 0) {
214 }
215 if (buf_size != avpkt->
size) {
217 "extra bytes at the end will be skipped.\n");
218 }
219
220 skip = buf[0];
221 last_frame = buf[1];
222 buf += 4;
223 buf_size -= 4;
224
225 /* get output buffer */
229
233 c->
dsp.
bswap_buf((uint32_t *)c->
bits, (
const uint32_t *)buf, buf_size >> 2);
236
237 /* read subband indexes */
239 for(ch = 0; ch < 2; ch++){
243 else bands[i].
res[ch] = bands[i-1].
res[ch] +
t;
244 if (bands[i].
res[ch] < -1 || bands[i].
res[ch] > 17) {
247 }
248 }
249
250 if(bands[i].
res[0] || bands[i].
res[1]){
251 mb = i;
253 }
254 }
255 /* get scale indexes coding method */
256 for(i = 0; i <=
mb; i++)
257 for(ch = 0; ch < 2; ch++)
259 /* get scale indexes */
260 for(i = 0; i <=
mb; i++){
261 for(ch = 0; ch < 2; ch++){
262 if(bands[i].
res[ch]){
265 switch(bands[i].scfi[ch]){
266 case 0:
269 break;
270 case 1:
273 break;
274 case 2:
277 break;
278 case 3:
280 break;
281 }
283 }
284 }
285 }
286 /* get quantizers */
287 memset(c->
Q, 0,
sizeof(c->
Q));
288 off = 0;
290 for(ch = 0; ch < 2; ch++)
292
294 if(last_frame)
296
298 bits_avail = buf_size * 8;
299 if (!last_frame && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))) {
300 av_log(avctx,
AV_LOG_ERROR,
"Error decoding frame: used %i of %i bits\n", bits_used, bits_avail);
301 return -1;
302 }
305 *got_frame_ptr = 0;
307 }
308
309 *got_frame_ptr = 1;
310
312 }
313
315 {
317
320 }
321
323 {
327 return 0;
328 }
329
343 };