FFmpeg  4.1.11
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 
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 #define MAX_FRAME_VERIFY_SIZE (MAX_FRAME_HEADER_SIZE)
59 
60 typedef struct FLACHeaderMarker {
61  int offset; /**< byte offset from start of FLACParseContext->buffer */
62  int *link_penalty; /**< pointer to array of local scores between this header
63  and the one at a distance equal 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 */
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 */
94 
95 static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf,
97 {
98  GetBitContext gb;
99  init_get_bits(&gb, buf, MAX_FRAME_HEADER_SIZE * 8);
100  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  */
117  uint8_t** wrap_buf, int* allocated_size)
118 {
119  AVFifoBuffer *f = fpc->fifo_buf;
120  uint8_t *start = f->rptr + offset;
121  uint8_t *tmp_buf;
122 
123  if (start >= f->end)
124  start -= f->end - f->buffer;
125  if (f->end - start >= len)
126  return start;
127 
128  tmp_buf = av_fast_realloc(*wrap_buf, allocated_size, len);
129 
130  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  *wrap_buf = tmp_buf;
136  do {
137  int seg_len = FFMIN(f->end - start, len);
138  memcpy(tmp_buf, start, seg_len);
139  tmp_buf = (uint8_t*)tmp_buf + seg_len;
140 // memory barrier needed for SMP here in theory
141 
142  start += seg_len - (f->end - f->buffer);
143  len -= seg_len;
144  } while (len > 0);
145 
146  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  **/
157 {
158  AVFifoBuffer *f = fpc->fifo_buf;
159  uint8_t *start = f->rptr + offset;
160 
161  if (start >= f->end)
162  start -= f->end - f->buffer;
163  *len = FFMIN(*len, f->end - start);
164  return start;
165 }
166 
168 {
170  uint8_t *header_buf;
171  int size = 0;
172  header_buf = flac_fifo_read_wrap(fpc, offset,
174  &fpc->wrap_buf,
176  if (frame_header_is_valid(fpc->avctx, header_buf, &fi)) {
177  FLACHeaderMarker **end_handle = &fpc->headers;
178  int i;
179 
180  size = 0;
181  while (*end_handle) {
182  end_handle = &(*end_handle)->next;
183  size++;
184  }
185 
186  *end_handle = av_mallocz(sizeof(**end_handle));
187  if (!*end_handle) {
188  av_log(fpc->avctx, AV_LOG_ERROR,
189  "couldn't allocate FLACHeaderMarker\n");
190  return AVERROR(ENOMEM);
191  }
192  (*end_handle)->fi = fi;
193  (*end_handle)->offset = offset;
194  (*end_handle)->link_penalty = av_malloc(sizeof(int) *
196  if (!(*end_handle)->link_penalty) {
197  av_freep(end_handle);
198  av_log(fpc->avctx, AV_LOG_ERROR,
199  "couldn't allocate link_penalty\n");
200  return AVERROR(ENOMEM);
201  }
202 
203  for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++)
204  (*end_handle)->link_penalty[i] = FLAC_HEADER_NOT_PENALIZED_YET;
205 
206  fpc->nb_headers_found++;
207  size++;
208  }
209  return size;
210 }
211 
212 static int find_headers_search(FLACParseContext *fpc, uint8_t *buf, int buf_size,
213  int search_start)
214 
215 {
216  int size = 0, mod_offset = (buf_size - 1) % 4, i, j;
217  uint32_t x;
218 
219  for (i = 0; i < mod_offset; i++) {
220  if ((AV_RB16(buf + i) & 0xFFFE) == 0xFFF8) {
221  int ret = find_headers_search_validate(fpc, search_start + i);
222  size = FFMAX(size, ret);
223  }
224  }
225 
226  for (; i < buf_size - 1; i += 4) {
227  x = AV_RB32(buf + i);
228  if (((x & ~(x + 0x01010101)) & 0x80808080)) {
229  for (j = 0; j < 4; j++) {
230  if ((AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8) {
231  int ret = find_headers_search_validate(fpc, search_start + i + j);
232  size = FFMAX(size, ret);
233  }
234  }
235  }
236  }
237  return size;
238 }
239 
240 static int find_new_headers(FLACParseContext *fpc, int search_start)
241 {
243  int search_end, size = 0, read_len, temp;
244  uint8_t *buf;
245  fpc->nb_headers_found = 0;
246 
247  /* Search for a new header of at most 16 bytes. */
248  search_end = av_fifo_size(fpc->fifo_buf) - (MAX_FRAME_HEADER_SIZE - 1);
249  read_len = search_end - search_start + 1;
250  buf = flac_fifo_read(fpc, search_start, &read_len);
251  size = find_headers_search(fpc, buf, read_len, search_start);
252  search_start += read_len - 1;
253 
254  /* If fifo end was hit do the wrap around. */
255  if (search_start != search_end) {
256  uint8_t wrap[2];
257 
258  wrap[0] = buf[read_len - 1];
259  read_len = search_end - search_start + 1;
260 
261  /* search_start + 1 is the post-wrap offset in the fifo. */
262  buf = flac_fifo_read(fpc, search_start + 1, &read_len);
263  wrap[1] = buf[0];
264 
265  if ((AV_RB16(wrap) & 0xFFFE) == 0xFFF8) {
266  temp = find_headers_search_validate(fpc, search_start);
267  size = FFMAX(size, temp);
268  }
269  search_start++;
270 
271  /* Continue to do the last half of the wrap. */
272  temp = find_headers_search(fpc, buf, read_len, search_start);
273  size = FFMAX(size, temp);
274  search_start += read_len - 1;
275  }
276 
277  /* Return the size even if no new headers were found. */
278  if (!size && fpc->headers)
279  for (end = fpc->headers; end; end = end->next)
280  size++;
281  return size;
282 }
283 
285  FLACFrameInfo *header_fi,
286  FLACFrameInfo *child_fi,
287  int log_level_offset)
288 {
289  int deduction = 0;
290  if (child_fi->samplerate != header_fi->samplerate) {
291  deduction += FLAC_HEADER_CHANGED_PENALTY;
292  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
293  "sample rate change detected in adjacent frames\n");
294  }
295  if (child_fi->bps != header_fi->bps) {
296  deduction += FLAC_HEADER_CHANGED_PENALTY;
297  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
298  "bits per sample change detected in adjacent frames\n");
299  }
300  if (child_fi->is_var_size != header_fi->is_var_size) {
301  /* Changing blocking strategy not allowed per the spec */
302  deduction += FLAC_HEADER_BASE_SCORE;
303  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
304  "blocking strategy change detected in adjacent frames\n");
305  }
306  if (child_fi->channels != header_fi->channels) {
307  deduction += FLAC_HEADER_CHANGED_PENALTY;
308  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
309  "number of channels change detected in adjacent frames\n");
310  }
311  return deduction;
312 }
313 
317  int log_level_offset)
318 {
319  FLACFrameInfo *header_fi = &header->fi, *child_fi = &child->fi;
320  int deduction, deduction_expected = 0, i;
321  deduction = check_header_fi_mismatch(fpc, header_fi, child_fi,
322  log_level_offset);
323  /* Check sample and frame numbers. */
324  if ((child_fi->frame_or_sample_num - header_fi->frame_or_sample_num
325  != header_fi->blocksize) &&
326  (child_fi->frame_or_sample_num
327  != header_fi->frame_or_sample_num + 1)) {
328  FLACHeaderMarker *curr;
329  int expected_frame_num, expected_sample_num;
330  /* If there are frames in the middle we expect this deduction,
331  as they are probably valid and this one follows it */
332 
333  expected_frame_num = expected_sample_num = header_fi->frame_or_sample_num;
334  curr = header;
335  while (curr != child) {
336  /* Ignore frames that failed all crc checks */
337  for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++) {
339  expected_frame_num++;
340  expected_sample_num += curr->fi.blocksize;
341  break;
342  }
343  }
344  curr = curr->next;
345  }
346 
347  if (expected_frame_num == child_fi->frame_or_sample_num ||
348  expected_sample_num == child_fi->frame_or_sample_num)
349  deduction_expected = deduction ? 0 : 1;
350 
351  deduction += FLAC_HEADER_CHANGED_PENALTY;
352  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
353  "sample/frame number mismatch in adjacent frames\n");
354  }
355 
356  /* If we have suspicious headers, check the CRC between them */
357  if (deduction && !deduction_expected) {
358  FLACHeaderMarker *curr;
359  int read_len;
360  uint8_t *buf;
361  uint32_t crc = 1;
362  int inverted_test = 0;
363 
364  /* Since CRC is expensive only do it if we haven't yet.
365  This assumes a CRC penalty is greater than all other check penalties */
366  curr = header->next;
367  for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS && curr != child; i++)
368  curr = curr->next;
369 
373 
374  /* Although overlapping chains are scored, the crc should never
375  have to be computed twice for a single byte. */
376  start = header;
377  end = child;
378  if (i > 0 &&
379  header->link_penalty[i - 1] >= FLAC_HEADER_CRC_FAIL_PENALTY) {
380  while (start->next != child)
381  start = start->next;
382  inverted_test = 1;
383  } else if (i > 0 &&
384  header->next->link_penalty[i-1] >=
386  end = header->next;
387  inverted_test = 1;
388  }
389 
390  read_len = end->offset - start->offset;
391  buf = flac_fifo_read(fpc, start->offset, &read_len);
392  crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf, read_len);
393  read_len = (end->offset - start->offset) - read_len;
394 
395  if (read_len) {
396  buf = flac_fifo_read(fpc, end->offset - read_len, &read_len);
397  crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), crc, buf, read_len);
398  }
399  }
400 
401  if (!crc ^ !inverted_test) {
402  deduction += FLAC_HEADER_CRC_FAIL_PENALTY;
403  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
404  "crc check failed from offset %i (frame %"PRId64") to %i (frame %"PRId64")\n",
405  header->offset, header_fi->frame_or_sample_num,
406  child->offset, child_fi->frame_or_sample_num);
407  }
408  }
409  return deduction;
410 }
411 
412 /**
413  * Score a header.
414  *
415  * Give FLAC_HEADER_BASE_SCORE points to a frame for existing.
416  * If it has children, (subsequent frames of which the preceding CRC footer
417  * validates against this one,) then take the maximum score of the children,
418  * with a penalty of FLAC_HEADER_CHANGED_PENALTY applied for each change to
419  * bps, sample rate, channels, but not decorrelation mode, or blocksize,
420  * because it can change often.
421  **/
423 {
425  int dist = 0;
426  int child_score;
427  int base_score = FLAC_HEADER_BASE_SCORE;
428  if (header->max_score != FLAC_HEADER_NOT_SCORED_YET)
429  return header->max_score;
430 
431  /* Modify the base score with changes from the last output header */
432  if (fpc->last_fi_valid) {
433  /* Silence the log since this will be repeated if selected */
434  base_score -= check_header_fi_mismatch(fpc, &fpc->last_fi, &header->fi,
435  AV_LOG_DEBUG);
436  }
437 
438  header->max_score = base_score;
439 
440  /* Check and compute the children's scores. */
441  child = header->next;
442  for (dist = 0; dist < FLAC_MAX_SEQUENTIAL_HEADERS && child; dist++) {
443  /* Look at the child's frame header info and penalize suspicious
444  changes between the headers. */
445  if (header->link_penalty[dist] == FLAC_HEADER_NOT_PENALIZED_YET) {
446  header->link_penalty[dist] = check_header_mismatch(fpc, header,
447  child, AV_LOG_DEBUG);
448  }
449  child_score = score_header(fpc, child) - header->link_penalty[dist];
450 
451  if (FLAC_HEADER_BASE_SCORE + child_score > header->max_score) {
452  /* Keep the child because the frame scoring is dynamic. */
453  header->best_child = child;
454  header->max_score = base_score + child_score;
455  }
456  child = child->next;
457  }
458 
459  return header->max_score;
460 }
461 
463 {
464  FLACHeaderMarker *curr;
465  int best_score = 0;//FLAC_HEADER_NOT_SCORED_YET;
466  /* First pass to clear all old scores. */
467  for (curr = fpc->headers; curr; curr = curr->next)
469 
470  /* Do a second pass to score them all. */
471  for (curr = fpc->headers; curr; curr = curr->next) {
472  if (score_header(fpc, curr) > best_score) {
473  fpc->best_header = curr;
474  best_score = curr->max_score;
475  }
476  }
477 }
478 
479 static int get_best_header(FLACParseContext* fpc, const uint8_t **poutbuf,
480  int *poutbuf_size)
481 {
483  FLACHeaderMarker *child = header->best_child;
484  if (!child) {
485  *poutbuf_size = av_fifo_size(fpc->fifo_buf) - header->offset;
486  } else {
487  *poutbuf_size = child->offset - header->offset;
488 
489  /* If the child has suspicious changes, log them */
490  check_header_mismatch(fpc, header, child, 0);
491  }
492 
493  if (header->fi.channels != fpc->avctx->channels ||
494  !fpc->avctx->channel_layout) {
495  fpc->avctx->channels = header->fi.channels;
497  }
498  fpc->avctx->sample_rate = header->fi.samplerate;
499  fpc->pc->duration = header->fi.blocksize;
500  *poutbuf = flac_fifo_read_wrap(fpc, header->offset, *poutbuf_size,
501  &fpc->wrap_buf,
503 
504 
505  if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS){
506  if (header->fi.is_var_size)
507  fpc->pc->pts = header->fi.frame_or_sample_num;
508  else if (header->best_child)
509  fpc->pc->pts = header->fi.frame_or_sample_num * header->fi.blocksize;
510  }
511 
512  fpc->best_header_valid = 0;
513  fpc->last_fi_valid = 1;
514  fpc->last_fi = header->fi;
515 
516  /* Return the negative overread index so the client can compute pos.
517  This should be the amount overread to the beginning of the child */
518  if (child)
519  return child->offset - av_fifo_size(fpc->fifo_buf);
520  return 0;
521 }
522 
524  const uint8_t **poutbuf, int *poutbuf_size,
525  const uint8_t *buf, int buf_size)
526 {
527  FLACParseContext *fpc = s->priv_data;
528  FLACHeaderMarker *curr;
529  int nb_headers;
530  const uint8_t *read_end = buf;
531  const uint8_t *read_start = buf;
532 
535  if (frame_header_is_valid(avctx, buf, &fi)) {
536  s->duration = fi.blocksize;
537  if (!avctx->sample_rate)
538  avctx->sample_rate = fi.samplerate;
539  if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS){
540  fpc->pc->pts = fi.frame_or_sample_num;
541  if (!fi.is_var_size)
542  fpc->pc->pts *= fi.blocksize;
543  }
544  }
545  *poutbuf = buf;
546  *poutbuf_size = buf_size;
547  return buf_size;
548  }
549 
550  fpc->avctx = avctx;
551  if (fpc->best_header_valid)
552  return get_best_header(fpc, poutbuf, poutbuf_size);
553 
554  /* If a best_header was found last call remove it with the buffer data. */
555  if (fpc->best_header && fpc->best_header->best_child) {
558 
559  /* Remove headers in list until the end of the best_header. */
560  for (curr = fpc->headers; curr != best_child; curr = temp) {
561  if (curr != fpc->best_header) {
562  av_log(avctx, AV_LOG_DEBUG,
563  "dropping low score %i frame header from offset %i to %i\n",
564  curr->max_score, curr->offset, curr->next->offset);
565  }
566  temp = curr->next;
567  av_freep(&curr->link_penalty);
568  av_free(curr);
569  fpc->nb_headers_buffered--;
570  }
571  /* Release returned data from ring buffer. */
572  av_fifo_drain(fpc->fifo_buf, best_child->offset);
573 
574  /* Fix the offset for the headers remaining to match the new buffer. */
575  for (curr = best_child->next; curr; curr = curr->next)
576  curr->offset -= best_child->offset;
577 
578  fpc->nb_headers_buffered--;
579  best_child->offset = 0;
580  fpc->headers = best_child;
582  fpc->best_header = best_child;
583  return get_best_header(fpc, poutbuf, poutbuf_size);
584  }
585  fpc->best_header = NULL;
586  } else if (fpc->best_header) {
587  /* No end frame no need to delete the buffer; probably eof */
589 
590  for (curr = fpc->headers; curr != fpc->best_header; curr = temp) {
591  temp = curr->next;
592  av_freep(&curr->link_penalty);
593  av_free(curr);
594  fpc->nb_headers_buffered--;
595  }
596  fpc->headers = fpc->best_header->next;
598  av_freep(&fpc->best_header);
599  fpc->nb_headers_buffered--;
600  }
601 
602  /* Find and score new headers. */
603  /* buf_size is to zero when padding, so check for this since we do */
604  /* not want to try to read more input once we have found the end. */
605  /* Note that as (non-modified) parameters, buf can be non-NULL, */
606  /* while buf_size is 0. */
607  while ((buf && buf_size && read_end < buf + buf_size &&
609  || ((!buf || !buf_size) && !fpc->end_padded)) {
610  int start_offset;
611 
612  /* Pad the end once if EOF, to check the final region for headers. */
613  if (!buf || !buf_size) {
614  fpc->end_padded = 1;
615  buf_size = MAX_FRAME_HEADER_SIZE;
616  read_end = read_start + MAX_FRAME_HEADER_SIZE;
617  } else {
618  /* The maximum read size is the upper-bound of what the parser
619  needs to have the required number of frames buffered */
620  int nb_desired = FLAC_MIN_HEADERS - fpc->nb_headers_buffered + 1;
621  read_end = read_end + FFMIN(buf + buf_size - read_end,
622  nb_desired * FLAC_AVG_FRAME_SIZE);
623  }
624 
625  if (!av_fifo_space(fpc->fifo_buf) &&
627  fpc->nb_headers_buffered * 20) {
628  /* There is less than one valid flac header buffered for 20 headers
629  * buffered. Therefore the fifo is most likely filled with invalid
630  * data and the input is not a flac file. */
631  goto handle_error;
632  }
633 
634  /* Fill the buffer. */
635  if ( av_fifo_space(fpc->fifo_buf) < read_end - read_start
636  && av_fifo_realloc2(fpc->fifo_buf, (read_end - read_start) + 2*av_fifo_size(fpc->fifo_buf)) < 0) {
637  av_log(avctx, AV_LOG_ERROR,
638  "couldn't reallocate buffer of size %"PTRDIFF_SPECIFIER"\n",
639  (read_end - read_start) + av_fifo_size(fpc->fifo_buf));
640  goto handle_error;
641  }
642 
643  if (buf && buf_size) {
644  av_fifo_generic_write(fpc->fifo_buf, (void*) read_start,
645  read_end - read_start, NULL);
646  } else {
647  int8_t pad[MAX_FRAME_HEADER_SIZE] = { 0 };
648  av_fifo_generic_write(fpc->fifo_buf, pad, sizeof(pad), NULL);
649  }
650 
651  /* Tag headers and update sequences. */
652  start_offset = av_fifo_size(fpc->fifo_buf) -
653  ((read_end - read_start) + (MAX_FRAME_HEADER_SIZE - 1));
654  start_offset = FFMAX(0, start_offset);
655  nb_headers = find_new_headers(fpc, start_offset);
656 
657  if (nb_headers < 0) {
658  av_log(avctx, AV_LOG_ERROR,
659  "find_new_headers couldn't allocate FLAC header\n");
660  goto handle_error;
661  }
662 
663  fpc->nb_headers_buffered = nb_headers;
664  /* Wait till FLAC_MIN_HEADERS to output a valid frame. */
665  if (!fpc->end_padded && fpc->nb_headers_buffered < FLAC_MIN_HEADERS) {
666  if (buf && read_end < buf + buf_size) {
667  read_start = read_end;
668  continue;
669  } else {
670  goto handle_error;
671  }
672  }
673 
674  /* If headers found, update the scores since we have longer chains. */
675  if (fpc->end_padded || fpc->nb_headers_found)
676  score_sequences(fpc);
677 
678  /* restore the state pre-padding */
679  if (fpc->end_padded) {
680  int warp = fpc->fifo_buf->wptr - fpc->fifo_buf->buffer < MAX_FRAME_HEADER_SIZE;
681  /* HACK: drain the tail of the fifo */
684  if (warp) {
685  fpc->fifo_buf->wptr += fpc->fifo_buf->end -
686  fpc->fifo_buf->buffer;
687  }
688  buf_size = 0;
689  read_start = read_end = NULL;
690  }
691  }
692 
693  for (curr = fpc->headers; curr; curr = curr->next) {
694  if (!fpc->best_header || curr->max_score > fpc->best_header->max_score) {
695  fpc->best_header = curr;
696  }
697  }
698 
699  if (fpc->best_header && fpc->best_header->max_score <= 0) {
700  // Only accept a bad header if there is no other option to continue
701  if (!buf_size || !buf || read_end != buf || fpc->nb_headers_buffered < FLAC_MIN_HEADERS)
702  fpc->best_header = NULL;
703  }
704 
705  if (fpc->best_header) {
706  fpc->best_header_valid = 1;
707  if (fpc->best_header->offset > 0) {
708  /* Output a junk frame. */
709  av_log(avctx, AV_LOG_DEBUG, "Junk frame till offset %i\n",
710  fpc->best_header->offset);
711 
712  /* Set duration to 0. It is unknown or invalid in a junk frame. */
713  s->duration = 0;
714  *poutbuf_size = fpc->best_header->offset;
715  *poutbuf = flac_fifo_read_wrap(fpc, 0, *poutbuf_size,
716  &fpc->wrap_buf,
718  return buf_size ? (read_end - buf) : (fpc->best_header->offset -
719  av_fifo_size(fpc->fifo_buf));
720  }
721  if (!buf_size)
722  return get_best_header(fpc, poutbuf, poutbuf_size);
723  }
724 
725 handle_error:
726  *poutbuf = NULL;
727  *poutbuf_size = 0;
728  return buf_size ? read_end - buf : 0;
729 }
730 
732 {
733  FLACParseContext *fpc = c->priv_data;
734  fpc->pc = c;
735  /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before
736  it drains. This is allocated early to avoid slow reallocation. */
738  if (!fpc->fifo_buf) {
739  av_log(fpc->avctx, AV_LOG_ERROR,
740  "couldn't allocate fifo_buf\n");
741  return AVERROR(ENOMEM);
742  }
743  return 0;
744 }
745 
747 {
748  FLACParseContext *fpc = c->priv_data;
749  FLACHeaderMarker *curr = fpc->headers, *temp;
750 
751  while (curr) {
752  temp = curr->next;
753  av_freep(&curr->link_penalty);
754  av_free(curr);
755  curr = temp;
756  }
757  av_fifo_freep(&fpc->fifo_buf);
758  av_freep(&fpc->wrap_buf);
759 }
760 
763  .priv_data_size = sizeof(FLACParseContext),
764  .parser_init = flac_parse_init,
765  .parser_parse = flac_parse,
766  .parser_close = flac_parse_close,
767 };
static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf, FLACFrameInfo *fi)
Definition: flac_parser.c:95
#define NULL
Definition: coverity.c:32
static void score_sequences(FLACParseContext *fpc)
Definition: flac_parser.c:462
int size
uint8_t * wptr
Definition: fifo.h:33
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
else temp
Definition: vf_mcdeint.c:256
int64_t frame_or_sample_num
frame number or sample number
Definition: flac.h:88
int codec_ids[5]
Definition: avcodec.h:5216
AVFifoBuffer * fifo_buf
buffer to store all data until headers can be verified
Definition: flac_parser.c:86
int duration
Duration of the current frame.
Definition: avcodec.h:5170
#define FLAC_HEADER_CRC_FAIL_PENALTY
Definition: flac_parser.c:52
FLACCOMMONINFO int blocksize
block size of the frame
Definition: flac.h:86
Macro definitions for various function/variable attributes.
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
Definition: fifo.c:122
AVCodecContext * avctx
codec context pointer for logging
Definition: flac_parser.c:77
int best_header_valid
flag set when the parser returns junk; if set return best_header next time
Definition: flac_parser.c:84
#define FLAC_AVG_FRAME_SIZE
estimate for average size of a FLAC frame
Definition: flac_parser.c:47
FLACFrameInfo last_fi
last decoded frame header info
Definition: flac_parser.c:91
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
int nb_headers_found
number of headers found in the last flac_parse() call
Definition: flac_parser.c:81
#define f(width, name)
Definition: cbs_vp9.c:255
AVS_FilterInfo AVS_Value child
Definition: avisynth_c.h:731
#define AV_RB32
Definition: intreadwrite.h:130
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int wrap_buf_allocated_size
actual allocated size of the buffer
Definition: flac_parser.c:90
int av_fifo_space(const AVFifoBuffer *f)
Return the amount of space in bytes in the AVFifoBuffer, that is the amount of data you can write int...
Definition: fifo.c:82
#define FLAC_HEADER_NOT_SCORED_YET
Definition: flac_parser.c:54
Public header for CRC hash function implementation.
static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf, int *poutbuf_size)
Definition: flac_parser.c:479
#define FLAC_HEADER_CHANGED_PENALTY
Definition: flac_parser.c:51
static void flac_parse_close(AVCodecParserContext *c)
Definition: flac_parser.c:746
#define FLAC_MAX_SEQUENTIAL_HEADERS
maximum number of adjacent headers that compare CRCs against each other
Definition: flac_parser.c:43
static const uint8_t header[24]
Definition: sdr2.c:67
static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: flac_parser.c:523
#define av_log(a,...)
#define MAX_FRAME_VERIFY_SIZE
Definition: flac_parser.c:58
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:258
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define PTRDIFF_SPECIFIER
Definition: internal.h:263
static int check_header_fi_mismatch(FLACParseContext *fpc, FLACFrameInfo *header_fi, FLACFrameInfo *child_fi, int log_level_offset)
Definition: flac_parser.c:284
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define wrap(func)
Definition: neontest.h:65
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
#define FFMAX(a, b)
Definition: common.h:94
int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, FLACFrameInfo *fi, int log_level_offset)
Validate and decode a frame header.
Definition: flac.c:50
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2240
int end_padded
specifies if fifo_buf&#39;s end is padded
Definition: flac_parser.c:88
struct FLACHeaderMarker * next
next CRC-8 verified header that immediately follows this one in the bytestream
Definition: flac_parser.c:67
#define FFMIN(a, b)
Definition: common.h:96
FLACHeaderMarker * headers
linked-list that starts at the first CRC-8 verified header within buffer
Definition: flac_parser.c:78
#define FLAC_MIN_HEADERS
minimum number of headers buffered and checked before returning frames
Definition: flac_parser.c:45
uint8_t * end
Definition: fifo.h:33
uint8_t * rptr
Definition: fifo.h:33
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:392
#define s(width, name)
Definition: cbs_vp9.c:257
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:476
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.
Definition: flac_parser.c:116
int last_fi_valid
set if last_fi is valid
Definition: flac_parser.c:92
if(ret< 0)
Definition: vf_mcdeint.c:279
int max_score
maximum score found after checking each child that has a valid CRC
Definition: flac_parser.c:64
static int find_headers_search_validate(FLACParseContext *fpc, int offset)
Definition: flac_parser.c:167
int av_fifo_size(const AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
Definition: fifo.c:77
uint8_t * wrap_buf
general fifo read buffer when wrapped
Definition: flac_parser.c:89
int sample_rate
samples per second
Definition: avcodec.h:2189
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
Definition: fifo.c:87
#define FLAC_HEADER_NOT_PENALIZED_YET
Definition: flac_parser.c:53
main external API structure.
Definition: avcodec.h:1533
a very simple circular buffer FIFO implementation
static int find_new_headers(FLACParseContext *fpc, int search_start)
Definition: flac_parser.c:240
void * buf
Definition: avisynth_c.h:690
void ff_flac_set_channel_layout(AVCodecContext *avctx)
Definition: flac.c:196
uint8_t * buffer
Definition: fifo.h:32
static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header)
Score a header.
Definition: flac_parser.c:422
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:615
FLACFrameInfo fi
decoded frame header info
Definition: flac_parser.c:66
int * link_penalty
pointer to array of local scores between this header and the one at a distance equal array position ...
Definition: flac_parser.c:62
int nb_headers_buffered
number of headers that are buffered
Definition: flac_parser.c:83
#define MAX_FRAME_HEADER_SIZE
largest possible size of flac header
Definition: flac_parser.c:57
uint32_t wndx
Definition: fifo.h:34
AVCodecParserContext * pc
parent context
Definition: flac_parser.c:76
FLACHeaderMarker * best_header
highest scoring header within buffer
Definition: flac_parser.c:80
AVFifoBuffer * av_fifo_alloc_array(size_t nmemb, size_t size)
Initialize an AVFifoBuffer.
Definition: fifo.c:49
AVCodecParser ff_flac_parser
Definition: flac_parser.c:761
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
static int find_headers_search(FLACParseContext *fpc, uint8_t *buf, int buf_size, int search_start)
Definition: flac_parser.c:212
static double c[64]
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:5082
int offset
byte offset from start of FLACParseContext->buffer
Definition: flac_parser.c:61
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:782
#define av_free(p)
int len
int channels
number of audio channels
Definition: avcodec.h:2190
static av_cold int flac_parse_init(AVCodecParserContext *c)
Definition: flac_parser.c:731
#define FLAC_HEADER_BASE_SCORE
scoring settings for score_header
Definition: flac_parser.c:50
int is_var_size
specifies if the stream uses variable block sizes or a fixed block size; also determines the meaning ...
Definition: flac.h:89
static uint8_t * flac_fifo_read(FLACParseContext *fpc, int offset, int *len)
Return a pointer in the fifo buffer where the offset starts at until the wrap point or end of request...
Definition: flac_parser.c:156
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
Definition: fifo.c:63
struct FLACHeaderMarker * best_child
following frame header with which this frame has the best score with
Definition: flac_parser.c:70
void av_fifo_drain(AVFifoBuffer *f, int size)
Discard data from the FIFO.
Definition: fifo.c:233
#define PARSER_FLAG_USE_CODEC_TS
Definition: avcodec.h:5086
static int check_header_mismatch(FLACParseContext *fpc, FLACHeaderMarker *header, FLACHeaderMarker *child, int log_level_offset)
Definition: flac_parser.c:314