yading@10: /* yading@10: * FLAC parser yading@10: * Copyright (c) 2010 Michael Chinen yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * FLAC parser yading@10: * yading@10: * The FLAC parser buffers input until FLAC_MIN_HEADERS has been found. yading@10: * Each time it finds and verifies a CRC-8 header it sees which of the yading@10: * FLAC_MAX_SEQUENTIAL_HEADERS that came before it have a valid CRC-16 footer yading@10: * that ends at the newly found header. yading@10: * Headers are scored by FLAC_HEADER_BASE_SCORE plus the max of it's crc-verified yading@10: * children, penalized by changes in sample rate, frame number, etc. yading@10: * The parser returns the frame with the highest score. yading@10: **/ yading@10: yading@10: #include "libavutil/crc.h" yading@10: #include "libavutil/fifo.h" yading@10: #include "bytestream.h" yading@10: #include "parser.h" yading@10: #include "flac.h" yading@10: yading@10: /** maximum number of adjacent headers that compare CRCs against each other */ yading@10: #define FLAC_MAX_SEQUENTIAL_HEADERS 3 yading@10: /** minimum number of headers buffered and checked before returning frames */ yading@10: #define FLAC_MIN_HEADERS 10 yading@10: /** estimate for average size of a FLAC frame */ yading@10: #define FLAC_AVG_FRAME_SIZE 8192 yading@10: yading@10: /** scoring settings for score_header */ yading@10: #define FLAC_HEADER_BASE_SCORE 10 yading@10: #define FLAC_HEADER_CHANGED_PENALTY 7 yading@10: #define FLAC_HEADER_CRC_FAIL_PENALTY 50 yading@10: #define FLAC_HEADER_NOT_PENALIZED_YET 100000 yading@10: #define FLAC_HEADER_NOT_SCORED_YET -100000 yading@10: yading@10: /** largest possible size of flac header */ yading@10: #define MAX_FRAME_HEADER_SIZE 16 yading@10: yading@10: typedef struct FLACHeaderMarker { yading@10: int offset; /**< byte offset from start of FLACParseContext->buffer */ yading@10: int *link_penalty; /**< pointer to array of local scores between this header yading@10: and the one at a distance equal array position */ yading@10: int max_score; /**< maximum score found after checking each child that yading@10: has a valid CRC */ yading@10: FLACFrameInfo fi; /**< decoded frame header info */ yading@10: struct FLACHeaderMarker *next; /**< next CRC-8 verified header that yading@10: immediately follows this one in yading@10: the bytestream */ yading@10: struct FLACHeaderMarker *best_child; /**< following frame header with yading@10: which this frame has the best yading@10: score with */ yading@10: } FLACHeaderMarker; yading@10: yading@10: typedef struct FLACParseContext { yading@10: AVCodecParserContext *pc; /**< parent context */ yading@10: AVCodecContext *avctx; /**< codec context pointer for logging */ yading@10: FLACHeaderMarker *headers; /**< linked-list that starts at the first yading@10: CRC-8 verified header within buffer */ yading@10: FLACHeaderMarker *best_header; /**< highest scoring header within buffer */ yading@10: int nb_headers_found; /**< number of headers found in the last yading@10: flac_parse() call */ yading@10: int nb_headers_buffered; /**< number of headers that are buffered */ yading@10: int best_header_valid; /**< flag set when the parser returns junk; yading@10: if set return best_header next time */ yading@10: AVFifoBuffer *fifo_buf; /**< buffer to store all data until headers yading@10: can be verified */ yading@10: int end_padded; /**< specifies if fifo_buf's end is padded */ yading@10: uint8_t *wrap_buf; /**< general fifo read buffer when wrapped */ yading@10: int wrap_buf_allocated_size; /**< actual allocated size of the buffer */ yading@10: } FLACParseContext; yading@10: yading@10: static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf, yading@10: FLACFrameInfo *fi) yading@10: { yading@10: GetBitContext gb; yading@10: init_get_bits(&gb, buf, MAX_FRAME_HEADER_SIZE * 8); yading@10: return !ff_flac_decode_frame_header(avctx, &gb, fi, 127); yading@10: } yading@10: yading@10: /** yading@10: * Non-destructive fast fifo pointer fetching yading@10: * Returns a pointer from the specified offset. yading@10: * If possible the pointer points within the fifo buffer. yading@10: * Otherwise (if it would cause a wrap around,) a pointer to a user-specified yading@10: * buffer is used. yading@10: * The pointer can be NULL. In any case it will be reallocated to hold the size. yading@10: * If the returned pointer will be used after subsequent calls to flac_fifo_read_wrap yading@10: * then the subsequent calls should pass in a different wrap_buf so as to not yading@10: * overwrite the contents of the previous wrap_buf. yading@10: * This function is based on av_fifo_generic_read, which is why there is a comment yading@10: * about a memory barrier for SMP. yading@10: */ yading@10: static uint8_t* flac_fifo_read_wrap(FLACParseContext *fpc, int offset, int len, yading@10: uint8_t** wrap_buf, int* allocated_size) yading@10: { yading@10: AVFifoBuffer *f = fpc->fifo_buf; yading@10: uint8_t *start = f->rptr + offset; yading@10: uint8_t *tmp_buf; yading@10: yading@10: if (start >= f->end) yading@10: start -= f->end - f->buffer; yading@10: if (f->end - start >= len) yading@10: return start; yading@10: yading@10: tmp_buf = av_fast_realloc(*wrap_buf, allocated_size, len); yading@10: yading@10: if (!tmp_buf) { yading@10: av_log(fpc->avctx, AV_LOG_ERROR, yading@10: "couldn't reallocate wrap buffer of size %d", len); yading@10: return NULL; yading@10: } yading@10: *wrap_buf = tmp_buf; yading@10: do { yading@10: int seg_len = FFMIN(f->end - start, len); yading@10: memcpy(tmp_buf, start, seg_len); yading@10: tmp_buf = (uint8_t*)tmp_buf + seg_len; yading@10: // memory barrier needed for SMP here in theory yading@10: yading@10: start += seg_len - (f->end - f->buffer); yading@10: len -= seg_len; yading@10: } while (len > 0); yading@10: yading@10: return *wrap_buf; yading@10: } yading@10: yading@10: /** yading@10: * Return a pointer in the fifo buffer where the offset starts at until yading@10: * the wrap point or end of request. yading@10: * len will contain the valid length of the returned buffer. yading@10: * A second call to flac_fifo_read (with new offset and len) should be called yading@10: * to get the post-wrap buf if the returned len is less than the requested. yading@10: **/ yading@10: static uint8_t* flac_fifo_read(FLACParseContext *fpc, int offset, int *len) yading@10: { yading@10: AVFifoBuffer *f = fpc->fifo_buf; yading@10: uint8_t *start = f->rptr + offset; yading@10: yading@10: if (start >= f->end) yading@10: start -= f->end - f->buffer; yading@10: *len = FFMIN(*len, f->end - start); yading@10: return start; yading@10: } yading@10: yading@10: static int find_headers_search_validate(FLACParseContext *fpc, int offset) yading@10: { yading@10: FLACFrameInfo fi; yading@10: uint8_t *header_buf; yading@10: int size = 0; yading@10: header_buf = flac_fifo_read_wrap(fpc, offset, yading@10: MAX_FRAME_HEADER_SIZE, yading@10: &fpc->wrap_buf, yading@10: &fpc->wrap_buf_allocated_size); yading@10: if (frame_header_is_valid(fpc->avctx, header_buf, &fi)) { yading@10: FLACHeaderMarker **end_handle = &fpc->headers; yading@10: int i; yading@10: yading@10: size = 0; yading@10: while (*end_handle) { yading@10: end_handle = &(*end_handle)->next; yading@10: size++; yading@10: } yading@10: yading@10: *end_handle = av_mallocz(sizeof(FLACHeaderMarker)); yading@10: if (!*end_handle) { yading@10: av_log(fpc->avctx, AV_LOG_ERROR, yading@10: "couldn't allocate FLACHeaderMarker\n"); yading@10: return AVERROR(ENOMEM); yading@10: } yading@10: (*end_handle)->fi = fi; yading@10: (*end_handle)->offset = offset; yading@10: (*end_handle)->link_penalty = av_malloc(sizeof(int) * yading@10: FLAC_MAX_SEQUENTIAL_HEADERS); yading@10: for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++) yading@10: (*end_handle)->link_penalty[i] = FLAC_HEADER_NOT_PENALIZED_YET; yading@10: yading@10: fpc->nb_headers_found++; yading@10: size++; yading@10: } yading@10: return size; yading@10: } yading@10: yading@10: static int find_headers_search(FLACParseContext *fpc, uint8_t *buf, int buf_size, yading@10: int search_start) yading@10: yading@10: { yading@10: int size = 0, mod_offset = (buf_size - 1) % 4, i, j; yading@10: uint32_t x; yading@10: yading@10: for (i = 0; i < mod_offset; i++) { yading@10: if ((AV_RB16(buf + i) & 0xFFFE) == 0xFFF8) yading@10: size = find_headers_search_validate(fpc, search_start + i); yading@10: } yading@10: yading@10: for (; i < buf_size - 1; i += 4) { yading@10: x = AV_RB32(buf + i); yading@10: if (((x & ~(x + 0x01010101)) & 0x80808080)) { yading@10: for (j = 0; j < 4; j++) { yading@10: if ((AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8) yading@10: size = find_headers_search_validate(fpc, search_start + i + j); yading@10: } yading@10: } yading@10: } yading@10: return size; yading@10: } yading@10: yading@10: static int find_new_headers(FLACParseContext *fpc, int search_start) yading@10: { yading@10: FLACHeaderMarker *end; yading@10: int search_end, size = 0, read_len, temp; yading@10: uint8_t *buf; yading@10: fpc->nb_headers_found = 0; yading@10: yading@10: /* Search for a new header of at most 16 bytes. */ yading@10: search_end = av_fifo_size(fpc->fifo_buf) - (MAX_FRAME_HEADER_SIZE - 1); yading@10: read_len = search_end - search_start + 1; yading@10: buf = flac_fifo_read(fpc, search_start, &read_len); yading@10: size = find_headers_search(fpc, buf, read_len, search_start); yading@10: search_start += read_len - 1; yading@10: yading@10: /* If fifo end was hit do the wrap around. */ yading@10: if (search_start != search_end) { yading@10: uint8_t wrap[2]; yading@10: yading@10: wrap[0] = buf[read_len - 1]; yading@10: read_len = search_end - search_start + 1; yading@10: yading@10: /* search_start + 1 is the post-wrap offset in the fifo. */ yading@10: buf = flac_fifo_read(fpc, search_start + 1, &read_len); yading@10: wrap[1] = buf[0]; yading@10: yading@10: if ((AV_RB16(wrap) & 0xFFFE) == 0xFFF8) { yading@10: temp = find_headers_search_validate(fpc, search_start); yading@10: size = FFMAX(size, temp); yading@10: } yading@10: search_start++; yading@10: yading@10: /* Continue to do the last half of the wrap. */ yading@10: temp = find_headers_search(fpc, buf, read_len, search_start); yading@10: size = FFMAX(size, temp); yading@10: search_start += read_len - 1; yading@10: } yading@10: yading@10: /* Return the size even if no new headers were found. */ yading@10: if (!size && fpc->headers) yading@10: for (end = fpc->headers; end; end = end->next) yading@10: size++; yading@10: return size; yading@10: } yading@10: yading@10: static int check_header_mismatch(FLACParseContext *fpc, yading@10: FLACHeaderMarker *header, yading@10: FLACHeaderMarker *child, yading@10: int log_level_offset) yading@10: { yading@10: FLACFrameInfo *header_fi = &header->fi, *child_fi = &child->fi; yading@10: int deduction = 0, deduction_expected = 0, i; yading@10: if (child_fi->samplerate != header_fi->samplerate) { yading@10: deduction += FLAC_HEADER_CHANGED_PENALTY; yading@10: av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset, yading@10: "sample rate change detected in adjacent frames\n"); yading@10: } yading@10: if (child_fi->bps != header_fi->bps) { yading@10: deduction += FLAC_HEADER_CHANGED_PENALTY; yading@10: av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset, yading@10: "bits per sample change detected in adjacent frames\n"); yading@10: } yading@10: if (child_fi->is_var_size != header_fi->is_var_size) { yading@10: /* Changing blocking strategy not allowed per the spec */ yading@10: deduction += FLAC_HEADER_BASE_SCORE; yading@10: av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset, yading@10: "blocking strategy change detected in adjacent frames\n"); yading@10: } yading@10: if (child_fi->channels != header_fi->channels) { yading@10: deduction += FLAC_HEADER_CHANGED_PENALTY; yading@10: av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset, yading@10: "number of channels change detected in adjacent frames\n"); yading@10: } yading@10: /* Check sample and frame numbers. */ yading@10: if ((child_fi->frame_or_sample_num - header_fi->frame_or_sample_num yading@10: != header_fi->blocksize) && yading@10: (child_fi->frame_or_sample_num yading@10: != header_fi->frame_or_sample_num + 1)) { yading@10: FLACHeaderMarker *curr; yading@10: int expected_frame_num, expected_sample_num; yading@10: /* If there are frames in the middle we expect this deduction, yading@10: as they are probably valid and this one follows it */ yading@10: yading@10: expected_frame_num = expected_sample_num = header_fi->frame_or_sample_num; yading@10: curr = header; yading@10: while (curr != child) { yading@10: /* Ignore frames that failed all crc checks */ yading@10: for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++) { yading@10: if (curr->link_penalty[i] < FLAC_HEADER_CRC_FAIL_PENALTY) { yading@10: expected_frame_num++; yading@10: expected_sample_num += curr->fi.blocksize; yading@10: break; yading@10: } yading@10: } yading@10: curr = curr->next; yading@10: } yading@10: yading@10: if (expected_frame_num == child_fi->frame_or_sample_num || yading@10: expected_sample_num == child_fi->frame_or_sample_num) yading@10: deduction_expected = deduction ? 0 : 1; yading@10: yading@10: deduction += FLAC_HEADER_CHANGED_PENALTY; yading@10: av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset, yading@10: "sample/frame number mismatch in adjacent frames\n"); yading@10: } yading@10: yading@10: /* If we have suspicious headers, check the CRC between them */ yading@10: if (deduction && !deduction_expected) { yading@10: FLACHeaderMarker *curr; yading@10: int read_len; yading@10: uint8_t *buf; yading@10: uint32_t crc = 1; yading@10: int inverted_test = 0; yading@10: yading@10: /* Since CRC is expensive only do it if we haven't yet. yading@10: This assumes a CRC penalty is greater than all other check penalties */ yading@10: curr = header->next; yading@10: for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS && curr != child; i++) yading@10: curr = curr->next; yading@10: yading@10: if (header->link_penalty[i] < FLAC_HEADER_CRC_FAIL_PENALTY || yading@10: header->link_penalty[i] == FLAC_HEADER_NOT_PENALIZED_YET) { yading@10: FLACHeaderMarker *start, *end; yading@10: yading@10: /* Although overlapping chains are scored, the crc should never yading@10: have to be computed twice for a single byte. */ yading@10: start = header; yading@10: end = child; yading@10: if (i > 0 && yading@10: header->link_penalty[i - 1] >= FLAC_HEADER_CRC_FAIL_PENALTY) { yading@10: while (start->next != child) yading@10: start = start->next; yading@10: inverted_test = 1; yading@10: } else if (i > 0 && yading@10: header->next->link_penalty[i-1] >= yading@10: FLAC_HEADER_CRC_FAIL_PENALTY ) { yading@10: end = header->next; yading@10: inverted_test = 1; yading@10: } yading@10: yading@10: read_len = end->offset - start->offset; yading@10: buf = flac_fifo_read(fpc, start->offset, &read_len); yading@10: crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf, read_len); yading@10: read_len = (end->offset - start->offset) - read_len; yading@10: yading@10: if (read_len) { yading@10: buf = flac_fifo_read(fpc, end->offset - read_len, &read_len); yading@10: crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), crc, buf, read_len); yading@10: } yading@10: } yading@10: yading@10: if (!crc ^ !inverted_test) { yading@10: deduction += FLAC_HEADER_CRC_FAIL_PENALTY; yading@10: av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset, yading@10: "crc check failed from offset %i (frame %"PRId64") to %i (frame %"PRId64")\n", yading@10: header->offset, header_fi->frame_or_sample_num, yading@10: child->offset, child_fi->frame_or_sample_num); yading@10: } yading@10: } yading@10: return deduction; yading@10: } yading@10: yading@10: /** yading@10: * Score a header. yading@10: * yading@10: * Give FLAC_HEADER_BASE_SCORE points to a frame for existing. yading@10: * If it has children, (subsequent frames of which the preceding CRC footer yading@10: * validates against this one,) then take the maximum score of the children, yading@10: * with a penalty of FLAC_HEADER_CHANGED_PENALTY applied for each change to yading@10: * bps, sample rate, channels, but not decorrelation mode, or blocksize, yading@10: * because it can change often. yading@10: **/ yading@10: static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header) yading@10: { yading@10: FLACHeaderMarker *child; yading@10: int dist = 0; yading@10: int child_score; yading@10: yading@10: if (header->max_score != FLAC_HEADER_NOT_SCORED_YET) yading@10: return header->max_score; yading@10: yading@10: header->max_score = FLAC_HEADER_BASE_SCORE; yading@10: yading@10: /* Check and compute the children's scores. */ yading@10: child = header->next; yading@10: for (dist = 0; dist < FLAC_MAX_SEQUENTIAL_HEADERS && child; dist++) { yading@10: /* Look at the child's frame header info and penalize suspicious yading@10: changes between the headers. */ yading@10: if (header->link_penalty[dist] == FLAC_HEADER_NOT_PENALIZED_YET) { yading@10: header->link_penalty[dist] = check_header_mismatch(fpc, header, yading@10: child, AV_LOG_DEBUG); yading@10: } yading@10: child_score = score_header(fpc, child) - header->link_penalty[dist]; yading@10: yading@10: if (FLAC_HEADER_BASE_SCORE + child_score > header->max_score) { yading@10: /* Keep the child because the frame scoring is dynamic. */ yading@10: header->best_child = child; yading@10: header->max_score = FLAC_HEADER_BASE_SCORE + child_score; yading@10: } yading@10: child = child->next; yading@10: } yading@10: yading@10: return header->max_score; yading@10: } yading@10: yading@10: static void score_sequences(FLACParseContext *fpc) yading@10: { yading@10: FLACHeaderMarker *curr; yading@10: int best_score = FLAC_HEADER_NOT_SCORED_YET; yading@10: /* First pass to clear all old scores. */ yading@10: for (curr = fpc->headers; curr; curr = curr->next) yading@10: curr->max_score = FLAC_HEADER_NOT_SCORED_YET; yading@10: yading@10: /* Do a second pass to score them all. */ yading@10: for (curr = fpc->headers; curr; curr = curr->next) { yading@10: if (score_header(fpc, curr) > best_score) { yading@10: fpc->best_header = curr; yading@10: best_score = curr->max_score; yading@10: } yading@10: } yading@10: } yading@10: yading@10: static int get_best_header(FLACParseContext* fpc, const uint8_t **poutbuf, yading@10: int *poutbuf_size) yading@10: { yading@10: FLACHeaderMarker *header = fpc->best_header; yading@10: FLACHeaderMarker *child = header->best_child; yading@10: if (!child) { yading@10: *poutbuf_size = av_fifo_size(fpc->fifo_buf) - header->offset; yading@10: } else { yading@10: *poutbuf_size = child->offset - header->offset; yading@10: yading@10: /* If the child has suspicious changes, log them */ yading@10: check_header_mismatch(fpc, header, child, 0); yading@10: } yading@10: yading@10: if (header->fi.channels != fpc->avctx->channels || yading@10: !fpc->avctx->channel_layout) { yading@10: fpc->avctx->channels = header->fi.channels; yading@10: ff_flac_set_channel_layout(fpc->avctx); yading@10: } yading@10: fpc->avctx->sample_rate = header->fi.samplerate; yading@10: fpc->pc->duration = header->fi.blocksize; yading@10: *poutbuf = flac_fifo_read_wrap(fpc, header->offset, *poutbuf_size, yading@10: &fpc->wrap_buf, yading@10: &fpc->wrap_buf_allocated_size); yading@10: yading@10: fpc->best_header_valid = 0; yading@10: /* Return the negative overread index so the client can compute pos. yading@10: This should be the amount overread to the beginning of the child */ yading@10: if (child) yading@10: return child->offset - av_fifo_size(fpc->fifo_buf); yading@10: return 0; yading@10: } yading@10: yading@10: static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx, yading@10: const uint8_t **poutbuf, int *poutbuf_size, yading@10: const uint8_t *buf, int buf_size) yading@10: { yading@10: FLACParseContext *fpc = s->priv_data; yading@10: FLACHeaderMarker *curr; yading@10: int nb_headers; yading@10: const uint8_t *read_end = buf; yading@10: const uint8_t *read_start = buf; yading@10: yading@10: if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { yading@10: FLACFrameInfo fi; yading@10: if (frame_header_is_valid(avctx, buf, &fi)) yading@10: s->duration = fi.blocksize; yading@10: *poutbuf = buf; yading@10: *poutbuf_size = buf_size; yading@10: return buf_size; yading@10: } yading@10: yading@10: fpc->avctx = avctx; yading@10: if (fpc->best_header_valid) yading@10: return get_best_header(fpc, poutbuf, poutbuf_size); yading@10: yading@10: /* If a best_header was found last call remove it with the buffer data. */ yading@10: if (fpc->best_header && fpc->best_header->best_child) { yading@10: FLACHeaderMarker *temp; yading@10: FLACHeaderMarker *best_child = fpc->best_header->best_child; yading@10: yading@10: /* Remove headers in list until the end of the best_header. */ yading@10: for (curr = fpc->headers; curr != best_child; curr = temp) { yading@10: if (curr != fpc->best_header) { yading@10: av_log(avctx, AV_LOG_DEBUG, yading@10: "dropping low score %i frame header from offset %i to %i\n", yading@10: curr->max_score, curr->offset, curr->next->offset); yading@10: } yading@10: temp = curr->next; yading@10: av_freep(&curr->link_penalty); yading@10: av_free(curr); yading@10: fpc->nb_headers_buffered--; yading@10: } yading@10: /* Release returned data from ring buffer. */ yading@10: av_fifo_drain(fpc->fifo_buf, best_child->offset); yading@10: yading@10: /* Fix the offset for the headers remaining to match the new buffer. */ yading@10: for (curr = best_child->next; curr; curr = curr->next) yading@10: curr->offset -= best_child->offset; yading@10: yading@10: fpc->nb_headers_buffered--; yading@10: best_child->offset = 0; yading@10: fpc->headers = best_child; yading@10: if (fpc->nb_headers_buffered >= FLAC_MIN_HEADERS) { yading@10: fpc->best_header = best_child; yading@10: return get_best_header(fpc, poutbuf, poutbuf_size); yading@10: } yading@10: fpc->best_header = NULL; yading@10: } else if (fpc->best_header) { yading@10: /* No end frame no need to delete the buffer; probably eof */ yading@10: FLACHeaderMarker *temp; yading@10: yading@10: for (curr = fpc->headers; curr != fpc->best_header; curr = temp) { yading@10: temp = curr->next; yading@10: av_freep(&curr->link_penalty); yading@10: av_free(curr); yading@10: } yading@10: fpc->headers = fpc->best_header->next; yading@10: av_freep(&fpc->best_header->link_penalty); yading@10: av_freep(&fpc->best_header); yading@10: } yading@10: yading@10: /* Find and score new headers. */ yading@10: /* buf_size is to zero when padding, so check for this since we do */ yading@10: /* not want to try to read more input once we have found the end. */ yading@10: /* Note that as (non-modified) parameters, buf can be non-NULL, */ yading@10: /* while buf_size is 0. */ yading@10: while ((buf && buf_size && read_end < buf + buf_size && yading@10: fpc->nb_headers_buffered < FLAC_MIN_HEADERS) yading@10: || ((!buf || !buf_size) && !fpc->end_padded)) { yading@10: int start_offset; yading@10: yading@10: /* Pad the end once if EOF, to check the final region for headers. */ yading@10: if (!buf || !buf_size) { yading@10: fpc->end_padded = 1; yading@10: buf_size = MAX_FRAME_HEADER_SIZE; yading@10: read_end = read_start + MAX_FRAME_HEADER_SIZE; yading@10: } else { yading@10: /* The maximum read size is the upper-bound of what the parser yading@10: needs to have the required number of frames buffered */ yading@10: int nb_desired = FLAC_MIN_HEADERS - fpc->nb_headers_buffered + 1; yading@10: read_end = read_end + FFMIN(buf + buf_size - read_end, yading@10: nb_desired * FLAC_AVG_FRAME_SIZE); yading@10: } yading@10: yading@10: /* Fill the buffer. */ yading@10: if ( av_fifo_space(fpc->fifo_buf) < read_end - read_start yading@10: && av_fifo_realloc2(fpc->fifo_buf, (read_end - read_start) + 2*av_fifo_size(fpc->fifo_buf)) < 0) { yading@10: av_log(avctx, AV_LOG_ERROR, yading@10: "couldn't reallocate buffer of size %td\n", yading@10: (read_end - read_start) + av_fifo_size(fpc->fifo_buf)); yading@10: goto handle_error; yading@10: } yading@10: yading@10: if (buf && buf_size) { yading@10: av_fifo_generic_write(fpc->fifo_buf, (void*) read_start, yading@10: read_end - read_start, NULL); yading@10: } else { yading@10: int8_t pad[MAX_FRAME_HEADER_SIZE] = { 0 }; yading@10: av_fifo_generic_write(fpc->fifo_buf, (void*) pad, sizeof(pad), NULL); yading@10: } yading@10: yading@10: /* Tag headers and update sequences. */ yading@10: start_offset = av_fifo_size(fpc->fifo_buf) - yading@10: ((read_end - read_start) + (MAX_FRAME_HEADER_SIZE - 1)); yading@10: start_offset = FFMAX(0, start_offset); yading@10: nb_headers = find_new_headers(fpc, start_offset); yading@10: yading@10: if (nb_headers < 0) { yading@10: av_log(avctx, AV_LOG_ERROR, yading@10: "find_new_headers couldn't allocate FLAC header\n"); yading@10: goto handle_error; yading@10: } yading@10: yading@10: fpc->nb_headers_buffered = nb_headers; yading@10: /* Wait till FLAC_MIN_HEADERS to output a valid frame. */ yading@10: if (!fpc->end_padded && fpc->nb_headers_buffered < FLAC_MIN_HEADERS) { yading@10: if (buf && read_end < buf + buf_size) { yading@10: read_start = read_end; yading@10: continue; yading@10: } else { yading@10: goto handle_error; yading@10: } yading@10: } yading@10: yading@10: /* If headers found, update the scores since we have longer chains. */ yading@10: if (fpc->end_padded || fpc->nb_headers_found) yading@10: score_sequences(fpc); yading@10: yading@10: /* restore the state pre-padding */ yading@10: if (fpc->end_padded) { yading@10: int warp = fpc->fifo_buf->wptr - fpc->fifo_buf->buffer < MAX_FRAME_HEADER_SIZE; yading@10: /* HACK: drain the tail of the fifo */ yading@10: fpc->fifo_buf->wptr -= MAX_FRAME_HEADER_SIZE; yading@10: fpc->fifo_buf->wndx -= MAX_FRAME_HEADER_SIZE; yading@10: if (warp) { yading@10: fpc->fifo_buf->wptr += fpc->fifo_buf->end - yading@10: fpc->fifo_buf->buffer; yading@10: } yading@10: buf_size = 0; yading@10: read_start = read_end = NULL; yading@10: } yading@10: } yading@10: yading@10: curr = fpc->headers; yading@10: for (curr = fpc->headers; curr; curr = curr->next) yading@10: if (!fpc->best_header || curr->max_score > fpc->best_header->max_score) yading@10: fpc->best_header = curr; yading@10: yading@10: if (fpc->best_header) { yading@10: fpc->best_header_valid = 1; yading@10: if (fpc->best_header->offset > 0) { yading@10: /* Output a junk frame. */ yading@10: av_log(avctx, AV_LOG_DEBUG, "Junk frame till offset %i\n", yading@10: fpc->best_header->offset); yading@10: yading@10: /* Set duration to 0. It is unknown or invalid in a junk frame. */ yading@10: s->duration = 0; yading@10: *poutbuf_size = fpc->best_header->offset; yading@10: *poutbuf = flac_fifo_read_wrap(fpc, 0, *poutbuf_size, yading@10: &fpc->wrap_buf, yading@10: &fpc->wrap_buf_allocated_size); yading@10: return buf_size ? (read_end - buf) : (fpc->best_header->offset - yading@10: av_fifo_size(fpc->fifo_buf)); yading@10: } yading@10: if (!buf_size) yading@10: return get_best_header(fpc, poutbuf, poutbuf_size); yading@10: } yading@10: yading@10: handle_error: yading@10: *poutbuf = NULL; yading@10: *poutbuf_size = 0; yading@10: return read_end - buf; yading@10: } yading@10: yading@10: static int flac_parse_init(AVCodecParserContext *c) yading@10: { yading@10: FLACParseContext *fpc = c->priv_data; yading@10: fpc->pc = c; yading@10: /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before yading@10: it drains. This is allocated early to avoid slow reallocation. */ yading@10: fpc->fifo_buf = av_fifo_alloc(FLAC_AVG_FRAME_SIZE * (FLAC_MIN_HEADERS + 3)); yading@10: return 0; yading@10: } yading@10: yading@10: static void flac_parse_close(AVCodecParserContext *c) yading@10: { yading@10: FLACParseContext *fpc = c->priv_data; yading@10: FLACHeaderMarker *curr = fpc->headers, *temp; yading@10: yading@10: while (curr) { yading@10: temp = curr->next; yading@10: av_freep(&curr->link_penalty); yading@10: av_free(curr); yading@10: curr = temp; yading@10: } yading@10: av_fifo_free(fpc->fifo_buf); yading@10: av_free(fpc->wrap_buf); yading@10: } yading@10: yading@10: AVCodecParser ff_flac_parser = { yading@10: .codec_ids = { AV_CODEC_ID_FLAC }, yading@10: .priv_data_size = sizeof(FLACParseContext), yading@10: .parser_init = flac_parse_init, yading@10: .parser_parse = flac_parse, yading@10: .parser_close = flac_parse_close, yading@10: };