PR #26238 opened by James Almer (jamrial)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26238
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26238.patch

If left to the generic code, durations (and timestamps if there's no lacing) 
will be derived from the internal rate instead of the file reported timebase.

`$ ./ffmpeg -i ~/fate-samples/audiomatch/tones_opus_48000_stereo.mka -c:a copy 
-f framecrc -`

Before:
```
[...]
Input #0, matroska,webm, from 
'../samples/audiomatch/tones_opus_48000_stereo.mka':
  Metadata:
    encoder         : libebml v1.4.5 + libmatroska v1.7.1
    creation_time   : 2026-06-14T02:06:53.000000Z
  Duration: 00:00:02.01, start: 0.000000, bitrate: 147 kb/s
  Stream #0:0: Audio: opus, 44100 Hz, stereo, fltp (default)
[...]
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
#extradata 0:       19, 0x38100445
#software: Lavf63.21.100
#tb 0: 11337/500000000
#media_type 0: audio
#codec_id 0: opus
#sample_rate 0: 44100
[...]
0,       -287,       -287,      960,      489, 0xb843f4b8, S=1, Skip Samples,   
    10, 0x013f0020
0,        673,        673,      960,      354, 0xa2a7b0c8
0,       1633,       1633,      960,      366, 0xf507c0ab
0,       2593,       2593,      960,      359, 0x0379b6ca
0,       3553,       3553,      960,      377, 0x7a62c054
0,       4513,       4513,      960,      369, 0x08c1c043
0,       5473,       5473,      960,      357, 0x689bab86
0,       6433,       6433,      960,      366, 0x5753bac7
0,       6770,       6770,      960,      342, 0xbf86a994
```

After:
```
[...]
Input #0, matroska,webm, from 
'../samples/audiomatch/tones_opus_48000_stereo.mka':
  Metadata:
    encoder         : libebml v1.4.5 + libmatroska v1.7.1
    creation_time   : 2026-06-14T02:06:53.000000Z
  Duration: 00:00:02.01, start: 0.000000, bitrate: 147 kb/s
  Stream #0:0: Audio: opus, 44100 Hz, stereo, fltp (default)
[...]
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
#extradata 0:       19, 0x38100445
#software: Lavf63.21.100
#tb 0: 11337/500000000
#media_type 0: audio
#codec_id 0: opus
#sample_rate 0: 44100
[...]
0,       -287,       -287,      882,      489, 0xb843f4b8, S=1, Skip Samples,   
    10, 0x013f0020
0,        595,        595,      882,      354, 0xa2a7b0c8
0,       1477,       1477,      882,      366, 0xf507c0ab
0,       2359,       2359,      882,      359, 0x0379b6ca
0,       3241,       3241,      882,      377, 0x7a62c054
0,       4123,       4123,      882,      369, 0x08c1c043
0,       5005,       5005,      882,      357, 0x689bab86
0,       5887,       5887,      882,      366, 0x5753bac7
0,       6770,       6770,      882,      342, 0xbf86a994
```

6770 is a coded timestamp (Second simple block, after the first had 8 frames 
using lacing), hence it being off by 1 (rounding during muxing).


>From 6669f1454c6e39828f7eba0b1af354ec3eaa7501 Mon Sep 17 00:00:00 2001
From: James Almer <[email protected]>
Date: Fri, 20 Nov 2026 18:00:44 -0300
Subject: [PATCH] avformat/matroskadec: fix Opus packet durations when not
 coded

If left to the generic code, durations will be derived from the internal rate
instead of the file reported timebase.

Signed-off-by: James Almer <[email protected]>
---
 libavformat/matroskadec.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index 745140cebfb..137c773d203 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -4556,7 +4556,7 @@ static int matroska_parse_block(MatroskaDemuxContext 
*matroska, AVBufferRef *buf
                                       (AVRational) { 1, 1000000000 },
                                       st->time_base);
 
-    if (track->exact_audio) {
+    if (track->exact_audio || !block_duration) {
         const uint8_t *lace_data = data;
         int64_t block_samples = 0;
 
@@ -4568,7 +4568,9 @@ static int matroska_parse_block(MatroskaDemuxContext 
*matroska, AVBufferRef *buf
             if (samples <= 0) {
                 block_samples = -1;
                 break;
-            }
+            } else if (!track->exact_audio)
+                samples = av_rescale_q(samples, (AVRational) { 1, 
OPUS_TIMESTAMP_RATE },
+                                       st->time_base);
             if ((track->grid_step && samples != track->grid_step) ||
                 (n && samples != lace_samples[0]))
                 track->grid_varies = 1;
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to