X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Fvf_psnr.c;h=e5459d3f347aa807aecd92e91e1dde813c9a9dac;hb=8410000f178134a7123b869b799f1a907c0028bf;hp=0675a17c5dba8078cfdcb3d65b4c4ce0b23d711b;hpb=4f9a8d3fe2f9485ee08848d336ee96f15ec0e7e6;p=ffmpeg diff --git a/libavfilter/vf_psnr.c b/libavfilter/vf_psnr.c index 0675a17c5db..e5459d3f347 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,35 +106,50 @@ 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) { char value[128]; - snprintf(value, sizeof(value), "%0.2f", d); + snprintf(value, sizeof(value), "%f", d); if (comp) { char key2[128]; snprintf(key2, sizeof(key2), "%s%c", key, comp); @@ -147,34 +164,55 @@ 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) return ret; - if (!ref) + if (ctx->is_disabled || !ref) 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; t++) { + s->score[t] = av_calloc(s->nb_components, sizeof(*s->score[0])); + if (!s->score[t]) + return AVERROR(ENOMEM); + } + return 0; } @@ -350,6 +399,14 @@ static int config_output(AVFilterLink *outlink) if ((ret = ff_framesync_configure(&s->fs)) < 0) return ret; + outlink->time_base = s->fs.time_base; + + if (av_cmp_q(mainlink->time_base, outlink->time_base) || + av_cmp_q(ctx->inputs[1]->time_base, outlink->time_base)) + av_log(ctx, AV_LOG_WARNING, "not matching timebases found between first input: %d/%d and second input %d/%d, results may be incorrect!\n", + mainlink->time_base.num, mainlink->time_base.den, + ctx->inputs[1]->time_base.num, ctx->inputs[1]->time_base.den); + return 0; } @@ -381,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); @@ -407,7 +467,7 @@ static const AVFilterPad psnr_outputs[] = { { NULL } }; -AVFilter ff_vf_psnr = { +const AVFilter ff_vf_psnr = { .name = "psnr", .description = NULL_IF_CONFIG_SMALL("Calculate the PSNR between two video streams."), .preinit = psnr_framesync_preinit, @@ -419,4 +479,5 @@ AVFilter ff_vf_psnr = { .priv_class = &psnr_class, .inputs = psnr_inputs, .outputs = psnr_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, };