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.

402 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/mathematics.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/samplefmt.h"
  30. #include "audio.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. struct ChannelMap {
  35. uint64_t in_channel;
  36. uint64_t out_channel;
  37. int in_channel_idx;
  38. int out_channel_idx;
  39. };
  40. enum MappingMode {
  41. MAP_NONE,
  42. MAP_ONE_INT,
  43. MAP_ONE_STR,
  44. MAP_PAIR_INT_INT,
  45. MAP_PAIR_INT_STR,
  46. MAP_PAIR_STR_INT,
  47. MAP_PAIR_STR_STR
  48. };
  49. #define MAX_CH 64
  50. typedef struct ChannelMapContext {
  51. const AVClass *class;
  52. AVFilterChannelLayouts *channel_layouts;
  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. static const AVOption options[] = {
  63. { "map", "A comma-separated list of input channel numbers in output order.",
  64. OFFSET(mapping_str), AV_OPT_TYPE_STRING, .flags = A },
  65. { "channel_layout", "Output channel layout.",
  66. OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  67. { NULL },
  68. };
  69. static const AVClass channelmap_class = {
  70. .class_name = "channel map filter",
  71. .item_name = av_default_item_name,
  72. .option = options,
  73. .version = LIBAVUTIL_VERSION_INT,
  74. };
  75. static char* split(char *message, char delim) {
  76. char *next = strchr(message, delim);
  77. if (next)
  78. *next++ = '\0';
  79. return next;
  80. }
  81. static int get_channel_idx(char **map, int *ch, char delim, int max_ch)
  82. {
  83. char *next = split(*map, delim);
  84. int len;
  85. int n = 0;
  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, const char *args)
  109. {
  110. ChannelMapContext *s = ctx->priv;
  111. int ret;
  112. char *mapping;
  113. enum mode;
  114. int map_entries = 0;
  115. char buf[256];
  116. enum MappingMode mode;
  117. uint64_t out_ch_mask = 0;
  118. int i;
  119. if (!args) {
  120. av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
  121. return AVERROR(EINVAL);
  122. }
  123. s->class = &channelmap_class;
  124. av_opt_set_defaults(s);
  125. if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
  126. av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
  127. return ret;
  128. }
  129. mapping = s->mapping_str;
  130. if (!mapping) {
  131. mode = MAP_NONE;
  132. } else {
  133. char *dash = strchr(mapping, '-');
  134. if (!dash) { // short mapping
  135. if (isdigit(*mapping))
  136. mode = MAP_ONE_INT;
  137. else
  138. mode = MAP_ONE_STR;
  139. } else if (isdigit(*mapping)) {
  140. if (isdigit(*(dash+1)))
  141. mode = MAP_PAIR_INT_INT;
  142. else
  143. mode = MAP_PAIR_INT_STR;
  144. } else {
  145. if (isdigit(*(dash+1)))
  146. mode = MAP_PAIR_STR_INT;
  147. else
  148. mode = MAP_PAIR_STR_STR;
  149. }
  150. }
  151. if (mode != MAP_NONE) {
  152. char *comma = mapping;
  153. map_entries = 1;
  154. while ((comma = strchr(comma, ','))) {
  155. if (*++comma) // Allow trailing comma
  156. map_entries++;
  157. }
  158. }
  159. if (map_entries > MAX_CH) {
  160. av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
  161. ret = AVERROR(EINVAL);
  162. goto fail;
  163. }
  164. for (i = 0; i < map_entries; i++) {
  165. int in_ch_idx = -1, out_ch_idx = -1;
  166. uint64_t in_ch = 0, out_ch = 0;
  167. static const char err[] = "Failed to parse channel map\n";
  168. switch (mode) {
  169. case MAP_ONE_INT:
  170. if (get_channel_idx(&mapping, &in_ch_idx, ',', MAX_CH) < 0) {
  171. ret = AVERROR(EINVAL);
  172. av_log(ctx, AV_LOG_ERROR, err);
  173. goto fail;
  174. }
  175. s->map[i].in_channel_idx = in_ch_idx;
  176. s->map[i].out_channel_idx = i;
  177. break;
  178. case MAP_ONE_STR:
  179. if (!get_channel(&mapping, &in_ch, ',')) {
  180. av_log(ctx, AV_LOG_ERROR, err);
  181. ret = AVERROR(EINVAL);
  182. goto fail;
  183. }
  184. s->map[i].in_channel = in_ch;
  185. s->map[i].out_channel_idx = i;
  186. break;
  187. case MAP_PAIR_INT_INT:
  188. if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
  189. get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) {
  190. av_log(ctx, AV_LOG_ERROR, err);
  191. ret = AVERROR(EINVAL);
  192. goto fail;
  193. }
  194. s->map[i].in_channel_idx = in_ch_idx;
  195. s->map[i].out_channel_idx = out_ch_idx;
  196. break;
  197. case MAP_PAIR_INT_STR:
  198. if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
  199. get_channel(&mapping, &out_ch, ',') < 0 ||
  200. out_ch & out_ch_mask) {
  201. av_log(ctx, AV_LOG_ERROR, err);
  202. ret = AVERROR(EINVAL);
  203. goto fail;
  204. }
  205. s->map[i].in_channel_idx = in_ch_idx;
  206. s->map[i].out_channel = out_ch;
  207. out_ch_mask |= out_ch;
  208. break;
  209. case MAP_PAIR_STR_INT:
  210. if (get_channel(&mapping, &in_ch, '-') < 0 ||
  211. get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) {
  212. av_log(ctx, AV_LOG_ERROR, err);
  213. ret = AVERROR(EINVAL);
  214. goto fail;
  215. }
  216. s->map[i].in_channel = in_ch;
  217. s->map[i].out_channel_idx = out_ch_idx;
  218. break;
  219. case MAP_PAIR_STR_STR:
  220. if (get_channel(&mapping, &in_ch, '-') < 0 ||
  221. get_channel(&mapping, &out_ch, ',') < 0 ||
  222. out_ch & out_ch_mask) {
  223. av_log(ctx, AV_LOG_ERROR, err);
  224. ret = AVERROR(EINVAL);
  225. goto fail;
  226. }
  227. s->map[i].in_channel = in_ch;
  228. s->map[i].out_channel = out_ch;
  229. out_ch_mask |= out_ch;
  230. break;
  231. }
  232. }
  233. s->mode = mode;
  234. s->nch = map_entries;
  235. s->output_layout = out_ch_mask ? out_ch_mask :
  236. av_get_default_channel_layout(map_entries);
  237. if (s->channel_layout_str) {
  238. uint64_t fmt;
  239. if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
  240. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
  241. s->channel_layout_str);
  242. ret = AVERROR(EINVAL);
  243. goto fail;
  244. }
  245. if (mode == MAP_NONE) {
  246. int i;
  247. s->nch = av_get_channel_layout_nb_channels(fmt);
  248. for (i = 0; i < s->nch; i++) {
  249. s->map[i].in_channel_idx = i;
  250. s->map[i].out_channel_idx = i;
  251. }
  252. } else if (out_ch_mask && out_ch_mask != fmt) {
  253. av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
  254. av_log(ctx, AV_LOG_ERROR,
  255. "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
  256. s->channel_layout_str, buf);
  257. ret = AVERROR(EINVAL);
  258. goto fail;
  259. } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
  260. av_log(ctx, AV_LOG_ERROR,
  261. "Output channel layout %s does not match the number of channels mapped %d.\n",
  262. s->channel_layout_str, s->nch);
  263. ret = AVERROR(EINVAL);
  264. goto fail;
  265. }
  266. s->output_layout = fmt;
  267. }
  268. ff_add_channel_layout(&s->channel_layouts, s->output_layout);
  269. if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
  270. for (i = 0; i < s->nch; i++) {
  271. s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
  272. s->output_layout, s->map[i].out_channel);
  273. }
  274. }
  275. fail:
  276. av_opt_free(s);
  277. return ret;
  278. }
  279. static int channelmap_query_formats(AVFilterContext *ctx)
  280. {
  281. ChannelMapContext *s = ctx->priv;
  282. ff_set_common_formats(ctx, ff_planar_sample_fmts());
  283. ff_set_common_samplerates(ctx, ff_all_samplerates());
  284. ff_channel_layouts_ref(ff_all_channel_layouts(), &ctx->inputs[0]->out_channel_layouts);
  285. ff_channel_layouts_ref(s->channel_layouts, &ctx->outputs[0]->in_channel_layouts);
  286. return 0;
  287. }
  288. static void channelmap_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
  289. {
  290. AVFilterContext *ctx = inlink->dst;
  291. AVFilterLink *outlink = ctx->outputs[0];
  292. const ChannelMapContext *s = ctx->priv;
  293. const int nch_in = av_get_channel_layout_nb_channels(inlink->channel_layout);
  294. const int nch_out = s->nch;
  295. int ch;
  296. uint8_t *source_planes[MAX_CH];
  297. memcpy(source_planes, buf->extended_data,
  298. nch_in * sizeof(source_planes[0]));
  299. if (nch_out > nch_in) {
  300. if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
  301. uint8_t **new_extended_data =
  302. av_mallocz(nch_out * sizeof(*buf->extended_data));
  303. if (!new_extended_data)
  304. return;
  305. if (buf->extended_data == buf->data) {
  306. buf->extended_data = new_extended_data;
  307. } else {
  308. buf->extended_data = new_extended_data;
  309. av_free(buf->extended_data);
  310. }
  311. } else if (buf->extended_data != buf->data) {
  312. av_free(buf->extended_data);
  313. buf->extended_data = buf->data;
  314. }
  315. }
  316. for (ch = 0; ch < nch_out; ch++) {
  317. buf->extended_data[s->map[ch].out_channel_idx] =
  318. source_planes[s->map[ch].in_channel_idx];
  319. }
  320. if (buf->data != buf->extended_data)
  321. memcpy(buf->data, buf->extended_data,
  322. FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
  323. ff_filter_samples(outlink, buf);
  324. }
  325. static int channelmap_config_input(AVFilterLink *inlink)
  326. {
  327. AVFilterContext *ctx = inlink->dst;
  328. ChannelMapContext *s = ctx->priv;
  329. int i, err = 0;
  330. const char *channel_name;
  331. char layout_name[256];
  332. if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
  333. for (i = 0; i < s->nch; i++) {
  334. s->map[i].in_channel_idx = av_get_channel_layout_channel_index(
  335. inlink->channel_layout, s->map[i].in_channel);
  336. if (s->map[i].in_channel_idx < 0) {
  337. channel_name = av_get_channel_name(s->map[i].in_channel);
  338. av_get_channel_layout_string(layout_name, sizeof(layout_name),
  339. 0, inlink->channel_layout);
  340. av_log(ctx, AV_LOG_ERROR,
  341. "input channel '%s' not available from input layout '%s'\n",
  342. channel_name, layout_name);
  343. err = AVERROR(EINVAL);
  344. }
  345. }
  346. }
  347. return err;
  348. }
  349. AVFilter avfilter_af_channelmap = {
  350. .name = "channelmap",
  351. .description = NULL_IF_CONFIG_SMALL("Remap audio channels."),
  352. .init = channelmap_init,
  353. .query_formats = channelmap_query_formats,
  354. .priv_size = sizeof(ChannelMapContext),
  355. .inputs = (AVFilterPad[]) {{ .name = "default",
  356. .type = AVMEDIA_TYPE_AUDIO,
  357. .filter_samples = channelmap_filter_samples,
  358. .config_props = channelmap_config_input },
  359. { .name = NULL }},
  360. .outputs = (AVFilterPad[]) {{ .name = "default",
  361. .type = AVMEDIA_TYPE_AUDIO },
  362. { .name = NULL }},
  363. };