FFmpeg
Loading...
Searching...
No Matches
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
21#include <inttypes.h>
22#include <stdio.h>
23#include <string.h>
24
25#include "libavutil/bprint.h"
27#include "libavutil/error.h"
28#include "libavutil/macros.h"
29#include "libavutil/mem.h"
30
31#define BPRINT_ARGS1(bp, ...) (bp), __VA_ARGS__
32#define BPRINT_ARGS0(bp, ...) __VA_ARGS__, (bp)
33#define ORD_ARGS1(str, size, ...) (str), (size), __VA_ARGS__
34#define ORD_ARGS0(str, size, ...) __VA_ARGS__, (str), (size)
35
36// This macro presumes the AVBPrint to have been cleared before usage.
37#define CMP_BPRINT_AND_NONBPRINT(bp, func_name, ARG_ORDER, ...) do { \
38 char *str; \
39 int size; \
40 func_name ## _bprint(BPRINT_ARGS ## ARG_ORDER((bp), __VA_ARGS__)); \
41 if (strlen((bp)->str) != (bp)->len) { \
42 printf("strlen of AVBPrint-string returned by "#func_name"_bprint" \
43 " differs from AVBPrint.len: %zu vs. %u\n", \
44 strlen((bp)->str), (bp)->len); \
45 break; \
46 } \
47 size = func_name(ORD_ARGS ## ARG_ORDER(NULL, 0, __VA_ARGS__)); \
48 if (size <= 0) { \
49 printf(#func_name " returned %d\n", size); \
50 break; \
51 } \
52 if ((bp)->len != size - 1) { \
53 printf("Return value %d of " #func_name " inconsistent with length"\
54 " %u obtained from corresponding bprint version\n", \
55 size, (bp)->len); \
56 break; \
57 } \
58 str = av_malloc(size); \
59 if (!str) { \
60 printf("string of size %d could not be allocated.\n", size); \
61 break; \
62 } \
63 size = func_name(ORD_ARGS ## ARG_ORDER(str, size, __VA_ARGS__)); \
64 if (size <= 0 || (bp)->len != size - 1) { \
65 printf("Return value %d of " #func_name " inconsistent with length"\
66 " %d obtained in first pass.\n", size, (bp)->len); \
67 av_free(str); \
68 break; \
69 } \
70 if (strcmp(str, (bp)->str)) { \
71 printf("Ordinary and _bprint versions of "#func_name" disagree: " \
72 "'%s' vs. '%s'\n", str, (bp)->str); \
73 av_free(str); \
74 break; \
75 } \
76 av_free(str); \
77 } while (0)
78
79
80static void channel_name(AVBPrint *bp, enum AVChannel channel)
81{
84}
85
91
93 AVBPrint *bp, uint64_t channel_layout)
94{
97 if (!av_channel_layout_from_mask(layout, channel_layout) &&
100 else
101 av_bprintf(bp, "fail");
102}
103
105 AVBPrint *bp, const char *channel_layout)
106{
108 av_bprint_clear(bp);
109 if (!av_channel_layout_from_string(layout, channel_layout) &&
112 else
113 av_bprintf(bp, "fail");
114}
115
116static const char* channel_order_names[] = {"UNSPEC", "NATIVE", "CUSTOM", "AMBI"};
117
118static void describe_type(AVBPrint *bp, AVChannelLayout *layout)
119{
120 if ((unsigned)layout->order < FF_ARRAY_ELEMS(channel_order_names)) {
121 av_bprintf(bp, "%-6s (", channel_order_names[layout->order]);
123 av_bprintf(bp, ")");
124 } else {
125 av_bprintf(bp, "???");
126 }
127}
128
129static const char *channel_layout_retype(AVChannelLayout *layout, AVBPrint *bp, const char *channel_layout)
130{
132 av_bprint_clear(bp);
133 if (!av_channel_layout_from_string(layout, channel_layout) &&
136 for (int i = 0; i < FF_CHANNEL_ORDER_NB; i++) {
137 int ret;
138 AVChannelLayout copy = {0};
139 av_bprintf(bp, "\n ");
141 return "nomem";
142 ret = av_channel_layout_retype(&copy, i, 0);
143 if (ret < 0 && (copy.order != layout->order || av_channel_layout_compare(&copy, layout)))
144 av_bprintf(bp, "failed to keep existing layout on failure");
145 if (ret >= 0 && copy.order != i)
146 av_bprintf(bp, "returned success but did not change order");
147 if (ret == AVERROR(ENOSYS)) {
148 av_bprintf(bp, " != %s", channel_order_names[i]);
149 } else if (ret < 0) {
150 av_bprintf(bp, "FAIL");
151 } else {
152 av_bprintf(bp, " %s ", ret ? "~~" : "==");
153 describe_type(bp, &copy);
154 }
156 }
157 } else {
158 av_bprintf(bp, "fail");
159 }
160 return bp->str;
161}
162
163#define CHANNEL_NAME(x) \
164 channel_name(&bp, (x)); \
165 printf("With %-32s %14s\n", AV_STRINGIFY(x)":", bp.str)
166
167#define CHANNEL_DESCRIPTION(x) \
168 channel_description(&bp, (x)); \
169 printf("With %-23s %23s\n", AV_STRINGIFY(x)":", bp.str);
170
171#define CHANNEL_FROM_STRING(x) \
172 printf("With %-38s %8d\n", AV_STRINGIFY(x)":", av_channel_from_string(x))
173
174#define CHANNEL_LAYOUT_FROM_MASK(x) \
175 channel_layout_from_mask(&layout, &bp, (x));
176
177#define CHANNEL_LAYOUT_FROM_STRING(x) \
178 channel_layout_from_string(&layout, &bp, (x)); \
179 printf("With \"%s\":%*s %32s\n", x, strlen(x) > 32 ? 0 : 32 - (int)strlen(x), "", bp.str);
180
181#define CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(l, x) \
182 ret = av_channel_layout_channel_from_index(&layout, x); \
183 if (ret < 0) \
184 ret = -1; \
185 printf("On \"%s\" layout with %2d: %8d\n", l, x, ret)
186
187#define CHANNEL_LAYOUT_SUBSET(l, xstr, x) \
188 mask = av_channel_layout_subset(&layout, x); \
189 printf("On \"%s\" layout with %-22s 0x%"PRIx64"\n", l, xstr, mask)
190
191#define CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(l, x) \
192 ret = av_channel_layout_index_from_channel(&layout, x); \
193 if (ret < 0) \
194 ret = -1; \
195 printf("On \"%s\" layout with %-23s %3d\n", l, AV_STRINGIFY(x)":", ret)
196
197#define CHANNEL_LAYOUT_CHANNEL_FROM_STRING(l, x) \
198 ret = av_channel_layout_channel_from_string(&layout, x); \
199 if (ret < 0) \
200 ret = -1; \
201 printf("On \"%s\" layout with %-21s %3d\n", bp.str, AV_STRINGIFY(x)":", ret);
202
203#define CHANNEL_LAYOUT_INDEX_FROM_STRING(l, x) \
204 ret = av_channel_layout_index_from_string(&layout, x); \
205 if (ret < 0) \
206 ret = -1; \
207 printf("On \"%s\" layout with %-20s %3d\n", l, AV_STRINGIFY(x)":", ret);
208
209int main(void)
210{
211 const AVChannelLayout *playout;
212 AVChannelLayout layout = { 0 }, layout2 = { 0 };
213 AVBPrint bp;
214 void *iter = NULL;
215 uint64_t mask;
216 int ret;
217
219
220 printf("Testing av_channel_layout_standard\n");
221 while (playout = av_channel_layout_standard(&iter)) {
223 printf("%-14s ", bp.str);
224 av_bprint_clear(&bp);
225 for (int i = 0; i < 63; i++) {
226 int idx = av_channel_layout_index_from_channel(playout, i);
227 if (idx >= 0) {
228 if (idx)
229 av_bprintf(&bp, "+");
231 }
232 }
233 printf("%s\n", bp.str);
234 av_bprint_clear(&bp);
235 }
236
237 printf("\nTesting av_channel_name\n");
240 CHANNEL_NAME(63);
243
244 printf("Testing av_channel_description\n");
250
251 printf("\nTesting av_channel_from_string\n");
254 CHANNEL_FROM_STRING("USR63");
255 CHANNEL_FROM_STRING("AMBI0");
256 CHANNEL_FROM_STRING("AMBI1023");
257 CHANNEL_FROM_STRING("AMBI1024");
258 CHANNEL_FROM_STRING("Dummy");
259 CHANNEL_FROM_STRING("FL@Foo");
260 CHANNEL_FROM_STRING("Foo@FL");
261 CHANNEL_FROM_STRING("@FL");
262
263 printf("\n==Native layouts==\n");
264
265 printf("\nTesting av_channel_layout_from_string\n");
270 CHANNEL_LAYOUT_FROM_STRING("6 channels");
271 CHANNEL_LAYOUT_FROM_STRING("6 channels (FL+FR+FC+LFE+BL+BR)");
272 CHANNEL_LAYOUT_FROM_STRING("FL+FR+FC+LFE+BL+BR");
274 CHANNEL_LAYOUT_FROM_STRING("FL+FR+USR63");
275 CHANNEL_LAYOUT_FROM_STRING("FL+FR+FC+LFE+SL+SR");
276 CHANNEL_LAYOUT_FROM_STRING("5.1(side)");
277
278 printf("\nTesting av_channel_layout_from_mask\n");
280 printf("With AV_CH_LAYOUT_5POINT1: %25s\n", bp.str);
281
282 printf("\nTesting av_channel_layout_channel_from_index\n");
290
291 printf("\nTesting av_channel_layout_index_from_channel\n");
299
300 printf("\nTesting av_channel_layout_channel_from_string\n");
310 CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "FL@Foo");
311
312 printf("\nTesting av_channel_layout_index_from_string\n");
320
321 printf("\nTesting av_channel_layout_subset\n");
322 CHANNEL_LAYOUT_SUBSET(bp.str, "AV_CH_LAYOUT_STEREO:", AV_CH_LAYOUT_STEREO);
323 CHANNEL_LAYOUT_SUBSET(bp.str, "AV_CH_LAYOUT_2POINT1:", AV_CH_LAYOUT_2POINT1);
324 CHANNEL_LAYOUT_SUBSET(bp.str, "AV_CH_LAYOUT_4POINT1:", AV_CH_LAYOUT_4POINT1);
325
326 printf("\n==Custom layouts==\n");
327
328 printf("\nTesting av_channel_layout_from_string\n");
329 CHANNEL_LAYOUT_FROM_STRING("FL+FR+FC+BL+BR+LFE");
330 CHANNEL_LAYOUT_FROM_STRING("2 channels (FR+FL)");
331 CHANNEL_LAYOUT_FROM_STRING("2 channels (AMBI1023+FL)");
332 CHANNEL_LAYOUT_FROM_STRING("3 channels (FR+FL)");
333 CHANNEL_LAYOUT_FROM_STRING("-3 channels (FR+FL)");
334 CHANNEL_LAYOUT_FROM_STRING("0 channels ()");
335 CHANNEL_LAYOUT_FROM_STRING("2 channels (FL+FR");
336 CHANNEL_LAYOUT_FROM_STRING("ambisonic 1+FR+FL");
337 CHANNEL_LAYOUT_FROM_STRING("ambisonic 2+FC@Foo");
338 CHANNEL_LAYOUT_FROM_STRING("FL@Foo+FR@Bar");
339 CHANNEL_LAYOUT_FROM_STRING("FL+stereo");
340 CHANNEL_LAYOUT_FROM_STRING("stereo+stereo");
341 CHANNEL_LAYOUT_FROM_STRING("stereo@Boo");
347 CHANNEL_LAYOUT_FROM_STRING("Dummy@FL");
348 CHANNEL_LAYOUT_FROM_STRING("FR+Dummy");
349 CHANNEL_LAYOUT_FROM_STRING("FR+Dummy@FL");
350 CHANNEL_LAYOUT_FROM_STRING("UNK+UNSD");
354 CHANNEL_LAYOUT_FROM_STRING("FR+FL@Foo+USR63@Foo");
355
356 ret = av_channel_layout_copy(&layout2, &layout);
357 if (ret < 0) {
358 printf("Copying channel layout \"FR+FL@Foo+USR63@Foo\" failed; "
359 "ret %d\n", ret);
360 }
361 ret = av_channel_layout_compare(&layout, &layout2);
362 if (ret)
363 printf("Channel layout and its copy compare unequal; ret: %d\n", ret);
364
365 printf("\nTesting av_channel_layout_index_from_string\n");
368 CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "USR63");
370 CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "@Foo");
371 CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "FR@Foo");
372 CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "FL@Foo");
373 CHANNEL_LAYOUT_INDEX_FROM_STRING(bp.str, "USR63@Foo");
375
376 printf("\nTesting av_channel_layout_channel_from_string\n");
379 CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "USR63");
382 CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "FR@Foo");
383 CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "FL@Foo");
384 CHANNEL_LAYOUT_CHANNEL_FROM_STRING(bp.str, "USR63@Foo");
386
387 printf("\nTesting av_channel_layout_index_from_channel\n");
392
393 printf("\nTesting av_channel_layout_channel_from_index\n");
398
399 printf("\nTesting av_channel_layout_subset\n");
400 CHANNEL_LAYOUT_SUBSET(bp.str, "AV_CH_LAYOUT_STEREO:", AV_CH_LAYOUT_STEREO);
401 CHANNEL_LAYOUT_SUBSET(bp.str, "AV_CH_LAYOUT_QUAD:", AV_CH_LAYOUT_QUAD);
402
403 printf("\n==Ambisonic layouts==\n");
404
405 printf("\nTesting av_channel_layout_from_string\n");
406 CHANNEL_LAYOUT_FROM_STRING("ambisonic 1");
407 CHANNEL_LAYOUT_FROM_STRING("ambisonic 2+stereo");
408
409 printf("\nTesting av_channel_layout_index_from_channel\n");
414
415 printf("\nTesting av_channel_layout_channel_from_index\n");
420
421 printf("\nTesting av_channel_layout_subset\n");
422 CHANNEL_LAYOUT_SUBSET(bp.str, "AV_CH_LAYOUT_STEREO:", AV_CH_LAYOUT_STEREO);
423 CHANNEL_LAYOUT_SUBSET(bp.str, "AV_CH_LAYOUT_QUAD:", AV_CH_LAYOUT_QUAD);
424
426 av_channel_layout_uninit(&layout2);
427
428 printf("\nTesting av_channel_layout_retype\n");
429 {
430 const char* layouts[] = {
431 "FL@Boo",
432 "stereo",
433 "FR+FL",
434 "ambisonic 2+stereo",
435 "2C",
436 NULL
437 };
438 for (int i = 0; layouts[i]; i++)
439 printf("With \"%s\": %s\n", layouts[i], channel_layout_retype(&layout, &bp, layouts[i]));
440 }
442
443 return 0;
444}
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:122
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:69
AVBPrint public header.
#define AV_BPRINT_SIZE_AUTOMATIC
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
__device__ int printf(const char *,...)
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition ebur128.h:39
error code definitions
#define AV_CH_LAYOUT_2POINT1
#define AV_CH_LAYOUT_QUAD
#define AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_5POINT1
#define AV_CH_LAYOUT_4POINT1
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
Initialize a channel layout from a given string description.
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.
int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrder order, int flags)
Change the AVChannelOrder of a channel layout.
int av_channel_layout_describe_bprint(const AVChannelLayout *channel_layout, AVBPrint *bp)
bprint variant of av_channel_layout_describe().
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
int av_channel_name(char *buf, size_t buf_size, enum AVChannel channel_id)
Get a human readable string in an abbreviated form describing a given channel.
int av_channel_description(char *buf, size_t buf_size, enum AVChannel channel_id)
Get a human readable string describing a given channel.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
AVChannel
void av_channel_name_bprint(AVBPrint *bp, enum AVChannel channel_id)
bprint variant of av_channel_name().
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
int av_channel_layout_check(const AVChannelLayout *channel_layout)
Check whether a channel layout is valid, i.e.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
const AVChannelLayout * av_channel_layout_standard(void **opaque)
Iterate over all standard channel layouts.
@ FF_CHANNEL_ORDER_NB
Number of channel orders, not part of ABI/API.
@ AV_CHAN_LOW_FREQUENCY
@ AV_CHAN_FRONT_LEFT
@ AV_CHAN_FRONT_RIGHT
@ AV_CHAN_AMBISONIC_BASE
Range of channels between AV_CHAN_AMBISONIC_BASE and AV_CHAN_AMBISONIC_END represent Ambisonic compon...
@ AV_CHAN_BACK_CENTER
@ AV_CHAN_FRONT_CENTER
@ AV_CHAN_SIDE_RIGHT
@ AV_CHAN_SIDE_LEFT
@ AV_CHAN_AMBISONIC_END
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:235
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition bprint.c:227
#define AVERROR(e)
Definition error.h:45
static const uint16_t mask[17]
Definition lzw.c:38
Utility Preprocessor macros.
uint64_t layout
Memory handling functions.
enum MovChannelLayoutTag * layouts
Definition mov_chan.c:335
#define FF_ARRAY_ELEMS(a)
An AVChannelLayout holds information about the channel layout of audio data.
#define CHANNEL_NAME(x)
static void describe_type(AVBPrint *bp, AVChannelLayout *layout)
#define CMP_BPRINT_AND_NONBPRINT(bp, func_name, ARG_ORDER,...)
static void channel_layout_from_mask(AVChannelLayout *layout, AVBPrint *bp, uint64_t channel_layout)
#define CHANNEL_LAYOUT_SUBSET(l, xstr, x)
#define CHANNEL_LAYOUT_INDEX_FROM_CHANNEL(l, x)
#define CHANNEL_FROM_STRING(x)
static const char * channel_order_names[]
#define CHANNEL_DESCRIPTION(x)
#define CHANNEL_LAYOUT_FROM_MASK(x)
int main(void)
#define CHANNEL_LAYOUT_CHANNEL_FROM_INDEX(l, x)
static void channel_layout_from_string(AVChannelLayout *layout, AVBPrint *bp, const char *channel_layout)
#define CHANNEL_LAYOUT_FROM_STRING(x)
#define CHANNEL_LAYOUT_INDEX_FROM_STRING(l, x)
static const char * channel_layout_retype(AVChannelLayout *layout, AVBPrint *bp, const char *channel_layout)
static void channel_description(AVBPrint *bp, enum AVChannel channel)
static void channel_name(AVBPrint *bp, enum AVChannel channel)
#define CHANNEL_LAYOUT_CHANNEL_FROM_STRING(l, x)
static void copy(const float *p1, float *p2, const int length)