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