annotate ffmpeg/libavcodec/flac_parser.c @ 13:844d341cf643 tip

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