]> git.sesse.net Git - nageru/commitdiff
Read timebase from the input video.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 10 Sep 2018 21:57:00 +0000 (23:57 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 15 Sep 2018 17:39:49 +0000 (19:39 +0200)
clip_list.cpp
main.cpp
player.cpp

index 09fe0348e3ff8dc81e7d8e301b7e00979c62743f..75f4a9f46b6be66e0f72523528600262aab20350 100644 (file)
@@ -5,14 +5,14 @@
 #include <vector>
 
 #include "clip_list.h"
+#include "timebase.h"
 #include "ui_mainwindow.h"
 
 using namespace std;
 
 string pts_to_string(int64_t pts)
 {
-       // FIXME: This depends on a fixed timebase.
-       int64_t t = lrint((pts / 12800.0) * 1e3);  // In milliseconds.
+       int64_t t = lrint((pts / double(TIMEBASE)) * 1e3);  // In milliseconds.
        int ms = t % 1000;
        t /= 1000;
        int sec = t % 60;
@@ -28,8 +28,7 @@ string pts_to_string(int64_t pts)
 
 string duration_to_string(int64_t pts_diff)
 {
-       // FIXME: This depends on a fixed timebase.
-       int64_t t = lrint((pts_diff / 12800.0) * 1e3);  // In milliseconds.
+       int64_t t = lrint((pts_diff / double(TIMEBASE)) * 1e3);  // In milliseconds.
        int ms = t % 1000;
        t /= 1000;
        int sec = t % 60;
index ccfb281ddecceadde96d16a15331d62e53dbb15f..fc7428586c599d523cc1adf33fe02cccc3b56d1c 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -28,6 +28,7 @@ extern "C" {
 #include "player.h"
 #include "post_to_main_thread.h"
 #include "ref_counted_gl_sync.h"
+#include "timebase.h"
 #include "ui_mainwindow.h"
 
 using namespace std;
@@ -119,6 +120,12 @@ int record_thread_func()
                if (av_read_frame(format_ctx.get(), &pkt) != 0) {
                        break;
                }
+
+               // Convert pts to our own timebase.
+               // TODO: Figure out offsets, too.
+               AVRational stream_timebase = format_ctx->streams[pkt.stream_index]->time_base;
+               pkt.pts = av_rescale_q(pkt.pts, stream_timebase, AVRational{ 1, TIMEBASE });
+
                //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
                //      pkt.stream_index, pkt.pts, pkt.size);
                string filename = filename_for_frame(pkt.stream_index, pkt.pts);
@@ -145,9 +152,9 @@ int record_thread_func()
                assert(pkt.stream_index < MAX_STREAMS);
                frames[pkt.stream_index].push_back(pkt.pts);
 
-               // Hack. Assumes a given timebase.
+               // Hack. Remove when we're dealing with live streams.
                if (last_pts != -1) {
-                       this_thread::sleep_for(microseconds((pkt.pts - last_pts) * 1000000 / 12800));
+                       this_thread::sleep_for(microseconds((pkt.pts - last_pts) * 1000000 / TIMEBASE));
                }
                last_pts = pkt.pts;
                current_pts = pkt.pts;
index 9f7be200ad7c9976ae71f1269bc7a9e339971414..bf478ab4819dbe84c39a6536a64fcc4e78e92255 100644 (file)
@@ -17,6 +17,7 @@
 #include "jpeg_frame_view.h"
 #include "mux.h"
 #include "player.h"
+#include "timebase.h"
 #include "video_stream.h"
 
 using namespace std;
@@ -82,10 +83,9 @@ void Player::thread_func(bool also_output_to_stream)
                                next_pts = *it;
                        }
 
-                       // FIXME: assumes a given timebase.
                        double speed = 0.5;
                        steady_clock::time_point next_frame_start =
-                               origin + microseconds((next_pts - pts_origin) * int(1000000 / speed) / 12800);
+                               origin + microseconds((next_pts - pts_origin) * int(1000000 / speed) / TIMEBASE);
 
                        // Sleep until the next frame start, or until there's a new clip we're supposed to play.
                        {
@@ -123,7 +123,7 @@ void Player::thread_func(bool also_output_to_stream)
                                        next_next_pts = *it;
                                }
                                if (next_next_pts != -1) {
-                                       auto frame_len = microseconds((next_next_pts - next_pts) * int(1000000 / speed) / 12800) / 2;
+                                       auto frame_len = microseconds((next_next_pts - next_pts) * int(1000000 / speed) / TIMEBASE) / 2;
                                        int64_t interpolated_pts = pts + lrint(duration<double>(frame_len).count() * TIMEBASE);
                                        video_stream->schedule_interpolated_frame(interpolated_pts, stream_idx, next_pts, next_next_pts, 0.5f);
                                }