You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

421 lines
13KB

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