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.

413 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 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. static const AVClass channelmap_class = {
  72. .class_name = "channel map filter",
  73. .item_name = av_default_item_name,
  74. .option = options,
  75. .version = LIBAVUTIL_VERSION_INT,
  76. };
  77. static char* split(char *message, char delim) {
  78. char *next = strchr(message, delim);
  79. if (next)
  80. *next++ = '\0';
  81. return next;
  82. }
  83. static int get_channel_idx(char **map, int *ch, char delim, int max_ch)
  84. {
  85. char *next = split(*map, delim);
  86. int len;
  87. int n = 0;
  88. if (!next && delim == '-')
  89. return AVERROR(EINVAL);
  90. len = strlen(*map);
  91. sscanf(*map, "%d%n", ch, &n);
  92. if (n != len)
  93. return AVERROR(EINVAL);
  94. if (*ch < 0 || *ch > max_ch)
  95. return AVERROR(EINVAL);
  96. *map = next;
  97. return 0;
  98. }
  99. static int get_channel(char **map, uint64_t *ch, char delim)
  100. {
  101. char *next = split(*map, delim);
  102. if (!next && delim == '-')
  103. return AVERROR(EINVAL);
  104. *ch = av_get_channel_layout(*map);
  105. if (av_get_channel_layout_nb_channels(*ch) != 1)
  106. return AVERROR(EINVAL);
  107. *map = next;
  108. return 0;
  109. }
  110. static av_cold int channelmap_init(AVFilterContext *ctx)
  111. {
  112. ChannelMapContext *s = ctx->priv;
  113. int ret = 0;
  114. char *mapping, separator = '|';
  115. int map_entries = 0;
  116. char buf[256];
  117. enum MappingMode mode;
  118. uint64_t out_ch_mask = 0;
  119. int i;
  120. mapping = s->mapping_str;
  121. if (!mapping) {
  122. mode = MAP_NONE;
  123. } else {
  124. char *dash = strchr(mapping, '-');
  125. if (!dash) { // short mapping
  126. if (av_isdigit(*mapping))
  127. mode = MAP_ONE_INT;
  128. else
  129. mode = MAP_ONE_STR;
  130. } else if (av_isdigit(*mapping)) {
  131. if (av_isdigit(*(dash+1)))
  132. mode = MAP_PAIR_INT_INT;
  133. else
  134. mode = MAP_PAIR_INT_STR;
  135. } else {
  136. if (av_isdigit(*(dash+1)))
  137. mode = MAP_PAIR_STR_INT;
  138. else
  139. mode = MAP_PAIR_STR_STR;
  140. }
  141. #if FF_API_OLD_FILTER_OPTS
  142. if (strchr(mapping, ',')) {
  143. av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use "
  144. "'|' to separate the mappings.\n");
  145. separator = ',';
  146. }
  147. #endif
  148. }
  149. if (mode != MAP_NONE) {
  150. char *sep = mapping;
  151. map_entries = 1;
  152. while ((sep = strchr(sep, separator))) {
  153. if (*++sep) // 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, separator, 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, separator)) {
  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, separator, 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, separator) < 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, separator, 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, separator) < 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_frame(AVFilterLink *inlink, AVFrame *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. av_frame_free(&buf);
  303. return AVERROR(ENOMEM);
  304. }
  305. if (buf->extended_data == buf->data) {
  306. buf->extended_data = new_extended_data;
  307. } else {
  308. av_free(buf->extended_data);
  309. buf->extended_data = new_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_frame(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. static const AVFilterPad avfilter_af_channelmap_inputs[] = {
  350. {
  351. .name = "default",
  352. .type = AVMEDIA_TYPE_AUDIO,
  353. .filter_frame = channelmap_filter_frame,
  354. .config_props = channelmap_config_input,
  355. .needs_writable = 1,
  356. },
  357. { NULL }
  358. };
  359. static const AVFilterPad avfilter_af_channelmap_outputs[] = {
  360. {
  361. .name = "default",
  362. .type = AVMEDIA_TYPE_AUDIO
  363. },
  364. { NULL }
  365. };
  366. AVFilter avfilter_af_channelmap = {
  367. .name = "channelmap",
  368. .description = NULL_IF_CONFIG_SMALL("Remap audio channels."),
  369. .init = channelmap_init,
  370. .query_formats = channelmap_query_formats,
  371. .priv_size = sizeof(ChannelMapContext),
  372. .priv_class = &channelmap_class,
  373. .inputs = avfilter_af_channelmap_inputs,
  374. .outputs = avfilter_af_channelmap_outputs,
  375. };