]> git.sesse.net Git - ffmpeg/blob - libavutil/audioconvert.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavutil / audioconvert.c
1 /*
2  * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
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  *
11  * FFmpeg is distributed in the hope that it will be useful,
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
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * audio conversion routines
24  */
25
26 #include "avstring.h"
27 #include "avutil.h"
28 #include "audioconvert.h"
29 #include "bprint.h"
30
31 static const char * const channel_names[] = {
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 */
52     [31] = "WL",        /* wide left */
53     [32] = "WR",        /* wide right */
54     [33] = "SDL",       /* surround direct left */
55     [34] = "SDR",       /* surround direct right */
56 };
57
58 static const char *get_channel_name(int channel_id)
59 {
60     if (channel_id < 0 || channel_id >= FF_ARRAY_ELEMS(channel_names))
61         return NULL;
62     return channel_names[channel_id];
63 }
64
65 static const struct {
66     const char *name;
67     int         nb_channels;
68     uint64_t     layout;
69 } channel_layout_map[] = {
70     { "mono",        1,  AV_CH_LAYOUT_MONO },
71     { "stereo",      2,  AV_CH_LAYOUT_STEREO },
72     { "2.1",         3,  AV_CH_LAYOUT_2POINT1 },
73     { "3.0",         3,  AV_CH_LAYOUT_SURROUND },
74     { "3.0(back)",   3,  AV_CH_LAYOUT_2_1 },
75     { "4.0",         4,  AV_CH_LAYOUT_4POINT0 },
76     { "quad",        4,  AV_CH_LAYOUT_QUAD },
77     { "quad(side)",  4,  AV_CH_LAYOUT_2_2 },
78     { "3.1",         4,  AV_CH_LAYOUT_3POINT1 },
79     { "5.0",         5,  AV_CH_LAYOUT_5POINT0_BACK },
80     { "5.0(side)",   5,  AV_CH_LAYOUT_5POINT0 },
81     { "4.1",         5,  AV_CH_LAYOUT_4POINT1 },
82     { "5.1",         6,  AV_CH_LAYOUT_5POINT1_BACK },
83     { "5.1(side)",   6,  AV_CH_LAYOUT_5POINT1 },
84     { "6.0",         6,  AV_CH_LAYOUT_6POINT0 },
85     { "6.0(front)",  6,  AV_CH_LAYOUT_6POINT0_FRONT },
86     { "hexagonal",   6,  AV_CH_LAYOUT_HEXAGONAL },
87     { "6.1",         7,  AV_CH_LAYOUT_6POINT1 },
88     { "6.1",         7,  AV_CH_LAYOUT_6POINT1_BACK },
89     { "6.1(front)",  7,  AV_CH_LAYOUT_6POINT1_FRONT },
90     { "7.0",         7,  AV_CH_LAYOUT_7POINT0 },
91     { "7.0(front)",  7,  AV_CH_LAYOUT_7POINT0_FRONT },
92     { "7.1",         8,  AV_CH_LAYOUT_7POINT1 },
93     { "7.1(wide)",   8,  AV_CH_LAYOUT_7POINT1_WIDE },
94     { "octagonal",   8,  AV_CH_LAYOUT_OCTAGONAL },
95     { "downmix",     2,  AV_CH_LAYOUT_STEREO_DOWNMIX, },
96 };
97
98 static uint64_t get_channel_layout_single(const char *name, int name_len)
99 {
100     int i;
101     char *end;
102     int64_t layout;
103
104     for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++) {
105         if (strlen(channel_layout_map[i].name) == name_len &&
106             !memcmp(channel_layout_map[i].name, name, name_len))
107             return channel_layout_map[i].layout;
108     }
109     for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
110         if (channel_names[i] &&
111             strlen(channel_names[i]) == name_len &&
112             !memcmp(channel_names[i], name, name_len))
113             return (int64_t)1 << i;
114     i = strtol(name, &end, 10);
115     if (end - name == name_len ||
116         (end + 1 - name == name_len && *end  == 'c'))
117         return av_get_default_channel_layout(i);
118     layout = strtoll(name, &end, 0);
119     if (end - name == name_len)
120         return FFMAX(layout, 0);
121     return 0;
122 }
123
124 uint64_t av_get_channel_layout(const char *name)
125 {
126     const char *n, *e;
127     const char *name_end = name + strlen(name);
128     int64_t layout = 0, layout_single;
129
130     for (n = name; n < name_end; n = e + 1) {
131         for (e = n; e < name_end && *e != '+' && *e != '|'; e++);
132         layout_single = get_channel_layout_single(n, e - n);
133         if (!layout_single)
134             return 0;
135         layout |= layout_single;
136     }
137     return layout;
138 }
139
140 void av_bprint_channel_layout(struct AVBPrint *bp,
141                               int nb_channels, uint64_t channel_layout)
142 {
143     int i;
144
145     if (nb_channels <= 0)
146         nb_channels = av_get_channel_layout_nb_channels(channel_layout);
147
148     for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
149         if (nb_channels    == channel_layout_map[i].nb_channels &&
150             channel_layout == channel_layout_map[i].layout) {
151             av_bprintf(bp, "%s", channel_layout_map[i].name);
152             return;
153         }
154
155     av_bprintf(bp, "%d channels", nb_channels);
156     if (channel_layout) {
157         int i, ch;
158         av_bprintf(bp, " (");
159         for (i = 0, ch = 0; i < 64; i++) {
160             if ((channel_layout & (UINT64_C(1) << i))) {
161                 const char *name = get_channel_name(i);
162                 if (name) {
163                     if (ch > 0)
164                         av_bprintf(bp, "+");
165                     av_bprintf(bp, "%s", name);
166                 }
167                 ch++;
168             }
169         }
170         av_bprintf(bp, ")");
171     }
172 }
173
174 void av_get_channel_layout_string(char *buf, int buf_size,
175                                   int nb_channels, uint64_t channel_layout)
176 {
177     AVBPrint bp;
178
179     av_bprint_init_for_buffer(&bp, buf, buf_size);
180     av_bprint_channel_layout(&bp, nb_channels, channel_layout);
181 }
182
183 int av_get_channel_layout_nb_channels(uint64_t channel_layout)
184 {
185     return av_popcount64(channel_layout);
186 }
187
188 int64_t av_get_default_channel_layout(int nb_channels) {
189     int i;
190     for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
191         if (nb_channels == channel_layout_map[i].nb_channels)
192             return channel_layout_map[i].layout;
193     return 0;
194 }
195
196 int av_get_channel_layout_channel_index(uint64_t channel_layout,
197                                         uint64_t channel)
198 {
199     if (!(channel_layout & channel) ||
200         av_get_channel_layout_nb_channels(channel) != 1)
201         return AVERROR(EINVAL);
202     channel_layout &= channel - 1;
203     return av_get_channel_layout_nb_channels(channel_layout);
204 }
205
206 const char *av_get_channel_name(uint64_t channel)
207 {
208     int i;
209     if (av_get_channel_layout_nb_channels(channel) != 1)
210         return NULL;
211     for (i = 0; i < 64; i++)
212         if ((1ULL<<i) & channel)
213             return get_channel_name(i);
214     return NULL;
215 }
216
217 uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
218 {
219     int i;
220
221     if (av_get_channel_layout_nb_channels(channel_layout) <= index)
222         return 0;
223
224     for (i = 0; i < 64; i++) {
225         if ((1ULL << i) & channel_layout && !index--)
226             return 1ULL << i;
227     }
228     return 0;
229 }