From eae89cea4be5b6abb147cdeabdbadd844984b182 Mon Sep 17 00:00:00 2001 From: k4yt3x Date: Sat, 18 Jan 2025 00:00:00 +0000 Subject: [PATCH] feat(libvideo2x): allow processing videos without PTS information Signed-off-by: k4yt3x --- CHANGELOG.md | 1 + include/libvideo2x/encoder.h | 2 +- src/encoder.cpp | 5 +---- src/libvideo2x.cpp | 8 +++++++- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d3f5ec..f4a2092 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Multi-versioning to critical functions to enhance performance in generic architecture builds. +- Support for processing videos without PTS information (#1278). - The feature to copy input streams' metadata to the output streams (#1282). ### Changed diff --git a/include/libvideo2x/encoder.h b/include/libvideo2x/encoder.h index 5e98ed8..fba93a5 100644 --- a/include/libvideo2x/encoder.h +++ b/include/libvideo2x/encoder.h @@ -65,7 +65,7 @@ class Encoder { int in_vstream_idx ); - int write_frame(AVFrame* frame, int64_t frame_idx); + int write_frame(AVFrame* frame); int flush(); AVCodecContext* get_encoder_context() const; diff --git a/src/encoder.cpp b/src/encoder.cpp index 1ddce69..7954e9c 100644 --- a/src/encoder.cpp +++ b/src/encoder.cpp @@ -269,16 +269,13 @@ int Encoder::init( } [[gnu::target_clones("arch=x86-64-v4", "arch=x86-64-v3", "default")]] -int Encoder::write_frame(AVFrame* frame, int64_t frame_idx) { +int Encoder::write_frame(AVFrame* frame) { AVFrame* converted_frame = nullptr; int ret; // Let the encoder decide the frame type frame->pict_type = AV_PICTURE_TYPE_NONE; - // Calculate this frame's presentation timestamp (PTS) - frame->pts = av_rescale_q(frame_idx, av_inv_q(enc_ctx_->framerate), enc_ctx_->time_base); - // Convert the frame to the encoder's pixel format if needed if (frame->format != enc_ctx_->pix_fmt) { converted_frame = conversions::convert_avframe_pix_fmt(frame, enc_ctx_->pix_fmt); diff --git a/src/libvideo2x.cpp b/src/libvideo2x.cpp index 751e9c3..fbcc185 100644 --- a/src/libvideo2x.cpp +++ b/src/libvideo2x.cpp @@ -1,4 +1,5 @@ #include "libvideo2x.h" +#include extern "C" { #include @@ -152,6 +153,7 @@ int VideoProcessor::process_frames( AVCodecContext* dec_ctx = decoder.get_codec_context(); int in_vstream_idx = decoder.get_video_stream_index(); AVFormatContext* ofmt_ctx = encoder.get_format_context(); + AVCodecContext* enc_ctx = encoder.get_encoder_context(); int* stream_map = encoder.get_stream_map(); // Reference to the previous frame does not require allocation @@ -235,6 +237,10 @@ int VideoProcessor::process_frames( return ret; } + // Calculate this frame's presentation timestamp (PTS) + frame->pts = + av_rescale_q(frame_idx_, av_inv_q(enc_ctx->framerate), enc_ctx->time_base); + // Process the frame based on the selected processing mode AVFrame* proc_frame = nullptr; switch (processor->get_processing_mode()) { @@ -308,7 +314,7 @@ int VideoProcessor::write_frame(AVFrame* frame, encoder::Encoder& encoder) { int ret = 0; if (!benchmark_) { - ret = encoder.write_frame(frame, frame_idx_); + ret = encoder.write_frame(frame); if (ret < 0) { av_strerror(ret, errbuf, sizeof(errbuf)); logger()->critical("Error encoding/writing frame: {}", errbuf);