FFmpeg
Data Structures | Macros | Functions | Variables
flac_parser.c File Reference
#include "libavutil/attributes.h"
#include "libavutil/crc.h"
#include "bytestream.h"
#include "parser.h"
#include "flac.h"

Go to the source code of this file.

Data Structures

struct  FifoBuffer
 
struct  FLACHeaderMarker
 
struct  FLACParseContext
 

Macros

#define FLAC_MAX_SEQUENTIAL_HEADERS   4
 maximum number of adjacent headers that compare CRCs against each other
More...
 
#define FLAC_MIN_HEADERS   10
 minimum number of headers buffered and checked before returning frames
More...
 
#define FLAC_AVG_FRAME_SIZE   8192
 estimate for average size of a FLAC frame
More...
 
#define FLAC_HEADER_BASE_SCORE   10
 scoring settings for score_header More...
 
#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
 
#define MAX_FRAME_HEADER_SIZE   16
 largest possible size of flac header More...
 
#define MAX_FRAME_VERIFY_SIZE   (MAX_FRAME_HEADER_SIZE + 1)
 

Functions

static int frame_header_is_valid (AVCodecContext *avctx, const uint8_t *buf, FLACFrameInfo *fi)
 
static size_t flac_fifo_size (const FifoBuffer *f)
 
static size_t flac_fifo_space (const FifoBuffer *f)
 
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. More...
 
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. More...
 
static int flac_fifo_grow (FifoBuffer *f, size_t inc)
 
static int flac_fifo_write (FifoBuffer *f, const uint8_t *src, size_t size)
 
static void flac_fifo_drain (FifoBuffer *f, size_t size)
 
static int flac_fifo_alloc (FifoBuffer *f, size_t size)
 
static void flac_fifo_free (FifoBuffer *f)
 
static int find_headers_search_validate (FLACParseContext *fpc, int offset)
 
static int find_headers_search (FLACParseContext *fpc, uint8_t *buf, int buf_size, int search_start)
 
static int find_new_headers (FLACParseContext *fpc, int search_start)
 
static int check_header_fi_mismatch (FLACParseContext *fpc, FLACFrameInfo *header_fi, FLACFrameInfo *child_fi, int log_level_offset)
 
static int check_header_mismatch (FLACParseContext *fpc, FLACHeaderMarker *header, FLACHeaderMarker *child, int log_level_offset)
 
static int score_header (FLACParseContext *fpc, FLACHeaderMarker *header)
 Score a header. More...
 
static void score_sequences (FLACParseContext *fpc)
 
static int get_best_header (FLACParseContext *fpc, const uint8_t **poutbuf, int *poutbuf_size)
 
static int flac_parse (AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
 
static av_cold int flac_parse_init (AVCodecParserContext *c)
 
static void flac_parse_close (AVCodecParserContext *c)
 

Variables

const AVCodecParser ff_flac_parser
 

Detailed Description

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. Headers are scored by FLAC_HEADER_BASE_SCORE plus the max of its crc-verified children, penalized by changes in sample rate, frame number, etc. The parser returns the frame with the highest score.

Definition in file flac_parser.c.

Macro Definition Documentation

◆ FLAC_MAX_SEQUENTIAL_HEADERS

#define FLAC_MAX_SEQUENTIAL_HEADERS   4

maximum number of adjacent headers that compare CRCs against each other

Definition at line 42 of file flac_parser.c.

◆ FLAC_MIN_HEADERS

#define FLAC_MIN_HEADERS   10

minimum number of headers buffered and checked before returning frames

Definition at line 44 of file flac_parser.c.

◆ FLAC_AVG_FRAME_SIZE

#define FLAC_AVG_FRAME_SIZE   8192

estimate for average size of a FLAC frame

Definition at line 46 of file flac_parser.c.

◆ FLAC_HEADER_BASE_SCORE

#define FLAC_HEADER_BASE_SCORE   10

scoring settings for score_header

Definition at line 49 of file flac_parser.c.

◆ FLAC_HEADER_CHANGED_PENALTY

#define FLAC_HEADER_CHANGED_PENALTY   7

Definition at line 50 of file flac_parser.c.

◆ FLAC_HEADER_CRC_FAIL_PENALTY

#define FLAC_HEADER_CRC_FAIL_PENALTY   50

Definition at line 51 of file flac_parser.c.

◆ FLAC_HEADER_NOT_PENALIZED_YET

#define FLAC_HEADER_NOT_PENALIZED_YET   100000

Definition at line 52 of file flac_parser.c.

◆ FLAC_HEADER_NOT_SCORED_YET

#define FLAC_HEADER_NOT_SCORED_YET   -100000

Definition at line 53 of file flac_parser.c.

◆ MAX_FRAME_HEADER_SIZE

#define MAX_FRAME_HEADER_SIZE   16

largest possible size of flac header

Definition at line 56 of file flac_parser.c.

◆ MAX_FRAME_VERIFY_SIZE

#define MAX_FRAME_VERIFY_SIZE   (MAX_FRAME_HEADER_SIZE + 1)

Definition at line 57 of file flac_parser.c.

Function Documentation

◆ frame_header_is_valid()

static int frame_header_is_valid ( AVCodecContext avctx,
const uint8_t *  buf,
FLACFrameInfo fi 
)
static

Definition at line 103 of file flac_parser.c.

Referenced by find_headers_search_validate(), and flac_parse().

◆ flac_fifo_size()

static size_t flac_fifo_size ( const FifoBuffer f)
static

◆ flac_fifo_space()

static size_t flac_fifo_space ( const FifoBuffer f)
static

Definition at line 144 of file flac_parser.c.

Referenced by flac_fifo_write(), and flac_parse().

◆ flac_fifo_read_wrap()

static uint8_t* flac_fifo_read_wrap ( FLACParseContext fpc,
int  offset,
int  len,
uint8_t **  wrap_buf,
int allocated_size 
)
static

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.

Definition at line 162 of file flac_parser.c.

Referenced by find_headers_search_validate(), flac_parse(), and get_best_header().

◆ flac_fifo_read()

static uint8_t* flac_fifo_read ( FifoBuffer f,
int  offset,
int len 
)
static

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.

Definition at line 202 of file flac_parser.c.

Referenced by check_header_mismatch(), and find_new_headers().

◆ flac_fifo_grow()

static int flac_fifo_grow ( FifoBuffer f,
size_t  inc 
)
static

Definition at line 212 of file flac_parser.c.

Referenced by flac_fifo_write().

◆ flac_fifo_write()

static int flac_fifo_write ( FifoBuffer f,
const uint8_t *  src,
size_t  size 
)
static

Definition at line 249 of file flac_parser.c.

Referenced by flac_parse().

◆ flac_fifo_drain()

static void flac_fifo_drain ( FifoBuffer f,
size_t  size 
)
static

Definition at line 278 of file flac_parser.c.

Referenced by flac_parse().

◆ flac_fifo_alloc()

static int flac_fifo_alloc ( FifoBuffer f,
size_t  size 
)
static

Definition at line 291 of file flac_parser.c.

Referenced by flac_parse_init().

◆ flac_fifo_free()

static void flac_fifo_free ( FifoBuffer f)
static

Definition at line 308 of file flac_parser.c.

Referenced by flac_parse_close().

◆ find_headers_search_validate()

static int find_headers_search_validate ( FLACParseContext fpc,
int  offset 
)
static

Definition at line 314 of file flac_parser.c.

Referenced by find_headers_search(), and find_new_headers().

◆ find_headers_search()

static int find_headers_search ( FLACParseContext fpc,
uint8_t *  buf,
int  buf_size,
int  search_start 
)
static

Definition at line 351 of file flac_parser.c.

Referenced by find_new_headers().

◆ find_new_headers()

static int find_new_headers ( FLACParseContext fpc,
int  search_start 
)
static

Definition at line 378 of file flac_parser.c.

Referenced by flac_parse().

◆ check_header_fi_mismatch()

static int check_header_fi_mismatch ( FLACParseContext fpc,
FLACFrameInfo header_fi,
FLACFrameInfo child_fi,
int  log_level_offset 
)
static

Definition at line 422 of file flac_parser.c.

Referenced by check_header_mismatch(), and score_header().

◆ check_header_mismatch()

static int check_header_mismatch ( FLACParseContext fpc,
FLACHeaderMarker header,
FLACHeaderMarker child,
int  log_level_offset 
)
static

Definition at line 452 of file flac_parser.c.

Referenced by get_best_header(), and score_header().

◆ score_header()

static int score_header ( FLACParseContext fpc,
FLACHeaderMarker header 
)
static

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.

Definition at line 560 of file flac_parser.c.

Referenced by score_sequences().

◆ score_sequences()

static void score_sequences ( FLACParseContext fpc)
static

Definition at line 600 of file flac_parser.c.

Referenced by flac_parse().

◆ get_best_header()

static int get_best_header ( FLACParseContext fpc,
const uint8_t **  poutbuf,
int poutbuf_size 
)
static

Definition at line 617 of file flac_parser.c.

Referenced by flac_parse().

◆ flac_parse()

static int flac_parse ( AVCodecParserContext s,
AVCodecContext avctx,
const uint8_t **  poutbuf,
int poutbuf_size,
const uint8_t *  buf,
int  buf_size 
)
static

Definition at line 657 of file flac_parser.c.

◆ flac_parse_init()

static av_cold int flac_parse_init ( AVCodecParserContext c)
static

Definition at line 855 of file flac_parser.c.

◆ flac_parse_close()

static void flac_parse_close ( AVCodecParserContext c)
static

Definition at line 872 of file flac_parser.c.

Variable Documentation

◆ ff_flac_parser

const AVCodecParser ff_flac_parser
Initial value:
= {
.codec_ids = { AV_CODEC_ID_FLAC },
.priv_data_size = sizeof(FLACParseContext),
.parser_init = flac_parse_init,
.parser_parse = flac_parse,
.parser_close = flac_parse_close,
}

Definition at line 887 of file flac_parser.c.

FLACParseContext
Definition: flac_parser.c:83
AV_CODEC_ID_FLAC
@ AV_CODEC_ID_FLAC
Definition: codec_id.h:441
flac_parse
static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: flac_parser.c:657
flac_parse_init
static av_cold int flac_parse_init(AVCodecParserContext *c)
Definition: flac_parser.c:855
flac_parse_close
static void flac_parse_close(AVCodecParserContext *c)
Definition: flac_parser.c:872