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.

502 lines
15KB

  1. /*
  2. *
  3. * This file is part of Libav.
  4. *
  5. * Libav is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * Libav is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with Libav; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file
  21. * Audio join filter
  22. *
  23. * Join multiple audio inputs as different channels in
  24. * a single output
  25. */
  26. #include "libavutil/audioconvert.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/opt.h"
  29. #include "audio.h"
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. typedef struct ChannelMap {
  34. int input; ///< input stream index
  35. int in_channel_idx; ///< index of in_channel in the input stream data
  36. uint64_t in_channel; ///< layout describing the input channel
  37. uint64_t out_channel; ///< layout describing the output channel
  38. } ChannelMap;
  39. typedef struct JoinContext {
  40. const AVClass *class;
  41. int inputs;
  42. char *map;
  43. char *channel_layout_str;
  44. uint64_t channel_layout;
  45. int nb_channels;
  46. ChannelMap *channels;
  47. /**
  48. * Temporary storage for input frames, until we get one on each input.
  49. */
  50. AVFilterBufferRef **input_frames;
  51. /**
  52. * Temporary storage for data pointers, for assembling the output buffer.
  53. */
  54. uint8_t **data;
  55. } JoinContext;
  56. /**
  57. * To avoid copying the data from input buffers, this filter creates
  58. * a custom output buffer that stores references to all inputs and
  59. * unrefs them on free.
  60. */
  61. typedef struct JoinBufferPriv {
  62. AVFilterBufferRef **in_buffers;
  63. int nb_in_buffers;
  64. } JoinBufferPriv;
  65. #define OFFSET(x) offsetof(JoinContext, x)
  66. #define A AV_OPT_FLAG_AUDIO_PARAM
  67. static const AVOption join_options[] = {
  68. { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { 2 }, 1, INT_MAX, A },
  69. { "channel_layout", "Channel layout of the "
  70. "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A },
  71. { "map", "A comma-separated list of channels maps in the format "
  72. "'input_stream.input_channel-output_channel.",
  73. OFFSET(map), AV_OPT_TYPE_STRING, .flags = A },
  74. { NULL },
  75. };
  76. static const AVClass join_class = {
  77. .class_name = "join filter",
  78. .item_name = av_default_item_name,
  79. .option = join_options,
  80. .version = LIBAVUTIL_VERSION_INT,
  81. };
  82. static void filter_samples(AVFilterLink *link, AVFilterBufferRef *buf)
  83. {
  84. AVFilterContext *ctx = link->dst;
  85. JoinContext *s = ctx->priv;
  86. int i;
  87. for (i = 0; i < ctx->nb_inputs; i++)
  88. if (link == ctx->inputs[i])
  89. break;
  90. av_assert0(i < ctx->nb_inputs);
  91. av_assert0(!s->input_frames[i]);
  92. s->input_frames[i] = buf;
  93. }
  94. static int parse_maps(AVFilterContext *ctx)
  95. {
  96. JoinContext *s = ctx->priv;
  97. char *cur = s->map;
  98. while (cur && *cur) {
  99. char *sep, *next, *p;
  100. uint64_t in_channel = 0, out_channel = 0;
  101. int input_idx, out_ch_idx, in_ch_idx;
  102. next = strchr(cur, ',');
  103. if (next)
  104. *next++ = 0;
  105. /* split the map into input and output parts */
  106. if (!(sep = strchr(cur, '-'))) {
  107. av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
  108. "map '%s'\n", cur);
  109. return AVERROR(EINVAL);
  110. }
  111. *sep++ = 0;
  112. #define PARSE_CHANNEL(str, var, inout) \
  113. if (!(var = av_get_channel_layout(str))) { \
  114. av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
  115. return AVERROR(EINVAL); \
  116. } \
  117. if (av_get_channel_layout_nb_channels(var) != 1) { \
  118. av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \
  119. inout " channel.\n"); \
  120. return AVERROR(EINVAL); \
  121. }
  122. /* parse output channel */
  123. PARSE_CHANNEL(sep, out_channel, "output");
  124. if (!(out_channel & s->channel_layout)) {
  125. av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
  126. "requested channel layout.\n", sep);
  127. return AVERROR(EINVAL);
  128. }
  129. out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
  130. out_channel);
  131. if (s->channels[out_ch_idx].input >= 0) {
  132. av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
  133. "'%s'.\n", sep);
  134. return AVERROR(EINVAL);
  135. }
  136. /* parse input channel */
  137. input_idx = strtol(cur, &cur, 0);
  138. if (input_idx < 0 || input_idx >= s->inputs) {
  139. av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
  140. input_idx);
  141. return AVERROR(EINVAL);
  142. }
  143. if (*cur)
  144. cur++;
  145. in_ch_idx = strtol(cur, &p, 0);
  146. if (p == cur) {
  147. /* channel specifier is not a number,
  148. * try to parse as channel name */
  149. PARSE_CHANNEL(cur, in_channel, "input");
  150. }
  151. s->channels[out_ch_idx].input = input_idx;
  152. if (in_channel)
  153. s->channels[out_ch_idx].in_channel = in_channel;
  154. else
  155. s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
  156. cur = next;
  157. }
  158. return 0;
  159. }
  160. static int join_init(AVFilterContext *ctx, const char *args)
  161. {
  162. JoinContext *s = ctx->priv;
  163. int ret, i;
  164. s->class = &join_class;
  165. av_opt_set_defaults(s);
  166. if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
  167. av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
  168. return ret;
  169. }
  170. if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
  171. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
  172. s->channel_layout_str);
  173. ret = AVERROR(EINVAL);
  174. goto fail;
  175. }
  176. s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
  177. s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
  178. s->data = av_mallocz(sizeof(*s->data) * s->nb_channels);
  179. s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
  180. if (!s->channels || !s->data || !s->input_frames) {
  181. ret = AVERROR(ENOMEM);
  182. goto fail;
  183. }
  184. for (i = 0; i < s->nb_channels; i++) {
  185. s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
  186. s->channels[i].input = -1;
  187. }
  188. if ((ret = parse_maps(ctx)) < 0)
  189. goto fail;
  190. for (i = 0; i < s->inputs; i++) {
  191. char name[32];
  192. AVFilterPad pad = { 0 };
  193. snprintf(name, sizeof(name), "input%d", i);
  194. pad.type = AVMEDIA_TYPE_AUDIO;
  195. pad.name = av_strdup(name);
  196. pad.filter_samples = filter_samples;
  197. pad.needs_fifo = 1;
  198. ff_insert_inpad(ctx, i, &pad);
  199. }
  200. fail:
  201. av_opt_free(s);
  202. return ret;
  203. }
  204. static void join_uninit(AVFilterContext *ctx)
  205. {
  206. JoinContext *s = ctx->priv;
  207. int i;
  208. for (i = 0; i < ctx->nb_inputs; i++) {
  209. av_freep(&ctx->input_pads[i].name);
  210. avfilter_unref_buffer(s->input_frames[i]);
  211. }
  212. av_freep(&s->channels);
  213. av_freep(&s->data);
  214. av_freep(&s->input_frames);
  215. }
  216. static int join_query_formats(AVFilterContext *ctx)
  217. {
  218. JoinContext *s = ctx->priv;
  219. AVFilterChannelLayouts *layouts = NULL;
  220. int i;
  221. ff_add_channel_layout(&layouts, s->channel_layout);
  222. ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
  223. for (i = 0; i < ctx->nb_inputs; i++)
  224. ff_channel_layouts_ref(ff_all_channel_layouts(),
  225. &ctx->inputs[i]->out_channel_layouts);
  226. ff_set_common_formats (ctx, ff_planar_sample_fmts());
  227. ff_set_common_samplerates(ctx, ff_all_samplerates());
  228. return 0;
  229. }
  230. static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
  231. uint64_t *inputs)
  232. {
  233. int i;
  234. for (i = 0; i < ctx->nb_inputs; i++) {
  235. AVFilterLink *link = ctx->inputs[i];
  236. if (ch->out_channel & link->channel_layout &&
  237. !(ch->out_channel & inputs[i])) {
  238. ch->input = i;
  239. ch->in_channel = ch->out_channel;
  240. inputs[i] |= ch->out_channel;
  241. return;
  242. }
  243. }
  244. }
  245. static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
  246. uint64_t *inputs)
  247. {
  248. int i;
  249. for (i = 0; i < ctx->nb_inputs; i++) {
  250. AVFilterLink *link = ctx->inputs[i];
  251. if ((inputs[i] & link->channel_layout) != link->channel_layout) {
  252. uint64_t unused = link->channel_layout & ~inputs[i];
  253. ch->input = i;
  254. ch->in_channel = av_channel_layout_extract_channel(unused, 0);
  255. inputs[i] |= ch->in_channel;
  256. return;
  257. }
  258. }
  259. }
  260. static int join_config_output(AVFilterLink *outlink)
  261. {
  262. AVFilterContext *ctx = outlink->src;
  263. JoinContext *s = ctx->priv;
  264. uint64_t *inputs; // nth element tracks which channels are used from nth input
  265. int i, ret = 0;
  266. /* initialize inputs to user-specified mappings */
  267. if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
  268. return AVERROR(ENOMEM);
  269. for (i = 0; i < s->nb_channels; i++) {
  270. ChannelMap *ch = &s->channels[i];
  271. AVFilterLink *inlink;
  272. if (ch->input < 0)
  273. continue;
  274. inlink = ctx->inputs[ch->input];
  275. if (!ch->in_channel)
  276. ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
  277. ch->in_channel_idx);
  278. if (!(ch->in_channel & inlink->channel_layout)) {
  279. av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
  280. "input stream #%d.\n", av_get_channel_name(ch->in_channel),
  281. ch->input);
  282. ret = AVERROR(EINVAL);
  283. goto fail;
  284. }
  285. inputs[ch->input] |= ch->in_channel;
  286. }
  287. /* guess channel maps when not explicitly defined */
  288. /* first try unused matching channels */
  289. for (i = 0; i < s->nb_channels; i++) {
  290. ChannelMap *ch = &s->channels[i];
  291. if (ch->input < 0)
  292. guess_map_matching(ctx, ch, inputs);
  293. }
  294. /* if the above failed, try to find _any_ unused input channel */
  295. for (i = 0; i < s->nb_channels; i++) {
  296. ChannelMap *ch = &s->channels[i];
  297. if (ch->input < 0)
  298. guess_map_any(ctx, ch, inputs);
  299. if (ch->input < 0) {
  300. av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
  301. "output channel '%s'.\n",
  302. av_get_channel_name(ch->out_channel));
  303. goto fail;
  304. }
  305. ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
  306. ch->in_channel);
  307. }
  308. /* print mappings */
  309. av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
  310. for (i = 0; i < s->nb_channels; i++) {
  311. ChannelMap *ch = &s->channels[i];
  312. av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
  313. av_get_channel_name(ch->in_channel),
  314. av_get_channel_name(ch->out_channel));
  315. }
  316. av_log(ctx, AV_LOG_VERBOSE, "\n");
  317. for (i = 0; i < ctx->nb_inputs; i++) {
  318. if (!inputs[i])
  319. av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
  320. "stream %d.\n", i);
  321. }
  322. fail:
  323. av_freep(&inputs);
  324. return ret;
  325. }
  326. static void join_free_buffer(AVFilterBuffer *buf)
  327. {
  328. JoinBufferPriv *priv = buf->priv;
  329. if (priv) {
  330. int i;
  331. for (i = 0; i < priv->nb_in_buffers; i++)
  332. avfilter_unref_buffer(priv->in_buffers[i]);
  333. av_freep(&priv->in_buffers);
  334. av_freep(&buf->priv);
  335. }
  336. if (buf->extended_data != buf->data)
  337. av_freep(&buf->extended_data);
  338. av_freep(&buf);
  339. }
  340. static int join_request_frame(AVFilterLink *outlink)
  341. {
  342. AVFilterContext *ctx = outlink->src;
  343. JoinContext *s = ctx->priv;
  344. AVFilterBufferRef *buf;
  345. JoinBufferPriv *priv;
  346. int linesize = INT_MAX;
  347. int perms = ~0;
  348. int nb_samples = 0;
  349. int i, j, ret;
  350. /* get a frame on each input */
  351. for (i = 0; i < ctx->nb_inputs; i++) {
  352. AVFilterLink *inlink = ctx->inputs[i];
  353. if (!s->input_frames[i] &&
  354. (ret = ff_request_frame(inlink)) < 0)
  355. return ret;
  356. /* request the same number of samples on all inputs */
  357. if (i == 0) {
  358. nb_samples = s->input_frames[0]->audio->nb_samples;
  359. for (j = 1; !i && j < ctx->nb_inputs; j++)
  360. ctx->inputs[j]->request_samples = nb_samples;
  361. }
  362. }
  363. for (i = 0; i < s->nb_channels; i++) {
  364. ChannelMap *ch = &s->channels[i];
  365. AVFilterBufferRef *cur_buf = s->input_frames[ch->input];
  366. s->data[i] = cur_buf->extended_data[ch->in_channel_idx];
  367. linesize = FFMIN(linesize, cur_buf->linesize[0]);
  368. perms &= cur_buf->perms;
  369. }
  370. av_assert0(nb_samples > 0);
  371. buf = avfilter_get_audio_buffer_ref_from_arrays(s->data, linesize, perms,
  372. nb_samples, outlink->format,
  373. outlink->channel_layout);
  374. if (!buf)
  375. return AVERROR(ENOMEM);
  376. buf->buf->free = join_free_buffer;
  377. buf->pts = s->input_frames[0]->pts;
  378. if (!(priv = av_mallocz(sizeof(*priv))))
  379. goto fail;
  380. if (!(priv->in_buffers = av_mallocz(sizeof(*priv->in_buffers) * ctx->nb_inputs)))
  381. goto fail;
  382. for (i = 0; i < ctx->nb_inputs; i++)
  383. priv->in_buffers[i] = s->input_frames[i];
  384. priv->nb_in_buffers = ctx->nb_inputs;
  385. buf->buf->priv = priv;
  386. ff_filter_samples(outlink, buf);
  387. memset(s->input_frames, 0, sizeof(*s->input_frames) * ctx->nb_inputs);
  388. return 0;
  389. fail:
  390. avfilter_unref_buffer(buf);
  391. if (priv)
  392. av_freep(&priv->in_buffers);
  393. av_freep(&priv);
  394. return AVERROR(ENOMEM);
  395. }
  396. AVFilter avfilter_af_join = {
  397. .name = "join",
  398. .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
  399. "multi-channel output"),
  400. .priv_size = sizeof(JoinContext),
  401. .init = join_init,
  402. .uninit = join_uninit,
  403. .query_formats = join_query_formats,
  404. .inputs = (const AVFilterPad[]){{ NULL }},
  405. .outputs = (const AVFilterPad[]){{ .name = "default",
  406. .type = AVMEDIA_TYPE_AUDIO,
  407. .config_props = join_config_output,
  408. .request_frame = join_request_frame, },
  409. { NULL }},
  410. };