]> git.sesse.net Git - nageru/commitdiff
Matroska hacks. kaeru-cef-overlay-hack
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 17 Apr 2022 15:10:37 +0000 (17:10 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 17 Apr 2022 15:10:37 +0000 (17:10 +0200)
nageru/kaeru.cpp

index 419a3d149f9275dcf59968d1320d8d6d896281c5..1a1cd6c7a8378ec37b95e4d5ce08884c2e805941 100644 (file)
@@ -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);
         }