../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
299 / 426
70.2%
Branches:
188 / 342
55.0%
Functions:
18 / 22
81.8%

libavcodec/flacdec.c
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/mem.h"
39 #include "libavutil/opt.h"
40 #include "avcodec.h"
41 #include "codec_internal.h"
42 #include "get_bits.h"
43 #include "golomb.h"
44 #include "flac.h"
45 #include "flacdsp.h"
46 #include "flac_parse.h"
47 #include "thread.h"
48 #include "unary.h"
49
50
51 typedef struct FLACContext {
52 AVClass *class;
53 FLACStreaminfo stream_info;
54
55 AVCodecContext *avctx; ///< parent AVCodecContext
56 GetBitContext gb; ///< GetBitContext initialized to start at the current frame
57
58 int blocksize; ///< number of samples in the current frame
59 int sample_shift; ///< shift required to make output samples 16-bit or 32-bit
60 int ch_mode; ///< channel decorrelation type in the current frame
61 int got_streaminfo; ///< indicates if the STREAMINFO has been read
62 unsigned streaminfo_serial; ///< counts the STREAMINFOs taken from packet side data
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 377 static void flac_set_bps(FLACContext *s)
78 {
79 377 enum AVSampleFormat req = s->avctx->request_sample_fmt;
80 377 int need32 = s->stream_info.bps > 16;
81 377 int want32 = av_get_bytes_per_sample(req) > 2;
82 377 int planar = av_sample_fmt_is_planar(req);
83
84
3/4
✓ Branch 4 → 5 taken 349 times.
✓ Branch 4 → 6 taken 28 times.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 10 taken 349 times.
377 if (need32 || want32) {
85
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 28 times.
28 if (planar)
86 ✗ s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
87 else
88 28 s->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
89 28 s->sample_shift = 32 - s->stream_info.bps;
90 } else {
91
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 349 times.
349 if (planar)
92 ✗ s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
93 else
94 349 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
95 349 s->sample_shift = 16 - s->stream_info.bps;
96 }
97 377 }
98
99 /**
100 * Set the decoder up for s->stream_info, which it cannot decode with until the
101 * buffers are allocated.
102 */
103 361 static int setup_streaminfo(FLACContext *s)
104 {
105 int ret;
106
107 361 s->got_streaminfo = 0;
108 361 ret = allocate_buffers(s);
109
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 361 times.
361 if (ret < 0)
110 ✗ return ret;
111 361 flac_set_bps(s);
112 361 ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
113 s->stream_info.channels);
114 361 s->got_streaminfo = 1;
115
116 361 return 0;
117 }
118
119 359 static int apply_streaminfo(FLACContext *s, const uint8_t *streaminfo)
120 {
121 FLACStreaminfo stream_info;
122 359 int ret = ff_flac_parse_streaminfo(s->avctx, &stream_info, streaminfo);
123
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 359 times.
359 if (ret < 0)
124 ✗ return ret;
125 359 s->stream_info = stream_info;
126
127 359 return setup_streaminfo(s);
128 }
129
130 367 static av_cold int flac_decode_init(AVCodecContext *avctx)
131 {
132 uint8_t *streaminfo;
133 367 FLACContext *s = avctx->priv_data;
134 367 s->avctx = avctx;
135
136 /* for now, the raw FLAC header is allowed to be passed to the decoder as
137 frame data instead of extradata. */
138
2/2
✓ Branch 2 → 3 taken 16 times.
✓ Branch 2 → 4 taken 351 times.
367 if (!avctx->extradata)
139 16 return 0;
140
141
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 351 times.
351 if (!ff_flac_is_extradata_valid(avctx, &streaminfo))
142 ✗ return AVERROR_INVALIDDATA;
143
144 /* initialize based on the demuxer-supplied streamdata header */
145 351 return apply_streaminfo(s, streaminfo);
146 }
147
148 16 static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
149 {
150 16 av_log(avctx, AV_LOG_DEBUG, " Max Blocksize: %d\n", s->max_blocksize);
151 16 av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize);
152 16 av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
153 16 av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
154 16 av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
155 16 }
156
157 377 static int allocate_buffers(FLACContext *s)
158 {
159 int buf_size;
160 int ret;
161
162
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 377 times.
377 av_assert0(s->stream_info.max_blocksize);
163
164 377 buf_size = av_samples_get_buffer_size(NULL, s->stream_info.channels,
165 s->stream_info.max_blocksize,
166 AV_SAMPLE_FMT_S32P, 0);
167
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 377 times.
377 if (buf_size < 0)
168 ✗ return buf_size;
169
170 377 av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size);
171
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 377 times.
377 if (!s->decoded_buffer)
172 ✗ return AVERROR(ENOMEM);
173
174 377 ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL,
175 377 s->decoded_buffer,
176 s->stream_info.channels,
177 s->stream_info.max_blocksize,
178 AV_SAMPLE_FMT_S32P, 0);
179
5/6
✓ Branch 12 → 13 taken 377 times.
✗ Branch 12 → 22 not taken.
✓ Branch 13 → 14 taken 5 times.
✓ Branch 13 → 22 taken 372 times.
✓ Branch 14 → 15 taken 2 times.
✓ Branch 14 → 22 taken 3 times.
377 if (ret >= 0 && s->stream_info.bps == 32 && s->stream_info.channels == 2) {
180 2 buf_size = av_samples_get_buffer_size(NULL, 1,
181 s->stream_info.max_blocksize,
182 AV_SAMPLE_FMT_S64P, 0);
183
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 2 times.
2 if (buf_size < 0)
184 ✗ return buf_size;
185
186 2 av_fast_malloc(&s->decoded_buffer_33bps, &s->decoded_buffer_size_33bps, buf_size);
187
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 2 times.
2 if (!s->decoded_buffer_33bps)
188 ✗ return AVERROR(ENOMEM);
189
190 2 ret = av_samples_fill_arrays((uint8_t **)&s->decoded_33bps, NULL,
191 2 s->decoded_buffer_33bps,
192 1,
193 s->stream_info.max_blocksize,
194 AV_SAMPLE_FMT_S64P, 0);
195
196 }
197 377 return ret < 0 ? ret : 0;
198 }
199
200 /**
201 * Parse the STREAMINFO from an inline header.
202 * @param s the flac decoding context
203 * @param buf input buffer, starting with the "fLaC" marker
204 * @param buf_size buffer size
205 * @return non-zero if metadata is invalid
206 */
207 ✗ static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
208 {
209 int metadata_type, metadata_size;
210
211
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (buf_size < FLAC_STREAMINFO_SIZE+8) {
212 /* need more data */
213 ✗ return 0;
214 }
215 ✗ flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size);
216
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO ||
217
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ metadata_size != FLAC_STREAMINFO_SIZE) {
218 ✗ return AVERROR_INVALIDDATA;
219 }
220 ✗ return apply_streaminfo(s, &buf[8]);
221 }
222
223 /**
224 * Determine the size of an inline header.
225 * @param buf input buffer, starting with the "fLaC" marker
226 * @param buf_size buffer size
227 * @return number of bytes in the header, or 0 if more data is needed
228 */
229 ✗ static int get_metadata_size(const uint8_t *buf, int buf_size)
230 {
231 int metadata_last, metadata_size;
232 ✗ const uint8_t *buf_end = buf + buf_size;
233
234 ✗ buf += 4;
235 do {
236
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (buf_end - buf < 4)
237 ✗ return AVERROR_INVALIDDATA;
238 ✗ flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size);
239 ✗ buf += 4;
240
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (buf_end - buf < metadata_size) {
241 /* need more data in order to read the complete header */
242 ✗ return AVERROR_INVALIDDATA;
243 }
244 ✗ buf += metadata_size;
245
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ } while (!metadata_last);
246
247 ✗ return buf_size - (buf_end - buf);
248 }
249
250 10968 static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
251 {
252 10968 GetBitContext gb = s->gb;
253 int i, tmp, partition, method_type, rice_order;
254 int rice_bits, rice_esc;
255 int samples;
256
257 10968 method_type = get_bits(&gb, 2);
258 10968 rice_order = get_bits(&gb, 4);
259
260 10968 samples = s->blocksize >> rice_order;
261 10968 rice_bits = 4 + method_type;
262 10968 rice_esc = (1 << rice_bits) - 1;
263
264 10968 decoded += pred_order;
265 10968 i = pred_order;
266
267
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 7 taken 10968 times.
10968 if (method_type > 1) {
268 ✗ av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
269 method_type);
270 ✗ return AVERROR_INVALIDDATA;
271 }
272
273
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 10968 times.
10968 if (samples << rice_order != s->blocksize) {
274 ✗ av_log(s->avctx, AV_LOG_ERROR, "invalid rice order: %i blocksize %i\n",
275 rice_order, s->blocksize);
276 ✗ return AVERROR_INVALIDDATA;
277 }
278
279
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 13 taken 10968 times.
10968 if (pred_order > samples) {
280 ✗ av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
281 pred_order, samples);
282 ✗ return AVERROR_INVALIDDATA;
283 }
284
285
2/2
✓ Branch 32 → 14 taken 139004 times.
✓ Branch 32 → 33 taken 10967 times.
149971 for (partition = 0; partition < (1 << rice_order); partition++) {
286 139004 tmp = get_bits(&gb, rice_bits);
287
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 21 taken 139004 times.
139004 if (tmp == rice_esc) {
288 ✗ tmp = get_bits(&gb, 5);
289
0/2
✗ Branch 20 → 18 not taken.
✗ Branch 20 → 31 not taken.
✗ for (; i < samples; i++)
290 ✗ *decoded++ = get_sbits_long(&gb, tmp);
291 } else {
292
2/2
✓ Branch 21 → 22 taken 132696 times.
✓ Branch 21 → 23 taken 6308 times.
139004 int real_limit = (tmp > 1) ? (INT_MAX >> (tmp - 1)) + 2 : INT_MAX;
293
2/2
✓ Branch 30 → 25 taken 58920289 times.
✓ Branch 30 → 31 taken 139003 times.
59059292 for (; i < samples; i++) {
294 58920289 int v = get_sr_golomb_flac(&gb, tmp, real_limit, 1);
295
2/2
✓ Branch 26 → 27 taken 1 time.
✓ Branch 26 → 29 taken 58920288 times.
58920289 if (v == 0x80000000){
296 1 av_log(s->avctx, AV_LOG_ERROR, "invalid residual\n");
297 1 return AVERROR_INVALIDDATA;
298 }
299
300 58920288 *decoded++ = v;
301 }
302 }
303 139003 i= 0;
304 }
305
306 10967 s->gb = gb;
307
308 10967 return 0;
309 }
310
311 463 static int decode_subframe_fixed(FLACContext *s, int32_t *decoded,
312 int pred_order, int bps)
313 {
314 463 const int blocksize = s->blocksize;
315 463 unsigned av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d);
316 int i;
317 int ret;
318
319 /* warm up samples */
320
2/2
✓ Branch 5 → 3 taken 1102 times.
✓ Branch 5 → 6 taken 463 times.
1565 for (i = 0; i < pred_order; i++) {
321 1102 decoded[i] = get_sbits_long(&s->gb, bps);
322 }
323
324
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 463 times.
463 if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
325 ✗ return ret;
326
327
2/2
✓ Branch 9 → 10 taken 437 times.
✓ Branch 9 → 11 taken 26 times.
463 if (pred_order > 0)
328 437 a = decoded[pred_order-1];
329
2/2
✓ Branch 11 → 12 taken 311 times.
✓ Branch 11 → 13 taken 152 times.
463 if (pred_order > 1)
330 311 b = a - decoded[pred_order-2];
331
2/2
✓ Branch 13 → 14 taken 183 times.
✓ Branch 13 → 15 taken 280 times.
463 if (pred_order > 2)
332 183 c = b - decoded[pred_order-2] + decoded[pred_order-3];
333
2/2
✓ Branch 15 → 16 taken 171 times.
✓ Branch 15 → 17 taken 292 times.
463 if (pred_order > 3)
334 171 d = c - decoded[pred_order-2] + 2U*decoded[pred_order-3] - decoded[pred_order-4];
335
336
5/6
✓ Branch 17 → 18 taken 26 times.
✓ Branch 17 → 19 taken 126 times.
✓ Branch 17 → 23 taken 128 times.
✓ Branch 17 → 27 taken 12 times.
✓ Branch 17 → 31 taken 171 times.
✗ Branch 17 → 35 not taken.
463 switch (pred_order) {
337 26 case 0:
338 26 break;
339 126 case 1:
340
2/2
✓ Branch 21 → 20 taken 515970 times.
✓ Branch 21 → 22 taken 126 times.
516096 for (i = pred_order; i < blocksize; i++)
341 515970 decoded[i] = a += decoded[i];
342 126 break;
343 128 case 2:
344
2/2
✓ Branch 25 → 24 taken 518156 times.
✓ Branch 25 → 26 taken 128 times.
518284 for (i = pred_order; i < blocksize; i++)
345 518156 decoded[i] = a += b += decoded[i];
346 128 break;
347 12 case 3:
348
2/2
✓ Branch 29 → 28 taken 49116 times.
✓ Branch 29 → 30 taken 12 times.
49128 for (i = pred_order; i < blocksize; i++)
349 49116 decoded[i] = a += b += c += decoded[i];
350 12 break;
351 171 case 4:
352
2/2
✓ Branch 33 → 32 taken 693172 times.
✓ Branch 33 → 34 taken 171 times.
693343 for (i = pred_order; i < blocksize; i++)
353 693172 decoded[i] = a += b += c += d += decoded[i];
354 171 break;
355 ✗ default:
356 ✗ av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
357 ✗ return AVERROR_INVALIDDATA;
358 }
359
360 463 return 0;
361 }
362
363 #define DECODER_SUBFRAME_FIXED_WIDE(residual) { \
364 const int blocksize = s->blocksize; \
365 int ret; \
366 \
367 if ((ret = decode_residuals(s, residual, pred_order)) < 0) \
368 return ret; \
369 \
370 switch (pred_order) { \
371 case 0: \
372 for (int i = pred_order; i < blocksize; i++) \
373 decoded[i] = residual[i]; \
374 break; \
375 case 1: \
376 for (int i = pred_order; i < blocksize; i++) \
377 decoded[i] = (uint64_t)residual[i] + (uint64_t)decoded[i-1];\
378 break; \
379 case 2: \
380 for (int i = pred_order; i < blocksize; i++) \
381 decoded[i] = (uint64_t)residual[i] + 2*(uint64_t)decoded[i-1] - (uint64_t)decoded[i-2]; \
382 break; \
383 case 3: \
384 for (int i = pred_order; i < blocksize; i++) \
385 decoded[i] = (uint64_t)residual[i] + 3*(uint64_t)decoded[i-1] - 3*(uint64_t)decoded[i-2] + (uint64_t)decoded[i-3]; \
386 break; \
387 case 4: \
388 for (int i = pred_order; i < blocksize; i++) \
389 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]; \
390 break; \
391 default: \
392 av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order); \
393 return AVERROR_INVALIDDATA; \
394 } \
395 return 0; \
396 }
397
398 ✗ static int decode_subframe_fixed_wide(FLACContext *s, int32_t *decoded,
399 int pred_order, int bps)
400 {
401 /* warm up samples */
402
0/2
✗ Branch 5 → 3 not taken.
✗ Branch 5 → 6 not taken.
✗ for (int i = 0; i < pred_order; i++) {
403 ✗ decoded[i] = get_sbits_long(&s->gb, bps);
404 }
405
0/18
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 14 not taken.
✗ Branch 9 → 18 not taken.
✗ Branch 9 → 22 not taken.
✗ Branch 9 → 26 not taken.
✗ Branch 9 → 30 not taken.
✗ Branch 12 → 11 not taken.
✗ Branch 12 → 13 not taken.
✗ Branch 16 → 15 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 20 → 19 not taken.
✗ Branch 20 → 21 not taken.
✗ Branch 24 → 23 not taken.
✗ Branch 24 → 25 not taken.
✗ Branch 28 → 27 not taken.
✗ Branch 28 → 29 not taken.
✗ DECODER_SUBFRAME_FIXED_WIDE(decoded);
406 }
407
408
409 ✗ static int decode_subframe_fixed_33bps(FLACContext *s, int64_t *decoded,
410 int32_t *residual, int pred_order)
411 {
412 /* warm up samples */ \
413
0/2
✗ Branch 5 → 3 not taken.
✗ Branch 5 → 6 not taken.
✗ for (int i = 0; i < pred_order; i++) { \
414 ✗ decoded[i] = get_sbits64(&s->gb, 33); \
415 } \
416
0/18
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 14 not taken.
✗ Branch 9 → 18 not taken.
✗ Branch 9 → 22 not taken.
✗ Branch 9 → 26 not taken.
✗ Branch 9 → 30 not taken.
✗ Branch 12 → 11 not taken.
✗ Branch 12 → 13 not taken.
✗ Branch 16 → 15 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 20 → 19 not taken.
✗ Branch 20 → 21 not taken.
✗ Branch 24 → 23 not taken.
✗ Branch 24 → 25 not taken.
✗ Branch 28 → 27 not taken.
✗ Branch 28 → 29 not taken.
✗ DECODER_SUBFRAME_FIXED_WIDE(residual);
417 }
418
419 2830 static void lpc_analyze_remodulate(SUINT32 *decoded, const int coeffs[32],
420 int order, int qlevel, int len, int bps)
421 {
422 int i, j;
423 2830 int ebps = 1 << (bps-1);
424 2830 unsigned sigma = 0;
425
426
2/2
✓ Branch 4 → 3 taken 9551136 times.
✓ Branch 4 → 5 taken 2830 times.
9553966 for (i = order; i < len; i++)
427 9551136 sigma |= decoded[i] + ebps;
428
429
1/2
✓ Branch 5 → 6 taken 2830 times.
✗ Branch 5 → 7 not taken.
2830 if (sigma < 2*ebps)
430 2830 return;
431
432
0/2
✗ Branch 12 → 8 not taken.
✗ Branch 12 → 13 not taken.
✗ for (i = len - 1; i >= order; i--) {
433 ✗ int64_t p = 0;
434
0/2
✗ Branch 10 → 9 not taken.
✗ Branch 10 → 11 not taken.
✗ for (j = 0; j < order; j++)
435 ✗ p += coeffs[j] * (int64_t)(int32_t)decoded[i-order+j];
436 ✗ decoded[i] -= p >> qlevel;
437 }
438
0/2
✗ Branch 18 → 14 not taken.
✗ Branch 18 → 19 not taken.
✗ for (i = order; i < len; i++, decoded++) {
439 ✗ int32_t p = 0;
440
0/2
✗ Branch 16 → 15 not taken.
✗ Branch 16 → 17 not taken.
✗ for (j = 0; j < order; j++)
441 ✗ p += coeffs[j] * (uint32_t)decoded[j];
442 ✗ decoded[j] += p >> qlevel;
443 }
444 }
445
446 10504 static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order,
447 int bps)
448 {
449 int i, ret;
450 int coeff_prec, qlevel;
451 int coeffs[32];
452
453 /* warm up samples */
454
2/2
✓ Branch 5 → 3 taken 48217 times.
✓ Branch 5 → 6 taken 10504 times.
58721 for (i = 0; i < pred_order; i++) {
455 48217 decoded[i] = get_sbits_long(&s->gb, bps);
456 }
457
458 10504 coeff_prec = get_bits(&s->gb, 4) + 1;
459
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 10504 times.
10504 if (coeff_prec == 16) {
460 ✗ av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
461 ✗ return AVERROR_INVALIDDATA;
462 }
463 10504 qlevel = get_sbits(&s->gb, 5);
464
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 10504 times.
10504 if (qlevel < 0) {
465 ✗ av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
466 qlevel);
467 ✗ return AVERROR_INVALIDDATA;
468 }
469
470
2/2
✓ Branch 17 → 15 taken 48217 times.
✓ Branch 17 → 18 taken 10504 times.
58721 for (i = 0; i < pred_order; i++) {
471 48217 coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
472 }
473
474
2/2
✓ Branch 19 → 20 taken 1 time.
✓ Branch 19 → 21 taken 10503 times.
10504 if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
475 1 return ret;
476
477
1/4
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 10503 times.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 26 not taken.
10503 if ( ( s->buggy_lpc && s->stream_info.bps <= 16)
478
3/4
✓ Branch 23 → 24 taken 10503 times.
✗ Branch 23 → 27 not taken.
✓ Branch 24 → 25 taken 7759 times.
✓ Branch 24 → 27 taken 2744 times.
10503 || ( !s->buggy_lpc && bps <= 16
479
2/2
✓ Branch 25 → 26 taken 6181 times.
✓ Branch 25 → 27 taken 1578 times.
7759 && bps + coeff_prec + av_log2(pred_order) <= 32)) {
480 6181 s->dsp.lpc16(decoded, coeffs, pred_order, qlevel, s->blocksize);
481 } else {
482 4322 s->dsp.lpc32(decoded, coeffs, pred_order, qlevel, s->blocksize);
483
2/2
✓ Branch 28 → 29 taken 2830 times.
✓ Branch 28 → 30 taken 1492 times.
4322 if (s->stream_info.bps <= 16)
484 2830 lpc_analyze_remodulate(decoded, coeffs, pred_order, qlevel, s->blocksize, bps);
485 }
486
487 10503 return 0;
488 }
489
490 1 static int decode_subframe_lpc_33bps(FLACContext *s, int64_t *decoded,
491 int32_t *residual, int pred_order)
492 {
493 int i, ret;
494 int coeff_prec, qlevel;
495 int coeffs[32];
496
497 /* warm up samples */
498
2/2
✓ Branch 5 → 3 taken 8 times.
✓ Branch 5 → 6 taken 1 time.
9 for (i = 0; i < pred_order; i++) {
499 8 decoded[i] = get_sbits64(&s->gb, 33);
500 }
501
502 1 coeff_prec = get_bits(&s->gb, 4) + 1;
503
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 1 time.
1 if (coeff_prec == 16) {
504 ✗ av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
505 ✗ return AVERROR_INVALIDDATA;
506 }
507 1 qlevel = get_sbits(&s->gb, 5);
508
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 1 time.
1 if (qlevel < 0) {
509 ✗ av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
510 qlevel);
511 ✗ return AVERROR_INVALIDDATA;
512 }
513
514
2/2
✓ Branch 17 → 15 taken 8 times.
✓ Branch 17 → 18 taken 1 time.
9 for (i = 0; i < pred_order; i++) {
515 8 coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
516 }
517
518
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 1 time.
1 if ((ret = decode_residuals(s, residual, pred_order)) < 0)
519 ✗ return ret;
520
521 1 s->dsp.lpc33(decoded, residual, coeffs, pred_order, qlevel, s->blocksize);
522
523 1 return 0;
524 }
525
526 16668 static inline int decode_subframe(FLACContext *s, int channel)
527 {
528 16668 int32_t *decoded = s->decoded[channel];
529 16668 int type, wasted = 0;
530 16668 int bps = s->stream_info.bps;
531 int i, ret;
532
533
2/2
✓ Branch 2 → 3 taken 6626 times.
✓ Branch 2 → 5 taken 10042 times.
16668 if (channel == 0) {
534
2/2
✓ Branch 3 → 4 taken 616 times.
✓ Branch 3 → 8 taken 6010 times.
6626 if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)
535 616 bps++;
536 } else {
537
4/4
✓ Branch 5 → 6 taken 8582 times.
✓ Branch 5 → 7 taken 1460 times.
✓ Branch 6 → 7 taken 912 times.
✓ Branch 6 → 8 taken 7670 times.
10042 if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE)
538 2372 bps++;
539 }
540
541
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 16668 times.
16668 if (get_bits1(&s->gb)) {
542 ✗ av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
543 ✗ return AVERROR_INVALIDDATA;
544 }
545 16668 type = get_bits(&s->gb, 6);
546
547
2/2
✓ Branch 14 → 15 taken 5292 times.
✓ Branch 14 → 27 taken 11376 times.
16668 if (get_bits1(&s->gb)) {
548 5292 int left = get_bits_left(&s->gb);
549
2/4
✓ Branch 16 → 17 taken 5292 times.
✗ Branch 16 → 22 not taken.
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 20 taken 5292 times.
5292 if ( left <= 0 ||
550
1/4
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 22 not taken.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 24 taken 5292 times.
5292 (left < bps && !show_bits_long(&s->gb, left)) ||
551 5292 !show_bits_long(&s->gb, bps-1)) {
552 ✗ av_log(s->avctx, AV_LOG_ERROR,
553 "Invalid number of wasted bits > available bits (%d) - left=%d\n",
554 bps, left);
555 ✗ return AVERROR_INVALIDDATA;
556 }
557 5292 wasted = 1 + get_unary(&s->gb, 1, get_bits_left(&s->gb));
558 5292 bps -= wasted;
559 }
560
561 //FIXME use av_log2 for types
562
2/2
✓ Branch 27 → 28 taken 5699 times.
✓ Branch 27 → 37 taken 10969 times.
16668 if (type == 0) {
563
1/2
✓ Branch 28 → 29 taken 5699 times.
✗ Branch 28 → 33 not taken.
5699 if (bps < 33) {
564 5699 int32_t tmp = get_sbits_long(&s->gb, bps);
565
2/2
✓ Branch 32 → 31 taken 36054656 times.
✓ Branch 32 → 71 taken 5699 times.
36060355 for (i = 0; i < s->blocksize; i++)
566 36054656 decoded[i] = tmp;
567 } else {
568 ✗ int64_t tmp = get_sbits64(&s->gb, 33);
569
0/2
✗ Branch 36 → 35 not taken.
✗ Branch 36 → 71 not taken.
✗ for (i = 0; i < s->blocksize; i++)
570 ✗ s->decoded_33bps[i] = tmp;
571 }
572
2/2
✓ Branch 37 → 38 taken 1 time.
✓ Branch 37 → 47 taken 10968 times.
10969 } else if (type == 1) {
573
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 43 taken 1 time.
1 if (bps < 33) {
574
0/2
✗ Branch 42 → 40 not taken.
✗ Branch 42 → 71 not taken.
✗ for (i = 0; i < s->blocksize; i++)
575 ✗ decoded[i] = get_sbits_long(&s->gb, bps);
576 } else {
577
2/2
✓ Branch 46 → 44 taken 4096 times.
✓ Branch 46 → 71 taken 1 time.
4097 for (i = 0; i < s->blocksize; i++)
578 4096 s->decoded_33bps[i] = get_sbits64(&s->gb, 33);
579 }
580
3/4
✓ Branch 47 → 48 taken 10968 times.
✗ Branch 47 → 61 not taken.
✓ Branch 48 → 49 taken 463 times.
✓ Branch 48 → 61 taken 10505 times.
11431 } else if ((type >= 8) && (type <= 12)) {
581 463 int order = type & ~0x8;
582
1/2
✓ Branch 49 → 50 taken 463 times.
✗ Branch 49 → 57 not taken.
463 if (bps < 33) {
583
1/2
✓ Branch 50 → 51 taken 463 times.
✗ Branch 50 → 54 not taken.
463 if (bps + order <= 32) {
584
1/2
✗ Branch 52 → 53 not taken.
✓ Branch 52 → 60 taken 463 times.
463 if ((ret = decode_subframe_fixed(s, decoded, order, bps)) < 0)
585 ✗ return ret;
586 } else {
587
0/2
✗ Branch 55 → 56 not taken.
✗ Branch 55 → 60 not taken.
✗ if ((ret = decode_subframe_fixed_wide(s, decoded, order, bps)) < 0)
588 ✗ return ret;
589 }
590 } else {
591
0/2
✗ Branch 58 → 59 not taken.
✗ Branch 58 → 60 not taken.
✗ if ((ret = decode_subframe_fixed_33bps(s, s->decoded_33bps, decoded, order)) < 0)
592 ✗ return ret;
593 }
594
1/2
✓ Branch 61 → 62 taken 10505 times.
✗ Branch 61 → 69 not taken.
10505 } else if (type >= 32) {
595
2/2
✓ Branch 62 → 63 taken 10504 times.
✓ Branch 62 → 66 taken 1 time.
10505 if (bps < 33) {
596
2/2
✓ Branch 64 → 65 taken 1 time.
✓ Branch 64 → 71 taken 10503 times.
10504 if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0)
597 1 return ret;
598 } else {
599
1/2
✗ Branch 67 → 68 not taken.
✓ Branch 67 → 71 taken 1 time.
1 if ((ret = decode_subframe_lpc_33bps(s, s->decoded_33bps, decoded, (type & ~0x20)+1)) < 0)
600 ✗ return ret;
601 }
602 } else {
603 ✗ av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
604 ✗ return AVERROR_INVALIDDATA;
605 }
606
607
2/2
✓ Branch 71 → 72 taken 5292 times.
✓ Branch 71 → 76 taken 11375 times.
16667 if (wasted) {
608
2/2
✓ Branch 72 → 73 taken 7 times.
✓ Branch 72 → 74 taken 5285 times.
5292 if (wasted+bps == 33) {
609 7 s->dsp.wasted33(s->decoded_33bps, decoded, wasted, s->blocksize);
610
1/2
✓ Branch 74 → 75 taken 5285 times.
✗ Branch 74 → 76 not taken.
5285 } else if (wasted < 32) {
611 5285 s->dsp.wasted32(decoded, wasted, s->blocksize);
612 }
613 }
614
615 16667 return 0;
616 }
617
618 6683 static int decode_frame_header(FLACContext *s)
619 {
620 int ret;
621 6683 GetBitContext *gb = &s->gb;
622 FLACFrameInfo fi;
623
624
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 6683 times.
6683 if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) {
625 ✗ av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
626 ✗ return ret;
627 }
628
629
2/2
✓ Branch 6 → 7 taken 6667 times.
✓ Branch 6 → 13 taken 16 times.
6683 if ( s->stream_info.channels
630
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 13 taken 6667 times.
6667 && fi.channels != s->stream_info.channels
631
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 13 not taken.
✗ && s->got_streaminfo) {
632 ✗ s->stream_info.channels = fi.channels;
633 ✗ ff_flac_set_channel_layout(s->avctx, fi.channels);
634 ✗ ret = allocate_buffers(s);
635
0/2
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ if (ret < 0)
636 ✗ return ret;
637 }
638 6683 s->stream_info.channels = fi.channels;
639 6683 ff_flac_set_channel_layout(s->avctx, fi.channels);
640 6683 s->ch_mode = fi.ch_mode;
641
642
3/4
✓ Branch 14 → 15 taken 16 times.
✓ Branch 14 → 18 taken 6667 times.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 16 times.
6683 if (!s->stream_info.bps && !fi.bps) {
643 ✗ av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n");
644 ✗ return AVERROR_INVALIDDATA;
645 }
646
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 6683 times.
6683 if (!fi.bps) {
647 ✗ fi.bps = s->stream_info.bps;
648
4/4
✓ Branch 20 → 21 taken 6667 times.
✓ Branch 20 → 24 taken 16 times.
✓ Branch 21 → 22 taken 44 times.
✓ Branch 21 → 24 taken 6623 times.
6683 } else if (s->stream_info.bps && fi.bps != s->stream_info.bps) {
649 44 av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "
650 "supported\n");
651 44 return AVERROR_INVALIDDATA;
652 }
653
654
2/2
✓ Branch 24 → 25 taken 16 times.
✓ Branch 24 → 26 taken 6623 times.
6639 if (!s->stream_info.bps) {
655 16 s->stream_info.bps = s->avctx->bits_per_raw_sample = fi.bps;
656 16 flac_set_bps(s);
657 }
658
659
2/2
✓ Branch 26 → 27 taken 16 times.
✓ Branch 26 → 28 taken 6623 times.
6639 if (!s->stream_info.max_blocksize)
660 16 s->stream_info.max_blocksize = FLAC_MAX_BLOCKSIZE;
661
2/2
✓ Branch 28 → 29 taken 13 times.
✓ Branch 28 → 31 taken 6626 times.
6639 if (fi.blocksize > s->stream_info.max_blocksize) {
662 13 av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,
663 s->stream_info.max_blocksize);
664 13 return AVERROR_INVALIDDATA;
665 }
666 6626 s->blocksize = fi.blocksize;
667
668
3/4
✓ Branch 31 → 32 taken 16 times.
✓ Branch 31 → 35 taken 6610 times.
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 35 taken 16 times.
6626 if (!s->stream_info.samplerate && !fi.samplerate) {
669 ✗ av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"
670 " or frame header\n");
671 ✗ return AVERROR_INVALIDDATA;
672 }
673
1/2
✗ Branch 35 → 36 not taken.
✓ Branch 35 → 37 taken 6626 times.
6626 if (fi.samplerate == 0)
674 ✗ fi.samplerate = s->stream_info.samplerate;
675 6626 s->stream_info.samplerate = s->avctx->sample_rate = fi.samplerate;
676
677
2/2
✓ Branch 37 → 38 taken 16 times.
✓ Branch 37 → 42 taken 6610 times.
6626 if (!s->got_streaminfo) {
678 16 ret = allocate_buffers(s);
679
1/2
✗ Branch 39 → 40 not taken.
✓ Branch 39 → 41 taken 16 times.
16 if (ret < 0)
680 ✗ return ret;
681 16 s->got_streaminfo = 1;
682 16 dump_headers(s->avctx, &s->stream_info);
683 }
684 6626 ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
685 s->stream_info.channels);
686
687 // dump_headers(s->avctx, &s->stream_info);
688
689 6626 return 0;
690 }
691
692 6626 static int decode_subframes(FLACContext *s)
693 {
694 int i, ret;
695 6626 GetBitContext *gb = &s->gb;
696
697 /* subframes */
698
2/2
✓ Branch 7 → 3 taken 16668 times.
✓ Branch 7 → 8 taken 6625 times.
23293 for (i = 0; i < s->stream_info.channels; i++) {
699
2/2
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 16667 times.
16668 if ((ret = decode_subframe(s, i)) < 0)
700 1 return ret;
701 }
702
703 6625 align_get_bits(gb);
704
705 /* frame footer */
706 6625 skip_bits(gb, 16); /* data crc */
707
708 6625 return 0;
709 }
710
711 9 static void decorrelate_33bps(int ch_mode, int32_t **decoded, int64_t *decoded_33bps, int len)
712 {
713 int i;
714
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 6 taken 9 times.
9 if (ch_mode == FLAC_CHMODE_LEFT_SIDE ) {
715
0/2
✗ Branch 5 → 4 not taken.
✗ Branch 5 → 14 not taken.
✗ for (i = 0; i < len; i++)
716 ✗ decoded[1][i] = decoded[0][i] - (uint64_t)decoded_33bps[i];
717
2/2
✓ Branch 6 → 7 taken 8 times.
✓ Branch 6 → 10 taken 1 time.
9 } else if (ch_mode == FLAC_CHMODE_RIGHT_SIDE ) {
718
2/2
✓ Branch 9 → 8 taken 31845 times.
✓ Branch 9 → 14 taken 8 times.
31853 for (i = 0; i < len; i++)
719 31845 decoded[0][i] = decoded[1][i] + (uint64_t)decoded_33bps[i];
720
1/2
✓ Branch 10 → 11 taken 1 time.
✗ Branch 10 → 14 not taken.
1 } else if (ch_mode == FLAC_CHMODE_MID_SIDE ) {
721
2/2
✓ Branch 13 → 12 taken 4096 times.
✓ Branch 13 → 14 taken 1 time.
4097 for (i = 0; i < len; i++) {
722 4096 uint64_t a = decoded[0][i];
723 4096 int64_t b = decoded_33bps[i];
724 4096 a -= b >> 1;
725 4096 decoded[0][i] = (a + b);
726 4096 decoded[1][i] = a;
727 }
728 }
729 9 }
730
731 6683 static int flac_decode_frame(AVCodecContext *avctx, AVFrame *frame,
732 int *got_frame_ptr, AVPacket *avpkt)
733 {
734 6683 const uint8_t *buf = avpkt->data;
735 6683 int buf_size = avpkt->size;
736 6683 FLACContext *s = avctx->priv_data;
737 const uint8_t *streaminfo;
738 size_t streaminfo_size;
739 6683 int bytes_read = 0;
740 int ret;
741
742 6683 *got_frame_ptr = 0;
743
744
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 6683 times.
6683 if (!ff_thread_can_start_frame(avctx)) {
745 ✗ av_log(avctx, AV_LOG_ERROR, "frame threads need one FLAC frame per packet\n");
746 ✗ return AVERROR_PATCHWELCOME;
747 }
748
749 6683 streaminfo = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
750 &streaminfo_size);
751
2/2
✓ Branch 7 → 8 taken 8 times.
✓ Branch 7 → 17 taken 6675 times.
6683 if (streaminfo) {
752
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 8 times.
8 if (streaminfo_size >= 8 + FLAC_STREAMINFO_SIZE &&
753
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 11 not taken.
✗ AV_RB32(streaminfo) == MKBETAG('f','L','a','C'))
754 ✗ streaminfo += 8;
755
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 8 times.
8 else if (streaminfo_size != FLAC_STREAMINFO_SIZE)
756 ✗ return AVERROR_INVALIDDATA;
757
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 8 times.
8 if ((ret = apply_streaminfo(s, streaminfo)) < 0)
758 ✗ return ret;
759 8 s->streaminfo_serial++;
760 }
761
762
2/4
✓ Branch 17 → 18 taken 6683 times.
✗ Branch 17 → 21 not taken.
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 21 taken 6683 times.
6683 if (buf_size > 5 && !memcmp(buf, "\177FLAC", 5)) {
763 ✗ av_log(s->avctx, AV_LOG_DEBUG, "skipping flac header packet 1\n");
764 ✗ return buf_size;
765 }
766
767
2/4
✓ Branch 21 → 22 taken 6683 times.
✗ Branch 21 → 25 not taken.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 25 taken 6683 times.
6683 if (buf_size > 0 && (*buf & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
768 ✗ av_log(s->avctx, AV_LOG_DEBUG, "skipping vorbis comment\n");
769 ✗ return buf_size;
770 }
771
772 /* check that there is at least the smallest decodable amount of data.
773 this amount corresponds to the smallest valid FLAC frame possible.
774 FF F8 69 02 00 00 9A 00 00 34 */
775
1/2
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 6683 times.
6683 if (buf_size < FLAC_MIN_FRAME_SIZE)
776 ✗ return buf_size;
777
778 /* check for inline header */
779
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 35 taken 6683 times.
6683 if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
780
0/4
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 33 not taken.
✗ Branch 30 → 31 not taken.
✗ Branch 30 → 33 not taken.
✗ if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) {
781 ✗ av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
782 ✗ return ret;
783 }
784 ✗ return get_metadata_size(buf, buf_size);
785 }
786
787 /* decode frame */
788
1/2
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 38 taken 6683 times.
6683 if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
789 ✗ return ret;
790
2/2
✓ Branch 39 → 40 taken 57 times.
✓ Branch 39 → 42 taken 6626 times.
6683 if ((ret = decode_frame_header(s)) < 0) {
791 57 av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
792 57 return ret;
793 }
794
795 /* get output buffer */
796 6626 frame->nb_samples = s->blocksize;
797
1/2
✗ Branch 43 → 44 not taken.
✓ Branch 43 → 45 taken 6626 times.
6626 if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
798 ✗ return ret;
799
800 6626 ff_thread_finish_setup(avctx);
801
802
2/2
✓ Branch 47 → 48 taken 1 time.
✓ Branch 47 → 50 taken 6625 times.
6626 if ((ret = decode_subframes(s)) < 0) {
803 1 av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
804 1 return ret;
805 }
806 6625 bytes_read = get_bits_count(&s->gb)/8;
807
808
1/2
✗ Branch 51 → 52 not taken.
✓ Branch 51 → 57 taken 6625 times.
6625 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_COMPLIANT)) &&
809
0/2
✗ Branch 53 → 54 not taken.
✗ Branch 53 → 57 not taken.
✗ av_crc(av_crc_get_table(AV_CRC_16_ANSI),
810 0, buf, bytes_read)) {
811 ✗ av_log(s->avctx, AV_LOG_ERROR, "CRC error at PTS %"PRId64"\n", avpkt->pts);
812
0/2
✗ Branch 55 → 56 not taken.
✗ Branch 55 → 57 not taken.
✗ if (s->avctx->err_recognition & AV_EF_EXPLODE)
813 ✗ return AVERROR_INVALIDDATA;
814 }
815
816
4/4
✓ Branch 57 → 58 taken 12 times.
✓ Branch 57 → 61 taken 6613 times.
✓ Branch 58 → 59 taken 9 times.
✓ Branch 58 → 61 taken 3 times.
6625 if (s->stream_info.bps == 32 && s->ch_mode > 0) {
817 9 decorrelate_33bps(s->ch_mode, s->decoded, s->decoded_33bps, s->blocksize);
818 9 s->dsp.decorrelate[0](frame->data, s->decoded, s->stream_info.channels,
819 s->blocksize, s->sample_shift);
820 } else {
821 6616 s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded,
822 s->stream_info.channels,
823 s->blocksize, s->sample_shift);
824 }
825
826
1/2
✗ Branch 62 → 63 not taken.
✓ Branch 62 → 65 taken 6625 times.
6625 if (bytes_read > buf_size) {
827 ✗ av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
828 ✗ return AVERROR_INVALIDDATA;
829 }
830
1/2
✗ Branch 65 → 66 not taken.
✓ Branch 65 → 67 taken 6625 times.
6625 if (bytes_read < buf_size) {
831 ✗ av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
832 buf_size - bytes_read, buf_size);
833 }
834
835 6625 *got_frame_ptr = 1;
836
837 6625 return bytes_read;
838 }
839
840 #if HAVE_THREADS
841 /**
842 * Hand the stream parameters on to the next frame thread, so that a STREAMINFO
843 * taken from packet side data holds for the frames after the one carrying it.
844 */
845 439 static int flac_update_thread_context(AVCodecContext *dst,
846 const AVCodecContext *src)
847 {
848 439 FLACContext *s = dst->priv_data;
849 439 const FLACContext *s1 = src->priv_data;
850
851
3/4
✓ Branch 2 → 3 taken 439 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 437 times.
✓ Branch 3 → 5 taken 2 times.
439 if (s == s1 || s->streaminfo_serial == s1->streaminfo_serial)
852 437 return 0;
853
854 2 s->streaminfo_serial = s1->streaminfo_serial;
855 2 s->stream_info = s1->stream_info;
856
857 2 return setup_streaminfo(s);
858 }
859 #endif
860
861 367 static av_cold int flac_decode_close(AVCodecContext *avctx)
862 {
863 367 FLACContext *s = avctx->priv_data;
864
865 367 av_freep(&s->decoded_buffer);
866 367 av_freep(&s->decoded_buffer_33bps);
867
868 367 return 0;
869 }
870
871 static const AVOption options[] = {
872 { "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 },
873 { NULL },
874 };
875
876 static const AVClass flac_decoder_class = {
877 .class_name = "FLAC decoder",
878 .item_name = av_default_item_name,
879 .option = options,
880 .version = LIBAVUTIL_VERSION_INT,
881 };
882
883 const FFCodec ff_flac_decoder = {
884 .p.name = "flac",
885 CODEC_LONG_NAME("FLAC (Free Lossless Audio Codec)"),
886 .p.type = AVMEDIA_TYPE_AUDIO,
887 .p.id = AV_CODEC_ID_FLAC,
888 .priv_data_size = sizeof(FLACContext),
889 .init = flac_decode_init,
890 .close = flac_decode_close,
891 FF_CODEC_DECODE_CB(flac_decode_frame),
892 UPDATE_THREAD_CONTEXT(flac_update_thread_context),
893 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
894 AV_CODEC_CAP_DR1 |
895 AV_CODEC_CAP_FRAME_THREADS,
896 CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
897 AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P),
898 .p.priv_class = &flac_decoder_class,
899 };
900