return v ? FFSIGN(v) : 0;
}
-static void lpc_prediction(int32_t *error_buffer, int32_t *buffer_out,
+static void lpc_prediction(int32_t *error_buffer, uint32_t *buffer_out,
int nb_samples, int bps, int16_t *lpc_coefs,
int lpc_order, int lpc_quant)
{
int i;
- int32_t *pred = buffer_out;
+ uint32_t *pred = buffer_out;
/* first sample always copies */
*buffer_out = *error_buffer;
for (; i < nb_samples; i++) {
int j;
int val = 0;
- int error_val = error_buffer[i];
+ unsigned error_val = error_buffer[i];
int error_sign;
int d = *pred++;
/* LPC prediction */
for (j = 0; j < lpc_order; j++)
val += (pred[j] - d) * lpc_coefs[j];
- val = (val + (1 << (lpc_quant - 1))) >> lpc_quant;
+ val = (val + (1LL << (lpc_quant - 1))) >> lpc_quant;
val += d + error_val;
buffer_out[i] = sign_extend(val, bps);
/* adapt LPC coefficients */
error_sign = sign_only(error_val);
if (error_sign) {
- for (j = 0; j < lpc_order && error_val * error_sign > 0; j++) {
+ for (j = 0; j < lpc_order && (int)(error_val * error_sign) > 0; j++) {
int sign;
val = d - pred[j];
sign = sign_only(val) * error_sign;
lpc_coefs[j] -= sign;
- val *= sign;
- error_val -= (val >> lpc_quant) * (j + 1);
+ val *= (unsigned)sign;
+ error_val -= (val >> lpc_quant) * (j + 1U);
}
}
}
alac->extra_bits = get_bits(&alac->gb, 2) << 3;
bps = alac->sample_size - alac->extra_bits + channels - 1;
- if (bps > 32U) {
+ if (bps > 32) {
avpriv_report_missing_feature(avctx, "bps %d", bps);
return AVERROR_PATCHWELCOME;
}
+ if (bps < 1)
+ return AVERROR_INVALIDDATA;
/* whether the frame is compressed */
is_compressed = !get_bits1(&alac->gb);
decorr_shift = get_bits(&alac->gb, 8);
decorr_left_weight = get_bits(&alac->gb, 8);
+ if (channels == 2 && decorr_left_weight && decorr_shift > 31)
+ return AVERROR_INVALIDDATA;
+
for (ch = 0; ch < channels; ch++) {
prediction_type[ch] = get_bits(&alac->gb, 4);
lpc_quant[ch] = get_bits(&alac->gb, 4);
rice_history_mult[ch] = get_bits(&alac->gb, 3);
lpc_order[ch] = get_bits(&alac->gb, 5);
- if (lpc_order[ch] >= alac->max_samples_per_frame)
+ if (lpc_order[ch] >= alac->max_samples_per_frame || !lpc_quant[ch])
return AVERROR_INVALIDDATA;
/* read the predictor table */
case 20: {
for (ch = 0; ch < channels; ch++) {
for (i = 0; i < alac->nb_samples; i++)
- alac->output_samples_buffer[ch][i] <<= 12;
+ alac->output_samples_buffer[ch][i] *= 1U << 12;
}}
break;
case 24: {
for (ch = 0; ch < channels; ch++) {
for (i = 0; i < alac->nb_samples; i++)
- alac->output_samples_buffer[ch][i] <<= 8;
+ alac->output_samples_buffer[ch][i] *= 1U << 8;
}}
break;
}
static int allocate_buffers(ALACContext *alac)
{
int ch;
- int buf_size = alac->max_samples_per_frame * sizeof(int32_t);
+ unsigned buf_size = alac->max_samples_per_frame * sizeof(int32_t);
+ unsigned extra_buf_size = buf_size + AV_INPUT_BUFFER_PADDING_SIZE;
for (ch = 0; ch < 2; ch++) {
alac->predict_error_buffer[ch] = NULL;
}
for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) {
- FF_ALLOC_OR_GOTO(alac->avctx, alac->predict_error_buffer[ch],
- buf_size, buf_alloc_fail);
+ if (!(alac->predict_error_buffer[ch] = av_malloc(buf_size)))
+ return AVERROR(ENOMEM);
alac->direct_output = alac->sample_size > 16;
if (!alac->direct_output) {
- FF_ALLOC_OR_GOTO(alac->avctx, alac->output_samples_buffer[ch],
- buf_size + AV_INPUT_BUFFER_PADDING_SIZE, buf_alloc_fail);
+ if (!(alac->output_samples_buffer[ch] = av_malloc(extra_buf_size)))
+ return AVERROR(ENOMEM);
}
- FF_ALLOC_OR_GOTO(alac->avctx, alac->extra_bits_buffer[ch],
- buf_size + AV_INPUT_BUFFER_PADDING_SIZE, buf_alloc_fail);
+ if (!(alac->extra_bits_buffer[ch] = av_malloc(extra_buf_size)))
+ return AVERROR(ENOMEM);
}
return 0;
-buf_alloc_fail:
- alac_decode_close(alac->avctx);
- return AVERROR(ENOMEM);
}
static int alac_set_info(ALACContext *alac)
return 0;
}
-#if HAVE_THREADS
-static int init_thread_copy(AVCodecContext *avctx)
-{
- ALACContext *alac = avctx->priv_data;
- alac->avctx = avctx;
- return allocate_buffers(alac);
-}
-#endif
-
static const AVOption options[] = {
{ "extra_bits_bug", "Force non-standard decoding process",
offsetof(ALACContext, extra_bit_bug), AV_OPT_TYPE_BOOL, { .i64 = 0 },
.version = LIBAVUTIL_VERSION_INT,
};
-AVCodec ff_alac_decoder = {
+const AVCodec ff_alac_decoder = {
.name = "alac",
.long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
.type = AVMEDIA_TYPE_AUDIO,
.init = alac_decode_init,
.close = alac_decode_close,
.decode = alac_decode_frame,
- .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
- .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
+ .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_CHANNEL_CONF,
+ .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
.priv_class = &alac_class
};