af_channelmap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright (c) 2012 Google, Inc.
  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 mapping filter
  23. */
  24. #include <ctype.h>
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/samplefmt.h"
  31. #include "audio.h"
  32. #include "avfilter.h"
  33. #include "formats.h"
  34. #include "internal.h"
  35. struct ChannelMap {
  36. uint64_t in_channel;
  37. uint64_t out_channel;
  38. int in_channel_idx;
  39. int out_channel_idx;
  40. };
  41. enum MappingMode {
  42. MAP_NONE,
  43. MAP_ONE_INT,
  44. MAP_ONE_STR,
  45. MAP_PAIR_INT_INT,
  46. MAP_PAIR_INT_STR,
  47. MAP_PAIR_STR_INT,
  48. MAP_PAIR_STR_STR
  49. };
  50. #define MAX_CH 64
  51. typedef struct ChannelMapContext {
  52. const AVClass *class;
  53. AVFilterChannelLayouts *channel_layouts;
  54. char *mapping_str;
  55. char *channel_layout_str;
  56. uint64_t output_layout;
  57. struct ChannelMap map[MAX_CH];
  58. int nch;
  59. enum MappingMode mode;
  60. } ChannelMapContext;
  61. #define OFFSET(x) offsetof(ChannelMapContext, x)
  62. #define A AV_OPT_FLAG_AUDIO_PARAM
  63. #define F AV_OPT_FLAG_FILTERING_PARAM
  64. static const AVOption options[] = {
  65. { "map", "A comma-separated list of input channel numbers in output order.",
  66. OFFSET(mapping_str), AV_OPT_TYPE_STRING, .flags = A|F },
  67. { "channel_layout", "Output channel layout.",
  68. OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A|F },
  69. { NULL },
  70. };
  71. static const AVClass channelmap_class = {
  72. .class_name = "channel map filter",
  73. .item_name = av_default_item_name,
  74. .option = options,
  75. .version = LIBAVUTIL_VERSION_INT,
  76. };
  77. static char* split(char *message, char delim) {
  78. char *next = strchr(message, delim);
  79. if (next)
  80. *next++ = '\0';
  81. return next;
  82. }
  83. static int get_channel_idx(char **map, int *ch, char delim, int max_ch)
  84. {
  85. char *next = split(*map, delim);
  86. int len;
  87. int n = 0;
  88. if (!next && delim == '-')
  89. return AVERROR(EINVAL);
  90. len = strlen(*map);
  91. sscanf(*map, "%d%n", ch, &n);
  92. if (n != len)
  93. return AVERROR(EINVAL);
  94. if (*ch < 0 || *ch > max_ch)
  95. return AVERROR(EINVAL);
  96. *map = next;
  97. return 0;
  98. }
  99. static int get_channel(char **map, uint64_t *ch, char delim)
  100. {
  101. char *next = split(*map, delim);
  102. if (!next && delim == '-')
  103. return AVERROR(EINVAL);
  104. *ch = av_get_channel_layout(*map);
  105. if (av_get_channel_layout_nb_channels(*ch) != 1)
  106. return AVERROR(EINVAL);
  107. *map = next;
  108. return 0;
  109. }
  110. static av_cold int channelmap_init(AVFilterContext *ctx, const char *args)
  111. {
  112. ChannelMapContext *s = ctx->priv;
  113. int ret;
  114. char *mapping;
  115. int map_entries = 0;
  116. char buf[256];
  117. enum MappingMode mode;
  118. uint64_t out_ch_mask = 0;
  119. int i;
  120. if (!args) {
  121. av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
  122. return AVERROR(EINVAL);
  123. }
  124. s->class = &channelmap_class;
  125. av_opt_set_defaults(s);
  126. if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
  127. return ret;
  128. mapping = s->mapping_str;
  129. if (!mapping) {
  130. mode = MAP_NONE;
  131. } else {
  132. char *dash = strchr(mapping, '-');
  133. if (!dash) { // short mapping
  134. if (isdigit(*mapping))
  135. mode = MAP_ONE_INT;
  136. else
  137. mode = MAP_ONE_STR;
  138. } else if (isdigit(*mapping)) {
  139. if (isdigit(*(dash+1)))
  140. mode = MAP_PAIR_INT_INT;
  141. else
  142. mode = MAP_PAIR_INT_STR;
  143. } else {
  144. if (isdigit(*(dash+1)))
  145. mode = MAP_PAIR_STR_INT;
  146. else
  147. mode = MAP_PAIR_STR_STR;
  148. }
  149. }
  150. if (mode != MAP_NONE) {
  151. char *comma = mapping;
  152. map_entries = 1;
  153. while ((comma = strchr(comma, ','))) {
  154. if (*++comma) // Allow trailing comma
  155. map_entries++;
  156. }
  157. }
  158. if (map_entries > MAX_CH) {
  159. av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
  160. ret = AVERROR(EINVAL);
  161. goto fail;
  162. }
  163. for (i = 0; i < map_entries; i++) {
  164. int in_ch_idx = -1, out_ch_idx = -1;
  165. uint64_t in_ch = 0, out_ch = 0;
  166. static const char err[] = "Failed to parse channel map\n";
  167. switch (mode) {
  168. case MAP_ONE_INT:
  169. if (get_channel_idx(&mapping, &in_ch_idx, ',', MAX_CH) < 0) {
  170. ret = AVERROR(EINVAL);
  171. av_log(ctx, AV_LOG_ERROR, err);
  172. goto fail;
  173. }
  174. s->map[i].in_channel_idx = in_ch_idx;
  175. s->map[i].out_channel_idx = i;
  176. break;
  177. case MAP_ONE_STR:
  178. if (get_channel(&mapping, &in_ch, ',') < 0) {
  179. av_log(ctx, AV_LOG_ERROR, err);
  180. ret = AVERROR(EINVAL);
  181. goto fail;
  182. }
  183. s->map[i].in_channel = in_ch;
  184. s->map[i].out_channel_idx = i;
  185. break;
  186. case MAP_PAIR_INT_INT:
  187. if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
  188. get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) {
  189. av_log(ctx, AV_LOG_ERROR, err);
  190. ret = AVERROR(EINVAL);
  191. goto fail;
  192. }
  193. s->map[i].in_channel_idx = in_ch_idx;
  194. s->map[i].out_channel_idx = out_ch_idx;
  195. break;
  196. case MAP_PAIR_INT_STR:
  197. if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
  198. get_channel(&mapping, &out_ch, ',') < 0 ||
  199. out_ch & out_ch_mask) {
  200. av_log(ctx, AV_LOG_ERROR, err);
  201. ret = AVERROR(EINVAL);
  202. goto fail;
  203. }
  204. s->map[i].in_channel_idx = in_ch_idx;
  205. s->map[i].out_channel = out_ch;
  206. out_ch_mask |= out_ch;
  207. break;
  208. case MAP_PAIR_STR_INT:
  209. if (get_channel(&mapping, &in_ch, '-') < 0 ||
  210. get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) {
  211. av_log(ctx, AV_LOG_ERROR, err);
  212. ret = AVERROR(EINVAL);
  213. goto fail;
  214. }
  215. s->map[i].in_channel = in_ch;
  216. s->map[i].out_channel_idx = out_ch_idx;
  217. break;
  218. case MAP_PAIR_STR_STR:
  219. if (get_channel(&mapping, &in_ch, '-') < 0 ||
  220. get_channel(&mapping, &out_ch, ',') < 0 ||
  221. out_ch & out_ch_mask) {
  222. av_log(ctx, AV_LOG_ERROR, err);
  223. ret = AVERROR(EINVAL);
  224. goto fail;
  225. }
  226. s->map[i].in_channel = in_ch;
  227. s->map[i].out_channel = out_ch;
  228. out_ch_mask |= out_ch;
  229. break;
  230. }
  231. }
  232. s->mode = mode;
  233. s->nch = map_entries;
  234. s->output_layout = out_ch_mask ? out_ch_mask :
  235. av_get_default_channel_layout(map_entries);
  236. if (s->channel_layout_str) {
  237. uint64_t fmt;
  238. if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
  239. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
  240. s->channel_layout_str);
  241. ret = AVERROR(EINVAL);
  242. goto fail;
  243. }
  244. if (mode == MAP_NONE) {
  245. int i;
  246. s->nch = av_get_channel_layout_nb_channels(fmt);
  247. for (i = 0; i < s->nch; i++) {
  248. s->map[i].in_channel_idx = i;
  249. s->map[i].out_channel_idx = i;
  250. }
  251. } else if (out_ch_mask && out_ch_mask != fmt) {
  252. av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
  253. av_log(ctx, AV_LOG_ERROR,
  254. "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
  255. s->channel_layout_str, buf);
  256. ret = AVERROR(EINVAL);
  257. goto fail;
  258. } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
  259. av_log(ctx, AV_LOG_ERROR,
  260. "Output channel layout %s does not match the number of channels mapped %d.\n",
  261. s->channel_layout_str, s->nch);
  262. ret = AVERROR(EINVAL);
  263. goto fail;
  264. }
  265. s->output_layout = fmt;
  266. }
  267. ff_add_channel_layout(&s->channel_layouts, s->output_layout);
  268. if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
  269. for (i = 0; i < s->nch; i++) {
  270. s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
  271. s->output_layout, s->map[i].out_channel);
  272. }
  273. }
  274. fail:
  275. av_opt_free(s);
  276. return ret;
  277. }
  278. static int channelmap_query_formats(AVFilterContext *ctx)
  279. {
  280. ChannelMapContext *s = ctx->priv;
  281. ff_set_common_formats(ctx, ff_planar_sample_fmts());
  282. ff_set_common_samplerates(ctx, ff_all_samplerates());
  283. ff_channel_layouts_ref(ff_all_channel_layouts(), &ctx->inputs[0]->out_channel_layouts);
  284. ff_channel_layouts_ref(s->channel_layouts, &ctx->outputs[0]->in_channel_layouts);
  285. return 0;
  286. }
  287. static int channelmap_filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
  288. {
  289. AVFilterContext *ctx = inlink->dst;
  290. AVFilterLink *outlink = ctx->outputs[0];
  291. const ChannelMapContext *s = ctx->priv;
  292. const int nch_in = av_get_channel_layout_nb_channels(inlink->channel_layout);
  293. const int nch_out = s->nch;
  294. int ch;
  295. uint8_t *source_planes[MAX_CH];
  296. memcpy(source_planes, buf->extended_data,
  297. nch_in * sizeof(source_planes[0]));
  298. if (nch_out > nch_in) {
  299. if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
  300. uint8_t **new_extended_data =
  301. av_mallocz(nch_out * sizeof(*buf->extended_data));
  302. if (!new_extended_data) {
  303. avfilter_unref_buffer(buf);
  304. return AVERROR(ENOMEM);
  305. }
  306. if (buf->extended_data == buf->data) {
  307. buf->extended_data = new_extended_data;
  308. } else {
  309. av_free(buf->extended_data);
  310. buf->extended_data = new_extended_data;
  311. }
  312. } else if (buf->extended_data != buf->data) {
  313. av_free(buf->extended_data);
  314. buf->extended_data = buf->data;
  315. }
  316. }
  317. for (ch = 0; ch < nch_out; ch++) {
  318. buf->extended_data[s->map[ch].out_channel_idx] =
  319. source_planes[s->map[ch].in_channel_idx];
  320. }
  321. if (buf->data != buf->extended_data)
  322. memcpy(buf->data, buf->extended_data,
  323. FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
  324. return ff_filter_frame(outlink, buf);
  325. }
  326. static int channelmap_config_input(AVFilterLink *inlink)
  327. {
  328. AVFilterContext *ctx = inlink->dst;
  329. ChannelMapContext *s = ctx->priv;
  330. int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
  331. int i, err = 0;
  332. const char *channel_name;
  333. char layout_name[256];
  334. for (i = 0; i < s->nch; i++) {
  335. if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
  336. s->map[i].in_channel_idx = av_get_channel_layout_channel_index(
  337. inlink->channel_layout, s->map[i].in_channel);
  338. }
  339. if (s->map[i].in_channel_idx < 0 ||
  340. s->map[i].in_channel_idx >= nb_channels) {
  341. av_get_channel_layout_string(layout_name, sizeof(layout_name),
  342. 0, inlink->channel_layout);
  343. if (s->map[i].in_channel) {
  344. channel_name = av_get_channel_name(s->map[i].in_channel);
  345. av_log(ctx, AV_LOG_ERROR,
  346. "input channel '%s' not available from input layout '%s'\n",
  347. channel_name, layout_name);
  348. } else {
  349. av_log(ctx, AV_LOG_ERROR,
  350. "input channel #%d not available from input layout '%s'\n",
  351. s->map[i].in_channel_idx, layout_name);
  352. }
  353. err = AVERROR(EINVAL);
  354. }
  355. }
  356. return err;
  357. }
  358. static const AVFilterPad avfilter_af_channelmap_inputs[] = {
  359. {
  360. .name = "default",
  361. .type = AVMEDIA_TYPE_AUDIO,
  362. .min_perms = AV_PERM_READ | AV_PERM_WRITE,
  363. .filter_frame = channelmap_filter_frame,
  364. .config_props = channelmap_config_input
  365. },
  366. { NULL }
  367. };
  368. static const AVFilterPad avfilter_af_channelmap_outputs[] = {
  369. {
  370. .name = "default",
  371. .type = AVMEDIA_TYPE_AUDIO
  372. },
  373. { NULL }
  374. };
  375. AVFilter avfilter_af_channelmap = {
  376. .name = "channelmap",
  377. .description = NULL_IF_CONFIG_SMALL("Remap audio channels."),
  378. .init = channelmap_init,
  379. .query_formats = channelmap_query_formats,
  380. .priv_size = sizeof(ChannelMapContext),
  381. .inputs = avfilter_af_channelmap_inputs,
  382. .outputs = avfilter_af_channelmap_outputs,
  383. .priv_class = &channelmap_class,
  384. };