From 2bf6c1c8e81e82c371d3eaad2503f1d00f7ffcee Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 12 Jul 2022 11:25:09 +0200 Subject: lavc/encode: pass through frame durations to encoded packets The generic code can only handle the no-delay case. Encoders with delay need to be handled individually, which will be done in the following commits. --- libavcodec/avcodec.h | 3 ++- libavcodec/encode.c | 14 ++++++++++++-- libavcodec/version.h | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 9c246c455c..96ee88c4fa 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -2647,7 +2647,8 @@ int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame); * * frame->time_base should be set to the same value as * avctx->time_base. This is not required yet, but may be in - * the future. + * the future. frame->duration will be ignored unless time_base + * is set. * * For audio: * If AV_CODEC_CAP_VARIABLE_FRAME_SIZE is set, then each frame diff --git a/libavcodec/encode.c b/libavcodec/encode.c index da910b1b60..76eb69bba8 100644 --- a/libavcodec/encode.c +++ b/libavcodec/encode.c @@ -230,10 +230,13 @@ static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt) if (avpkt->pts == AV_NOPTS_VALUE) avpkt->pts = frame->pts; - if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) { - if (!avpkt->duration) + if (!avpkt->duration) { + if (frame->duration) + avpkt->duration = frame->duration; + else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) { avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples); + } } } @@ -424,6 +427,13 @@ FF_ENABLE_DEPRECATION_WARNINGS return ret; } + // only use the frame duration if the timebase is set; + // otherwise we cannot be sure that whatever value it has is in the right + // timebase, so we would produce an incorrect value, which is worse than + // none at all + if (!dst->time_base.num || !dst->time_base.den) + dst->duration = 0; + return 0; } diff --git a/libavcodec/version.h b/libavcodec/version.h index 12421666b9..d7d5fca6b2 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -30,7 +30,7 @@ #include "version_major.h" #define LIBAVCODEC_VERSION_MINOR 42 -#define LIBAVCODEC_VERSION_MICRO 102 +#define LIBAVCODEC_VERSION_MICRO 103 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \ -- cgit v1.2.3