af_join.c 15 KB

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