[FFmpeg-devel] [PATCH] mov.c fix the duration for the last audio frame.

Wang Cao wangcao at google.com
Thu May 30 22:38:36 EEST 2024


It is possible that the actual audio data only occupy part of the last audio frame. This can be signaled by duration in the "trun" atom. We should respect the metadata of file and set the duration correctly.
---
 libavformat/mov.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/libavformat/mov.c b/libavformat/mov.c
index 45eca74d1d..caea36b495 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -5700,11 +5700,16 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 
         sc->ctts_data[index_entry_pos].count = 1;
         sc->ctts_data[index_entry_pos].duration = ctts_duration;
+        if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
+            sc->ctts_data[index_entry_pos].duration = sample_duration;
+        } else {
+            sc->ctts_data[index_entry_pos].duration = ctts_duration;
+        }
         index_entry_pos++;
 
         av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
-                "size %u, distance %d, keyframe %d\n", st->index,
-                index_entry_pos, offset, dts, sample_size, distance, keyframe);
+                "size %u, distance %d, keyframe %d duration %d\n", st->index,
+                index_entry_pos, offset, dts, sample_size, distance, keyframe, sc->ctts_data[index_entry_pos-1].duration);
         distance++;
         if (av_sat_add64(dts, sample_duration) != dts + (uint64_t)sample_duration)
             return AVERROR_INVALIDDATA;
@@ -9894,6 +9899,9 @@ static int mov_finalize_packet(AVFormatContext *s, AVStream *st, AVIndexEntry *s
     }
     if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
         pkt->pts = av_sat_add64(pkt->dts, av_sat_add64(sc->dts_shift, sc->ctts_data[sc->ctts_index].duration));
+        if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
+            pkt->duration = sc->ctts_data[sc->ctts_index].duration;
+        }
         /* update ctts context */
         sc->ctts_sample++;
         if (sc->ctts_index < sc->ctts_count &&
-- 
2.45.1.288.g0e0cd299f1-goog



More information about the ffmpeg-devel mailing list