Browse code

lavfi/colormatrix: add support for named options

Stefano Sabatini authored on 2013/03/24 21:40:57
Showing 3 changed files
... ...
@@ -2056,12 +2056,37 @@ boxblur=min(h\,w)/10:1:min(cw\,ch)/10:1
2056 2056
 
2057 2057
 @section colormatrix
2058 2058
 
2059
-The colormatrix filter allows conversion between any of the following color
2060
-space: BT.709 (@var{bt709}), BT.601 (@var{bt601}), SMPTE-240M (@var{smpte240m})
2061
-and FCC (@var{fcc}).
2059
+Convert color matrix.
2062 2060
 
2063
-The syntax of the parameters is @var{source}:@var{destination}:
2061
+The filter accepts parameters as a list of @var{key}=@var{value}
2062
+pairs, separated by ":". If the key of the first options is omitted,
2063
+the arguments are interpreted according to the syntax
2064
+@var{src}:@var{dst}.
2065
+
2066
+A description of the accepted options follows:
2067
+@table @option
2068
+@item src
2069
+@item dst
2070
+Specify the source and destination color matrix. Both values must be
2071
+specified.
2072
+
2073
+The accepted values are:
2074
+@table @samp
2075
+@item bt709
2076
+BT.709
2077
+
2078
+@item bt601
2079
+BT.601
2080
+
2081
+@item smpte240m
2082
+SMPTE-240M
2083
+
2084
+@item fcc
2085
+FCC
2086
+@end table
2087
+@end table
2064 2088
 
2089
+For example to convert from BT.601 to SMPTE-240M, use the command:
2065 2090
 @example
2066 2091
 colormatrix=bt601:smpte240m
2067 2092
 @end example
... ...
@@ -30,7 +30,7 @@
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32 32
 #define LIBAVFILTER_VERSION_MINOR  48
33
-#define LIBAVFILTER_VERSION_MICRO 100
33
+#define LIBAVFILTER_VERSION_MICRO 101
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \
... ...
@@ -33,6 +33,7 @@
33 33
 #include "formats.h"
34 34
 #include "internal.h"
35 35
 #include "video.h"
36
+#include "libavutil/opt.h"
36 37
 #include "libavutil/pixdesc.h"
37 38
 #include "libavutil/avstring.h"
38 39
 
... ...
@@ -54,15 +55,39 @@ static const double yuv_coeff[4][3][3] = {
54 54
       { -0.4450, -0.0550, +0.5000 } },
55 55
 };
56 56
 
57
+enum ColorMode {
58
+    COLOR_MODE_NONE = -1,
59
+    COLOR_MODE_BT709,
60
+    COLOR_MODE_FCC,
61
+    COLOR_MODE_BT601,
62
+    COLOR_MODE_SMPTE240M,
63
+    COLOR_MODE_COUNT
64
+};
65
+
57 66
 typedef struct {
67
+    const AVClass *class;
58 68
     int yuv_convert[16][3][3];
59 69
     int interlaced;
60
-    int source, dest, mode;
61
-    char src[256];
62
-    char dst[256];
70
+    enum ColorMode source, dest;
71
+    int mode;
63 72
     int hsub, vsub;
64 73
 } ColorMatrixContext;
65 74
 
75
+#define OFFSET(x) offsetof(ColorMatrixContext, x)
76
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
77
+
78
+static const AVOption colormatrix_options[] = {
79
+    { "src", "set source color matrix",      OFFSET(source), AV_OPT_TYPE_INT, {.i64=COLOR_MODE_NONE}, COLOR_MODE_NONE, COLOR_MODE_COUNT-1, .flags=FLAGS, .unit="color_mode" },
80
+    { "dst", "set destination color matrix", OFFSET(dest),   AV_OPT_TYPE_INT, {.i64=COLOR_MODE_NONE}, COLOR_MODE_NONE, COLOR_MODE_COUNT-1, .flags=FLAGS, .unit="color_mode" },
81
+    { "bt709",     "set BT.709 colorspace",      0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT709},       .flags=FLAGS, .unit="color_mode" },
82
+    { "fcc",       "set FCC colorspace   ",      0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_FCC},         .flags=FLAGS, .unit="color_mode" },
83
+    { "bt601",     "set BT.601 colorspace",      0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT601},       .flags=FLAGS, .unit="color_mode" },
84
+    { "smpte240m", "set SMPTE-240M colorspace",  0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_SMPTE240M},   .flags=FLAGS, .unit="color_mode" },
85
+    { NULL }
86
+};
87
+
88
+AVFILTER_DEFINE_CLASS(colormatrix);
89
+
66 90
 #define ma m[0][0]
67 91
 #define mb m[0][1]
68 92
 #define mc m[0][2]
... ...
@@ -133,40 +158,14 @@ static void calc_coefficients(AVFilterContext *ctx)
133 133
     }
134 134
 }
135 135
 
136
-static const char *color_modes[] = {"bt709", "FCC", "bt601", "smpte240m"};
137
-
138
-static int get_color_mode_index(const char *name)
139
-{
140
-    int i;
141
-
142
-    for (i = 0; i < FF_ARRAY_ELEMS(color_modes); i++)
143
-        if (!av_strcasecmp(color_modes[i], name))
144
-            return i;
145
-    return -1;
146
-}
136
+static const char *color_modes[] = {"bt709", "fcc", "bt601", "smpte240m"};
147 137
 
148 138
 static av_cold int init(AVFilterContext *ctx, const char *args)
149 139
 {
150 140
     ColorMatrixContext *color = ctx->priv;
151 141
 
152
-    if (!args)
153
-        goto usage;
154
-    if (sscanf(args, "%255[^:]:%255[^:]", color->src, color->dst) != 2) {
155
-    usage:
156
-        av_log(ctx, AV_LOG_ERROR, "usage: <src>:<dst>\n");
157
-        av_log(ctx, AV_LOG_ERROR, "possible options: bt709,bt601,smpte240m,fcc\n");
158
-        return -1;
159
-    }
160
-
161
-    color->source = get_color_mode_index(color->src);
162
-    if (color->source < 0) {
163
-        av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->src);
164
-        return AVERROR(EINVAL);
165
-    }
166
-
167
-    color->dest = get_color_mode_index(color->dst);
168
-    if (color->dest < 0) {
169
-        av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->dst);
142
+    if (color->source == COLOR_MODE_NONE || color->dest == COLOR_MODE_NONE) {
143
+        av_log(ctx, AV_LOG_ERROR, "Unspecified source or destination color space\n");
170 144
         return AVERROR(EINVAL);
171 145
     }
172 146
 
... ...
@@ -313,7 +312,8 @@ static int config_input(AVFilterLink *inlink)
313 313
     color->hsub = pix_desc->log2_chroma_w;
314 314
     color->vsub = pix_desc->log2_chroma_h;
315 315
 
316
-    av_log(ctx, AV_LOG_VERBOSE, "%s -> %s\n", color->src, color->dst);
316
+    av_log(ctx, AV_LOG_VERBOSE, "%s -> %s\n",
317
+           color_modes[color->source], color_modes[color->dest]);
317 318
 
318 319
     return 0;
319 320
 }
... ...
@@ -375,13 +375,17 @@ static const AVFilterPad colormatrix_outputs[] = {
375 375
     { NULL }
376 376
 };
377 377
 
378
+static const char *const shorthand[] = { "src", "dst", NULL };
379
+
378 380
 AVFilter avfilter_vf_colormatrix = {
379 381
     .name          = "colormatrix",
380
-    .description   = NULL_IF_CONFIG_SMALL("Color matrix conversion"),
382
+    .description   = NULL_IF_CONFIG_SMALL("Convert color matrix."),
381 383
 
382 384
     .priv_size     = sizeof(ColorMatrixContext),
383 385
     .init          = init,
384 386
     .query_formats = query_formats,
385 387
     .inputs        = colormatrix_inputs,
386 388
     .outputs       = colormatrix_outputs,
389
+    .priv_class    = &colormatrix_class,
390
+    .shorthand     = shorthand,
387 391
 };