Commit 9a0cfb0ee3 for ffmpeg
commit 9a0cfb0ee3c4572a410e10088bd9b89e8df5222d
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Fri Nov 27 08:03:37 2026 -0600
avformat/mov: trim the end of Opus from the packet length
Opus in ISOBMFF trims the end of the stream by shortening the duration
of the last sample. The decoded length of a packet is only in its TOC,
so read it there and pass it to the end trim.
The TOC needs no state, so the length does not depend on the sample
description or on seeking. Without a decryption key, the payload stays
encrypted and is left alone.
diff --git a/libavcodec/opus/Makefile b/libavcodec/opus/Makefile
index 08260a86da..d5bd03db36 100644
--- a/libavcodec/opus/Makefile
+++ b/libavcodec/opus/Makefile
@@ -30,4 +30,5 @@ OBJS-$(CONFIG_OPUS_ENCODER) += \
STLIBOBJS-$(CONFIG_MATROSKA_DEMUXER) += opus/frame_duration_tab.o
STLIBOBJS-$(CONFIG_MATROSKA_MUXER) += opus/frame_duration_tab.o
+STLIBOBJS-$(CONFIG_MOV_DEMUXER) += opus/frame_duration_tab.o
STLIBOBJS-$(CONFIG_WEBM_MUXER) += opus/frame_duration_tab.o
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 9b5731d959..63a4baea1e 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -755,7 +755,8 @@ SHLIBOBJS-$(CONFIG_JPEGXL_ANIM_DEMUXER) += jpegxl_parse.o
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
+SHLIBOBJS-$(CONFIG_MOV_DEMUXER) += ac3_channel_layout_tab.o \
+ opus_frame_duration_tab.o
SHLIBOBJS-$(CONFIG_MOV_MUXER) += mpegaudiotabs.o
SHLIBOBJS-$(CONFIG_MP3_MUXER) += mpegaudiotabs.o
SHLIBOBJS-$(CONFIG_MXF_MUXER) += rangecoder_dec.o
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 75040c19e4..a5798b391f 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -71,6 +71,7 @@
#include "riff.h"
#include "isom.h"
#include "libavcodec/get_bits.h"
+#include "libavcodec/opus/parse.h"
#include "libavcodec/opus/tab.h"
#include "id3v1.h"
#include "mov_chan.h"
@@ -9191,6 +9192,45 @@ static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPa
return 0;
}
+/**
+ * @return the decoded length of a packet, in samples, read from its payload,
+ * or 0 when unknown
+ */
+static int64_t mov_packet_samples(AVStream *st, const AVPacket *pkt)
+{
+ int samples;
+
+ switch (st->codecpar->codec_id) {
+ case AV_CODEC_ID_OPUS:
+ samples = ff_opus_packet_samples(pkt->data, pkt->size);
+ return samples > 0 ? av_rescale(samples, st->codecpar->sample_rate,
+ OPUS_TIMESTAMP_RATE) : 0;
+ default:
+ return 0;
+ }
+}
+
+/**
+ * Apply the end trim with the decoded length of the packet, for codecs
+ * whose length is only in the payload.
+ */
+static int mov_end_trim(AVStream *st, AVPacket *pkt)
+{
+ int64_t samples;
+
+ // Without a decryption key, the payload is still encrypted.
+ if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO ||
+ av_packet_get_side_data(pkt, AV_PKT_DATA_ENCRYPTION_INFO, NULL))
+ return 0;
+
+ // Without a discard range, no packet is trimmed.
+ if (!ffstream(st)->first_discard_sample)
+ return 0;
+
+ samples = mov_packet_samples(st, pkt);
+ return samples > 0 ? ff_end_trim_packet(st, pkt, samples) : 0;
+}
+
static int mov_read_dops(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
const int OPUS_SEEK_PREROLL_MS = 80;
@@ -13455,7 +13495,7 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
return ret;
}
- return 0;
+ return mov_end_trim(st, pkt);
}
static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
diff --git a/tests/ref/fate/mov-mp4-opus-cenc-trim b/tests/ref/fate/mov-mp4-opus-cenc-trim
index 02eb500508..8174bd2a48 100644
--- a/tests/ref/fate/mov-mp4-opus-cenc-trim
+++ b/tests/ref/fate/mov-mp4-opus-cenc-trim
@@ -7,6 +7,6 @@
#sample_rate 0: 48000
#channel_layout_name 0: stereo
0, 0, 0, 960, 3, 0x05f002f9
-0, 960, 960, 168, 3, 0x05f002f9
+0, 960, 960, 168, 3, 0x05f002f9, S=1, Skip Samples, 10, 0x009f001b
packet|duration=960|side_datum/encryption_info:side_data_type=Encryption info
packet|duration=168|side_datum/encryption_info:side_data_type=Encryption info
diff --git a/tests/ref/fate/mov-mp4-opus-trim b/tests/ref/fate/mov-mp4-opus-trim
index 8481cf8604..674602d89f 100644
--- a/tests/ref/fate/mov-mp4-opus-trim
+++ b/tests/ref/fate/mov-mp4-opus-trim
@@ -7,4 +7,4 @@ dcb4e18e9a275929672ff5a1df480e81 *tests/data/fate/mov-mp4-opus-trim.mp4
#sample_rate 0: 48000
#channel_layout_name 0: stereo
0, 0, 0, 960, 3, 0x05f002f9
-0, 960, 960, 168, 3, 0x05f002f9
+0, 960, 960, 168, 3, 0x05f002f9, S=1, Skip Samples, 10, 0x009f001b
diff --git a/tests/ref/fate/mov-opus-timescale1000-trim b/tests/ref/fate/mov-opus-timescale1000-trim
index f5920f351e..0b5d817b2f 100644
--- a/tests/ref/fate/mov-opus-timescale1000-trim
+++ b/tests/ref/fate/mov-opus-timescale1000-trim
@@ -13,4 +13,4 @@
0, 15, 15, 2, 103, 0x1bc130d9
0, 17, 17, 3, 103, 0xb10e2c7b
0, 20, 20, 2, 103, 0x99c83350
-0, 22, 22, 1, 103, 0x3a42336b
+0, 22, 22, 1, 103, 0x3a42336b, S=1, Skip Samples, 10, 0x01b00048
diff --git a/tests/ref/fate/mov-opus-timescale32000-trim b/tests/ref/fate/mov-opus-timescale32000-trim
index df1c45ebc7..93209de075 100644
--- a/tests/ref/fate/mov-opus-timescale32000-trim
+++ b/tests/ref/fate/mov-opus-timescale32000-trim
@@ -5,4 +5,4 @@
#sample_rate 0: 48000
#channel_layout_name 0: stereo
0, 0, 0, 640, 3, 0x05f002f9
-0, 640, 640, 639, 3, 0x05f002f9
+0, 640, 640, 639, 3, 0x05f002f9, S=1, Skip Samples, 10, 0x00060001