Go to the documentation of this file.
42 #define FLAC_MAX_SEQUENTIAL_HEADERS 4
44 #define FLAC_MIN_HEADERS 10
46 #define FLAC_AVG_FRAME_SIZE 8192
49 #define FLAC_HEADER_BASE_SCORE 10
50 #define FLAC_HEADER_CHANGED_PENALTY 7
51 #define FLAC_HEADER_CRC_FAIL_PENALTY 50
52 #define FLAC_HEADER_NOT_PENALIZED_YET 100000
53 #define FLAC_HEADER_NOT_SCORED_YET -100000
56 #define MAX_FRAME_HEADER_SIZE 16
57 #define MAX_FRAME_VERIFY_SIZE (MAX_FRAME_HEADER_SIZE + 1)
107 uint8_t subframe_type;
127 if (!(subframe_type == 0 ||
128 subframe_type == 1 ||
129 ((subframe_type >= 8) && (subframe_type <= 12)) ||
130 (subframe_type >= 32))) {
139 if (
f->wptr <=
f->rptr && !
f->empty)
140 return (
f->wptr -
f->buffer) + (
f->end -
f->rptr);
141 return f->wptr -
f->rptr;
163 uint8_t **wrap_buf,
int *allocated_size)
166 uint8_t *start =
f->rptr +
offset;
170 start -=
f->end -
f->buffer;
171 if (
f->end - start >=
len)
178 "couldn't reallocate wrap buffer of size %d",
len);
183 int seg_len =
FFMIN(
f->end - start,
len);
184 memcpy(tmp_buf, start, seg_len);
185 tmp_buf = (uint8_t*)tmp_buf + seg_len;
188 start += seg_len - (
f->end -
f->buffer);
204 uint8_t *start =
f->rptr +
offset;
207 start -=
f->end -
f->buffer;
214 size_t size_old =
f->end -
f->buffer;
215 size_t offset_r =
f->rptr -
f->buffer;
216 size_t offset_w =
f->wptr -
f->buffer;
221 if (size_old > SIZE_MAX - inc)
223 size_new = size_old + inc;
231 if (offset_w <= offset_r && !f->empty) {
232 const size_t copy =
FFMIN(inc, offset_w);
234 if (
copy < offset_w) {
238 offset_w = size_old +
copy;
242 f->end =
f->buffer + size_new;
243 f->rptr =
f->buffer + offset_r;
244 f->wptr =
f->buffer + offset_w;
283 if (size_cur ==
size)
287 if (
f->rptr >=
f->end)
288 f->rptr -=
f->end -
f->buffer;
293 memset(
f, 0,
sizeof(*
f));
301 f->end =
f->buffer +
size;
311 memset(
f, 0,
sizeof(*
f));
328 while (*end_handle) {
329 end_handle = &(*end_handle)->
next;
333 *end_handle =
av_mallocz(
sizeof(**end_handle));
336 "couldn't allocate FLACHeaderMarker\n");
339 (*end_handle)->fi = fi;
340 (*end_handle)->offset =
offset;
352 int buf_size,
int search_start)
354 int size = 0, mod_offset = (buf_size - 1) % 4,
i, j;
357 for (
i = 0;
i < mod_offset;
i++) {
358 if ((
AV_RB16(buf +
i) & 0xFFFE) == 0xFFF8) {
364 for (;
i < buf_size - 1;
i += 4) {
366 if (((x & ~(x + 0x01010101)) & 0x80808080)) {
367 for (j = 0; j < 4; j++) {
368 if ((
AV_RB16(buf +
i + j) & 0xFFFE) == 0xFFF8) {
381 int search_end,
size = 0, read_len,
temp;
387 read_len = search_end - search_start + 1;
390 search_start += read_len - 1;
393 if (search_start != search_end) {
396 wrap[0] = buf[read_len - 1];
398 read_len = search_end - (search_start + 1) + 1;
412 search_start += read_len - 1;
425 int log_level_offset)
431 "sample rate change detected in adjacent frames\n");
433 if (child_fi->
bps != header_fi->
bps) {
436 "bits per sample change detected in adjacent frames\n");
442 "blocking strategy change detected in adjacent frames\n");
447 "number of channels change detected in adjacent frames\n");
455 int log_level_offset)
458 int check_crc, deduction, deduction_expected = 0,
i;
464 (child_fi->frame_or_sample_num
467 int64_t expected_frame_num, expected_sample_num;
473 while (curr != child) {
477 expected_frame_num++;
485 if (expected_frame_num == child_fi->frame_or_sample_num ||
486 expected_sample_num == child_fi->frame_or_sample_num)
487 deduction_expected = deduction ? 0 : 1;
491 "sample/frame number mismatch in adjacent frames\n");
502 check_crc = !deduction && !deduction_expected;
505 check_crc = !deduction && !deduction_expected;
509 if (check_crc || (deduction && !deduction_expected)) {
514 int inverted_test = 0;
532 while (start->
next != child)
536 header->next->link_penalty[
i-1] >=
553 if (!crc ^ !inverted_test) {
556 "crc check failed from offset %i (frame %"PRId64
") to %i (frame %"PRId64
")\n",
558 child->
offset, child_fi->frame_or_sample_num);
590 header->max_score = base_score;
605 header->best_child = child;
606 header->max_score = base_score + child_score;
623 for (curr = fpc->
headers; curr; curr = curr->
next) {
654 if (
header->fi.is_var_size)
656 else if (
header->best_child)
675 const uint8_t **poutbuf,
int *poutbuf_size,
676 const uint8_t *buf,
int buf_size)
681 const uint8_t *read_end = buf;
682 const uint8_t *read_start = buf;
697 *poutbuf_size = buf_size;
711 for (curr = fpc->
headers; curr != best_child; curr =
temp) {
714 "dropping low score %i frame header from offset %i to %i\n",
725 for (curr = best_child->
next; curr; curr = curr->
next)
753 while ((buf_size && read_end < buf + buf_size &&
756 int start_offset,
ret;
766 read_end = read_end +
FFMIN(buf + buf_size - read_end,
782 read_end - read_start);
795 start_offset =
FFMAX(0, start_offset);
798 if (nb_headers < 0) {
800 "find_new_headers couldn't allocate FLAC header\n");
807 if (read_end < buf + buf_size) {
808 read_start = read_end;
830 read_start = read_end =
NULL;
834 for (curr = fpc->
headers; curr; curr = curr->
next) {
869 return buf_size ? read_end - buf : 0;
883 "couldn't allocate fifo_buf\n");
FLACHeaderMarker * headers
linked-list that starts at the first CRC-8 verified header within buffer
#define FLAC_HEADER_BASE_SCORE
scoring settings for score_header
static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf, FLACFrameInfo *fi)
#define AV_LOG_WARNING
Something somehow does not look correct.
static int check_header_fi_mismatch(FLACParseContext *fpc, FLACFrameInfo *header_fi, FLACFrameInfo *child_fi, int log_level_offset)
int nb_headers_buffered
number of headers that are buffered
static int find_new_headers(FLACParseContext *fpc, int search_start)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
int sample_rate
samples per second
static void flac_fifo_free(FifoBuffer *f)
int duration
Duration of the current frame.
uint8_t * wrap_buf
general fifo read buffer when wrapped
int64_t frame_or_sample_num
frame number or sample number
int is_var_size
specifies if the stream uses variable block sizes or a fixed block size; also determines the meaning ...
static void score_sequences(FLACParseContext *fpc)
#define FLAC_HEADER_CHANGED_PENALTY
#define FLAC_MAX_SEQUENTIAL_HEADERS
maximum number of adjacent headers that compare CRCs against each other
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
int wrap_buf_allocated_size
actual allocated size of the buffer
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.
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
const AVCodecParser ff_flac_parser
void ff_flac_set_channel_layout(AVCodecContext *avctx, int channels)
int ff_flac_decode_frame_header(void *logctx, GetBitContext *gb, FLACFrameInfo *fi, int log_level_offset)
Validate and decode a frame header.
int best_header_valid
flag set when the parser returns junk; if set return best_header next time
static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf, int *poutbuf_size)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
FifoBuffer fifo_buf
buffer to store all data until headers can be verified
int channels
number of channels
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.
#define FLAC_AVG_FRAME_SIZE
estimate for average size of a FLAC frame
int blocksize
block size of the frame
static int flac_fifo_alloc(FifoBuffer *f, size_t size)
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...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int last_fi_valid
set if last_fi is valid
AVCodecContext * avctx
codec context pointer for logging
static int find_headers_search_validate(FLACParseContext *fpc, int offset)
AVCodecParserContext * pc
parent context
static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header)
Score a header.
static void flac_fifo_drain(FifoBuffer *f, size_t size)
static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
static int find_headers_search(FLACParseContext *fpc, uint8_t *buf, int buf_size, int search_start)
static unsigned int get_bits1(GetBitContext *s)
#define FLAC_HEADER_CRC_FAIL_PENALTY
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
#define FLAC_HEADER_NOT_SCORED_YET
static int flac_fifo_write(FifoBuffer *f, const uint8_t *src, size_t size)
static void copy(const float *p1, float *p2, const int length)
static int flac_fifo_grow(FifoBuffer *f, size_t inc)
static const uint8_t header[24]
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
static av_cold int flac_parse_init(AVCodecParserContext *c)
FLACHeaderMarker * best_header
highest scoring header within buffer
static size_t flac_fifo_space(const FifoBuffer *f)
#define PARSER_FLAG_COMPLETE_FRAMES
#define i(width, name, range_min, range_max)
int end_padded
specifies if fifo_buf's end is padded
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
#define MAX_FRAME_VERIFY_SIZE
static size_t flac_fifo_size(const FifoBuffer *f)
static int check_header_mismatch(FLACParseContext *fpc, FLACHeaderMarker *header, FLACHeaderMarker *child, int log_level_offset)
int samplerate
sample rate
int nb_headers_found
number of headers found in the last flac_parse() call
#define MAX_FRAME_HEADER_SIZE
largest possible size of flac header
#define AV_INPUT_BUFFER_PADDING_SIZE
main external API structure.
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
#define PARSER_FLAG_USE_CODEC_TS
#define FLAC_MIN_HEADERS
minimum number of headers buffered and checked before returning frames
#define FLAC_HEADER_NOT_PENALIZED_YET
FLACFrameInfo last_fi
last decoded frame header info
static void flac_parse_close(AVCodecParserContext *c)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.