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 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/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. return ret;
  127. mapping = s->mapping_str;
  128. if (!mapping) {
  129. mode = MAP_NONE;
  130. } else {
  131. char *dash = strchr(mapping, '-');
  132. if (!dash) { // short mapping
  133. if (isdigit(*mapping))
  134. mode = MAP_ONE_INT;
  135. else
  136. mode = MAP_ONE_STR;
  137. } else if (isdigit(*mapping)) {
  138. if (isdigit(*(dash+1)))
  139. mode = MAP_PAIR_INT_INT;
  140. else
  141. mode = MAP_PAIR_INT_STR;
  142. } else {
  143. if (isdigit(*(dash+1)))
  144. mode = MAP_PAIR_STR_INT;
  145. else
  146. mode = MAP_PAIR_STR_STR;
  147. }
  148. }
  149. if (mode != MAP_NONE) {
  150. char *comma = mapping;
  151. map_entries = 1;
  152. while ((comma = strchr(comma, ','))) {
  153. if (*++comma) // Allow trailing comma
  154. map_entries++;
  155. }
  156. }
  157. if (map_entries > MAX_CH) {
  158. av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
  159. ret = AVERROR(EINVAL);
  160. goto fail;
  161. }
  162. for (i = 0; i < map_entries; i++) {
  163. int in_ch_idx = -1, out_ch_idx = -1;
  164. uint64_t in_ch = 0, out_ch = 0;
  165. static const char err[] = "Failed to parse channel map\n";
  166. switch (mode) {
  167. case MAP_ONE_INT:
  168. if (get_channel_idx(&mapping, &in_ch_idx, ',', MAX_CH) < 0) {
  169. ret = AVERROR(EINVAL);
  170. av_log(ctx, AV_LOG_ERROR, err);
  171. goto fail;
  172. }
  173. s->map[i].in_channel_idx = in_ch_idx;
  174. s->map[i].out_channel_idx = i;
  175. break;
  176. case MAP_ONE_STR:
  177. if (!get_channel(&mapping, &in_ch, ',')) {
  178. av_log(ctx, AV_LOG_ERROR, err);
  179. ret = AVERROR(EINVAL);
  180. goto fail;
  181. }
  182. s->map[i].in_channel = in_ch;
  183. s->map[i].out_channel_idx = i;
  184. break;
  185. case MAP_PAIR_INT_INT:
  186. if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
  187. get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) {
  188. av_log(ctx, AV_LOG_ERROR, err);
  189. ret = AVERROR(EINVAL);
  190. goto fail;
  191. }
  192. s->map[i].in_channel_idx = in_ch_idx;
  193. s->map[i].out_channel_idx = out_ch_idx;
  194. break;
  195. case MAP_PAIR_INT_STR:
  196. if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
  197. get_channel(&mapping, &out_ch, ',') < 0 ||
  198. out_ch & out_ch_mask) {
  199. av_log(ctx, AV_LOG_ERROR, err);
  200. ret = AVERROR(EINVAL);
  201. goto fail;
  202. }
  203. s->map[i].in_channel_idx = in_ch_idx;
  204. s->map[i].out_channel = out_ch;
  205. out_ch_mask |= out_ch;
  206. break;
  207. case MAP_PAIR_STR_INT:
  208. if (get_channel(&mapping, &in_ch, '-') < 0 ||
  209. get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) {
  210. av_log(ctx, AV_LOG_ERROR, err);
  211. ret = AVERROR(EINVAL);
  212. goto fail;
  213. }
  214. s->map[i].in_channel = in_ch;
  215. s->map[i].out_channel_idx = out_ch_idx;
  216. break;
  217. case MAP_PAIR_STR_STR:
  218. if (get_channel(&mapping, &in_ch, '-') < 0 ||
  219. get_channel(&mapping, &out_ch, ',') < 0 ||
  220. out_ch & out_ch_mask) {
  221. av_log(ctx, AV_LOG_ERROR, err);
  222. ret = AVERROR(EINVAL);
  223. goto fail;
  224. }
  225. s->map[i].in_channel = in_ch;
  226. s->map[i].out_channel = out_ch;
  227. out_ch_mask |= out_ch;
  228. break;
  229. }
  230. }
  231. s->mode = mode;
  232. s->nch = map_entries;
  233. s->output_layout = out_ch_mask ? out_ch_mask :
  234. av_get_default_channel_layout(map_entries);
  235. if (s->channel_layout_str) {
  236. uint64_t fmt;
  237. if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
  238. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
  239. s->channel_layout_str);
  240. ret = AVERROR(EINVAL);
  241. goto fail;
  242. }
  243. if (mode == MAP_NONE) {
  244. int i;
  245. s->nch = av_get_channel_layout_nb_channels(fmt);
  246. for (i = 0; i < s->nch; i++) {
  247. s->map[i].in_channel_idx = i;
  248. s->map[i].out_channel_idx = i;
  249. }
  250. } else if (out_ch_mask && out_ch_mask != fmt) {
  251. av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
  252. av_log(ctx, AV_LOG_ERROR,
  253. "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
  254. s->channel_layout_str, buf);
  255. ret = AVERROR(EINVAL);
  256. goto fail;
  257. } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
  258. av_log(ctx, AV_LOG_ERROR,
  259. "Output channel layout %s does not match the number of channels mapped %d.\n",
  260. s->channel_layout_str, s->nch);
  261. ret = AVERROR(EINVAL);
  262. goto fail;
  263. }
  264. s->output_layout = fmt;
  265. }
  266. ff_add_channel_layout(&s->channel_layouts, s->output_layout);
  267. if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
  268. for (i = 0; i < s->nch; i++) {
  269. s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
  270. s->output_layout, s->map[i].out_channel);
  271. }
  272. }
  273. fail:
  274. av_opt_free(s);
  275. return ret;
  276. }
  277. static int channelmap_query_formats(AVFilterContext *ctx)
  278. {
  279. ChannelMapContext *s = ctx->priv;
  280. ff_set_common_formats(ctx, ff_planar_sample_fmts());
  281. ff_set_common_samplerates(ctx, ff_all_samplerates());
  282. ff_channel_layouts_ref(ff_all_channel_layouts(), &ctx->inputs[0]->out_channel_layouts);
  283. ff_channel_layouts_ref(s->channel_layouts, &ctx->outputs[0]->in_channel_layouts);
  284. return 0;
  285. }
  286. static int channelmap_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
  287. {
  288. AVFilterContext *ctx = inlink->dst;
  289. AVFilterLink *outlink = ctx->outputs[0];
  290. const ChannelMapContext *s = ctx->priv;
  291. const int nch_in = av_get_channel_layout_nb_channels(inlink->channel_layout);
  292. const int nch_out = s->nch;
  293. int ch;
  294. uint8_t *source_planes[MAX_CH];
  295. memcpy(source_planes, buf->extended_data,
  296. nch_in * sizeof(source_planes[0]));
  297. if (nch_out > nch_in) {
  298. if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
  299. uint8_t **new_extended_data =
  300. av_mallocz(nch_out * sizeof(*buf->extended_data));
  301. if (!new_extended_data) {
  302. avfilter_unref_buffer(buf);
  303. return AVERROR(ENOMEM);
  304. }
  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. return 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 = (const 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 = (const AVFilterPad[]) {{ .name = "default",
  361. .type = AVMEDIA_TYPE_AUDIO },
  362. { .name = NULL }},
  363. };