/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 | 416k | { |
46 | 416k | int len = mpc8_cnk_len[k-1][n-1] - 1; |
47 | 416k | int code = len ? get_bits_long(gb, len) : 0; |
48 | | |
49 | 416k | if (code >= mpc8_cnk_lost[k-1][n-1]) |
50 | 145k | code = ((code << 1) | get_bits1(gb)) - mpc8_cnk_lost[k-1][n-1]; |
51 | | |
52 | 416k | return code; |
53 | 416k | } |
54 | | |
55 | | static inline int mpc8_dec_enum(GetBitContext *gb, int k, int n) |
56 | 285k | { |
57 | 285k | int bits = 0; |
58 | 285k | const uint32_t * C = mpc8_cnk[k-1]; |
59 | 285k | int code = mpc8_dec_base(gb, k, n); |
60 | | |
61 | 4.19M | do { |
62 | 4.19M | n--; |
63 | 4.19M | if (code >= C[n]) { |
64 | 1.15M | bits |= 1U << n; |
65 | 1.15M | code -= C[n]; |
66 | 1.15M | C -= 32; |
67 | 1.15M | k--; |
68 | 1.15M | } |
69 | 4.19M | } while(k > 0); |
70 | | |
71 | 285k | return bits; |
72 | 285k | } |
73 | | |
74 | | static inline int mpc8_get_mod_golomb(GetBitContext *gb, int m) |
75 | 216k | { |
76 | 216k | if(mpc8_cnk_len[0][m] < 1) return 0; |
77 | 131k | return mpc8_dec_base(gb, 1, m+1); |
78 | 216k | } |
79 | | |
80 | | static int mpc8_get_mask(GetBitContext *gb, int size, int t) |
81 | 416k | { |
82 | 416k | int mask = 0; |
83 | | |
84 | 416k | if(t && t != size) |
85 | 285k | mask = mpc8_dec_enum(gb, FFMIN(t, size - t), size); |
86 | 416k | if((t << 1) > size) mask = ~mask; |
87 | | |
88 | 416k | return mask; |
89 | 416k | } |
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.13k | { |
143 | 1.13k | static AVOnce init_static_once = AV_ONCE_INIT; |
144 | 1.13k | MPCContext *c = avctx->priv_data; |
145 | 1.13k | GetBitContext gb; |
146 | 1.13k | int channels; |
147 | | |
148 | 1.13k | if(avctx->extradata_size < 2){ |
149 | 174 | av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size); |
150 | 174 | return -1; |
151 | 174 | } |
152 | 965 | memset(c->oldDSCF, 0, sizeof(c->oldDSCF)); |
153 | 965 | av_lfg_init(&c->rnd, 0xDEADBEEF); |
154 | 965 | ff_mpadsp_init(&c->mpadsp); |
155 | | |
156 | 965 | init_get_bits(&gb, avctx->extradata, 16); |
157 | | |
158 | 965 | uint8_t sample_rate_idx = get_bits(&gb, 3); |
159 | 965 | static const int sample_rates[] = { 44100, 48000, 37800, 32000 }; |
160 | 965 | if (sample_rate_idx >= FF_ARRAY_ELEMS(sample_rates)) { |
161 | 5 | av_log(avctx, AV_LOG_ERROR, "invalid sample rate index (%u)\n", sample_rate_idx); |
162 | 5 | return AVERROR_INVALIDDATA; |
163 | 5 | } |
164 | 960 | avctx->sample_rate = sample_rates[sample_rate_idx]; |
165 | 960 | c->maxbands = get_bits(&gb, 5) + 1; |
166 | 960 | 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 | 959 | channels = get_bits(&gb, 4) + 1; |
171 | 959 | if (channels > 2) { |
172 | 7 | avpriv_request_sample(avctx, "Multichannel MPC SV8"); |
173 | 7 | return AVERROR_PATCHWELCOME; |
174 | 7 | } |
175 | 952 | c->MSS = get_bits1(&gb); |
176 | 952 | c->frames = 1 << (get_bits(&gb, 3) * 2); |
177 | | |
178 | 952 | avctx->sample_fmt = AV_SAMPLE_FMT_S16P; |
179 | 952 | av_channel_layout_uninit(&avctx->ch_layout); |
180 | 952 | av_channel_layout_default(&avctx->ch_layout, channels); |
181 | | |
182 | 952 | ff_thread_once(&init_static_once, mpc8_init_static); |
183 | | |
184 | 952 | return 0; |
185 | 959 | } |
186 | | |
187 | | static int mpc8_decode_frame(AVCodecContext *avctx, AVFrame *frame, |
188 | | int *got_frame_ptr, AVPacket *avpkt) |
189 | 470k | { |
190 | 470k | const uint8_t *buf = avpkt->data; |
191 | 470k | int buf_size = avpkt->size; |
192 | 470k | MPCContext *c = avctx->priv_data; |
193 | 470k | GetBitContext gb2, *gb = &gb2; |
194 | 470k | int i, j, k, ch, cnt, res, t; |
195 | 470k | Band *bands = c->bands; |
196 | 470k | int off; |
197 | 470k | int maxband, keyframe; |
198 | 470k | int last[2]; |
199 | | |
200 | 470k | keyframe = c->cur_frame == 0; |
201 | | |
202 | 470k | if(keyframe){ |
203 | 73.7k | memset(c->Q, 0, sizeof(c->Q)); |
204 | 73.7k | c->last_bits_used = 0; |
205 | 73.7k | } |
206 | 470k | if ((res = init_get_bits8(gb, buf, buf_size)) < 0) |
207 | 0 | return res; |
208 | | |
209 | 470k | skip_bits(gb, c->last_bits_used & 7); |
210 | | |
211 | 470k | if(keyframe) |
212 | 73.7k | maxband = mpc8_get_mod_golomb(gb, c->maxbands + 1); |
213 | 396k | else{ |
214 | 396k | maxband = c->last_max_band + get_vlc2(gb, band_vlc.table, MPC8_BANDS_BITS, 2); |
215 | 396k | if(maxband > 32) maxband -= 33; |
216 | 396k | } |
217 | | |
218 | 470k | if (get_bits_left(gb) < 0) { |
219 | 117k | *got_frame_ptr = 0; |
220 | 117k | return buf_size; |
221 | 117k | } |
222 | | |
223 | 352k | if(maxband > c->maxbands + 1) { |
224 | 2.64k | av_log(avctx, AV_LOG_ERROR, "maxband %d too large\n",maxband); |
225 | 2.64k | return AVERROR_INVALIDDATA; |
226 | 2.64k | } |
227 | 349k | c->last_max_band = maxband; |
228 | | |
229 | | /* read subband indexes */ |
230 | 349k | if(maxband){ |
231 | 174k | last[0] = last[1] = 0; |
232 | 2.00M | for(i = maxband - 1; i >= 0; i--){ |
233 | 5.48M | for(ch = 0; ch < 2; ch++){ |
234 | 3.65M | last[ch] = get_vlc2(gb, res_vlc[last[ch] > 2].table, MPC8_RES_BITS, 2) + last[ch]; |
235 | 3.65M | if(last[ch] > 15) last[ch] -= 17; |
236 | 3.65M | bands[i].res[ch] = last[ch]; |
237 | 3.65M | } |
238 | 1.82M | } |
239 | 174k | if(c->MSS){ |
240 | 142k | int mask; |
241 | | |
242 | 142k | cnt = 0; |
243 | 1.88M | for(i = 0; i < maxband; i++) |
244 | 1.73M | if(bands[i].res[0] || bands[i].res[1]) |
245 | 849k | cnt++; |
246 | 142k | t = mpc8_get_mod_golomb(gb, cnt); |
247 | 142k | mask = mpc8_get_mask(gb, cnt, t); |
248 | 1.88M | for(i = maxband - 1; i >= 0; i--) |
249 | 1.73M | if(bands[i].res[0] || bands[i].res[1]){ |
250 | 849k | bands[i].msf = mask & 1; |
251 | 849k | mask >>= 1; |
252 | 849k | } |
253 | 142k | } |
254 | 174k | } |
255 | 4.02M | for(i = maxband; i < c->maxbands; i++) |
256 | 3.67M | bands[i].res[0] = bands[i].res[1] = 0; |
257 | | |
258 | 349k | if(keyframe){ |
259 | 2.43M | for(i = 0; i < 32; i++) |
260 | 2.35M | c->oldDSCF[0][i] = c->oldDSCF[1][i] = 1; |
261 | 73.7k | } |
262 | | |
263 | 2.17M | for(i = 0; i < maxband; i++){ |
264 | 1.82M | if(bands[i].res[0] || bands[i].res[1]){ |
265 | 905k | cnt = !!bands[i].res[0] + !!bands[i].res[1] - 1; |
266 | 905k | if(cnt >= 0){ |
267 | 905k | t = get_vlc2(gb, scfi_vlc[cnt].table, scfi_vlc[cnt].bits, 1); |
268 | 905k | if(bands[i].res[0]) bands[i].scfi[0] = t >> (2 * cnt); |
269 | 905k | if(bands[i].res[1]) bands[i].scfi[1] = t & 3; |
270 | 905k | } |
271 | 905k | } |
272 | 1.82M | } |
273 | | |
274 | 2.17M | for(i = 0; i < maxband; i++){ |
275 | 5.48M | for(ch = 0; ch < 2; ch++){ |
276 | 3.65M | if(!bands[i].res[ch]) continue; |
277 | | |
278 | 1.68M | if(c->oldDSCF[ch][i]){ |
279 | 604k | bands[i].scf_idx[ch][0] = get_bits(gb, 7) - 6; |
280 | 604k | c->oldDSCF[ch][i] = 0; |
281 | 1.07M | }else{ |
282 | 1.07M | t = get_vlc2(gb, dscf_vlc[1].table, MPC8_DSCF1_BITS, 2); |
283 | 1.07M | if(t == 64) |
284 | 576 | t += get_bits(gb, 6); |
285 | 1.07M | bands[i].scf_idx[ch][0] = ((bands[i].scf_idx[ch][2] + t - 25) & 0x7F) - 6; |
286 | 1.07M | } |
287 | 5.04M | for(j = 0; j < 2; j++){ |
288 | 3.36M | if((bands[i].scfi[ch] << j) & 2) |
289 | 1.49M | bands[i].scf_idx[ch][j + 1] = bands[i].scf_idx[ch][j]; |
290 | 1.86M | else{ |
291 | 1.86M | t = get_vlc2(gb, dscf_vlc[0].table, MPC8_DSCF0_BITS, 2); |
292 | 1.86M | if(t == 31) |
293 | 1.16k | t = 64 + get_bits(gb, 6); |
294 | 1.86M | bands[i].scf_idx[ch][j + 1] = ((bands[i].scf_idx[ch][j] + t - 25) & 0x7F) - 6; |
295 | 1.86M | } |
296 | 3.36M | } |
297 | 1.68M | } |
298 | 1.82M | } |
299 | | |
300 | 2.17M | for(i = 0, off = 0; i < maxband; i++, off += SAMPLES_PER_BAND){ |
301 | 5.48M | for(ch = 0; ch < 2; ch++){ |
302 | 3.65M | res = bands[i].res[ch]; |
303 | 3.65M | switch(res){ |
304 | 69.4k | case -1: |
305 | 2.56M | for(j = 0; j < SAMPLES_PER_BAND; j++) |
306 | 2.49M | c->Q[ch][off + j] = (av_lfg_get(&c->rnd) & 0x3FC) - 510; |
307 | 69.4k | break; |
308 | 1.97M | case 0: |
309 | 1.97M | break; |
310 | 136k | case 1: |
311 | 410k | for(j = 0; j < SAMPLES_PER_BAND; j += SAMPLES_PER_BAND / 2){ |
312 | 273k | cnt = get_vlc2(gb, q1_vlc.table, MPC8_Q1_BITS, 2); |
313 | 273k | t = mpc8_get_mask(gb, 18, cnt); |
314 | 5.19M | for(k = 0; k < SAMPLES_PER_BAND / 2; k++) |
315 | 4.92M | c->Q[ch][off + j + k] = t & (1 << (SAMPLES_PER_BAND / 2 - k - 1)) |
316 | 4.92M | ? (get_bits1(gb) << 1) - 1 : 0; |
317 | 273k | } |
318 | 136k | break; |
319 | 238k | case 2: |
320 | 238k | cnt = 6;//2*mpc8_thres[res] |
321 | 3.10M | for(j = 0; j < SAMPLES_PER_BAND; j += 3){ |
322 | 2.86M | t = get_vlc2(gb, q2_vlc[cnt > 3].table, MPC8_Q2_BITS, 2); |
323 | 2.86M | c->Q[ch][off + j + 0] = mpc8_idx50[t]; |
324 | 2.86M | c->Q[ch][off + j + 1] = mpc8_idx51[t]; |
325 | 2.86M | c->Q[ch][off + j + 2] = mpc8_idx52[t]; |
326 | 2.86M | cnt = (cnt >> 1) + mpc8_huffq2[t]; |
327 | 2.86M | } |
328 | 238k | break; |
329 | 151k | case 3: |
330 | 309k | case 4: |
331 | 5.88M | for(j = 0; j < SAMPLES_PER_BAND; j += 2){ |
332 | 5.57M | t = get_vlc2(gb, q3_vlc[res - 3].table, MPC8_Q3_BITS, 2); |
333 | 5.57M | c->Q[ch][off + j + 1] = t >> 4; |
334 | 5.57M | c->Q[ch][off + j + 0] = sign_extend(t, 4); |
335 | 5.57M | } |
336 | 309k | break; |
337 | 117k | case 5: |
338 | 231k | case 6: |
339 | 276k | case 7: |
340 | 319k | case 8: |
341 | 319k | cnt = 2 * mpc8_thres[res]; |
342 | 11.8M | for(j = 0; j < SAMPLES_PER_BAND; j++){ |
343 | 11.4M | const VLC *vlc = &quant_vlc[res - 5][cnt > mpc8_thres[res]]; |
344 | 11.4M | c->Q[ch][off + j] = get_vlc2(gb, vlc->table, vlc->bits, 2); |
345 | 11.4M | cnt = (cnt >> 1) + FFABS(c->Q[ch][off + j]); |
346 | 11.4M | } |
347 | 319k | break; |
348 | 608k | default: |
349 | 22.5M | for(j = 0; j < SAMPLES_PER_BAND; j++){ |
350 | 21.9M | c->Q[ch][off + j] = get_vlc2(gb, q9up_vlc.table, MPC8_Q9UP_BITS, 2); |
351 | 21.9M | if(res != 9){ |
352 | 20.4M | c->Q[ch][off + j] <<= res - 9; |
353 | 20.4M | c->Q[ch][off + j] |= get_bits(gb, res - 9); |
354 | 20.4M | } |
355 | 21.9M | c->Q[ch][off + j] -= (1 << (res - 2)) - 1; |
356 | 21.9M | } |
357 | 3.65M | } |
358 | 3.65M | } |
359 | 1.82M | } |
360 | | |
361 | 349k | frame->nb_samples = MPC_FRAME_SIZE; |
362 | 349k | if ((res = ff_get_buffer(avctx, frame, 0)) < 0) |
363 | 0 | return res; |
364 | | |
365 | 349k | ff_mpc_dequantize_and_synth(c, maxband - 1, |
366 | 349k | (int16_t **)frame->extended_data, |
367 | 349k | avctx->ch_layout.nb_channels); |
368 | | |
369 | 349k | c->cur_frame++; |
370 | | |
371 | 349k | c->last_bits_used = get_bits_count(gb); |
372 | 349k | if(c->cur_frame >= c->frames) |
373 | 17.7k | c->cur_frame = 0; |
374 | 349k | if (get_bits_left(gb) < 0) { |
375 | 33.2k | av_log(avctx, AV_LOG_ERROR, "Overread %d\n", -get_bits_left(gb)); |
376 | 33.2k | c->last_bits_used = buf_size << 3; |
377 | 316k | } else if (c->cur_frame == 0 && get_bits_left(gb) < 8) {// we have only padding left |
378 | 11.8k | c->last_bits_used = buf_size << 3; |
379 | 11.8k | } |
380 | | |
381 | 349k | *got_frame_ptr = 1; |
382 | | |
383 | 349k | return c->cur_frame ? c->last_bits_used >> 3 : buf_size; |
384 | 349k | } |
385 | | |
386 | | static av_cold void mpc8_decode_flush(AVCodecContext *avctx) |
387 | 68.4k | { |
388 | 68.4k | MPCContext *c = avctx->priv_data; |
389 | 68.4k | c->cur_frame = 0; |
390 | 68.4k | } |
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 | | }; |