diff mbox series

[FFmpeg-devel,2/6] lavf/codec2: Compute duration from filesize

Message ID 7ff90b8b486e63412e3a3ba3dac9dc550bbf17c5.camel@haerdin.se
State New
Headers show
Series [FFmpeg-devel,1/6] lavc/codec2utils: Use actual libcodec2 version | expand

Checks

Context Check Description
yinshiyou/configure_loongarch64 warning Failed to apply patch
andriy/configure_x86 warning Failed to apply patch

Commit Message

Tomas Härdin Dec. 28, 2023, 6:43 p.m. UTC

diff mbox series

Patch

From 1576f9206b51ea9946495e7c5e293403541d49a4 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 2/6] lavf/codec2: Compute duration from filesize

---
 libavformat/codec2.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/libavformat/codec2.c b/libavformat/codec2.c
index 1d6ef2a14a..47fff2a695 100644
--- a/libavformat/codec2.c
+++ b/libavformat/codec2.c
@@ -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_EXTRADATA_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