]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_gradfun.c
Merge commit '555000c7d5c1e13043a948ebc48d2939b0ba6536'
[ffmpeg] / libavfilter / vf_gradfun.c
index ca7ef69144bfef4a3b79b74030d4aa7dabdc8398..9e377614aed1127e8040decf0b3985bce24f275e 100644 (file)
@@ -2,20 +2,20 @@
  * Copyright (c) 2010 Nolan Lum <nol888@gmail.com>
  * Copyright (c) 2009 Loren Merritt <lorenm@u.washignton.edu>
  *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include "libavutil/common.h"
 #include "libavutil/cpu.h"
 #include "libavutil/pixdesc.h"
+#include "libavutil/opt.h"
 #include "avfilter.h"
 #include "formats.h"
 #include "gradfun.h"
 #include "internal.h"
 #include "video.h"
 
+#define OFFSET(x) offsetof(GradFunContext, x)
+#define F AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption gradfun_options[] = {
+    { "strength", "set the maximum amount by which the filter will change any one pixel", OFFSET(strength), AV_OPT_TYPE_DOUBLE, {.dbl = 1.2}, 0.51, 64, F },
+    { "radius",   "set the neighborhood to fit the gradient to",                          OFFSET(radius),   AV_OPT_TYPE_INT,    {.i64 =  16},    4, 32, F },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(gradfun);
+
 DECLARE_ALIGNED(16, static const uint16_t, dither)[8][8] = {
     {0x00,0x60,0x18,0x78,0x06,0x66,0x1E,0x7E},
     {0x40,0x20,0x58,0x38,0x46,0x26,0x5E,0x3E},
@@ -53,10 +65,10 @@ DECLARE_ALIGNED(16, static const uint16_t, dither)[8][8] = {
     {0x54,0x34,0x4C,0x2C,0x52,0x32,0x4A,0x2A},
 };
 
-void ff_gradfun_filter_line_c(uint8_t *dst, uint8_t *src, uint16_t *dc, int width, int thresh, const uint16_t *dithers)
+void ff_gradfun_filter_line_c(uint8_t *dst, const uint8_t *src, const uint16_t *dc, int width, int thresh, const uint16_t *dithers)
 {
     int x;
-    for (x = 0; x < width; x++, dc += x & 1) {
+    for (x = 0; x < width; dc += x & 1, x++) {
         int pix = src[x] << 7;
         int delta = dc[0] - pix;
         int m = abs(delta) * thresh >> 16;
@@ -67,7 +79,7 @@ void ff_gradfun_filter_line_c(uint8_t *dst, uint8_t *src, uint16_t *dc, int widt
     }
 }
 
-void ff_gradfun_blur_line_c(uint16_t *dc, uint16_t *buf, uint16_t *buf1, uint8_t *src, int src_linesize, int width)
+void ff_gradfun_blur_line_c(uint16_t *dc, uint16_t *buf, const uint16_t *buf1, const uint8_t *src, int src_linesize, int width)
 {
     int x, v, old;
     for (x = 0; x < width; x++) {
@@ -78,7 +90,7 @@ void ff_gradfun_blur_line_c(uint16_t *dc, uint16_t *buf, uint16_t *buf1, uint8_t
     }
 }
 
-static void filter(GradFunContext *ctx, uint8_t *dst, uint8_t *src, int width, int height, int dst_linesize, int src_linesize, int r)
+static void filter(GradFunContext *ctx, uint8_t *dst, const uint8_t *src, int width, int height, int dst_linesize, int src_linesize, int r)
 {
     int bstride = FFALIGN(width, 16) / 2;
     int y;
@@ -121,24 +133,26 @@ static void filter(GradFunContext *ctx, uint8_t *dst, uint8_t *src, int width, i
 
 static av_cold int init(AVFilterContext *ctx, const char *args)
 {
+    int ret;
     GradFunContext *gf = ctx->priv;
-    float thresh = 1.2;
-    int radius = 16;
+    static const char *shorthand[] = { "strength", "radius", NULL };
+
+    gf->class = &gradfun_class;
+    av_opt_set_defaults(gf);
 
-    if (args)
-        sscanf(args, "%f:%d", &thresh, &radius);
+    if ((ret = av_opt_set_from_string(gf, args, shorthand, "=", ":")) < 0)
+        return ret;
 
-    thresh = av_clipf(thresh, 0.51, 255);
-    gf->thresh = (1 << 15) / thresh;
-    gf->radius = av_clip((radius + 1) & ~1, 4, 32);
+    gf->thresh = (1 << 15) / gf->strength;
+    gf->radius = av_clip((gf->radius + 1) & ~1, 4, 32);
 
-    gf->blur_line = ff_gradfun_blur_line_c;
+    gf->blur_line   = ff_gradfun_blur_line_c;
     gf->filter_line = ff_gradfun_filter_line_c;
 
     if (ARCH_X86)
         ff_gradfun_init_x86(gf);
 
-    av_log(ctx, AV_LOG_VERBOSE, "threshold:%.2f radius:%d\n", thresh, gf->radius);
+    av_log(ctx, AV_LOG_VERBOSE, "threshold:%.2f radius:%d\n", gf->strength, gf->radius);
 
     return 0;
 }
@@ -156,6 +170,7 @@ static int query_formats(AVFilterContext *ctx)
         AV_PIX_FMT_GRAY8,              AV_PIX_FMT_NV12,
         AV_PIX_FMT_NV21,               AV_PIX_FMT_YUV444P,
         AV_PIX_FMT_YUV422P,            AV_PIX_FMT_YUV411P,
+        AV_PIX_FMT_YUV440P,
         AV_PIX_FMT_NONE
     };
 
@@ -199,10 +214,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
             av_frame_free(&in);
             return AVERROR(ENOMEM);
         }
-
         av_frame_copy_props(out, in);
-        out->width  = outlink->w;
-        out->height = outlink->h;
     }
 
     for (p = 0; p < 4 && in->data[p]; p++) {
@@ -252,7 +264,7 @@ AVFilter avfilter_vf_gradfun = {
     .init          = init,
     .uninit        = uninit,
     .query_formats = query_formats,
-
-    .inputs    = avfilter_vf_gradfun_inputs,
-    .outputs   = avfilter_vf_gradfun_outputs,
+    .inputs        = avfilter_vf_gradfun_inputs,
+    .outputs       = avfilter_vf_gradfun_outputs,
+    .priv_class    = &gradfun_class,
 };