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