Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] mov.c fix the duration for the last audio frame.
@ 2024-05-30 19:38 Wang Cao via ffmpeg-devel
  2024-05-30 23:49 ` Michael Niedermayer
  0 siblings, 1 reply; 2+ messages in thread
From: Wang Cao via ffmpeg-devel @ 2024-05-30 19:38 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Wang Cao

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

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-05-30 23:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-30 19:38 [FFmpeg-devel] [PATCH] mov.c fix the duration for the last audio frame Wang Cao via ffmpeg-devel
2024-05-30 23:49 ` Michael Niedermayer

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git