From 28c341824076ec208e855c9c7b3c41ad97020f56 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Mon, 10 Sep 2018 23:57:00 +0200 Subject: [PATCH] Read timebase from the input video. --- clip_list.cpp | 7 +++---- main.cpp | 11 +++++++++-- player.cpp | 6 +++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/clip_list.cpp b/clip_list.cpp index 09fe034..75f4a9f 100644 --- a/clip_list.cpp +++ b/clip_list.cpp @@ -5,14 +5,14 @@ #include #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; diff --git a/main.cpp b/main.cpp index ccfb281..fc74285 100644 --- 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; diff --git a/player.cpp b/player.cpp index 9f7be20..bf478ab 100644 --- a/player.cpp +++ b/player.cpp @@ -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(frame_len).count() * TIMEBASE); video_stream->schedule_interpolated_frame(interpolated_pts, stream_idx, next_pts, next_next_pts, 0.5f); } -- 2.39.2