Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 1/7] avutil/channel_layout: add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL
@ 2024-03-09 21:54 Marton Balint
  2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 2/7] avformat/mov_chan: simplify channel layout canonicalization Marton Balint
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Marton Balint @ 2024-03-09 21:54 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Marton Balint

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 doc/APIchanges             |  3 +++
 libavutil/channel_layout.c | 30 ++++++++++++++++++++++++++++++
 libavutil/channel_layout.h |  7 +++++++
 libavutil/version.h        |  2 +-
 4 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index cf58c8c5f0..a44c8e4f10 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2024-03-07
 
 API changes, most recent first:
 
+2024-03-xx - xxxxxxxxxx - lavu 59.2.100 - channel_layout.h
+  Add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL.
+
 2024-03-08 - xxxxxxxxxx - lavc 61.1.100 - avcodec.h
   Add AVCodecContext.[nb_]side_data_prefer_packet.
 
diff --git a/libavutil/channel_layout.c b/libavutil/channel_layout.c
index 8c1b3362f7..d3abb2dc42 100644
--- a/libavutil/channel_layout.c
+++ b/libavutil/channel_layout.c
@@ -553,6 +553,33 @@ static int ambisonic_order(const AVChannelLayout *channel_layout)
     return order;
 }
 
+static enum AVChannelOrder canonical_order(AVChannelLayout *channel_layout)
+{
+    int has_known_channel = 0;
+    int order;
+
+    if (channel_layout->order != AV_CHANNEL_ORDER_CUSTOM)
+        return channel_layout->order;
+
+    if (has_channel_names(channel_layout))
+        return AV_CHANNEL_ORDER_CUSTOM;
+
+    for (int i = 0; i < channel_layout->nb_channels && !has_known_channel; i++)
+        if (channel_layout->u.map[i].id != AV_CHAN_UNKNOWN)
+            has_known_channel = 1;
+    if (!has_known_channel)
+        return AV_CHANNEL_ORDER_UNSPEC;
+
+    if (masked_description(channel_layout, 0) > 0)
+        return AV_CHANNEL_ORDER_NATIVE;
+
+    order = ambisonic_order(channel_layout);
+    if (order >= 0 && masked_description(channel_layout, (order + 1) * (order + 1)) >= 0)
+        return AV_CHANNEL_ORDER_AMBISONIC;
+
+    return AV_CHANNEL_ORDER_CUSTOM;
+}
+
 /**
  * If the custom layout is n-th order standard-order ambisonic, with optional
  * extra non-diegetic channels at the end, write its string description in bp.
@@ -892,6 +919,9 @@ int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrde
     if (!av_channel_layout_check(channel_layout))
         return AVERROR(EINVAL);
 
+    if (flags & AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL)
+        order = canonical_order(channel_layout);
+
     if (channel_layout->order == order)
         return 0;
 
diff --git a/libavutil/channel_layout.h b/libavutil/channel_layout.h
index 10ffe74468..a1e9b08064 100644
--- a/libavutil/channel_layout.h
+++ b/libavutil/channel_layout.h
@@ -680,6 +680,13 @@ int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout
  */
 #define AV_CHANNEL_LAYOUT_RETYPE_FLAG_LOSSLESS (1 << 0)
 
+/**
+ * The specified retype target order is ignored and the simplest possible
+ * (canonical) order is used for which the input layout can be losslessy
+ * represented.
+ */
+#define AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL (1 << 1)
+
 /**
  * Change the AVChannelOrder of a channel layout.
  *
diff --git a/libavutil/version.h b/libavutil/version.h
index 09f8cdc292..57cad02ec0 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,7 +79,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  59
-#define LIBAVUTIL_VERSION_MINOR   1
+#define LIBAVUTIL_VERSION_MINOR   2
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
-- 
2.35.3

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [FFmpeg-devel] [PATCH 2/7] avformat/mov_chan: simplify channel layout canonicalization
  2024-03-09 21:54 [FFmpeg-devel] [PATCH 1/7] avutil/channel_layout: add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL Marton Balint
@ 2024-03-09 21:54 ` Marton Balint
  2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 3/7] avutil/tests/channel_layout: make printing results part of the tests Marton Balint
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Marton Balint @ 2024-03-09 21:54 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Marton Balint

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/mov_chan.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/libavformat/mov_chan.c b/libavformat/mov_chan.c
index 3e186b0837..ead3a9b91b 100644
--- a/libavformat/mov_chan.c
+++ b/libavformat/mov_chan.c
@@ -543,10 +543,8 @@ int ff_mov_read_chan(AVFormatContext *s, AVIOContext *pb, AVStream *st,
             ch_layout->u.map[i].id = mov_get_channel_id(label);
         }
 
-        ret = av_channel_layout_retype(ch_layout, AV_CHANNEL_ORDER_NATIVE, AV_CHANNEL_LAYOUT_RETYPE_FLAG_LOSSLESS);
-        if (ret == AVERROR(ENOSYS))
-            ret = av_channel_layout_retype(ch_layout, AV_CHANNEL_ORDER_UNSPEC, AV_CHANNEL_LAYOUT_RETYPE_FLAG_LOSSLESS);
-        if (ret < 0 && ret != AVERROR(ENOSYS))
+        ret = av_channel_layout_retype(ch_layout, 0, AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL);
+        if (ret < 0)
             goto out;
     } else {
         uint64_t mask = mov_get_channel_layout(layout_tag, bitmap);
@@ -843,10 +841,8 @@ int ff_mov_read_chnl(AVFormatContext *s, AVIOContext *pb, AVStream *st)
                 ch_layout->u.map[i].id = channel;
             }
 
-            ret = av_channel_layout_retype(ch_layout, AV_CHANNEL_ORDER_NATIVE, AV_CHANNEL_LAYOUT_RETYPE_FLAG_LOSSLESS);
-            if (ret == AVERROR(ENOSYS))
-                ret = av_channel_layout_retype(ch_layout, AV_CHANNEL_ORDER_UNSPEC, AV_CHANNEL_LAYOUT_RETYPE_FLAG_LOSSLESS);
-            if (ret < 0 && ret != AVERROR(ENOSYS))
+            ret = av_channel_layout_retype(ch_layout, 0, AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL);
+            if (ret < 0)
                 return ret;
         } else {
             uint64_t omitted_channel_map = avio_rb64(pb);
-- 
2.35.3

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [FFmpeg-devel] [PATCH 3/7] avutil/tests/channel_layout: make printing results part of the tests
  2024-03-09 21:54 [FFmpeg-devel] [PATCH 1/7] avutil/channel_layout: add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL Marton Balint
  2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 2/7] avformat/mov_chan: simplify channel layout canonicalization Marton Balint
@ 2024-03-09 21:54 ` Marton Balint
  2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 4/7] avutil/tests/channel_layout: add some av_channel_from_string and av_channel_layout_from_string tests Marton Balint
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Marton Balint @ 2024-03-09 21:54 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Marton Balint

Deduplicates a lot of code.

Some minor differences (mostly white space and inconsistent use of quotes) are
expected in the fate tests, there was no point aiming for exactly the same
formatting.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavutil/tests/channel_layout.c | 283 +++++++++++--------------------
 tests/ref/fate/channel_layout    | 176 +++++++++----------
 2 files changed, 186 insertions(+), 273 deletions(-)

diff --git a/libavutil/tests/channel_layout.c b/libavutil/tests/channel_layout.c
index 23bb6009fb..d62b69a5db 100644
--- a/libavutil/tests/channel_layout.c
+++ b/libavutil/tests/channel_layout.c
@@ -162,39 +162,50 @@ static const char *channel_layout_retype(AVChannelLayout *layout, AVBPrint *bp,
 }
 
 #define CHANNEL_NAME(x)                                                    \
-    channel_name(&bp, (x));
+    channel_name(&bp, (x));                                                \
+    printf("With %-32s %14s\n", AV_STRINGIFY(x)":", bp.str)
 
 #define CHANNEL_DESCRIPTION(x)                                             \
-    channel_description(&bp, (x));
+    channel_description(&bp, (x));                                         \
+    printf("With %-23s %23s\n", AV_STRINGIFY(x)":", bp.str);
+
+#define CHANNEL_FROM_STRING(x)                                             \
+    printf("With %-38s %8d\n", AV_STRINGIFY(x)":", av_channel_from_string(x))
 
 #define CHANNEL_LAYOUT_FROM_MASK(x)                                        \
     channel_layout_from_mask(&layout, &bp, (x));
 
 #define CHANNEL_LAYOUT_FROM_STRING(x)                                      \
-    channel_layout_from_string(&layout, &bp, (x));
+    channel_layout_from_string(&layout, &bp, (x));                         \
+    printf("With \"%s\":%*s %32s\n", x, strlen(x) > 32 ? 0 : 32 - (int)strlen(x), "", bp.str);
 
-#define CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(x)                               \
+#define CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(l, x)                            \
     ret = av_channel_layout_channel_from_index(&layout, x);                \
     if (ret < 0)                                                           \
-        ret = -1
+        ret = -1;                                                          \
+    printf("On \"%s\" layout with %2d: %8d\n", l,  x, ret)
 
-#define CHANNEL_LAYOUT_SUBSET(x)                                           \
-    mask = av_channel_layout_subset(&layout, x)
+#define CHANNEL_LAYOUT_SUBSET(l, xstr, x)                                  \
+    mask = av_channel_layout_subset(&layout, x);                           \
+    printf("On \"%s\" layout with %-22s 0x%"PRIx64"\n", l, xstr, mask)
 
-#define CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(x)                               \
+#define CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(l, x)                            \
     ret = av_channel_layout_index_from_channel(&layout, x);                \
     if (ret < 0)                                                           \
-        ret = -1
+        ret = -1;                                                          \
+    printf("On \"%s\" layout with %-23s %3d\n", l, AV_STRINGIFY(x)":", ret)
 
-#define CHANNEL_LAYOUT_CHANNEL_FROM_STRING(x)                              \
+#define CHANNEL_LAYOUT_CHANNEL_FROM_STRING(l, x)                           \
     ret = av_channel_layout_channel_from_string(&layout, x);               \
     if (ret < 0)                                                           \
-        ret = -1
+        ret = -1;                                                          \
+    printf("On \"%s\" layout with %-21s %3d\n", bp.str, AV_STRINGIFY(x)":", ret);
 
-#define CHANNEL_LAYOUT_INDEX_FROM_STRING(x)                                \
+#define CHANNEL_LAYOUT_INDEX_FROM_STRING(l, x)                             \
     ret = av_channel_layout_index_from_string(&layout, x);                 \
     if (ret < 0)                                                           \
-        ret = -1
+        ret = -1;                                                          \
+    printf("On \"%s\" layout with %-20s %3d\n", l, AV_STRINGIFY(x)":", ret);
 
 int main(void)
 {
@@ -226,152 +237,94 @@ int main(void)
 
     printf("\nTesting av_channel_name\n");
     CHANNEL_NAME(AV_CHAN_FRONT_LEFT);
-    printf("With AV_CHAN_FRONT_LEFT: %27s\n", bp.str);
     CHANNEL_NAME(AV_CHAN_FRONT_RIGHT);
-    printf("With AV_CHAN_FRONT_RIGHT: %26s\n", bp.str);
     CHANNEL_NAME(63);
-    printf("With 63: %43s\n", bp.str);
     CHANNEL_NAME(AV_CHAN_AMBISONIC_BASE);
-    printf("With AV_CHAN_AMBISONIC_BASE: %23s\n", bp.str);
     CHANNEL_NAME(AV_CHAN_AMBISONIC_END);
-    printf("With AV_CHAN_AMBISONIC_END: %24s\n", bp.str);
 
     printf("Testing av_channel_description\n");
     CHANNEL_DESCRIPTION(AV_CHAN_FRONT_LEFT);
-    printf("With AV_CHAN_FRONT_LEFT: %27s\n", bp.str);
     CHANNEL_DESCRIPTION(AV_CHAN_FRONT_RIGHT);
-    printf("With AV_CHAN_FRONT_RIGHT: %26s\n", bp.str);
     CHANNEL_DESCRIPTION(63);
-    printf("With 63: %43s\n", bp.str);
     CHANNEL_DESCRIPTION(AV_CHAN_AMBISONIC_BASE);
-    printf("With AV_CHAN_AMBISONIC_BASE: %23s\n", bp.str);
     CHANNEL_DESCRIPTION(AV_CHAN_AMBISONIC_END);
-    printf("With AV_CHAN_AMBISONIC_END: %24s\n", bp.str);
 
     printf("\nTesting av_channel_from_string\n");
-    printf("With \"FL\": %41d\n", av_channel_from_string("FL"));
-    printf("With \"FR\": %41d\n", av_channel_from_string("FR"));
-    printf("With \"USR63\": %38d\n", av_channel_from_string("USR63"));
-    printf("With \"AMBI0\": %38d\n", av_channel_from_string("AMBI0"));
-    printf("With \"AMBI1023\": %35d\n", av_channel_from_string("AMBI1023"));
+    CHANNEL_FROM_STRING("FL");
+    CHANNEL_FROM_STRING("FR");
+    CHANNEL_FROM_STRING("USR63");
+    CHANNEL_FROM_STRING("AMBI0");
+    CHANNEL_FROM_STRING("AMBI1023");
 
     printf("\n==Native layouts==\n");
 
     printf("\nTesting av_channel_layout_from_string\n");
     CHANNEL_LAYOUT_FROM_STRING("0x3f");
-    printf("With \"0x3f\": %39s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("63");
-    printf("With \"63\": %41s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("6c");
-    printf("With \"6c\": %41s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("6C");
-    printf("With \"6C\": %41s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("6 channels");
-    printf("With \"6 channels\": %33s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("6 channels (FL+FR+FC+LFE+BL+BR)");
-    printf("With \"6 channels (FL+FR+FC+LFE+BL+BR)\": %12s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("FL+FR+FC+LFE+BL+BR");
-    printf("With \"FL+FR+FC+LFE+BL+BR\": %25s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("5.1");
-    printf("With \"5.1\": %40s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("FL+FR+USR63");
-    printf("With \"FL+FR+USR63\": %32s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("FL+FR+FC+LFE+SL+SR");
-    printf("With \"FL+FR+FC+LFE+SL+SR\": %25s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("5.1(side)");
-    printf("With \"5.1(side)\": %34s\n", bp.str);
 
     printf("\nTesting av_channel_layout_from_mask\n");
     CHANNEL_LAYOUT_FROM_MASK(AV_CH_LAYOUT_5POINT1);
     printf("With AV_CH_LAYOUT_5POINT1: %25s\n", bp.str);
 
     printf("\nTesting av_channel_layout_channel_from_index\n");
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(0);
-    printf("On 5.1(side) layout with 0: %24d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(1);
-    printf("On 5.1(side) layout with 1: %24d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(2);
-    printf("On 5.1(side) layout with 2: %24d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(3);
-    printf("On 5.1(side) layout with 3: %24d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(4);
-    printf("On 5.1(side) layout with 4: %24d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(5);
-    printf("On 5.1(side) layout with 5: %24d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(6);
-    printf("On 5.1(side) layout with 6: %24d\n", ret);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 0);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 1);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 2);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 3);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 4);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 5);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 6);
 
     printf("\nTesting av_channel_layout_index_from_channel\n");
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(AV_CHAN_FRONT_LEFT);
-    printf("On 5.1(side) layout with AV_CHAN_FRONT_LEFT: %7d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(AV_CHAN_FRONT_RIGHT);
-    printf("On 5.1(side) layout with AV_CHAN_FRONT_RIGHT: %6d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(AV_CHAN_FRONT_CENTER);
-    printf("On 5.1(side) layout with AV_CHAN_FRONT_CENTER: %5d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(AV_CHAN_LOW_FREQUENCY);
-    printf("On 5.1(side) layout with AV_CHAN_LOW_FREQUENCY: %4d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(AV_CHAN_SIDE_LEFT);
-    printf("On 5.1(side) layout with AV_CHAN_SIDE_LEFT: %8d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(AV_CHAN_SIDE_RIGHT);
-    printf("On 5.1(side) layout with AV_CHAN_SIDE_RIGHT: %7d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(AV_CHAN_BACK_CENTER);
-    printf("On 5.1(side) layout with AV_CHAN_BACK_CENTER: %6d\n", ret);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, AV_CHAN_FRONT_LEFT);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, AV_CHAN_FRONT_RIGHT);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, AV_CHAN_FRONT_CENTER);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, AV_CHAN_LOW_FREQUENCY);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, AV_CHAN_SIDE_LEFT);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, AV_CHAN_SIDE_RIGHT);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, AV_CHAN_BACK_CENTER);
 
     printf("\nTesting av_channel_layout_channel_from_string\n");
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("FL");
-    printf("On 5.1(side) layout with \"FL\": %21d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("FR");
-    printf("On 5.1(side) layout with \"FR\": %21d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("FC");
-    printf("On 5.1(side) layout with \"FC\": %21d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("LFE");
-    printf("On 5.1(side) layout with \"LFE\": %20d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("SL");
-    printf("On 5.1(side) layout with \"SL\": %21d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("SR");
-    printf("On 5.1(side) layout with \"SR\": %21d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("BC");
-    printf("On 5.1(side) layout with \"BC\": %21d\n", ret);
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "FL");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "FR");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "FC");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "LFE");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "SL");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "SR");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "BC");
 
     printf("\nTesting av_channel_layout_index_from_string\n");
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("FL");
-    printf("On 5.1(side) layout with \"FL\": %21d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("FR");
-    printf("On 5.1(side) layout with \"FR\": %21d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("FC");
-    printf("On 5.1(side) layout with \"FC\": %21d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("LFE");
-    printf("On 5.1(side) layout with \"LFE\": %20d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("SL");
-    printf("On 5.1(side) layout with \"SL\": %21d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("SR");
-    printf("On 5.1(side) layout with \"SR\": %21d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("BC");
-    printf("On 5.1(side) layout with \"BC\": %21d\n", ret);
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "FL");
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "FR");
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "FC");
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "LFE");
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "SL");
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "SR");
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "BC");
 
     printf("\nTesting av_channel_layout_subset\n");
-    CHANNEL_LAYOUT_SUBSET(AV_CH_LAYOUT_STEREO);
-    printf("On 5.1(side) layout with AV_CH_LAYOUT_STEREO:    0x%"PRIx64"\n", mask);
-    CHANNEL_LAYOUT_SUBSET(AV_CH_LAYOUT_2POINT1);
-    printf("On 5.1(side) layout with AV_CH_LAYOUT_2POINT1:   0x%"PRIx64"\n", mask);
-    CHANNEL_LAYOUT_SUBSET(AV_CH_LAYOUT_4POINT1);
-    printf("On 5.1(side) layout with AV_CH_LAYOUT_4POINT1:   0x%"PRIx64"\n", mask);
+    CHANNEL_LAYOUT_SUBSET(bp.str, "AV_CH_LAYOUT_STEREO:",  AV_CH_LAYOUT_STEREO);
+    CHANNEL_LAYOUT_SUBSET(bp.str, "AV_CH_LAYOUT_2POINT1:", AV_CH_LAYOUT_2POINT1);
+    CHANNEL_LAYOUT_SUBSET(bp.str, "AV_CH_LAYOUT_4POINT1:", AV_CH_LAYOUT_4POINT1);
 
     printf("\n==Custom layouts==\n");
 
     printf("\nTesting av_channel_layout_from_string\n");
     CHANNEL_LAYOUT_FROM_STRING("FL+FR+FC+BL+BR+LFE");
-    printf("With \"FL+FR+FC+BL+BR+LFE\": %34s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("2 channels (FR+FL)");
-    printf("With \"2 channels (FR+FL)\": %34s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("ambisonic 1+FR+FL");
-    printf("With \"ambisonic 1+FR+FL\": %35s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("ambisonic 2+FC@Foo");
-    printf("With \"ambisonic 2+FC@Foo\": %34s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("FL@Foo+FR@Bar");
-    printf("With \"FL@Foo+FR@Bar\": %39s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("FR+FL@Foo+USR63@Foo");
-    printf("With \"FR+FL@Foo+USR63@Foo\": %33s\n", bp.str);
 
     ret = av_channel_layout_copy(&layout2, &layout);
     if (ret < 0) {
@@ -383,104 +336,64 @@ int main(void)
         printf("Channel layout and its copy compare unequal; ret: %d\n", ret);
 
     printf("\nTesting av_channel_layout_index_from_string\n");
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("FR");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FR\": %18d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("FL");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FL\": %18d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("USR63");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"USR63\": %15d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("Foo");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"Foo\": %17d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("@Foo");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"@Foo\": %16d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("FR@Foo");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FR@Foo\": %14d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("FL@Foo");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FL@Foo\": %14d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("USR63@Foo");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"USR63@Foo\": %11d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_STRING("BC");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"BC\": %18d\n", ret);
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "FR");
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "FL");
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "USR63");
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "Foo");
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "@Foo");
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "FR@Foo");
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "FL@Foo");
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "USR63@Foo");
+    CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "BC");
 
     printf("\nTesting av_channel_layout_channel_from_string\n");
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("FR");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FR\": %18d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("FL");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FL\": %18d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("USR63");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"USR63\": %15d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("Foo");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"Foo\": %17d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("@Foo");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"@Foo\": %16d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("FR@Foo");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FR@Foo\": %14d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("FL@Foo");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FL@Foo\": %14d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("USR63@Foo");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"USR63@Foo\": %11d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_STRING("BC");
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"BC\": %18d\n", ret);
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "FR");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "FL");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "USR63");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "Foo");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "@Foo");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "FR@Foo");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "FL@Foo");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "USR63@Foo");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "BC");
 
     printf("\nTesting av_channel_layout_index_from_channel\n");
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(AV_CHAN_FRONT_RIGHT);
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with AV_CHAN_FRONT_RIGHT: %3d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(AV_CHAN_FRONT_LEFT);
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with AV_CHAN_FRONT_LEFT: %4d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(63);
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with 63: %20d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(AV_CHAN_BACK_CENTER);
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with AV_CHAN_BACK_CENTER: %3d\n", ret);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, AV_CHAN_FRONT_RIGHT);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, AV_CHAN_FRONT_LEFT);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, 63);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, AV_CHAN_BACK_CENTER);
 
     printf("\nTesting av_channel_layout_channel_from_index\n");
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(0);
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with 0: %21d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(1);
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with 1: %21d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(2);
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with 2: %21d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(3);
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with 3: %21d\n", ret);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 0);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 1);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 2);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 3);
 
     printf("\nTesting av_channel_layout_subset\n");
-    CHANNEL_LAYOUT_SUBSET(AV_CH_LAYOUT_STEREO);
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with AV_CH_LAYOUT_STEREO: 0x%"PRIx64"\n", mask);
-    CHANNEL_LAYOUT_SUBSET(AV_CH_LAYOUT_QUAD);
-    printf("On \"FR+FL@Foo+USR63@Foo\" layout with AV_CH_LAYOUT_QUAD:   0x%"PRIx64"\n", mask);
+    CHANNEL_LAYOUT_SUBSET(bp.str, "AV_CH_LAYOUT_STEREO:", AV_CH_LAYOUT_STEREO);
+    CHANNEL_LAYOUT_SUBSET(bp.str, "AV_CH_LAYOUT_QUAD:", AV_CH_LAYOUT_QUAD);
 
     printf("\n==Ambisonic layouts==\n");
 
     printf("\nTesting av_channel_layout_from_string\n");
     CHANNEL_LAYOUT_FROM_STRING("ambisonic 1");
-    printf("With \"ambisonic 1\": %41s\n", bp.str);
     CHANNEL_LAYOUT_FROM_STRING("ambisonic 2+stereo");
-    printf("With \"ambisonic 2+stereo\": %34s\n", bp.str);
 
     printf("\nTesting av_channel_layout_index_from_channel\n");
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(AV_CHAN_AMBISONIC_BASE);
-    printf("On \"ambisonic 2+stereo\" layout with AV_CHAN_AMBISONIC_BASE: %d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(AV_CHAN_FRONT_LEFT);
-    printf("On \"ambisonic 2+stereo\" layout with AV_CHAN_FRONT_LEFT: %5d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(AV_CHAN_FRONT_RIGHT);
-    printf("On \"ambisonic 2+stereo\" layout with AV_CHAN_FRONT_RIGHT: %4d\n", ret);
-    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(AV_CHAN_BACK_CENTER);
-    printf("On \"ambisonic 2+stereo\" layout with AV_CHAN_BACK_CENTER: %4d\n", ret);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, AV_CHAN_AMBISONIC_BASE);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, AV_CHAN_FRONT_LEFT);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, AV_CHAN_FRONT_RIGHT);
+    CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(bp.str, AV_CHAN_BACK_CENTER);
 
     printf("\nTesting av_channel_layout_channel_from_index\n");
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(0);
-    printf("On \"ambisonic 2+stereo\" layout with 0: %22d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(9);
-    printf("On \"ambisonic 2+stereo\" layout with 9: %22d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(10);
-    printf("On \"ambisonic 2+stereo\" layout with 10: %21d\n", ret);
-    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(11);
-    printf("On \"ambisonic 2+stereo\" layout with 11: %21d\n", ret);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 0);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 9);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 10);
+    CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(bp.str, 11);
 
     printf("\nTesting av_channel_layout_subset\n");
-    CHANNEL_LAYOUT_SUBSET(AV_CH_LAYOUT_STEREO);
-    printf("On \"ambisonic 2+stereo\" layout with AV_CH_LAYOUT_STEREO:  0x%"PRIx64"\n", mask);
-    CHANNEL_LAYOUT_SUBSET(AV_CH_LAYOUT_QUAD);
-    printf("On \"ambisonic 2+stereo\" layout with AV_CH_LAYOUT_QUAD:    0x%"PRIx64"\n", mask);
+    CHANNEL_LAYOUT_SUBSET(bp.str, "AV_CH_LAYOUT_STEREO:", AV_CH_LAYOUT_STEREO);
+    CHANNEL_LAYOUT_SUBSET(bp.str, "AV_CH_LAYOUT_QUAD:", AV_CH_LAYOUT_QUAD);
 
     av_channel_layout_uninit(&layout);
     av_channel_layout_uninit(&layout2);
diff --git a/tests/ref/fate/channel_layout b/tests/ref/fate/channel_layout
index 466fa78d9e..b98ccdb0f0 100644
--- a/tests/ref/fate/channel_layout
+++ b/tests/ref/fate/channel_layout
@@ -60,131 +60,131 @@ With "AMBI1023":                                2047
 ==Native layouts==
 
 Testing av_channel_layout_from_string
-With "0x3f":                                     5.1
-With "63":                                       5.1
-With "6c":                                       5.1
-With "6C":                                6 channels
-With "6 channels":                        6 channels
-With "6 channels (FL+FR+FC+LFE+BL+BR)":          5.1
-With "FL+FR+FC+LFE+BL+BR":                       5.1
-With "5.1":                                      5.1
-With "FL+FR+USR63":         3 channels (FL+FR+USR63)
-With "FL+FR+FC+LFE+SL+SR":                 5.1(side)
-With "5.1(side)":                          5.1(side)
+With "0x3f":                                                          5.1
+With "63":                                                            5.1
+With "6c":                                                            5.1
+With "6C":                                                     6 channels
+With "6 channels":                                             6 channels
+With "6 channels (FL+FR+FC+LFE+BL+BR)":                               5.1
+With "FL+FR+FC+LFE+BL+BR":                                            5.1
+With "5.1":                                                           5.1
+With "FL+FR+USR63":                              3 channels (FL+FR+USR63)
+With "FL+FR+FC+LFE+SL+SR":                                      5.1(side)
+With "5.1(side)":                                               5.1(side)
 
 Testing av_channel_layout_from_mask
 With AV_CH_LAYOUT_5POINT1:                 5.1(side)
 
 Testing av_channel_layout_channel_from_index
-On 5.1(side) layout with 0:                        0
-On 5.1(side) layout with 1:                        1
-On 5.1(side) layout with 2:                        2
-On 5.1(side) layout with 3:                        3
-On 5.1(side) layout with 4:                        9
-On 5.1(side) layout with 5:                       10
-On 5.1(side) layout with 6:                       -1
+On "5.1(side)" layout with  0:        0
+On "5.1(side)" layout with  1:        1
+On "5.1(side)" layout with  2:        2
+On "5.1(side)" layout with  3:        3
+On "5.1(side)" layout with  4:        9
+On "5.1(side)" layout with  5:       10
+On "5.1(side)" layout with  6:       -1
 
 Testing av_channel_layout_index_from_channel
-On 5.1(side) layout with AV_CHAN_FRONT_LEFT:       0
-On 5.1(side) layout with AV_CHAN_FRONT_RIGHT:      1
-On 5.1(side) layout with AV_CHAN_FRONT_CENTER:     2
-On 5.1(side) layout with AV_CHAN_LOW_FREQUENCY:    3
-On 5.1(side) layout with AV_CHAN_SIDE_LEFT:        4
-On 5.1(side) layout with AV_CHAN_SIDE_RIGHT:       5
-On 5.1(side) layout with AV_CHAN_BACK_CENTER:     -1
+On "5.1(side)" layout with AV_CHAN_FRONT_LEFT:       0
+On "5.1(side)" layout with AV_CHAN_FRONT_RIGHT:      1
+On "5.1(side)" layout with AV_CHAN_FRONT_CENTER:     2
+On "5.1(side)" layout with AV_CHAN_LOW_FREQUENCY:    3
+On "5.1(side)" layout with AV_CHAN_SIDE_LEFT:        4
+On "5.1(side)" layout with AV_CHAN_SIDE_RIGHT:       5
+On "5.1(side)" layout with AV_CHAN_BACK_CENTER:     -1
 
 Testing av_channel_layout_channel_from_string
-On 5.1(side) layout with "FL":                     0
-On 5.1(side) layout with "FR":                     1
-On 5.1(side) layout with "FC":                     2
-On 5.1(side) layout with "LFE":                    3
-On 5.1(side) layout with "SL":                     9
-On 5.1(side) layout with "SR":                    10
-On 5.1(side) layout with "BC":                    -1
+On "5.1(side)" layout with "FL":                   0
+On "5.1(side)" layout with "FR":                   1
+On "5.1(side)" layout with "FC":                   2
+On "5.1(side)" layout with "LFE":                  3
+On "5.1(side)" layout with "SL":                   9
+On "5.1(side)" layout with "SR":                  10
+On "5.1(side)" layout with "BC":                  -1
 
 Testing av_channel_layout_index_from_string
-On 5.1(side) layout with "FL":                     0
-On 5.1(side) layout with "FR":                     1
-On 5.1(side) layout with "FC":                     2
-On 5.1(side) layout with "LFE":                    3
-On 5.1(side) layout with "SL":                     4
-On 5.1(side) layout with "SR":                     5
-On 5.1(side) layout with "BC":                    -1
+On "5.1(side)" layout with "FL":                  0
+On "5.1(side)" layout with "FR":                  1
+On "5.1(side)" layout with "FC":                  2
+On "5.1(side)" layout with "LFE":                 3
+On "5.1(side)" layout with "SL":                  4
+On "5.1(side)" layout with "SR":                  5
+On "5.1(side)" layout with "BC":                 -1
 
 Testing av_channel_layout_subset
-On 5.1(side) layout with AV_CH_LAYOUT_STEREO:    0x3
-On 5.1(side) layout with AV_CH_LAYOUT_2POINT1:   0xb
-On 5.1(side) layout with AV_CH_LAYOUT_4POINT1:   0xf
+On "5.1(side)" layout with AV_CH_LAYOUT_STEREO:   0x3
+On "5.1(side)" layout with AV_CH_LAYOUT_2POINT1:  0xb
+On "5.1(side)" layout with AV_CH_LAYOUT_4POINT1:  0xf
 
 ==Custom layouts==
 
 Testing av_channel_layout_from_string
-With "FL+FR+FC+BL+BR+LFE":    6 channels (FL+FR+FC+BL+BR+LFE)
-With "2 channels (FR+FL)":                 2 channels (FR+FL)
-With "ambisonic 1+FR+FL":      ambisonic 1+2 channels (FR+FL)
-With "ambisonic 2+FC@Foo":    ambisonic 2+1 channels (FC@Foo)
-With "FL@Foo+FR@Bar":              2 channels (FL@Foo+FR@Bar)
-With "FR+FL@Foo+USR63@Foo":  3 channels (FR+FL@Foo+USR63@Foo)
+With "FL+FR+FC+BL+BR+LFE":                6 channels (FL+FR+FC+BL+BR+LFE)
+With "2 channels (FR+FL)":                             2 channels (FR+FL)
+With "ambisonic 1+FR+FL":                  ambisonic 1+2 channels (FR+FL)
+With "ambisonic 2+FC@Foo":                ambisonic 2+1 channels (FC@Foo)
+With "FL@Foo+FR@Bar":                          2 channels (FL@Foo+FR@Bar)
+With "FR+FL@Foo+USR63@Foo":              3 channels (FR+FL@Foo+USR63@Foo)
 
 Testing av_channel_layout_index_from_string
-On "FR+FL@Foo+USR63@Foo" layout with "FR":                  0
-On "FR+FL@Foo+USR63@Foo" layout with "FL":                  1
-On "FR+FL@Foo+USR63@Foo" layout with "USR63":               2
-On "FR+FL@Foo+USR63@Foo" layout with "Foo":                -1
-On "FR+FL@Foo+USR63@Foo" layout with "@Foo":                1
-On "FR+FL@Foo+USR63@Foo" layout with "FR@Foo":             -1
-On "FR+FL@Foo+USR63@Foo" layout with "FL@Foo":              1
-On "FR+FL@Foo+USR63@Foo" layout with "USR63@Foo":           2
-On "FR+FL@Foo+USR63@Foo" layout with "BC":                 -1
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "FR":                  0
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "FL":                  1
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "USR63":               2
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "Foo":                -1
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "@Foo":                1
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "FR@Foo":             -1
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "FL@Foo":              1
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "USR63@Foo":           2
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "BC":                 -1
 
 Testing av_channel_layout_channel_from_string
-On "FR+FL@Foo+USR63@Foo" layout with "FR":                  1
-On "FR+FL@Foo+USR63@Foo" layout with "FL":                  0
-On "FR+FL@Foo+USR63@Foo" layout with "USR63":              63
-On "FR+FL@Foo+USR63@Foo" layout with "Foo":                -1
-On "FR+FL@Foo+USR63@Foo" layout with "@Foo":                0
-On "FR+FL@Foo+USR63@Foo" layout with "FR@Foo":             -1
-On "FR+FL@Foo+USR63@Foo" layout with "FL@Foo":              0
-On "FR+FL@Foo+USR63@Foo" layout with "USR63@Foo":          63
-On "FR+FL@Foo+USR63@Foo" layout with "BC":                 -1
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "FR":                   1
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "FL":                   0
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "USR63":               63
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "Foo":                 -1
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "@Foo":                 0
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "FR@Foo":              -1
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "FL@Foo":               0
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "USR63@Foo":           63
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with "BC":                  -1
 
 Testing av_channel_layout_index_from_channel
-On "FR+FL@Foo+USR63@Foo" layout with AV_CHAN_FRONT_RIGHT:   0
-On "FR+FL@Foo+USR63@Foo" layout with AV_CHAN_FRONT_LEFT:    1
-On "FR+FL@Foo+USR63@Foo" layout with 63:                    2
-On "FR+FL@Foo+USR63@Foo" layout with AV_CHAN_BACK_CENTER:  -1
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with AV_CHAN_FRONT_RIGHT:      0
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with AV_CHAN_FRONT_LEFT:       1
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with 63:                       2
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with AV_CHAN_BACK_CENTER:     -1
 
 Testing av_channel_layout_channel_from_index
-On "FR+FL@Foo+USR63@Foo" layout with 0:                     1
-On "FR+FL@Foo+USR63@Foo" layout with 1:                     0
-On "FR+FL@Foo+USR63@Foo" layout with 2:                    63
-On "FR+FL@Foo+USR63@Foo" layout with 3:                    -1
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with  0:        1
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with  1:        0
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with  2:       63
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with  3:       -1
 
 Testing av_channel_layout_subset
-On "FR+FL@Foo+USR63@Foo" layout with AV_CH_LAYOUT_STEREO: 0x3
-On "FR+FL@Foo+USR63@Foo" layout with AV_CH_LAYOUT_QUAD:   0x3
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with AV_CH_LAYOUT_STEREO:   0x3
+On "3 channels (FR+FL@Foo+USR63@Foo)" layout with AV_CH_LAYOUT_QUAD:     0x3
 
 ==Ambisonic layouts==
 
 Testing av_channel_layout_from_string
-With "ambisonic 1":                               ambisonic 1
-With "ambisonic 2+stereo":                 ambisonic 2+stereo
+With "ambisonic 1":                                           ambisonic 1
+With "ambisonic 2+stereo":                             ambisonic 2+stereo
 
 Testing av_channel_layout_index_from_channel
-On "ambisonic 2+stereo" layout with AV_CHAN_AMBISONIC_BASE: 0
-On "ambisonic 2+stereo" layout with AV_CHAN_FRONT_LEFT:     9
-On "ambisonic 2+stereo" layout with AV_CHAN_FRONT_RIGHT:   10
-On "ambisonic 2+stereo" layout with AV_CHAN_BACK_CENTER:   -1
+On "ambisonic 2+stereo" layout with AV_CHAN_AMBISONIC_BASE:   0
+On "ambisonic 2+stereo" layout with AV_CHAN_FRONT_LEFT:       9
+On "ambisonic 2+stereo" layout with AV_CHAN_FRONT_RIGHT:     10
+On "ambisonic 2+stereo" layout with AV_CHAN_BACK_CENTER:     -1
 
 Testing av_channel_layout_channel_from_index
-On "ambisonic 2+stereo" layout with 0:                   1024
-On "ambisonic 2+stereo" layout with 9:                      0
-On "ambisonic 2+stereo" layout with 10:                     1
-On "ambisonic 2+stereo" layout with 11:                    -1
+On "ambisonic 2+stereo" layout with  0:     1024
+On "ambisonic 2+stereo" layout with  9:        0
+On "ambisonic 2+stereo" layout with 10:        1
+On "ambisonic 2+stereo" layout with 11:       -1
 
 Testing av_channel_layout_subset
-On "ambisonic 2+stereo" layout with AV_CH_LAYOUT_STEREO:  0x3
-On "ambisonic 2+stereo" layout with AV_CH_LAYOUT_QUAD:    0x3
+On "ambisonic 2+stereo" layout with AV_CH_LAYOUT_STEREO:   0x3
+On "ambisonic 2+stereo" layout with AV_CH_LAYOUT_QUAD:     0x3
 
 Testing av_channel_layout_retype
 With "FL@Boo": CUSTOM (1 channels (FL@Boo))
-- 
2.35.3

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [FFmpeg-devel] [PATCH 4/7] avutil/tests/channel_layout: add some av_channel_from_string and av_channel_layout_from_string tests
  2024-03-09 21:54 [FFmpeg-devel] [PATCH 1/7] avutil/channel_layout: add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL Marton Balint
  2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 2/7] avformat/mov_chan: simplify channel layout canonicalization Marton Balint
  2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 3/7] avutil/tests/channel_layout: make printing results part of the tests Marton Balint
@ 2024-03-09 21:54 ` Marton Balint
  2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 5/7] avutil/channel_layout: factorize parsing list of channel names Marton Balint
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Marton Balint @ 2024-03-09 21:54 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Marton Balint

We lacked tests which supposed to fail, and there are some which should fail
but right now it does not. This will be fixed in a later commit.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavutil/tests/channel_layout.c | 26 ++++++++++++++++++++++++++
 tests/ref/fate/channel_layout    | 26 ++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)

diff --git a/libavutil/tests/channel_layout.c b/libavutil/tests/channel_layout.c
index d62b69a5db..14d36c71b5 100644
--- a/libavutil/tests/channel_layout.c
+++ b/libavutil/tests/channel_layout.c
@@ -255,6 +255,11 @@ int main(void)
     CHANNEL_FROM_STRING("USR63");
     CHANNEL_FROM_STRING("AMBI0");
     CHANNEL_FROM_STRING("AMBI1023");
+    CHANNEL_FROM_STRING("AMBI1024");
+    CHANNEL_FROM_STRING("Dummy");
+    CHANNEL_FROM_STRING("FL@Foo");
+    CHANNEL_FROM_STRING("Foo@FL");
+    CHANNEL_FROM_STRING("@FL");
 
     printf("\n==Native layouts==\n");
 
@@ -301,6 +306,9 @@ int main(void)
     CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "SL");
     CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "SR");
     CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "BC");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "@");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "@Foo");
+    CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "FL@Foo");
 
     printf("\nTesting av_channel_layout_index_from_string\n");
     CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "FL");
@@ -321,9 +329,27 @@ int main(void)
     printf("\nTesting av_channel_layout_from_string\n");
     CHANNEL_LAYOUT_FROM_STRING("FL+FR+FC+BL+BR+LFE");
     CHANNEL_LAYOUT_FROM_STRING("2 channels (FR+FL)");
+    CHANNEL_LAYOUT_FROM_STRING("2 channels (AMBI1023+FL)");
+    CHANNEL_LAYOUT_FROM_STRING("3 channels (FR+FL)");
+    CHANNEL_LAYOUT_FROM_STRING("-3 channels (FR+FL)");
+    CHANNEL_LAYOUT_FROM_STRING("0 channels ()");
+    CHANNEL_LAYOUT_FROM_STRING("2 channels (FL+FR");
     CHANNEL_LAYOUT_FROM_STRING("ambisonic 1+FR+FL");
     CHANNEL_LAYOUT_FROM_STRING("ambisonic 2+FC@Foo");
     CHANNEL_LAYOUT_FROM_STRING("FL@Foo+FR@Bar");
+    CHANNEL_LAYOUT_FROM_STRING("FL+stereo");
+    CHANNEL_LAYOUT_FROM_STRING("stereo+stereo");
+    CHANNEL_LAYOUT_FROM_STRING("stereo@Boo");
+    CHANNEL_LAYOUT_FROM_STRING("");
+    CHANNEL_LAYOUT_FROM_STRING("@");
+    CHANNEL_LAYOUT_FROM_STRING("@Dummy");
+    CHANNEL_LAYOUT_FROM_STRING("@FL");
+    CHANNEL_LAYOUT_FROM_STRING("Dummy");
+    CHANNEL_LAYOUT_FROM_STRING("Dummy@FL");
+    CHANNEL_LAYOUT_FROM_STRING("FR+Dummy");
+    CHANNEL_LAYOUT_FROM_STRING("FR+Dummy@FL");
+    CHANNEL_LAYOUT_FROM_STRING("FR+@FL");
+    CHANNEL_LAYOUT_FROM_STRING("FL+@");
     CHANNEL_LAYOUT_FROM_STRING("FR+FL@Foo+USR63@Foo");
 
     ret = av_channel_layout_copy(&layout2, &layout);
diff --git a/tests/ref/fate/channel_layout b/tests/ref/fate/channel_layout
index b98ccdb0f0..ea7ec6fa3c 100644
--- a/tests/ref/fate/channel_layout
+++ b/tests/ref/fate/channel_layout
@@ -56,6 +56,11 @@ With "FR":                                         1
 With "USR63":                                     63
 With "AMBI0":                                   1024
 With "AMBI1023":                                2047
+With "AMBI1024":                                  -1
+With "Dummy":                                     -1
+With "FL@Foo":                                    -1
+With "Foo@FL":                                    -1
+With "@FL":                                       -1
 
 ==Native layouts==
 
@@ -101,6 +106,9 @@ On "5.1(side)" layout with "LFE":                  3
 On "5.1(side)" layout with "SL":                   9
 On "5.1(side)" layout with "SR":                  10
 On "5.1(side)" layout with "BC":                  -1
+On "5.1(side)" layout with "@":                   -1
+On "5.1(side)" layout with "@Foo":                -1
+On "5.1(side)" layout with "FL@Foo":              -1
 
 Testing av_channel_layout_index_from_string
 On "5.1(side)" layout with "FL":                  0
@@ -121,9 +129,27 @@ On "5.1(side)" layout with AV_CH_LAYOUT_4POINT1:  0xf
 Testing av_channel_layout_from_string
 With "FL+FR+FC+BL+BR+LFE":                6 channels (FL+FR+FC+BL+BR+LFE)
 With "2 channels (FR+FL)":                             2 channels (FR+FL)
+With "2 channels (AMBI1023+FL)":                                     fail
+With "3 channels (FR+FL)":                                           fail
+With "-3 channels (FR+FL)":                                          fail
+With "0 channels ()":                                                fail
+With "2 channels (FL+FR":                                            fail
 With "ambisonic 1+FR+FL":                  ambisonic 1+2 channels (FR+FL)
 With "ambisonic 2+FC@Foo":                ambisonic 2+1 channels (FC@Foo)
 With "FL@Foo+FR@Bar":                          2 channels (FL@Foo+FR@Bar)
+With "FL+stereo":                                                    fail
+With "stereo+stereo":                                                fail
+With "stereo@Boo":                                                   fail
+With "":                                                             fail
+With "@":                                                            fail
+With "@Dummy":                                                       fail
+With "@FL":                                               1 channels (FL)
+With "Dummy":                                                        fail
+With "Dummy@FL":                                                     fail
+With "FR+Dummy":                                                     fail
+With "FR+Dummy@FL":                                       1 channels (FR)
+With "FR+@FL":                                      2 channels (FR+FL@FL)
+With "FL+@":                                                         fail
 With "FR+FL@Foo+USR63@Foo":              3 channels (FR+FL@Foo+USR63@Foo)
 
 Testing av_channel_layout_index_from_string
-- 
2.35.3

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [FFmpeg-devel] [PATCH 5/7] avutil/channel_layout: factorize parsing list of channel names
  2024-03-09 21:54 [FFmpeg-devel] [PATCH 1/7] avutil/channel_layout: add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL Marton Balint
                   ` (2 preceding siblings ...)
  2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 4/7] avutil/tests/channel_layout: add some av_channel_from_string and av_channel_layout_from_string tests Marton Balint
@ 2024-03-09 21:54 ` Marton Balint
  2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 6/7] avutil/channel_layout: fix some (un)initialization issues in av_channel_layout_from_string() Marton Balint
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Marton Balint @ 2024-03-09 21:54 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Marton Balint

Also make use of the av_channel_from_string() function to determine the channel
id. This fixes some parse issues in av_channel_layout_from_string().

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavutil/channel_layout.c    | 172 ++++++++++++----------------------
 tests/ref/fate/channel_layout |   8 +-
 2 files changed, 62 insertions(+), 118 deletions(-)

diff --git a/libavutil/channel_layout.c b/libavutil/channel_layout.c
index d3abb2dc42..5db4cc9df0 100644
--- a/libavutil/channel_layout.c
+++ b/libavutil/channel_layout.c
@@ -239,13 +239,58 @@ int av_channel_layout_from_mask(AVChannelLayout *channel_layout,
     return 0;
 }
 
+static int parse_channel_list(AVChannelLayout *ch_layout, const char *str)
+{
+    int ret;
+    int nb_channels = 0;
+    AVChannelCustom *map = NULL;
+    AVChannelCustom custom = {0};
+
+    while (*str) {
+        char *channel, *chname;
+        int ret = av_opt_get_key_value(&str, "@", "+", AV_OPT_FLAG_IMPLICIT_KEY, &channel, &chname);
+        if (ret < 0) {
+            av_freep(&map);
+            return ret;
+        }
+        if (*str)
+            str++; // skip separator
+        if (!channel) {
+            channel = chname;
+            chname = NULL;
+        }
+        av_strlcpy(custom.name, chname ? chname : "", sizeof(custom.name));
+        custom.id = av_channel_from_string(channel);
+        av_free(channel);
+        av_free(chname);
+        if (custom.id == AV_CHAN_NONE) {
+            av_freep(&map);
+            return AVERROR(EINVAL);
+        }
+
+        av_dynarray2_add((void **)&map, &nb_channels, sizeof(custom), (void *)&custom);
+        if (!map)
+            return AVERROR(ENOMEM);
+    }
+
+    if (!nb_channels)
+        return AVERROR(EINVAL);
+
+    ch_layout->order = AV_CHANNEL_ORDER_CUSTOM;
+    ch_layout->u.map = map;
+    ch_layout->nb_channels = nb_channels;
+
+    ret = av_channel_layout_retype(ch_layout, 0, AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL);
+    av_assert0(ret == 0);
+
+    return 0;
+}
+
 int av_channel_layout_from_string(AVChannelLayout *channel_layout,
                                   const char *str)
 {
-    int i;
-    int channels = 0, nb_channels = 0, native = 1;
-    enum AVChannel highest_channel = AV_CHAN_NONE;
-    const char *dup;
+    int i, matches, ret;
+    int channels = 0, nb_channels = 0;
     char *chlist, *end;
     uint64_t mask = 0;
 
@@ -321,121 +366,20 @@ int av_channel_layout_from_string(AVChannelLayout *channel_layout,
         return AVERROR(ENOMEM);
 
     /* channel names */
-    av_sscanf(str, "%d channels (%[^)]", &nb_channels, chlist);
-    end = strchr(str, ')');
-
-    dup = chlist;
-    while (*dup) {
-        char *channel, *chname;
-        int ret = av_opt_get_key_value(&dup, "@", "+", AV_OPT_FLAG_IMPLICIT_KEY, &channel, &chname);
-        if (ret < 0) {
-            av_free(chlist);
-            return ret;
-        }
-        if (*dup)
-            dup++; // skip separator
-        if (channel && !*channel)
-            av_freep(&channel);
-        for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++) {
-            if (channel_names[i].name && !strcmp(channel ? channel : chname, channel_names[i].name)) {
-                if (channel || i < highest_channel || mask & (1ULL << i))
-                    native = 0; // Not a native layout, use a custom one
-                highest_channel = i;
-                mask |= 1ULL << i;
-                break;
-            }
-        }
-
-        if (!channel && i >= FF_ARRAY_ELEMS(channel_names)) {
-            char *endptr = chname;
-            enum AVChannel id = AV_CHAN_NONE;
-
-            if (!strncmp(chname, "USR", 3)) {
-                const char *p = chname + 3;
-                id = strtol(p, &endptr, 0);
-            }
-            if (id < 0 || *endptr) {
-                native = 0; // Unknown channel name
-                channels = 0;
-                mask = 0;
-                av_free(chname);
-                break;
-            }
-            if (id > 63)
-                native = 0; // Not a native layout, use a custom one
-            else {
-                if (id < highest_channel || mask & (1ULL << id))
-                    native = 0; // Not a native layout, use a custom one
-                highest_channel = id;
-                mask |= 1ULL << id;
-            }
-        }
-        channels++;
-        av_free(channel);
-        av_free(chname);
-    }
-
-    if (mask && native) {
-        av_free(chlist);
-        if (nb_channels && ((nb_channels != channels) || (!end || *++end)))
-            return AVERROR(EINVAL);
-        av_channel_layout_from_mask(channel_layout, mask);
-        return 0;
-    }
-
-    /* custom layout of channel names */
-    if (channels && !native) {
-        int idx = 0;
+    matches = av_sscanf(str, "%d channels (%[^)]", &nb_channels, chlist);
+    ret = parse_channel_list(channel_layout, chlist);
+    av_freep(&chlist);
+    if (ret < 0 && ret != AVERROR(EINVAL))
+        return ret;
 
-        if (nb_channels && ((nb_channels != channels) || (!end || *++end))) {
-            av_free(chlist);
+    if (ret >= 0) {
+        end = strchr(str, ')');
+        if (matches == 2 && (nb_channels != channel_layout->nb_channels || !end || *++end)) {
+            av_channel_layout_uninit(channel_layout);
             return AVERROR(EINVAL);
         }
-
-        channel_layout->u.map = av_calloc(channels, sizeof(*channel_layout->u.map));
-        if (!channel_layout->u.map) {
-            av_free(chlist);
-            return AVERROR(ENOMEM);
-        }
-
-        channel_layout->order = AV_CHANNEL_ORDER_CUSTOM;
-        channel_layout->nb_channels = channels;
-
-        dup = chlist;
-        while (*dup) {
-            char *channel, *chname;
-            int ret = av_opt_get_key_value(&dup, "@", "+", AV_OPT_FLAG_IMPLICIT_KEY, &channel, &chname);
-            if (ret < 0) {
-                av_freep(&channel_layout->u.map);
-                av_free(chlist);
-                return ret;
-            }
-            if (*dup)
-                dup++; // skip separator
-            for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++) {
-                if (channel_names[i].name && !strcmp(channel ? channel : chname, channel_names[i].name)) {
-                    channel_layout->u.map[idx].id = i;
-                    if (channel)
-                        av_strlcpy(channel_layout->u.map[idx].name, chname, sizeof(channel_layout->u.map[idx].name));
-                    idx++;
-                    break;
-                }
-            }
-            if (i >= FF_ARRAY_ELEMS(channel_names)) {
-                const char *p = (channel ? channel : chname) + 3;
-                channel_layout->u.map[idx].id = strtol(p, NULL, 0);
-                if (channel)
-                    av_strlcpy(channel_layout->u.map[idx].name, chname, sizeof(channel_layout->u.map[idx].name));
-                idx++;
-            }
-            av_free(channel);
-            av_free(chname);
-        }
-        av_free(chlist);
-
         return 0;
     }
-    av_freep(&chlist);
 
     errno = 0;
     mask = strtoull(str, &end, 0);
diff --git a/tests/ref/fate/channel_layout b/tests/ref/fate/channel_layout
index ea7ec6fa3c..117a5fd84d 100644
--- a/tests/ref/fate/channel_layout
+++ b/tests/ref/fate/channel_layout
@@ -129,7 +129,7 @@ On "5.1(side)" layout with AV_CH_LAYOUT_4POINT1:  0xf
 Testing av_channel_layout_from_string
 With "FL+FR+FC+BL+BR+LFE":                6 channels (FL+FR+FC+BL+BR+LFE)
 With "2 channels (FR+FL)":                             2 channels (FR+FL)
-With "2 channels (AMBI1023+FL)":                                     fail
+With "2 channels (AMBI1023+FL)":                 2 channels (AMBI1023+FL)
 With "3 channels (FR+FL)":                                           fail
 With "-3 channels (FR+FL)":                                          fail
 With "0 channels ()":                                                fail
@@ -143,12 +143,12 @@ With "stereo@Boo":                                                   fail
 With "":                                                             fail
 With "@":                                                            fail
 With "@Dummy":                                                       fail
-With "@FL":                                               1 channels (FL)
+With "@FL":                                                          fail
 With "Dummy":                                                        fail
 With "Dummy@FL":                                                     fail
 With "FR+Dummy":                                                     fail
-With "FR+Dummy@FL":                                       1 channels (FR)
-With "FR+@FL":                                      2 channels (FR+FL@FL)
+With "FR+Dummy@FL":                                                  fail
+With "FR+@FL":                                                       fail
 With "FL+@":                                                         fail
 With "FR+FL@Foo+USR63@Foo":              3 channels (FR+FL@Foo+USR63@Foo)
 
-- 
2.35.3

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [FFmpeg-devel] [PATCH 6/7] avutil/channel_layout: fix some (un)initialization issues in av_channel_layout_from_string()
  2024-03-09 21:54 [FFmpeg-devel] [PATCH 1/7] avutil/channel_layout: add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL Marton Balint
                   ` (3 preceding siblings ...)
  2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 5/7] avutil/channel_layout: factorize parsing list of channel names Marton Balint
@ 2024-03-09 21:54 ` Marton Balint
  2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 7/7] avutil/channel_layout: add specific text versions for unknown and unused channels Marton Balint
  2024-03-16  8:34 ` [FFmpeg-devel] [PATCH 1/7] avutil/channel_layout: add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL Marton Balint
  6 siblings, 0 replies; 8+ messages in thread
From: Marton Balint @ 2024-03-09 21:54 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Marton Balint

Also make initialization/uninitialization behaviour more explicit in the docs,
and make sure we do not leak a channel map on error.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavutil/channel_layout.c | 5 +++++
 libavutil/channel_layout.h | 8 ++++++--
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/libavutil/channel_layout.c b/libavutil/channel_layout.c
index 5db4cc9df0..9e8a1c12e6 100644
--- a/libavutil/channel_layout.c
+++ b/libavutil/channel_layout.c
@@ -302,6 +302,10 @@ int av_channel_layout_from_string(AVChannelLayout *channel_layout,
         }
     }
 
+    /* This function is a channel layout initializer, so we have to
+     * zero-initialize before we start setting fields individually. */
+    memset(channel_layout, 0, sizeof(*channel_layout));
+
     /* ambisonic */
     if (!strncmp(str, "ambisonic ", 10)) {
         const char *p = str + 10;
@@ -343,6 +347,7 @@ int av_channel_layout_from_string(AVChannelLayout *channel_layout,
                 for (i = 0; i < extra.nb_channels; i++) {
                     enum AVChannel ch = av_channel_layout_channel_from_index(&extra, i);
                     if (CHAN_IS_AMBI(ch)) {
+                        av_channel_layout_uninit(channel_layout);
                         av_channel_layout_uninit(&extra);
                         return AVERROR(EINVAL);
                     }
diff --git a/libavutil/channel_layout.h b/libavutil/channel_layout.h
index a1e9b08064..8a078d1601 100644
--- a/libavutil/channel_layout.h
+++ b/libavutil/channel_layout.h
@@ -512,10 +512,14 @@ int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask);
  *  - the number of unordered channels (eg. "4C" or "4 channels")
  *  - the ambisonic order followed by optional non-diegetic channels (eg.
  *    "ambisonic 2+stereo")
+ * On error, the channel layout will remain uninitialized, but not necessarily
+ * untouched.
  *
- * @param channel_layout input channel layout
+ * @param channel_layout uninitialized channel layout for the result
  * @param str string describing the channel layout
- * @return 0 channel layout was detected, AVERROR_INVALIDATATA otherwise
+ * @return 0 on success parsing the channel layout
+ *         AVERROR(EINVAL) if an invalid channel layout string was provided
+ *         AVERROR(ENOMEM) if there was not enough memory
  */
 int av_channel_layout_from_string(AVChannelLayout *channel_layout,
                                   const char *str);
-- 
2.35.3

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [FFmpeg-devel] [PATCH 7/7] avutil/channel_layout: add specific text versions for unknown and unused channels
  2024-03-09 21:54 [FFmpeg-devel] [PATCH 1/7] avutil/channel_layout: add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL Marton Balint
                   ` (4 preceding siblings ...)
  2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 6/7] avutil/channel_layout: fix some (un)initialization issues in av_channel_layout_from_string() Marton Balint
@ 2024-03-09 21:54 ` Marton Balint
  2024-03-16  8:34 ` [FFmpeg-devel] [PATCH 1/7] avutil/channel_layout: add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL Marton Balint
  6 siblings, 0 replies; 8+ messages in thread
From: Marton Balint @ 2024-03-09 21:54 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Marton Balint

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavutil/channel_layout.c       | 13 +++++++++++++
 libavutil/tests/channel_layout.c |  2 ++
 tests/ref/fate/channel_layout    |  4 +++-
 3 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/libavutil/channel_layout.c b/libavutil/channel_layout.c
index 9e8a1c12e6..a83618bcc7 100644
--- a/libavutil/channel_layout.c
+++ b/libavutil/channel_layout.c
@@ -86,6 +86,10 @@ void av_channel_name_bprint(AVBPrint *bp, enum AVChannel channel_id)
         av_bprintf(bp, "%s", channel_names[channel_id].name);
     else if (channel_id == AV_CHAN_NONE)
         av_bprintf(bp, "NONE");
+    else if (channel_id == AV_CHAN_UNKNOWN)
+        av_bprintf(bp, "UNK");
+    else if (channel_id == AV_CHAN_UNUSED)
+        av_bprintf(bp, "UNSD");
     else
         av_bprintf(bp, "USR%d", channel_id);
 }
@@ -115,6 +119,10 @@ void av_channel_description_bprint(AVBPrint *bp, enum AVChannel channel_id)
         av_bprintf(bp, "%s", channel_names[channel_id].description);
     else if (channel_id == AV_CHAN_NONE)
         av_bprintf(bp, "none");
+    else if (channel_id == AV_CHAN_UNKNOWN)
+        av_bprintf(bp, "unknown");
+    else if (channel_id == AV_CHAN_UNUSED)
+        av_bprintf(bp, "unused");
     else
         av_bprintf(bp, "user %d", channel_id);
 }
@@ -151,6 +159,11 @@ enum AVChannel av_channel_from_string(const char *str)
         if (channel_names[i].name && !strcmp(str, channel_names[i].name))
             return i;
     }
+    if (!strcmp(str, "UNK"))
+        return AV_CHAN_UNKNOWN;
+    if (!strcmp(str, "UNSD"))
+        return AV_CHAN_UNUSED;
+
     if (!strncmp(str, "USR", 3)) {
         const char *p = str + 3;
         id = strtol(p, &endptr, 0);
diff --git a/libavutil/tests/channel_layout.c b/libavutil/tests/channel_layout.c
index 14d36c71b5..8132b83434 100644
--- a/libavutil/tests/channel_layout.c
+++ b/libavutil/tests/channel_layout.c
@@ -348,6 +348,8 @@ int main(void)
     CHANNEL_LAYOUT_FROM_STRING("Dummy@FL");
     CHANNEL_LAYOUT_FROM_STRING("FR+Dummy");
     CHANNEL_LAYOUT_FROM_STRING("FR+Dummy@FL");
+    CHANNEL_LAYOUT_FROM_STRING("UNK+UNSD");
+    CHANNEL_LAYOUT_FROM_STRING("NONE");
     CHANNEL_LAYOUT_FROM_STRING("FR+@FL");
     CHANNEL_LAYOUT_FROM_STRING("FL+@");
     CHANNEL_LAYOUT_FROM_STRING("FR+FL@Foo+USR63@Foo");
diff --git a/tests/ref/fate/channel_layout b/tests/ref/fate/channel_layout
index 117a5fd84d..0bcfefce3b 100644
--- a/tests/ref/fate/channel_layout
+++ b/tests/ref/fate/channel_layout
@@ -148,6 +148,8 @@ With "Dummy":                                                        fail
 With "Dummy@FL":                                                     fail
 With "FR+Dummy":                                                     fail
 With "FR+Dummy@FL":                                                  fail
+With "UNK+UNSD":                                    2 channels (UNK+UNSD)
+With "NONE":                                                         fail
 With "FR+@FL":                                                       fail
 With "FL+@":                                                         fail
 With "FR+FL@Foo+USR63@Foo":              3 channels (FR+FL@Foo+USR63@Foo)
@@ -236,5 +238,5 @@ With "ambisonic 2+stereo": AMBI   (ambisonic 2+stereo)
 With "2C": UNSPEC (2 channels)
   == UNSPEC (2 channels)
   != NATIVE
-  == CUSTOM (2 channels (USR768+USR768))
+  == CUSTOM (2 channels (UNK+UNK))
   != AMBI
-- 
2.35.3

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/7] avutil/channel_layout: add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL
  2024-03-09 21:54 [FFmpeg-devel] [PATCH 1/7] avutil/channel_layout: add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL Marton Balint
                   ` (5 preceding siblings ...)
  2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 7/7] avutil/channel_layout: add specific text versions for unknown and unused channels Marton Balint
@ 2024-03-16  8:34 ` Marton Balint
  6 siblings, 0 replies; 8+ messages in thread
From: Marton Balint @ 2024-03-16  8:34 UTC (permalink / raw)
  To: FFmpeg development discussions and patches



On Sat, 9 Mar 2024, Marton Balint wrote:

> Signed-off-by: Marton Balint <cus@passwd.hu>
> ---
> doc/APIchanges             |  3 +++
> libavutil/channel_layout.c | 30 ++++++++++++++++++++++++++++++
> libavutil/channel_layout.h |  7 +++++++
> libavutil/version.h        |  2 +-
> 4 files changed, 41 insertions(+), 1 deletion(-)

Will apply the series.

Regards,
Marton

>
> diff --git a/doc/APIchanges b/doc/APIchanges
> index cf58c8c5f0..a44c8e4f10 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -2,6 +2,9 @@ The last version increases of all libraries were on 2024-03-07
>
> API changes, most recent first:
>
> +2024-03-xx - xxxxxxxxxx - lavu 59.2.100 - channel_layout.h
> +  Add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL.
> +
> 2024-03-08 - xxxxxxxxxx - lavc 61.1.100 - avcodec.h
>   Add AVCodecContext.[nb_]side_data_prefer_packet.
>
> diff --git a/libavutil/channel_layout.c b/libavutil/channel_layout.c
> index 8c1b3362f7..d3abb2dc42 100644
> --- a/libavutil/channel_layout.c
> +++ b/libavutil/channel_layout.c
> @@ -553,6 +553,33 @@ static int ambisonic_order(const AVChannelLayout *channel_layout)
>     return order;
> }
>
> +static enum AVChannelOrder canonical_order(AVChannelLayout *channel_layout)
> +{
> +    int has_known_channel = 0;
> +    int order;
> +
> +    if (channel_layout->order != AV_CHANNEL_ORDER_CUSTOM)
> +        return channel_layout->order;
> +
> +    if (has_channel_names(channel_layout))
> +        return AV_CHANNEL_ORDER_CUSTOM;
> +
> +    for (int i = 0; i < channel_layout->nb_channels && !has_known_channel; i++)
> +        if (channel_layout->u.map[i].id != AV_CHAN_UNKNOWN)
> +            has_known_channel = 1;
> +    if (!has_known_channel)
> +        return AV_CHANNEL_ORDER_UNSPEC;
> +
> +    if (masked_description(channel_layout, 0) > 0)
> +        return AV_CHANNEL_ORDER_NATIVE;
> +
> +    order = ambisonic_order(channel_layout);
> +    if (order >= 0 && masked_description(channel_layout, (order + 1) * (order + 1)) >= 0)
> +        return AV_CHANNEL_ORDER_AMBISONIC;
> +
> +    return AV_CHANNEL_ORDER_CUSTOM;
> +}
> +
> /**
>  * If the custom layout is n-th order standard-order ambisonic, with optional
>  * extra non-diegetic channels at the end, write its string description in bp.
> @@ -892,6 +919,9 @@ int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrde
>     if (!av_channel_layout_check(channel_layout))
>         return AVERROR(EINVAL);
>
> +    if (flags & AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL)
> +        order = canonical_order(channel_layout);
> +
>     if (channel_layout->order == order)
>         return 0;
>
> diff --git a/libavutil/channel_layout.h b/libavutil/channel_layout.h
> index 10ffe74468..a1e9b08064 100644
> --- a/libavutil/channel_layout.h
> +++ b/libavutil/channel_layout.h
> @@ -680,6 +680,13 @@ int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout
>  */
> #define AV_CHANNEL_LAYOUT_RETYPE_FLAG_LOSSLESS (1 << 0)
>
> +/**
> + * The specified retype target order is ignored and the simplest possible
> + * (canonical) order is used for which the input layout can be losslessy
> + * represented.
> + */
> +#define AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL (1 << 1)
> +
> /**
>  * Change the AVChannelOrder of a channel layout.
>  *
> diff --git a/libavutil/version.h b/libavutil/version.h
> index 09f8cdc292..57cad02ec0 100644
> --- a/libavutil/version.h
> +++ b/libavutil/version.h
> @@ -79,7 +79,7 @@
>  */
>
> #define LIBAVUTIL_VERSION_MAJOR  59
> -#define LIBAVUTIL_VERSION_MINOR   1
> +#define LIBAVUTIL_VERSION_MINOR   2
> #define LIBAVUTIL_VERSION_MICRO 100
>
> #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
> -- 
> 2.35.3
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
>
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-03-16  8:34 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-09 21:54 [FFmpeg-devel] [PATCH 1/7] avutil/channel_layout: add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL Marton Balint
2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 2/7] avformat/mov_chan: simplify channel layout canonicalization Marton Balint
2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 3/7] avutil/tests/channel_layout: make printing results part of the tests Marton Balint
2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 4/7] avutil/tests/channel_layout: add some av_channel_from_string and av_channel_layout_from_string tests Marton Balint
2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 5/7] avutil/channel_layout: factorize parsing list of channel names Marton Balint
2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 6/7] avutil/channel_layout: fix some (un)initialization issues in av_channel_layout_from_string() Marton Balint
2024-03-09 21:54 ` [FFmpeg-devel] [PATCH 7/7] avutil/channel_layout: add specific text versions for unknown and unused channels Marton Balint
2024-03-16  8:34 ` [FFmpeg-devel] [PATCH 1/7] avutil/channel_layout: add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL Marton Balint

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git