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.

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