From e477123e88fe1bdcc46dda316af77c2807967386 Mon Sep 17 00:00:00 2001 From: k4yt3x Date: Sun, 10 Nov 2024 00:00:00 +0000 Subject: [PATCH] fix(encoder): timestamp errors processing frames with PTS equal to 0 Signed-off-by: k4yt3x --- CHANGELOG.md | 6 ++++++ include/libvideo2x/encoder.h | 3 ++- src/encoder.cpp | 9 ++++++++- src/libvideo2x.cpp | 12 ++++++++++-- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9538a90..7f09f5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- Timestamp errors processing frames with PTS equal to 0 (#1222). + ## [6.1.1] - 2024-11-07 ### Added diff --git a/include/libvideo2x/encoder.h b/include/libvideo2x/encoder.h index 64ecb35..13d7814 100644 --- a/include/libvideo2x/encoder.h +++ b/include/libvideo2x/encoder.h @@ -28,7 +28,8 @@ int write_frame( AVFrame *frame, AVCodecContext *enc_ctx, AVFormatContext *ofmt_ctx, - int out_vstream_idx + int out_vstream_idx, + int64_t frame_idx ); int flush_encoder(AVCodecContext *enc_ctx, AVFormatContext *ofmt_ctx, int out_vstream_idx); diff --git a/src/encoder.cpp b/src/encoder.cpp index 44bc272..6826c6b 100644 --- a/src/encoder.cpp +++ b/src/encoder.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include @@ -202,11 +203,17 @@ int write_frame( AVFrame *frame, AVCodecContext *enc_ctx, AVFormatContext *ofmt_ctx, - int out_vstream_idx + int out_vstream_idx, + int64_t frame_idx ) { AVFrame *converted_frame = nullptr; int ret; + // Set the frame's presentation timestamp if not set + if (frame->pts <= 0) { + frame->pts = frame_idx; + } + // Convert the frame to the encoder's pixel format if needed if (frame->format != enc_ctx->pix_fmt) { converted_frame = convert_avframe_pix_fmt(frame, enc_ctx->pix_fmt); diff --git a/src/libvideo2x.cpp b/src/libvideo2x.cpp index f9f70f0..e56552b 100644 --- a/src/libvideo2x.cpp +++ b/src/libvideo2x.cpp @@ -124,7 +124,13 @@ static int process_frames( return ret; } else if (ret == 0 && processed_frame != nullptr) { if (!benchmark) { - ret = write_frame(processed_frame, enc_ctx, ofmt_ctx, out_vstream_idx); + ret = write_frame( + processed_frame, + enc_ctx, + ofmt_ctx, + out_vstream_idx, + proc_ctx->processed_frames + ); if (ret < 0) { av_strerror(ret, errbuf, sizeof(errbuf)); spdlog::critical("Error encoding/writing frame: {}", errbuf); @@ -174,7 +180,9 @@ static int process_frames( // Encode and write all flushed frames for (AVFrame *&flushed_frame : flushed_frames) { - ret = write_frame(flushed_frame, enc_ctx, ofmt_ctx, out_vstream_idx); + ret = write_frame( + flushed_frame, enc_ctx, ofmt_ctx, out_vstream_idx, proc_ctx->processed_frames + ); if (ret < 0) { av_strerror(ret, errbuf, sizeof(errbuf)); spdlog::critical("Error encoding/writing flushed frame: {}", errbuf);