45#define FLAC_MAX_SEQUENTIAL_HEADERS 4
47#define FLAC_MIN_HEADERS 10
49#define FLAC_AVG_FRAME_SIZE 8192
52#define FLAC_HEADER_BASE_SCORE 10
53#define FLAC_HEADER_CHANGED_PENALTY 7
54#define FLAC_HEADER_CRC_FAIL_PENALTY 50
55#define FLAC_HEADER_NOT_PENALIZED_YET 100000
56#define FLAC_HEADER_NOT_SCORED_YET -100000
59#define MAX_FRAME_HEADER_SIZE 16
60#define MAX_FRAME_VERIFY_SIZE (MAX_FRAME_HEADER_SIZE + 1)
118 int min_blocksize, max_blocksize;
132 max_blocksize =
AV_RB16(si + 2);
136 if (min_blocksize && max_blocksize && min_blocksize == max_blocksize &&
147 uint8_t subframe_type;
167 if (!(subframe_type == 0 ||
168 subframe_type == 1 ||
169 ((subframe_type >= 8) && (subframe_type <= 12)) ||
170 (subframe_type >= 32))) {
179 if (
f->wptr <=
f->rptr && !
f->empty)
180 return (
f->wptr -
f->buffer) + (
f->end -
f->rptr);
181 return f->wptr -
f->rptr;
203 uint8_t **wrap_buf,
int *allocated_size)
206 uint8_t *start =
f->rptr +
offset;
210 start -=
f->end -
f->buffer;
211 if (
f->end - start >=
len)
218 "couldn't reallocate wrap buffer of size %d",
len);
223 int seg_len =
FFMIN(
f->end - start,
len);
224 memcpy(tmp_buf, start, seg_len);
225 tmp_buf = (uint8_t*)tmp_buf + seg_len;
228 start += seg_len - (
f->end -
f->buffer);
244 uint8_t *start =
f->rptr +
offset;
247 start -=
f->end -
f->buffer;
254 size_t size_old =
f->end -
f->buffer;
255 size_t offset_r =
f->rptr -
f->buffer;
256 size_t offset_w =
f->wptr -
f->buffer;
261 if (size_old > SIZE_MAX -
inc)
263 size_new = size_old +
inc;
271 if (offset_w <= offset_r && !f->empty) {
274 if (
copy < offset_w) {
278 offset_w = size_old +
copy;
282 f->end =
f->buffer + size_new;
283 f->rptr =
f->buffer + offset_r;
284 f->wptr =
f->buffer + offset_w;
323 if (size_cur ==
size)
327 if (
f->rptr >=
f->end)
328 f->rptr -=
f->end -
f->buffer;
333 memset(
f, 0,
sizeof(*
f));
341 f->end =
f->buffer +
size;
351 memset(
f, 0,
sizeof(*
f));
369 while (*end_handle) {
371 end_handle = &(*end_handle)->
next;
375 *end_handle =
av_mallocz(
sizeof(**end_handle));
378 "couldn't allocate FLACHeaderMarker\n");
381 (*end_handle)->fi = fi;
382 (*end_handle)->offset =
offset;
383 (*end_handle)->prev = prev;
395 int buf_size,
int search_start)
397 int size = 0, mod_offset = (buf_size - 1) % 4,
i, j;
400 for (
i = 0;
i < mod_offset;
i++) {
401 if ((
AV_RB16(buf +
i) & 0xFFFE) == 0xFFF8) {
407 for (;
i < buf_size - 1;
i += 4) {
409 if (((x & ~(x + 0x01010101)) & 0x80808080)) {
410 for (j = 0; j < 4; j++) {
411 if ((
AV_RB16(buf +
i + j) & 0xFFFE) == 0xFFF8) {
424 int search_end,
size = 0, read_len,
temp;
430 read_len = search_end - search_start + 1;
433 search_start += read_len - 1;
436 if (search_start != search_end) {
439 wrap[0] = buf[read_len - 1];
441 read_len = search_end - (search_start + 1) + 1;
455 search_start += read_len - 1;
468 int log_level_offset)
474 "sample rate change detected in adjacent frames\n");
476 if (child_fi->
bps != header_fi->
bps) {
479 "bits per sample change detected in adjacent frames\n");
485 "blocking strategy change detected in adjacent frames\n");
490 "number of channels change detected in adjacent frames\n");
498 int log_level_offset)
501 int check_crc, deduction, deduction_expected = 0,
i;
507 (child_fi->frame_or_sample_num
510 int64_t expected_frame_num, expected_sample_num;
516 while (curr != child) {
520 expected_frame_num++;
528 if (expected_frame_num == child_fi->frame_or_sample_num ||
529 expected_sample_num == child_fi->frame_or_sample_num)
530 deduction_expected = deduction ? 0 : 1;
534 "sample/frame number mismatch in adjacent frames\n");
545 check_crc = !deduction && !deduction_expected;
548 check_crc = !deduction && !deduction_expected;
552 if (
check_crc || (deduction && !deduction_expected)) {
557 int inverted_test = 0;
577 while (start->
next != child)
581 header->next->link_penalty[
i-1] >=
598 if (!crc ^ !inverted_test) {
601 "crc check failed from offset %i (frame %"PRId64
") to %i (frame %"PRId64
")\n",
603 child->
offset, child_fi->frame_or_sample_num);
635 header->max_score = base_score;
650 header->best_child = child;
651 header->max_score = base_score + child_score;
664 for (curr = fpc->
headers; curr; curr = curr->
next) {
671 for (curr = last; curr; curr = curr->
prev) {
702 if (
header->fi.is_var_size)
704 else if (
header->best_child)
723 const uint8_t **poutbuf,
int *poutbuf_size,
724 const uint8_t *buf,
int buf_size)
729 const uint8_t *read_end = buf;
730 const uint8_t *read_start = buf;
745 *poutbuf_size = buf_size;
759 for (curr = fpc->
headers; curr != best_child; curr =
temp) {
762 "dropping low score %i frame header from offset %i to %i\n",
773 for (curr = best_child->
next; curr; curr = curr->
next)
819 while ((buf_size && read_end < buf + buf_size &&
822 int start_offset,
ret;
832 read_end = read_end +
FFMIN(buf + buf_size - read_end,
848 read_end - read_start);
861 start_offset =
FFMAX(0, start_offset);
864 if (nb_headers < 0) {
866 "find_new_headers couldn't allocate FLAC header\n");
873 if (read_end < buf + buf_size) {
874 read_start = read_end;
896 read_start = read_end =
NULL;
900 for (curr = fpc->
headers; curr; curr = curr->
next) {
935 return buf_size ? read_end - buf : 0;
949 "couldn't allocate fifo_buf\n");
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define PARSER_FLAG_USE_CODEC_TS
#define PARSER_FLAG_COMPLETE_FRAMES
#define i(width, name, range_min, range_max)
Public header for CRC hash function implementation.
int(* init)(AVBSFContext *ctx)
FLAC (Free Lossless Audio Codec) common stuff.
#define FLAC_STREAMINFO_SIZE
FLAC (Free Lossless Audio Codec) decoder/parser common functions.
static void score_sequences(FLACParseContext *fpc)
static int flac_fifo_alloc(FifoBuffer *f, size_t size)
#define FLAC_HEADER_CHANGED_PENALTY
#define FLAC_MAX_SEQUENTIAL_HEADERS
maximum number of adjacent headers that compare CRCs against each other
static av_cold void flac_parse_close(AVCodecParserContext *c)
static void flac_fifo_drain(FifoBuffer *f, size_t size)
static av_cold int flac_parse_init(AVCodecParserContext *c)
static int find_headers_search_validate(FLACParseContext *fpc, int offset)
static int flac_blocking_strategy_matches(const AVCodecContext *avctx, const FLACFrameInfo *fi)
Reject a variable-blocksize candidate when STREAMINFO declares a fixed blocksize.
static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header)
Score a header.
#define FLAC_HEADER_NOT_SCORED_YET
static size_t flac_fifo_size(const FifoBuffer *f)
static size_t flac_fifo_space(const FifoBuffer *f)
static int check_header_mismatch(FLACParseContext *fpc, FLACHeaderMarker *header, FLACHeaderMarker *child, int log_level_offset)
#define FLAC_HEADER_BASE_SCORE
scoring settings for score_header
#define FLAC_AVG_FRAME_SIZE
estimate for average size of a FLAC frame
static int flac_fifo_write(FifoBuffer *f, const uint8_t *src, size_t size)
#define MAX_FRAME_VERIFY_SIZE
static int flac_fifo_grow(FifoBuffer *f, size_t inc)
#define FLAC_HEADER_NOT_PENALIZED_YET
static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf, FLACFrameInfo *fi)
static int check_header_fi_mismatch(FLACParseContext *fpc, FLACFrameInfo *header_fi, FLACFrameInfo *child_fi, int log_level_offset)
static int find_headers_search(FLACParseContext *fpc, uint8_t *buf, int buf_size, int search_start)
#define MAX_FRAME_HEADER_SIZE
largest possible size of flac header
#define FLAC_HEADER_CRC_FAIL_PENALTY
static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf, int *poutbuf_size)
static uint8_t * flac_fifo_read_wrap(FLACParseContext *fpc, int offset, int len, uint8_t **wrap_buf, int *allocated_size)
Non-destructive fast fifo pointer fetching Returns a pointer from the specified offset.
const FFCodecParser ff_flac_parser
static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
#define FLAC_MIN_HEADERS
minimum number of headers buffered and checked before returning frames
static int find_new_headers(FLACParseContext *fpc, int search_start)
static uint8_t * flac_fifo_read(FifoBuffer *f, int offset, int *len)
Return a pointer in the fifo buffer where the offset starts at until the wrap point or end of request...
static void flac_fifo_free(FifoBuffer *f)
static unsigned int get_bits1(GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
int ff_flac_decode_frame_header(void *logctx, GetBitContext *gb, FLACFrameInfo *fi, int log_level_offset)
Validate and decode a frame header.
void ff_flac_set_channel_layout(AVCodecContext *avctx, int channels)
Macro definitions for various function/variable attributes.
Utility Preprocessor macros.
#define MKTAG(a, b, c, d)
Memory handling functions.
#define PARSER_CODEC_LIST(...)
static int inc(int num, int period)
static const uint8_t header[24]
main external API structure.
int sample_rate
samples per second
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
int duration
Duration of the current frame.
int blocksize
block size of the frame
int channels
number of channels
int samplerate
sample rate
int is_var_size
specifies if the stream uses variable block sizes or a fixed block size; also determines the meaning ...
int64_t frame_or_sample_num
frame number or sample number
uint8_t * wrap_buf
general fifo read buffer when wrapped
FLACHeaderMarker * headers
linked-list that starts at the first CRC-8 verified header within buffer
int last_fi_valid
set if last_fi is valid
FLACFrameInfo last_fi
last decoded frame header info
AVCodecContext * avctx
codec context pointer for logging
FLACHeaderMarker * best_header
highest scoring header within buffer
int nb_headers_buffered
number of headers that are buffered
int end_padded
specifies if fifo_buf's end is padded
FifoBuffer fifo_buf
buffer to store all data until headers can be verified
int best_header_valid
flag set when the parser returns junk; if set return best_header next time
AVCodecParserContext * pc
parent context
int nb_headers_found
number of headers found in the last flac_parse() call
int wrap_buf_allocated_size
actual allocated size of the buffer
static void check_crc(const AVCRC *table_new, const char *name, size_t size, size_t offset)
static void copy(const float *p1, float *p2, const int length)