22 #include "libavutil/avassert.h"
23 #include "libavutil/crc.h"
25 #include "libavutil/md5.h"
26 #include "libavutil/opt.h"
37 #define FLAC_SUBFRAME_CONSTANT 0
38 #define FLAC_SUBFRAME_VERBATIM 1
39 #define FLAC_SUBFRAME_FIXED 8
40 #define FLAC_SUBFRAME_LPC 32
42 #define MAX_FIXED_ORDER 4
43 #define MAX_PARTITION_ORDER 8
44 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
45 #define MAX_LPC_PRECISION 15
46 #define MAX_LPC_SHIFT 15
143 memcpy(&header[18], s->
md5sum, 16);
159 target = (samplerate * block_time_ms) / 1000;
160 for (i = 0; i < 16; i++) {
185 av_log(avctx,
AV_LOG_DEBUG,
" lpc type: Levinson-Durbin recursion with Welch window\n");
253 channels, FLAC_MAX_CHANNELS);
261 for (i = 4; i < 12; i++) {
271 if (freq % 1000 == 0 && freq < 255000) {
274 }
else if (freq % 10 == 0 && freq < 655350) {
277 }
else if (freq < 65535) {
300 s->
options.
block_time_ms = ((
int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
307 FF_LPC_TYPE_LEVINSON})[level];
309 s->
options.
min_prediction_order = ((
int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
310 s->
options.
max_prediction_order = ((
int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
317 ORDER_METHOD_SEARCH})[level];
325 s->
options.
min_partition_order = ((
int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
327 s->
options.
max_partition_order = ((
int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
415 "output stream will have incorrect "
416 "channel layout.\n");
419 "will use Flac channel layout for "
420 "%d channels.\n", channels);
444 for (i = 0; i < 16; i++) {
463 for (ch = 0; ch < s->
channels; ch++) {
489 #define COPY_SAMPLES(bits) do { \
490 const int ## bits ## _t *samples0 = samples; \
492 for (i = 0, j = 0; i < frame->blocksize; i++) \
493 for (ch = 0; ch < s->channels; ch++, j++) \
494 frame->subframes[ch].samples[i] = samples0[j] >> shift; \
509 for (i = 0; i <
n; i++) {
512 count += (v >> k) + 1 + k;
521 int p, porder, psize;
535 count += pred_order * sub->
obits;
552 for (p = 0; p < 1 << porder; p++) {
565 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
577 sum2 = sum - (n >> 1);
578 k =
av_log2(av_clipl_int32(sum2 / n));
579 return FFMIN(k, max_param);
584 uint64_t *sums,
int n,
int pred_order)
587 int k, cnt, part, max_param;
592 part = (1 << porder);
595 cnt = (n >> porder) - pred_order;
596 for (i = 0; i < part; i++) {
614 uint32_t *
res, *res_end;
618 res = &data[pred_order];
619 res_end = &data[n >> pmax];
620 for (i = 0; i < parts; i++) {
622 while (res < res_end)
625 res_end += n >> pmax;
628 for (i = pmax - 1; i >= pmin; i--) {
630 for (j = 0; j < parts; j++)
631 sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
653 for (i = 0; i <
n; i++)
654 udata[i] = (2*data[i]) ^ (data[i]>>31);
656 calc_sums(pmin, pmax, udata, n, pred_order, sums);
659 bits[pmin] = UINT32_MAX;
660 for (i = pmin; i <= pmax; i++) {
662 if (bits[i] <= bits[opt_porder]) {
669 return bits[opt_porder];
704 for (i = 0; i < order; i++)
708 for (i = order; i <
n; i++)
710 }
else if (order == 1) {
711 for (i = order; i <
n; i++)
712 res[i] = smp[i] - smp[i-1];
713 }
else if (order == 2) {
714 int a = smp[order-1] - smp[order-2];
715 for (i = order; i <
n; i += 2) {
716 int b = smp[i ] - smp[i-1];
718 a = smp[i+1] - smp[i ];
721 }
else if (order == 3) {
722 int a = smp[order-1] - smp[order-2];
723 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
724 for (i = order; i <
n; i += 2) {
725 int b = smp[i ] - smp[i-1];
728 a = smp[i+1] - smp[i ];
733 int a = smp[order-1] - smp[order-2];
734 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
735 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
736 for (i = order; i <
n; i += 2) {
737 int b = smp[i ] - smp[i-1];
741 a = smp[i+1] - smp[i ];
753 int min_order, max_order, opt_order, omethod;
767 for (i = 1; i <
n; i++)
779 memcpy(res, smp, n *
sizeof(
int32_t));
795 bits[0] = UINT32_MAX;
796 for (i = min_order; i <= max_order; i++) {
799 if (bits[i] < bits[opt_order])
802 sub->
order = opt_order;
804 if (sub->
order != max_order) {
821 int levels = 1 << omethod;
824 int opt_index = levels-1;
825 opt_order = max_order-1;
826 bits[opt_index] = UINT32_MAX;
827 for (i = levels-1; i >= 0; i--) {
828 int last_order = order;
829 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
830 order = av_clip(order, min_order - 1, max_order - 1);
831 if (order == last_order)
836 if (bits[i] < bits[opt_index]) {
846 bits[0] = UINT32_MAX;
847 for (i = min_order-1; i < max_order; i++) {
850 if (bits[i] < bits[opt_order])
858 opt_order = min_order - 1 + (max_order-min_order)/3;
859 memset(bits, -1,
sizeof(bits));
861 for (step = 16; step; step >>= 1) {
862 int last = opt_order;
863 for (i = last-step; i <= last+step; i += step) {
864 if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
868 if (bits[i] < bits[opt_order])
875 sub->
order = opt_order;
878 for (i = 0; i < sub->
order; i++)
932 for (ch = 0; ch < s->
channels; ch++)
935 count += (8 - (count & 7)) & 7;
949 for (ch = 0; ch < s->
channels; ch++) {
987 sum[0] = sum[1] = sum[2] = sum[3] = 0;
988 for (i = 2; i <
n; i++) {
989 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
990 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
991 sum[2] +=
FFABS((lt + rt) >> 1);
992 sum[3] +=
FFABS(lt - rt);
997 for (i = 0; i < 4; i++) {
1003 score[0] = sum[0] + sum[1];
1004 score[1] = sum[0] + sum[3];
1005 score[2] = sum[1] + sum[3];
1006 score[3] = sum[2] + sum[3];
1010 for (i = 1; i < 4; i++)
1011 if (score[i] < score[best])
1048 for (i = 0; i <
n; i++) {
1050 left[i] = (tmp + right[i]) >> 1;
1051 right[i] = tmp - right[i];
1055 for (i = 0; i <
n; i++)
1056 right[i] = left[i] - right[i];
1059 for (i = 0; i <
n; i++)
1060 left[i] -= right[i];
1095 else if (frame->
bs_code[0] == 7)
1114 for (ch = 0; ch < s->
channels; ch++) {
1116 int i, p, porder, psize;
1132 while (res < frame_end)
1136 for (i = 0; i < sub->
order; i++)
1144 for (i = 0; i < sub->
order; i++)
1158 for (p = 0; p < 1 << porder; p++) {
1161 while (res < part_end)
1163 part_end =
FFMIN(frame_end, part_end + psize);
1204 buf = (
const uint8_t *)samples;
1207 (
const uint16_t *)samples, buf_size / 2);
1212 const int32_t *samples0 = samples;
1217 *tmp++ = (
v ) & 0xFF;
1218 *tmp++ = (v >> 8) & 0xFF;
1219 *tmp++ = (v >> 16) & 0xFF;
1233 int frame_bytes, out_bytes,
ret;
1267 if (frame_bytes < 0) {
1286 if (out_bytes < s->min_framesize)
1291 avpkt->
size = out_bytes;
1292 *got_packet_ptr = 1;
1310 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1313 {
"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" },
1318 {
"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 },
1321 {
"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" },
1328 {
"ch_mode",
"Stereo decorrelation mode", offsetof(
FlacEncodeContext, options.ch_mode),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1,
FLAC_CHMODE_MID_SIDE,
FLAGS,
"ch_mode" },
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
#define rice_encode_count(sum, n, k)
const char const char void * val
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
#define ORDER_METHOD_SEARCH
#define PUT_UTF8(val, tmp, PUT_BYTE)
static int shift(int a, int b)
This structure describes decoded (raw) audio or video data.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
#define ORDER_METHOD_8LEVEL
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)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
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.
static av_cold int init(AVCodecContext *avctx)
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 CODEC_CAP_LOSSLESS
Codec is lossless.
#define MAX_PARTITION_ORDER
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
#define FLAC_MAX_BLOCKSIZE
#define AV_CH_LAYOUT_STEREO
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)
#define AV_CH_LAYOUT_5POINT0
static uint64_t rice_count_exact(int32_t *res, int n, int k)
void av_md5_update(struct AVMD5 *ctx, const uint8_t *src, int len)
Update hash value.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
static int select_blocksize(int samplerate, int block_time_ms)
Set blocksize based on samplerate.
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
const char * av_default_item_name(void *ctx)
Return the context name.
enum AVSampleFormat sample_fmt
audio sample format
do not use LPC prediction or use all zero coefficients
int32_t coefs[MAX_LPC_ORDER]
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
const char * name
Name of the codec implementation.
static uint64_t find_subframe_rice_params(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
int params[MAX_PARTITIONS]
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
const OptionDef options[]
#define FLAC_MIN_BLOCKSIZE
static void write_subframes(FlacEncodeContext *s)
#define AV_CH_LAYOUT_5POINT1
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void(* bswap16_buf)(uint16_t *dst, const uint16_t *src, int len)
#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...
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
#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
#define AV_CH_LAYOUT_QUAD
#define COPY_SAMPLES(bits)
#define FLAC_SUBFRAME_VERBATIM
int32_t samples[FLAC_MAX_BLOCKSIZE]
Libavcodec external API header.
#define av_unused
Disable warnings about deprecated features This is useful for sections of code kept for backward comp...
static void remove_wasted_bits(FlacEncodeContext *s)
#define FLAC_SUBFRAME_CONSTANT
uint64_t channel_layout
Audio channel layout.
static int put_bits_count(PutBitContext *s)
#define ORDER_METHOD_2LEVEL
#define FLAC_SUBFRAME_FIXED
static int encode_residual_ch(FlacEncodeContext *s, int ch)
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
void(* lpc_encode)(int32_t *res, const int32_t *smp, int len, int order, const int32_t *coefs, int shift)
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length) av_pure
Calculate the CRC of a block.
#define FLAC_STREAMINFO_SIZE
#define AV_CH_FRONT_CENTER
int prediction_order_method
#define AV_CH_LAYOUT_5POINT1_BACK
static int get_max_p_order(int max_porder, int n, int order)
static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
int AC3_NAME() encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
static void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len)
write signed golomb rice code (flac).
static void channel_decorrelation(FlacEncodeContext *s)
Perform stereo channel decorrelation.
int frame_size
Number of samples per channel in an audio frame.
AVSampleFormat
Audio Sample Formats.
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)
Levinson-Durbin recursion.
void av_md5_init(struct AVMD5 *ctx)
Initialize MD5 hashing.
Describe the class of an AVClass context structure.
use the codec default LPC type
#define AV_CH_LAYOUT_5POINT0_BACK
static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n, int order)
void av_md5_final(struct AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
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
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
static void copy_samples(FlacEncodeContext *s, const void *samples)
Copy channel-interleaved input samples into separate subframes.
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.
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
const int32_t ff_flac_blocksize_table[16]
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 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 enum AVSampleFormat sample_fmts[]
static void init_frame(FlacEncodeContext *s, int nb_samples)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
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)
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
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