From patchwork Thu Jul 28 14:24:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 37018 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:1649:b0:8b:613a:194d with SMTP id no9csp313377pzb; Thu, 28 Jul 2022 07:25:04 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tY8HP8LMTlrioBBjlPjCaZmGyTjkk00916f9pVjJtiG1F+Wm0MmPshj9hdqF1lNcI+FCgn X-Received: by 2002:a17:907:9612:b0:72e:56bd:2b9a with SMTP id gb18-20020a170907961200b0072e56bd2b9amr21532599ejc.526.1659018303732; Thu, 28 Jul 2022 07:25:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659018303; cv=none; d=google.com; s=arc-20160816; b=g3mQoPQ90Uf+c3533EqUx75VJPUYH+UNJDnobWMN5TYceQMctImUNDbxL8x83VbX3V 2RHlDuqdgv3F0rgx6BUtYuog4n9bApBsjhOXNgY5v+4foobBza431Y2bGh2R9mwKvZaY aVBHMT/y5gb+SOaAAbYdSZ6ljqvF4jjV3wfsF7XhOfAsYDd0JogcGP+F18GSpFAdUuwF //czd6gn64AqHjyY2QmHCUqlAcS13l016Fz4f16eRIXjNBlK8AXhMXTHCJI8UbTWpHAr s81EMLwSNJzTJVKvo2jqOUZjW5cGN5BOldYpaspBzUHqs/QmzvQwrfLPGF7Rerru2TCU 9rTA== 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=bkRB/DPGcu0mV/hJG+WTN736uSSWKSatRVPNzQdyrLg=; b=oyYRySbvFUryGS832QEDH++NZnOdJcSXm2JCVew5ZpqpRJwA2Gl/fOyGTjLZs7rLZN W3l8+a1Q/uq8bNENnp5Q8YPDODPetZMLDK0JWlCb/hZP70SKnzaBRsHV8ctFzHIR6eI3 kpUpwiGxhRWyjAhrlrbugcetfOKaKqEnUu6irRXmMtpbgHSP0nL+y52hQEze4W0KCTjB D3G63w7EVuvHAWpL7C17mhPZ/WNEGNTxfBEdLBChfEa0RRXtmEgbaZcY5Gp8u+Pv5SEs 0KeFGaVtKsdom7lQoj7JDMafP7kB0UExH0P2mKIOA31u8KG1FuobOAcVbvX8gaMErP9Q 4/kw== 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 bo27-20020a0564020b3b00b0043cdb04b9f1si754358edb.411.2022.07.28.07.25.03; Thu, 28 Jul 2022 07:25:03 -0700 (PDT) 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 1F7B768B99A; Thu, 28 Jul 2022 17:25:00 +0300 (EEST) 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 9C60C68B92F for ; Thu, 28 Jul 2022 17:24:53 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 131EC240179 for ; Thu, 28 Jul 2022 16:24:53 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id psWJnyHMGo08 for ; Thu, 28 Jul 2022 16:24:52 +0200 (CEST) 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 60BC82400F5 for ; Thu, 28 Jul 2022 16:24:52 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id A5C473A03A9; Thu, 28 Jul 2022 16:24:49 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 28 Jul 2022 16:24:01 +0200 Message-Id: <20220728142401.669-1-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] lavc/libx264: support AV_CODEC_CAP_ENCODER_RECON_FRAME 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: c+59pouCQ+n4 Bump the version requirement to 122, which introduced b_full_recon. --- configure | 2 +- libavcodec/libx264.c | 55 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/configure b/configure index 6629d14099..50d73a5325 100755 --- a/configure +++ b/configure @@ -6674,7 +6674,7 @@ enabled libwebp && { enabled libwebp_encoder && require_pkg_config libwebp "libwebp >= 0.2.0" webp/encode.h WebPGetEncoderVersion enabled libwebp_anim_encoder && check_pkg_config libwebp_anim_encoder "libwebpmux >= 0.4.0" webp/mux.h WebPAnimEncoderOptionsInit; } enabled libx264 && require_pkg_config libx264 x264 "stdint.h x264.h" x264_encoder_encode && - require_cpp_condition libx264 x264.h "X264_BUILD >= 118" && { + require_cpp_condition libx264 x264.h "X264_BUILD >= 122" && { [ "$toolchain" != "msvc" ] || require_cpp_condition libx264 x264.h "X264_BUILD >= 158"; } && check_cpp_condition libx262 x264.h "X264_MPEG2" diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index e8c1fb2106..6afa3cdadb 100644 --- a/libavcodec/libx264.c +++ b/libavcodec/libx264.c @@ -311,6 +311,28 @@ static void free_picture(AVCodecContext *ctx) pic->extra_sei.num_payloads = 0; } +static enum AVPixelFormat csp_to_pixfmt(int csp) +{ + switch (csp) { +#ifdef X264_CSP_I400 + case X264_CSP_I400: return AV_PIX_FMT_GRAY8; + case X264_CSP_I400 | X264_CSP_HIGH_DEPTH: return AV_PIX_FMT_GRAY10; +#endif + case X264_CSP_I420: return AV_PIX_FMT_YUV420P; + case X264_CSP_I420 | X264_CSP_HIGH_DEPTH: return AV_PIX_FMT_YUV420P10; + case X264_CSP_I422: return AV_PIX_FMT_YUV422P; + case X264_CSP_I422 | X264_CSP_HIGH_DEPTH: return AV_PIX_FMT_YUV422P10; + case X264_CSP_I444: return AV_PIX_FMT_YUV444P; + case X264_CSP_I444 | X264_CSP_HIGH_DEPTH: return AV_PIX_FMT_YUV444P10; + case X264_CSP_NV12: return AV_PIX_FMT_NV12; +#ifdef X264_CSP_NV21 + case X264_CSP_NV21: return AV_PIX_FMT_NV21; +#endif + case X264_CSP_NV16: return AV_PIX_FMT_NV16; + }; + return AV_PIX_FMT_NONE; +} + static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, int *got_packet) { @@ -496,6 +518,33 @@ static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, if (x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL, &pic_out) < 0) return AVERROR_EXTERNAL; + if (nnal && (ctx->flags & AV_CODEC_FLAG_RECON_FRAME)) { + AVCodecInternal *avci = ctx->internal; + + av_frame_unref(avci->recon_frame); + + avci->recon_frame->format = csp_to_pixfmt(pic_out.img.i_csp); + if (avci->recon_frame->format == AV_PIX_FMT_NONE) { + av_log(ctx, AV_LOG_ERROR, + "Unhandled reconstructed frame colorspace: %d\n", + pic_out.img.i_csp); + return AVERROR(ENOSYS); + } + + avci->recon_frame->width = ctx->width; + avci->recon_frame->height = ctx->height; + for (int i = 0; i < pic_out.img.i_plane; i++) { + avci->recon_frame->data[i] = pic_out.img.plane[i]; + avci->recon_frame->linesize[i] = pic_out.img.i_stride[i]; + } + + ret = av_frame_make_writable(avci->recon_frame); + if (ret < 0) { + av_frame_unref(avci->recon_frame); + return ret; + } + } + ret = encode_nals(ctx, pkt, nal, nnal); if (ret < 0) return ret; @@ -928,6 +977,9 @@ static av_cold int X264_init(AVCodecContext *avctx) if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) x4->params.b_repeat_headers = 0; + if (avctx->flags & AV_CODEC_FLAG_RECON_FRAME) + x4->params.b_full_recon = 1; + if(x4->x264opts){ const char *p= x4->x264opts; while(p){ @@ -1223,7 +1275,8 @@ FFCodec ff_libx264_encoder = { .p.id = AV_CODEC_ID_H264, .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS | - AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, + AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE | + AV_CODEC_CAP_ENCODER_RECON_FRAME, .p.priv_class = &x264_class, .p.wrapper_name = "libx264", .priv_data_size = sizeof(X264Context),