From 6fda817b2bc237081e6bf6daeff9087b6543647b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Wed, 27 Dec 2023 16:32:49 +0100
Subject: [PATCH 3/7] lavf/codec2: Compute duration from filesize
---
libavformat/codec2.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
@@ -124,9 +124,10 @@ static int codec2_mode_bit_rate(AVFormatContext *s, int mode)
return 8 * 8000 * block_align / frame_size;
}
-static int codec2_read_header_common(AVFormatContext *s, AVStream *st)
+static int codec2_read_header_common(AVFormatContext *s, AVStream *st, int header)
{
int mode = codec2_mode_from_extradata(st->codecpar->extradata);
+ int64_t sz = avio_size(s->pb);
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
st->codecpar->codec_id = AV_CODEC_ID_CODEC2;
@@ -143,6 +144,12 @@ static int codec2_read_header_common(AVFormatContext *s, AVStream *st)
return AVERROR_INVALIDDATA;
}
+ // compute duration from filesize
+ if (sz >= header) {
+ int64_t frames = (sz - header) / st->codecpar->block_align;
+ if (frames <= INT64_MAX / st->codecpar->frame_size)
+ st->duration = frames * st->codecpar->frame_size;
+ }
avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
return 0;
@@ -180,7 +187,7 @@ static int codec2_read_header(AVFormatContext *s)
ffformatcontext(s)->data_offset = CODEC2_HEADER_SIZE;
- return codec2_read_header_common(s, st);
+ return codec2_read_header_common(s, st, CODEC2_HEADER_SIZE);
}
static int codec2_read_packet(AVFormatContext *s, AVPacket *pkt)
@@ -258,7 +265,7 @@ static int codec2raw_read_header(AVFormatContext *s)
codec2_make_extradata(st->codecpar->extradata, c2->mode);
- return codec2_read_header_common(s, st);
+ return codec2_read_header_common(s, st, 0);
}
//transcoding report2074.c2 to wav went from 7.391s to 5.322s with -frames_per_packet 1000 compared to default, same sha1sum
--
2.39.2