af_channelmap.c 14 KB

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