Skip to content

Commit

Permalink
avformat: Forward errors where possible
Browse files Browse the repository at this point in the history
It is not uncommon to find code where the caller thinks to know better
what the return value should be than the callee. E.g. something like
"if (av_new_packet(pkt, size) < 0) return AVERROR(ENOMEM);". This commit
changes several instances of this to instead forward the actual error.

Signed-off-by: Andreas Rheinhardt <[email protected]>
Signed-off-by: Michael Niedermayer <[email protected]>
  • Loading branch information
mkver authored and michaelni committed Dec 12, 2019
1 parent cb88cdf commit c1e439d
Show file tree
Hide file tree
Showing 71 changed files with 310 additions and 275 deletions.
6 changes: 3 additions & 3 deletions libavformat/adxdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static int adx_read_header(AVFormatContext *s)
{
ADXDemuxerContext *c = s->priv_data;
AVCodecParameters *par;

int ret;
AVStream *st = avformat_new_stream(s, NULL);
if (!st)
return AVERROR(ENOMEM);
Expand All @@ -94,8 +94,8 @@ static int adx_read_header(AVFormatContext *s)
c->header_size = avio_rb16(s->pb) + 4;
avio_seek(s->pb, -4, SEEK_CUR);

if (ff_get_extradata(s, par, s->pb, c->header_size) < 0)
return AVERROR(ENOMEM);
if ((ret = ff_get_extradata(s, par, s->pb, c->header_size)) < 0)
return ret;

if (par->extradata_size < 12) {
av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
Expand Down
5 changes: 3 additions & 2 deletions libavformat/afc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static int afc_read_header(AVFormatContext *s)
{
AFCDemuxContext *c = s->priv_data;
AVStream *st;
int ret;

st = avformat_new_stream(s, NULL);
if (!st)
Expand All @@ -40,8 +41,8 @@ static int afc_read_header(AVFormatContext *s)
st->codecpar->channels = 2;
st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;

if (ff_alloc_extradata(st->codecpar, 1))
return AVERROR(ENOMEM);
if ((ret = ff_alloc_extradata(st->codecpar, 1)) < 0)
return ret;
st->codecpar->extradata[0] = 8 * st->codecpar->channels;

c->data_end = avio_rb32(s->pb) + 32LL;
Expand Down
8 changes: 4 additions & 4 deletions libavformat/aiffdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ static int aiff_read_header(AVFormatContext *s)
case MKTAG('w', 'a', 'v', 'e'):
if ((uint64_t)size > (1<<30))
return -1;
if (ff_get_extradata(s, st->codecpar, pb, size) < 0)
return AVERROR(ENOMEM);
if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0)
return ret;
if ( (st->codecpar->codec_id == AV_CODEC_ID_QDMC || st->codecpar->codec_id == AV_CODEC_ID_QDM2)
&& size>=12*4 && !st->codecpar->block_align) {
st->codecpar->block_align = AV_RB32(st->codecpar->extradata+11*4);
Expand All @@ -325,8 +325,8 @@ static int aiff_read_header(AVFormatContext *s)
}
break;
case MKTAG('C','H','A','N'):
if(ff_mov_read_chan(s, pb, st, size) < 0)
return AVERROR_INVALIDDATA;
if ((ret = ff_mov_read_chan(s, pb, st, size)) < 0)
return ret;
break;
case MKTAG('A','P','C','M'): /* XA ADPCM compressed sound chunk */
st->codecpar->codec_id = AV_CODEC_ID_ADPCM_XA;
Expand Down
5 changes: 3 additions & 2 deletions libavformat/apc.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static int apc_read_header(AVFormatContext *s)
{
AVIOContext *pb = s->pb;
AVStream *st;
int ret;

avio_rl32(pb); /* CRYO */
avio_rl32(pb); /* _APC */
Expand All @@ -53,8 +54,8 @@ static int apc_read_header(AVFormatContext *s)
st->codecpar->sample_rate = avio_rl32(pb);

/* initial predictor values for adpcm decoder */
if (ff_get_extradata(s, st->codecpar, pb, 2 * 4) < 0)
return AVERROR(ENOMEM);
if ((ret = ff_get_extradata(s, st->codecpar, pb, 2 * 4)) < 0)
return ret;

if (avio_rl32(pb)) {
st->codecpar->channels = 2;
Expand Down
22 changes: 13 additions & 9 deletions libavformat/ape.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ static int ape_read_header(AVFormatContext * s)
APEContext *ape = s->priv_data;
AVStream *st;
uint32_t tag;
int i;
int i, ret;
int total_blocks, final_size = 0;
int64_t pts, file_size;

Expand Down Expand Up @@ -358,8 +358,8 @@ static int ape_read_header(AVFormatContext * s)
st->duration = total_blocks;
avpriv_set_pts_info(st, 64, 1, ape->samplerate);

if (ff_alloc_extradata(st->codecpar, APE_EXTRADATA_SIZE))
return AVERROR(ENOMEM);
if ((ret = ff_alloc_extradata(st->codecpar, APE_EXTRADATA_SIZE)) < 0)
return ret;
AV_WL16(st->codecpar->extradata + 0, ape->fileversion);
AV_WL16(st->codecpar->extradata + 2, ape->compressiontype);
AV_WL16(st->codecpar->extradata + 4, ape->formatflags);
Expand All @@ -386,14 +386,16 @@ static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
int nblocks;
APEContext *ape = s->priv_data;
uint32_t extra_size = 8;
int64_t ret64;

if (avio_feof(s->pb))
return AVERROR_EOF;
if (ape->currentframe >= ape->totalframes)
return AVERROR_EOF;

if (avio_seek(s->pb, ape->frames[ape->currentframe].pos, SEEK_SET) < 0)
return AVERROR(EIO);
ret64 = avio_seek(s->pb, ape->frames[ape->currentframe].pos, SEEK_SET);
if (ret64 < 0)
return ret64;

/* Calculate how many blocks there are in this frame */
if (ape->currentframe == (ape->totalframes - 1))
Expand All @@ -409,8 +411,9 @@ static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
return AVERROR(EIO);
}

if (av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size) < 0)
return AVERROR(ENOMEM);
ret = av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size);
if (ret < 0)
return ret;

AV_WL32(pkt->data , nblocks);
AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip);
Expand Down Expand Up @@ -447,12 +450,13 @@ static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp
AVStream *st = s->streams[stream_index];
APEContext *ape = s->priv_data;
int index = av_index_search_timestamp(st, timestamp, flags);
int64_t ret;

if (index < 0)
return -1;

if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
return -1;
if ((ret = avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET)) < 0)
return ret;
ape->currentframe = index;
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions libavformat/apetag.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ static int ape_tag_read_field(AVFormatContext *s)
st->attached_pic.stream_index = st->index;
st->attached_pic.flags |= AV_PKT_FLAG_KEY;
} else {
if (ff_get_extradata(s, st->codecpar, s->pb, size) < 0)
return AVERROR(ENOMEM);
if ((ret = ff_get_extradata(s, st->codecpar, s->pb, size)) < 0)
return ret;
st->codecpar->codec_type = AVMEDIA_TYPE_ATTACHMENT;
}
} else {
Expand Down
18 changes: 9 additions & 9 deletions libavformat/asfenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,12 @@ static int asf_write_markers(AVFormatContext *s)
int64_t pres_time = av_rescale_q(c->start, c->time_base, scale);
uint64_t offset;
int32_t send_time = get_send_time(asf, pres_time, &offset);
int len = 0;
int len = 0, ret;
uint8_t *buf;
AVIOContext *dyn_buf;
if (t) {
if (avio_open_dyn_buf(&dyn_buf) < 0)
return AVERROR(ENOMEM);
if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
return ret;
avio_put_str16le(dyn_buf, t->value);
len = avio_close_dyn_buf(dyn_buf, &buf);
}
Expand Down Expand Up @@ -579,12 +579,12 @@ static int asf_write_header1(AVFormatContext *s, int64_t file_size,

/* title and other info */
if (has_title) {
int len;
int len, ret;
uint8_t *buf;
AVIOContext *dyn_buf;

if (avio_open_dyn_buf(&dyn_buf) < 0)
return AVERROR(ENOMEM);
if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
return ret;

hpos = put_header(pb, &ff_asf_comment_header);

Expand Down Expand Up @@ -714,10 +714,10 @@ static int asf_write_header1(AVFormatContext *s, int64_t file_size,
if (desc) {
AVIOContext *dyn_buf;
uint8_t *buf;
int len;
int len, ret;

if (avio_open_dyn_buf(&dyn_buf) < 0)
return AVERROR(ENOMEM);
if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
return ret;

avio_put_str16le(dyn_buf, desc);
len = avio_close_dyn_buf(dyn_buf, &buf);
Expand Down
10 changes: 6 additions & 4 deletions libavformat/avidec.c
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,10 @@ FF_ENABLE_DEPRECATION_WARNINGS
av_log(s, AV_LOG_WARNING, "New extradata in strf chunk, freeing previous one.\n");
av_freep(&st->codecpar->extradata);
}
if (ff_get_extradata(s, st->codecpar, pb, st->codecpar->extradata_size) < 0)
return AVERROR(ENOMEM);
ret = ff_get_extradata(s, st->codecpar, pb,
st->codecpar->extradata_size);
if (ret < 0)
return ret;
}

// FIXME: check if the encoder really did this correctly
Expand Down Expand Up @@ -932,8 +934,8 @@ FF_ENABLE_DEPRECATION_WARNINGS
av_log(s, AV_LOG_WARNING, "New extradata in strd chunk, freeing previous one.\n");
av_freep(&st->codecpar->extradata);
}
if (ff_get_extradata(s, st->codecpar, pb, size) < 0)
return AVERROR(ENOMEM);
if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0)
return ret;
}

if (st->codecpar->extradata_size & 1) //FIXME check if the encoder really did this correctly
Expand Down
12 changes: 6 additions & 6 deletions libavformat/avisynth.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
AVS_VideoFrame *frame;
unsigned char *dst_p;
const unsigned char *src_p;
int n, i, plane, rowsize, planeheight, pitch, bits;
int n, i, plane, rowsize, planeheight, pitch, bits, ret;
const char *error;
int avsplus av_unused;

Expand Down Expand Up @@ -676,8 +676,8 @@ static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
if (!pkt->size)
return AVERROR_UNKNOWN;

if (av_new_packet(pkt, pkt->size) < 0)
return AVERROR(ENOMEM);
if ((ret = av_new_packet(pkt, pkt->size)) < 0)
return ret;

pkt->pts = n;
pkt->dts = n;
Expand Down Expand Up @@ -739,7 +739,7 @@ static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
{
AviSynthContext *avs = s->priv_data;
AVRational fps, samplerate;
int samples;
int samples, ret;
int64_t n;
const char *error;

Expand Down Expand Up @@ -782,8 +782,8 @@ static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
if (!pkt->size)
return AVERROR_UNKNOWN;

if (av_new_packet(pkt, pkt->size) < 0)
return AVERROR(ENOMEM);
if ((ret = av_new_packet(pkt, pkt->size)) < 0)
return ret;

pkt->pts = n;
pkt->dts = n;
Expand Down
14 changes: 8 additions & 6 deletions libavformat/bink.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ static int read_header(AVFormatContext *s)
vst->codecpar->codec_id = AV_CODEC_ID_NONE;
}

if (ff_get_extradata(s, vst->codecpar, pb, 4) < 0)
return AVERROR(ENOMEM);
if ((ret = ff_get_extradata(s, vst->codecpar, pb, 4)) < 0)
return ret;

bink->num_audio_tracks = avio_rl32(pb);

Expand Down Expand Up @@ -190,8 +190,8 @@ static int read_header(AVFormatContext *s)
ast->codecpar->channels = 1;
ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
}
if (ff_alloc_extradata(ast->codecpar, 4))
return AVERROR(ENOMEM);
if ((ret = ff_alloc_extradata(ast->codecpar, 4)) < 0)
return ret;
AV_WL32(ast->codecpar->extradata, vst->codecpar->codec_tag);
}

Expand Down Expand Up @@ -302,13 +302,15 @@ static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, in
{
BinkDemuxContext *bink = s->priv_data;
AVStream *vst = s->streams[0];
int64_t ret;

if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
return -1;

/* seek to the first frame */
if (avio_seek(s->pb, vst->index_entries[0].pos + bink->smush_size, SEEK_SET) < 0)
return -1;
ret = avio_seek(s->pb, vst->index_entries[0].pos + bink->smush_size, SEEK_SET);
if (ret < 0)
return ret;

bink->video_pts = 0;
memset(bink->audio_pts, 0, sizeof(bink->audio_pts));
Expand Down
24 changes: 13 additions & 11 deletions libavformat/bintext.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,14 @@ static int bintext_read_header(AVFormatContext *s)
{
BinDemuxContext *bin = s->priv_data;
AVIOContext *pb = s->pb;

int ret;
AVStream *st = init_stream(s);
if (!st)
return AVERROR(ENOMEM);
st->codecpar->codec_id = AV_CODEC_ID_BINTEXT;

if (ff_alloc_extradata(st->codecpar, 2))
return AVERROR(ENOMEM);
if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
return ret;
st->codecpar->extradata[0] = 16;
st->codecpar->extradata[1] = 0;

Expand Down Expand Up @@ -222,7 +222,7 @@ static int xbin_read_header(AVFormatContext *s)
BinDemuxContext *bin = s->priv_data;
AVIOContext *pb = s->pb;
char fontheight, flags;

int ret;
AVStream *st = init_stream(s);
if (!st)
return AVERROR(ENOMEM);
Expand All @@ -241,8 +241,9 @@ static int xbin_read_header(AVFormatContext *s)
st->codecpar->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
st->codecpar->codec_id = flags & 4 ? AV_CODEC_ID_XBIN : AV_CODEC_ID_BINTEXT;

if (ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size))
return AVERROR(ENOMEM);
ret = ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size);
if (ret < 0)
return ret;
st->codecpar->extradata[0] = fontheight;
st->codecpar->extradata[1] = flags;
if (avio_read(pb, st->codecpar->extradata + 2, st->codecpar->extradata_size - 2) < 0)
Expand All @@ -264,6 +265,7 @@ static int adf_read_header(AVFormatContext *s)
BinDemuxContext *bin = s->priv_data;
AVIOContext *pb = s->pb;
AVStream *st;
int ret;

if (avio_r8(pb) != 1)
return AVERROR_INVALIDDATA;
Expand All @@ -273,8 +275,8 @@ static int adf_read_header(AVFormatContext *s)
return AVERROR(ENOMEM);
st->codecpar->codec_id = AV_CODEC_ID_BINTEXT;

if (ff_alloc_extradata(st->codecpar, 2 + 48 + 4096))
return AVERROR(ENOMEM);
if ((ret = ff_alloc_extradata(st->codecpar, 2 + 48 + 4096)) < 0)
return ret;
st->codecpar->extradata[0] = 16;
st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;

Expand Down Expand Up @@ -318,7 +320,7 @@ static int idf_read_header(AVFormatContext *s)
BinDemuxContext *bin = s->priv_data;
AVIOContext *pb = s->pb;
AVStream *st;
int got_width = 0;
int got_width = 0, ret;

if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
return AVERROR(EIO);
Expand All @@ -328,8 +330,8 @@ static int idf_read_header(AVFormatContext *s)
return AVERROR(ENOMEM);
st->codecpar->codec_id = AV_CODEC_ID_IDF;

if (ff_alloc_extradata(st->codecpar, 2 + 48 + 4096))
return AVERROR(ENOMEM);
if ((ret = ff_alloc_extradata(st->codecpar, 2 + 48 + 4096)) < 0)
return ret;
st->codecpar->extradata[0] = 16;
st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;

Expand Down
4 changes: 2 additions & 2 deletions libavformat/bit.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ static int read_packet(AVFormatContext *s,
if(ret != 8 * packet_size * sizeof(uint16_t))
return AVERROR(EIO);

if (av_new_packet(pkt, packet_size) < 0)
return AVERROR(ENOMEM);
if ((ret = av_new_packet(pkt, packet_size)) < 0)
return ret;

init_put_bits(&pbo, pkt->data, packet_size);
for(j=0; j < packet_size; j++)
Expand Down
Loading

0 comments on commit c1e439d

Please sign in to comment.