]> code.delx.au - pulseaudio/blob - src/pulse/channelmap.c
introduce upper channel map definition limit PA_CHANNEL_MAP_DEF_MAX
[pulseaudio] / src / pulse / channelmap.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2005-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <pulse/xmalloc.h>
32 #include <pulse/i18n.h>
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/macro.h>
35
36 #include "channelmap.h"
37
38 const char *const table[PA_CHANNEL_POSITION_MAX] = {
39 [PA_CHANNEL_POSITION_MONO] = "mono",
40
41 [PA_CHANNEL_POSITION_FRONT_CENTER] = "front-center",
42 [PA_CHANNEL_POSITION_FRONT_LEFT] = "front-left",
43 [PA_CHANNEL_POSITION_FRONT_RIGHT] = "front-right",
44
45 [PA_CHANNEL_POSITION_REAR_CENTER] = "rear-center",
46 [PA_CHANNEL_POSITION_REAR_LEFT] = "rear-left",
47 [PA_CHANNEL_POSITION_REAR_RIGHT] = "rear-right",
48
49 [PA_CHANNEL_POSITION_LFE] = "lfe",
50
51 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = "front-left-of-center",
52 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = "front-right-of-center",
53
54 [PA_CHANNEL_POSITION_SIDE_LEFT] = "side-left",
55 [PA_CHANNEL_POSITION_SIDE_RIGHT] = "side-right",
56
57 [PA_CHANNEL_POSITION_AUX0] = "aux0",
58 [PA_CHANNEL_POSITION_AUX1] = "aux1",
59 [PA_CHANNEL_POSITION_AUX2] = "aux2",
60 [PA_CHANNEL_POSITION_AUX3] = "aux3",
61 [PA_CHANNEL_POSITION_AUX4] = "aux4",
62 [PA_CHANNEL_POSITION_AUX5] = "aux5",
63 [PA_CHANNEL_POSITION_AUX6] = "aux6",
64 [PA_CHANNEL_POSITION_AUX7] = "aux7",
65 [PA_CHANNEL_POSITION_AUX8] = "aux8",
66 [PA_CHANNEL_POSITION_AUX9] = "aux9",
67 [PA_CHANNEL_POSITION_AUX10] = "aux10",
68 [PA_CHANNEL_POSITION_AUX11] = "aux11",
69 [PA_CHANNEL_POSITION_AUX12] = "aux12",
70 [PA_CHANNEL_POSITION_AUX13] = "aux13",
71 [PA_CHANNEL_POSITION_AUX14] = "aux14",
72 [PA_CHANNEL_POSITION_AUX15] = "aux15",
73 [PA_CHANNEL_POSITION_AUX16] = "aux16",
74 [PA_CHANNEL_POSITION_AUX17] = "aux17",
75 [PA_CHANNEL_POSITION_AUX18] = "aux18",
76 [PA_CHANNEL_POSITION_AUX19] = "aux19",
77 [PA_CHANNEL_POSITION_AUX20] = "aux20",
78 [PA_CHANNEL_POSITION_AUX21] = "aux21",
79 [PA_CHANNEL_POSITION_AUX22] = "aux22",
80 [PA_CHANNEL_POSITION_AUX23] = "aux23",
81 [PA_CHANNEL_POSITION_AUX24] = "aux24",
82 [PA_CHANNEL_POSITION_AUX25] = "aux25",
83 [PA_CHANNEL_POSITION_AUX26] = "aux26",
84 [PA_CHANNEL_POSITION_AUX27] = "aux27",
85 [PA_CHANNEL_POSITION_AUX28] = "aux28",
86 [PA_CHANNEL_POSITION_AUX29] = "aux29",
87 [PA_CHANNEL_POSITION_AUX30] = "aux30",
88 [PA_CHANNEL_POSITION_AUX31] = "aux31",
89
90 [PA_CHANNEL_POSITION_TOP_CENTER] = "top-center",
91
92 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = "top-front-center",
93 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = "top-front-left",
94 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = "top-front-right",
95
96 [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = "top-rear-center",
97 [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = "top-rear-left",
98 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = "top-rear-right"
99 };
100
101 const char *const pretty_table[PA_CHANNEL_POSITION_MAX] = {
102 [PA_CHANNEL_POSITION_MONO] = N_("Mono"),
103
104 [PA_CHANNEL_POSITION_FRONT_CENTER] = N_("Front Center"),
105 [PA_CHANNEL_POSITION_FRONT_LEFT] = N_("Front Left"),
106 [PA_CHANNEL_POSITION_FRONT_RIGHT] = N_("Front Right"),
107
108 [PA_CHANNEL_POSITION_REAR_CENTER] = N_("Rear Center"),
109 [PA_CHANNEL_POSITION_REAR_LEFT] = N_("Rear Left"),
110 [PA_CHANNEL_POSITION_REAR_RIGHT] = N_("Rear Right"),
111
112 [PA_CHANNEL_POSITION_LFE] = N_("Low Frequency Emmiter"),
113
114 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = N_("Front Left-of-center"),
115 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = N_("Front Right-of-center"),
116
117 [PA_CHANNEL_POSITION_SIDE_LEFT] = N_("Side Left"),
118 [PA_CHANNEL_POSITION_SIDE_RIGHT] = N_("Side Right"),
119
120 [PA_CHANNEL_POSITION_AUX0] = N_("Auxiliary 0"),
121 [PA_CHANNEL_POSITION_AUX1] = N_("Auxiliary 1"),
122 [PA_CHANNEL_POSITION_AUX2] = N_("Auxiliary 2"),
123 [PA_CHANNEL_POSITION_AUX3] = N_("Auxiliary 3"),
124 [PA_CHANNEL_POSITION_AUX4] = N_("Auxiliary 4"),
125 [PA_CHANNEL_POSITION_AUX5] = N_("Auxiliary 5"),
126 [PA_CHANNEL_POSITION_AUX6] = N_("Auxiliary 6"),
127 [PA_CHANNEL_POSITION_AUX7] = N_("Auxiliary 7"),
128 [PA_CHANNEL_POSITION_AUX8] = N_("Auxiliary 8"),
129 [PA_CHANNEL_POSITION_AUX9] = N_("Auxiliary 9"),
130 [PA_CHANNEL_POSITION_AUX10] = N_("Auxiliary 10"),
131 [PA_CHANNEL_POSITION_AUX11] = N_("Auxiliary 11"),
132 [PA_CHANNEL_POSITION_AUX12] = N_("Auxiliary 12"),
133 [PA_CHANNEL_POSITION_AUX13] = N_("Auxiliary 13"),
134 [PA_CHANNEL_POSITION_AUX14] = N_("Auxiliary 14"),
135 [PA_CHANNEL_POSITION_AUX15] = N_("Auxiliary 15"),
136 [PA_CHANNEL_POSITION_AUX16] = N_("Auxiliary 16"),
137 [PA_CHANNEL_POSITION_AUX17] = N_("Auxiliary 17"),
138 [PA_CHANNEL_POSITION_AUX18] = N_("Auxiliary 18"),
139 [PA_CHANNEL_POSITION_AUX19] = N_("Auxiliary 19"),
140 [PA_CHANNEL_POSITION_AUX20] = N_("Auxiliary 20"),
141 [PA_CHANNEL_POSITION_AUX21] = N_("Auxiliary 21"),
142 [PA_CHANNEL_POSITION_AUX22] = N_("Auxiliary 22"),
143 [PA_CHANNEL_POSITION_AUX23] = N_("Auxiliary 23"),
144 [PA_CHANNEL_POSITION_AUX24] = N_("Auxiliary 24"),
145 [PA_CHANNEL_POSITION_AUX25] = N_("Auxiliary 25"),
146 [PA_CHANNEL_POSITION_AUX26] = N_("Auxiliary 26"),
147 [PA_CHANNEL_POSITION_AUX27] = N_("Auxiliary 27"),
148 [PA_CHANNEL_POSITION_AUX28] = N_("Auxiliary 28"),
149 [PA_CHANNEL_POSITION_AUX29] = N_("Auxiliary 29"),
150 [PA_CHANNEL_POSITION_AUX30] = N_("Auxiliary 30"),
151 [PA_CHANNEL_POSITION_AUX31] = N_("Auxiliary 31"),
152
153 [PA_CHANNEL_POSITION_TOP_CENTER] = N_("Top Center"),
154
155 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = N_("Top Front Center"),
156 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = N_("Top Front Left"),
157 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = N_("Top Front Right"),
158
159 [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = N_("Top Rear Center"),
160 [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = N_("Top Rear Left"),
161 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = N_("Top Rear Right")
162 };
163
164 pa_channel_map* pa_channel_map_init(pa_channel_map *m) {
165 unsigned c;
166 pa_assert(m);
167
168 m->channels = 0;
169
170 for (c = 0; c < PA_CHANNELS_MAX; c++)
171 m->map[c] = PA_CHANNEL_POSITION_INVALID;
172
173 return m;
174 }
175
176 pa_channel_map* pa_channel_map_init_mono(pa_channel_map *m) {
177 pa_assert(m);
178
179 pa_channel_map_init(m);
180
181 m->channels = 1;
182 m->map[0] = PA_CHANNEL_POSITION_MONO;
183 return m;
184 }
185
186 pa_channel_map* pa_channel_map_init_stereo(pa_channel_map *m) {
187 pa_assert(m);
188
189 pa_channel_map_init(m);
190
191 m->channels = 2;
192 m->map[0] = PA_CHANNEL_POSITION_LEFT;
193 m->map[1] = PA_CHANNEL_POSITION_RIGHT;
194 return m;
195 }
196
197 pa_channel_map* pa_channel_map_init_auto(pa_channel_map *m, unsigned channels, pa_channel_map_def_t def) {
198 pa_assert(m);
199 pa_assert(channels > 0);
200 pa_assert(channels <= PA_CHANNELS_MAX);
201 pa_assert(def < PA_CHANNEL_MAP_DEF_MAX);
202
203 pa_channel_map_init(m);
204
205 m->channels = (uint8_t) channels;
206
207 switch (def) {
208 case PA_CHANNEL_MAP_AIFF:
209
210 /* This is somewhat compatible with RFC3551 */
211
212 switch (channels) {
213 case 1:
214 m->map[0] = PA_CHANNEL_POSITION_MONO;
215 return m;
216
217 case 6:
218 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
219 m->map[1] = PA_CHANNEL_POSITION_SIDE_LEFT;
220 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
221 m->map[3] = PA_CHANNEL_POSITION_FRONT_RIGHT;
222 m->map[4] = PA_CHANNEL_POSITION_SIDE_RIGHT;
223 m->map[5] = PA_CHANNEL_POSITION_LFE;
224 return m;
225
226 case 5:
227 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
228 m->map[3] = PA_CHANNEL_POSITION_REAR_LEFT;
229 m->map[4] = PA_CHANNEL_POSITION_REAR_RIGHT;
230 /* Fall through */
231
232 case 2:
233 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
234 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
235 return m;
236
237 case 3:
238 m->map[0] = PA_CHANNEL_POSITION_LEFT;
239 m->map[1] = PA_CHANNEL_POSITION_RIGHT;
240 m->map[2] = PA_CHANNEL_POSITION_CENTER;
241 return m;
242
243 case 4:
244 m->map[0] = PA_CHANNEL_POSITION_LEFT;
245 m->map[1] = PA_CHANNEL_POSITION_CENTER;
246 m->map[2] = PA_CHANNEL_POSITION_RIGHT;
247 m->map[3] = PA_CHANNEL_POSITION_LFE;
248 return m;
249
250 default:
251 return NULL;
252 }
253
254 case PA_CHANNEL_MAP_ALSA:
255
256 switch (channels) {
257 case 1:
258 m->map[0] = PA_CHANNEL_POSITION_MONO;
259 return m;
260
261 case 8:
262 m->map[6] = PA_CHANNEL_POSITION_SIDE_LEFT;
263 m->map[7] = PA_CHANNEL_POSITION_SIDE_RIGHT;
264 /* Fall through */
265
266 case 6:
267 m->map[5] = PA_CHANNEL_POSITION_LFE;
268 /* Fall through */
269
270 case 5:
271 m->map[4] = PA_CHANNEL_POSITION_FRONT_CENTER;
272 /* Fall through */
273
274 case 4:
275 m->map[2] = PA_CHANNEL_POSITION_REAR_LEFT;
276 m->map[3] = PA_CHANNEL_POSITION_REAR_RIGHT;
277 /* Fall through */
278
279 case 2:
280 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
281 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
282 return m;
283
284 default:
285 return NULL;
286 }
287
288 case PA_CHANNEL_MAP_AUX: {
289 unsigned i;
290
291 if (channels >= PA_CHANNELS_MAX)
292 return NULL;
293
294 for (i = 0; i < channels; i++)
295 m->map[i] = PA_CHANNEL_POSITION_AUX0 + i;
296
297 return m;
298 }
299
300 case PA_CHANNEL_MAP_WAVEEX:
301
302 switch (channels) {
303 case 1:
304 m->map[0] = PA_CHANNEL_POSITION_MONO;
305 return m;
306
307 case 18:
308 m->map[15] = PA_CHANNEL_POSITION_TOP_REAR_LEFT;
309 m->map[16] = PA_CHANNEL_POSITION_TOP_REAR_CENTER;
310 m->map[17] = PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
311 /* Fall through */
312
313 case 15:
314 m->map[12] = PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
315 m->map[13] = PA_CHANNEL_POSITION_TOP_FRONT_CENTER;
316 m->map[14] = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
317 /* Fall through */
318
319 case 12:
320 m->map[11] = PA_CHANNEL_POSITION_TOP_CENTER;
321 /* Fall through */
322
323 case 11:
324 m->map[9] = PA_CHANNEL_POSITION_SIDE_LEFT;
325 m->map[10] = PA_CHANNEL_POSITION_SIDE_RIGHT;
326 /* Fall through */
327
328 case 9:
329 m->map[8] = PA_CHANNEL_POSITION_REAR_CENTER;
330 /* Fall through */
331
332 case 8:
333 m->map[6] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
334 m->map[7] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
335 /* Fall through */
336
337 case 6:
338 m->map[4] = PA_CHANNEL_POSITION_REAR_LEFT;
339 m->map[5] = PA_CHANNEL_POSITION_REAR_RIGHT;
340 /* Fall through */
341
342 case 4:
343 m->map[3] = PA_CHANNEL_POSITION_LFE;
344 /* Fall through */
345
346 case 3:
347 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
348 /* Fall through */
349
350 case 2:
351 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
352 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
353 return m;
354
355 default:
356 return NULL;
357 }
358
359 case PA_CHANNEL_MAP_OSS:
360
361 switch (channels) {
362 case 1:
363 m->map[0] = PA_CHANNEL_POSITION_MONO;
364 return m;
365
366 case 8:
367 m->map[6] = PA_CHANNEL_POSITION_REAR_LEFT;
368 m->map[7] = PA_CHANNEL_POSITION_REAR_RIGHT;
369 /* Fall through */
370
371 case 6:
372 m->map[4] = PA_CHANNEL_POSITION_SIDE_LEFT;
373 m->map[5] = PA_CHANNEL_POSITION_SIDE_RIGHT;
374 /* Fall through */
375
376 case 4:
377 m->map[3] = PA_CHANNEL_POSITION_LFE;
378 /* Fall through */
379
380 case 3:
381 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
382 /* Fall through */
383
384 case 2:
385 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
386 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
387 return m;
388
389 default:
390 return NULL;
391 }
392
393
394 default:
395 pa_assert_not_reached();
396 }
397 }
398
399 pa_channel_map* pa_channel_map_init_extend(pa_channel_map *m, unsigned channels, pa_channel_map_def_t def) {
400 unsigned c;
401
402 pa_assert(m);
403 pa_assert(channels > 0);
404 pa_assert(channels <= PA_CHANNELS_MAX);
405 pa_assert(def < PA_CHANNEL_MAP_DEF_MAX);
406
407 pa_channel_map_init(m);
408
409 for (c = channels; c > 0; c--) {
410
411 if (pa_channel_map_init_auto(m, c, def)) {
412 unsigned i = 0;
413
414 for (; c < channels; c++) {
415
416 m->map[c] = PA_CHANNEL_POSITION_AUX0 + i;
417 i++;
418 }
419
420 m->channels = (uint8_t) channels;
421
422 return m;
423 }
424 }
425
426 return NULL;
427 }
428
429 const char* pa_channel_position_to_string(pa_channel_position_t pos) {
430
431 if (pos < 0 || pos >= PA_CHANNEL_POSITION_MAX)
432 return NULL;
433
434 return table[pos];
435 }
436
437 const char* pa_channel_position_to_pretty_string(pa_channel_position_t pos) {
438
439 pa_init_i18n();
440
441 if (pos < 0 || pos >= PA_CHANNEL_POSITION_MAX)
442 return NULL;
443
444 return _(pretty_table[pos]);
445 }
446
447 int pa_channel_map_equal(const pa_channel_map *a, const pa_channel_map *b) {
448 unsigned c;
449
450 pa_assert(a);
451 pa_assert(b);
452
453 if (a->channels != b->channels)
454 return 0;
455
456 for (c = 0; c < a->channels; c++)
457 if (a->map[c] != b->map[c])
458 return 0;
459
460 return 1;
461 }
462
463 char* pa_channel_map_snprint(char *s, size_t l, const pa_channel_map *map) {
464 unsigned channel;
465 pa_bool_t first = TRUE;
466 char *e;
467
468 pa_assert(s);
469 pa_assert(l > 0);
470 pa_assert(map);
471
472 *(e = s) = 0;
473
474 for (channel = 0; channel < map->channels && l > 1; channel++) {
475 l -= pa_snprintf(e, l, "%s%s",
476 first ? "" : ",",
477 pa_channel_position_to_string(map->map[channel]));
478
479 e = strchr(e, 0);
480 first = FALSE;
481 }
482
483 return s;
484 }
485
486 pa_channel_map *pa_channel_map_parse(pa_channel_map *rmap, const char *s) {
487 const char *state;
488 pa_channel_map map;
489 char *p;
490
491 pa_assert(rmap);
492 pa_assert(s);
493
494 memset(&map, 0, sizeof(map));
495
496 if (strcmp(s, "stereo") == 0) {
497 map.channels = 2;
498 map.map[0] = PA_CHANNEL_POSITION_LEFT;
499 map.map[1] = PA_CHANNEL_POSITION_RIGHT;
500 goto finish;
501 }
502
503 state = NULL;
504 map.channels = 0;
505
506 while ((p = pa_split(s, ",", &state))) {
507
508 if (map.channels >= PA_CHANNELS_MAX) {
509 pa_xfree(p);
510 return NULL;
511 }
512
513 /* Some special aliases */
514 if (strcmp(p, "left") == 0)
515 map.map[map.channels++] = PA_CHANNEL_POSITION_LEFT;
516 else if (strcmp(p, "right") == 0)
517 map.map[map.channels++] = PA_CHANNEL_POSITION_RIGHT;
518 else if (strcmp(p, "center") == 0)
519 map.map[map.channels++] = PA_CHANNEL_POSITION_CENTER;
520 else if (strcmp(p, "subwoofer") == 0)
521 map.map[map.channels++] = PA_CHANNEL_POSITION_SUBWOOFER;
522 else {
523 pa_channel_position_t i;
524
525 for (i = 0; i < PA_CHANNEL_POSITION_MAX; i++)
526 if (strcmp(p, table[i]) == 0) {
527 map.map[map.channels++] = i;
528 break;
529 }
530
531 if (i >= PA_CHANNEL_POSITION_MAX) {
532 pa_xfree(p);
533 return NULL;
534 }
535 }
536
537 pa_xfree(p);
538 }
539
540 finish:
541
542 if (!pa_channel_map_valid(&map))
543 return NULL;
544
545 *rmap = map;
546 return rmap;
547 }
548
549 int pa_channel_map_valid(const pa_channel_map *map) {
550 unsigned c;
551
552 pa_assert(map);
553
554 if (map->channels <= 0 || map->channels > PA_CHANNELS_MAX)
555 return 0;
556
557 for (c = 0; c < map->channels; c++) {
558
559 if (map->map[c] < 0 ||map->map[c] >= PA_CHANNEL_POSITION_MAX)
560 return 0;
561 }
562
563 return 1;
564 }
565
566 int pa_channel_map_compatible(const pa_channel_map *map, const pa_sample_spec *ss) {
567 pa_assert(map);
568 pa_assert(ss);
569
570 return map->channels == ss->channels;
571 }