FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/flac_parser.c
Date: 2026-06-06 18:10:07
Exec Total Coverage
Lines: 360 422 85.3%
Functions: 21 21 100.0%
Branches: 208 286 72.7%

Line Branch Exec Source
1 /*
2 * FLAC parser
3 * Copyright (c) 2010 Michael Chinen
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 parser
25 *
26 * The FLAC parser buffers input until FLAC_MIN_HEADERS has been found.
27 * Each time it finds and verifies a CRC-8 header it sees which of the
28 * FLAC_MAX_SEQUENTIAL_HEADERS that came before it have a valid CRC-16 footer
29 * that ends at the newly found header.
30 * Headers are scored by FLAC_HEADER_BASE_SCORE plus the max of its crc-verified
31 * children, penalized by changes in sample rate, frame number, etc.
32 * The parser returns the frame with the highest score.
33 **/
34
35 #include "libavutil/attributes.h"
36 #include "libavutil/crc.h"
37 #include "libavutil/mem.h"
38 #include "flac_parse.h"
39 #include "parser_internal.h"
40
41 /** maximum number of adjacent headers that compare CRCs against each other */
42 #define FLAC_MAX_SEQUENTIAL_HEADERS 4
43 /** minimum number of headers buffered and checked before returning frames */
44 #define FLAC_MIN_HEADERS 10
45 /** estimate for average size of a FLAC frame */
46 #define FLAC_AVG_FRAME_SIZE 8192
47
48 /** scoring settings for score_header */
49 #define FLAC_HEADER_BASE_SCORE 10
50 #define FLAC_HEADER_CHANGED_PENALTY 7
51 #define FLAC_HEADER_CRC_FAIL_PENALTY 50
52 #define FLAC_HEADER_NOT_PENALIZED_YET 100000
53 #define FLAC_HEADER_NOT_SCORED_YET -100000
54
55 /** largest possible size of flac header */
56 #define MAX_FRAME_HEADER_SIZE 16
57 #define MAX_FRAME_VERIFY_SIZE (MAX_FRAME_HEADER_SIZE + 1)
58
59 typedef struct FifoBuffer {
60 uint8_t *buffer;
61 uint8_t *end;
62 uint8_t *rptr;
63 uint8_t *wptr;
64 int empty;
65 } FifoBuffer;
66
67 typedef struct FLACHeaderMarker {
68 int offset; /**< byte offset from start of FLACParseContext->buffer */
69 int link_penalty[FLAC_MAX_SEQUENTIAL_HEADERS]; /**< array of local scores
70 between this header and the one at a distance equal
71 array position */
72 int max_score; /**< maximum score found after checking each child that
73 has a valid CRC */
74 FLACFrameInfo fi; /**< decoded frame header info */
75 struct FLACHeaderMarker *next; /**< next CRC-8 verified header that
76 immediately follows this one in
77 the bytestream */
78 struct FLACHeaderMarker *best_child; /**< following frame header with
79 which this frame has the best
80 score with */
81 } FLACHeaderMarker;
82
83 typedef struct FLACParseContext {
84 AVCodecParserContext *pc; /**< parent context */
85 AVCodecContext *avctx; /**< codec context pointer for logging */
86 FLACHeaderMarker *headers; /**< linked-list that starts at the first
87 CRC-8 verified header within buffer */
88 FLACHeaderMarker *best_header; /**< highest scoring header within buffer */
89 int nb_headers_found; /**< number of headers found in the last
90 flac_parse() call */
91 int nb_headers_buffered; /**< number of headers that are buffered */
92 int best_header_valid; /**< flag set when the parser returns junk;
93 if set return best_header next time */
94 FifoBuffer fifo_buf; /**< buffer to store all data until headers
95 can be verified */
96 int end_padded; /**< specifies if fifo_buf's end is padded */
97 uint8_t *wrap_buf; /**< general fifo read buffer when wrapped */
98 int wrap_buf_allocated_size; /**< actual allocated size of the buffer */
99 FLACFrameInfo last_fi; /**< last decoded frame header info */
100 int last_fi_valid; /**< set if last_fi is valid */
101 } FLACParseContext;
102
103 4935 static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf,
104 FLACFrameInfo *fi)
105 {
106 GetBitContext gb;
107 uint8_t subframe_type;
108
109 // header plus one byte from first subframe
110 4935 init_get_bits(&gb, buf, MAX_FRAME_VERIFY_SIZE * 8);
111
2/2
✓ Branch 1 taken 1006 times.
✓ Branch 2 taken 3929 times.
4935 if (ff_flac_decode_frame_header(avctx, &gb, fi, 127)) {
112 1006 return 0;
113 }
114 // subframe zero bit
115
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3929 times.
3929 if (get_bits1(&gb) != 0) {
116 return 0;
117 }
118 // subframe type
119 // 000000 : SUBFRAME_CONSTANT
120 // 000001 : SUBFRAME_VERBATIM
121 // 00001x : reserved
122 // 0001xx : reserved
123 // 001xxx : if(xxx <= 4) SUBFRAME_FIXED, xxx=order ; else reserved
124 // 01xxxx : reserved
125 // 1xxxxx : SUBFRAME_LPC, xxxxx=order-1
126 3929 subframe_type = get_bits(&gb, 6);
127
4/6
✓ Branch 0 taken 3790 times.
✓ Branch 1 taken 139 times.
✓ Branch 2 taken 3790 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2278 times.
6207 if (!(subframe_type == 0 ||
128
1/2
✓ Branch 0 taken 3790 times.
✗ Branch 1 not taken.
3790 subframe_type == 1 ||
129
2/2
✓ Branch 0 taken 2278 times.
✓ Branch 1 taken 1512 times.
3790 ((subframe_type >= 8) && (subframe_type <= 12)) ||
130 (subframe_type >= 32))) {
131 return 0;
132 }
133
134 3929 return 1;
135 }
136
137 161041 static size_t flac_fifo_size(const FifoBuffer *f)
138 {
139
4/4
✓ Branch 0 taken 76196 times.
✓ Branch 1 taken 84845 times.
✓ Branch 2 taken 75856 times.
✓ Branch 3 taken 340 times.
161041 if (f->wptr <= f->rptr && !f->empty)
140 75856 return (f->wptr - f->buffer) + (f->end - f->rptr);
141 85185 return f->wptr - f->rptr;
142 }
143
144 77862 static size_t flac_fifo_space(const FifoBuffer *f)
145 {
146 77862 return f->end - f->buffer - flac_fifo_size(f);
147 }
148
149 /**
150 * Non-destructive fast fifo pointer fetching
151 * Returns a pointer from the specified offset.
152 * If possible the pointer points within the fifo buffer.
153 * Otherwise (if it would cause a wrap around,) a pointer to a user-specified
154 * buffer is used.
155 * The pointer can be NULL. In any case it will be reallocated to hold the size.
156 * If the returned pointer will be used after subsequent calls to flac_fifo_read_wrap
157 * then the subsequent calls should pass in a different wrap_buf so as to not
158 * overwrite the contents of the previous wrap_buf.
159 * This function is based on av_fifo_generic_read, which is why there is a comment
160 * about a memory barrier for SMP.
161 */
162 7552 static uint8_t *flac_fifo_read_wrap(FLACParseContext *fpc, int offset, int len,
163 uint8_t **wrap_buf, int *allocated_size)
164 {
165 7552 FifoBuffer *f = &fpc->fifo_buf;
166 7552 uint8_t *start = f->rptr + offset;
167 uint8_t *tmp_buf;
168
169
2/2
✓ Branch 0 taken 1932 times.
✓ Branch 1 taken 5620 times.
7552 if (start >= f->end)
170 1932 start -= f->end - f->buffer;
171
2/2
✓ Branch 0 taken 7378 times.
✓ Branch 1 taken 174 times.
7552 if (f->end - start >= len)
172 7378 return start;
173
174 174 tmp_buf = av_fast_realloc(*wrap_buf, allocated_size, len);
175
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 174 times.
174 if (!tmp_buf) {
177 av_log(fpc->avctx, AV_LOG_ERROR,
178 "couldn't reallocate wrap buffer of size %d", len);
179 return NULL;
180 }
181 174 *wrap_buf = tmp_buf;
182 do {
183 348 int seg_len = FFMIN(f->end - start, len);
184 348 memcpy(tmp_buf, start, seg_len);
185 348 tmp_buf = (uint8_t*)tmp_buf + seg_len;
186 // memory barrier needed for SMP here in theory
187
188 348 start += seg_len - (f->end - f->buffer);
189 348 len -= seg_len;
190
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 174 times.
348 } while (len > 0);
191
192 174 return *wrap_buf;
193 }
194
195 /**
196 * Return a pointer in the fifo buffer where the offset starts at until
197 * the wrap point or end of request.
198 * len will contain the valid length of the returned buffer.
199 * A second call to flac_fifo_read (with new offset and len) should be called
200 * to get the post-wrap buf if the returned len is less than the requested.
201 **/
202 43021 static uint8_t *flac_fifo_read(FifoBuffer *f, int offset, int *len)
203 {
204 43021 uint8_t *start = f->rptr + offset;
205
206
2/2
✓ Branch 0 taken 19716 times.
✓ Branch 1 taken 23305 times.
43021 if (start >= f->end)
207 19716 start -= f->end - f->buffer;
208 43021 *len = FFMIN(*len, f->end - start);
209 43021 return start;
210 }
211
212 19 static int flac_fifo_grow(FifoBuffer *f, size_t inc)
213 {
214 19 size_t size_old = f->end - f->buffer;
215 19 size_t offset_r = f->rptr - f->buffer;
216 19 size_t offset_w = f->wptr - f->buffer;
217 size_t size_new;
218
219 uint8_t *tmp;
220
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (size_old > SIZE_MAX - inc)
222 return AVERROR(EINVAL);
223 19 size_new = size_old + inc;
224
225 19 tmp = av_realloc(f->buffer, size_new);
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!tmp)
227 return AVERROR(ENOMEM);
228
229 // move the data from the beginning of the ring buffer
230 // to the newly allocated space
231
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
19 if (offset_w <= offset_r && !f->empty) {
232 11 const size_t copy = FFMIN(inc, offset_w);
233 11 memcpy(tmp + size_old, tmp, copy);
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (copy < offset_w) {
235 memmove(tmp, tmp + copy, offset_w - copy);
236 offset_w -= copy;
237 } else
238 11 offset_w = size_old + copy;
239 }
240
241 19 f->buffer = tmp;
242 19 f->end = f->buffer + size_new;
243 19 f->rptr = f->buffer + offset_r;
244 19 f->wptr = f->buffer + offset_w;
245
246 19 return 0;
247 }
248
249 38931 static int flac_fifo_write(FifoBuffer *f, const uint8_t *src, size_t size)
250 {
251 uint8_t *wptr;
252
253
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 38912 times.
38931 if (flac_fifo_space(f) < size) {
254
1/2
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
19 int ret = flac_fifo_grow(f, FFMAX(flac_fifo_size(f), size));
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0)
256 return ret;
257 }
258
259
1/2
✓ Branch 0 taken 38931 times.
✗ Branch 1 not taken.
38931 if (size)
260 38931 f->empty = 0;
261
262 38931 wptr = f->wptr;
263 do {
264 39108 size_t len = FFMIN(f->end - wptr, size);
265 39108 memcpy(wptr, src, len);
266 39108 src += len;
267 39108 wptr += len;
268
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 38924 times.
39108 if (wptr >= f->end)
269 184 wptr = f->buffer;
270 39108 size -= len;
271
2/2
✓ Branch 0 taken 177 times.
✓ Branch 1 taken 38931 times.
39108 } while (size > 0);
272
273 38931 f->wptr = wptr;
274
275 38931 return 0;
276 }
277
278 2546 static void flac_fifo_drain(FifoBuffer *f, size_t size)
279 {
280 2546 size_t size_cur = flac_fifo_size(f);
281
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2546 times.
2546 av_assert0(size_cur >= size);
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2546 times.
2546 if (size_cur == size)
284 f->empty = 1;
285
286 2546 f->rptr += size;
287
2/2
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 2373 times.
2546 if (f->rptr >= f->end)
288 173 f->rptr -= f->end - f->buffer;
289 2546 }
290
291 338 static int flac_fifo_alloc(FifoBuffer *f, size_t size)
292 {
293 338 memset(f, 0, sizeof(*f));
294
295 338 f->buffer = av_realloc(NULL, size);
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 338 times.
338 if (!f->buffer)
297 return AVERROR(ENOMEM);
298
299 338 f->wptr = f->buffer;
300 338 f->rptr = f->buffer;
301 338 f->end = f->buffer + size;
302
303 338 f->empty = 1;
304
305 338 return 0;
306 }
307
308 338 static void flac_fifo_free(FifoBuffer *f)
309 {
310 338 av_freep(&f->buffer);
311 338 memset(f, 0, sizeof(*f));
312 338 }
313
314 4781 static int find_headers_search_validate(FLACParseContext *fpc, int offset)
315 {
316 FLACFrameInfo fi;
317 uint8_t *header_buf;
318 4781 int size = 0;
319 4781 header_buf = flac_fifo_read_wrap(fpc, offset,
320 MAX_FRAME_VERIFY_SIZE + AV_INPUT_BUFFER_PADDING_SIZE,
321 &fpc->wrap_buf,
322 &fpc->wrap_buf_allocated_size);
323
2/2
✓ Branch 1 taken 3788 times.
✓ Branch 2 taken 993 times.
4781 if (frame_header_is_valid(fpc->avctx, header_buf, &fi)) {
324 3788 FLACHeaderMarker **end_handle = &fpc->headers;
325 int i;
326
327 3788 size = 0;
328
2/2
✓ Branch 0 taken 27092 times.
✓ Branch 1 taken 3788 times.
30880 while (*end_handle) {
329 27092 end_handle = &(*end_handle)->next;
330 27092 size++;
331 }
332
333 3788 *end_handle = av_mallocz(sizeof(**end_handle));
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3788 times.
3788 if (!*end_handle) {
335 av_log(fpc->avctx, AV_LOG_ERROR,
336 "couldn't allocate FLACHeaderMarker\n");
337 return AVERROR(ENOMEM);
338 }
339 3788 (*end_handle)->fi = fi;
340 3788 (*end_handle)->offset = offset;
341
342
2/2
✓ Branch 0 taken 15152 times.
✓ Branch 1 taken 3788 times.
18940 for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++)
343 15152 (*end_handle)->link_penalty[i] = FLAC_HEADER_NOT_PENALIZED_YET;
344
345 3788 fpc->nb_headers_found++;
346 3788 size++;
347 }
348 4781 return size;
349 }
350
351 39113 static int find_headers_search(FLACParseContext *fpc, uint8_t *buf,
352 int buf_size, int search_start)
353 {
354 39113 int size = 0, mod_offset = (buf_size - 1) % 4, i, j;
355 uint32_t x;
356
357
2/2
✓ Branch 0 taken 788 times.
✓ Branch 1 taken 39113 times.
39901 for (i = 0; i < mod_offset; i++) {
358
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 722 times.
788 if ((AV_RB16(buf + i) & 0xFFFE) == 0xFFF8) {
359 66 int ret = find_headers_search_validate(fpc, search_start + i);
360 66 size = FFMAX(size, ret);
361 }
362 }
363
364
2/2
✓ Branch 0 taken 9943545 times.
✓ Branch 1 taken 39113 times.
9982658 for (; i < buf_size - 1; i += 4) {
365 9943545 x = AV_RN32(buf + i);
366
2/2
✓ Branch 0 taken 144403 times.
✓ Branch 1 taken 9799142 times.
9943545 if (((x & ~(x + 0x01010101)) & 0x80808080)) {
367
2/2
✓ Branch 0 taken 577612 times.
✓ Branch 1 taken 144403 times.
722015 for (j = 0; j < 4; j++) {
368
2/2
✓ Branch 0 taken 4715 times.
✓ Branch 1 taken 572897 times.
577612 if ((AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8) {
369 4715 int ret = find_headers_search_validate(fpc, search_start + i + j);
370 4715 size = FFMAX(size, ret);
371 }
372 }
373 }
374 }
375 39113 return size;
376 }
377
378 38931 static int find_new_headers(FLACParseContext *fpc, int search_start)
379 {
380 FLACHeaderMarker *end;
381 38931 int search_end, size = 0, read_len, temp;
382 uint8_t *buf;
383 38931 fpc->nb_headers_found = 0;
384
385 /* Search for a new header of at most 16 bytes. */
386 38931 search_end = flac_fifo_size(&fpc->fifo_buf) - (MAX_FRAME_HEADER_SIZE - 1);
387 38931 read_len = search_end - search_start + 1;
388 38931 buf = flac_fifo_read(&fpc->fifo_buf, search_start, &read_len);
389 38931 size = find_headers_search(fpc, buf, read_len, search_start);
390 38931 search_start += read_len - 1;
391
392 /* If fifo end was hit do the wrap around. */
393
2/2
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 38749 times.
38931 if (search_start != search_end) {
394 uint8_t wrap[2];
395
396 182 wrap[0] = buf[read_len - 1];
397 /* search_start + 1 is the post-wrap offset in the fifo. */
398 182 read_len = search_end - (search_start + 1) + 1;
399
400 182 buf = flac_fifo_read(&fpc->fifo_buf, search_start + 1, &read_len);
401 182 wrap[1] = buf[0];
402
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 182 times.
182 if ((AV_RB16(wrap) & 0xFFFE) == 0xFFF8) {
404 temp = find_headers_search_validate(fpc, search_start);
405 size = FFMAX(size, temp);
406 }
407 182 search_start++;
408
409 /* Continue to do the last half of the wrap. */
410 182 temp = find_headers_search(fpc, buf, read_len, search_start);
411 182 size = FFMAX(size, temp);
412 182 search_start += read_len - 1;
413 }
414
415 /* Return the size even if no new headers were found. */
416
4/4
✓ Branch 0 taken 35179 times.
✓ Branch 1 taken 3752 times.
✓ Branch 2 taken 34920 times.
✓ Branch 3 taken 259 times.
38931 if (!size && fpc->headers)
417
2/2
✓ Branch 0 taken 287929 times.
✓ Branch 1 taken 34920 times.
322849 for (end = fpc->headers; end; end = end->next)
418 287929 size++;
419 38931 return size;
420 }
421
422 39066 static int check_header_fi_mismatch(FLACParseContext *fpc,
423 FLACFrameInfo *header_fi,
424 FLACFrameInfo *child_fi,
425 int log_level_offset)
426 {
427 39066 int deduction = 0;
428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39066 times.
39066 if (child_fi->samplerate != header_fi->samplerate) {
429 deduction += FLAC_HEADER_CHANGED_PENALTY;
430 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
431 "sample rate change detected in adjacent frames\n");
432 }
433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39066 times.
39066 if (child_fi->bps != header_fi->bps) {
434 deduction += FLAC_HEADER_CHANGED_PENALTY;
435 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
436 "bits per sample change detected in adjacent frames\n");
437 }
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39066 times.
39066 if (child_fi->is_var_size != header_fi->is_var_size) {
439 /* Changing blocking strategy not allowed per the spec */
440 deduction += FLAC_HEADER_BASE_SCORE;
441 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
442 "blocking strategy change detected in adjacent frames\n");
443 }
444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39066 times.
39066 if (child_fi->channels != header_fi->channels) {
445 deduction += FLAC_HEADER_CHANGED_PENALTY;
446 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
447 "number of channels change detected in adjacent frames\n");
448 }
449 39066 return deduction;
450 }
451
452 16182 static int check_header_mismatch(FLACParseContext *fpc,
453 FLACHeaderMarker *header,
454 FLACHeaderMarker *child,
455 int log_level_offset)
456 {
457 16182 FLACFrameInfo *header_fi = &header->fi, *child_fi = &child->fi;
458 16182 int check_crc, deduction, deduction_expected = 0, i;
459 16182 deduction = check_header_fi_mismatch(fpc, header_fi, child_fi,
460 log_level_offset);
461 /* Check sample and frame numbers. */
462 16182 if ((child_fi->frame_or_sample_num - header_fi->frame_or_sample_num
463
1/2
✓ Branch 0 taken 16182 times.
✗ Branch 1 not taken.
16182 != header_fi->blocksize) &&
464 16182 (child_fi->frame_or_sample_num
465
2/2
✓ Branch 0 taken 9924 times.
✓ Branch 1 taken 6258 times.
16182 != header_fi->frame_or_sample_num + 1)) {
466 FLACHeaderMarker *curr;
467 int64_t expected_frame_num, expected_sample_num;
468 /* If there are frames in the middle we expect this deduction,
469 as they are probably valid and this one follows it */
470
471 9924 expected_frame_num = expected_sample_num = header_fi->frame_or_sample_num;
472 9924 curr = header;
473
2/2
✓ Branch 0 taken 29466 times.
✓ Branch 1 taken 9924 times.
39390 while (curr != child) {
474 /* Ignore frames that failed all crc checks */
475
1/2
✓ Branch 0 taken 29466 times.
✗ Branch 1 not taken.
29466 for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++) {
476
1/2
✓ Branch 0 taken 29466 times.
✗ Branch 1 not taken.
29466 if (curr->link_penalty[i] < FLAC_HEADER_CRC_FAIL_PENALTY) {
477 29466 expected_frame_num++;
478 29466 expected_sample_num += curr->fi.blocksize;
479 29466 break;
480 }
481 }
482 29466 curr = curr->next;
483 }
484
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9924 times.
9924 if (expected_frame_num == child_fi->frame_or_sample_num ||
486 expected_sample_num == child_fi->frame_or_sample_num)
487 9924 deduction_expected = deduction ? 0 : 1;
488
489 9924 deduction += FLAC_HEADER_CHANGED_PENALTY;
490 9924 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
491 "sample/frame number mismatch in adjacent frames\n");
492 }
493
494
1/2
✓ Branch 0 taken 16182 times.
✗ Branch 1 not taken.
16182 if (fpc->last_fi.is_var_size == header_fi->is_var_size) {
495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16182 times.
16182 if (fpc->last_fi.is_var_size &&
496 fpc->last_fi.frame_or_sample_num + fpc->last_fi.blocksize == header_fi->frame_or_sample_num) {
497 check_crc = 0;
498
1/2
✓ Branch 0 taken 16182 times.
✗ Branch 1 not taken.
16182 } else if (!fpc->last_fi.is_var_size &&
499
2/2
✓ Branch 0 taken 2657 times.
✓ Branch 1 taken 13525 times.
16182 fpc->last_fi.frame_or_sample_num + 1 == header_fi->frame_or_sample_num) {
500 2657 check_crc = 0;
501 } else {
502
3/4
✓ Branch 0 taken 3734 times.
✓ Branch 1 taken 9791 times.
✓ Branch 2 taken 3734 times.
✗ Branch 3 not taken.
13525 check_crc = !deduction && !deduction_expected;
503 }
504 } else {
505 check_crc = !deduction && !deduction_expected;
506 }
507
508 /* If we have suspicious headers, check the CRC between them */
509
5/6
✓ Branch 0 taken 12448 times.
✓ Branch 1 taken 3734 times.
✓ Branch 2 taken 9924 times.
✓ Branch 3 taken 2524 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9924 times.
16182 if (check_crc || (deduction && !deduction_expected)) {
510 FLACHeaderMarker *curr;
511 int read_len;
512 uint8_t *buf;
513 3734 uint32_t crc = 1;
514 3734 int inverted_test = 0;
515
516 /* Since CRC is expensive only do it if we haven't yet.
517 This assumes a CRC penalty is greater than all other check penalties */
518 3734 curr = header->next;
519
2/4
✓ Branch 0 taken 3734 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3734 times.
3734 for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS && curr != child; i++)
520 curr = curr->next;
521
522
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3734 times.
3734 av_assert0(i < FLAC_MAX_SEQUENTIAL_HEADERS);
523
524
2/2
✓ Branch 0 taken 3578 times.
✓ Branch 1 taken 156 times.
3734 if (header->link_penalty[i] < FLAC_HEADER_CRC_FAIL_PENALTY ||
525
1/2
✓ Branch 0 taken 3578 times.
✗ Branch 1 not taken.
3578 header->link_penalty[i] == FLAC_HEADER_NOT_PENALIZED_YET) {
526 FLACHeaderMarker *start, *end;
527
528 /* Although overlapping chains are scored, the crc should never
529 have to be computed twice for a single byte. */
530 3734 start = header;
531 3734 end = child;
532
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3734 times.
3734 if (i > 0 &&
533 header->link_penalty[i - 1] >= FLAC_HEADER_CRC_FAIL_PENALTY) {
534 while (start->next != child)
535 start = start->next;
536 inverted_test = 1;
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3734 times.
3734 } else if (i > 0 &&
538 header->next->link_penalty[i-1] >=
539 FLAC_HEADER_CRC_FAIL_PENALTY ) {
540 end = header->next;
541 inverted_test = 1;
542 }
543
544 3734 read_len = end->offset - start->offset;
545 3734 buf = flac_fifo_read(&fpc->fifo_buf, start->offset, &read_len);
546 3734 crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf, read_len);
547 3734 read_len = (end->offset - start->offset) - read_len;
548
549
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 3560 times.
3734 if (read_len) {
550 174 buf = flac_fifo_read(&fpc->fifo_buf, end->offset - read_len, &read_len);
551 174 crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), crc, buf, read_len);
552 }
553 }
554
555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3734 times.
3734 if (!crc ^ !inverted_test) {
556 deduction += FLAC_HEADER_CRC_FAIL_PENALTY;
557 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
558 "crc check failed from offset %i (frame %"PRId64") to %i (frame %"PRId64")\n",
559 header->offset, header_fi->frame_or_sample_num,
560 child->offset, child_fi->frame_or_sample_num);
561 }
562 }
563 16182 return deduction;
564 }
565
566 /**
567 * Score a header.
568 *
569 * Give FLAC_HEADER_BASE_SCORE points to a frame for existing.
570 * If it has children, (subsequent frames of which the preceding CRC footer
571 * validates against this one,) then take the maximum score of the children,
572 * with a penalty of FLAC_HEADER_CHANGED_PENALTY applied for each change to
573 * bps, sample rate, channels, but not decorrelation mode, or blocksize,
574 * because it can change often.
575 **/
576 97520 static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header)
577 {
578 FLACHeaderMarker *child;
579 97520 int dist = 0;
580 int child_score;
581 97520 int base_score = FLAC_HEADER_BASE_SCORE;
582
2/2
✓ Branch 0 taken 73113 times.
✓ Branch 1 taken 24407 times.
97520 if (header->max_score != FLAC_HEADER_NOT_SCORED_YET)
583 73113 return header->max_score;
584
585 /* Modify the base score with changes from the last output header */
586
2/2
✓ Branch 0 taken 22884 times.
✓ Branch 1 taken 1523 times.
24407 if (fpc->last_fi_valid) {
587 /* Silence the log since this will be repeated if selected */
588 22884 base_score -= check_header_fi_mismatch(fpc, &fpc->last_fi, &header->fi,
589 AV_LOG_DEBUG);
590 }
591
592 24407 header->max_score = base_score;
593
594 /* Check and compute the children's scores. */
595 24407 child = header->next;
596
4/4
✓ Branch 0 taken 82910 times.
✓ Branch 1 taken 14610 times.
✓ Branch 2 taken 73113 times.
✓ Branch 3 taken 9797 times.
97520 for (dist = 0; dist < FLAC_MAX_SEQUENTIAL_HEADERS && child; dist++) {
597 /* Look at the child's frame header info and penalize suspicious
598 changes between the headers. */
599
2/2
✓ Branch 0 taken 13547 times.
✓ Branch 1 taken 59566 times.
73113 if (header->link_penalty[dist] == FLAC_HEADER_NOT_PENALIZED_YET) {
600 13547 header->link_penalty[dist] = check_header_mismatch(fpc, header,
601 child, AV_LOG_DEBUG);
602 }
603 73113 child_score = score_header(fpc, child) - header->link_penalty[dist];
604
605
2/2
✓ Branch 0 taken 21951 times.
✓ Branch 1 taken 51162 times.
73113 if (FLAC_HEADER_BASE_SCORE + child_score > header->max_score) {
606 /* Keep the child because the frame scoring is dynamic. */
607 21951 header->best_child = child;
608 21951 header->max_score = base_score + child_score;
609 }
610 73113 child = child->next;
611 }
612
613 24407 return header->max_score;
614 }
615
616 2461 static void score_sequences(FLACParseContext *fpc)
617 {
618 FLACHeaderMarker *curr;
619 2461 int best_score = FLAC_HEADER_NOT_SCORED_YET;
620 /* First pass to clear all old scores. */
621
2/2
✓ Branch 0 taken 24407 times.
✓ Branch 1 taken 2461 times.
26868 for (curr = fpc->headers; curr; curr = curr->next)
622 24407 curr->max_score = FLAC_HEADER_NOT_SCORED_YET;
623
624 /* Do a second pass to score them all. */
625
2/2
✓ Branch 0 taken 24407 times.
✓ Branch 1 taken 2461 times.
26868 for (curr = fpc->headers; curr; curr = curr->next) {
626
2/2
✓ Branch 1 taken 2456 times.
✓ Branch 2 taken 21951 times.
24407 if (score_header(fpc, curr) > best_score) {
627 2456 fpc->best_header = curr;
628 2456 best_score = curr->max_score;
629 }
630 }
631 2461 }
632
633 2672 static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf,
634 int *poutbuf_size)
635 {
636 2672 FLACHeaderMarker *header = fpc->best_header;
637 2672 FLACHeaderMarker *child = header->best_child;
638
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 2635 times.
2672 if (!child) {
639 37 *poutbuf_size = flac_fifo_size(&fpc->fifo_buf) - header->offset;
640 } else {
641 2635 *poutbuf_size = child->offset - header->offset;
642
643 /* If the child has suspicious changes, log them */
644 2635 check_header_mismatch(fpc, header, child, 0);
645 }
646
647 2672 ff_flac_set_channel_layout(fpc->avctx, header->fi.channels);
648
649 2672 fpc->avctx->sample_rate = header->fi.samplerate;
650 2672 fpc->pc->duration = header->fi.blocksize;
651 2672 *poutbuf = flac_fifo_read_wrap(fpc, header->offset, *poutbuf_size,
652 &fpc->wrap_buf,
653 &fpc->wrap_buf_allocated_size);
654
655
1/2
✓ Branch 0 taken 2672 times.
✗ Branch 1 not taken.
2672 if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) {
656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2672 times.
2672 if (header->fi.is_var_size)
657 fpc->pc->pts = header->fi.frame_or_sample_num;
658
2/2
✓ Branch 0 taken 2635 times.
✓ Branch 1 taken 37 times.
2672 else if (header->best_child)
659 2635 fpc->pc->pts = header->fi.frame_or_sample_num * header->fi.blocksize;
660 }
661
662 2672 fpc->best_header_valid = 0;
663 2672 fpc->last_fi_valid = 1;
664 2672 fpc->last_fi = header->fi;
665
666 /* Return the negative overread index so the client can compute pos.
667 This should be the amount overread to the beginning of the child */
668
2/2
✓ Branch 0 taken 2635 times.
✓ Branch 1 taken 37 times.
2672 if (child) {
669 2635 int64_t offset = child->offset - flac_fifo_size(&fpc->fifo_buf);
670
1/2
✓ Branch 0 taken 2635 times.
✗ Branch 1 not taken.
2635 if (offset > -(1 << 28))
671 2635 return offset;
672 }
673 37 return 0;
674 }
675
676 41766 static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
677 const uint8_t **poutbuf, int *poutbuf_size,
678 const uint8_t *buf, int buf_size)
679 {
680 41766 FLACParseContext *fpc = s->priv_data;
681 FLACHeaderMarker *curr;
682 int nb_headers;
683 41766 const uint8_t *read_end = buf;
684 41766 const uint8_t *read_start = buf;
685
686
2/2
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 41612 times.
41766 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
687 FLACFrameInfo fi;
688
2/2
✓ Branch 1 taken 141 times.
✓ Branch 2 taken 13 times.
154 if (frame_header_is_valid(avctx, buf, &fi)) {
689 141 s->duration = fi.blocksize;
690
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 141 times.
141 if (!avctx->sample_rate)
691 avctx->sample_rate = fi.samplerate;
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 141 times.
141 if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) {
693 fpc->pc->pts = fi.frame_or_sample_num;
694 if (!fi.is_var_size)
695 fpc->pc->pts *= fi.blocksize;
696 }
697 }
698 154 *poutbuf = buf;
699 154 *poutbuf_size = buf_size;
700 154 return buf_size;
701 }
702
703 41612 fpc->avctx = avctx;
704
4/4
✓ Branch 0 taken 2425 times.
✓ Branch 1 taken 39187 times.
✓ Branch 2 taken 2413 times.
✓ Branch 3 taken 12 times.
41612 if (fpc->best_header_valid && fpc->nb_headers_buffered >= FLAC_MIN_HEADERS)
705 2413 return get_best_header(fpc, poutbuf, poutbuf_size);
706
707 /* If a best_header was found last call remove it with the buffer data. */
708
4/4
✓ Branch 0 taken 2586 times.
✓ Branch 1 taken 36613 times.
✓ Branch 2 taken 2546 times.
✓ Branch 3 taken 40 times.
39199 if (fpc->best_header && fpc->best_header->best_child) {
709 FLACHeaderMarker *temp;
710 2546 FLACHeaderMarker *best_child = fpc->best_header->best_child;
711
712 /* Remove headers in list until the end of the best_header. */
713
2/2
✓ Branch 0 taken 2546 times.
✓ Branch 1 taken 2546 times.
5092 for (curr = fpc->headers; curr != best_child; curr = temp) {
714
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2546 times.
2546 if (curr != fpc->best_header) {
715 av_log(avctx, AV_LOG_DEBUG,
716 "dropping low score %i frame header from offset %i to %i\n",
717 curr->max_score, curr->offset, curr->next->offset);
718 }
719 2546 temp = curr->next;
720 2546 av_free(curr);
721 2546 fpc->nb_headers_buffered--;
722 }
723 /* Release returned data from ring buffer. */
724 2546 flac_fifo_drain(&fpc->fifo_buf, best_child->offset);
725
726 /* Fix the offset for the headers remaining to match the new buffer. */
727
2/2
✓ Branch 0 taken 19290 times.
✓ Branch 1 taken 2546 times.
21836 for (curr = best_child->next; curr; curr = curr->next)
728 19290 curr->offset -= best_child->offset;
729
730 2546 best_child->offset = 0;
731 2546 fpc->headers = best_child;
732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2546 times.
2546 if (fpc->nb_headers_buffered >= FLAC_MIN_HEADERS) {
733 fpc->best_header = best_child;
734 return get_best_header(fpc, poutbuf, poutbuf_size);
735 }
736 2546 fpc->best_header = NULL;
737
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 36613 times.
36653 } else if (fpc->best_header) {
738 /* No end frame no need to delete the buffer; probably eof */
739 FLACHeaderMarker *temp;
740
741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 for (curr = fpc->headers; curr != fpc->best_header; curr = temp) {
742 temp = curr->next;
743 av_free(curr);
744 fpc->nb_headers_buffered--;
745 }
746 40 fpc->headers = fpc->best_header->next;
747 40 av_freep(&fpc->best_header);
748 40 fpc->nb_headers_buffered--;
749 }
750
751 /* Find and score new headers. */
752 /* buf_size is zero when flushing, so check for this since we do */
753 /* not want to try to read more input once we have found the end. */
754 /* Also note that buf can't be NULL. */
755
2/2
✓ Branch 0 taken 38883 times.
✓ Branch 1 taken 2413 times.
80495 while ((buf_size && read_end < buf + buf_size &&
756
1/2
✓ Branch 0 taken 38883 times.
✗ Branch 1 not taken.
38883 fpc->nb_headers_buffered < FLAC_MIN_HEADERS)
757
6/6
✓ Branch 0 taken 41296 times.
✓ Branch 1 taken 364 times.
✓ Branch 2 taken 364 times.
✓ Branch 3 taken 2413 times.
✓ Branch 4 taken 48 times.
✓ Branch 5 taken 316 times.
44073 || (!buf_size && !fpc->end_padded)) {
758 int start_offset, ret;
759
760 /* Pad the end once if EOF, to check the final region for headers. */
761
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 38883 times.
38931 if (!buf_size) {
762 48 fpc->end_padded = 1;
763 48 read_end = read_start + MAX_FRAME_HEADER_SIZE;
764 } else {
765 /* The maximum read size is the upper-bound of what the parser
766 needs to have the required number of frames buffered */
767 38883 int nb_desired = FLAC_MIN_HEADERS - fpc->nb_headers_buffered + 1;
768
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38883 times.
38883 read_end = read_end + FFMIN(buf + buf_size - read_end,
769 nb_desired * FLAC_AVG_FRAME_SIZE);
770 }
771
772
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 38930 times.
38931 if (!flac_fifo_space(&fpc->fifo_buf) &&
773 1 flac_fifo_size(&fpc->fifo_buf) / FLAC_AVG_FRAME_SIZE >
774
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 fpc->nb_headers_buffered * 20) {
775 /* There is less than one valid flac header buffered for 20 headers
776 * buffered. Therefore the fifo is most likely filled with invalid
777 * data and the input is not a flac file. */
778 goto handle_error;
779 }
780
781 /* Fill the buffer. */
782
2/2
✓ Branch 0 taken 38883 times.
✓ Branch 1 taken 48 times.
38931 if (buf_size) {
783 38883 ret = flac_fifo_write(&fpc->fifo_buf, read_start,
784 38883 read_end - read_start);
785 } else {
786 48 int8_t pad[MAX_FRAME_HEADER_SIZE] = { 0 };
787 48 ret = flac_fifo_write(&fpc->fifo_buf, pad, sizeof(pad));
788 }
789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38931 times.
38931 if (ret < 0) {
790 av_log(avctx, AV_LOG_ERROR, "Error buffering data\n");
791 goto handle_error;
792 }
793
794 /* Tag headers and update sequences. */
795 38931 start_offset = flac_fifo_size(&fpc->fifo_buf) -
796 38931 ((read_end - read_start) + (MAX_FRAME_HEADER_SIZE - 1));
797 38931 start_offset = FFMAX(0, start_offset);
798 38931 nb_headers = find_new_headers(fpc, start_offset);
799
800
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38931 times.
38931 if (nb_headers < 0) {
801 av_log(avctx, AV_LOG_ERROR,
802 "find_new_headers couldn't allocate FLAC header\n");
803 goto handle_error;
804 }
805
806 38931 fpc->nb_headers_buffered = nb_headers;
807 /* Wait till FLAC_MIN_HEADERS to output a valid frame. */
808
4/4
✓ Branch 0 taken 38883 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 36470 times.
✓ Branch 3 taken 2413 times.
38931 if (!fpc->end_padded && fpc->nb_headers_buffered < FLAC_MIN_HEADERS) {
809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36470 times.
36470 if (read_end < buf + buf_size) {
810 read_start = read_end;
811 continue;
812 } else {
813 36470 goto handle_error;
814 }
815 }
816
817 /* If headers found, update the scores since we have longer chains. */
818
3/4
✓ Branch 0 taken 2413 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 2413 times.
✗ Branch 3 not taken.
2461 if (fpc->end_padded || fpc->nb_headers_found)
819 2461 score_sequences(fpc);
820
821 /* restore the state pre-padding */
822
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 2413 times.
2461 if (fpc->end_padded) {
823 48 int empty = flac_fifo_size(&fpc->fifo_buf) == MAX_FRAME_HEADER_SIZE;
824 48 int warp = fpc->fifo_buf.wptr - fpc->fifo_buf.buffer < MAX_FRAME_HEADER_SIZE;
825 /* HACK: drain the tail of the fifo */
826 48 fpc->fifo_buf.wptr -= MAX_FRAME_HEADER_SIZE;
827
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (warp) {
828 fpc->fifo_buf.wptr += fpc->fifo_buf.end -
829 fpc->fifo_buf.buffer;
830 }
831 48 fpc->fifo_buf.empty = empty;
832 48 read_start = read_end = NULL;
833 }
834 }
835
836
2/2
✓ Branch 0 taken 25381 times.
✓ Branch 1 taken 2729 times.
28110 for (curr = fpc->headers; curr; curr = curr->next) {
837
3/4
✓ Branch 0 taken 25153 times.
✓ Branch 1 taken 228 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 25153 times.
25381 if (!fpc->best_header || curr->max_score > fpc->best_header->max_score) {
838 228 fpc->best_header = curr;
839 }
840 }
841
842
3/4
✓ Branch 0 taken 2684 times.
✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2684 times.
2729 if (fpc->best_header && fpc->best_header->max_score <= 0) {
843 // Only accept a bad header if there is no other option to continue
844 if (!buf_size || read_end != buf || fpc->nb_headers_buffered < FLAC_MIN_HEADERS)
845 fpc->best_header = NULL;
846 }
847
848
2/2
✓ Branch 0 taken 2684 times.
✓ Branch 1 taken 45 times.
2729 if (fpc->best_header) {
849 2684 fpc->best_header_valid = 1;
850
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 2585 times.
2684 if (fpc->best_header->offset > 0) {
851 /* Output a junk frame. */
852 99 av_log(avctx, AV_LOG_DEBUG, "Junk frame till offset %i\n",
853 99 fpc->best_header->offset);
854
855 /* Set duration to 0. It is unknown or invalid in a junk frame. */
856 99 s->duration = 0;
857 99 *poutbuf_size = fpc->best_header->offset;
858 99 *poutbuf = flac_fifo_read_wrap(fpc, 0, *poutbuf_size,
859 &fpc->wrap_buf,
860 &fpc->wrap_buf_allocated_size);
861
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 12 times.
111 return buf_size ? (read_end - buf) : (fpc->best_header->offset -
862 12 flac_fifo_size(&fpc->fifo_buf));
863 }
864
2/2
✓ Branch 0 taken 2326 times.
✓ Branch 1 taken 259 times.
2585 if (!buf_size)
865 259 return get_best_header(fpc, poutbuf, poutbuf_size);
866 }
867
868 2371 handle_error:
869 38841 *poutbuf = NULL;
870 38841 *poutbuf_size = 0;
871
2/2
✓ Branch 0 taken 38796 times.
✓ Branch 1 taken 45 times.
38841 return buf_size ? read_end - buf : 0;
872 }
873
874 338 static av_cold int flac_parse_init(AVCodecParserContext *c)
875 {
876 338 FLACParseContext *fpc = c->priv_data;
877 int ret;
878
879 338 fpc->pc = c;
880 /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before
881 it drains. This is allocated early to avoid slow reallocation. */
882 338 ret = flac_fifo_alloc(&fpc->fifo_buf, (FLAC_MIN_HEADERS + 3) * FLAC_AVG_FRAME_SIZE);
883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 338 times.
338 if (ret < 0) {
884 av_log(fpc->avctx, AV_LOG_ERROR,
885 "couldn't allocate fifo_buf\n");
886 return AVERROR(ENOMEM);
887 }
888 338 return 0;
889 }
890
891 338 static av_cold void flac_parse_close(AVCodecParserContext *c)
892 {
893 338 FLACParseContext *fpc = c->priv_data;
894 338 FLACHeaderMarker *curr = fpc->headers, *temp;
895
896
2/2
✓ Branch 0 taken 1202 times.
✓ Branch 1 taken 338 times.
1540 while (curr) {
897 1202 temp = curr->next;
898 1202 av_free(curr);
899 1202 curr = temp;
900 }
901 338 fpc->headers = NULL;
902 338 flac_fifo_free(&fpc->fifo_buf);
903 338 av_freep(&fpc->wrap_buf);
904 338 }
905
906 const FFCodecParser ff_flac_parser = {
907 PARSER_CODEC_LIST(AV_CODEC_ID_FLAC),
908 .priv_data_size = sizeof(FLACParseContext),
909 .init = flac_parse_init,
910 .parse = flac_parse,
911 .close = flac_parse_close,
912 };
913