From 9ca63b321a91da727c62b4d93b9948ed1065aca7 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Sat, 3 Jul 2021 17:01:55 -0500 Subject: [PATCH] Reverting video_timestamp increment logic which used pkt.duration. In some codecs (such as vp8), this approach breaks due to differences in the timebase vs the framerate. For example, if the timebase is an inverse of the FPS, everything works. But if the timebase is not, for example 1/1000000, this approach breaks. --- src/FFmpegReader.cpp | 4 ++-- src/FFmpegWriter.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index b97d7345b..98d39ce1f 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -768,12 +768,12 @@ void FFmpegReader::UpdateVideoInfo() { // Check for valid duration (if found) if (info.duration <= 0.0f && pFormatCtx->duration >= 0) // Use the format's duration - info.duration = pFormatCtx->duration / AV_TIME_BASE; + info.duration = float(pFormatCtx->duration) / AV_TIME_BASE; // Calculate duration from filesize and bitrate (if any) if (info.duration <= 0.0f && info.video_bit_rate > 0 && info.file_size > 0) // Estimate from bitrate, total bytes, and framerate - info.duration = (info.file_size / info.video_bit_rate); + info.duration = float(info.file_size) / info.video_bit_rate; // No duration found in stream of file if (info.duration <= 0.0f) { diff --git a/src/FFmpegWriter.cpp b/src/FFmpegWriter.cpp index 92e9f8fa1..f3b12800a 100644 --- a/src/FFmpegWriter.cpp +++ b/src/FFmpegWriter.cpp @@ -947,7 +947,7 @@ void FFmpegWriter::flush_encoders() { // Increment PTS by duration of packet audio_timestamp += pkt.duration; - // deallocate memory for packet + // deallocate memory for packet AV_FREE_PACKET(&pkt); } } @@ -1885,8 +1885,8 @@ void FFmpegWriter::write_audio_packets(bool is_final) { ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code); } - // Increment PTS by duration of packet - audio_timestamp += pkt.duration; + // Increment PTS (no pkt.duration, so calculate with maths) + audio_timestamp += FFMIN(audio_input_frame_size, audio_input_position); // deallocate AVFrame av_freep(&(frame_final->data[0])); @@ -2134,9 +2134,6 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr frame, AVFrame *fra } } - // Increment PTS (in frames and scaled to the codec's timebase) - video_timestamp += pkt.duration; - // Deallocate packet AV_FREE_PACKET(&pkt); #if USE_HW_ACCEL @@ -2149,6 +2146,9 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr frame, AVFrame *fra #endif // USE_HW_ACCEL } + // Increment PTS (in frames and scaled to the codec's timebase) + video_timestamp += av_rescale_q(1, av_make_q(info.fps.den, info.fps.num), video_codec_ctx->time_base); + // Success return true; }