1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/*
* Copyright (C) 2010-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "AEChannelInfo.h"
#include <algorithm>
#include <assert.h>
#include <limits>
#include <string.h>
CAEChannelInfo::CAEChannelInfo()
{
Reset();
}
CAEChannelInfo::CAEChannelInfo(const enum AEChannel* rhs)
{
*this = rhs;
}
CAEChannelInfo::CAEChannelInfo(const AEStdChLayout rhs)
{
*this = rhs;
}
void CAEChannelInfo::ResolveChannels(const CAEChannelInfo& rhs)
{
/* mono gets upmixed to dual mono */
if (m_channelCount == 1 && m_channels[0] == AE_CH_FC)
{
Reset();
*this += AE_CH_FL;
*this += AE_CH_FR;
return;
}
bool srcHasSL = false;
bool srcHasSR = false;
bool srcHasRL = false;
bool srcHasRR = false;
bool srcHasBC = false;
bool dstHasSL = false;
bool dstHasSR = false;
bool dstHasRL = false;
bool dstHasRR = false;
bool dstHasBC = false;
for (unsigned int c = 0; c < rhs.m_channelCount; ++c)
switch(rhs.m_channels[c])
{
case AE_CH_SL: dstHasSL = true; break;
case AE_CH_SR: dstHasSR = true; break;
case AE_CH_BL: dstHasRL = true; break;
case AE_CH_BR: dstHasRR = true; break;
case AE_CH_BC: dstHasBC = true; break;
default:
break;
}
CAEChannelInfo newInfo;
for (unsigned int i = 0; i < m_channelCount; ++i)
{
switch (m_channels[i])
{
case AE_CH_SL: srcHasSL = true; break;
case AE_CH_SR: srcHasSR = true; break;
case AE_CH_BL: srcHasRL = true; break;
case AE_CH_BR: srcHasRR = true; break;
case AE_CH_BC: srcHasBC = true; break;
default:
break;
}
bool found = false;
for (unsigned int c = 0; c < rhs.m_channelCount; ++c)
if (m_channels[i] == rhs.m_channels[c])
{
found = true;
break;
}
if (found)
newInfo += m_channels[i];
}
/* we need to ensure we end up with rear or side channels for downmix to work */
if (srcHasSL && !dstHasSL && dstHasRL && !newInfo.HasChannel(AE_CH_BL))
newInfo += AE_CH_BL;
if (srcHasSR && !dstHasSR && dstHasRR && !newInfo.HasChannel(AE_CH_BR))
newInfo += AE_CH_BR;
if (srcHasRL && !dstHasRL && dstHasSL && !newInfo.HasChannel(AE_CH_SL))
newInfo += AE_CH_SL;
if (srcHasRR && !dstHasRR && dstHasSR && !newInfo.HasChannel(AE_CH_SR))
newInfo += AE_CH_SR;
// mix back center if not available in destination layout
// prefer mixing into backs if available
if (srcHasBC && !dstHasBC)
{
if (dstHasRL && !newInfo.HasChannel(AE_CH_BL))
newInfo += AE_CH_BL;
else if (dstHasSL && !newInfo.HasChannel(AE_CH_SL))
newInfo += AE_CH_SL;
if (dstHasRR && !newInfo.HasChannel(AE_CH_BR))
newInfo += AE_CH_BR;
else if (dstHasSR && !newInfo.HasChannel(AE_CH_SR))
newInfo += AE_CH_SR;
}
*this = newInfo;
}
void CAEChannelInfo::Reset()
{
m_channelCount = 0;
for (AEChannel& channel : m_channels)
channel = AE_CH_NULL;
}
CAEChannelInfo& CAEChannelInfo::operator=(const CAEChannelInfo& rhs)
{
if (this == &rhs)
return *this;
/* clone the information */
m_channelCount = rhs.m_channelCount;
memcpy(m_channels, rhs.m_channels, sizeof(enum AEChannel) * rhs.m_channelCount);
return *this;
}
CAEChannelInfo& CAEChannelInfo::operator=(const enum AEChannel* rhs)
{
Reset();
if (rhs == NULL)
return *this;
while (m_channelCount < AE_CH_MAX && rhs[m_channelCount] != AE_CH_NULL)
{
m_channels[m_channelCount] = rhs[m_channelCount];
++m_channelCount;
}
/* the last entry should be NULL, if not we were passed a non null terminated list */
assert(rhs[m_channelCount] == AE_CH_NULL);
return *this;
}
CAEChannelInfo& CAEChannelInfo::operator=(const enum AEStdChLayout rhs)
{
assert(rhs > AE_CH_LAYOUT_INVALID && rhs < AE_CH_LAYOUT_MAX);
static constexpr enum AEChannel layouts[AE_CH_LAYOUT_MAX][9] = {
{AE_CH_FC, AE_CH_NULL},
{AE_CH_FL, AE_CH_FR, AE_CH_NULL},
{AE_CH_FL, AE_CH_FR, AE_CH_LFE, AE_CH_NULL},
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_NULL},
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_NULL},
{AE_CH_FL, AE_CH_FR, AE_CH_BL, AE_CH_BR, AE_CH_NULL},
{AE_CH_FL, AE_CH_FR, AE_CH_BL, AE_CH_BR, AE_CH_LFE, AE_CH_NULL},
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_BL, AE_CH_BR, AE_CH_NULL},
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_BL, AE_CH_BR, AE_CH_NULL},
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_BL, AE_CH_BR, AE_CH_SL, AE_CH_SR, AE_CH_NULL},
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_BL, AE_CH_BR, AE_CH_SL, AE_CH_SR,
AE_CH_NULL}};
*this = layouts[rhs];
return *this;
}
bool CAEChannelInfo::operator==(const CAEChannelInfo& rhs) const
{
/* if the channel count doesn't match, no need to check further */
if (m_channelCount != rhs.m_channelCount)
return false;
/* make sure the channel order is the same */
for (unsigned int i = 0; i < m_channelCount; ++i)
if (m_channels[i] != rhs.m_channels[i])
return false;
return true;
}
bool CAEChannelInfo::operator!=(const CAEChannelInfo& rhs) const
{
return !(*this == rhs);
}
CAEChannelInfo& CAEChannelInfo::operator+=(const enum AEChannel& rhs)
{
assert(m_channelCount < AE_CH_MAX);
assert(rhs > AE_CH_NULL && rhs < AE_CH_MAX);
m_channels[m_channelCount++] = rhs;
return *this;
}
CAEChannelInfo& CAEChannelInfo::operator-=(const enum AEChannel& rhs)
{
assert(rhs > AE_CH_NULL && rhs < AE_CH_MAX);
unsigned int i = 0;
while(i < m_channelCount && m_channels[i] != rhs)
i++;
if (i >= m_channelCount)
return *this; // Channel not found
for(; i < m_channelCount-1; i++)
m_channels[i] = m_channels[i+1];
m_channels[i] = AE_CH_NULL;
m_channelCount--;
return *this;
}
enum AEChannel CAEChannelInfo::operator[](unsigned int i) const
{
assert(i < m_channelCount);
return m_channels[i];
}
CAEChannelInfo::operator std::string() const
{
if (m_channelCount == 0)
return "NULL";
std::string s;
for (unsigned int i = 0; i < m_channelCount - 1; ++i)
{
s.append(GetChName(m_channels[i]));
s.append(", ");
}
s.append(GetChName(m_channels[m_channelCount-1]));
return s;
}
bool CAEChannelInfo::IsChannelValid(const unsigned int pos)
{
assert(pos < m_channelCount);
bool isValid = false;
if (m_channels[pos] > AE_CH_NULL && m_channels[pos] < AE_CH_UNKNOWN1)
isValid = true;
return isValid;
}
bool CAEChannelInfo::IsLayoutValid()
{
if (m_channelCount == 0)
return false;
for (unsigned int i = 0; i < m_channelCount; ++i)
{
// we need at least one valid channel
if (IsChannelValid(i))
return true;
}
return false;
}
const char* CAEChannelInfo::GetChName(const enum AEChannel ch)
{
assert(ch >= 0 && ch < AE_CH_MAX);
static const char* channels[AE_CH_MAX] =
{
"RAW" ,
"FL" , "FR" , "FC" , "LFE", "BL" , "BR" , "FLOC",
"FROC", "BC" , "SL" , "SR" , "TFL" , "TFR" , "TFC" ,
"TC" , "TBL", "TBR", "TBC", "BLOC", "BROC",
/* p16v devices */
"UNKNOWN1" , "UNKNOWN2" , "UNKNOWN3" , "UNKNOWN4" ,
"UNKNOWN5" , "UNKNOWN6" , "UNKNOWN7" , "UNKNOWN8" ,
"UNKNOWN9" , "UNKNOWN10", "UNKNOWN11", "UNKNOWN12",
"UNKNOWN13", "UNKNOWN14", "UNKNOWN15", "UNKNOWN16",
"UNKNOWN17", "UNKNOWN18", "UNKNOWN19", "UNKNOWN20",
"UNKNOWN21", "UNKNOWN22", "UNKNOWN23", "UNKNOWN24",
"UNKNOWN25", "UNKNOWN26", "UNKNOWN27", "UNKNOWN28",
"UNKNOWN29", "UNKNOWN30", "UNKNOWN31", "UNKNOWN32",
"UNKNOWN33", "UNKNOWN34", "UNKNOWN35", "UNKNOWN36",
"UNKNOWN37", "UNKNOWN38", "UNKNOWN39", "UNKNOWN40",
"UNKNOWN41", "UNKNOWN42", "UNKNOWN43", "UNKNOWN44",
"UNKNOWN45", "UNKNOWN46", "UNKNOWN47", "UNKNOWN48",
"UNKNOWN49", "UNKNOWN50", "UNKNOWN51", "UNKNOWN52",
"UNKNOWN53", "UNKNOWN54", "UNKNOWN55", "UNKNOWN56",
"UNKNOWN57", "UNKNOWN58", "UNKNOWN59", "UNKNOWN60",
"UNKNOWN61", "UNKNOWN62", "UNKNOWN63", "UNKNOWN64"
};
return channels[ch];
}
bool CAEChannelInfo::HasChannel(const enum AEChannel ch) const
{
for (unsigned int i = 0; i < m_channelCount; ++i)
if (m_channels[i] == ch)
return true;
return false;
}
bool CAEChannelInfo::ContainsChannels(const CAEChannelInfo& rhs) const
{
for (unsigned int i = 0; i < rhs.m_channelCount; ++i)
{
if (!HasChannel(rhs.m_channels[i]))
return false;
}
return true;
}
void CAEChannelInfo::ReplaceChannel(const enum AEChannel from, const enum AEChannel to)
{
for (unsigned int i = 0; i < m_channelCount; ++i)
{
if (m_channels[i] == from)
{
m_channels[i] = to;
break;
}
}
}
int CAEChannelInfo::BestMatch(const std::vector<CAEChannelInfo>& dsts, int* score) const
{
CAEChannelInfo availableDstChannels;
for (unsigned int i = 0; i < dsts.size(); i++)
availableDstChannels.AddMissingChannels(dsts[i]);
/* if we have channels not existing in any destination layout but that
* are remappable (e.g. RC => RL+RR), do those remaps */
CAEChannelInfo src(*this);
src.ResolveChannels(availableDstChannels);
bool remapped = (src != *this);
/* good enough approximation (does not account for added channels) */
int dropped = std::max((int)src.Count() - (int)Count(), 0);
int bestScore = std::numeric_limits<int>::min();
int bestMatch = -1;
for (unsigned int i = 0; i < dsts.size(); i++)
{
const CAEChannelInfo& dst = dsts[i];
int okChannels = 0;
for (unsigned int j = 0; j < src.Count(); j++)
okChannels += dst.HasChannel(src[j]);
int missingChannels = src.Count() - okChannels;
int extraChannels = dst.Count() - okChannels;
int curScore = 0 - (missingChannels + dropped) * 1000 - extraChannels * 10 - (remapped ? 1 : 0);
if (curScore > bestScore)
{
bestScore = curScore;
bestMatch = i;
if (curScore == 0)
break;
}
}
if (score)
*score = bestScore;
return bestMatch;
}
void CAEChannelInfo::AddMissingChannels(const CAEChannelInfo& rhs)
{
for (unsigned int i = 0; i < rhs.Count(); i++)
if (!HasChannel(rhs[i]))
*this += rhs[i];
}
|