36 #define FLAC_SUBFRAME_CONSTANT 0 37 #define FLAC_SUBFRAME_VERBATIM 1 38 #define FLAC_SUBFRAME_FIXED 8 39 #define FLAC_SUBFRAME_LPC 32 41 #define MAX_FIXED_ORDER 4 42 #define MAX_PARTITION_ORDER 8 43 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER) 44 #define MAX_LPC_PRECISION 15 45 #define MAX_LPC_SHIFT 15 145 memcpy(&header[18], s->
md5sum, 16);
159 assert(samplerate > 0);
162 for (i = 0; i < 16; i++) {
187 av_log(avctx,
AV_LOG_DEBUG,
" lpc type: Levinson-Durbin recursion with Welch window\n");
260 for (i = 4; i < 12; i++) {
270 if (freq % 1000 == 0 && freq < 255000) {
273 }
else if (freq % 10 == 0 && freq < 655350) {
276 }
else if (freq < 65535) {
298 s->
options.
block_time_ms = ((
int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
305 FF_LPC_TYPE_LEVINSON})[level];
307 s->
options.
min_prediction_order = ((
int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
308 s->
options.
max_prediction_order = ((
int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
315 ORDER_METHOD_SEARCH})[level];
323 s->
options.
min_partition_order = ((
int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
325 s->
options.
max_partition_order = ((
int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
420 for (i = 0; i < 16; i++) {
439 for (ch = 0; ch < s->
channels; ch++) {
465 #define COPY_SAMPLES(bits) do { \ 466 const int ## bits ## _t *samples0 = samples; \ 468 for (i = 0, j = 0; i < frame->blocksize; i++) \ 469 for (ch = 0; ch < s->channels; ch++, j++) \ 470 frame->subframes[ch].samples[i] = samples0[j] >> shift; \ 485 for (i = 0; i < n; i++) {
488 count += (v >> k) + 1 + k;
497 int p, porder, psize;
511 count += pred_order * sub->
obits;
528 for (p = 0; p < 1 << porder; p++) {
541 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k))) 553 sum2 = sum - (n >> 1);
554 k =
av_log2(av_clipl_int32(sum2 / n));
555 return FFMIN(k, max_param);
560 uint64_t *sums,
int n,
int pred_order)
563 int k, cnt, part, max_param;
568 part = (1 << porder);
571 cnt = (n >> porder) - pred_order;
572 for (i = 0; i < part; i++) {
585 static void calc_sums(
int pmin,
int pmax, uint32_t *
data,
int n,
int pred_order,
590 uint32_t *res, *res_end;
594 res = &data[pred_order];
595 res_end = &data[n >> pmax];
596 for (i = 0; i < parts; i++) {
598 while (res < res_end)
601 res_end += n >> pmax;
604 for (i = pmax - 1; i >= pmin; i--) {
606 for (j = 0; j < parts; j++)
607 sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
624 assert(pmin <= pmax);
629 for (i = 0; i < n; i++)
630 udata[i] = (2*data[i]) ^ (data[i]>>31);
632 calc_sums(pmin, pmax, udata, n, pred_order, sums);
635 bits[pmin] = UINT32_MAX;
636 for (i = pmin; i <= pmax; i++) {
638 if (bits[i] <= bits[opt_porder]) {
645 return bits[opt_porder];
680 for (i = 0; i < order; i++)
684 for (i = order; i < n; i++)
686 }
else if (order == 1) {
687 for (i = order; i < n; i++)
688 res[i] = smp[i] - smp[i-1];
689 }
else if (order == 2) {
690 int a = smp[order-1] - smp[order-2];
691 for (i = order; i < n; i += 2) {
692 int b = smp[i ] - smp[i-1];
694 a = smp[i+1] - smp[i ];
697 }
else if (order == 3) {
698 int a = smp[order-1] - smp[order-2];
699 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
700 for (i = order; i < n; i += 2) {
701 int b = smp[i ] - smp[i-1];
704 a = smp[i+1] - smp[i ];
709 int a = smp[order-1] - smp[order-2];
710 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
711 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
712 for (i = order; i < n; i += 2) {
713 int b = smp[i ] - smp[i-1];
717 a = smp[i+1] - smp[i ];
729 int min_order, max_order, opt_order, omethod;
743 for (i = 1; i < n; i++)
755 memcpy(res, smp, n *
sizeof(
int32_t));
771 bits[0] = UINT32_MAX;
772 for (i = min_order; i <= max_order; i++) {
775 if (bits[i] < bits[opt_order])
778 sub->
order = opt_order;
780 if (sub->
order != max_order) {
797 int levels = 1 << omethod;
800 int opt_index = levels-1;
801 opt_order = max_order-1;
802 bits[opt_index] = UINT32_MAX;
803 for (i = levels-1; i >= 0; i--) {
804 int last_order = order;
805 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
806 order = av_clip(order, min_order - 1, max_order - 1);
807 if (order == last_order)
812 if (bits[i] < bits[opt_index]) {
822 bits[0] = UINT32_MAX;
823 for (i = min_order-1; i < max_order; i++) {
826 if (bits[i] < bits[opt_order])
834 opt_order = min_order - 1 + (max_order-min_order)/3;
835 memset(bits, -1,
sizeof(bits));
837 for (step = 16;
step; step >>= 1) {
838 int last = opt_order;
839 for (i = last-step; i <= last+
step; i +=
step) {
840 if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
844 if (bits[i] < bits[opt_order])
851 sub->
order = opt_order;
854 for (i = 0; i < sub->
order; i++)
908 for (ch = 0; ch < s->
channels; ch++)
911 count += (8 - (count & 7)) & 7;
925 for (ch = 0; ch < s->
channels; ch++) {
963 sum[0] = sum[1] = sum[2] = sum[3] = 0;
964 for (i = 2; i < n; i++) {
965 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
966 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
967 sum[2] +=
FFABS((lt + rt) >> 1);
968 sum[3] +=
FFABS(lt - rt);
973 for (i = 0; i < 4; i++) {
979 score[0] = sum[0] + sum[1];
980 score[1] = sum[0] + sum[3];
981 score[2] = sum[1] + sum[3];
982 score[3] = sum[2] + sum[3];
986 for (i = 1; i < 4; i++)
987 if (score[i] < score[best])
1024 for (i = 0; i < n; i++) {
1026 left[i] = (tmp + right[i]) >> 1;
1027 right[i] = tmp - right[i];
1031 for (i = 0; i < n; i++)
1032 right[i] = left[i] - right[i];
1035 for (i = 0; i < n; i++)
1036 left[i] -= right[i];
1071 else if (frame->
bs_code[0] == 7)
1090 for (ch = 0; ch < s->
channels; ch++) {
1092 int i, p, porder, psize;
1108 while (res < frame_end)
1112 for (i = 0; i < sub->
order; i++)
1120 for (i = 0; i < sub->
order; i++)
1134 for (p = 0; p < 1 << porder; p++) {
1137 while (res < part_end)
1139 part_end =
FFMIN(frame_end, part_end + psize);
1180 buf = (
const uint8_t *)samples;
1183 (
const uint16_t *) samples, buf_size / 2);
1188 const int32_t *samples0 = samples;
1193 *tmp++ = (v ) & 0xFF;
1194 *tmp++ = (v >> 8) & 0xFF;
1195 *tmp++ = (v >> 16) & 0xFF;
1206 const AVFrame *frame,
int *got_packet_ptr)
1209 int frame_bytes, out_bytes, ret;
1228 *got_packet_ptr = 1;
1257 if (frame_bytes < 0) {
1278 if (out_bytes < s->min_framesize)
1283 avpkt->
size = out_bytes;
1287 *got_packet_ptr = 1;
1305 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM 1308 {
"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" },
1313 {
"lpc_passes",
"Number of passes to use for Cholesky factorization during LPC analysis", offsetof(
FlacEncodeContext, options.lpc_passes),
AV_OPT_TYPE_INT, {.i64 = 1 }, 1, INT_MAX, FLAGS },
1316 {
"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" },
1323 {
"ch_mode",
"Stereo decorrelation mode", offsetof(
FlacEncodeContext, options.ch_mode),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1,
FLAC_CHMODE_MID_SIDE,
FLAGS,
"ch_mode" },
#define rice_encode_count(sum, n, k)
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define ORDER_METHOD_SEARCH
This structure describes decoded (raw) audio or video data.
void(* bswap16_buf)(uint16_t *dst, const uint16_t *src, int len)
#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 uint64_t calc_optimal_rice_params(RiceContext *rc, int porder, uint64_t *sums, int n, int pred_order)
#define AV_LOG_WARNING
Something somehow does not look correct.
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.
int av_ctz(int v)
Trailing zero bit count.
av_cold void ff_flacdsp_init(FLACDSPContext *c, enum AVSampleFormat fmt, int bps)
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.
#define MAX_PARTITION_ORDER
#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
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
static uint64_t calc_rice_params(RiceContext *rc, int pmin, int pmax, int32_t *data, int n, int pred_order)
static uint64_t rice_count_exact(int32_t *res, int n, int k)
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.
struct AVMD5 * av_md5_alloc(void)
enum AVSampleFormat sample_fmt
audio sample format
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]
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static uint64_t find_subframe_rice_params(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
bitstream reader API header.
int params[MAX_PARTITIONS]
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
#define FLAC_MIN_BLOCKSIZE
static void write_subframes(FlacEncodeContext *s)
const int16_t ff_flac_blocksize_table[16]
void av_md5_update(AVMD5 *ctx, const uint8_t *src, const int len)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define ORDER_METHOD_4LEVEL
static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n, int max_rice_param)
unsigned int md5_buffer_size
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
#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. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define FLAC_SUBFRAME_LPC
enum CodingMode coding_mode
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
#define COPY_SAMPLES(bits)
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]
static void remove_wasted_bits(FlacEncodeContext *s)
#define FLAC_SUBFRAME_CONSTANT
static int put_bits_count(PutBitContext *s)
#define ORDER_METHOD_2LEVEL
static void frame_end(MpegEncContext *s)
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
void(* lpc_encode)(int32_t *res, const int32_t *smp, int len, int order, const int32_t *coefs, int shift)
#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)
int side_data_only_packets
Encoding only.
#define FLAC_STREAMINFO_SIZE
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
int prediction_order_method
static int get_max_p_order(int max_porder, int n, int order)
static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
if(ac->has_optimized_func)
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
Number of samples per channel in an audio frame.
const int ff_flac_sample_rate_table[16]
Libavcodec external API header.
AVSampleFormat
Audio Sample Formats.
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)
Levinson-Durbin recursion.
void av_md5_init(AVMD5 *ctx)
#define AVERROR_BUG
Bug detected, please report the issue.
Describe the class of an AVClass context structure.
use the codec default LPC type
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 copy_samples(FlacEncodeContext *s, const void *samples)
Copy channel-interleaved input samples into separate subframes.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
static void write_frame_footer(FlacEncodeContext *s)
int32_t residual[FLAC_MAX_BLOCKSIZE+1]
FlacSubframe subframes[FLAC_MAX_CHANNELS]
CompressionOptions options
FFLPCType
LPC analysis type.
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order, uint64_t sums[][MAX_PARTITIONS])
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)
static av_cold int init(AVCodecParserContext *s)
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.
int channels
number of audio channels
static uint64_t subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
static const AVClass flac_encoder_class
static void init_frame(FlacEncodeContext *s, int nb_samples)
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
static int update_md5_sum(FlacEncodeContext *s, const void *samples)
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
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