29 #define DEFAULT_FRAME_SIZE 4096
30 #define ALAC_EXTRADATA_SIZE 36
31 #define ALAC_FRAME_HEADER_SIZE 55
32 #define ALAC_FRAME_FOOTER_SIZE 3
34 #define ALAC_ESCAPE_CODE 0x1FF
35 #define ALAC_MAX_LPC_ORDER 30
36 #define DEFAULT_MAX_PRED_ORDER 6
37 #define DEFAULT_MIN_PRED_ORDER 4
38 #define ALAC_MAX_LPC_PRECISION 9
39 #define ALAC_MAX_LPC_SHIFT 9
41 #define ALAC_CHMODE_LEFT_RIGHT 0
42 #define ALAC_CHMODE_LEFT_SIDE 1
43 #define ALAC_CHMODE_RIGHT_SIDE 2
44 #define ALAC_CHMODE_MID_SIDE 3
87 #define COPY_SAMPLES(type) do { \
88 for (ch = 0; ch < channels; ch++) { \
89 int32_t *bptr = s->sample_buf[ch]; \
90 const type *sptr = (const type *)samples[ch]; \
91 for (i = 0; i < s->frame_size; i++) \
92 bptr[i] = sptr[i] >> shift; \
103 int k,
int write_sample_size)
108 divisor = (1<<k) - 1;
175 memcpy(s->
lpc[ch].
lpc_coeff, coefs[opt_order-1], opt_order*
sizeof(
int));
187 sum[0] = sum[1] = sum[2] = sum[3] = 0;
188 for (i = 2; i <
n; i++) {
189 lt = left_ch[i] - 2 * left_ch[i - 1] + left_ch[i - 2];
190 rt = right_ch[i] - 2 * right_ch[i - 1] + right_ch[i - 2];
191 sum[2] +=
FFABS((lt + rt) >> 1);
192 sum[3] +=
FFABS(lt - rt);
198 score[0] = sum[0] + sum[1];
199 score[1] = sum[0] + sum[3];
200 score[2] = sum[1] + sum[3];
201 score[3] = sum[2] + sum[3];
205 for (i = 1; i < 4; i++) {
206 if (score[i] < score[best])
226 for (i = 0; i <
n; i++)
227 right[i] = left[i] - right[i];
232 for (i = 0; i <
n; i++) {
234 right[i] = left[i] - right[i];
235 left[i] = tmp + (right[i] >> 31);
241 for (i = 0; i <
n; i++) {
243 left[i] = (tmp + right[i]) >> 1;
244 right[i] = tmp - right[i];
275 residual[0] = samples[0];
281 int sum = 1 << (lpc.
lpc_quant - 1), res_val, j;
284 sum += (samples[lpc.
lpc_order-j] - samples[0]) *
292 res_val = residual[i];
296 int neg = (res_val < 0);
298 while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
300 int sign = (val ?
FFSIGN(val) : 0);
319 int sign_modifier = 0, i, k;
325 k =
av_log2((history >> 9) + 3);
327 x = -2 * (*samples) -1;
343 unsigned int block_size = 0;
345 k = 7 -
av_log2(history) + ((history + 16) >> 6);
347 while (*samples == 0 && i < s->frame_size) {
353 sign_modifier = (block_size <= 0xFFFF);
364 uint8_t const *samples[2] = { samples0, samples1 };
366 int prediction_type = 0;
369 channels = element ==
TYPE_CPE ? 2 : 1;
379 for (j = 0; j < channels; j++)
381 samples_s32[j][i] >> shift);
383 int16_t
const *samples_s16[2] = { (
const int16_t *)samples0,
384 (
const int16_t *)samples1 };
386 for (j = 0; j < channels; j++)
400 for (j = 0; j < channels; j++) {
404 extra[i] = smp[i] &
mask;
417 for (i = 0; i < channels; i++) {
433 for (j = 0; j < channels; j++) {
440 for (i = 0; i < channels; i++) {
444 if (prediction_type == 15) {
448 residual[j] -= residual[j - 1];
461 int ch, element, sce, cpe;
465 ch = element = sce = cpe = 0;
466 while (ch < s->avctx->channels) {
467 if (ch_elements[element] ==
TYPE_CPE) {
469 samples[ch_map[ch + 1]]);
489 return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
587 "invalid prediction orders: min=%d max=%d\n",
611 int out_bytes, max_frame_size,
ret;
635 if (out_bytes > max_frame_size) {
642 avpkt->
size = out_bytes;
#define DEFAULT_MIN_PRED_ORDER
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
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.
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)
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 CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
#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.
Libavcodec external API header.
static void alac_entropy_coder(AlacEncodeContext *s, int ch)
int frame_size
current frame size
static int put_bits_count(PutBitContext *s)
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
int verbatim
current frame verbatim mode flag
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]
#define ALAC_CHMODE_LEFT_SIDE
#define ALAC_MAX_LPC_ORDER
int frame_size
Number of samples per channel in an audio frame.
int32_t sample_buf[2][DEFAULT_FRAME_SIZE]
AVSampleFormat
Audio sample formats.
int sample_rate
samples per second
#define ALAC_MAX_LPC_SHIFT
main external API structure.
Levinson-Durbin recursion.
int32_t predictor_buf[2][DEFAULT_FRAME_SIZE]
static void init_sample_buffers(AlacEncodeContext *s, int channels, uint8_t const *samples[2])
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)
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)
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)
int lpc_coeff[ALAC_MAX_LPC_ORDER+1]
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)