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
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
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 */
66 immediately follows this one in
67 the bytestream */
69 which this frame has the best
70 score with */
72
77 CRC-8 verified header within buffer */
80 flac_parse() call */
83 if set return best_header next time */
85 can be verified */
86 int end_padded;
/**< specifies if fifo_buf's end is padded */
90
93 {
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 */
113 uint8_t** wrap_buf,
int* allocated_size)
114 {
118
121 if (f->
end - start >= len)
122 return start;
123
125
126 if (!tmp_buf) {
128 "couldn't reallocate wrap buffer of size %d", len);
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
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 **/
153 {
156
160 return start;
161 }
162
164 {
174 int i;
175
176 size = 0;
177 while (*end_handle) {
178 end_handle = &(*end_handle)->
next;
179 size++;
180 }
181
183 if (!*end_handle) {
185 "couldn't allocate FLACHeaderMarker\n");
187 }
188 (*end_handle)->fi = fi;
189 (*end_handle)->offset =
offset;
190 (*end_handle)->link_penalty =
av_malloc(
sizeof(
int) *
194
196 size++;
197 }
199 }
200
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)
211 }
212
213 for (; i < buf_size - 1; i += 4) {
215 if (((x & ~(x + 0x01010101)) & 0x80808080)) {
216 for (j = 0; j < 4; j++) {
217 if ((
AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8)
219 }
220 }
221 }
223 }
224
226 {
228 int search_end,
size = 0, read_len,
temp;
231
232 /* Search for a new header of at most 16 bytes. */
234 read_len = search_end - search_start + 1;
237 search_start += read_len - 1;
238
239 /* If fifo end was hit do the wrap around. */
240 if (search_start != search_end) {
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. */
248 wrap[1] = buf[0];
249
250 if ((
AV_RB16(wrap) & 0xFFFE) == 0xFFF8) {
252 size =
FFMAX(size, temp);
253 }
254 search_start++;
255
256 /* Continue to do the last half of the wrap. */
258 size =
FFMAX(size, temp);
259 search_start += read_len - 1;
260 }
261
262 /* Return the size even if no new headers were found. */
265 size++;
267 }
268
272 int log_level_offset)
273 {
275 int deduction = 0, deduction_expected = 0, i;
276 if (child_fi->samplerate != header_fi->samplerate) {
279 "sample rate change detected in adjacent frames\n");
280 }
281 if (child_fi->bps != header_fi->bps) {
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 */
290 "blocking strategy change detected in adjacent frames\n");
291 }
292 if (child_fi->channels != header_fi->channels) {
295 "number of channels change detected in adjacent frames\n");
296 }
297 /* Check sample and frame numbers. */
300 (child_fi->frame_or_sample_num
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
308 curr = header;
309 while (curr != child) {
310 /* Ignore frames that failed all crc checks */
313 expected_frame_num++;
315 break;
316 }
317 }
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
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) {
333 int read_len;
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 */
343
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 &&
354 while (start->
next != child)
356 inverted_test = 1;
357 } else if (i > 0 &&
361 inverted_test = 1;
362 }
363
368
369 if (read_len) {
372 }
373 }
374
375 if (!crc ^ !inverted_test) {
378 "crc check failed from offset %i (frame %"PRId64") to %i (frame %"PRId64")\n",
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 **/
397 {
399 int dist = 0;
400 int child_score;
401
404
406
407 /* Check and compute the children's scores. */
408 child = header->
next;
410 /* Look at the child's frame header info and penalize suspicious
411 changes between the headers. */
415 }
417
419 /* Keep the child because the frame scoring is dynamic. */
422 }
423 child = child->next;
424 }
425
427 }
428
430 {
433 /* First pass to clear all old scores. */
436
437 /* Do a second pass to score them all. */
438 for (curr = fpc->
headers; curr; curr = curr->
next) {
442 }
443 }
444 }
445
447 int *poutbuf_size)
448 {
451 if (!child) {
453 } else {
455
456 /* If the child has suspicious changes, log them */
458 }
459
464 }
470
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)
476 return 0;
477 }
478
480 const uint8_t **poutbuf,
int *poutbuf_size,
481 const uint8_t *buf,
int buf_size)
482 {
485 int nb_headers;
487 const uint8_t *read_start = buf;
488
493 *poutbuf = buf;
494 *poutbuf_size = buf_size;
495 return buf_size;
496 }
497
501
502 /* If a best_header was found last call remove it with the buffer data. */
506
507 /* Remove headers in list until the end of the best_header. */
508 for (curr = fpc->
headers; curr != best_child; curr = temp) {
511 "dropping low score %i frame header from offset %i to %i\n",
513 }
518 }
519 /* Release returned data from ring buffer. */
521
522 /* Fix the offset for the headers remaining to match the new buffer. */
523 for (curr = best_child->
next; curr; curr = curr->
next)
525
532 }
535 /* No end frame no need to delete the buffer; probably eof */
537
542 }
546 }
547
548 /* Find and score new headers. */
549 /* buf_size is to zero when padding, so check for this since we do */
550 /* not want to try to read more input once we have found the end. */
551 /* Note that as (non-modified) parameters, buf can be non-NULL, */
552 /* while buf_size is 0. */
553 while ((buf && buf_size && read_end < buf + buf_size &&
555 || ((!buf || !buf_size) && !fpc->
end_padded)) {
556 int start_offset;
557
558 /* Pad the end once if EOF, to check the final region for headers. */
559 if (!buf || !buf_size) {
563 } else {
564 /* The maximum read size is the upper-bound of what the parser
565 needs to have the required number of frames buffered */
567 read_end = read_end +
FFMIN(buf + buf_size - read_end,
569 }
570
571 /* Fill the buffer. */
575 "couldn't reallocate buffer of size %td\n",
577 goto handle_error;
578 }
579
580 if (buf && buf_size) {
582 read_end - read_start,
NULL);
583 } else {
586 }
587
588 /* Tag headers and update sequences. */
591 start_offset =
FFMAX(0, start_offset);
593
594 if (nb_headers < 0) {
596 "find_new_headers couldn't allocate FLAC header\n");
597 goto handle_error;
598 }
599
601 /* Wait till FLAC_MIN_HEADERS to output a valid frame. */
603 if (buf && read_end < buf + buf_size) {
604 read_start = read_end;
605 continue;
606 } else {
607 goto handle_error;
608 }
609 }
610
611 /* If headers found, update the scores since we have longer chains. */
614
615 /* restore the state pre-padding */
618 /* HACK: drain the tail of the fifo */
621 if (warp) {
624 }
625 buf_size = 0;
626 read_start = read_end =
NULL;
627 }
628 }
629
634
638 /* Output a junk frame. */
641
642 /* Set duration to 0. It is unknown or invalid in a junk frame. */
650 }
651 if (!buf_size)
653 }
654
655 handle_error:
657 *poutbuf_size = 0;
658 return read_end - buf;
659 }
660
662 {
665 /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before
666 it drains. This is allocated early to avoid slow reallocation. */
668 return 0;
669 }
670
672 {
675
676 while (curr) {
681 }
684 }
685
692 };