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