2010-12-07 16:50:50 +02:00
|
|
|
/*
|
|
|
|
* FLAC parser
|
|
|
|
* Copyright (c) 2010 Michael Chinen
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* FLAC parser
|
|
|
|
*
|
|
|
|
* The FLAC parser buffers input until FLAC_MIN_HEADERS has been found.
|
|
|
|
* Each time it finds and verifies a CRC-8 header it sees which of the
|
|
|
|
* FLAC_MAX_SEQUENTIAL_HEADERS that came before it have a valid CRC-16 footer
|
|
|
|
* that ends at the newly found header.
|
2012-08-21 02:02:13 +03:00
|
|
|
* Headers are scored by FLAC_HEADER_BASE_SCORE plus the max of its crc-verified
|
2010-12-07 16:50:50 +02:00
|
|
|
* children, penalized by changes in sample rate, frame number, etc.
|
|
|
|
* The parser returns the frame with the highest score.
|
|
|
|
**/
|
|
|
|
|
2013-02-01 12:31:59 +03:00
|
|
|
#include "libavutil/attributes.h"
|
2010-12-07 16:50:50 +02:00
|
|
|
#include "libavutil/crc.h"
|
|
|
|
#include "bytestream.h"
|
|
|
|
#include "parser.h"
|
2022-08-28 17:12:30 +02:00
|
|
|
#include "flac_parse.h"
|
2010-12-07 16:50:50 +02:00
|
|
|
|
|
|
|
/** maximum number of adjacent headers that compare CRCs against each other */
|
2016-06-24 02:08:41 +02:00
|
|
|
#define FLAC_MAX_SEQUENTIAL_HEADERS 4
|
2010-12-07 16:50:50 +02:00
|
|
|
/** minimum number of headers buffered and checked before returning frames */
|
|
|
|
#define FLAC_MIN_HEADERS 10
|
2010-12-12 02:52:57 +02:00
|
|
|
/** estimate for average size of a FLAC frame */
|
|
|
|
#define FLAC_AVG_FRAME_SIZE 8192
|
2010-12-07 16:50:50 +02:00
|
|
|
|
|
|
|
/** scoring settings for score_header */
|
|
|
|
#define FLAC_HEADER_BASE_SCORE 10
|
|
|
|
#define FLAC_HEADER_CHANGED_PENALTY 7
|
|
|
|
#define FLAC_HEADER_CRC_FAIL_PENALTY 50
|
|
|
|
#define FLAC_HEADER_NOT_PENALIZED_YET 100000
|
|
|
|
#define FLAC_HEADER_NOT_SCORED_YET -100000
|
|
|
|
|
|
|
|
/** largest possible size of flac header */
|
|
|
|
#define MAX_FRAME_HEADER_SIZE 16
|
2021-10-21 13:25:59 +02:00
|
|
|
#define MAX_FRAME_VERIFY_SIZE (MAX_FRAME_HEADER_SIZE + 1)
|
2010-12-07 16:50:50 +02:00
|
|
|
|
2021-12-29 18:02:46 +02:00
|
|
|
typedef struct FifoBuffer {
|
|
|
|
uint8_t *buffer;
|
|
|
|
uint8_t *end;
|
|
|
|
uint8_t *rptr;
|
|
|
|
uint8_t *wptr;
|
|
|
|
int empty;
|
|
|
|
} FifoBuffer;
|
|
|
|
|
2010-12-07 16:50:50 +02:00
|
|
|
typedef struct FLACHeaderMarker {
|
|
|
|
int offset; /**< byte offset from start of FLACParseContext->buffer */
|
2019-10-06 07:01:14 +02:00
|
|
|
int link_penalty[FLAC_MAX_SEQUENTIAL_HEADERS]; /**< array of local scores
|
|
|
|
between this header and the one at a distance equal
|
|
|
|
array position */
|
2010-12-07 16:50:50 +02:00
|
|
|
int max_score; /**< maximum score found after checking each child that
|
|
|
|
has a valid CRC */
|
|
|
|
FLACFrameInfo fi; /**< decoded frame header info */
|
|
|
|
struct FLACHeaderMarker *next; /**< next CRC-8 verified header that
|
|
|
|
immediately follows this one in
|
|
|
|
the bytestream */
|
|
|
|
struct FLACHeaderMarker *best_child; /**< following frame header with
|
|
|
|
which this frame has the best
|
|
|
|
score with */
|
|
|
|
} FLACHeaderMarker;
|
|
|
|
|
|
|
|
typedef struct FLACParseContext {
|
2012-01-13 04:53:09 +03:00
|
|
|
AVCodecParserContext *pc; /**< parent context */
|
2010-12-07 16:50:50 +02:00
|
|
|
AVCodecContext *avctx; /**< codec context pointer for logging */
|
|
|
|
FLACHeaderMarker *headers; /**< linked-list that starts at the first
|
|
|
|
CRC-8 verified header within buffer */
|
|
|
|
FLACHeaderMarker *best_header; /**< highest scoring header within buffer */
|
|
|
|
int nb_headers_found; /**< number of headers found in the last
|
|
|
|
flac_parse() call */
|
2010-12-12 02:52:54 +02:00
|
|
|
int nb_headers_buffered; /**< number of headers that are buffered */
|
2010-12-07 16:50:50 +02:00
|
|
|
int best_header_valid; /**< flag set when the parser returns junk;
|
|
|
|
if set return best_header next time */
|
2021-12-29 18:02:46 +02:00
|
|
|
FifoBuffer fifo_buf; /**< buffer to store all data until headers
|
2010-12-07 16:50:50 +02:00
|
|
|
can be verified */
|
|
|
|
int end_padded; /**< specifies if fifo_buf's end is padded */
|
|
|
|
uint8_t *wrap_buf; /**< general fifo read buffer when wrapped */
|
|
|
|
int wrap_buf_allocated_size; /**< actual allocated size of the buffer */
|
2013-06-28 08:16:39 +03:00
|
|
|
FLACFrameInfo last_fi; /**< last decoded frame header info */
|
|
|
|
int last_fi_valid; /**< set if last_fi is valid */
|
2010-12-07 16:50:50 +02:00
|
|
|
} FLACParseContext;
|
|
|
|
|
|
|
|
static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf,
|
|
|
|
FLACFrameInfo *fi)
|
|
|
|
{
|
|
|
|
GetBitContext gb;
|
2021-10-13 18:15:26 +02:00
|
|
|
uint8_t subframe_type;
|
|
|
|
|
|
|
|
// header plus one byte from first subframe
|
2021-10-21 13:25:59 +02:00
|
|
|
init_get_bits(&gb, buf, MAX_FRAME_VERIFY_SIZE * 8);
|
2021-10-13 18:15:26 +02:00
|
|
|
if (ff_flac_decode_frame_header(avctx, &gb, fi, 127)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// subframe zero bit
|
|
|
|
if (get_bits1(&gb) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// subframe type
|
|
|
|
// 000000 : SUBFRAME_CONSTANT
|
|
|
|
// 000001 : SUBFRAME_VERBATIM
|
|
|
|
// 00001x : reserved
|
|
|
|
// 0001xx : reserved
|
|
|
|
// 001xxx : if(xxx <= 4) SUBFRAME_FIXED, xxx=order ; else reserved
|
|
|
|
// 01xxxx : reserved
|
|
|
|
// 1xxxxx : SUBFRAME_LPC, xxxxx=order-1
|
|
|
|
subframe_type = get_bits(&gb, 6);
|
|
|
|
if (!(subframe_type == 0 ||
|
|
|
|
subframe_type == 1 ||
|
|
|
|
((subframe_type >= 8) && (subframe_type <= 12)) ||
|
|
|
|
(subframe_type >= 32))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
|
|
|
|
2021-12-29 18:02:46 +02:00
|
|
|
static size_t flac_fifo_size(const FifoBuffer *f)
|
|
|
|
{
|
|
|
|
if (f->wptr <= f->rptr && !f->empty)
|
|
|
|
return (f->wptr - f->buffer) + (f->end - f->rptr);
|
|
|
|
return f->wptr - f->rptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t flac_fifo_space(const FifoBuffer *f)
|
|
|
|
{
|
|
|
|
return f->end - f->buffer - flac_fifo_size(f);
|
|
|
|
}
|
|
|
|
|
2010-12-07 16:50:50 +02:00
|
|
|
/**
|
|
|
|
* Non-destructive fast fifo pointer fetching
|
|
|
|
* Returns a pointer from the specified offset.
|
|
|
|
* If possible the pointer points within the fifo buffer.
|
|
|
|
* Otherwise (if it would cause a wrap around,) a pointer to a user-specified
|
|
|
|
* buffer is used.
|
|
|
|
* The pointer can be NULL. In any case it will be reallocated to hold the size.
|
|
|
|
* If the returned pointer will be used after subsequent calls to flac_fifo_read_wrap
|
|
|
|
* then the subsequent calls should pass in a different wrap_buf so as to not
|
|
|
|
* overwrite the contents of the previous wrap_buf.
|
|
|
|
* This function is based on av_fifo_generic_read, which is why there is a comment
|
|
|
|
* about a memory barrier for SMP.
|
|
|
|
*/
|
2019-10-06 07:01:20 +02:00
|
|
|
static uint8_t *flac_fifo_read_wrap(FLACParseContext *fpc, int offset, int len,
|
|
|
|
uint8_t **wrap_buf, int *allocated_size)
|
2010-12-07 16:50:50 +02:00
|
|
|
{
|
2021-12-29 18:02:46 +02:00
|
|
|
FifoBuffer *f = &fpc->fifo_buf;
|
2010-12-07 16:50:50 +02:00
|
|
|
uint8_t *start = f->rptr + offset;
|
|
|
|
uint8_t *tmp_buf;
|
|
|
|
|
|
|
|
if (start >= f->end)
|
|
|
|
start -= f->end - f->buffer;
|
|
|
|
if (f->end - start >= len)
|
|
|
|
return start;
|
|
|
|
|
|
|
|
tmp_buf = av_fast_realloc(*wrap_buf, allocated_size, len);
|
|
|
|
|
|
|
|
if (!tmp_buf) {
|
|
|
|
av_log(fpc->avctx, AV_LOG_ERROR,
|
|
|
|
"couldn't reallocate wrap buffer of size %d", len);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*wrap_buf = tmp_buf;
|
|
|
|
do {
|
|
|
|
int seg_len = FFMIN(f->end - start, len);
|
|
|
|
memcpy(tmp_buf, start, seg_len);
|
|
|
|
tmp_buf = (uint8_t*)tmp_buf + seg_len;
|
|
|
|
// memory barrier needed for SMP here in theory
|
|
|
|
|
|
|
|
start += seg_len - (f->end - f->buffer);
|
|
|
|
len -= seg_len;
|
|
|
|
} while (len > 0);
|
|
|
|
|
|
|
|
return *wrap_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a pointer in the fifo buffer where the offset starts at until
|
|
|
|
* the wrap point or end of request.
|
|
|
|
* len will contain the valid length of the returned buffer.
|
|
|
|
* A second call to flac_fifo_read (with new offset and len) should be called
|
|
|
|
* to get the post-wrap buf if the returned len is less than the requested.
|
|
|
|
**/
|
2021-12-29 18:02:46 +02:00
|
|
|
static uint8_t *flac_fifo_read(FifoBuffer *f, int offset, int *len)
|
2010-12-07 16:50:50 +02:00
|
|
|
{
|
|
|
|
uint8_t *start = f->rptr + offset;
|
|
|
|
|
|
|
|
if (start >= f->end)
|
|
|
|
start -= f->end - f->buffer;
|
|
|
|
*len = FFMIN(*len, f->end - start);
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2021-12-29 18:02:46 +02:00
|
|
|
static int flac_fifo_grow(FifoBuffer *f, size_t inc)
|
|
|
|
{
|
|
|
|
size_t size_old = f->end - f->buffer;
|
|
|
|
size_t offset_r = f->rptr - f->buffer;
|
|
|
|
size_t offset_w = f->wptr - f->buffer;
|
|
|
|
size_t size_new;
|
|
|
|
|
|
|
|
uint8_t *tmp;
|
|
|
|
|
|
|
|
if (size_old > SIZE_MAX - inc)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
size_new = size_old + inc;
|
|
|
|
|
|
|
|
tmp = av_realloc(f->buffer, size_new);
|
|
|
|
if (!tmp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
// move the data from the beginning of the ring buffer
|
|
|
|
// to the newly allocated space
|
|
|
|
if (offset_w <= offset_r && !f->empty) {
|
|
|
|
const size_t copy = FFMIN(inc, offset_w);
|
|
|
|
memcpy(tmp + size_old, tmp, copy);
|
|
|
|
if (copy < offset_w) {
|
|
|
|
memmove(tmp, tmp + copy, offset_w - copy);
|
|
|
|
offset_w -= copy;
|
|
|
|
} else
|
|
|
|
offset_w = size_old + copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
f->buffer = tmp;
|
|
|
|
f->end = f->buffer + size_new;
|
|
|
|
f->rptr = f->buffer + offset_r;
|
|
|
|
f->wptr = f->buffer + offset_w;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int flac_fifo_write(FifoBuffer *f, const uint8_t *src, size_t size)
|
|
|
|
{
|
|
|
|
uint8_t *wptr;
|
|
|
|
|
|
|
|
if (flac_fifo_space(f) < size) {
|
|
|
|
int ret = flac_fifo_grow(f, FFMAX(flac_fifo_size(f), size));
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size)
|
|
|
|
f->empty = 0;
|
|
|
|
|
|
|
|
wptr = f->wptr;
|
|
|
|
do {
|
|
|
|
size_t len = FFMIN(f->end - wptr, size);
|
|
|
|
memcpy(wptr, src, len);
|
|
|
|
src += len;
|
|
|
|
wptr += len;
|
|
|
|
if (wptr >= f->end)
|
|
|
|
wptr = f->buffer;
|
|
|
|
size -= len;
|
|
|
|
} while (size > 0);
|
|
|
|
|
|
|
|
f->wptr = wptr;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void flac_fifo_drain(FifoBuffer *f, size_t size)
|
|
|
|
{
|
|
|
|
size_t size_cur = flac_fifo_size(f);
|
|
|
|
|
|
|
|
av_assert0(size_cur >= size);
|
|
|
|
if (size_cur == size)
|
|
|
|
f->empty = 1;
|
|
|
|
|
|
|
|
f->rptr += size;
|
|
|
|
if (f->rptr >= f->end)
|
|
|
|
f->rptr -= f->end - f->buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int flac_fifo_alloc(FifoBuffer *f, size_t size)
|
|
|
|
{
|
|
|
|
memset(f, 0, sizeof(*f));
|
|
|
|
|
|
|
|
f->buffer = av_realloc(NULL, size);
|
|
|
|
if (!f->buffer)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
f->wptr = f->buffer;
|
|
|
|
f->rptr = f->buffer;
|
|
|
|
f->end = f->buffer + size;
|
|
|
|
|
|
|
|
f->empty = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void flac_fifo_free(FifoBuffer *f)
|
|
|
|
{
|
|
|
|
av_freep(&f->buffer);
|
|
|
|
memset(f, 0, sizeof(*f));
|
|
|
|
}
|
|
|
|
|
2010-12-07 16:50:50 +02:00
|
|
|
static int find_headers_search_validate(FLACParseContext *fpc, int offset)
|
|
|
|
{
|
|
|
|
FLACFrameInfo fi;
|
|
|
|
uint8_t *header_buf;
|
|
|
|
int size = 0;
|
|
|
|
header_buf = flac_fifo_read_wrap(fpc, offset,
|
2021-10-21 13:25:59 +02:00
|
|
|
MAX_FRAME_VERIFY_SIZE + AV_INPUT_BUFFER_PADDING_SIZE,
|
2010-12-07 16:50:50 +02:00
|
|
|
&fpc->wrap_buf,
|
|
|
|
&fpc->wrap_buf_allocated_size);
|
|
|
|
if (frame_header_is_valid(fpc->avctx, header_buf, &fi)) {
|
|
|
|
FLACHeaderMarker **end_handle = &fpc->headers;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
while (*end_handle) {
|
|
|
|
end_handle = &(*end_handle)->next;
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
|
2015-01-15 17:48:54 +02:00
|
|
|
*end_handle = av_mallocz(sizeof(**end_handle));
|
2010-12-07 16:50:50 +02:00
|
|
|
if (!*end_handle) {
|
|
|
|
av_log(fpc->avctx, AV_LOG_ERROR,
|
|
|
|
"couldn't allocate FLACHeaderMarker\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2019-10-06 07:01:20 +02:00
|
|
|
(*end_handle)->fi = fi;
|
|
|
|
(*end_handle)->offset = offset;
|
2015-01-15 17:51:08 +02:00
|
|
|
|
2010-12-07 16:50:50 +02:00
|
|
|
for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++)
|
|
|
|
(*end_handle)->link_penalty[i] = FLAC_HEADER_NOT_PENALIZED_YET;
|
|
|
|
|
|
|
|
fpc->nb_headers_found++;
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-10-06 07:01:20 +02:00
|
|
|
static int find_headers_search(FLACParseContext *fpc, uint8_t *buf,
|
|
|
|
int buf_size, int search_start)
|
2010-12-07 16:50:50 +02:00
|
|
|
{
|
|
|
|
int size = 0, mod_offset = (buf_size - 1) % 4, i, j;
|
|
|
|
uint32_t x;
|
|
|
|
|
|
|
|
for (i = 0; i < mod_offset; i++) {
|
2020-02-04 00:20:23 +02:00
|
|
|
if ((AV_RB16(buf + i) & 0xFFFE) == 0xFFF8) {
|
|
|
|
int ret = find_headers_search_validate(fpc, search_start + i);
|
|
|
|
size = FFMAX(size, ret);
|
|
|
|
}
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (; i < buf_size - 1; i += 4) {
|
2019-10-06 07:01:13 +02:00
|
|
|
x = AV_RN32(buf + i);
|
2010-12-07 16:50:50 +02:00
|
|
|
if (((x & ~(x + 0x01010101)) & 0x80808080)) {
|
|
|
|
for (j = 0; j < 4; j++) {
|
2020-02-04 00:20:23 +02:00
|
|
|
if ((AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8) {
|
|
|
|
int ret = find_headers_search_validate(fpc, search_start + i + j);
|
|
|
|
size = FFMAX(size, ret);
|
|
|
|
}
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int find_new_headers(FLACParseContext *fpc, int search_start)
|
|
|
|
{
|
|
|
|
FLACHeaderMarker *end;
|
|
|
|
int search_end, size = 0, read_len, temp;
|
|
|
|
uint8_t *buf;
|
|
|
|
fpc->nb_headers_found = 0;
|
|
|
|
|
|
|
|
/* Search for a new header of at most 16 bytes. */
|
2021-12-29 18:02:46 +02:00
|
|
|
search_end = flac_fifo_size(&fpc->fifo_buf) - (MAX_FRAME_HEADER_SIZE - 1);
|
2010-12-07 16:50:50 +02:00
|
|
|
read_len = search_end - search_start + 1;
|
2021-12-29 18:02:46 +02:00
|
|
|
buf = flac_fifo_read(&fpc->fifo_buf, search_start, &read_len);
|
2010-12-07 16:50:50 +02:00
|
|
|
size = find_headers_search(fpc, buf, read_len, search_start);
|
|
|
|
search_start += read_len - 1;
|
|
|
|
|
|
|
|
/* If fifo end was hit do the wrap around. */
|
|
|
|
if (search_start != search_end) {
|
|
|
|
uint8_t wrap[2];
|
|
|
|
|
|
|
|
wrap[0] = buf[read_len - 1];
|
|
|
|
/* search_start + 1 is the post-wrap offset in the fifo. */
|
2019-10-06 07:01:15 +02:00
|
|
|
read_len = search_end - (search_start + 1) + 1;
|
|
|
|
|
2021-12-29 18:02:46 +02:00
|
|
|
buf = flac_fifo_read(&fpc->fifo_buf, search_start + 1, &read_len);
|
2010-12-07 16:50:50 +02:00
|
|
|
wrap[1] = buf[0];
|
|
|
|
|
|
|
|
if ((AV_RB16(wrap) & 0xFFFE) == 0xFFF8) {
|
|
|
|
temp = find_headers_search_validate(fpc, search_start);
|
|
|
|
size = FFMAX(size, temp);
|
|
|
|
}
|
|
|
|
search_start++;
|
|
|
|
|
|
|
|
/* Continue to do the last half of the wrap. */
|
|
|
|
temp = find_headers_search(fpc, buf, read_len, search_start);
|
|
|
|
size = FFMAX(size, temp);
|
|
|
|
search_start += read_len - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the size even if no new headers were found. */
|
|
|
|
if (!size && fpc->headers)
|
|
|
|
for (end = fpc->headers; end; end = end->next)
|
|
|
|
size++;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2013-06-28 08:16:39 +03:00
|
|
|
static int check_header_fi_mismatch(FLACParseContext *fpc,
|
|
|
|
FLACFrameInfo *header_fi,
|
|
|
|
FLACFrameInfo *child_fi,
|
|
|
|
int log_level_offset)
|
2010-12-07 16:50:50 +02:00
|
|
|
{
|
2013-06-28 08:16:39 +03:00
|
|
|
int deduction = 0;
|
2010-12-07 16:50:50 +02:00
|
|
|
if (child_fi->samplerate != header_fi->samplerate) {
|
|
|
|
deduction += FLAC_HEADER_CHANGED_PENALTY;
|
|
|
|
av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
|
|
|
|
"sample rate change detected in adjacent frames\n");
|
|
|
|
}
|
|
|
|
if (child_fi->bps != header_fi->bps) {
|
|
|
|
deduction += FLAC_HEADER_CHANGED_PENALTY;
|
|
|
|
av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
|
|
|
|
"bits per sample change detected in adjacent frames\n");
|
|
|
|
}
|
|
|
|
if (child_fi->is_var_size != header_fi->is_var_size) {
|
|
|
|
/* Changing blocking strategy not allowed per the spec */
|
|
|
|
deduction += FLAC_HEADER_BASE_SCORE;
|
|
|
|
av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
|
2013-06-28 08:16:39 +03:00
|
|
|
"blocking strategy change detected in adjacent frames\n");
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
|
|
|
if (child_fi->channels != header_fi->channels) {
|
|
|
|
deduction += FLAC_HEADER_CHANGED_PENALTY;
|
|
|
|
av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
|
2013-06-28 08:16:39 +03:00
|
|
|
"number of channels change detected in adjacent frames\n");
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
2013-06-28 08:16:39 +03:00
|
|
|
return deduction;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_header_mismatch(FLACParseContext *fpc,
|
|
|
|
FLACHeaderMarker *header,
|
|
|
|
FLACHeaderMarker *child,
|
|
|
|
int log_level_offset)
|
|
|
|
{
|
|
|
|
FLACFrameInfo *header_fi = &header->fi, *child_fi = &child->fi;
|
2022-09-05 20:16:13 +02:00
|
|
|
int check_crc, deduction, deduction_expected = 0, i;
|
2013-06-28 08:16:39 +03:00
|
|
|
deduction = check_header_fi_mismatch(fpc, header_fi, child_fi,
|
|
|
|
log_level_offset);
|
2010-12-07 16:50:50 +02:00
|
|
|
/* Check sample and frame numbers. */
|
|
|
|
if ((child_fi->frame_or_sample_num - header_fi->frame_or_sample_num
|
|
|
|
!= header_fi->blocksize) &&
|
|
|
|
(child_fi->frame_or_sample_num
|
|
|
|
!= header_fi->frame_or_sample_num + 1)) {
|
|
|
|
FLACHeaderMarker *curr;
|
2019-09-19 16:28:57 +02:00
|
|
|
int64_t expected_frame_num, expected_sample_num;
|
2010-12-07 16:50:50 +02:00
|
|
|
/* If there are frames in the middle we expect this deduction,
|
|
|
|
as they are probably valid and this one follows it */
|
|
|
|
|
|
|
|
expected_frame_num = expected_sample_num = header_fi->frame_or_sample_num;
|
|
|
|
curr = header;
|
|
|
|
while (curr != child) {
|
|
|
|
/* Ignore frames that failed all crc checks */
|
|
|
|
for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS; i++) {
|
|
|
|
if (curr->link_penalty[i] < FLAC_HEADER_CRC_FAIL_PENALTY) {
|
|
|
|
expected_frame_num++;
|
|
|
|
expected_sample_num += curr->fi.blocksize;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expected_frame_num == child_fi->frame_or_sample_num ||
|
|
|
|
expected_sample_num == child_fi->frame_or_sample_num)
|
|
|
|
deduction_expected = deduction ? 0 : 1;
|
|
|
|
|
|
|
|
deduction += FLAC_HEADER_CHANGED_PENALTY;
|
|
|
|
av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
|
|
|
|
"sample/frame number mismatch in adjacent frames\n");
|
|
|
|
}
|
|
|
|
|
2022-09-05 20:16:13 +02:00
|
|
|
if (fpc->last_fi.is_var_size == header_fi->is_var_size) {
|
|
|
|
if (fpc->last_fi.is_var_size &&
|
|
|
|
fpc->last_fi.frame_or_sample_num + fpc->last_fi.blocksize == header_fi->frame_or_sample_num) {
|
|
|
|
check_crc = 0;
|
|
|
|
} else if (!fpc->last_fi.is_var_size &&
|
|
|
|
fpc->last_fi.frame_or_sample_num + 1 == header_fi->frame_or_sample_num) {
|
|
|
|
check_crc = 0;
|
|
|
|
} else {
|
|
|
|
check_crc = !deduction && !deduction_expected;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
check_crc = !deduction && !deduction_expected;
|
|
|
|
}
|
|
|
|
|
2010-12-07 16:50:50 +02:00
|
|
|
/* If we have suspicious headers, check the CRC between them */
|
2022-09-05 20:16:13 +02:00
|
|
|
if (check_crc || (deduction && !deduction_expected)) {
|
2010-12-07 16:50:50 +02:00
|
|
|
FLACHeaderMarker *curr;
|
|
|
|
int read_len;
|
|
|
|
uint8_t *buf;
|
|
|
|
uint32_t crc = 1;
|
|
|
|
int inverted_test = 0;
|
|
|
|
|
|
|
|
/* Since CRC is expensive only do it if we haven't yet.
|
|
|
|
This assumes a CRC penalty is greater than all other check penalties */
|
|
|
|
curr = header->next;
|
|
|
|
for (i = 0; i < FLAC_MAX_SEQUENTIAL_HEADERS && curr != child; i++)
|
|
|
|
curr = curr->next;
|
|
|
|
|
|
|
|
if (header->link_penalty[i] < FLAC_HEADER_CRC_FAIL_PENALTY ||
|
|
|
|
header->link_penalty[i] == FLAC_HEADER_NOT_PENALIZED_YET) {
|
|
|
|
FLACHeaderMarker *start, *end;
|
|
|
|
|
|
|
|
/* Although overlapping chains are scored, the crc should never
|
|
|
|
have to be computed twice for a single byte. */
|
|
|
|
start = header;
|
|
|
|
end = child;
|
|
|
|
if (i > 0 &&
|
|
|
|
header->link_penalty[i - 1] >= FLAC_HEADER_CRC_FAIL_PENALTY) {
|
|
|
|
while (start->next != child)
|
|
|
|
start = start->next;
|
|
|
|
inverted_test = 1;
|
|
|
|
} else if (i > 0 &&
|
|
|
|
header->next->link_penalty[i-1] >=
|
|
|
|
FLAC_HEADER_CRC_FAIL_PENALTY ) {
|
|
|
|
end = header->next;
|
|
|
|
inverted_test = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
read_len = end->offset - start->offset;
|
2021-12-29 18:02:46 +02:00
|
|
|
buf = flac_fifo_read(&fpc->fifo_buf, start->offset, &read_len);
|
2010-12-07 16:50:50 +02:00
|
|
|
crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf, read_len);
|
|
|
|
read_len = (end->offset - start->offset) - read_len;
|
|
|
|
|
|
|
|
if (read_len) {
|
2021-12-29 18:02:46 +02:00
|
|
|
buf = flac_fifo_read(&fpc->fifo_buf, end->offset - read_len, &read_len);
|
2010-12-07 16:50:50 +02:00
|
|
|
crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), crc, buf, read_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!crc ^ !inverted_test) {
|
|
|
|
deduction += FLAC_HEADER_CRC_FAIL_PENALTY;
|
|
|
|
av_log(fpc->avctx, AV_LOG_WARNING + log_level_offset,
|
|
|
|
"crc check failed from offset %i (frame %"PRId64") to %i (frame %"PRId64")\n",
|
|
|
|
header->offset, header_fi->frame_or_sample_num,
|
|
|
|
child->offset, child_fi->frame_or_sample_num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return deduction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Score a header.
|
|
|
|
*
|
|
|
|
* Give FLAC_HEADER_BASE_SCORE points to a frame for existing.
|
|
|
|
* If it has children, (subsequent frames of which the preceding CRC footer
|
|
|
|
* validates against this one,) then take the maximum score of the children,
|
|
|
|
* with a penalty of FLAC_HEADER_CHANGED_PENALTY applied for each change to
|
|
|
|
* bps, sample rate, channels, but not decorrelation mode, or blocksize,
|
|
|
|
* because it can change often.
|
|
|
|
**/
|
|
|
|
static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header)
|
|
|
|
{
|
|
|
|
FLACHeaderMarker *child;
|
|
|
|
int dist = 0;
|
|
|
|
int child_score;
|
2013-06-28 08:16:39 +03:00
|
|
|
int base_score = FLAC_HEADER_BASE_SCORE;
|
2010-12-07 16:50:50 +02:00
|
|
|
if (header->max_score != FLAC_HEADER_NOT_SCORED_YET)
|
|
|
|
return header->max_score;
|
|
|
|
|
2013-06-28 08:16:39 +03:00
|
|
|
/* Modify the base score with changes from the last output header */
|
|
|
|
if (fpc->last_fi_valid) {
|
|
|
|
/* Silence the log since this will be repeated if selected */
|
|
|
|
base_score -= check_header_fi_mismatch(fpc, &fpc->last_fi, &header->fi,
|
|
|
|
AV_LOG_DEBUG);
|
|
|
|
}
|
|
|
|
|
|
|
|
header->max_score = base_score;
|
2010-12-07 16:50:50 +02:00
|
|
|
|
|
|
|
/* Check and compute the children's scores. */
|
|
|
|
child = header->next;
|
|
|
|
for (dist = 0; dist < FLAC_MAX_SEQUENTIAL_HEADERS && child; dist++) {
|
|
|
|
/* Look at the child's frame header info and penalize suspicious
|
|
|
|
changes between the headers. */
|
|
|
|
if (header->link_penalty[dist] == FLAC_HEADER_NOT_PENALIZED_YET) {
|
|
|
|
header->link_penalty[dist] = check_header_mismatch(fpc, header,
|
|
|
|
child, AV_LOG_DEBUG);
|
|
|
|
}
|
|
|
|
child_score = score_header(fpc, child) - header->link_penalty[dist];
|
|
|
|
|
|
|
|
if (FLAC_HEADER_BASE_SCORE + child_score > header->max_score) {
|
|
|
|
/* Keep the child because the frame scoring is dynamic. */
|
|
|
|
header->best_child = child;
|
2013-06-28 08:16:39 +03:00
|
|
|
header->max_score = base_score + child_score;
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
|
|
|
child = child->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return header->max_score;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void score_sequences(FLACParseContext *fpc)
|
|
|
|
{
|
|
|
|
FLACHeaderMarker *curr;
|
2022-09-05 18:12:10 +02:00
|
|
|
int best_score = FLAC_HEADER_NOT_SCORED_YET;
|
2010-12-07 16:50:50 +02:00
|
|
|
/* First pass to clear all old scores. */
|
|
|
|
for (curr = fpc->headers; curr; curr = curr->next)
|
|
|
|
curr->max_score = FLAC_HEADER_NOT_SCORED_YET;
|
|
|
|
|
|
|
|
/* Do a second pass to score them all. */
|
|
|
|
for (curr = fpc->headers; curr; curr = curr->next) {
|
|
|
|
if (score_header(fpc, curr) > best_score) {
|
|
|
|
fpc->best_header = curr;
|
|
|
|
best_score = curr->max_score;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-06 07:01:20 +02:00
|
|
|
static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf,
|
2010-12-07 16:50:50 +02:00
|
|
|
int *poutbuf_size)
|
|
|
|
{
|
|
|
|
FLACHeaderMarker *header = fpc->best_header;
|
|
|
|
FLACHeaderMarker *child = header->best_child;
|
|
|
|
if (!child) {
|
2021-12-29 18:02:46 +02:00
|
|
|
*poutbuf_size = flac_fifo_size(&fpc->fifo_buf) - header->offset;
|
2010-12-07 16:50:50 +02:00
|
|
|
} else {
|
|
|
|
*poutbuf_size = child->offset - header->offset;
|
|
|
|
|
|
|
|
/* If the child has suspicious changes, log them */
|
|
|
|
check_header_mismatch(fpc, header, child, 0);
|
|
|
|
}
|
|
|
|
|
2013-05-07 08:20:32 +03:00
|
|
|
ff_flac_set_channel_layout(fpc->avctx, header->fi.channels);
|
|
|
|
|
2010-12-07 16:50:50 +02:00
|
|
|
fpc->avctx->sample_rate = header->fi.samplerate;
|
2012-01-13 04:53:09 +03:00
|
|
|
fpc->pc->duration = header->fi.blocksize;
|
2010-12-07 16:50:50 +02:00
|
|
|
*poutbuf = flac_fifo_read_wrap(fpc, header->offset, *poutbuf_size,
|
|
|
|
&fpc->wrap_buf,
|
|
|
|
&fpc->wrap_buf_allocated_size);
|
|
|
|
|
2019-10-06 07:01:20 +02:00
|
|
|
if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) {
|
2014-04-12 19:13:32 +03:00
|
|
|
if (header->fi.is_var_size)
|
|
|
|
fpc->pc->pts = header->fi.frame_or_sample_num;
|
|
|
|
else if (header->best_child)
|
|
|
|
fpc->pc->pts = header->fi.frame_or_sample_num * header->fi.blocksize;
|
|
|
|
}
|
|
|
|
|
2010-12-07 16:50:50 +02:00
|
|
|
fpc->best_header_valid = 0;
|
2013-06-28 08:16:39 +03:00
|
|
|
fpc->last_fi_valid = 1;
|
|
|
|
fpc->last_fi = header->fi;
|
|
|
|
|
2010-12-07 16:50:50 +02:00
|
|
|
/* Return the negative overread index so the client can compute pos.
|
|
|
|
This should be the amount overread to the beginning of the child */
|
|
|
|
if (child)
|
2021-12-29 18:02:46 +02:00
|
|
|
return child->offset - flac_fifo_size(&fpc->fifo_buf);
|
2010-12-07 16:50:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
|
|
|
|
const uint8_t **poutbuf, int *poutbuf_size,
|
|
|
|
const uint8_t *buf, int buf_size)
|
|
|
|
{
|
|
|
|
FLACParseContext *fpc = s->priv_data;
|
|
|
|
FLACHeaderMarker *curr;
|
|
|
|
int nb_headers;
|
2010-12-30 06:09:39 +02:00
|
|
|
const uint8_t *read_end = buf;
|
|
|
|
const uint8_t *read_start = buf;
|
2010-12-07 16:50:50 +02:00
|
|
|
|
|
|
|
if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
|
|
|
|
FLACFrameInfo fi;
|
2013-10-07 02:11:22 +03:00
|
|
|
if (frame_header_is_valid(avctx, buf, &fi)) {
|
2012-01-13 04:53:09 +03:00
|
|
|
s->duration = fi.blocksize;
|
2013-10-07 02:11:22 +03:00
|
|
|
if (!avctx->sample_rate)
|
|
|
|
avctx->sample_rate = fi.samplerate;
|
2019-10-06 07:01:20 +02:00
|
|
|
if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) {
|
2014-04-12 19:13:32 +03:00
|
|
|
fpc->pc->pts = fi.frame_or_sample_num;
|
|
|
|
if (!fi.is_var_size)
|
|
|
|
fpc->pc->pts *= fi.blocksize;
|
|
|
|
}
|
2013-10-07 02:11:22 +03:00
|
|
|
}
|
2010-12-07 16:50:50 +02:00
|
|
|
*poutbuf = buf;
|
|
|
|
*poutbuf_size = buf_size;
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
fpc->avctx = avctx;
|
2022-09-05 18:12:10 +02:00
|
|
|
if (fpc->best_header_valid && fpc->nb_headers_buffered >= FLAC_MIN_HEADERS)
|
2010-12-07 16:50:50 +02:00
|
|
|
return get_best_header(fpc, poutbuf, poutbuf_size);
|
|
|
|
|
|
|
|
/* If a best_header was found last call remove it with the buffer data. */
|
|
|
|
if (fpc->best_header && fpc->best_header->best_child) {
|
|
|
|
FLACHeaderMarker *temp;
|
|
|
|
FLACHeaderMarker *best_child = fpc->best_header->best_child;
|
|
|
|
|
|
|
|
/* Remove headers in list until the end of the best_header. */
|
|
|
|
for (curr = fpc->headers; curr != best_child; curr = temp) {
|
|
|
|
if (curr != fpc->best_header) {
|
|
|
|
av_log(avctx, AV_LOG_DEBUG,
|
|
|
|
"dropping low score %i frame header from offset %i to %i\n",
|
|
|
|
curr->max_score, curr->offset, curr->next->offset);
|
|
|
|
}
|
|
|
|
temp = curr->next;
|
|
|
|
av_free(curr);
|
2010-12-12 02:52:54 +02:00
|
|
|
fpc->nb_headers_buffered--;
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
|
|
|
/* Release returned data from ring buffer. */
|
2021-12-29 18:02:46 +02:00
|
|
|
flac_fifo_drain(&fpc->fifo_buf, best_child->offset);
|
2010-12-07 16:50:50 +02:00
|
|
|
|
|
|
|
/* Fix the offset for the headers remaining to match the new buffer. */
|
|
|
|
for (curr = best_child->next; curr; curr = curr->next)
|
|
|
|
curr->offset -= best_child->offset;
|
|
|
|
|
|
|
|
best_child->offset = 0;
|
|
|
|
fpc->headers = best_child;
|
2010-12-12 02:52:54 +02:00
|
|
|
if (fpc->nb_headers_buffered >= FLAC_MIN_HEADERS) {
|
|
|
|
fpc->best_header = best_child;
|
|
|
|
return get_best_header(fpc, poutbuf, poutbuf_size);
|
|
|
|
}
|
2010-12-07 16:50:50 +02:00
|
|
|
fpc->best_header = NULL;
|
|
|
|
} else if (fpc->best_header) {
|
|
|
|
/* No end frame no need to delete the buffer; probably eof */
|
|
|
|
FLACHeaderMarker *temp;
|
|
|
|
|
|
|
|
for (curr = fpc->headers; curr != fpc->best_header; curr = temp) {
|
|
|
|
temp = curr->next;
|
|
|
|
av_free(curr);
|
2016-11-24 16:29:52 +02:00
|
|
|
fpc->nb_headers_buffered--;
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
|
|
|
fpc->headers = fpc->best_header->next;
|
|
|
|
av_freep(&fpc->best_header);
|
2016-11-24 16:29:52 +02:00
|
|
|
fpc->nb_headers_buffered--;
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
|
|
|
|
2012-10-14 10:08:43 +03:00
|
|
|
/* Find and score new headers. */
|
2019-10-06 07:01:17 +02:00
|
|
|
/* buf_size is zero when flushing, so check for this since we do */
|
2012-10-14 10:08:43 +03:00
|
|
|
/* not want to try to read more input once we have found the end. */
|
2019-10-06 07:01:17 +02:00
|
|
|
/* Also note that buf can't be NULL. */
|
|
|
|
while ((buf_size && read_end < buf + buf_size &&
|
2010-12-30 06:09:39 +02:00
|
|
|
fpc->nb_headers_buffered < FLAC_MIN_HEADERS)
|
2019-10-06 07:01:17 +02:00
|
|
|
|| (!buf_size && !fpc->end_padded)) {
|
2021-12-29 18:02:46 +02:00
|
|
|
int start_offset, ret;
|
2010-12-07 16:50:50 +02:00
|
|
|
|
|
|
|
/* Pad the end once if EOF, to check the final region for headers. */
|
2019-10-06 07:01:17 +02:00
|
|
|
if (!buf_size) {
|
2019-10-06 07:01:20 +02:00
|
|
|
fpc->end_padded = 1;
|
2010-12-30 06:09:39 +02:00
|
|
|
read_end = read_start + MAX_FRAME_HEADER_SIZE;
|
2010-12-12 02:52:57 +02:00
|
|
|
} else {
|
|
|
|
/* The maximum read size is the upper-bound of what the parser
|
|
|
|
needs to have the required number of frames buffered */
|
|
|
|
int nb_desired = FLAC_MIN_HEADERS - fpc->nb_headers_buffered + 1;
|
2010-12-30 06:09:39 +02:00
|
|
|
read_end = read_end + FFMIN(buf + buf_size - read_end,
|
|
|
|
nb_desired * FLAC_AVG_FRAME_SIZE);
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
|
|
|
|
2021-12-29 18:02:46 +02:00
|
|
|
if (!flac_fifo_space(&fpc->fifo_buf) &&
|
|
|
|
flac_fifo_size(&fpc->fifo_buf) / FLAC_AVG_FRAME_SIZE >
|
2016-06-22 20:19:40 +02:00
|
|
|
fpc->nb_headers_buffered * 20) {
|
|
|
|
/* There is less than one valid flac header buffered for 20 headers
|
2015-04-16 18:33:31 +02:00
|
|
|
* buffered. Therefore the fifo is most likely filled with invalid
|
|
|
|
* data and the input is not a flac file. */
|
|
|
|
goto handle_error;
|
|
|
|
}
|
|
|
|
|
2010-12-07 16:50:50 +02:00
|
|
|
/* Fill the buffer. */
|
2019-10-06 07:01:17 +02:00
|
|
|
if (buf_size) {
|
2021-12-29 18:02:46 +02:00
|
|
|
ret = flac_fifo_write(&fpc->fifo_buf, read_start,
|
|
|
|
read_end - read_start);
|
2010-12-07 16:50:50 +02:00
|
|
|
} else {
|
2011-06-06 15:13:02 +03:00
|
|
|
int8_t pad[MAX_FRAME_HEADER_SIZE] = { 0 };
|
2021-12-29 18:02:46 +02:00
|
|
|
ret = flac_fifo_write(&fpc->fifo_buf, pad, sizeof(pad));
|
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Error buffering data\n");
|
|
|
|
goto handle_error;
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Tag headers and update sequences. */
|
2021-12-29 18:02:46 +02:00
|
|
|
start_offset = flac_fifo_size(&fpc->fifo_buf) -
|
2010-12-30 06:09:39 +02:00
|
|
|
((read_end - read_start) + (MAX_FRAME_HEADER_SIZE - 1));
|
2010-12-07 16:50:50 +02:00
|
|
|
start_offset = FFMAX(0, start_offset);
|
|
|
|
nb_headers = find_new_headers(fpc, start_offset);
|
|
|
|
|
2010-12-12 02:52:54 +02:00
|
|
|
if (nb_headers < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"find_new_headers couldn't allocate FLAC header\n");
|
|
|
|
goto handle_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
fpc->nb_headers_buffered = nb_headers;
|
2010-12-07 16:50:50 +02:00
|
|
|
/* Wait till FLAC_MIN_HEADERS to output a valid frame. */
|
2010-12-30 06:09:39 +02:00
|
|
|
if (!fpc->end_padded && fpc->nb_headers_buffered < FLAC_MIN_HEADERS) {
|
2019-10-06 07:01:17 +02:00
|
|
|
if (read_end < buf + buf_size) {
|
2010-12-30 06:09:39 +02:00
|
|
|
read_start = read_end;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
goto handle_error;
|
|
|
|
}
|
|
|
|
}
|
2010-12-07 16:50:50 +02:00
|
|
|
|
|
|
|
/* If headers found, update the scores since we have longer chains. */
|
|
|
|
if (fpc->end_padded || fpc->nb_headers_found)
|
|
|
|
score_sequences(fpc);
|
|
|
|
|
|
|
|
/* restore the state pre-padding */
|
|
|
|
if (fpc->end_padded) {
|
2021-12-29 18:02:46 +02:00
|
|
|
int empty = flac_fifo_size(&fpc->fifo_buf) == MAX_FRAME_HEADER_SIZE;
|
|
|
|
int warp = fpc->fifo_buf.wptr - fpc->fifo_buf.buffer < MAX_FRAME_HEADER_SIZE;
|
2010-12-07 16:50:50 +02:00
|
|
|
/* HACK: drain the tail of the fifo */
|
2021-12-29 18:02:46 +02:00
|
|
|
fpc->fifo_buf.wptr -= MAX_FRAME_HEADER_SIZE;
|
2012-09-22 05:40:10 +03:00
|
|
|
if (warp) {
|
2021-12-29 18:02:46 +02:00
|
|
|
fpc->fifo_buf.wptr += fpc->fifo_buf.end -
|
|
|
|
fpc->fifo_buf.buffer;
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
2021-12-29 18:02:46 +02:00
|
|
|
fpc->fifo_buf.empty = empty;
|
2010-12-30 06:09:39 +02:00
|
|
|
read_start = read_end = NULL;
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-28 08:16:39 +03:00
|
|
|
for (curr = fpc->headers; curr; curr = curr->next) {
|
2018-04-30 22:20:28 +02:00
|
|
|
if (!fpc->best_header || curr->max_score > fpc->best_header->max_score) {
|
2010-12-07 16:50:50 +02:00
|
|
|
fpc->best_header = curr;
|
2013-06-28 08:16:39 +03:00
|
|
|
}
|
|
|
|
}
|
2010-12-07 16:50:50 +02:00
|
|
|
|
2018-04-30 22:20:28 +02:00
|
|
|
if (fpc->best_header && fpc->best_header->max_score <= 0) {
|
|
|
|
// Only accept a bad header if there is no other option to continue
|
2019-10-06 07:01:17 +02:00
|
|
|
if (!buf_size || read_end != buf || fpc->nb_headers_buffered < FLAC_MIN_HEADERS)
|
2018-04-30 22:20:28 +02:00
|
|
|
fpc->best_header = NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-07 16:50:50 +02:00
|
|
|
if (fpc->best_header) {
|
|
|
|
fpc->best_header_valid = 1;
|
|
|
|
if (fpc->best_header->offset > 0) {
|
|
|
|
/* Output a junk frame. */
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "Junk frame till offset %i\n",
|
|
|
|
fpc->best_header->offset);
|
|
|
|
|
2012-01-13 04:53:09 +03:00
|
|
|
/* Set duration to 0. It is unknown or invalid in a junk frame. */
|
2019-10-06 07:01:20 +02:00
|
|
|
s->duration = 0;
|
|
|
|
*poutbuf_size = fpc->best_header->offset;
|
|
|
|
*poutbuf = flac_fifo_read_wrap(fpc, 0, *poutbuf_size,
|
|
|
|
&fpc->wrap_buf,
|
|
|
|
&fpc->wrap_buf_allocated_size);
|
2010-12-30 06:09:39 +02:00
|
|
|
return buf_size ? (read_end - buf) : (fpc->best_header->offset -
|
2021-12-29 18:02:46 +02:00
|
|
|
flac_fifo_size(&fpc->fifo_buf));
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
|
|
|
if (!buf_size)
|
|
|
|
return get_best_header(fpc, poutbuf, poutbuf_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_error:
|
|
|
|
*poutbuf = NULL;
|
|
|
|
*poutbuf_size = 0;
|
2015-01-17 02:56:03 +02:00
|
|
|
return buf_size ? read_end - buf : 0;
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
|
|
|
|
2013-02-01 12:31:59 +03:00
|
|
|
static av_cold int flac_parse_init(AVCodecParserContext *c)
|
2010-12-07 16:50:50 +02:00
|
|
|
{
|
|
|
|
FLACParseContext *fpc = c->priv_data;
|
2021-12-29 18:02:46 +02:00
|
|
|
int ret;
|
|
|
|
|
2012-01-13 04:53:09 +03:00
|
|
|
fpc->pc = c;
|
2010-12-07 16:50:50 +02:00
|
|
|
/* There will generally be FLAC_MIN_HEADERS buffered in the fifo before
|
2010-12-12 02:52:57 +02:00
|
|
|
it drains. This is allocated early to avoid slow reallocation. */
|
2021-12-29 18:02:46 +02:00
|
|
|
ret = flac_fifo_alloc(&fpc->fifo_buf, (FLAC_MIN_HEADERS + 3) * FLAC_AVG_FRAME_SIZE);
|
|
|
|
if (ret < 0) {
|
2015-01-17 02:40:39 +02:00
|
|
|
av_log(fpc->avctx, AV_LOG_ERROR,
|
|
|
|
"couldn't allocate fifo_buf\n");
|
2013-06-30 19:49:45 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2015-01-17 02:40:39 +02:00
|
|
|
}
|
2010-12-07 16:50:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void flac_parse_close(AVCodecParserContext *c)
|
|
|
|
{
|
|
|
|
FLACParseContext *fpc = c->priv_data;
|
|
|
|
FLACHeaderMarker *curr = fpc->headers, *temp;
|
|
|
|
|
|
|
|
while (curr) {
|
|
|
|
temp = curr->next;
|
|
|
|
av_free(curr);
|
|
|
|
curr = temp;
|
|
|
|
}
|
2019-10-06 07:01:19 +02:00
|
|
|
fpc->headers = NULL;
|
2021-12-29 18:02:46 +02:00
|
|
|
flac_fifo_free(&fpc->fifo_buf);
|
2014-10-14 19:29:49 +03:00
|
|
|
av_freep(&fpc->wrap_buf);
|
2010-12-07 16:50:50 +02:00
|
|
|
}
|
|
|
|
|
2021-03-07 01:20:43 +02:00
|
|
|
const AVCodecParser ff_flac_parser = {
|
2012-08-05 12:11:04 +03:00
|
|
|
.codec_ids = { AV_CODEC_ID_FLAC },
|
2011-11-02 11:34:41 +03:00
|
|
|
.priv_data_size = sizeof(FLACParseContext),
|
|
|
|
.parser_init = flac_parse_init,
|
|
|
|
.parser_parse = flac_parse,
|
|
|
|
.parser_close = flac_parse_close,
|
2010-12-07 16:50:50 +02:00
|
|
|
};
|