-
Notifications
You must be signed in to change notification settings - Fork 42
/
chanmap.cpp
397 lines (378 loc) · 15.2 KB
/
chanmap.cpp
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
386
387
388
389
390
391
392
393
394
395
396
397
#include "chanmap.h"
#include <numeric>
#include <cassert>
#include "strutil.h"
namespace chanmap {
const char *getChannelName(uint32_t n)
{
const char *tab[] = {
"?","L","R","C","LFE","Rls","Rrs","Lc","Rc","Cs",
"Ls","Rs","Ts","Vhl","Vhc","Vhr","Tbl","Tbc","Tbr"
};
if (n <= 18) return tab[n];
switch (n) {
case 33: return "Rls";
case 34: return "Rrs";
case 35: return "Lw";
case 36: return "Rw";
}
return "?";
}
std::string getChannelNames(const std::vector<uint32_t> &channels)
{
const char *delim = "";
std::string result;
std::for_each(channels.begin(), channels.end(), [&](uint32_t c) {
result.append(delim).append(getChannelName(c));
delim = " ";
});
size_t lfe_count = std::count(channels.begin(), channels.end(), 4);
size_t count = channels.size() - lfe_count;
if (count <= 2 && lfe_count == 0)
return count == 1 ? "Mono" : "Stereo";
else
return strutil::format("%u.%u (%s)",
static_cast<uint32_t>(count),
static_cast<uint32_t>(lfe_count),
result.c_str());
}
uint32_t getChannelMask(const std::vector<uint32_t>& channels)
{
if (std::count_if(channels.begin(), channels.end(), [](uint32_t c) {
return c >= 33; }))
throw std::runtime_error("Not supported channel layout");
return std::accumulate(channels.begin(), channels.end(), 0,
[](uint32_t a, uint32_t c) {
return a | (1 << (c - 1));
});
}
std::vector<uint32_t> getChannels(uint32_t bitmap, uint32_t limit)
{
std::vector<uint32_t> channels;
for (unsigned i = 0; i < 32 && channels.size() < limit; ++i) {
if (bitmap & (1<<i))
channels.push_back(i + 1);
}
return channels;
}
std::vector<uint32_t> getChannels(const AudioChannelLayout *acl)
{
std::vector<uint32_t> channels;
uint32_t bitmap = 0;
const char *layout = 0;
switch (acl->mChannelLayoutTag) {
case kAudioChannelLayoutTag_UseChannelBitmap:
return getChannels(acl->mChannelBitmap);
case kAudioChannelLayoutTag_UseChannelDescriptions:
{
const AudioChannelDescription *desc = acl->mChannelDescriptions;
for (size_t i = 0; i < acl->mNumberChannelDescriptions; ++i)
channels.push_back(desc[i].mChannelLabel);
return channels;
}
/* 1ch */
case kAudioChannelLayoutTag_Mono:
layout = "\x03"; break;
/* 1.1ch */
case kAudioChannelLayoutTag_AC3_1_0_1:
layout = "\x03\x04"; break;
/* 2ch */
case kAudioChannelLayoutTag_Stereo:
case kAudioChannelLayoutTag_MatrixStereo:
case kAudioChannelLayoutTag_Binaural:
layout = "\x01\x02"; break;
/* 2.1ch */
case kAudioChannelLayoutTag_DVD_4:
layout = "\x01\x02\x04"; break;
/* 3ch */
case kAudioChannelLayoutTag_MPEG_3_0_A:
layout = "\x01\x02\x03"; break;
case kAudioChannelLayoutTag_AC3_3_0:
layout = "\x01\x03\x02"; break;
case kAudioChannelLayoutTag_MPEG_3_0_B:
layout = "\x03\x01\x02"; break;
case kAudioChannelLayoutTag_ITU_2_1:
layout = "\x01\x02\x09"; break;
/* 3.1ch */
case kAudioChannelLayoutTag_DVD_10:
layout = "\x01\x02\x03\x04"; break;
case kAudioChannelLayoutTag_AC3_3_0_1:
layout = "\x01\x03\x02\x04"; break;
case kAudioChannelLayoutTag_DVD_5:
layout = "\x01\x02\x04\x09"; break;
case kAudioChannelLayoutTag_AC3_2_1_1:
layout = "\x01\x02\x09\x04"; break;
case kAudioChannelLayoutTag_DTS_3_1:
layout = "\x03\x01\x02\x04"; break;
/* 4ch */
case kAudioChannelLayoutTag_Quadraphonic:
case kAudioChannelLayoutTag_ITU_2_2:
layout = "\x01\x02\x0A\x0B"; break;
case kAudioChannelLayoutTag_MPEG_4_0_A:
layout = "\x01\x02\x03\x09"; break;
case kAudioChannelLayoutTag_MPEG_4_0_B:
layout = "\x03\x01\x02\x09"; break;
case kAudioChannelLayoutTag_AC3_3_1:
layout = "\x01\x03\x02\x09"; break;
case kAudioChannelLayoutTag_WAVE_4_0_B:
layout = "\x01\x02\x05\x06"; break;
case kAudioChannelLayoutTag_Logic_4_0_C:
layout = "\x01\x02\x09\x03"; break;
/* 4.1ch */
case kAudioChannelLayoutTag_DVD_6:
layout = "\x01\x02\x04\x0A\x0B"; break;
case kAudioChannelLayoutTag_DVD_18:
layout = "\x01\x02\x0A\x0B\x04"; break;
case kAudioChannelLayoutTag_DVD_11:
layout = "\x01\x02\x03\x04\x09"; break;
case kAudioChannelLayoutTag_AC3_3_1_1:
layout = "\x01\x03\x02\x09\x04"; break;
case kAudioChannelLayoutTag_DTS_4_1:
layout = "\x03\x01\x02\x09\x04"; break;
/* 5ch */
case kAudioChannelLayoutTag_MPEG_5_0_A:
layout = "\x01\x02\x03\x0A\x0B"; break;
case kAudioChannelLayoutTag_Pentagonal:
case kAudioChannelLayoutTag_MPEG_5_0_B:
layout = "\x01\x02\x0A\x0B\x03"; break;
case kAudioChannelLayoutTag_MPEG_5_0_C:
layout = "\x01\x03\x02\x0A\x0B"; break;
case kAudioChannelLayoutTag_MPEG_5_0_D:
layout = "\x03\x01\x02\x0A\x0B"; break;
case kAudioChannelLayoutTag_WAVE_5_0_B:
layout = "\x01\x02\x03\x05\x06"; break;
/* 5.1ch */
case kAudioChannelLayoutTag_MPEG_5_1_A:
layout = "\x01\x02\x03\x04\x0A\x0B"; break;
case kAudioChannelLayoutTag_MPEG_5_1_B:
layout = "\x01\x02\x0A\x0B\x03\x04"; break;
case kAudioChannelLayoutTag_MPEG_5_1_C:
layout = "\x01\x03\x02\x0A\x0B\x04"; break;
case kAudioChannelLayoutTag_MPEG_5_1_D:
layout = "\x03\x01\x02\x0A\x0B\x04"; break;
case kAudioChannelLayoutTag_WAVE_5_1_B:
layout = "\x01\x02\x03\x04\x05\x06"; break;
/* 6ch */
case kAudioChannelLayoutTag_Hexagonal:
case kAudioChannelLayoutTag_AudioUnit_6_0:
layout = "\x01\x02\x0A\x0B\x03\x09"; break;
case kAudioChannelLayoutTag_AAC_6_0:
layout = "\x03\x01\x02\x0A\x0B\x09"; break;
case kAudioChannelLayoutTag_EAC_6_0_A:
layout = "\x01\x03\x02\x0A\x0B\x09"; break;
case kAudioChannelLayoutTag_DTS_6_0_A:
layout = "\x07\x08\x01\x02\x0A\x0B"; break;
case kAudioChannelLayoutTag_DTS_6_0_B:
layout = "\x03\x01\x02\x05\x06\x0C"; break;
case kAudioChannelLayoutTag_DTS_6_0_C:
layout = "\x03\x09\x01\x02\x05\x06"; break;
case kAudioChannelLayoutTag_Logic_6_0_B:
layout = "\x01\x02\x0A\x0B\x09\x03"; break;
/* 6.1ch */
case kAudioChannelLayoutTag_MPEG_6_1_A:
layout = "\x01\x02\x03\x04\x0A\x0B\x09"; break;
case kAudioChannelLayoutTag_AAC_6_1:
layout = "\x03\x01\x02\x0A\x0B\x09\x04"; break;
case kAudioChannelLayoutTag_EAC3_6_1_A:
layout = "\x01\x03\x02\x0A\x0B\x04\x09"; break;
case kAudioChannelLayoutTag_EAC3_6_1_B:
layout = "\x01\x03\x02\x0A\x0B\x04\x0C"; break;
case kAudioChannelLayoutTag_EAC3_6_1_C:
layout = "\x01\x03\x02\x0A\x0B\x04\x0E"; break;
case kAudioChannelLayoutTag_DTS_6_1_A:
layout = "\x07\x08\x01\x02\x0A\x0B\x04"; break;
case kAudioChannelLayoutTag_DTS_6_1_B:
layout = "\x03\x01\x02\x05\x06\x0C\x04"; break;
case kAudioChannelLayoutTag_DTS_6_1_C:
layout = "\x03\x09\x01\x02\x05\x06\x04"; break;
case kAudioChannelLayoutTag_DTS_6_1_D:
layout = "\x03\x01\x02\x0A\x0B\x04\x09"; break;
case kAudioChannelLayoutTag_WAVE_6_1:
layout = "\x01\x02\x03\x04\x09\x0A\x0B"; break;
case kAudioChannelLayoutTag_Logic_6_1_B:
layout = "\x01\x02\x0A\x0B\x09\x03\x04"; break;
case kAudioChannelLayoutTag_Logic_6_1_D:
layout = "\x01\x03\x02\x0A\x09\x0B\x04"; break;
/* 7ch */
case kAudioChannelLayoutTag_AudioUnit_7_0:
layout = "\x01\x02\x0A\x0B\x03\x05\x06"; break;
case kAudioChannelLayoutTag_AudioUnit_7_0_Front:
layout = "\x01\x02\x0A\x0B\x03\x07\x08"; break;
case kAudioChannelLayoutTag_AAC_7_0:
layout = "\x03\x01\x02\x0A\x0B\x05\x06"; break;
case kAudioChannelLayoutTag_EAC_7_0_A:
layout = "\x01\x03\x02\x0A\x0B\x05\x06"; break;
case kAudioChannelLayoutTag_DTS_7_0:
layout = "\x07\x03\x08\x01\x02\x0A\x0B"; break;
/* 7.1ch */
case kAudioChannelLayoutTag_MPEG_7_1_A:
layout = "\x01\x02\x03\x04\x0A\x0B\x07\x08"; break;
case kAudioChannelLayoutTag_MPEG_7_1_B:
layout = "\x03\x07\x08\x01\x02\x0A\x0B\x04"; break;
case kAudioChannelLayoutTag_MPEG_7_1_C:
layout = "\x01\x02\x03\x04\x0A\x0B\x05\x06"; break;
case kAudioChannelLayoutTag_Emagic_Default_7_1:
layout = "\x01\x02\x0A\x0B\x03\x04\x07\x08"; break;
case kAudioChannelLayoutTag_AAC_7_1_B:
layout = "\x03\x01\x02\x0A\x0B\x05\x06\x04"; break;
case kAudioChannelLayoutTag_AAC_7_1_C:
layout = "\x03\x01\x02\x0A\x0B\x04\x0D\x0F"; break;
case kAudioChannelLayoutTag_EAC3_7_1_A:
layout = "\x01\x03\x02\x0A\x0B\x04\x05\x06"; break;
case kAudioChannelLayoutTag_EAC3_7_1_B:
layout = "\x01\x03\x02\x0A\x0B\x04\x07\x08"; break;
case kAudioChannelLayoutTag_EAC3_7_1_E:
layout = "\x01\x03\x02\x0A\x0B\x04\x0D\x0F"; break;
case kAudioChannelLayoutTag_EAC3_7_1_F:
layout = "\x01\x03\x02\x0A\x0B\x04\x09\x0C"; break;
case kAudioChannelLayoutTag_EAC3_7_1_G:
layout = "\x01\x03\x02\x0A\x0B\x04\x09\x0E"; break;
case kAudioChannelLayoutTag_EAC3_7_1_H:
layout = "\x01\x03\x02\x0A\x0B\x04\x0C\x0E"; break;
case kAudioChannelLayoutTag_DTS_7_1:
layout = "\x07\x03\x08\x01\x02\x0A\x0B\x04"; break;
case kAudioChannelLayoutTag_WAVE_7_1:
layout = "\x01\x02\x03\x04\x05\x06\x0A\x0B"; break;
case kAudioChannelLayoutTag_Logic_7_1_B:
layout = "\x01\x02\x0A\x0B\x05\x06\x03\x04"; break;
/* 8ch */
case kAudioChannelLayoutTag_Octagonal:
layout = "\x01\x02\x05\x06\x03\x09\x0A\x0B"; break;
case kAudioChannelLayoutTag_AAC_Octagonal:
layout = "\x03\x01\x02\x0A\x0B\x05\x06\x09"; break;
case kAudioChannelLayoutTag_DTS_8_0_A:
layout = "\x07\x08\x01\x02\x0A\x0B\x05\x06"; break;
case kAudioChannelLayoutTag_DTS_8_0_B:
layout = "\x07\x03\x08\x01\x02\x0A\x09\x0B"; break;
default:
throw std::runtime_error("Unsupported channel layout");
}
while (*layout) channels.push_back(*layout++);
return channels;
}
std::vector<uint32_t>
convertFromAppleLayout(const std::vector<uint32_t> &channels)
{
if (!std::count_if(channels.begin(), channels.end(),
[](uint32_t c) {
return c > 18;
}))
return channels;
std::vector<uint32_t> v(channels.size());
std::transform(channels.begin(), channels.end(), v.begin(),
[](uint32_t x) -> uint32_t {
switch (x) {
case kAudioChannelLabel_Mono:
return kAudioChannelLabel_Center;
case kAudioChannelLabel_HeadphonesLeft:
return kAudioChannelLabel_Left;
case kAudioChannelLabel_HeadphonesRight:
return kAudioChannelLabel_Right;
}
return x;
});
size_t back = std::count(v.begin(), v.end(), 5) +
std::count(v.begin(), v.end(), 6);
size_t side = std::count(v.begin(), v.end(), 10) +
std::count(v.begin(), v.end(), 11);
std::transform(v.begin(), v.end(), v.begin(), [&](uint32_t c) -> uint32_t {
switch (c) {
case kAudioChannelLabel_LeftSurround:
case kAudioChannelLabel_RightSurround:
if (!side) c += 5;
break;
case kAudioChannelLabel_RearSurroundLeft:
case kAudioChannelLabel_RearSurroundRight:
if (!back || !side) c -= 28;
break;
};
return c;
});
return v;
}
std::vector<uint32_t>
getMappingToUSBOrder(const std::vector<uint32_t> &channels)
{
std::vector<uint32_t> index(channels.size());
std::iota(index.begin(), index.end(), 1);
std::sort(index.begin(), index.end(), [&](uint32_t a, uint32_t b) {
return channels[a-1] < channels[b-1];
});
return index;
}
uint32_t defaultChannelMask(const uint32_t nchannels)
{
static const uint32_t tab[] = {
0x4, // FC
0x3, // FL FR
0x7, // FL FR FC
0x33, // FL FR BL BR
0x37, // FL FR FC BL BR
0x3f, // FL FR FC LFE BL BR
0x13f, // FL FR FC LFE BL BR BC
0x63f // FL FR FC LFE BL BR SL SR
};
return tab[nchannels - 1];
}
uint32_t AACLayoutFromBitmap(uint32_t bitmap)
{
if ((bitmap & 0x600) == 0x600 && (bitmap & 0x30) == 0) {
bitmap &= ~0x600;
bitmap |= 0x30;
}
switch (bitmap) {
case 0x4: return kAudioChannelLayoutTag_Mono;
case 0x3: return kAudioChannelLayoutTag_Stereo;
case 0x7: return kAudioChannelLayoutTag_AAC_3_0;
case 0x33: return kAudioChannelLayoutTag_Quadraphonic;
case 0x107: return kAudioChannelLayoutTag_AAC_4_0;
case 0x1c4: return kAudioChannelLayoutTag_AAC_4_0;
case 0x37: return kAudioChannelLayoutTag_AAC_5_0;
case 0x3f: return kAudioChannelLayoutTag_AAC_5_1;
case 0x137: return kAudioChannelLayoutTag_AAC_6_0;
case 0x13f: return kAudioChannelLayoutTag_AAC_6_1;
case 0x637: return kAudioChannelLayoutTag_AAC_7_0;
case 0xff: return kAudioChannelLayoutTag_AAC_7_1;
case 0x63f: return kAudioChannelLayoutTag_AAC_7_1_B;
case 0x737: return kAudioChannelLayoutTag_AAC_Octagonal;
}
throw std::runtime_error("No channel mapping to AAC defined");
}
std::vector<uint32_t> getMappingToAAC(uint32_t bitmap)
{
AudioChannelLayout aacLayout = { 0 };
aacLayout.mChannelLayoutTag = AACLayoutFromBitmap(bitmap);
auto cs = getChannels(&aacLayout);
/*
* rewrite channels in pre-defined AAC channel layout to match with
* input channel bitmap
*/
std::transform(cs.begin(), cs.end(), cs.begin(),
[&](uint32_t c) -> uint32_t {
switch (c) {
case kAudioChannelLabel_Mono:
c = kAudioChannelLabel_Center;
break;
case kAudioChannelLabel_Left:
case kAudioChannelLabel_Right:
if (!(bitmap & 0x3) && (bitmap & 0xc))
c += 6;
break;
case kAudioChannelLabel_LeftSurround:
case kAudioChannelLabel_RightSurround:
if (bitmap & 0x600)
c += 5;
break;
case kAudioChannelLabel_RearSurroundLeft:
case kAudioChannelLabel_RearSurroundRight:
c -= 28;
break;
}
return c;
});
assert(getChannelMask(cs) == bitmap);
auto mapping = getMappingToUSBOrder(cs);
mapping = getMappingToUSBOrder(mapping);
return mapping;
}
} // namespace