Commit | Line | Data |
---|---|---|
4f52c312 MN |
1 | /* |
2 | * FLAC (Free Lossless Audio Codec) decoder | |
3 | * Copyright (c) 2003 Alex Beregszaszi | |
4 | * | |
2912e87a | 5 | * This file is part of Libav. |
b78e7197 | 6 | * |
2912e87a | 7 | * Libav is free software; you can redistribute it and/or |
4f52c312 MN |
8 | * modify it under the terms of the GNU Lesser General Public |
9 | * License as published by the Free Software Foundation; either | |
b78e7197 | 10 | * version 2.1 of the License, or (at your option) any later version. |
4f52c312 | 11 | * |
2912e87a | 12 | * Libav is distributed in the hope that it will be useful, |
4f52c312 MN |
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 | |
2912e87a | 18 | * License along with Libav; if not, write to the Free Software |
5509bffa | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
4f52c312 MN |
20 | */ |
21 | ||
22 | /** | |
ba87f080 | 23 | * @file |
4f52c312 MN |
24 | * FLAC (Free Lossless Audio Codec) decoder |
25 | * @author Alex Beregszaszi | |
ad4cd0c2 | 26 | * @see http://flac.sourceforge.net/ |
9eda2f94 MM |
27 | * |
28 | * This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed | |
29 | * through, starting from the initial 'fLaC' signature; or by passing the | |
30 | * 34-byte streaminfo structure through avctx->extradata[_size] followed | |
31 | * by data starting with the 0xFFF8 marker. | |
4f52c312 | 32 | */ |
115329f1 | 33 | |
ac2570a8 | 34 | #include <limits.h> |
115329f1 | 35 | |
4f52c312 | 36 | #include "avcodec.h" |
5b37e2fc | 37 | #include "internal.h" |
9106a698 | 38 | #include "get_bits.h" |
a8ec12bc | 39 | #include "bytestream.h" |
4f52c312 | 40 | #include "golomb.h" |
9d48410f | 41 | #include "flac.h" |
d4df4e50 | 42 | #include "flacdata.h" |
4a852834 | 43 | #include "flacdsp.h" |
4f52c312 | 44 | |
4f52c312 | 45 | typedef struct FLACContext { |
9d48410f JR |
46 | FLACSTREAMINFO |
47 | ||
64cb3765 JR |
48 | AVCodecContext *avctx; ///< parent AVCodecContext |
49 | GetBitContext gb; ///< GetBitContext initialized to start at the current frame | |
4f52c312 | 50 | |
64cb3765 | 51 | int blocksize; ///< number of samples in the current frame |
7f3a6a05 | 52 | int sample_shift; ///< shift required to make output samples 16-bit or 32-bit |
2a346725 | 53 | int ch_mode; ///< channel decorrelation type in the current frame |
7d030358 | 54 | int got_streaminfo; ///< indicates if the STREAMINFO has been read |
4f52c312 | 55 | |
07d16e2e | 56 | int32_t *decoded[FLAC_MAX_CHANNELS]; ///< decoded samples |
268f8ba1 JR |
57 | uint8_t *decoded_buffer; |
58 | unsigned int decoded_buffer_size; | |
4a852834 MR |
59 | |
60 | FLACDSPContext dsp; | |
4f52c312 MN |
61 | } FLACContext; |
62 | ||
268f8ba1 | 63 | static int allocate_buffers(FLACContext *s); |
59c6178a | 64 | |
87466f81 MR |
65 | static void flac_set_bps(FLACContext *s) |
66 | { | |
784514a4 MR |
67 | enum AVSampleFormat req = s->avctx->request_sample_fmt; |
68 | int need32 = s->bps > 16; | |
69 | int want32 = av_get_bytes_per_sample(req) > 2; | |
70 | int planar = av_sample_fmt_is_planar(req); | |
71 | ||
72 | if (need32 || want32) { | |
73 | if (planar) | |
74 | s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P; | |
75 | else | |
76 | s->avctx->sample_fmt = AV_SAMPLE_FMT_S32; | |
87466f81 | 77 | s->sample_shift = 32 - s->bps; |
87466f81 | 78 | } else { |
784514a4 MR |
79 | if (planar) |
80 | s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P; | |
81 | else | |
82 | s->avctx->sample_fmt = AV_SAMPLE_FMT_S16; | |
87466f81 | 83 | s->sample_shift = 16 - s->bps; |
87466f81 MR |
84 | } |
85 | } | |
86 | ||
1bec121f | 87 | static av_cold int flac_decode_init(AVCodecContext *avctx) |
4f52c312 | 88 | { |
59c6178a JR |
89 | enum FLACExtradataFormat format; |
90 | uint8_t *streaminfo; | |
268f8ba1 | 91 | int ret; |
9eda2f94 MM |
92 | FLACContext *s = avctx->priv_data; |
93 | s->avctx = avctx; | |
94 | ||
59c6178a JR |
95 | /* for now, the raw FLAC header is allowed to be passed to the decoder as |
96 | frame data instead of extradata. */ | |
97 | if (!avctx->extradata) | |
98 | return 0; | |
99 | ||
d9cca9fc | 100 | if (!avpriv_flac_is_extradata_valid(avctx, &format, &streaminfo)) |
0e78ef0f | 101 | return AVERROR_INVALIDDATA; |
59c6178a | 102 | |
26adc8d0 | 103 | /* initialize based on the demuxer-supplied streamdata header */ |
d9cca9fc | 104 | avpriv_flac_parse_streaminfo(avctx, (FLACStreaminfo *)s, streaminfo); |
268f8ba1 JR |
105 | ret = allocate_buffers(s); |
106 | if (ret < 0) | |
107 | return ret; | |
87466f81 | 108 | flac_set_bps(s); |
784514a4 | 109 | ff_flacdsp_init(&s->dsp, avctx->sample_fmt, s->bps); |
7d030358 | 110 | s->got_streaminfo = 1; |
9eda2f94 | 111 | |
4f52c312 MN |
112 | return 0; |
113 | } | |
114 | ||
9482171b | 115 | static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s) |
4f52c312 | 116 | { |
95db6659 | 117 | av_log(avctx, AV_LOG_DEBUG, " Max Blocksize: %d\n", s->max_blocksize); |
4bc07e78 JR |
118 | av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize); |
119 | av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate); | |
120 | av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels); | |
121 | av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps); | |
4f52c312 MN |
122 | } |
123 | ||
268f8ba1 | 124 | static int allocate_buffers(FLACContext *s) |
1bec121f | 125 | { |
268f8ba1 | 126 | int buf_size; |
4f52c312 | 127 | |
268f8ba1 JR |
128 | buf_size = av_samples_get_buffer_size(NULL, s->channels, s->max_blocksize, |
129 | AV_SAMPLE_FMT_S32P, 0); | |
130 | if (buf_size < 0) | |
131 | return buf_size; | |
132 | ||
133 | av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size); | |
134 | if (!s->decoded_buffer) | |
135 | return AVERROR(ENOMEM); | |
136 | ||
137 | return av_samples_fill_arrays((uint8_t **)s->decoded, NULL, | |
138 | s->decoded_buffer, s->channels, | |
139 | s->max_blocksize, AV_SAMPLE_FMT_S32P, 0); | |
ac2570a8 MN |
140 | } |
141 | ||
17c90b9d | 142 | /** |
a8ec12bc JR |
143 | * Parse the STREAMINFO from an inline header. |
144 | * @param s the flac decoding context | |
145 | * @param buf input buffer, starting with the "fLaC" marker | |
146 | * @param buf_size buffer size | |
55a72738 | 147 | * @return non-zero if metadata is invalid |
17c90b9d | 148 | */ |
a8ec12bc | 149 | static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size) |
17c90b9d | 150 | { |
268f8ba1 | 151 | int metadata_type, metadata_size, ret; |
17c90b9d | 152 | |
a8ec12bc JR |
153 | if (buf_size < FLAC_STREAMINFO_SIZE+8) { |
154 | /* need more data */ | |
155 | return 0; | |
156 | } | |
5fdaf312 | 157 | flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size); |
a8ec12bc JR |
158 | if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO || |
159 | metadata_size != FLAC_STREAMINFO_SIZE) { | |
160 | return AVERROR_INVALIDDATA; | |
161 | } | |
d9cca9fc | 162 | avpriv_flac_parse_streaminfo(s->avctx, (FLACStreaminfo *)s, &buf[8]); |
268f8ba1 JR |
163 | ret = allocate_buffers(s); |
164 | if (ret < 0) | |
165 | return ret; | |
4a852834 | 166 | flac_set_bps(s); |
784514a4 | 167 | ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, s->bps); |
a8ec12bc | 168 | s->got_streaminfo = 1; |
17c90b9d | 169 | |
a8ec12bc JR |
170 | return 0; |
171 | } | |
17c90b9d | 172 | |
a8ec12bc JR |
173 | /** |
174 | * Determine the size of an inline header. | |
175 | * @param buf input buffer, starting with the "fLaC" marker | |
176 | * @param buf_size buffer size | |
177 | * @return number of bytes in the header, or 0 if more data is needed | |
178 | */ | |
179 | static int get_metadata_size(const uint8_t *buf, int buf_size) | |
180 | { | |
181 | int metadata_last, metadata_size; | |
182 | const uint8_t *buf_end = buf + buf_size; | |
e0168e3b | 183 | |
a8ec12bc JR |
184 | buf += 4; |
185 | do { | |
4c5e7b27 JR |
186 | if (buf_end - buf < 4) |
187 | return 0; | |
5fdaf312 | 188 | flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size); |
5b63d33d | 189 | buf += 4; |
4c5e7b27 | 190 | if (buf_end - buf < metadata_size) { |
a8ec12bc JR |
191 | /* need more data in order to read the complete header */ |
192 | return 0; | |
6a85fb34 | 193 | } |
a8ec12bc | 194 | buf += metadata_size; |
6a85fb34 | 195 | } while (!metadata_last); |
17c90b9d | 196 | |
a8ec12bc | 197 | return buf_size - (buf_end - buf); |
4f52c312 MN |
198 | } |
199 | ||
44c56a16 | 200 | static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order) |
4f52c312 MN |
201 | { |
202 | int i, tmp, partition, method_type, rice_order; | |
ea98507d | 203 | int rice_bits, rice_esc; |
44c56a16 | 204 | int samples; |
4f52c312 MN |
205 | |
206 | method_type = get_bits(&s->gb, 2); | |
1bec121f | 207 | if (method_type > 1) { |
9f3d3ecf JR |
208 | av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n", |
209 | method_type); | |
0e78ef0f | 210 | return AVERROR_INVALIDDATA; |
ac2570a8 | 211 | } |
115329f1 | 212 | |
4f52c312 MN |
213 | rice_order = get_bits(&s->gb, 4); |
214 | ||
ac2570a8 | 215 | samples= s->blocksize >> rice_order; |
5484dad7 | 216 | if (pred_order > samples) { |
9f3d3ecf JR |
217 | av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n", |
218 | pred_order, samples); | |
0e78ef0f | 219 | return AVERROR_INVALIDDATA; |
5484dad7 | 220 | } |
4f52c312 | 221 | |
ea98507d MR |
222 | rice_bits = 4 + method_type; |
223 | rice_esc = (1 << rice_bits) - 1; | |
224 | ||
44c56a16 | 225 | decoded += pred_order; |
ac2570a8 | 226 | i= pred_order; |
1bec121f | 227 | for (partition = 0; partition < (1 << rice_order); partition++) { |
ea98507d MR |
228 | tmp = get_bits(&s->gb, rice_bits); |
229 | if (tmp == rice_esc) { | |
4f52c312 | 230 | tmp = get_bits(&s->gb, 5); |
44c56a16 MR |
231 | for (; i < samples; i++) |
232 | *decoded++ = get_sbits_long(&s->gb, tmp); | |
1bec121f | 233 | } else { |
44c56a16 MR |
234 | for (; i < samples; i++) { |
235 | *decoded++ = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0); | |
ac2570a8 | 236 | } |
4f52c312 | 237 | } |
ac2570a8 | 238 | i= 0; |
4f52c312 MN |
239 | } |
240 | ||
4f52c312 | 241 | return 0; |
115329f1 | 242 | } |
4f52c312 | 243 | |
44c56a16 MR |
244 | static int decode_subframe_fixed(FLACContext *s, int32_t *decoded, |
245 | int pred_order, int bps) | |
4f52c312 | 246 | { |
08965b22 | 247 | const int blocksize = s->blocksize; |
0e78ef0f | 248 | int a, b, c, d, i, ret; |
115329f1 | 249 | |
4f52c312 | 250 | /* warm up samples */ |
1bec121f | 251 | for (i = 0; i < pred_order; i++) { |
0da301e1 | 252 | decoded[i] = get_sbits_long(&s->gb, bps); |
4f52c312 | 253 | } |
115329f1 | 254 | |
0e78ef0f LB |
255 | if ((ret = decode_residuals(s, decoded, pred_order)) < 0) |
256 | return ret; | |
4f52c312 | 257 | |
1bec121f | 258 | if (pred_order > 0) |
1f4fa6a4 | 259 | a = decoded[pred_order-1]; |
1bec121f | 260 | if (pred_order > 1) |
1f4fa6a4 | 261 | b = a - decoded[pred_order-2]; |
1bec121f | 262 | if (pred_order > 2) |
1f4fa6a4 | 263 | c = b - decoded[pred_order-2] + decoded[pred_order-3]; |
1bec121f | 264 | if (pred_order > 3) |
1f4fa6a4 | 265 | d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4]; |
08965b22 | 266 | |
1bec121f JR |
267 | switch (pred_order) { |
268 | case 0: | |
269 | break; | |
270 | case 1: | |
271 | for (i = pred_order; i < blocksize; i++) | |
272 | decoded[i] = a += decoded[i]; | |
273 | break; | |
274 | case 2: | |
275 | for (i = pred_order; i < blocksize; i++) | |
276 | decoded[i] = a += b += decoded[i]; | |
277 | break; | |
278 | case 3: | |
279 | for (i = pred_order; i < blocksize; i++) | |
280 | decoded[i] = a += b += c += decoded[i]; | |
281 | break; | |
282 | case 4: | |
283 | for (i = pred_order; i < blocksize; i++) | |
284 | decoded[i] = a += b += c += d += decoded[i]; | |
285 | break; | |
286 | default: | |
287 | av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order); | |
0e78ef0f | 288 | return AVERROR_INVALIDDATA; |
4f52c312 | 289 | } |
ac2570a8 | 290 | |
4f52c312 MN |
291 | return 0; |
292 | } | |
293 | ||
44c56a16 | 294 | static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order, |
0da301e1 | 295 | int bps) |
4f52c312 | 296 | { |
0e78ef0f | 297 | int i, ret; |
4f52c312 | 298 | int coeff_prec, qlevel; |
8313e179 | 299 | int coeffs[32]; |
115329f1 | 300 | |
4f52c312 | 301 | /* warm up samples */ |
1bec121f | 302 | for (i = 0; i < pred_order; i++) { |
0da301e1 | 303 | decoded[i] = get_sbits_long(&s->gb, bps); |
4f52c312 | 304 | } |
115329f1 | 305 | |
4f52c312 | 306 | coeff_prec = get_bits(&s->gb, 4) + 1; |
1bec121f | 307 | if (coeff_prec == 16) { |
5305f40b | 308 | av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n"); |
0e78ef0f | 309 | return AVERROR_INVALIDDATA; |
4f52c312 | 310 | } |
ac2570a8 | 311 | qlevel = get_sbits(&s->gb, 5); |
1bec121f | 312 | if (qlevel < 0) { |
9f3d3ecf JR |
313 | av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n", |
314 | qlevel); | |
0e78ef0f | 315 | return AVERROR_INVALIDDATA; |
9d656110 MN |
316 | } |
317 | ||
1bec121f | 318 | for (i = 0; i < pred_order; i++) { |
bf1cf4d5 | 319 | coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec); |
4f52c312 | 320 | } |
115329f1 | 321 | |
0e78ef0f LB |
322 | if ((ret = decode_residuals(s, decoded, pred_order)) < 0) |
323 | return ret; | |
4f52c312 | 324 | |
25accf93 | 325 | s->dsp.lpc(decoded, coeffs, pred_order, qlevel, s->blocksize); |
115329f1 | 326 | |
4f52c312 MN |
327 | return 0; |
328 | } | |
329 | ||
330 | static inline int decode_subframe(FLACContext *s, int channel) | |
331 | { | |
44c56a16 | 332 | int32_t *decoded = s->decoded[channel]; |
4f52c312 | 333 | int type, wasted = 0; |
0da301e1 | 334 | int bps = s->bps; |
0e78ef0f | 335 | int i, tmp, ret; |
115329f1 | 336 | |
1bec121f | 337 | if (channel == 0) { |
2a346725 | 338 | if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE) |
0da301e1 | 339 | bps++; |
1bec121f | 340 | } else { |
2a346725 | 341 | if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE) |
0da301e1 | 342 | bps++; |
ac2570a8 MN |
343 | } |
344 | ||
1bec121f | 345 | if (get_bits1(&s->gb)) { |
1cef211d | 346 | av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n"); |
0e78ef0f | 347 | return AVERROR_INVALIDDATA; |
4f52c312 MN |
348 | } |
349 | type = get_bits(&s->gb, 6); | |
c5706efd | 350 | |
1bec121f | 351 | if (get_bits1(&s->gb)) { |
52e4018b | 352 | int left = get_bits_left(&s->gb); |
4f52c312 | 353 | wasted = 1; |
52e4018b | 354 | if ( left < 0 || |
0da301e1 MR |
355 | (left < bps && !show_bits_long(&s->gb, left)) || |
356 | !show_bits_long(&s->gb, bps)) { | |
52e4018b RB |
357 | av_log(s->avctx, AV_LOG_ERROR, |
358 | "Invalid number of wasted bits > available bits (%d) - left=%d\n", | |
0da301e1 | 359 | bps, left); |
52e4018b RB |
360 | return AVERROR_INVALIDDATA; |
361 | } | |
4f52c312 MN |
362 | while (!get_bits1(&s->gb)) |
363 | wasted++; | |
0da301e1 | 364 | bps -= wasted; |
4f52c312 | 365 | } |
0da301e1 | 366 | if (bps > 32) { |
63d744e2 | 367 | avpriv_report_missing_feature(s->avctx, "Decorrelated bit depth > 32"); |
717addec | 368 | return AVERROR_PATCHWELCOME; |
a3d2379b | 369 | } |
c5706efd | 370 | |
ac2570a8 | 371 | //FIXME use av_log2 for types |
1bec121f | 372 | if (type == 0) { |
0da301e1 | 373 | tmp = get_sbits_long(&s->gb, bps); |
4f52c312 | 374 | for (i = 0; i < s->blocksize; i++) |
44c56a16 | 375 | decoded[i] = tmp; |
1bec121f | 376 | } else if (type == 1) { |
4f52c312 | 377 | for (i = 0; i < s->blocksize; i++) |
44c56a16 | 378 | decoded[i] = get_sbits_long(&s->gb, bps); |
1bec121f | 379 | } else if ((type >= 8) && (type <= 12)) { |
0e78ef0f LB |
380 | if ((ret = decode_subframe_fixed(s, decoded, type & ~0x8, bps)) < 0) |
381 | return ret; | |
1bec121f | 382 | } else if (type >= 32) { |
0e78ef0f LB |
383 | if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0) |
384 | return ret; | |
1bec121f | 385 | } else { |
1cef211d | 386 | av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n"); |
0e78ef0f | 387 | return AVERROR_INVALIDDATA; |
4f52c312 | 388 | } |
115329f1 | 389 | |
1bec121f | 390 | if (wasted) { |
4f52c312 MN |
391 | int i; |
392 | for (i = 0; i < s->blocksize; i++) | |
44c56a16 | 393 | decoded[i] <<= wasted; |
4f52c312 MN |
394 | } |
395 | ||
396 | return 0; | |
397 | } | |
398 | ||
cd98a030 JR |
399 | static int decode_frame(FLACContext *s) |
400 | { | |
90fcac0e | 401 | int i, ret; |
cd98a030 JR |
402 | GetBitContext *gb = &s->gb; |
403 | FLACFrameInfo fi; | |
404 | ||
0e78ef0f | 405 | if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) { |
cd98a030 | 406 | av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n"); |
0e78ef0f | 407 | return ret; |
9d656110 | 408 | } |
115329f1 | 409 | |
90fcac0e JR |
410 | if (s->channels && fi.channels != s->channels && s->got_streaminfo) { |
411 | s->channels = s->avctx->channels = fi.channels; | |
412 | ff_flac_set_channel_layout(s->avctx); | |
413 | ret = allocate_buffers(s); | |
414 | if (ret < 0) | |
415 | return ret; | |
fbc4d9c9 | 416 | } |
2e0559b7 | 417 | s->channels = s->avctx->channels = fi.channels; |
41244e13 | 418 | if (!s->avctx->channel_layout) |
aef51507 | 419 | ff_flac_set_channel_layout(s->avctx); |
cd98a030 | 420 | s->ch_mode = fi.ch_mode; |
fbc4d9c9 | 421 | |
2e0559b7 JR |
422 | if (!s->bps && !fi.bps) { |
423 | av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n"); | |
0e78ef0f | 424 | return AVERROR_INVALIDDATA; |
2e0559b7 JR |
425 | } |
426 | if (!fi.bps) { | |
427 | fi.bps = s->bps; | |
428 | } else if (s->bps && fi.bps != s->bps) { | |
fbc4d9c9 JR |
429 | av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not " |
430 | "supported\n"); | |
0e78ef0f | 431 | return AVERROR_INVALIDDATA; |
fbc4d9c9 | 432 | } |
2e0559b7 | 433 | |
784514a4 MR |
434 | if (!s->bps) { |
435 | s->bps = s->avctx->bits_per_raw_sample = fi.bps; | |
436 | flac_set_bps(s); | |
437 | } | |
fbc4d9c9 | 438 | |
2e0559b7 JR |
439 | if (!s->max_blocksize) |
440 | s->max_blocksize = FLAC_MAX_BLOCKSIZE; | |
cd98a030 JR |
441 | if (fi.blocksize > s->max_blocksize) { |
442 | av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize, | |
fbc4d9c9 | 443 | s->max_blocksize); |
0e78ef0f | 444 | return AVERROR_INVALIDDATA; |
fbc4d9c9 | 445 | } |
cd98a030 | 446 | s->blocksize = fi.blocksize; |
fbc4d9c9 | 447 | |
2e0559b7 JR |
448 | if (!s->samplerate && !fi.samplerate) { |
449 | av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO" | |
450 | " or frame header\n"); | |
0e78ef0f | 451 | return AVERROR_INVALIDDATA; |
2e0559b7 | 452 | } |
99d86863 | 453 | if (fi.samplerate == 0) |
cd98a030 | 454 | fi.samplerate = s->samplerate; |
cd98a030 | 455 | s->samplerate = s->avctx->sample_rate = fi.samplerate; |
4f52c312 | 456 | |
2e0559b7 | 457 | if (!s->got_streaminfo) { |
90fcac0e | 458 | ret = allocate_buffers(s); |
268f8ba1 JR |
459 | if (ret < 0) |
460 | return ret; | |
784514a4 | 461 | ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, s->bps); |
2e0559b7 JR |
462 | s->got_streaminfo = 1; |
463 | dump_headers(s->avctx, (FLACStreaminfo *)s); | |
464 | } | |
465 | ||
9482171b | 466 | // dump_headers(s->avctx, (FLACStreaminfo *)s); |
4f52c312 MN |
467 | |
468 | /* subframes */ | |
1bec121f | 469 | for (i = 0; i < s->channels; i++) { |
0e78ef0f LB |
470 | if ((ret = decode_subframe(s, i)) < 0) |
471 | return ret; | |
4f52c312 | 472 | } |
115329f1 | 473 | |
7e00bd84 | 474 | align_get_bits(gb); |
4f52c312 MN |
475 | |
476 | /* frame footer */ | |
7e00bd84 | 477 | skip_bits(gb, 16); /* data crc */ |
4f52c312 MN |
478 | |
479 | return 0; | |
480 | } | |
481 | ||
0eea2129 JR |
482 | static int flac_decode_frame(AVCodecContext *avctx, void *data, |
483 | int *got_frame_ptr, AVPacket *avpkt) | |
4f52c312 | 484 | { |
b8e9c99e | 485 | AVFrame *frame = data; |
7a00bbad TB |
486 | const uint8_t *buf = avpkt->data; |
487 | int buf_size = avpkt->size; | |
4f52c312 | 488 | FLACContext *s = avctx->priv_data; |
4a852834 | 489 | int bytes_read = 0; |
0eea2129 | 490 | int ret; |
ac66834c | 491 | |
0eea2129 | 492 | *got_frame_ptr = 0; |
4f52c312 | 493 | |
1bec121f | 494 | if (s->max_framesize == 0) { |
2e0559b7 JR |
495 | s->max_framesize = |
496 | ff_flac_get_max_frame_size(s->max_blocksize ? s->max_blocksize : FLAC_MAX_BLOCKSIZE, | |
497 | FLAC_MAX_CHANNELS, 32); | |
ac2570a8 | 498 | } |
4f52c312 | 499 | |
5ef4fa87 | 500 | /* check that there is at least the smallest decodable amount of data. |
8d1e885f JR |
501 | this amount corresponds to the smallest valid FLAC frame possible. |
502 | FF F8 69 02 00 00 9A 00 00 34 46 */ | |
a4151444 | 503 | if (buf_size < FLAC_MIN_FRAME_SIZE) |
60a68493 | 504 | return buf_size; |
5ef4fa87 | 505 | |
55a72738 | 506 | /* check for inline header */ |
a8ec12bc | 507 | if (AV_RB32(buf) == MKBETAG('f','L','a','C')) { |
0e78ef0f | 508 | if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) { |
55a72738 | 509 | av_log(s->avctx, AV_LOG_ERROR, "invalid header\n"); |
0e78ef0f | 510 | return ret; |
55a72738 | 511 | } |
60a68493 | 512 | return get_metadata_size(buf, buf_size); |
13de8a08 | 513 | } |
fd6fd470 JR |
514 | |
515 | /* decode frame */ | |
516 | init_get_bits(&s->gb, buf, buf_size*8); | |
0e78ef0f | 517 | if ((ret = decode_frame(s)) < 0) { |
13de8a08 | 518 | av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n"); |
0e78ef0f | 519 | return ret; |
13de8a08 | 520 | } |
bf1d7e28 | 521 | bytes_read = (get_bits_count(&s->gb)+7)/8; |
ac2570a8 | 522 | |
0eea2129 | 523 | /* get output buffer */ |
b8e9c99e | 524 | frame->nb_samples = s->blocksize; |
759001c5 | 525 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) { |
0eea2129 JR |
526 | av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); |
527 | return ret; | |
02b26d2d | 528 | } |
4a852834 | 529 | |
b8e9c99e | 530 | s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded, s->channels, |
4a852834 | 531 | s->blocksize, s->sample_shift); |
4f52c312 | 532 | |
c5199729 JR |
533 | if (bytes_read > buf_size) { |
534 | av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size); | |
0e78ef0f | 535 | return AVERROR_INVALIDDATA; |
ac2570a8 | 536 | } |
60a68493 MC |
537 | if (bytes_read < buf_size) { |
538 | av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n", | |
539 | buf_size - bytes_read, buf_size); | |
540 | } | |
4f52c312 | 541 | |
b8e9c99e | 542 | *got_frame_ptr = 1; |
0eea2129 | 543 | |
60a68493 | 544 | return bytes_read; |
4f52c312 MN |
545 | } |
546 | ||
98a6fff9 | 547 | static av_cold int flac_decode_close(AVCodecContext *avctx) |
4f52c312 MN |
548 | { |
549 | FLACContext *s = avctx->priv_data; | |
115329f1 | 550 | |
268f8ba1 | 551 | av_freep(&s->decoded_buffer); |
115329f1 | 552 | |
4f52c312 MN |
553 | return 0; |
554 | } | |
555 | ||
d36beb3f | 556 | AVCodec ff_flac_decoder = { |
ec6402b7 | 557 | .name = "flac", |
b2bed932 | 558 | .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"), |
ec6402b7 | 559 | .type = AVMEDIA_TYPE_AUDIO, |
36ef5369 | 560 | .id = AV_CODEC_ID_FLAC, |
ec6402b7 AK |
561 | .priv_data_size = sizeof(FLACContext), |
562 | .init = flac_decode_init, | |
563 | .close = flac_decode_close, | |
564 | .decode = flac_decode_frame, | |
0eea2129 | 565 | .capabilities = CODEC_CAP_DR1, |
784514a4 MR |
566 | .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, |
567 | AV_SAMPLE_FMT_S16P, | |
568 | AV_SAMPLE_FMT_S32, | |
569 | AV_SAMPLE_FMT_S32P, | |
570 | -1 }, | |
4f52c312 | 571 | }; |