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.

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