1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

avfilter/vf_avgblur: switch to faster algorithm

This commit is contained in:
Paul B Mahol 2021-09-24 16:28:51 +02:00
parent a673761ce8
commit a7fc78c1a6
2 changed files with 210 additions and 179 deletions

View File

@ -20,6 +20,7 @@
* SOFTWARE. * SOFTWARE.
*/ */
#include "libavutil/avassert.h"
#include "libavutil/imgutils.h" #include "libavutil/imgutils.h"
#include "libavutil/opt.h" #include "libavutil/opt.h"
#include "libavutil/pixdesc.h" #include "libavutil/pixdesc.h"
@ -36,13 +37,15 @@ typedef struct AverageBlurContext {
int planes; int planes;
int depth; int depth;
int max;
int area;
int planewidth[4]; int planewidth[4];
int planeheight[4]; int planeheight[4];
float *buffer; void *buffer;
uint16_t lut[256 * 256 * 256];
int nb_planes; int nb_planes;
int (*filter_horizontally)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); int (*filter[2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
int (*filter_vertically)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
} AverageBlurContext; } AverageBlurContext;
#define OFFSET(x) offsetof(AverageBlurContext, x) #define OFFSET(x) offsetof(AverageBlurContext, x)
@ -60,124 +63,138 @@ AVFILTER_DEFINE_CLASS(avgblur);
typedef struct ThreadData { typedef struct ThreadData {
int height; int height;
int width; int width;
uint8_t *ptr; const void *ptr;
int linesize; void *dptr;
int linesize, dlinesize;
} ThreadData; } ThreadData;
#define HORIZONTAL_FILTER(name, type) \ #define LUT_DIV(sum, area) (lut[(sum)])
static int filter_horizontally_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)\ #define SLOW_DIV(sum, area) ((sum) / (area))
{ \
AverageBlurContext *s = ctx->priv; \ #define FILTER(name, type, btype, lutunused, areaunused, lutdiv) \
ThreadData *td = arg; \ static int filter_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
const int height = td->height; \ { \
const int width = td->width; \ AverageBlurContext *s = ctx->priv; \
const int slice_start = (height * jobnr ) / nb_jobs; \ ThreadData *td = arg; \
const int slice_end = (height * (jobnr+1)) / nb_jobs; \ areaunused const int area = s->area; \
const int radius = FFMIN(s->radius, width / 2); \ lutunused const uint16_t *lut = s->lut; \
const int linesize = td->linesize / sizeof(type); \ const int size_w = s->radius; \
float *buffer = s->buffer; \ const int size_h = s->radiusV; \
const type *src; \ btype *col_sum = (btype *)s->buffer + size_w; \
float *ptr; \ const int dlinesize = td->dlinesize / sizeof(type); \
int y, x; \ const int linesize = td->linesize / sizeof(type); \
\ const int height = td->height; \
/* Filter horizontally along each row */ \ const int width = td->width; \
for (y = slice_start; y < slice_end; y++) { \ const type *src = td->ptr; \
float acc = 0; \ type *dst = td->dptr; \
int count = 0; \ btype sum = 0; \
\ \
src = (const type *)td->ptr + linesize * y; \ for (int x = -size_w; x < 0; x++) { \
ptr = buffer + width * y; \ sum = src[0] * size_h; \
\ for (int y = 0; y <= size_h; y++) \
for (x = 0; x < radius; x++) { \ sum += src[y * linesize]; \
acc += src[x]; \ av_assert2(sum >= 0); \
} \ col_sum[x] = sum; \
count += radius; \ } \
\ \
for (x = 0; x <= radius; x++) { \ for (int x = 0; x < width; x++) { \
acc += src[x + radius]; \ sum = src[x] * size_h; \
count++; \ for (int y = 0; y <= size_h; y++) \
ptr[x] = acc / count; \ sum += src[x + y * linesize]; \
} \ av_assert2(sum >= 0); \
\ col_sum[x] = sum; \
for (; x < width - radius; x++) { \ } \
acc += src[x + radius] - src[x - radius - 1]; \ \
ptr[x] = acc / count; \ for (int x = width; x < width + size_w; x++) { \
} \ sum = src[width - 1] * size_h; \
\ for (int y = 0; y <= size_h; y++) \
for (; x < width; x++) { \ sum += src[width - 1 + y * linesize]; \
acc -= src[x - radius]; \ av_assert2(sum >= 0); \
count--; \ col_sum[x] = sum; \
ptr[x] = acc / count; \ } \
} \ \
} \ sum = 0; \
\ for (int x = -size_w; x <= size_w; x++) \
return 0; \ sum += col_sum[x]; \
av_assert2(sum >= 0); \
dst[0] = lutdiv(sum, area); \
\
for (int x = 1; x < width; x++) { \
sum = sum - col_sum[x - size_w - 1] + col_sum[x + size_w]; \
av_assert2(sum >= 0); \
dst[x] = lutdiv(sum, area); \
} \
\
src = td->ptr; \
src += linesize; \
dst += dlinesize; \
\
for (int y = 1; y < height; y++) { \
const int syp = FFMIN(size_h, height - y - 1) * linesize; \
const int syn = FFMIN(y, size_h + 1) * linesize; \
\
sum = 0; \
\
for (int x = -size_w; x < 0; x++) \
col_sum[x] += src[0 + syp] - src[0 - syn]; \
\
for (int x = 0; x < width; x++) \
col_sum[x] += src[x + syp] - src[x - syn]; \
\
for (int x = width; x < width + size_w; x++) \
col_sum[x] += src[width - 1 + syp] - src[width - 1 - syn]; \
\
for (int x = -size_w; x <= size_w; x++) \
sum += col_sum[x]; \
av_assert2(sum >= 0); \
dst[0] = lutdiv(sum, area); \
\
for (int x = 1; x < width; x++) { \
sum = sum - col_sum[x - size_w - 1] + col_sum[x + size_w]; \
av_assert2(sum >= 0); \
dst[x] = lutdiv(sum, area); \
} \
\
src += linesize; \
dst += dlinesize; \
} \
\
return 0; \
} }
HORIZONTAL_FILTER(8, uint8_t) FILTER(lut8, uint8_t, int32_t, , av_unused, LUT_DIV)
HORIZONTAL_FILTER(16, uint16_t) FILTER(lut16, uint16_t, int64_t, , av_unused, LUT_DIV)
#define VERTICAL_FILTER(name, type) \ FILTER(slow8, uint8_t, int32_t, av_unused, , SLOW_DIV)
static int filter_vertically_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \ FILTER(slow16, uint16_t, int64_t, av_unused, , SLOW_DIV)
{ \
AverageBlurContext *s = ctx->priv; \ static void build_lut(AVFilterContext *ctx, int max)
ThreadData *td = arg; \ {
const int height = td->height; \ AverageBlurContext *s = ctx->priv;
const int width = td->width; \ const int area = (2 * s->radiusV + 1) * (2 * s->radius + 1);
const int slice_start = (width * jobnr ) / nb_jobs; \
const int slice_end = (width * (jobnr+1)) / nb_jobs; \ s->area = area;
const int radius = FFMIN(s->radiusV, height / 2); \ if (max * area >= FF_ARRAY_ELEMS(s->lut))
const int linesize = td->linesize / sizeof(type); \ return;
type *buffer = (type *)td->ptr; \
const float *src; \ for (int i = 0, j = 0, k = 0; i < max * area; i++, j++) {
type *ptr; \ if (j == area) {
int i, x; \ k++;
\ j = 0;
/* Filter vertically along each column */ \ }
for (x = slice_start; x < slice_end; x++) { \
float acc = 0; \ s->lut[i] = k;
int count = 0; \ }
\
src = s->buffer + x; \
\
for (i = 0; i < radius; i++) { \
acc += src[0]; \
src += width; \
} \
count += radius; \
\
src = s->buffer + x; \
ptr = buffer + x; \
for (i = 0; i + radius < height && i <= radius; i++) { \
acc += src[(i + radius) * width]; \
count++; \
ptr[i * linesize] = acc / count; \
} \
\
for (; i < height - radius; i++) { \
acc += src[(i + radius) * width] - src[(i - radius - 1) * width]; \
ptr[i * linesize] = acc / count; \
} \
\
for (; i < height; i++) { \
acc -= src[(i - radius) * width]; \
count--; \
ptr[i * linesize] = acc / count; \
} \
} \
\
return 0; \
} }
VERTICAL_FILTER(8, uint8_t)
VERTICAL_FILTER(16, uint16_t)
static int config_input(AVFilterLink *inlink) static int config_input(AVFilterLink *inlink)
{ {
AVFilterContext *ctx = inlink->dst;
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
AverageBlurContext *s = inlink->dst->priv; AverageBlurContext *s = ctx->priv;
s->depth = desc->comp[0].depth; s->depth = desc->comp[0].depth;
s->max = 1 << s->depth;
s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
s->planewidth[0] = s->planewidth[3] = inlink->w; s->planewidth[0] = s->planewidth[3] = inlink->w;
s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
@ -185,21 +202,20 @@ static int config_input(AVFilterLink *inlink)
s->nb_planes = av_pix_fmt_count_planes(inlink->format); s->nb_planes = av_pix_fmt_count_planes(inlink->format);
s->buffer = av_malloc_array(inlink->w, inlink->h * sizeof(*s->buffer)); s->buffer = av_calloc(inlink->w + (1024 * 2 + 1), 4 * ((s->depth + 7) / 8));
if (!s->buffer) if (!s->buffer)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
if (s->radiusV <= 0) { if (s->radiusV <= 0)
s->radiusV = s->radius; s->radiusV = s->radius;
}
if (s->depth == 8) { s->filter[0] = s->depth <= 8 ? filter_lut8 : filter_lut16;
s->filter_horizontally = filter_horizontally_8; s->filter[1] = s->depth <= 8 ? filter_slow8 : filter_slow16;
s->filter_vertically = filter_vertically_8;
} else { s->radius = FFMIN(s->planewidth[1] / 2, s->radius);
s->filter_horizontally = filter_horizontally_16; s->radiusV = FFMIN(s->planeheight[1] / 2, s->radiusV);
s->filter_vertically = filter_vertically_16;
} build_lut(ctx, s->max);
return 0; return 0;
} }
@ -209,19 +225,16 @@ static void averageiir2d(AVFilterContext *ctx, AVFrame *in, AVFrame *out, int pl
AverageBlurContext *s = ctx->priv; AverageBlurContext *s = ctx->priv;
const int width = s->planewidth[plane]; const int width = s->planewidth[plane];
const int height = s->planeheight[plane]; const int height = s->planeheight[plane];
const int nb_threads = ff_filter_get_nb_threads(ctx); const int slow = (s->max * s->area) >= FF_ARRAY_ELEMS(s->lut);
ThreadData td; ThreadData td;
td.width = width; td.width = width;
td.height = height; td.height = height;
td.ptr = in->data[plane]; td.ptr = in->data[plane];
td.linesize = in->linesize[plane]; td.linesize = in->linesize[plane];
ff_filter_execute(ctx, s->filter_horizontally, &td, td.dptr = out->data[plane];
NULL, FFMIN(height, nb_threads)); td.dlinesize = out->linesize[plane];
td.ptr = out->data[plane]; s->filter[slow](ctx, &td, 0, 0);
td.linesize = out->linesize[plane];
ff_filter_execute(ctx, s->filter_vertically, &td,
NULL, FFMIN(width, nb_threads));
} }
static int query_formats(AVFilterContext *ctx) static int query_formats(AVFilterContext *ctx)
@ -259,16 +272,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
AVFrame *out; AVFrame *out;
int plane; int plane;
if (av_frame_is_writable(in)) { out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
out = in; if (!out) {
} else { av_frame_free(&in);
out = ff_get_video_buffer(outlink, outlink->w, outlink->h); return AVERROR(ENOMEM);
if (!out) {
av_frame_free(&in);
return AVERROR(ENOMEM);
}
av_frame_copy_props(out, in);
} }
av_frame_copy_props(out, in);
for (plane = 0; plane < s->nb_planes; plane++) { for (plane = 0; plane < s->nb_planes; plane++) {
const int height = s->planeheight[plane]; const int height = s->planeheight[plane];
@ -285,11 +294,33 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
averageiir2d(ctx, in, out, plane); averageiir2d(ctx, in, out, plane);
} }
if (out != in) av_frame_free(&in);
av_frame_free(&in);
return ff_filter_frame(outlink, out); return ff_filter_frame(outlink, out);
} }
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
char *res, int res_len, int flags)
{
AverageBlurContext *s = ctx->priv;
const int area = s->area;
int ret;
ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
if (ret < 0)
return ret;
if (s->radiusV <= 0)
s->radiusV = s->radius;
s->radius = FFMIN(s->planewidth[1] / 2, s->radius);
s->radiusV = FFMIN(s->planeheight[1] / 2, s->radiusV);
if (area != (2 * s->radiusV + 1) * (2 * s->radius + 1))
build_lut(ctx, s->max);
return 0;
}
static av_cold void uninit(AVFilterContext *ctx) static av_cold void uninit(AVFilterContext *ctx)
{ {
AverageBlurContext *s = ctx->priv; AverageBlurContext *s = ctx->priv;
@ -322,6 +353,6 @@ const AVFilter ff_vf_avgblur = {
.query_formats = query_formats, .query_formats = query_formats,
FILTER_INPUTS(avgblur_inputs), FILTER_INPUTS(avgblur_inputs),
FILTER_OUTPUTS(avgblur_outputs), FILTER_OUTPUTS(avgblur_outputs),
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
.process_command = ff_filter_process_command, .process_command = process_command,
}; };

View File

@ -1,45 +1,45 @@
frame:0 pts:0 pts_time:0 frame:0 pts:0 pts_time:0
lavfi.psnr.mse.y=222.06 lavfi.psnr.mse.y=218.435333
lavfi.psnr.psnr.y=24.67 lavfi.psnr.psnr.y=24.737576
lavfi.psnr.mse.u=339.38 lavfi.psnr.mse.u=336.693390
lavfi.psnr.psnr.u=22.82 lavfi.psnr.psnr.u=22.858458
lavfi.psnr.mse.v=705.41 lavfi.psnr.mse.v=698.968384
lavfi.psnr.psnr.v=19.65 lavfi.psnr.psnr.v=19.686228
lavfi.psnr.mse_avg=372.23 lavfi.psnr.mse_avg=368.133118
lavfi.psnr.psnr_avg=22.42 lavfi.psnr.psnr_avg=22.470755
frame:1 pts:1 pts_time:1 frame:1 pts:1 pts_time:1
lavfi.psnr.mse.y=236.74 lavfi.psnr.mse.y=232.656189
lavfi.psnr.psnr.y=24.39 lavfi.psnr.psnr.y=24.463657
lavfi.psnr.mse.u=416.17 lavfi.psnr.mse.u=413.841064
lavfi.psnr.psnr.u=21.94 lavfi.psnr.psnr.u=21.962467
lavfi.psnr.mse.v=704.98 lavfi.psnr.mse.v=693.103577
lavfi.psnr.psnr.v=19.65 lavfi.psnr.psnr.v=19.722822
lavfi.psnr.mse_avg=398.66 lavfi.psnr.mse_avg=393.064240
lavfi.psnr.psnr_avg=22.12 lavfi.psnr.psnr_avg=22.186169
frame:2 pts:2 pts_time:2 frame:2 pts:2 pts_time:2
lavfi.psnr.mse.y=234.79 lavfi.psnr.mse.y=230.470032
lavfi.psnr.psnr.y=24.42 lavfi.psnr.psnr.y=24.504660
lavfi.psnr.mse.u=435.72 lavfi.psnr.mse.u=433.524109
lavfi.psnr.psnr.u=21.74 lavfi.psnr.psnr.u=21.760672
lavfi.psnr.mse.v=699.60 lavfi.psnr.mse.v=693.391174
lavfi.psnr.psnr.v=19.68 lavfi.psnr.psnr.v=19.721020
lavfi.psnr.mse_avg=401.23 lavfi.psnr.mse_avg=396.963837
lavfi.psnr.psnr_avg=22.10 lavfi.psnr.psnr_avg=22.143293
frame:3 pts:3 pts_time:3 frame:3 pts:3 pts_time:3
lavfi.psnr.mse.y=250.88 lavfi.psnr.mse.y=247.346817
lavfi.psnr.psnr.y=24.14 lavfi.psnr.psnr.y=24.197741
lavfi.psnr.mse.u=479.73 lavfi.psnr.mse.u=476.365723
lavfi.psnr.psnr.u=21.32 lavfi.psnr.psnr.u=21.351398
lavfi.psnr.mse.v=707.55 lavfi.psnr.mse.v=700.987549
lavfi.psnr.psnr.v=19.63 lavfi.psnr.psnr.v=19.673700
lavfi.psnr.mse_avg=422.26 lavfi.psnr.mse_avg=418.011719
lavfi.psnr.psnr_avg=21.88 lavfi.psnr.psnr_avg=21.918919
frame:4 pts:4 pts_time:4 frame:4 pts:4 pts_time:4
lavfi.psnr.mse.y=241.05 lavfi.psnr.mse.y=237.129654
lavfi.psnr.psnr.y=24.31 lavfi.psnr.psnr.y=24.380945
lavfi.psnr.mse.u=505.04 lavfi.psnr.mse.u=503.722931
lavfi.psnr.psnr.u=21.10 lavfi.psnr.psnr.u=21.108887
lavfi.psnr.mse.v=716.00 lavfi.psnr.mse.v=708.932678
lavfi.psnr.psnr.v=19.58 lavfi.psnr.psnr.v=19.624754
lavfi.psnr.mse_avg=425.79 lavfi.psnr.mse_avg=421.728729
lavfi.psnr.psnr_avg=21.84 lavfi.psnr.psnr_avg=21.880472