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
205 };
206
207 #if FF_API_OLD_CHANNEL_LAYOUT
210 {
212 char *end;
214
219 }
224 return (int64_t)1 <<
i;
225
226 errno = 0;
227 i = strtol(
name, &end, 10);
228
229 if (!errno && (end + 1 -
name == name_len && *end ==
'c'))
231
232 errno = 0;
234 if (!errno && end -
name == name_len)
236 return 0;
237 }
238
240 {
241 const char *n, *e;
242 const char *name_end =
name + strlen(
name);
243 int64_t
layout = 0, layout_single;
244
245 for (n =
name; n < name_end; n = e + 1) {
246 for (e = n; e < name_end && *e != '+' && *e != '|'; e++);
248 if (!layout_single)
249 return 0;
251 }
253 }
254
256 {
257 int nb = 0;
258 char *end;
260
264 return 0;
265 }
266
267 nb = strtol(
name, &end, 10);
268 if (!errno && *end == 'C' && *(end + 1) == '0円' && nb > 0 && nb < 64) {
269 *channel_layout = 0;
270 *nb_channels = nb;
271 return 0;
272 }
273
275 }
276
278 int nb_channels, uint64_t channel_layout)
279 {
281
282 if (nb_channels <= 0)
284
289 return;
290 }
291
293 if (channel_layout) {
296 for (
i = 0, ch = 0;
i < 64;
i++) {
297 if ((channel_layout & (UINT64_C(1) <<
i))) {
300 if (ch > 0)
303 }
304 ch++;
305 }
306 }
308 }
309 }
310
312 int nb_channels, uint64_t channel_layout)
313 {
314 AVBPrint bp;
315
318 }
319
321 {
323 }
324
330 return 0;
331 }
332
335 {
336 if (!(channel_layout &
channel) ||
341 }
342
344 {
348 for (
i = 0;
i < 64;
i++)
352 }
353
355 {
363 }
364
366 {
368
370 return 0;
371
372 for (
i = 0;
i < 64;
i++) {
373 if ((1ULL <<
i) & channel_layout && !
index--)
375 }
376 return 0;
377 }
378
381 {
386 return 0;
387 }
389 #endif
390
393 {
396
400
401 return 0;
402 }
403
406 {
408 int channels = 0, nb_channels = 0, native = 1;
410 const char *dup;
411 char *chlist, *end;
413
414 /* channel layout names */
418 return 0;
419 }
420 }
421
422 /* ambisonic */
423 if (!strncmp(
str,
"ambisonic ", 10)) {
424 const char *p =
str + 10;
425 char *endptr;
427 int order;
428
429 order = strtol(p, &endptr, 0);
430 if (order < 0 || order + 1 > INT_MAX / (order + 1) ||
431 (*endptr && *endptr != '+'))
433
435 channel_layout->
nb_channels = (order + 1) * (order + 1);
436
437 if (*endptr) {
444 }
445
448 } else {
450 channel_layout->
u.
map =
452 sizeof(*channel_layout->
u.
map));
453 if (!channel_layout->
u.
map) {
456 }
457
465 }
472 }
473 }
476 }
477
478 return 0;
479 }
480
482 if (!chlist)
484
485 /* channel names */
486 av_sscanf(
str,
"%d channels (%[^)]", &nb_channels, chlist);
487 end = strchr(
str,
')');
488
489 dup = chlist;
490 while (*dup) {
496 }
497 if (*dup)
498 dup++; // skip separator
504 native = 0; // Not a native layout, use a custom one
507 break;
508 }
509 }
510
512 char *endptr = chname;
514
515 if (!strncmp(chname, "USR", 3)) {
516 const char *p = chname + 3;
517 id = strtol(p, &endptr, 0);
518 }
519 if (id < 0 || *endptr) {
520 native = 0; // Unknown channel name
524 break;
525 }
526 if (id > 63)
527 native = 0; // Not a native layout, use a custom one
528 else {
529 if (
id < highest_channel ||
mask & (1ULL <<
id))
530 native = 0; // Not a native layout, use a custom one
531 highest_channel =
id;
533 }
534 }
538 }
539
540 if (
mask && native) {
542 if (nb_channels && ((nb_channels !=
channels) || (!end || *++end)))
545 return 0;
546 }
547
548 /* custom layout of channel names */
550 int idx = 0;
551
552 if (nb_channels && ((nb_channels !=
channels) || (!end || *++end))) {
555 }
556
558 if (!channel_layout->
u.
map) {
561 }
562
565
566 dup = chlist;
567 while (*dup) {
574 }
575 if (*dup)
576 dup++; // skip separator
579 channel_layout->
u.
map[idx].
id =
i;
582 idx++;
583 break;
584 }
585 }
588 channel_layout->
u.
map[idx].
id = strtol(p,
NULL, 0);
591 idx++;
592 }
595 }
597
598 return 0;
599 }
601
602 errno = 0;
604
605 /* channel layout mask */
606 if (!errno && !*end && !strchr(
str,
'-') &&
mask) {
608 return 0;
609 }
610
611 errno = 0;
613
614 /* number of channels */
615 if (!errno && !strcmp(end,
"c") &&
channels > 0) {
618 return 0;
619 }
620
621 /* number of unordered channels */
622 if (!errno && (!strcmp(end, "C") || !strcmp(end, " channels"))
626 return 0;
627 }
628
630 }
631
633 {
636 memset(channel_layout, 0, sizeof(*channel_layout));
637 }
638
640 {
647 memcpy(dst->
u.
map,
src->u.map,
src->nb_channels *
sizeof(*
src->u.map));
648 }
649 return 0;
650 }
651
652 /**
653 * If the layout is n-th order standard-order ambisonic, with optional
654 * extra non-diegetic channels at the end, return the order.
655 * Return a negative error code otherwise.
656 */
658 {
659 int i, highest_ambi, order;
660
661 highest_ambi = -1;
664 else {
667
670
671 /* ambisonic following non-ambisonic */
674
675 /* non-default ordering */
678
681 }
682 }
683 /* no ambisonic channels*/
684 if (highest_ambi < 0)
686
687 order =
floor(sqrt(highest_ambi));
688 /* incomplete order - some harmonics are missing */
689 if ((order + 1) * (order + 1) != highest_ambi + 1)
691
692 return order;
693 }
694
695 /**
696 * If the custom layout is n-th order standard-order ambisonic, with optional
697 * extra non-diegetic channels at the end, write its string description in bp.
698 * Return a negative error code otherwise.
699 */
701 {
702 int nb_ambi_channels;
704 if (order < 0)
705 return order;
706
708
709 /* extra channels present */
710 nb_ambi_channels = (order + 1) * (order + 1);
711 if (nb_ambi_channels < channel_layout->nb_channels) {
713
718 } else {
721 extra.
u.
map = channel_layout->
u.
map + nb_ambi_channels;
722 }
723
726 /* Not calling uninit here on extra because we don't own the u.map pointer */
727 }
728
729 return 0;
730 }
731
733 AVBPrint *bp)
734 {
736
737 switch (channel_layout->
order) {
742 return 0;
743 }
744 // fall-through
748 if (res >= 0)
749 return 0;
750 }
755
762 }
765 return 0;
766 }
767 // fall-through
770 return 0;
773 default:
775 }
776 }
777
779 char *buf, size_t buf_size)
780 {
781 AVBPrint bp;
783
784 if (!buf && buf_size)
786
791
792 return bp.len;
793 }
794
797 unsigned int idx)
798 {
800
803
804 switch (channel_layout->
order) {
806 return channel_layout->
u.
map[idx].
id;
809 if (idx < ambi_channels)
811 idx -= ambi_channels;
812 }
813 // fall-through
815 for (
i = 0;
i < 64;
i++) {
816 if ((1ULL <<
i) & channel_layout->
u.
mask && !idx--)
818 }
819 default:
821 }
822 }
823
827 {
829
832
834 }
835
838 {
840
843
844 switch (channel_layout->
order) {
859 }
864 }
865 default:
867 }
868 }
869
872 {
873 char *chname;
875
876 switch (channel_layout->
order) {
878 chname = strstr(
str,
"@");
879 if (chname) {
880 char buf[16];
881 chname++;
883 if (!*chname)
888 }
890 if (!strcmp(chname, channel_layout->
u.
map[
i].
name) &&
893 }
894 // fall-through
901 }
902
904 }
905
907 {
909 return 0;
910
911 switch (channel_layout->
order) {
915 if (!channel_layout->
u.
map)
916 return 0;
919 return 0;
920 }
921 return 1;
923 /* If non-diegetic channels are present, ensure they are taken into account */
926 return 1;
927 default:
928 return 0;
929 }
930 }
931
933 {
935
936 /* different channel counts -> not equal */
938 return 1;
939
940 /* if only one is unspecified -> not equal */
943 return 1;
944 /* both are unspecified -> equal */
946 return 0;
947
948 /* can compare masks directly */
953
954 /* compare channel by channel */
958 return 1;
959 return 0;
960 }
961
963 {
968 return;
969 }
970
973 }
974
976 {
977 uintptr_t
i = (uintptr_t)*opaque;
979
982 *opaque = (
void*)(
i + 1);
983 }
984
985 return ch_layout;
986 }
987
990 {
993
994 switch (channel_layout->
order) {
999 for (
i = 0;
i < 64;
i++)
1002 break;
1003 }
1004
1006 }