af_channelmap.c 13 KB

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