FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpc7.c
Date: 2026-10-29 17:07:12
Exec Total Coverage
Lines: 148 179 82.7%
Functions: 6 7 85.7%
Branches: 68 89 76.4%

Line Branch Exec Source
1 /*
2 * Musepack SV7 decoder
3 * Copyright (c) 2006 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/attributes.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/lfg.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/mem_internal.h"
34 #include "libavutil/thread.h"
35
36 #include "avcodec.h"
37 #include "codec_internal.h"
38 #include "decode.h"
39 #include "get_bits.h"
40 #include "mpegaudiodsp.h"
41
42 #include "mpc.h"
43 #include "mpc7data.h"
44
45 static VLCElem scfi_vlc[1 << MPC7_SCFI_BITS];
46 static VLCElem dscf_vlc[1 << MPC7_DSCF_BITS];
47 static VLCElem hdr_vlc [1 << MPC7_HDR_BITS];
48 static const VLCElem *quant_vlc[MPC7_QUANT_VLC_TABLES][2];
49
50 2 static av_cold void mpc7_init_static(void)
51 {
52 static VLCElem quant_tables[7224];
53 2 VLCInitState state = VLC_INIT_STATE(quant_tables);
54 2 const uint8_t *raw_quant_table = mpc7_quant_vlcs;
55
56 2 VLC_INIT_STATIC_TABLE_FROM_LENGTHS(scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE,
57 &mpc7_scfi[1], 2,
58 &mpc7_scfi[0], 2, 1, 0, 0);
59 2 VLC_INIT_STATIC_TABLE_FROM_LENGTHS(dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE,
60 &mpc7_dscf[1], 2,
61 &mpc7_dscf[0], 2, 1, -7, 0);
62 2 VLC_INIT_STATIC_TABLE_FROM_LENGTHS(hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE,
63 &mpc7_hdr[1], 2,
64 &mpc7_hdr[0], 2, 1, -5, 0);
65
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
16 for (int i = 0; i < MPC7_QUANT_VLC_TABLES; i++) {
66
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 14 times.
42 for (int j = 0; j < 2; j++) {
67 28 quant_vlc[i][j] =
68 28 ff_vlc_init_tables_from_lengths(&state, 9, mpc7_quant_vlc_sizes[i],
69 28 &raw_quant_table[1], 2,
70 &raw_quant_table[0], 2, 1,
71 28 mpc7_quant_vlc_off[i], 0);
72 28 raw_quant_table += 2 * mpc7_quant_vlc_sizes[i];
73 }
74 }
75 2 ff_mpa_synth_init_fixed();
76 2 }
77
78 3 static av_cold int mpc7_decode_init(AVCodecContext * avctx)
79 {
80 static AVOnce init_static_once = AV_ONCE_INIT;
81 3 MPCContext *c = avctx->priv_data;
82 GetBitContext gb;
83 3 LOCAL_ALIGNED_16(uint8_t, buf, [16]);
84
85 /* Musepack SV7 is always stereo */
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (avctx->ch_layout.nb_channels != 2) {
87 ✗ avpriv_request_sample(avctx, "%d channels", avctx->ch_layout.nb_channels);
88 ✗ return AVERROR_PATCHWELCOME;
89 }
90
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if(avctx->extradata_size < 16){
92 ✗ av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
93 ✗ return AVERROR_INVALIDDATA;
94 }
95 3 memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
96 3 av_lfg_init(&c->rnd, 0xDEADBEEF);
97 3 ff_bswapdsp_init(&c->bdsp);
98 3 ff_mpadsp_init(&c->mpadsp);
99 3 c->bdsp.bswap_buf((uint32_t *) buf, (const uint32_t *) avctx->extradata, 4);
100 3 init_get_bits(&gb, buf, 128);
101
102 3 c->IS = get_bits1(&gb);
103 3 c->MSS = get_bits1(&gb);
104 3 c->maxbands = get_bits(&gb, 6);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if(c->maxbands >= BANDS){
106 ✗ av_log(avctx, AV_LOG_ERROR, "Too many bands: %i\n", c->maxbands);
107 ✗ return AVERROR_INVALIDDATA;
108 }
109 3 skip_bits_long(&gb, 88);
110 3 c->gapless = get_bits1(&gb);
111 3 c->lastframelen = get_bits(&gb, 11);
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (c->lastframelen > MPC_FRAME_SIZE) {
113 ✗ av_log(avctx, AV_LOG_ERROR, "Invalid last frame length: %d\n", c->lastframelen);
114 ✗ return AVERROR_INVALIDDATA;
115 }
116 3 av_log(avctx, AV_LOG_DEBUG, "IS: %d, MSS: %d, TG: %d, LFL: %d, bands: %d\n",
117 c->IS, c->MSS, c->gapless, c->lastframelen, c->maxbands);
118 3 c->frames_to_skip = 0;
119
120 3 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
121 3 av_channel_layout_uninit(&avctx->ch_layout);
122 3 avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
123
124 3 ff_thread_once(&init_static_once, mpc7_init_static);
125
126 3 return 0;
127 }
128
129 /**
130 * Fill samples for given subband
131 */
132 29184 static inline void idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst)
133 {
134 int i, i1, t;
135
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 3499 times.
✓ Branch 2 taken 6128 times.
✓ Branch 3 taken 7935 times.
✓ Branch 4 taken 720 times.
✓ Branch 5 taken 10902 times.
29184 switch(idx){
136 ✗ case -1:
137 ✗ for(i = 0; i < SAMPLES_PER_BAND; i++){
138 ✗ *dst++ = (av_lfg_get(&c->rnd) & 0x3FC) - 510;
139 }
140 ✗ break;
141 3499 case 1:
142 3499 i1 = get_bits1(gb);
143
2/2
✓ Branch 0 taken 41988 times.
✓ Branch 1 taken 3499 times.
45487 for(i = 0; i < SAMPLES_PER_BAND/3; i++){
144 41988 t = get_vlc2(gb, quant_vlc[0][i1], 9, 2);
145 41988 *dst++ = mpc7_idx30[t];
146 41988 *dst++ = mpc7_idx31[t];
147 41988 *dst++ = mpc7_idx32[t];
148 }
149 3499 break;
150 6128 case 2:
151 6128 i1 = get_bits1(gb);
152
2/2
✓ Branch 0 taken 110304 times.
✓ Branch 1 taken 6128 times.
116432 for(i = 0; i < SAMPLES_PER_BAND/2; i++){
153 110304 t = get_vlc2(gb, quant_vlc[1][i1], 9, 2);
154 110304 *dst++ = mpc7_idx50[t];
155 110304 *dst++ = mpc7_idx51[t];
156 }
157 6128 break;
158 7935 case 3: case 4: case 5: case 6: case 7:
159 7935 i1 = get_bits1(gb);
160
2/2
✓ Branch 0 taken 285660 times.
✓ Branch 1 taken 7935 times.
293595 for(i = 0; i < SAMPLES_PER_BAND; i++)
161 285660 *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1], 9, 2);
162 7935 break;
163 720 case 8: case 9: case 10: case 11: case 12:
164 case 13: case 14: case 15: case 16: case 17:
165 720 t = (1 << (idx - 2)) - 1;
166
2/2
✓ Branch 0 taken 25920 times.
✓ Branch 1 taken 720 times.
26640 for(i = 0; i < SAMPLES_PER_BAND; i++)
167 25920 *dst++ = get_bits(gb, idx - 1) - t;
168 720 break;
169 10902 default: // case 0 and -2..-17
170 10902 return;
171 }
172 }
173
174 31340 static int get_scale_idx(GetBitContext *gb, int ref)
175 {
176 31340 int t = get_vlc2(gb, dscf_vlc, MPC7_DSCF_BITS, 1);
177
2/2
✓ Branch 0 taken 3784 times.
✓ Branch 1 taken 27556 times.
31340 if (t == 8)
178 3784 return get_bits(gb, 6);
179 27556 return ref + t;
180 }
181
182 456 static int mpc7_decode_frame(AVCodecContext *avctx, AVFrame *frame,
183 int *got_frame_ptr, AVPacket *avpkt)
184 {
185 456 const uint8_t *buf = avpkt->data;
186 int buf_size;
187 456 MPCContext *c = avctx->priv_data;
188 GetBitContext gb;
189 int i, ch;
190 456 int mb = -1;
191 456 Band *bands = c->bands;
192 int off, ret, last_frame, skip;
193 int bits_used, bits_avail;
194
195 456 memset(bands, 0, sizeof(*bands) * (c->maxbands + 1));
196
197 456 buf_size = avpkt->size & ~3;
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
456 if (buf_size <= 0) {
199 ✗ av_log(avctx, AV_LOG_ERROR, "packet size is too small (%i bytes)\n",
200 avpkt->size);
201 ✗ return AVERROR_INVALIDDATA;
202 }
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
456 if (buf_size != avpkt->size) {
204 ✗ av_log(avctx, AV_LOG_WARNING, "packet size is not a multiple of 4. "
205 "extra bytes at the end will be skipped.\n");
206 }
207
208 456 skip = buf[0];
209 456 last_frame = buf[1];
210 456 buf += 4;
211 456 buf_size -= 4;
212
213 /* get output buffer */
214 456 frame->nb_samples = MPC_FRAME_SIZE;
215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 456 times.
456 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
216 ✗ return ret;
217
218 456 av_fast_padded_malloc(&c->bits, &c->buf_size, buf_size);
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
456 if (!c->bits)
220 ✗ return AVERROR(ENOMEM);
221 456 c->bdsp.bswap_buf((uint32_t *) c->bits, (const uint32_t *) buf,
222 buf_size >> 2);
223
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 456 times.
456 if ((ret = init_get_bits8(&gb, c->bits, buf_size)) < 0)
224 ✗ return ret;
225 456 skip_bits_long(&gb, skip);
226
227 /* read subband indexes */
228
2/2
✓ Branch 0 taken 13224 times.
✓ Branch 1 taken 456 times.
13680 for(i = 0; i <= c->maxbands; i++){
229
2/2
✓ Branch 0 taken 26448 times.
✓ Branch 1 taken 13224 times.
39672 for(ch = 0; ch < 2; ch++){
230
2/2
✓ Branch 0 taken 25536 times.
✓ Branch 1 taken 912 times.
26448 int t = i ? get_vlc2(&gb, hdr_vlc, MPC7_HDR_BITS, 1) : 4;
231
2/2
✓ Branch 0 taken 983 times.
✓ Branch 1 taken 25465 times.
26448 if(t == 4) bands[i].res[ch] = get_bits(&gb, 4);
232 25465 else bands[i].res[ch] = bands[i-1].res[ch] + t;
233
2/4
✓ Branch 0 taken 26448 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 26448 times.
26448 if (bands[i].res[ch] < -1 || bands[i].res[ch] > 17) {
234 ✗ av_log(avctx, AV_LOG_ERROR, "subband index invalid\n");
235 ✗ return AVERROR_INVALIDDATA;
236 }
237 }
238
239
4/4
✓ Branch 0 taken 2913 times.
✓ Branch 1 taken 10311 times.
✓ Branch 2 taken 103 times.
✓ Branch 3 taken 2810 times.
13224 if(bands[i].res[0] || bands[i].res[1]){
240 10414 mb = i;
241
1/2
✓ Branch 0 taken 10414 times.
✗ Branch 1 not taken.
10414 if(c->MSS) bands[i].msf = get_bits1(&gb);
242 }
243 }
244 /* get scale indexes coding method */
245
2/2
✓ Branch 0 taken 10414 times.
✓ Branch 1 taken 456 times.
10870 for(i = 0; i <= mb; i++)
246
2/2
✓ Branch 0 taken 20828 times.
✓ Branch 1 taken 10414 times.
31242 for(ch = 0; ch < 2; ch++)
247
2/2
✓ Branch 0 taken 18282 times.
✓ Branch 1 taken 2546 times.
20828 if (bands[i].res[ch])
248 18282 bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc, MPC7_SCFI_BITS, 1);
249 /* get scale indexes */
250
2/2
✓ Branch 0 taken 10414 times.
✓ Branch 1 taken 456 times.
10870 for(i = 0; i <= mb; i++){
251
2/2
✓ Branch 0 taken 20828 times.
✓ Branch 1 taken 10414 times.
31242 for(ch = 0; ch < 2; ch++){
252
2/2
✓ Branch 0 taken 18282 times.
✓ Branch 1 taken 2546 times.
20828 if(bands[i].res[ch]){
253 18282 bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i];
254 18282 bands[i].scf_idx[ch][0] = get_scale_idx(&gb, bands[i].scf_idx[ch][2]);
255
4/5
✓ Branch 0 taken 1558 times.
✓ Branch 1 taken 2381 times.
✓ Branch 2 taken 7561 times.
✓ Branch 3 taken 6782 times.
✗ Branch 4 not taken.
18282 switch(bands[i].scfi[ch]){
256 1558 case 0:
257 1558 bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]);
258 1558 bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]);
259 1558 break;
260 2381 case 1:
261 2381 bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]);
262 2381 bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1];
263 2381 break;
264 7561 case 2:
265 7561 bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
266 7561 bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]);
267 7561 break;
268 6782 case 3:
269 6782 bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
270 6782 break;
271 }
272 18282 c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2];
273 }
274 }
275 }
276 /* get quantizers */
277 456 memset(c->Q, 0, sizeof(c->Q));
278 456 off = 0;
279
2/2
✓ Branch 0 taken 14592 times.
✓ Branch 1 taken 456 times.
15048 for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND)
280
2/2
✓ Branch 0 taken 29184 times.
✓ Branch 1 taken 14592 times.
43776 for(ch = 0; ch < 2; ch++)
281 29184 idx_to_quant(c, &gb, bands[i].res[ch], c->Q[ch] + off);
282
283 456 ff_mpc_dequantize_and_synth(c, mb, (int16_t **)frame->extended_data, 2);
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
456 if(last_frame)
285 ✗ frame->nb_samples = c->lastframelen;
286
287 456 bits_used = get_bits_count(&gb);
288 456 bits_avail = buf_size * 8;
289
3/6
✓ Branch 0 taken 456 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 456 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 456 times.
456 if (!last_frame && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))) {
290 ✗ av_log(avctx, AV_LOG_ERROR, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail);
291 ✗ return AVERROR_INVALIDDATA;
292 }
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
456 if(c->frames_to_skip){
294 ✗ c->frames_to_skip--;
295 ✗ *got_frame_ptr = 0;
296 ✗ return avpkt->size;
297 }
298
299 456 *got_frame_ptr = 1;
300
301 456 return avpkt->size;
302 }
303
304 ✗ static av_cold void mpc7_decode_flush(AVCodecContext *avctx)
305 {
306 ✗ MPCContext *c = avctx->priv_data;
307
308 ✗ memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
309 ✗ c->frames_to_skip = 32;
310 ✗ }
311
312 3 static av_cold int mpc7_decode_close(AVCodecContext *avctx)
313 {
314 3 MPCContext *c = avctx->priv_data;
315 3 av_freep(&c->bits);
316 3 c->buf_size = 0;
317 3 return 0;
318 }
319
320 const FFCodec ff_mpc7_decoder = {
321 .p.name = "mpc7",
322 CODEC_LONG_NAME("Musepack SV7"),
323 .p.type = AVMEDIA_TYPE_AUDIO,
324 .p.id = AV_CODEC_ID_MUSEPACK7,
325 .priv_data_size = sizeof(MPCContext),
326 .init = mpc7_decode_init,
327 .close = mpc7_decode_close,
328 FF_CODEC_DECODE_CB(mpc7_decode_frame),
329 .flush = mpc7_decode_flush,
330 .p.capabilities = AV_CODEC_CAP_DR1,
331 };
332