FFmpeg coverage


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