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