]> git.sesse.net Git - casparcg/blob - unit-test/audio_channel_layout_test.cpp
[ffmpeg_consumer] Retired old implementation in favour of the now updated streaming_c...
[casparcg] / unit-test / audio_channel_layout_test.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "stdafx.h"
23
24 #include <gtest/gtest.h>
25
26 #include <common/memory.h>
27
28 #include <core/frame/audio_channel_layout.h>
29 #include <core/frame/frame.h>
30
31 #include <boost/range/algorithm/equal.hpp>
32
33 namespace {
34
35 ::caspar::core::audio_buffer get_buffer(::caspar::core::mutable_audio_buffer buffer)
36 {
37         ::caspar::spl::shared_ptr<::caspar::core::mutable_audio_buffer> buf(new ::caspar::core::mutable_audio_buffer(std::move(buffer)));
38         return ::caspar::core::audio_buffer(buf->data(), buf->size(), true, std::move(buf));
39 }
40
41 }
42
43 namespace caspar {
44
45 bool operator==(const ::caspar::core::audio_buffer& lhs, const ::caspar::core::audio_buffer& rhs)
46 {
47         return boost::range::equal(lhs, rhs);
48 }
49
50 namespace core {
51
52 TEST(AudioChannelLayoutTest, PassThrough)
53 {
54         audio_channel_layout input_layout(2, L"stereo", L"L R");
55         audio_channel_layout output_layout(2, L"stereo", L"L R" );
56         audio_channel_remapper remapper(input_layout, output_layout);
57
58         auto result = remapper.mix_and_rearrange(get_buffer({ 1, 2 }));
59
60         EXPECT_EQ(get_buffer({ 1, 2 }), result);
61 }
62
63 TEST(AudioChannelLayoutTest, ReverseLeftAndRight)
64 {
65         audio_channel_layout input_layout(2,L"stereo", L"L R");
66         audio_channel_layout output_layout(2, L"stereo", L"R L");
67         audio_channel_remapper remapper(input_layout, output_layout);
68
69         auto result = remapper.mix_and_rearrange(get_buffer({ 1, 2, 3, 4 }));
70
71         EXPECT_EQ(get_buffer({ 2, 1, 4, 3 }), result);
72 }
73
74 TEST(AudioChannelLayoutTest, StereoToMono)
75 {
76         spl::shared_ptr<audio_mix_config_repository> mix_repo;
77         mix_repo->register_config(L"stereo", { L"mono" }, L"C < L + R");
78         audio_channel_layout input_layout(2, L"stereo", L"L R");
79         audio_channel_layout output_layout(1, L"mono", L"C");
80         audio_channel_remapper remapper(input_layout, output_layout, mix_repo);
81
82         auto result = remapper.mix_and_rearrange(get_buffer({ 10, 30, 50, 30 }));
83
84         EXPECT_EQ(get_buffer({ 20, 40 }), result);
85 }
86
87 TEST(AudioChannelLayoutTest, StereoToPassthru)
88 {
89         audio_channel_layout input_layout(2, L"stereo", L"L R");
90         audio_channel_layout output_layout(16, L"16ch", L"");
91         audio_channel_remapper remapper(input_layout, output_layout);
92
93         auto result = remapper.mix_and_rearrange(get_buffer({ 1, 2 }));
94
95         EXPECT_EQ(get_buffer({ 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }), result);
96 }
97
98 TEST(AudioChannelLayoutTest, 16ChTo8ChPassthruDropLast8)
99 {
100         audio_channel_layout input_layout(16, L"16ch", L"");
101         audio_channel_layout output_layout(8, L"8ch", L"");
102         audio_channel_remapper remapper(input_layout, output_layout);
103
104         auto result = remapper.mix_and_rearrange(get_buffer(
105         {
106                 1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16, // Sample 1
107                 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, // Sample 2
108         }));
109
110         EXPECT_EQ(get_buffer(
111         {
112                 1,  2,  3,  4,  5,  6,  7,  8, // Sample 1
113                 17, 18, 19, 20, 21, 22, 23, 24 // Sample 2
114         }), result);
115 }
116
117 TEST(AudioChannelLayoutTest, MixMultipliers)
118 {
119         spl::shared_ptr<audio_mix_config_repository> mix_repo;
120         mix_repo->register_config(
121                         L"5.1",
122                         { L"stereo" },
123                         L"FL < FL + 0.5 * FC + 0.5 * BL | FR < FR + 0.5 * FC + 0.5 * BR"); // = 1.0 / (1.0 + 0.5 + 0.5) = 0.5 scaled => 0.5 + 0.25 + 0.25
124         audio_channel_layout input_layout(6, L"5.1", L"FL FR FC LFE BL BR");
125         audio_channel_layout output_layout(2, L"stereo", L"FL FR");
126         audio_channel_remapper remapper(input_layout, output_layout, mix_repo);
127         //                                                    FL   FR  FC   LFE   BL  BR
128         auto result = remapper.mix_and_rearrange(get_buffer({ 200, 50, 100, 1000, 40, 80 }));
129
130         EXPECT_EQ(get_buffer({
131                 100 + 25 + 10,
132                 25  + 25 + 20
133         }), result);
134 }
135
136 }}