af_channelmap.c 13 KB

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