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
41
42 /** maximum number of adjacent headers that compare CRCs against each other */
43 #define FLAC_MAX_SEQUENTIAL_HEADERS 3
44 /** minimum number of headers buffered and checked before returning frames */
45 #define FLAC_MIN_HEADERS 10
46 /** estimate for average size of a FLAC frame */
47 #define FLAC_AVG_FRAME_SIZE 8192
48
49 /** scoring settings for score_header */
50 #define FLAC_HEADER_BASE_SCORE 10
51 #define FLAC_HEADER_CHANGED_PENALTY 7
52 #define FLAC_HEADER_CRC_FAIL_PENALTY 50
53 #define FLAC_HEADER_NOT_PENALIZED_YET 100000
54 #define FLAC_HEADER_NOT_SCORED_YET -100000
55
56 /** largest possible size of flac header */
57 #define MAX_FRAME_HEADER_SIZE 16
58
60 int offset;
/**< byte offset from start of FLACParseContext->buffer */
61 int *
link_penalty;
/**< pointer to array of local scores between this header
62 and the one at a distance equal array position */
63 int max_score;
/**< maximum score found after checking each child that
64 has a valid CRC */
67 immediately follows this one in
68 the bytestream */
70 which this frame has the best
71 score with */
73
78 CRC-8 verified header within buffer */
81 flac_parse() call */
84 if set return best_header next time */
86 can be verified */
87 int end_padded;
/**< specifies if fifo_buf's end is padded */
93
96 {
100 }
101
102 /**
103 * Non-destructive fast fifo pointer fetching
104 * Returns a pointer from the specified offset.
105 * If possible the pointer points within the fifo buffer.
106 * Otherwise (if it would cause a wrap around,) a pointer to a user-specified
107 * buffer is used.
108 * The pointer can be NULL. In any case it will be reallocated to hold the size.
109 * If the returned pointer will be used after subsequent calls to flac_fifo_read_wrap
110 * then the subsequent calls should pass in a different wrap_buf so as to not
111 * overwrite the contents of the previous wrap_buf.
112 * This function is based on av_fifo_generic_read, which is why there is a comment
113 * about a memory barrier for SMP.
114 */
116 uint8_t** wrap_buf,
int* allocated_size)
117 {
121
124 if (f->
end - start >= len)
126
128
129 if (!tmp_buf) {
131 "couldn't reallocate wrap buffer of size %d", len);
132 return NULL;
133 }
134 *wrap_buf = tmp_buf;
135 do {
136 int seg_len =
FFMIN(f->
end - start, len);
137 memcpy(tmp_buf, start, seg_len);
138 tmp_buf = (
uint8_t*)tmp_buf + seg_len;
139 // memory barrier needed for SMP here in theory
140
142 len -= seg_len;
143 } while (len > 0);
144
145 return *wrap_buf;
146 }
147
148 /**
149 * Return a pointer in the fifo buffer where the offset starts at until
150 * the wrap point or end of request.
151 * len will contain the valid length of the returned buffer.
152 * A second call to flac_fifo_read (with new offset and len) should be called
153 * to get the post-wrap buf if the returned len is less than the requested.
154 **/
156 {
159
164 }
165
167 {
177 int i;
178
179 size = 0;
180 while (*end_handle) {
181 end_handle = &(*end_handle)->
next;
182 size++;
183 }
184
186 if (!*end_handle) {
188 "couldn't allocate FLACHeaderMarker\n");
190 }
191 (*end_handle)->fi =
fi;
192 (*end_handle)->offset =
offset;
193 (*end_handle)->link_penalty =
av_malloc(
sizeof(
int) *
197
199 size++;
200 }
202 }
203
205 int search_start)
206
207 {
208 int size = 0, mod_offset = (buf_size - 1) % 4, i, j;
209 uint32_t x;
210
211 for (i = 0; i < mod_offset; i++) {
212 if ((
AV_RB16(buf + i) & 0xFFFE) == 0xFFF8)
214 }
215
216 for (; i < buf_size - 1; i += 4) {
218 if (((x & ~(x + 0x01010101)) & 0x80808080)) {
219 for (j = 0; j < 4; j++) {
220 if ((
AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8)
222 }
223 }
224 }
226 }
227
229 {
231 int search_end,
size = 0, read_len,
temp;
234
235 /* Search for a new header of at most 16 bytes. */
237 read_len = search_end - search_start + 1;
240 search_start += read_len - 1;
241
242 /* If fifo end was hit do the wrap around. */
243 if (search_start != search_end) {
245
246 wrap[0] = buf[read_len - 1];
247 read_len = search_end - search_start + 1;
248
249 /* search_start + 1 is the post-wrap offset in the fifo. */
251 wrap[1] = buf[0];
252
253 if ((
AV_RB16(wrap) & 0xFFFE) == 0xFFF8) {
255 size =
FFMAX(size, temp);
256 }
257 search_start++;
258
259 /* Continue to do the last half of the wrap. */
261 size =
FFMAX(size, temp);
262 search_start += read_len - 1;
263 }
264
265 /* Return the size even if no new headers were found. */
268 size++;
270 }
271
275 int log_level_offset)
276 {
277 int deduction = 0;
278 if (child_fi->samplerate != header_fi->samplerate) {
281 "sample rate change detected in adjacent frames\n");
282 }
283 if (child_fi->bps != header_fi->bps) {
286 "bits per sample change detected in adjacent frames\n");
287 }
289 /* Changing blocking strategy not allowed per the spec */
292 "blocking strategy change detected in adjacent frames\n");
293 }
294 if (child_fi->channels != header_fi->channels) {
297 "number of channels change detected in adjacent frames\n");
298 }
299 return deduction;
300 }
301
305 int log_level_offset)
306 {
308 int deduction, deduction_expected = 0, i;
310 log_level_offset);
311 /* Check sample and frame numbers. */
314 (child_fi->frame_or_sample_num
317 int expected_frame_num, expected_sample_num;
318 /* If there are frames in the middle we expect this deduction,
319 as they are probably valid and this one follows it */
320
322 curr = header;
323 while (curr != child) {
324 /* Ignore frames that failed all crc checks */
327 expected_frame_num++;
329 break;
330 }
331 }
333 }
334
335 if (expected_frame_num == child_fi->frame_or_sample_num ||
336 expected_sample_num == child_fi->frame_or_sample_num)
337 deduction_expected = deduction ? 0 : 1;
338
341 "sample/frame number mismatch in adjacent frames\n");
342 }
343
344 /* If we have suspicious headers, check the CRC between them */
345 if (deduction && !deduction_expected) {
347 int read_len;
349 uint32_t crc = 1;
350 int inverted_test = 0;
351
352 /* Since CRC is expensive only do it if we haven't yet.
353 This assumes a CRC penalty is greater than all other check penalties */
357
361
362 /* Although overlapping chains are scored, the crc should never
363 have to be computed twice for a single byte. */
364 start = header;
366 if (i > 0 &&
368 while (start->
next != child)
370 inverted_test = 1;
371 } else if (i > 0 &&
375 inverted_test = 1;
376 }
377
382
383 if (read_len) {
386 }
387 }
388
389 if (!crc ^ !inverted_test) {
392 "crc check failed from offset %i (frame %"PRId64") to %i (frame %"PRId64")\n",
394 child->
offset, child_fi->frame_or_sample_num);
395 }
396 }
397 return deduction;
398 }
399
400 /**
401 * Score a header.
402 *
403 * Give FLAC_HEADER_BASE_SCORE points to a frame for existing.
404 * If it has children, (subsequent frames of which the preceding CRC footer
405 * validates against this one,) then take the maximum score of the children,
406 * with a penalty of FLAC_HEADER_CHANGED_PENALTY applied for each change to
407 * bps, sample rate, channels, but not decorrelation mode, or blocksize,
408 * because it can change often.
409 **/
411 {
413 int dist = 0;
414 int child_score;
418
419 /* Modify the base score with changes from the last output header */
421 /* Silence the log since this will be repeated if selected */
424 }
425
427
428 /* Check and compute the children's scores. */
429 child = header->
next;
431 /* Look at the child's frame header info and penalize suspicious
432 changes between the headers. */
436 }
438
440 /* Keep the child because the frame scoring is dynamic. */
442 header->
max_score = base_score + child_score;
443 }
444 child = child->next;
445 }
446
448 }
449
451 {
453 int best_score = 0;//FLAC_HEADER_NOT_SCORED_YET;
454 /* First pass to clear all old scores. */
457
458 /* Do a second pass to score them all. */
459 for (curr = fpc->
headers; curr; curr = curr->
next) {
463 }
464 }
465 }
466
468 int *poutbuf_size)
469 {
472 if (!child) {
474 } else {
476
477 /* If the child has suspicious changes, log them */
479 }
480
485 }
491
495
496 /* Return the negative overread index so the client can compute pos.
497 This should be the amount overread to the beginning of the child */
498 if (child)
500 return 0;
501 }
502
504 const uint8_t **poutbuf,
int *poutbuf_size,
506 {
509 int nb_headers;
512
518 *poutbuf_size = buf_size;
519 return buf_size;
520 }
521
525
526 /* If a best_header was found last call remove it with the buffer data. */
530
531 /* Remove headers in list until the end of the best_header. */
532 for (curr = fpc->
headers; curr != best_child; curr = temp) {
535 "dropping low score %i frame header from offset %i to %i\n",
537 }
542 }
543 /* Release returned data from ring buffer. */
545
546 /* Fix the offset for the headers remaining to match the new buffer. */
547 for (curr = best_child->
next; curr; curr = curr->
next)
549
556 }
559 /* No end frame no need to delete the buffer; probably eof */
561
566 }
570 }
571
572 /* Find and score new headers. */
573 /* buf_size is to zero when padding, so check for this since we do */
574 /* not want to try to read more input once we have found the end. */
575 /* Note that as (non-modified) parameters, buf can be non-NULL, */
576 /* while buf_size is 0. */
577 while ((buf && buf_size && read_end < buf + buf_size &&
579 || ((!buf || !buf_size) && !fpc->
end_padded)) {
580 int start_offset;
581
582 /* Pad the end once if EOF, to check the final region for headers. */
583 if (!buf || !buf_size) {
587 } else {
588 /* The maximum read size is the upper-bound of what the parser
589 needs to have the required number of frames buffered */
591 read_end = read_end +
FFMIN(buf + buf_size - read_end,
593 }
594
595 /* Fill the buffer. */
599 "couldn't reallocate buffer of size %td\n",
601 goto handle_error;
602 }
603
604 if (buf && buf_size) {
606 read_end - read_start, NULL);
607 } else {
610 }
611
612 /* Tag headers and update sequences. */
615 start_offset =
FFMAX(0, start_offset);
617
618 if (nb_headers < 0) {
620 "find_new_headers couldn't allocate FLAC header\n");
621 goto handle_error;
622 }
623
625 /* Wait till FLAC_MIN_HEADERS to output a valid frame. */
627 if (buf && read_end < buf + buf_size) {
628 read_start = read_end;
629 continue;
630 } else {
631 goto handle_error;
632 }
633 }
634
635 /* If headers found, update the scores since we have longer chains. */
638
639 /* restore the state pre-padding */
642 /* HACK: drain the tail of the fifo */
645 if (warp) {
648 }
649 buf_size = 0;
650 read_start = read_end = NULL;
651 }
652 }
653
655 for (curr = fpc->
headers; curr; curr = curr->
next) {
659 }
660 }
661
665 /* Output a junk frame. */
668
669 /* Set duration to 0. It is unknown or invalid in a junk frame. */
677 }
678 if (!buf_size)
680 }
681
682 handle_error:
683 *poutbuf = NULL;
684 *poutbuf_size = 0;
685 return read_end -
buf;
686 }
687
689 {
692 /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before
693 it drains. This is allocated early to avoid slow reallocation. */
697 return 0;
698 }
699
701 {
704
705 while (curr) {
710 }
713 }
714
721 };