channel_layout.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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. * @file
  22. * audio channel layout utility functions
  23. */
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "avassert.h"
  28. #include "channel_layout.h"
  29. #include "bprint.h"
  30. #include "common.h"
  31. #include "error.h"
  32. #include "macros.h"
  33. #include "mem.h"
  34. #include "opt.h"
  35. #define CHAN_IS_AMBI(x) ((x) >= AV_CHAN_AMBISONIC_BASE &&\
  36. (x) <= AV_CHAN_AMBISONIC_END)
  37. struct channel_name {
  38. const char *name;
  39. const char *description;
  40. };
  41. static const struct channel_name channel_names[] = {
  42. [AV_CHAN_FRONT_LEFT ] = { "FL", "front left" },
  43. [AV_CHAN_FRONT_RIGHT ] = { "FR", "front right" },
  44. [AV_CHAN_FRONT_CENTER ] = { "FC", "front center" },
  45. [AV_CHAN_LOW_FREQUENCY ] = { "LFE", "low frequency" },
  46. [AV_CHAN_BACK_LEFT ] = { "BL", "back left" },
  47. [AV_CHAN_BACK_RIGHT ] = { "BR", "back right" },
  48. [AV_CHAN_FRONT_LEFT_OF_CENTER ] = { "FLC", "front left-of-center" },
  49. [AV_CHAN_FRONT_RIGHT_OF_CENTER] = { "FRC", "front right-of-center" },
  50. [AV_CHAN_BACK_CENTER ] = { "BC", "back center" },
  51. [AV_CHAN_SIDE_LEFT ] = { "SL", "side left" },
  52. [AV_CHAN_SIDE_RIGHT ] = { "SR", "side right" },
  53. [AV_CHAN_TOP_CENTER ] = { "TC", "top center" },
  54. [AV_CHAN_TOP_FRONT_LEFT ] = { "TFL", "top front left" },
  55. [AV_CHAN_TOP_FRONT_CENTER ] = { "TFC", "top front center" },
  56. [AV_CHAN_TOP_FRONT_RIGHT ] = { "TFR", "top front right" },
  57. [AV_CHAN_TOP_BACK_LEFT ] = { "TBL", "top back left" },
  58. [AV_CHAN_TOP_BACK_CENTER ] = { "TBC", "top back center" },
  59. [AV_CHAN_TOP_BACK_RIGHT ] = { "TBR", "top back right" },
  60. [AV_CHAN_STEREO_LEFT ] = { "DL", "downmix left" },
  61. [AV_CHAN_STEREO_RIGHT ] = { "DR", "downmix right" },
  62. [AV_CHAN_WIDE_LEFT ] = { "WL", "wide left" },
  63. [AV_CHAN_WIDE_RIGHT ] = { "WR", "wide right" },
  64. [AV_CHAN_SURROUND_DIRECT_LEFT ] = { "SDL", "surround direct left" },
  65. [AV_CHAN_SURROUND_DIRECT_RIGHT] = { "SDR", "surround direct right" },
  66. [AV_CHAN_LOW_FREQUENCY_2 ] = { "LFE2", "low frequency 2" },
  67. [AV_CHAN_TOP_SIDE_LEFT ] = { "TSL", "top side left" },
  68. [AV_CHAN_TOP_SIDE_RIGHT ] = { "TSR", "top side right" },
  69. [AV_CHAN_BOTTOM_FRONT_CENTER ] = { "BFC", "bottom front center" },
  70. [AV_CHAN_BOTTOM_FRONT_LEFT ] = { "BFL", "bottom front left" },
  71. [AV_CHAN_BOTTOM_FRONT_RIGHT ] = { "BFR", "bottom front right" },
  72. [AV_CHAN_SIDE_SURROUND_LEFT ] = { "SSL", "side surround left" },
  73. [AV_CHAN_SIDE_SURROUND_RIGHT ] = { "SSR", "side surround right" },
  74. [AV_CHAN_TOP_SURROUND_LEFT ] = { "TTL", "top surround left" },
  75. [AV_CHAN_TOP_SURROUND_RIGHT ] = { "TTR", "top surround right" },
  76. [AV_CHAN_BINAURAL_LEFT ] = { "BIL", "binaural left" },
  77. [AV_CHAN_BINAURAL_RIGHT ] = { "BIR", "binaural right" },
  78. };
  79. void av_channel_name_bprint(AVBPrint *bp, enum AVChannel channel_id)
  80. {
  81. if (channel_id >= AV_CHAN_AMBISONIC_BASE &&
  82. channel_id <= AV_CHAN_AMBISONIC_END)
  83. av_bprintf(bp, "AMBI%d", channel_id - AV_CHAN_AMBISONIC_BASE);
  84. else if ((unsigned)channel_id < FF_ARRAY_ELEMS(channel_names) &&
  85. channel_names[channel_id].name)
  86. av_bprintf(bp, "%s", channel_names[channel_id].name);
  87. else if (channel_id == AV_CHAN_NONE)
  88. av_bprintf(bp, "NONE");
  89. else if (channel_id == AV_CHAN_UNKNOWN)
  90. av_bprintf(bp, "UNK");
  91. else if (channel_id == AV_CHAN_UNUSED)
  92. av_bprintf(bp, "UNSD");
  93. else
  94. av_bprintf(bp, "USR%d", channel_id);
  95. }
  96. int av_channel_name(char *buf, size_t buf_size, enum AVChannel channel_id)
  97. {
  98. AVBPrint bp;
  99. if (!buf && buf_size)
  100. return AVERROR(EINVAL);
  101. av_bprint_init_for_buffer(&bp, buf, buf_size);
  102. av_channel_name_bprint(&bp, channel_id);
  103. if (bp.len >= INT_MAX)
  104. return AVERROR(ERANGE);
  105. return bp.len + 1;
  106. }
  107. void av_channel_description_bprint(AVBPrint *bp, enum AVChannel channel_id)
  108. {
  109. if (channel_id >= AV_CHAN_AMBISONIC_BASE &&
  110. channel_id <= AV_CHAN_AMBISONIC_END)
  111. av_bprintf(bp, "ambisonic ACN %d", channel_id - AV_CHAN_AMBISONIC_BASE);
  112. else if ((unsigned)channel_id < FF_ARRAY_ELEMS(channel_names) &&
  113. channel_names[channel_id].description)
  114. av_bprintf(bp, "%s", channel_names[channel_id].description);
  115. else if (channel_id == AV_CHAN_NONE)
  116. av_bprintf(bp, "none");
  117. else if (channel_id == AV_CHAN_UNKNOWN)
  118. av_bprintf(bp, "unknown");
  119. else if (channel_id == AV_CHAN_UNUSED)
  120. av_bprintf(bp, "unused");
  121. else
  122. av_bprintf(bp, "user %d", channel_id);
  123. }
  124. int av_channel_description(char *buf, size_t buf_size, enum AVChannel channel_id)
  125. {
  126. AVBPrint bp;
  127. if (!buf && buf_size)
  128. return AVERROR(EINVAL);
  129. av_bprint_init_for_buffer(&bp, buf, buf_size);
  130. av_channel_description_bprint(&bp, channel_id);
  131. if (bp.len >= INT_MAX)
  132. return AVERROR(ERANGE);
  133. return bp.len + 1;
  134. }
  135. enum AVChannel av_channel_from_string(const char *str)
  136. {
  137. int i;
  138. char *endptr = (char *)str;
  139. enum AVChannel id = AV_CHAN_NONE;
  140. if (!strncmp(str, "AMBI", 4)) {
  141. i = strtol(str + 4, NULL, 0);
  142. if (i < 0 || i > AV_CHAN_AMBISONIC_END - AV_CHAN_AMBISONIC_BASE)
  143. return AV_CHAN_NONE;
  144. return AV_CHAN_AMBISONIC_BASE + i;
  145. }
  146. for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++) {
  147. if (channel_names[i].name && !strcmp(str, channel_names[i].name))
  148. return i;
  149. }
  150. if (!strcmp(str, "UNK"))
  151. return AV_CHAN_UNKNOWN;
  152. if (!strcmp(str, "UNSD"))
  153. return AV_CHAN_UNUSED;
  154. if (!strncmp(str, "USR", 3)) {
  155. const char *p = str + 3;
  156. id = strtol(p, &endptr, 0);
  157. }
  158. if (id >= 0 && !*endptr)
  159. return id;
  160. return AV_CHAN_NONE;
  161. }
  162. struct channel_layout_name {
  163. const char *name;
  164. AVChannelLayout layout;
  165. };
  166. static const struct channel_layout_name channel_layout_map[] = {
  167. { "mono", AV_CHANNEL_LAYOUT_MONO },
  168. { "stereo", AV_CHANNEL_LAYOUT_STEREO },
  169. { "2.1", AV_CHANNEL_LAYOUT_2POINT1 },
  170. { "3.0", AV_CHANNEL_LAYOUT_SURROUND },
  171. { "3.0(back)", AV_CHANNEL_LAYOUT_2_1 },
  172. { "4.0", AV_CHANNEL_LAYOUT_4POINT0 },
  173. { "quad", AV_CHANNEL_LAYOUT_QUAD },
  174. { "quad(side)", AV_CHANNEL_LAYOUT_2_2 },
  175. { "3.1", AV_CHANNEL_LAYOUT_3POINT1 },
  176. { "5.0", AV_CHANNEL_LAYOUT_5POINT0_BACK },
  177. { "5.0(side)", AV_CHANNEL_LAYOUT_5POINT0 },
  178. { "4.1", AV_CHANNEL_LAYOUT_4POINT1 },
  179. { "5.1", AV_CHANNEL_LAYOUT_5POINT1_BACK },
  180. { "5.1(side)", AV_CHANNEL_LAYOUT_5POINT1 },
  181. { "6.0", AV_CHANNEL_LAYOUT_6POINT0 },
  182. { "6.0(front)", AV_CHANNEL_LAYOUT_6POINT0_FRONT },
  183. { "3.1.2", AV_CHANNEL_LAYOUT_3POINT1POINT2 },
  184. { "hexagonal", AV_CHANNEL_LAYOUT_HEXAGONAL },
  185. { "6.1", AV_CHANNEL_LAYOUT_6POINT1 },
  186. { "6.1(back)", AV_CHANNEL_LAYOUT_6POINT1_BACK },
  187. { "6.1(front)", AV_CHANNEL_LAYOUT_6POINT1_FRONT },
  188. { "7.0", AV_CHANNEL_LAYOUT_7POINT0 },
  189. { "7.0(front)", AV_CHANNEL_LAYOUT_7POINT0_FRONT },
  190. { "7.1", AV_CHANNEL_LAYOUT_7POINT1 },
  191. { "7.1(wide)", AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK },
  192. { "7.1(wide-side)", AV_CHANNEL_LAYOUT_7POINT1_WIDE },
  193. { "5.1.2", AV_CHANNEL_LAYOUT_5POINT1POINT2_BACK },
  194. { "octagonal", AV_CHANNEL_LAYOUT_OCTAGONAL },
  195. { "cube", AV_CHANNEL_LAYOUT_CUBE },
  196. { "5.1.4", AV_CHANNEL_LAYOUT_5POINT1POINT4_BACK },
  197. { "7.1.2", AV_CHANNEL_LAYOUT_7POINT1POINT2 },
  198. { "7.1.4", AV_CHANNEL_LAYOUT_7POINT1POINT4_BACK },
  199. { "7.2.3", AV_CHANNEL_LAYOUT_7POINT2POINT3 },
  200. { "9.1.4", AV_CHANNEL_LAYOUT_9POINT1POINT4_BACK },
  201. { "9.1.6", AV_CHANNEL_LAYOUT_9POINT1POINT6 },
  202. { "hexadecagonal", AV_CHANNEL_LAYOUT_HEXADECAGONAL },
  203. { "binaural", AV_CHANNEL_LAYOUT_BINAURAL },
  204. { "downmix", AV_CHANNEL_LAYOUT_STEREO_DOWNMIX, },
  205. { "22.2", AV_CHANNEL_LAYOUT_22POINT2, },
  206. };
  207. int av_channel_layout_custom_init(AVChannelLayout *channel_layout, int nb_channels)
  208. {
  209. AVChannelCustom *map;
  210. if (nb_channels <= 0)
  211. return AVERROR(EINVAL);
  212. map = av_calloc(nb_channels, sizeof(*channel_layout->u.map));
  213. if (!map)
  214. return AVERROR(ENOMEM);
  215. for (int i = 0; i < nb_channels; i++)
  216. map[i].id = AV_CHAN_UNKNOWN;
  217. channel_layout->order = AV_CHANNEL_ORDER_CUSTOM;
  218. channel_layout->nb_channels = nb_channels;
  219. channel_layout->u.map = map;
  220. return 0;
  221. }
  222. int av_channel_layout_from_mask(AVChannelLayout *channel_layout,
  223. uint64_t mask)
  224. {
  225. if (!mask)
  226. return AVERROR(EINVAL);
  227. channel_layout->order = AV_CHANNEL_ORDER_NATIVE;
  228. channel_layout->nb_channels = av_popcount64(mask);
  229. channel_layout->u.mask = mask;
  230. return 0;
  231. }
  232. static int parse_channel_list(AVChannelLayout *ch_layout, const char *str)
  233. {
  234. int ret;
  235. int nb_channels = 0;
  236. AVChannelCustom *map = NULL;
  237. AVChannelCustom custom = {0};
  238. while (*str) {
  239. char *channel, *chname;
  240. int ret = av_opt_get_key_value(&str, "@", "+", AV_OPT_FLAG_IMPLICIT_KEY, &channel, &chname);
  241. if (ret < 0) {
  242. av_freep(&map);
  243. return ret;
  244. }
  245. if (*str)
  246. str++; // skip separator
  247. if (!channel) {
  248. channel = chname;
  249. chname = NULL;
  250. }
  251. av_strlcpy(custom.name, chname ? chname : "", sizeof(custom.name));
  252. custom.id = av_channel_from_string(channel);
  253. av_free(channel);
  254. av_free(chname);
  255. if (custom.id == AV_CHAN_NONE) {
  256. av_freep(&map);
  257. return AVERROR(EINVAL);
  258. }
  259. av_dynarray2_add((void **)&map, &nb_channels, sizeof(custom), (void *)&custom);
  260. if (!map)
  261. return AVERROR(ENOMEM);
  262. }
  263. if (!nb_channels)
  264. return AVERROR(EINVAL);
  265. ch_layout->order = AV_CHANNEL_ORDER_CUSTOM;
  266. ch_layout->u.map = map;
  267. ch_layout->nb_channels = nb_channels;
  268. ret = av_channel_layout_retype(ch_layout, 0, AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL);
  269. av_assert0(ret == 0);
  270. return 0;
  271. }
  272. int av_channel_layout_from_string(AVChannelLayout *channel_layout,
  273. const char *str)
  274. {
  275. int i, matches, ret;
  276. int channels = 0, nb_channels = 0;
  277. char *chlist, *end;
  278. uint64_t mask = 0;
  279. /* channel layout names */
  280. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++) {
  281. if (channel_layout_map[i].name && !strcmp(str, channel_layout_map[i].name)) {
  282. *channel_layout = channel_layout_map[i].layout;
  283. return 0;
  284. }
  285. }
  286. /* This function is a channel layout initializer, so we have to
  287. * zero-initialize before we start setting fields individually. */
  288. memset(channel_layout, 0, sizeof(*channel_layout));
  289. /* ambisonic */
  290. if (!strncmp(str, "ambisonic ", 10)) {
  291. const char *p = str + 10;
  292. char *endptr;
  293. AVChannelLayout extra = {0};
  294. int order;
  295. order = strtol(p, &endptr, 0);
  296. if (order < 0 || order + 1 > INT_MAX / (order + 1) ||
  297. (*endptr && *endptr != '+'))
  298. return AVERROR(EINVAL);
  299. channel_layout->order = AV_CHANNEL_ORDER_AMBISONIC;
  300. channel_layout->nb_channels = (order + 1) * (order + 1);
  301. if (*endptr) {
  302. int ret = av_channel_layout_from_string(&extra, endptr + 1);
  303. if (ret < 0)
  304. return ret;
  305. if (extra.nb_channels >= INT_MAX - channel_layout->nb_channels) {
  306. av_channel_layout_uninit(&extra);
  307. return AVERROR(EINVAL);
  308. }
  309. if (extra.order == AV_CHANNEL_ORDER_NATIVE) {
  310. channel_layout->u.mask = extra.u.mask;
  311. } else {
  312. channel_layout->order = AV_CHANNEL_ORDER_CUSTOM;
  313. channel_layout->u.map =
  314. av_calloc(channel_layout->nb_channels + extra.nb_channels,
  315. sizeof(*channel_layout->u.map));
  316. if (!channel_layout->u.map) {
  317. av_channel_layout_uninit(&extra);
  318. return AVERROR(ENOMEM);
  319. }
  320. for (i = 0; i < channel_layout->nb_channels; i++)
  321. channel_layout->u.map[i].id = AV_CHAN_AMBISONIC_BASE + i;
  322. for (i = 0; i < extra.nb_channels; i++) {
  323. enum AVChannel ch = av_channel_layout_channel_from_index(&extra, i);
  324. if (CHAN_IS_AMBI(ch)) {
  325. av_channel_layout_uninit(channel_layout);
  326. av_channel_layout_uninit(&extra);
  327. return AVERROR(EINVAL);
  328. }
  329. channel_layout->u.map[channel_layout->nb_channels + i].id = ch;
  330. if (extra.order == AV_CHANNEL_ORDER_CUSTOM &&
  331. extra.u.map[i].name[0])
  332. av_strlcpy(channel_layout->u.map[channel_layout->nb_channels + i].name,
  333. extra.u.map[i].name,
  334. sizeof(channel_layout->u.map[channel_layout->nb_channels + i].name));
  335. }
  336. }
  337. channel_layout->nb_channels += extra.nb_channels;
  338. av_channel_layout_uninit(&extra);
  339. }
  340. return 0;
  341. }
  342. chlist = av_strdup(str);
  343. if (!chlist)
  344. return AVERROR(ENOMEM);
  345. /* channel names */
  346. matches = av_sscanf(str, "%d channels (%[^)]", &nb_channels, chlist);
  347. ret = parse_channel_list(channel_layout, chlist);
  348. av_freep(&chlist);
  349. if (ret < 0 && ret != AVERROR(EINVAL))
  350. return ret;
  351. if (ret >= 0) {
  352. end = strchr(str, ')');
  353. if (matches == 2 && (nb_channels != channel_layout->nb_channels || !end || *++end)) {
  354. av_channel_layout_uninit(channel_layout);
  355. return AVERROR(EINVAL);
  356. }
  357. return 0;
  358. }
  359. errno = 0;
  360. mask = strtoull(str, &end, 0);
  361. /* channel layout mask */
  362. if (!errno && !*end && !strchr(str, '-') && mask) {
  363. av_channel_layout_from_mask(channel_layout, mask);
  364. return 0;
  365. }
  366. errno = 0;
  367. channels = strtol(str, &end, 10);
  368. /* number of channels */
  369. if (!errno && !strcmp(end, "c") && channels > 0) {
  370. av_channel_layout_default(channel_layout, channels);
  371. if (channel_layout->order == AV_CHANNEL_ORDER_NATIVE)
  372. return 0;
  373. }
  374. /* number of unordered channels */
  375. if (!errno && (!strcmp(end, "C") || !strcmp(end, " channels"))
  376. && channels > 0) {
  377. channel_layout->order = AV_CHANNEL_ORDER_UNSPEC;
  378. channel_layout->nb_channels = channels;
  379. return 0;
  380. }
  381. return AVERROR(EINVAL);
  382. }
  383. void av_channel_layout_uninit(AVChannelLayout *channel_layout)
  384. {
  385. if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM)
  386. av_freep(&channel_layout->u.map);
  387. memset(channel_layout, 0, sizeof(*channel_layout));
  388. }
  389. int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
  390. {
  391. av_channel_layout_uninit(dst);
  392. *dst = *src;
  393. if (src->order == AV_CHANNEL_ORDER_CUSTOM) {
  394. dst->u.map = av_malloc_array(src->nb_channels, sizeof(*dst->u.map));
  395. if (!dst->u.map)
  396. return AVERROR(ENOMEM);
  397. memcpy(dst->u.map, src->u.map, src->nb_channels * sizeof(*src->u.map));
  398. }
  399. return 0;
  400. }
  401. static int64_t masked_description(const AVChannelLayout *channel_layout, int start_channel)
  402. {
  403. uint64_t mask = 0;
  404. for (int i = start_channel; i < channel_layout->nb_channels; i++) {
  405. enum AVChannel ch = channel_layout->u.map[i].id;
  406. if (ch >= 0 && ch < 63 && mask < (1ULL << ch))
  407. mask |= (1ULL << ch);
  408. else
  409. return AVERROR(EINVAL);
  410. }
  411. return mask;
  412. }
  413. static int has_channel_names(const AVChannelLayout *channel_layout)
  414. {
  415. if (channel_layout->order != AV_CHANNEL_ORDER_CUSTOM)
  416. return 0;
  417. for (int i = 0; i < channel_layout->nb_channels; i++)
  418. if (channel_layout->u.map[i].name[0])
  419. return 1;
  420. return 0;
  421. }
  422. int av_channel_layout_ambisonic_order(const AVChannelLayout *channel_layout)
  423. {
  424. int i, highest_ambi, order;
  425. if (channel_layout->order != AV_CHANNEL_ORDER_AMBISONIC &&
  426. channel_layout->order != AV_CHANNEL_ORDER_CUSTOM)
  427. return AVERROR(EINVAL);
  428. highest_ambi = -1;
  429. if (channel_layout->order == AV_CHANNEL_ORDER_AMBISONIC)
  430. highest_ambi = channel_layout->nb_channels - av_popcount64(channel_layout->u.mask) - 1;
  431. else {
  432. const AVChannelCustom *map = channel_layout->u.map;
  433. av_assert0(channel_layout->order == AV_CHANNEL_ORDER_CUSTOM);
  434. for (i = 0; i < channel_layout->nb_channels; i++) {
  435. int is_ambi = CHAN_IS_AMBI(map[i].id);
  436. /* ambisonic following non-ambisonic */
  437. if (i > 0 && is_ambi && !CHAN_IS_AMBI(map[i - 1].id))
  438. return AVERROR(EINVAL);
  439. /* non-default ordering */
  440. if (is_ambi && map[i].id - AV_CHAN_AMBISONIC_BASE != i)
  441. return AVERROR(EINVAL);
  442. if (CHAN_IS_AMBI(map[i].id))
  443. highest_ambi = i;
  444. }
  445. }
  446. /* no ambisonic channels*/
  447. if (highest_ambi < 0)
  448. return AVERROR(EINVAL);
  449. order = floor(sqrt(highest_ambi));
  450. /* incomplete order - some harmonics are missing */
  451. if ((order + 1) * (order + 1) != highest_ambi + 1)
  452. return AVERROR(EINVAL);
  453. return order;
  454. }
  455. static enum AVChannelOrder canonical_order(AVChannelLayout *channel_layout)
  456. {
  457. int has_known_channel = 0;
  458. int order;
  459. if (channel_layout->order != AV_CHANNEL_ORDER_CUSTOM)
  460. return channel_layout->order;
  461. if (has_channel_names(channel_layout))
  462. return AV_CHANNEL_ORDER_CUSTOM;
  463. for (int i = 0; i < channel_layout->nb_channels && !has_known_channel; i++)
  464. if (channel_layout->u.map[i].id != AV_CHAN_UNKNOWN)
  465. has_known_channel = 1;
  466. if (!has_known_channel)
  467. return AV_CHANNEL_ORDER_UNSPEC;
  468. if (masked_description(channel_layout, 0) > 0)
  469. return AV_CHANNEL_ORDER_NATIVE;
  470. order = av_channel_layout_ambisonic_order(channel_layout);
  471. if (order >= 0 && masked_description(channel_layout, (order + 1) * (order + 1)) >= 0)
  472. return AV_CHANNEL_ORDER_AMBISONIC;
  473. return AV_CHANNEL_ORDER_CUSTOM;
  474. }
  475. /**
  476. * If the custom layout is n-th order standard-order ambisonic, with optional
  477. * extra non-diegetic channels at the end, write its string description in bp.
  478. * Return a negative error code otherwise.
  479. */
  480. static int try_describe_ambisonic(AVBPrint *bp, const AVChannelLayout *channel_layout)
  481. {
  482. int nb_ambi_channels;
  483. int order = av_channel_layout_ambisonic_order(channel_layout);
  484. if (order < 0)
  485. return order;
  486. av_bprintf(bp, "ambisonic %d", order);
  487. /* extra channels present */
  488. nb_ambi_channels = (order + 1) * (order + 1);
  489. if (nb_ambi_channels < channel_layout->nb_channels) {
  490. AVChannelLayout extra = { 0 };
  491. if (channel_layout->order == AV_CHANNEL_ORDER_AMBISONIC) {
  492. extra.order = AV_CHANNEL_ORDER_NATIVE;
  493. extra.nb_channels = av_popcount64(channel_layout->u.mask);
  494. extra.u.mask = channel_layout->u.mask;
  495. } else {
  496. int64_t mask;
  497. if (!has_channel_names(channel_layout) &&
  498. (mask = masked_description(channel_layout, nb_ambi_channels)) > 0) {
  499. extra.order = AV_CHANNEL_ORDER_NATIVE;
  500. extra.nb_channels = av_popcount64(mask);
  501. extra.u.mask = mask;
  502. } else {
  503. extra.order = AV_CHANNEL_ORDER_CUSTOM;
  504. extra.nb_channels = channel_layout->nb_channels - nb_ambi_channels;
  505. extra.u.map = channel_layout->u.map + nb_ambi_channels;
  506. }
  507. }
  508. av_bprint_chars(bp, '+', 1);
  509. av_channel_layout_describe_bprint(&extra, bp);
  510. /* Not calling uninit here on extra because we don't own the u.map pointer */
  511. }
  512. return 0;
  513. }
  514. int av_channel_layout_describe_bprint(const AVChannelLayout *channel_layout,
  515. AVBPrint *bp)
  516. {
  517. int i;
  518. switch (channel_layout->order) {
  519. case AV_CHANNEL_ORDER_NATIVE:
  520. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
  521. if (channel_layout->u.mask == channel_layout_map[i].layout.u.mask) {
  522. av_bprintf(bp, "%s", channel_layout_map[i].name);
  523. return 0;
  524. }
  525. // fall-through
  526. case AV_CHANNEL_ORDER_CUSTOM:
  527. if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
  528. int64_t mask;
  529. int res = try_describe_ambisonic(bp, channel_layout);
  530. if (res >= 0)
  531. return 0;
  532. if (!has_channel_names(channel_layout) &&
  533. (mask = masked_description(channel_layout, 0)) > 0) {
  534. AVChannelLayout native = { .order = AV_CHANNEL_ORDER_NATIVE,
  535. .nb_channels = av_popcount64(mask),
  536. .u.mask = mask };
  537. return av_channel_layout_describe_bprint(&native, bp);
  538. }
  539. }
  540. if (channel_layout->nb_channels)
  541. av_bprintf(bp, "%d channels (", channel_layout->nb_channels);
  542. for (i = 0; i < channel_layout->nb_channels; i++) {
  543. enum AVChannel ch = av_channel_layout_channel_from_index(channel_layout, i);
  544. if (i)
  545. av_bprintf(bp, "+");
  546. av_channel_name_bprint(bp, ch);
  547. if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM &&
  548. channel_layout->u.map[i].name[0])
  549. av_bprintf(bp, "@%s", channel_layout->u.map[i].name);
  550. }
  551. if (channel_layout->nb_channels) {
  552. av_bprintf(bp, ")");
  553. return 0;
  554. }
  555. // fall-through
  556. case AV_CHANNEL_ORDER_UNSPEC:
  557. av_bprintf(bp, "%d channels", channel_layout->nb_channels);
  558. return 0;
  559. case AV_CHANNEL_ORDER_AMBISONIC:
  560. return try_describe_ambisonic(bp, channel_layout);
  561. default:
  562. return AVERROR(EINVAL);
  563. }
  564. }
  565. int av_channel_layout_describe(const AVChannelLayout *channel_layout,
  566. char *buf, size_t buf_size)
  567. {
  568. AVBPrint bp;
  569. int ret;
  570. if (!buf && buf_size)
  571. return AVERROR(EINVAL);
  572. av_bprint_init_for_buffer(&bp, buf, buf_size);
  573. ret = av_channel_layout_describe_bprint(channel_layout, &bp);
  574. if (ret < 0)
  575. return ret;
  576. if (bp.len >= INT_MAX)
  577. return AVERROR(ERANGE);
  578. return bp.len + 1;
  579. }
  580. enum AVChannel
  581. av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout,
  582. unsigned int idx)
  583. {
  584. int i;
  585. if (idx >= channel_layout->nb_channels)
  586. return AV_CHAN_NONE;
  587. switch (channel_layout->order) {
  588. case AV_CHANNEL_ORDER_CUSTOM:
  589. return channel_layout->u.map[idx].id;
  590. case AV_CHANNEL_ORDER_AMBISONIC: {
  591. int ambi_channels = channel_layout->nb_channels - av_popcount64(channel_layout->u.mask);
  592. if (idx < ambi_channels)
  593. return AV_CHAN_AMBISONIC_BASE + idx;
  594. idx -= ambi_channels;
  595. }
  596. // fall-through
  597. case AV_CHANNEL_ORDER_NATIVE:
  598. for (i = 0; i < 64; i++) {
  599. if ((1ULL << i) & channel_layout->u.mask && !idx--)
  600. return i;
  601. }
  602. default:
  603. return AV_CHAN_NONE;
  604. }
  605. }
  606. enum AVChannel
  607. av_channel_layout_channel_from_string(const AVChannelLayout *channel_layout,
  608. const char *str)
  609. {
  610. int index = av_channel_layout_index_from_string(channel_layout, str);
  611. if (index < 0)
  612. return AV_CHAN_NONE;
  613. return av_channel_layout_channel_from_index(channel_layout, index);
  614. }
  615. int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout,
  616. enum AVChannel channel)
  617. {
  618. int i;
  619. if (channel == AV_CHAN_NONE)
  620. return AVERROR(EINVAL);
  621. switch (channel_layout->order) {
  622. case AV_CHANNEL_ORDER_CUSTOM:
  623. for (i = 0; i < channel_layout->nb_channels; i++)
  624. if (channel_layout->u.map[i].id == channel)
  625. return i;
  626. return AVERROR(EINVAL);
  627. case AV_CHANNEL_ORDER_AMBISONIC:
  628. case AV_CHANNEL_ORDER_NATIVE: {
  629. uint64_t mask = channel_layout->u.mask;
  630. int ambi_channels = channel_layout->nb_channels - av_popcount64(mask);
  631. if (channel_layout->order == AV_CHANNEL_ORDER_AMBISONIC &&
  632. channel >= AV_CHAN_AMBISONIC_BASE) {
  633. if (channel - AV_CHAN_AMBISONIC_BASE >= ambi_channels)
  634. return AVERROR(EINVAL);
  635. return channel - AV_CHAN_AMBISONIC_BASE;
  636. }
  637. if ((unsigned)channel > 63 || !(mask & (1ULL << channel)))
  638. return AVERROR(EINVAL);
  639. mask &= (1ULL << channel) - 1;
  640. return av_popcount64(mask) + ambi_channels;
  641. }
  642. default:
  643. return AVERROR(EINVAL);
  644. }
  645. }
  646. int av_channel_layout_index_from_string(const AVChannelLayout *channel_layout,
  647. const char *str)
  648. {
  649. char *chname;
  650. enum AVChannel ch = AV_CHAN_NONE;
  651. switch (channel_layout->order) {
  652. case AV_CHANNEL_ORDER_CUSTOM:
  653. chname = strstr(str, "@");
  654. if (chname) {
  655. char buf[16];
  656. chname++;
  657. av_strlcpy(buf, str, FFMIN(sizeof(buf), chname - str));
  658. if (!*chname)
  659. chname = NULL;
  660. ch = av_channel_from_string(buf);
  661. if (ch == AV_CHAN_NONE && *buf)
  662. return AVERROR(EINVAL);
  663. }
  664. for (int i = 0; chname && i < channel_layout->nb_channels; i++) {
  665. if (!strcmp(chname, channel_layout->u.map[i].name) &&
  666. (ch == AV_CHAN_NONE || ch == channel_layout->u.map[i].id))
  667. return i;
  668. }
  669. // fall-through
  670. case AV_CHANNEL_ORDER_AMBISONIC:
  671. case AV_CHANNEL_ORDER_NATIVE:
  672. ch = av_channel_from_string(str);
  673. if (ch == AV_CHAN_NONE)
  674. return AVERROR(EINVAL);
  675. return av_channel_layout_index_from_channel(channel_layout, ch);
  676. }
  677. return AVERROR(EINVAL);
  678. }
  679. int av_channel_layout_check(const AVChannelLayout *channel_layout)
  680. {
  681. if (channel_layout->nb_channels <= 0)
  682. return 0;
  683. switch (channel_layout->order) {
  684. case AV_CHANNEL_ORDER_NATIVE:
  685. return av_popcount64(channel_layout->u.mask) == channel_layout->nb_channels;
  686. case AV_CHANNEL_ORDER_CUSTOM:
  687. if (!channel_layout->u.map)
  688. return 0;
  689. for (int i = 0; i < channel_layout->nb_channels; i++) {
  690. if (channel_layout->u.map[i].id == AV_CHAN_NONE)
  691. return 0;
  692. }
  693. return 1;
  694. case AV_CHANNEL_ORDER_AMBISONIC:
  695. /* If non-diegetic channels are present, ensure they are taken into account */
  696. return av_popcount64(channel_layout->u.mask) < channel_layout->nb_channels;
  697. case AV_CHANNEL_ORDER_UNSPEC:
  698. return 1;
  699. default:
  700. return 0;
  701. }
  702. }
  703. int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
  704. {
  705. int i;
  706. /* different channel counts -> not equal */
  707. if (chl->nb_channels != chl1->nb_channels)
  708. return 1;
  709. /* if only one is unspecified -> not equal */
  710. if ((chl->order == AV_CHANNEL_ORDER_UNSPEC) !=
  711. (chl1->order == AV_CHANNEL_ORDER_UNSPEC))
  712. return 1;
  713. /* both are unspecified -> equal */
  714. else if (chl->order == AV_CHANNEL_ORDER_UNSPEC)
  715. return 0;
  716. /* can compare masks directly */
  717. if ((chl->order == AV_CHANNEL_ORDER_NATIVE ||
  718. chl->order == AV_CHANNEL_ORDER_AMBISONIC) &&
  719. chl->order == chl1->order)
  720. return chl->u.mask != chl1->u.mask;
  721. /* compare channel by channel */
  722. for (i = 0; i < chl->nb_channels; i++)
  723. if (av_channel_layout_channel_from_index(chl, i) !=
  724. av_channel_layout_channel_from_index(chl1, i))
  725. return 1;
  726. return 0;
  727. }
  728. void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
  729. {
  730. int i;
  731. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
  732. if (nb_channels == channel_layout_map[i].layout.nb_channels) {
  733. *ch_layout = channel_layout_map[i].layout;
  734. return;
  735. }
  736. ch_layout->order = AV_CHANNEL_ORDER_UNSPEC;
  737. ch_layout->nb_channels = nb_channels;
  738. }
  739. const AVChannelLayout *av_channel_layout_standard(void **opaque)
  740. {
  741. uintptr_t i = (uintptr_t)*opaque;
  742. const AVChannelLayout *ch_layout = NULL;
  743. if (i < FF_ARRAY_ELEMS(channel_layout_map)) {
  744. ch_layout = &channel_layout_map[i].layout;
  745. *opaque = (void*)(i + 1);
  746. }
  747. return ch_layout;
  748. }
  749. uint64_t av_channel_layout_subset(const AVChannelLayout *channel_layout,
  750. uint64_t mask)
  751. {
  752. uint64_t ret = 0;
  753. int i;
  754. switch (channel_layout->order) {
  755. case AV_CHANNEL_ORDER_NATIVE:
  756. case AV_CHANNEL_ORDER_AMBISONIC:
  757. return channel_layout->u.mask & mask;
  758. case AV_CHANNEL_ORDER_CUSTOM:
  759. for (i = 0; i < 64; i++)
  760. if (mask & (1ULL << i) && av_channel_layout_index_from_channel(channel_layout, i) >= 0)
  761. ret |= (1ULL << i);
  762. break;
  763. }
  764. return ret;
  765. }
  766. int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrder order, int flags)
  767. {
  768. int allow_lossy = !(flags & AV_CHANNEL_LAYOUT_RETYPE_FLAG_LOSSLESS);
  769. int lossy;
  770. if (!av_channel_layout_check(channel_layout))
  771. return AVERROR(EINVAL);
  772. if (flags & AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL)
  773. order = canonical_order(channel_layout);
  774. if (channel_layout->order == order)
  775. return 0;
  776. switch (order) {
  777. case AV_CHANNEL_ORDER_UNSPEC: {
  778. int nb_channels = channel_layout->nb_channels;
  779. if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
  780. lossy = 0;
  781. for (int i = 0; i < nb_channels; i++) {
  782. if (channel_layout->u.map[i].id != AV_CHAN_UNKNOWN || channel_layout->u.map[i].name[0]) {
  783. lossy = 1;
  784. break;
  785. }
  786. }
  787. } else {
  788. lossy = 1;
  789. }
  790. if (!lossy || allow_lossy) {
  791. void *opaque = channel_layout->opaque;
  792. av_channel_layout_uninit(channel_layout);
  793. channel_layout->order = AV_CHANNEL_ORDER_UNSPEC;
  794. channel_layout->nb_channels = nb_channels;
  795. channel_layout->opaque = opaque;
  796. return lossy;
  797. }
  798. return AVERROR(ENOSYS);
  799. }
  800. case AV_CHANNEL_ORDER_NATIVE:
  801. if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
  802. int64_t mask = masked_description(channel_layout, 0);
  803. if (mask < 0)
  804. return AVERROR(ENOSYS);
  805. lossy = has_channel_names(channel_layout);
  806. if (!lossy || allow_lossy) {
  807. void *opaque = channel_layout->opaque;
  808. av_channel_layout_uninit(channel_layout);
  809. av_channel_layout_from_mask(channel_layout, mask);
  810. channel_layout->opaque = opaque;
  811. return lossy;
  812. }
  813. }
  814. return AVERROR(ENOSYS);
  815. case AV_CHANNEL_ORDER_CUSTOM: {
  816. AVChannelLayout custom = { 0 };
  817. int ret = av_channel_layout_custom_init(&custom, channel_layout->nb_channels);
  818. void *opaque = channel_layout->opaque;
  819. if (ret < 0)
  820. return ret;
  821. if (channel_layout->order != AV_CHANNEL_ORDER_UNSPEC)
  822. for (int i = 0; i < channel_layout->nb_channels; i++)
  823. custom.u.map[i].id = av_channel_layout_channel_from_index(channel_layout, i);
  824. av_channel_layout_uninit(channel_layout);
  825. *channel_layout = custom;
  826. channel_layout->opaque = opaque;
  827. return 0;
  828. }
  829. case AV_CHANNEL_ORDER_AMBISONIC:
  830. if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
  831. int64_t mask;
  832. int nb_channels = channel_layout->nb_channels;
  833. int order = av_channel_layout_ambisonic_order(channel_layout);
  834. if (order < 0)
  835. return AVERROR(ENOSYS);
  836. mask = masked_description(channel_layout, (order + 1) * (order + 1));
  837. if (mask < 0)
  838. return AVERROR(ENOSYS);
  839. lossy = has_channel_names(channel_layout);
  840. if (!lossy || allow_lossy) {
  841. void *opaque = channel_layout->opaque;
  842. av_channel_layout_uninit(channel_layout);
  843. channel_layout->order = AV_CHANNEL_ORDER_AMBISONIC;
  844. channel_layout->nb_channels = nb_channels;
  845. channel_layout->u.mask = mask;
  846. channel_layout->opaque = opaque;
  847. return lossy;
  848. }
  849. }
  850. return AVERROR(ENOSYS);
  851. default:
  852. return AVERROR(EINVAL);
  853. }
  854. }