aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat/matroskaenc.c
diff options
context:
space:
mode:
authorAndreas Rheinhardt <andreas.rheinhardt@outlook.com>2022-08-31 06:23:08 +0200
committerAndreas Rheinhardt <andreas.rheinhardt@outlook.com>2022-09-05 20:35:20 +0200
commiteb325324aabe6ae479bbf15256321da8e39238d4 (patch)
tree0ec2eb2ee9263f99cf44504f360bd7d2a310e0f7 /libavformat/matroskaenc.c
parentbe0a2515ab25e4531464f84c4c4d5a97e1a3e8ef (diff)
downloadffmpeg-eb325324aabe6ae479bbf15256321da8e39238d4.tar.gz
avformat/matroskaenc: Actually apply timestamp offset for Opus
Matroska generally requires timestamps to be nonnegative, but there is an exception: Data that corresponds to encoder delay and is not supposed to be output anyway can have a negative timestamp. This is achieved by using the CodecDelay header field: The demuxer has to subtract this value from the raw (nonnegative) timestamps of the corresponding track. Therefore the muxer has to add this value first to write this raw timestamp. Support for writing CodecDelay has been added in FFmpeg commit d92b1b1babe69268971863649c225e1747358a74 and in Libav commit a1aa37dd0b96710d4a17718198a3f56aea2040c1. The former simply wrote the header field and did not apply any timestamp offsets, leading to desynchronisation (if one uses multiple tracks). The latter applied it at two places, but not at the one where it actually matters, namely in mkv_write_block(), leading to the same desynchronisation as with the former commit. It furthermore used the wrong stream timebase to convert the delay to the stream's timebase, as the conversion used the timebase from before avpriv_set_pts_info(). When the latter was merged in 82e4f39883932c1b1e5c7792a1be12dec6ab603d, it was only done in a deactivated state that still did not offset the timestamps when muxing due to "assertion failures and av sync errors". a1aa37dd0b96710d4a17718198a3f56aea2040c1 made it definitely more likely to run into assertion failures (namely if the relative block timestamp doesn't fit into an int16_t). Yet all of the above issues have been fixed (in commits 962d63157322466a9a82f9f9d84c1b6f1b582f65, 5d3953a5dcfd5f71391b7f34908517eb6f7e5146 and 4ebeab15b037a21f195696cef1f7522daf42f3ee. This commit therefore enables applying CodecDelay, fixing ticket #7182. There is just one slight regression from this: If one has input with encoder delay where the first timestamp is negative, but the pts of the part of the data that is actually intended to be output is nonnegative, then the timestamps will currently by default be shifted to make them nonnegative before they reach the muxer; the muxer will then ensure that the shifted timestamps are retained. Before this commit, the muxer did not ensure this; instead the timestamps that the demuxer will output were shifted and if the first timestamp of the actually intended output was zero before shifting, then this unintentional shift just cancels the shift performed before the packet reached the muxer. (But notice that this only applies if all the tracks use the same CodecDelay, or the relative sync between tracks will be impaired.) This happens in the matroska-opus-remux and matroska-ogg-opus-remux FATE tests. Future commits will forward the information that the Matroska muxer has a limited capability to handle negative timestamps so that the shifting in libavformat can take advantage of it. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Diffstat (limited to 'libavformat/matroskaenc.c')
-rw-r--r--libavformat/matroskaenc.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index c525edb39f..0ded53dc21 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -1836,11 +1836,11 @@ static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv,
av_log(s, AV_LOG_ERROR, "Initial padding is invalid\n");
return AVERROR(EINVAL);
}
-// track->ts_offset = av_rescale_q(par->initial_padding,
-// (AVRational){ 1, par->sample_rate },
-// st->time_base);
-
put_ebml_uint(pb, MATROSKA_ID_CODECDELAY, codecdelay);
+
+ track->ts_offset = av_rescale_q(par->initial_padding,
+ (AVRational){ 1, par->sample_rate },
+ st->time_base);
}
if (par->codec_id == AV_CODEC_ID_OPUS)
put_ebml_uint(pb, MATROSKA_ID_SEEKPREROLL, OPUS_SEEK_PREROLL);