42 #define FLAC_MAX_SEQUENTIAL_HEADERS 3
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
112 uint8_t** wrap_buf,
int* allocated_size)
115 uint8_t *start = f->
rptr + offset;
120 if (f->
end - start >= len)
127 "couldn't reallocate wrap buffer of size %d", len);
132 int seg_len =
FFMIN(f->
end - start, len);
133 memcpy(tmp_buf, start, seg_len);
134 tmp_buf = (uint8_t*)tmp_buf + seg_len;
154 uint8_t *start = f->
rptr + offset;
176 while (*end_handle) {
177 end_handle = &(*end_handle)->
next;
184 "couldn't allocate FLACHeaderMarker\n");
187 (*end_handle)->fi = fi;
188 (*end_handle)->offset = offset;
189 (*end_handle)->link_penalty =
av_malloc(
sizeof(
int) *
204 int size = 0, mod_offset = (buf_size - 1) % 4, i, j;
207 for (i = 0; i < mod_offset; i++) {
208 if ((
AV_RB16(buf + i) & 0xFFFE) == 0xFFF8)
212 for (; i < buf_size - 1; i += 4) {
214 if (((x & ~(x + 0x01010101)) & 0x80808080)) {
215 for (j = 0; j < 4; j++) {
216 if ((
AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8)
227 int search_end,
size = 0, read_len, temp;
233 read_len = search_end - search_start + 1;
236 search_start += read_len - 1;
239 if (search_start != search_end) {
242 wrap[0] = buf[read_len - 1];
243 read_len = search_end - search_start + 1;
249 if ((
AV_RB16(wrap) & 0xFFFE) == 0xFFF8) {
251 size =
FFMAX(size, temp);
257 size =
FFMAX(size, temp);
258 search_start += read_len - 1;
271 int log_level_offset)
274 int deduction = 0, deduction_expected = 0, i;
275 if (child_fi->samplerate != header_fi->samplerate) {
278 "sample rate change detected in adjacent frames\n");
280 if (child_fi->bps != header_fi->bps) {
283 "bits per sample change detected in adjacent frames\n");
285 if (child_fi->is_var_size != header_fi->
is_var_size) {
289 "blocking strategy change detected in adjacent frames\n");
291 if (child_fi->channels != header_fi->channels) {
294 "number of channels change detected in adjacent frames\n");
299 (child_fi->frame_or_sample_num
302 int expected_frame_num, expected_sample_num;
308 while (curr != child) {
312 expected_frame_num++;
320 if (expected_frame_num == child_fi->frame_or_sample_num ||
321 expected_sample_num == child_fi->frame_or_sample_num)
322 deduction_expected = deduction ? 0 : 1;
326 "sample/frame number mismatch in adjacent frames\n");
330 if (deduction && !deduction_expected) {
335 int inverted_test = 0;
353 while (start->
next != child)
374 if (!crc ^ !inverted_test) {
377 "crc check failed from offset %i (frame %"PRId64
") to %i (frame %"PRId64
")\n",
379 child->
offset, child_fi->frame_or_sample_num);
407 child = header->
next;
437 for (curr = fpc->
headers; curr; curr = curr->
next) {
475 const uint8_t **poutbuf,
int *poutbuf_size,
476 const uint8_t *buf,
int buf_size)
481 const uint8_t *read_end = buf;
482 const uint8_t *read_start = buf;
489 *poutbuf_size = buf_size;
503 for (curr = fpc->
headers; curr != best_child; curr = temp) {
506 "dropping low score %i frame header from offset %i to %i\n",
518 for (curr = best_child->
next; curr; curr = curr->
next)
544 while ((buf && read_end < buf + buf_size &&
558 read_end = read_end +
FFMIN(buf + buf_size - read_end,
566 "couldn't reallocate buffer of size %td\n",
573 read_end - read_start,
NULL);
576 memset(pad, 0,
sizeof(pad));
583 start_offset =
FFMAX(0, start_offset);
586 if (nb_headers < 0) {
588 "find_new_headers couldn't allocate FLAC header\n");
595 if (buf && read_end < buf + buf_size) {
596 read_start = read_end;
617 read_start = read_end =
NULL;
649 return read_end - buf;
static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf, FLACFrameInfo *fi)
struct FLACHeaderMarker FLACHeaderMarker
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static void score_sequences(FLACParseContext *fpc)
AV_WL32 AV_WL24 AV_WL16 AV_WB32 AV_WB24 AV_RB16
AV_WL32 AV_WL24 AV_WL16 AV_RB32
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
int av_fifo_size(AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
int64_t frame_or_sample_num
frame number or sample number
#define AV_LOG_WARNING
Something somehow does not look correct.
struct FLACParseContext FLACParseContext
AVFifoBuffer * fifo_buf
buffer to store all data until headers can be verified
#define FLAC_HEADER_CRC_FAIL_PENALTY
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
FLACCOMMONINFO int blocksize
block size of the frame
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
AVCodecContext * avctx
codec context pointer for logging
int best_header_valid
flag set when the parser returns junk; if set return best_header next time
#define FLAC_AVG_FRAME_SIZE
estimate for average size of a FLAC frame
int nb_headers_found
number of headers found in the last flac_parse() call
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
int wrap_buf_allocated_size
actual allocated size of the buffer
#define FLAC_HEADER_NOT_SCORED_YET
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf, int *poutbuf_size)
#define FLAC_HEADER_CHANGED_PENALTY
static void flac_parse_close(AVCodecParserContext *c)
#define FLAC_MAX_SEQUENTIAL_HEADERS
maximum number of adjacent headers that compare CRCs against each other
static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
void av_log(void *avcl, int level, const char *fmt,...)
int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, FLACFrameInfo *fi, int log_level_offset)
Validate and decode a frame header.
int end_padded
specifies if fifo_buf's end is padded
FLACHeaderMarker * headers
linked-list that starts at the first CRC-8 verified header within buffer
#define FLAC_MIN_HEADERS
minimum number of headers buffered and checked before returning frames
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.
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
int frame_size
Samples per packet, initialized when calling 'init'.
static int find_headers_search_validate(FLACParseContext *fpc, int offset)
uint8_t * wrap_buf
general fifo read buffer when wrapped
int sample_rate
samples per second
#define FLAC_HEADER_NOT_PENALIZED_YET
main external API structure.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
a very simple circular buffer FIFO implementation
static int find_new_headers(FLACParseContext *fpc, int search_start)
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header)
Score a header.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
int nb_headers_buffered
number of headers that are buffered
#define MAX_FRAME_HEADER_SIZE
largest possible size of flac header
FLACHeaderMarker * best_header
highest scoring header within buffer
AVCodecParser ff_flac_parser
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static int find_headers_search(FLACParseContext *fpc, uint8_t *buf, int buf_size, int search_start)
#define PARSER_FLAG_COMPLETE_FRAMES
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
int channels
number of audio channels
#define FLAC_HEADER_BASE_SCORE
scoring settings for score_header
int is_var_size
specifies if the stream uses variable block sizes or a fixed block size; also determines the meaning ...
void av_fifo_drain(AVFifoBuffer *f, int size)
Discard data from the FIFO.
static uint8_t * flac_fifo_read(FLACParseContext *fpc, 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 int flac_parse_init(AVCodecParserContext *c)
static int check_header_mismatch(FLACParseContext *fpc, FLACHeaderMarker *header, FLACHeaderMarker *child, int log_level_offset)