]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpc8.c
avcodec/mpc8: Reduce the size of the length tables to initialize VLCs
[ffmpeg] / libavcodec / mpc8.c
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
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/lfg.h"
30 #include "avcodec.h"
31 #include "get_bits.h"
32 #include "internal.h"
33 #include "mpegaudiodsp.h"
34
35 #include "mpc.h"
36 #include "mpc8data.h"
37 #include "mpc8huff.h"
38
39 static VLC band_vlc, scfi_vlc[2], dscf_vlc[2], res_vlc[2];
40 static VLC q1_vlc, q2_vlc[2], q3_vlc[2], quant_vlc[4][2], q9up_vlc;
41
42 static inline int mpc8_dec_base(GetBitContext *gb, int k, int n)
43 {
44     int len = mpc8_cnk_len[k-1][n-1] - 1;
45     int code = len ? get_bits_long(gb, len) : 0;
46
47     if (code >= mpc8_cnk_lost[k-1][n-1])
48         code = ((code << 1) | get_bits1(gb)) - mpc8_cnk_lost[k-1][n-1];
49
50     return code;
51 }
52
53 static inline int mpc8_dec_enum(GetBitContext *gb, int k, int n)
54 {
55     int bits = 0;
56     const uint32_t * C = mpc8_cnk[k-1];
57     int code = mpc8_dec_base(gb, k, n);
58
59     do {
60         n--;
61         if (code >= C[n]) {
62             bits |= 1U << n;
63             code -= C[n];
64             C -= 32;
65             k--;
66         }
67     } while(k > 0);
68
69     return bits;
70 }
71
72 static inline int mpc8_get_mod_golomb(GetBitContext *gb, int m)
73 {
74     if(mpc8_cnk_len[0][m] < 1) return 0;
75     return mpc8_dec_base(gb, 1, m+1);
76 }
77
78 static int mpc8_get_mask(GetBitContext *gb, int size, int t)
79 {
80     int mask = 0;
81
82     if(t && t != size)
83          mask = mpc8_dec_enum(gb, FFMIN(t, size - t), size);
84     if((t << 1) > size) mask = ~mask;
85
86     return mask;
87 }
88
89 static const uint16_t vlc_offsets[13] = {
90     0, 640, 1184, 1748, 2298, 2426, 2554, 3066, 3578, 4106, 4618, 5196, 5708
91 };
92
93 static av_cold void build_vlc(VLC *vlc, int nb_bits,
94                               const uint8_t codes_counts[16],
95                               const uint8_t syms[], int offset)
96 {
97     uint8_t len[MPC8_MAX_VLC_SIZE];
98     unsigned num = 0;
99
100     for (int i = 16; i > 0; i--)
101         for (unsigned tmp = num + codes_counts[i - 1]; num < tmp; num++)
102             len[num] = i;
103
104     ff_init_vlc_from_lengths(vlc, nb_bits, num, len, 1,
105                              syms, 1, 1, offset, INIT_VLC_USE_NEW_STATIC, NULL);
106 }
107
108 static av_cold int mpc8_decode_init(AVCodecContext * avctx)
109 {
110     int i;
111     MPCContext *c = avctx->priv_data;
112     GetBitContext gb;
113     static int vlc_initialized = 0;
114     int channels;
115     static VLC_TYPE codes_table[5708][2];
116
117     if(avctx->extradata_size < 2){
118         av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
119         return -1;
120     }
121     memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
122     av_lfg_init(&c->rnd, 0xDEADBEEF);
123     ff_mpadsp_init(&c->mpadsp);
124
125     init_get_bits(&gb, avctx->extradata, 16);
126
127     skip_bits(&gb, 3);//sample rate
128     c->maxbands = get_bits(&gb, 5) + 1;
129     if (c->maxbands >= BANDS) {
130         av_log(avctx,AV_LOG_ERROR, "maxbands %d too high\n", c->maxbands);
131         return AVERROR_INVALIDDATA;
132     }
133     channels = get_bits(&gb, 4) + 1;
134     if (channels > 2) {
135         avpriv_request_sample(avctx, "Multichannel MPC SV8");
136         return AVERROR_PATCHWELCOME;
137     }
138     c->MSS = get_bits1(&gb);
139     c->frames = 1 << (get_bits(&gb, 3) * 2);
140
141     avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
142     avctx->channel_layout = (channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
143     avctx->channels = channels;
144
145     if(vlc_initialized) return 0;
146     av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
147
148 #define INIT_VLC(vlc, bits, len_counts, symbols, offset, static_size) \
149     do {                                                                   \
150         static VLC_TYPE table[static_size][2];                             \
151         (vlc)->table           = table;                                    \
152         (vlc)->table_allocated = static_size;                              \
153         build_vlc(vlc, bits, len_counts, symbols, offset);                 \
154     } while (0)
155
156
157     INIT_VLC(&band_vlc, MPC8_BANDS_BITS,
158              mpc8_bands_len_counts, mpc8_bands_syms, 0, 542);
159
160     INIT_VLC(&q1_vlc, MPC8_Q1_BITS,
161              mpc8_q1_len_counts, mpc8_q1_syms, 0, 520);
162     INIT_VLC(&q9up_vlc, MPC8_Q9UP_BITS,
163              mpc8_q9up_len_counts, mpc8_q9up_syms, 0, 524);
164
165     INIT_VLC(&scfi_vlc[0], MPC8_SCFI0_BITS,
166              mpc8_scfi_len_counts[0], mpc8_scfi0_syms, 0, 1 << MPC8_SCFI0_BITS);
167     INIT_VLC(&scfi_vlc[1], MPC8_SCFI1_BITS,
168              mpc8_scfi_len_counts[1], mpc8_scfi1_syms, 0, 1 << MPC8_SCFI1_BITS);
169
170     INIT_VLC(&dscf_vlc[0], MPC8_DSCF0_BITS,
171              mpc8_dscf_len_counts[0], mpc8_dscf0_syms, 0, 560);
172     INIT_VLC(&dscf_vlc[1], MPC8_DSCF1_BITS,
173              mpc8_dscf_len_counts[1], mpc8_dscf1_syms, 0, 598);
174
175     INIT_VLC(&q3_vlc[0], MPC8_Q3_BITS,
176              mpc8_q3_len_counts, mpc8_q3_syms, MPC8_Q3_OFFSET, 512);
177     INIT_VLC(&q3_vlc[1], MPC8_Q4_BITS,
178              mpc8_q4_len_counts, mpc8_q4_syms, MPC8_Q4_OFFSET, 516);
179
180     for(i = 0; i < 2; i++){
181         res_vlc[i].table = &codes_table[vlc_offsets[0+i]];
182         res_vlc[i].table_allocated = vlc_offsets[1+i] - vlc_offsets[0+i];
183         build_vlc(&res_vlc[i], MPC8_RES_BITS,
184                   mpc8_res_len_counts[i], mpc8_res_syms[i], 0);
185
186         q2_vlc[i].table = &codes_table[vlc_offsets[2+i]];
187         q2_vlc[i].table_allocated = vlc_offsets[3+i] - vlc_offsets[2+i];
188         build_vlc(&q2_vlc[i], MPC8_Q2_BITS,
189                   mpc8_q2_len_counts[i], mpc8_q2_syms[i], 0);
190
191         quant_vlc[0][i].table = &codes_table[vlc_offsets[4+i]];
192         quant_vlc[0][i].table_allocated = vlc_offsets[5+i] - vlc_offsets[4+i];
193         build_vlc(&quant_vlc[0][i], MPC8_Q5_BITS,
194                   mpc8_q5_len_counts[i], mpc8_q5_syms[i], MPC8_Q5_OFFSET);
195         quant_vlc[1][i].table = &codes_table[vlc_offsets[6+i]];
196         quant_vlc[1][i].table_allocated = vlc_offsets[7+i] - vlc_offsets[6+i];
197         build_vlc(&quant_vlc[1][i], MPC8_Q6_BITS,
198                   mpc8_q6_len_counts[i], mpc8_q6_syms[i], MPC8_Q6_OFFSET);
199         quant_vlc[2][i].table = &codes_table[vlc_offsets[8+i]];
200         quant_vlc[2][i].table_allocated = vlc_offsets[9+i] - vlc_offsets[8+i];
201         build_vlc(&quant_vlc[2][i], MPC8_Q7_BITS,
202                   mpc8_q7_len_counts[i], mpc8_q7_syms[i], MPC8_Q7_OFFSET);
203         quant_vlc[3][i].table = &codes_table[vlc_offsets[10+i]];
204         quant_vlc[3][i].table_allocated = vlc_offsets[11+i] - vlc_offsets[10+i];
205         build_vlc(&quant_vlc[3][i], MPC8_Q8_BITS,
206                   mpc8_q8_len_counts[i], mpc8_q8_syms[i], MPC8_Q8_OFFSET);
207     }
208     vlc_initialized = 1;
209     ff_mpa_synth_init_fixed();
210
211     return 0;
212 }
213
214 static int mpc8_decode_frame(AVCodecContext * avctx, void *data,
215                              int *got_frame_ptr, AVPacket *avpkt)
216 {
217     AVFrame *frame     = data;
218     const uint8_t *buf = avpkt->data;
219     int buf_size = avpkt->size;
220     MPCContext *c = avctx->priv_data;
221     GetBitContext gb2, *gb = &gb2;
222     int i, j, k, ch, cnt, res, t;
223     Band *bands = c->bands;
224     int off;
225     int maxband, keyframe;
226     int last[2];
227
228     keyframe = c->cur_frame == 0;
229
230     if(keyframe){
231         memset(c->Q, 0, sizeof(c->Q));
232         c->last_bits_used = 0;
233     }
234     if ((res = init_get_bits8(gb, buf, buf_size)) < 0)
235         return res;
236
237     skip_bits(gb, c->last_bits_used & 7);
238
239     if(keyframe)
240         maxband = mpc8_get_mod_golomb(gb, c->maxbands + 1);
241     else{
242         maxband = c->last_max_band + get_vlc2(gb, band_vlc.table, MPC8_BANDS_BITS, 2);
243         if(maxband > 32) maxband -= 33;
244     }
245
246     if (get_bits_left(gb) < 0) {
247         *got_frame_ptr = 0;
248         return buf_size;
249     }
250
251     if(maxband > c->maxbands + 1) {
252         av_log(avctx, AV_LOG_ERROR, "maxband %d too large\n",maxband);
253         return AVERROR_INVALIDDATA;
254     }
255     c->last_max_band = maxband;
256
257     /* read subband indexes */
258     if(maxband){
259         last[0] = last[1] = 0;
260         for(i = maxband - 1; i >= 0; i--){
261             for(ch = 0; ch < 2; ch++){
262                 last[ch] = get_vlc2(gb, res_vlc[last[ch] > 2].table, MPC8_RES_BITS, 2) + last[ch];
263                 if(last[ch] > 15) last[ch] -= 17;
264                 bands[i].res[ch] = last[ch];
265             }
266         }
267         if(c->MSS){
268             int mask;
269
270             cnt = 0;
271             for(i = 0; i < maxband; i++)
272                 if(bands[i].res[0] || bands[i].res[1])
273                     cnt++;
274             t = mpc8_get_mod_golomb(gb, cnt);
275             mask = mpc8_get_mask(gb, cnt, t);
276             for(i = maxband - 1; i >= 0; i--)
277                 if(bands[i].res[0] || bands[i].res[1]){
278                     bands[i].msf = mask & 1;
279                     mask >>= 1;
280                 }
281         }
282     }
283     for(i = maxband; i < c->maxbands; i++)
284         bands[i].res[0] = bands[i].res[1] = 0;
285
286     if(keyframe){
287         for(i = 0; i < 32; i++)
288             c->oldDSCF[0][i] = c->oldDSCF[1][i] = 1;
289     }
290
291     for(i = 0; i < maxband; i++){
292         if(bands[i].res[0] || bands[i].res[1]){
293             cnt = !!bands[i].res[0] + !!bands[i].res[1] - 1;
294             if(cnt >= 0){
295                 t = get_vlc2(gb, scfi_vlc[cnt].table, scfi_vlc[cnt].bits, 1);
296                 if(bands[i].res[0]) bands[i].scfi[0] = t >> (2 * cnt);
297                 if(bands[i].res[1]) bands[i].scfi[1] = t & 3;
298             }
299         }
300     }
301
302     for(i = 0; i < maxband; i++){
303         for(ch = 0; ch < 2; ch++){
304             if(!bands[i].res[ch]) continue;
305
306             if(c->oldDSCF[ch][i]){
307                 bands[i].scf_idx[ch][0] = get_bits(gb, 7) - 6;
308                 c->oldDSCF[ch][i] = 0;
309             }else{
310                 t = get_vlc2(gb, dscf_vlc[1].table, MPC8_DSCF1_BITS, 2);
311                 if(t == 64)
312                     t += get_bits(gb, 6);
313                 bands[i].scf_idx[ch][0] = ((bands[i].scf_idx[ch][2] + t - 25) & 0x7F) - 6;
314             }
315             for(j = 0; j < 2; j++){
316                 if((bands[i].scfi[ch] << j) & 2)
317                     bands[i].scf_idx[ch][j + 1] = bands[i].scf_idx[ch][j];
318                 else{
319                     t = get_vlc2(gb, dscf_vlc[0].table, MPC8_DSCF0_BITS, 2);
320                     if(t == 31)
321                         t = 64 + get_bits(gb, 6);
322                     bands[i].scf_idx[ch][j + 1] = ((bands[i].scf_idx[ch][j] + t - 25) & 0x7F) - 6;
323                 }
324             }
325         }
326     }
327
328     for(i = 0, off = 0; i < maxband; i++, off += SAMPLES_PER_BAND){
329         for(ch = 0; ch < 2; ch++){
330             res = bands[i].res[ch];
331             switch(res){
332             case -1:
333                 for(j = 0; j < SAMPLES_PER_BAND; j++)
334                     c->Q[ch][off + j] = (av_lfg_get(&c->rnd) & 0x3FC) - 510;
335                 break;
336             case 0:
337                 break;
338             case 1:
339                 for(j = 0; j < SAMPLES_PER_BAND; j += SAMPLES_PER_BAND / 2){
340                     cnt = get_vlc2(gb, q1_vlc.table, MPC8_Q1_BITS, 2);
341                     t = mpc8_get_mask(gb, 18, cnt);
342                     for(k = 0; k < SAMPLES_PER_BAND / 2; k++)
343                         c->Q[ch][off + j + k] = t & (1 << (SAMPLES_PER_BAND / 2 - k - 1))
344                                                 ? (get_bits1(gb) << 1) - 1 : 0;
345                 }
346                 break;
347             case 2:
348                 cnt = 6;//2*mpc8_thres[res]
349                 for(j = 0; j < SAMPLES_PER_BAND; j += 3){
350                     t = get_vlc2(gb, q2_vlc[cnt > 3].table, MPC8_Q2_BITS, 2);
351                     c->Q[ch][off + j + 0] = mpc8_idx50[t];
352                     c->Q[ch][off + j + 1] = mpc8_idx51[t];
353                     c->Q[ch][off + j + 2] = mpc8_idx52[t];
354                     cnt = (cnt >> 1) + mpc8_huffq2[t];
355                 }
356                 break;
357             case 3:
358             case 4:
359                 for(j = 0; j < SAMPLES_PER_BAND; j += 2){
360                     t = get_vlc2(gb, q3_vlc[res - 3].table, MPC8_Q3_BITS, 2);
361                     c->Q[ch][off + j + 1] = t >> 4;
362                     c->Q[ch][off + j + 0] = sign_extend(t, 4);
363                 }
364                 break;
365             case 5:
366             case 6:
367             case 7:
368             case 8:
369                 cnt = 2 * mpc8_thres[res];
370                 for(j = 0; j < SAMPLES_PER_BAND; j++){
371                     const VLC *vlc = &quant_vlc[res - 5][cnt > mpc8_thres[res]];
372                     c->Q[ch][off + j] = get_vlc2(gb, vlc->table, vlc->bits, 2);
373                     cnt = (cnt >> 1) + FFABS(c->Q[ch][off + j]);
374                 }
375                 break;
376             default:
377                 for(j = 0; j < SAMPLES_PER_BAND; j++){
378                     c->Q[ch][off + j] = get_vlc2(gb, q9up_vlc.table, MPC8_Q9UP_BITS, 2);
379                     if(res != 9){
380                         c->Q[ch][off + j] <<= res - 9;
381                         c->Q[ch][off + j] |= get_bits(gb, res - 9);
382                     }
383                     c->Q[ch][off + j] -= (1 << (res - 2)) - 1;
384                 }
385             }
386         }
387     }
388
389     frame->nb_samples = MPC_FRAME_SIZE;
390     if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
391         return res;
392
393     ff_mpc_dequantize_and_synth(c, maxband - 1,
394                                 (int16_t **)frame->extended_data,
395                                 avctx->channels);
396
397     c->cur_frame++;
398
399     c->last_bits_used = get_bits_count(gb);
400     if(c->cur_frame >= c->frames)
401         c->cur_frame = 0;
402     if (get_bits_left(gb) < 0) {
403         av_log(avctx, AV_LOG_ERROR, "Overread %d\n", -get_bits_left(gb));
404         c->last_bits_used = buf_size << 3;
405     } else if (c->cur_frame == 0 && get_bits_left(gb) < 8) {// we have only padding left
406         c->last_bits_used = buf_size << 3;
407     }
408
409     *got_frame_ptr = 1;
410
411     return c->cur_frame ? c->last_bits_used >> 3 : buf_size;
412 }
413
414 static av_cold void mpc8_decode_flush(AVCodecContext *avctx)
415 {
416     MPCContext *c = avctx->priv_data;
417     c->cur_frame = 0;
418 }
419
420 AVCodec ff_mpc8_decoder = {
421     .name           = "mpc8",
422     .long_name      = NULL_IF_CONFIG_SMALL("Musepack SV8"),
423     .type           = AVMEDIA_TYPE_AUDIO,
424     .id             = AV_CODEC_ID_MUSEPACK8,
425     .priv_data_size = sizeof(MPCContext),
426     .init           = mpc8_decode_init,
427     .decode         = mpc8_decode_frame,
428     .flush          = mpc8_decode_flush,
429     .capabilities   = AV_CODEC_CAP_DR1,
430     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
431                                                       AV_SAMPLE_FMT_NONE },
432 };