31 #define DEFAULT_FRAME_SIZE 4096
32 #define ALAC_EXTRADATA_SIZE 36
33 #define ALAC_FRAME_HEADER_SIZE 55
34 #define ALAC_FRAME_FOOTER_SIZE 3
36 #define ALAC_ESCAPE_CODE 0x1FF
37 #define ALAC_MAX_LPC_ORDER 30
38 #define DEFAULT_MAX_PRED_ORDER 6
39 #define DEFAULT_MIN_PRED_ORDER 4
40 #define ALAC_MAX_LPC_PRECISION 9
41 #define ALAC_MIN_LPC_SHIFT 0
42 #define ALAC_MAX_LPC_SHIFT 9
44 #define ALAC_CHMODE_LEFT_RIGHT 0
45 #define ALAC_CHMODE_LEFT_SIDE 1
46 #define ALAC_CHMODE_RIGHT_SIDE 2
47 #define ALAC_CHMODE_MID_SIDE 3
89 s->avctx->bits_per_raw_sample;
91 #define COPY_SAMPLES(type) do { \
92 for (ch = 0; ch < channels; ch++) { \
93 int32_t *bptr = s->sample_buf[ch]; \
94 const type *sptr = (const type *)samples[ch]; \
95 for (i = 0; i < s->frame_size; i++) \
96 bptr[i] = sptr[i] >> shift; \
107 int k,
int write_sample_size)
111 k =
FFMIN(k,
s->rc.k_modifier);
112 divisor = (1<<k) - 1;
119 put_bits(&
s->pbctx, write_sample_size, x);
159 if (
s->compression_level == 1) {
160 s->lpc[ch].lpc_order = 6;
161 s->lpc[ch].lpc_quant = 6;
162 s->lpc[ch].lpc_coeff[0] = 160;
163 s->lpc[ch].lpc_coeff[1] = -190;
164 s->lpc[ch].lpc_coeff[2] = 170;
165 s->lpc[ch].lpc_coeff[3] = -130;
166 s->lpc[ch].lpc_coeff[4] = 80;
167 s->lpc[ch].lpc_coeff[5] = -25;
171 s->min_prediction_order,
172 s->max_prediction_order,
178 s->lpc[ch].lpc_order = opt_order;
179 s->lpc[ch].lpc_quant =
shift[opt_order-1];
180 memcpy(
s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*
sizeof(
int));
192 sum[0] = sum[1] = sum[2] = sum[3] = 0;
193 for (
i = 2;
i < n;
i++) {
194 lt = left_ch[
i] - 2 * left_ch[
i - 1] + left_ch[
i - 2];
195 rt = right_ch[
i] - 2 * right_ch[
i - 1] + right_ch[
i - 2];
196 sum[2] +=
FFABS((lt + rt) >> 1);
197 sum[3] +=
FFABS(lt - rt);
203 score[0] = sum[0] + sum[1];
204 score[1] = sum[0] + sum[3];
205 score[2] = sum[1] + sum[3];
206 score[3] = sum[2] + sum[3];
210 for (
i = 1;
i < 4;
i++) {
211 if (score[
i] < score[best])
219 int32_t *left =
s->sample_buf[0], *right =
s->sample_buf[1];
220 int i,
mode, n =
s->frame_size;
227 s->interlacing_leftweight = 0;
228 s->interlacing_shift = 0;
231 for (
i = 0;
i < n;
i++)
232 right[
i] = left[
i] - right[
i];
233 s->interlacing_leftweight = 1;
234 s->interlacing_shift = 0;
237 for (
i = 0;
i < n;
i++) {
239 right[
i] = left[
i] - right[
i];
240 left[
i] =
tmp + (right[
i] >> 31);
242 s->interlacing_leftweight = 1;
243 s->interlacing_shift = 31;
246 for (
i = 0;
i < n;
i++) {
248 left[
i] = (
tmp + right[
i]) >> 1;
249 right[
i] =
tmp - right[
i];
251 s->interlacing_leftweight = 1;
252 s->interlacing_shift = 1;
261 int32_t *residual =
s->predictor_buf[ch];
264 residual[0] =
s->sample_buf[ch][0];
266 for (
i = 1;
i <
s->frame_size;
i++) {
267 residual[
i] =
s->sample_buf[ch][
i ] -
268 s->sample_buf[ch][
i - 1];
277 int32_t *samples =
s->sample_buf[ch];
280 residual[0] = samples[0];
282 residual[
i] =
sign_extend(samples[
i] - samples[
i-1],
s->write_sample_size);
286 int sum = 1 << (lpc.
lpc_quant - 1), res_val, j;
289 sum += (samples[lpc.
lpc_order-j] - samples[0]) *
296 s->write_sample_size);
297 res_val = residual[
i];
301 int neg = (res_val < 0);
303 while (
index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
323 unsigned int history =
s->rc.initial_history;
324 int sign_modifier = 0,
i, k;
325 int32_t *samples =
s->predictor_buf[ch];
327 for (
i = 0;
i <
s->frame_size;) {
330 k =
av_log2((history >> 9) + 3);
332 x = -2 * (*samples) -1;
340 history += x *
s->rc.history_mult -
341 ((history *
s->rc.history_mult) >> 9);
348 unsigned int block_size = 0;
350 k = 7 -
av_log2(history) + ((history + 16) >> 6);
358 sign_modifier = (block_size <= 0xFFFF);
369 const uint8_t *samples[2] = { samples0, samples1 };
371 int prediction_type = 0;
380 int shift = 32 -
s->avctx->bits_per_raw_sample;
383 for (
i = 0;
i <
s->frame_size;
i++)
386 samples_s32[j][
i] >>
shift);
388 const int16_t *samples_s16[2] = { (
const int16_t *)samples0,
389 (
const int16_t *)samples1 };
390 for (
i = 0;
i <
s->frame_size;
i++)
396 s->write_sample_size =
s->avctx->bits_per_raw_sample -
s->extra_bits +
404 uint32_t
mask = (1 <<
s->extra_bits) - 1;
406 int32_t *extra =
s->predictor_buf[j];
408 for (
i = 0;
i <
s->frame_size;
i++) {
410 smp[
i] >>=
s->extra_bits;
418 s->interlacing_shift =
s->interlacing_leftweight = 0;
420 put_bits(pb, 8,
s->interlacing_leftweight);
431 for (j = 0; j <
s->lpc[
i].lpc_order; j++)
437 for (
i = 0;
i <
s->frame_size;
i++) {
439 put_bits(pb,
s->extra_bits,
s->predictor_buf[j][
i]);
449 if (prediction_type == 15) {
452 for (j =
s->frame_size - 1; j > 0; j--)
453 residual[j] -= residual[j - 1];
466 int ch, element, sce, cpe;
470 ch = element = sce = cpe = 0;
471 while (ch < s->avctx->channels) {
472 if (ch_elements[element] ==
TYPE_CPE) {
474 samples[ch_map[ch + 1]]);
523 s->compression_level = 2;
528 s->rc.history_mult = 40;
529 s->rc.initial_history = 10;
530 s->rc.k_modifier = 14;
531 s->rc.rice_modifier = 4;
548 AV_WB32(alac_extradata+24,
s->max_coded_frame_size);
554 if (
s->compression_level > 0) {
555 AV_WB8(alac_extradata+18,
s->rc.history_mult);
556 AV_WB8(alac_extradata+19,
s->rc.initial_history);
557 AV_WB8(alac_extradata+20,
s->rc.k_modifier);
560 #if FF_API_PRIVATE_OPT
586 if (
s->max_prediction_order <
s->min_prediction_order) {
588 "invalid prediction orders: min=%d max=%d\n",
589 s->min_prediction_order,
s->max_prediction_order);
596 s->max_prediction_order,
608 int out_bytes, max_frame_size, ret;
616 max_frame_size =
s->max_coded_frame_size;
622 if (
s->compression_level) {
632 if (out_bytes > max_frame_size) {
639 avpkt->
size = out_bytes;
644 #define OFFSET(x) offsetof(AlacEncodeContext, x)
645 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
static enum AVSampleFormat sample_fmts[]
static double val(void *priv, double ch)
enum AlacRawDataBlockType ff_alac_channel_elements[ALAC_MAX_CHANNELS][5]
const uint8_t ff_alac_channel_layout_offsets[ALAC_MAX_CHANNELS][ALAC_MAX_CHANNELS]
const uint64_t ff_alac_channel_layouts[ALAC_MAX_CHANNELS+1]
#define ALAC_CHMODE_LEFT_RIGHT
#define COPY_SAMPLES(type)
#define ALAC_MAX_LPC_ORDER
static const AVOption options[]
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_EXTRADATA_SIZE
#define ALAC_CHMODE_RIGHT_SIDE
Libavcodec external API header.
#define FF_COMPRESSION_DEFAULT
static av_cold int init(AVCodecContext *avctx)
#define MKBETAG(a, b, c, d)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static const uint16_t channel_layouts[7]
mode
Use these values in ebur128_init (or'ed).
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
#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.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
AVSampleFormat
Audio sample formats.
@ 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
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
#define FF_DISABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
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]
static av_const int sign_extend(int val, unsigned bits)
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 av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
static int put_bits_count(PutBitContext *s)
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static int shift(int a, int b)
Describe the class of an AVClass context structure.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
main external API structure.
attribute_deprecated int min_prediction_order
enum AVSampleFormat sample_fmt
audio sample format
attribute_deprecated int max_prediction_order
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
int sample_rate
samples per second
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int channels
number of audio channels
int frame_size
Number of samples per channel in an audio frame.
const char * name
Name of the codec implementation.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
uint8_t ** extended_data
pointers to the data planes/channels.
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]