FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/flac_parser.c
Date: 2022-11-28 23:49:43
Exec Total Coverage
Lines: 361 421 85.7%
Branches: 208 284 73.2%

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 "bytestream.h"
38 #include "parser.h"
39 #include "flac_parse.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 5185 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 5185 init_get_bits(&gb, buf, MAX_FRAME_VERIFY_SIZE * 8);
111
2/2
✓ Branch 1 taken 886 times.
✓ Branch 2 taken 4299 times.
5185 if (ff_flac_decode_frame_header(avctx, &gb, fi, 127)) {
112 886 return 0;
113 }
114 // subframe zero bit
115
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4299 times.
4299 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 4299 subframe_type = get_bits(&gb, 6);
127
4/6
✓ Branch 0 taken 4164 times.
✓ Branch 1 taken 135 times.
✓ Branch 2 taken 4164 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2181 times.
6480 if (!(subframe_type == 0 ||
128
1/2
✓ Branch 0 taken 4164 times.
✗ Branch 1 not taken.
4164 subframe_type == 1 ||
129
2/2
✓ Branch 0 taken 2181 times.
✓ Branch 1 taken 1983 times.
4164 ((subframe_type >= 8) && (subframe_type <= 12)) ||
130 (subframe_type >= 32))) {
131 return 0;
132 }
133
134 4299 return 1;
135 }
136
137 141420 static size_t flac_fifo_size(const FifoBuffer *f)
138 {
139
4/4
✓ Branch 0 taken 66408 times.
✓ Branch 1 taken 75012 times.
✓ Branch 2 taken 66054 times.
✓ Branch 3 taken 354 times.
141420 if (f->wptr <= f->rptr && !f->empty)
140 66054 return (f->wptr - f->buffer) + (f->end - f->rptr);
141 75366 return f->wptr - f->rptr;
142 }
143
144 67806 static size_t flac_fifo_space(const FifoBuffer *f)
145 {
146 67806 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 8079 static uint8_t *flac_fifo_read_wrap(FLACParseContext *fpc, int offset, int len,
163 uint8_t **wrap_buf, int *allocated_size)
164 {
165 8079 FifoBuffer *f = &fpc->fifo_buf;
166 8079 uint8_t *start = f->rptr + offset;
167 uint8_t *tmp_buf;
168
169
2/2
✓ Branch 0 taken 1610 times.
✓ Branch 1 taken 6469 times.
8079 if (start >= f->end)
170 1610 start -= f->end - f->buffer;
171
2/2
✓ Branch 0 taken 7936 times.
✓ Branch 1 taken 143 times.
8079 if (f->end - start >= len)
172 7936 return start;
173
174 143 tmp_buf = av_fast_realloc(*wrap_buf, allocated_size, len);
175
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143 times.
143 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 143 *wrap_buf = tmp_buf;
182 do {
183 286 int seg_len = FFMIN(f->end - start, len);
184 286 memcpy(tmp_buf, start, seg_len);
185 286 tmp_buf = (uint8_t*)tmp_buf + seg_len;
186 // memory barrier needed for SMP here in theory
187
188 286 start += seg_len - (f->end - f->buffer);
189 286 len -= seg_len;
190
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 143 times.
286 } while (len > 0);
191
192 143 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 38325 static uint8_t *flac_fifo_read(FifoBuffer *f, int offset, int *len)
203 {
204 38325 uint8_t *start = f->rptr + offset;
205
206
2/2
✓ Branch 0 taken 17126 times.
✓ Branch 1 taken 21199 times.
38325 if (start >= f->end)
207 17126 start -= f->end - f->buffer;
208 38325 *len = FFMIN(*len, f->end - start);
209 38325 return start;
210 }
211
212 24 static int flac_fifo_grow(FifoBuffer *f, size_t inc)
213 {
214 24 size_t size_old = f->end - f->buffer;
215 24 size_t offset_r = f->rptr - f->buffer;
216 24 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 24 times.
24 if (size_old > SIZE_MAX - inc)
222 return AVERROR(EINVAL);
223 24 size_new = size_old + inc;
224
225 24 tmp = av_realloc(f->buffer, size_new);
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 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 16 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
24 if (offset_w <= offset_r && !f->empty) {
232 16 const size_t copy = FFMIN(inc, offset_w);
233 16 memcpy(tmp + size_old, tmp, copy);
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (copy < offset_w) {
235 memmove(tmp, tmp + copy, offset_w - copy);
236 offset_w -= copy;
237 } else
238 16 offset_w = size_old + copy;
239 }
240
241 24 f->buffer = tmp;
242 24 f->end = f->buffer + size_new;
243 24 f->rptr = f->buffer + offset_r;
244 24 f->wptr = f->buffer + offset_w;
245
246 24 return 0;
247 }
248
249 33903 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 24 times.
✓ Branch 2 taken 33879 times.
33903 if (flac_fifo_space(f) < size) {
254
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 int ret = flac_fifo_grow(f, FFMAX(flac_fifo_size(f), size));
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
256 return ret;
257 }
258
259
1/2
✓ Branch 0 taken 33903 times.
✗ Branch 1 not taken.
33903 if (size)
260 33903 f->empty = 0;
261
262 33903 wptr = f->wptr;
263 do {
264 34056 size_t len = FFMIN(f->end - wptr, size);
265 34056 memcpy(wptr, src, len);
266 34056 src += len;
267 34056 wptr += len;
268
2/2
✓ Branch 0 taken 159 times.
✓ Branch 1 taken 33897 times.
34056 if (wptr >= f->end)
269 159 wptr = f->buffer;
270 34056 size -= len;
271
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 33903 times.
34056 } while (size > 0);
272
273 33903 f->wptr = wptr;
274
275 33903 return 0;
276 }
277
278 2782 static void flac_fifo_drain(FifoBuffer *f, size_t size)
279 {
280 2782 size_t size_cur = flac_fifo_size(f);
281
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2782 times.
2782 av_assert0(size_cur >= size);
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2782 times.
2782 if (size_cur == size)
284 f->empty = 1;
285
286 2782 f->rptr += size;
287
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 2639 times.
2782 if (f->rptr >= f->end)
288 143 f->rptr -= f->end - f->buffer;
289 2782 }
290
291 214 static int flac_fifo_alloc(FifoBuffer *f, size_t size)
292 {
293 214 memset(f, 0, sizeof(*f));
294
295 214 f->buffer = av_realloc(NULL, size);
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 214 times.
214 if (!f->buffer)
297 return AVERROR(ENOMEM);
298
299 214 f->wptr = f->buffer;
300 214 f->rptr = f->buffer;
301 214 f->end = f->buffer + size;
302
303 214 f->empty = 1;
304
305 214 return 0;
306 }
307
308 214 static void flac_fifo_free(FifoBuffer *f)
309 {
310 214 av_freep(&f->buffer);
311 214 memset(f, 0, sizeof(*f));
312 214 }
313
314 5050 static int find_headers_search_validate(FLACParseContext *fpc, int offset)
315 {
316 FLACFrameInfo fi;
317 uint8_t *header_buf;
318 5050 int size = 0;
319 5050 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 4172 times.
✓ Branch 2 taken 878 times.
5050 if (frame_header_is_valid(fpc->avctx, header_buf, &fi)) {
324 4172 FLACHeaderMarker **end_handle = &fpc->headers;
325 int i;
326
327 4172 size = 0;
328
2/2
✓ Branch 0 taken 30084 times.
✓ Branch 1 taken 4172 times.
34256 while (*end_handle) {
329 30084 end_handle = &(*end_handle)->next;
330 30084 size++;
331 }
332
333 4172 *end_handle = av_mallocz(sizeof(**end_handle));
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4172 times.
4172 if (!*end_handle) {
335 av_log(fpc->avctx, AV_LOG_ERROR,
336 "couldn't allocate FLACHeaderMarker\n");
337 return AVERROR(ENOMEM);
338 }
339 4172 (*end_handle)->fi = fi;
340 4172 (*end_handle)->offset = offset;
341
342
2/2
✓ Branch 0 taken 16688 times.
✓ Branch 1 taken 4172 times.
20860 for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++)
343 16688 (*end_handle)->link_penalty[i] = FLAC_HEADER_NOT_PENALIZED_YET;
344
345 4172 fpc->nb_headers_found++;
346 4172 size++;
347 }
348 5050 return size;
349 }
350
351 34061 static int find_headers_search(FLACParseContext *fpc, uint8_t *buf,
352 int buf_size, int search_start)
353 {
354 34061 int size = 0, mod_offset = (buf_size - 1) % 4, i, j;
355 uint32_t x;
356
357
2/2
✓ Branch 0 taken 736 times.
✓ Branch 1 taken 34061 times.
34797 for (i = 0; i < mod_offset; i++) {
358
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 671 times.
736 if ((AV_RB16(buf + i) & 0xFFFE) == 0xFFF8) {
359 65 int ret = find_headers_search_validate(fpc, search_start + i);
360 65 size = FFMAX(size, ret);
361 }
362 }
363
364
2/2
✓ Branch 0 taken 8655763 times.
✓ Branch 1 taken 34061 times.
8689824 for (; i < buf_size - 1; i += 4) {
365 8655763 x = AV_RN32(buf + i);
366
2/2
✓ Branch 0 taken 120403 times.
✓ Branch 1 taken 8535360 times.
8655763 if (((x & ~(x + 0x01010101)) & 0x80808080)) {
367
2/2
✓ Branch 0 taken 481612 times.
✓ Branch 1 taken 120403 times.
602015 for (j = 0; j < 4; j++) {
368
2/2
✓ Branch 0 taken 4985 times.
✓ Branch 1 taken 476627 times.
481612 if ((AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8) {
369 4985 int ret = find_headers_search_validate(fpc, search_start + i + j);
370 4985 size = FFMAX(size, ret);
371 }
372 }
373 }
374 }
375 34061 return size;
376 }
377
378 33903 static int find_new_headers(FLACParseContext *fpc, int search_start)
379 {
380 FLACHeaderMarker *end;
381 33903 int search_end, size = 0, read_len, temp;
382 uint8_t *buf;
383 33903 fpc->nb_headers_found = 0;
384
385 /* Search for a new header of at most 16 bytes. */
386 33903 search_end = flac_fifo_size(&fpc->fifo_buf) - (MAX_FRAME_HEADER_SIZE - 1);
387 33903 read_len = search_end - search_start + 1;
388 33903 buf = flac_fifo_read(&fpc->fifo_buf, search_start, &read_len);
389 33903 size = find_headers_search(fpc, buf, read_len, search_start);
390 33903 search_start += read_len - 1;
391
392 /* If fifo end was hit do the wrap around. */
393
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 33745 times.
33903 if (search_start != search_end) {
394 uint8_t wrap[2];
395
396 158 wrap[0] = buf[read_len - 1];
397 /* search_start + 1 is the post-wrap offset in the fifo. */
398 158 read_len = search_end - (search_start + 1) + 1;
399
400 158 buf = flac_fifo_read(&fpc->fifo_buf, search_start + 1, &read_len);
401 158 wrap[1] = buf[0];
402
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
158 if ((AV_RB16(wrap) & 0xFFFE) == 0xFFF8) {
404 temp = find_headers_search_validate(fpc, search_start);
405 size = FFMAX(size, temp);
406 }
407 158 search_start++;
408
409 /* Continue to do the last half of the wrap. */
410 158 temp = find_headers_search(fpc, buf, read_len, search_start);
411 158 size = FFMAX(size, temp);
412 158 search_start += read_len - 1;
413 }
414
415 /* Return the size even if no new headers were found. */
416
4/4
✓ Branch 0 taken 30076 times.
✓ Branch 1 taken 3827 times.
✓ Branch 2 taken 30025 times.
✓ Branch 3 taken 51 times.
33903 if (!size && fpc->headers)
417
2/2
✓ Branch 0 taken 261075 times.
✓ Branch 1 taken 30025 times.
291100 for (end = fpc->headers; end; end = end->next)
418 261075 size++;
419 33903 return size;
420 }
421
422 43060 static int check_header_fi_mismatch(FLACParseContext *fpc,
423 FLACFrameInfo *header_fi,
424 FLACFrameInfo *child_fi,
425 int log_level_offset)
426 {
427 43060 int deduction = 0;
428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43060 times.
43060 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 43060 times.
43060 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 43060 times.
43060 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 43060 times.
43060 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 43060 return deduction;
450 }
451
452 17844 static int check_header_mismatch(FLACParseContext *fpc,
453 FLACHeaderMarker *header,
454 FLACHeaderMarker *child,
455 int log_level_offset)
456 {
457 17844 FLACFrameInfo *header_fi = &header->fi, *child_fi = &child->fi;
458 17844 int check_crc, deduction, deduction_expected = 0, i;
459 17844 deduction = check_header_fi_mismatch(fpc, header_fi, child_fi,
460 log_level_offset);
461 /* Check sample and frame numbers. */
462 17844 if ((child_fi->frame_or_sample_num - header_fi->frame_or_sample_num
463
1/2
✓ Branch 0 taken 17844 times.
✗ Branch 1 not taken.
17844 != header_fi->blocksize) &&
464 17844 (child_fi->frame_or_sample_num
465
2/2
✓ Branch 0 taken 10968 times.
✓ Branch 1 taken 6876 times.
17844 != 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 10968 expected_frame_num = expected_sample_num = header_fi->frame_or_sample_num;
472 10968 curr = header;
473
2/2
✓ Branch 0 taken 32574 times.
✓ Branch 1 taken 10968 times.
43542 while (curr != child) {
474 /* Ignore frames that failed all crc checks */
475
1/2
✓ Branch 0 taken 32574 times.
✗ Branch 1 not taken.
32574 for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++) {
476
1/2
✓ Branch 0 taken 32574 times.
✗ Branch 1 not taken.
32574 if (curr->link_penalty[i] < FLAC_HEADER_CRC_FAIL_PENALTY) {
477 32574 expected_frame_num++;
478 32574 expected_sample_num += curr->fi.blocksize;
479 32574 break;
480 }
481 }
482 32574 curr = curr->next;
483 }
484
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10968 times.
10968 if (expected_frame_num == child_fi->frame_or_sample_num ||
486 expected_sample_num == child_fi->frame_or_sample_num)
487 10968 deduction_expected = deduction ? 0 : 1;
488
489 10968 deduction += FLAC_HEADER_CHANGED_PENALTY;
490 10968 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 17844 times.
✗ Branch 1 not taken.
17844 if (fpc->last_fi.is_var_size == header_fi->is_var_size) {
495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17844 times.
17844 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 17844 times.
✗ Branch 1 not taken.
17844 } else if (!fpc->last_fi.is_var_size &&
499
2/2
✓ Branch 0 taken 2885 times.
✓ Branch 1 taken 14959 times.
17844 fpc->last_fi.frame_or_sample_num + 1 == header_fi->frame_or_sample_num) {
500 2885 check_crc = 0;
501 } else {
502
3/4
✓ Branch 0 taken 4119 times.
✓ Branch 1 taken 10840 times.
✓ Branch 2 taken 4119 times.
✗ Branch 3 not taken.
14959 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 13725 times.
✓ Branch 1 taken 4119 times.
✓ Branch 2 taken 10968 times.
✓ Branch 3 taken 2757 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 10968 times.
17844 if (check_crc || (deduction && !deduction_expected)) {
510 FLACHeaderMarker *curr;
511 int read_len;
512 uint8_t *buf;
513 4119 uint32_t crc = 1;
514 4119 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 4119 curr = header->next;
519
2/4
✓ Branch 0 taken 4119 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4119 times.
4119 for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS && curr != child; i++)
520 curr = curr->next;
521
522
2/2
✓ Branch 0 taken 3951 times.
✓ Branch 1 taken 168 times.
4119 if (header->link_penalty[i] < FLAC_HEADER_CRC_FAIL_PENALTY ||
523
1/2
✓ Branch 0 taken 3951 times.
✗ Branch 1 not taken.
3951 header->link_penalty[i] == FLAC_HEADER_NOT_PENALIZED_YET) {
524 FLACHeaderMarker *start, *end;
525
526 /* Although overlapping chains are scored, the crc should never
527 have to be computed twice for a single byte. */
528 4119 start = header;
529 4119 end = child;
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4119 times.
4119 if (i > 0 &&
531 header->link_penalty[i - 1] >= FLAC_HEADER_CRC_FAIL_PENALTY) {
532 while (start->next != child)
533 start = start->next;
534 inverted_test = 1;
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4119 times.
4119 } else if (i > 0 &&
536 header->next->link_penalty[i-1] >=
537 FLAC_HEADER_CRC_FAIL_PENALTY ) {
538 end = header->next;
539 inverted_test = 1;
540 }
541
542 4119 read_len = end->offset - start->offset;
543 4119 buf = flac_fifo_read(&fpc->fifo_buf, start->offset, &read_len);
544 4119 crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf, read_len);
545 4119 read_len = (end->offset - start->offset) - read_len;
546
547
2/2
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 3974 times.
4119 if (read_len) {
548 145 buf = flac_fifo_read(&fpc->fifo_buf, end->offset - read_len, &read_len);
549 145 crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), crc, buf, read_len);
550 }
551 }
552
553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4119 times.
4119 if (!crc ^ !inverted_test) {
554 deduction += FLAC_HEADER_CRC_FAIL_PENALTY;
555 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
556 "crc check failed from offset %i (frame %"PRId64") to %i (frame %"PRId64")\n",
557 header->offset, header_fi->frame_or_sample_num,
558 child->offset, child_fi->frame_or_sample_num);
559 }
560 }
561 17844 return deduction;
562 }
563
564 /**
565 * Score a header.
566 *
567 * Give FLAC_HEADER_BASE_SCORE points to a frame for existing.
568 * If it has children, (subsequent frames of which the preceding CRC footer
569 * validates against this one,) then take the maximum score of the children,
570 * with a penalty of FLAC_HEADER_CHANGED_PENALTY applied for each change to
571 * bps, sample rate, channels, but not decorrelation mode, or blocksize,
572 * because it can change often.
573 **/
574 107335 static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header)
575 {
576 FLACHeaderMarker *child;
577 107335 int dist = 0;
578 int child_score;
579 107335 int base_score = FLAC_HEADER_BASE_SCORE;
580
2/2
✓ Branch 0 taken 80483 times.
✓ Branch 1 taken 26852 times.
107335 if (header->max_score != FLAC_HEADER_NOT_SCORED_YET)
581 80483 return header->max_score;
582
583 /* Modify the base score with changes from the last output header */
584
2/2
✓ Branch 0 taken 25216 times.
✓ Branch 1 taken 1636 times.
26852 if (fpc->last_fi_valid) {
585 /* Silence the log since this will be repeated if selected */
586 25216 base_score -= check_header_fi_mismatch(fpc, &fpc->last_fi, &header->fi,
587 AV_LOG_DEBUG);
588 }
589
590 26852 header->max_score = base_score;
591
592 /* Check and compute the children's scores. */
593 26852 child = header->next;
594
4/4
✓ Branch 0 taken 91244 times.
✓ Branch 1 taken 16091 times.
✓ Branch 2 taken 80483 times.
✓ Branch 3 taken 10761 times.
107335 for (dist = 0; dist < FLAC_MAX_SEQUENTIAL_HEADERS && child; dist++) {
595 /* Look at the child's frame header info and penalize suspicious
596 changes between the headers. */
597
2/2
✓ Branch 0 taken 14963 times.
✓ Branch 1 taken 65520 times.
80483 if (header->link_penalty[dist] == FLAC_HEADER_NOT_PENALIZED_YET) {
598 14963 header->link_penalty[dist] = check_header_mismatch(fpc, header,
599 child, AV_LOG_DEBUG);
600 }
601 80483 child_score = score_header(fpc, child) - header->link_penalty[dist];
602
603
2/2
✓ Branch 0 taken 24155 times.
✓ Branch 1 taken 56328 times.
80483 if (FLAC_HEADER_BASE_SCORE + child_score > header->max_score) {
604 /* Keep the child because the frame scoring is dynamic. */
605 24155 header->best_child = child;
606 24155 header->max_score = base_score + child_score;
607 }
608 80483 child = child->next;
609 }
610
611 26852 return header->max_score;
612 }
613
614 2697 static void score_sequences(FLACParseContext *fpc)
615 {
616 FLACHeaderMarker *curr;
617 2697 int best_score = FLAC_HEADER_NOT_SCORED_YET;
618 /* First pass to clear all old scores. */
619
2/2
✓ Branch 0 taken 26852 times.
✓ Branch 1 taken 2697 times.
29549 for (curr = fpc->headers; curr; curr = curr->next)
620 26852 curr->max_score = FLAC_HEADER_NOT_SCORED_YET;
621
622 /* Do a second pass to score them all. */
623
2/2
✓ Branch 0 taken 26852 times.
✓ Branch 1 taken 2697 times.
29549 for (curr = fpc->headers; curr; curr = curr->next) {
624
2/2
✓ Branch 1 taken 2697 times.
✓ Branch 2 taken 24155 times.
26852 if (score_header(fpc, curr) > best_score) {
625 2697 fpc->best_header = curr;
626 2697 best_score = curr->max_score;
627 }
628 }
629 2697 }
630
631 2917 static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf,
632 int *poutbuf_size)
633 {
634 2917 FLACHeaderMarker *header = fpc->best_header;
635 2917 FLACHeaderMarker *child = header->best_child;
636
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 2881 times.
2917 if (!child) {
637 36 *poutbuf_size = flac_fifo_size(&fpc->fifo_buf) - header->offset;
638 } else {
639 2881 *poutbuf_size = child->offset - header->offset;
640
641 /* If the child has suspicious changes, log them */
642 2881 check_header_mismatch(fpc, header, child, 0);
643 }
644
645 2917 ff_flac_set_channel_layout(fpc->avctx, header->fi.channels);
646
647 2917 fpc->avctx->sample_rate = header->fi.samplerate;
648 2917 fpc->pc->duration = header->fi.blocksize;
649 2917 *poutbuf = flac_fifo_read_wrap(fpc, header->offset, *poutbuf_size,
650 &fpc->wrap_buf,
651 &fpc->wrap_buf_allocated_size);
652
653
1/2
✓ Branch 0 taken 2917 times.
✗ Branch 1 not taken.
2917 if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) {
654
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2917 times.
2917 if (header->fi.is_var_size)
655 fpc->pc->pts = header->fi.frame_or_sample_num;
656
2/2
✓ Branch 0 taken 2881 times.
✓ Branch 1 taken 36 times.
2917 else if (header->best_child)
657 2881 fpc->pc->pts = header->fi.frame_or_sample_num * header->fi.blocksize;
658 }
659
660 2917 fpc->best_header_valid = 0;
661 2917 fpc->last_fi_valid = 1;
662 2917 fpc->last_fi = header->fi;
663
664 /* Return the negative overread index so the client can compute pos.
665 This should be the amount overread to the beginning of the child */
666
2/2
✓ Branch 0 taken 2881 times.
✓ Branch 1 taken 36 times.
2917 if (child) {
667 2881 int64_t offset = child->offset - flac_fifo_size(&fpc->fifo_buf);
668
1/2
✓ Branch 0 taken 2881 times.
✗ Branch 1 not taken.
2881 if (offset > -(1 << 28))
669 2881 return offset;
670 }
671 36 return 0;
672 }
673
674 36964 static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
675 const uint8_t **poutbuf, int *poutbuf_size,
676 const uint8_t *buf, int buf_size)
677 {
678 36964 FLACParseContext *fpc = s->priv_data;
679 FLACHeaderMarker *curr;
680 int nb_headers;
681 36964 const uint8_t *read_end = buf;
682 36964 const uint8_t *read_start = buf;
683
684
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 36829 times.
36964 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
685 FLACFrameInfo fi;
686
2/2
✓ Branch 1 taken 127 times.
✓ Branch 2 taken 8 times.
135 if (frame_header_is_valid(avctx, buf, &fi)) {
687 127 s->duration = fi.blocksize;
688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
127 if (!avctx->sample_rate)
689 avctx->sample_rate = fi.samplerate;
690
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
127 if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) {
691 fpc->pc->pts = fi.frame_or_sample_num;
692 if (!fi.is_var_size)
693 fpc->pc->pts *= fi.blocksize;
694 }
695 }
696 135 *poutbuf = buf;
697 135 *poutbuf_size = buf_size;
698 135 return buf_size;
699 }
700
701 36829 fpc->avctx = avctx;
702
4/4
✓ Branch 0 taken 2652 times.
✓ Branch 1 taken 34177 times.
✓ Branch 2 taken 2637 times.
✓ Branch 3 taken 15 times.
36829 if (fpc->best_header_valid && fpc->nb_headers_buffered >= FLAC_MIN_HEADERS)
703 2637 return get_best_header(fpc, poutbuf, poutbuf_size);
704
705 /* If a best_header was found last call remove it with the buffer data. */
706
4/4
✓ Branch 0 taken 2821 times.
✓ Branch 1 taken 31371 times.
✓ Branch 2 taken 2782 times.
✓ Branch 3 taken 39 times.
34192 if (fpc->best_header && fpc->best_header->best_child) {
707 FLACHeaderMarker *temp;
708 2782 FLACHeaderMarker *best_child = fpc->best_header->best_child;
709
710 /* Remove headers in list until the end of the best_header. */
711
2/2
✓ Branch 0 taken 2782 times.
✓ Branch 1 taken 2782 times.
5564 for (curr = fpc->headers; curr != best_child; curr = temp) {
712
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2782 times.
2782 if (curr != fpc->best_header) {
713 av_log(avctx, AV_LOG_DEBUG,
714 "dropping low score %i frame header from offset %i to %i\n",
715 curr->max_score, curr->offset, curr->next->offset);
716 }
717 2782 temp = curr->next;
718 2782 av_free(curr);
719 2782 fpc->nb_headers_buffered--;
720 }
721 /* Release returned data from ring buffer. */
722 2782 flac_fifo_drain(&fpc->fifo_buf, best_child->offset);
723
724 /* Fix the offset for the headers remaining to match the new buffer. */
725
2/2
✓ Branch 0 taken 21241 times.
✓ Branch 1 taken 2782 times.
24023 for (curr = best_child->next; curr; curr = curr->next)
726 21241 curr->offset -= best_child->offset;
727
728 2782 best_child->offset = 0;
729 2782 fpc->headers = best_child;
730
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 2753 times.
2782 if (fpc->nb_headers_buffered >= FLAC_MIN_HEADERS) {
731 29 fpc->best_header = best_child;
732 29 return get_best_header(fpc, poutbuf, poutbuf_size);
733 }
734 2753 fpc->best_header = NULL;
735
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 31371 times.
31410 } else if (fpc->best_header) {
736 /* No end frame no need to delete the buffer; probably eof */
737 FLACHeaderMarker *temp;
738
739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 for (curr = fpc->headers; curr != fpc->best_header; curr = temp) {
740 temp = curr->next;
741 av_free(curr);
742 fpc->nb_headers_buffered--;
743 }
744 39 fpc->headers = fpc->best_header->next;
745 39 av_freep(&fpc->best_header);
746 39 fpc->nb_headers_buffered--;
747 }
748
749 /* Find and score new headers. */
750 /* buf_size is zero when flushing, so check for this since we do */
751 /* not want to try to read more input once we have found the end. */
752 /* Also note that buf can't be NULL. */
753
2/2
✓ Branch 0 taken 33858 times.
✓ Branch 1 taken 2652 times.
70673 while ((buf_size && read_end < buf + buf_size &&
754
1/2
✓ Branch 0 taken 33858 times.
✗ Branch 1 not taken.
33858 fpc->nb_headers_buffered < FLAC_MIN_HEADERS)
755
6/6
✓ Branch 0 taken 36510 times.
✓ Branch 1 taken 350 times.
✓ Branch 2 taken 350 times.
✓ Branch 3 taken 2652 times.
✓ Branch 4 taken 45 times.
✓ Branch 5 taken 305 times.
39512 || (!buf_size && !fpc->end_padded)) {
756 int start_offset, ret;
757
758 /* Pad the end once if EOF, to check the final region for headers. */
759
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 33858 times.
33903 if (!buf_size) {
760 45 fpc->end_padded = 1;
761 45 read_end = read_start + MAX_FRAME_HEADER_SIZE;
762 } else {
763 /* The maximum read size is the upper-bound of what the parser
764 needs to have the required number of frames buffered */
765 33858 int nb_desired = FLAC_MIN_HEADERS - fpc->nb_headers_buffered + 1;
766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33858 times.
33858 read_end = read_end + FFMIN(buf + buf_size - read_end,
767 nb_desired * FLAC_AVG_FRAME_SIZE);
768 }
769
770
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 33902 times.
33903 if (!flac_fifo_space(&fpc->fifo_buf) &&
771 1 flac_fifo_size(&fpc->fifo_buf) / FLAC_AVG_FRAME_SIZE >
772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 fpc->nb_headers_buffered * 20) {
773 /* There is less than one valid flac header buffered for 20 headers
774 * buffered. Therefore the fifo is most likely filled with invalid
775 * data and the input is not a flac file. */
776 goto handle_error;
777 }
778
779 /* Fill the buffer. */
780
2/2
✓ Branch 0 taken 33858 times.
✓ Branch 1 taken 45 times.
33903 if (buf_size) {
781 33858 ret = flac_fifo_write(&fpc->fifo_buf, read_start,
782 33858 read_end - read_start);
783 } else {
784 45 int8_t pad[MAX_FRAME_HEADER_SIZE] = { 0 };
785 45 ret = flac_fifo_write(&fpc->fifo_buf, pad, sizeof(pad));
786 }
787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33903 times.
33903 if (ret < 0) {
788 av_log(avctx, AV_LOG_ERROR, "Error buffering data\n");
789 goto handle_error;
790 }
791
792 /* Tag headers and update sequences. */
793 33903 start_offset = flac_fifo_size(&fpc->fifo_buf) -
794 33903 ((read_end - read_start) + (MAX_FRAME_HEADER_SIZE - 1));
795 33903 start_offset = FFMAX(0, start_offset);
796 33903 nb_headers = find_new_headers(fpc, start_offset);
797
798
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33903 times.
33903 if (nb_headers < 0) {
799 av_log(avctx, AV_LOG_ERROR,
800 "find_new_headers couldn't allocate FLAC header\n");
801 goto handle_error;
802 }
803
804 33903 fpc->nb_headers_buffered = nb_headers;
805 /* Wait till FLAC_MIN_HEADERS to output a valid frame. */
806
4/4
✓ Branch 0 taken 33858 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 31206 times.
✓ Branch 3 taken 2652 times.
33903 if (!fpc->end_padded && fpc->nb_headers_buffered < FLAC_MIN_HEADERS) {
807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31206 times.
31206 if (read_end < buf + buf_size) {
808 read_start = read_end;
809 continue;
810 } else {
811 31206 goto handle_error;
812 }
813 }
814
815 /* If headers found, update the scores since we have longer chains. */
816
3/4
✓ Branch 0 taken 2652 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 2652 times.
✗ Branch 3 not taken.
2697 if (fpc->end_padded || fpc->nb_headers_found)
817 2697 score_sequences(fpc);
818
819 /* restore the state pre-padding */
820
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 2652 times.
2697 if (fpc->end_padded) {
821 45 int empty = flac_fifo_size(&fpc->fifo_buf) == MAX_FRAME_HEADER_SIZE;
822 45 int warp = fpc->fifo_buf.wptr - fpc->fifo_buf.buffer < MAX_FRAME_HEADER_SIZE;
823 /* HACK: drain the tail of the fifo */
824 45 fpc->fifo_buf.wptr -= MAX_FRAME_HEADER_SIZE;
825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (warp) {
826 fpc->fifo_buf.wptr += fpc->fifo_buf.end -
827 fpc->fifo_buf.buffer;
828 }
829 45 fpc->fifo_buf.empty = empty;
830 45 read_start = read_end = NULL;
831 }
832 }
833
834
2/2
✓ Branch 0 taken 27797 times.
✓ Branch 1 taken 2957 times.
30754 for (curr = fpc->headers; curr; curr = curr->next) {
835
3/4
✓ Branch 0 taken 27576 times.
✓ Branch 1 taken 221 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27576 times.
27797 if (!fpc->best_header || curr->max_score > fpc->best_header->max_score) {
836 221 fpc->best_header = curr;
837 }
838 }
839
840
3/4
✓ Branch 0 taken 2918 times.
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2918 times.
2957 if (fpc->best_header && fpc->best_header->max_score <= 0) {
841 // Only accept a bad header if there is no other option to continue
842 if (!buf_size || read_end != buf || fpc->nb_headers_buffered < FLAC_MIN_HEADERS)
843 fpc->best_header = NULL;
844 }
845
846
2/2
✓ Branch 0 taken 2918 times.
✓ Branch 1 taken 39 times.
2957 if (fpc->best_header) {
847 2918 fpc->best_header_valid = 1;
848
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 2806 times.
2918 if (fpc->best_header->offset > 0) {
849 /* Output a junk frame. */
850 112 av_log(avctx, AV_LOG_DEBUG, "Junk frame till offset %i\n",
851 112 fpc->best_header->offset);
852
853 /* Set duration to 0. It is unknown or invalid in a junk frame. */
854 112 s->duration = 0;
855 112 *poutbuf_size = fpc->best_header->offset;
856 112 *poutbuf = flac_fifo_read_wrap(fpc, 0, *poutbuf_size,
857 &fpc->wrap_buf,
858 &fpc->wrap_buf_allocated_size);
859
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 15 times.
127 return buf_size ? (read_end - buf) : (fpc->best_header->offset -
860 15 flac_fifo_size(&fpc->fifo_buf));
861 }
862
2/2
✓ Branch 0 taken 2555 times.
✓ Branch 1 taken 251 times.
2806 if (!buf_size)
863 251 return get_best_header(fpc, poutbuf, poutbuf_size);
864 }
865
866 2594 handle_error:
867 33800 *poutbuf = NULL;
868 33800 *poutbuf_size = 0;
869
2/2
✓ Branch 0 taken 33761 times.
✓ Branch 1 taken 39 times.
33800 return buf_size ? read_end - buf : 0;
870 }
871
872 214 static av_cold int flac_parse_init(AVCodecParserContext *c)
873 {
874 214 FLACParseContext *fpc = c->priv_data;
875 int ret;
876
877 214 fpc->pc = c;
878 /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before
879 it drains. This is allocated early to avoid slow reallocation. */
880 214 ret = flac_fifo_alloc(&fpc->fifo_buf, (FLAC_MIN_HEADERS + 3) * FLAC_AVG_FRAME_SIZE);
881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 214 times.
214 if (ret < 0) {
882 av_log(fpc->avctx, AV_LOG_ERROR,
883 "couldn't allocate fifo_buf\n");
884 return AVERROR(ENOMEM);
885 }
886 214 return 0;
887 }
888
889 214 static void flac_parse_close(AVCodecParserContext *c)
890 {
891 214 FLACParseContext *fpc = c->priv_data;
892 214 FLACHeaderMarker *curr = fpc->headers, *temp;
893
894
2/2
✓ Branch 0 taken 1351 times.
✓ Branch 1 taken 214 times.
1565 while (curr) {
895 1351 temp = curr->next;
896 1351 av_free(curr);
897 1351 curr = temp;
898 }
899 214 fpc->headers = NULL;
900 214 flac_fifo_free(&fpc->fifo_buf);
901 214 av_freep(&fpc->wrap_buf);
902 214 }
903
904 const AVCodecParser ff_flac_parser = {
905 .codec_ids = { AV_CODEC_ID_FLAC },
906 .priv_data_size = sizeof(FLACParseContext),
907 .parser_init = flac_parse_init,
908 .parser_parse = flac_parse,
909 .parser_close = flac_parse_close,
910 };
911