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