From 547408ce1d904b5676f65c1edc062e540dbd087f Mon Sep 17 00:00:00 2001 From: James Almer Date: Mon, 30 Dec 2024 17:04:45 -0300 Subject: [PATCH] avformat/iamfdec: swap back and side streams if both are present Layouts with both pairs (7.1, 7.1.2, etc) in IAMF that follow the definition in ITU-R BS.2051-3 for Systems I and J also follow its ordering. This means side comes before back, which is the inverse of how it's defined in AVChannel. To workaround this without having to use custom order channel layouts, swap the stream ids in the input IAMF structure, so packets for one are mapped to the other. Signed-off-by: James Almer --- libavformat/iamfdec.c | 24 +++++++++++++++++++- libavformat/version.h | 2 +- tests/ref/fate/iamf-7_1_4 | 48 +++++++++++++++++++-------------------- 3 files changed, 48 insertions(+), 26 deletions(-) diff --git a/libavformat/iamfdec.c b/libavformat/iamfdec.c index ec80000e36..e1a913dd1c 100644 --- a/libavformat/iamfdec.c +++ b/libavformat/iamfdec.c @@ -81,7 +81,10 @@ static int iamf_read_header(AVFormatContext *s) for (int i = 0; i < iamf->nb_audio_elements; i++) { IAMFAudioElement *audio_element = iamf->audio_elements[i]; + const AVIAMFLayer *layer = audio_element->element->layers[audio_element->nb_layers - 1]; AVStreamGroup *stg = avformat_stream_group_create(s, AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT, NULL); + int coupled_substream_count = audio_element->layers[audio_element->nb_layers - 1].coupled_substream_count; + int side_substream_id = -1, back_substream_id = -1; if (!stg) return AVERROR(ENOMEM); @@ -92,7 +95,7 @@ static int iamf_read_header(AVFormatContext *s) stg->params.iamf_audio_element = audio_element->element; audio_element->element = NULL; - for (int j = 0; j < audio_element->nb_substreams; j++) { + for (int j = 0, k = 0; j < audio_element->nb_substreams; j++) { IAMFSubStream *substream = &audio_element->substreams[j]; AVStream *st = avformat_new_stream(s, NULL); @@ -111,8 +114,27 @@ static int iamf_read_header(AVFormatContext *s) st->disposition |= AV_DISPOSITION_DEFAULT; else if (audio_element->nb_layers > 1 || audio_element->layers[0].substream_count > 1) st->disposition |= AV_DISPOSITION_DEPENDENT; + if (k == av_channel_layout_index_from_channel(&layer->ch_layout, AV_CHAN_BACK_LEFT)) + back_substream_id = j; + else if (k == av_channel_layout_index_from_channel(&layer->ch_layout, AV_CHAN_SIDE_LEFT)) + side_substream_id = j; st->id = substream->audio_substream_id; avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); + + k += 1 + (coupled_substream_count-- > 0); + } + + // Swap back and side stream ids as our native channel layout ordering doen't match the + // order from ITU-R - BS.2051-3 for Systems I and J (where side channels come before back ones). + if (back_substream_id >= 0 && av_channel_layout_compare(&layer->ch_layout, + &(AVChannelLayout)AV_CHANNEL_LAYOUT_9POINT1POINT6)) { + const IAMFSubStream *back_substream = &audio_element->substreams[back_substream_id]; + const IAMFSubStream *side_substream = &audio_element->substreams[side_substream_id]; + AVStream *back_st = stg->streams[back_substream_id]; + AVStream *side_st = stg->streams[side_substream_id]; + + back_st->id = side_substream->audio_substream_id; + side_st->id = back_substream->audio_substream_id; } } diff --git a/libavformat/version.h b/libavformat/version.h index a0691612ca..57d1cb0410 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -32,7 +32,7 @@ #include "version_major.h" #define LIBAVFORMAT_VERSION_MINOR 9 -#define LIBAVFORMAT_VERSION_MICRO 103 +#define LIBAVFORMAT_VERSION_MICRO 104 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \ diff --git a/tests/ref/fate/iamf-7_1_4 b/tests/ref/fate/iamf-7_1_4 index ccce96ea84..72b10f771a 100644 --- a/tests/ref/fate/iamf-7_1_4 +++ b/tests/ref/fate/iamf-7_1_4 @@ -45,72 +45,72 @@ 0, 0, 0, 4608, 1399, 0x6e89566e 1, 0, 0, 4608, 1399, 0x6e89566e 2, 0, 0, 4608, 1396, 0x0dcb5677 -3, 0, 0, 4608, 1396, 0x0dcb5677 +3, 0, 0, 4608, 1399, 0x6e89566e 4, 0, 0, 4608, 1399, 0x6e89566e -5, 0, 0, 4608, 1399, 0x6e89566e +5, 0, 0, 4608, 1396, 0x0dcb5677 6, 0, 0, 4608, 1399, 0x6e89566e 0, 4608, 4608, 4608, 1442, 0x6c3c5b13 1, 4608, 4608, 4608, 1442, 0x6c3c5b13 2, 4608, 4608, 4608, 1439, 0xc46b5ac5 -3, 4608, 4608, 4608, 1439, 0xc46b5ac5 +3, 4608, 4608, 4608, 1442, 0x6c3c5b13 4, 4608, 4608, 4608, 1442, 0x6c3c5b13 -5, 4608, 4608, 4608, 1442, 0x6c3c5b13 +5, 4608, 4608, 4608, 1439, 0xc46b5ac5 6, 4608, 4608, 4608, 1442, 0x6c3c5b13 0, 9216, 9216, 4608, 1380, 0xc497571b 1, 9216, 9216, 4608, 1380, 0xc497571b 2, 9216, 9216, 4608, 1377, 0x5b2a55fe -3, 9216, 9216, 4608, 1377, 0x5b2a55fe +3, 9216, 9216, 4608, 1380, 0xc497571b 4, 9216, 9216, 4608, 1380, 0xc497571b -5, 9216, 9216, 4608, 1380, 0xc497571b +5, 9216, 9216, 4608, 1377, 0x5b2a55fe 6, 9216, 9216, 4608, 1380, 0xc497571b 0, 13824, 13824, 4608, 1383, 0x48e9510f 1, 13824, 13824, 4608, 1383, 0x48e9510f 2, 13824, 13824, 4608, 1380, 0x045550d3 -3, 13824, 13824, 4608, 1380, 0x045550d3 +3, 13824, 13824, 4608, 1383, 0x48e9510f 4, 13824, 13824, 4608, 1383, 0x48e9510f -5, 13824, 13824, 4608, 1383, 0x48e9510f +5, 13824, 13824, 4608, 1380, 0x045550d3 6, 13824, 13824, 4608, 1383, 0x48e9510f 0, 18432, 18432, 4608, 1572, 0x9a514719 1, 18432, 18432, 4608, 1572, 0x9a514719 2, 18432, 18432, 4608, 1568, 0xa2bc45f4 -3, 18432, 18432, 4608, 1568, 0xa2bc45f4 +3, 18432, 18432, 4608, 1572, 0x9a514719 4, 18432, 18432, 4608, 1572, 0x9a514719 -5, 18432, 18432, 4608, 1572, 0x9a514719 +5, 18432, 18432, 4608, 1568, 0xa2bc45f4 6, 18432, 18432, 4608, 1572, 0x9a514719 0, 23040, 23040, 4608, 1391, 0x74ac5014 1, 23040, 23040, 4608, 1391, 0x74ac5014 2, 23040, 23040, 4608, 1388, 0x96c85007 -3, 23040, 23040, 4608, 1388, 0x96c85007 +3, 23040, 23040, 4608, 1391, 0x74ac5014 4, 23040, 23040, 4608, 1391, 0x74ac5014 -5, 23040, 23040, 4608, 1391, 0x74ac5014 +5, 23040, 23040, 4608, 1388, 0x96c85007 6, 23040, 23040, 4608, 1391, 0x74ac5014 0, 27648, 27648, 4608, 1422, 0x2f9d47c5 1, 27648, 27648, 4608, 1422, 0x2f9d47c5 2, 27648, 27648, 4608, 1419, 0x4d4d466a -3, 27648, 27648, 4608, 1419, 0x4d4d466a +3, 27648, 27648, 4608, 1422, 0x2f9d47c5 4, 27648, 27648, 4608, 1422, 0x2f9d47c5 -5, 27648, 27648, 4608, 1422, 0x2f9d47c5 +5, 27648, 27648, 4608, 1419, 0x4d4d466a 6, 27648, 27648, 4608, 1422, 0x2f9d47c5 0, 32256, 32256, 4608, 1768, 0x2a044b99 1, 32256, 32256, 4608, 1768, 0x2a044b99 2, 32256, 32256, 4608, 1765, 0xacb84b24 -3, 32256, 32256, 4608, 1765, 0xacb84b24 +3, 32256, 32256, 4608, 1768, 0x2a044b99 4, 32256, 32256, 4608, 1768, 0x2a044b99 -5, 32256, 32256, 4608, 1768, 0x2a044b99 +5, 32256, 32256, 4608, 1765, 0xacb84b24 6, 32256, 32256, 4608, 1768, 0x2a044b99 0, 36864, 36864, 4608, 1534, 0xb0b35a3f 1, 36864, 36864, 4608, 1534, 0xb0b35a3f 2, 36864, 36864, 4608, 1531, 0x996458aa -3, 36864, 36864, 4608, 1531, 0x996458aa +3, 36864, 36864, 4608, 1534, 0xb0b35a3f 4, 36864, 36864, 4608, 1534, 0xb0b35a3f -5, 36864, 36864, 4608, 1534, 0xb0b35a3f +5, 36864, 36864, 4608, 1531, 0x996458aa 6, 36864, 36864, 4608, 1534, 0xb0b35a3f 0, 41472, 41472, 4608, 926, 0xc26a5eae 1, 41472, 41472, 4608, 926, 0xc26a5eae 2, 41472, 41472, 4608, 923, 0xa7225edf -3, 41472, 41472, 4608, 923, 0xa7225edf +3, 41472, 41472, 4608, 926, 0xc26a5eae 4, 41472, 41472, 4608, 926, 0xc26a5eae -5, 41472, 41472, 4608, 926, 0xc26a5eae +5, 41472, 41472, 4608, 923, 0xa7225edf 6, 41472, 41472, 4608, 926, 0xc26a5eae [STREAM_GROUP] index=0 @@ -226,7 +226,7 @@ DISPOSITION:multilayer=0 [/STREAM] [STREAM] index=3 -id=0x3 +id=0x5 DISPOSITION:default=0 DISPOSITION:dub=0 DISPOSITION:original=0 @@ -272,7 +272,7 @@ DISPOSITION:multilayer=0 [/STREAM] [STREAM] index=5 -id=0x5 +id=0x3 DISPOSITION:default=0 DISPOSITION:dub=0 DISPOSITION:original=0 @@ -462,7 +462,7 @@ DISPOSITION:multilayer=0 [/STREAM] [STREAM] index=3 -id=0x3 +id=0x5 DISPOSITION:default=0 DISPOSITION:dub=0 DISPOSITION:original=0 @@ -508,7 +508,7 @@ DISPOSITION:multilayer=0 [/STREAM] [STREAM] index=5 -id=0x5 +id=0x3 DISPOSITION:default=0 DISPOSITION:dub=0 DISPOSITION:original=0