Libav
channel_layout.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
26 #include <stdint.h>
27 
28 #include "avstring.h"
29 #include "avutil.h"
30 #include "channel_layout.h"
31 #include "common.h"
32 
33 static const char * const channel_names[] = {
34  [0] = "FL", /* front left */
35  [1] = "FR", /* front right */
36  [2] = "FC", /* front center */
37  [3] = "LFE", /* low frequency */
38  [4] = "BL", /* back left */
39  [5] = "BR", /* back right */
40  [6] = "FLC", /* front left-of-center */
41  [7] = "FRC", /* front right-of-center */
42  [8] = "BC", /* back-center */
43  [9] = "SL", /* side left */
44  [10] = "SR", /* side right */
45  [11] = "TC", /* top center */
46  [12] = "TFL", /* top front left */
47  [13] = "TFC", /* top front center */
48  [14] = "TFR", /* top front right */
49  [15] = "TBL", /* top back left */
50  [16] = "TBC", /* top back center */
51  [17] = "TBR", /* top back right */
52  [29] = "DL", /* downmix left */
53  [30] = "DR", /* downmix right */
54  [31] = "WL", /* wide left */
55  [32] = "WR", /* wide right */
56  [33] = "SDL", /* surround direct left */
57  [34] = "SDR", /* surround direct right */
58  [35] = "LFE2", /* low frequency 2 */
59 };
60 
61 static const char *get_channel_name(int channel_id)
62 {
63  if (channel_id < 0 || channel_id >= FF_ARRAY_ELEMS(channel_names))
64  return NULL;
65  return channel_names[channel_id];
66 }
67 
68 static const struct {
69  const char *name;
71  uint64_t layout;
72 } channel_layout_map[] = {
73  { "mono", 1, AV_CH_LAYOUT_MONO },
74  { "stereo", 2, AV_CH_LAYOUT_STEREO },
75  { "stereo", 2, AV_CH_LAYOUT_STEREO_DOWNMIX },
76  { "2.1", 3, AV_CH_LAYOUT_2POINT1 },
77  { "3.0", 3, AV_CH_LAYOUT_SURROUND },
78  { "3.0(back)", 3, AV_CH_LAYOUT_2_1 },
79  { "3.1", 4, AV_CH_LAYOUT_3POINT1 },
80  { "4.0", 4, AV_CH_LAYOUT_4POINT0 },
81  { "quad", 4, AV_CH_LAYOUT_QUAD },
82  { "quad(side)", 4, AV_CH_LAYOUT_2_2 },
83  { "4.1", 5, AV_CH_LAYOUT_4POINT1 },
84  { "5.0", 5, AV_CH_LAYOUT_5POINT0 },
85  { "5.0", 5, AV_CH_LAYOUT_5POINT0_BACK },
86  { "5.1", 6, AV_CH_LAYOUT_5POINT1 },
87  { "5.1", 6, AV_CH_LAYOUT_5POINT1_BACK },
88  { "6.0", 6, AV_CH_LAYOUT_6POINT0 },
89  { "6.0(front)", 6, AV_CH_LAYOUT_6POINT0_FRONT },
90  { "hexagonal", 6, AV_CH_LAYOUT_HEXAGONAL },
91  { "6.1", 7, AV_CH_LAYOUT_6POINT1 },
92  { "6.1", 7, AV_CH_LAYOUT_6POINT1_BACK },
93  { "6.1(front)", 7, AV_CH_LAYOUT_6POINT1_FRONT },
94  { "7.0", 7, AV_CH_LAYOUT_7POINT0 },
95  { "7.0(front)", 7, AV_CH_LAYOUT_7POINT0_FRONT },
96  { "7.1", 8, AV_CH_LAYOUT_7POINT1 },
97  { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE },
98  { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE_BACK },
99  { "octagonal", 8, AV_CH_LAYOUT_OCTAGONAL },
100  { "hexadecagonal", 16, AV_CH_LAYOUT_HEXADECAGONAL },
101  { "downmix", 2, AV_CH_LAYOUT_STEREO_DOWNMIX, },
102  { 0 }
103 };
104 
105 static uint64_t get_channel_layout_single(const char *name, int name_len)
106 {
107  int i;
108  char *end;
109  int64_t layout;
110 
111  for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map) - 1; i++) {
112  if (strlen(channel_layout_map[i].name) == name_len &&
113  !memcmp(channel_layout_map[i].name, name, name_len))
114  return channel_layout_map[i].layout;
115  }
116  for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
117  if (channel_names[i] &&
118  strlen(channel_names[i]) == name_len &&
119  !memcmp(channel_names[i], name, name_len))
120  return (int64_t)1 << i;
121  i = strtol(name, &end, 10);
122  if (end - name == name_len ||
123  (end + 1 - name == name_len && *end == 'c'))
125  layout = strtoll(name, &end, 0);
126  if (end - name == name_len)
127  return FFMAX(layout, 0);
128  return 0;
129 }
130 
131 uint64_t av_get_channel_layout(const char *name)
132 {
133  const char *n, *e;
134  const char *name_end = name + strlen(name);
135  int64_t layout = 0, layout_single;
136 
137  for (n = name; n < name_end; n = e + 1) {
138  for (e = n; e < name_end && *e != '+' && *e != '|'; e++);
139  layout_single = get_channel_layout_single(n, e - n);
140  if (!layout_single)
141  return 0;
142  layout |= layout_single;
143  }
144  return layout;
145 }
146 
147 void av_get_channel_layout_string(char *buf, int buf_size,
148  int nb_channels, uint64_t channel_layout)
149 {
150  int i;
151 
152  if (nb_channels <= 0)
153  nb_channels = av_get_channel_layout_nb_channels(channel_layout);
154 
155  for (i = 0; channel_layout_map[i].name; i++)
156  if (nb_channels == channel_layout_map[i].nb_channels &&
157  channel_layout == channel_layout_map[i].layout) {
158  av_strlcpy(buf, channel_layout_map[i].name, buf_size);
159  return;
160  }
161 
162  snprintf(buf, buf_size, "%d channels", nb_channels);
163  if (channel_layout) {
164  int i, ch;
165  av_strlcat(buf, " (", buf_size);
166  for (i = 0, ch = 0; i < 64; i++) {
167  if ((channel_layout & (UINT64_C(1) << i))) {
168  const char *name = get_channel_name(i);
169  if (name) {
170  if (ch > 0)
171  av_strlcat(buf, "|", buf_size);
172  av_strlcat(buf, name, buf_size);
173  }
174  ch++;
175  }
176  }
177  av_strlcat(buf, ")", buf_size);
178  }
179 }
180 
181 int av_get_channel_layout_nb_channels(uint64_t channel_layout)
182 {
183  return av_popcount64(channel_layout);
184 }
185 
187 {
188  switch(nb_channels) {
189  case 1: return AV_CH_LAYOUT_MONO;
190  case 2: return AV_CH_LAYOUT_STEREO;
191  case 3: return AV_CH_LAYOUT_SURROUND;
192  case 4: return AV_CH_LAYOUT_QUAD;
193  case 5: return AV_CH_LAYOUT_5POINT0;
194  case 6: return AV_CH_LAYOUT_5POINT1;
195  case 7: return AV_CH_LAYOUT_6POINT1;
196  case 8: return AV_CH_LAYOUT_7POINT1;
197  case 16: return AV_CH_LAYOUT_HEXADECAGONAL;
198  default: return 0;
199  }
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 uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
224 {
225  int i;
226 
227  if (av_get_channel_layout_nb_channels(channel_layout) <= index)
228  return 0;
229 
230  for (i = 0; i < 64; i++) {
231  if ((1ULL << i) & channel_layout && !index--)
232  return 1ULL << i;
233  }
234  return 0;
235 }
uint64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
#define AV_CH_LAYOUT_7POINT1
#define AV_CH_LAYOUT_4POINT1
#define AV_CH_LAYOUT_HEXADECAGONAL
#define AV_CH_LAYOUT_6POINT1
#define AV_CH_LAYOUT_6POINT0
#define AV_CH_LAYOUT_SURROUND
static const char * get_channel_name(int channel_id)
external API header
#define AV_CH_LAYOUT_7POINT0_FRONT
#define AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_7POINT0
#define FF_ARRAY_ELEMS(a)
#define AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_5POINT0
uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
Get the channel with the given index in channel_layout.
#define AV_CH_LAYOUT_HEXAGONAL
const char * name
int av_get_channel_layout_channel_index(uint64_t channel_layout, uint64_t channel)
Get the index of a channel in channel_layout.
#define AV_CH_LAYOUT_5POINT1
#define AVERROR(e)
Definition: error.h:43
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
#define AV_CH_LAYOUT_QUAD
const char * av_get_channel_name(uint64_t channel)
Get the name of a given channel.
#define FFMAX(a, b)
Definition: common.h:64
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
#define AV_CH_LAYOUT_2_1
#define AV_CH_LAYOUT_2_2
#define AV_CH_LAYOUT_6POINT0_FRONT
audio channel layout utility functions
#define AV_CH_LAYOUT_STEREO_DOWNMIX
#define AV_CH_LAYOUT_3POINT1
#define AV_CH_LAYOUT_5POINT1_BACK
#define AV_CH_LAYOUT_6POINT1_BACK
NULL
Definition: eval.c:55
static const struct @156 channel_layout_map[]
#define AV_CH_LAYOUT_7POINT1_WIDE_BACK
int index
Definition: gxfenc.c:72
#define AV_CH_LAYOUT_5POINT0_BACK
#define AV_CH_LAYOUT_OCTAGONAL
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:91
#define AV_CH_LAYOUT_7POINT1_WIDE
common internal and external API header
#define AV_CH_LAYOUT_2POINT1
static uint64_t get_channel_layout_single(const char *name, int name_len)
static const char *const channel_names[]
uint64_t av_get_channel_layout(const char *name)
Return a channel layout id that matches name, or 0 if no match is found.
uint64_t layout
#define AV_CH_LAYOUT_6POINT1_FRONT
int nb_channels
#define AV_CH_LAYOUT_MONO
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.