Skip to content

Commit

Permalink
feat(libvideo2x): allow processing videos without PTS information
Browse files Browse the repository at this point in the history
Signed-off-by: k4yt3x <[email protected]>
  • Loading branch information
k4yt3x committed Jan 18, 2025
1 parent 43ecf9e commit eae89ce
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/libvideo2x/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions src/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/libvideo2x.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "libvideo2x.h"
#include <libavcodec/avcodec.h>

extern "C" {
#include <libavutil/avutil.h>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit eae89ce

Please sign in to comment.