Skip to content

Truly independent web browser

LibMedia+Tests: Use a custom layout for channels outside a native mask

Aliaksandr Kalenik929a21ad49e9
diff --git a/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cpp b/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cpp
index 6c74d6ac1e9..62ed03a8b00 100644
--- a/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cpp
+++ b/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cpp
@@ -127,7 +127,8 @@ ErrorOr<FFmpegChannelLayout> FFmpegChannelLayout::from_channel_map(FFmpegFunctio
// Native order follows the order of the declarations in AVChannel, which are sequential.
// If we find that one of the channels in our input mapping doesn't match this requirement,
// fall back to a custom order.
- if (channel <= last_channel)
+ auto channel_has_bit_in_native_mask = channel >= 0 && static_cast<size_t>(channel) < NumericLimits<decltype(AVChannelLayout::u.mask)>::digits();
+ if (channel <= last_channel || !channel_has_bit_in_native_mask)
return FFmpegChannelLayout(functions, TRY(channel_map_to_custom_av_channel_layout(functions, channel_map)));
layout.u.mask |= 1ULL << channel;
last_channel = channel;
diff --git a/Tests/LibMedia/TestFFmpegAudioConverter.cpp b/Tests/LibMedia/TestFFmpegAudioConverter.cpp
index 0863521a88a..3dc43b23018 100644
--- a/Tests/LibMedia/TestFFmpegAudioConverter.cpp
+++ b/Tests/LibMedia/TestFFmpegAudioConverter.cpp
@@ -96,3 +96,16 @@ TEST_CASE(stereo_converts_into_a_layout_with_top_side_speakers)
EXPECT_EQ(converted_block.sample(channel, frame), 0.0f);
}
}
+
+TEST_CASE(a_channel_map_ending_in_one_unknown_channel_becomes_a_custom_layout)
+{
+ auto channel_map = Audio::ChannelMap(Audio::Channel::FrontLeft, Audio::Channel::FrontRight, Audio::Channel::Unknown);
+ auto layout = MUST(Media::FFmpeg::FFmpegChannelLayout::from_channel_map(Media::FFmpeg::FFmpegFunctions::bundled(), channel_map));
+ EXPECT_EQ(layout.layout().order, AV_CHANNEL_ORDER_CUSTOM);
+ EXPECT_EQ(layout.layout().nb_channels, 3);
+ if (layout.layout().order != AV_CHANNEL_ORDER_CUSTOM || layout.layout().nb_channels != 3)
+ return;
+ EXPECT_EQ(layout.layout().u.map[0].id, AV_CHAN_FRONT_LEFT);
+ EXPECT_EQ(layout.layout().u.map[1].id, AV_CHAN_FRONT_RIGHT);
+ EXPECT_EQ(layout.layout().u.map[2].id, AV_CHAN_UNKNOWN);
+}