33#define DEFAULT_FRAME_SIZE 4096
34#define ALAC_EXTRADATA_SIZE 36
35#define ALAC_FRAME_HEADER_SIZE 55
36#define ALAC_FRAME_FOOTER_SIZE 3
38#define ALAC_ESCAPE_CODE 0x1FF
39#define ALAC_MAX_LPC_ORDER 30
40#define DEFAULT_MAX_PRED_ORDER 6
41#define DEFAULT_MIN_PRED_ORDER 4
42#define ALAC_MAX_LPC_PRECISION 9
43#define ALAC_MIN_LPC_SHIFT 0
44#define ALAC_MAX_LPC_SHIFT 9
46#define ALAC_CHMODE_LEFT_RIGHT 0
47#define ALAC_CHMODE_LEFT_SIDE 1
48#define ALAC_CHMODE_RIGHT_SIDE 2
49#define ALAC_CHMODE_MID_SIDE 3
87 const uint8_t *samples[2])
91 s->avctx->bits_per_raw_sample;
93#define COPY_SAMPLES(type) do { \
94 for (ch = 0; ch < channels; ch++) { \
95 int32_t *bptr = s->sample_buf[ch]; \
96 const type *sptr = (const type *)samples[ch]; \
97 for (i = 0; i < s->frame_size; i++) \
98 bptr[i] = sptr[i] >> shift; \
109 int k,
int write_sample_size)
113 k =
FFMIN(k,
s->rc.k_modifier);
114 divisor = (1<<k) - 1;
121 put_bits(&
s->pbctx, write_sample_size, x);
161 if (
s->compression_level == 1) {
162 s->lpc[ch].lpc_order = 6;
163 s->lpc[ch].lpc_quant = 6;
164 s->lpc[ch].lpc_coeff[0] = 160;
165 s->lpc[ch].lpc_coeff[1] = -190;
166 s->lpc[ch].lpc_coeff[2] = 170;
167 s->lpc[ch].lpc_coeff[3] = -130;
168 s->lpc[ch].lpc_coeff[4] = 80;
169 s->lpc[ch].lpc_coeff[5] = -25;
173 s->min_prediction_order,
174 s->max_prediction_order,
180 s->lpc[ch].lpc_order = opt_order;
181 s->lpc[ch].lpc_quant =
shift[opt_order-1];
182 memcpy(
s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*
sizeof(
int));
194 sum[0] = sum[1] = sum[2] = sum[3] = 0;
195 for (
i = 2;
i < n;
i++) {
196 lt = left_ch[
i] - 2 * left_ch[
i - 1] + left_ch[
i - 2];
197 rt = right_ch[
i] - 2 * right_ch[
i - 1] + right_ch[
i - 2];
198 sum[2] +=
FFABS((lt + rt) >> 1);
199 sum[3] +=
FFABS(lt - rt);
205 score[0] = sum[0] + sum[1];
206 score[1] = sum[0] + sum[3];
207 score[2] = sum[1] + sum[3];
208 score[3] = sum[2] + sum[3];
212 for (
i = 1;
i < 4;
i++) {
213 if (score[
i] < score[best])
222 int i,
mode, n =
s->frame_size;
229 s->interlacing_leftweight = 0;
230 s->interlacing_shift = 0;
233 for (
i = 0;
i < n;
i++)
235 s->interlacing_leftweight = 1;
236 s->interlacing_shift = 0;
239 for (
i = 0;
i < n;
i++) {
244 s->interlacing_leftweight = 1;
245 s->interlacing_shift = 31;
248 for (
i = 0;
i < n;
i++) {
251 right[
i] =
tmp - right[
i];
253 s->interlacing_leftweight = 1;
254 s->interlacing_shift = 1;
263 int32_t *residual =
s->predictor_buf[ch];
266 residual[0] =
s->sample_buf[ch][0];
268 for (
i = 1;
i <
s->frame_size;
i++) {
269 residual[
i] =
s->sample_buf[ch][
i ] -
270 s->sample_buf[ch][
i - 1];
279 int32_t *samples =
s->sample_buf[ch];
282 residual[0] = samples[0];
284 residual[
i] =
sign_extend(samples[
i] - samples[
i-1],
s->write_sample_size);
288 int sum = 1 << (lpc.
lpc_quant - 1), res_val, j;
291 sum += (samples[lpc.
lpc_order-j] - samples[0]) *
298 s->write_sample_size);
299 res_val = residual[
i];
303 int neg = (res_val < 0);
305 while (
index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
325 unsigned int history =
s->rc.initial_history;
326 int sign_modifier = 0,
i, k;
327 int32_t *samples =
s->predictor_buf[ch];
329 for (
i = 0;
i <
s->frame_size;) {
332 k =
av_log2((history >> 9) + 3);
334 x = -2 * (*samples) -1;
342 history += x *
s->rc.history_mult -
343 ((history *
s->rc.history_mult) >> 9);
350 unsigned int block_size = 0;
352 k = 7 -
av_log2(history) + ((history + 16) >> 6);
360 sign_modifier = (block_size <= 0xFFFF);
369 const uint8_t *samples0,
const uint8_t *samples1)
371 const uint8_t *samples[2] = { samples0, samples1 };
373 int prediction_type = 0;
382 int shift = 32 -
s->avctx->bits_per_raw_sample;
385 for (
i = 0;
i <
s->frame_size;
i++)
388 samples_s32[j][
i] >>
shift);
390 const int16_t *samples_s16[2] = { (
const int16_t *)samples0,
391 (
const int16_t *)samples1 };
392 for (
i = 0;
i <
s->frame_size;
i++)
398 s->write_sample_size =
s->avctx->bits_per_raw_sample -
s->extra_bits +
406 uint32_t
mask = (1 <<
s->extra_bits) - 1;
408 int32_t *extra =
s->predictor_buf[j];
410 for (
i = 0;
i <
s->frame_size;
i++) {
412 smp[
i] >>=
s->extra_bits;
420 s->interlacing_shift =
s->interlacing_leftweight = 0;
422 put_bits(pb, 8,
s->interlacing_leftweight);
433 for (j = 0; j <
s->lpc[
i].lpc_order; j++)
439 for (
i = 0;
i <
s->frame_size;
i++) {
441 put_bits(pb,
s->extra_bits,
s->predictor_buf[j][
i]);
451 if (prediction_type == 15) {
454 for (j =
s->frame_size - 1; j > 0; j--)
455 residual[j] -= residual[j - 1];
463 uint8_t *
const *samples)
466 int channels =
s->avctx->ch_layout.nb_channels;
469 int ch, element, sce, cpe;
473 ch = element = sce = cpe = 0;
475 if (ch_elements[element] ==
TYPE_CPE) {
511 uint8_t *alac_extradata;
526 s->compression_level = 2;
531 s->rc.history_mult = 40;
532 s->rc.initial_history = 10;
533 s->rc.k_modifier = 14;
534 s->rc.rice_modifier = 4;
551 AV_WB32(alac_extradata+24,
s->max_coded_frame_size);
557 if (
s->compression_level > 0) {
558 AV_WB8(alac_extradata+18,
s->rc.history_mult);
559 AV_WB8(alac_extradata+19,
s->rc.initial_history);
560 AV_WB8(alac_extradata+20,
s->rc.k_modifier);
563 if (
s->max_prediction_order <
s->min_prediction_order) {
565 "invalid prediction orders: min=%d max=%d\n",
566 s->min_prediction_order,
s->max_prediction_order);
573 s->max_prediction_order,
585 int out_bytes, max_frame_size, ret;
587 s->frame_size =
frame->nb_samples;
593 max_frame_size =
s->max_coded_frame_size;
599 if (
s->compression_level) {
609 if (out_bytes > max_frame_size) {
616 avpkt->
size = out_bytes;
621#define OFFSET(x) offsetof(AlacEncodeContext, x)
622#define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
631 .class_name =
"alacenc",
static double val(void *priv, double ch)
static const int ch_map[SC_NB]
#define ALAC_EXTRADATA_SIZE
enum AlacRawDataBlockType ff_alac_channel_elements[ALAC_MAX_CHANNELS][5]
const AVChannelLayout ff_alac_ch_layouts[ALAC_MAX_CHANNELS+1]
const uint8_t ff_alac_channel_layout_offsets[ALAC_MAX_CHANNELS][ALAC_MAX_CHANNELS]
#define ALAC_CHMODE_LEFT_RIGHT
#define COPY_SAMPLES(type)
#define ALAC_MAX_LPC_ORDER
const FFCodec ff_alac_encoder
static int write_frame(AlacEncodeContext *s, AVPacket *avpkt, uint8_t *const *samples)
static const AVClass alacenc_class
static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
static void calc_predictor_params(AlacEncodeContext *s, int ch)
static av_cold int alac_encode_init(AVCodecContext *avctx)
#define DEFAULT_MIN_PRED_ORDER
static void write_element(AlacEncodeContext *s, enum AlacRawDataBlockType element, int instance, const uint8_t *samples0, const uint8_t *samples1)
static void init_sample_buffers(AlacEncodeContext *s, int channels, const uint8_t *samples[2])
static void alac_stereo_decorrelation(AlacEncodeContext *s)
static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
static void write_element_header(AlacEncodeContext *s, enum AlacRawDataBlockType element, int instance)
static av_cold int alac_encode_close(AVCodecContext *avctx)
#define ALAC_MAX_LPC_SHIFT
static void alac_linear_predictor(AlacEncodeContext *s, int ch)
static void alac_entropy_coder(AlacEncodeContext *s, int ch)
#define ALAC_CHMODE_LEFT_SIDE
#define DEFAULT_MAX_PRED_ORDER
static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)
#define ALAC_MIN_LPC_SHIFT
#define DEFAULT_FRAME_SIZE
#define ALAC_MAX_LPC_PRECISION
#define ALAC_CHMODE_RIGHT_SIDE
Libavcodec external API header.
#define FF_COMPRESSION_DEFAULT
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define CODEC_SAMPLEFMTS(...)
#define CODEC_CH_LAYOUTS_ARRAY(array)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
static const uint8_t frame_size[4]
@ AV_OPT_TYPE_INT
Underlying C type is int.
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
#define LIBAVUTIL_VERSION_INT
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static int shift(int a, int b)
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
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 min_shift, int max_shift, int zero_shift)
Calculate LPC coefficients for multiple orders.
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
@ FF_LPC_TYPE_LEVINSON
Levinson-Durbin recursion.
static const uint16_t mask[17]
#define MKBETAG(a, b, c, d)
static av_const int sign_extend(int val, unsigned bits)
Memory handling functions.
static void put_sbits(PutBitContext *pb, int n, int32_t value)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static int put_bytes_output(const PutBitContext *s)
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
enum AVSampleFormat sample_fmt
audio sample format
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
int sample_rate
samples per second
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
int frame_size
Number of samples per channel in an audio frame.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
int interlacing_leftweight
int frame_size
current frame size
int verbatim
current frame verbatim mode flag
int32_t predictor_buf[2][DEFAULT_FRAME_SIZE]
int32_t sample_buf[2][DEFAULT_FRAME_SIZE]
int lpc_coeff[ALAC_MAX_LPC_ORDER+1]