Commit cc69293a3a for ffmpeg
commit cc69293a3a932f09759f92edbd49409d2c2d526d
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Fri Nov 27 08:07:00 2026 -0600
avformat/mov: trim the end of DTS from the packet length
The decoded length of a DTS frame can change within a track. Without a
frame size from the ddts box, read it from the core frame header, so
that the end trim gets the length of each packet. A frame without a
core keeps its container duration.
A frame whose core rate is not the stream's keeps its container
duration too. A matching rate does not rule out an extension such as
X96 that changes the decoder's output rate.
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index c3da8412c6..3029b43139 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1141,6 +1141,7 @@ SHLIBOBJS-$(CONFIG_PHM_ENCODER) += float2half.o
# General libavformat dependencies
OBJS-$(CONFIG_FITS_DEMUXER) += fits.o
+OBJS-$(CONFIG_MOV_DEMUXER) += dca.o dca_sample_rate_tab.o
OBJS-$(CONFIG_TAK_DEMUXER) += tak.o
# libavformat dependencies for static builds
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 63a4baea1e..f85a6854ac 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -756,6 +756,7 @@ SHLIBOBJS-$(CONFIG_MATROSKA_DEMUXER) += mpeg4audio_sample_rates.o \
opus_frame_duration_tab.o
SHLIBOBJS-$(CONFIG_MATROSKA_MUXER) += opus_frame_duration_tab.o
SHLIBOBJS-$(CONFIG_MOV_DEMUXER) += ac3_channel_layout_tab.o \
+ dca_sample_rate_tab.o \
opus_frame_duration_tab.o
SHLIBOBJS-$(CONFIG_MOV_MUXER) += mpegaudiotabs.o
SHLIBOBJS-$(CONFIG_MP3_MUXER) += mpegaudiotabs.o
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 188b4ff639..ea8c49487a 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -54,6 +54,7 @@
#include "libavutil/timecode.h"
#include "libavutil/uuid.h"
#include "libavcodec/ac3tab.h"
+#include "libavcodec/dca.h"
#include "libavcodec/exif.h"
#include "libavcodec/flac.h"
#include "libavcodec/hevc/hevc.h"
@@ -9228,6 +9229,29 @@ static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPa
return 0;
}
+/**
+ * @return the decoded length of a DTS packet from its core frame header, or
+ * 0 without one
+ */
+static int64_t mov_dts_packet_samples(AVStream *st, const AVPacket *pkt)
+{
+ uint8_t hdr[DCA_CORE_FRAME_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
+ DCACoreFrameHeader h;
+ int ret;
+
+ if (pkt->size < DCA_CORE_FRAME_HEADER_SIZE)
+ return 0;
+ ret = avpriv_dca_convert_bitstream(pkt->data, DCA_CORE_FRAME_HEADER_SIZE,
+ hdr, DCA_CORE_FRAME_HEADER_SIZE);
+ if (ret < 0 || avpriv_dca_parse_core_frame_header(&h, hdr, ret) < 0)
+ return 0;
+ // The decoder can output an extension such as X96 at another rate than
+ // the core's. A core at the stream rate is assumed to decode at it.
+ if (ff_dca_sample_rates[h.sr_code] != st->codecpar->sample_rate)
+ return 0;
+ return h.npcmblocks * DCA_PCMBLOCK_SAMPLES;
+}
+
/**
* @return the decoded length of a packet, in samples, read from its payload,
* or 0 when unknown
@@ -9267,6 +9291,10 @@ static int64_t mov_packet_samples(AVStream *st, const AVPacket *pkt,
if (vorbis->sample_rate != st->codecpar->sample_rate)
return 0;
return FFMAX(samples, 0);
+ case AV_CODEC_ID_DTS:
+ // The frame size from the ddts box applies first.
+ return st->codecpar->frame_size > 0 ? 0
+ : mov_dts_packet_samples(st, pkt);
default:
return 0;
}
diff --git a/tests/ref/fate/mov-mp4-dts-trim b/tests/ref/fate/mov-mp4-dts-trim
index af3d6cd5b0..894ac833de 100644
--- a/tests/ref/fate/mov-mp4-dts-trim
+++ b/tests/ref/fate/mov-mp4-dts-trim
@@ -6,4 +6,4 @@ d80c6a0cce6346e2a7547226c577bac9 *tests/data/fate/mov-mp4-dts-trim.mp4
#sample_rate 0: 48000
#channel_layout_name 0: mono
0, 0, 0, 512, 1884, 0x56d32840
-0, 512, 512, 496, 1884, 0x56d32840
+0, 512, 512, 496, 1884, 0x56d32840, S=1, Skip Samples, 10, 0x00600010