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_MAX_LPC_SHIFT 9
43 #define ALAC_CHMODE_LEFT_RIGHT 0
44 #define ALAC_CHMODE_LEFT_SIDE 1
45 #define ALAC_CHMODE_RIGHT_SIDE 2
46 #define ALAC_CHMODE_MID_SIDE 3
90 #define COPY_SAMPLES(type) do { \
91 for (ch = 0; ch < channels; ch++) { \
92 int32_t *bptr = s->sample_buf[ch]; \
93 const type *sptr = (const type *)samples[ch]; \
94 for (i = 0; i < s->frame_size; i++) \
95 bptr[i] = sptr[i] >> shift; \
106 int k,
int write_sample_size)
111 divisor = (1<<k) - 1;
178 memcpy(s->
lpc[ch].
lpc_coeff, coefs[opt_order-1], opt_order*
sizeof(
int));
190 sum[0] = sum[1] = sum[2] = sum[3] = 0;
191 for (i = 2; i <
n; i++) {
192 lt = left_ch[i] - 2 * left_ch[i - 1] + left_ch[i - 2];
193 rt = right_ch[i] - 2 * right_ch[i - 1] + right_ch[i - 2];
194 sum[2] +=
FFABS((lt + rt) >> 1);
195 sum[3] +=
FFABS(lt - rt);
201 score[0] = sum[0] + sum[1];
202 score[1] = sum[0] + sum[3];
203 score[2] = sum[1] + sum[3];
204 score[3] = sum[2] + sum[3];
208 for (i = 1; i < 4; i++) {
209 if (score[i] < score[best])
229 for (i = 0; i <
n; i++)
230 right[i] = left[i] - right[i];
235 for (i = 0; i <
n; i++) {
237 right[i] = left[i] - right[i];
238 left[i] = tmp + (right[i] >> 31);
244 for (i = 0; i <
n; i++) {
246 left[i] = (tmp + right[i]) >> 1;
247 right[i] = tmp - right[i];
278 residual[0] = samples[0];
284 int sum = 1 << (lpc.
lpc_quant - 1), res_val, j;
287 sum += (samples[lpc.
lpc_order-j] - samples[0]) *
295 res_val = residual[i];
299 int neg = (res_val < 0);
301 while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
303 int sign = (val ?
FFSIGN(val) : 0);
322 int sign_modifier = 0, i, k;
328 k =
av_log2((history >> 9) + 3);
330 x = -2 * (*samples) -1;
346 unsigned int block_size = 0;
348 k = 7 -
av_log2(history) + ((history + 16) >> 6);
350 while (*samples == 0 && i < s->frame_size) {
356 sign_modifier = (block_size <= 0xFFFF);
367 const uint8_t *samples[2] = { samples0, samples1 };
369 int prediction_type = 0;
372 channels = element ==
TYPE_CPE ? 2 : 1;
382 for (j = 0; j < channels; j++)
384 samples_s32[j][i] >> shift);
386 const int16_t *samples_s16[2] = { (
const int16_t *)samples0,
387 (
const int16_t *)samples1 };
389 for (j = 0; j < channels; j++)
403 for (j = 0; j < channels; j++) {
407 extra[i] = smp[i] &
mask;
420 for (i = 0; i < channels; i++) {
436 for (j = 0; j < channels; j++) {
443 for (i = 0; i < channels; i++) {
447 if (prediction_type == 15) {
451 residual[j] -= residual[j - 1];
464 int ch, element, sce, cpe;
468 ch = element = sce = cpe = 0;
469 while (ch < s->avctx->channels) {
470 if (ch_elements[element] ==
TYPE_CPE) {
472 samples[ch_map[ch + 1]]);
492 return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
562 #if FF_API_PRIVATE_OPT
592 "invalid prediction orders: min=%d max=%d\n",
616 int out_bytes, max_frame_size, ret;
640 if (out_bytes > max_frame_size) {
647 avpkt->
size = out_bytes;
652 #define OFFSET(x) offsetof(AlacEncodeContext, x)
653 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
674 .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
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)
#define COPY_SAMPLES(type)
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
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.
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.
#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
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)