From patchwork Wed Mar 15 23:34:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 40692 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:d046:b0:cd:afd7:272c with SMTP id hv6csp3840493pzb; Wed, 15 Mar 2023 16:35:18 -0700 (PDT) X-Google-Smtp-Source: AK7set+ev9Bm7uf5JELBdcXKS5rg7L2bos+6QnOhBsKx7ErUThOXQoEhpjBwpTDgHaCb2Edeta2Y X-Received: by 2002:aa7:cc8e:0:b0:4fa:39a6:af25 with SMTP id p14-20020aa7cc8e000000b004fa39a6af25mr5051257edt.16.1678923318673; Wed, 15 Mar 2023 16:35:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678923318; cv=none; d=google.com; s=arc-20160816; b=A39OyIq8TdrlOldfAqrNbf03YkCXA5meYvPVHs2Kt8Iu02fRmm1GAZZeu+tgW5dg1U Ik/I2B636otz2w2GTUWdpmtVJ2wMJQF77WvnIwB6x3pABwX3lrwOABznkK7q2ujf8Xex W40rlCd7+yOUBpxI0fu1EDkDs4kHVv+oa3rCvQCXRb1sUsTsCtfKA+nInwa8RVd7ORB6 YvsQw0ckEd+ChDlAdvNjRvjOW50GFB8Rjgocb+jwqfx3aWDIoLtVmkMvg4ONxgI82gKA 10MyP3RWAcD0NZ/ESWGfJYGYALy0XDkxwCpZC/G2+UN5ANkoU/vDpkm7jaWFyt+g9AQ7 5XAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:delivered-to; bh=qOKTPus5LJ/4PERPEh0QpeDpStbLqVxtYQ+laZn9p+0=; b=GeSEXWvkbJhX8rfNoLyPa9d/AVFRXP4PZdfoivu3iVJqx4b6qJBnJi5W/h/q2m8h+5 PCv6ehPpPwfdQcrHW76UBv0KhH6yUHGA9iIyaMWpk/4qNQpWRq5vJfFXfBmtuu0IRyZX AaR8UDWPxXp8FnmQpOomReskhLhEWTzdvg3Nav308LFwEkBHJOMjpF0Jnr5HFTnbWjLm Riyu1VJaaBke5EI29hzCD89K1o+X4KQB4TXk88UnZPAtYmFxWzE70gz2iYvGz3nLyO5S D7BTc2jINJ54ZmqjQUVFzKK7qsEppDIdH/C+NviVe2Yagm8cChrzlxhu5ExXGErrk8Cz 9UZg== 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 q22-20020aa7d456000000b004acc68d5d98si6774994edr.489.2023.03.15.16.35.18; Wed, 15 Mar 2023 16:35:18 -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 7BFB268BD67; Thu, 16 Mar 2023 01:34:57 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 445C968BCE8 for ; Thu, 16 Mar 2023 01:34:49 +0200 (EET) Received: (Authenticated sender: michael@niedermayer.cc) by mail.gandi.net (Postfix) with ESMTPSA id 6296B1BF207 for ; Wed, 15 Mar 2023 23:34:48 +0000 (UTC) From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Thu, 16 Mar 2023 00:34:45 +0100 Message-Id: <20230315233445.5282-3-michael@niedermayer.cc> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230315233445.5282-1-michael@niedermayer.cc> References: <20230315233445.5282-1-michael@niedermayer.cc> Subject: [FFmpeg-devel] [PATCH 3/3] avfilter/vf_uspp: 1000% faster with threads 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Teuhz2aZt2TL Signed-off-by: Michael Niedermayer --- libavfilter/vf_uspp.c | 181 +++++++++++++++++++++++------------------- 1 file changed, 99 insertions(+), 82 deletions(-) diff --git a/libavfilter/vf_uspp.c b/libavfilter/vf_uspp.c index a7bf8e3087..0a992df898 100644 --- a/libavfilter/vf_uspp.c +++ b/libavfilter/vf_uspp.c @@ -44,6 +44,7 @@ typedef struct USPPContext { const AVClass *av_class; int log2_count; + int count; int hsub, vsub; int qp; char *codec_name; @@ -55,12 +56,13 @@ typedef struct USPPContext { uint8_t *outbuf; AVCodecContext *avctx_enc[BLOCK*BLOCK]; AVCodecContext *avctx_dec[BLOCK*BLOCK]; - AVPacket *pkt; - AVFrame *frame; - AVFrame *frame_dec; + AVPacket *pkt [BLOCK*BLOCK]; + AVFrame *frame [BLOCK*BLOCK]; + AVFrame *frame_dec [BLOCK*BLOCK]; int8_t *non_b_qp_table; int non_b_qp_stride; int use_bframe_qp; + int quality; } USPPContext; #define OFFSET(x) offsetof(USPPContext, x) @@ -188,13 +190,87 @@ static void store_slice_c(uint8_t *dst, const uint16_t *src, } } -static void filter(USPPContext *p, uint8_t *dst[3], uint8_t *src[3], +static int filter_1phase(AVFilterContext *ctx, void *arg, int i, int nb_jobs) +{ + USPPContext *p = ctx->priv; + int ret, x, y; + int width = ctx->inputs[0]->w; + int height = ctx->inputs[0]->h; + + const int x1 = offset[i+nb_jobs-1][0]; + const int y1 = offset[i+nb_jobs-1][1]; + const int x1c = x1 >> p->hsub; + const int y1c = y1 >> p->vsub; + const int BLOCKc = BLOCK >> p->hsub; + int offset; + AVPacket *pkt = p->pkt[i]; + + av_packet_unref(pkt); + pkt->data = p->outbuf; + pkt->size = p->outbuf_size; + + p->frame[i]->linesize[0] = p->temp_stride[0]; + p->frame[i]->linesize[1] = p->temp_stride[1]; + p->frame[i]->linesize[2] = p->temp_stride[2]; + p->frame[i]->height = height + BLOCK; + p->frame[i]->width = width + BLOCK; + p->frame[i]->data[0] = p->src[0] + x1 + y1 * p->frame[i]->linesize[0]; + p->frame[i]->data[1] = p->src[1] + x1c + y1c * p->frame[i]->linesize[1]; + p->frame[i]->data[2] = p->src[2] + x1c + y1c * p->frame[i]->linesize[2]; + p->frame[i]->format = p->avctx_enc[i]->pix_fmt; + p->frame[i]->quality = p->quality; + + ret = avcodec_send_frame(p->avctx_enc[i], p->frame[i]); + if (ret < 0) { + av_log(p->avctx_enc[i], AV_LOG_ERROR, "Error sending a frame for encoding\n"); + return ret; + } + ret = avcodec_receive_packet(p->avctx_enc[i], pkt); + if (ret < 0) { + av_log(p->avctx_enc[i], AV_LOG_ERROR, "Error receiving a packet from encoding\n"); + return ret; + } + + ret = avcodec_send_packet(p->avctx_dec[i], pkt); + av_packet_unref(pkt); + if (ret < 0) { + av_log(p->avctx_dec[i], AV_LOG_ERROR, "Error sending a packet for decoding\n"); + return ret; + } + ret = avcodec_receive_frame(p->avctx_dec[i], p->frame_dec[i]); + if (ret < 0) { + av_log(p->avctx_dec[i], AV_LOG_ERROR, "Error receiving a frame from decoding\n"); + return ret; + } + + offset = (BLOCK-x1) + (BLOCK-y1) * p->frame_dec[i]->linesize[0]; + + for (y = 0; y < height; y++) + for (x = 0; x < width; x++) + p->temp[0][x + y * p->temp_stride[0]] += p->frame_dec[i]->data[0][x + y * p->frame_dec[i]->linesize[0] + offset]; + + + if (!p->frame_dec[i]->data[2] || !p->temp[2]) + return 0; + + offset = (BLOCKc-x1c) + (BLOCKc-y1c) * p->frame_dec[i]->linesize[1]; + + for (y = 0; y < AV_CEIL_RSHIFT(height, p->vsub); y++) { + for (x = 0; x < AV_CEIL_RSHIFT(width, p->hsub); x++) { + p->temp[1][x + y * p->temp_stride[1]] += p->frame_dec[i]->data[1][x + y * p->frame_dec[i]->linesize[1] + offset]; + p->temp[2][x + y * p->temp_stride[2]] += p->frame_dec[i]->data[2][x + y * p->frame_dec[i]->linesize[2] + offset]; + } + } + + return 0; +} + +static void filter(AVFilterContext *ctx, uint8_t *dst[3], uint8_t *src[3], int dst_stride[3], int src_stride[3], int width, int height, uint8_t *qp_store, int qp_stride) { + USPPContext *p = ctx->priv; int x, y, i, j; - const int count = 1<log2_count; - int ret; for (i = 0; i < 3; i++) { int is_chroma = !!i; @@ -219,12 +295,11 @@ static void filter(USPPContext *p, uint8_t *dst[3], uint8_t *src[3], memcpy(p->src[i] + (h+block +y) * stride, p->src[i] + (h-y+block-1) * stride, stride); } - p->frame->linesize[i] = stride; memset(p->temp[i], 0, (h + 2 * block) * stride * sizeof(int16_t)); } if (p->qp) - p->frame->quality = p->qp * FF_QP2LAMBDA; + p->quality = p->qp * FF_QP2LAMBDA; else { int qpsum=0; int qpcount = (height>>4) * (height>>4); @@ -233,71 +308,11 @@ static void filter(USPPContext *p, uint8_t *dst[3], uint8_t *src[3], for (x = 0; x < (width>>4); x++) qpsum += qp_store[x + y * qp_stride]; } - p->frame->quality = ff_norm_qscale((qpsum + qpcount/2) / qpcount, p->qscale_type) * FF_QP2LAMBDA; + p->quality = ff_norm_qscale((qpsum + qpcount/2) / qpcount, p->qscale_type) * FF_QP2LAMBDA; } // init per MB qscale stuff FIXME - p->frame->height = height + BLOCK; - p->frame->width = width + BLOCK; - - for (i = 0; i < count; i++) { - const int x1 = offset[i+count-1][0]; - const int y1 = offset[i+count-1][1]; - const int x1c = x1 >> p->hsub; - const int y1c = y1 >> p->vsub; - const int BLOCKc = BLOCK >> p->hsub; - int offset; - AVPacket *pkt = p->pkt; - - av_packet_unref(pkt); - pkt->data = p->outbuf; - pkt->size = p->outbuf_size; - - p->frame->data[0] = p->src[0] + x1 + y1 * p->frame->linesize[0]; - p->frame->data[1] = p->src[1] + x1c + y1c * p->frame->linesize[1]; - p->frame->data[2] = p->src[2] + x1c + y1c * p->frame->linesize[2]; - p->frame->format = p->avctx_enc[i]->pix_fmt; - - ret = avcodec_send_frame(p->avctx_enc[i], p->frame); - if (ret < 0) { - av_log(p->avctx_enc[i], AV_LOG_ERROR, "Error sending a frame for encoding\n"); - continue; - } - ret = avcodec_receive_packet(p->avctx_enc[i], pkt); - if (ret < 0) { - av_log(p->avctx_enc[i], AV_LOG_ERROR, "Error receiving a packet from encoding\n"); - continue; - } - - ret = avcodec_send_packet(p->avctx_dec[i], pkt); - av_packet_unref(pkt); - if (ret < 0) { - av_log(p->avctx_dec[i], AV_LOG_ERROR, "Error sending a packet for decoding\n"); - continue; - } - ret = avcodec_receive_frame(p->avctx_dec[i], p->frame_dec); - if (ret < 0) { - av_log(p->avctx_dec[i], AV_LOG_ERROR, "Error receiving a frame from decoding\n"); - continue; - } - - offset = (BLOCK-x1) + (BLOCK-y1) * p->frame_dec->linesize[0]; - - for (y = 0; y < height; y++) - for (x = 0; x < width; x++) - p->temp[0][x + y * p->temp_stride[0]] += p->frame_dec->data[0][x + y * p->frame_dec->linesize[0] + offset]; - - if (!src[2] || !dst[2]) - continue; - - offset = (BLOCKc-x1c) + (BLOCKc-y1c) * p->frame_dec->linesize[1]; - for (y = 0; y < AV_CEIL_RSHIFT(height, p->vsub); y++) { - for (x = 0; x < AV_CEIL_RSHIFT(width, p->hsub); x++) { - p->temp[1][x + y * p->temp_stride[1]] += p->frame_dec->data[1][x + y * p->frame_dec->linesize[1] + offset]; - p->temp[2][x + y * p->temp_stride[2]] += p->frame_dec->data[2][x + y * p->frame_dec->linesize[2] + offset]; - } - } - } + ff_filter_execute(ctx, filter_1phase, NULL, NULL, p->count); for (j = 0; j < 3; j++) { int is_chroma = !!j; @@ -342,6 +357,7 @@ static int config_input(AVFilterLink *inlink) uspp->hsub = desc->log2_chroma_w; uspp->vsub = desc->log2_chroma_h; + uspp->count = 1<log2_count; for (i = 0; i < 3; i++) { int is_chroma = !!i; @@ -360,7 +376,7 @@ static int config_input(AVFilterLink *inlink) return AVERROR(ENOMEM); } - for (i = 0; i < (1<log2_count); i++) { + for (i = 0; i < uspp->count; i++) { AVCodecContext *avctx_enc, *avctx_dec; AVDictionary *opts = NULL; int ret; @@ -394,15 +410,15 @@ static int config_input(AVFilterLink *inlink) if (ret < 0) return ret; + if (!(uspp->frame[i] = av_frame_alloc())) + return AVERROR(ENOMEM); + if (!(uspp->frame_dec[i] = av_frame_alloc())) + return AVERROR(ENOMEM); + if (!(uspp->pkt[i] = av_packet_alloc())) + return AVERROR(ENOMEM); } uspp->outbuf_size = (width + BLOCK) * (height + BLOCK) * 10; - if (!(uspp->frame = av_frame_alloc())) - return AVERROR(ENOMEM); - if (!(uspp->frame_dec = av_frame_alloc())) - return AVERROR(ENOMEM); - if (!(uspp->pkt = av_packet_alloc())) - return AVERROR(ENOMEM); if (!(uspp->outbuf = av_malloc(uspp->outbuf_size))) return AVERROR(ENOMEM); @@ -464,7 +480,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) out->height = in->height; } - filter(uspp, out->data, in->data, out->linesize, in->linesize, + filter(ctx, out->data, in->data, out->linesize, in->linesize, inlink->w, inlink->h, qp_table, qp_stride); } } @@ -492,15 +508,16 @@ static av_cold void uninit(AVFilterContext *ctx) av_freep(&uspp->src[i]); } - for (i = 0; i < (1 << uspp->log2_count); i++) { + for (i = 0; i < uspp->count; i++) { avcodec_free_context(&uspp->avctx_enc[i]); avcodec_free_context(&uspp->avctx_dec[i]); + av_frame_free(&uspp->frame[i]); + av_frame_free(&uspp->frame_dec[i]); + av_packet_free(&uspp->pkt[i]); } av_freep(&uspp->non_b_qp_table); av_freep(&uspp->outbuf); - av_packet_free(&uspp->pkt); - av_frame_free(&uspp->frame); } static const AVFilterPad uspp_inputs[] = { @@ -528,5 +545,5 @@ const AVFilter ff_vf_uspp = { FILTER_OUTPUTS(uspp_outputs), FILTER_PIXFMTS_ARRAY(pix_fmts), .priv_class = &uspp_class, - .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, };