Browse code

asrc_anullsrc: extend syntax to make it accept a non positional list of arguments

The new syntax is more extensible and more user-friendly.

This breaks the previous syntax, should not be an issue as possibly
no-one is already using anullsrc.

Stefano Sabatini authored on 2011/09/17 19:31:21
Showing 3 changed files
... ...
@@ -312,26 +312,34 @@ value is "-1".
312 312
 Null audio source, never return audio frames. It is mainly useful as a
313 313
 template and to be employed in analysis / debugging tools.
314 314
 
315
-It accepts as optional parameter a string of the form
316
-@var{sample_rate}:@var{channel_layout}.
315
+It accepts an optional sequence of @var{key}=@var{value} pairs,
316
+separated by ":".
317
+
318
+The description of the accepted options follows.
317 319
 
318
-@var{sample_rate} specify the sample rate, and defaults to 44100.
320
+@table @option
321
+
322
+@item sample_rate, s
323
+Specify the sample rate, and defaults to 44100.
319 324
 
320
-@var{channel_layout} specify the channel layout, and can be either an
321
-integer or a string representing a channel layout. The default value
322
-of @var{channel_layout} is 3, which corresponds to CH_LAYOUT_STEREO.
325
+@item channel_layout, cl
326
+
327
+Specify the channel layout, and can be either an integer or a string
328
+representing a channel layout. The default value of @var{channel_layout}
329
+is "stereo".
323 330
 
324 331
 Check the channel_layout_map definition in
325 332
 @file{libavcodec/audioconvert.c} for the mapping between strings and
326 333
 channel layout values.
334
+@end table
327 335
 
328 336
 Follow some examples:
329 337
 @example
330
-#  set the sample rate to 48000 Hz and the channel layout to CH_LAYOUT_MONO.
331
-anullsrc=48000:4
338
+#  set the sample rate to 48000 Hz and the channel layout to AV_CH_LAYOUT_MONO.
339
+anullsrc=r=48000:cl=4
332 340
 
333 341
 # same as
334
-anullsrc=48000:mono
342
+anullsrc=r=48000:cl=mono
335 343
 @end example
336 344
 
337 345
 @c man end AUDIO SOURCES
... ...
@@ -21,37 +21,61 @@
21 21
  * null audio source
22 22
  */
23 23
 
24
-#include "avfilter.h"
25 24
 #include "libavutil/audioconvert.h"
25
+#include "libavutil/opt.h"
26
+
27
+#include "avfilter.h"
28
+#include "internal.h"
26 29
 
27 30
 typedef struct {
31
+    const AVClass *class;
32
+    char   *channel_layout_str;
28 33
     int64_t channel_layout;
29
-    int64_t sample_rate;
34
+    char   *sample_rate_str;
35
+    int     sample_rate;
30 36
 } ANullContext;
31 37
 
38
+#define OFFSET(x) offsetof(ANullContext, x)
39
+
40
+static const AVOption anullsrc_options[]= {
41
+    { "channel_layout", "set channel_layout", OFFSET(channel_layout_str), FF_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0 },
42
+    { "cl",             "set channel_layout", OFFSET(channel_layout_str), FF_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0 },
43
+    { "sample_rate",    "set sample rate",    OFFSET(sample_rate_str)   , FF_OPT_TYPE_STRING, {.str = "44100"}, 0, 0 },
44
+    { "r",              "set sample rate",    OFFSET(sample_rate_str)   , FF_OPT_TYPE_STRING, {.str = "44100"}, 0, 0 },
45
+    { NULL },
46
+};
47
+
48
+static const char *anullsrc_get_name(void *ctx)
49
+{
50
+    return "anullsrc";
51
+}
52
+
53
+static const AVClass anullsrc_class = {
54
+    "ANullSrcContext",
55
+    anullsrc_get_name,
56
+    anullsrc_options
57
+};
58
+
32 59
 static int init(AVFilterContext *ctx, const char *args, void *opaque)
33 60
 {
34 61
     ANullContext *priv = ctx->priv;
35
-    char channel_layout_str[128] = "";
36
-
37
-    priv->sample_rate = 44100;
38
-    priv->channel_layout = AV_CH_LAYOUT_STEREO;
62
+    int ret;
39 63
 
40
-    if (args)
41
-        sscanf(args, "%"PRId64":%s", &priv->sample_rate, channel_layout_str);
64
+    priv->class = &anullsrc_class;
65
+    av_opt_set_defaults(priv);
42 66
 
43
-    if (priv->sample_rate < 0) {
44
-        av_log(ctx, AV_LOG_ERROR, "Invalid negative sample rate: %"PRId64"\n", priv->sample_rate);
45
-        return AVERROR(EINVAL);
67
+    if ((ret = (av_set_options_string(priv, args, "=", ":"))) < 0) {
68
+        av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
69
+        return ret;
46 70
     }
47 71
 
48
-    if (*channel_layout_str)
49
-        if (!(priv->channel_layout = av_get_channel_layout(channel_layout_str))
50
-            && sscanf(channel_layout_str, "%"PRId64, &priv->channel_layout) != 1) {
51
-            av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for channel layout\n",
52
-                   channel_layout_str);
53
-            return AVERROR(EINVAL);
54
-        }
72
+    if ((ret = ff_parse_sample_rate(&priv->sample_rate,
73
+                                     priv->sample_rate_str, ctx)) < 0)
74
+        return ret;
75
+
76
+    if ((ret = ff_parse_channel_layout(&priv->channel_layout,
77
+                                        priv->channel_layout_str, ctx)) < 0)
78
+        return ret;
55 79
 
56 80
     return 0;
57 81
 }
... ...
@@ -68,7 +92,7 @@ static int config_props(AVFilterLink *outlink)
68 68
     chans_nb = av_get_channel_layout_nb_channels(priv->channel_layout);
69 69
     av_get_channel_layout_string(buf, sizeof(buf), chans_nb, priv->channel_layout);
70 70
     av_log(outlink->src, AV_LOG_INFO,
71
-           "sample_rate:%"PRId64 " channel_layout:%"PRId64 " channel_layout_description:'%s'\n",
71
+           "sample_rate:%d channel_layout:%"PRId64 " channel_layout_description:'%s'\n",
72 72
            priv->sample_rate, priv->channel_layout, buf);
73 73
 
74 74
     return 0;
... ...
@@ -30,7 +30,7 @@
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  2
32 32
 #define LIBAVFILTER_VERSION_MINOR 43
33
-#define LIBAVFILTER_VERSION_MICRO  0
33
+#define LIBAVFILTER_VERSION_MICRO  1
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \