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 | 3 | av_log(avctx, AV_LOG_DEBUG, "IS: %d, MSS: %d, TG: %d, LFL: %d, bands: %d\n", | |
113 | c->IS, c->MSS, c->gapless, c->lastframelen, c->maxbands); | ||
114 | 3 | c->frames_to_skip = 0; | |
115 | |||
116 | 3 | avctx->sample_fmt = AV_SAMPLE_FMT_S16P; | |
117 | 3 | av_channel_layout_uninit(&avctx->ch_layout); | |
118 | 3 | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; | |
119 | |||
120 | 3 | ff_thread_once(&init_static_once, mpc7_init_static); | |
121 | |||
122 | 3 | return 0; | |
123 | } | ||
124 | |||
125 | /** | ||
126 | * Fill samples for given subband | ||
127 | */ | ||
128 | 29184 | static inline void idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst) | |
129 | { | ||
130 | int i, i1, t; | ||
131 |
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){ |
132 | ✗ | case -1: | |
133 | ✗ | for(i = 0; i < SAMPLES_PER_BAND; i++){ | |
134 | ✗ | *dst++ = (av_lfg_get(&c->rnd) & 0x3FC) - 510; | |
135 | } | ||
136 | ✗ | break; | |
137 | 3499 | case 1: | |
138 | 3499 | i1 = get_bits1(gb); | |
139 |
2/2✓ Branch 0 taken 41988 times.
✓ Branch 1 taken 3499 times.
|
45487 | for(i = 0; i < SAMPLES_PER_BAND/3; i++){ |
140 | 41988 | t = get_vlc2(gb, quant_vlc[0][i1], 9, 2); | |
141 | 41988 | *dst++ = mpc7_idx30[t]; | |
142 | 41988 | *dst++ = mpc7_idx31[t]; | |
143 | 41988 | *dst++ = mpc7_idx32[t]; | |
144 | } | ||
145 | 3499 | break; | |
146 | 6128 | case 2: | |
147 | 6128 | i1 = get_bits1(gb); | |
148 |
2/2✓ Branch 0 taken 110304 times.
✓ Branch 1 taken 6128 times.
|
116432 | for(i = 0; i < SAMPLES_PER_BAND/2; i++){ |
149 | 110304 | t = get_vlc2(gb, quant_vlc[1][i1], 9, 2); | |
150 | 110304 | *dst++ = mpc7_idx50[t]; | |
151 | 110304 | *dst++ = mpc7_idx51[t]; | |
152 | } | ||
153 | 6128 | break; | |
154 | 7935 | case 3: case 4: case 5: case 6: case 7: | |
155 | 7935 | i1 = get_bits1(gb); | |
156 |
2/2✓ Branch 0 taken 285660 times.
✓ Branch 1 taken 7935 times.
|
293595 | for(i = 0; i < SAMPLES_PER_BAND; i++) |
157 | 285660 | *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1], 9, 2); | |
158 | 7935 | break; | |
159 | 720 | case 8: case 9: case 10: case 11: case 12: | |
160 | case 13: case 14: case 15: case 16: case 17: | ||
161 | 720 | t = (1 << (idx - 2)) - 1; | |
162 |
2/2✓ Branch 0 taken 25920 times.
✓ Branch 1 taken 720 times.
|
26640 | for(i = 0; i < SAMPLES_PER_BAND; i++) |
163 | 25920 | *dst++ = get_bits(gb, idx - 1) - t; | |
164 | 720 | break; | |
165 | 10902 | default: // case 0 and -2..-17 | |
166 | 10902 | return; | |
167 | } | ||
168 | } | ||
169 | |||
170 | 31340 | static int get_scale_idx(GetBitContext *gb, int ref) | |
171 | { | ||
172 | 31340 | int t = get_vlc2(gb, dscf_vlc, MPC7_DSCF_BITS, 1); | |
173 |
2/2✓ Branch 0 taken 3784 times.
✓ Branch 1 taken 27556 times.
|
31340 | if (t == 8) |
174 | 3784 | return get_bits(gb, 6); | |
175 | 27556 | return ref + t; | |
176 | } | ||
177 | |||
178 | 456 | static int mpc7_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
179 | int *got_frame_ptr, AVPacket *avpkt) | ||
180 | { | ||
181 | 456 | const uint8_t *buf = avpkt->data; | |
182 | int buf_size; | ||
183 | 456 | MPCContext *c = avctx->priv_data; | |
184 | GetBitContext gb; | ||
185 | int i, ch; | ||
186 | 456 | int mb = -1; | |
187 | 456 | Band *bands = c->bands; | |
188 | int off, ret, last_frame, skip; | ||
189 | int bits_used, bits_avail; | ||
190 | |||
191 | 456 | memset(bands, 0, sizeof(*bands) * (c->maxbands + 1)); | |
192 | |||
193 | 456 | buf_size = avpkt->size & ~3; | |
194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
|
456 | if (buf_size <= 0) { |
195 | ✗ | av_log(avctx, AV_LOG_ERROR, "packet size is too small (%i bytes)\n", | |
196 | avpkt->size); | ||
197 | ✗ | return AVERROR_INVALIDDATA; | |
198 | } | ||
199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
|
456 | if (buf_size != avpkt->size) { |
200 | ✗ | av_log(avctx, AV_LOG_WARNING, "packet size is not a multiple of 4. " | |
201 | "extra bytes at the end will be skipped.\n"); | ||
202 | } | ||
203 | |||
204 | 456 | skip = buf[0]; | |
205 | 456 | last_frame = buf[1]; | |
206 | 456 | buf += 4; | |
207 | 456 | buf_size -= 4; | |
208 | |||
209 | /* get output buffer */ | ||
210 | 456 | frame->nb_samples = MPC_FRAME_SIZE; | |
211 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 456 times.
|
456 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
212 | ✗ | return ret; | |
213 | |||
214 | 456 | av_fast_padded_malloc(&c->bits, &c->buf_size, buf_size); | |
215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
|
456 | if (!c->bits) |
216 | ✗ | return AVERROR(ENOMEM); | |
217 | 456 | c->bdsp.bswap_buf((uint32_t *) c->bits, (const uint32_t *) buf, | |
218 | buf_size >> 2); | ||
219 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 456 times.
|
456 | if ((ret = init_get_bits8(&gb, c->bits, buf_size)) < 0) |
220 | ✗ | return ret; | |
221 | 456 | skip_bits_long(&gb, skip); | |
222 | |||
223 | /* read subband indexes */ | ||
224 |
2/2✓ Branch 0 taken 13224 times.
✓ Branch 1 taken 456 times.
|
13680 | for(i = 0; i <= c->maxbands; i++){ |
225 |
2/2✓ Branch 0 taken 26448 times.
✓ Branch 1 taken 13224 times.
|
39672 | for(ch = 0; ch < 2; ch++){ |
226 |
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; |
227 |
2/2✓ Branch 0 taken 983 times.
✓ Branch 1 taken 25465 times.
|
26448 | if(t == 4) bands[i].res[ch] = get_bits(&gb, 4); |
228 | 25465 | else bands[i].res[ch] = bands[i-1].res[ch] + t; | |
229 |
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) { |
230 | ✗ | av_log(avctx, AV_LOG_ERROR, "subband index invalid\n"); | |
231 | ✗ | return AVERROR_INVALIDDATA; | |
232 | } | ||
233 | } | ||
234 | |||
235 |
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]){ |
236 | 10414 | mb = i; | |
237 |
1/2✓ Branch 0 taken 10414 times.
✗ Branch 1 not taken.
|
10414 | if(c->MSS) bands[i].msf = get_bits1(&gb); |
238 | } | ||
239 | } | ||
240 | /* get scale indexes coding method */ | ||
241 |
2/2✓ Branch 0 taken 10414 times.
✓ Branch 1 taken 456 times.
|
10870 | for(i = 0; i <= mb; i++) |
242 |
2/2✓ Branch 0 taken 20828 times.
✓ Branch 1 taken 10414 times.
|
31242 | for(ch = 0; ch < 2; ch++) |
243 |
2/2✓ Branch 0 taken 18282 times.
✓ Branch 1 taken 2546 times.
|
20828 | if (bands[i].res[ch]) |
244 | 18282 | bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc, MPC7_SCFI_BITS, 1); | |
245 | /* get scale indexes */ | ||
246 |
2/2✓ Branch 0 taken 10414 times.
✓ Branch 1 taken 456 times.
|
10870 | for(i = 0; i <= mb; i++){ |
247 |
2/2✓ Branch 0 taken 20828 times.
✓ Branch 1 taken 10414 times.
|
31242 | for(ch = 0; ch < 2; ch++){ |
248 |
2/2✓ Branch 0 taken 18282 times.
✓ Branch 1 taken 2546 times.
|
20828 | if(bands[i].res[ch]){ |
249 | 18282 | bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i]; | |
250 | 18282 | bands[i].scf_idx[ch][0] = get_scale_idx(&gb, bands[i].scf_idx[ch][2]); | |
251 |
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]){ |
252 | 1558 | case 0: | |
253 | 1558 | bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]); | |
254 | 1558 | bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]); | |
255 | 1558 | break; | |
256 | 2381 | case 1: | |
257 | 2381 | bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]); | |
258 | 2381 | bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1]; | |
259 | 2381 | break; | |
260 | 7561 | case 2: | |
261 | 7561 | bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0]; | |
262 | 7561 | bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]); | |
263 | 7561 | break; | |
264 | 6782 | case 3: | |
265 | 6782 | bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0]; | |
266 | 6782 | break; | |
267 | } | ||
268 | 18282 | c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2]; | |
269 | } | ||
270 | } | ||
271 | } | ||
272 | /* get quantizers */ | ||
273 | 456 | memset(c->Q, 0, sizeof(c->Q)); | |
274 | 456 | off = 0; | |
275 |
2/2✓ Branch 0 taken 14592 times.
✓ Branch 1 taken 456 times.
|
15048 | for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND) |
276 |
2/2✓ Branch 0 taken 29184 times.
✓ Branch 1 taken 14592 times.
|
43776 | for(ch = 0; ch < 2; ch++) |
277 | 29184 | idx_to_quant(c, &gb, bands[i].res[ch], c->Q[ch] + off); | |
278 | |||
279 | 456 | ff_mpc_dequantize_and_synth(c, mb, (int16_t **)frame->extended_data, 2); | |
280 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
|
456 | if(last_frame) |
281 | ✗ | frame->nb_samples = c->lastframelen; | |
282 | |||
283 | 456 | bits_used = get_bits_count(&gb); | |
284 | 456 | bits_avail = buf_size * 8; | |
285 |
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))) { |
286 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail); | |
287 | ✗ | return AVERROR_INVALIDDATA; | |
288 | } | ||
289 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
|
456 | if(c->frames_to_skip){ |
290 | ✗ | c->frames_to_skip--; | |
291 | ✗ | *got_frame_ptr = 0; | |
292 | ✗ | return avpkt->size; | |
293 | } | ||
294 | |||
295 | 456 | *got_frame_ptr = 1; | |
296 | |||
297 | 456 | return avpkt->size; | |
298 | } | ||
299 | |||
300 | ✗ | static av_cold void mpc7_decode_flush(AVCodecContext *avctx) | |
301 | { | ||
302 | ✗ | MPCContext *c = avctx->priv_data; | |
303 | |||
304 | ✗ | memset(c->oldDSCF, 0, sizeof(c->oldDSCF)); | |
305 | ✗ | c->frames_to_skip = 32; | |
306 | ✗ | } | |
307 | |||
308 | 3 | static av_cold int mpc7_decode_close(AVCodecContext *avctx) | |
309 | { | ||
310 | 3 | MPCContext *c = avctx->priv_data; | |
311 | 3 | av_freep(&c->bits); | |
312 | 3 | c->buf_size = 0; | |
313 | 3 | return 0; | |
314 | } | ||
315 | |||
316 | const FFCodec ff_mpc7_decoder = { | ||
317 | .p.name = "mpc7", | ||
318 | CODEC_LONG_NAME("Musepack SV7"), | ||
319 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
320 | .p.id = AV_CODEC_ID_MUSEPACK7, | ||
321 | .priv_data_size = sizeof(MPCContext), | ||
322 | .init = mpc7_decode_init, | ||
323 | .close = mpc7_decode_close, | ||
324 | FF_CODEC_DECODE_CB(mpc7_decode_frame), | ||
325 | .flush = mpc7_decode_flush, | ||
326 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
327 | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16P), | ||
328 | }; | ||
329 |