FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/flacdec.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 273 407 67.1%
Functions: 14 18 77.8%
Branches: 182 336 54.2%

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 int64_t *decoded_33bps; ///< decoded samples for a 33 bps subframe
68 uint8_t *decoded_buffer_33bps;
69 unsigned int decoded_buffer_size_33bps;
70 int buggy_lpc; ///< use workaround for old lavc encoded files
71
72 FLACDSPContext dsp;
73 } FLACContext;
74
75 static int allocate_buffers(FLACContext *s);
76
77 126 static void flac_set_bps(FLACContext *s)
78 {
79 126 enum AVSampleFormat req = s->avctx->request_sample_fmt;
80 126 int need32 = s->stream_info.bps > 16;
81 126 int want32 = av_get_bytes_per_sample(req) > 2;
82 126 int planar = av_sample_fmt_is_planar(req);
83
84
3/4
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 108 times.
126 if (need32 || want32) {
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (planar)
86 s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
87 else
88 18 s->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
89 18 s->sample_shift = 32 - s->stream_info.bps;
90 } else {
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
108 if (planar)
92 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
93 else
94 108 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
95 108 s->sample_shift = 16 - s->stream_info.bps;
96 }
97 126 }
98
99 126 static av_cold int flac_decode_init(AVCodecContext *avctx)
100 {
101 uint8_t *streaminfo;
102 int ret;
103 126 FLACContext *s = avctx->priv_data;
104 126 s->avctx = avctx;
105
106 /* for now, the raw FLAC header is allowed to be passed to the decoder as
107 frame data instead of extradata. */
108
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 110 times.
126 if (!avctx->extradata)
109 16 return 0;
110
111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 110 times.
110 if (!ff_flac_is_extradata_valid(avctx, &streaminfo))
112 return AVERROR_INVALIDDATA;
113
114 /* initialize based on the demuxer-supplied streamdata header */
115 110 ret = ff_flac_parse_streaminfo(avctx, &s->stream_info, streaminfo);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (ret < 0)
117 return ret;
118 110 ret = allocate_buffers(s);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (ret < 0)
120 return ret;
121 110 flac_set_bps(s);
122 110 ff_flacdsp_init(&s->dsp, avctx->sample_fmt,
123 s->stream_info.channels);
124 110 s->got_streaminfo = 1;
125
126 110 return 0;
127 }
128
129 16 static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
130 {
131 16 av_log(avctx, AV_LOG_DEBUG, " Max Blocksize: %d\n", s->max_blocksize);
132 16 av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize);
133 16 av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
134 16 av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
135 16 av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
136 16 }
137
138 126 static int allocate_buffers(FLACContext *s)
139 {
140 int buf_size;
141 int ret;
142
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 av_assert0(s->stream_info.max_blocksize);
144
145 126 buf_size = av_samples_get_buffer_size(NULL, s->stream_info.channels,
146 s->stream_info.max_blocksize,
147 AV_SAMPLE_FMT_S32P, 0);
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (buf_size < 0)
149 return buf_size;
150
151 126 av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size);
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (!s->decoded_buffer)
153 return AVERROR(ENOMEM);
154
155 126 ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL,
156 126 s->decoded_buffer,
157 s->stream_info.channels,
158 s->stream_info.max_blocksize,
159 AV_SAMPLE_FMT_S32P, 0);
160
4/6
✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 124 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
126 if (ret >= 0 && s->stream_info.bps == 32 && s->stream_info.channels == 2) {
161 2 buf_size = av_samples_get_buffer_size(NULL, 1,
162 s->stream_info.max_blocksize,
163 AV_SAMPLE_FMT_S64P, 0);
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (buf_size < 0)
165 return buf_size;
166
167 2 av_fast_malloc(&s->decoded_buffer_33bps, &s->decoded_buffer_size_33bps, buf_size);
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->decoded_buffer_33bps)
169 return AVERROR(ENOMEM);
170
171 2 ret = av_samples_fill_arrays((uint8_t **)&s->decoded_33bps, NULL,
172 2 s->decoded_buffer_33bps,
173 1,
174 s->stream_info.max_blocksize,
175 AV_SAMPLE_FMT_S64P, 0);
176
177 }
178 126 return ret < 0 ? ret : 0;
179 }
180
181 /**
182 * Parse the STREAMINFO from an inline header.
183 * @param s the flac decoding context
184 * @param buf input buffer, starting with the "fLaC" marker
185 * @param buf_size buffer size
186 * @return non-zero if metadata is invalid
187 */
188 static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
189 {
190 int metadata_type, metadata_size, ret;
191
192 if (buf_size < FLAC_STREAMINFO_SIZE+8) {
193 /* need more data */
194 return 0;
195 }
196 flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size);
197 if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO ||
198 metadata_size != FLAC_STREAMINFO_SIZE) {
199 return AVERROR_INVALIDDATA;
200 }
201 ret = ff_flac_parse_streaminfo(s->avctx, &s->stream_info, &buf[8]);
202 if (ret < 0)
203 return ret;
204 ret = allocate_buffers(s);
205 if (ret < 0)
206 return ret;
207 flac_set_bps(s);
208 ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
209 s->stream_info.channels);
210 s->got_streaminfo = 1;
211
212 return 0;
213 }
214
215 /**
216 * Determine the size of an inline header.
217 * @param buf input buffer, starting with the "fLaC" marker
218 * @param buf_size buffer size
219 * @return number of bytes in the header, or 0 if more data is needed
220 */
221 static int get_metadata_size(const uint8_t *buf, int buf_size)
222 {
223 int metadata_last, metadata_size;
224 const uint8_t *buf_end = buf + buf_size;
225
226 buf += 4;
227 do {
228 if (buf_end - buf < 4)
229 return AVERROR_INVALIDDATA;
230 flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size);
231 buf += 4;
232 if (buf_end - buf < metadata_size) {
233 /* need more data in order to read the complete header */
234 return AVERROR_INVALIDDATA;
235 }
236 buf += metadata_size;
237 } while (!metadata_last);
238
239 return buf_size - (buf_end - buf);
240 }
241
242 10490 static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
243 {
244 10490 GetBitContext gb = s->gb;
245 int i, tmp, partition, method_type, rice_order;
246 int rice_bits, rice_esc;
247 int samples;
248
249 10490 method_type = get_bits(&gb, 2);
250 10490 rice_order = get_bits(&gb, 4);
251
252 10490 samples = s->blocksize >> rice_order;
253 10490 rice_bits = 4 + method_type;
254 10490 rice_esc = (1 << rice_bits) - 1;
255
256 10490 decoded += pred_order;
257 10490 i = pred_order;
258
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10490 times.
10490 if (method_type > 1) {
260 av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
261 method_type);
262 return AVERROR_INVALIDDATA;
263 }
264
265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10490 times.
10490 if (samples << rice_order != s->blocksize) {
266 av_log(s->avctx, AV_LOG_ERROR, "invalid rice order: %i blocksize %i\n",
267 rice_order, s->blocksize);
268 return AVERROR_INVALIDDATA;
269 }
270
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10490 times.
10490 if (pred_order > samples) {
272 av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
273 pred_order, samples);
274 return AVERROR_INVALIDDATA;
275 }
276
277
2/2
✓ Branch 0 taken 137620 times.
✓ Branch 1 taken 10489 times.
148109 for (partition = 0; partition < (1 << rice_order); partition++) {
278 137620 tmp = get_bits(&gb, rice_bits);
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137620 times.
137620 if (tmp == rice_esc) {
280 tmp = get_bits(&gb, 5);
281 for (; i < samples; i++)
282 *decoded++ = get_sbits_long(&gb, tmp);
283 } else {
284
2/2
✓ Branch 0 taken 131787 times.
✓ Branch 1 taken 5833 times.
137620 int real_limit = (tmp > 1) ? (INT_MAX >> (tmp - 1)) + 2 : INT_MAX;
285
2/2
✓ Branch 0 taken 57768762 times.
✓ Branch 1 taken 137619 times.
57906381 for (; i < samples; i++) {
286 57768762 int v = get_sr_golomb_flac(&gb, tmp, real_limit, 1);
287
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 57768761 times.
57768762 if (v == 0x80000000){
288 1 av_log(s->avctx, AV_LOG_ERROR, "invalid residual\n");
289 1 return AVERROR_INVALIDDATA;
290 }
291
292 57768761 *decoded++ = v;
293 }
294 }
295 137619 i= 0;
296 }
297
298 10489 s->gb = gb;
299
300 10489 return 0;
301 }
302
303 935 static int decode_subframe_fixed(FLACContext *s, int32_t *decoded,
304 int pred_order, int bps)
305 {
306 935 const int blocksize = s->blocksize;
307 935 unsigned av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d);
308 int i;
309 int ret;
310
311 /* warm up samples */
312
2/2
✓ Branch 0 taken 2662 times.
✓ Branch 1 taken 935 times.
3597 for (i = 0; i < pred_order; i++) {
313 2662 decoded[i] = get_sbits_long(&s->gb, bps);
314 }
315
316
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 935 times.
935 if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
317 return ret;
318
319
2/2
✓ Branch 0 taken 837 times.
✓ Branch 1 taken 98 times.
935 if (pred_order > 0)
320 837 a = decoded[pred_order-1];
321
2/2
✓ Branch 0 taken 721 times.
✓ Branch 1 taken 214 times.
935 if (pred_order > 1)
322 721 b = a - decoded[pred_order-2];
323
2/2
✓ Branch 0 taken 591 times.
✓ Branch 1 taken 344 times.
935 if (pred_order > 2)
324 591 c = b - decoded[pred_order-2] + decoded[pred_order-3];
325
2/2
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 422 times.
935 if (pred_order > 3)
326 513 d = c - decoded[pred_order-2] + 2U*decoded[pred_order-3] - decoded[pred_order-4];
327
328
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) {
329 98 case 0:
330 98 break;
331 116 case 1:
332
2/2
✓ Branch 0 taken 505740 times.
✓ Branch 1 taken 116 times.
505856 for (i = pred_order; i < blocksize; i++)
333 505740 decoded[i] = a += decoded[i];
334 116 break;
335 130 case 2:
336
2/2
✓ Branch 0 taken 549640 times.
✓ Branch 1 taken 130 times.
549770 for (i = pred_order; i < blocksize; i++)
337 549640 decoded[i] = a += b += decoded[i];
338 130 break;
339 78 case 3:
340
2/2
✓ Branch 0 taken 117270 times.
✓ Branch 1 taken 78 times.
117348 for (i = pred_order; i < blocksize; i++)
341 117270 decoded[i] = a += b += c += decoded[i];
342 78 break;
343 513 case 4:
344
2/2
✓ Branch 0 taken 587484 times.
✓ Branch 1 taken 513 times.
587997 for (i = pred_order; i < blocksize; i++)
345 587484 decoded[i] = a += b += c += d += decoded[i];
346 513 break;
347 default:
348 av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
349 return AVERROR_INVALIDDATA;
350 }
351
352 935 return 0;
353 }
354
355 #define DECODER_SUBFRAME_FIXED_WIDE(residual) { \
356 const int blocksize = s->blocksize; \
357 int ret; \
358 \
359 if ((ret = decode_residuals(s, residual, pred_order)) < 0) \
360 return ret; \
361 \
362 switch (pred_order) { \
363 case 0: \
364 for (int i = pred_order; i < blocksize; i++) \
365 decoded[i] = residual[i]; \
366 break; \
367 case 1: \
368 for (int i = pred_order; i < blocksize; i++) \
369 decoded[i] = (uint64_t)residual[i] + (uint64_t)decoded[i-1];\
370 break; \
371 case 2: \
372 for (int i = pred_order; i < blocksize; i++) \
373 decoded[i] = (uint64_t)residual[i] + 2*(uint64_t)decoded[i-1] - (uint64_t)decoded[i-2]; \
374 break; \
375 case 3: \
376 for (int i = pred_order; i < blocksize; i++) \
377 decoded[i] = (uint64_t)residual[i] + 3*(uint64_t)decoded[i-1] - 3*(uint64_t)decoded[i-2] + (uint64_t)decoded[i-3]; \
378 break; \
379 case 4: \
380 for (int i = pred_order; i < blocksize; i++) \
381 decoded[i] = (uint64_t)residual[i] + 4*(uint64_t)decoded[i-1] - 6*(uint64_t)decoded[i-2] + 4*(uint64_t)decoded[i-3] - (uint64_t)decoded[i-4]; \
382 break; \
383 default: \
384 av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order); \
385 return AVERROR_INVALIDDATA; \
386 } \
387 return 0; \
388 }
389
390 static int decode_subframe_fixed_wide(FLACContext *s, int32_t *decoded,
391 int pred_order, int bps)
392 {
393 /* warm up samples */
394 for (int i = 0; i < pred_order; i++) {
395 decoded[i] = get_sbits_long(&s->gb, bps);
396 }
397 DECODER_SUBFRAME_FIXED_WIDE(decoded);
398 }
399
400
401 static int decode_subframe_fixed_33bps(FLACContext *s, int64_t *decoded,
402 int32_t *residual, int pred_order)
403 {
404 /* warm up samples */ \
405 for (int i = 0; i < pred_order; i++) { \
406 decoded[i] = get_sbits64(&s->gb, 33); \
407 } \
408 DECODER_SUBFRAME_FIXED_WIDE(residual);
409 }
410
411 1932 static void lpc_analyze_remodulate(SUINT32 *decoded, const int coeffs[32],
412 int order, int qlevel, int len, int bps)
413 {
414 int i, j;
415 1932 int ebps = 1 << (bps-1);
416 1932 unsigned sigma = 0;
417
418
2/2
✓ Branch 0 taken 8474229 times.
✓ Branch 1 taken 1932 times.
8476161 for (i = order; i < len; i++)
419 8474229 sigma |= decoded[i] + ebps;
420
421
1/2
✓ Branch 0 taken 1932 times.
✗ Branch 1 not taken.
1932 if (sigma < 2*ebps)
422 1932 return;
423
424 for (i = len - 1; i >= order; i--) {
425 int64_t p = 0;
426 for (j = 0; j < order; j++)
427 p += coeffs[j] * (int64_t)(int32_t)decoded[i-order+j];
428 decoded[i] -= p >> qlevel;
429 }
430 for (i = order; i < len; i++, decoded++) {
431 int32_t p = 0;
432 for (j = 0; j < order; j++)
433 p += coeffs[j] * (uint32_t)decoded[j];
434 decoded[j] += p >> qlevel;
435 }
436 }
437
438 9553 static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order,
439 int bps)
440 {
441 int i, ret;
442 int coeff_prec, qlevel;
443 int coeffs[32];
444
445 /* warm up samples */
446
2/2
✓ Branch 0 taken 41424 times.
✓ Branch 1 taken 9553 times.
50977 for (i = 0; i < pred_order; i++) {
447 41424 decoded[i] = get_sbits_long(&s->gb, bps);
448 }
449
450 9553 coeff_prec = get_bits(&s->gb, 4) + 1;
451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9553 times.
9553 if (coeff_prec == 16) {
452 av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
453 return AVERROR_INVALIDDATA;
454 }
455 9553 qlevel = get_sbits(&s->gb, 5);
456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9553 times.
9553 if (qlevel < 0) {
457 av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
458 qlevel);
459 return AVERROR_INVALIDDATA;
460 }
461
462
2/2
✓ Branch 0 taken 41424 times.
✓ Branch 1 taken 9553 times.
50977 for (i = 0; i < pred_order; i++) {
463 41424 coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
464 }
465
466
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9552 times.
9553 if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
467 1 return ret;
468
469
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9552 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
9552 if ( ( s->buggy_lpc && s->stream_info.bps <= 16)
470
3/4
✓ Branch 0 taken 9552 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6866 times.
✓ Branch 3 taken 2686 times.
9552 || ( !s->buggy_lpc && bps <= 16
471
2/2
✓ Branch 0 taken 6173 times.
✓ Branch 1 taken 693 times.
6866 && bps + coeff_prec + av_log2(pred_order) <= 32)) {
472 6173 s->dsp.lpc16(decoded, coeffs, pred_order, qlevel, s->blocksize);
473 } else {
474 3379 s->dsp.lpc32(decoded, coeffs, pred_order, qlevel, s->blocksize);
475
2/2
✓ Branch 0 taken 1932 times.
✓ Branch 1 taken 1447 times.
3379 if (s->stream_info.bps <= 16)
476 1932 lpc_analyze_remodulate(decoded, coeffs, pred_order, qlevel, s->blocksize, bps);
477 }
478
479 9552 return 0;
480 }
481
482 2 static int decode_subframe_lpc_33bps(FLACContext *s, int64_t *decoded,
483 int32_t *residual, int pred_order)
484 {
485 int i, j, ret;
486 int coeff_prec, qlevel;
487 int coeffs[32];
488
489 /* warm up samples */
490
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (i = 0; i < pred_order; i++) {
491 16 decoded[i] = get_sbits64(&s->gb, 33);
492 }
493
494 2 coeff_prec = get_bits(&s->gb, 4) + 1;
495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (coeff_prec == 16) {
496 av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
497 return AVERROR_INVALIDDATA;
498 }
499 2 qlevel = get_sbits(&s->gb, 5);
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (qlevel < 0) {
501 av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
502 qlevel);
503 return AVERROR_INVALIDDATA;
504 }
505
506
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (i = 0; i < pred_order; i++) {
507 16 coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
508 }
509
510
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = decode_residuals(s, residual, pred_order)) < 0)
511 return ret;
512
513
2/2
✓ Branch 0 taken 9200 times.
✓ Branch 1 taken 2 times.
9202 for (i = pred_order; i < s->blocksize; i++, decoded++) {
514 9200 int64_t sum = 0;
515
2/2
✓ Branch 0 taken 73600 times.
✓ Branch 1 taken 9200 times.
82800 for (j = 0; j < pred_order; j++)
516 73600 sum += (int64_t)coeffs[j] * (uint64_t)decoded[j];
517 9200 decoded[j] = residual[i] + (sum >> qlevel);
518 }
519
520 2 return 0;
521 }
522
523 16282 static inline int decode_subframe(FLACContext *s, int channel)
524 {
525 16282 int32_t *decoded = s->decoded[channel];
526 16282 int type, wasted = 0;
527 16282 int bps = s->stream_info.bps;
528 int i, ret;
529
530
2/2
✓ Branch 0 taken 6061 times.
✓ Branch 1 taken 10221 times.
16282 if (channel == 0) {
531
2/2
✓ Branch 0 taken 642 times.
✓ Branch 1 taken 5419 times.
6061 if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)
532 642 bps++;
533 } else {
534
4/4
✓ Branch 0 taken 8592 times.
✓ Branch 1 taken 1629 times.
✓ Branch 2 taken 907 times.
✓ Branch 3 taken 7685 times.
10221 if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE)
535 2536 bps++;
536 }
537
538
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16282 times.
16282 if (get_bits1(&s->gb)) {
539 av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
540 return AVERROR_INVALIDDATA;
541 }
542 16282 type = get_bits(&s->gb, 6);
543
544
2/2
✓ Branch 1 taken 5246 times.
✓ Branch 2 taken 11036 times.
16282 if (get_bits1(&s->gb)) {
545 5246 int left = get_bits_left(&s->gb);
546
2/4
✓ Branch 0 taken 5246 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5246 times.
5246 if ( left <= 0 ||
547
1/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5246 times.
5246 (left < bps && !show_bits_long(&s->gb, left)) ||
548 5246 !show_bits_long(&s->gb, bps-1)) {
549 av_log(s->avctx, AV_LOG_ERROR,
550 "Invalid number of wasted bits > available bits (%d) - left=%d\n",
551 bps, left);
552 return AVERROR_INVALIDDATA;
553 }
554 5246 wasted = 1 + get_unary(&s->gb, 1, get_bits_left(&s->gb));
555 5246 bps -= wasted;
556 }
557
558 //FIXME use av_log2 for types
559
2/2
✓ Branch 0 taken 5791 times.
✓ Branch 1 taken 10491 times.
16282 if (type == 0) {
560
1/2
✓ Branch 0 taken 5791 times.
✗ Branch 1 not taken.
5791 if (bps < 33) {
561 5791 int32_t tmp = get_sbits_long(&s->gb, bps);
562
2/2
✓ Branch 0 taken 35781120 times.
✓ Branch 1 taken 5791 times.
35786911 for (i = 0; i < s->blocksize; i++)
563 35781120 decoded[i] = tmp;
564 } else {
565 int64_t tmp = get_sbits64(&s->gb, 33);
566 for (i = 0; i < s->blocksize; i++)
567 s->decoded_33bps[i] = tmp;
568 }
569
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10490 times.
10491 } else if (type == 1) {
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (bps < 33) {
571 for (i = 0; i < s->blocksize; i++)
572 decoded[i] = get_sbits_long(&s->gb, bps);
573 } else {
574
2/2
✓ Branch 0 taken 4608 times.
✓ Branch 1 taken 1 times.
4609 for (i = 0; i < s->blocksize; i++)
575 4608 s->decoded_33bps[i] = get_sbits64(&s->gb, 33);
576 }
577
3/4
✓ Branch 0 taken 10490 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 935 times.
✓ Branch 3 taken 9555 times.
11425 } else if ((type >= 8) && (type <= 12)) {
578 935 int order = type & ~0x8;
579
1/2
✓ Branch 0 taken 935 times.
✗ Branch 1 not taken.
935 if (bps < 33) {
580
1/2
✓ Branch 0 taken 935 times.
✗ Branch 1 not taken.
935 if (bps + order <= 32) {
581
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 935 times.
935 if ((ret = decode_subframe_fixed(s, decoded, order, bps)) < 0)
582 return ret;
583 } else {
584 if ((ret = decode_subframe_fixed_wide(s, decoded, order, bps)) < 0)
585 return ret;
586 }
587 } else {
588 if ((ret = decode_subframe_fixed_33bps(s, s->decoded_33bps, decoded, order)) < 0)
589 return ret;
590 }
591
1/2
✓ Branch 0 taken 9555 times.
✗ Branch 1 not taken.
9555 } else if (type >= 32) {
592
2/2
✓ Branch 0 taken 9553 times.
✓ Branch 1 taken 2 times.
9555 if (bps < 33) {
593
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9552 times.
9553 if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0)
594 1 return ret;
595 } else {
596
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = decode_subframe_lpc_33bps(s, s->decoded_33bps, decoded, (type & ~0x20)+1)) < 0)
597 return ret;
598 }
599 } else {
600 av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
601 return AVERROR_INVALIDDATA;
602 }
603
604
2/2
✓ Branch 0 taken 5246 times.
✓ Branch 1 taken 11035 times.
16281 if (wasted) {
605
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5240 times.
5246 if (wasted+bps == 33) {
606 int i;
607
2/2
✓ Branch 0 taken 25701 times.
✓ Branch 1 taken 6 times.
25707 for (i = 0; i < s->blocksize; i++)
608 25701 s->decoded_33bps[i] = (uint64_t)decoded[i] << wasted;
609
1/2
✓ Branch 0 taken 5240 times.
✗ Branch 1 not taken.
5240 } else if (wasted < 32) {
610 int i;
611
2/2
✓ Branch 0 taken 34204525 times.
✓ Branch 1 taken 5240 times.
34209765 for (i = 0; i < s->blocksize; i++)
612 34204525 decoded[i] = (unsigned)decoded[i] << wasted;
613 }
614 }
615
616 16281 return 0;
617 }
618
619 6061 static int decode_frame(FLACContext *s)
620 {
621 int i, ret;
622 6061 GetBitContext *gb = &s->gb;
623 FLACFrameInfo fi;
624
625
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6061 times.
6061 if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) {
626 av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
627 return ret;
628 }
629
630
2/2
✓ Branch 0 taken 6045 times.
✓ Branch 1 taken 16 times.
6061 if ( s->stream_info.channels
631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6045 times.
6045 && fi.channels != s->stream_info.channels
632 && s->got_streaminfo) {
633 s->stream_info.channels = fi.channels;
634 ff_flac_set_channel_layout(s->avctx, fi.channels);
635 ret = allocate_buffers(s);
636 if (ret < 0)
637 return ret;
638 }
639 6061 s->stream_info.channels = fi.channels;
640 6061 ff_flac_set_channel_layout(s->avctx, fi.channels);
641 6061 s->ch_mode = fi.ch_mode;
642
643
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6045 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
6061 if (!s->stream_info.bps && !fi.bps) {
644 av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n");
645 return AVERROR_INVALIDDATA;
646 }
647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6061 times.
6061 if (!fi.bps) {
648 fi.bps = s->stream_info.bps;
649
3/4
✓ Branch 0 taken 6045 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6045 times.
6061 } else if (s->stream_info.bps && fi.bps != s->stream_info.bps) {
650 av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "
651 "supported\n");
652 return AVERROR_INVALIDDATA;
653 }
654
655
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6045 times.
6061 if (!s->stream_info.bps) {
656 16 s->stream_info.bps = s->avctx->bits_per_raw_sample = fi.bps;
657 16 flac_set_bps(s);
658 }
659
660
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6045 times.
6061 if (!s->stream_info.max_blocksize)
661 16 s->stream_info.max_blocksize = FLAC_MAX_BLOCKSIZE;
662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6061 times.
6061 if (fi.blocksize > s->stream_info.max_blocksize) {
663 av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,
664 s->stream_info.max_blocksize);
665 return AVERROR_INVALIDDATA;
666 }
667 6061 s->blocksize = fi.blocksize;
668
669
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6045 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
6061 if (!s->stream_info.samplerate && !fi.samplerate) {
670 av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"
671 " or frame header\n");
672 return AVERROR_INVALIDDATA;
673 }
674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6061 times.
6061 if (fi.samplerate == 0)
675 fi.samplerate = s->stream_info.samplerate;
676 6061 s->stream_info.samplerate = s->avctx->sample_rate = fi.samplerate;
677
678
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6045 times.
6061 if (!s->got_streaminfo) {
679 16 ret = allocate_buffers(s);
680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0)
681 return ret;
682 16 s->got_streaminfo = 1;
683 16 dump_headers(s->avctx, &s->stream_info);
684 }
685 6061 ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
686 s->stream_info.channels);
687
688 // dump_headers(s->avctx, &s->stream_info);
689
690 /* subframes */
691
2/2
✓ Branch 0 taken 16282 times.
✓ Branch 1 taken 6060 times.
22342 for (i = 0; i < s->stream_info.channels; i++) {
692
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 16281 times.
16282 if ((ret = decode_subframe(s, i)) < 0)
693 1 return ret;
694 }
695
696 6060 align_get_bits(gb);
697
698 /* frame footer */
699 6060 skip_bits(gb, 16); /* data crc */
700
701 6060 return 0;
702 }
703
704 9 static void decorrelate_33bps(int ch_mode, int32_t **decoded, int64_t *decoded_33bps, int len)
705 {
706 int i;
707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ch_mode == FLAC_CHMODE_LEFT_SIDE ) {
708 for (i = 0; i < len; i++)
709 decoded[1][i] = decoded[0][i] - (uint64_t)decoded_33bps[i];
710
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 } else if (ch_mode == FLAC_CHMODE_RIGHT_SIDE ) {
711
2/2
✓ Branch 0 taken 34917 times.
✓ Branch 1 taken 8 times.
34925 for (i = 0; i < len; i++)
712 34917 decoded[0][i] = decoded[1][i] + (uint64_t)decoded_33bps[i];
713
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (ch_mode == FLAC_CHMODE_MID_SIDE ) {
714
2/2
✓ Branch 0 taken 4608 times.
✓ Branch 1 taken 1 times.
4609 for (i = 0; i < len; i++) {
715 4608 uint64_t a = decoded[0][i];
716 4608 int64_t b = decoded_33bps[i];
717 4608 a -= b >> 1;
718 4608 decoded[0][i] = (a + b);
719 4608 decoded[1][i] = a;
720 }
721 }
722 9 }
723
724 6061 static int flac_decode_frame(AVCodecContext *avctx, AVFrame *frame,
725 int *got_frame_ptr, AVPacket *avpkt)
726 {
727 6061 const uint8_t *buf = avpkt->data;
728 6061 int buf_size = avpkt->size;
729 6061 FLACContext *s = avctx->priv_data;
730 6061 int bytes_read = 0;
731 int ret;
732
733 6061 *got_frame_ptr = 0;
734
735
2/4
✓ Branch 0 taken 6061 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6061 times.
6061 if (buf_size > 5 && !memcmp(buf, "\177FLAC", 5)) {
736 av_log(s->avctx, AV_LOG_DEBUG, "skipping flac header packet 1\n");
737 return buf_size;
738 }
739
740
2/4
✓ Branch 0 taken 6061 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6061 times.
6061 if (buf_size > 0 && (*buf & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
741 av_log(s->avctx, AV_LOG_DEBUG, "skipping vorbis comment\n");
742 return buf_size;
743 }
744
745 /* check that there is at least the smallest decodable amount of data.
746 this amount corresponds to the smallest valid FLAC frame possible.
747 FF F8 69 02 00 00 9A 00 00 34 */
748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6061 times.
6061 if (buf_size < FLAC_MIN_FRAME_SIZE)
749 return buf_size;
750
751 /* check for inline header */
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6061 times.
6061 if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
753 if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) {
754 av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
755 return ret;
756 }
757 return get_metadata_size(buf, buf_size);
758 }
759
760 /* decode frame */
761
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6061 times.
6061 if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
762 return ret;
763
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6060 times.
6061 if ((ret = decode_frame(s)) < 0) {
764 1 av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
765 1 return ret;
766 }
767 6060 bytes_read = get_bits_count(&s->gb)/8;
768
769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6060 times.
6060 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_COMPLIANT)) &&
770 av_crc(av_crc_get_table(AV_CRC_16_ANSI),
771 0, buf, bytes_read)) {
772 av_log(s->avctx, AV_LOG_ERROR, "CRC error at PTS %"PRId64"\n", avpkt->pts);
773 if (s->avctx->err_recognition & AV_EF_EXPLODE)
774 return AVERROR_INVALIDDATA;
775 }
776
777 /* get output buffer */
778 6060 frame->nb_samples = s->blocksize;
779
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6060 times.
6060 if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
780 return ret;
781
782
4/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6049 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 2 times.
6060 if (s->stream_info.bps == 32 && s->ch_mode > 0) {
783 9 decorrelate_33bps(s->ch_mode, s->decoded, s->decoded_33bps, s->blocksize);
784 9 s->dsp.decorrelate[0](frame->data, s->decoded, s->stream_info.channels,
785 s->blocksize, s->sample_shift);
786 } else {
787 6051 s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded,
788 s->stream_info.channels,
789 s->blocksize, s->sample_shift);
790 }
791
792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6060 times.
6060 if (bytes_read > buf_size) {
793 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
794 return AVERROR_INVALIDDATA;
795 }
796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6060 times.
6060 if (bytes_read < buf_size) {
797 av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
798 buf_size - bytes_read, buf_size);
799 }
800
801 6060 *got_frame_ptr = 1;
802
803 6060 return bytes_read;
804 }
805
806 126 static av_cold int flac_decode_close(AVCodecContext *avctx)
807 {
808 126 FLACContext *s = avctx->priv_data;
809
810 126 av_freep(&s->decoded_buffer);
811 126 av_freep(&s->decoded_buffer_33bps);
812
813 126 return 0;
814 }
815
816 static const AVOption options[] = {
817 { "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 },
818 { NULL },
819 };
820
821 static const AVClass flac_decoder_class = {
822 .class_name = "FLAC decoder",
823 .item_name = av_default_item_name,
824 .option = options,
825 .version = LIBAVUTIL_VERSION_INT,
826 };
827
828 const FFCodec ff_flac_decoder = {
829 .p.name = "flac",
830 CODEC_LONG_NAME("FLAC (Free Lossless Audio Codec)"),
831 .p.type = AVMEDIA_TYPE_AUDIO,
832 .p.id = AV_CODEC_ID_FLAC,
833 .priv_data_size = sizeof(FLACContext),
834 .init = flac_decode_init,
835 .close = flac_decode_close,
836 FF_CODEC_DECODE_CB(flac_decode_frame),
837 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
838 AV_CODEC_CAP_DR1 |
839 AV_CODEC_CAP_FRAME_THREADS,
840 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
841 AV_SAMPLE_FMT_S16P,
842 AV_SAMPLE_FMT_S32,
843 AV_SAMPLE_FMT_S32P,
844 AV_SAMPLE_FMT_NONE },
845 .p.priv_class = &flac_decoder_class,
846 };
847