FFmpeg
Loading...
Searching...
No Matches
flac_parser.c
Go to the documentation of this file.
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
36#include "libavutil/crc.h"
37#include "libavutil/mem.h"
38#include "flac_parse.h"
39#include "parser_internal.h"
40
41/** maximum number of adjacent headers that compare CRCs against each other */
42#define FLAC_MAX_SEQUENTIAL_HEADERS 4
43/** minimum number of headers buffered and checked before returning frames */
44#define FLAC_MIN_HEADERS 10
45/** estimate for average size of a FLAC frame */
46#define FLAC_AVG_FRAME_SIZE 8192
47
48/** scoring settings for score_header */
49#define FLAC_HEADER_BASE_SCORE 10
50#define FLAC_HEADER_CHANGED_PENALTY 7
51#define FLAC_HEADER_CRC_FAIL_PENALTY 50
52#define FLAC_HEADER_NOT_PENALIZED_YET 100000
53#define FLAC_HEADER_NOT_SCORED_YET -100000
54
55/** largest possible size of flac header */
56#define MAX_FRAME_HEADER_SIZE 16
57#define MAX_FRAME_VERIFY_SIZE (MAX_FRAME_HEADER_SIZE + 1)
58
59typedef struct FifoBuffer {
60 uint8_t *buffer;
61 uint8_t *end;
62 uint8_t *rptr;
63 uint8_t *wptr;
64 int empty;
66
67typedef 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 */
82
83typedef 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 */
102
103static 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
111 if (ff_flac_decode_frame_header(avctx, &gb, fi, 127)) {
112 return 0;
113 }
114 // subframe zero bit
115 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 subframe_type = get_bits(&gb, 6);
127 if (!(subframe_type == 0 ||
128 subframe_type == 1 ||
129 ((subframe_type >= 8) && (subframe_type <= 12)) ||
130 (subframe_type >= 32))) {
131 return 0;
132 }
133
134 return 1;
135}
136
137static size_t flac_fifo_size(const FifoBuffer *f)
138{
139 if (f->wptr <= f->rptr && !f->empty)
140 return (f->wptr - f->buffer) + (f->end - f->rptr);
141 return f->wptr - f->rptr;
142}
143
144static size_t flac_fifo_space(const FifoBuffer *f)
145{
146 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 */
162static uint8_t *flac_fifo_read_wrap(FLACParseContext *fpc, int offset, int len,
163 uint8_t **wrap_buf, int *allocated_size)
164{
165 FifoBuffer *f = &fpc->fifo_buf;
166 uint8_t *start = f->rptr + offset;
167 uint8_t *tmp_buf;
168
169 if (start >= f->end)
170 start -= f->end - f->buffer;
171 if (f->end - start >= len)
172 return start;
173
174 tmp_buf = av_fast_realloc(*wrap_buf, allocated_size, len);
175
176 if (!tmp_buf) {
178 "couldn't reallocate wrap buffer of size %d", len);
179 return NULL;
180 }
181 *wrap_buf = tmp_buf;
182 do {
183 int seg_len = FFMIN(f->end - start, len);
184 memcpy(tmp_buf, start, seg_len);
185 tmp_buf = (uint8_t*)tmp_buf + seg_len;
186// memory barrier needed for SMP here in theory
187
188 start += seg_len - (f->end - f->buffer);
189 len -= seg_len;
190 } while (len > 0);
191
192 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 **/
202static uint8_t *flac_fifo_read(FifoBuffer *f, int offset, int *len)
203{
204 uint8_t *start = f->rptr + offset;
205
206 if (start >= f->end)
207 start -= f->end - f->buffer;
208 *len = FFMIN(*len, f->end - start);
209 return start;
210}
211
212static int flac_fifo_grow(FifoBuffer *f, size_t inc)
213{
214 size_t size_old = f->end - f->buffer;
215 size_t offset_r = f->rptr - f->buffer;
216 size_t offset_w = f->wptr - f->buffer;
217 size_t size_new;
218
219 uint8_t *tmp;
220
221 if (size_old > SIZE_MAX - inc)
222 return AVERROR(EINVAL);
223 size_new = size_old + inc;
224
225 tmp = av_realloc(f->buffer, size_new);
226 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 if (offset_w <= offset_r && !f->empty) {
232 const size_t copy = FFMIN(inc, offset_w);
233 memcpy(tmp + size_old, tmp, copy);
234 if (copy < offset_w) {
235 memmove(tmp, tmp + copy, offset_w - copy);
236 offset_w -= copy;
237 } else
238 offset_w = size_old + copy;
239 }
240
241 f->buffer = tmp;
242 f->end = f->buffer + size_new;
243 f->rptr = f->buffer + offset_r;
244 f->wptr = f->buffer + offset_w;
245
246 return 0;
247}
248
249static int flac_fifo_write(FifoBuffer *f, const uint8_t *src, size_t size)
250{
251 uint8_t *wptr;
252
253 if (flac_fifo_space(f) < size) {
254 int ret = flac_fifo_grow(f, FFMAX(flac_fifo_size(f), size));
255 if (ret < 0)
256 return ret;
257 }
258
259 if (size)
260 f->empty = 0;
261
262 wptr = f->wptr;
263 do {
264 size_t len = FFMIN(f->end - wptr, size);
265 memcpy(wptr, src, len);
266 src += len;
267 wptr += len;
268 if (wptr >= f->end)
269 wptr = f->buffer;
270 size -= len;
271 } while (size > 0);
272
273 f->wptr = wptr;
274
275 return 0;
276}
277
278static void flac_fifo_drain(FifoBuffer *f, size_t size)
279{
280 size_t size_cur = flac_fifo_size(f);
281
282 av_assert0(size_cur >= size);
283 if (size_cur == size)
284 f->empty = 1;
285
286 f->rptr += size;
287 if (f->rptr >= f->end)
288 f->rptr -= f->end - f->buffer;
289}
290
291static int flac_fifo_alloc(FifoBuffer *f, size_t size)
292{
293 memset(f, 0, sizeof(*f));
294
295 f->buffer = av_realloc(NULL, size);
296 if (!f->buffer)
297 return AVERROR(ENOMEM);
298
299 f->wptr = f->buffer;
300 f->rptr = f->buffer;
301 f->end = f->buffer + size;
302
303 f->empty = 1;
304
305 return 0;
306}
307
309{
310 av_freep(&f->buffer);
311 memset(f, 0, sizeof(*f));
312}
313
315{
316 FLACFrameInfo fi;
317 uint8_t *header_buf;
318 int size = 0;
319 header_buf = flac_fifo_read_wrap(fpc, offset,
321 &fpc->wrap_buf,
323 if (frame_header_is_valid(fpc->avctx, header_buf, &fi)) {
324 FLACHeaderMarker **end_handle = &fpc->headers;
325 int i;
326
327 size = 0;
328 while (*end_handle) {
329 end_handle = &(*end_handle)->next;
330 size++;
331 }
332
333 *end_handle = av_mallocz(sizeof(**end_handle));
334 if (!*end_handle) {
336 "couldn't allocate FLACHeaderMarker\n");
337 return AVERROR(ENOMEM);
338 }
339 (*end_handle)->fi = fi;
340 (*end_handle)->offset = offset;
341
342 for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++)
343 (*end_handle)->link_penalty[i] = FLAC_HEADER_NOT_PENALIZED_YET;
344
345 fpc->nb_headers_found++;
346 size++;
347 }
348 return size;
349}
350
351static int find_headers_search(FLACParseContext *fpc, uint8_t *buf,
352 int buf_size, int search_start)
353{
354 int size = 0, mod_offset = (buf_size - 1) % 4, i, j;
355 uint32_t x;
356
357 for (i = 0; i < mod_offset; i++) {
358 if ((AV_RB16(buf + i) & 0xFFFE) == 0xFFF8) {
359 int ret = find_headers_search_validate(fpc, search_start + i);
360 size = FFMAX(size, ret);
361 }
362 }
363
364 for (; i < buf_size - 1; i += 4) {
365 x = AV_RN32(buf + i);
366 if (((x & ~(x + 0x01010101)) & 0x80808080)) {
367 for (j = 0; j < 4; j++) {
368 if ((AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8) {
369 int ret = find_headers_search_validate(fpc, search_start + i + j);
370 size = FFMAX(size, ret);
371 }
372 }
373 }
374 }
375 return size;
376}
377
378static int find_new_headers(FLACParseContext *fpc, int search_start)
379{
380 FLACHeaderMarker *end;
381 int search_end, size = 0, read_len, temp;
382 uint8_t *buf;
383 fpc->nb_headers_found = 0;
384
385 /* Search for a new header of at most 16 bytes. */
386 search_end = flac_fifo_size(&fpc->fifo_buf) - (MAX_FRAME_HEADER_SIZE - 1);
387 read_len = search_end - search_start + 1;
388 buf = flac_fifo_read(&fpc->fifo_buf, search_start, &read_len);
389 size = find_headers_search(fpc, buf, read_len, search_start);
390 search_start += read_len - 1;
391
392 /* If fifo end was hit do the wrap around. */
393 if (search_start != search_end) {
394 uint8_t wrap[2];
395
396 wrap[0] = buf[read_len - 1];
397 /* search_start + 1 is the post-wrap offset in the fifo. */
398 read_len = search_end - (search_start + 1) + 1;
399
400 buf = flac_fifo_read(&fpc->fifo_buf, search_start + 1, &read_len);
401 wrap[1] = buf[0];
402
403 if ((AV_RB16(wrap) & 0xFFFE) == 0xFFF8) {
404 temp = find_headers_search_validate(fpc, search_start);
405 size = FFMAX(size, temp);
406 }
407 search_start++;
408
409 /* Continue to do the last half of the wrap. */
410 temp = find_headers_search(fpc, buf, read_len, search_start);
411 size = FFMAX(size, temp);
412 search_start += read_len - 1;
413 }
414
415 /* Return the size even if no new headers were found. */
416 if (!size && fpc->headers)
417 for (end = fpc->headers; end; end = end->next)
418 size++;
419 return size;
420}
421
423 FLACFrameInfo *header_fi,
424 FLACFrameInfo *child_fi,
425 int log_level_offset)
426{
427 int deduction = 0;
428 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 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 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 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 return deduction;
450}
451
454 FLACHeaderMarker *child,
455 int log_level_offset)
456{
457 FLACFrameInfo *header_fi = &header->fi, *child_fi = &child->fi;
458 int check_crc, deduction, deduction_expected = 0, i;
459 deduction = check_header_fi_mismatch(fpc, header_fi, child_fi,
460 log_level_offset);
461 /* Check sample and frame numbers. */
462 if ((child_fi->frame_or_sample_num - header_fi->frame_or_sample_num
463 != header_fi->blocksize) &&
464 (child_fi->frame_or_sample_num
465 != 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 expected_frame_num = expected_sample_num = header_fi->frame_or_sample_num;
472 curr = header;
473 while (curr != child) {
474 /* Ignore frames that failed all crc checks */
475 for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++) {
477 expected_frame_num++;
478 expected_sample_num += curr->fi.blocksize;
479 break;
480 }
481 }
482 curr = curr->next;
483 }
484
485 if (expected_frame_num == child_fi->frame_or_sample_num ||
486 expected_sample_num == child_fi->frame_or_sample_num)
487 deduction_expected = deduction ? 0 : 1;
488
489 deduction += FLAC_HEADER_CHANGED_PENALTY;
490 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
491 "sample/frame number mismatch in adjacent frames\n");
492 }
493
494 if (fpc->last_fi.is_var_size == header_fi->is_var_size) {
495 if (fpc->last_fi.is_var_size &&
497 check_crc = 0;
498 } else if (!fpc->last_fi.is_var_size &&
499 fpc->last_fi.frame_or_sample_num + 1 == header_fi->frame_or_sample_num) {
500 check_crc = 0;
501 } else {
502 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 if (check_crc || (deduction && !deduction_expected)) {
510 FLACHeaderMarker *curr;
511 int read_len;
512 uint8_t *buf;
513 uint32_t crc = 1;
514 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 curr = header->next;
519 for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS && curr != child; i++)
520 curr = curr->next;
521
523
524 if (header->link_penalty[i] < FLAC_HEADER_CRC_FAIL_PENALTY ||
525 header->link_penalty[i] == FLAC_HEADER_NOT_PENALIZED_YET) {
526 FLACHeaderMarker *start, *end;
527
528 /* Although overlapping chains are scored, the crc should never
529 have to be computed twice for a single byte. */
530 start = header;
531 end = child;
532 if (i > 0 &&
533 header->link_penalty[i - 1] >= FLAC_HEADER_CRC_FAIL_PENALTY) {
534 while (start->next != child)
535 start = start->next;
536 inverted_test = 1;
537 } else if (i > 0 &&
538 header->next->link_penalty[i-1] >=
540 end = header->next;
541 inverted_test = 1;
542 }
543
544 read_len = end->offset - start->offset;
545 buf = flac_fifo_read(&fpc->fifo_buf, start->offset, &read_len);
546 crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf, read_len);
547 read_len = (end->offset - start->offset) - read_len;
548
549 if (read_len) {
550 buf = flac_fifo_read(&fpc->fifo_buf, end->offset - read_len, &read_len);
551 crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), crc, buf, read_len);
552 }
553 }
554
555 if (!crc ^ !inverted_test) {
556 deduction += FLAC_HEADER_CRC_FAIL_PENALTY;
557 av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
558 "crc check failed from offset %i (frame %"PRId64") to %i (frame %"PRId64")\n",
559 header->offset, header_fi->frame_or_sample_num,
560 child->offset, child_fi->frame_or_sample_num);
561 }
562 }
563 return deduction;
564}
565
566/**
567 * Score a header.
568 *
569 * Give FLAC_HEADER_BASE_SCORE points to a frame for existing.
570 * If it has children, (subsequent frames of which the preceding CRC footer
571 * validates against this one,) then take the maximum score of the children,
572 * with a penalty of FLAC_HEADER_CHANGED_PENALTY applied for each change to
573 * bps, sample rate, channels, but not decorrelation mode, or blocksize,
574 * because it can change often.
575 **/
577{
578 FLACHeaderMarker *child;
579 int dist = 0;
580 int child_score;
581 int base_score = FLAC_HEADER_BASE_SCORE;
582 if (header->max_score != FLAC_HEADER_NOT_SCORED_YET)
583 return header->max_score;
584
585 /* Modify the base score with changes from the last output header */
586 if (fpc->last_fi_valid) {
587 /* Silence the log since this will be repeated if selected */
588 base_score -= check_header_fi_mismatch(fpc, &fpc->last_fi, &header->fi,
590 }
591
592 header->max_score = base_score;
593
594 /* Check and compute the children's scores. */
595 child = header->next;
596 for (dist = 0; dist < FLAC_MAX_SEQUENTIAL_HEADERS && child; dist++) {
597 /* Look at the child's frame header info and penalize suspicious
598 changes between the headers. */
599 if (header->link_penalty[dist] == FLAC_HEADER_NOT_PENALIZED_YET) {
600 header->link_penalty[dist] = check_header_mismatch(fpc, header,
601 child, AV_LOG_DEBUG);
602 }
603 child_score = score_header(fpc, child) - header->link_penalty[dist];
604
605 if (FLAC_HEADER_BASE_SCORE + child_score > header->max_score) {
606 /* Keep the child because the frame scoring is dynamic. */
607 header->best_child = child;
608 header->max_score = base_score + child_score;
609 }
610 child = child->next;
611 }
612
613 return header->max_score;
614}
615
617{
618 FLACHeaderMarker *curr;
619 int best_score = FLAC_HEADER_NOT_SCORED_YET;
620 /* First pass to clear all old scores. */
621 for (curr = fpc->headers; curr; curr = curr->next)
623
624 /* Do a second pass to score them all. */
625 for (curr = fpc->headers; curr; curr = curr->next) {
626 if (score_header(fpc, curr) > best_score) {
627 fpc->best_header = curr;
628 best_score = curr->max_score;
629 }
630 }
631}
632
633static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf,
634 int *poutbuf_size)
635{
637 FLACHeaderMarker *child = header->best_child;
638 if (!child) {
639 *poutbuf_size = flac_fifo_size(&fpc->fifo_buf) - header->offset;
640 } else {
641 *poutbuf_size = child->offset - header->offset;
642
643 /* If the child has suspicious changes, log them */
644 check_header_mismatch(fpc, header, child, 0);
645 }
646
647 ff_flac_set_channel_layout(fpc->avctx, header->fi.channels);
648
649 fpc->avctx->sample_rate = header->fi.samplerate;
650 fpc->pc->duration = header->fi.blocksize;
651 *poutbuf = flac_fifo_read_wrap(fpc, header->offset, *poutbuf_size,
652 &fpc->wrap_buf,
654
655 if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) {
656 if (header->fi.is_var_size)
657 fpc->pc->pts = header->fi.frame_or_sample_num;
658 else if (header->best_child)
659 fpc->pc->pts = header->fi.frame_or_sample_num * header->fi.blocksize;
660 }
661
662 fpc->best_header_valid = 0;
663 fpc->last_fi_valid = 1;
664 fpc->last_fi = header->fi;
665
666 /* Return the negative overread index so the client can compute pos.
667 This should be the amount overread to the beginning of the child */
668 if (child) {
669 int64_t offset = child->offset - flac_fifo_size(&fpc->fifo_buf);
670 if (offset > -(1 << 28))
671 return offset;
672 }
673 return 0;
674}
675
677 const uint8_t **poutbuf, int *poutbuf_size,
678 const uint8_t *buf, int buf_size)
679{
680 FLACParseContext *fpc = s->priv_data;
681 FLACHeaderMarker *curr;
682 int nb_headers;
683 const uint8_t *read_end = buf;
684 const uint8_t *read_start = buf;
685
686 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
687 FLACFrameInfo fi;
688 if (frame_header_is_valid(avctx, buf, &fi)) {
689 s->duration = fi.blocksize;
690 if (!avctx->sample_rate)
691 avctx->sample_rate = fi.samplerate;
692 if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) {
693 fpc->pc->pts = fi.frame_or_sample_num;
694 if (!fi.is_var_size)
695 fpc->pc->pts *= fi.blocksize;
696 }
697 }
698 *poutbuf = buf;
699 *poutbuf_size = buf_size;
700 return buf_size;
701 }
702
703 fpc->avctx = avctx;
705 return get_best_header(fpc, poutbuf, poutbuf_size);
706
707 /* If a best_header was found last call remove it with the buffer data. */
708 if (fpc->best_header && fpc->best_header->best_child) {
710 FLACHeaderMarker *best_child = fpc->best_header->best_child;
711
712 /* Remove headers in list until the end of the best_header. */
713 for (curr = fpc->headers; curr != best_child; curr = temp) {
714 if (curr != fpc->best_header) {
715 av_log(avctx, AV_LOG_DEBUG,
716 "dropping low score %i frame header from offset %i to %i\n",
717 curr->max_score, curr->offset, curr->next->offset);
718 }
719 temp = curr->next;
720 av_free(curr);
721 fpc->nb_headers_buffered--;
722 }
723 /* Release returned data from ring buffer. */
724 flac_fifo_drain(&fpc->fifo_buf, best_child->offset);
725
726 /* Fix the offset for the headers remaining to match the new buffer. */
727 for (curr = best_child->next; curr; curr = curr->next)
728 curr->offset -= best_child->offset;
729
730 best_child->offset = 0;
731 fpc->headers = best_child;
733 fpc->best_header = best_child;
734 return get_best_header(fpc, poutbuf, poutbuf_size);
735 }
736 fpc->best_header = NULL;
737 } else if (fpc->best_header) {
738 /* No end frame no need to delete the buffer; probably eof */
740
741 for (curr = fpc->headers; curr != fpc->best_header; curr = temp) {
742 temp = curr->next;
743 av_free(curr);
744 fpc->nb_headers_buffered--;
745 }
746 fpc->headers = fpc->best_header->next;
747 av_freep(&fpc->best_header);
748 fpc->nb_headers_buffered--;
749 }
750
751 /* Find and score new headers. */
752 /* buf_size is zero when flushing, so check for this since we do */
753 /* not want to try to read more input once we have found the end. */
754 /* Also note that buf can't be NULL. */
755 while ((buf_size && read_end < buf + buf_size &&
757 || (!buf_size && !fpc->end_padded)) {
758 int start_offset, ret;
759
760 /* Pad the end once if EOF, to check the final region for headers. */
761 if (!buf_size) {
762 fpc->end_padded = 1;
763 read_end = read_start + MAX_FRAME_HEADER_SIZE;
764 } else {
765 /* The maximum read size is the upper-bound of what the parser
766 needs to have the required number of frames buffered */
767 int nb_desired = FLAC_MIN_HEADERS - fpc->nb_headers_buffered + 1;
768 read_end = read_end + FFMIN(buf + buf_size - read_end,
769 nb_desired * FLAC_AVG_FRAME_SIZE);
770 }
771
772 if (!flac_fifo_space(&fpc->fifo_buf) &&
774 fpc->nb_headers_buffered * 20) {
775 /* There is less than one valid flac header buffered for 20 headers
776 * buffered. Therefore the fifo is most likely filled with invalid
777 * data and the input is not a flac file. */
778 goto handle_error;
779 }
780
781 /* Fill the buffer. */
782 if (buf_size) {
783 ret = flac_fifo_write(&fpc->fifo_buf, read_start,
784 read_end - read_start);
785 } else {
786 int8_t pad[MAX_FRAME_HEADER_SIZE] = { 0 };
787 ret = flac_fifo_write(&fpc->fifo_buf, pad, sizeof(pad));
788 }
789 if (ret < 0) {
790 av_log(avctx, AV_LOG_ERROR, "Error buffering data\n");
791 goto handle_error;
792 }
793
794 /* Tag headers and update sequences. */
795 start_offset = flac_fifo_size(&fpc->fifo_buf) -
796 ((read_end - read_start) + (MAX_FRAME_HEADER_SIZE - 1));
797 start_offset = FFMAX(0, start_offset);
798 nb_headers = find_new_headers(fpc, start_offset);
799
800 if (nb_headers < 0) {
801 av_log(avctx, AV_LOG_ERROR,
802 "find_new_headers couldn't allocate FLAC header\n");
803 goto handle_error;
804 }
805
806 fpc->nb_headers_buffered = nb_headers;
807 /* Wait till FLAC_MIN_HEADERS to output a valid frame. */
808 if (!fpc->end_padded && fpc->nb_headers_buffered < FLAC_MIN_HEADERS) {
809 if (read_end < buf + buf_size) {
810 read_start = read_end;
811 continue;
812 } else {
813 goto handle_error;
814 }
815 }
816
817 /* If headers found, update the scores since we have longer chains. */
818 if (fpc->end_padded || fpc->nb_headers_found)
819 score_sequences(fpc);
820
821 /* restore the state pre-padding */
822 if (fpc->end_padded) {
823 int empty = flac_fifo_size(&fpc->fifo_buf) == MAX_FRAME_HEADER_SIZE;
824 int warp = fpc->fifo_buf.wptr - fpc->fifo_buf.buffer < MAX_FRAME_HEADER_SIZE;
825 /* HACK: drain the tail of the fifo */
827 if (warp) {
828 fpc->fifo_buf.wptr += fpc->fifo_buf.end -
829 fpc->fifo_buf.buffer;
830 }
831 fpc->fifo_buf.empty = empty;
832 read_start = read_end = NULL;
833 }
834 }
835
836 for (curr = fpc->headers; curr; curr = curr->next) {
837 if (!fpc->best_header || curr->max_score > fpc->best_header->max_score) {
838 fpc->best_header = curr;
839 }
840 }
841
842 if (fpc->best_header && fpc->best_header->max_score <= 0) {
843 // Only accept a bad header if there is no other option to continue
844 if (!buf_size || read_end != buf || fpc->nb_headers_buffered < FLAC_MIN_HEADERS)
845 fpc->best_header = NULL;
846 }
847
848 if (fpc->best_header) {
849 fpc->best_header_valid = 1;
850 if (fpc->best_header->offset > 0) {
851 /* Output a junk frame. */
852 av_log(avctx, AV_LOG_DEBUG, "Junk frame till offset %i\n",
853 fpc->best_header->offset);
854
855 /* Set duration to 0. It is unknown or invalid in a junk frame. */
856 s->duration = 0;
857 *poutbuf_size = fpc->best_header->offset;
858 *poutbuf = flac_fifo_read_wrap(fpc, 0, *poutbuf_size,
859 &fpc->wrap_buf,
861 return buf_size ? (read_end - buf) : (fpc->best_header->offset -
862 flac_fifo_size(&fpc->fifo_buf));
863 }
864 if (!buf_size)
865 return get_best_header(fpc, poutbuf, poutbuf_size);
866 }
867
868handle_error:
869 *poutbuf = NULL;
870 *poutbuf_size = 0;
871 return buf_size ? read_end - buf : 0;
872}
873
875{
876 FLACParseContext *fpc = c->priv_data;
877 int ret;
878
879 fpc->pc = c;
880 /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before
881 it drains. This is allocated early to avoid slow reallocation. */
883 if (ret < 0) {
885 "couldn't allocate fifo_buf\n");
886 return AVERROR(ENOMEM);
887 }
888 return 0;
889}
890
892{
893 FLACParseContext *fpc = c->priv_data;
894 FLACHeaderMarker *curr = fpc->headers, *temp;
895
896 while (curr) {
897 temp = curr->next;
898 av_free(curr);
899 curr = temp;
900 }
901 fpc->headers = NULL;
903 av_freep(&fpc->wrap_buf);
904}
905
#define wrap(func)
Definition neontest.h:65
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition apv_parser.c:92
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define PARSER_FLAG_USE_CODEC_TS
Definition avcodec.h:2656
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2652
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_flac_decode_frame_header(void *logctx, GetBitContext *gb, FLACFrameInfo *fi, int log_level_offset)
Validate and decode a frame header.
Definition flac.c:51
void ff_flac_set_channel_layout(AVCodecContext *avctx, int channels)
Definition flac.c:173
FLAC (Free Lossless Audio Codec) decoder/parser common functions.
static void score_sequences(FLACParseContext *fpc)
static int flac_fifo_alloc(FifoBuffer *f, size_t size)
#define FLAC_HEADER_CHANGED_PENALTY
Definition flac_parser.c:50
#define FLAC_MAX_SEQUENTIAL_HEADERS
maximum number of adjacent headers that compare CRCs against each other
Definition flac_parser.c:42
static av_cold void flac_parse_close(AVCodecParserContext *c)
static void flac_fifo_drain(FifoBuffer *f, size_t size)
static av_cold int flac_parse_init(AVCodecParserContext *c)
static int find_headers_search_validate(FLACParseContext *fpc, int offset)
static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header)
Score a header.
#define FLAC_HEADER_NOT_SCORED_YET
Definition flac_parser.c:53
static size_t flac_fifo_size(const FifoBuffer *f)
static size_t flac_fifo_space(const FifoBuffer *f)
static int check_header_mismatch(FLACParseContext *fpc, FLACHeaderMarker *header, FLACHeaderMarker *child, int log_level_offset)
#define FLAC_HEADER_BASE_SCORE
scoring settings for score_header
Definition flac_parser.c:49
#define FLAC_AVG_FRAME_SIZE
estimate for average size of a FLAC frame
Definition flac_parser.c:46
static int flac_fifo_write(FifoBuffer *f, const uint8_t *src, size_t size)
#define MAX_FRAME_VERIFY_SIZE
Definition flac_parser.c:57
static int flac_fifo_grow(FifoBuffer *f, size_t inc)
#define FLAC_HEADER_NOT_PENALIZED_YET
Definition flac_parser.c:52
static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf, FLACFrameInfo *fi)
static int check_header_fi_mismatch(FLACParseContext *fpc, FLACFrameInfo *header_fi, FLACFrameInfo *child_fi, int log_level_offset)
static int find_headers_search(FLACParseContext *fpc, uint8_t *buf, int buf_size, int search_start)
#define MAX_FRAME_HEADER_SIZE
largest possible size of flac header
Definition flac_parser.c:56
#define FLAC_HEADER_CRC_FAIL_PENALTY
Definition flac_parser.c:51
static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf, int *poutbuf_size)
static uint8_t * flac_fifo_read_wrap(FLACParseContext *fpc, int offset, int len, uint8_t **wrap_buf, int *allocated_size)
Non-destructive fast fifo pointer fetching Returns a pointer from the specified offset.
const FFCodecParser ff_flac_parser
static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
#define FLAC_MIN_HEADERS
minimum number of headers buffered and checked before returning frames
Definition flac_parser.c:44
static int find_new_headers(FLACParseContext *fpc, int search_start)
static uint8_t * flac_fifo_read(FifoBuffer *f, int offset, int *len)
Return a pointer in the fifo buffer where the offset starts at until the wrap point or end of request...
static void flac_fifo_free(FifoBuffer *f)
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
@ AV_CODEC_ID_FLAC
Definition codec_id.h:465
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition crc.c:421
@ AV_CRC_16_ANSI
Definition crc.h:50
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition mem.c:495
#define AV_RN32(p)
#define AV_RB16(p)
unsigned offset
Definition libaomenc.c:763
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
#define av_realloc(p, s)
Definition ops_static.c:54
#define PARSER_CODEC_LIST(...)
static int inc(int num, int period)
Definition perlin.c:34
static const uint8_t header[24]
Definition sdr2.c:68
main external API structure.
Definition avcodec.h:443
int sample_rate
samples per second
Definition avcodec.h:1040
int duration
Duration of the current frame.
Definition avcodec.h:2732
int blocksize
block size of the frame
Definition flac_parse.h:46
int channels
number of channels
Definition flac_parse.h:44
int samplerate
sample rate
Definition flac_parse.h:43
int is_var_size
specifies if the stream uses variable block sizes or a fixed block size; also determines the meaning ...
Definition flac_parse.h:49
int64_t frame_or_sample_num
frame number or sample number
Definition flac_parse.h:48
int bps
bits-per-sample
Definition flac_parse.h:45
int link_penalty[FLAC_MAX_SEQUENTIAL_HEADERS]
array of local scores between this header and the one at a distance equal array position
Definition flac_parser.c:69
int max_score
maximum score found after checking each child that has a valid CRC
Definition flac_parser.c:72
FLACFrameInfo fi
decoded frame header info
Definition flac_parser.c:74
struct FLACHeaderMarker * best_child
following frame header with which this frame has the best score with
Definition flac_parser.c:78
struct FLACHeaderMarker * next
next CRC-8 verified header that immediately follows this one in the bytestream
Definition flac_parser.c:75
int offset
byte offset from start of FLACParseContext->buffer
Definition flac_parser.c:68
uint8_t * wrap_buf
general fifo read buffer when wrapped
Definition flac_parser.c:97
FLACHeaderMarker * headers
linked-list that starts at the first CRC-8 verified header within buffer
Definition flac_parser.c:86
int last_fi_valid
set if last_fi is valid
FLACFrameInfo last_fi
last decoded frame header info
Definition flac_parser.c:99
AVCodecContext * avctx
codec context pointer for logging
Definition flac_parser.c:85
FLACHeaderMarker * best_header
highest scoring header within buffer
Definition flac_parser.c:88
int nb_headers_buffered
number of headers that are buffered
Definition flac_parser.c:91
int end_padded
specifies if fifo_buf's end is padded
Definition flac_parser.c:96
FifoBuffer fifo_buf
buffer to store all data until headers can be verified
Definition flac_parser.c:94
int best_header_valid
flag set when the parser returns junk; if set return best_header next time
Definition flac_parser.c:92
AVCodecParserContext * pc
parent context
Definition flac_parser.c:84
int nb_headers_found
number of headers found in the last flac_parse() call
Definition flac_parser.c:89
int wrap_buf_allocated_size
actual allocated size of the buffer
Definition flac_parser.c:98
uint8_t * end
Definition flac_parser.c:61
uint8_t * buffer
Definition flac_parser.c:60
uint8_t * rptr
Definition flac_parser.c:62
uint8_t * wptr
Definition flac_parser.c:63
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
static void check_crc(const AVCRC *table_new, const char *name, size_t size, size_t offset)
Definition crc.c:58
#define src
Definition vp8dsp.c:248
int size
else temp
Definition vf_mcdeint.c:275
static void copy(const float *p1, float *p2, const int length)
int len
static double c[64]