FFmpeg
channel_layout.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 James Almer
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 
22 
23 #define CHANNEL_NAME(x) \
24  av_bprint_clear(&bp); \
25  av_channel_name_bprint(&bp, x);
26 
27 #define CHANNEL_DESCRIPTION(x) \
28  av_bprint_clear(&bp); \
29  av_channel_description_bprint(&bp, x);
30 
31 #define CHANNEL_LAYOUT_FROM_MASK(x) \
32  av_channel_layout_uninit(&layout); \
33  av_bprint_clear(&bp); \
34  if (!av_channel_layout_from_mask(&layout, x) && \
35  av_channel_layout_check(&layout)) \
36  av_channel_layout_describe_bprint(&layout, &bp); \
37  else \
38  av_bprintf(&bp, "fail");
39 
40 #define CHANNEL_LAYOUT_FROM_STRING(x) \
41  av_channel_layout_uninit(&layout); \
42  av_bprint_clear(&bp); \
43  if (!av_channel_layout_from_string(&layout, x) && \
44  av_channel_layout_check(&layout)) \
45  av_channel_layout_describe_bprint(&layout, &bp); \
46  else \
47  av_bprintf(&bp, "fail");
48 
49 #define CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(x) \
50  ret = av_channel_layout_channel_from_index(&layout, x); \
51  if (ret < 0) \
52  ret = -1
53 
54 #define CHANNEL_LAYOUT_SUBSET(x) \
55  mask = av_channel_layout_subset(&layout, x)
56 
57 #define CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(x) \
58  ret = av_channel_layout_index_from_channel(&layout, x); \
59  if (ret < 0) \
60  ret = -1
61 
62 #define CHANNEL_LAYOUT_CHANNEL_FROM_STRING(x) \
63  ret = av_channel_layout_channel_from_string(&layout, x); \
64  if (ret < 0) \
65  ret = -1
66 
67 #define CHANNEL_LAYOUT_INDEX_FROM_STRING(x) \
68  ret = av_channel_layout_index_from_string(&layout, x); \
69  if (ret < 0) \
70  ret = -1
71 
72 int main(void)
73 {
74  const AVChannelLayout *playout;
75  AVChannelLayout layout = { 0 };
76  AVBPrint bp;
77  void *iter = NULL;
78  uint64_t mask;
79  int ret;
80 
82 
83  printf("Testing av_channel_layout_standard\n");
84  while (playout = av_channel_layout_standard(&iter)) {
86  printf("%-14s ", bp.str);
87  av_bprint_clear(&bp);
88  for (int i = 0; i < 63; i++) {
89  int idx = av_channel_layout_index_from_channel(playout, i);
90  if (idx >= 0) {
91  if (idx)
92  av_bprintf(&bp, "+");
94  }
95  }
96  printf("%s\n", bp.str);
97  av_bprint_clear(&bp);
98  }
99 
100  printf("\nTesting av_channel_name\n");
102  printf("With AV_CHAN_FRONT_LEFT: %27s\n", bp.str);
104  printf("With AV_CHAN_FRONT_RIGHT: %26s\n", bp.str);
105  CHANNEL_NAME(63);
106  printf("With 63: %43s\n", bp.str);
108  printf("With AV_CHAN_AMBISONIC_BASE: %23s\n", bp.str);
110  printf("With AV_CHAN_AMBISONIC_END: %24s\n", bp.str);
111 
112  printf("Testing av_channel_description\n");
114  printf("With AV_CHAN_FRONT_LEFT: %27s\n", bp.str);
116  printf("With AV_CHAN_FRONT_RIGHT: %26s\n", bp.str);
118  printf("With 63: %43s\n", bp.str);
120  printf("With AV_CHAN_AMBISONIC_BASE: %23s\n", bp.str);
122  printf("With AV_CHAN_AMBISONIC_END: %24s\n", bp.str);
123 
124  printf("\nTesting av_channel_from_string\n");
125  printf("With \"FL\": %41d\n", av_channel_from_string("FL"));
126  printf("With \"FR\": %41d\n", av_channel_from_string("FR"));
127  printf("With \"USR63\": %38d\n", av_channel_from_string("USR63"));
128  printf("With \"AMBI0\": %38d\n", av_channel_from_string("AMBI0"));
129  printf("With \"AMBI1023\": %35d\n", av_channel_from_string("AMBI1023"));
130 
131  printf("\n==Native layouts==\n");
132 
133  printf("\nTesting av_channel_layout_from_string\n");
135  printf("With \"0x3f\": %39s\n", bp.str);
137  printf("With \"63\": %41s\n", bp.str);
139  printf("With \"6c\": %41s\n", bp.str);
141  printf("With \"6C\": %41s\n", bp.str);
142  CHANNEL_LAYOUT_FROM_STRING("6 channels");
143  printf("With \"6 channels\": %33s\n", bp.str);
144  CHANNEL_LAYOUT_FROM_STRING("6 channels (FL+FR+FC+LFE+BL+BR)");
145  printf("With \"6 channels (FL+FR+FC+LFE+BL+BR)\": %12s\n", bp.str);
146  CHANNEL_LAYOUT_FROM_STRING("FL+FR+FC+LFE+BL+BR");
147  printf("With \"FL+FR+FC+LFE+BL+BR\": %25s\n", bp.str);
149  printf("With \"5.1\": %40s\n", bp.str);
150  CHANNEL_LAYOUT_FROM_STRING("FL+FR+USR63");
151  printf("With \"FL+FR+USR63\": %32s\n", bp.str);
152  CHANNEL_LAYOUT_FROM_STRING("FL+FR+FC+LFE+SL+SR");
153  printf("With \"FL+FR+FC+LFE+SL+SR\": %25s\n", bp.str);
154  CHANNEL_LAYOUT_FROM_STRING("5.1(side)");
155  printf("With \"5.1(side)\": %34s\n", bp.str);
156 
157  printf("\nTesting av_channel_layout_from_mask\n");
159  printf("With AV_CH_LAYOUT_5POINT1: %25s\n", bp.str);
160 
161  printf("\nTesting av_channel_layout_channel_from_index\n");
163  printf("On 5.1(side) layout with 0: %24d\n", ret);
165  printf("On 5.1(side) layout with 1: %24d\n", ret);
167  printf("On 5.1(side) layout with 2: %24d\n", ret);
169  printf("On 5.1(side) layout with 3: %24d\n", ret);
171  printf("On 5.1(side) layout with 4: %24d\n", ret);
173  printf("On 5.1(side) layout with 5: %24d\n", ret);
175  printf("On 5.1(side) layout with 6: %24d\n", ret);
176 
177  printf("\nTesting av_channel_layout_index_from_channel\n");
179  printf("On 5.1(side) layout with AV_CHAN_FRONT_LEFT: %7d\n", ret);
181  printf("On 5.1(side) layout with AV_CHAN_FRONT_RIGHT: %6d\n", ret);
183  printf("On 5.1(side) layout with AV_CHAN_FRONT_CENTER: %5d\n", ret);
185  printf("On 5.1(side) layout with AV_CHAN_LOW_FREQUENCY: %4d\n", ret);
187  printf("On 5.1(side) layout with AV_CHAN_SIDE_LEFT: %8d\n", ret);
189  printf("On 5.1(side) layout with AV_CHAN_SIDE_RIGHT: %7d\n", ret);
191  printf("On 5.1(side) layout with AV_CHAN_BACK_CENTER: %6d\n", ret);
192 
193  printf("\nTesting av_channel_layout_channel_from_string\n");
195  printf("On 5.1(side) layout with \"FL\": %21d\n", ret);
197  printf("On 5.1(side) layout with \"FR\": %21d\n", ret);
199  printf("On 5.1(side) layout with \"FC\": %21d\n", ret);
201  printf("On 5.1(side) layout with \"LFE\": %20d\n", ret);
203  printf("On 5.1(side) layout with \"SL\": %21d\n", ret);
205  printf("On 5.1(side) layout with \"SR\": %21d\n", ret);
207  printf("On 5.1(side) layout with \"BC\": %21d\n", ret);
208 
209  printf("\nTesting av_channel_layout_index_from_string\n");
211  printf("On 5.1(side) layout with \"FL\": %21d\n", ret);
213  printf("On 5.1(side) layout with \"FR\": %21d\n", ret);
215  printf("On 5.1(side) layout with \"FC\": %21d\n", ret);
217  printf("On 5.1(side) layout with \"LFE\": %20d\n", ret);
219  printf("On 5.1(side) layout with \"SL\": %21d\n", ret);
221  printf("On 5.1(side) layout with \"SR\": %21d\n", ret);
223  printf("On 5.1(side) layout with \"BC\": %21d\n", ret);
224 
225  printf("\nTesting av_channel_layout_subset\n");
227  printf("On 5.1(side) layout with AV_CH_LAYOUT_STEREO: 0x%"PRIx64"\n", mask);
229  printf("On 5.1(side) layout with AV_CH_LAYOUT_2POINT1: 0x%"PRIx64"\n", mask);
231  printf("On 5.1(side) layout with AV_CH_LAYOUT_4POINT1: 0x%"PRIx64"\n", mask);
232 
233  printf("\n==Custom layouts==\n");
234 
235  printf("\nTesting av_channel_layout_from_string\n");
236  CHANNEL_LAYOUT_FROM_STRING("FL+FR+FC+BL+BR+LFE");
237  printf("With \"FL+FR+FC+BL+BR+LFE\": %34s\n", bp.str);
238  CHANNEL_LAYOUT_FROM_STRING("2 channels (FR+FL)");
239  printf("With \"2 channels (FR+FL)\": %34s\n", bp.str);
240  CHANNEL_LAYOUT_FROM_STRING("ambisonic 1+FR+FL");
241  printf("With \"ambisonic 1+FR+FL\": %35s\n", bp.str);
242  CHANNEL_LAYOUT_FROM_STRING("ambisonic 2+FC@Foo");
243  printf("With \"ambisonic 2+FC@Foo\": %34s\n", bp.str);
244  CHANNEL_LAYOUT_FROM_STRING("FL@Foo+FR@Bar");
245  printf("With \"FL@Foo+FR@Bar\": %39s\n", bp.str);
246  CHANNEL_LAYOUT_FROM_STRING("FR+FL@Foo+USR63@Foo");
247  printf("With \"FR+FL@Foo+USR63@Foo\": %33s\n", bp.str);
248 
249  printf("\nTesting av_channel_layout_index_from_string\n");
251  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FR\": %18d\n", ret);
253  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FL\": %18d\n", ret);
255  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"USR63\": %15d\n", ret);
257  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"Foo\": %17d\n", ret);
259  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"@Foo\": %16d\n", ret);
261  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FR@Foo\": %14d\n", ret);
263  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FL@Foo\": %14d\n", ret);
265  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"USR63@Foo\": %11d\n", ret);
267  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"BC\": %18d\n", ret);
268 
269  printf("\nTesting av_channel_layout_channel_from_string\n");
271  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FR\": %18d\n", ret);
273  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FL\": %18d\n", ret);
275  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"USR63\": %15d\n", ret);
277  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"Foo\": %17d\n", ret);
279  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"@Foo\": %16d\n", ret);
281  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FR@Foo\": %14d\n", ret);
283  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"FL@Foo\": %14d\n", ret);
285  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"USR63@Foo\": %11d\n", ret);
287  printf("On \"FR+FL@Foo+USR63@Foo\" layout with \"BC\": %18d\n", ret);
288 
289  printf("\nTesting av_channel_layout_index_from_channel\n");
291  printf("On \"FR+FL@Foo+USR63@Foo\" layout with AV_CHAN_FRONT_RIGHT: %3d\n", ret);
293  printf("On \"FR+FL@Foo+USR63@Foo\" layout with AV_CHAN_FRONT_LEFT: %4d\n", ret);
295  printf("On \"FR+FL@Foo+USR63@Foo\" layout with 63: %20d\n", ret);
297  printf("On \"FR+FL@Foo+USR63@Foo\" layout with AV_CHAN_BACK_CENTER: %3d\n", ret);
298 
299  printf("\nTesting av_channel_layout_channel_from_index\n");
301  printf("On \"FR+FL@Foo+USR63@Foo\" layout with 0: %21d\n", ret);
303  printf("On \"FR+FL@Foo+USR63@Foo\" layout with 1: %21d\n", ret);
305  printf("On \"FR+FL@Foo+USR63@Foo\" layout with 2: %21d\n", ret);
307  printf("On \"FR+FL@Foo+USR63@Foo\" layout with 3: %21d\n", ret);
308 
309  printf("\nTesting av_channel_layout_subset\n");
311  printf("On \"FR+FL@Foo+USR63@Foo\" layout with AV_CH_LAYOUT_STEREO: 0x%"PRIx64"\n", mask);
313  printf("On \"FR+FL@Foo+USR63@Foo\" layout with AV_CH_LAYOUT_QUAD: 0x%"PRIx64"\n", mask);
314 
315  printf("\n==Ambisonic layouts==\n");
316 
317  printf("\nTesting av_channel_layout_from_string\n");
318  CHANNEL_LAYOUT_FROM_STRING("ambisonic 1");
319  printf("With \"ambisonic 1\": %41s\n", bp.str);
320  CHANNEL_LAYOUT_FROM_STRING("ambisonic 2+stereo");
321  printf("With \"ambisonic 2+stereo\": %34s\n", bp.str);
322 
323  printf("\nTesting av_channel_layout_index_from_channel\n");
325  printf("On \"ambisonic 2+stereo\" layout with AV_CHAN_AMBISONIC_BASE: %d\n", ret);
327  printf("On \"ambisonic 2+stereo\" layout with AV_CHAN_FRONT_LEFT: %5d\n", ret);
329  printf("On \"ambisonic 2+stereo\" layout with AV_CHAN_FRONT_RIGHT: %4d\n", ret);
331  printf("On \"ambisonic 2+stereo\" layout with AV_CHAN_BACK_CENTER: %4d\n", ret);
332 
333  printf("\nTesting av_channel_layout_channel_from_index\n");
335  printf("On \"ambisonic 2+stereo\" layout with 0: %22d\n", ret);
337  printf("On \"ambisonic 2+stereo\" layout with 9: %22d\n", ret);
339  printf("On \"ambisonic 2+stereo\" layout with 10: %21d\n", ret);
341  printf("On \"ambisonic 2+stereo\" layout with 11: %21d\n", ret);
342 
343  printf("\nTesting av_channel_layout_subset\n");
345  printf("On \"ambisonic 2+stereo\" layout with AV_CH_LAYOUT_STEREO: 0x%"PRIx64"\n", mask);
347  printf("On \"ambisonic 2+stereo\" layout with AV_CH_LAYOUT_QUAD: 0x%"PRIx64"\n", mask);
348 
350  av_bprint_finalize(&bp, NULL);
351 
352  return 0;
353 }
CHANNEL_DESCRIPTION
#define CHANNEL_DESCRIPTION(x)
Definition: channel_layout.c:27
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
AV_CHAN_LOW_FREQUENCY
@ AV_CHAN_LOW_FREQUENCY
Definition: channel_layout.h:47
CHANNEL_LAYOUT_FROM_STRING
#define CHANNEL_LAYOUT_FROM_STRING(x)
Definition: channel_layout.c:40
AV_BPRINT_SIZE_AUTOMATIC
#define AV_BPRINT_SIZE_AUTOMATIC
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:205
AV_CHAN_AMBISONIC_END
@ AV_CHAN_AMBISONIC_END
Definition: channel_layout.h:98
AV_CH_LAYOUT_QUAD
#define AV_CH_LAYOUT_QUAD
Definition: channel_layout.h:213
av_channel_layout_describe_bprint
int av_channel_layout_describe_bprint(const AVChannelLayout *channel_layout, AVBPrint *bp)
bprint variant of av_channel_layout_describe().
Definition: channel_layout.c:730
mask
static const uint16_t mask[17]
Definition: lzw.c:38
AV_CHAN_FRONT_RIGHT
@ AV_CHAN_FRONT_RIGHT
Definition: channel_layout.h:45
CHANNEL_LAYOUT_CHANNEL_FROM_INDEX
#define CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(x)
Definition: channel_layout.c:49
CHANNEL_LAYOUT_CHANNEL_FROM_STRING
#define CHANNEL_LAYOUT_CHANNEL_FROM_STRING(x)
Definition: channel_layout.c:62
NULL
#define NULL
Definition: coverity.c:32
CHANNEL_LAYOUT_SUBSET
#define CHANNEL_LAYOUT_SUBSET(x)
Definition: channel_layout.c:54
AV_CHAN_AMBISONIC_BASE
@ AV_CHAN_AMBISONIC_BASE
Range of channels between AV_CHAN_AMBISONIC_BASE and AV_CHAN_AMBISONIC_END represent Ambisonic compon...
Definition: channel_layout.h:95
channel_layout.c
AV_CHAN_FRONT_LEFT
@ AV_CHAN_FRONT_LEFT
Definition: channel_layout.h:44
AV_CH_LAYOUT_5POINT1
#define AV_CH_LAYOUT_5POINT1
Definition: channel_layout.h:215
main
int main(void)
Definition: channel_layout.c:72
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:630
AV_CHAN_SIDE_RIGHT
@ AV_CHAN_SIDE_RIGHT
Definition: channel_layout.h:54
CHANNEL_LAYOUT_FROM_MASK
#define CHANNEL_LAYOUT_FROM_MASK(x)
Definition: channel_layout.c:31
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:293
av_channel_from_string
enum AVChannel av_channel_from_string(const char *str)
This is the inverse function of av_channel_name().
Definition: channel_layout.c:141
AV_CHAN_BACK_CENTER
@ AV_CHAN_BACK_CENTER
Definition: channel_layout.h:52
printf
printf("static const uint8_t my_array[100] = {\n")
AV_CHAN_SIDE_LEFT
@ AV_CHAN_SIDE_LEFT
Definition: channel_layout.h:53
av_channel_layout_standard
const AVChannelLayout * av_channel_layout_standard(void **opaque)
Iterate over all standard channel layouts.
Definition: channel_layout.c:973
layout
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel layout
Definition: filter_design.txt:18
CHANNEL_NAME
#define CHANNEL_NAME(x)
Definition: channel_layout.c:23
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AV_CH_LAYOUT_2POINT1
#define AV_CH_LAYOUT_2POINT1
Definition: channel_layout.h:206
AV_CHAN_FRONT_CENTER
@ AV_CHAN_FRONT_CENTER
Definition: channel_layout.h:46
AV_CH_LAYOUT_4POINT1
#define AV_CH_LAYOUT_4POINT1
Definition: channel_layout.h:211
av_channel_layout_index_from_channel
int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout, enum AVChannel channel)
Get the index of a given channel in a channel layout.
Definition: channel_layout.c:834
ret
ret
Definition: filter_design.txt:187
CHANNEL_LAYOUT_INDEX_FROM_STRING
#define CHANNEL_LAYOUT_INDEX_FROM_STRING(x)
Definition: channel_layout.c:67
av_bprintf
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:227
av_channel_name_bprint
void av_channel_name_bprint(AVBPrint *bp, enum AVChannel channel_id)
bprint variant of av_channel_name().
Definition: channel_layout.c:87
CHANNEL_LAYOUT_INDEX_FROM_CHANNEL
#define CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(x)
Definition: channel_layout.c:57