1 /*
2 * Musepack SV8 decoder
3 * Copyright (c) 2007 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
35
39
42
45
47 {
50
53
54 return code;
55 }
56
58 {
62
63 do {
64 n--;
65 if (code >= C[n]) {
68 C -= 32;
69 k--;
70 }
71 } while(k > 0);
72
74 }
75
77 {
80 }
81
83 {
85
86 if(t && t != size)
88 if((t << 1) > size) mask = ~mask;
89
91 }
92
94 0, 640, 1184, 1748, 2298, 2426, 2554, 3066, 3578, 4106, 4618, 5196, 5708
95 };
96
98 {
99 int i;
102 static int vlc_initialized = 0;
103 int channels;
104
110 static VLC_TYPE dscf0_table[560][2];
111 static VLC_TYPE dscf1_table[598][2];
114 static VLC_TYPE codes_table[5708][2];
115
118 return -1;
119 }
124
126
128
134 }
136 if (channels > 2) {
139 }
142
146
147 if(vlc_initialized) return 0;
149
150 band_vlc.
table = band_table;
155
156 q1_vlc.
table = q1_table;
161 q9up_vlc.
table = q9up_table;
166
167 scfi_vlc[0].
table = scfi0_table;
172 scfi_vlc[1].
table = scfi1_table;
177
178 dscf_vlc[0].
table = dscf0_table;
183 dscf_vlc[1].
table = dscf1_table;
188
189 q3_vlc[0].
table = q3_0_table;
195 q3_vlc[1].
table = q3_1_table;
201
202 for(i = 0; i < 2; i++){
208
209 q2_vlc[i].
table = &codes_table[vlc_offsets[2+i]];
214
215 quant_vlc[0][i].
table = &codes_table[vlc_offsets[4+i]];
220 quant_vlc[1][i].
table = &codes_table[vlc_offsets[6+i]];
225 quant_vlc[2][i].
table = &codes_table[vlc_offsets[8+i]];
230 quant_vlc[3][i].
table = &codes_table[vlc_offsets[10+i]];
231 quant_vlc[3][i].
table_allocated = vlc_offsets[11+i] - vlc_offsets[10+i];
235 }
236 vlc_initialized = 1;
237
238 return 0;
239 }
240
242 int *got_frame_ptr,
AVPacket *avpkt)
243 {
246 int buf_size = avpkt->
size;
249 int i, j, k, ch, cnt,
res,
t;
252 int maxband, keyframe;
253 int last[2];
254
255 /* get output buffer */
259
261
262 if(keyframe){
263 memset(c->
Q, 0,
sizeof(c->
Q));
265 }
268
269 if(keyframe)
271 else{
273 if(maxband > 32) maxband -= 33;
274 }
275
279 }
281
282 /* read subband indexes */
283 if(maxband){
284 last[0] = last[1] = 0;
285 for(i = maxband - 1; i >= 0; i--){
286 for(ch = 0; ch < 2; ch++){
288 if(last[ch] > 15) last[ch] -= 17;
289 bands[i].
res[ch] = last[ch];
290 }
291 }
294
295 cnt = 0;
296 for(i = 0; i < maxband; i++)
297 if(bands[i].res[0] || bands[i].res[1])
298 cnt++;
301 for(i = maxband - 1; i >= 0; i--)
302 if(bands[i].res[0] || bands[i].res[1]){
303 bands[i].
msf = mask & 1;
304 mask >>= 1;
305 }
306 }
307 }
308 for(i = maxband; i < c->
maxbands; i++)
309 bands[i].res[0] = bands[i].res[1] = 0;
310
311 if(keyframe){
312 for(i = 0; i < 32; i++)
314 }
315
316 for(i = 0; i < maxband; i++){
317 if(bands[i].res[0] || bands[i].res[1]){
318 cnt = !!bands[i].
res[0] + !!bands[i].
res[1] - 1;
319 if(cnt >= 0){
321 if(bands[i].res[0]) bands[i].
scfi[0] = t >> (2 * cnt);
322 if(bands[i].res[1]) bands[i].
scfi[1] = t & 3;
323 }
324 }
325 }
326
327 for(i = 0; i < maxband; i++){
328 for(ch = 0; ch < 2; ch++){
329 if(!bands[i].res[ch]) continue;
330
334 }else{
336 if(t == 64)
338 bands[i].
scf_idx[ch][0] = ((bands[i].
scf_idx[ch][2] + t - 25) & 0x7F) - 6;
339 }
340 for(j = 0; j < 2; j++){
341 if((bands[i].scfi[ch] << j) & 2)
342 bands[i].scf_idx[ch][j + 1] = bands[i].scf_idx[ch][j];
343 else{
345 if(t == 31)
347 bands[i].
scf_idx[ch][j + 1] = ((bands[i].
scf_idx[ch][j] + t - 25) & 0x7F) - 6;
348 }
349 }
350 }
351 }
352
354 for(ch = 0; ch < 2; ch++){
355 res = bands[i].
res[ch];
356 switch(res){
357 case -1:
360 break;
361 case 0:
362 break;
363 case 1:
367 for(k = 0; k < SAMPLES_PER_BAND / 2; k++, t <<= 1)
368 c->
Q[ch][off + j + k] = (t & 0x20000) ? (
get_bits1(gb) << 1) - 1 : 0;
369 }
370 break;
371 case 2:
372 cnt = 6;//2*mpc8_thres[res]
379 }
380 break;
381 case 3:
382 case 4:
385 c->
Q[ch][off + j + 1] = t >> 4;
386 c->
Q[ch][off + j + 0] = (t & 8) ? (t & 0xF) - 16 : (t & 0xF);
387 }
388 break;
389 case 5:
390 case 6:
391 case 7:
392 case 8:
396 c->
Q[ch][off + j] =
t;
397 cnt = (cnt >> 1) +
FFABS(c->
Q[ch][off + j]);
398 }
399 break;
400 default:
403 if(res != 9){
404 c->
Q[ch][off + j] <<= res - 9;
405 c->
Q[ch][off + j] |=
get_bits(gb, res - 9);
406 }
407 c->
Q[ch][off + j] -= (1 << (res - 2)) - 1;
408 }
409 }
410 }
411 }
412
416
418
424
425 *got_frame_ptr = 1;
426
428 }
429
431 {
434 }
435
448 };