FFmpeg
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/mem.h"
38 #include "flac_parse.h"
39 
40 /** maximum number of adjacent headers that compare CRCs against each other */
41 #define FLAC_MAX_SEQUENTIAL_HEADERS 4
42 /** minimum number of headers buffered and checked before returning frames */
43 #define FLAC_MIN_HEADERS 10
44 /** estimate for average size of a FLAC frame */
45 #define FLAC_AVG_FRAME_SIZE 8192
46 
47 /** scoring settings for score_header */
48 #define FLAC_HEADER_BASE_SCORE 10
49 #define FLAC_HEADER_CHANGED_PENALTY 7
50 #define FLAC_HEADER_CRC_FAIL_PENALTY 50
51 #define FLAC_HEADER_NOT_PENALIZED_YET 100000
52 #define FLAC_HEADER_NOT_SCORED_YET -100000
53 
54 /** largest possible size of flac header */
55 #define MAX_FRAME_HEADER_SIZE 16
56 #define MAX_FRAME_VERIFY_SIZE (MAX_FRAME_HEADER_SIZE + 1)
57 
58 typedef struct FifoBuffer {
59  uint8_t *buffer;
60  uint8_t *end;
61  uint8_t *rptr;
62  uint8_t *wptr;
63  int empty;
64 } FifoBuffer;
65 
66 typedef struct FLACHeaderMarker {
67  int offset; /**< byte offset from start of FLACParseContext->buffer */
68  int link_penalty[FLAC_MAX_SEQUENTIAL_HEADERS]; /**< array of local scores
69  between this header and the one at a distance equal
70  array position */
71  int max_score; /**< maximum score found after checking each child that
72  has a valid CRC */
73  FLACFrameInfo fi; /**< decoded frame header info */
74  struct FLACHeaderMarker *next; /**< next CRC-8 verified header that
75  immediately follows this one in
76  the bytestream */
77  struct FLACHeaderMarker *best_child; /**< following frame header with
78  which this frame has the best
79  score with */
81 
82 typedef struct FLACParseContext {
83  AVCodecParserContext *pc; /**< parent context */
84  AVCodecContext *avctx; /**< codec context pointer for logging */
85  FLACHeaderMarker *headers; /**< linked-list that starts at the first
86  CRC-8 verified header within buffer */
87  FLACHeaderMarker *best_header; /**< highest scoring header within buffer */
88  int nb_headers_found; /**< number of headers found in the last
89  flac_parse() call */
90  int nb_headers_buffered; /**< number of headers that are buffered */
91  int best_header_valid; /**< flag set when the parser returns junk;
92  if set return best_header next time */
93  FifoBuffer fifo_buf; /**< buffer to store all data until headers
94  can be verified */
95  int end_padded; /**< specifies if fifo_buf's end is padded */
96  uint8_t *wrap_buf; /**< general fifo read buffer when wrapped */
97  int wrap_buf_allocated_size; /**< actual allocated size of the buffer */
98  FLACFrameInfo last_fi; /**< last decoded frame header info */
99  int last_fi_valid; /**< set if last_fi is valid */
101 
102 static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf,
103  FLACFrameInfo *fi)
104 {
105  GetBitContext gb;
106  uint8_t subframe_type;
107 
108  // header plus one byte from first subframe
109  init_get_bits(&gb, buf, MAX_FRAME_VERIFY_SIZE * 8);
110  if (ff_flac_decode_frame_header(avctx, &gb, fi, 127)) {
111  return 0;
112  }
113  // subframe zero bit
114  if (get_bits1(&gb) != 0) {
115  return 0;
116  }
117  // subframe type
118  // 000000 : SUBFRAME_CONSTANT
119  // 000001 : SUBFRAME_VERBATIM
120  // 00001x : reserved
121  // 0001xx : reserved
122  // 001xxx : if(xxx <= 4) SUBFRAME_FIXED, xxx=order ; else reserved
123  // 01xxxx : reserved
124  // 1xxxxx : SUBFRAME_LPC, xxxxx=order-1
125  subframe_type = get_bits(&gb, 6);
126  if (!(subframe_type == 0 ||
127  subframe_type == 1 ||
128  ((subframe_type >= 8) && (subframe_type <= 12)) ||
129  (subframe_type >= 32))) {
130  return 0;
131  }
132 
133  return 1;
134 }
135 
136 static size_t flac_fifo_size(const FifoBuffer *f)
137 {
138  if (f->wptr <= f->rptr && !f->empty)
139  return (f->wptr - f->buffer) + (f->end - f->rptr);
140  return f->wptr - f->rptr;
141 }
142 
143 static size_t flac_fifo_space(const FifoBuffer *f)
144 {
145  return f->end - f->buffer - flac_fifo_size(f);
146 }
147 
148 /**
149  * Non-destructive fast fifo pointer fetching
150  * Returns a pointer from the specified offset.
151  * If possible the pointer points within the fifo buffer.
152  * Otherwise (if it would cause a wrap around,) a pointer to a user-specified
153  * buffer is used.
154  * The pointer can be NULL. In any case it will be reallocated to hold the size.
155  * If the returned pointer will be used after subsequent calls to flac_fifo_read_wrap
156  * then the subsequent calls should pass in a different wrap_buf so as to not
157  * overwrite the contents of the previous wrap_buf.
158  * This function is based on av_fifo_generic_read, which is why there is a comment
159  * about a memory barrier for SMP.
160  */
161 static uint8_t *flac_fifo_read_wrap(FLACParseContext *fpc, int offset, int len,
162  uint8_t **wrap_buf, int *allocated_size)
163 {
164  FifoBuffer *f = &fpc->fifo_buf;
165  uint8_t *start = f->rptr + offset;
166  uint8_t *tmp_buf;
167 
168  if (start >= f->end)
169  start -= f->end - f->buffer;
170  if (f->end - start >= len)
171  return start;
172 
173  tmp_buf = av_fast_realloc(*wrap_buf, allocated_size, len);
174 
175  if (!tmp_buf) {
176  av_log(fpc->avctx, AV_LOG_ERROR,
177  "couldn't reallocate wrap buffer of size %d", len);
178  return NULL;
179  }
180  *wrap_buf = tmp_buf;
181  do {
182  int seg_len = FFMIN(f->end - start, len);
183  memcpy(tmp_buf, start, seg_len);
184  tmp_buf = (uint8_t*)tmp_buf + seg_len;
185 // memory barrier needed for SMP here in theory
186 
187  start += seg_len - (f->end - f->buffer);
188  len -= seg_len;
189  } while (len > 0);
190 
191  return *wrap_buf;
192 }
193 
194 /**
195  * Return a pointer in the fifo buffer where the offset starts at until
196  * the wrap point or end of request.
197  * len will contain the valid length of the returned buffer.
198  * A second call to flac_fifo_read (with new offset and len) should be called
199  * to get the post-wrap buf if the returned len is less than the requested.
200  **/
201 static uint8_t *flac_fifo_read(FifoBuffer *f, int offset, int *len)
202 {
203  uint8_t *start = f->rptr + offset;
204 
205  if (start >= f->end)
206  start -= f->end - f->buffer;
207  *len = FFMIN(*len, f->end - start);
208  return start;
209 }
210 
211 static int flac_fifo_grow(FifoBuffer *f, size_t inc)
212 {
213  size_t size_old = f->end - f->buffer;
214  size_t offset_r = f->rptr - f->buffer;
215  size_t offset_w = f->wptr - f->buffer;
216  size_t size_new;
217 
218  uint8_t *tmp;
219 
220  if (size_old > SIZE_MAX - inc)
221  return AVERROR(EINVAL);
222  size_new = size_old + inc;
223 
224  tmp = av_realloc(f->buffer, size_new);
225  if (!tmp)
226  return AVERROR(ENOMEM);
227 
228  // move the data from the beginning of the ring buffer
229  // to the newly allocated space
230  if (offset_w <= offset_r && !f->empty) {
231  const size_t copy = FFMIN(inc, offset_w);
232  memcpy(tmp + size_old, tmp, copy);
233  if (copy < offset_w) {
234  memmove(tmp, tmp + copy, offset_w - copy);
235  offset_w -= copy;
236  } else
237  offset_w = size_old + copy;
238  }
239 
240  f->buffer = tmp;
241  f->end = f->buffer + size_new;
242  f->rptr = f->buffer + offset_r;
243  f->wptr = f->buffer + offset_w;
244 
245  return 0;
246 }
247 
248 static int flac_fifo_write(FifoBuffer *f, const uint8_t *src, size_t size)
249 {
250  uint8_t *wptr;
251 
252  if (flac_fifo_space(f) < size) {
254  if (ret < 0)
255  return ret;
256  }
257 
258  if (size)
259  f->empty = 0;
260 
261  wptr = f->wptr;
262  do {
263  size_t len = FFMIN(f->end - wptr, size);
264  memcpy(wptr, src, len);
265  src += len;
266  wptr += len;
267  if (wptr >= f->end)
268  wptr = f->buffer;
269  size -= len;
270  } while (size > 0);
271 
272  f->wptr = wptr;
273 
274  return 0;
275 }
276 
277 static void flac_fifo_drain(FifoBuffer *f, size_t size)
278 {
279  size_t size_cur = flac_fifo_size(f);
280 
281  av_assert0(size_cur >= size);
282  if (size_cur == size)
283  f->empty = 1;
284 
285  f->rptr += size;
286  if (f->rptr >= f->end)
287  f->rptr -= f->end - f->buffer;
288 }
289 
290 static int flac_fifo_alloc(FifoBuffer *f, size_t size)
291 {
292  memset(f, 0, sizeof(*f));
293 
294  f->buffer = av_realloc(NULL, size);
295  if (!f->buffer)
296  return AVERROR(ENOMEM);
297 
298  f->wptr = f->buffer;
299  f->rptr = f->buffer;
300  f->end = f->buffer + size;
301 
302  f->empty = 1;
303 
304  return 0;
305 }
306 
308 {
309  av_freep(&f->buffer);
310  memset(f, 0, sizeof(*f));
311 }
312 
314 {
315  FLACFrameInfo fi;
316  uint8_t *header_buf;
317  int size = 0;
318  header_buf = flac_fifo_read_wrap(fpc, offset,
320  &fpc->wrap_buf,
322  if (frame_header_is_valid(fpc->avctx, header_buf, &fi)) {
323  FLACHeaderMarker **end_handle = &fpc->headers;
324  int i;
325 
326  size = 0;
327  while (*end_handle) {
328  end_handle = &(*end_handle)->next;
329  size++;
330  }
331 
332  *end_handle = av_mallocz(sizeof(**end_handle));
333  if (!*end_handle) {
334  av_log(fpc->avctx, AV_LOG_ERROR,
335  "couldn't allocate FLACHeaderMarker\n");
336  return AVERROR(ENOMEM);
337  }
338  (*end_handle)->fi = fi;
339  (*end_handle)->offset = offset;
340 
341  for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++)
342  (*end_handle)->link_penalty[i] = FLAC_HEADER_NOT_PENALIZED_YET;
343 
344  fpc->nb_headers_found++;
345  size++;
346  }
347  return size;
348 }
349 
350 static int find_headers_search(FLACParseContext *fpc, uint8_t *buf,
351  int buf_size, int search_start)
352 {
353  int size = 0, mod_offset = (buf_size - 1) % 4, i, j;
354  uint32_t x;
355 
356  for (i = 0; i < mod_offset; i++) {
357  if ((AV_RB16(buf + i) & 0xFFFE) == 0xFFF8) {
358  int ret = find_headers_search_validate(fpc, search_start + i);
359  size = FFMAX(size, ret);
360  }
361  }
362 
363  for (; i < buf_size - 1; i += 4) {
364  x = AV_RN32(buf + i);
365  if (((x & ~(x + 0x01010101)) & 0x80808080)) {
366  for (j = 0; j < 4; j++) {
367  if ((AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8) {
368  int ret = find_headers_search_validate(fpc, search_start + i + j);
369  size = FFMAX(size, ret);
370  }
371  }
372  }
373  }
374  return size;
375 }
376 
377 static int find_new_headers(FLACParseContext *fpc, int search_start)
378 {
379  FLACHeaderMarker *end;
380  int search_end, size = 0, read_len, temp;
381  uint8_t *buf;
382  fpc->nb_headers_found = 0;
383 
384  /* Search for a new header of at most 16 bytes. */
385  search_end = flac_fifo_size(&fpc->fifo_buf) - (MAX_FRAME_HEADER_SIZE - 1);
386  read_len = search_end - search_start + 1;
387  buf = flac_fifo_read(&fpc->fifo_buf, search_start, &read_len);
388  size = find_headers_search(fpc, buf, read_len, search_start);
389  search_start += read_len - 1;
390 
391  /* If fifo end was hit do the wrap around. */
392  if (search_start != search_end) {
393  uint8_t wrap[2];
394 
395  wrap[0] = buf[read_len - 1];
396  /* search_start + 1 is the post-wrap offset in the fifo. */
397  read_len = search_end - (search_start + 1) + 1;
398 
399  buf = flac_fifo_read(&fpc->fifo_buf, search_start + 1, &read_len);
400  wrap[1] = buf[0];
401 
402  if ((AV_RB16(wrap) & 0xFFFE) == 0xFFF8) {
403  temp = find_headers_search_validate(fpc, search_start);
404  size = FFMAX(size, temp);
405  }
406  search_start++;
407 
408  /* Continue to do the last half of the wrap. */
409  temp = find_headers_search(fpc, buf, read_len, search_start);
410  size = FFMAX(size, temp);
411  search_start += read_len - 1;
412  }
413 
414  /* Return the size even if no new headers were found. */
415  if (!size && fpc->headers)
416  for (end = fpc->headers; end; end = end->next)
417  size++;
418  return size;
419 }
420 
422  FLACFrameInfo *header_fi,
423  FLACFrameInfo *child_fi,
424  int log_level_offset)
425 {
426  int deduction = 0;
427  if (child_fi->samplerate != header_fi->samplerate) {
428  deduction += FLAC_HEADER_CHANGED_PENALTY;
429  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
430  "sample rate change detected in adjacent frames\n");
431  }
432  if (child_fi->bps != header_fi->bps) {
433  deduction += FLAC_HEADER_CHANGED_PENALTY;
434  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
435  "bits per sample change detected in adjacent frames\n");
436  }
437  if (child_fi->is_var_size != header_fi->is_var_size) {
438  /* Changing blocking strategy not allowed per the spec */
439  deduction += FLAC_HEADER_BASE_SCORE;
440  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
441  "blocking strategy change detected in adjacent frames\n");
442  }
443  if (child_fi->channels != header_fi->channels) {
444  deduction += FLAC_HEADER_CHANGED_PENALTY;
445  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
446  "number of channels change detected in adjacent frames\n");
447  }
448  return deduction;
449 }
450 
453  FLACHeaderMarker *child,
454  int log_level_offset)
455 {
456  FLACFrameInfo *header_fi = &header->fi, *child_fi = &child->fi;
457  int check_crc, deduction, deduction_expected = 0, i;
458  deduction = check_header_fi_mismatch(fpc, header_fi, child_fi,
459  log_level_offset);
460  /* Check sample and frame numbers. */
461  if ((child_fi->frame_or_sample_num - header_fi->frame_or_sample_num
462  != header_fi->blocksize) &&
463  (child_fi->frame_or_sample_num
464  != header_fi->frame_or_sample_num + 1)) {
465  FLACHeaderMarker *curr;
466  int64_t expected_frame_num, expected_sample_num;
467  /* If there are frames in the middle we expect this deduction,
468  as they are probably valid and this one follows it */
469 
470  expected_frame_num = expected_sample_num = header_fi->frame_or_sample_num;
471  curr = header;
472  while (curr != child) {
473  /* Ignore frames that failed all crc checks */
474  for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++) {
476  expected_frame_num++;
477  expected_sample_num += curr->fi.blocksize;
478  break;
479  }
480  }
481  curr = curr->next;
482  }
483 
484  if (expected_frame_num == child_fi->frame_or_sample_num ||
485  expected_sample_num == child_fi->frame_or_sample_num)
486  deduction_expected = deduction ? 0 : 1;
487 
488  deduction += FLAC_HEADER_CHANGED_PENALTY;
489  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
490  "sample/frame number mismatch in adjacent frames\n");
491  }
492 
493  if (fpc->last_fi.is_var_size == header_fi->is_var_size) {
494  if (fpc->last_fi.is_var_size &&
496  check_crc = 0;
497  } else if (!fpc->last_fi.is_var_size &&
498  fpc->last_fi.frame_or_sample_num + 1 == header_fi->frame_or_sample_num) {
499  check_crc = 0;
500  } else {
501  check_crc = !deduction && !deduction_expected;
502  }
503  } else {
504  check_crc = !deduction && !deduction_expected;
505  }
506 
507  /* If we have suspicious headers, check the CRC between them */
508  if (check_crc || (deduction && !deduction_expected)) {
509  FLACHeaderMarker *curr;
510  int read_len;
511  uint8_t *buf;
512  uint32_t crc = 1;
513  int inverted_test = 0;
514 
515  /* Since CRC is expensive only do it if we haven't yet.
516  This assumes a CRC penalty is greater than all other check penalties */
517  curr = header->next;
518  for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS && curr != child; i++)
519  curr = curr->next;
520 
522 
523  if (header->link_penalty[i] < FLAC_HEADER_CRC_FAIL_PENALTY ||
524  header->link_penalty[i] == FLAC_HEADER_NOT_PENALIZED_YET) {
525  FLACHeaderMarker *start, *end;
526 
527  /* Although overlapping chains are scored, the crc should never
528  have to be computed twice for a single byte. */
529  start = header;
530  end = child;
531  if (i > 0 &&
532  header->link_penalty[i - 1] >= FLAC_HEADER_CRC_FAIL_PENALTY) {
533  while (start->next != child)
534  start = start->next;
535  inverted_test = 1;
536  } else if (i > 0 &&
537  header->next->link_penalty[i-1] >=
539  end = header->next;
540  inverted_test = 1;
541  }
542 
543  read_len = end->offset - start->offset;
544  buf = flac_fifo_read(&fpc->fifo_buf, start->offset, &read_len);
545  crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf, read_len);
546  read_len = (end->offset - start->offset) - read_len;
547 
548  if (read_len) {
549  buf = flac_fifo_read(&fpc->fifo_buf, end->offset - read_len, &read_len);
550  crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), crc, buf, read_len);
551  }
552  }
553 
554  if (!crc ^ !inverted_test) {
555  deduction += FLAC_HEADER_CRC_FAIL_PENALTY;
556  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
557  "crc check failed from offset %i (frame %"PRId64") to %i (frame %"PRId64")\n",
558  header->offset, header_fi->frame_or_sample_num,
559  child->offset, child_fi->frame_or_sample_num);
560  }
561  }
562  return deduction;
563 }
564 
565 /**
566  * Score a header.
567  *
568  * Give FLAC_HEADER_BASE_SCORE points to a frame for existing.
569  * If it has children, (subsequent frames of which the preceding CRC footer
570  * validates against this one,) then take the maximum score of the children,
571  * with a penalty of FLAC_HEADER_CHANGED_PENALTY applied for each change to
572  * bps, sample rate, channels, but not decorrelation mode, or blocksize,
573  * because it can change often.
574  **/
576 {
577  FLACHeaderMarker *child;
578  int dist = 0;
579  int child_score;
580  int base_score = FLAC_HEADER_BASE_SCORE;
581  if (header->max_score != FLAC_HEADER_NOT_SCORED_YET)
582  return header->max_score;
583 
584  /* Modify the base score with changes from the last output header */
585  if (fpc->last_fi_valid) {
586  /* Silence the log since this will be repeated if selected */
587  base_score -= check_header_fi_mismatch(fpc, &fpc->last_fi, &header->fi,
588  AV_LOG_DEBUG);
589  }
590 
591  header->max_score = base_score;
592 
593  /* Check and compute the children's scores. */
594  child = header->next;
595  for (dist = 0; dist < FLAC_MAX_SEQUENTIAL_HEADERS && child; dist++) {
596  /* Look at the child's frame header info and penalize suspicious
597  changes between the headers. */
598  if (header->link_penalty[dist] == FLAC_HEADER_NOT_PENALIZED_YET) {
599  header->link_penalty[dist] = check_header_mismatch(fpc, header,
600  child, AV_LOG_DEBUG);
601  }
602  child_score = score_header(fpc, child) - header->link_penalty[dist];
603 
604  if (FLAC_HEADER_BASE_SCORE + child_score > header->max_score) {
605  /* Keep the child because the frame scoring is dynamic. */
606  header->best_child = child;
607  header->max_score = base_score + child_score;
608  }
609  child = child->next;
610  }
611 
612  return header->max_score;
613 }
614 
616 {
617  FLACHeaderMarker *curr;
618  int best_score = FLAC_HEADER_NOT_SCORED_YET;
619  /* First pass to clear all old scores. */
620  for (curr = fpc->headers; curr; curr = curr->next)
622 
623  /* Do a second pass to score them all. */
624  for (curr = fpc->headers; curr; curr = curr->next) {
625  if (score_header(fpc, curr) > best_score) {
626  fpc->best_header = curr;
627  best_score = curr->max_score;
628  }
629  }
630 }
631 
632 static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf,
633  int *poutbuf_size)
634 {
636  FLACHeaderMarker *child = header->best_child;
637  if (!child) {
638  *poutbuf_size = flac_fifo_size(&fpc->fifo_buf) - header->offset;
639  } else {
640  *poutbuf_size = child->offset - header->offset;
641 
642  /* If the child has suspicious changes, log them */
643  check_header_mismatch(fpc, header, child, 0);
644  }
645 
646  ff_flac_set_channel_layout(fpc->avctx, header->fi.channels);
647 
648  fpc->avctx->sample_rate = header->fi.samplerate;
649  fpc->pc->duration = header->fi.blocksize;
650  *poutbuf = flac_fifo_read_wrap(fpc, header->offset, *poutbuf_size,
651  &fpc->wrap_buf,
653 
654  if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) {
655  if (header->fi.is_var_size)
656  fpc->pc->pts = header->fi.frame_or_sample_num;
657  else if (header->best_child)
658  fpc->pc->pts = header->fi.frame_or_sample_num * header->fi.blocksize;
659  }
660 
661  fpc->best_header_valid = 0;
662  fpc->last_fi_valid = 1;
663  fpc->last_fi = header->fi;
664 
665  /* Return the negative overread index so the client can compute pos.
666  This should be the amount overread to the beginning of the child */
667  if (child) {
668  int64_t offset = child->offset - flac_fifo_size(&fpc->fifo_buf);
669  if (offset > -(1 << 28))
670  return offset;
671  }
672  return 0;
673 }
674 
676  const uint8_t **poutbuf, int *poutbuf_size,
677  const uint8_t *buf, int buf_size)
678 {
679  FLACParseContext *fpc = s->priv_data;
680  FLACHeaderMarker *curr;
681  int nb_headers;
682  const uint8_t *read_end = buf;
683  const uint8_t *read_start = buf;
684 
685  if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
686  FLACFrameInfo fi;
687  if (frame_header_is_valid(avctx, buf, &fi)) {
688  s->duration = fi.blocksize;
689  if (!avctx->sample_rate)
690  avctx->sample_rate = fi.samplerate;
691  if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) {
692  fpc->pc->pts = fi.frame_or_sample_num;
693  if (!fi.is_var_size)
694  fpc->pc->pts *= fi.blocksize;
695  }
696  }
697  *poutbuf = buf;
698  *poutbuf_size = buf_size;
699  return buf_size;
700  }
701 
702  fpc->avctx = avctx;
704  return get_best_header(fpc, poutbuf, poutbuf_size);
705 
706  /* If a best_header was found last call remove it with the buffer data. */
707  if (fpc->best_header && fpc->best_header->best_child) {
709  FLACHeaderMarker *best_child = fpc->best_header->best_child;
710 
711  /* Remove headers in list until the end of the best_header. */
712  for (curr = fpc->headers; curr != best_child; curr = temp) {
713  if (curr != fpc->best_header) {
714  av_log(avctx, AV_LOG_DEBUG,
715  "dropping low score %i frame header from offset %i to %i\n",
716  curr->max_score, curr->offset, curr->next->offset);
717  }
718  temp = curr->next;
719  av_free(curr);
720  fpc->nb_headers_buffered--;
721  }
722  /* Release returned data from ring buffer. */
723  flac_fifo_drain(&fpc->fifo_buf, best_child->offset);
724 
725  /* Fix the offset for the headers remaining to match the new buffer. */
726  for (curr = best_child->next; curr; curr = curr->next)
727  curr->offset -= best_child->offset;
728 
729  best_child->offset = 0;
730  fpc->headers = best_child;
732  fpc->best_header = best_child;
733  return get_best_header(fpc, poutbuf, poutbuf_size);
734  }
735  fpc->best_header = NULL;
736  } else if (fpc->best_header) {
737  /* No end frame no need to delete the buffer; probably eof */
739 
740  for (curr = fpc->headers; curr != fpc->best_header; curr = temp) {
741  temp = curr->next;
742  av_free(curr);
743  fpc->nb_headers_buffered--;
744  }
745  fpc->headers = fpc->best_header->next;
746  av_freep(&fpc->best_header);
747  fpc->nb_headers_buffered--;
748  }
749 
750  /* Find and score new headers. */
751  /* buf_size is zero when flushing, so check for this since we do */
752  /* not want to try to read more input once we have found the end. */
753  /* Also note that buf can't be NULL. */
754  while ((buf_size && read_end < buf + buf_size &&
756  || (!buf_size && !fpc->end_padded)) {
757  int start_offset, ret;
758 
759  /* Pad the end once if EOF, to check the final region for headers. */
760  if (!buf_size) {
761  fpc->end_padded = 1;
762  read_end = read_start + MAX_FRAME_HEADER_SIZE;
763  } else {
764  /* The maximum read size is the upper-bound of what the parser
765  needs to have the required number of frames buffered */
766  int nb_desired = FLAC_MIN_HEADERS - fpc->nb_headers_buffered + 1;
767  read_end = read_end + FFMIN(buf + buf_size - read_end,
768  nb_desired * FLAC_AVG_FRAME_SIZE);
769  }
770 
771  if (!flac_fifo_space(&fpc->fifo_buf) &&
773  fpc->nb_headers_buffered * 20) {
774  /* There is less than one valid flac header buffered for 20 headers
775  * buffered. Therefore the fifo is most likely filled with invalid
776  * data and the input is not a flac file. */
777  goto handle_error;
778  }
779 
780  /* Fill the buffer. */
781  if (buf_size) {
782  ret = flac_fifo_write(&fpc->fifo_buf, read_start,
783  read_end - read_start);
784  } else {
785  int8_t pad[MAX_FRAME_HEADER_SIZE] = { 0 };
786  ret = flac_fifo_write(&fpc->fifo_buf, pad, sizeof(pad));
787  }
788  if (ret < 0) {
789  av_log(avctx, AV_LOG_ERROR, "Error buffering data\n");
790  goto handle_error;
791  }
792 
793  /* Tag headers and update sequences. */
794  start_offset = flac_fifo_size(&fpc->fifo_buf) -
795  ((read_end - read_start) + (MAX_FRAME_HEADER_SIZE - 1));
796  start_offset = FFMAX(0, start_offset);
797  nb_headers = find_new_headers(fpc, start_offset);
798 
799  if (nb_headers < 0) {
800  av_log(avctx, AV_LOG_ERROR,
801  "find_new_headers couldn't allocate FLAC header\n");
802  goto handle_error;
803  }
804 
805  fpc->nb_headers_buffered = nb_headers;
806  /* Wait till FLAC_MIN_HEADERS to output a valid frame. */
807  if (!fpc->end_padded && fpc->nb_headers_buffered < FLAC_MIN_HEADERS) {
808  if (read_end < buf + buf_size) {
809  read_start = read_end;
810  continue;
811  } else {
812  goto handle_error;
813  }
814  }
815 
816  /* If headers found, update the scores since we have longer chains. */
817  if (fpc->end_padded || fpc->nb_headers_found)
818  score_sequences(fpc);
819 
820  /* restore the state pre-padding */
821  if (fpc->end_padded) {
822  int empty = flac_fifo_size(&fpc->fifo_buf) == MAX_FRAME_HEADER_SIZE;
823  int warp = fpc->fifo_buf.wptr - fpc->fifo_buf.buffer < MAX_FRAME_HEADER_SIZE;
824  /* HACK: drain the tail of the fifo */
826  if (warp) {
827  fpc->fifo_buf.wptr += fpc->fifo_buf.end -
828  fpc->fifo_buf.buffer;
829  }
830  fpc->fifo_buf.empty = empty;
831  read_start = read_end = NULL;
832  }
833  }
834 
835  for (curr = fpc->headers; curr; curr = curr->next) {
836  if (!fpc->best_header || curr->max_score > fpc->best_header->max_score) {
837  fpc->best_header = curr;
838  }
839  }
840 
841  if (fpc->best_header && fpc->best_header->max_score <= 0) {
842  // Only accept a bad header if there is no other option to continue
843  if (!buf_size || read_end != buf || fpc->nb_headers_buffered < FLAC_MIN_HEADERS)
844  fpc->best_header = NULL;
845  }
846 
847  if (fpc->best_header) {
848  fpc->best_header_valid = 1;
849  if (fpc->best_header->offset > 0) {
850  /* Output a junk frame. */
851  av_log(avctx, AV_LOG_DEBUG, "Junk frame till offset %i\n",
852  fpc->best_header->offset);
853 
854  /* Set duration to 0. It is unknown or invalid in a junk frame. */
855  s->duration = 0;
856  *poutbuf_size = fpc->best_header->offset;
857  *poutbuf = flac_fifo_read_wrap(fpc, 0, *poutbuf_size,
858  &fpc->wrap_buf,
860  return buf_size ? (read_end - buf) : (fpc->best_header->offset -
861  flac_fifo_size(&fpc->fifo_buf));
862  }
863  if (!buf_size)
864  return get_best_header(fpc, poutbuf, poutbuf_size);
865  }
866 
867 handle_error:
868  *poutbuf = NULL;
869  *poutbuf_size = 0;
870  return buf_size ? read_end - buf : 0;
871 }
872 
874 {
875  FLACParseContext *fpc = c->priv_data;
876  int ret;
877 
878  fpc->pc = c;
879  /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before
880  it drains. This is allocated early to avoid slow reallocation. */
882  if (ret < 0) {
883  av_log(fpc->avctx, AV_LOG_ERROR,
884  "couldn't allocate fifo_buf\n");
885  return AVERROR(ENOMEM);
886  }
887  return 0;
888 }
889 
891 {
892  FLACParseContext *fpc = c->priv_data;
893  FLACHeaderMarker *curr = fpc->headers, *temp;
894 
895  while (curr) {
896  temp = curr->next;
897  av_free(curr);
898  curr = temp;
899  }
900  fpc->headers = NULL;
901  flac_fifo_free(&fpc->fifo_buf);
902  av_freep(&fpc->wrap_buf);
903 }
904 
907  .priv_data_size = sizeof(FLACParseContext),
908  .parser_init = flac_parse_init,
909  .parser_parse = flac_parse,
910  .parser_close = flac_parse_close,
911 };
FLACParseContext
Definition: flac_parser.c:82
FLACParseContext::headers
FLACHeaderMarker * headers
linked-list that starts at the first CRC-8 verified header within buffer
Definition: flac_parser.c:85
FLAC_HEADER_BASE_SCORE
#define FLAC_HEADER_BASE_SCORE
scoring settings for score_header
Definition: flac_parser.c:48
frame_header_is_valid
static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf, FLACFrameInfo *fi)
Definition: flac_parser.c:102
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
check_header_fi_mismatch
static int check_header_fi_mismatch(FLACParseContext *fpc, FLACFrameInfo *header_fi, FLACFrameInfo *child_fi, int log_level_offset)
Definition: flac_parser.c:421
FLACParseContext::nb_headers_buffered
int nb_headers_buffered
number of headers that are buffered
Definition: flac_parser.c:90
AVCodecParserContext::pts
int64_t pts
Definition: avcodec.h:2767
find_new_headers
static int find_new_headers(FLACParseContext *fpc, int search_start)
Definition: flac_parser.c:377
FLACHeaderMarker
Definition: flac_parser.c:66
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1056
flac_fifo_free
static void flac_fifo_free(FifoBuffer *f)
Definition: flac_parser.c:307
AVCodecParserContext::duration
int duration
Duration of the current frame.
Definition: avcodec.h:2862
int64_t
long long int64_t
Definition: coverity.c:34
FLACParseContext::wrap_buf
uint8_t * wrap_buf
general fifo read buffer when wrapped
Definition: flac_parser.c:96
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
FLACHeaderMarker::best_child
struct FLACHeaderMarker * best_child
following frame header with which this frame has the best score with
Definition: flac_parser.c:77
FifoBuffer::buffer
uint8_t * buffer
Definition: flac_parser.c:59
FLACFrameInfo::frame_or_sample_num
int64_t frame_or_sample_num
frame number or sample number
Definition: flac_parse.h:48
FLACFrameInfo::is_var_size
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
score_sequences
static void score_sequences(FLACParseContext *fpc)
Definition: flac_parser.c:615
FifoBuffer::end
uint8_t * end
Definition: flac_parser.c:60
AV_CODEC_ID_FLAC
@ AV_CODEC_ID_FLAC
Definition: codec_id.h:458
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
FLAC_HEADER_CHANGED_PENALTY
#define FLAC_HEADER_CHANGED_PENALTY
Definition: flac_parser.c:49
FLAC_MAX_SEQUENTIAL_HEADERS
#define FLAC_MAX_SEQUENTIAL_HEADERS
maximum number of adjacent headers that compare CRCs against each other
Definition: flac_parser.c:41
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
FLACParseContext::wrap_buf_allocated_size
int wrap_buf_allocated_size
actual allocated size of the buffer
Definition: flac_parser.c:97
crc.h
flac_parse.h
flac_fifo_read_wrap
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:161
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
ff_flac_parser
const AVCodecParser ff_flac_parser
Definition: flac_parser.c:905
ff_flac_set_channel_layout
void ff_flac_set_channel_layout(AVCodecContext *avctx, int channels)
Definition: flac.c:173
wrap
#define wrap(func)
Definition: neontest.h:65
ff_flac_decode_frame_header
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
GetBitContext
Definition: get_bits.h:108
FLACParseContext::best_header_valid
int best_header_valid
flag set when the parser returns junk; if set return best_header next time
Definition: flac_parser.c:91
get_best_header
static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf, int *poutbuf_size)
Definition: flac_parser.c:632
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
FLACParseContext::fifo_buf
FifoBuffer fifo_buf
buffer to store all data until headers can be verified
Definition: flac_parser.c:93
av_cold
#define av_cold
Definition: attributes.h:90
FLACFrameInfo::channels
int channels
number of channels
Definition: flac_parse.h:44
FLACHeaderMarker::next
struct FLACHeaderMarker * next
next CRC-8 verified header that immediately follows this one in the bytestream
Definition: flac_parser.c:74
av_fast_realloc
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:497
FifoBuffer::wptr
uint8_t * wptr
Definition: flac_parser.c:62
FLAC_AVG_FRAME_SIZE
#define FLAC_AVG_FRAME_SIZE
estimate for average size of a FLAC frame
Definition: flac_parser.c:45
s
#define s(width, name)
Definition: cbs_vp9.c:198
FLACFrameInfo::blocksize
int blocksize
block size of the frame
Definition: flac_parse.h:46
flac_fifo_alloc
static int flac_fifo_alloc(FifoBuffer *f, size_t size)
Definition: flac_parser.c:290
flac_fifo_read
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...
Definition: flac_parser.c:201
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
FLACParseContext::last_fi_valid
int last_fi_valid
set if last_fi is valid
Definition: flac_parser.c:99
FLACParseContext::avctx
AVCodecContext * avctx
codec context pointer for logging
Definition: flac_parser.c:84
find_headers_search_validate
static int find_headers_search_validate(FLACParseContext *fpc, int offset)
Definition: flac_parser.c:313
FLACParseContext::pc
AVCodecParserContext * pc
parent context
Definition: flac_parser.c:83
AV_CRC_16_ANSI
@ AV_CRC_16_ANSI
Definition: crc.h:50
score_header
static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header)
Score a header.
Definition: flac_parser.c:575
flac_fifo_drain
static void flac_fifo_drain(FifoBuffer *f, size_t size)
Definition: flac_parser.c:277
NULL
#define NULL
Definition: coverity.c:32
flac_parse
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:675
find_headers_search
static int find_headers_search(FLACParseContext *fpc, uint8_t *buf, int buf_size, int search_start)
Definition: flac_parser.c:350
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
AV_RN32
#define AV_RN32(p)
Definition: intreadwrite.h:360
AVCodecParserContext::flags
int flags
Definition: avcodec.h:2781
FLACFrameInfo
Definition: flac_parse.h:42
FLAC_HEADER_CRC_FAIL_PENALTY
#define FLAC_HEADER_CRC_FAIL_PENALTY
Definition: flac_parser.c:50
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
inc
static int inc(int num, int period)
Definition: perlin.c:34
FLAC_HEADER_NOT_SCORED_YET
#define FLAC_HEADER_NOT_SCORED_YET
Definition: flac_parser.c:52
f
f
Definition: af_crystalizer.c:122
flac_fifo_write
static int flac_fifo_write(FifoBuffer *f, const uint8_t *src, size_t size)
Definition: flac_parser.c:248
copy
static void copy(const float *p1, float *p2, const int length)
Definition: vf_vaguedenoiser.c:186
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2908
size
int size
Definition: twinvq_data.h:10344
flac_fifo_grow
static int flac_fifo_grow(FifoBuffer *f, size_t inc)
Definition: flac_parser.c:211
header
static const uint8_t header[24]
Definition: sdr2.c:68
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
attributes.h
flac_parse_init
static av_cold int flac_parse_init(AVCodecParserContext *c)
Definition: flac_parser.c:873
FLACHeaderMarker::fi
FLACFrameInfo fi
decoded frame header info
Definition: flac_parser.c:73
FLACParseContext::best_header
FLACHeaderMarker * best_header
highest scoring header within buffer
Definition: flac_parser.c:87
flac_fifo_space
static size_t flac_fifo_space(const FifoBuffer *f)
Definition: flac_parser.c:143
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2782
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
FLACHeaderMarker::max_score
int max_score
maximum score found after checking each child that has a valid CRC
Definition: flac_parser.c:71
FifoBuffer::rptr
uint8_t * rptr
Definition: flac_parser.c:61
FLACParseContext::end_padded
int end_padded
specifies if fifo_buf's end is padded
Definition: flac_parser.c:95
FifoBuffer
Definition: flac_parser.c:58
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
MAX_FRAME_VERIFY_SIZE
#define MAX_FRAME_VERIFY_SIZE
Definition: flac_parser.c:56
len
int len
Definition: vorbis_enc_data.h:426
flac_fifo_size
static size_t flac_fifo_size(const FifoBuffer *f)
Definition: flac_parser.c:136
check_header_mismatch
static int check_header_mismatch(FLACParseContext *fpc, FLACHeaderMarker *header, FLACHeaderMarker *child, int log_level_offset)
Definition: flac_parser.c:451
FLACFrameInfo::samplerate
int samplerate
sample rate
Definition: flac_parse.h:43
AVCodecParserContext
Definition: avcodec.h:2748
ret
ret
Definition: filter_design.txt:187
FLACParseContext::nb_headers_found
int nb_headers_found
number of headers found in the last flac_parse() call
Definition: flac_parser.c:88
MAX_FRAME_HEADER_SIZE
#define MAX_FRAME_HEADER_SIZE
largest possible size of flac header
Definition: flac_parser.c:55
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
AVCodecContext
main external API structure.
Definition: avcodec.h:451
av_crc
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
PARSER_FLAG_USE_CODEC_TS
#define PARSER_FLAG_USE_CODEC_TS
Definition: avcodec.h:2786
temp
else temp
Definition: vf_mcdeint.c:263
FLACHeaderMarker::link_penalty
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:68
mem.h
FLAC_MIN_HEADERS
#define FLAC_MIN_HEADERS
minimum number of headers buffered and checked before returning frames
Definition: flac_parser.c:43
FLACHeaderMarker::offset
int offset
byte offset from start of FLACParseContext->buffer
Definition: flac_parser.c:67
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
FifoBuffer::empty
int empty
Definition: flac_parser.c:63
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
FLAC_HEADER_NOT_PENALIZED_YET
#define FLAC_HEADER_NOT_PENALIZED_YET
Definition: flac_parser.c:51
AVCodecParser
Definition: avcodec.h:2907
FLACParseContext::last_fi
FLACFrameInfo last_fi
last decoded frame header info
Definition: flac_parser.c:98
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
FLACFrameInfo::bps
int bps
bits-per-sample
Definition: flac_parse.h:45
flac_parse_close
static void flac_parse_close(AVCodecParserContext *c)
Definition: flac_parser.c:890
src
#define src
Definition: vp8dsp.c:248
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:155