FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpc7.c
Date: 2024-07-26 21:54:09
Exec Total Coverage
Lines: 147 176 83.5%
Functions: 6 7 85.7%
Branches: 67 87 77.0%

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