From 4fae57000f4e40f28adf6c44f8cba19823e5c0f4 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 17 Apr 2022 17:10:37 +0200 Subject: [PATCH] Matroska hacks. --- nageru/kaeru.cpp | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/nageru/kaeru.cpp b/nageru/kaeru.cpp index 419a3d1..1a1cd6c 100644 --- a/nageru/kaeru.cpp +++ b/nageru/kaeru.cpp @@ -359,6 +359,9 @@ string last_ts; int64_t video_pts_offset = 0, audio_pts_offset = 0; int64_t next_video_pts = 0, next_audio_pts = 0; +AVRational prev_video_timebase{0, 0}; +AVRational prev_audio_timebase{0, 0}; + void video_frame_callback(FFmpegCapture *video, X264Encoder *x264_encoder, AudioEncoder *audio_encoder, int64_t video_pts, AVRational video_timebase, int64_t audio_pts, AVRational audio_timebase, @@ -366,6 +369,22 @@ void video_frame_callback(FFmpegCapture *video, X264Encoder *x264_encoder, Audio FrameAllocator::Frame video_frame, size_t video_offset, VideoFormat video_format, FrameAllocator::Frame audio_frame, size_t audio_offset, AudioFormat audio_format) { + // Our splicing wants consistent timebases... + if (video_pts >= 0) { + if (prev_video_timebase.den == 0) { + prev_video_timebase = video_timebase; + } + video_pts = av_rescale_q(video_pts, video_timebase, prev_video_timebase); + video_timebase = prev_video_timebase; + } + if (audio_pts >= 0) { + if (prev_audio_timebase.den == 0) { + prev_audio_timebase = audio_timebase; + } + audio_pts = av_rescale_q(audio_pts, audio_timebase, prev_audio_timebase); + audio_timebase = prev_audio_timebase; + } + if (video_pts >= 0) video_pts += video_pts_offset; if (audio_pts >= 0) @@ -413,7 +432,11 @@ void video_frame_callback(FFmpegCapture *video, X264Encoder *x264_encoder, Audio ReceivedTimestamps ts; ts.ts.push_back(steady_clock::now()); - next_video_pts = video_pts + av_rescale_q(1, AVRational{ 1001, 60000 }, video_timebase); + //next_video_pts = video_pts + av_rescale_q(1, AVRational{ 1001, 60000 }, video_timebase); + + // I hate Matroska timestamps and their inaccuracy... + next_video_pts = video_pts + av_rescale_q(1, AVRational{ 500, 60000 }, video_timebase); + video_pts = av_rescale_q(video_pts, video_timebase, AVRational{ 1, TIMEBASE }); int64_t frame_duration = int64_t(TIMEBASE) * video_format.frame_rate_den / video_format.frame_rate_nom; if (team1 != "nocef") { @@ -451,7 +474,9 @@ void video_frame_callback(FFmpegCapture *video, X264Encoder *x264_encoder, Audio } else { assert(false); } - next_audio_pts = audio_pts + av_rescale_q(num_samples / 2, AVRational{ 1, OUTPUT_FREQUENCY }, audio_timebase); + //next_audio_pts = audio_pts + av_rescale_q(num_samples / 2, AVRational{ 1, OUTPUT_FREQUENCY }, audio_timebase); + // Matroska hack... + next_audio_pts = audio_pts + av_rescale_q(num_samples / 4, AVRational{ 1, OUTPUT_FREQUENCY }, audio_timebase); audio_pts = av_rescale_q(audio_pts, audio_timebase, AVRational{ 1, TIMEBASE }); audio_encoder->encode_audio(float_samples, audio_pts); } -- 2.39.2