00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "avstring.h"
00027 #include "avutil.h"
00028 #include "audioconvert.h"
00029 #include "bprint.h"
00030 #include "common.h"
00031
00032 struct channel_name {
00033 const char *name;
00034 const char *description;
00035 };
00036
00037 static const struct channel_name channel_names[] = {
00038 [0] = { "FL", "front left" },
00039 [1] = { "FR", "front right" },
00040 [2] = { "FC", "front center" },
00041 [3] = { "LFE", "low frequency" },
00042 [4] = { "BL", "back left" },
00043 [5] = { "BR", "back right" },
00044 [6] = { "FLC", "front left-of-center" },
00045 [7] = { "FRC", "front right-of-center" },
00046 [8] = { "BC", "back center" },
00047 [9] = { "SL", "side left" },
00048 [10] = { "SR", "side right" },
00049 [11] = { "TC", "top center" },
00050 [12] = { "TFL", "top front left" },
00051 [13] = { "TFC", "top front center" },
00052 [14] = { "TFR", "top front right" },
00053 [15] = { "TBL", "top back left" },
00054 [16] = { "TBC", "top back center" },
00055 [17] = { "TBR", "top back right" },
00056 [29] = { "DL", "downmix left" },
00057 [30] = { "DR", "downmix right" },
00058 [31] = { "WL", "wide left" },
00059 [32] = { "WR", "wide right" },
00060 [33] = { "SDL", "surround direct left" },
00061 [34] = { "SDR", "surround direct right" },
00062 [35] = { "LFE2", "low frequency 2" },
00063 };
00064
00065 static const char *get_channel_name(int channel_id)
00066 {
00067 if (channel_id < 0 || channel_id >= FF_ARRAY_ELEMS(channel_names))
00068 return NULL;
00069 return channel_names[channel_id].name;
00070 }
00071
00072 static const struct {
00073 const char *name;
00074 int nb_channels;
00075 uint64_t layout;
00076 } channel_layout_map[] = {
00077 { "mono", 1, AV_CH_LAYOUT_MONO },
00078 { "stereo", 2, AV_CH_LAYOUT_STEREO },
00079 { "2.1", 3, AV_CH_LAYOUT_2POINT1 },
00080 { "3.0", 3, AV_CH_LAYOUT_SURROUND },
00081 { "3.0(back)", 3, AV_CH_LAYOUT_2_1 },
00082 { "4.0", 4, AV_CH_LAYOUT_4POINT0 },
00083 { "quad", 4, AV_CH_LAYOUT_QUAD },
00084 { "quad(side)", 4, AV_CH_LAYOUT_2_2 },
00085 { "3.1", 4, AV_CH_LAYOUT_3POINT1 },
00086 { "5.0", 5, AV_CH_LAYOUT_5POINT0_BACK },
00087 { "5.0(side)", 5, AV_CH_LAYOUT_5POINT0 },
00088 { "4.1", 5, AV_CH_LAYOUT_4POINT1 },
00089 { "5.1", 6, AV_CH_LAYOUT_5POINT1_BACK },
00090 { "5.1(side)", 6, AV_CH_LAYOUT_5POINT1 },
00091 { "6.0", 6, AV_CH_LAYOUT_6POINT0 },
00092 { "6.0(front)", 6, AV_CH_LAYOUT_6POINT0_FRONT },
00093 { "hexagonal", 6, AV_CH_LAYOUT_HEXAGONAL },
00094 { "6.1", 7, AV_CH_LAYOUT_6POINT1 },
00095 { "6.1", 7, AV_CH_LAYOUT_6POINT1_BACK },
00096 { "6.1(front)", 7, AV_CH_LAYOUT_6POINT1_FRONT },
00097 { "7.0", 7, AV_CH_LAYOUT_7POINT0 },
00098 { "7.0(front)", 7, AV_CH_LAYOUT_7POINT0_FRONT },
00099 { "7.1", 8, AV_CH_LAYOUT_7POINT1 },
00100 { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE },
00101 { "octagonal", 8, AV_CH_LAYOUT_OCTAGONAL },
00102 { "downmix", 2, AV_CH_LAYOUT_STEREO_DOWNMIX, },
00103 };
00104
00105 static uint64_t get_channel_layout_single(const char *name, int name_len)
00106 {
00107 int i;
00108 char *end;
00109 int64_t layout;
00110
00111 for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++) {
00112 if (strlen(channel_layout_map[i].name) == name_len &&
00113 !memcmp(channel_layout_map[i].name, name, name_len))
00114 return channel_layout_map[i].layout;
00115 }
00116 for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
00117 if (channel_names[i].name &&
00118 strlen(channel_names[i].name) == name_len &&
00119 !memcmp(channel_names[i].name, name, name_len))
00120 return (int64_t)1 << i;
00121 i = strtol(name, &end, 10);
00122 if (end - name == name_len ||
00123 (end + 1 - name == name_len && *end == 'c'))
00124 return av_get_default_channel_layout(i);
00125 layout = strtoll(name, &end, 0);
00126 if (end - name == name_len)
00127 return FFMAX(layout, 0);
00128 return 0;
00129 }
00130
00131 uint64_t av_get_channel_layout(const char *name)
00132 {
00133 const char *n, *e;
00134 const char *name_end = name + strlen(name);
00135 int64_t layout = 0, layout_single;
00136
00137 for (n = name; n < name_end; n = e + 1) {
00138 for (e = n; e < name_end && *e != '+' && *e != '|'; e++);
00139 layout_single = get_channel_layout_single(n, e - n);
00140 if (!layout_single)
00141 return 0;
00142 layout |= layout_single;
00143 }
00144 return layout;
00145 }
00146
00147 void av_bprint_channel_layout(struct AVBPrint *bp,
00148 int nb_channels, uint64_t channel_layout)
00149 {
00150 int i;
00151
00152 if (nb_channels <= 0)
00153 nb_channels = av_get_channel_layout_nb_channels(channel_layout);
00154
00155 for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
00156 if (nb_channels == channel_layout_map[i].nb_channels &&
00157 channel_layout == channel_layout_map[i].layout) {
00158 av_bprintf(bp, "%s", channel_layout_map[i].name);
00159 return;
00160 }
00161
00162 av_bprintf(bp, "%d channels", nb_channels);
00163 if (channel_layout) {
00164 int i, ch;
00165 av_bprintf(bp, " (");
00166 for (i = 0, ch = 0; i < 64; i++) {
00167 if ((channel_layout & (UINT64_C(1) << i))) {
00168 const char *name = get_channel_name(i);
00169 if (name) {
00170 if (ch > 0)
00171 av_bprintf(bp, "+");
00172 av_bprintf(bp, "%s", name);
00173 }
00174 ch++;
00175 }
00176 }
00177 av_bprintf(bp, ")");
00178 }
00179 }
00180
00181 void av_get_channel_layout_string(char *buf, int buf_size,
00182 int nb_channels, uint64_t channel_layout)
00183 {
00184 AVBPrint bp;
00185
00186 av_bprint_init_for_buffer(&bp, buf, buf_size);
00187 av_bprint_channel_layout(&bp, nb_channels, channel_layout);
00188 }
00189
00190 int av_get_channel_layout_nb_channels(uint64_t channel_layout)
00191 {
00192 return av_popcount64(channel_layout);
00193 }
00194
00195 int64_t av_get_default_channel_layout(int nb_channels) {
00196 int i;
00197 for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
00198 if (nb_channels == channel_layout_map[i].nb_channels)
00199 return channel_layout_map[i].layout;
00200 return 0;
00201 }
00202
00203 int av_get_channel_layout_channel_index(uint64_t channel_layout,
00204 uint64_t channel)
00205 {
00206 if (!(channel_layout & channel) ||
00207 av_get_channel_layout_nb_channels(channel) != 1)
00208 return AVERROR(EINVAL);
00209 channel_layout &= channel - 1;
00210 return av_get_channel_layout_nb_channels(channel_layout);
00211 }
00212
00213 const char *av_get_channel_name(uint64_t channel)
00214 {
00215 int i;
00216 if (av_get_channel_layout_nb_channels(channel) != 1)
00217 return NULL;
00218 for (i = 0; i < 64; i++)
00219 if ((1ULL<<i) & channel)
00220 return get_channel_name(i);
00221 return NULL;
00222 }
00223
00224 const char *av_get_channel_description(uint64_t channel)
00225 {
00226 int i;
00227 if (av_get_channel_layout_nb_channels(channel) != 1)
00228 return NULL;
00229 for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
00230 if ((1ULL<<i) & channel)
00231 return channel_names[i].description;
00232 return NULL;
00233 }
00234
00235 uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
00236 {
00237 int i;
00238
00239 if (av_get_channel_layout_nb_channels(channel_layout) <= index)
00240 return 0;
00241
00242 for (i = 0; i < 64; i++) {
00243 if ((1ULL << i) & channel_layout && !index--)
00244 return 1ULL << i;
00245 }
00246 return 0;
00247 }
00248
00249 int av_get_standard_channel_layout(unsigned index, uint64_t *layout,
00250 const char **name)
00251 {
00252 if (index >= FF_ARRAY_ELEMS(channel_layout_map))
00253 return AVERROR_EOF;
00254 if (layout) *layout = channel_layout_map[index].layout;
00255 if (name) *name = channel_layout_map[index].name;
00256 return 0;
00257 }