From patchwork Tue Feb 16 20:54:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 25666 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 31C0A44A0A6 for ; Tue, 16 Feb 2021 22:54:44 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0927F687FD4; Tue, 16 Feb 2021 22:54:44 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CE348687FD4 for ; Tue, 16 Feb 2021 22:54:36 +0200 (EET) Received: by mail-ej1-f48.google.com with SMTP id w1so9644476ejk.6 for ; Tue, 16 Feb 2021 12:54:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=1yLvD/SM3xzK/byd263Ey3HW/htmyoaD5Hko94WY1Tc=; b=ha+g254V0EQ/xG2GqaE8wNVfxMIRjTGGtWNiucOWYmkdfWzbjvK73aoMixGBOCopet 4WMeyNy4UTAu6yBnwsxj6rlS7yyESEQHOdFf7fs2J5exg5niCMH9YAB4X2wCd6dIQmJr XCQsGttgpzX184NYr8K6LqJENFxboPoJBilyAWhKdSKPCAHlMK+yrheYO/xT3f38vjyC XiQbbpHUGTP3ZIWoFxK3A7IYv6h4TLsc1TKHv/64+QRP8A9AAEPvLCQJUtwcQlVflkNq +Mw4vllg10Fet04xnPkDqI6QUjaB4MKpazLo4nw4cV8QJxggTsvl0LPndZFdGtW/E6Y8 7HZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=1yLvD/SM3xzK/byd263Ey3HW/htmyoaD5Hko94WY1Tc=; b=ccd3AocEzLfMZawheipruIkpru2xl0PMl2++b4vv3bHyIIqjV0qOZbWvYuiDRj2nKV 6QeqUFw/rZKIFynw2bX1MS1eOH2vb8ljuvoY8FfdSDMg348TRdUuk9LNzbnpXVT80VYx CAhUyewMo9wz/HLYeWLfhi/p7bL7+HhJJ0dYJcRIndc3ldP0rD+iE71uMR4cuXwFBTq9 g2M3yxBxm1+ZcJ4a7BcMm3gAkujh+e+vEzBOoRpZ5k1eAj95AtsM+j4gHVs+OUJX4fRW 5GAwntRf2T7qe5p+Dyn209T2Kj7GIKZFFwHLKpk42EvHUUJLsasexV7TpTJAD41XnBZE odJA== X-Gm-Message-State: AOAM5338fVaLGCYnhBZyaEi+vctcVKYL7yFpwRbxn6Z5w+x4po/WgRax quFGbYlQkhVur31fpxtb1FrQjEuF4V4FAA== X-Google-Smtp-Source: ABdhPJx7na7p1PRSWQdGrh4iS+s3qalIVM/0CYcME4s6aW6cge66pfgjEKjYRObOJUmNQFQQGNnuiA== X-Received: by 2002:a17:906:27d2:: with SMTP id k18mr16190359ejc.74.1613508876351; Tue, 16 Feb 2021 12:54:36 -0800 (PST) Received: from localhost.localdomain ([94.250.162.225]) by smtp.gmail.com with ESMTPSA id bm2sm14471797ejb.87.2021.02.16.12.54.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Feb 2021 12:54:35 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Tue, 16 Feb 2021 21:54:26 +0100 Message-Id: <20210216205426.24744-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter/vf_psnr: add support for slice threading X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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" Signed-off-by: Paul B Mahol --- libavfilter/vf_psnr.c | 126 +++++++++++++++++++++++++++++------------- 1 file changed, 89 insertions(+), 37 deletions(-) diff --git a/libavfilter/vf_psnr.c b/libavfilter/vf_psnr.c index 0d6bbad42f..0d0c0f35aa 100644 --- a/libavfilter/vf_psnr.c +++ b/libavfilter/vf_psnr.c @@ -51,9 +51,11 @@ typedef struct PSNRContext { uint8_t rgba_map[4]; char comps[4]; int nb_components; + int nb_threads; int planewidth[4]; int planeheight[4]; double planeweight[4]; + uint64_t **score; PSNRDSPContext dsp; } PSNRContext; @@ -104,29 +106,44 @@ static uint64_t sse_line_16bit(const uint8_t *_main_line, const uint8_t *_ref_li return m2; } -static inline -void compute_images_mse(PSNRContext *s, - const uint8_t *main_data[4], const int main_linesizes[4], - const uint8_t *ref_data[4], const int ref_linesizes[4], - int w, int h, double mse[4]) +typedef struct ThreadData { + const uint8_t *main_data[4]; + const uint8_t *ref_data[4]; + int main_linesize[4]; + int ref_linesize[4]; + int planewidth[4]; + int planeheight[4]; + uint64_t **score; + int nb_components; + PSNRDSPContext *dsp; +} ThreadData; + +static +int compute_images_mse(AVFilterContext *ctx, void *arg, + int jobnr, int nb_jobs) { - int i, c; - - for (c = 0; c < s->nb_components; c++) { - const int outw = s->planewidth[c]; - const int outh = s->planeheight[c]; - const uint8_t *main_line = main_data[c]; - const uint8_t *ref_line = ref_data[c]; - const int ref_linesize = ref_linesizes[c]; - const int main_linesize = main_linesizes[c]; + ThreadData *td = arg; + uint64_t *score = td->score[jobnr]; + + for (int c = 0; c < td->nb_components; c++) { + const int outw = td->planewidth[c]; + const int outh = td->planeheight[c]; + const int slice_start = (outh * jobnr) / nb_jobs; + const int slice_end = (outh * (jobnr+1)) / nb_jobs; + const int ref_linesize = td->ref_linesize[c]; + const int main_linesize = td->main_linesize[c]; + const uint8_t *main_line = td->main_data[c] + main_linesize * slice_start; + const uint8_t *ref_line = td->ref_data[c] + ref_linesize * slice_start; uint64_t m = 0; - for (i = 0; i < outh; i++) { - m += s->dsp.sse_line(main_line, ref_line, outw); + for (int i = slice_start; i < slice_end; i++) { + m += td->dsp->sse_line(main_line, ref_line, outw); ref_line += ref_linesize; main_line += main_linesize; } - mse[c] = m / (double)(outw * outh); + score[c] = m; } + + return 0; } static void set_meta(AVDictionary **metadata, const char *key, char comp, float d) @@ -147,9 +164,11 @@ static int do_psnr(FFFrameSync *fs) AVFilterContext *ctx = fs->parent; PSNRContext *s = ctx->priv; AVFrame *master, *ref; - double comp_mse[4], mse = 0; - int ret, j, c; + double comp_mse[4], mse = 0.; + uint64_t comp_sum[4] = { 0 }; AVDictionary **metadata; + ThreadData td; + int ret; ret = ff_framesync_dualinput_get(fs, &master, &ref); if (ret < 0) @@ -158,23 +177,42 @@ static int do_psnr(FFFrameSync *fs) return ff_filter_frame(ctx->outputs[0], master); metadata = &master->metadata; - compute_images_mse(s, (const uint8_t **)master->data, master->linesize, - (const uint8_t **)ref->data, ref->linesize, - master->width, master->height, comp_mse); + td.nb_components = s->nb_components; + td.dsp = &s->dsp; + td.score = s->score; + for (int c = 0; c < s->nb_components; c++) { + td.main_data[c] = master->data[c]; + td.ref_data[c] = ref->data[c]; + td.main_linesize[c] = master->linesize[c]; + td.ref_linesize[c] = ref->linesize[c]; + td.planewidth[c] = s->planewidth[c]; + td.planeheight[c] = s->planeheight[c]; + } - for (j = 0; j < s->nb_components; j++) - mse += comp_mse[j] * s->planeweight[j]; + ctx->internal->execute(ctx, compute_images_mse, &td, NULL, FFMIN(s->planeheight[1], s->nb_threads)); + + for (int j = 0; j < s->nb_threads; j++) { + for (int c = 0; c < s->nb_components; c++) + comp_sum[c] += s->score[j][c]; + } + + for (int c = 0; c < s->nb_components; c++) + comp_mse[c] = comp_sum[c] / ((double)s->planewidth[c] * s->planeheight[c]); + + for (int c = 0; c < s->nb_components; c++) + mse += comp_mse[c] * s->planeweight[c]; s->min_mse = FFMIN(s->min_mse, mse); s->max_mse = FFMAX(s->max_mse, mse); s->mse += mse; - for (j = 0; j < s->nb_components; j++) + + for (int j = 0; j < s->nb_components; j++) s->mse_comp[j] += comp_mse[j]; s->nb_frames++; - for (j = 0; j < s->nb_components; j++) { - c = s->is_rgb ? s->rgba_map[j] : j; + for (int j = 0; j < s->nb_components; j++) { + int c = s->is_rgb ? s->rgba_map[j] : j; set_meta(metadata, "lavfi.psnr.mse.", s->comps[j], comp_mse[c]); set_meta(metadata, "lavfi.psnr.psnr.", s->comps[j], get_psnr(comp_mse[c], 1, s->max[c])); } @@ -185,16 +223,16 @@ static int do_psnr(FFFrameSync *fs) if (s->stats_version == 2 && !s->stats_header_written) { fprintf(s->stats_file, "psnr_log_version:2 fields:n"); fprintf(s->stats_file, ",mse_avg"); - for (j = 0; j < s->nb_components; j++) { + for (int j = 0; j < s->nb_components; j++) { fprintf(s->stats_file, ",mse_%c", s->comps[j]); } fprintf(s->stats_file, ",psnr_avg"); - for (j = 0; j < s->nb_components; j++) { + for (int j = 0; j < s->nb_components; j++) { fprintf(s->stats_file, ",psnr_%c", s->comps[j]); } if (s->stats_add_max) { fprintf(s->stats_file, ",max_avg"); - for (j = 0; j < s->nb_components; j++) { + for (int j = 0; j < s->nb_components; j++) { fprintf(s->stats_file, ",max_%c", s->comps[j]); } } @@ -202,20 +240,20 @@ static int do_psnr(FFFrameSync *fs) s->stats_header_written = 1; } fprintf(s->stats_file, "n:%"PRId64" mse_avg:%0.2f ", s->nb_frames, mse); - for (j = 0; j < s->nb_components; j++) { - c = s->is_rgb ? s->rgba_map[j] : j; + for (int j = 0; j < s->nb_components; j++) { + int c = s->is_rgb ? s->rgba_map[j] : j; fprintf(s->stats_file, "mse_%c:%0.2f ", s->comps[j], comp_mse[c]); } fprintf(s->stats_file, "psnr_avg:%0.2f ", get_psnr(mse, 1, s->average_max)); - for (j = 0; j < s->nb_components; j++) { - c = s->is_rgb ? s->rgba_map[j] : j; + for (int j = 0; j < s->nb_components; j++) { + int c = s->is_rgb ? s->rgba_map[j] : j; fprintf(s->stats_file, "psnr_%c:%0.2f ", s->comps[j], get_psnr(comp_mse[c], 1, s->max[c])); } if (s->stats_version == 2 && s->stats_add_max) { fprintf(s->stats_file, "max_avg:%d ", s->average_max); - for (j = 0; j < s->nb_components; j++) { - c = s->is_rgb ? s->rgba_map[j] : j; + for (int j = 0; j < s->nb_components; j++) { + int c = s->is_rgb ? s->rgba_map[j] : j; fprintf(s->stats_file, "max_%c:%d ", s->comps[j], s->max[c]); } } @@ -289,6 +327,7 @@ static int config_input_ref(AVFilterLink *inlink) unsigned sum; int j; + s->nb_threads = ff_filter_get_nb_threads(ctx); s->nb_components = desc->nb_components; if (ctx->inputs[0]->w != ctx->inputs[1]->w || ctx->inputs[0]->h != ctx->inputs[1]->h) { @@ -329,6 +368,16 @@ static int config_input_ref(AVFilterLink *inlink) if (ARCH_X86) ff_psnr_init_x86(&s->dsp, desc->comp[0].depth); + s->score = av_calloc(s->nb_threads, sizeof(*s->score)); + if (!s->score) + return AVERROR(ENOMEM); + + for (int t = 0; t < s->nb_threads && s->score; t++) { + s->score[t] = av_calloc(s->nb_components, sizeof(*s->score[0])); + if (!s->score[t]) + return AVERROR(ENOMEM); + } + return 0; } @@ -389,6 +438,9 @@ static av_cold void uninit(AVFilterContext *ctx) } ff_framesync_uninit(&s->fs); + for (int t = 0; t < s->nb_threads && s->score; t++) + av_freep(&s->score[t]); + av_freep(&s->score); if (s->stats_file && s->stats_file != stdout) fclose(s->stats_file); @@ -427,5 +479,5 @@ AVFilter ff_vf_psnr = { .priv_class = &psnr_class, .inputs = psnr_inputs, .outputs = psnr_outputs, - .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, };