lavu: rename audioconvert.* to channel_layout.* and deprecate audioconvert.h
[libav.git] / libavutil / channel_layout.c
CommitLineData
176a6157
SS
1/*
2 * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 *
2912e87a 4 * This file is part of Libav.
176a6157 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
176a6157
SS
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
176a6157
SS
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
2912e87a 17 * License along with Libav; if not, write to the Free Software
176a6157
SS
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file
5980f5dd 23 * audio channel layout utility functions
176a6157
SS
24 */
25
dc25d79f
DB
26#include "avstring.h"
27#include "avutil.h"
5980f5dd 28#include "channel_layout.h"
1d9c2dc8 29#include "common.h"
176a6157
SS
30
31static const char * const channel_names[] = {
6cfed112
SS
32 [0] = "FL", /* front left */
33 [1] = "FR", /* front right */
34 [2] = "FC", /* front center */
35 [3] = "LFE", /* low frequency */
36 [4] = "BL", /* back left */
37 [5] = "BR", /* back right */
38 [6] = "FLC", /* front left-of-center */
39 [7] = "FRC", /* front right-of-center */
40 [8] = "BC", /* back-center */
41 [9] = "SL", /* side left */
42 [10] = "SR", /* side right */
43 [11] = "TC", /* top center */
44 [12] = "TFL", /* top front left */
45 [13] = "TFC", /* top front center */
46 [14] = "TFR", /* top front right */
47 [15] = "TBL", /* top back left */
48 [16] = "TBC", /* top back center */
49 [17] = "TBR", /* top back right */
50 [29] = "DL", /* downmix left */
51 [30] = "DR", /* downmix right */
147715ca
TW
52 [31] = "WL", /* wide left */
53 [32] = "WR", /* wide right */
54 [33] = "SDL", /* surround direct left */
55 [34] = "SDR", /* surround direct right */
8919fee7 56 [35] = "LFE2", /* low frequency 2 */
176a6157
SS
57};
58
59static const char *get_channel_name(int channel_id)
60{
61 if (channel_id < 0 || channel_id >= FF_ARRAY_ELEMS(channel_names))
62 return NULL;
63 return channel_names[channel_id];
64}
65
66static const struct {
67 const char *name;
68 int nb_channels;
cc276c85 69 uint64_t layout;
176a6157
SS
70} channel_layout_map[] = {
71 { "mono", 1, AV_CH_LAYOUT_MONO },
72 { "stereo", 2, AV_CH_LAYOUT_STEREO },
b38b6d56
T
73 { "stereo", 2, AV_CH_LAYOUT_STEREO_DOWNMIX },
74 { "2.1", 3, AV_CH_LAYOUT_2POINT1 },
75 { "3.0", 3, AV_CH_LAYOUT_SURROUND },
76 { "3.0(back)", 3, AV_CH_LAYOUT_2_1 },
77 { "3.1", 4, AV_CH_LAYOUT_3POINT1 },
176a6157
SS
78 { "4.0", 4, AV_CH_LAYOUT_4POINT0 },
79 { "quad", 4, AV_CH_LAYOUT_QUAD },
b38b6d56
T
80 { "quad(side)", 4, AV_CH_LAYOUT_2_2 },
81 { "4.1", 5, AV_CH_LAYOUT_4POINT1 },
176a6157
SS
82 { "5.0", 5, AV_CH_LAYOUT_5POINT0 },
83 { "5.0", 5, AV_CH_LAYOUT_5POINT0_BACK },
84 { "5.1", 6, AV_CH_LAYOUT_5POINT1 },
85 { "5.1", 6, AV_CH_LAYOUT_5POINT1_BACK },
b38b6d56
T
86 { "6.0", 6, AV_CH_LAYOUT_6POINT0 },
87 { "6.0(front)", 6, AV_CH_LAYOUT_6POINT0_FRONT },
88 { "hexagonal", 6, AV_CH_LAYOUT_HEXAGONAL },
89 { "6.1", 7, AV_CH_LAYOUT_6POINT1 },
90 { "6.1", 7, AV_CH_LAYOUT_6POINT1_BACK },
91 { "6.1(front)", 7, AV_CH_LAYOUT_6POINT1_FRONT },
b38b6d56
T
92 { "7.0", 7, AV_CH_LAYOUT_7POINT0 },
93 { "7.0(front)", 7, AV_CH_LAYOUT_7POINT0_FRONT },
176a6157
SS
94 { "7.1", 8, AV_CH_LAYOUT_7POINT1 },
95 { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE },
b38b6d56 96 { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE_BACK },
b38b6d56 97 { "octagonal", 8, AV_CH_LAYOUT_OCTAGONAL },
b2db3599 98 { "downmix", 2, AV_CH_LAYOUT_STEREO_DOWNMIX, },
176a6157
SS
99 { 0 }
100};
101
b2db3599 102static uint64_t get_channel_layout_single(const char *name, int name_len)
176a6157 103{
b2db3599
NG
104 int i;
105 char *end;
106 int64_t layout;
176a6157 107
b2db3599
NG
108 for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map) - 1; i++) {
109 if (strlen(channel_layout_map[i].name) == name_len &&
110 !memcmp(channel_layout_map[i].name, name, name_len))
111 return channel_layout_map[i].layout;
112 }
113 for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
114 if (channel_names[i] &&
115 strlen(channel_names[i]) == name_len &&
116 !memcmp(channel_names[i], name, name_len))
117 return (int64_t)1 << i;
118 i = strtol(name, &end, 10);
119 if (end - name == name_len ||
120 (end + 1 - name == name_len && *end == 'c'))
121 return av_get_default_channel_layout(i);
122 layout = strtoll(name, &end, 0);
123 if (end - name == name_len)
124 return FFMAX(layout, 0);
176a6157
SS
125 return 0;
126}
127
b2db3599
NG
128uint64_t av_get_channel_layout(const char *name)
129{
130 const char *n, *e;
131 const char *name_end = name + strlen(name);
132 int64_t layout = 0, layout_single;
133
134 for (n = name; n < name_end; n = e + 1) {
135 for (e = n; e < name_end && *e != '+' && *e != '|'; e++);
136 layout_single = get_channel_layout_single(n, e - n);
137 if (!layout_single)
138 return 0;
139 layout |= layout_single;
140 }
141 return layout;
142}
143
176a6157 144void av_get_channel_layout_string(char *buf, int buf_size,
cc276c85 145 int nb_channels, uint64_t channel_layout)
176a6157
SS
146{
147 int i;
148
8b69c2f4
SS
149 if (nb_channels <= 0)
150 nb_channels = av_get_channel_layout_nb_channels(channel_layout);
151
176a6157
SS
152 for (i = 0; channel_layout_map[i].name; i++)
153 if (nb_channels == channel_layout_map[i].nb_channels &&
154 channel_layout == channel_layout_map[i].layout) {
155 av_strlcpy(buf, channel_layout_map[i].name, buf_size);
156 return;
157 }
158
159 snprintf(buf, buf_size, "%d channels", nb_channels);
160 if (channel_layout) {
3a0b5fa5 161 int i, ch;
176a6157 162 av_strlcat(buf, " (", buf_size);
3a0b5fa5 163 for (i = 0, ch = 0; i < 64; i++) {
019dd236 164 if ((channel_layout & (UINT64_C(1) << i))) {
176a6157
SS
165 const char *name = get_channel_name(i);
166 if (name) {
3a0b5fa5
SS
167 if (ch > 0)
168 av_strlcat(buf, "|", buf_size);
176a6157
SS
169 av_strlcat(buf, name, buf_size);
170 }
171 ch++;
172 }
173 }
174 av_strlcat(buf, ")", buf_size);
175 }
176}
177
cc276c85 178int av_get_channel_layout_nb_channels(uint64_t channel_layout)
176a6157 179{
3596de55 180 return av_popcount64(channel_layout);
176a6157 181}
5cc51a58
JR
182
183uint64_t av_get_default_channel_layout(int nb_channels)
184{
185 switch(nb_channels) {
186 case 1: return AV_CH_LAYOUT_MONO;
187 case 2: return AV_CH_LAYOUT_STEREO;
188 case 3: return AV_CH_LAYOUT_SURROUND;
189 case 4: return AV_CH_LAYOUT_QUAD;
190 case 5: return AV_CH_LAYOUT_5POINT0;
191 case 6: return AV_CH_LAYOUT_5POINT1;
192 case 7: return AV_CH_LAYOUT_6POINT1;
193 case 8: return AV_CH_LAYOUT_7POINT1;
194 default: return 0;
195 }
196}
406b257d
AK
197
198int av_get_channel_layout_channel_index(uint64_t channel_layout,
199 uint64_t channel)
200{
201 if (!(channel_layout & channel) ||
202 av_get_channel_layout_nb_channels(channel) != 1)
203 return AVERROR(EINVAL);
204 channel_layout &= channel - 1;
205 return av_get_channel_layout_nb_channels(channel_layout);
206}
5df320e1
AK
207
208const char *av_get_channel_name(uint64_t channel)
209{
210 int i;
211 if (av_get_channel_layout_nb_channels(channel) != 1)
212 return NULL;
213 for (i = 0; i < 64; i++)
214 if ((1ULL<<i) & channel)
215 return get_channel_name(i);
216 return NULL;
217}
59d58b33
AK
218
219uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
220{
221 int i;
222
223 if (av_get_channel_layout_nb_channels(channel_layout) <= index)
224 return 0;
225
226 for (i = 0; i < 64; i++) {
227 if ((1ULL << i) & channel_layout && !index--)
228 return 1ULL << i;
229 }
230 return 0;
231}