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