]> git.sesse.net Git - ffmpeg/blob - libavutil/audioconvert.c
doc/ffmpeg: extend documentation for -(no)stdin option
[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 #include "common.h"
31
32 struct channel_name {
33     const char *name;
34     const char *description;
35 };
36
37 static const struct channel_name channel_names[] = {
38      [0] = { "FL",        "front left"            },
39      [1] = { "FR",        "front right"           },
40      [2] = { "FC",        "front center"          },
41      [3] = { "LFE",       "low frequency"         },
42      [4] = { "BL",        "back left"             },
43      [5] = { "BR",        "back right"            },
44      [6] = { "FLC",       "front left-of-center"  },
45      [7] = { "FRC",       "front right-of-center" },
46      [8] = { "BC",        "back center"           },
47      [9] = { "SL",        "side left"             },
48     [10] = { "SR",        "side right"            },
49     [11] = { "TC",        "top center"            },
50     [12] = { "TFL",       "top front left"        },
51     [13] = { "TFC",       "top front center"      },
52     [14] = { "TFR",       "top front right"       },
53     [15] = { "TBL",       "top back left"         },
54     [16] = { "TBC",       "top back center"       },
55     [17] = { "TBR",       "top back right"        },
56     [29] = { "DL",        "downmix left"          },
57     [30] = { "DR",        "downmix right"         },
58     [31] = { "WL",        "wide left"             },
59     [32] = { "WR",        "wide right"            },
60     [33] = { "SDL",       "surround direct left"  },
61     [34] = { "SDR",       "surround direct right" },
62 };
63
64 static const char *get_channel_name(int channel_id)
65 {
66     if (channel_id < 0 || channel_id >= FF_ARRAY_ELEMS(channel_names))
67         return NULL;
68     return channel_names[channel_id].name;
69 }
70
71 static const struct {
72     const char *name;
73     int         nb_channels;
74     uint64_t     layout;
75 } channel_layout_map[] = {
76     { "mono",        1,  AV_CH_LAYOUT_MONO },
77     { "stereo",      2,  AV_CH_LAYOUT_STEREO },
78     { "2.1",         3,  AV_CH_LAYOUT_2POINT1 },
79     { "3.0",         3,  AV_CH_LAYOUT_SURROUND },
80     { "3.0(back)",   3,  AV_CH_LAYOUT_2_1 },
81     { "4.0",         4,  AV_CH_LAYOUT_4POINT0 },
82     { "quad",        4,  AV_CH_LAYOUT_QUAD },
83     { "quad(side)",  4,  AV_CH_LAYOUT_2_2 },
84     { "3.1",         4,  AV_CH_LAYOUT_3POINT1 },
85     { "5.0",         5,  AV_CH_LAYOUT_5POINT0_BACK },
86     { "5.0(side)",   5,  AV_CH_LAYOUT_5POINT0 },
87     { "4.1",         5,  AV_CH_LAYOUT_4POINT1 },
88     { "5.1",         6,  AV_CH_LAYOUT_5POINT1_BACK },
89     { "5.1(side)",   6,  AV_CH_LAYOUT_5POINT1 },
90     { "6.0",         6,  AV_CH_LAYOUT_6POINT0 },
91     { "6.0(front)",  6,  AV_CH_LAYOUT_6POINT0_FRONT },
92     { "hexagonal",   6,  AV_CH_LAYOUT_HEXAGONAL },
93     { "6.1",         7,  AV_CH_LAYOUT_6POINT1 },
94     { "6.1",         7,  AV_CH_LAYOUT_6POINT1_BACK },
95     { "6.1(front)",  7,  AV_CH_LAYOUT_6POINT1_FRONT },
96     { "7.0",         7,  AV_CH_LAYOUT_7POINT0 },
97     { "7.0(front)",  7,  AV_CH_LAYOUT_7POINT0_FRONT },
98     { "7.1",         8,  AV_CH_LAYOUT_7POINT1 },
99     { "7.1(wide)",   8,  AV_CH_LAYOUT_7POINT1_WIDE },
100     { "octagonal",   8,  AV_CH_LAYOUT_OCTAGONAL },
101     { "downmix",     2,  AV_CH_LAYOUT_STEREO_DOWNMIX, },
102 };
103
104 static uint64_t get_channel_layout_single(const char *name, int name_len)
105 {
106     int i;
107     char *end;
108     int64_t layout;
109
110     for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++) {
111         if (strlen(channel_layout_map[i].name) == name_len &&
112             !memcmp(channel_layout_map[i].name, name, name_len))
113             return channel_layout_map[i].layout;
114     }
115     for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
116         if (channel_names[i].name &&
117             strlen(channel_names[i].name) == name_len &&
118             !memcmp(channel_names[i].name, name, name_len))
119             return (int64_t)1 << i;
120     i = strtol(name, &end, 10);
121     if (end - name == name_len ||
122         (end + 1 - name == name_len && *end  == 'c'))
123         return av_get_default_channel_layout(i);
124     layout = strtoll(name, &end, 0);
125     if (end - name == name_len)
126         return FFMAX(layout, 0);
127     return 0;
128 }
129
130 uint64_t av_get_channel_layout(const char *name)
131 {
132     const char *n, *e;
133     const char *name_end = name + strlen(name);
134     int64_t layout = 0, layout_single;
135
136     for (n = name; n < name_end; n = e + 1) {
137         for (e = n; e < name_end && *e != '+' && *e != '|'; e++);
138         layout_single = get_channel_layout_single(n, e - n);
139         if (!layout_single)
140             return 0;
141         layout |= layout_single;
142     }
143     return layout;
144 }
145
146 void av_bprint_channel_layout(struct AVBPrint *bp,
147                               int nb_channels, uint64_t channel_layout)
148 {
149     int i;
150
151     if (nb_channels <= 0)
152         nb_channels = av_get_channel_layout_nb_channels(channel_layout);
153
154     for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
155         if (nb_channels    == channel_layout_map[i].nb_channels &&
156             channel_layout == channel_layout_map[i].layout) {
157             av_bprintf(bp, "%s", channel_layout_map[i].name);
158             return;
159         }
160
161     av_bprintf(bp, "%d channels", nb_channels);
162     if (channel_layout) {
163         int i, ch;
164         av_bprintf(bp, " (");
165         for (i = 0, ch = 0; i < 64; i++) {
166             if ((channel_layout & (UINT64_C(1) << i))) {
167                 const char *name = get_channel_name(i);
168                 if (name) {
169                     if (ch > 0)
170                         av_bprintf(bp, "+");
171                     av_bprintf(bp, "%s", name);
172                 }
173                 ch++;
174             }
175         }
176         av_bprintf(bp, ")");
177     }
178 }
179
180 void av_get_channel_layout_string(char *buf, int buf_size,
181                                   int nb_channels, uint64_t channel_layout)
182 {
183     AVBPrint bp;
184
185     av_bprint_init_for_buffer(&bp, buf, buf_size);
186     av_bprint_channel_layout(&bp, nb_channels, channel_layout);
187 }
188
189 int av_get_channel_layout_nb_channels(uint64_t channel_layout)
190 {
191     return av_popcount64(channel_layout);
192 }
193
194 int64_t av_get_default_channel_layout(int nb_channels) {
195     int i;
196     for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
197         if (nb_channels == channel_layout_map[i].nb_channels)
198             return channel_layout_map[i].layout;
199     return 0;
200 }
201
202 int av_get_channel_layout_channel_index(uint64_t channel_layout,
203                                         uint64_t channel)
204 {
205     if (!(channel_layout & channel) ||
206         av_get_channel_layout_nb_channels(channel) != 1)
207         return AVERROR(EINVAL);
208     channel_layout &= channel - 1;
209     return av_get_channel_layout_nb_channels(channel_layout);
210 }
211
212 const char *av_get_channel_name(uint64_t channel)
213 {
214     int i;
215     if (av_get_channel_layout_nb_channels(channel) != 1)
216         return NULL;
217     for (i = 0; i < 64; i++)
218         if ((1ULL<<i) & channel)
219             return get_channel_name(i);
220     return NULL;
221 }
222
223 const char *av_get_channel_description(uint64_t channel)
224 {
225     int i;
226     if (av_get_channel_layout_nb_channels(channel) != 1)
227         return NULL;
228     for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
229         if ((1ULL<<i) & channel)
230             return channel_names[i].description;
231     return NULL;
232 }
233
234 uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
235 {
236     int i;
237
238     if (av_get_channel_layout_nb_channels(channel_layout) <= index)
239         return 0;
240
241     for (i = 0; i < 64; i++) {
242         if ((1ULL << i) & channel_layout && !index--)
243             return 1ULL << i;
244     }
245     return 0;
246 }
247
248 int av_get_standard_channel_layout(unsigned index, uint64_t *layout,
249                                    const char **name)
250 {
251     if (index >= FF_ARRAY_ELEMS(channel_layout_map))
252         return AVERROR_EOF;
253     if (layout) *layout = channel_layout_map[index].layout;
254     if (name)   *name   = channel_layout_map[index].name;
255     return 0;
256 }