Browse code

lavfi: refactor query_formats and auto-insert audio conversion filters

Behaviour has been changed to fail negotiation if fields have been
left empty.

Patch was originally based on a patch by Hemanth.

Signed-off-by: Stefano Sabatini <stefasab@gmail.com>

Mina Nagy Zaki authored on 2011/07/14 00:04:03
Showing 1 changed files
... ...
@@ -23,6 +23,7 @@
23 23
 #include <ctype.h>
24 24
 #include <string.h>
25 25
 
26
+#include "libavutil/audioconvert.h"
26 27
 #include "avfilter.h"
27 28
 #include "avfiltergraph.h"
28 29
 #include "internal.h"
... ...
@@ -135,11 +136,57 @@ AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
135 135
     return NULL;
136 136
 }
137 137
 
138
+static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
139
+                              const char *filt_name, const char *filt_args)
140
+{
141
+    static int auto_count = 0, ret;
142
+    char inst_name[32];
143
+    AVFilterContext *filt_ctx;
144
+
145
+    snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
146
+            filt_name, auto_count++);
147
+
148
+    if ((ret = avfilter_graph_create_filter(&filt_ctx,
149
+                                            avfilter_get_by_name(filt_name),
150
+                                            inst_name, filt_args, NULL, graph)) < 0)
151
+        return ret;
152
+    if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
153
+        return ret;
154
+
155
+    filt_ctx->filter->query_formats(filt_ctx);
156
+
157
+    if ( ((link = filt_ctx-> inputs[0]) &&
158
+           !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
159
+         ((link = filt_ctx->outputs[0]) &&
160
+           !avfilter_merge_formats(link->in_formats, link->out_formats))
161
+       ) {
162
+        av_log(NULL, AV_LOG_ERROR,
163
+               "Impossible to convert between the formats supported by the filter "
164
+               "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
165
+        return AVERROR(EINVAL);
166
+    }
167
+
168
+    if (link->type == AVMEDIA_TYPE_AUDIO &&
169
+         (((link = filt_ctx-> inputs[0]) &&
170
+           (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
171
+            !avfilter_merge_formats(link->in_packing,   link->out_packing))) ||
172
+         ((link = filt_ctx->outputs[0]) &&
173
+           (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
174
+            !avfilter_merge_formats(link->in_packing,   link->out_packing))))
175
+       ) {
176
+        av_log(NULL, AV_LOG_ERROR,
177
+               "Impossible to convert between the channel layouts/packing formats supported by the filter "
178
+               "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
179
+        return AVERROR(EINVAL);
180
+    }
181
+
182
+    return 0;
183
+}
184
+
138 185
 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
139 186
 {
140 187
     int i, j, ret;
141
-    int scaler_count = 0;
142
-    char inst_name[30];
188
+    char filt_args[128];
143 189
 
144 190
     /* ask all the sub-filters for their supported media formats */
145 191
     for (i = 0; i < graph->filter_count; i++) {
... ...
@@ -155,32 +202,30 @@ static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
155 155
 
156 156
         for (j = 0; j < filter->input_count; j++) {
157 157
             AVFilterLink *link = filter->inputs[j];
158
-            if (link && link->in_formats != link->out_formats) {
159
-                if (!avfilter_merge_formats(link->in_formats,
160
-                                            link->out_formats)) {
161
-                    AVFilterContext *scale;
162
-                    char scale_args[256];
163
-                    /* couldn't merge format lists. auto-insert scale filter */
164
-                    snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
165
-                             scaler_count++);
166
-                    snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
167
-                    if ((ret = avfilter_graph_create_filter(&scale, avfilter_get_by_name("scale"),
168
-                                                            inst_name, scale_args, NULL, graph)) < 0)
169
-                        return ret;
170
-                    if ((ret = avfilter_insert_filter(link, scale, 0, 0)) < 0)
171
-                        return ret;
172
-
173
-                    scale->filter->query_formats(scale);
174
-                    if (((link = scale-> inputs[0]) &&
175
-                         !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
176
-                        ((link = scale->outputs[0]) &&
177
-                         !avfilter_merge_formats(link->in_formats, link->out_formats))) {
178
-                        av_log(log_ctx, AV_LOG_ERROR,
179
-                               "Impossible to convert between the formats supported by the filter "
180
-                               "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
181
-                        return AVERROR(EINVAL);
182
-                    }
183
-                }
158
+            if (!link) continue;
159
+
160
+            if (!link->in_formats || !link->out_formats)
161
+                return AVERROR(EINVAL);
162
+
163
+            if (link->type == AVMEDIA_TYPE_VIDEO &&
164
+                !avfilter_merge_formats(link->in_formats, link->out_formats)) {
165
+
166
+                /* couldn't merge format lists, auto-insert scale filter */
167
+                snprintf(filt_args, sizeof(filt_args), "0:0:%s",
168
+                         graph->scale_sws_opts);
169
+                if (ret = insert_conv_filter(graph, link, "scale", filt_args))
170
+                    return ret;
171
+            }
172
+            else if (link->type == AVMEDIA_TYPE_AUDIO) {
173
+                if (!link->in_chlayouts || !link->out_chlayouts ||
174
+                    !link->in_packing   || !link->out_packing)
175
+                    return AVERROR(EINVAL);
176
+
177
+                if (!avfilter_merge_formats(link->in_formats,   link->out_formats)   ||
178
+                    !avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
179
+                    !avfilter_merge_formats(link->in_packing,   link->out_packing))
180
+                    if (ret = insert_conv_filter(graph, link, "aconvert", NULL))
181
+                       return ret;
184 182
             }
185 183
         }
186 184
     }