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.

416 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. 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. return AVERROR(EINVAL);
  159. }
  160. for (i = 0; i < map_entries; i++) {
  161. int in_ch_idx = -1, out_ch_idx = -1;
  162. uint64_t in_ch = 0, out_ch = 0;
  163. static const char err[] = "Failed to parse channel map\n";
  164. switch (mode) {
  165. case MAP_ONE_INT:
  166. if (get_channel_idx(&mapping, &in_ch_idx, separator, MAX_CH) < 0) {
  167. av_log(ctx, AV_LOG_ERROR, err);
  168. return AVERROR(EINVAL);
  169. }
  170. s->map[i].in_channel_idx = in_ch_idx;
  171. s->map[i].out_channel_idx = i;
  172. break;
  173. case MAP_ONE_STR:
  174. if (!get_channel(&mapping, &in_ch, separator)) {
  175. av_log(ctx, AV_LOG_ERROR, err);
  176. return AVERROR(EINVAL);
  177. }
  178. s->map[i].in_channel = in_ch;
  179. s->map[i].out_channel_idx = i;
  180. break;
  181. case MAP_PAIR_INT_INT:
  182. if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
  183. get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
  184. av_log(ctx, AV_LOG_ERROR, err);
  185. return AVERROR(EINVAL);
  186. }
  187. s->map[i].in_channel_idx = in_ch_idx;
  188. s->map[i].out_channel_idx = out_ch_idx;
  189. break;
  190. case MAP_PAIR_INT_STR:
  191. if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
  192. get_channel(&mapping, &out_ch, separator) < 0 ||
  193. out_ch & out_ch_mask) {
  194. av_log(ctx, AV_LOG_ERROR, err);
  195. return AVERROR(EINVAL);
  196. }
  197. s->map[i].in_channel_idx = in_ch_idx;
  198. s->map[i].out_channel = out_ch;
  199. out_ch_mask |= out_ch;
  200. break;
  201. case MAP_PAIR_STR_INT:
  202. if (get_channel(&mapping, &in_ch, '-') < 0 ||
  203. get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
  204. av_log(ctx, AV_LOG_ERROR, err);
  205. return AVERROR(EINVAL);
  206. }
  207. s->map[i].in_channel = in_ch;
  208. s->map[i].out_channel_idx = out_ch_idx;
  209. break;
  210. case MAP_PAIR_STR_STR:
  211. if (get_channel(&mapping, &in_ch, '-') < 0 ||
  212. get_channel(&mapping, &out_ch, separator) < 0 ||
  213. out_ch & out_ch_mask) {
  214. av_log(ctx, AV_LOG_ERROR, err);
  215. return AVERROR(EINVAL);
  216. }
  217. s->map[i].in_channel = in_ch;
  218. s->map[i].out_channel = out_ch;
  219. out_ch_mask |= out_ch;
  220. break;
  221. }
  222. }
  223. s->mode = mode;
  224. s->nch = map_entries;
  225. s->output_layout = out_ch_mask ? out_ch_mask :
  226. av_get_default_channel_layout(map_entries);
  227. if (s->channel_layout_str) {
  228. uint64_t fmt;
  229. if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
  230. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
  231. s->channel_layout_str);
  232. return AVERROR(EINVAL);
  233. }
  234. if (mode == MAP_NONE) {
  235. int i;
  236. s->nch = av_get_channel_layout_nb_channels(fmt);
  237. for (i = 0; i < s->nch; i++) {
  238. s->map[i].in_channel_idx = i;
  239. s->map[i].out_channel_idx = i;
  240. }
  241. } else if (out_ch_mask && out_ch_mask != fmt) {
  242. av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
  243. av_log(ctx, AV_LOG_ERROR,
  244. "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
  245. s->channel_layout_str, buf);
  246. return AVERROR(EINVAL);
  247. } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
  248. av_log(ctx, AV_LOG_ERROR,
  249. "Output channel layout %s does not match the number of channels mapped %d.\n",
  250. s->channel_layout_str, s->nch);
  251. return AVERROR(EINVAL);
  252. }
  253. s->output_layout = fmt;
  254. }
  255. if (!s->output_layout) {
  256. av_log(ctx, AV_LOG_ERROR, "Output channel layout is not set and "
  257. "cannot be guessed from the maps.\n");
  258. return AVERROR(EINVAL);
  259. }
  260. ff_add_channel_layout(&s->channel_layouts, s->output_layout);
  261. if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
  262. for (i = 0; i < s->nch; i++) {
  263. s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
  264. s->output_layout, s->map[i].out_channel);
  265. }
  266. }
  267. return 0;
  268. }
  269. static int channelmap_query_formats(AVFilterContext *ctx)
  270. {
  271. ChannelMapContext *s = ctx->priv;
  272. ff_set_common_formats(ctx, ff_planar_sample_fmts());
  273. ff_set_common_samplerates(ctx, ff_all_samplerates());
  274. ff_channel_layouts_ref(ff_all_channel_layouts(), &ctx->inputs[0]->out_channel_layouts);
  275. ff_channel_layouts_ref(s->channel_layouts, &ctx->outputs[0]->in_channel_layouts);
  276. return 0;
  277. }
  278. static int channelmap_filter_frame(AVFilterLink *inlink, AVFrame *buf)
  279. {
  280. AVFilterContext *ctx = inlink->dst;
  281. AVFilterLink *outlink = ctx->outputs[0];
  282. const ChannelMapContext *s = ctx->priv;
  283. const int nch_in = av_get_channel_layout_nb_channels(inlink->channel_layout);
  284. const int nch_out = s->nch;
  285. int ch;
  286. uint8_t *source_planes[MAX_CH];
  287. memcpy(source_planes, buf->extended_data,
  288. nch_in * sizeof(source_planes[0]));
  289. if (nch_out > nch_in) {
  290. if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
  291. uint8_t **new_extended_data =
  292. av_mallocz(nch_out * sizeof(*buf->extended_data));
  293. if (!new_extended_data) {
  294. av_frame_free(&buf);
  295. return AVERROR(ENOMEM);
  296. }
  297. if (buf->extended_data == buf->data) {
  298. buf->extended_data = new_extended_data;
  299. } else {
  300. av_free(buf->extended_data);
  301. buf->extended_data = new_extended_data;
  302. }
  303. } else if (buf->extended_data != buf->data) {
  304. av_free(buf->extended_data);
  305. buf->extended_data = buf->data;
  306. }
  307. }
  308. for (ch = 0; ch < nch_out; ch++) {
  309. buf->extended_data[s->map[ch].out_channel_idx] =
  310. source_planes[s->map[ch].in_channel_idx];
  311. }
  312. if (buf->data != buf->extended_data)
  313. memcpy(buf->data, buf->extended_data,
  314. FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
  315. return ff_filter_frame(outlink, buf);
  316. }
  317. static int channelmap_config_input(AVFilterLink *inlink)
  318. {
  319. AVFilterContext *ctx = inlink->dst;
  320. ChannelMapContext *s = ctx->priv;
  321. int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
  322. int i, err = 0;
  323. const char *channel_name;
  324. char layout_name[256];
  325. for (i = 0; i < s->nch; i++) {
  326. struct ChannelMap *m = &s->map[i];
  327. if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
  328. m->in_channel_idx = av_get_channel_layout_channel_index(
  329. inlink->channel_layout, m->in_channel);
  330. }
  331. if (m->in_channel_idx < 0 || m->in_channel_idx >= nb_channels) {
  332. av_get_channel_layout_string(layout_name, sizeof(layout_name),
  333. 0, inlink->channel_layout);
  334. if (m->in_channel) {
  335. channel_name = av_get_channel_name(m->in_channel);
  336. av_log(ctx, AV_LOG_ERROR,
  337. "input channel '%s' not available from input layout '%s'\n",
  338. channel_name, layout_name);
  339. } else {
  340. av_log(ctx, AV_LOG_ERROR,
  341. "input channel #%d not available from input layout '%s'\n",
  342. m->in_channel_idx, layout_name);
  343. }
  344. err = AVERROR(EINVAL);
  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. };