32 #define FLAC_SUBFRAME_CONSTANT 0
33 #define FLAC_SUBFRAME_VERBATIM 1
34 #define FLAC_SUBFRAME_FIXED 8
35 #define FLAC_SUBFRAME_LPC 32
37 #define MAX_FIXED_ORDER 4
38 #define MAX_PARTITION_ORDER 8
39 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
40 #define MAX_LPC_PRECISION 15
41 #define MAX_LPC_SHIFT 15
42 #define MAX_RICE_PARAM 14
126 memcpy(&header[18], s->
md5sum, 16);
140 assert(samplerate > 0);
142 target = (samplerate * block_time_ms) / 1000;
143 for (i = 0; i < 16; i++) {
168 av_log(avctx,
AV_LOG_DEBUG,
" lpc type: Levinson-Durbin recursion with Welch window\n");
231 for (i = 4; i < 12; i++) {
241 if (freq % 1000 == 0 && freq < 255000) {
244 }
else if (freq % 10 == 0 && freq < 655350) {
247 }
else if (freq < 65535) {
269 s->
options.
block_time_ms = ((
int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
276 FF_LPC_TYPE_LEVINSON})[level];
278 s->
options.
min_prediction_order = ((
int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
279 s->
options.
max_prediction_order = ((
int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
286 ORDER_METHOD_SEARCH})[level];
294 s->
options.
min_partition_order = ((
int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
296 s->
options.
max_partition_order = ((
int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
299 #if FF_API_FLAC_GLOBAL_OPTS
307 if (avctx->lpc_passes < 0) {
310 }
else if (avctx->lpc_passes == 0) {
361 #if FF_API_FLAC_GLOBAL_OPTS
362 if (avctx->prediction_order_method >= 0) {
365 avctx->prediction_order_method);
371 if (avctx->min_partition_order >= 0) {
374 avctx->min_partition_order);
379 if (avctx->max_partition_order >= 0) {
382 avctx->max_partition_order);
406 #if FF_API_FLAC_GLOBAL_OPTS
408 if (avctx->lpc_coeff_precision > 0) {
411 avctx->lpc_coeff_precision);
458 for (i = 0; i < 16; i++) {
477 for (ch = 0; ch < s->
channels; ch++)
493 for (i = 0, j = 0; i < frame->
blocksize; i++)
494 for (ch = 0; ch < s->
channels; ch++, j++)
504 for (i = 0; i < n; i++) {
505 int32_t
v = -2 * res[i] - 1;
507 count += (v >> k) + 1 + k;
516 int p, porder, psize;
530 count += pred_order * sub->
obits;
547 for (p = 0; p < 1 << porder; p++) {
560 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
572 sum2 = sum - (n >> 1);
573 k = av_log2(n < 256 ?
FASTDIV(sum2, n) : sum2 / n);
579 uint32_t *sums,
int n,
int pred_order)
585 part = (1 << porder);
588 cnt = (n >> porder) - pred_order;
589 for (i = 0; i < part; i++) {
602 static void calc_sums(
int pmin,
int pmax, uint32_t *
data,
int n,
int pred_order,
607 uint32_t *res, *res_end;
611 res = &data[pred_order];
612 res_end = &data[n >> pmax];
613 for (i = 0; i < parts; i++) {
615 while (res < res_end)
618 res_end += n >> pmax;
621 for (i = pmax - 1; i >= pmin; i--) {
623 for (j = 0; j < parts; j++)
624 sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
630 int32_t *
data,
int n,
int pred_order)
641 assert(pmin <= pmax);
644 for (i = 0; i < n; i++)
645 udata[i] = (2*data[i]) ^ (data[i]>>31);
647 calc_sums(pmin, pmax, udata, n, pred_order, sums);
651 for (i = pmin; i <= pmax; i++) {
653 if (bits[i] <= bits[opt_porder]) {
660 return bits[opt_porder];
666 int porder =
FFMIN(max_porder, av_log2(n^(n-1)));
668 porder =
FFMIN(porder, av_log2(n/order));
681 uint32_t
bits = 8 + pred_order * sub->
obits + 2 + 4;
695 for (i = 0; i < order; i++)
699 for (i = order; i < n; i++)
701 }
else if (order == 1) {
702 for (i = order; i < n; i++)
703 res[i] = smp[i] - smp[i-1];
704 }
else if (order == 2) {
705 int a = smp[order-1] - smp[order-2];
706 for (i = order; i < n; i += 2) {
707 int b = smp[i ] - smp[i-1];
709 a = smp[i+1] - smp[i ];
712 }
else if (order == 3) {
713 int a = smp[order-1] - smp[order-2];
714 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
715 for (i = order; i < n; i += 2) {
716 int b = smp[i ] - smp[i-1];
719 a = smp[i+1] - smp[i ];
724 int a = smp[order-1] - smp[order-2];
725 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
726 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
727 for (i = order; i < n; i += 2) {
728 int b = smp[i ] - smp[i-1];
732 a = smp[i+1] - smp[i ];
742 int c = coefs[(x)-1];\
749 const int32_t *smp,
int n,
int order,
750 const int32_t *coefs,
int shift,
int big)
753 for (i = order; i < n; i += 2) {
754 int s = smp[i-order];
803 res[i ] = smp[i ] - (p0 >> shift);
804 res[i+1] = smp[i+1] - (p1 >> shift);
810 int order,
const int32_t *coefs,
int shift)
813 for (i = 0; i < order; i++)
816 for (i = order; i < n; i += 2) {
820 for (j = 0; j < order; j++) {
826 res[i ] = smp[i ] - (p0 >> shift);
827 res[i+1] = smp[i+1] - (p1 >> shift);
848 int min_order, max_order, opt_order, omethod;
862 for (i = 1; i < n; i++)
874 memcpy(res, smp, n *
sizeof(int32_t));
891 for (i = min_order; i <= max_order; i++) {
894 if (bits[i] < bits[opt_order])
897 sub->
order = opt_order;
899 if (sub->
order != max_order) {
916 int levels = 1 << omethod;
919 int opt_index = levels-1;
920 opt_order = max_order-1;
922 for (i = levels-1; i >= 0; i--) {
923 int last_order = order;
924 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
925 order = av_clip(order, min_order - 1, max_order - 1);
926 if (order == last_order)
930 if (bits[i] < bits[opt_index]) {
941 for (i = min_order-1; i < max_order; i++) {
944 if (bits[i] < bits[opt_order])
952 opt_order = min_order - 1 + (max_order-min_order)/3;
953 memset(bits, -1,
sizeof(bits));
955 for (step = 16;
step; step >>= 1) {
956 int last = opt_order;
957 for (i = last-step; i <= last+
step; i +=
step) {
958 if (i < min_order-1 || i >= max_order || bits[i] <
UINT32_MAX)
962 if (bits[i] < bits[opt_order])
969 sub->
order = opt_order;
972 for (i = 0; i < sub->
order; i++)
1025 for (ch = 0; ch < s->
channels; ch++)
1028 count += (8 - (count & 7)) & 7;
1044 sum[0] = sum[1] = sum[2] = sum[3] = 0;
1045 for (i = 2; i < n; i++) {
1046 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
1047 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
1048 sum[2] +=
FFABS((lt + rt) >> 1);
1049 sum[3] +=
FFABS(lt - rt);
1050 sum[0] +=
FFABS(lt);
1051 sum[1] +=
FFABS(rt);
1054 for (i = 0; i < 4; i++) {
1060 score[0] = sum[0] + sum[1];
1061 score[1] = sum[0] + sum[3];
1062 score[2] = sum[1] + sum[3];
1063 score[3] = sum[2] + sum[3];
1067 for (i = 1; i < 4; i++)
1068 if (score[i] < score[best])
1072 }
else if (best == 1) {
1074 }
else if (best == 2) {
1088 int32_t *left, *right;
1108 for (i = 0; i < n; i++) {
1110 left[i] = (tmp + right[i]) >> 1;
1111 right[i] = tmp - right[i];
1115 for (i = 0; i < n; i++)
1116 right[i] = left[i] - right[i];
1119 for (i = 0; i < n; i++)
1120 left[i] -= right[i];
1155 else if (frame->
bs_code[0] == 7)
1174 for (ch = 0; ch < s->
channels; ch++) {
1176 int i, p, porder, psize;
1190 while (res < frame_end)
1194 for (i = 0; i < sub->
order; i++)
1202 for (i = 0; i < sub->
order; i++)
1216 for (p = 0; p < 1 << porder; p++) {
1219 while (res < part_end)
1221 part_end =
FFMIN(frame_end, part_end + psize);
1264 int buf_size,
void *
data)
1268 int frame_bytes, out_bytes;
1301 if (buf_size < frame_bytes) {
1313 if (out_bytes < s->min_framesize)
1333 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1336 {
"lpc_type",
"LPC algorithm", offsetof(
FlacEncodeContext, options.lpc_type),
AV_OPT_TYPE_INT, {.dbl =
FF_LPC_TYPE_DEFAULT },
FF_LPC_TYPE_DEFAULT,
FF_LPC_TYPE_NB-1,
FLAGS,
"lpc_type" },
1341 {
"lpc_passes",
"Number of passes to use for Cholesky factorization during LPC analysis", offsetof(
FlacEncodeContext, options.lpc_passes),
AV_OPT_TYPE_INT, {.dbl = -1 }, INT_MIN, INT_MAX,
FLAGS },
1344 {
"prediction_order_method",
"Search method for selecting prediction order", offsetof(
FlacEncodeContext, options.prediction_order_method),
AV_OPT_TYPE_INT, {.dbl = -1 }, -1,
ORDER_METHOD_LOG,
FLAGS,
"predm" },
#define rice_encode_count(sum, n, k)
#define ORDER_METHOD_SEARCH
#define ORDER_METHOD_8LEVEL
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
static void put_sbits(PutBitContext *pb, int n, int32_t value)
static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame, int buf_size, void *data)
int ff_lpc_calc_coefs(LPCContext *s, const int32_t *samples, int blocksize, int min_order, int max_order, int precision, int32_t coefs[][MAX_LPC_ORDER], int *shift, enum FFLPCType lpc_type, int lpc_passes, int omethod, int max_shift, int zero_shift)
Calculate LPC coefficients for multiple orders.
AVFrame * coded_frame
the picture in the bitstream
int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
Calculate an estimate for the maximum frame size based on verbatim mode.
static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n, int order, const int32_t *coefs, int shift)
static int rice_count_exact(int32_t *res, int n, int k)
#define MAX_PARTITION_ORDER
static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order, uint32_t sums[][MAX_PARTITIONS])
#define PUT_UTF8(val, tmp, PUT_BYTE)
Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
#define FLAC_MAX_BLOCKSIZE
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
static int select_blocksize(int samplerate, int block_time_ms)
Set blocksize based on samplerate.
enum AVSampleFormat sample_fmt
audio sample format
struct CompressionOptions CompressionOptions
do not use LPC prediction or use all zero coefficients
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
int32_t coefs[MAX_LPC_ORDER]
static void copy_samples(FlacEncodeContext *s, const int16_t *samples)
Copy channel-interleaved input samples into separate subframes.
static int find_optimal_param(uint32_t sum, int n)
Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
int64_t pts
presentation timestamp in time_base units (time when frame should be shown to user) If AV_NOPTS_VALUE...
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
bitstream reader API header.
int params[MAX_PARTITIONS]
#define FLAC_MIN_BLOCKSIZE
static int init(AVCodecParserContext *s)
#define CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
static void write_subframes(FlacEncodeContext *s)
static uint32_t find_subframe_rice_params(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
const int16_t ff_flac_blocksize_table[16]
void av_md5_update(AVMD5 *ctx, const uint8_t *src, const int len)
#define ORDER_METHOD_4LEVEL
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define FLAC_SUBFRAME_LPC
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
#define FLAC_SUBFRAME_VERBATIM
int32_t samples[FLAC_MAX_BLOCKSIZE]
#define FLAC_SUBFRAME_CONSTANT
FLAC audio encoder Copyright (c) 2006 Justin Ruggles justin.ruggles@gmail.com
static int put_bits_count(PutBitContext *s)
#define ORDER_METHOD_2LEVEL
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
struct FlacSubframe FlacSubframe
struct RiceContext RiceContext
#define FLAC_SUBFRAME_FIXED
static int encode_residual_ch(FlacEncodeContext *s, int ch)
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
static int encode_frame(FlacEncodeContext *s)
static void update_md5_sum(FlacEncodeContext *s, const int16_t *samples)
#define FLAC_STREAMINFO_SIZE
int prediction_order_method
static int get_max_p_order(int max_porder, int n, int order)
static void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len)
write signed golomb rice code (flac).
static const AVOption options[]
static void channel_decorrelation(FlacEncodeContext *s)
Perform stereo channel decorrelation.
int frame_size
Samples per packet, initialized when calling 'init'.
static av_always_inline void encode_residual_lpc_unrolled(int32_t *res, const int32_t *smp, int n, int order, const int32_t *coefs, int shift, int big)
struct FlacEncodeContext FlacEncodeContext
const int ff_flac_sample_rate_table[16]
int sample_rate
samples per second
static void write_frame_header(FlacEncodeContext *s)
main external API structure.
static void close(AVCodecParserContext *s)
static int count_frame_header(FlacEncodeContext *s)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Levinson-Durbin recursion.
#define ORDER_METHOD_EST
LPC utility code Copyright (c) 2006 Justin Ruggles justin.ruggles@gmail.com
void av_md5_init(AVMD5 *ctx)
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Describe the class of an AVClass context structure.
use the codec default LPC type
struct FlacFrame FlacFrame
static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n, int order)
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
int max_encoded_framesize
static void write_utf8(PutBitContext *pb, uint32_t val)
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
#define MAX_LPC_PRECISION
static void write_frame_footer(FlacEncodeContext *s)
static int subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
int32_t residual[FLAC_MAX_BLOCKSIZE+1]
FlacSubframe subframes[FLAC_MAX_CHANNELS]
CompressionOptions options
FFLPCType
LPC analysis type.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax, int32_t *data, int n, int pred_order)
AVSampleFormat
all in native-endian format
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static av_cold int flac_encode_close(AVCodecContext *avctx)
static av_cold void dprint_compression_options(FlacEncodeContext *s)
int channels
number of audio channels
static void init_frame(FlacEncodeContext *s)
static const AVClass flac_encoder_class
static int write_frame(FlacEncodeContext *s, uint8_t *frame, int buf_size)
static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder, uint32_t *sums, int n, int pred_order)
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
static av_cold int flac_encode_init(AVCodecContext *avctx)
static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
Write streaminfo metadata block to byte array.
#define FLAC_MAX_CHANNELS