]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpc8.c
avcodec/mpc8: Avoid code duplication when initializing 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 av_cold void build_vlc(VLC *vlc, unsigned *buf_offset,
90                               const uint8_t codes_counts[16],
91                               const uint8_t **syms, int offset)
92 {
93     static VLC_TYPE vlc_buf[9296][2];
94     uint8_t len[MPC8_MAX_VLC_SIZE];
95     unsigned num = 0;
96
97     vlc->table           = &vlc_buf[*buf_offset];
98     vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *buf_offset;
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, FFMIN(len[0], 9), num, len, 1,
105                              *syms, 1, 1, offset, INIT_VLC_STATIC_OVERLONG, NULL);
106     *buf_offset += vlc->table_size;
107     *syms       += num;
108 }
109
110 static av_cold int mpc8_decode_init(AVCodecContext * avctx)
111 {
112     int i, offset = 0;
113     MPCContext *c = avctx->priv_data;
114     GetBitContext gb;
115     static int vlc_initialized = 0;
116     const uint8_t *q_syms   = mpc8_q_syms,  *bands_syms = mpc8_bands_syms;
117     const uint8_t *res_syms = mpc8_res_syms, *scfi_syms = mpc8_scfi_syms;
118     const uint8_t *dscf_syms = mpc8_dscf_syms;
119     int channels;
120
121     if(avctx->extradata_size < 2){
122         av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
123         return -1;
124     }
125     memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
126     av_lfg_init(&c->rnd, 0xDEADBEEF);
127     ff_mpadsp_init(&c->mpadsp);
128
129     init_get_bits(&gb, avctx->extradata, 16);
130
131     skip_bits(&gb, 3);//sample rate
132     c->maxbands = get_bits(&gb, 5) + 1;
133     if (c->maxbands >= BANDS) {
134         av_log(avctx,AV_LOG_ERROR, "maxbands %d too high\n", c->maxbands);
135         return AVERROR_INVALIDDATA;
136     }
137     channels = get_bits(&gb, 4) + 1;
138     if (channels > 2) {
139         avpriv_request_sample(avctx, "Multichannel MPC SV8");
140         return AVERROR_PATCHWELCOME;
141     }
142     c->MSS = get_bits1(&gb);
143     c->frames = 1 << (get_bits(&gb, 3) * 2);
144
145     avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
146     avctx->channel_layout = (channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
147     avctx->channels = channels;
148
149     if(vlc_initialized) return 0;
150     av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
151
152     build_vlc(&band_vlc, &offset, mpc8_bands_len_counts, &bands_syms, 0);
153
154     build_vlc(&q1_vlc,   &offset, mpc8_q1_len_counts,   &q_syms, 0);
155     build_vlc(&q9up_vlc, &offset, mpc8_q9up_len_counts, &q_syms, 0);
156
157     for(i = 0; i < 2; i++){
158         build_vlc(&scfi_vlc[i], &offset, mpc8_scfi_len_counts[i], &scfi_syms, 0);
159
160         build_vlc(&dscf_vlc[i], &offset, mpc8_dscf_len_counts[i], &dscf_syms, 0);
161
162         build_vlc(&res_vlc[i],  &offset, mpc8_res_len_counts[i],  &res_syms,  0);
163
164         build_vlc(&q2_vlc[i], &offset, mpc8_q2_len_counts[i], &q_syms, 0);
165         build_vlc(&q3_vlc[i], &offset, mpc8_q34_len_counts[i],
166                   &q_syms, -48 - 16 * i);
167         for (int j = 0; j < 4; j++)
168             build_vlc(&quant_vlc[j][i], &offset, mpc8_q5_8_len_counts[i][j],
169                       &q_syms, -((8 << j) - 1));
170     }
171     vlc_initialized = 1;
172     ff_mpa_synth_init_fixed();
173
174     return 0;
175 }
176
177 static int mpc8_decode_frame(AVCodecContext * avctx, void *data,
178                              int *got_frame_ptr, AVPacket *avpkt)
179 {
180     AVFrame *frame     = data;
181     const uint8_t *buf = avpkt->data;
182     int buf_size = avpkt->size;
183     MPCContext *c = avctx->priv_data;
184     GetBitContext gb2, *gb = &gb2;
185     int i, j, k, ch, cnt, res, t;
186     Band *bands = c->bands;
187     int off;
188     int maxband, keyframe;
189     int last[2];
190
191     keyframe = c->cur_frame == 0;
192
193     if(keyframe){
194         memset(c->Q, 0, sizeof(c->Q));
195         c->last_bits_used = 0;
196     }
197     if ((res = init_get_bits8(gb, buf, buf_size)) < 0)
198         return res;
199
200     skip_bits(gb, c->last_bits_used & 7);
201
202     if(keyframe)
203         maxband = mpc8_get_mod_golomb(gb, c->maxbands + 1);
204     else{
205         maxband = c->last_max_band + get_vlc2(gb, band_vlc.table, MPC8_BANDS_BITS, 2);
206         if(maxband > 32) maxband -= 33;
207     }
208
209     if (get_bits_left(gb) < 0) {
210         *got_frame_ptr = 0;
211         return buf_size;
212     }
213
214     if(maxband > c->maxbands + 1) {
215         av_log(avctx, AV_LOG_ERROR, "maxband %d too large\n",maxband);
216         return AVERROR_INVALIDDATA;
217     }
218     c->last_max_band = maxband;
219
220     /* read subband indexes */
221     if(maxband){
222         last[0] = last[1] = 0;
223         for(i = maxband - 1; i >= 0; i--){
224             for(ch = 0; ch < 2; ch++){
225                 last[ch] = get_vlc2(gb, res_vlc[last[ch] > 2].table, MPC8_RES_BITS, 2) + last[ch];
226                 if(last[ch] > 15) last[ch] -= 17;
227                 bands[i].res[ch] = last[ch];
228             }
229         }
230         if(c->MSS){
231             int mask;
232
233             cnt = 0;
234             for(i = 0; i < maxband; i++)
235                 if(bands[i].res[0] || bands[i].res[1])
236                     cnt++;
237             t = mpc8_get_mod_golomb(gb, cnt);
238             mask = mpc8_get_mask(gb, cnt, t);
239             for(i = maxband - 1; i >= 0; i--)
240                 if(bands[i].res[0] || bands[i].res[1]){
241                     bands[i].msf = mask & 1;
242                     mask >>= 1;
243                 }
244         }
245     }
246     for(i = maxband; i < c->maxbands; i++)
247         bands[i].res[0] = bands[i].res[1] = 0;
248
249     if(keyframe){
250         for(i = 0; i < 32; i++)
251             c->oldDSCF[0][i] = c->oldDSCF[1][i] = 1;
252     }
253
254     for(i = 0; i < maxband; i++){
255         if(bands[i].res[0] || bands[i].res[1]){
256             cnt = !!bands[i].res[0] + !!bands[i].res[1] - 1;
257             if(cnt >= 0){
258                 t = get_vlc2(gb, scfi_vlc[cnt].table, scfi_vlc[cnt].bits, 1);
259                 if(bands[i].res[0]) bands[i].scfi[0] = t >> (2 * cnt);
260                 if(bands[i].res[1]) bands[i].scfi[1] = t & 3;
261             }
262         }
263     }
264
265     for(i = 0; i < maxband; i++){
266         for(ch = 0; ch < 2; ch++){
267             if(!bands[i].res[ch]) continue;
268
269             if(c->oldDSCF[ch][i]){
270                 bands[i].scf_idx[ch][0] = get_bits(gb, 7) - 6;
271                 c->oldDSCF[ch][i] = 0;
272             }else{
273                 t = get_vlc2(gb, dscf_vlc[1].table, MPC8_DSCF1_BITS, 2);
274                 if(t == 64)
275                     t += get_bits(gb, 6);
276                 bands[i].scf_idx[ch][0] = ((bands[i].scf_idx[ch][2] + t - 25) & 0x7F) - 6;
277             }
278             for(j = 0; j < 2; j++){
279                 if((bands[i].scfi[ch] << j) & 2)
280                     bands[i].scf_idx[ch][j + 1] = bands[i].scf_idx[ch][j];
281                 else{
282                     t = get_vlc2(gb, dscf_vlc[0].table, MPC8_DSCF0_BITS, 2);
283                     if(t == 31)
284                         t = 64 + get_bits(gb, 6);
285                     bands[i].scf_idx[ch][j + 1] = ((bands[i].scf_idx[ch][j] + t - 25) & 0x7F) - 6;
286                 }
287             }
288         }
289     }
290
291     for(i = 0, off = 0; i < maxband; i++, off += SAMPLES_PER_BAND){
292         for(ch = 0; ch < 2; ch++){
293             res = bands[i].res[ch];
294             switch(res){
295             case -1:
296                 for(j = 0; j < SAMPLES_PER_BAND; j++)
297                     c->Q[ch][off + j] = (av_lfg_get(&c->rnd) & 0x3FC) - 510;
298                 break;
299             case 0:
300                 break;
301             case 1:
302                 for(j = 0; j < SAMPLES_PER_BAND; j += SAMPLES_PER_BAND / 2){
303                     cnt = get_vlc2(gb, q1_vlc.table, MPC8_Q1_BITS, 2);
304                     t = mpc8_get_mask(gb, 18, cnt);
305                     for(k = 0; k < SAMPLES_PER_BAND / 2; k++)
306                         c->Q[ch][off + j + k] = t & (1 << (SAMPLES_PER_BAND / 2 - k - 1))
307                                                 ? (get_bits1(gb) << 1) - 1 : 0;
308                 }
309                 break;
310             case 2:
311                 cnt = 6;//2*mpc8_thres[res]
312                 for(j = 0; j < SAMPLES_PER_BAND; j += 3){
313                     t = get_vlc2(gb, q2_vlc[cnt > 3].table, MPC8_Q2_BITS, 2);
314                     c->Q[ch][off + j + 0] = mpc8_idx50[t];
315                     c->Q[ch][off + j + 1] = mpc8_idx51[t];
316                     c->Q[ch][off + j + 2] = mpc8_idx52[t];
317                     cnt = (cnt >> 1) + mpc8_huffq2[t];
318                 }
319                 break;
320             case 3:
321             case 4:
322                 for(j = 0; j < SAMPLES_PER_BAND; j += 2){
323                     t = get_vlc2(gb, q3_vlc[res - 3].table, MPC8_Q3_BITS, 2);
324                     c->Q[ch][off + j + 1] = t >> 4;
325                     c->Q[ch][off + j + 0] = sign_extend(t, 4);
326                 }
327                 break;
328             case 5:
329             case 6:
330             case 7:
331             case 8:
332                 cnt = 2 * mpc8_thres[res];
333                 for(j = 0; j < SAMPLES_PER_BAND; j++){
334                     const VLC *vlc = &quant_vlc[res - 5][cnt > mpc8_thres[res]];
335                     c->Q[ch][off + j] = get_vlc2(gb, vlc->table, vlc->bits, 2);
336                     cnt = (cnt >> 1) + FFABS(c->Q[ch][off + j]);
337                 }
338                 break;
339             default:
340                 for(j = 0; j < SAMPLES_PER_BAND; j++){
341                     c->Q[ch][off + j] = get_vlc2(gb, q9up_vlc.table, MPC8_Q9UP_BITS, 2);
342                     if(res != 9){
343                         c->Q[ch][off + j] <<= res - 9;
344                         c->Q[ch][off + j] |= get_bits(gb, res - 9);
345                     }
346                     c->Q[ch][off + j] -= (1 << (res - 2)) - 1;
347                 }
348             }
349         }
350     }
351
352     frame->nb_samples = MPC_FRAME_SIZE;
353     if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
354         return res;
355
356     ff_mpc_dequantize_and_synth(c, maxband - 1,
357                                 (int16_t **)frame->extended_data,
358                                 avctx->channels);
359
360     c->cur_frame++;
361
362     c->last_bits_used = get_bits_count(gb);
363     if(c->cur_frame >= c->frames)
364         c->cur_frame = 0;
365     if (get_bits_left(gb) < 0) {
366         av_log(avctx, AV_LOG_ERROR, "Overread %d\n", -get_bits_left(gb));
367         c->last_bits_used = buf_size << 3;
368     } else if (c->cur_frame == 0 && get_bits_left(gb) < 8) {// we have only padding left
369         c->last_bits_used = buf_size << 3;
370     }
371
372     *got_frame_ptr = 1;
373
374     return c->cur_frame ? c->last_bits_used >> 3 : buf_size;
375 }
376
377 static av_cold void mpc8_decode_flush(AVCodecContext *avctx)
378 {
379     MPCContext *c = avctx->priv_data;
380     c->cur_frame = 0;
381 }
382
383 AVCodec ff_mpc8_decoder = {
384     .name           = "mpc8",
385     .long_name      = NULL_IF_CONFIG_SMALL("Musepack SV8"),
386     .type           = AVMEDIA_TYPE_AUDIO,
387     .id             = AV_CODEC_ID_MUSEPACK8,
388     .priv_data_size = sizeof(MPCContext),
389     .init           = mpc8_decode_init,
390     .decode         = mpc8_decode_frame,
391     .flush          = mpc8_decode_flush,
392     .capabilities   = AV_CODEC_CAP_DR1,
393     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
394                                                       AV_SAMPLE_FMT_NONE },
395 };