X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Falac.c;h=67c02ab6af0e034af69eee79ed6ea864acf5ece0;hb=e625ae609206e0550ff733965c6f5447579320aa;hp=6086e2caa895881f4e7084bf6803f4ced9bc1506;hpb=ae3d6a337ad25527bcd3172e3885e45fadf9908c;p=ffmpeg diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 6086e2caa89..67c02ab6af0 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -215,20 +215,20 @@ static void lpc_prediction(int32_t *error_buffer, uint32_t *buffer_out, /* 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 && (int)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); } } } @@ -250,10 +250,12 @@ static int decode_element(AVCodecContext *avctx, AVFrame *frame, int ch_index, 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); @@ -300,6 +302,9 @@ static int decode_element(AVCodecContext *avctx, AVFrame *frame, int ch_index, 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); @@ -395,13 +400,13 @@ static int decode_element(AVCodecContext *avctx, AVFrame *frame, int ch_index, 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; } @@ -487,6 +492,7 @@ static int allocate_buffers(ALACContext *alac) { int ch; 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; @@ -495,22 +501,19 @@ static int allocate_buffers(ALACContext *alac) } 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) @@ -599,15 +602,6 @@ static av_cold int alac_decode_init(AVCodecContext * avctx) 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 }, @@ -622,7 +616,7 @@ static const AVClass alac_class = { .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, @@ -631,7 +625,7 @@ AVCodec ff_alac_decoder = { .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 };