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.

414 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. char *mapping, separator = '|';
  113. int map_entries = 0;
  114. char buf[256];
  115. enum MappingMode mode;
  116. uint64_t out_ch_mask = 0;
  117. int i;
  118. mapping = s->mapping_str;
  119. if (!mapping) {
  120. mode = MAP_NONE;
  121. } else {
  122. char *dash = strchr(mapping, '-');
  123. if (!dash) { // short mapping
  124. if (av_isdigit(*mapping))
  125. mode = MAP_ONE_INT;
  126. else
  127. mode = MAP_ONE_STR;
  128. } else if (av_isdigit(*mapping)) {
  129. if (av_isdigit(*(dash+1)))
  130. mode = MAP_PAIR_INT_INT;
  131. else
  132. mode = MAP_PAIR_INT_STR;
  133. } else {
  134. if (av_isdigit(*(dash+1)))
  135. mode = MAP_PAIR_STR_INT;
  136. else
  137. mode = MAP_PAIR_STR_STR;
  138. }
  139. #if FF_API_OLD_FILTER_OPTS
  140. if (strchr(mapping, ',')) {
  141. av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use "
  142. "'|' to separate the mappings.\n");
  143. separator = ',';
  144. }
  145. #endif
  146. }
  147. if (mode != MAP_NONE) {
  148. char *sep = mapping;
  149. map_entries = 1;
  150. while ((sep = strchr(sep, separator))) {
  151. if (*++sep) // Allow trailing comma
  152. map_entries++;
  153. }
  154. }
  155. if (map_entries > MAX_CH) {
  156. av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
  157. return AVERROR(EINVAL);
  158. }
  159. for (i = 0; i < map_entries; i++) {
  160. int in_ch_idx = -1, out_ch_idx = -1;
  161. uint64_t in_ch = 0, out_ch = 0;
  162. static const char err[] = "Failed to parse channel map\n";
  163. switch (mode) {
  164. case MAP_ONE_INT:
  165. if (get_channel_idx(&mapping, &in_ch_idx, separator, MAX_CH) < 0) {
  166. av_log(ctx, AV_LOG_ERROR, err);
  167. return AVERROR(EINVAL);
  168. }
  169. s->map[i].in_channel_idx = in_ch_idx;
  170. s->map[i].out_channel_idx = i;
  171. break;
  172. case MAP_ONE_STR:
  173. if (!get_channel(&mapping, &in_ch, separator)) {
  174. av_log(ctx, AV_LOG_ERROR, err);
  175. return AVERROR(EINVAL);
  176. }
  177. s->map[i].in_channel = in_ch;
  178. s->map[i].out_channel_idx = i;
  179. break;
  180. case MAP_PAIR_INT_INT:
  181. if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
  182. get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
  183. av_log(ctx, AV_LOG_ERROR, err);
  184. return AVERROR(EINVAL);
  185. }
  186. s->map[i].in_channel_idx = in_ch_idx;
  187. s->map[i].out_channel_idx = out_ch_idx;
  188. break;
  189. case MAP_PAIR_INT_STR:
  190. if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
  191. get_channel(&mapping, &out_ch, separator) < 0 ||
  192. out_ch & out_ch_mask) {
  193. av_log(ctx, AV_LOG_ERROR, err);
  194. return AVERROR(EINVAL);
  195. }
  196. s->map[i].in_channel_idx = in_ch_idx;
  197. s->map[i].out_channel = out_ch;
  198. out_ch_mask |= out_ch;
  199. break;
  200. case MAP_PAIR_STR_INT:
  201. if (get_channel(&mapping, &in_ch, '-') < 0 ||
  202. get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
  203. av_log(ctx, AV_LOG_ERROR, err);
  204. return AVERROR(EINVAL);
  205. }
  206. s->map[i].in_channel = in_ch;
  207. s->map[i].out_channel_idx = out_ch_idx;
  208. break;
  209. case MAP_PAIR_STR_STR:
  210. if (get_channel(&mapping, &in_ch, '-') < 0 ||
  211. get_channel(&mapping, &out_ch, separator) < 0 ||
  212. out_ch & out_ch_mask) {
  213. av_log(ctx, AV_LOG_ERROR, err);
  214. return AVERROR(EINVAL);
  215. }
  216. s->map[i].in_channel = in_ch;
  217. s->map[i].out_channel = out_ch;
  218. out_ch_mask |= out_ch;
  219. break;
  220. }
  221. }
  222. s->mode = mode;
  223. s->nch = map_entries;
  224. s->output_layout = out_ch_mask ? out_ch_mask :
  225. av_get_default_channel_layout(map_entries);
  226. if (s->channel_layout_str) {
  227. uint64_t fmt;
  228. if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
  229. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
  230. s->channel_layout_str);
  231. return AVERROR(EINVAL);
  232. }
  233. if (mode == MAP_NONE) {
  234. int i;
  235. s->nch = av_get_channel_layout_nb_channels(fmt);
  236. for (i = 0; i < s->nch; i++) {
  237. s->map[i].in_channel_idx = i;
  238. s->map[i].out_channel_idx = i;
  239. }
  240. } else if (out_ch_mask && out_ch_mask != fmt) {
  241. av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
  242. av_log(ctx, AV_LOG_ERROR,
  243. "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
  244. s->channel_layout_str, buf);
  245. return AVERROR(EINVAL);
  246. } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
  247. av_log(ctx, AV_LOG_ERROR,
  248. "Output channel layout %s does not match the number of channels mapped %d.\n",
  249. s->channel_layout_str, s->nch);
  250. return AVERROR(EINVAL);
  251. }
  252. s->output_layout = fmt;
  253. }
  254. if (!s->output_layout) {
  255. av_log(ctx, AV_LOG_ERROR, "Output channel layout is not set and "
  256. "cannot be guessed from the maps.\n");
  257. return AVERROR(EINVAL);
  258. }
  259. ff_add_channel_layout(&s->channel_layouts, s->output_layout);
  260. if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
  261. for (i = 0; i < s->nch; i++) {
  262. s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
  263. s->output_layout, s->map[i].out_channel);
  264. }
  265. }
  266. return 0;
  267. }
  268. static int channelmap_query_formats(AVFilterContext *ctx)
  269. {
  270. ChannelMapContext *s = ctx->priv;
  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(s->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. return ff_filter_frame(outlink, buf);
  315. }
  316. static int channelmap_config_input(AVFilterLink *inlink)
  317. {
  318. AVFilterContext *ctx = inlink->dst;
  319. ChannelMapContext *s = ctx->priv;
  320. int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
  321. int i, err = 0;
  322. const char *channel_name;
  323. char layout_name[256];
  324. for (i = 0; i < s->nch; i++) {
  325. struct ChannelMap *m = &s->map[i];
  326. if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
  327. m->in_channel_idx = av_get_channel_layout_channel_index(
  328. inlink->channel_layout, m->in_channel);
  329. }
  330. if (m->in_channel_idx < 0 || m->in_channel_idx >= nb_channels) {
  331. av_get_channel_layout_string(layout_name, sizeof(layout_name),
  332. 0, inlink->channel_layout);
  333. if (m->in_channel) {
  334. channel_name = av_get_channel_name(m->in_channel);
  335. av_log(ctx, AV_LOG_ERROR,
  336. "input channel '%s' not available from input layout '%s'\n",
  337. channel_name, layout_name);
  338. } else {
  339. av_log(ctx, AV_LOG_ERROR,
  340. "input channel #%d not available from input layout '%s'\n",
  341. m->in_channel_idx, layout_name);
  342. }
  343. err = AVERROR(EINVAL);
  344. }
  345. }
  346. return err;
  347. }
  348. static const AVFilterPad avfilter_af_channelmap_inputs[] = {
  349. {
  350. .name = "default",
  351. .type = AVMEDIA_TYPE_AUDIO,
  352. .filter_frame = channelmap_filter_frame,
  353. .config_props = channelmap_config_input
  354. },
  355. { NULL }
  356. };
  357. static const AVFilterPad avfilter_af_channelmap_outputs[] = {
  358. {
  359. .name = "default",
  360. .type = AVMEDIA_TYPE_AUDIO
  361. },
  362. { NULL }
  363. };
  364. AVFilter ff_af_channelmap = {
  365. .name = "channelmap",
  366. .description = NULL_IF_CONFIG_SMALL("Remap audio channels."),
  367. .init = channelmap_init,
  368. .query_formats = channelmap_query_formats,
  369. .priv_size = sizeof(ChannelMapContext),
  370. .priv_class = &channelmap_class,
  371. .inputs = avfilter_af_channelmap_inputs,
  372. .outputs = avfilter_af_channelmap_outputs,
  373. };