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 4
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 #define MAX_FRAME_VERIFY_SIZE (MAX_FRAME_HEADER_SIZE + 1)
59
61 int offset;
/**< byte offset from start of FLACParseContext->buffer */
63 between this header and the one at a distance equal
64 array position */
65 int max_score;
/**< maximum score found after checking each child that
66 has a valid CRC */
69 immediately follows this one in
70 the bytestream */
72 which this frame has the best
73 score with */
75
80 CRC-8 verified header within buffer */
83 flac_parse() call */
86 if set return best_header next time */
88 can be verified */
89 int end_padded;
/**< specifies if fifo_buf's end is padded */
90 uint8_t *
wrap_buf;
/**< general fifo read buffer when wrapped */
95
98 {
100 uint8_t subframe_type;
101
102 // header plus one byte from first subframe
105 return 0;
106 }
107 // subframe zero bit
109 return 0;
110 }
111 // subframe type
112 // 000000 : SUBFRAME_CONSTANT
113 // 000001 : SUBFRAME_VERBATIM
114 // 00001x : reserved
115 // 0001xx : reserved
116 // 001xxx : if(xxx <= 4) SUBFRAME_FIXED, xxx=order ; else reserved
117 // 01xxxx : reserved
118 // 1xxxxx : SUBFRAME_LPC, xxxxx=order-1
120 if (!(subframe_type == 0 ||
121 subframe_type == 1 ||
122 ((subframe_type >= 8) && (subframe_type <= 12)) ||
123 (subframe_type >= 32))) {
124 return 0;
125 }
126
127 return 1;
128 }
129
130 /**
131 * Non-destructive fast fifo pointer fetching
132 * Returns a pointer from the specified offset.
133 * If possible the pointer points within the fifo buffer.
134 * Otherwise (if it would cause a wrap around,) a pointer to a user-specified
135 * buffer is used.
136 * The pointer can be NULL. In any case it will be reallocated to hold the size.
137 * If the returned pointer will be used after subsequent calls to flac_fifo_read_wrap
138 * then the subsequent calls should pass in a different wrap_buf so as to not
139 * overwrite the contents of the previous wrap_buf.
140 * This function is based on av_fifo_generic_read, which is why there is a comment
141 * about a memory barrier for SMP.
142 */
144 uint8_t **wrap_buf, int *allocated_size)
145 {
147 uint8_t *start =
f->rptr +
offset;
148 uint8_t *tmp_buf;
149
151 start -=
f->end -
f->buffer;
152 if (
f->end - start >=
len)
153 return start;
154
156
157 if (!tmp_buf) {
159 "couldn't reallocate wrap buffer of size %d",
len);
161 }
162 *wrap_buf = tmp_buf;
163 do {
164 int seg_len =
FFMIN(
f->end - start,
len);
165 memcpy(tmp_buf, start, seg_len);
166 tmp_buf = (uint8_t*)tmp_buf + seg_len;
167 // memory barrier needed for SMP here in theory
168
169 start += seg_len - (
f->end -
f->buffer);
172
173 return *wrap_buf;
174 }
175
176 /**
177 * Return a pointer in the fifo buffer where the offset starts at until
178 * the wrap point or end of request.
179 * len will contain the valid length of the returned buffer.
180 * A second call to flac_fifo_read (with new offset and len) should be called
181 * to get the post-wrap buf if the returned len is less than the requested.
182 **/
184 {
186 uint8_t *start =
f->rptr +
offset;
187
189 start -=
f->end -
f->buffer;
191 return start;
192 }
193
195 {
197 uint8_t *header_buf;
206
208 while (*end_handle) {
209 end_handle = &(*end_handle)->
next;
211 }
212
213 *end_handle =
av_mallocz(
sizeof(**end_handle));
214 if (!*end_handle) {
216 "couldn't allocate FLACHeaderMarker\n");
218 }
219 (*end_handle)->fi = fi;
220 (*end_handle)->offset =
offset;
221
224
227 }
229 }
230
232 int buf_size, int search_start)
233 {
234 int size = 0, mod_offset = (buf_size - 1) % 4,
i, j;
235 uint32_t x;
236
237 for (
i = 0;
i < mod_offset;
i++) {
238 if ((
AV_RB16(buf +
i) & 0xFFFE) == 0xFFF8) {
241 }
242 }
243
244 for (;
i < buf_size - 1;
i += 4) {
246 if (((x & ~(x + 0x01010101)) & 0x80808080)) {
247 for (j = 0; j < 4; j++) {
248 if ((
AV_RB16(buf +
i + j) & 0xFFFE) == 0xFFF8) {
251 }
252 }
253 }
254 }
256 }
257
259 {
261 int search_end,
size = 0, read_len,
temp;
262 uint8_t *buf;
264
265 /* Search for a new header of at most 16 bytes. */
267 read_len = search_end - search_start + 1;
270 search_start += read_len - 1;
271
272 /* If fifo end was hit do the wrap around. */
273 if (search_start != search_end) {
275
276 wrap[0] = buf[read_len - 1];
277 /* search_start + 1 is the post-wrap offset in the fifo. */
278 read_len = search_end - (search_start + 1) + 1;
279
282
286 }
287 search_start++;
288
289 /* Continue to do the last half of the wrap. */
292 search_start += read_len - 1;
293 }
294
295 /* Return the size even if no new headers were found. */
300 }
301
305 int log_level_offset)
306 {
307 int deduction = 0;
308 if (child_fi->samplerate != header_fi->samplerate) {
311 "sample rate change detected in adjacent frames\n");
312 }
313 if (child_fi->bps != header_fi->bps) {
316 "bits per sample change detected in adjacent frames\n");
317 }
319 /* Changing blocking strategy not allowed per the spec */
322 "blocking strategy change detected in adjacent frames\n");
323 }
324 if (child_fi->channels != header_fi->channels) {
327 "number of channels change detected in adjacent frames\n");
328 }
329 return deduction;
330 }
331
335 int log_level_offset)
336 {
338 int deduction, deduction_expected = 0,
i;
340 log_level_offset);
341 /* Check sample and frame numbers. */
344 (child_fi->frame_or_sample_num
347 int64_t expected_frame_num, expected_sample_num;
348 /* If there are frames in the middle we expect this deduction,
349 as they are probably valid and this one follows it */
350
353 while (curr != child) {
354 /* Ignore frames that failed all crc checks */
357 expected_frame_num++;
359 break;
360 }
361 }
363 }
364
365 if (expected_frame_num == child_fi->frame_or_sample_num ||
366 expected_sample_num == child_fi->frame_or_sample_num)
367 deduction_expected = deduction ? 0 : 1;
368
371 "sample/frame number mismatch in adjacent frames\n");
372 }
373
374 /* If we have suspicious headers, check the CRC between them */
375 if (deduction && !deduction_expected) {
377 int read_len;
378 uint8_t *buf;
379 uint32_t crc = 1;
380 int inverted_test = 0;
381
382 /* Since CRC is expensive only do it if we haven't yet.
383 This assumes a CRC penalty is greater than all other check penalties */
387
391
392 /* Although overlapping chains are scored, the crc should never
393 have to be computed twice for a single byte. */
395 end = child;
398 while (start->
next != child)
400 inverted_test = 1;
402 header->next->link_penalty[
i-1] >=
405 inverted_test = 1;
406 }
407
412
413 if (read_len) {
416 }
417 }
418
419 if (!crc ^ !inverted_test) {
422 "crc check failed from offset %i (frame %"PRId64") to %i (frame %"PRId64")\n",
424 child->
offset, child_fi->frame_or_sample_num);
425 }
426 }
427 return deduction;
428 }
429
430 /**
431 * Score a header.
432 *
433 * Give FLAC_HEADER_BASE_SCORE points to a frame for existing.
434 * If it has children, (subsequent frames of which the preceding CRC footer
435 * validates against this one,) then take the maximum score of the children,
436 * with a penalty of FLAC_HEADER_CHANGED_PENALTY applied for each change to
437 * bps, sample rate, channels, but not decorrelation mode, or blocksize,
438 * because it can change often.
439 **/
441 {
443 int dist = 0;
444 int child_score;
448
449 /* Modify the base score with changes from the last output header */
451 /* Silence the log since this will be repeated if selected */
454 }
455
456 header->max_score = base_score;
457
458 /* Check and compute the children's scores. */
461 /* Look at the child's frame header info and penalize suspicious
462 changes between the headers. */
466 }
468
470 /* Keep the child because the frame scoring is dynamic. */
471 header->best_child = child;
472 header->max_score = base_score + child_score;
473 }
474 child = child->next;
475 }
476
478 }
479
481 {
483 int best_score = 0;//FLAC_HEADER_NOT_SCORED_YET;
484 /* First pass to clear all old scores. */
487
488 /* Do a second pass to score them all. */
489 for (curr = fpc->
headers; curr; curr = curr->
next) {
493 }
494 }
495 }
496
498 int *poutbuf_size)
499 {
502 if (!child) {
504 } else {
506
507 /* If the child has suspicious changes, log them */
509 }
510
515 }
521
522
524 if (
header->fi.is_var_size)
526 else if (
header->best_child)
528 }
529
533
534 /* Return the negative overread index so the client can compute pos.
535 This should be the amount overread to the beginning of the child */
536 if (child)
538 return 0;
539 }
540
542 const uint8_t **poutbuf, int *poutbuf_size,
543 const uint8_t *buf, int buf_size)
544 {
547 int nb_headers;
548 const uint8_t *read_end = buf;
549 const uint8_t *read_start = buf;
550
561 }
562 }
563 *poutbuf = buf;
564 *poutbuf_size = buf_size;
565 return buf_size;
566 }
567
571
572 /* If a best_header was found last call remove it with the buffer data. */
576
577 /* Remove headers in list until the end of the best_header. */
578 for (curr = fpc->
headers; curr != best_child; curr =
temp) {
581 "dropping low score %i frame header from offset %i to %i\n",
583 }
587 }
588 /* Release returned data from ring buffer. */
590
591 /* Fix the offset for the headers remaining to match the new buffer. */
592 for (curr = best_child->
next; curr; curr = curr->
next)
594
600 }
603 /* No end frame no need to delete the buffer; probably eof */
605
610 }
614 }
615
616 /* Find and score new headers. */
617 /* buf_size is zero when flushing, so check for this since we do */
618 /* not want to try to read more input once we have found the end. */
619 /* Also note that buf can't be NULL. */
620 while ((buf_size && read_end < buf + buf_size &&
623 int start_offset;
624
625 /* Pad the end once if EOF, to check the final region for headers. */
626 if (!buf_size) {
629 } else {
630 /* The maximum read size is the upper-bound of what the parser
631 needs to have the required number of frames buffered */
633 read_end = read_end +
FFMIN(buf + buf_size - read_end,
635 }
636
640 /* There is less than one valid flac header buffered for 20 headers
641 * buffered. Therefore the fifo is most likely filled with invalid
642 * data and the input is not a flac file. */
643 goto handle_error;
644 }
645
646 /* Fill the buffer. */
652 goto handle_error;
653 }
654
655 if (buf_size) {
657 read_end - read_start,
NULL);
658 } else {
661 }
662
663 /* Tag headers and update sequences. */
666 start_offset =
FFMAX(0, start_offset);
668
669 if (nb_headers < 0) {
671 "find_new_headers couldn't allocate FLAC header\n");
672 goto handle_error;
673 }
674
676 /* Wait till FLAC_MIN_HEADERS to output a valid frame. */
678 if (read_end < buf + buf_size) {
679 read_start = read_end;
680 continue;
681 } else {
682 goto handle_error;
683 }
684 }
685
686 /* If headers found, update the scores since we have longer chains. */
689
690 /* restore the state pre-padding */
693 /* HACK: drain the tail of the fifo */
696 if (warp) {
699 }
700 read_start = read_end =
NULL;
701 }
702 }
703
704 for (curr = fpc->
headers; curr; curr = curr->
next) {
707 }
708 }
709
711 // Only accept a bad header if there is no other option to continue
714 }
715
719 /* Output a junk frame. */
722
723 /* Set duration to 0. It is unknown or invalid in a junk frame. */
731 }
732 if (!buf_size)
734 }
735
736 handle_error:
738 *poutbuf_size = 0;
739 return buf_size ? read_end - buf : 0;
740 }
741
743 {
746 /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before
747 it drains. This is allocated early to avoid slow reallocation. */
751 "couldn't allocate fifo_buf\n");
753 }
754 return 0;
755 }
756
758 {
761
762 while (curr) {
766 }
770 }
771
778 };