From patchwork Wed Jan 25 16:55:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 40107 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3ca3:b0:b9:1511:ac2c with SMTP id b35csp546263pzj; Wed, 25 Jan 2023 08:56:36 -0800 (PST) X-Google-Smtp-Source: AMrXdXsepI6+45U7QGIpQJggejAK6Y8qZ+but4IZQvmh2QwK+mUiz/Xddg0MyoVPWNaQAFPYAmjR X-Received: by 2002:a17:906:ca16:b0:877:6a03:9ad4 with SMTP id jt22-20020a170906ca1600b008776a039ad4mr26362752ejb.56.1674665796647; Wed, 25 Jan 2023 08:56:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674665796; cv=none; d=google.com; s=arc-20160816; b=RTe48aqV7c95tak33BiifZa5O2awnmtGG12EBOfPREKafdmvZRp/Fc8pAEvGgeIr6B yfjJnwE2D2/8qyJwffNiI8D6ZV7hT7kC8mwDerEDc6JbGLcXODimc5UNLIveAWWv4ZPU 5uiVurW8rkGMKDexcwe2KiOsweuTomnxmpoju86DoKZdQgHr9kKAUVQ83ppKhsoxXeFf dyBeuE+3ocnkX3x23FBEhlDxfYwU9Q1MEra/WCCbAYQqW6onzSLSsywFEc9FMOW2DnSY VHusa6JRXwOlZdQV+He2IvXyO1Jvfe4U2i9Si4GfMa0G+X+O26/MPfBxCTl6SpFW5Wpv qZ6A== 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=cb35MBkDAGdGiw3BQqY54Ic1ao+W7kB81lVikjYy+5A=; b=iGfdVmdeZBWA8o/3X7ZHdOP3Ujb5+ZtoVZ6AOxM3U3DlNPFI0p+8Z44fAfZ6x9XFET NvLSOX2L3SY+DULeCe+ttV1RgkiCHnWfqjRcx3u9b2dIyv97sqZS/tXKKqDgbZMxdNa+ FczC6GFpSmNpEVnAmWO31GbRAxCFsOyek+bwfZys62R4twOHwc8FkvYBScsswbrfjBNZ KyBOnZ+I8qxpGyhUHlWCO6IYGZ604ukIaMLSvgvE/mYLPr8Gn0Uz/NMQiJw7ndITdl84 HJ6vqHmWzpitlGnsegZPpOAPZ6hXSEU7GcVWylKSXdczyBKB7mGflG6EdJbRJo4hyWn3 Lx3g== 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 y15-20020a170906518f00b0087852bdea4csi100974ejk.839.2023.01.25.08.56.36; Wed, 25 Jan 2023 08:56:36 -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 0475368B440; Wed, 25 Jan 2023 18:56:22 +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 D4A8168BBEB for ; Wed, 25 Jan 2023 18:56:13 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 7369B2404EC for ; Wed, 25 Jan 2023 17:56:13 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 2rBdiawGB2qn for ; Wed, 25 Jan 2023 17:56:05 +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 EE2B1240178 for ; Wed, 25 Jan 2023 17:56:04 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 98A7D3A034A 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:21 +0100 Message-Id: <20230125165537.5371-3-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 03/19] lavc: add a private cap for fake-delay encoders 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: GujWUeEg4jOs Some encoders (ffv1, flac, adx) are marked with AV_CODEC_CAP_DELAY onky in order to be flushed at the end, otherwise they behave as no-delay encoders. Add a capability to mark these encoders. Use it for setting pts generically. --- libavcodec/adxenc.c | 3 +-- libavcodec/codec_internal.h | 8 ++++++++ libavcodec/encode.c | 6 ++++-- libavcodec/ffv1enc.c | 4 +--- libavcodec/flacenc.c | 7 ++----- libavcodec/tests/avcodec.c | 4 ++++ 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/libavcodec/adxenc.c b/libavcodec/adxenc.c index 153c91b852..6e12a58b16 100644 --- a/libavcodec/adxenc.c +++ b/libavcodec/adxenc.c @@ -183,8 +183,6 @@ static int adx_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, dst += BLOCK_SIZE; } - avpkt->pts = frame->pts; - avpkt->duration = frame->nb_samples; *got_packet_ptr = 1; return 0; } @@ -200,4 +198,5 @@ const FFCodec ff_adpcm_adx_encoder = { FF_CODEC_ENCODE_CB(adx_encode_frame), .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE }, + .caps_internal = FF_CODEC_CAP_EOF_FLUSH, }; diff --git a/libavcodec/codec_internal.h b/libavcodec/codec_internal.h index e3b77e6dea..130a7dc3cd 100644 --- a/libavcodec/codec_internal.h +++ b/libavcodec/codec_internal.h @@ -80,6 +80,14 @@ * Codec supports embedded ICC profiles (AV_FRAME_DATA_ICC_PROFILE). */ #define FF_CODEC_CAP_ICC_PROFILES (1 << 9) +/** + * The encoder has AV_CODEC_CAP_DELAY set, but does not actually have delay - it + * only wants to be flushed at the end to update some context variables (e.g. + * 2pass stats) or produce a trailing packet. Besides that it immediately + * produces exactly one output packet per each input frame, just as no-delay + * encoders do. + */ +#define FF_CODEC_CAP_EOF_FLUSH (1 << 10) /** * FFCodec.codec_tags termination value diff --git a/libavcodec/encode.c b/libavcodec/encode.c index fbe2c97cd6..e0b3e43840 100644 --- a/libavcodec/encode.c +++ b/libavcodec/encode.c @@ -211,7 +211,8 @@ int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt, // set the timestamps for the simple no-delay case // encoders with delay have to set the timestamps themselves - if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) { + if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || + (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) { if (avpkt->pts == AV_NOPTS_VALUE) avpkt->pts = frame->pts; @@ -225,7 +226,8 @@ int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt, // dts equals pts unless there is reordering // there can be no reordering if there is no encoder delay if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) || - !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) + !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || + (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH)) avpkt->dts = avpkt->pts; } else { unref: diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c index 0237ac48eb..6649ec7e88 100644 --- a/libavcodec/ffv1enc.c +++ b/libavcodec/ffv1enc.c @@ -1231,8 +1231,6 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, f->picture_number++; pkt->size = buf_p - pkt->data; - pkt->pts = - pkt->dts = pict->pts; pkt->flags |= AV_PKT_FLAG_KEY * f->key_frame; *got_packet = 1; @@ -1301,5 +1299,5 @@ const FFCodec ff_ffv1_encoder = { }, .p.priv_class = &ffv1_class, - .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, + .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_EOF_FLUSH, }; diff --git a/libavcodec/flacenc.c b/libavcodec/flacenc.c index 8aacc93e28..9a9835dfa6 100644 --- a/libavcodec/flacenc.c +++ b/libavcodec/flacenc.c @@ -1690,10 +1690,7 @@ static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, if (out_bytes < s->min_framesize) s->min_framesize = out_bytes; - avpkt->pts = frame->pts; - avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples); - - s->next_pts = avpkt->pts + avpkt->duration; + s->next_pts = frame->pts + ff_samples_to_time_base(avctx, frame->nb_samples); av_shrink_packet(avpkt, out_bytes); @@ -1766,5 +1763,5 @@ const FFCodec ff_flac_encoder = { AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE }, .p.priv_class = &flac_encoder_class, - .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, + .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_EOF_FLUSH, }; diff --git a/libavcodec/tests/avcodec.c b/libavcodec/tests/avcodec.c index 3288a85f64..4c1730425d 100644 --- a/libavcodec/tests/avcodec.c +++ b/libavcodec/tests/avcodec.c @@ -158,6 +158,10 @@ int main(void){ if (codec->capabilities & AV_CODEC_CAP_FRAME_THREADS && codec->capabilities & AV_CODEC_CAP_DELAY) ERR("Frame-threaded encoder %s claims to have delay\n"); + + if (codec2->caps_internal & FF_CODEC_CAP_EOF_FLUSH && + !(codec->capabilities & AV_CODEC_CAP_DELAY)) + ERR("EOF_FLUSH encoder %s is not marked as having delay\n"); } else { if ((codec->type == AVMEDIA_TYPE_SUBTITLE) != (codec2->cb_type == FF_CODEC_CB_TYPE_DECODE_SUB)) ERR("Subtitle decoder %s does not implement decode_sub callback\n");