FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/flacdec.c
Date: 2022-11-28 23:49:43
Exec Total Coverage
Lines: 221 332 66.6%
Branches: 140 234 59.8%

Line Branch Exec Source
1 /*
2 * FLAC (Free Lossless Audio Codec) decoder
3 * Copyright (c) 2003 Alex Beregszaszi
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 * FLAC (Free Lossless Audio Codec) decoder
25 * @author Alex Beregszaszi
26 * @see http://flac.sourceforge.net/
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.
32 */
33
34 #include <limits.h>
35
36 #include "libavutil/avassert.h"
37 #include "libavutil/crc.h"
38 #include "libavutil/opt.h"
39 #include "avcodec.h"
40 #include "codec_internal.h"
41 #include "get_bits.h"
42 #include "bytestream.h"
43 #include "golomb.h"
44 #include "flac.h"
45 #include "flacdata.h"
46 #include "flacdsp.h"
47 #include "flac_parse.h"
48 #include "thread.h"
49 #include "unary.h"
50
51
52 typedef struct FLACContext {
53 AVClass *class;
54 FLACStreaminfo stream_info;
55
56 AVCodecContext *avctx; ///< parent AVCodecContext
57 GetBitContext gb; ///< GetBitContext initialized to start at the current frame
58
59 int blocksize; ///< number of samples in the current frame
60 int sample_shift; ///< shift required to make output samples 16-bit or 32-bit
61 int ch_mode; ///< channel decorrelation type in the current frame
62 int got_streaminfo; ///< indicates if the STREAMINFO has been read
63
64 int32_t *decoded[FLAC_MAX_CHANNELS]; ///< decoded samples
65 uint8_t *decoded_buffer;
66 unsigned int decoded_buffer_size;
67 int buggy_lpc; ///< use workaround for old lavc encoded files
68
69 FLACDSPContext dsp;
70 } FLACContext;
71
72 static int allocate_buffers(FLACContext *s);
73
74 106 static void flac_set_bps(FLACContext *s)
75 {
76 106 enum AVSampleFormat req = s->avctx->request_sample_fmt;
77 106 int need32 = s->stream_info.bps > 16;
78 106 int want32 = av_get_bytes_per_sample(req) > 2;
79 106 int planar = av_sample_fmt_is_planar(req);
80
81
3/4
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 90 times.
106 if (need32 || want32) {
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (planar)
83 s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
84 else
85 16 s->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
86 16 s->sample_shift = 32 - s->stream_info.bps;
87 } else {
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
90 if (planar)
89 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
90 else
91 90 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
92 90 s->sample_shift = 16 - s->stream_info.bps;
93 }
94 106 }
95
96 106 static av_cold int flac_decode_init(AVCodecContext *avctx)
97 {
98 uint8_t *streaminfo;
99 int ret;
100 106 FLACContext *s = avctx->priv_data;
101 106 s->avctx = avctx;
102
103 /* for now, the raw FLAC header is allowed to be passed to the decoder as
104 frame data instead of extradata. */
105
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 90 times.
106 if (!avctx->extradata)
106 16 return 0;
107
108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
90 if (!ff_flac_is_extradata_valid(avctx, &streaminfo))
109 return AVERROR_INVALIDDATA;
110
111 /* initialize based on the demuxer-supplied streamdata header */
112 90 ret = ff_flac_parse_streaminfo(avctx, &s->stream_info, streaminfo);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
90 if (ret < 0)
114 return ret;
115 90 ret = allocate_buffers(s);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
90 if (ret < 0)
117 return ret;
118 90 flac_set_bps(s);
119 90 ff_flacdsp_init(&s->dsp, avctx->sample_fmt,
120 s->stream_info.channels);
121 90 s->got_streaminfo = 1;
122
123 90 return 0;
124 }
125
126 16 static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
127 {
128 16 av_log(avctx, AV_LOG_DEBUG, " Max Blocksize: %d\n", s->max_blocksize);
129 16 av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize);
130 16 av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
131 16 av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
132 16 av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
133 16 }
134
135 106 static int allocate_buffers(FLACContext *s)
136 {
137 int buf_size;
138 int ret;
139
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 av_assert0(s->stream_info.max_blocksize);
141
142 106 buf_size = av_samples_get_buffer_size(NULL, s->stream_info.channels,
143 s->stream_info.max_blocksize,
144 AV_SAMPLE_FMT_S32P, 0);
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 if (buf_size < 0)
146 return buf_size;
147
148 106 av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size);
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 if (!s->decoded_buffer)
150 return AVERROR(ENOMEM);
151
152 106 ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL,
153 106 s->decoded_buffer,
154 s->stream_info.channels,
155 s->stream_info.max_blocksize,
156 AV_SAMPLE_FMT_S32P, 0);
157 106 return ret < 0 ? ret : 0;
158 }
159
160 /**
161 * Parse the STREAMINFO from an inline header.
162 * @param s the flac decoding context
163 * @param buf input buffer, starting with the "fLaC" marker
164 * @param buf_size buffer size
165 * @return non-zero if metadata is invalid
166 */
167 static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
168 {
169 int metadata_type, metadata_size, ret;
170
171 if (buf_size < FLAC_STREAMINFO_SIZE+8) {
172 /* need more data */
173 return 0;
174 }
175 flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size);
176 if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO ||
177 metadata_size != FLAC_STREAMINFO_SIZE) {
178 return AVERROR_INVALIDDATA;
179 }
180 ret = ff_flac_parse_streaminfo(s->avctx, &s->stream_info, &buf[8]);
181 if (ret < 0)
182 return ret;
183 ret = allocate_buffers(s);
184 if (ret < 0)
185 return ret;
186 flac_set_bps(s);
187 ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
188 s->stream_info.channels);
189 s->got_streaminfo = 1;
190
191 return 0;
192 }
193
194 /**
195 * Determine the size of an inline header.
196 * @param buf input buffer, starting with the "fLaC" marker
197 * @param buf_size buffer size
198 * @return number of bytes in the header, or 0 if more data is needed
199 */
200 static int get_metadata_size(const uint8_t *buf, int buf_size)
201 {
202 int metadata_last, metadata_size;
203 const uint8_t *buf_end = buf + buf_size;
204
205 buf += 4;
206 do {
207 if (buf_end - buf < 4)
208 return AVERROR_INVALIDDATA;
209 flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size);
210 buf += 4;
211 if (buf_end - buf < metadata_size) {
212 /* need more data in order to read the complete header */
213 return AVERROR_INVALIDDATA;
214 }
215 buf += metadata_size;
216 } while (!metadata_last);
217
218 return buf_size - (buf_end - buf);
219 }
220
221 10473 static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
222 {
223 10473 GetBitContext gb = s->gb;
224 int i, tmp, partition, method_type, rice_order;
225 int rice_bits, rice_esc;
226 int samples;
227
228 10473 method_type = get_bits(&gb, 2);
229 10473 rice_order = get_bits(&gb, 4);
230
231 10473 samples = s->blocksize >> rice_order;
232 10473 rice_bits = 4 + method_type;
233 10473 rice_esc = (1 << rice_bits) - 1;
234
235 10473 decoded += pred_order;
236 10473 i = pred_order;
237
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10473 times.
10473 if (method_type > 1) {
239 av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
240 method_type);
241 return AVERROR_INVALIDDATA;
242 }
243
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10473 times.
10473 if (samples << rice_order != s->blocksize) {
245 av_log(s->avctx, AV_LOG_ERROR, "invalid rice order: %i blocksize %i\n",
246 rice_order, s->blocksize);
247 return AVERROR_INVALIDDATA;
248 }
249
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10473 times.
10473 if (pred_order > samples) {
251 av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
252 pred_order, samples);
253 return AVERROR_INVALIDDATA;
254 }
255
256
2/2
✓ Branch 0 taken 137333 times.
✓ Branch 1 taken 10472 times.
147805 for (partition = 0; partition < (1 << rice_order); partition++) {
257 137333 tmp = get_bits(&gb, rice_bits);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137333 times.
137333 if (tmp == rice_esc) {
259 tmp = get_bits(&gb, 5);
260 for (; i < samples; i++)
261 *decoded++ = get_sbits_long(&gb, tmp);
262 } else {
263
2/2
✓ Branch 0 taken 131617 times.
✓ Branch 1 taken 5716 times.
137333 int real_limit = (tmp > 1) ? (INT_MAX >> (tmp - 1)) + 2 : INT_MAX;
264
2/2
✓ Branch 0 taken 57694445 times.
✓ Branch 1 taken 137332 times.
57831777 for (; i < samples; i++) {
265 57694445 int v = get_sr_golomb_flac(&gb, tmp, real_limit, 1);
266
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 57694444 times.
57694445 if (v == 0x80000000){
267 1 av_log(s->avctx, AV_LOG_ERROR, "invalid residual\n");
268 1 return AVERROR_INVALIDDATA;
269 }
270
271 57694444 *decoded++ = v;
272 }
273 }
274 137332 i= 0;
275 }
276
277 10472 s->gb = gb;
278
279 10472 return 0;
280 }
281
282 935 static int decode_subframe_fixed(FLACContext *s, int32_t *decoded,
283 int pred_order, int bps)
284 {
285 935 const int blocksize = s->blocksize;
286 935 unsigned av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d);
287 int i;
288 int ret;
289
290 /* warm up samples */
291
2/2
✓ Branch 0 taken 2662 times.
✓ Branch 1 taken 935 times.
3597 for (i = 0; i < pred_order; i++) {
292 2662 decoded[i] = get_sbits_long(&s->gb, bps);
293 }
294
295
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 935 times.
935 if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
296 return ret;
297
298
2/2
✓ Branch 0 taken 837 times.
✓ Branch 1 taken 98 times.
935 if (pred_order > 0)
299 837 a = decoded[pred_order-1];
300
2/2
✓ Branch 0 taken 721 times.
✓ Branch 1 taken 214 times.
935 if (pred_order > 1)
301 721 b = a - decoded[pred_order-2];
302
2/2
✓ Branch 0 taken 591 times.
✓ Branch 1 taken 344 times.
935 if (pred_order > 2)
303 591 c = b - decoded[pred_order-2] + decoded[pred_order-3];
304
2/2
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 422 times.
935 if (pred_order > 3)
305 513 d = c - decoded[pred_order-2] + 2U*decoded[pred_order-3] - decoded[pred_order-4];
306
307
5/6
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 116 times.
✓ Branch 2 taken 130 times.
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 513 times.
✗ Branch 5 not taken.
935 switch (pred_order) {
308 98 case 0:
309 98 break;
310 116 case 1:
311
2/2
✓ Branch 0 taken 505740 times.
✓ Branch 1 taken 116 times.
505856 for (i = pred_order; i < blocksize; i++)
312 505740 decoded[i] = a += decoded[i];
313 116 break;
314 130 case 2:
315
2/2
✓ Branch 0 taken 549640 times.
✓ Branch 1 taken 130 times.
549770 for (i = pred_order; i < blocksize; i++)
316 549640 decoded[i] = a += b += decoded[i];
317 130 break;
318 78 case 3:
319
2/2
✓ Branch 0 taken 117270 times.
✓ Branch 1 taken 78 times.
117348 for (i = pred_order; i < blocksize; i++)
320 117270 decoded[i] = a += b += c += decoded[i];
321 78 break;
322 513 case 4:
323
2/2
✓ Branch 0 taken 587484 times.
✓ Branch 1 taken 513 times.
587997 for (i = pred_order; i < blocksize; i++)
324 587484 decoded[i] = a += b += c += d += decoded[i];
325 513 break;
326 default:
327 av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
328 return AVERROR_INVALIDDATA;
329 }
330
331 935 return 0;
332 }
333
334 1932 static void lpc_analyze_remodulate(SUINT32 *decoded, const int coeffs[32],
335 int order, int qlevel, int len, int bps)
336 {
337 int i, j;
338 1932 int ebps = 1 << (bps-1);
339 1932 unsigned sigma = 0;
340
341
2/2
✓ Branch 0 taken 8474229 times.
✓ Branch 1 taken 1932 times.
8476161 for (i = order; i < len; i++)
342 8474229 sigma |= decoded[i] + ebps;
343
344
1/2
✓ Branch 0 taken 1932 times.
✗ Branch 1 not taken.
1932 if (sigma < 2*ebps)
345 1932 return;
346
347 for (i = len - 1; i >= order; i--) {
348 int64_t p = 0;
349 for (j = 0; j < order; j++)
350 p += coeffs[j] * (int64_t)(int32_t)decoded[i-order+j];
351 decoded[i] -= p >> qlevel;
352 }
353 for (i = order; i < len; i++, decoded++) {
354 int32_t p = 0;
355 for (j = 0; j < order; j++)
356 p += coeffs[j] * (uint32_t)decoded[j];
357 decoded[j] += p >> qlevel;
358 }
359 }
360
361 9538 static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order,
362 int bps)
363 {
364 int i, ret;
365 int coeff_prec, qlevel;
366 int coeffs[32];
367
368 /* warm up samples */
369
2/2
✓ Branch 0 taken 41315 times.
✓ Branch 1 taken 9538 times.
50853 for (i = 0; i < pred_order; i++) {
370 41315 decoded[i] = get_sbits_long(&s->gb, bps);
371 }
372
373 9538 coeff_prec = get_bits(&s->gb, 4) + 1;
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9538 times.
9538 if (coeff_prec == 16) {
375 av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
376 return AVERROR_INVALIDDATA;
377 }
378 9538 qlevel = get_sbits(&s->gb, 5);
379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9538 times.
9538 if (qlevel < 0) {
380 av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
381 qlevel);
382 return AVERROR_INVALIDDATA;
383 }
384
385
2/2
✓ Branch 0 taken 41315 times.
✓ Branch 1 taken 9538 times.
50853 for (i = 0; i < pred_order; i++) {
386 41315 coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
387 }
388
389
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9537 times.
9538 if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
390 1 return ret;
391
392
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9537 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
9537 if ( ( s->buggy_lpc && s->stream_info.bps <= 16)
393
3/4
✓ Branch 0 taken 9537 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6866 times.
✓ Branch 3 taken 2671 times.
9537 || ( !s->buggy_lpc && bps <= 16
394
2/2
✓ Branch 0 taken 6173 times.
✓ Branch 1 taken 693 times.
6866 && bps + coeff_prec + av_log2(pred_order) <= 32)) {
395 6173 s->dsp.lpc16(decoded, coeffs, pred_order, qlevel, s->blocksize);
396 } else {
397 3364 s->dsp.lpc32(decoded, coeffs, pred_order, qlevel, s->blocksize);
398
2/2
✓ Branch 0 taken 1932 times.
✓ Branch 1 taken 1432 times.
3364 if (s->stream_info.bps <= 16)
399 1932 lpc_analyze_remodulate(decoded, coeffs, pred_order, qlevel, s->blocksize, bps);
400 }
401
402 9537 return 0;
403 }
404
405 16248 static inline int decode_subframe(FLACContext *s, int channel)
406 {
407 16248 int32_t *decoded = s->decoded[channel];
408 16248 int type, wasted = 0;
409 16248 int bps = s->stream_info.bps;
410 int i, tmp, ret;
411
412
2/2
✓ Branch 0 taken 6048 times.
✓ Branch 1 taken 10200 times.
16248 if (channel == 0) {
413
2/2
✓ Branch 0 taken 634 times.
✓ Branch 1 taken 5414 times.
6048 if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)
414 634 bps++;
415 } else {
416
4/4
✓ Branch 0 taken 8571 times.
✓ Branch 1 taken 1629 times.
✓ Branch 2 taken 906 times.
✓ Branch 3 taken 7665 times.
10200 if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE)
417 2535 bps++;
418 }
419
420
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16248 times.
16248 if (get_bits1(&s->gb)) {
421 av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
422 return AVERROR_INVALIDDATA;
423 }
424 16248 type = get_bits(&s->gb, 6);
425
426
2/2
✓ Branch 1 taken 5234 times.
✓ Branch 2 taken 11014 times.
16248 if (get_bits1(&s->gb)) {
427 5234 int left = get_bits_left(&s->gb);
428
2/4
✓ Branch 0 taken 5234 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5234 times.
5234 if ( left <= 0 ||
429
1/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5234 times.
5234 (left < bps && !show_bits_long(&s->gb, left)) ||
430 5234 !show_bits_long(&s->gb, bps)) {
431 av_log(s->avctx, AV_LOG_ERROR,
432 "Invalid number of wasted bits > available bits (%d) - left=%d\n",
433 bps, left);
434 return AVERROR_INVALIDDATA;
435 }
436 5234 wasted = 1 + get_unary(&s->gb, 1, get_bits_left(&s->gb));
437 5234 bps -= wasted;
438 }
439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16248 times.
16248 if (bps > 32) {
440 avpriv_report_missing_feature(s->avctx, "Decorrelated bit depth > 32");
441 return AVERROR_PATCHWELCOME;
442 }
443
444 //FIXME use av_log2 for types
445
2/2
✓ Branch 0 taken 5775 times.
✓ Branch 1 taken 10473 times.
16248 if (type == 0) {
446 5775 tmp = get_sbits_long(&s->gb, bps);
447
2/2
✓ Branch 0 taken 35713536 times.
✓ Branch 1 taken 5775 times.
35719311 for (i = 0; i < s->blocksize; i++)
448 35713536 decoded[i] = tmp;
449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10473 times.
10473 } else if (type == 1) {
450 for (i = 0; i < s->blocksize; i++)
451 decoded[i] = get_sbits_long(&s->gb, bps);
452
3/4
✓ Branch 0 taken 10473 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 935 times.
✓ Branch 3 taken 9538 times.
10473 } else if ((type >= 8) && (type <= 12)) {
453
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 935 times.
935 if ((ret = decode_subframe_fixed(s, decoded, type & ~0x8, bps)) < 0)
454 return ret;
455
1/2
✓ Branch 0 taken 9538 times.
✗ Branch 1 not taken.
9538 } else if (type >= 32) {
456
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9537 times.
9538 if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0)
457 1 return ret;
458 } else {
459 av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
460 return AVERROR_INVALIDDATA;
461 }
462
463
3/4
✓ Branch 0 taken 5234 times.
✓ Branch 1 taken 11013 times.
✓ Branch 2 taken 5234 times.
✗ Branch 3 not taken.
16247 if (wasted && wasted < 32) {
464 int i;
465
2/2
✓ Branch 0 taken 34178824 times.
✓ Branch 1 taken 5234 times.
34184058 for (i = 0; i < s->blocksize; i++)
466 34178824 decoded[i] = (unsigned)decoded[i] << wasted;
467 }
468
469 16247 return 0;
470 }
471
472 6048 static int decode_frame(FLACContext *s)
473 {
474 int i, ret;
475 6048 GetBitContext *gb = &s->gb;
476 FLACFrameInfo fi;
477
478
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6048 times.
6048 if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) {
479 av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
480 return ret;
481 }
482
483
2/2
✓ Branch 0 taken 6032 times.
✓ Branch 1 taken 16 times.
6048 if ( s->stream_info.channels
484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6032 times.
6032 && fi.channels != s->stream_info.channels
485 && s->got_streaminfo) {
486 s->stream_info.channels = fi.channels;
487 ff_flac_set_channel_layout(s->avctx, fi.channels);
488 ret = allocate_buffers(s);
489 if (ret < 0)
490 return ret;
491 }
492 6048 s->stream_info.channels = fi.channels;
493 6048 ff_flac_set_channel_layout(s->avctx, fi.channels);
494 6048 s->ch_mode = fi.ch_mode;
495
496
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6032 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
6048 if (!s->stream_info.bps && !fi.bps) {
497 av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n");
498 return AVERROR_INVALIDDATA;
499 }
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6048 times.
6048 if (!fi.bps) {
501 fi.bps = s->stream_info.bps;
502
3/4
✓ Branch 0 taken 6032 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6032 times.
6048 } else if (s->stream_info.bps && fi.bps != s->stream_info.bps) {
503 av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "
504 "supported\n");
505 return AVERROR_INVALIDDATA;
506 }
507
508
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6032 times.
6048 if (!s->stream_info.bps) {
509 16 s->stream_info.bps = s->avctx->bits_per_raw_sample = fi.bps;
510 16 flac_set_bps(s);
511 }
512
513
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6032 times.
6048 if (!s->stream_info.max_blocksize)
514 16 s->stream_info.max_blocksize = FLAC_MAX_BLOCKSIZE;
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6048 times.
6048 if (fi.blocksize > s->stream_info.max_blocksize) {
516 av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,
517 s->stream_info.max_blocksize);
518 return AVERROR_INVALIDDATA;
519 }
520 6048 s->blocksize = fi.blocksize;
521
522
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6032 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
6048 if (!s->stream_info.samplerate && !fi.samplerate) {
523 av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"
524 " or frame header\n");
525 return AVERROR_INVALIDDATA;
526 }
527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6048 times.
6048 if (fi.samplerate == 0)
528 fi.samplerate = s->stream_info.samplerate;
529 6048 s->stream_info.samplerate = s->avctx->sample_rate = fi.samplerate;
530
531
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6032 times.
6048 if (!s->got_streaminfo) {
532 16 ret = allocate_buffers(s);
533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0)
534 return ret;
535 16 s->got_streaminfo = 1;
536 16 dump_headers(s->avctx, &s->stream_info);
537 }
538 6048 ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
539 s->stream_info.channels);
540
541 // dump_headers(s->avctx, &s->stream_info);
542
543 /* subframes */
544
2/2
✓ Branch 0 taken 16248 times.
✓ Branch 1 taken 6047 times.
22295 for (i = 0; i < s->stream_info.channels; i++) {
545
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 16247 times.
16248 if ((ret = decode_subframe(s, i)) < 0)
546 1 return ret;
547 }
548
549 6047 align_get_bits(gb);
550
551 /* frame footer */
552 6047 skip_bits(gb, 16); /* data crc */
553
554 6047 return 0;
555 }
556
557 6048 static int flac_decode_frame(AVCodecContext *avctx, AVFrame *frame,
558 int *got_frame_ptr, AVPacket *avpkt)
559 {
560 6048 const uint8_t *buf = avpkt->data;
561 6048 int buf_size = avpkt->size;
562 6048 FLACContext *s = avctx->priv_data;
563 6048 int bytes_read = 0;
564 int ret;
565
566 6048 *got_frame_ptr = 0;
567
568
2/4
✓ Branch 0 taken 6048 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6048 times.
6048 if (buf_size > 5 && !memcmp(buf, "\177FLAC", 5)) {
569 av_log(s->avctx, AV_LOG_DEBUG, "skipping flac header packet 1\n");
570 return buf_size;
571 }
572
573
2/4
✓ Branch 0 taken 6048 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6048 times.
6048 if (buf_size > 0 && (*buf & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
574 av_log(s->avctx, AV_LOG_DEBUG, "skipping vorbis comment\n");
575 return buf_size;
576 }
577
578 /* check that there is at least the smallest decodable amount of data.
579 this amount corresponds to the smallest valid FLAC frame possible.
580 FF F8 69 02 00 00 9A 00 00 34 */
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6048 times.
6048 if (buf_size < FLAC_MIN_FRAME_SIZE)
582 return buf_size;
583
584 /* check for inline header */
585
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6048 times.
6048 if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
586 if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) {
587 av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
588 return ret;
589 }
590 return get_metadata_size(buf, buf_size);
591 }
592
593 /* decode frame */
594
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6048 times.
6048 if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
595 return ret;
596
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6047 times.
6048 if ((ret = decode_frame(s)) < 0) {
597 1 av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
598 1 return ret;
599 }
600 6047 bytes_read = get_bits_count(&s->gb)/8;
601
602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6047 times.
6047 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_COMPLIANT)) &&
603 av_crc(av_crc_get_table(AV_CRC_16_ANSI),
604 0, buf, bytes_read)) {
605 av_log(s->avctx, AV_LOG_ERROR, "CRC error at PTS %"PRId64"\n", avpkt->pts);
606 if (s->avctx->err_recognition & AV_EF_EXPLODE)
607 return AVERROR_INVALIDDATA;
608 }
609
610 /* get output buffer */
611 6047 frame->nb_samples = s->blocksize;
612
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6047 times.
6047 if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
613 return ret;
614
615 6047 s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded,
616 s->stream_info.channels,
617 s->blocksize, s->sample_shift);
618
619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6047 times.
6047 if (bytes_read > buf_size) {
620 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
621 return AVERROR_INVALIDDATA;
622 }
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6047 times.
6047 if (bytes_read < buf_size) {
624 av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
625 buf_size - bytes_read, buf_size);
626 }
627
628 6047 *got_frame_ptr = 1;
629
630 6047 return bytes_read;
631 }
632
633 106 static av_cold int flac_decode_close(AVCodecContext *avctx)
634 {
635 106 FLACContext *s = avctx->priv_data;
636
637 106 av_freep(&s->decoded_buffer);
638
639 106 return 0;
640 }
641
642 static const AVOption options[] = {
643 { "use_buggy_lpc", "emulate old buggy lavc behavior", offsetof(FLACContext, buggy_lpc), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
644 { NULL },
645 };
646
647 static const AVClass flac_decoder_class = {
648 .class_name = "FLAC decoder",
649 .item_name = av_default_item_name,
650 .option = options,
651 .version = LIBAVUTIL_VERSION_INT,
652 };
653
654 const FFCodec ff_flac_decoder = {
655 .p.name = "flac",
656 CODEC_LONG_NAME("FLAC (Free Lossless Audio Codec)"),
657 .p.type = AVMEDIA_TYPE_AUDIO,
658 .p.id = AV_CODEC_ID_FLAC,
659 .priv_data_size = sizeof(FLACContext),
660 .init = flac_decode_init,
661 .close = flac_decode_close,
662 FF_CODEC_DECODE_CB(flac_decode_frame),
663 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
664 AV_CODEC_CAP_DR1 |
665 AV_CODEC_CAP_FRAME_THREADS,
666 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
667 AV_SAMPLE_FMT_S16P,
668 AV_SAMPLE_FMT_S32,
669 AV_SAMPLE_FMT_S32P,
670 AV_SAMPLE_FMT_NONE },
671 .p.priv_class = &flac_decoder_class,
672 };
673