Coverage Report

Created: 2026-01-16 07:48

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