Skip to content

Commit 0c722b4

Browse files
committed
fix: add FFmpeg 5.0 compatibility for AVFrame::duration
AVFrame::duration was added in FFmpeg 5.1 (libavutil 57.28.100). For FFmpeg 5.0, use the deprecated pkt_duration field instead. Added AV_FRAME_DURATION and AV_FRAME_SET_DURATION macros in common.h that select the correct field based on FFmpeg version.
1 parent e303f8a commit 0c722b4

4 files changed

Lines changed: 14 additions & 4 deletions

File tree

‎src/common.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ extern "C" {
2626
#error "FFmpeg 5.0+ (libavcodec 59+) is required"
2727
#endif
2828

29+
// AVFrame::duration was added in FFmpeg 5.1 (libavutil 57.28.100)
30+
// Before that, use pkt_duration (deprecated in 5.1+)
31+
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
32+
#define AV_FRAME_DURATION(frame) ((frame)->duration)
33+
#define AV_FRAME_SET_DURATION(frame, val) ((frame)->duration = (val))
34+
#else
35+
#define AV_FRAME_DURATION(frame) ((frame)->pkt_duration)
36+
#define AV_FRAME_SET_DURATION(frame, val) ((frame)->pkt_duration = (val))
37+
#endif
38+
2939
namespace webcodecs {
3040

3141
//==============================================================================

‎src/image_decoder.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,8 @@ bool ImageDecoder::ParseAnimatedImageMetadata() {
546546

547547
// Duration from packet or frame
548548
int64_t duration = pkt->duration;
549-
if (duration <= 0 && frm->duration > 0) {
550-
duration = frm->duration;
549+
if (duration <= 0 && AV_FRAME_DURATION(frm.get()) > 0) {
550+
duration = AV_FRAME_DURATION(frm.get());
551551
}
552552
if (duration > 0) {
553553
decoded_frame.duration =

‎src/video_encoder.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ Napi::Value VideoEncoder::Encode(const Napi::CallbackInfo& info) {
526526
frame->height = video_frame->GetHeight();
527527
frame->format = AV_PIX_FMT_RGBA;
528528
frame->pts = video_frame->GetTimestampValue();
529-
frame->duration = video_frame->GetDurationValue();
529+
AV_FRAME_SET_DURATION(frame.get(), video_frame->GetDurationValue());
530530

531531
// Copy RGBA data
532532
size_t data_size = frame->width * frame->height * kBytesPerPixelRgba;

‎src/video_encoder_worker.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ void VideoEncoderWorker::OnEncode(const EncodeMessage& msg) {
341341
int src_width = src_frame->width;
342342
int src_height = src_frame->height;
343343
int64_t timestamp = src_frame->pts;
344-
int64_t duration = src_frame->duration;
344+
int64_t duration = AV_FRAME_DURATION(src_frame);
345345

346346
// For RGBA input, convert to YUV420P
347347
// The source frame contains RGBA data packed in data[0]

0 commit comments

Comments
 (0)