]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/avfiltergraph.c
lavfi: make formats API private on next bump.
[ffmpeg] / libavfilter / avfiltergraph.c
index 01f7411b973383dcd31d8985c1b0d64794991c28..42bf1f7547127ddd7ba26687017a49e678c77450 100644 (file)
@@ -93,7 +93,15 @@ fail:
     return ret;
 }
 
-int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
+/**
+ * Check for the validity of graph.
+ *
+ * A graph is considered valid if all its input and output pads are
+ * connected.
+ *
+ * @return 0 in case of success, a negative value otherwise
+ */
+static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
 {
     AVFilterContext *filt;
     int i, j;
@@ -123,7 +131,12 @@ int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
     return 0;
 }
 
-int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
+/**
+ * Configure all the links of graphctx.
+ *
+ * @return 0 in case of success, a negative value otherwise
+ */
+static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
 {
     AVFilterContext *filt;
     int i, ret;
@@ -161,7 +174,7 @@ static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
         if (graph->filters[i]->filter->query_formats)
             graph->filters[i]->filter->query_formats(graph->filters[i]);
         else
-            avfilter_default_query_formats(graph->filters[i]);
+            ff_default_query_formats(graph->filters[i]);
     }
 
     /* go through and merge as many format lists as possible */
@@ -176,7 +189,7 @@ static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
                 continue;
 
             if (link->in_formats != link->out_formats &&
-                !avfilter_merge_formats(link->in_formats,
+                !ff_merge_formats(link->in_formats,
                                         link->out_formats))
                 convert_needed = 1;
             if (link->type == AVMEDIA_TYPE_AUDIO) {
@@ -191,70 +204,70 @@ static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
             }
 
             if (convert_needed) {
-                    AVFilterContext *convert;
-                    AVFilter *filter;
-                    AVFilterLink *inlink, *outlink;
-                    char scale_args[256];
-                    char inst_name[30];
-
-                    /* couldn't merge format lists. auto-insert conversion filter */
-                    switch (link->type) {
-                    case AVMEDIA_TYPE_VIDEO:
-                        snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
-                                 scaler_count++);
-                        snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
-                        if ((ret = avfilter_graph_create_filter(&convert,
-                                                                avfilter_get_by_name("scale"),
-                                                                inst_name, scale_args, NULL,
-                                                                graph)) < 0)
-                            return ret;
-                        break;
-                    case AVMEDIA_TYPE_AUDIO:
-                        if (!(filter = avfilter_get_by_name("resample"))) {
-                            av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
-                                   "not present, cannot convert audio formats.\n");
-                            return AVERROR(EINVAL);
-                        }
-
-                        snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
-                                 resampler_count++);
-                        if ((ret = avfilter_graph_create_filter(&convert,
-                                                                avfilter_get_by_name("resample"),
-                                                                inst_name, NULL, NULL, graph)) < 0)
-                            return ret;
-                        break;
-                    default:
+                AVFilterContext *convert;
+                AVFilter *filter;
+                AVFilterLink *inlink, *outlink;
+                char scale_args[256];
+                char inst_name[30];
+
+                /* couldn't merge format lists. auto-insert conversion filter */
+                switch (link->type) {
+                case AVMEDIA_TYPE_VIDEO:
+                    snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
+                             scaler_count++);
+                    snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
+                    if ((ret = avfilter_graph_create_filter(&convert,
+                                                            avfilter_get_by_name("scale"),
+                                                            inst_name, scale_args, NULL,
+                                                            graph)) < 0)
+                        return ret;
+                    break;
+                case AVMEDIA_TYPE_AUDIO:
+                    if (!(filter = avfilter_get_by_name("resample"))) {
+                        av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
+                               "not present, cannot convert audio formats.\n");
                         return AVERROR(EINVAL);
                     }
 
-                    if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
-                        return ret;
-
-                    convert->filter->query_formats(convert);
-                    inlink  = convert->inputs[0];
-                    outlink = convert->outputs[0];
-                    if (!avfilter_merge_formats( inlink->in_formats,  inlink->out_formats) ||
-                        !avfilter_merge_formats(outlink->in_formats, outlink->out_formats))
-                        ret |= AVERROR(ENOSYS);
-                    if (inlink->type == AVMEDIA_TYPE_AUDIO &&
-                        (!ff_merge_samplerates(inlink->in_samplerates,
-                                               inlink->out_samplerates) ||
-                         !ff_merge_channel_layouts(inlink->in_channel_layouts,
-                                                   inlink->out_channel_layouts)))
-                        ret |= AVERROR(ENOSYS);
-                    if (outlink->type == AVMEDIA_TYPE_AUDIO &&
-                        (!ff_merge_samplerates(outlink->in_samplerates,
-                                               outlink->out_samplerates) ||
-                         !ff_merge_channel_layouts(outlink->in_channel_layouts,
-                                                   outlink->out_channel_layouts)))
-                        ret |= AVERROR(ENOSYS);
-
-                    if (ret < 0) {
-                        av_log(log_ctx, AV_LOG_ERROR,
-                               "Impossible to convert between the formats supported by the filter "
-                               "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
+                    snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
+                             resampler_count++);
+                    if ((ret = avfilter_graph_create_filter(&convert,
+                                                            avfilter_get_by_name("resample"),
+                                                            inst_name, NULL, NULL, graph)) < 0)
                         return ret;
-                    }
+                    break;
+                default:
+                    return AVERROR(EINVAL);
+                }
+
+                if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
+                    return ret;
+
+                convert->filter->query_formats(convert);
+                inlink  = convert->inputs[0];
+                outlink = convert->outputs[0];
+                if (!ff_merge_formats( inlink->in_formats,  inlink->out_formats) ||
+                    !ff_merge_formats(outlink->in_formats, outlink->out_formats))
+                    ret |= AVERROR(ENOSYS);
+                if (inlink->type == AVMEDIA_TYPE_AUDIO &&
+                    (!ff_merge_samplerates(inlink->in_samplerates,
+                                           inlink->out_samplerates) ||
+                     !ff_merge_channel_layouts(inlink->in_channel_layouts,
+                                               inlink->out_channel_layouts)))
+                    ret |= AVERROR(ENOSYS);
+                if (outlink->type == AVMEDIA_TYPE_AUDIO &&
+                    (!ff_merge_samplerates(outlink->in_samplerates,
+                                           outlink->out_samplerates) ||
+                     !ff_merge_channel_layouts(outlink->in_channel_layouts,
+                                               outlink->out_channel_layouts)))
+                    ret |= AVERROR(ENOSYS);
+
+                if (ret < 0) {
+                    av_log(log_ctx, AV_LOG_ERROR,
+                           "Impossible to convert between the formats supported by the filter "
+                           "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
+                    return ret;
+                }
             }
         }
     }
@@ -290,10 +303,10 @@ static int pick_format(AVFilterLink *link)
         link->channel_layout = link->in_channel_layouts->channel_layouts[0];
     }
 
-    avfilter_formats_unref(&link->in_formats);
-    avfilter_formats_unref(&link->out_formats);
-    avfilter_formats_unref(&link->in_samplerates);
-    avfilter_formats_unref(&link->out_samplerates);
+    ff_formats_unref(&link->in_formats);
+    ff_formats_unref(&link->out_formats);
+    ff_formats_unref(&link->in_samplerates);
+    ff_formats_unref(&link->out_samplerates);
     ff_channel_layouts_unref(&link->in_channel_layouts);
     ff_channel_layouts_unref(&link->out_channel_layouts);
 
@@ -340,9 +353,9 @@ static int reduce_formats_on_filter(AVFilterContext *filter)
     int i, j, k, ret = 0;
 
     REDUCE_FORMATS(int,      AVFilterFormats,        formats,         formats,
-                   format_count, avfilter_add_format);
+                   format_count, ff_add_format);
     REDUCE_FORMATS(int,      AVFilterFormats,        samplerates,     formats,
-                   format_count, avfilter_add_format);
+                   format_count, ff_add_format);
     REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
                    channel_layouts, nb_channel_layouts, ff_add_channel_layout);
 
@@ -461,6 +474,74 @@ static void swap_channel_layouts(AVFilterGraph *graph)
         swap_channel_layouts_on_filter(graph->filters[i]);
 }
 
+static void swap_sample_fmts_on_filter(AVFilterContext *filter)
+{
+    AVFilterLink *link = NULL;
+    int format, bps;
+    int i, j;
+
+    for (i = 0; i < filter->input_count; i++) {
+        link = filter->inputs[i];
+
+        if (link->type == AVMEDIA_TYPE_AUDIO &&
+            link->out_formats->format_count == 1)
+            break;
+    }
+    if (i == filter->input_count)
+        return;
+
+    format = link->out_formats->formats[0];
+    bps    = av_get_bytes_per_sample(format);
+
+    for (i = 0; i < filter->output_count; i++) {
+        AVFilterLink *outlink = filter->outputs[i];
+        int best_idx, best_score = INT_MIN;
+
+        if (outlink->type != AVMEDIA_TYPE_AUDIO ||
+            outlink->in_formats->format_count < 2)
+            continue;
+
+        for (j = 0; j < outlink->in_formats->format_count; j++) {
+            int out_format = outlink->in_formats->formats[j];
+            int out_bps    = av_get_bytes_per_sample(out_format);
+            int score;
+
+            if (av_get_packed_sample_fmt(out_format) == format ||
+                av_get_planar_sample_fmt(out_format) == format) {
+                best_idx   = j;
+                break;
+            }
+
+            /* for s32 and float prefer double to prevent loss of information */
+            if (bps == 4 && out_bps == 8) {
+                best_idx = j;
+                break;
+            }
+
+            /* prefer closest higher or equal bps */
+            score = -abs(out_bps - bps);
+            if (out_bps >= bps)
+                score += INT_MAX/2;
+
+            if (score > best_score) {
+                best_score = score;
+                best_idx   = j;
+            }
+        }
+        FFSWAP(int, outlink->in_formats->formats[0],
+               outlink->in_formats->formats[best_idx]);
+    }
+}
+
+static void swap_sample_fmts(AVFilterGraph *graph)
+{
+    int i;
+
+    for (i = 0; i < graph->filter_count; i++)
+        swap_sample_fmts_on_filter(graph->filters[i]);
+
+}
+
 static int pick_formats(AVFilterGraph *graph)
 {
     int i, j, ret;
@@ -478,7 +559,10 @@ static int pick_formats(AVFilterGraph *graph)
     return 0;
 }
 
-int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
+/**
+ * Configure the formats of all the links in the graph.
+ */
+static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
 {
     int ret;
 
@@ -491,8 +575,9 @@ int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
      * of format conversion inside filters */
     reduce_formats(graph);
 
-    /* for audio filters, ensure the best sample rate and channel layout
+    /* for audio filters, ensure the best format, sample rate and channel layout
      * is selected */
+    swap_sample_fmts(graph);
     swap_samplerates(graph);
     swap_channel_layouts(graph);
 
@@ -506,11 +591,11 @@ int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
 {
     int ret;
 
-    if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
+    if ((ret = graph_check_validity(graphctx, log_ctx)))
         return ret;
-    if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
+    if ((ret = graph_config_formats(graphctx, log_ctx)))
         return ret;
-    if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
+    if ((ret = graph_config_links(graphctx, log_ctx)))
         return ret;
 
     return 0;