]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_gblur.c
avfilter: Constify all AVFilters
[ffmpeg] / libavfilter / vf_gblur.c
index fd901e20d46be531bcd47bb964634629c87a0256..3f61275658fa610489ed6076f347cbf6791b7b13 100644 (file)
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <float.h>
+
 #include "libavutil/imgutils.h"
 #include "libavutil/opt.h"
 #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
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
 
 static const AVOption gblur_options[] = {
     { "sigma",  "set sigma",            OFFSET(sigma),  AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0.0, 1024, FLAGS },
@@ -72,6 +54,37 @@ typedef struct ThreadData {
     int width;
 } ThreadData;
 
+static void postscale_c(float *buffer, int length,
+                        float postscale, float min, float max)
+{
+    for (int i = 0; i < length; i++) {
+        buffer[i] *= postscale;
+        buffer[i] = av_clipf(buffer[i], min, max);
+    }
+}
+
+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 +97,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,50 +147,38 @@ 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;
+    int aligned_end;
 
-    /* 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];
-
-            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;
 }
 
-
 static int filter_postscale(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
     GBlurContext *s = ctx->priv;
     ThreadData *td = arg;
+    const float max = s->flt ?  FLT_MAX : (1 << s->depth) - 1;
+    const float min = s->flt ? -FLT_MAX : 0.f;
     const int height = td->height;
     const int width = td->width;
-    const int64_t numpixels = width * (int64_t)height;
-    const unsigned slice_start = (numpixels *  jobnr   ) / nb_jobs;
-    const unsigned slice_end   = (numpixels * (jobnr+1)) / nb_jobs;
+    const int awidth = FFALIGN(width, 64);
+    const int slice_start = (height *  jobnr   ) / nb_jobs;
+    const int slice_end   = (height * (jobnr+1)) / nb_jobs;
     const float postscale = s->postscale * s->postscaleV;
-    float *buffer = s->buffer;
-    unsigned i;
+    const int slice_size = slice_end - slice_start;
 
-    for (i = slice_start; i < slice_end; i++)
-        buffer[i] *= postscale;
+    s->postscale_slice(s->buffer + slice_start * awidth,
+                       slice_size * awidth, postscale, min, max);
 
     return 0;
 }
@@ -198,23 +216,35 @@ static int query_formats(AVFilterContext *ctx)
         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
         AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
         AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
+        AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
         AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
         AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
-        AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
+        AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
+        AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32,
+        AV_PIX_FMT_GRAYF32,
         AV_PIX_FMT_NONE
     };
 
     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
 }
 
+void ff_gblur_init(GBlurContext *s)
+{
+    s->horiz_slice = horiz_slice_c;
+    s->postscale_slice = postscale_c;
+    if (ARCH_X86)
+        ff_gblur_init_x86(s);
+}
+
 static int config_input(AVFilterLink *inlink)
 {
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     GBlurContext *s = inlink->dst->priv;
 
     s->depth = desc->comp[0].depth;
+    s->flt = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT);
     s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
     s->planewidth[0] = s->planewidth[3] = inlink->w;
     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
@@ -222,13 +252,14 @@ static int config_input(AVFilterLink *inlink)
 
     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_malloc_array(FFALIGN(inlink->w, 64), FFALIGN(inlink->h, 64) * sizeof(*s->buffer));
     if (!s->buffer)
         return AVERROR(ENOMEM);
 
     if (s->sigmaV < 0) {
         s->sigmaV = s->sigma;
     }
+    ff_gblur_init(s);
 
     return 0;
 }
@@ -284,7 +315,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
             continue;
         }
 
-        if (s->depth == 8) {
+        if (s->flt) {
+            av_image_copy_plane((uint8_t *)bptr, width * sizeof(float),
+                                in->data[plane], in->linesize[plane],
+                                width * sizeof(float), height);
+        } else if (s->depth == 8) {
             for (y = 0; y < height; y++) {
                 for (x = 0; x < width; x++) {
                     bptr[x] = src[x];
@@ -305,7 +340,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
         gaussianiir2d(ctx, plane);
 
         bptr = s->buffer;
-        if (s->depth == 8) {
+        if (s->flt) {
+            av_image_copy_plane(out->data[plane], out->linesize[plane],
+                                (uint8_t *)bptr, width * sizeof(float),
+                                width * sizeof(float), height);
+        } else if (s->depth == 8) {
             for (y = 0; y < height; y++) {
                 for (x = 0; x < width; x++) {
                     dst[x] = bptr[x];
@@ -354,7 +393,7 @@ static const AVFilterPad gblur_outputs[] = {
     { NULL }
 };
 
-AVFilter ff_vf_gblur = {
+const AVFilter ff_vf_gblur = {
     .name          = "gblur",
     .description   = NULL_IF_CONFIG_SMALL("Apply Gaussian Blur filter."),
     .priv_size     = sizeof(GBlurContext),
@@ -364,4 +403,5 @@ AVFilter ff_vf_gblur = {
     .inputs        = gblur_inputs,
     .outputs       = gblur_outputs,
     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
+    .process_command = ff_filter_process_command,
 };