avfilter: add stereowiden filter
[ffmpeg.git] / libavfilter / formats.c
index 42afcf4..8758b3d 100644 (file)
@@ -262,18 +262,6 @@ int ff_fmt_is_in(int fmt, const int *fmts)
     return 0;
 }
 
-#define COPY_INT_LIST(list_copy, list, type) {                          \
-    int count = 0;                                                      \
-    if (list)                                                           \
-        for (count = 0; list[count] != -1; count++)                     \
-            ;                                                           \
-    list_copy = av_calloc(count+1, sizeof(type));                       \
-    if (list_copy) {                                                    \
-        memcpy(list_copy, list, sizeof(type) * count);                  \
-        list_copy[count] = -1;                                          \
-    }                                                                   \
-}
-
 #define MAKE_FORMAT_LIST(type, field, count_field)                      \
     type *formats;                                                      \
     int count = 0;                                                      \
@@ -281,12 +269,13 @@ int ff_fmt_is_in(int fmt, const int *fmts)
         for (count = 0; fmts[count] != -1; count++)                     \
             ;                                                           \
     formats = av_mallocz(sizeof(*formats));                             \
-    if (!formats) return NULL;                                          \
+    if (!formats)                                                       \
+        return NULL;                                                    \
     formats->count_field = count;                                       \
     if (count) {                                                        \
         formats->field = av_malloc_array(count, sizeof(*formats->field));      \
         if (!formats->field) {                                          \
-            av_free(formats);                                           \
+            av_freep(&formats);                                         \
             return NULL;                                                \
         }                                                               \
     }
@@ -319,8 +308,8 @@ do {                                                        \
     if (!(*f) && !(*f = av_mallocz(sizeof(**f))))           \
         return AVERROR(ENOMEM);                             \
                                                             \
-    fmts = av_realloc((*f)->list,                           \
-                      sizeof(*(*f)->list) * ((*f)->nb + 1));\
+    fmts = av_realloc_array((*f)->list, (*f)->nb + 1,       \
+                            sizeof(*(*f)->list));           \
     if (!fmts) {                                            \
         if (!oldf)                                          \
             av_freep(f);                                    \
@@ -411,19 +400,26 @@ AVFilterChannelLayouts *ff_all_channel_counts(void)
     return ret;
 }
 
-#define FORMATS_REF(f, ref)                                          \
-do {                                                                 \
-    *ref = f;                                                        \
-    f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
-    f->refs[f->refcount-1] = ref;                                    \
-} while (0)
-
-void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
+#define FORMATS_REF(f, ref)                                                     \
+    void *tmp;                                                                  \
+                                                                                \
+    if (!ref)                                                                   \
+        return AVERROR_BUG;                                                     \
+                                                                                \
+    tmp = av_realloc_array(f->refs, sizeof(*f->refs), f->refcount + 1);         \
+    if (!tmp)                                                                   \
+        return AVERROR(ENOMEM);                                                 \
+    f->refs = tmp;                                                              \
+    f->refs[f->refcount++] = ref;                                               \
+    *ref = f;                                                                   \
+    return 0
+
+int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
 {
     FORMATS_REF(f, ref);
 }
 
-void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
+int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
 {
     FORMATS_REF(f, ref);
 }
@@ -494,18 +490,24 @@ void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
 }
 
 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
-{                                                                   \
     int count = 0, i;                                               \
                                                                     \
+    if (!fmts)                                                      \
+        return AVERROR_BUG;                                         \
+                                                                    \
     for (i = 0; i < ctx->nb_inputs; i++) {                          \
         if (ctx->inputs[i] && !ctx->inputs[i]->out_fmts) {          \
-            ref(fmts, &ctx->inputs[i]->out_fmts);                   \
+            int ret = ref(fmts, &ctx->inputs[i]->out_fmts);         \
+            if (ret < 0)                                            \
+                return ret;                                         \
             count++;                                                \
         }                                                           \
     }                                                               \
     for (i = 0; i < ctx->nb_outputs; i++) {                         \
         if (ctx->outputs[i] && !ctx->outputs[i]->in_fmts) {         \
-            ref(fmts, &ctx->outputs[i]->in_fmts);                   \
+            int ret = ref(fmts, &ctx->outputs[i]->in_fmts);         \
+            if (ret < 0)                                            \
+                return ret;                                         \
             count++;                                                \
         }                                                           \
     }                                                               \
@@ -515,17 +517,18 @@ void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
         av_freep(&fmts->refs);                                      \
         av_freep(&fmts);                                            \
     }                                                               \
-}
+                                                                    \
+    return 0;
 
-void ff_set_common_channel_layouts(AVFilterContext *ctx,
-                                   AVFilterChannelLayouts *layouts)
+int ff_set_common_channel_layouts(AVFilterContext *ctx,
+                                  AVFilterChannelLayouts *layouts)
 {
     SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
                        ff_channel_layouts_ref, channel_layouts);
 }
 
-void ff_set_common_samplerates(AVFilterContext *ctx,
-                               AVFilterFormats *samplerates)
+int ff_set_common_samplerates(AVFilterContext *ctx,
+                              AVFilterFormats *samplerates)
 {
     SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
                        ff_formats_ref, formats);
@@ -536,7 +539,7 @@ void ff_set_common_samplerates(AVFilterContext *ctx,
  * formats. If there are no links hooked to this filter, the list of formats is
  * freed.
  */
-void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
+int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
 {
     SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
                        ff_formats_ref, formats);
@@ -545,14 +548,21 @@ void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
 static int default_query_formats_common(AVFilterContext *ctx,
                                         AVFilterChannelLayouts *(layouts)(void))
 {
+    int ret;
     enum AVMediaType type = ctx->inputs  && ctx->inputs [0] ? ctx->inputs [0]->type :
                             ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
                             AVMEDIA_TYPE_VIDEO;
 
-    ff_set_common_formats(ctx, ff_all_formats(type));
+    ret = ff_set_common_formats(ctx, ff_all_formats(type));
+    if (ret < 0)
+        return ret;
     if (type == AVMEDIA_TYPE_AUDIO) {
-        ff_set_common_channel_layouts(ctx, layouts());
-        ff_set_common_samplerates(ctx, ff_all_samplerates());
+        ret = ff_set_common_channel_layouts(ctx, layouts());
+        if (ret < 0)
+            return ret;
+        ret = ff_set_common_samplerates(ctx, ff_all_samplerates());
+        if (ret < 0)
+            return ret;
     }
 
     return 0;
@@ -627,23 +637,20 @@ int ff_parse_channel_layout(int64_t *ret, int *nret, const char *arg,
                             void *log_ctx)
 {
     char *tail;
-    int64_t chlayout, count;
+    int64_t chlayout;
 
-    if (nret) {
-        count = strtol(arg, &tail, 10);
-        if (*tail == 'c' && !tail[1] && count > 0 && count < 63) {
-            *nret = count;
-            *ret = 0;
-            return 0;
-        }
-    }
     chlayout = av_get_channel_layout(arg);
     if (chlayout == 0) {
         chlayout = strtol(arg, &tail, 10);
-        if (*tail || chlayout == 0) {
+        if (!(*tail == '\0' || *tail == 'c' && *(tail + 1) == '\0') || chlayout <= 0 || chlayout > 63) {
             av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
             return AVERROR(EINVAL);
         }
+        if (nret) {
+            *nret = chlayout;
+            *ret = 0;
+            return 0;
+        }
     }
     *ret = chlayout;
     if (nret)
@@ -659,12 +666,41 @@ int main(void)
 {
     const int64_t *cl;
     char buf[512];
+    int i;
+    const char *teststrings[] ={
+        "blah",
+        "1",
+        "2",
+        "-1",
+        "60",
+        "65",
+        "1c",
+        "2c",
+        "-1c",
+        "60c",
+        "65c",
+        "5.1",
+        "stereo",
+        "1+1+1+1",
+        "1c+1c+1c+1c",
+        "2c+1c",
+        "0x3",
+    };
 
     for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
         av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);
         printf("%s\n", buf);
     }
 
+    for ( i = 0; i<FF_ARRAY_ELEMS(teststrings); i++) {
+        int64_t layout = -1;
+        int count = -1;
+        int ret;
+        ret = ff_parse_channel_layout(&layout, &count, teststrings[i], NULL);
+
+        printf ("%d = ff_parse_channel_layout(%016"PRIX64", %2d, %s);\n", ret ? -1 : 0, layout, count, teststrings[i]);
+    }
+
     return 0;
 }