fix(encoder): timestamp errors processing frames with PTS equal to 0

Signed-off-by: k4yt3x <i@k4yt3x.com>
This commit is contained in:
k4yt3x 2024-11-10 00:00:00 +00:00
parent c8f2acdea6
commit e477123e88
No known key found for this signature in database
4 changed files with 26 additions and 4 deletions

View File

@ -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

View File

@ -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);

View File

@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdint>
#include <spdlog/spdlog.h>
@ -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);

View File

@ -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);