Data Structures | Macros | Functions | Variables
alacenc.c File Reference
#include "libavutil/opt.h"
#include "avcodec.h"
#include "put_bits.h"
#include "internal.h"
#include "lpc.h"
#include "mathops.h"
#include "alac_data.h"

Go to the source code of this file.

Data Structures

struct  RiceContext
struct  AlacLPCContext
struct  AlacEncodeContext


#define DEFAULT_FRAME_SIZE   4096
#define ALAC_ESCAPE_CODE   0x1FF
#define ALAC_MAX_LPC_ORDER   30
#define ALAC_MIN_LPC_SHIFT   0
#define ALAC_MAX_LPC_SHIFT   9
#define COPY_SAMPLES(type)
#define OFFSET(x)   offsetof(AlacEncodeContext, x)


static void init_sample_buffers (AlacEncodeContext *s, int channels, const uint8_t *samples[2])
static void encode_scalar (AlacEncodeContext *s, int x, int k, int write_sample_size)
static void write_element_header (AlacEncodeContext *s, enum AlacRawDataBlockType element, int instance)
static void calc_predictor_params (AlacEncodeContext *s, int ch)
static int estimate_stereo_mode (int32_t *left_ch, int32_t *right_ch, int n)
static void alac_stereo_decorrelation (AlacEncodeContext *s)
static void alac_linear_predictor (AlacEncodeContext *s, int ch)
static void alac_entropy_coder (AlacEncodeContext *s, int ch)
static void write_element (AlacEncodeContext *s, enum AlacRawDataBlockType element, int instance, const uint8_t *samples0, const uint8_t *samples1)
static int write_frame (AlacEncodeContext *s, AVPacket *avpkt, uint8_t *const *samples)
static av_always_inline int get_max_frame_size (int frame_size, int ch, int bps)
static av_cold int alac_encode_close (AVCodecContext *avctx)
static av_cold int alac_encode_init (AVCodecContext *avctx)
static int alac_encode_frame (AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)


static const AVOption options []
static const AVClass alacenc_class
AVCodec ff_alac_encoder

Macro Definition Documentation

#define DEFAULT_FRAME_SIZE   4096

Definition at line 32 of file alacenc.c.

Referenced by alac_encode_init().


Definition at line 33 of file alacenc.c.


Definition at line 34 of file alacenc.c.

#define ALAC_ESCAPE_CODE   0x1FF

Definition at line 36 of file alacenc.c.

Referenced by encode_scalar().

#define ALAC_MAX_LPC_ORDER   30

Definition at line 37 of file alacenc.c.

Referenced by alac_encode_init().


Definition at line 38 of file alacenc.c.


Definition at line 39 of file alacenc.c.


Definition at line 40 of file alacenc.c.

Referenced by calc_predictor_params().

#define ALAC_MIN_LPC_SHIFT   0

Definition at line 41 of file alacenc.c.

Referenced by calc_predictor_params().

#define ALAC_MAX_LPC_SHIFT   9

Definition at line 42 of file alacenc.c.

Referenced by calc_predictor_params().


Definition at line 44 of file alacenc.c.

Referenced by alac_stereo_decorrelation().


Definition at line 45 of file alacenc.c.

Referenced by alac_stereo_decorrelation().


Definition at line 46 of file alacenc.c.

Referenced by alac_stereo_decorrelation().


Definition at line 47 of file alacenc.c.

#define COPY_SAMPLES (   type)
do { \
for (ch = 0; ch < channels; ch++) { \
int32_t *bptr = s->sample_buf[ch]; \
const type *sptr = (const type *)samples[ch]; \
for (i = 0; i < s->frame_size; i++) \
bptr[i] = sptr[i] >> shift; \
} \
} while (0)
static int shift(int a, int b)
Definition: sonic.c:82
GLint GLenum type
Definition: opengl_enc.c:104
Definition: aptx.h:33
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
#define s(width, name)
Definition: cbs_vp9.c:257
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(const int64_t *) pi *(1.0f/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(const int64_t *) pi *(1.0/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(UINT64_C(1)<< 63)))#define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_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_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT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), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};static void cpy1(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, len);}static void cpy2(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 2 *len);}static void cpy4(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 4 *len);}static void cpy8(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags){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) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;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)){case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&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);return ctx;}void swri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len){int ch;int off=0;const int os=(out->planar?1:out->ch_count)*out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){int planes=in->planar?in->ch_count:1;unsigned m=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){int planes=out->planar?out->ch_count:1;unsigned m=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){int planes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
Filter the word “frame” indicates either a video frame or a group of audio samples
for(j=16;j >0;--j)

Referenced by init_sample_buffers().

#define OFFSET (   x)    offsetof(AlacEncodeContext, x)

Definition at line 654 of file alacenc.c.

Definition at line 655 of file alacenc.c.

Referenced by g726_reset().

Function Documentation

static void init_sample_buffers ( AlacEncodeContext s,
int  channels,
const uint8_t samples[2] 

Definition at line 84 of file alacenc.c.

Referenced by write_element().

static void encode_scalar ( AlacEncodeContext s,
int  x,
int  k,
int  write_sample_size 

Definition at line 106 of file alacenc.c.

Referenced by alac_entropy_coder().

static void write_element_header ( AlacEncodeContext s,
enum AlacRawDataBlockType  element,
int  instance 

Definition at line 134 of file alacenc.c.

Referenced by write_element().

static void calc_predictor_params ( AlacEncodeContext s,
int  ch 

Definition at line 153 of file alacenc.c.

Referenced by write_element().

static int estimate_stereo_mode ( int32_t left_ch,
int32_t right_ch,
int  n 

Definition at line 184 of file alacenc.c.

Referenced by alac_stereo_decorrelation().

static void alac_stereo_decorrelation ( AlacEncodeContext s)

Definition at line 217 of file alacenc.c.

Referenced by write_element().

static void alac_linear_predictor ( AlacEncodeContext s,
int  ch 

Definition at line 257 of file alacenc.c.

Referenced by write_element().

static void alac_entropy_coder ( AlacEncodeContext s,
int  ch 

Definition at line 321 of file alacenc.c.

Referenced by write_element().

static void write_element ( AlacEncodeContext s,
enum AlacRawDataBlockType  element,
int  instance,
const uint8_t samples0,
const uint8_t samples1 

Definition at line 365 of file alacenc.c.

Referenced by write_frame().

static int write_frame ( AlacEncodeContext s,
AVPacket avpkt,
uint8_t *const *  samples 

Definition at line 460 of file alacenc.c.

Referenced by alac_encode_frame().

static av_always_inline int get_max_frame_size ( int  frame_size,
int  ch,
int  bps 

Definition at line 491 of file alacenc.c.

Referenced by alac_encode_frame(), and alac_encode_init().

static av_cold int alac_encode_close ( AVCodecContext avctx)

Definition at line 497 of file alacenc.c.

Referenced by alac_encode_init().

static av_cold int alac_encode_init ( AVCodecContext avctx)

Definition at line 506 of file alacenc.c.

static int alac_encode_frame ( AVCodecContext avctx,
AVPacket avpkt,
const AVFrame frame,
int got_packet_ptr 

Definition at line 614 of file alacenc.c.

Variable Documentation

const AVOption options[]
Initial value:
= {
{ "min_prediction_order", NULL, OFFSET(min_prediction_order), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MIN_PRED_ORDER }, MIN_LPC_ORDER, ALAC_MAX_LPC_ORDER, AE },
{ "max_prediction_order", NULL, OFFSET(max_prediction_order), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MAX_PRED_ORDER }, MIN_LPC_ORDER, ALAC_MAX_LPC_ORDER, AE },
{ NULL },
Definition: alacenc.c:39
#define NULL
Definition: coverity.c:32
Definition: alacenc.c:38
#define AE
Definition: alacenc.c:655
Definition: alacenc.c:37
Definition: lpc.h:37
#define OFFSET(x)
Definition: alacenc.c:654

Definition at line 656 of file alacenc.c.

const AVClass alacenc_class
Initial value:
= {
.class_name = "alacenc",
.item_name = av_default_item_name,
.option = options,
Definition: version.h:85
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
static const AVOption options[]
Definition: alacenc.c:656

Definition at line 663 of file alacenc.c.

AVCodec ff_alac_encoder
Initial value:
= {
.name = "alac",
.long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
.priv_data_size = sizeof(AlacEncodeContext),
.priv_class = &alacenc_class,
.encode2 = alac_encode_frame,
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static av_cold int alac_encode_init(AVCodecContext *avctx)
Definition: alacenc.c:506
static const AVClass alacenc_class
Definition: alacenc.c:663
static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: alacenc.c:614
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
Codec can be fed a final frame with a smaller size.
Definition: avcodec.h:435
signed 32 bits, planar
Definition: samplefmt.h:68
const uint64_t ff_alac_channel_layouts[ALAC_MAX_CHANNELS+1]
Definition: alac_data.c:35
Audio sample formats.
Definition: samplefmt.h:58
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:113
static av_cold int alac_encode_close(AVCodecContext *avctx)
Definition: alacenc.c:497
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
signed 16 bits, planar
Definition: samplefmt.h:67

Definition at line 670 of file alacenc.c.