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
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)
112 divisor = (1<<k) - 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])
231 for (i = 0; i <
n; i++)
232 right[i] = left[i] - right[i];
237 for (i = 0; i <
n; i++) {
239 right[i] = left[i] - right[i];
240 left[i] = tmp + (right[i] >> 31);
246 for (i = 0; i <
n; i++) {
248 left[i] = (tmp + right[i]) >> 1;
249 right[i] = tmp - right[i];
280 residual[0] = samples[0];
286 int sum = 1 << (lpc.
lpc_quant - 1), res_val, j;
289 sum += (samples[lpc.
lpc_order-j] - samples[0]) *
297 res_val = residual[i];
301 int neg = (res_val < 0);
303 while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
305 int sign = (val ?
FFSIGN(val) : 0);
324 int sign_modifier = 0, i, k;
330 k =
av_log2((history >> 9) + 3);
332 x = -2 * (*samples) -1;
348 unsigned int block_size = 0;
350 k = 7 -
av_log2(history) + ((history + 16) >> 6);
352 while (*samples == 0 && i < s->frame_size) {
358 sign_modifier = (block_size <= 0xFFFF);
369 const uint8_t *samples[2] = { samples0, samples1 };
371 int prediction_type = 0;
374 channels = element ==
TYPE_CPE ? 2 : 1;
384 for (j = 0; j < channels; j++)
386 samples_s32[j][i] >> shift);
388 const int16_t *samples_s16[2] = { (
const int16_t *)samples0,
389 (
const int16_t *)samples1 };
391 for (j = 0; j < channels; j++)
405 for (j = 0; j < channels; j++) {
409 extra[i] = smp[i] &
mask;
422 for (i = 0; i < channels; i++) {
438 for (j = 0; j < channels; j++) {
445 for (i = 0; i < channels; i++) {
449 if (prediction_type == 15) {
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]]);
494 return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
564 #if FF_API_PRIVATE_OPT
594 "invalid prediction orders: min=%d max=%d\n",
618 int out_bytes, max_frame_size, ret;
642 if (out_bytes > max_frame_size) {
649 avpkt->
size = out_bytes;
654 #define OFFSET(x) offsetof(AlacEncodeContext, x)
655 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
676 .priv_class = &alacenc_class,
#define DEFAULT_MIN_PRED_ORDER
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
const char const char void * val
#define FF_COMPRESSION_DEFAULT
static int shift(int a, int b)
This structure describes decoded (raw) audio or video data.
static void put_sbits(PutBitContext *pb, int n, int32_t value)
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
static av_cold int init(AVCodecContext *avctx)
#define COPY_SAMPLES(type)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
static void write_element_header(AlacEncodeContext *s, enum AlacRawDataBlockType element, int instance)
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
static av_cold int alac_encode_init(AVCodecContext *avctx)
#define DEFAULT_MAX_PRED_ORDER
static void write_element(AlacEncodeContext *s, enum AlacRawDataBlockType element, int instance, const uint8_t *samples0, const uint8_t *samples1)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static const AVClass alacenc_class
enum AVSampleFormat sample_fmt
audio sample format
static void alac_linear_predictor(AlacEncodeContext *s, int ch)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int interlacing_leftweight
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static void calc_predictor_params(AlacEncodeContext *s, int ch)
static const uint16_t mask[17]
static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
#define ALAC_MAX_LPC_PRECISION
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
const char * name
Name of the codec implementation.
static void alac_entropy_coder(AlacEncodeContext *s, int ch)
int frame_size
current frame size
static int put_bits_count(PutBitContext *s)
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
int verbatim
current frame verbatim mode flag
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
const uint64_t ff_alac_channel_layouts[ALAC_MAX_CHANNELS+1]
static const AVOption options[]
#define ALAC_CHMODE_LEFT_SIDE
#define ALAC_MAX_LPC_ORDER
attribute_deprecated int max_prediction_order
#define ALAC_MIN_LPC_SHIFT
int frame_size
Number of samples per channel in an audio frame.
int32_t sample_buf[2][DEFAULT_FRAME_SIZE]
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
int sample_rate
samples per second
#define ALAC_MAX_LPC_SHIFT
main external API structure.
Levinson-Durbin recursion.
static const uint16_t channel_layouts[7]
int32_t predictor_buf[2][DEFAULT_FRAME_SIZE]
Describe the class of an AVClass context structure.
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate 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 min_shift, int max_shift, int zero_shift)
Calculate LPC coefficients for multiple orders.
const uint8_t ff_alac_channel_layout_offsets[ALAC_MAX_CHANNELS][ALAC_MAX_CHANNELS]
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
static int write_frame(AlacEncodeContext *s, AVPacket *avpkt, uint8_t *const *samples)
static av_const int sign_extend(int val, unsigned bits)
static void init_sample_buffers(AlacEncodeContext *s, int channels, const uint8_t *samples[2])
enum AlacRawDataBlockType ff_alac_channel_elements[ALAC_MAX_CHANNELS][5]
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
#define ALAC_CHMODE_LEFT_RIGHT
#define ALAC_CHMODE_RIGHT_SIDE
static av_cold int alac_encode_close(AVCodecContext *avctx)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_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T_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_YASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
#define MKBETAG(a, b, c, d)
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int lpc_coeff[ALAC_MAX_LPC_ORDER+1]
#define FF_ENABLE_DEPRECATION_WARNINGS
attribute_deprecated int min_prediction_order
int channels
number of audio channels
static enum AVSampleFormat sample_fmts[]
#define ALAC_EXTRADATA_SIZE
uint8_t ** extended_data
pointers to the data planes/channels.
#define DEFAULT_FRAME_SIZE
This structure stores compressed data.
static void alac_stereo_decorrelation(AlacEncodeContext *s)
int nb_samples
number of audio samples (per channel) described by this frame
static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)