Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/flac_parser.c |
Date: | 2022-07-05 19:52:29 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 335 | 410 | 81.7% |
Branches: | 185 | 260 | 71.2% |
Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
35 | #include "libavutil/attributes.h" | ||
36 | #include "libavutil/crc.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 4 | ||
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 | #define MAX_FRAME_VERIFY_SIZE (MAX_FRAME_HEADER_SIZE + 1) | ||
58 | |||
59 | typedef struct FifoBuffer { | ||
60 | uint8_t *buffer; | ||
61 | uint8_t *end; | ||
62 | uint8_t *rptr; | ||
63 | uint8_t *wptr; | ||
64 | int empty; | ||
65 | } FifoBuffer; | ||
66 | |||
67 | typedef struct FLACHeaderMarker { | ||
68 | int offset; /**< byte offset from start of FLACParseContext->buffer */ | ||
69 | int link_penalty[FLAC_MAX_SEQUENTIAL_HEADERS]; /**< array of local scores | ||
70 | between this header and the one at a distance equal | ||
71 | array position */ | ||
72 | int max_score; /**< maximum score found after checking each child that | ||
73 | has a valid CRC */ | ||
74 | FLACFrameInfo fi; /**< decoded frame header info */ | ||
75 | struct FLACHeaderMarker *next; /**< next CRC-8 verified header that | ||
76 | immediately follows this one in | ||
77 | the bytestream */ | ||
78 | struct FLACHeaderMarker *best_child; /**< following frame header with | ||
79 | which this frame has the best | ||
80 | score with */ | ||
81 | } FLACHeaderMarker; | ||
82 | |||
83 | typedef struct FLACParseContext { | ||
84 | AVCodecParserContext *pc; /**< parent context */ | ||
85 | AVCodecContext *avctx; /**< codec context pointer for logging */ | ||
86 | FLACHeaderMarker *headers; /**< linked-list that starts at the first | ||
87 | CRC-8 verified header within buffer */ | ||
88 | FLACHeaderMarker *best_header; /**< highest scoring header within buffer */ | ||
89 | int nb_headers_found; /**< number of headers found in the last | ||
90 | flac_parse() call */ | ||
91 | int nb_headers_buffered; /**< number of headers that are buffered */ | ||
92 | int best_header_valid; /**< flag set when the parser returns junk; | ||
93 | if set return best_header next time */ | ||
94 | FifoBuffer fifo_buf; /**< buffer to store all data until headers | ||
95 | can be verified */ | ||
96 | int end_padded; /**< specifies if fifo_buf's end is padded */ | ||
97 | uint8_t *wrap_buf; /**< general fifo read buffer when wrapped */ | ||
98 | int wrap_buf_allocated_size; /**< actual allocated size of the buffer */ | ||
99 | FLACFrameInfo last_fi; /**< last decoded frame header info */ | ||
100 | int last_fi_valid; /**< set if last_fi is valid */ | ||
101 | } FLACParseContext; | ||
102 | |||
103 | 4724 | static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf, | |
104 | FLACFrameInfo *fi) | ||
105 | { | ||
106 | GetBitContext gb; | ||
107 | uint8_t subframe_type; | ||
108 | |||
109 | // header plus one byte from first subframe | ||
110 | 4724 | init_get_bits(&gb, buf, MAX_FRAME_VERIFY_SIZE * 8); | |
111 |
2/2✓ Branch 1 taken 764 times.
✓ Branch 2 taken 3960 times.
|
4724 | if (ff_flac_decode_frame_header(avctx, &gb, fi, 127)) { |
112 | 764 | return 0; | |
113 | } | ||
114 | // subframe zero bit | ||
115 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3960 times.
|
3960 | if (get_bits1(&gb) != 0) { |
116 | ✗ | return 0; | |
117 | } | ||
118 | // subframe type | ||
119 | // 000000 : SUBFRAME_CONSTANT | ||
120 | // 000001 : SUBFRAME_VERBATIM | ||
121 | // 00001x : reserved | ||
122 | // 0001xx : reserved | ||
123 | // 001xxx : if(xxx <= 4) SUBFRAME_FIXED, xxx=order ; else reserved | ||
124 | // 01xxxx : reserved | ||
125 | // 1xxxxx : SUBFRAME_LPC, xxxxx=order-1 | ||
126 | 3960 | subframe_type = get_bits(&gb, 6); | |
127 |
4/6✓ Branch 0 taken 3837 times.
✓ Branch 1 taken 123 times.
✓ Branch 2 taken 3837 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1887 times.
|
5847 | if (!(subframe_type == 0 || |
128 |
1/2✓ Branch 0 taken 3837 times.
✗ Branch 1 not taken.
|
3837 | subframe_type == 1 || |
129 |
2/2✓ Branch 0 taken 1887 times.
✓ Branch 1 taken 1950 times.
|
3837 | ((subframe_type >= 8) && (subframe_type <= 12)) || |
130 | (subframe_type >= 32))) { | ||
131 | ✗ | return 0; | |
132 | } | ||
133 | |||
134 | 3960 | return 1; | |
135 | } | ||
136 | |||
137 | 123403 | static size_t flac_fifo_size(const FifoBuffer *f) | |
138 | { | ||
139 |
4/4✓ Branch 0 taken 56166 times.
✓ Branch 1 taken 67237 times.
✓ Branch 2 taken 55828 times.
✓ Branch 3 taken 338 times.
|
123403 | if (f->wptr <= f->rptr && !f->empty) |
140 | 55828 | return (f->wptr - f->buffer) + (f->end - f->rptr); | |
141 | 67575 | return f->wptr - f->rptr; | |
142 | } | ||
143 | |||
144 | 59106 | static size_t flac_fifo_space(const FifoBuffer *f) | |
145 | { | ||
146 | 59106 | return f->end - f->buffer - flac_fifo_size(f); | |
147 | } | ||
148 | |||
149 | /** | ||
150 | * Non-destructive fast fifo pointer fetching | ||
151 | * Returns a pointer from the specified offset. | ||
152 | * If possible the pointer points within the fifo buffer. | ||
153 | * Otherwise (if it would cause a wrap around,) a pointer to a user-specified | ||
154 | * buffer is used. | ||
155 | * The pointer can be NULL. In any case it will be reallocated to hold the size. | ||
156 | * If the returned pointer will be used after subsequent calls to flac_fifo_read_wrap | ||
157 | * then the subsequent calls should pass in a different wrap_buf so as to not | ||
158 | * overwrite the contents of the previous wrap_buf. | ||
159 | * This function is based on av_fifo_generic_read, which is why there is a comment | ||
160 | * about a memory barrier for SMP. | ||
161 | */ | ||
162 | 7327 | static uint8_t *flac_fifo_read_wrap(FLACParseContext *fpc, int offset, int len, | |
163 | uint8_t **wrap_buf, int *allocated_size) | ||
164 | { | ||
165 | 7327 | FifoBuffer *f = &fpc->fifo_buf; | |
166 | 7327 | uint8_t *start = f->rptr + offset; | |
167 | uint8_t *tmp_buf; | ||
168 | |||
169 |
2/2✓ Branch 0 taken 1350 times.
✓ Branch 1 taken 5977 times.
|
7327 | if (start >= f->end) |
170 | 1350 | start -= f->end - f->buffer; | |
171 |
2/2✓ Branch 0 taken 7204 times.
✓ Branch 1 taken 123 times.
|
7327 | if (f->end - start >= len) |
172 | 7204 | return start; | |
173 | |||
174 | 123 | tmp_buf = av_fast_realloc(*wrap_buf, allocated_size, len); | |
175 | |||
176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 123 times.
|
123 | if (!tmp_buf) { |
177 | ✗ | av_log(fpc->avctx, AV_LOG_ERROR, | |
178 | "couldn't reallocate wrap buffer of size %d", len); | ||
179 | ✗ | return NULL; | |
180 | } | ||
181 | 123 | *wrap_buf = tmp_buf; | |
182 | do { | ||
183 | 246 | int seg_len = FFMIN(f->end - start, len); | |
184 | 246 | memcpy(tmp_buf, start, seg_len); | |
185 | 246 | tmp_buf = (uint8_t*)tmp_buf + seg_len; | |
186 | // memory barrier needed for SMP here in theory | ||
187 | |||
188 | 246 | start += seg_len - (f->end - f->buffer); | |
189 | 246 | len -= seg_len; | |
190 |
2/2✓ Branch 0 taken 123 times.
✓ Branch 1 taken 123 times.
|
246 | } while (len > 0); |
191 | |||
192 | 123 | return *wrap_buf; | |
193 | } | ||
194 | |||
195 | /** | ||
196 | * Return a pointer in the fifo buffer where the offset starts at until | ||
197 | * the wrap point or end of request. | ||
198 | * len will contain the valid length of the returned buffer. | ||
199 | * A second call to flac_fifo_read (with new offset and len) should be called | ||
200 | * to get the post-wrap buf if the returned len is less than the requested. | ||
201 | **/ | ||
202 | 29691 | static uint8_t *flac_fifo_read(FifoBuffer *f, int offset, int *len) | |
203 | { | ||
204 | 29691 | uint8_t *start = f->rptr + offset; | |
205 | |||
206 |
2/2✓ Branch 0 taken 13444 times.
✓ Branch 1 taken 16247 times.
|
29691 | if (start >= f->end) |
207 | 13444 | start -= f->end - f->buffer; | |
208 | 29691 | *len = FFMIN(*len, f->end - start); | |
209 | 29691 | return start; | |
210 | } | ||
211 | |||
212 | 23 | static int flac_fifo_grow(FifoBuffer *f, size_t inc) | |
213 | { | ||
214 | 23 | size_t size_old = f->end - f->buffer; | |
215 | 23 | size_t offset_r = f->rptr - f->buffer; | |
216 | 23 | size_t offset_w = f->wptr - f->buffer; | |
217 | size_t size_new; | ||
218 | |||
219 | uint8_t *tmp; | ||
220 | |||
221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (size_old > SIZE_MAX - inc) |
222 | ✗ | return AVERROR(EINVAL); | |
223 | 23 | size_new = size_old + inc; | |
224 | |||
225 | 23 | tmp = av_realloc(f->buffer, size_new); | |
226 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (!tmp) |
227 | ✗ | return AVERROR(ENOMEM); | |
228 | |||
229 | // move the data from the beginning of the ring buffer | ||
230 | // to the newly allocated space | ||
231 |
3/4✓ Branch 0 taken 16 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
23 | if (offset_w <= offset_r && !f->empty) { |
232 | 16 | const size_t copy = FFMIN(inc, offset_w); | |
233 | 16 | memcpy(tmp + size_old, tmp, copy); | |
234 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (copy < offset_w) { |
235 | ✗ | memmove(tmp, tmp + copy, offset_w - copy); | |
236 | ✗ | offset_w -= copy; | |
237 | } else | ||
238 | 16 | offset_w = size_old + copy; | |
239 | } | ||
240 | |||
241 | 23 | f->buffer = tmp; | |
242 | 23 | f->end = f->buffer + size_new; | |
243 | 23 | f->rptr = f->buffer + offset_r; | |
244 | 23 | f->wptr = f->buffer + offset_w; | |
245 | |||
246 | 23 | return 0; | |
247 | } | ||
248 | |||
249 | 29553 | static int flac_fifo_write(FifoBuffer *f, const uint8_t *src, size_t size) | |
250 | { | ||
251 | uint8_t *wptr; | ||
252 | |||
253 |
2/2✓ Branch 1 taken 23 times.
✓ Branch 2 taken 29530 times.
|
29553 | if (flac_fifo_space(f) < size) { |
254 |
1/2✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
|
23 | int ret = flac_fifo_grow(f, FFMAX(flac_fifo_size(f), size)); |
255 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (ret < 0) |
256 | ✗ | return ret; | |
257 | } | ||
258 | |||
259 |
1/2✓ Branch 0 taken 29553 times.
✗ Branch 1 not taken.
|
29553 | if (size) |
260 | 29553 | f->empty = 0; | |
261 | |||
262 | 29553 | wptr = f->wptr; | |
263 | do { | ||
264 | 29687 | size_t len = FFMIN(f->end - wptr, size); | |
265 | 29687 | memcpy(wptr, src, len); | |
266 | 29687 | src += len; | |
267 | 29687 | wptr += len; | |
268 |
2/2✓ Branch 0 taken 139 times.
✓ Branch 1 taken 29548 times.
|
29687 | if (wptr >= f->end) |
269 | 139 | wptr = f->buffer; | |
270 | 29687 | size -= len; | |
271 |
2/2✓ Branch 0 taken 134 times.
✓ Branch 1 taken 29553 times.
|
29687 | } while (size > 0); |
272 | |||
273 | 29553 | f->wptr = wptr; | |
274 | |||
275 | 29553 | return 0; | |
276 | } | ||
277 | |||
278 | 2478 | static void flac_fifo_drain(FifoBuffer *f, size_t size) | |
279 | { | ||
280 | 2478 | size_t size_cur = flac_fifo_size(f); | |
281 | |||
282 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2478 times.
|
2478 | av_assert0(size_cur >= size); |
283 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2478 times.
|
2478 | if (size_cur == size) |
284 | ✗ | f->empty = 1; | |
285 | |||
286 | 2478 | f->rptr += size; | |
287 |
2/2✓ Branch 0 taken 123 times.
✓ Branch 1 taken 2355 times.
|
2478 | if (f->rptr >= f->end) |
288 | 123 | f->rptr -= f->end - f->buffer; | |
289 | 2478 | } | |
290 | |||
291 | 204 | static int flac_fifo_alloc(FifoBuffer *f, size_t size) | |
292 | { | ||
293 | 204 | memset(f, 0, sizeof(*f)); | |
294 | |||
295 | 204 | f->buffer = av_realloc(NULL, size); | |
296 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
|
204 | if (!f->buffer) |
297 | ✗ | return AVERROR(ENOMEM); | |
298 | |||
299 | 204 | f->wptr = f->buffer; | |
300 | 204 | f->rptr = f->buffer; | |
301 | 204 | f->end = f->buffer + size; | |
302 | |||
303 | 204 | f->empty = 1; | |
304 | |||
305 | 204 | return 0; | |
306 | } | ||
307 | |||
308 | 204 | static void flac_fifo_free(FifoBuffer *f) | |
309 | { | ||
310 | 204 | av_freep(&f->buffer); | |
311 | 204 | memset(f, 0, sizeof(*f)); | |
312 | 204 | } | |
313 | |||
314 | 4601 | static int find_headers_search_validate(FLACParseContext *fpc, int offset) | |
315 | { | ||
316 | FLACFrameInfo fi; | ||
317 | uint8_t *header_buf; | ||
318 | 4601 | int size = 0; | |
319 | 4601 | header_buf = flac_fifo_read_wrap(fpc, offset, | |
320 | MAX_FRAME_VERIFY_SIZE + AV_INPUT_BUFFER_PADDING_SIZE, | ||
321 | &fpc->wrap_buf, | ||
322 | &fpc->wrap_buf_allocated_size); | ||
323 |
2/2✓ Branch 1 taken 3845 times.
✓ Branch 2 taken 756 times.
|
4601 | if (frame_header_is_valid(fpc->avctx, header_buf, &fi)) { |
324 | 3845 | FLACHeaderMarker **end_handle = &fpc->headers; | |
325 | int i; | ||
326 | |||
327 | 3845 | size = 0; | |
328 |
2/2✓ Branch 0 taken 27434 times.
✓ Branch 1 taken 3845 times.
|
31279 | while (*end_handle) { |
329 | 27434 | end_handle = &(*end_handle)->next; | |
330 | 27434 | size++; | |
331 | } | ||
332 | |||
333 | 3845 | *end_handle = av_mallocz(sizeof(**end_handle)); | |
334 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3845 times.
|
3845 | if (!*end_handle) { |
335 | ✗ | av_log(fpc->avctx, AV_LOG_ERROR, | |
336 | "couldn't allocate FLACHeaderMarker\n"); | ||
337 | ✗ | return AVERROR(ENOMEM); | |
338 | } | ||
339 | 3845 | (*end_handle)->fi = fi; | |
340 | 3845 | (*end_handle)->offset = offset; | |
341 | |||
342 |
2/2✓ Branch 0 taken 15380 times.
✓ Branch 1 taken 3845 times.
|
19225 | for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++) |
343 | 15380 | (*end_handle)->link_penalty[i] = FLAC_HEADER_NOT_PENALIZED_YET; | |
344 | |||
345 | 3845 | fpc->nb_headers_found++; | |
346 | 3845 | size++; | |
347 | } | ||
348 | 4601 | return size; | |
349 | } | ||
350 | |||
351 | 29691 | static int find_headers_search(FLACParseContext *fpc, uint8_t *buf, | |
352 | int buf_size, int search_start) | ||
353 | { | ||
354 | 29691 | int size = 0, mod_offset = (buf_size - 1) % 4, i, j; | |
355 | uint32_t x; | ||
356 | |||
357 |
2/2✓ Branch 0 taken 664 times.
✓ Branch 1 taken 29691 times.
|
30355 | for (i = 0; i < mod_offset; i++) { |
358 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 601 times.
|
664 | if ((AV_RB16(buf + i) & 0xFFFE) == 0xFFF8) { |
359 | 63 | int ret = find_headers_search_validate(fpc, search_start + i); | |
360 | 63 | size = FFMAX(size, ret); | |
361 | } | ||
362 | } | ||
363 | |||
364 |
2/2✓ Branch 0 taken 7545991 times.
✓ Branch 1 taken 29691 times.
|
7575682 | for (; i < buf_size - 1; i += 4) { |
365 | 7545991 | x = AV_RN32(buf + i); | |
366 |
2/2✓ Branch 0 taken 105249 times.
✓ Branch 1 taken 7440742 times.
|
7545991 | if (((x & ~(x + 0x01010101)) & 0x80808080)) { |
367 |
2/2✓ Branch 0 taken 420996 times.
✓ Branch 1 taken 105249 times.
|
526245 | for (j = 0; j < 4; j++) { |
368 |
2/2✓ Branch 0 taken 4538 times.
✓ Branch 1 taken 416458 times.
|
420996 | if ((AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8) { |
369 | 4538 | int ret = find_headers_search_validate(fpc, search_start + i + j); | |
370 | 4538 | size = FFMAX(size, ret); | |
371 | } | ||
372 | } | ||
373 | } | ||
374 | } | ||
375 | 29691 | return size; | |
376 | } | ||
377 | |||
378 | 29553 | static int find_new_headers(FLACParseContext *fpc, int search_start) | |
379 | { | ||
380 | FLACHeaderMarker *end; | ||
381 | 29553 | int search_end, size = 0, read_len, temp; | |
382 | uint8_t *buf; | ||
383 | 29553 | fpc->nb_headers_found = 0; | |
384 | |||
385 | /* Search for a new header of at most 16 bytes. */ | ||
386 | 29553 | search_end = flac_fifo_size(&fpc->fifo_buf) - (MAX_FRAME_HEADER_SIZE - 1); | |
387 | 29553 | read_len = search_end - search_start + 1; | |
388 | 29553 | buf = flac_fifo_read(&fpc->fifo_buf, search_start, &read_len); | |
389 | 29553 | size = find_headers_search(fpc, buf, read_len, search_start); | |
390 | 29553 | search_start += read_len - 1; | |
391 | |||
392 | /* If fifo end was hit do the wrap around. */ | ||
393 |
2/2✓ Branch 0 taken 138 times.
✓ Branch 1 taken 29415 times.
|
29553 | if (search_start != search_end) { |
394 | uint8_t wrap[2]; | ||
395 | |||
396 | 138 | wrap[0] = buf[read_len - 1]; | |
397 | /* search_start + 1 is the post-wrap offset in the fifo. */ | ||
398 | 138 | read_len = search_end - (search_start + 1) + 1; | |
399 | |||
400 | 138 | buf = flac_fifo_read(&fpc->fifo_buf, search_start + 1, &read_len); | |
401 | 138 | wrap[1] = buf[0]; | |
402 | |||
403 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
|
138 | if ((AV_RB16(wrap) & 0xFFFE) == 0xFFF8) { |
404 | ✗ | temp = find_headers_search_validate(fpc, search_start); | |
405 | ✗ | size = FFMAX(size, temp); | |
406 | } | ||
407 | 138 | search_start++; | |
408 | |||
409 | /* Continue to do the last half of the wrap. */ | ||
410 | 138 | temp = find_headers_search(fpc, buf, read_len, search_start); | |
411 | 138 | size = FFMAX(size, temp); | |
412 | 138 | search_start += read_len - 1; | |
413 | } | ||
414 | |||
415 | /* Return the size even if no new headers were found. */ | ||
416 |
4/4✓ Branch 0 taken 26053 times.
✓ Branch 1 taken 3500 times.
✓ Branch 2 taken 26004 times.
✓ Branch 3 taken 49 times.
|
29553 | if (!size && fpc->headers) |
417 |
2/2✓ Branch 0 taken 225504 times.
✓ Branch 1 taken 26004 times.
|
251508 | for (end = fpc->headers; end; end = end->next) |
418 | 225504 | size++; | |
419 | 29553 | return size; | |
420 | } | ||
421 | |||
422 | 38714 | static int check_header_fi_mismatch(FLACParseContext *fpc, | |
423 | FLACFrameInfo *header_fi, | ||
424 | FLACFrameInfo *child_fi, | ||
425 | int log_level_offset) | ||
426 | { | ||
427 | 38714 | int deduction = 0; | |
428 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38714 times.
|
38714 | if (child_fi->samplerate != header_fi->samplerate) { |
429 | ✗ | deduction += FLAC_HEADER_CHANGED_PENALTY; | |
430 | ✗ | av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset, | |
431 | "sample rate change detected in adjacent frames\n"); | ||
432 | } | ||
433 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38714 times.
|
38714 | if (child_fi->bps != header_fi->bps) { |
434 | ✗ | deduction += FLAC_HEADER_CHANGED_PENALTY; | |
435 | ✗ | av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset, | |
436 | "bits per sample change detected in adjacent frames\n"); | ||
437 | } | ||
438 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38714 times.
|
38714 | if (child_fi->is_var_size != header_fi->is_var_size) { |
439 | /* Changing blocking strategy not allowed per the spec */ | ||
440 | ✗ | deduction += FLAC_HEADER_BASE_SCORE; | |
441 | ✗ | av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset, | |
442 | "blocking strategy change detected in adjacent frames\n"); | ||
443 | } | ||
444 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38714 times.
|
38714 | if (child_fi->channels != header_fi->channels) { |
445 | ✗ | deduction += FLAC_HEADER_CHANGED_PENALTY; | |
446 | ✗ | av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset, | |
447 | "number of channels change detected in adjacent frames\n"); | ||
448 | } | ||
449 | 38714 | return deduction; | |
450 | } | ||
451 | |||
452 | 16327 | static int check_header_mismatch(FLACParseContext *fpc, | |
453 | FLACHeaderMarker *header, | ||
454 | FLACHeaderMarker *child, | ||
455 | int log_level_offset) | ||
456 | { | ||
457 | 16327 | FLACFrameInfo *header_fi = &header->fi, *child_fi = &child->fi; | |
458 | 16327 | int deduction, deduction_expected = 0, i; | |
459 | 16327 | deduction = check_header_fi_mismatch(fpc, header_fi, child_fi, | |
460 | log_level_offset); | ||
461 | /* Check sample and frame numbers. */ | ||
462 | 16327 | if ((child_fi->frame_or_sample_num - header_fi->frame_or_sample_num | |
463 |
1/2✓ Branch 0 taken 16327 times.
✗ Branch 1 not taken.
|
16327 | != header_fi->blocksize) && |
464 | 16327 | (child_fi->frame_or_sample_num | |
465 |
2/2✓ Branch 0 taken 10065 times.
✓ Branch 1 taken 6262 times.
|
16327 | != header_fi->frame_or_sample_num + 1)) { |
466 | FLACHeaderMarker *curr; | ||
467 | int64_t expected_frame_num, expected_sample_num; | ||
468 | /* If there are frames in the middle we expect this deduction, | ||
469 | as they are probably valid and this one follows it */ | ||
470 | |||
471 | 10065 | expected_frame_num = expected_sample_num = header_fi->frame_or_sample_num; | |
472 | 10065 | curr = header; | |
473 |
2/2✓ Branch 0 taken 29879 times.
✓ Branch 1 taken 10065 times.
|
39944 | while (curr != child) { |
474 | /* Ignore frames that failed all crc checks */ | ||
475 |
1/2✓ Branch 0 taken 29879 times.
✗ Branch 1 not taken.
|
29879 | for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++) { |
476 |
1/2✓ Branch 0 taken 29879 times.
✗ Branch 1 not taken.
|
29879 | if (curr->link_penalty[i] < FLAC_HEADER_CRC_FAIL_PENALTY) { |
477 | 29879 | expected_frame_num++; | |
478 | 29879 | expected_sample_num += curr->fi.blocksize; | |
479 | 29879 | break; | |
480 | } | ||
481 | } | ||
482 | 29879 | curr = curr->next; | |
483 | } | ||
484 | |||
485 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10065 times.
|
10065 | if (expected_frame_num == child_fi->frame_or_sample_num || |
486 | ✗ | expected_sample_num == child_fi->frame_or_sample_num) | |
487 | 10065 | deduction_expected = deduction ? 0 : 1; | |
488 | |||
489 | 10065 | deduction += FLAC_HEADER_CHANGED_PENALTY; | |
490 | 10065 | av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset, | |
491 | "sample/frame number mismatch in adjacent frames\n"); | ||
492 | } | ||
493 | |||
494 | /* If we have suspicious headers, check the CRC between them */ | ||
495 |
3/4✓ Branch 0 taken 10065 times.
✓ Branch 1 taken 6262 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10065 times.
|
16327 | if (deduction && !deduction_expected) { |
496 | FLACHeaderMarker *curr; | ||
497 | int read_len; | ||
498 | uint8_t *buf; | ||
499 | ✗ | uint32_t crc = 1; | |
500 | ✗ | int inverted_test = 0; | |
501 | |||
502 | /* Since CRC is expensive only do it if we haven't yet. | ||
503 | This assumes a CRC penalty is greater than all other check penalties */ | ||
504 | ✗ | curr = header->next; | |
505 | ✗ | for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS && curr != child; i++) | |
506 | ✗ | curr = curr->next; | |
507 | |||
508 | ✗ | if (header->link_penalty[i] < FLAC_HEADER_CRC_FAIL_PENALTY || | |
509 | ✗ | header->link_penalty[i] == FLAC_HEADER_NOT_PENALIZED_YET) { | |
510 | FLACHeaderMarker *start, *end; | ||
511 | |||
512 | /* Although overlapping chains are scored, the crc should never | ||
513 | have to be computed twice for a single byte. */ | ||
514 | ✗ | start = header; | |
515 | ✗ | end = child; | |
516 | ✗ | if (i > 0 && | |
517 | ✗ | header->link_penalty[i - 1] >= FLAC_HEADER_CRC_FAIL_PENALTY) { | |
518 | ✗ | while (start->next != child) | |
519 | ✗ | start = start->next; | |
520 | ✗ | inverted_test = 1; | |
521 | ✗ | } else if (i > 0 && | |
522 | ✗ | header->next->link_penalty[i-1] >= | |
523 | FLAC_HEADER_CRC_FAIL_PENALTY ) { | ||
524 | ✗ | end = header->next; | |
525 | ✗ | inverted_test = 1; | |
526 | } | ||
527 | |||
528 | ✗ | read_len = end->offset - start->offset; | |
529 | ✗ | buf = flac_fifo_read(&fpc->fifo_buf, start->offset, &read_len); | |
530 | ✗ | crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf, read_len); | |
531 | ✗ | read_len = (end->offset - start->offset) - read_len; | |
532 | |||
533 | ✗ | if (read_len) { | |
534 | ✗ | buf = flac_fifo_read(&fpc->fifo_buf, end->offset - read_len, &read_len); | |
535 | ✗ | crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), crc, buf, read_len); | |
536 | } | ||
537 | } | ||
538 | |||
539 | ✗ | if (!crc ^ !inverted_test) { | |
540 | ✗ | deduction += FLAC_HEADER_CRC_FAIL_PENALTY; | |
541 | ✗ | av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset, | |
542 | "crc check failed from offset %i (frame %"PRId64") to %i (frame %"PRId64")\n", | ||
543 | header->offset, header_fi->frame_or_sample_num, | ||
544 | child->offset, child_fi->frame_or_sample_num); | ||
545 | } | ||
546 | } | ||
547 | 16327 | return deduction; | |
548 | } | ||
549 | |||
550 | /** | ||
551 | * Score a header. | ||
552 | * | ||
553 | * Give FLAC_HEADER_BASE_SCORE points to a frame for existing. | ||
554 | * If it has children, (subsequent frames of which the preceding CRC footer | ||
555 | * validates against this one,) then take the maximum score of the children, | ||
556 | * with a penalty of FLAC_HEADER_CHANGED_PENALTY applied for each change to | ||
557 | * bps, sample rate, channels, but not decorrelation mode, or blocksize, | ||
558 | * because it can change often. | ||
559 | **/ | ||
560 | 95881 | static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header) | |
561 | { | ||
562 | FLACHeaderMarker *child; | ||
563 | 95881 | int dist = 0; | |
564 | int child_score; | ||
565 | 95881 | int base_score = FLAC_HEADER_BASE_SCORE; | |
566 |
2/2✓ Branch 0 taken 71903 times.
✓ Branch 1 taken 23978 times.
|
95881 | if (header->max_score != FLAC_HEADER_NOT_SCORED_YET) |
567 | 71903 | return header->max_score; | |
568 | |||
569 | /* Modify the base score with changes from the last output header */ | ||
570 |
2/2✓ Branch 0 taken 22387 times.
✓ Branch 1 taken 1591 times.
|
23978 | if (fpc->last_fi_valid) { |
571 | /* Silence the log since this will be repeated if selected */ | ||
572 | 22387 | base_score -= check_header_fi_mismatch(fpc, &fpc->last_fi, &header->fi, | |
573 | AV_LOG_DEBUG); | ||
574 | } | ||
575 | |||
576 | 23978 | header->max_score = base_score; | |
577 | |||
578 | /* Check and compute the children's scores. */ | ||
579 | 23978 | child = header->next; | |
580 |
4/4✓ Branch 0 taken 81499 times.
✓ Branch 1 taken 14382 times.
✓ Branch 2 taken 71903 times.
✓ Branch 3 taken 9596 times.
|
95881 | for (dist = 0; dist < FLAC_MAX_SEQUENTIAL_HEADERS && child; dist++) { |
581 | /* Look at the child's frame header info and penalize suspicious | ||
582 | changes between the headers. */ | ||
583 |
2/2✓ Branch 0 taken 13741 times.
✓ Branch 1 taken 58162 times.
|
71903 | if (header->link_penalty[dist] == FLAC_HEADER_NOT_PENALIZED_YET) { |
584 | 13741 | header->link_penalty[dist] = check_header_mismatch(fpc, header, | |
585 | child, AV_LOG_DEBUG); | ||
586 | } | ||
587 | 71903 | child_score = score_header(fpc, child) - header->link_penalty[dist]; | |
588 | |||
589 |
2/2✓ Branch 0 taken 21572 times.
✓ Branch 1 taken 50331 times.
|
71903 | if (FLAC_HEADER_BASE_SCORE + child_score > header->max_score) { |
590 | /* Keep the child because the frame scoring is dynamic. */ | ||
591 | 21572 | header->best_child = child; | |
592 | 21572 | header->max_score = base_score + child_score; | |
593 | } | ||
594 | 71903 | child = child->next; | |
595 | } | ||
596 | |||
597 | 23978 | return header->max_score; | |
598 | } | ||
599 | |||
600 | 2406 | static void score_sequences(FLACParseContext *fpc) | |
601 | { | ||
602 | FLACHeaderMarker *curr; | ||
603 | 2406 | int best_score = 0;//FLAC_HEADER_NOT_SCORED_YET; | |
604 | /* First pass to clear all old scores. */ | ||
605 |
2/2✓ Branch 0 taken 23978 times.
✓ Branch 1 taken 2406 times.
|
26384 | for (curr = fpc->headers; curr; curr = curr->next) |
606 | 23978 | curr->max_score = FLAC_HEADER_NOT_SCORED_YET; | |
607 | |||
608 | /* Do a second pass to score them all. */ | ||
609 |
2/2✓ Branch 0 taken 23978 times.
✓ Branch 1 taken 2406 times.
|
26384 | for (curr = fpc->headers; curr; curr = curr->next) { |
610 |
2/2✓ Branch 1 taken 2406 times.
✓ Branch 2 taken 21572 times.
|
23978 | if (score_header(fpc, curr) > best_score) { |
611 | 2406 | fpc->best_header = curr; | |
612 | 2406 | best_score = curr->max_score; | |
613 | } | ||
614 | } | ||
615 | 2406 | } | |
616 | |||
617 | 2620 | static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf, | |
618 | int *poutbuf_size) | ||
619 | { | ||
620 | 2620 | FLACHeaderMarker *header = fpc->best_header; | |
621 | 2620 | FLACHeaderMarker *child = header->best_child; | |
622 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 2586 times.
|
2620 | if (!child) { |
623 | 34 | *poutbuf_size = flac_fifo_size(&fpc->fifo_buf) - header->offset; | |
624 | } else { | ||
625 | 2586 | *poutbuf_size = child->offset - header->offset; | |
626 | |||
627 | /* If the child has suspicious changes, log them */ | ||
628 | 2586 | check_header_mismatch(fpc, header, child, 0); | |
629 | } | ||
630 | |||
631 | 2620 | ff_flac_set_channel_layout(fpc->avctx, header->fi.channels); | |
632 | |||
633 | 2620 | fpc->avctx->sample_rate = header->fi.samplerate; | |
634 | 2620 | fpc->pc->duration = header->fi.blocksize; | |
635 | 2620 | *poutbuf = flac_fifo_read_wrap(fpc, header->offset, *poutbuf_size, | |
636 | &fpc->wrap_buf, | ||
637 | &fpc->wrap_buf_allocated_size); | ||
638 | |||
639 |
1/2✓ Branch 0 taken 2620 times.
✗ Branch 1 not taken.
|
2620 | if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) { |
640 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2620 times.
|
2620 | if (header->fi.is_var_size) |
641 | ✗ | fpc->pc->pts = header->fi.frame_or_sample_num; | |
642 |
2/2✓ Branch 0 taken 2586 times.
✓ Branch 1 taken 34 times.
|
2620 | else if (header->best_child) |
643 | 2586 | fpc->pc->pts = header->fi.frame_or_sample_num * header->fi.blocksize; | |
644 | } | ||
645 | |||
646 | 2620 | fpc->best_header_valid = 0; | |
647 | 2620 | fpc->last_fi_valid = 1; | |
648 | 2620 | fpc->last_fi = header->fi; | |
649 | |||
650 | /* Return the negative overread index so the client can compute pos. | ||
651 | This should be the amount overread to the beginning of the child */ | ||
652 |
2/2✓ Branch 0 taken 2586 times.
✓ Branch 1 taken 34 times.
|
2620 | if (child) |
653 | 2586 | return child->offset - flac_fifo_size(&fpc->fifo_buf); | |
654 | 34 | return 0; | |
655 | } | ||
656 | |||
657 | 32302 | static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx, | |
658 | const uint8_t **poutbuf, int *poutbuf_size, | ||
659 | const uint8_t *buf, int buf_size) | ||
660 | { | ||
661 | 32302 | FLACParseContext *fpc = s->priv_data; | |
662 | FLACHeaderMarker *curr; | ||
663 | int nb_headers; | ||
664 | 32302 | const uint8_t *read_end = buf; | |
665 | 32302 | const uint8_t *read_start = buf; | |
666 | |||
667 |
2/2✓ Branch 0 taken 123 times.
✓ Branch 1 taken 32179 times.
|
32302 | if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
668 | FLACFrameInfo fi; | ||
669 |
2/2✓ Branch 1 taken 115 times.
✓ Branch 2 taken 8 times.
|
123 | if (frame_header_is_valid(avctx, buf, &fi)) { |
670 | 115 | s->duration = fi.blocksize; | |
671 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
|
115 | if (!avctx->sample_rate) |
672 | ✗ | avctx->sample_rate = fi.samplerate; | |
673 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
|
115 | if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) { |
674 | ✗ | fpc->pc->pts = fi.frame_or_sample_num; | |
675 | ✗ | if (!fi.is_var_size) | |
676 | ✗ | fpc->pc->pts *= fi.blocksize; | |
677 | } | ||
678 | } | ||
679 | 123 | *poutbuf = buf; | |
680 | 123 | *poutbuf_size = buf_size; | |
681 | 123 | return buf_size; | |
682 | } | ||
683 | |||
684 | 32179 | fpc->avctx = avctx; | |
685 |
2/2✓ Branch 0 taken 2363 times.
✓ Branch 1 taken 29816 times.
|
32179 | if (fpc->best_header_valid) |
686 | 2363 | return get_best_header(fpc, poutbuf, poutbuf_size); | |
687 | |||
688 | /* If a best_header was found last call remove it with the buffer data. */ | ||
689 |
4/4✓ Branch 0 taken 2512 times.
✓ Branch 1 taken 27304 times.
✓ Branch 2 taken 2478 times.
✓ Branch 3 taken 34 times.
|
29816 | if (fpc->best_header && fpc->best_header->best_child) { |
690 | FLACHeaderMarker *temp; | ||
691 | 2478 | FLACHeaderMarker *best_child = fpc->best_header->best_child; | |
692 | |||
693 | /* Remove headers in list until the end of the best_header. */ | ||
694 |
2/2✓ Branch 0 taken 2478 times.
✓ Branch 1 taken 2478 times.
|
4956 | for (curr = fpc->headers; curr != best_child; curr = temp) { |
695 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2478 times.
|
2478 | if (curr != fpc->best_header) { |
696 | ✗ | av_log(avctx, AV_LOG_DEBUG, | |
697 | "dropping low score %i frame header from offset %i to %i\n", | ||
698 | ✗ | curr->max_score, curr->offset, curr->next->offset); | |
699 | } | ||
700 | 2478 | temp = curr->next; | |
701 | 2478 | av_free(curr); | |
702 | 2478 | fpc->nb_headers_buffered--; | |
703 | } | ||
704 | /* Release returned data from ring buffer. */ | ||
705 | 2478 | flac_fifo_drain(&fpc->fifo_buf, best_child->offset); | |
706 | |||
707 | /* Fix the offset for the headers remaining to match the new buffer. */ | ||
708 |
2/2✓ Branch 0 taken 18931 times.
✓ Branch 1 taken 2478 times.
|
21409 | for (curr = best_child->next; curr; curr = curr->next) |
709 | 18931 | curr->offset -= best_child->offset; | |
710 | |||
711 | 2478 | best_child->offset = 0; | |
712 | 2478 | fpc->headers = best_child; | |
713 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 2449 times.
|
2478 | if (fpc->nb_headers_buffered >= FLAC_MIN_HEADERS) { |
714 | 29 | fpc->best_header = best_child; | |
715 | 29 | return get_best_header(fpc, poutbuf, poutbuf_size); | |
716 | } | ||
717 | 2449 | fpc->best_header = NULL; | |
718 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 27304 times.
|
27338 | } else if (fpc->best_header) { |
719 | /* No end frame no need to delete the buffer; probably eof */ | ||
720 | FLACHeaderMarker *temp; | ||
721 | |||
722 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
|
34 | for (curr = fpc->headers; curr != fpc->best_header; curr = temp) { |
723 | ✗ | temp = curr->next; | |
724 | ✗ | av_free(curr); | |
725 | ✗ | fpc->nb_headers_buffered--; | |
726 | } | ||
727 | 34 | fpc->headers = fpc->best_header->next; | |
728 | 34 | av_freep(&fpc->best_header); | |
729 | 34 | fpc->nb_headers_buffered--; | |
730 | } | ||
731 | |||
732 | /* Find and score new headers. */ | ||
733 | /* buf_size is zero when flushing, so check for this since we do */ | ||
734 | /* not want to try to read more input once we have found the end. */ | ||
735 | /* Also note that buf can't be NULL. */ | ||
736 |
2/2✓ Branch 0 taken 29516 times.
✓ Branch 1 taken 2369 times.
|
61672 | while ((buf_size && read_end < buf + buf_size && |
737 |
1/2✓ Branch 0 taken 29516 times.
✗ Branch 1 not taken.
|
29516 | fpc->nb_headers_buffered < FLAC_MIN_HEADERS) |
738 |
6/6✓ Branch 0 taken 31885 times.
✓ Branch 1 taken 308 times.
✓ Branch 2 taken 308 times.
✓ Branch 3 taken 2369 times.
✓ Branch 4 taken 37 times.
✓ Branch 5 taken 271 times.
|
34562 | || (!buf_size && !fpc->end_padded)) { |
739 | int start_offset, ret; | ||
740 | |||
741 | /* Pad the end once if EOF, to check the final region for headers. */ | ||
742 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 29516 times.
|
29553 | if (!buf_size) { |
743 | 37 | fpc->end_padded = 1; | |
744 | 37 | read_end = read_start + MAX_FRAME_HEADER_SIZE; | |
745 | } else { | ||
746 | /* The maximum read size is the upper-bound of what the parser | ||
747 | needs to have the required number of frames buffered */ | ||
748 | 29516 | int nb_desired = FLAC_MIN_HEADERS - fpc->nb_headers_buffered + 1; | |
749 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29516 times.
|
29516 | read_end = read_end + FFMIN(buf + buf_size - read_end, |
750 | nb_desired * FLAC_AVG_FRAME_SIZE); | ||
751 | } | ||
752 | |||
753 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 29552 times.
|
29553 | if (!flac_fifo_space(&fpc->fifo_buf) && |
754 | 1 | flac_fifo_size(&fpc->fifo_buf) / FLAC_AVG_FRAME_SIZE > | |
755 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | fpc->nb_headers_buffered * 20) { |
756 | /* There is less than one valid flac header buffered for 20 headers | ||
757 | * buffered. Therefore the fifo is most likely filled with invalid | ||
758 | * data and the input is not a flac file. */ | ||
759 | ✗ | goto handle_error; | |
760 | } | ||
761 | |||
762 | /* Fill the buffer. */ | ||
763 |
2/2✓ Branch 0 taken 29516 times.
✓ Branch 1 taken 37 times.
|
29553 | if (buf_size) { |
764 | 29516 | ret = flac_fifo_write(&fpc->fifo_buf, read_start, | |
765 | 29516 | read_end - read_start); | |
766 | } else { | ||
767 | 37 | int8_t pad[MAX_FRAME_HEADER_SIZE] = { 0 }; | |
768 | 37 | ret = flac_fifo_write(&fpc->fifo_buf, pad, sizeof(pad)); | |
769 | } | ||
770 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29553 times.
|
29553 | if (ret < 0) { |
771 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error buffering data\n"); | |
772 | ✗ | goto handle_error; | |
773 | } | ||
774 | |||
775 | /* Tag headers and update sequences. */ | ||
776 | 29553 | start_offset = flac_fifo_size(&fpc->fifo_buf) - | |
777 | 29553 | ((read_end - read_start) + (MAX_FRAME_HEADER_SIZE - 1)); | |
778 | 29553 | start_offset = FFMAX(0, start_offset); | |
779 | 29553 | nb_headers = find_new_headers(fpc, start_offset); | |
780 | |||
781 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29553 times.
|
29553 | if (nb_headers < 0) { |
782 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
783 | "find_new_headers couldn't allocate FLAC header\n"); | ||
784 | ✗ | goto handle_error; | |
785 | } | ||
786 | |||
787 | 29553 | fpc->nb_headers_buffered = nb_headers; | |
788 | /* Wait till FLAC_MIN_HEADERS to output a valid frame. */ | ||
789 |
4/4✓ Branch 0 taken 29516 times.
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 27147 times.
✓ Branch 3 taken 2369 times.
|
29553 | if (!fpc->end_padded && fpc->nb_headers_buffered < FLAC_MIN_HEADERS) { |
790 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27147 times.
|
27147 | if (read_end < buf + buf_size) { |
791 | ✗ | read_start = read_end; | |
792 | ✗ | continue; | |
793 | } else { | ||
794 | 27147 | goto handle_error; | |
795 | } | ||
796 | } | ||
797 | |||
798 | /* If headers found, update the scores since we have longer chains. */ | ||
799 |
3/4✓ Branch 0 taken 2369 times.
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 2369 times.
✗ Branch 3 not taken.
|
2406 | if (fpc->end_padded || fpc->nb_headers_found) |
800 | 2406 | score_sequences(fpc); | |
801 | |||
802 | /* restore the state pre-padding */ | ||
803 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 2369 times.
|
2406 | if (fpc->end_padded) { |
804 | 37 | int empty = flac_fifo_size(&fpc->fifo_buf) == MAX_FRAME_HEADER_SIZE; | |
805 | 37 | int warp = fpc->fifo_buf.wptr - fpc->fifo_buf.buffer < MAX_FRAME_HEADER_SIZE; | |
806 | /* HACK: drain the tail of the fifo */ | ||
807 | 37 | fpc->fifo_buf.wptr -= MAX_FRAME_HEADER_SIZE; | |
808 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
|
37 | if (warp) { |
809 | ✗ | fpc->fifo_buf.wptr += fpc->fifo_buf.end - | |
810 | ✗ | fpc->fifo_buf.buffer; | |
811 | } | ||
812 | 37 | fpc->fifo_buf.empty = empty; | |
813 | 37 | read_start = read_end = NULL; | |
814 | } | ||
815 | } | ||
816 | |||
817 |
2/2✓ Branch 0 taken 24856 times.
✓ Branch 1 taken 2640 times.
|
27496 | for (curr = fpc->headers; curr; curr = curr->next) { |
818 |
3/4✓ Branch 0 taken 24656 times.
✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24656 times.
|
24856 | if (!fpc->best_header || curr->max_score > fpc->best_header->max_score) { |
819 | 200 | fpc->best_header = curr; | |
820 | } | ||
821 | } | ||
822 | |||
823 |
3/4✓ Branch 0 taken 2606 times.
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2606 times.
|
2640 | if (fpc->best_header && fpc->best_header->max_score <= 0) { |
824 | // Only accept a bad header if there is no other option to continue | ||
825 | ✗ | if (!buf_size || read_end != buf || fpc->nb_headers_buffered < FLAC_MIN_HEADERS) | |
826 | ✗ | fpc->best_header = NULL; | |
827 | } | ||
828 | |||
829 |
2/2✓ Branch 0 taken 2606 times.
✓ Branch 1 taken 34 times.
|
2640 | if (fpc->best_header) { |
830 | 2606 | fpc->best_header_valid = 1; | |
831 |
2/2✓ Branch 0 taken 106 times.
✓ Branch 1 taken 2500 times.
|
2606 | if (fpc->best_header->offset > 0) { |
832 | /* Output a junk frame. */ | ||
833 | 106 | av_log(avctx, AV_LOG_DEBUG, "Junk frame till offset %i\n", | |
834 | 106 | fpc->best_header->offset); | |
835 | |||
836 | /* Set duration to 0. It is unknown or invalid in a junk frame. */ | ||
837 | 106 | s->duration = 0; | |
838 | 106 | *poutbuf_size = fpc->best_header->offset; | |
839 | 106 | *poutbuf = flac_fifo_read_wrap(fpc, 0, *poutbuf_size, | |
840 | &fpc->wrap_buf, | ||
841 | &fpc->wrap_buf_allocated_size); | ||
842 |
2/2✓ Branch 0 taken 97 times.
✓ Branch 1 taken 9 times.
|
115 | return buf_size ? (read_end - buf) : (fpc->best_header->offset - |
843 | 9 | flac_fifo_size(&fpc->fifo_buf)); | |
844 | } | ||
845 |
2/2✓ Branch 0 taken 2272 times.
✓ Branch 1 taken 228 times.
|
2500 | if (!buf_size) |
846 | 228 | return get_best_header(fpc, poutbuf, poutbuf_size); | |
847 | } | ||
848 | |||
849 | 2306 | handle_error: | |
850 | 29453 | *poutbuf = NULL; | |
851 | 29453 | *poutbuf_size = 0; | |
852 |
2/2✓ Branch 0 taken 29419 times.
✓ Branch 1 taken 34 times.
|
29453 | return buf_size ? read_end - buf : 0; |
853 | } | ||
854 | |||
855 | 204 | static av_cold int flac_parse_init(AVCodecParserContext *c) | |
856 | { | ||
857 | 204 | FLACParseContext *fpc = c->priv_data; | |
858 | int ret; | ||
859 | |||
860 | 204 | fpc->pc = c; | |
861 | /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before | ||
862 | it drains. This is allocated early to avoid slow reallocation. */ | ||
863 | 204 | ret = flac_fifo_alloc(&fpc->fifo_buf, (FLAC_MIN_HEADERS + 3) * FLAC_AVG_FRAME_SIZE); | |
864 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
|
204 | if (ret < 0) { |
865 | ✗ | av_log(fpc->avctx, AV_LOG_ERROR, | |
866 | "couldn't allocate fifo_buf\n"); | ||
867 | ✗ | return AVERROR(ENOMEM); | |
868 | } | ||
869 | 204 | return 0; | |
870 | } | ||
871 | |||
872 | 204 | static void flac_parse_close(AVCodecParserContext *c) | |
873 | { | ||
874 | 204 | FLACParseContext *fpc = c->priv_data; | |
875 | 204 | FLACHeaderMarker *curr = fpc->headers, *temp; | |
876 | |||
877 |
2/2✓ Branch 0 taken 1333 times.
✓ Branch 1 taken 204 times.
|
1537 | while (curr) { |
878 | 1333 | temp = curr->next; | |
879 | 1333 | av_free(curr); | |
880 | 1333 | curr = temp; | |
881 | } | ||
882 | 204 | fpc->headers = NULL; | |
883 | 204 | flac_fifo_free(&fpc->fifo_buf); | |
884 | 204 | av_freep(&fpc->wrap_buf); | |
885 | 204 | } | |
886 | |||
887 | const AVCodecParser ff_flac_parser = { | ||
888 | .codec_ids = { AV_CODEC_ID_FLAC }, | ||
889 | .priv_data_size = sizeof(FLACParseContext), | ||
890 | .parser_init = flac_parse_init, | ||
891 | .parser_parse = flac_parse, | ||
892 | .parser_close = flac_parse_close, | ||
893 | }; | ||
894 |