merge 'lennart' branch back into trunk.
[platform/upstream/pulseaudio.git] / src / pulse / channelmap.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2005-2006 Lennart Poettering
7   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9   PulseAudio is free software; you can redistribute it and/or modify
10   it under the terms of the GNU Lesser General Public License as published
11   by the Free Software Foundation; either version 2 of the License,
12   or (at your option) any later version.
13
14   PulseAudio is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with PulseAudio; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include <pulse/xmalloc.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/macro.h>
36
37 #include "channelmap.h"
38
39 const char *const table[] = {
40     [PA_CHANNEL_POSITION_MONO] = "mono",
41
42     [PA_CHANNEL_POSITION_FRONT_CENTER] = "front-center",
43     [PA_CHANNEL_POSITION_FRONT_LEFT] = "front-left",
44     [PA_CHANNEL_POSITION_FRONT_RIGHT] = "front-right",
45
46     [PA_CHANNEL_POSITION_REAR_CENTER] = "rear-center",
47     [PA_CHANNEL_POSITION_REAR_LEFT] = "rear-left",
48     [PA_CHANNEL_POSITION_REAR_RIGHT] = "rear-right",
49
50     [PA_CHANNEL_POSITION_LFE] = "lfe",
51
52     [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = "front-left-of-center",
53     [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = "front-right-of-center",
54
55     [PA_CHANNEL_POSITION_SIDE_LEFT] = "side-left",
56     [PA_CHANNEL_POSITION_SIDE_RIGHT] = "side-right",
57
58     [PA_CHANNEL_POSITION_AUX0] = "aux0",
59     [PA_CHANNEL_POSITION_AUX1] = "aux1",
60     [PA_CHANNEL_POSITION_AUX2] = "aux2",
61     [PA_CHANNEL_POSITION_AUX3] = "aux3",
62     [PA_CHANNEL_POSITION_AUX4] = "aux4",
63     [PA_CHANNEL_POSITION_AUX5] = "aux5",
64     [PA_CHANNEL_POSITION_AUX6] = "aux6",
65     [PA_CHANNEL_POSITION_AUX7] = "aux7",
66     [PA_CHANNEL_POSITION_AUX8] = "aux8",
67     [PA_CHANNEL_POSITION_AUX9] = "aux9",
68     [PA_CHANNEL_POSITION_AUX10] = "aux10",
69     [PA_CHANNEL_POSITION_AUX11] = "aux11",
70     [PA_CHANNEL_POSITION_AUX12] = "aux12",
71     [PA_CHANNEL_POSITION_AUX13] = "aux13",
72     [PA_CHANNEL_POSITION_AUX14] = "aux14",
73     [PA_CHANNEL_POSITION_AUX15] = "aux15",
74     [PA_CHANNEL_POSITION_AUX16] = "aux16",
75     [PA_CHANNEL_POSITION_AUX17] = "aux17",
76     [PA_CHANNEL_POSITION_AUX18] = "aux18",
77     [PA_CHANNEL_POSITION_AUX19] = "aux19",
78     [PA_CHANNEL_POSITION_AUX20] = "aux20",
79     [PA_CHANNEL_POSITION_AUX21] = "aux21",
80     [PA_CHANNEL_POSITION_AUX22] = "aux22",
81     [PA_CHANNEL_POSITION_AUX23] = "aux23",
82     [PA_CHANNEL_POSITION_AUX24] = "aux24",
83     [PA_CHANNEL_POSITION_AUX25] = "aux25",
84     [PA_CHANNEL_POSITION_AUX26] = "aux26",
85     [PA_CHANNEL_POSITION_AUX27] = "aux27",
86     [PA_CHANNEL_POSITION_AUX28] = "aux28",
87     [PA_CHANNEL_POSITION_AUX29] = "aux29",
88     [PA_CHANNEL_POSITION_AUX30] = "aux30",
89     [PA_CHANNEL_POSITION_AUX31] = "aux31",
90
91     [PA_CHANNEL_POSITION_TOP_CENTER] = "top-center",
92
93     [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = "top-front-center",
94     [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = "top-front-left",
95     [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = "top-front-right",
96
97     [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = "top-rear-center",
98     [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = "top-rear-left",
99     [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = "top-rear-right"
100 };
101
102 const char *const pretty_table[] = {
103     [PA_CHANNEL_POSITION_MONO] = "Mono",
104
105     [PA_CHANNEL_POSITION_FRONT_CENTER] = "Front Center",
106     [PA_CHANNEL_POSITION_FRONT_LEFT] = "Front Left",
107     [PA_CHANNEL_POSITION_FRONT_RIGHT] = "Front Right",
108
109     [PA_CHANNEL_POSITION_REAR_CENTER] = "Rear Center",
110     [PA_CHANNEL_POSITION_REAR_LEFT] = "Rear Left",
111     [PA_CHANNEL_POSITION_REAR_RIGHT] = "Rear Right",
112
113     [PA_CHANNEL_POSITION_LFE] = "Low Frequency Emmiter",
114
115     [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = "Front Left-of-center",
116     [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = "Front Right-of-center",
117
118     [PA_CHANNEL_POSITION_SIDE_LEFT] = "Side Left",
119     [PA_CHANNEL_POSITION_SIDE_RIGHT] = "Side Right",
120
121     [PA_CHANNEL_POSITION_AUX0] = "Auxiliary 0",
122     [PA_CHANNEL_POSITION_AUX1] = "Auxiliary 1",
123     [PA_CHANNEL_POSITION_AUX2] = "Auxiliary 2",
124     [PA_CHANNEL_POSITION_AUX3] = "Auxiliary 3",
125     [PA_CHANNEL_POSITION_AUX4] = "Auxiliary 4",
126     [PA_CHANNEL_POSITION_AUX5] = "Auxiliary 5",
127     [PA_CHANNEL_POSITION_AUX6] = "Auxiliary 6",
128     [PA_CHANNEL_POSITION_AUX7] = "Auxiliary 7",
129     [PA_CHANNEL_POSITION_AUX8] = "Auxiliary 8",
130     [PA_CHANNEL_POSITION_AUX9] = "Auxiliary 9",
131     [PA_CHANNEL_POSITION_AUX10] = "Auxiliary 10",
132     [PA_CHANNEL_POSITION_AUX11] = "Auxiliary 11",
133     [PA_CHANNEL_POSITION_AUX12] = "Auxiliary 12",
134     [PA_CHANNEL_POSITION_AUX13] = "Auxiliary 13",
135     [PA_CHANNEL_POSITION_AUX14] = "Auxiliary 14",
136     [PA_CHANNEL_POSITION_AUX15] = "Auxiliary 15",
137     [PA_CHANNEL_POSITION_AUX16] = "Auxiliary 16",
138     [PA_CHANNEL_POSITION_AUX17] = "Auxiliary 17",
139     [PA_CHANNEL_POSITION_AUX18] = "Auxiliary 18",
140     [PA_CHANNEL_POSITION_AUX19] = "Auxiliary 19",
141     [PA_CHANNEL_POSITION_AUX20] = "Auxiliary 20",
142     [PA_CHANNEL_POSITION_AUX21] = "Auxiliary 21",
143     [PA_CHANNEL_POSITION_AUX22] = "Auxiliary 22",
144     [PA_CHANNEL_POSITION_AUX23] = "Auxiliary 23",
145     [PA_CHANNEL_POSITION_AUX24] = "Auxiliary 24",
146     [PA_CHANNEL_POSITION_AUX25] = "Auxiliary 25",
147     [PA_CHANNEL_POSITION_AUX26] = "Auxiliary 26",
148     [PA_CHANNEL_POSITION_AUX27] = "Auxiliary 27",
149     [PA_CHANNEL_POSITION_AUX28] = "Auxiliary 28",
150     [PA_CHANNEL_POSITION_AUX29] = "Auxiliary 29",
151     [PA_CHANNEL_POSITION_AUX30] = "Auxiliary 30",
152     [PA_CHANNEL_POSITION_AUX31] = "Auxiliary 31",
153
154     [PA_CHANNEL_POSITION_TOP_CENTER] = "Top Center",
155
156     [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = "Top Front Center",
157     [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = "Top Front Left",
158     [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = "Top Front Right",
159
160     [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = "Top Rear Center",
161     [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = "Top Rear left",
162     [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = "Top Rear Right"
163 };
164
165 pa_channel_map* pa_channel_map_init(pa_channel_map *m) {
166     unsigned c;
167     pa_assert(m);
168
169     m->channels = 0;
170
171     for (c = 0; c < PA_CHANNELS_MAX; c++)
172         m->map[c] = PA_CHANNEL_POSITION_INVALID;
173
174     return m;
175 }
176
177 pa_channel_map* pa_channel_map_init_mono(pa_channel_map *m) {
178     pa_assert(m);
179
180     pa_channel_map_init(m);
181
182     m->channels = 1;
183     m->map[0] = PA_CHANNEL_POSITION_MONO;
184     return m;
185 }
186
187 pa_channel_map* pa_channel_map_init_stereo(pa_channel_map *m) {
188     pa_assert(m);
189
190     pa_channel_map_init(m);
191
192     m->channels = 2;
193     m->map[0] = PA_CHANNEL_POSITION_LEFT;
194     m->map[1] = PA_CHANNEL_POSITION_RIGHT;
195     return m;
196 }
197
198 pa_channel_map* pa_channel_map_init_auto(pa_channel_map *m, unsigned channels, pa_channel_map_def_t def) {
199     pa_assert(m);
200     pa_assert(channels > 0);
201     pa_assert(channels <= PA_CHANNELS_MAX);
202
203     pa_channel_map_init(m);
204
205     m->channels = 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             return NULL;
396     }
397 }
398
399
400 const char* pa_channel_position_to_string(pa_channel_position_t pos) {
401
402     if (pos < 0 || pos >= PA_CHANNEL_POSITION_MAX)
403         return NULL;
404
405     return table[pos];
406 }
407
408 const char* pa_channel_position_to_pretty_string(pa_channel_position_t pos) {
409     if (pos < 0 || pos >= PA_CHANNEL_POSITION_MAX)
410         return NULL;
411
412     return pretty_table[pos];
413 }
414
415 int pa_channel_map_equal(const pa_channel_map *a, const pa_channel_map *b) {
416     unsigned c;
417
418     pa_assert(a);
419     pa_assert(b);
420
421     if (a->channels != b->channels)
422         return 0;
423
424     for (c = 0; c < a->channels; c++)
425         if (a->map[c] != b->map[c])
426             return 0;
427
428     return 1;
429 }
430
431 char* pa_channel_map_snprint(char *s, size_t l, const pa_channel_map *map) {
432     unsigned channel;
433     int first = 1;
434     char *e;
435
436     pa_assert(s);
437     pa_assert(l > 0);
438     pa_assert(map);
439
440     *(e = s) = 0;
441
442     for (channel = 0; channel < map->channels && l > 1; channel++) {
443         l -= pa_snprintf(e, l, "%s%s",
444                       first ? "" : ",",
445                       pa_channel_position_to_string(map->map[channel]));
446
447         e = strchr(e, 0);
448         first = 0;
449     }
450
451     return s;
452 }
453
454 pa_channel_map *pa_channel_map_parse(pa_channel_map *rmap, const char *s) {
455     const char *state;
456     pa_channel_map map;
457     char *p;
458
459     pa_assert(rmap);
460     pa_assert(s);
461
462     memset(&map, 0, sizeof(map));
463
464     if (strcmp(s, "stereo") == 0) {
465         map.channels = 2;
466         map.map[0] = PA_CHANNEL_POSITION_LEFT;
467         map.map[1] = PA_CHANNEL_POSITION_RIGHT;
468         goto finish;
469     }
470
471     state = NULL;
472     map.channels = 0;
473
474     while ((p = pa_split(s, ",", &state))) {
475
476         if (map.channels >= PA_CHANNELS_MAX) {
477             pa_xfree(p);
478             return NULL;
479         }
480
481         /* Some special aliases */
482         if (strcmp(p, "left") == 0)
483             map.map[map.channels++] = PA_CHANNEL_POSITION_LEFT;
484         else if (strcmp(p, "right") == 0)
485             map.map[map.channels++] = PA_CHANNEL_POSITION_RIGHT;
486         else if (strcmp(p, "center") == 0)
487             map.map[map.channels++] = PA_CHANNEL_POSITION_CENTER;
488         else if (strcmp(p, "subwoofer") == 0)
489             map.map[map.channels++] = PA_CHANNEL_POSITION_SUBWOOFER;
490         else {
491             pa_channel_position_t i;
492
493             for (i = 0; i < PA_CHANNEL_POSITION_MAX; i++)
494                 if (strcmp(p, table[i]) == 0) {
495                     map.map[map.channels++] = i;
496                     break;
497                 }
498
499             if (i >= PA_CHANNEL_POSITION_MAX) {
500                 pa_xfree(p);
501                 return NULL;
502             }
503         }
504
505         pa_xfree(p);
506     }
507
508 finish:
509
510     if (!pa_channel_map_valid(&map))
511         return NULL;
512
513     *rmap = map;
514     return rmap;
515 }
516
517 int pa_channel_map_valid(const pa_channel_map *map) {
518     unsigned c;
519
520     pa_assert(map);
521
522     if (map->channels <= 0 || map->channels > PA_CHANNELS_MAX)
523         return 0;
524
525     for (c = 0; c < map->channels; c++) {
526
527         if (map->map[c] < 0 ||map->map[c] >= PA_CHANNEL_POSITION_MAX)
528             return 0;
529
530     }
531
532     return 1;
533 }
534