af_aformat: use evil ADD_FORMATS macro for cutting out duplicated code
authorStefano Sabatini <stefano.sabatini-lala@poste.it>
Tue, 16 Aug 2011 14:54:04 +0000 (16:54 +0200)
committerStefano Sabatini <stefano.sabatini-lala@poste.it>
Mon, 22 Aug 2011 08:34:43 +0000 (10:34 +0200)
libavfilter/af_aformat.c

index c753ea7..0eb7dfa 100644 (file)
@@ -42,60 +42,30 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
     if (!args)
         goto arg_fail;
 
-    fmts_str = av_get_token(&args, ":");
-    if (!fmts_str || !*fmts_str)
-        goto arg_fail;
-    if (!strcmp(fmts_str, "all")) {
-        aformat->formats = avfilter_all_formats(AVMEDIA_TYPE_AUDIO);
-    } else {
-        for (fmt_str = fmts_str;
-             fmt_str = strtok_r(fmt_str, ",", &ptr); fmt_str = NULL) {
-            if ((ret = ff_parse_sample_format((int*)&fmt, fmt_str, ctx)) < 0) {
-                av_freep(&fmts_str);
-                return ret;
-            }
-            avfilter_add_format(&aformat->formats, fmt);
-        }
-    }
-    av_freep(&fmts_str);
-
-    if (*args)
+#define ADD_FORMATS(all_formats, fmt_name, fmt_type, fmts_list)         \
+    fmts_str = av_get_token(&args, ":");                                \
+    if (!fmts_str || !*fmts_str)                                        \
+        goto arg_fail;                                                  \
+    if (!strcmp(fmts_str, "all")) {                                     \
+        aformat->fmts_list = all_formats;                               \
+    } else {                                                            \
+        for (fmt_str = fmts_str;                                        \
+             fmt_str = strtok_r(fmt_str, ",", &ptr); fmt_str = NULL) {  \
+            if ((ret = ff_parse_##fmt_name((fmt_type *)&fmt,            \
+                                           fmt_str, ctx)) < 0) {        \
+                av_freep(&fmts_str);                                    \
+                return ret;                                             \
+            }                                                           \
+            avfilter_add_format(&aformat->fmts_list, fmt);              \
+        }                                                               \
+    }                                                                   \
+    av_freep(&fmts_str);                                                \
+    if (*args)                                                          \
         args++;
-    fmts_str = av_get_token(&args, ":");
-    if (!fmts_str || !*fmts_str)
-        goto arg_fail;
-    if (!strcmp(fmts_str, "all")) {
-        aformat->chlayouts = avfilter_all_channel_layouts();
-    } else {
-        for (fmt_str = fmts_str;
-             fmt_str = strtok_r(fmt_str, ",", &ptr); fmt_str = NULL) {
-            if ((ret = ff_parse_channel_layout(&fmt, fmt_str, ctx)) < 0) {
-                av_freep(&fmts_str);
-                return ret;
-            }
-            avfilter_add_format(&aformat->chlayouts, fmt);
-        }
-    }
-    av_freep(&fmts_str);
 
-    if (*args)
-        args++;
-    fmts_str = av_get_token(&args, ":");
-    if (!fmts_str || !*fmts_str)
-        goto arg_fail;
-    if (!strcmp(fmts_str, "all")) {
-        aformat->packing = avfilter_all_packing_formats();
-    } else {
-        for (fmt_str = fmts_str;
-             fmt_str = strtok_r(fmt_str, ",", &ptr); fmt_str = NULL) {
-            if ((ret = ff_parse_packing_format((int*)&fmt, fmt_str, ctx)) < 0) {
-                av_freep(&fmts_str);
-                return ret;
-            }
-            avfilter_add_format(&aformat->packing, fmt);
-        }
-    }
-    av_freep(&fmts_str);
+    ADD_FORMATS(avfilter_all_formats(AVMEDIA_TYPE_AUDIO), sample_format, int, formats);
+    ADD_FORMATS(avfilter_all_channel_layouts(), channel_layout, int64_t, chlayouts);
+    ADD_FORMATS(avfilter_all_packing_formats(), packing_format, int, packing);
 
     return 0;