FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/flac_parser.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 266 337 78.9%
Branches: 153 218 70.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/fifo.h"
38 #include "bytestream.h"
39 #include "parser.h"
40 #include "flac.h"
41
42 /** maximum number of adjacent headers that compare CRCs against each other */
43 #define FLAC_MAX_SEQUENTIAL_HEADERS 4
44 /** minimum number of headers buffered and checked before returning frames */
45 #define FLAC_MIN_HEADERS 10
46 /** estimate for average size of a FLAC frame */
47 #define FLAC_AVG_FRAME_SIZE 8192
48
49 /** scoring settings for score_header */
50 #define FLAC_HEADER_BASE_SCORE 10
51 #define FLAC_HEADER_CHANGED_PENALTY 7
52 #define FLAC_HEADER_CRC_FAIL_PENALTY 50
53 #define FLAC_HEADER_NOT_PENALIZED_YET 100000
54 #define FLAC_HEADER_NOT_SCORED_YET -100000
55
56 /** largest possible size of flac header */
57 #define MAX_FRAME_HEADER_SIZE 16
58
59 typedef struct FLACHeaderMarker {
60 int offset; /**< byte offset from start of FLACParseContext->buffer */
61 int link_penalty[FLAC_MAX_SEQUENTIAL_HEADERS]; /**< array of local scores
62 between this header and the one at a distance equal
63 array position */
64 int max_score; /**< maximum score found after checking each child that
65 has a valid CRC */
66 FLACFrameInfo fi; /**< decoded frame header info */
67 struct FLACHeaderMarker *next; /**< next CRC-8 verified header that
68 immediately follows this one in
69 the bytestream */
70 struct FLACHeaderMarker *best_child; /**< following frame header with
71 which this frame has the best
72 score with */
73 } FLACHeaderMarker;
74
75 typedef struct FLACParseContext {
76 AVCodecParserContext *pc; /**< parent context */
77 AVCodecContext *avctx; /**< codec context pointer for logging */
78 FLACHeaderMarker *headers; /**< linked-list that starts at the first
79 CRC-8 verified header within buffer */
80 FLACHeaderMarker *best_header; /**< highest scoring header within buffer */
81 int nb_headers_found; /**< number of headers found in the last
82 flac_parse() call */
83 int nb_headers_buffered; /**< number of headers that are buffered */
84 int best_header_valid; /**< flag set when the parser returns junk;
85 if set return best_header next time */
86 AVFifoBuffer *fifo_buf; /**< buffer to store all data until headers
87 can be verified */
88 int end_padded; /**< specifies if fifo_buf's end is padded */
89 uint8_t *wrap_buf; /**< general fifo read buffer when wrapped */
90 int wrap_buf_allocated_size; /**< actual allocated size of the buffer */
91 FLACFrameInfo last_fi; /**< last decoded frame header info */
92 int last_fi_valid; /**< set if last_fi is valid */
93 } FLACParseContext;
94
95 4714 static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf,
96 FLACFrameInfo *fi)
97 {
98 GetBitContext gb;
99 4714 init_get_bits(&gb, buf, MAX_FRAME_HEADER_SIZE * 8);
100 4714 return !ff_flac_decode_frame_header(avctx, &gb, fi, 127);
101 }
102
103 /**
104 * Non-destructive fast fifo pointer fetching
105 * Returns a pointer from the specified offset.
106 * If possible the pointer points within the fifo buffer.
107 * Otherwise (if it would cause a wrap around,) a pointer to a user-specified
108 * buffer is used.
109 * The pointer can be NULL. In any case it will be reallocated to hold the size.
110 * If the returned pointer will be used after subsequent calls to flac_fifo_read_wrap
111 * then the subsequent calls should pass in a different wrap_buf so as to not
112 * overwrite the contents of the previous wrap_buf.
113 * This function is based on av_fifo_generic_read, which is why there is a comment
114 * about a memory barrier for SMP.
115 */
116 7309 static uint8_t *flac_fifo_read_wrap(FLACParseContext *fpc, int offset, int len,
117 uint8_t **wrap_buf, int *allocated_size)
118 {
119 7309 AVFifoBuffer *f = fpc->fifo_buf;
120 7309 uint8_t *start = f->rptr + offset;
121 uint8_t *tmp_buf;
122
123
2/2
✓ Branch 0 taken 1343 times.
✓ Branch 1 taken 5966 times.
7309 if (start >= f->end)
124 1343 start -= f->end - f->buffer;
125
2/2
✓ Branch 0 taken 7186 times.
✓ Branch 1 taken 123 times.
7309 if (f->end - start >= len)
126 7186 return start;
127
128 123 tmp_buf = av_fast_realloc(*wrap_buf, allocated_size, len);
129
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 123 times.
123 if (!tmp_buf) {
131 av_log(fpc->avctx, AV_LOG_ERROR,
132 "couldn't reallocate wrap buffer of size %d", len);
133 return NULL;
134 }
135 123 *wrap_buf = tmp_buf;
136 do {
137 246 int seg_len = FFMIN(f->end - start, len);
138 246 memcpy(tmp_buf, start, seg_len);
139 246 tmp_buf = (uint8_t*)tmp_buf + seg_len;
140 // memory barrier needed for SMP here in theory
141
142 246 start += seg_len - (f->end - f->buffer);
143 246 len -= seg_len;
144
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 123 times.
246 } while (len > 0);
145
146 123 return *wrap_buf;
147 }
148
149 /**
150 * Return a pointer in the fifo buffer where the offset starts at until
151 * the wrap point or end of request.
152 * len will contain the valid length of the returned buffer.
153 * A second call to flac_fifo_read (with new offset and len) should be called
154 * to get the post-wrap buf if the returned len is less than the requested.
155 **/
156 29651 static uint8_t *flac_fifo_read(FLACParseContext *fpc, int offset, int *len)
157 {
158 29651 AVFifoBuffer *f = fpc->fifo_buf;
159 29651 uint8_t *start = f->rptr + offset;
160
161
2/2
✓ Branch 0 taken 13454 times.
✓ Branch 1 taken 16197 times.
29651 if (start >= f->end)
162 13454 start -= f->end - f->buffer;
163 29651 *len = FFMIN(*len, f->end - start);
164 29651 return start;
165 }
166
167 4591 static int find_headers_search_validate(FLACParseContext *fpc, int offset)
168 {
169 FLACFrameInfo fi;
170 uint8_t *header_buf;
171 4591 int size = 0;
172 4591 header_buf = flac_fifo_read_wrap(fpc, offset,
173 MAX_FRAME_HEADER_SIZE,
174 &fpc->wrap_buf,
175 &fpc->wrap_buf_allocated_size);
176
2/2
✓ Branch 1 taken 3837 times.
✓ Branch 2 taken 754 times.
4591 if (frame_header_is_valid(fpc->avctx, header_buf, &fi)) {
177 3837 FLACHeaderMarker **end_handle = &fpc->headers;
178 int i;
179
180 3837 size = 0;
181
2/2
✓ Branch 0 taken 27422 times.
✓ Branch 1 taken 3837 times.
31259 while (*end_handle) {
182 27422 end_handle = &(*end_handle)->next;
183 27422 size++;
184 }
185
186 3837 *end_handle = av_mallocz(sizeof(**end_handle));
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3837 times.
3837 if (!*end_handle) {
188 av_log(fpc->avctx, AV_LOG_ERROR,
189 "couldn't allocate FLACHeaderMarker\n");
190 return AVERROR(ENOMEM);
191 }
192 3837 (*end_handle)->fi = fi;
193 3837 (*end_handle)->offset = offset;
194
195
2/2
✓ Branch 0 taken 15348 times.
✓ Branch 1 taken 3837 times.
19185 for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++)
196 15348 (*end_handle)->link_penalty[i] = FLAC_HEADER_NOT_PENALIZED_YET;
197
198 3837 fpc->nb_headers_found++;
199 3837 size++;
200 }
201 4591 return size;
202 }
203
204 29651 static int find_headers_search(FLACParseContext *fpc, uint8_t *buf,
205 int buf_size, int search_start)
206 {
207 29651 int size = 0, mod_offset = (buf_size - 1) % 4, i, j;
208 uint32_t x;
209
210
2/2
✓ Branch 0 taken 674 times.
✓ Branch 1 taken 29651 times.
30325 for (i = 0; i < mod_offset; i++) {
211
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 613 times.
674 if ((AV_RB16(buf + i) & 0xFFFE) == 0xFFF8) {
212 61 int ret = find_headers_search_validate(fpc, search_start + i);
213 61 size = FFMAX(size, ret);
214 }
215 }
216
217
2/2
✓ Branch 0 taken 7537057 times.
✓ Branch 1 taken 29651 times.
7566708 for (; i < buf_size - 1; i += 4) {
218 7537057 x = AV_RN32(buf + i);
219
2/2
✓ Branch 0 taken 105077 times.
✓ Branch 1 taken 7431980 times.
7537057 if (((x & ~(x + 0x01010101)) & 0x80808080)) {
220
2/2
✓ Branch 0 taken 420308 times.
✓ Branch 1 taken 105077 times.
525385 for (j = 0; j < 4; j++) {
221
2/2
✓ Branch 0 taken 4530 times.
✓ Branch 1 taken 415778 times.
420308 if ((AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8) {
222 4530 int ret = find_headers_search_validate(fpc, search_start + i + j);
223 4530 size = FFMAX(size, ret);
224 }
225 }
226 }
227 }
228 29651 return size;
229 }
230
231 29513 static int find_new_headers(FLACParseContext *fpc, int search_start)
232 {
233 FLACHeaderMarker *end;
234 29513 int search_end, size = 0, read_len, temp;
235 uint8_t *buf;
236 29513 fpc->nb_headers_found = 0;
237
238 /* Search for a new header of at most 16 bytes. */
239 29513 search_end = av_fifo_size(fpc->fifo_buf) - (MAX_FRAME_HEADER_SIZE - 1);
240 29513 read_len = search_end - search_start + 1;
241 29513 buf = flac_fifo_read(fpc, search_start, &read_len);
242 29513 size = find_headers_search(fpc, buf, read_len, search_start);
243 29513 search_start += read_len - 1;
244
245 /* If fifo end was hit do the wrap around. */
246
2/2
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 29375 times.
29513 if (search_start != search_end) {
247 uint8_t wrap[2];
248
249 138 wrap[0] = buf[read_len - 1];
250 /* search_start + 1 is the post-wrap offset in the fifo. */
251 138 read_len = search_end - (search_start + 1) + 1;
252
253 138 buf = flac_fifo_read(fpc, search_start + 1, &read_len);
254 138 wrap[1] = buf[0];
255
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
138 if ((AV_RB16(wrap) & 0xFFFE) == 0xFFF8) {
257 temp = find_headers_search_validate(fpc, search_start);
258 size = FFMAX(size, temp);
259 }
260 138 search_start++;
261
262 /* Continue to do the last half of the wrap. */
263 138 temp = find_headers_search(fpc, buf, read_len, search_start);
264 138 size = FFMAX(size, temp);
265 138 search_start += read_len - 1;
266 }
267
268 /* Return the size even if no new headers were found. */
269
4/4
✓ Branch 0 taken 26019 times.
✓ Branch 1 taken 3494 times.
✓ Branch 2 taken 25970 times.
✓ Branch 3 taken 49 times.
29513 if (!size && fpc->headers)
270
2/2
✓ Branch 0 taken 225404 times.
✓ Branch 1 taken 25970 times.
251374 for (end = fpc->headers; end; end = end->next)
271 225404 size++;
272 29513 return size;
273 }
274
275 38696 static int check_header_fi_mismatch(FLACParseContext *fpc,
276 FLACFrameInfo *header_fi,
277 FLACFrameInfo *child_fi,
278 int log_level_offset)
279 {
280 38696 int deduction = 0;
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38696 times.
38696 if (child_fi->samplerate != header_fi->samplerate) {
282 deduction += FLAC_HEADER_CHANGED_PENALTY;
283 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
284 "sample rate change detected in adjacent frames\n");
285 }
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38696 times.
38696 if (child_fi->bps != header_fi->bps) {
287 deduction += FLAC_HEADER_CHANGED_PENALTY;
288 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
289 "bits per sample change detected in adjacent frames\n");
290 }
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38696 times.
38696 if (child_fi->is_var_size != header_fi->is_var_size) {
292 /* Changing blocking strategy not allowed per the spec */
293 deduction += FLAC_HEADER_BASE_SCORE;
294 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
295 "blocking strategy change detected in adjacent frames\n");
296 }
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38696 times.
38696 if (child_fi->channels != header_fi->channels) {
298 deduction += FLAC_HEADER_CHANGED_PENALTY;
299 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
300 "number of channels change detected in adjacent frames\n");
301 }
302 38696 return deduction;
303 }
304
305 16309 static int check_header_mismatch(FLACParseContext *fpc,
306 FLACHeaderMarker *header,
307 FLACHeaderMarker *child,
308 int log_level_offset)
309 {
310 16309 FLACFrameInfo *header_fi = &header->fi, *child_fi = &child->fi;
311 16309 int deduction, deduction_expected = 0, i;
312 16309 deduction = check_header_fi_mismatch(fpc, header_fi, child_fi,
313 log_level_offset);
314 /* Check sample and frame numbers. */
315 16309 if ((child_fi->frame_or_sample_num - header_fi->frame_or_sample_num
316
1/2
✓ Branch 0 taken 16309 times.
✗ Branch 1 not taken.
16309 != header_fi->blocksize) &&
317 16309 (child_fi->frame_or_sample_num
318
2/2
✓ Branch 0 taken 10059 times.
✓ Branch 1 taken 6250 times.
16309 != header_fi->frame_or_sample_num + 1)) {
319 FLACHeaderMarker *curr;
320 int64_t expected_frame_num, expected_sample_num;
321 /* If there are frames in the middle we expect this deduction,
322 as they are probably valid and this one follows it */
323
324 10059 expected_frame_num = expected_sample_num = header_fi->frame_or_sample_num;
325 10059 curr = header;
326
2/2
✓ Branch 0 taken 29865 times.
✓ Branch 1 taken 10059 times.
39924 while (curr != child) {
327 /* Ignore frames that failed all crc checks */
328
1/2
✓ Branch 0 taken 29865 times.
✗ Branch 1 not taken.
29865 for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++) {
329
1/2
✓ Branch 0 taken 29865 times.
✗ Branch 1 not taken.
29865 if (curr->link_penalty[i] < FLAC_HEADER_CRC_FAIL_PENALTY) {
330 29865 expected_frame_num++;
331 29865 expected_sample_num += curr->fi.blocksize;
332 29865 break;
333 }
334 }
335 29865 curr = curr->next;
336 }
337
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10059 times.
10059 if (expected_frame_num == child_fi->frame_or_sample_num ||
339 expected_sample_num == child_fi->frame_or_sample_num)
340 10059 deduction_expected = deduction ? 0 : 1;
341
342 10059 deduction += FLAC_HEADER_CHANGED_PENALTY;
343 10059 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
344 "sample/frame number mismatch in adjacent frames\n");
345 }
346
347 /* If we have suspicious headers, check the CRC between them */
348
3/4
✓ Branch 0 taken 10059 times.
✓ Branch 1 taken 6250 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10059 times.
16309 if (deduction && !deduction_expected) {
349 FLACHeaderMarker *curr;
350 int read_len;
351 uint8_t *buf;
352 uint32_t crc = 1;
353 int inverted_test = 0;
354
355 /* Since CRC is expensive only do it if we haven't yet.
356 This assumes a CRC penalty is greater than all other check penalties */
357 curr = header->next;
358 for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS && curr != child; i++)
359 curr = curr->next;
360
361 if (header->link_penalty[i] < FLAC_HEADER_CRC_FAIL_PENALTY ||
362 header->link_penalty[i] == FLAC_HEADER_NOT_PENALIZED_YET) {
363 FLACHeaderMarker *start, *end;
364
365 /* Although overlapping chains are scored, the crc should never
366 have to be computed twice for a single byte. */
367 start = header;
368 end = child;
369 if (i > 0 &&
370 header->link_penalty[i - 1] >= FLAC_HEADER_CRC_FAIL_PENALTY) {
371 while (start->next != child)
372 start = start->next;
373 inverted_test = 1;
374 } else if (i > 0 &&
375 header->next->link_penalty[i-1] >=
376 FLAC_HEADER_CRC_FAIL_PENALTY ) {
377 end = header->next;
378 inverted_test = 1;
379 }
380
381 read_len = end->offset - start->offset;
382 buf = flac_fifo_read(fpc, start->offset, &read_len);
383 crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf, read_len);
384 read_len = (end->offset - start->offset) - read_len;
385
386 if (read_len) {
387 buf = flac_fifo_read(fpc, end->offset - read_len, &read_len);
388 crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), crc, buf, read_len);
389 }
390 }
391
392 if (!crc ^ !inverted_test) {
393 deduction += FLAC_HEADER_CRC_FAIL_PENALTY;
394 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
395 "crc check failed from offset %i (frame %"PRId64") to %i (frame %"PRId64")\n",
396 header->offset, header_fi->frame_or_sample_num,
397 child->offset, child_fi->frame_or_sample_num);
398 }
399 }
400 16309 return deduction;
401 }
402
403 /**
404 * Score a header.
405 *
406 * Give FLAC_HEADER_BASE_SCORE points to a frame for existing.
407 * If it has children, (subsequent frames of which the preceding CRC footer
408 * validates against this one,) then take the maximum score of the children,
409 * with a penalty of FLAC_HEADER_CHANGED_PENALTY applied for each change to
410 * bps, sample rate, channels, but not decorrelation mode, or blocksize,
411 * because it can change often.
412 **/
413 95861 static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header)
414 {
415 FLACHeaderMarker *child;
416 95861 int dist = 0;
417 int child_score;
418 95861 int base_score = FLAC_HEADER_BASE_SCORE;
419
2/2
✓ Branch 0 taken 71891 times.
✓ Branch 1 taken 23970 times.
95861 if (header->max_score != FLAC_HEADER_NOT_SCORED_YET)
420 71891 return header->max_score;
421
422 /* Modify the base score with changes from the last output header */
423
2/2
✓ Branch 0 taken 22387 times.
✓ Branch 1 taken 1583 times.
23970 if (fpc->last_fi_valid) {
424 /* Silence the log since this will be repeated if selected */
425 22387 base_score -= check_header_fi_mismatch(fpc, &fpc->last_fi, &header->fi,
426 AV_LOG_DEBUG);
427 }
428
429 23970 header->max_score = base_score;
430
431 /* Check and compute the children's scores. */
432 23970 child = header->next;
433
4/4
✓ Branch 0 taken 81479 times.
✓ Branch 1 taken 14382 times.
✓ Branch 2 taken 71891 times.
✓ Branch 3 taken 9588 times.
95861 for (dist = 0; dist < FLAC_MAX_SEQUENTIAL_HEADERS && child; dist++) {
434 /* Look at the child's frame header info and penalize suspicious
435 changes between the headers. */
436
2/2
✓ Branch 0 taken 13729 times.
✓ Branch 1 taken 58162 times.
71891 if (header->link_penalty[dist] == FLAC_HEADER_NOT_PENALIZED_YET) {
437 13729 header->link_penalty[dist] = check_header_mismatch(fpc, header,
438 child, AV_LOG_DEBUG);
439 }
440 71891 child_score = score_header(fpc, child) - header->link_penalty[dist];
441
442
2/2
✓ Branch 0 taken 21566 times.
✓ Branch 1 taken 50325 times.
71891 if (FLAC_HEADER_BASE_SCORE + child_score > header->max_score) {
443 /* Keep the child because the frame scoring is dynamic. */
444 21566 header->best_child = child;
445 21566 header->max_score = base_score + child_score;
446 }
447 71891 child = child->next;
448 }
449
450 23970 return header->max_score;
451 }
452
453 2404 static void score_sequences(FLACParseContext *fpc)
454 {
455 FLACHeaderMarker *curr;
456 2404 int best_score = 0;//FLAC_HEADER_NOT_SCORED_YET;
457 /* First pass to clear all old scores. */
458
2/2
✓ Branch 0 taken 23970 times.
✓ Branch 1 taken 2404 times.
26374 for (curr = fpc->headers; curr; curr = curr->next)
459 23970 curr->max_score = FLAC_HEADER_NOT_SCORED_YET;
460
461 /* Do a second pass to score them all. */
462
2/2
✓ Branch 0 taken 23970 times.
✓ Branch 1 taken 2404 times.
26374 for (curr = fpc->headers; curr; curr = curr->next) {
463
2/2
✓ Branch 1 taken 2404 times.
✓ Branch 2 taken 21566 times.
23970 if (score_header(fpc, curr) > best_score) {
464 2404 fpc->best_header = curr;
465 2404 best_score = curr->max_score;
466 }
467 }
468 2404 }
469
470 2612 static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf,
471 int *poutbuf_size)
472 {
473 2612 FLACHeaderMarker *header = fpc->best_header;
474 2612 FLACHeaderMarker *child = header->best_child;
475
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2580 times.
2612 if (!child) {
476 32 *poutbuf_size = av_fifo_size(fpc->fifo_buf) - header->offset;
477 } else {
478 2580 *poutbuf_size = child->offset - header->offset;
479
480 /* If the child has suspicious changes, log them */
481 2580 check_header_mismatch(fpc, header, child, 0);
482 }
483
484
1/2
✓ Branch 0 taken 2612 times.
✗ Branch 1 not taken.
2612 if (header->fi.channels != fpc->avctx->channels ||
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2612 times.
2612 !fpc->avctx->channel_layout) {
486 fpc->avctx->channels = header->fi.channels;
487 ff_flac_set_channel_layout(fpc->avctx);
488 }
489 2612 fpc->avctx->sample_rate = header->fi.samplerate;
490 2612 fpc->pc->duration = header->fi.blocksize;
491 2612 *poutbuf = flac_fifo_read_wrap(fpc, header->offset, *poutbuf_size,
492 &fpc->wrap_buf,
493 &fpc->wrap_buf_allocated_size);
494
495
496
1/2
✓ Branch 0 taken 2612 times.
✗ Branch 1 not taken.
2612 if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) {
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2612 times.
2612 if (header->fi.is_var_size)
498 fpc->pc->pts = header->fi.frame_or_sample_num;
499
2/2
✓ Branch 0 taken 2580 times.
✓ Branch 1 taken 32 times.
2612 else if (header->best_child)
500 2580 fpc->pc->pts = header->fi.frame_or_sample_num * header->fi.blocksize;
501 }
502
503 2612 fpc->best_header_valid = 0;
504 2612 fpc->last_fi_valid = 1;
505 2612 fpc->last_fi = header->fi;
506
507 /* Return the negative overread index so the client can compute pos.
508 This should be the amount overread to the beginning of the child */
509
2/2
✓ Branch 0 taken 2580 times.
✓ Branch 1 taken 32 times.
2612 if (child)
510 2580 return child->offset - av_fifo_size(fpc->fifo_buf);
511 32 return 0;
512 }
513
514 32254 static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
515 const uint8_t **poutbuf, int *poutbuf_size,
516 const uint8_t *buf, int buf_size)
517 {
518 32254 FLACParseContext *fpc = s->priv_data;
519 FLACHeaderMarker *curr;
520 int nb_headers;
521 32254 const uint8_t *read_end = buf;
522 32254 const uint8_t *read_start = buf;
523
524
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 32131 times.
32254 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
525 FLACFrameInfo fi;
526
2/2
✓ Branch 1 taken 115 times.
✓ Branch 2 taken 8 times.
123 if (frame_header_is_valid(avctx, buf, &fi)) {
527 115 s->duration = fi.blocksize;
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
115 if (!avctx->sample_rate)
529 avctx->sample_rate = fi.samplerate;
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
115 if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) {
531 fpc->pc->pts = fi.frame_or_sample_num;
532 if (!fi.is_var_size)
533 fpc->pc->pts *= fi.blocksize;
534 }
535 }
536 123 *poutbuf = buf;
537 123 *poutbuf_size = buf_size;
538 123 return buf_size;
539 }
540
541 32131 fpc->avctx = avctx;
542
2/2
✓ Branch 0 taken 2363 times.
✓ Branch 1 taken 29768 times.
32131 if (fpc->best_header_valid)
543 2363 return get_best_header(fpc, poutbuf, poutbuf_size);
544
545 /* If a best_header was found last call remove it with the buffer data. */
546
4/4
✓ Branch 0 taken 2504 times.
✓ Branch 1 taken 27264 times.
✓ Branch 2 taken 2472 times.
✓ Branch 3 taken 32 times.
29768 if (fpc->best_header && fpc->best_header->best_child) {
547 FLACHeaderMarker *temp;
548 2472 FLACHeaderMarker *best_child = fpc->best_header->best_child;
549
550 /* Remove headers in list until the end of the best_header. */
551
2/2
✓ Branch 0 taken 2472 times.
✓ Branch 1 taken 2472 times.
4944 for (curr = fpc->headers; curr != best_child; curr = temp) {
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2472 times.
2472 if (curr != fpc->best_header) {
553 av_log(avctx, AV_LOG_DEBUG,
554 "dropping low score %i frame header from offset %i to %i\n",
555 curr->max_score, curr->offset, curr->next->offset);
556 }
557 2472 temp = curr->next;
558 2472 av_free(curr);
559 2472 fpc->nb_headers_buffered--;
560 }
561 /* Release returned data from ring buffer. */
562 2472 av_fifo_drain(fpc->fifo_buf, best_child->offset);
563
564 /* Fix the offset for the headers remaining to match the new buffer. */
565
2/2
✓ Branch 0 taken 18925 times.
✓ Branch 1 taken 2472 times.
21397 for (curr = best_child->next; curr; curr = curr->next)
566 18925 curr->offset -= best_child->offset;
567
568 2472 best_child->offset = 0;
569 2472 fpc->headers = best_child;
570
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 2443 times.
2472 if (fpc->nb_headers_buffered >= FLAC_MIN_HEADERS) {
571 29 fpc->best_header = best_child;
572 29 return get_best_header(fpc, poutbuf, poutbuf_size);
573 }
574 2443 fpc->best_header = NULL;
575
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 27264 times.
27296 } else if (fpc->best_header) {
576 /* No end frame no need to delete the buffer; probably eof */
577 FLACHeaderMarker *temp;
578
579
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 for (curr = fpc->headers; curr != fpc->best_header; curr = temp) {
580 temp = curr->next;
581 av_free(curr);
582 fpc->nb_headers_buffered--;
583 }
584 32 fpc->headers = fpc->best_header->next;
585 32 av_freep(&fpc->best_header);
586 32 fpc->nb_headers_buffered--;
587 }
588
589 /* Find and score new headers. */
590 /* buf_size is zero when flushing, so check for this since we do */
591 /* not want to try to read more input once we have found the end. */
592 /* Also note that buf can't be NULL. */
593
2/2
✓ Branch 0 taken 29478 times.
✓ Branch 1 taken 2369 times.
61586 while ((buf_size && read_end < buf + buf_size &&
594
1/2
✓ Branch 0 taken 29478 times.
✗ Branch 1 not taken.
29478 fpc->nb_headers_buffered < FLAC_MIN_HEADERS)
595
6/6
✓ Branch 0 taken 31847 times.
✓ Branch 1 taken 296 times.
✓ Branch 2 taken 296 times.
✓ Branch 3 taken 2369 times.
✓ Branch 4 taken 35 times.
✓ Branch 5 taken 261 times.
34512 || (!buf_size && !fpc->end_padded)) {
596 int start_offset;
597
598 /* Pad the end once if EOF, to check the final region for headers. */
599
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 29478 times.
29513 if (!buf_size) {
600 35 fpc->end_padded = 1;
601 35 read_end = read_start + MAX_FRAME_HEADER_SIZE;
602 } else {
603 /* The maximum read size is the upper-bound of what the parser
604 needs to have the required number of frames buffered */
605 29478 int nb_desired = FLAC_MIN_HEADERS - fpc->nb_headers_buffered + 1;
606
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29478 times.
29478 read_end = read_end + FFMIN(buf + buf_size - read_end,
607 nb_desired * FLAC_AVG_FRAME_SIZE);
608 }
609
610
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29513 times.
29513 if (!av_fifo_space(fpc->fifo_buf) &&
611 av_fifo_size(fpc->fifo_buf) / FLAC_AVG_FRAME_SIZE >
612 fpc->nb_headers_buffered * 20) {
613 /* There is less than one valid flac header buffered for 20 headers
614 * buffered. Therefore the fifo is most likely filled with invalid
615 * data and the input is not a flac file. */
616 goto handle_error;
617 }
618
619 /* Fill the buffer. */
620
2/2
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 29490 times.
29513 if ( av_fifo_space(fpc->fifo_buf) < read_end - read_start
621
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 23 times.
23 && av_fifo_realloc2(fpc->fifo_buf, (read_end - read_start) + 2*av_fifo_size(fpc->fifo_buf)) < 0) {
622 av_log(avctx, AV_LOG_ERROR,
623 "couldn't reallocate buffer of size %"PTRDIFF_SPECIFIER"\n",
624 (read_end - read_start) + av_fifo_size(fpc->fifo_buf));
625 goto handle_error;
626 }
627
628
2/2
✓ Branch 0 taken 29478 times.
✓ Branch 1 taken 35 times.
29513 if (buf_size) {
629 29478 av_fifo_generic_write(fpc->fifo_buf, (void*) read_start,
630 29478 read_end - read_start, NULL);
631 } else {
632 35 int8_t pad[MAX_FRAME_HEADER_SIZE] = { 0 };
633 35 av_fifo_generic_write(fpc->fifo_buf, pad, sizeof(pad), NULL);
634 }
635
636 /* Tag headers and update sequences. */
637 29513 start_offset = av_fifo_size(fpc->fifo_buf) -
638 29513 ((read_end - read_start) + (MAX_FRAME_HEADER_SIZE - 1));
639 29513 start_offset = FFMAX(0, start_offset);
640 29513 nb_headers = find_new_headers(fpc, start_offset);
641
642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29513 times.
29513 if (nb_headers < 0) {
643 av_log(avctx, AV_LOG_ERROR,
644 "find_new_headers couldn't allocate FLAC header\n");
645 goto handle_error;
646 }
647
648 29513 fpc->nb_headers_buffered = nb_headers;
649 /* Wait till FLAC_MIN_HEADERS to output a valid frame. */
650
4/4
✓ Branch 0 taken 29478 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 27109 times.
✓ Branch 3 taken 2369 times.
29513 if (!fpc->end_padded && fpc->nb_headers_buffered < FLAC_MIN_HEADERS) {
651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27109 times.
27109 if (read_end < buf + buf_size) {
652 read_start = read_end;
653 continue;
654 } else {
655 27109 goto handle_error;
656 }
657 }
658
659 /* If headers found, update the scores since we have longer chains. */
660
3/4
✓ Branch 0 taken 2369 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 2369 times.
✗ Branch 3 not taken.
2404 if (fpc->end_padded || fpc->nb_headers_found)
661 2404 score_sequences(fpc);
662
663 /* restore the state pre-padding */
664
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 2369 times.
2404 if (fpc->end_padded) {
665 35 int warp = fpc->fifo_buf->wptr - fpc->fifo_buf->buffer < MAX_FRAME_HEADER_SIZE;
666 /* HACK: drain the tail of the fifo */
667 35 fpc->fifo_buf->wptr -= MAX_FRAME_HEADER_SIZE;
668 35 fpc->fifo_buf->wndx -= MAX_FRAME_HEADER_SIZE;
669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (warp) {
670 fpc->fifo_buf->wptr += fpc->fifo_buf->end -
671 fpc->fifo_buf->buffer;
672 }
673 35 read_start = read_end = NULL;
674 }
675 }
676
677
2/2
✓ Branch 0 taken 24836 times.
✓ Branch 1 taken 2630 times.
27466 for (curr = fpc->headers; curr; curr = curr->next) {
678
3/4
✓ Branch 0 taken 24642 times.
✓ Branch 1 taken 194 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24642 times.
24836 if (!fpc->best_header || curr->max_score > fpc->best_header->max_score) {
679 194 fpc->best_header = curr;
680 }
681 }
682
683
3/4
✓ Branch 0 taken 2598 times.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2598 times.
2630 if (fpc->best_header && fpc->best_header->max_score <= 0) {
684 // Only accept a bad header if there is no other option to continue
685 if (!buf_size || read_end != buf || fpc->nb_headers_buffered < FLAC_MIN_HEADERS)
686 fpc->best_header = NULL;
687 }
688
689
2/2
✓ Branch 0 taken 2598 times.
✓ Branch 1 taken 32 times.
2630 if (fpc->best_header) {
690 2598 fpc->best_header_valid = 1;
691
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 2492 times.
2598 if (fpc->best_header->offset > 0) {
692 /* Output a junk frame. */
693 106 av_log(avctx, AV_LOG_DEBUG, "Junk frame till offset %i\n",
694 106 fpc->best_header->offset);
695
696 /* Set duration to 0. It is unknown or invalid in a junk frame. */
697 106 s->duration = 0;
698 106 *poutbuf_size = fpc->best_header->offset;
699 106 *poutbuf = flac_fifo_read_wrap(fpc, 0, *poutbuf_size,
700 &fpc->wrap_buf,
701 &fpc->wrap_buf_allocated_size);
702
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 9 times.
115 return buf_size ? (read_end - buf) : (fpc->best_header->offset -
703 9 av_fifo_size(fpc->fifo_buf));
704 }
705
2/2
✓ Branch 0 taken 2272 times.
✓ Branch 1 taken 220 times.
2492 if (!buf_size)
706 220 return get_best_header(fpc, poutbuf, poutbuf_size);
707 }
708
709 2304 handle_error:
710 29413 *poutbuf = NULL;
711 29413 *poutbuf_size = 0;
712
2/2
✓ Branch 0 taken 29381 times.
✓ Branch 1 taken 32 times.
29413 return buf_size ? read_end - buf : 0;
713 }
714
715 202 static av_cold int flac_parse_init(AVCodecParserContext *c)
716 {
717 202 FLACParseContext *fpc = c->priv_data;
718 202 fpc->pc = c;
719 /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before
720 it drains. This is allocated early to avoid slow reallocation. */
721 202 fpc->fifo_buf = av_fifo_alloc_array(FLAC_MIN_HEADERS + 3, FLAC_AVG_FRAME_SIZE);
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
202 if (!fpc->fifo_buf) {
723 av_log(fpc->avctx, AV_LOG_ERROR,
724 "couldn't allocate fifo_buf\n");
725 return AVERROR(ENOMEM);
726 }
727 202 return 0;
728 }
729
730 202 static void flac_parse_close(AVCodecParserContext *c)
731 {
732 202 FLACParseContext *fpc = c->priv_data;
733 202 FLACHeaderMarker *curr = fpc->headers, *temp;
734
735
2/2
✓ Branch 0 taken 1333 times.
✓ Branch 1 taken 202 times.
1535 while (curr) {
736 1333 temp = curr->next;
737 1333 av_free(curr);
738 1333 curr = temp;
739 }
740 202 fpc->headers = NULL;
741 202 av_fifo_freep(&fpc->fifo_buf);
742 202 av_freep(&fpc->wrap_buf);
743 202 }
744
745 const AVCodecParser ff_flac_parser = {
746 .codec_ids = { AV_CODEC_ID_FLAC },
747 .priv_data_size = sizeof(FLACParseContext),
748 .parser_init = flac_parse_init,
749 .parser_parse = flac_parse,
750 .parser_close = flac_parse_close,
751 };
752