Skip to content

Commit

Permalink
Reverting video_timestamp increment logic which used pkt.duration. In…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
jonoomph committed Jul 3, 2021
1 parent 0f2f2e1 commit 9ca63b3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/FFmpegReader.cpp
Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions src/FFmpegWriter.cpp
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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]));
Expand Down Expand Up @@ -2134,9 +2134,6 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr<Frame> 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
Expand All @@ -2149,6 +2146,9 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr<Frame> 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;
}
Expand Down

0 comments on commit 9ca63b3

Please sign in to comment.