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 channel layout utility functions
24 */
25
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29
37
38 #define CHAN_IS_AMBI(x) ((x) >= AV_CHAN_AMBISONIC_BASE &&\
39 (x) <= AV_CHAN_AMBISONIC_END)
40
44 };
45
77 };
78
80 {
85 }
86
88 {
97 else
99 }
100
102 {
103 AVBPrint bp;
104
105 if (!buf && buf_size)
107
110
111 return bp.len;
112 }
113
115 {
124 else
126 }
127
129 {
130 AVBPrint bp;
131
132 if (!buf && buf_size)
134
137
138 return bp.len;
139 }
140
142 {
144 char *endptr = (
char *)
str;
146
147 if (!strncmp(
str,
"AMBI", 4)) {
152 }
153
157 }
158 if (!strncmp(
str,
"USR", 3)) {
159 const char *p =
str + 3;
160 id = strtol(p, &endptr, 0);
161 }
162 if (id >= 0 && !*endptr)
164
166 }
167
171 };
172
203 };
204
205 #if FF_API_OLD_CHANNEL_LAYOUT
208 {
210 char *end;
212
217 }
222 return (int64_t)1 <<
i;
223
224 errno = 0;
225 i = strtol(
name, &end, 10);
226
227 if (!errno && (end + 1 -
name == name_len && *end ==
'c'))
229
230 errno = 0;
232 if (!errno && end -
name == name_len)
234 return 0;
235 }
236
238 {
239 const char *n, *e;
240 const char *name_end =
name + strlen(
name);
241 int64_t
layout = 0, layout_single;
242
243 for (n =
name; n < name_end; n = e + 1) {
244 for (e = n; e < name_end && *e != '+' && *e != '|'; e++);
246 if (!layout_single)
247 return 0;
249 }
251 }
252
254 {
255 int nb = 0;
256 char *end;
258
262 return 0;
263 }
264
265 nb = strtol(
name, &end, 10);
266 if (!errno && *end == 'C' && *(end + 1) == '0円' && nb > 0 && nb < 64) {
267 *channel_layout = 0;
268 *nb_channels = nb;
269 return 0;
270 }
271
273 }
274
276 int nb_channels, uint64_t channel_layout)
277 {
279
280 if (nb_channels <= 0)
282
287 return;
288 }
289
291 if (channel_layout) {
294 for (
i = 0, ch = 0;
i < 64;
i++) {
295 if ((channel_layout & (UINT64_C(1) <<
i))) {
298 if (ch > 0)
301 }
302 ch++;
303 }
304 }
306 }
307 }
308
310 int nb_channels, uint64_t channel_layout)
311 {
312 AVBPrint bp;
313
316 }
317
319 {
321 }
322
328 return 0;
329 }
330
333 {
334 if (!(channel_layout &
channel) ||
339 }
340
342 {
346 for (
i = 0;
i < 64;
i++)
350 }
351
353 {
361 }
362
364 {
366
368 return 0;
369
370 for (
i = 0;
i < 64;
i++) {
371 if ((1ULL <<
i) & channel_layout && !
index--)
373 }
374 return 0;
375 }
376
379 {
384 return 0;
385 }
387 #endif
388
391 {
394
398
399 return 0;
400 }
401
404 {
406 int channels = 0, nb_channels = 0, native = 1;
408 const char *dup;
409 char *chlist, *end;
411
412 /* channel layout names */
416 return 0;
417 }
418 }
419
420 /* ambisonic */
421 if (!strncmp(
str,
"ambisonic ", 10)) {
422 const char *p =
str + 10;
423 char *endptr;
425 int order;
426
427 order = strtol(p, &endptr, 0);
428 if (order < 0 || order + 1 > INT_MAX / (order + 1) ||
429 (*endptr && *endptr != '+'))
431
433 channel_layout->
nb_channels = (order + 1) * (order + 1);
434
435 if (*endptr) {
442 }
443
446 } else {
448 channel_layout->
u.
map =
450 sizeof(*channel_layout->
u.
map));
451 if (!channel_layout->
u.
map) {
454 }
455
463 }
470 }
471 }
474 }
475
476 return 0;
477 }
478
480 if (!chlist)
482
483 /* channel names */
484 av_sscanf(
str,
"%d channels (%[^)]", &nb_channels, chlist);
485 end = strchr(
str,
')');
486
487 dup = chlist;
488 while (*dup) {
494 }
495 if (*dup)
496 dup++; // skip separator
502 native = 0; // Not a native layout, use a custom one
505 break;
506 }
507 }
508
510 char *endptr = chname;
512
513 if (!strncmp(chname, "USR", 3)) {
514 const char *p = chname + 3;
515 id = strtol(p, &endptr, 0);
516 }
517 if (id < 0 || *endptr) {
518 native = 0; // Unknown channel name
522 break;
523 }
524 if (id > 63)
525 native = 0; // Not a native layout, use a custom one
526 else {
527 if (
id < highest_channel ||
mask & (1ULL <<
id))
528 native = 0; // Not a native layout, use a custom one
529 highest_channel =
id;
531 }
532 }
536 }
537
538 if (
mask && native) {
540 if (nb_channels && ((nb_channels !=
channels) || (!end || *++end)))
543 return 0;
544 }
545
546 /* custom layout of channel names */
548 int idx = 0;
549
550 if (nb_channels && ((nb_channels !=
channels) || (!end || *++end))) {
553 }
554
556 if (!channel_layout->
u.
map) {
559 }
560
563
564 dup = chlist;
565 while (*dup) {
572 }
573 if (*dup)
574 dup++; // skip separator
577 channel_layout->
u.
map[idx].
id =
i;
580 idx++;
581 break;
582 }
583 }
586 channel_layout->
u.
map[idx].
id = strtol(p,
NULL, 0);
589 idx++;
590 }
593 }
595
596 return 0;
597 }
599
600 errno = 0;
602
603 /* channel layout mask */
604 if (!errno && !*end && !strchr(
str,
'-') &&
mask) {
606 return 0;
607 }
608
609 errno = 0;
611
612 /* number of channels */
613 if (!errno && !strcmp(end,
"c") &&
channels > 0) {
616 return 0;
617 }
618
619 /* number of unordered channels */
620 if (!errno && (!strcmp(end, "C") || !strcmp(end, " channels"))
624 return 0;
625 }
626
628 }
629
631 {
634 memset(channel_layout, 0, sizeof(*channel_layout));
635 }
636
638 {
645 memcpy(dst->
u.
map,
src->u.map,
src->nb_channels *
sizeof(*
src->u.map));
646 }
647 return 0;
648 }
649
650 /**
651 * If the layout is n-th order standard-order ambisonic, with optional
652 * extra non-diegetic channels at the end, return the order.
653 * Return a negative error code otherwise.
654 */
656 {
657 int i, highest_ambi, order;
658
659 highest_ambi = -1;
662 else {
665
668
669 /* ambisonic following non-ambisonic */
672
673 /* non-default ordering */
676
679 }
680 }
681 /* no ambisonic channels*/
682 if (highest_ambi < 0)
684
685 order =
floor(sqrt(highest_ambi));
686 /* incomplete order - some harmonics are missing */
687 if ((order + 1) * (order + 1) != highest_ambi + 1)
689
690 return order;
691 }
692
693 /**
694 * If the custom layout is n-th order standard-order ambisonic, with optional
695 * extra non-diegetic channels at the end, write its string description in bp.
696 * Return a negative error code otherwise.
697 */
699 {
700 int nb_ambi_channels;
702 if (order < 0)
703 return order;
704
706
707 /* extra channels present */
708 nb_ambi_channels = (order + 1) * (order + 1);
709 if (nb_ambi_channels < channel_layout->nb_channels) {
711
716 } else {
719 extra.
u.
map = channel_layout->
u.
map + nb_ambi_channels;
720 }
721
724 /* Not calling uninit here on extra because we don't own the u.map pointer */
725 }
726
727 return 0;
728 }
729
731 AVBPrint *bp)
732 {
734
735 switch (channel_layout->
order) {
740 return 0;
741 }
742 // fall-through
746 if (res >= 0)
747 return 0;
748 }
753
760 }
763 return 0;
764 }
765 // fall-through
768 return 0;
771 default:
773 }
774 }
775
777 char *buf, size_t buf_size)
778 {
779 AVBPrint bp;
781
782 if (!buf && buf_size)
784
789
790 return bp.len;
791 }
792
795 unsigned int idx)
796 {
798
801
802 switch (channel_layout->
order) {
804 return channel_layout->
u.
map[idx].
id;
807 if (idx < ambi_channels)
809 idx -= ambi_channels;
810 }
811 // fall-through
813 for (
i = 0;
i < 64;
i++) {
814 if ((1ULL <<
i) & channel_layout->
u.
mask && !idx--)
816 }
817 default:
819 }
820 }
821
825 {
827
830
832 }
833
836 {
838
841
842 switch (channel_layout->
order) {
857 }
862 }
863 default:
865 }
866 }
867
870 {
871 char *chname;
873
874 switch (channel_layout->
order) {
876 chname = strstr(
str,
"@");
877 if (chname) {
878 char buf[16];
879 chname++;
881 if (!*chname)
886 }
888 if (!strcmp(chname, channel_layout->
u.
map[
i].
name) &&
891 }
892 // fall-through
899 }
900
902 }
903
905 {
907 return 0;
908
909 switch (channel_layout->
order) {
913 if (!channel_layout->
u.
map)
914 return 0;
917 return 0;
918 }
919 return 1;
921 /* If non-diegetic channels are present, ensure they are taken into account */
924 return 1;
925 default:
926 return 0;
927 }
928 }
929
931 {
933
934 /* different channel counts -> not equal */
936 return 1;
937
938 /* if only one is unspecified -> not equal */
941 return 1;
942 /* both are unspecified -> equal */
944 return 0;
945
946 /* can compare masks directly */
951
952 /* compare channel by channel */
956 return 1;
957 return 0;
958 }
959
961 {
966 return;
967 }
968
971 }
972
974 {
975 uintptr_t
i = (uintptr_t)*opaque;
977
980 *opaque = (
void*)(
i + 1);
981 }
982
983 return ch_layout;
984 }
985
988 {
991
992 switch (channel_layout->
order) {
997 for (
i = 0;
i < 64;
i++)
1000 break;
1001 }
1002
1004 }