From patchwork Sun Nov 27 17:03:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 39542 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a05:6a11:561f:b0:33d:fc04:f431 with SMTP id rr31csp8295465pxb; Sun, 27 Nov 2022 09:09:14 -0800 (PST) X-Google-Smtp-Source: AA0mqf7J+Vqjc37/6fzFy0e14AR+TYB/j0dhyb5YIDCGhyVhUgdrfOkm+SgdbnmlgZqrhrEYpYJ0 X-Received: by 2002:a05:6402:528d:b0:468:dc9:ec08 with SMTP id en13-20020a056402528d00b004680dc9ec08mr29346199edb.17.1669568954507; Sun, 27 Nov 2022 09:09:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669568954; cv=none; d=google.com; s=arc-20160816; b=X6S+C8yfTZJWKebsAH+OCwZP1rP1bRTPbRuLL9qLwIxQriUDVxplLqYcN6IKuP9xAR hInhdf5vHCH09mEV/DWPKi1cYuTOj9plu2lLVEyumXRvlYX2AtGlVKznrp40gcQDtxlo kWyAVd18sRf+jx1hJwzOGVAtWd3CD7LLDF6SRaWNEiOYm1qtVJvUzMMJBEOARjqW2T5J 5hIU9kGOgl0b5SMYugXJsK3Z0tgPfn+j5Xmf6taMOvAp+cqns4zgZfAni5kSG9NaaMm5 N1kqVd0XVBC2beOo2OOKkjhW2Z5R1u7hOmhjHaM1LZ2oG+1SCOIgQpM4duJ9eJ+Forl9 /PJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=Q+v3tASp6NkH6rIMgYEuguc7Q8hTgbZ7j2PqVCExo/s=; b=IHgDPjY6NneqthBGtsibWfD0pTX0kBegNlrRRoyjdHqjJuvadmeJ1bxQAR0aZMCp+P cY4VtU/dsz5nwtXYb57P6N7qmhDh32r5NhGDpMBlVCvcaCstEHM7AqonbnWKfSw1vKYG ek/8+DATSMUGRdpJzaGeQnHjfj4ozFxZFBfbhSgcuQW4C4zsjwA8xxmps6tyEdApJgWs 0hQVSYEuPVlrk6iTrbmeTWBEVdRGaos7iDuKzCygYIIBlYHMSsoHE2u8wqWI5ZLjwyiT JKsSbw712lujcdes/l3JHoN8kjZfhhh7comI+86tN8uBci3IH3pf5iWCYoJlNRbJWgjq X+vQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id g8-20020a056402320800b0046afbd6c84fsi2911605eda.553.2022.11.27.09.09.14; Sun, 27 Nov 2022 09:09:14 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0CAF268BC47; Sun, 27 Nov 2022 19:06:21 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F360968B735 for ; Sun, 27 Nov 2022 19:05:58 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id DD1682406CD for ; Sun, 27 Nov 2022 18:05:56 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id nAxcrYq6Z51H for ; Sun, 27 Nov 2022 18:05:55 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id AA5B32406CB for ; Sun, 27 Nov 2022 18:05:51 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 999483A25E9 for ; Sun, 27 Nov 2022 18:05:46 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sun, 27 Nov 2022 18:03:39 +0100 Message-Id: <20221127170351.11477-18-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221127170351.11477-1-anton@khirnov.net> References: <20221127170351.11477-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 18/30] lavc/encode: pass through frame durations to encoded packets X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: UrAX2061v3So Content-Length: 4214 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. --- doc/APIchanges | 2 +- libavcodec/avcodec.h | 7 +++++++ libavcodec/encode.c | 14 ++++++++++++-- libavcodec/options_table.h | 1 + 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index a3c9819e32..4ae5571f71 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -15,7 +15,7 @@ libavutil: 2021-04-27 API changes, most recent first: 2022-12-xx - xxxxxxxxxx - lavc 59.55.100 - avcodec.h - Add AV_CODEC_FLAG_COPY_OPAQUE. + Add AV_CODEC_FLAG_COPY_OPAQUE and AV_CODEC_FLAG_FRAME_DURATION. 2022-11-xx - xxxxxxxxxx - lavu 57.43.100 - tx.h Add AV_TX_FLOAT_DCT, AV_TX_DOUBLE_DCT and AV_TX_INT32_DCT. diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 79558786ee..a31d413179 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -249,6 +249,13 @@ typedef struct RcOverride{ * @ref AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE capability flag. */ #define AV_CODEC_FLAG_COPY_OPAQUE (1 << 7) +/** + * Signal to the encoder that the values of AVFrame.duration are valid and + * should be used (typically for transferring them to output packets). + * + * If this flag is not set, frame durations are ignored. + */ +#define AV_CODEC_FLAG_FRAME_DURATION (1 << 8) /** * Use internal 2pass ratecontrol in first pass mode. */ diff --git a/libavcodec/encode.c b/libavcodec/encode.c index e4270b6c34..00b7a7dde8 100644 --- a/libavcodec/encode.c +++ b/libavcodec/encode.c @@ -215,10 +215,13 @@ int ff_encode_encode_cb(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); + } } avctx->reordered_opaque = frame->reordered_opaque; @@ -454,6 +457,13 @@ FF_ENABLE_DEPRECATION_WARNINGS return ret; } + // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set, + // since 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 (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION)) + dst->duration = 0; + return 0; } diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h index ad41dc7bed..011314db96 100644 --- a/libavcodec/options_table.h +++ b/libavcodec/options_table.h @@ -59,6 +59,7 @@ static const AVOption avcodec_options[] = { {"qscale", "use fixed qscale", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_QSCALE }, INT_MIN, INT_MAX, 0, "flags"}, {"recon_frame", "export reconstructed frames", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_RECON_FRAME}, .unit = "flags"}, {"copy_opaque", "propagate opaque values", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_COPY_OPAQUE}, .unit = "flags"}, +{"frame_duration", "use frame durations", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_FRAME_DURATION}, .unit = "flags"}, {"pass1", "use internal 2-pass ratecontrol in first pass mode", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_PASS1 }, INT_MIN, INT_MAX, 0, "flags"}, {"pass2", "use internal 2-pass ratecontrol in second pass mode", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_PASS2 }, INT_MIN, INT_MAX, 0, "flags"}, {"gray", "only decode/encode grayscale", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_GRAY }, INT_MIN, INT_MAX, V|E|D, "flags"},