/src/ffmpeg/libavcodec/mpc7.c
Line | Count | 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 | | static av_cold void mpc7_init_static(void) |
51 | 1 | { |
52 | 1 | static VLCElem quant_tables[7224]; |
53 | 1 | VLCInitState state = VLC_INIT_STATE(quant_tables); |
54 | 1 | const uint8_t *raw_quant_table = mpc7_quant_vlcs; |
55 | | |
56 | 1 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE, |
57 | 1 | &mpc7_scfi[1], 2, |
58 | 1 | &mpc7_scfi[0], 2, 1, 0, 0); |
59 | 1 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE, |
60 | 1 | &mpc7_dscf[1], 2, |
61 | 1 | &mpc7_dscf[0], 2, 1, -7, 0); |
62 | 1 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE, |
63 | 1 | &mpc7_hdr[1], 2, |
64 | 1 | &mpc7_hdr[0], 2, 1, -5, 0); |
65 | 8 | for (int i = 0; i < MPC7_QUANT_VLC_TABLES; i++) { |
66 | 21 | for (int j = 0; j < 2; j++) { |
67 | 14 | quant_vlc[i][j] = |
68 | 14 | ff_vlc_init_tables_from_lengths(&state, 9, mpc7_quant_vlc_sizes[i], |
69 | 14 | &raw_quant_table[1], 2, |
70 | 14 | &raw_quant_table[0], 2, 1, |
71 | 14 | mpc7_quant_vlc_off[i], 0); |
72 | 14 | raw_quant_table += 2 * mpc7_quant_vlc_sizes[i]; |
73 | 14 | } |
74 | 7 | } |
75 | 1 | ff_mpa_synth_init_fixed(); |
76 | 1 | } |
77 | | |
78 | | static av_cold int mpc7_decode_init(AVCodecContext * avctx) |
79 | 996 | { |
80 | 996 | static AVOnce init_static_once = AV_ONCE_INIT; |
81 | 996 | MPCContext *c = avctx->priv_data; |
82 | 996 | GetBitContext gb; |
83 | 996 | LOCAL_ALIGNED_16(uint8_t, buf, [16]); |
84 | | |
85 | | /* Musepack SV7 is always stereo */ |
86 | 996 | if (avctx->ch_layout.nb_channels != 2) { |
87 | 115 | avpriv_request_sample(avctx, "%d channels", avctx->ch_layout.nb_channels); |
88 | 115 | return AVERROR_PATCHWELCOME; |
89 | 115 | } |
90 | | |
91 | 881 | if(avctx->extradata_size < 16){ |
92 | 8 | av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size); |
93 | 8 | return AVERROR_INVALIDDATA; |
94 | 8 | } |
95 | 873 | memset(c->oldDSCF, 0, sizeof(c->oldDSCF)); |
96 | 873 | av_lfg_init(&c->rnd, 0xDEADBEEF); |
97 | 873 | ff_bswapdsp_init(&c->bdsp); |
98 | 873 | ff_mpadsp_init(&c->mpadsp); |
99 | 873 | c->bdsp.bswap_buf((uint32_t *) buf, (const uint32_t *) avctx->extradata, 4); |
100 | 873 | init_get_bits(&gb, buf, 128); |
101 | | |
102 | 873 | c->IS = get_bits1(&gb); |
103 | 873 | c->MSS = get_bits1(&gb); |
104 | 873 | c->maxbands = get_bits(&gb, 6); |
105 | 873 | if(c->maxbands >= BANDS){ |
106 | 2 | av_log(avctx, AV_LOG_ERROR, "Too many bands: %i\n", c->maxbands); |
107 | 2 | return AVERROR_INVALIDDATA; |
108 | 2 | } |
109 | 871 | skip_bits_long(&gb, 88); |
110 | 871 | c->gapless = get_bits1(&gb); |
111 | 871 | c->lastframelen = get_bits(&gb, 11); |
112 | 871 | av_log(avctx, AV_LOG_DEBUG, "IS: %d, MSS: %d, TG: %d, LFL: %d, bands: %d\n", |
113 | 871 | c->IS, c->MSS, c->gapless, c->lastframelen, c->maxbands); |
114 | 871 | c->frames_to_skip = 0; |
115 | | |
116 | 871 | avctx->sample_fmt = AV_SAMPLE_FMT_S16P; |
117 | 871 | av_channel_layout_uninit(&avctx->ch_layout); |
118 | 871 | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; |
119 | | |
120 | 871 | ff_thread_once(&init_static_once, mpc7_init_static); |
121 | | |
122 | 871 | return 0; |
123 | 873 | } |
124 | | |
125 | | /** |
126 | | * Fill samples for given subband |
127 | | */ |
128 | | static inline void idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst) |
129 | 9.37M | { |
130 | 9.37M | int i, i1, t; |
131 | 9.37M | switch(idx){ |
132 | 47.3k | case -1: |
133 | 1.75M | for(i = 0; i < SAMPLES_PER_BAND; i++){ |
134 | 1.70M | *dst++ = (av_lfg_get(&c->rnd) & 0x3FC) - 510; |
135 | 1.70M | } |
136 | 47.3k | break; |
137 | 9.17k | case 1: |
138 | 9.17k | i1 = get_bits1(gb); |
139 | 119k | for(i = 0; i < SAMPLES_PER_BAND/3; i++){ |
140 | 110k | t = get_vlc2(gb, quant_vlc[0][i1], 9, 2); |
141 | 110k | *dst++ = mpc7_idx30[t]; |
142 | 110k | *dst++ = mpc7_idx31[t]; |
143 | 110k | *dst++ = mpc7_idx32[t]; |
144 | 110k | } |
145 | 9.17k | break; |
146 | 8.28k | case 2: |
147 | 8.28k | i1 = get_bits1(gb); |
148 | 157k | for(i = 0; i < SAMPLES_PER_BAND/2; i++){ |
149 | 149k | t = get_vlc2(gb, quant_vlc[1][i1], 9, 2); |
150 | 149k | *dst++ = mpc7_idx50[t]; |
151 | 149k | *dst++ = mpc7_idx51[t]; |
152 | 149k | } |
153 | 8.28k | break; |
154 | 44.3k | case 3: case 4: case 5: case 6: case 7: |
155 | 44.3k | i1 = get_bits1(gb); |
156 | 1.64M | for(i = 0; i < SAMPLES_PER_BAND; i++) |
157 | 1.59M | *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1], 9, 2); |
158 | 44.3k | break; |
159 | 36.8k | case 8: case 9: case 10: case 11: case 12: |
160 | 55.2k | case 13: case 14: case 15: case 16: case 17: |
161 | 55.2k | t = (1 << (idx - 2)) - 1; |
162 | 2.04M | for(i = 0; i < SAMPLES_PER_BAND; i++) |
163 | 1.98M | *dst++ = get_bits(gb, idx - 1) - t; |
164 | 55.2k | break; |
165 | 9.20M | default: // case 0 and -2..-17 |
166 | 9.20M | return; |
167 | 9.37M | } |
168 | 9.37M | } |
169 | | |
170 | | static int get_scale_idx(GetBitContext *gb, int ref) |
171 | 258k | { |
172 | 258k | int t = get_vlc2(gb, dscf_vlc, MPC7_DSCF_BITS, 1); |
173 | 258k | if (t == 8) |
174 | 7.73k | return get_bits(gb, 6); |
175 | 250k | return ref + t; |
176 | 258k | } |
177 | | |
178 | | static int mpc7_decode_frame(AVCodecContext *avctx, AVFrame *frame, |
179 | | int *got_frame_ptr, AVPacket *avpkt) |
180 | 257k | { |
181 | 257k | const uint8_t *buf = avpkt->data; |
182 | 257k | int buf_size; |
183 | 257k | MPCContext *c = avctx->priv_data; |
184 | 257k | GetBitContext gb; |
185 | 257k | int i, ch; |
186 | 257k | int mb = -1; |
187 | 257k | Band *bands = c->bands; |
188 | 257k | int off, ret, last_frame, skip; |
189 | 257k | int bits_used, bits_avail; |
190 | | |
191 | 257k | memset(bands, 0, sizeof(*bands) * (c->maxbands + 1)); |
192 | | |
193 | 257k | buf_size = avpkt->size & ~3; |
194 | 257k | if (buf_size <= 0) { |
195 | 96.2k | av_log(avctx, AV_LOG_ERROR, "packet size is too small (%i bytes)\n", |
196 | 96.2k | avpkt->size); |
197 | 96.2k | return AVERROR_INVALIDDATA; |
198 | 96.2k | } |
199 | 160k | if (buf_size != avpkt->size) { |
200 | 25.4k | av_log(avctx, AV_LOG_WARNING, "packet size is not a multiple of 4. " |
201 | 25.4k | "extra bytes at the end will be skipped.\n"); |
202 | 25.4k | } |
203 | | |
204 | 160k | skip = buf[0]; |
205 | 160k | last_frame = buf[1]; |
206 | 160k | buf += 4; |
207 | 160k | buf_size -= 4; |
208 | | |
209 | | /* get output buffer */ |
210 | 160k | frame->nb_samples = MPC_FRAME_SIZE; |
211 | 160k | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
212 | 0 | return ret; |
213 | | |
214 | 160k | av_fast_padded_malloc(&c->bits, &c->buf_size, buf_size); |
215 | 160k | if (!c->bits) |
216 | 0 | return AVERROR(ENOMEM); |
217 | 160k | c->bdsp.bswap_buf((uint32_t *) c->bits, (const uint32_t *) buf, |
218 | 160k | buf_size >> 2); |
219 | 160k | if ((ret = init_get_bits8(&gb, c->bits, buf_size)) < 0) |
220 | 0 | return ret; |
221 | 160k | skip_bits_long(&gb, skip); |
222 | | |
223 | | /* read subband indexes */ |
224 | 418k | for(i = 0; i <= c->maxbands; i++){ |
225 | 787k | for(ch = 0; ch < 2; ch++){ |
226 | 530k | int t = i ? get_vlc2(&gb, hdr_vlc, MPC7_HDR_BITS, 1) : 4; |
227 | 530k | if(t == 4) bands[i].res[ch] = get_bits(&gb, 4); |
228 | 208k | else bands[i].res[ch] = bands[i-1].res[ch] + t; |
229 | 530k | if (bands[i].res[ch] < -1 || bands[i].res[ch] > 17) { |
230 | 14.4k | av_log(avctx, AV_LOG_ERROR, "subband index invalid\n"); |
231 | 14.4k | return AVERROR_INVALIDDATA; |
232 | 14.4k | } |
233 | 530k | } |
234 | | |
235 | 257k | if(bands[i].res[0] || bands[i].res[1]){ |
236 | 111k | mb = i; |
237 | 111k | if(c->MSS) bands[i].msf = get_bits1(&gb); |
238 | 111k | } |
239 | 257k | } |
240 | | /* get scale indexes coding method */ |
241 | 253k | for(i = 0; i <= mb; i++) |
242 | 322k | for(ch = 0; ch < 2; ch++) |
243 | 214k | if (bands[i].res[ch]) |
244 | 164k | bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc, MPC7_SCFI_BITS, 1); |
245 | | /* get scale indexes */ |
246 | 253k | for(i = 0; i <= mb; i++){ |
247 | 322k | for(ch = 0; ch < 2; ch++){ |
248 | 214k | if(bands[i].res[ch]){ |
249 | 164k | bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i]; |
250 | 164k | bands[i].scf_idx[ch][0] = get_scale_idx(&gb, bands[i].scf_idx[ch][2]); |
251 | 164k | switch(bands[i].scfi[ch]){ |
252 | 13.4k | case 0: |
253 | 13.4k | bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]); |
254 | 13.4k | bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]); |
255 | 13.4k | break; |
256 | 53.3k | case 1: |
257 | 53.3k | bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]); |
258 | 53.3k | bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1]; |
259 | 53.3k | break; |
260 | 13.8k | case 2: |
261 | 13.8k | bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0]; |
262 | 13.8k | bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]); |
263 | 13.8k | break; |
264 | 83.7k | case 3: |
265 | 83.7k | bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0]; |
266 | 83.7k | break; |
267 | 164k | } |
268 | 164k | c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2]; |
269 | 164k | } |
270 | 214k | } |
271 | 107k | } |
272 | | /* get quantizers */ |
273 | 146k | memset(c->Q, 0, sizeof(c->Q)); |
274 | 146k | off = 0; |
275 | 4.83M | for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND) |
276 | 14.0M | for(ch = 0; ch < 2; ch++) |
277 | 9.37M | idx_to_quant(c, &gb, bands[i].res[ch], c->Q[ch] + off); |
278 | | |
279 | 146k | ff_mpc_dequantize_and_synth(c, mb, (int16_t **)frame->extended_data, 2); |
280 | 146k | if(last_frame) |
281 | 144k | frame->nb_samples = c->lastframelen; |
282 | | |
283 | 146k | bits_used = get_bits_count(&gb); |
284 | 146k | bits_avail = buf_size * 8; |
285 | 146k | if (!last_frame && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))) { |
286 | 2.16k | av_log(avctx, AV_LOG_ERROR, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail); |
287 | 2.16k | return AVERROR_INVALIDDATA; |
288 | 2.16k | } |
289 | 144k | if(c->frames_to_skip){ |
290 | 14.2k | c->frames_to_skip--; |
291 | 14.2k | *got_frame_ptr = 0; |
292 | 14.2k | return avpkt->size; |
293 | 14.2k | } |
294 | | |
295 | 129k | *got_frame_ptr = 1; |
296 | | |
297 | 129k | return avpkt->size; |
298 | 144k | } |
299 | | |
300 | | static av_cold void mpc7_decode_flush(AVCodecContext *avctx) |
301 | 32.4k | { |
302 | 32.4k | MPCContext *c = avctx->priv_data; |
303 | | |
304 | 32.4k | memset(c->oldDSCF, 0, sizeof(c->oldDSCF)); |
305 | 32.4k | c->frames_to_skip = 32; |
306 | 32.4k | } |
307 | | |
308 | | static av_cold int mpc7_decode_close(AVCodecContext *avctx) |
309 | 871 | { |
310 | 871 | MPCContext *c = avctx->priv_data; |
311 | 871 | av_freep(&c->bits); |
312 | 871 | c->buf_size = 0; |
313 | 871 | return 0; |
314 | 871 | } |
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 | | }; |