]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_gblur.c
avformat/hlsenc: replace with av_freep for all av_free
[ffmpeg] / libavfilter / vf_gblur.c
index f77a3fffc383a456e7202f1b5ccec66b931145a7..6ef261478fae455cfaec5fcc3bfd566dc3ba6ceb 100644 (file)
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
 #include "formats.h"
+#include "gblur.h"
 #include "internal.h"
 #include "video.h"
 
-typedef struct GBlurContext {
-    const AVClass *class;
-
-    float sigma;
-    float sigmaV;
-    int steps;
-    int planes;
-
-    int depth;
-    int planewidth[4];
-    int planeheight[4];
-    float *buffer;
-    float boundaryscale;
-    float boundaryscaleV;
-    float postscale;
-    float postscaleV;
-    float nu;
-    float nuV;
-    int nb_planes;
-} GBlurContext;
-
 #define OFFSET(x) offsetof(GBlurContext, x)
 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
 
@@ -72,6 +52,28 @@ typedef struct ThreadData {
     int width;
 } ThreadData;
 
+static void horiz_slice_c(float *buffer, int width, int height, int steps,
+                          float nu, float bscale)
+{
+    int step, x, y;
+    float *ptr;
+    for (y = 0; y < height; y++) {
+        for (step = 0; step < steps; step++) {
+            ptr = buffer + width * y;
+            ptr[0] *= bscale;
+
+            /* Filter rightwards */
+            for (x = 1; x < width; x++)
+                ptr[x] += nu * ptr[x - 1];
+            ptr[x = width - 1] *= bscale;
+
+            /* Filter leftwards */
+            for (; x > 0; x--)
+                ptr[x - 1] += nu * ptr[x];
+        }
+    }
+}
+
 static int filter_horizontally(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
     GBlurContext *s = ctx->priv;
@@ -84,28 +86,45 @@ static int filter_horizontally(AVFilterContext *ctx, void *arg, int jobnr, int n
     const int steps = s->steps;
     const float nu = s->nu;
     float *buffer = s->buffer;
-    int y, x, step;
-    float *ptr;
 
-    /* Filter horizontally along each row */
-    for (y = slice_start; y < slice_end; y++) {
-        for (step = 0; step < steps; step++) {
-            ptr = buffer + width * y;
-            ptr[0] *= boundaryscale;
+    s->horiz_slice(buffer + width * slice_start, width, slice_end - slice_start,
+                   steps, nu, boundaryscale);
+    emms_c();
+    return 0;
+}
 
-            /* Filter rightwards */
-            for (x = 1; x < width; x++)
-                ptr[x] += nu * ptr[x - 1];
+static void do_vertical_columns(float *buffer, int width, int height,
+                                int column_begin, int column_end, int steps,
+                                float nu, float boundaryscale, int column_step)
+{
+    const int numpixels = width * height;
+    int i, x, k, step;
+    float *ptr;
+    for (x = column_begin; x < column_end;) {
+        for (step = 0; step < steps; step++) {
+            ptr = buffer + x;
+            for (k = 0; k < column_step; k++) {
+                ptr[k] *= boundaryscale;
+            }
+            /* Filter downwards */
+            for (i = width; i < numpixels; i += width) {
+                for (k = 0; k < column_step; k++) {
+                    ptr[i + k] += nu * ptr[i - width + k];
+                }
+            }
+            i = numpixels - width;
 
-            ptr[x = width - 1] *= boundaryscale;
+            for (k = 0; k < column_step; k++)
+                ptr[i + k] *= boundaryscale;
 
-            /* Filter leftwards */
-            for (; x > 0; x--)
-                ptr[x - 1] += nu * ptr[x];
+            /* Filter upwards */
+            for (; i > 0; i -= width) {
+                for (k = 0; k < column_step; k++)
+                    ptr[i - width + k] += nu * ptr[i + k];
+            }
         }
+        x += column_step;
     }
-
-    return 0;
 }
 
 static int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
@@ -117,31 +136,19 @@ static int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_
     const int slice_start = (width *  jobnr   ) / nb_jobs;
     const int slice_end   = (width * (jobnr+1)) / nb_jobs;
     const float boundaryscale = s->boundaryscaleV;
-    const int numpixels = width * height;
     const int steps = s->steps;
     const float nu = s->nuV;
     float *buffer = s->buffer;
-    int i, x, step;
-    float *ptr;
-
-    /* Filter vertically along each column */
-    for (x = slice_start; x < slice_end; x++) {
-        for (step = 0; step < steps; step++) {
-            ptr = buffer + x;
-            ptr[0] *= boundaryscale;
-
-            /* Filter downwards */
-            for (i = width; i < numpixels; i += width)
-                ptr[i] += nu * ptr[i - width];
+    int aligned_end;
 
-            ptr[i = numpixels - width] *= boundaryscale;
-
-            /* Filter upwards */
-            for (; i > 0; i -= width)
-                ptr[i - width] += nu * ptr[i];
-        }
-    }
+    aligned_end = slice_start + (((slice_end - slice_start) >> 3) << 3);
+    /* Filter vertically along columns (process 8 columns in each step) */
+    do_vertical_columns(buffer, width, height, slice_start, aligned_end,
+                        steps, nu, boundaryscale, 8);
 
+    /* Filter un-aligned columns one by one */
+    do_vertical_columns(buffer, width, height, aligned_end, slice_end,
+                        steps, nu, boundaryscale, 1);
     return 0;
 }
 
@@ -209,6 +216,13 @@ static int query_formats(AVFilterContext *ctx)
     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
 }
 
+void ff_gblur_init(GBlurContext *s)
+{
+    s->horiz_slice = horiz_slice_c;
+    if (ARCH_X86_64)
+        ff_gblur_init_x86(s);
+}
+
 static int config_input(AVFilterLink *inlink)
 {
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
@@ -229,6 +243,7 @@ static int config_input(AVFilterLink *inlink)
     if (s->sigmaV < 0) {
         s->sigmaV = s->sigma;
     }
+    ff_gblur_init(s);
 
     return 0;
 }
@@ -329,6 +344,22 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     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)
+{
+    GBlurContext *s = ctx->priv;
+    int ret = 0;
+
+    if (   !strcmp(cmd, "sigma") || !strcmp(cmd, "sigmaV")
+        || !strcmp(cmd, "steps") || !strcmp(cmd, "planes")) {
+        av_opt_set(s, cmd, args, 0);
+    } else {
+        ret = AVERROR(ENOSYS);
+    }
+
+    return ret;
+}
+
 static av_cold void uninit(AVFilterContext *ctx)
 {
     GBlurContext *s = ctx->priv;
@@ -364,4 +395,5 @@ AVFilter ff_vf_gblur = {
     .inputs        = gblur_inputs,
     .outputs       = gblur_outputs,
     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
+    .process_command = process_command,
 };