diff mbox series

[FFmpeg-devel,v3,4/4] lavfi/buffersink: remove redundant channel layouts.

Message ID 20200824113652.255648-4-george@nsup.org
State Accepted
Commit 0d942357f6ea918473ab0140b8deb37af84b094d
Headers show
Series [FFmpeg-devel,v3,1/4] lavfi: regroup formats lists in a single structure. | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Nicolas George Aug. 24, 2020, 11:36 a.m. UTC
The channel_layouts and channel_counts options set what buffersink
is supposed to accept. If channel_counts contains 2, then stereo is
already accepted, there is no point in having it in channel_layouts
too. This was not properly documented until now, so only print a
warning.

Signed-off-by: Nicolas George <george@nsup.org>
---
 libavfilter/buffersink.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)


Faster algorithm.
diff mbox series

Patch

diff --git a/libavfilter/buffersink.c b/libavfilter/buffersink.c
index 76a46f6678..9338969bf1 100644
--- a/libavfilter/buffersink.c
+++ b/libavfilter/buffersink.c
@@ -62,6 +62,29 @@  typedef struct BufferSinkContext {
 
 #define NB_ITEMS(list) (list ## _size / sizeof(*list))
 
+static void cleanup_redundant_layouts(AVFilterContext *ctx)
+{
+    BufferSinkContext *buf = ctx->priv;
+    int nb_layouts = NB_ITEMS(buf->channel_layouts);
+    int nb_counts = NB_ITEMS(buf->channel_counts);
+    uint64_t counts = 0;
+    int i, lc, n;
+
+    for (i = 0; i < nb_counts; i++)
+        if (buf->channel_counts[i] < 64)
+            counts |= 1 << buf->channel_counts[i];
+    for (i = lc = 0; i < nb_layouts; i++) {
+        n = av_get_channel_layout_nb_channels(buf->channel_layouts[i]);
+        if (n < 64 && (counts & (1 << n)))
+            av_log(ctx, AV_LOG_WARNING,
+                   "Removing channel layout 0x%"PRIx64", redundant with %d channels\n",
+                   buf->channel_layouts[i], n);
+        else
+            buf->channel_layouts[lc++] = buf->channel_layouts[i];
+    }
+    buf->channel_layouts_size = lc * sizeof(*buf->channel_layouts);
+}
+
 int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
 {
     return av_buffersink_get_frame_flags(ctx, frame, 0);
@@ -253,6 +276,7 @@  static int asink_query_formats(AVFilterContext *ctx)
 
     if (buf->channel_layouts_size || buf->channel_counts_size ||
         buf->all_channel_counts) {
+        cleanup_redundant_layouts(ctx);
         for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)
             if ((ret = ff_add_channel_layout(&layouts, buf->channel_layouts[i])) < 0)
                 return ret;