libavcodec/flacenc.c File Reference
#include "libavutil/avassert.h"
#include "libavutil/crc.h"
#include "libavutil/intmath.h"
#include "libavutil/md5.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "dsputil.h"
#include "put_bits.h"
#include "golomb.h"
#include "internal.h"
#include "lpc.h"
#include "flac.h"
#include "flacdata.h"
#include "flacdsp.h"
Include dependency graph for libavcodec/flacenc.c:

Go to the source code of this file.

Data Structures

struct  CompressionOptions
 
struct  RiceContext
 
struct  FlacSubframe
 
struct  FlacFrame
 
struct  FlacEncodeContext
 

Macros

#define FLAC_SUBFRAME_CONSTANT   0
 
#define FLAC_SUBFRAME_VERBATIM   1
 
#define FLAC_SUBFRAME_FIXED   8
 
#define FLAC_SUBFRAME_LPC   32
 
#define MAX_FIXED_ORDER   4
 
#define MAX_PARTITION_ORDER   8
 
#define MAX_PARTITIONS   (1 << MAX_PARTITION_ORDER)
 
#define MAX_LPC_PRECISION   15
 
#define MAX_LPC_SHIFT   15
 
#define COPY_SAMPLES(bits)
 
#define rice_encode_count(sum, n, k)   (((n)*((k)+1))+((sum-(n>>1))>>(k)))
 
#define FLAGS   AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
 

Typedefs

typedef struct CompressionOptions CompressionOptions
 
typedef struct RiceContext RiceContext
 
typedef struct FlacSubframe FlacSubframe
 
typedef struct FlacFrame FlacFrame
 
typedef struct FlacEncodeContext FlacEncodeContext
 

Enumerations

enum  CodingMode { CODING_MODE_RICE = 4, CODING_MODE_RICE2 = 5 }
 

Functions

static void write_streaminfo (FlacEncodeContext *s, uint8_t *header)
 Write streaminfo metadata block to byte array. More...
 
static int select_blocksize (int samplerate, int block_time_ms)
 Set blocksize based on samplerate. More...
 
static av_cold void dprint_compression_options (FlacEncodeContext *s)
 
static av_cold int flac_encode_init (AVCodecContext *avctx)
 
static void init_frame (FlacEncodeContext *s, int nb_samples)
 
static void copy_samples (FlacEncodeContext *s, const void *samples)
 Copy channel-interleaved input samples into separate subframes. More...
 
static uint64_t rice_count_exact (int32_t *res, int n, int k)
 
static uint64_t subframe_count_exact (FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
 
static int find_optimal_param (uint64_t sum, int n, int max_param)
 Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0. More...
 
static uint64_t calc_optimal_rice_params (RiceContext *rc, int porder, uint64_t *sums, int n, int pred_order)
 
static void calc_sums (int pmin, int pmax, uint32_t *data, int n, int pred_order, uint64_t sums[][MAX_PARTITIONS])
 
static uint64_t calc_rice_params (RiceContext *rc, int pmin, int pmax, int32_t *data, int n, int pred_order)
 
static int get_max_p_order (int max_porder, int n, int order)
 
static uint64_t find_subframe_rice_params (FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
 
static void encode_residual_fixed (int32_t *res, const int32_t *smp, int n, int order)
 
static int encode_residual_ch (FlacEncodeContext *s, int ch)
 
static int count_frame_header (FlacEncodeContext *s)
 
static int encode_frame (FlacEncodeContext *s)
 
static void remove_wasted_bits (FlacEncodeContext *s)
 
static int estimate_stereo_mode (int32_t *left_ch, int32_t *right_ch, int n, int max_rice_param)
 
static void channel_decorrelation (FlacEncodeContext *s)
 Perform stereo channel decorrelation. More...
 
static void write_utf8 (PutBitContext *pb, uint32_t val)
 
static void write_frame_header (FlacEncodeContext *s)
 
static void write_subframes (FlacEncodeContext *s)
 
static void write_frame_footer (FlacEncodeContext *s)
 
static int write_frame (FlacEncodeContext *s, AVPacket *avpkt)
 
static int update_md5_sum (FlacEncodeContext *s, const void *samples)
 
static int flac_encode_frame (AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
 
static av_cold int flac_encode_close (AVCodecContext *avctx)
 

Variables

static const AVOption options []
 
static const AVClass flac_encoder_class
 
AVCodec ff_flac_encoder
 

Macro Definition Documentation

#define COPY_SAMPLES (   bits)
Value:
do { \
const int ## bits ## _t *samples0 = samples; \
frame = &s->frame; \
for (i = 0, j = 0; i < frame->blocksize; i++) \
for (ch = 0; ch < s->channels; ch++, j++) \
frame->subframes[ch].samples[i] = samples0[j] >> shift; \
} while (0)
const char * s
Definition: avisynth_c.h:668
static int shift(int a, int b)
Definition: sonic.c:86
uint8_t bits
Definition: crc.c:216
frame
Definition: stft.m:14
synthesis window for stochastic i
Filter the word “frame” indicates either a video frame or a group of audio samples
for(j=16;j >0;--j)

Referenced by copy_samples().

#define FLAC_SUBFRAME_CONSTANT   0

Definition at line 37 of file libavcodec/flacenc.c.

Referenced by encode_residual_ch(), subframe_count_exact(), and write_subframes().

#define FLAC_SUBFRAME_FIXED   8

Definition at line 39 of file libavcodec/flacenc.c.

Referenced by encode_residual_ch().

#define FLAC_SUBFRAME_LPC   32
#define FLAC_SUBFRAME_VERBATIM   1

Definition at line 38 of file libavcodec/flacenc.c.

Referenced by encode_residual_ch(), subframe_count_exact(), and write_subframes().

Definition at line 1310 of file libavcodec/flacenc.c.

#define MAX_FIXED_ORDER   4

Definition at line 42 of file libavcodec/flacenc.c.

Referenced by encode_residual_ch(), and flac_encode_init().

#define MAX_LPC_PRECISION   15

Definition at line 45 of file libavcodec/flacenc.c.

#define MAX_LPC_SHIFT   15

Definition at line 46 of file libavcodec/flacenc.c.

Referenced by encode_residual_ch().

#define MAX_PARTITION_ORDER   8

Definition at line 43 of file libavcodec/flacenc.c.

Referenced by calc_rice_params().

#define MAX_PARTITIONS   (1 << MAX_PARTITION_ORDER)

Definition at line 44 of file libavcodec/flacenc.c.

Referenced by calc_rice_params().

#define rice_encode_count (   sum,
  n,
  k 
)    (((n)*((k)+1))+((sum-(n>>1))>>(k)))

Definition at line 565 of file libavcodec/flacenc.c.

Referenced by calc_optimal_rice_params(), and estimate_stereo_mode().

Typedef Documentation

typedef struct FlacFrame FlacFrame
typedef struct FlacSubframe FlacSubframe
typedef struct RiceContext RiceContext

Enumeration Type Documentation

enum CodingMode
Enumerator
CODING_MODE_RICE 
CODING_MODE_RICE2 

Definition at line 48 of file libavcodec/flacenc.c.

Function Documentation

static uint64_t calc_optimal_rice_params ( RiceContext rc,
int  porder,
uint64_t *  sums,
int  n,
int  pred_order 
)
static

Definition at line 583 of file libavcodec/flacenc.c.

Referenced by calc_rice_params().

static uint64_t calc_rice_params ( RiceContext rc,
int  pmin,
int  pmax,
int32_t data,
int  n,
int  pred_order 
)
static

Definition at line 636 of file libavcodec/flacenc.c.

Referenced by find_subframe_rice_params().

static void calc_sums ( int  pmin,
int  pmax,
uint32_t *  data,
int  n,
int  pred_order,
uint64_t  sums[][MAX_PARTITIONS] 
)
static

Definition at line 609 of file libavcodec/flacenc.c.

Referenced by calc_rice_params().

static void channel_decorrelation ( FlacEncodeContext s)
static

Perform stereo channel decorrelation.

Definition at line 1021 of file libavcodec/flacenc.c.

Referenced by flac_encode_frame().

static void copy_samples ( FlacEncodeContext s,
const void samples 
)
static

Copy channel-interleaved input samples into separate subframes.

Definition at line 482 of file libavcodec/flacenc.c.

Referenced by flac_encode_frame().

static int count_frame_header ( FlacEncodeContext s)
static

Definition at line 889 of file libavcodec/flacenc.c.

Referenced by encode_frame().

static av_cold void dprint_compression_options ( FlacEncodeContext s)
static

Definition at line 170 of file libavcodec/flacenc.c.

Referenced by flac_encode_init().

static int encode_frame ( FlacEncodeContext s)
static

Definition at line 925 of file libavcodec/flacenc.c.

Referenced by flac_encode_frame().

static int encode_residual_ch ( FlacEncodeContext s,
int  ch 
)
static

Definition at line 750 of file libavcodec/flacenc.c.

Referenced by encode_frame().

static void encode_residual_fixed ( int32_t res,
const int32_t smp,
int  n,
int  order 
)
static

Definition at line 699 of file libavcodec/flacenc.c.

Referenced by encode_residual_ch().

static int estimate_stereo_mode ( int32_t left_ch,
int32_t right_ch,
int  n,
int  max_rice_param 
)
static

Definition at line 977 of file libavcodec/flacenc.c.

Referenced by channel_decorrelation().

static int find_optimal_param ( uint64_t  sum,
int  n,
int  max_param 
)
static

Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.

Definition at line 570 of file libavcodec/flacenc.c.

Referenced by calc_optimal_rice_params(), and estimate_stereo_mode().

static uint64_t find_subframe_rice_params ( FlacEncodeContext s,
FlacSubframe sub,
int  pred_order 
)
static

Definition at line 682 of file libavcodec/flacenc.c.

Referenced by encode_residual_ch().

static av_cold int flac_encode_close ( AVCodecContext avctx)
static

Definition at line 1297 of file libavcodec/flacenc.c.

static int flac_encode_frame ( AVCodecContext avctx,
AVPacket avpkt,
const AVFrame frame,
int *  got_packet_ptr 
)
static

Definition at line 1229 of file libavcodec/flacenc.c.

static av_cold int flac_encode_init ( AVCodecContext avctx)
static

Definition at line 228 of file libavcodec/flacenc.c.

static int get_max_p_order ( int  max_porder,
int  n,
int  order 
)
static

Definition at line 673 of file libavcodec/flacenc.c.

Referenced by find_subframe_rice_params().

static void init_frame ( FlacEncodeContext s,
int  nb_samples 
)
static

Definition at line 437 of file libavcodec/flacenc.c.

Referenced by decode_frame(), and flac_encode_frame().

static void remove_wasted_bits ( FlacEncodeContext s)
static

Definition at line 945 of file libavcodec/flacenc.c.

Referenced by flac_encode_frame().

static uint64_t rice_count_exact ( int32_t res,
int  n,
int  k 
)
static

Definition at line 504 of file libavcodec/flacenc.c.

Referenced by subframe_count_exact().

static int select_blocksize ( int  samplerate,
int  block_time_ms 
)
static

Set blocksize based on samplerate.

Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds.

Definition at line 151 of file libavcodec/flacenc.c.

Referenced by flac_encode_init().

static uint64_t subframe_count_exact ( FlacEncodeContext s,
FlacSubframe sub,
int  pred_order 
)
static

Definition at line 518 of file libavcodec/flacenc.c.

Referenced by encode_residual_ch().

static int update_md5_sum ( FlacEncodeContext s,
const void samples 
)
static

Definition at line 1191 of file libavcodec/flacenc.c.

Referenced by flac_encode_frame().

static int write_frame ( FlacEncodeContext s,
AVPacket avpkt 
)
static

Definition at line 1181 of file libavcodec/flacenc.c.

Referenced by flac_encode_frame().

static void write_frame_footer ( FlacEncodeContext s)
static

Definition at line 1170 of file libavcodec/flacenc.c.

Referenced by write_frame().

static void write_frame_header ( FlacEncodeContext s)
static

Definition at line 1073 of file libavcodec/flacenc.c.

Referenced by write_frame().

static void write_streaminfo ( FlacEncodeContext s,
uint8_t header 
)
static

Write streaminfo metadata block to byte array.

Definition at line 124 of file libavcodec/flacenc.c.

Referenced by flac_encode_frame(), and flac_encode_init().

static void write_subframes ( FlacEncodeContext s)
static

Definition at line 1110 of file libavcodec/flacenc.c.

Referenced by write_frame().

static void write_utf8 ( PutBitContext pb,
uint32_t  val 
)
static

Definition at line 1066 of file libavcodec/flacenc.c.

Referenced by write_frame_header().

Variable Documentation

AVCodec ff_flac_encoder
Initial value:
= {
.name = "flac",
.priv_data_size = sizeof(FlacEncodeContext),
.encode2 = flac_encode_frame,
.long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
.priv_class = &flac_encoder_class,
}
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define CODEC_CAP_LOSSLESS
Codec is lossless.
static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
signed 16 bits
Definition: samplefmt.h:52
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
signed 32 bits
Definition: samplefmt.h:53
struct FlacEncodeContext FlacEncodeContext
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
static av_cold int flac_encode_close(AVCodecContext *avctx)
static const AVClass flac_encoder_class
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
static av_cold int flac_encode_init(AVCodecContext *avctx)

Definition at line 1344 of file libavcodec/flacenc.c.

const AVClass flac_encoder_class
static
Initial value:
= {
"FLAC encoder",
}
av_default_item_name
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static const AVOption options[]

Definition at line 1337 of file libavcodec/flacenc.c.

const AVOption options[]
static
Initial value:
= {
{ "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.i64 = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
{ "lpc_type", "LPC algorithm", offsetof(FlacEncodeContext, options.lpc_type), AV_OPT_TYPE_INT, {.i64 = FF_LPC_TYPE_DEFAULT }, FF_LPC_TYPE_DEFAULT, FF_LPC_TYPE_NB-1, FLAGS, "lpc_type" },
{ "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
{ "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
{ "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
{ "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
{ "lpc_passes", "Number of passes to use for Cholesky factorization during LPC analysis", offsetof(FlacEncodeContext, options.lpc_passes), AV_OPT_TYPE_INT, {.i64 = 2 }, 1, INT_MAX, FLAGS },
{ "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
{ "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
{ "prediction_order_method", "Search method for selecting prediction order", offsetof(FlacEncodeContext, options.prediction_order_method), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, ORDER_METHOD_LOG, FLAGS, "predm" },
{ "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, "predm" },
{ "2level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
{ "4level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
{ "8level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
{ "search", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
{ "log", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, "predm" },
{ "ch_mode", "Stereo decorrelation mode", offsetof(FlacEncodeContext, options.ch_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, FLAC_CHMODE_MID_SIDE, FLAGS, "ch_mode" },
{ "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
{ "indep", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_INDEPENDENT }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
{ "left_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_LEFT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
{ "right_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_RIGHT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
{ "mid_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_MID_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
{ NULL },
}
#define ORDER_METHOD_SEARCH
Definition: lpc.h:32
#define ORDER_METHOD_8LEVEL
Definition: lpc.h:31
#define MAX_PARTITION_ORDER
#define ORDER_METHOD_LOG
Definition: lpc.h:33
do not use LPC prediction or use all zero coefficients
Definition: lpc.h:43
#define ORDER_METHOD_4LEVEL
Definition: lpc.h:30
#define ORDER_METHOD_2LEVEL
Definition: lpc.h:29
#define FLAGS
Not part of ABI.
Definition: lpc.h:47
static const AVOption options[]
NULL
Definition: eval.c:55
Levinson-Durbin recursion.
Definition: lpc.h:45
#define ORDER_METHOD_EST
Definition: lpc.h:28
use the codec default LPC type
Definition: lpc.h:42
#define MAX_LPC_PRECISION
Cholesky factorization.
Definition: lpc.h:46
fixed LPC coefficients
Definition: lpc.h:44

Definition at line 1311 of file libavcodec/flacenc.c.