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.

417 lines
13KB

  1. /*
  2. * Copyright (c) 2012 Google, Inc.
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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/audioconvert.h"
  26. #include "libavutil/avstring.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. static const AVOption options[] = {
  64. { "map", "A comma-separated list of input channel numbers in output order.",
  65. OFFSET(mapping_str), AV_OPT_TYPE_STRING, .flags = A },
  66. { "channel_layout", "Output channel layout.",
  67. OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  68. { NULL },
  69. };
  70. static const AVClass channelmap_class = {
  71. .class_name = "channel map filter",
  72. .item_name = av_default_item_name,
  73. .option = options,
  74. .version = LIBAVUTIL_VERSION_INT,
  75. };
  76. static char* split(char *message, char delim) {
  77. char *next = strchr(message, delim);
  78. if (next)
  79. *next++ = '\0';
  80. return next;
  81. }
  82. static int get_channel_idx(char **map, int *ch, char delim, int max_ch)
  83. {
  84. char *next = split(*map, delim);
  85. int len;
  86. int n = 0;
  87. if (!next && delim == '-')
  88. return AVERROR(EINVAL);
  89. len = strlen(*map);
  90. sscanf(*map, "%d%n", ch, &n);
  91. if (n != len)
  92. return AVERROR(EINVAL);
  93. if (*ch < 0 || *ch > max_ch)
  94. return AVERROR(EINVAL);
  95. *map = next;
  96. return 0;
  97. }
  98. static int get_channel(char **map, uint64_t *ch, char delim)
  99. {
  100. char *next = split(*map, delim);
  101. if (!next && delim == '-')
  102. return AVERROR(EINVAL);
  103. *ch = av_get_channel_layout(*map);
  104. if (av_get_channel_layout_nb_channels(*ch) != 1)
  105. return AVERROR(EINVAL);
  106. *map = next;
  107. return 0;
  108. }
  109. static av_cold int channelmap_init(AVFilterContext *ctx, const char *args)
  110. {
  111. ChannelMapContext *s = ctx->priv;
  112. int ret;
  113. char *mapping;
  114. enum mode;
  115. int map_entries = 0;
  116. char buf[256];
  117. enum MappingMode mode;
  118. uint64_t out_ch_mask = 0;
  119. int i;
  120. if (!args) {
  121. av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
  122. return AVERROR(EINVAL);
  123. }
  124. s->class = &channelmap_class;
  125. av_opt_set_defaults(s);
  126. if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
  127. av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
  128. return ret;
  129. }
  130. mapping = s->mapping_str;
  131. if (!mapping) {
  132. mode = MAP_NONE;
  133. } else {
  134. char *dash = strchr(mapping, '-');
  135. if (!dash) { // short mapping
  136. if (isdigit(*mapping))
  137. mode = MAP_ONE_INT;
  138. else
  139. mode = MAP_ONE_STR;
  140. } else if (isdigit(*mapping)) {
  141. if (isdigit(*(dash+1)))
  142. mode = MAP_PAIR_INT_INT;
  143. else
  144. mode = MAP_PAIR_INT_STR;
  145. } else {
  146. if (isdigit(*(dash+1)))
  147. mode = MAP_PAIR_STR_INT;
  148. else
  149. mode = MAP_PAIR_STR_STR;
  150. }
  151. }
  152. if (mode != MAP_NONE) {
  153. char *comma = mapping;
  154. map_entries = 1;
  155. while ((comma = strchr(comma, ','))) {
  156. if (*++comma) // Allow trailing comma
  157. map_entries++;
  158. }
  159. }
  160. if (map_entries > MAX_CH) {
  161. av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
  162. ret = AVERROR(EINVAL);
  163. goto fail;
  164. }
  165. for (i = 0; i < map_entries; i++) {
  166. int in_ch_idx = -1, out_ch_idx = -1;
  167. uint64_t in_ch = 0, out_ch = 0;
  168. static const char err[] = "Failed to parse channel map\n";
  169. switch (mode) {
  170. case MAP_ONE_INT:
  171. if (get_channel_idx(&mapping, &in_ch_idx, ',', MAX_CH) < 0) {
  172. ret = AVERROR(EINVAL);
  173. av_log(ctx, AV_LOG_ERROR, err);
  174. goto fail;
  175. }
  176. s->map[i].in_channel_idx = in_ch_idx;
  177. s->map[i].out_channel_idx = i;
  178. break;
  179. case MAP_ONE_STR:
  180. if (!get_channel(&mapping, &in_ch, ',')) {
  181. av_log(ctx, AV_LOG_ERROR, err);
  182. ret = AVERROR(EINVAL);
  183. goto fail;
  184. }
  185. s->map[i].in_channel = in_ch;
  186. s->map[i].out_channel_idx = i;
  187. break;
  188. case MAP_PAIR_INT_INT:
  189. if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
  190. get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) {
  191. av_log(ctx, AV_LOG_ERROR, err);
  192. ret = AVERROR(EINVAL);
  193. goto fail;
  194. }
  195. s->map[i].in_channel_idx = in_ch_idx;
  196. s->map[i].out_channel_idx = out_ch_idx;
  197. break;
  198. case MAP_PAIR_INT_STR:
  199. if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
  200. get_channel(&mapping, &out_ch, ',') < 0 ||
  201. out_ch & out_ch_mask) {
  202. av_log(ctx, AV_LOG_ERROR, err);
  203. ret = AVERROR(EINVAL);
  204. goto fail;
  205. }
  206. s->map[i].in_channel_idx = in_ch_idx;
  207. s->map[i].out_channel = out_ch;
  208. out_ch_mask |= out_ch;
  209. break;
  210. case MAP_PAIR_STR_INT:
  211. if (get_channel(&mapping, &in_ch, '-') < 0 ||
  212. get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) {
  213. av_log(ctx, AV_LOG_ERROR, err);
  214. ret = AVERROR(EINVAL);
  215. goto fail;
  216. }
  217. s->map[i].in_channel = in_ch;
  218. s->map[i].out_channel_idx = out_ch_idx;
  219. break;
  220. case MAP_PAIR_STR_STR:
  221. if (get_channel(&mapping, &in_ch, '-') < 0 ||
  222. get_channel(&mapping, &out_ch, ',') < 0 ||
  223. out_ch & out_ch_mask) {
  224. av_log(ctx, AV_LOG_ERROR, err);
  225. ret = AVERROR(EINVAL);
  226. goto fail;
  227. }
  228. s->map[i].in_channel = in_ch;
  229. s->map[i].out_channel = out_ch;
  230. out_ch_mask |= out_ch;
  231. break;
  232. }
  233. }
  234. s->mode = mode;
  235. s->nch = map_entries;
  236. s->output_layout = out_ch_mask ? out_ch_mask :
  237. av_get_default_channel_layout(map_entries);
  238. if (s->channel_layout_str) {
  239. uint64_t fmt;
  240. if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
  241. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
  242. s->channel_layout_str);
  243. ret = AVERROR(EINVAL);
  244. goto fail;
  245. }
  246. if (mode == MAP_NONE) {
  247. int i;
  248. s->nch = av_get_channel_layout_nb_channels(fmt);
  249. for (i = 0; i < s->nch; i++) {
  250. s->map[i].in_channel_idx = i;
  251. s->map[i].out_channel_idx = i;
  252. }
  253. } else if (out_ch_mask && out_ch_mask != fmt) {
  254. av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
  255. av_log(ctx, AV_LOG_ERROR,
  256. "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
  257. s->channel_layout_str, buf);
  258. ret = AVERROR(EINVAL);
  259. goto fail;
  260. } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
  261. av_log(ctx, AV_LOG_ERROR,
  262. "Output channel layout %s does not match the number of channels mapped %d.\n",
  263. s->channel_layout_str, s->nch);
  264. ret = AVERROR(EINVAL);
  265. goto fail;
  266. }
  267. s->output_layout = fmt;
  268. }
  269. ff_add_channel_layout(&s->channel_layouts, s->output_layout);
  270. if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
  271. for (i = 0; i < s->nch; i++) {
  272. s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
  273. s->output_layout, s->map[i].out_channel);
  274. }
  275. }
  276. fail:
  277. av_opt_free(s);
  278. return ret;
  279. }
  280. static int channelmap_query_formats(AVFilterContext *ctx)
  281. {
  282. ChannelMapContext *s = ctx->priv;
  283. ff_set_common_formats(ctx, ff_planar_sample_fmts());
  284. ff_set_common_samplerates(ctx, ff_all_samplerates());
  285. ff_channel_layouts_ref(ff_all_channel_layouts(), &ctx->inputs[0]->out_channel_layouts);
  286. ff_channel_layouts_ref(s->channel_layouts, &ctx->outputs[0]->in_channel_layouts);
  287. return 0;
  288. }
  289. static int channelmap_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *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(nch_out * sizeof(*buf->extended_data));
  304. if (!new_extended_data) {
  305. avfilter_unref_buffer(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. return ff_filter_samples(outlink, buf);
  327. }
  328. static int channelmap_config_input(AVFilterLink *inlink)
  329. {
  330. AVFilterContext *ctx = inlink->dst;
  331. ChannelMapContext *s = ctx->priv;
  332. int i, err = 0;
  333. const char *channel_name;
  334. char layout_name[256];
  335. if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
  336. for (i = 0; i < s->nch; i++) {
  337. s->map[i].in_channel_idx = av_get_channel_layout_channel_index(
  338. inlink->channel_layout, s->map[i].in_channel);
  339. if (s->map[i].in_channel_idx < 0) {
  340. channel_name = av_get_channel_name(s->map[i].in_channel);
  341. av_get_channel_layout_string(layout_name, sizeof(layout_name),
  342. 0, inlink->channel_layout);
  343. av_log(ctx, AV_LOG_ERROR,
  344. "input channel '%s' not available from input layout '%s'\n",
  345. channel_name, layout_name);
  346. err = AVERROR(EINVAL);
  347. }
  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_samples = channelmap_filter_samples,
  357. .config_props = channelmap_config_input
  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 avfilter_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. .inputs = avfilter_af_channelmap_inputs,
  375. .outputs = avfilter_af_channelmap_outputs,
  376. };