FFmpeg coverage


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