[FFmpeg-cvslog] avfilter/vf_psnr: add support for slice threading

Paul B Mahol git at videolan.org
Fri Feb 19 22:25:22 EET 2021


ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Sat Feb 13 00:53:16 2021 +0100| [680ba23bc278517b672de9c2975f7a860c477dc3] | committer: Paul B Mahol

avfilter/vf_psnr: add support for slice threading

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=680ba23bc278517b672de9c2975f7a860c477dc3
---

 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,
 };



More information about the ffmpeg-cvslog mailing list