Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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 
43 #define FLAC_MAX_SEQUENTIAL_HEADERS 3
44 
45 #define FLAC_MIN_HEADERS 10
46 
47 #define FLAC_AVG_FRAME_SIZE 8192
48 
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 
57 #define MAX_FRAME_HEADER_SIZE 16
58 
59 typedef struct FLACHeaderMarker {
60  int offset;
61  int *link_penalty;
63  int max_score;
73 
74 typedef struct FLACParseContext {
87  int end_padded;
91 
92 static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf,
93  FLACFrameInfo *fi)
94 {
95  GetBitContext gb;
96  init_get_bits(&gb, buf, MAX_FRAME_HEADER_SIZE * 8);
97  return !ff_flac_decode_frame_header(avctx, &gb, fi, 127);
98 }
99 
113 static uint8_t* flac_fifo_read_wrap(FLACParseContext *fpc, int offset, int len,
114  uint8_t** wrap_buf, int* allocated_size)
115 {
116  AVFifoBuffer *f = fpc->fifo_buf;
117  uint8_t *start = f->rptr + offset;
118  uint8_t *tmp_buf;
119 
120  if (start >= f->end)
121  start -= f->end - f->buffer;
122  if (f->end - start >= len)
123  return start;
124 
125  tmp_buf = av_fast_realloc(*wrap_buf, allocated_size, len);
126 
127  if (!tmp_buf) {
128  av_log(fpc->avctx, AV_LOG_ERROR,
129  "couldn't reallocate wrap buffer of size %d", len);
130  return NULL;
131  }
132  *wrap_buf = tmp_buf;
133  do {
134  int seg_len = FFMIN(f->end - start, len);
135  memcpy(tmp_buf, start, seg_len);
136  tmp_buf = (uint8_t*)tmp_buf + seg_len;
137 // memory barrier needed for SMP here in theory
138 
139  start += seg_len - (f->end - f->buffer);
140  len -= seg_len;
141  } while (len > 0);
142 
143  return *wrap_buf;
144 }
145 
153 static uint8_t* flac_fifo_read(FLACParseContext *fpc, int offset, int *len)
154 {
155  AVFifoBuffer *f = fpc->fifo_buf;
156  uint8_t *start = f->rptr + offset;
157 
158  if (start >= f->end)
159  start -= f->end - f->buffer;
160  *len = FFMIN(*len, f->end - start);
161  return start;
162 }
163 
164 static int find_headers_search_validate(FLACParseContext *fpc, int offset)
165 {
166  FLACFrameInfo fi;
167  uint8_t *header_buf;
168  int size = 0;
169  header_buf = flac_fifo_read_wrap(fpc, offset,
171  &fpc->wrap_buf,
173  if (frame_header_is_valid(fpc->avctx, header_buf, &fi)) {
174  FLACHeaderMarker **end_handle = &fpc->headers;
175  int i;
176 
177  size = 0;
178  while (*end_handle) {
179  end_handle = &(*end_handle)->next;
180  size++;
181  }
182 
183  *end_handle = av_mallocz(sizeof(FLACHeaderMarker));
184  if (!*end_handle) {
185  av_log(fpc->avctx, AV_LOG_ERROR,
186  "couldn't allocate FLACHeaderMarker\n");
187  return AVERROR(ENOMEM);
188  }
189  (*end_handle)->fi = fi;
190  (*end_handle)->offset = offset;
191  (*end_handle)->link_penalty = av_malloc(sizeof(int) *
193  for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++)
194  (*end_handle)->link_penalty[i] = FLAC_HEADER_NOT_PENALIZED_YET;
195 
196  fpc->nb_headers_found++;
197  size++;
198  }
199  return size;
200 }
201 
202 static int find_headers_search(FLACParseContext *fpc, uint8_t *buf, int buf_size,
203  int search_start)
204 
205 {
206  int size = 0, mod_offset = (buf_size - 1) % 4, i, j;
207  uint32_t x;
208 
209  for (i = 0; i < mod_offset; i++) {
210  if ((AV_RB16(buf + i) & 0xFFFE) == 0xFFF8)
211  size = find_headers_search_validate(fpc, search_start + i);
212  }
213 
214  for (; i < buf_size - 1; i += 4) {
215  x = AV_RB32(buf + i);
216  if (((x & ~(x + 0x01010101)) & 0x80808080)) {
217  for (j = 0; j < 4; j++) {
218  if ((AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8)
219  size = find_headers_search_validate(fpc, search_start + i + j);
220  }
221  }
222  }
223  return size;
224 }
225 
226 static int find_new_headers(FLACParseContext *fpc, int search_start)
227 {
228  FLACHeaderMarker *end;
229  int search_end, size = 0, read_len, temp;
230  uint8_t *buf;
231  fpc->nb_headers_found = 0;
232 
233  /* Search for a new header of at most 16 bytes. */
234  search_end = av_fifo_size(fpc->fifo_buf) - (MAX_FRAME_HEADER_SIZE - 1);
235  read_len = search_end - search_start + 1;
236  buf = flac_fifo_read(fpc, search_start, &read_len);
237  size = find_headers_search(fpc, buf, read_len, search_start);
238  search_start += read_len - 1;
239 
240  /* If fifo end was hit do the wrap around. */
241  if (search_start != search_end) {
242  uint8_t wrap[2];
243 
244  wrap[0] = buf[read_len - 1];
245  read_len = search_end - search_start + 1;
246 
247  /* search_start + 1 is the post-wrap offset in the fifo. */
248  buf = flac_fifo_read(fpc, search_start + 1, &read_len);
249  wrap[1] = buf[0];
250 
251  if ((AV_RB16(wrap) & 0xFFFE) == 0xFFF8) {
252  temp = find_headers_search_validate(fpc, search_start);
253  size = FFMAX(size, temp);
254  }
255  search_start++;
256 
257  /* Continue to do the last half of the wrap. */
258  temp = find_headers_search(fpc, buf, read_len, search_start);
259  size = FFMAX(size, temp);
260  search_start += read_len - 1;
261  }
262 
263  /* Return the size even if no new headers were found. */
264  if (!size && fpc->headers)
265  for (end = fpc->headers; end; end = end->next)
266  size++;
267  return size;
268 }
269 
271  FLACHeaderMarker *header,
272  FLACHeaderMarker *child,
273  int log_level_offset)
274 {
275  FLACFrameInfo *header_fi = &header->fi, *child_fi = &child->fi;
276  int deduction = 0, deduction_expected = 0, i;
277  if (child_fi->samplerate != header_fi->samplerate) {
278  deduction += FLAC_HEADER_CHANGED_PENALTY;
279  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
280  "sample rate change detected in adjacent frames\n");
281  }
282  if (child_fi->bps != header_fi->bps) {
283  deduction += FLAC_HEADER_CHANGED_PENALTY;
284  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
285  "bits per sample change detected in adjacent frames\n");
286  }
287  if (child_fi->is_var_size != header_fi->is_var_size) {
288  /* Changing blocking strategy not allowed per the spec */
289  deduction += FLAC_HEADER_BASE_SCORE;
290  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
291  "blocking strategy change detected in adjacent frames\n");
292  }
293  if (child_fi->channels != header_fi->channels) {
294  deduction += FLAC_HEADER_CHANGED_PENALTY;
295  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
296  "number of channels change detected in adjacent frames\n");
297  }
298  /* Check sample and frame numbers. */
299  if ((child_fi->frame_or_sample_num - header_fi->frame_or_sample_num
300  != header_fi->blocksize) &&
301  (child_fi->frame_or_sample_num
302  != header_fi->frame_or_sample_num + 1)) {
303  FLACHeaderMarker *curr;
304  int expected_frame_num, expected_sample_num;
305  /* If there are frames in the middle we expect this deduction,
306  as they are probably valid and this one follows it */
307 
308  expected_frame_num = expected_sample_num = header_fi->frame_or_sample_num;
309  curr = header;
310  while (curr != child) {
311  /* Ignore frames that failed all crc checks */
312  for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++) {
314  expected_frame_num++;
315  expected_sample_num += curr->fi.blocksize;
316  break;
317  }
318  }
319  curr = curr->next;
320  }
321 
322  if (expected_frame_num == child_fi->frame_or_sample_num ||
323  expected_sample_num == child_fi->frame_or_sample_num)
324  deduction_expected = deduction ? 0 : 1;
325 
326  deduction += FLAC_HEADER_CHANGED_PENALTY;
327  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
328  "sample/frame number mismatch in adjacent frames\n");
329  }
330 
331  /* If we have suspicious headers, check the CRC between them */
332  if (deduction && !deduction_expected) {
333  FLACHeaderMarker *curr;
334  int read_len;
335  uint8_t *buf;
336  uint32_t crc = 1;
337  int inverted_test = 0;
338 
339  /* Since CRC is expensive only do it if we haven't yet.
340  This assumes a CRC penalty is greater than all other check penalties */
341  curr = header->next;
342  for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS && curr != child; i++)
343  curr = curr->next;
344 
347  FLACHeaderMarker *start, *end;
348 
349  /* Although overlapping chains are scored, the crc should never
350  have to be computed twice for a single byte. */
351  start = header;
352  end = child;
353  if (i > 0 &&
354  header->link_penalty[i - 1] >= FLAC_HEADER_CRC_FAIL_PENALTY) {
355  while (start->next != child)
356  start = start->next;
357  inverted_test = 1;
358  } else if (i > 0 &&
359  header->next->link_penalty[i-1] >=
361  end = header->next;
362  inverted_test = 1;
363  }
364 
365  read_len = end->offset - start->offset;
366  buf = flac_fifo_read(fpc, start->offset, &read_len);
367  crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf, read_len);
368  read_len = (end->offset - start->offset) - read_len;
369 
370  if (read_len) {
371  buf = flac_fifo_read(fpc, end->offset - read_len, &read_len);
372  crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), crc, buf, read_len);
373  }
374  }
375 
376  if (!crc ^ !inverted_test) {
377  deduction += FLAC_HEADER_CRC_FAIL_PENALTY;
378  av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
379  "crc check failed from offset %i (frame %"PRId64") to %i (frame %"PRId64")\n",
380  header->offset, header_fi->frame_or_sample_num,
381  child->offset, child_fi->frame_or_sample_num);
382  }
383  }
384  return deduction;
385 }
386 
398 {
399  FLACHeaderMarker *child;
400  int dist = 0;
401  int child_score;
402 
403  if (header->max_score != FLAC_HEADER_NOT_SCORED_YET)
404  return header->max_score;
405 
407 
408  /* Check and compute the children's scores. */
409  child = header->next;
410  for (dist = 0; dist < FLAC_MAX_SEQUENTIAL_HEADERS && child; dist++) {
411  /* Look at the child's frame header info and penalize suspicious
412  changes between the headers. */
413  if (header->link_penalty[dist] == FLAC_HEADER_NOT_PENALIZED_YET) {
414  header->link_penalty[dist] = check_header_mismatch(fpc, header,
415  child, AV_LOG_DEBUG);
416  }
417  child_score = score_header(fpc, child) - header->link_penalty[dist];
418 
419  if (FLAC_HEADER_BASE_SCORE + child_score > header->max_score) {
420  /* Keep the child because the frame scoring is dynamic. */
421  header->best_child = child;
422  header->max_score = FLAC_HEADER_BASE_SCORE + child_score;
423  }
424  child = child->next;
425  }
426 
427  return header->max_score;
428 }
429 
431 {
432  FLACHeaderMarker *curr;
433  int best_score = FLAC_HEADER_NOT_SCORED_YET;
434  /* First pass to clear all old scores. */
435  for (curr = fpc->headers; curr; curr = curr->next)
437 
438  /* Do a second pass to score them all. */
439  for (curr = fpc->headers; curr; curr = curr->next) {
440  if (score_header(fpc, curr) > best_score) {
441  fpc->best_header = curr;
442  best_score = curr->max_score;
443  }
444  }
445 }
446 
447 static int get_best_header(FLACParseContext* fpc, const uint8_t **poutbuf,
448  int *poutbuf_size)
449 {
450  FLACHeaderMarker *header = fpc->best_header;
451  FLACHeaderMarker *child = header->best_child;
452  if (!child) {
453  *poutbuf_size = av_fifo_size(fpc->fifo_buf) - header->offset;
454  } else {
455  *poutbuf_size = child->offset - header->offset;
456 
457  /* If the child has suspicious changes, log them */
458  check_header_mismatch(fpc, header, child, 0);
459  }
460 
461  if (header->fi.channels != fpc->avctx->channels ||
462  !fpc->avctx->channel_layout) {
463  fpc->avctx->channels = header->fi.channels;
465  }
466  fpc->avctx->sample_rate = header->fi.samplerate;
467  fpc->pc->duration = header->fi.blocksize;
468  *poutbuf = flac_fifo_read_wrap(fpc, header->offset, *poutbuf_size,
469  &fpc->wrap_buf,
471 
472  fpc->best_header_valid = 0;
473  /* Return the negative overread index so the client can compute pos.
474  This should be the amount overread to the beginning of the child */
475  if (child)
476  return child->offset - av_fifo_size(fpc->fifo_buf);
477  return 0;
478 }
479 
481  const uint8_t **poutbuf, int *poutbuf_size,
482  const uint8_t *buf, int buf_size)
483 {
484  FLACParseContext *fpc = s->priv_data;
485  FLACHeaderMarker *curr;
486  int nb_headers;
487  const uint8_t *read_end = buf;
488  const uint8_t *read_start = buf;
489 
491  FLACFrameInfo fi;
492  if (frame_header_is_valid(avctx, buf, &fi))
493  s->duration = fi.blocksize;
494  *poutbuf = buf;
495  *poutbuf_size = buf_size;
496  return buf_size;
497  }
498 
499  fpc->avctx = avctx;
500  if (fpc->best_header_valid)
501  return get_best_header(fpc, poutbuf, poutbuf_size);
502 
503  /* If a best_header was found last call remove it with the buffer data. */
504  if (fpc->best_header && fpc->best_header->best_child) {
505  FLACHeaderMarker *temp;
506  FLACHeaderMarker *best_child = fpc->best_header->best_child;
507 
508  /* Remove headers in list until the end of the best_header. */
509  for (curr = fpc->headers; curr != best_child; curr = temp) {
510  if (curr != fpc->best_header) {
511  av_log(avctx, AV_LOG_DEBUG,
512  "dropping low score %i frame header from offset %i to %i\n",
513  curr->max_score, curr->offset, curr->next->offset);
514  }
515  temp = curr->next;
516  av_freep(&curr->link_penalty);
517  av_free(curr);
518  fpc->nb_headers_buffered--;
519  }
520  /* Release returned data from ring buffer. */
521  av_fifo_drain(fpc->fifo_buf, best_child->offset);
522 
523  /* Fix the offset for the headers remaining to match the new buffer. */
524  for (curr = best_child->next; curr; curr = curr->next)
525  curr->offset -= best_child->offset;
526 
527  fpc->nb_headers_buffered--;
528  best_child->offset = 0;
529  fpc->headers = best_child;
531  fpc->best_header = best_child;
532  return get_best_header(fpc, poutbuf, poutbuf_size);
533  }
534  fpc->best_header = NULL;
535  } else if (fpc->best_header) {
536  /* No end frame no need to delete the buffer; probably eof */
537  FLACHeaderMarker *temp;
538 
539  for (curr = fpc->headers; curr != fpc->best_header; curr = temp) {
540  temp = curr->next;
541  av_freep(&curr->link_penalty);
542  av_free(curr);
543  }
544  fpc->headers = fpc->best_header->next;
546  av_freep(&fpc->best_header);
547  }
548 
549  /* Find and score new headers. */
550  while ((buf && read_end < buf + buf_size &&
552  || (!buf && !fpc->end_padded)) {
553  int start_offset;
554 
555  /* Pad the end once if EOF, to check the final region for headers. */
556  if (!buf) {
557  fpc->end_padded = 1;
558  buf_size = MAX_FRAME_HEADER_SIZE;
559  read_end = read_start + MAX_FRAME_HEADER_SIZE;
560  } else {
561  /* The maximum read size is the upper-bound of what the parser
562  needs to have the required number of frames buffered */
563  int nb_desired = FLAC_MIN_HEADERS - fpc->nb_headers_buffered + 1;
564  read_end = read_end + FFMIN(buf + buf_size - read_end,
565  nb_desired * FLAC_AVG_FRAME_SIZE);
566  }
567 
568  /* Fill the buffer. */
569  if (av_fifo_realloc2(fpc->fifo_buf,
570  (read_end - read_start) + av_fifo_size(fpc->fifo_buf)) < 0) {
571  av_log(avctx, AV_LOG_ERROR,
572  "couldn't reallocate buffer of size %td\n",
573  (read_end - read_start) + av_fifo_size(fpc->fifo_buf));
574  goto handle_error;
575  }
576 
577  if (buf) {
578  av_fifo_generic_write(fpc->fifo_buf, (void*) read_start,
579  read_end - read_start, NULL);
580  } else {
581  int8_t pad[MAX_FRAME_HEADER_SIZE] = { 0 };
582  av_fifo_generic_write(fpc->fifo_buf, (void*) pad, sizeof(pad), NULL);
583  }
584 
585  /* Tag headers and update sequences. */
586  start_offset = av_fifo_size(fpc->fifo_buf) -
587  ((read_end - read_start) + (MAX_FRAME_HEADER_SIZE - 1));
588  start_offset = FFMAX(0, start_offset);
589  nb_headers = find_new_headers(fpc, start_offset);
590 
591  if (nb_headers < 0) {
592  av_log(avctx, AV_LOG_ERROR,
593  "find_new_headers couldn't allocate FLAC header\n");
594  goto handle_error;
595  }
596 
597  fpc->nb_headers_buffered = nb_headers;
598  /* Wait till FLAC_MIN_HEADERS to output a valid frame. */
599  if (!fpc->end_padded && fpc->nb_headers_buffered < FLAC_MIN_HEADERS) {
600  if (buf && read_end < buf + buf_size) {
601  read_start = read_end;
602  continue;
603  } else {
604  goto handle_error;
605  }
606  }
607 
608  /* If headers found, update the scores since we have longer chains. */
609  if (fpc->end_padded || fpc->nb_headers_found)
610  score_sequences(fpc);
611 
612  /* restore the state pre-padding */
613  if (fpc->end_padded) {
614  /* HACK: drain the tail of the fifo */
617  if (fpc->fifo_buf->wptr < 0) {
618  fpc->fifo_buf->wptr += fpc->fifo_buf->end -
619  fpc->fifo_buf->buffer;
620  }
621  buf_size = 0;
622  read_start = read_end = NULL;
623  }
624  }
625 
626  curr = fpc->headers;
627  for (curr = fpc->headers; curr; curr = curr->next)
628  if (!fpc->best_header || curr->max_score > fpc->best_header->max_score)
629  fpc->best_header = curr;
630 
631  if (fpc->best_header) {
632  fpc->best_header_valid = 1;
633  if (fpc->best_header->offset > 0) {
634  /* Output a junk frame. */
635  av_log(avctx, AV_LOG_DEBUG, "Junk frame till offset %i\n",
636  fpc->best_header->offset);
637 
638  /* Set duration to 0. It is unknown or invalid in a junk frame. */
639  s->duration = 0;
640  *poutbuf_size = fpc->best_header->offset;
641  *poutbuf = flac_fifo_read_wrap(fpc, 0, *poutbuf_size,
642  &fpc->wrap_buf,
644  return buf_size ? (read_end - buf) : (fpc->best_header->offset -
645  av_fifo_size(fpc->fifo_buf));
646  }
647  if (!buf_size)
648  return get_best_header(fpc, poutbuf, poutbuf_size);
649  }
650 
651 handle_error:
652  *poutbuf = NULL;
653  *poutbuf_size = 0;
654  return read_end - buf;
655 }
656 
658 {
659  FLACParseContext *fpc = c->priv_data;
660  fpc->pc = c;
661  /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before
662  it drains. This is allocated early to avoid slow reallocation. */
664  return 0;
665 }
666 
668 {
669  FLACParseContext *fpc = c->priv_data;
670  FLACHeaderMarker *curr = fpc->headers, *temp;
671 
672  while (curr) {
673  temp = curr->next;
674  av_freep(&curr->link_penalty);
675  av_free(curr);
676  curr = temp;
677  }
678  av_fifo_free(fpc->fifo_buf);
679  av_free(fpc->wrap_buf);
680 }
681 
684  .priv_data_size = sizeof(FLACParseContext),
685  .parser_init = flac_parse_init,
686  .parser_parse = flac_parse,
687  .parser_close = flac_parse_close,
688 };
static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf, FLACFrameInfo *fi)
Definition: flac_parser.c:92
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
static void score_sequences(FLACParseContext *fpc)
Definition: flac_parser.c:430
int size
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:311
uint8_t * wptr
Definition: fifo.h:33
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
int64_t frame_or_sample_num
frame number or sample number
Definition: flac.h:88
int codec_ids[5]
Definition: avcodec.h:3822
AVFifoBuffer * fifo_buf
buffer to store all data until headers can be verified
Definition: flac_parser.c:85
int duration
Duration of the current frame.
Definition: avcodec.h:3798
#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:84
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
AVCodecContext * avctx
codec context pointer for logging
Definition: flac_parser.c:76
int best_header_valid
flag set when the parser returns junk; if set return best_header next time
Definition: flac_parser.c:83
#define FLAC_AVG_FRAME_SIZE
estimate for average size of a FLAC frame
Definition: flac_parser.c:47
uint8_t
#define av_cold
Definition: attributes.h:66
int nb_headers_found
number of headers found in the last flac_parse() call
Definition: flac_parser.c:80
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:297
#define AV_RB32
Definition: intreadwrite.h:130
int wrap_buf_allocated_size
actual allocated size of the buffer
Definition: flac_parser.c:89
#define FLAC_HEADER_NOT_SCORED_YET
Definition: flac_parser.c:54
static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf, int *poutbuf_size)
Definition: flac_parser.c:447
#define FLAC_HEADER_CHANGED_PENALTY
Definition: flac_parser.c:51
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
Definition: fifo.c:38
static void flac_parse_close(AVCodecParserContext *c)
Definition: flac_parser.c:667
#define FLAC_MAX_SEQUENTIAL_HEADERS
maximum number of adjacent headers that compare CRCs against each other
Definition: flac_parser.c:43
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:480
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
Definition: mem.c:369
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:144
#define wrap(func)
Definition: neontest.h:62
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
#define FFMAX(a, b)
Definition: common.h:55
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:1852
int end_padded
specifies if fifo_buf's end is padded
Definition: flac_parser.c:87
struct FLACHeaderMarker * next
next CRC-8 verified header that immediately follows this one in the bytestream
Definition: flac_parser.c:66
#define FFMIN(a, b)
Definition: common.h:57
FLACHeaderMarker * headers
linked-list that starts at the first CRC-8 verified header within buffer
Definition: flac_parser.c:77
#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
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:113
if(ac->has_optimized_func)
NULL
Definition: eval.c:55
int max_score
maximum score found after checking each child that has a valid CRC
Definition: flac_parser.c:63
static int find_headers_search_validate(FLACParseContext *fpc, int offset)
Definition: flac_parser.c:164
uint8_t * wrap_buf
general fifo read buffer when wrapped
Definition: flac_parser.c:88
int sample_rate
samples per second
Definition: avcodec.h:1791
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
Definition: fifo.c:62
#define FLAC_HEADER_NOT_PENALIZED_YET
Definition: flac_parser.c:53
main external API structure.
Definition: avcodec.h:1050
a very simple circular buffer FIFO implementation
static int find_new_headers(FLACParseContext *fpc, int search_start)
Definition: flac_parser.c:226
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:397
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
FLACFrameInfo fi
decoded frame header info
Definition: flac_parser.c:65
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:61
int nb_headers_buffered
number of headers that are buffered
Definition: flac_parser.c:82
#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:75
FLACHeaderMarker * best_header
highest scoring header within buffer
Definition: flac_parser.c:79
AVCodecParser ff_flac_parser
Definition: flac_parser.c:682
int av_fifo_size(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:52
static int find_headers_search(FLACParseContext *fpc, uint8_t *buf, int buf_size, int search_start)
Definition: flac_parser.c:202
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:3700
int offset
byte offset from start of FLACParseContext->buffer
Definition: flac_parser.c:60
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:25
int len
int channels
number of audio channels
Definition: avcodec.h:1792
static av_cold int flac_parse_init(AVCodecParserContext *c)
Definition: flac_parser.c:657
#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:153
struct FLACHeaderMarker * best_child
following frame header with which this frame has the best score with
Definition: flac_parser.c:69
void av_fifo_drain(AVFifoBuffer *f, int size)
Discard data from the FIFO.
Definition: fifo.c:127
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
static int check_header_mismatch(FLACParseContext *fpc, FLACHeaderMarker *header, FLACHeaderMarker *child, int log_level_offset)
Definition: flac_parser.c:270