From patchwork Wed Jan 25 16:55:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 40117 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3ca3:b0:b9:1511:ac2c with SMTP id b35csp547109pzj; Wed, 25 Jan 2023 08:58:13 -0800 (PST) X-Google-Smtp-Source: AMrXdXskyuuNFeuhFH8oNz7sHe0qgSmZg7KjPoBAf1X2e2CgnR+4EDfpXYJ9IUyaTTia1Pffizhn X-Received: by 2002:a17:906:6844:b0:86e:f478:f598 with SMTP id a4-20020a170906684400b0086ef478f598mr33371403ejs.44.1674665893468; Wed, 25 Jan 2023 08:58:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674665893; cv=none; d=google.com; s=arc-20160816; b=CiL7OpP0hmX09PXw31aFdTTBimTqcC5HzXBk2eEoCRfe6m6z5U1UyGK0dE/LWL9M4L UocL7MlKm01faWexAY9cuPt7G+QxCO96B6/6Ge++qKr+E0hCWo26XwzarpAyPRH6ZNP4 TWk+V2CWOSPxL7tuCTqdDawabSRbA58ivTiopJ1MEhTv/CvaCCjDBPxsEwtlUaq4qFix qXJecUn9djDv4yFssyKLiQpjyZtv3zbcd9P7tatfwSteV6b3f6nGxGg0rsgW8HIp8jjS GY0kpd5pmUcd51/KlDEOrVDjIsENP+vM/sb9aGcO8o8bYAytMFASiNxH+7tMqqQ5J9pN xjPQ== 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=D47lTmOhBjtjQsF4/YIoN8JR4QqwzNQ5euahBionK/Q=; b=I1ZX8HfWEOCS2Z+BozjYcrVM18xcgKR5fYqwmpFNENlQLyCTPhai30ARXbRKk8mXCh aSdHZKpa1Z4sNKkabqk2SS3cA6IBpShEfO5rMoWWbXwrbGpAmIw3jg39cZwuLiT6oiPX QQZOCaf4sRCPZjBWu2tvnEl4i5obufvI1Wtd9Lua0OWqlDZkt4oFUjH/joA2brJIkjNT PMvWvTDX66kCZxKp+NArAI2qqJoJwaSQHKPq+VgbJlI73raE0qVJovBzYdLRB0hWvA7n W9A94zn5LijOiXfEZXxbb5ARb7KaPYxwjJSDT6VuWXDUTv9Ld7L4BNyQkQSk+Ey/y5uA 7Xvg== 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 uj42-20020a170907c9aa00b0087345b10ebfsi5782982ejc.14.2023.01.25.08.58.13; Wed, 25 Jan 2023 08:58:13 -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 5CB8F68BE0B; Wed, 25 Jan 2023 18:56:33 +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 33B6468BD81 for ; Wed, 25 Jan 2023 18:56:19 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id D4E612405EC for ; Wed, 25 Jan 2023 17:56:16 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 6jXul-suJQ_K for ; Wed, 25 Jan 2023 17:56:16 +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 0F2962405B5 for ; Wed, 25 Jan 2023 17:56:05 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id BEBD53A035B for ; Wed, 25 Jan 2023 17:55:58 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 25 Jan 2023 17:55:24 +0100 Message-Id: <20230125165537.5371-6-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230125165537.5371-1-anton@khirnov.net> References: <20230125165537.5371-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 06/19] 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: bgtOZy99TuZv 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 f52337990f..a20a899365 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -15,7 +15,7 @@ libavutil: 2021-04-27 API changes, most recent first: 2023-01-xx - xxxxxxxxxx - lavc 59.57.100 - avcodec.h - Add AV_CODEC_FLAG_COPY_OPAQUE. + Add AV_CODEC_FLAG_COPY_OPAQUE and AV_CODEC_FLAG_FRAME_DURATION. 2023-01-13 - xxxxxxxxxx - lavu 57.44.100 - ambient_viewing_environment.h frame.h Adds a new structure for holding H.274 Ambient Viewing Environment metadata, diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 54ab7b81f9..1daa1106aa 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -267,6 +267,13 @@ typedef struct RcOverride{ * taken from the first of those. */ #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 555bd3ff96..c92beaf8e1 100644 --- a/libavcodec/encode.c +++ b/libavcodec/encode.c @@ -231,10 +231,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); + } } ret = ff_encode_reordered_opaque(avctx, avpkt, frame); @@ -463,6 +466,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 4b38c42043..322ec7a156 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"},