Commit 53df079a authored by Justin Ruggles's avatar Justin Ruggles

alacdec: check for buffer allocation failure.

Also rearranges some functions for easier cleanup on failure.
parent e5e4f92b
...@@ -85,49 +85,6 @@ typedef struct { ...@@ -85,49 +85,6 @@ typedef struct {
int wasted_bits; int wasted_bits;
} ALACContext; } ALACContext;
static void allocate_buffers(ALACContext *alac)
{
int chan;
for (chan = 0; chan < alac->numchannels; chan++) {
alac->predicterror_buffer[chan] =
av_malloc(alac->setinfo_max_samples_per_frame * 4);
alac->outputsamples_buffer[chan] =
av_malloc(alac->setinfo_max_samples_per_frame * 4);
alac->wasted_bits_buffer[chan] = av_malloc(alac->setinfo_max_samples_per_frame * 4);
}
}
static int alac_set_info(ALACContext *alac)
{
const unsigned char *ptr = alac->avctx->extradata;
ptr += 4; /* size */
ptr += 4; /* alac */
ptr += 4; /* 0 ? */
if(AV_RB32(ptr) >= UINT_MAX/4){
av_log(alac->avctx, AV_LOG_ERROR, "setinfo_max_samples_per_frame too large\n");
return -1;
}
/* buffer size / 2 ? */
alac->setinfo_max_samples_per_frame = bytestream_get_be32(&ptr);
ptr++; /* ??? */
alac->setinfo_sample_size = *ptr++;
alac->setinfo_rice_historymult = *ptr++;
alac->setinfo_rice_initialhistory = *ptr++;
alac->setinfo_rice_kmodifier = *ptr++;
alac->numchannels = *ptr++;
bytestream_get_be16(&ptr); /* ??? */
bytestream_get_be32(&ptr); /* max coded frame size */
bytestream_get_be32(&ptr); /* bitrate ? */
bytestream_get_be32(&ptr); /* samplerate */
return 0;
}
static inline int decode_scalar(GetBitContext *gb, int k, int limit, int readsamplesize){ static inline int decode_scalar(GetBitContext *gb, int k, int limit, int readsamplesize){
/* read x - number of 1s before 0 represent the rice */ /* read x - number of 1s before 0 represent the rice */
int x = get_unary_0_9(gb); int x = get_unary_0_9(gb);
...@@ -627,8 +584,74 @@ static int alac_decode_frame(AVCodecContext *avctx, ...@@ -627,8 +584,74 @@ static int alac_decode_frame(AVCodecContext *avctx,
return input_buffer_size; return input_buffer_size;
} }
static av_cold int alac_decode_close(AVCodecContext *avctx)
{
ALACContext *alac = avctx->priv_data;
int chan;
for (chan = 0; chan < alac->numchannels; chan++) {
av_freep(&alac->predicterror_buffer[chan]);
av_freep(&alac->outputsamples_buffer[chan]);
av_freep(&alac->wasted_bits_buffer[chan]);
}
return 0;
}
static int allocate_buffers(ALACContext *alac)
{
int chan;
for (chan = 0; chan < alac->numchannels; chan++) {
alac->predicterror_buffer[chan] =
av_malloc(alac->setinfo_max_samples_per_frame * 4);
alac->outputsamples_buffer[chan] =
av_malloc(alac->setinfo_max_samples_per_frame * 4);
alac->wasted_bits_buffer[chan] = av_malloc(alac->setinfo_max_samples_per_frame * 4);
if (!alac->predicterror_buffer[chan] ||
!alac->outputsamples_buffer[chan] ||
!alac->wasted_bits_buffer[chan]) {
alac_decode_close(alac->avctx);
return AVERROR(ENOMEM);
}
}
return 0;
}
static int alac_set_info(ALACContext *alac)
{
const unsigned char *ptr = alac->avctx->extradata;
ptr += 4; /* size */
ptr += 4; /* alac */
ptr += 4; /* 0 ? */
if(AV_RB32(ptr) >= UINT_MAX/4){
av_log(alac->avctx, AV_LOG_ERROR, "setinfo_max_samples_per_frame too large\n");
return -1;
}
/* buffer size / 2 ? */
alac->setinfo_max_samples_per_frame = bytestream_get_be32(&ptr);
ptr++; /* ??? */
alac->setinfo_sample_size = *ptr++;
alac->setinfo_rice_historymult = *ptr++;
alac->setinfo_rice_initialhistory = *ptr++;
alac->setinfo_rice_kmodifier = *ptr++;
alac->numchannels = *ptr++;
bytestream_get_be16(&ptr); /* ??? */
bytestream_get_be32(&ptr); /* max coded frame size */
bytestream_get_be32(&ptr); /* bitrate ? */
bytestream_get_be32(&ptr); /* samplerate */
return 0;
}
static av_cold int alac_decode_init(AVCodecContext * avctx) static av_cold int alac_decode_init(AVCodecContext * avctx)
{ {
int ret;
ALACContext *alac = avctx->priv_data; ALACContext *alac = avctx->priv_data;
alac->avctx = avctx; alac->avctx = avctx;
...@@ -668,20 +691,9 @@ static av_cold int alac_decode_init(AVCodecContext * avctx) ...@@ -668,20 +691,9 @@ static av_cold int alac_decode_init(AVCodecContext * avctx)
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
} }
allocate_buffers(alac); if ((ret = allocate_buffers(alac)) < 0) {
av_log(avctx, AV_LOG_ERROR, "Error allocating buffers\n");
return 0; return ret;
}
static av_cold int alac_decode_close(AVCodecContext *avctx)
{
ALACContext *alac = avctx->priv_data;
int chan;
for (chan = 0; chan < alac->numchannels; chan++) {
av_freep(&alac->predicterror_buffer[chan]);
av_freep(&alac->outputsamples_buffer[chan]);
av_freep(&alac->wasted_bits_buffer[chan]);
} }
return 0; return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment