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.

420 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. 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. if (!*map)
  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. ff_add_channel_layout(&s->channel_layouts, s->output_layout);
  258. if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
  259. for (i = 0; i < s->nch; i++) {
  260. s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
  261. s->output_layout, s->map[i].out_channel);
  262. }
  263. }
  264. return 0;
  265. }
  266. static int channelmap_query_formats(AVFilterContext *ctx)
  267. {
  268. ChannelMapContext *s = ctx->priv;
  269. AVFilterChannelLayouts *layouts;
  270. ff_set_common_formats(ctx, ff_planar_sample_fmts());
  271. ff_set_common_samplerates(ctx, ff_all_samplerates());
  272. layouts = ff_all_channel_layouts();
  273. if (!layouts)
  274. return AVERROR(ENOMEM);
  275. ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
  276. ff_channel_layouts_ref(s->channel_layouts, &ctx->outputs[0]->in_channel_layouts);
  277. return 0;
  278. }
  279. static int channelmap_filter_frame(AVFilterLink *inlink, AVFrame *buf)
  280. {
  281. AVFilterContext *ctx = inlink->dst;
  282. AVFilterLink *outlink = ctx->outputs[0];
  283. const ChannelMapContext *s = ctx->priv;
  284. const int nch_in = av_get_channel_layout_nb_channels(inlink->channel_layout);
  285. const int nch_out = s->nch;
  286. int ch;
  287. uint8_t *source_planes[MAX_CH];
  288. memcpy(source_planes, buf->extended_data,
  289. nch_in * sizeof(source_planes[0]));
  290. if (nch_out > nch_in) {
  291. if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
  292. uint8_t **new_extended_data =
  293. av_mallocz_array(nch_out, sizeof(*buf->extended_data));
  294. if (!new_extended_data) {
  295. av_frame_free(&buf);
  296. return AVERROR(ENOMEM);
  297. }
  298. if (buf->extended_data == buf->data) {
  299. buf->extended_data = new_extended_data;
  300. } else {
  301. av_free(buf->extended_data);
  302. buf->extended_data = new_extended_data;
  303. }
  304. } else if (buf->extended_data != buf->data) {
  305. av_free(buf->extended_data);
  306. buf->extended_data = buf->data;
  307. }
  308. }
  309. for (ch = 0; ch < nch_out; ch++) {
  310. buf->extended_data[s->map[ch].out_channel_idx] =
  311. source_planes[s->map[ch].in_channel_idx];
  312. }
  313. if (buf->data != buf->extended_data)
  314. memcpy(buf->data, buf->extended_data,
  315. FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
  316. buf->channel_layout = outlink->channel_layout;
  317. return ff_filter_frame(outlink, buf);
  318. }
  319. static int channelmap_config_input(AVFilterLink *inlink)
  320. {
  321. AVFilterContext *ctx = inlink->dst;
  322. ChannelMapContext *s = ctx->priv;
  323. int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
  324. int i, err = 0;
  325. const char *channel_name;
  326. char layout_name[256];
  327. for (i = 0; i < s->nch; i++) {
  328. struct ChannelMap *m = &s->map[i];
  329. if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
  330. m->in_channel_idx = av_get_channel_layout_channel_index(
  331. inlink->channel_layout, m->in_channel);
  332. }
  333. if (m->in_channel_idx < 0 || m->in_channel_idx >= nb_channels) {
  334. av_get_channel_layout_string(layout_name, sizeof(layout_name),
  335. 0, inlink->channel_layout);
  336. if (m->in_channel) {
  337. channel_name = av_get_channel_name(m->in_channel);
  338. av_log(ctx, AV_LOG_ERROR,
  339. "input channel '%s' not available from input layout '%s'\n",
  340. channel_name, layout_name);
  341. } else {
  342. av_log(ctx, AV_LOG_ERROR,
  343. "input channel #%d not available from input layout '%s'\n",
  344. m->in_channel_idx, layout_name);
  345. }
  346. err = AVERROR(EINVAL);
  347. }
  348. }
  349. return err;
  350. }
  351. static const AVFilterPad avfilter_af_channelmap_inputs[] = {
  352. {
  353. .name = "default",
  354. .type = AVMEDIA_TYPE_AUDIO,
  355. .filter_frame = channelmap_filter_frame,
  356. .config_props = channelmap_config_input,
  357. .needs_writable = 1,
  358. },
  359. { NULL }
  360. };
  361. static const AVFilterPad avfilter_af_channelmap_outputs[] = {
  362. {
  363. .name = "default",
  364. .type = AVMEDIA_TYPE_AUDIO
  365. },
  366. { NULL }
  367. };
  368. AVFilter ff_af_channelmap = {
  369. .name = "channelmap",
  370. .description = NULL_IF_CONFIG_SMALL("Remap audio channels."),
  371. .init = channelmap_init,
  372. .query_formats = channelmap_query_formats,
  373. .priv_size = sizeof(ChannelMapContext),
  374. .priv_class = &channelmap_class,
  375. .inputs = avfilter_af_channelmap_inputs,
  376. .outputs = avfilter_af_channelmap_outputs,
  377. };