AVStream *st;
uint32_t tag;
int i;
- int total_blocks;
- int64_t pts;
+ int total_blocks, final_size = 0;
+ int64_t pts, file_size;
/* Skip any leading junk such as id3v2 tags */
ape->junklength = avio_tell(pb);
ape->frames[i - 1].size = ape->frames[i].pos - ape->frames[i - 1].pos;
ape->frames[i].skip = (ape->frames[i].pos - ape->frames[0].pos) & 3;
}
- ape->frames[ape->totalframes - 1].size = ape->finalframeblocks * 4;
ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks;
+ /* calculate final packet size from total file size, if available */
+ file_size = avio_size(pb);
+ if (file_size > 0) {
+ final_size = file_size - ape->frames[ape->totalframes - 1].pos -
+ ape->wavtaillength;
+ final_size -= final_size & 3;
+ }
+ if (file_size <= 0 || final_size <= 0)
+ final_size = ape->finalframeblocks * 8;
+ ape->frames[ape->totalframes - 1].size = final_size;
for (i = 0; i < ape->totalframes; i++) {
if(ape->frames[i].skip){
ape_dumpinfo(s, ape);
- /* try to read APE tags */
- if (pb->seekable) {
- ff_ape_parse_tag(s);
- avio_seek(pb, 0, SEEK_SET);
- }
-
av_log(s, AV_LOG_DEBUG, "Decoding file - v%d.%02d, compression level %"PRIu16"\n",
ape->fileversion / 1000, (ape->fileversion % 1000) / 10,
ape->compressiontype);
st->codec->channels = ape->channels;
st->codec->sample_rate = ape->samplerate;
st->codec->bits_per_coded_sample = ape->bps;
- st->codec->frame_size = MAC_SUBFRAME_SIZE;
st->nb_frames = ape->totalframes;
st->start_time = 0;
pts += ape->blocksperframe / MAC_SUBFRAME_SIZE;
}
+ /* try to read APE tags */
+ if (pb->seekable) {
+ ff_ape_parse_tag(s);
+ avio_seek(pb, 0, SEEK_SET);
+ }
+
return 0;
}
uint32_t extra_size = 8;
if (s->pb->eof_reached)
- return AVERROR(EIO);
- if (ape->currentframe > ape->totalframes)
- return AVERROR(EIO);
+ return AVERROR_EOF;
+ if (ape->currentframe >= ape->totalframes)
+ return AVERROR_EOF;
- avio_seek (s->pb, ape->frames[ape->currentframe].pos, SEEK_SET);
+ if (avio_seek(s->pb, ape->frames[ape->currentframe].pos, SEEK_SET) < 0)
+ return AVERROR(EIO);
/* Calculate how many blocks there are in this frame */
if (ape->currentframe == (ape->totalframes - 1))
else
nblocks = ape->blocksperframe;
+ if (ape->frames[ape->currentframe].size <= 0 ||
+ ape->frames[ape->currentframe].size > INT_MAX - extra_size) {
+ av_log(s, AV_LOG_ERROR, "invalid packet size: %d\n",
+ ape->frames[ape->currentframe].size);
+ ape->currentframe++;
+ return AVERROR(EIO);
+ }
+
if (av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size) < 0)
return AVERROR(ENOMEM);
.read_packet = ape_read_packet,
.read_close = ape_read_close,
.read_seek = ape_read_seek,
- .extensions = "ape,apl,mac"
+ .extensions = "ape,apl,mac",
};