af_join.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * Audio join filter
  21. *
  22. * Join multiple audio inputs as different channels in
  23. * a single output
  24. */
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/common.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. AVFrame **input_frames;
  51. /**
  52. * Temporary storage for buffer references, for assembling the output frame.
  53. */
  54. AVBufferRef **buffers;
  55. } JoinContext;
  56. #define OFFSET(x) offsetof(JoinContext, x)
  57. #define A AV_OPT_FLAG_AUDIO_PARAM
  58. static const AVOption join_options[] = {
  59. { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A },
  60. { "channel_layout", "Channel layout of the "
  61. "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A },
  62. { "map", "A comma-separated list of channels maps in the format "
  63. "'input_stream.input_channel-output_channel.",
  64. OFFSET(map), AV_OPT_TYPE_STRING, .flags = A },
  65. { NULL },
  66. };
  67. static const AVClass join_class = {
  68. .class_name = "join filter",
  69. .item_name = av_default_item_name,
  70. .option = join_options,
  71. .version = LIBAVUTIL_VERSION_INT,
  72. };
  73. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  74. {
  75. AVFilterContext *ctx = link->dst;
  76. JoinContext *s = ctx->priv;
  77. int i;
  78. for (i = 0; i < ctx->nb_inputs; i++)
  79. if (link == ctx->inputs[i])
  80. break;
  81. av_assert0(i < ctx->nb_inputs);
  82. av_assert0(!s->input_frames[i]);
  83. s->input_frames[i] = frame;
  84. return 0;
  85. }
  86. static int parse_maps(AVFilterContext *ctx)
  87. {
  88. JoinContext *s = ctx->priv;
  89. char separator = '|';
  90. char *cur = s->map;
  91. #if FF_API_OLD_FILTER_OPTS
  92. if (cur && strchr(cur, ',')) {
  93. av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "
  94. "separate the mappings.\n");
  95. separator = ',';
  96. }
  97. #endif
  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, separator);
  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 av_cold int join_init(AVFilterContext *ctx)
  161. {
  162. JoinContext *s = ctx->priv;
  163. int ret, i;
  164. if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
  165. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
  166. s->channel_layout_str);
  167. ret = AVERROR(EINVAL);
  168. goto fail;
  169. }
  170. s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
  171. s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
  172. s->buffers = av_mallocz(sizeof(*s->buffers) * s->nb_channels);
  173. s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
  174. if (!s->channels || !s->buffers|| !s->input_frames) {
  175. ret = AVERROR(ENOMEM);
  176. goto fail;
  177. }
  178. for (i = 0; i < s->nb_channels; i++) {
  179. s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
  180. s->channels[i].input = -1;
  181. }
  182. if ((ret = parse_maps(ctx)) < 0)
  183. goto fail;
  184. for (i = 0; i < s->inputs; i++) {
  185. char name[32];
  186. AVFilterPad pad = { 0 };
  187. snprintf(name, sizeof(name), "input%d", i);
  188. pad.type = AVMEDIA_TYPE_AUDIO;
  189. pad.name = av_strdup(name);
  190. pad.filter_frame = filter_frame;
  191. pad.needs_fifo = 1;
  192. ff_insert_inpad(ctx, i, &pad);
  193. }
  194. fail:
  195. av_opt_free(s);
  196. return ret;
  197. }
  198. static av_cold void join_uninit(AVFilterContext *ctx)
  199. {
  200. JoinContext *s = ctx->priv;
  201. int i;
  202. for (i = 0; i < ctx->nb_inputs; i++) {
  203. av_freep(&ctx->input_pads[i].name);
  204. av_frame_free(&s->input_frames[i]);
  205. }
  206. av_freep(&s->channels);
  207. av_freep(&s->buffers);
  208. av_freep(&s->input_frames);
  209. }
  210. static int join_query_formats(AVFilterContext *ctx)
  211. {
  212. JoinContext *s = ctx->priv;
  213. AVFilterChannelLayouts *layouts = NULL;
  214. int i;
  215. ff_add_channel_layout(&layouts, s->channel_layout);
  216. ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
  217. for (i = 0; i < ctx->nb_inputs; i++)
  218. ff_channel_layouts_ref(ff_all_channel_layouts(),
  219. &ctx->inputs[i]->out_channel_layouts);
  220. ff_set_common_formats (ctx, ff_planar_sample_fmts());
  221. ff_set_common_samplerates(ctx, ff_all_samplerates());
  222. return 0;
  223. }
  224. static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
  225. uint64_t *inputs)
  226. {
  227. int i;
  228. for (i = 0; i < ctx->nb_inputs; i++) {
  229. AVFilterLink *link = ctx->inputs[i];
  230. if (ch->out_channel & link->channel_layout &&
  231. !(ch->out_channel & inputs[i])) {
  232. ch->input = i;
  233. ch->in_channel = ch->out_channel;
  234. inputs[i] |= ch->out_channel;
  235. return;
  236. }
  237. }
  238. }
  239. static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
  240. uint64_t *inputs)
  241. {
  242. int i;
  243. for (i = 0; i < ctx->nb_inputs; i++) {
  244. AVFilterLink *link = ctx->inputs[i];
  245. if ((inputs[i] & link->channel_layout) != link->channel_layout) {
  246. uint64_t unused = link->channel_layout & ~inputs[i];
  247. ch->input = i;
  248. ch->in_channel = av_channel_layout_extract_channel(unused, 0);
  249. inputs[i] |= ch->in_channel;
  250. return;
  251. }
  252. }
  253. }
  254. static int join_config_output(AVFilterLink *outlink)
  255. {
  256. AVFilterContext *ctx = outlink->src;
  257. JoinContext *s = ctx->priv;
  258. uint64_t *inputs; // nth element tracks which channels are used from nth input
  259. int i, ret = 0;
  260. /* initialize inputs to user-specified mappings */
  261. if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
  262. return AVERROR(ENOMEM);
  263. for (i = 0; i < s->nb_channels; i++) {
  264. ChannelMap *ch = &s->channels[i];
  265. AVFilterLink *inlink;
  266. if (ch->input < 0)
  267. continue;
  268. inlink = ctx->inputs[ch->input];
  269. if (!ch->in_channel)
  270. ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
  271. ch->in_channel_idx);
  272. if (!(ch->in_channel & inlink->channel_layout)) {
  273. av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
  274. "input stream #%d.\n", av_get_channel_name(ch->in_channel),
  275. ch->input);
  276. ret = AVERROR(EINVAL);
  277. goto fail;
  278. }
  279. inputs[ch->input] |= ch->in_channel;
  280. }
  281. /* guess channel maps when not explicitly defined */
  282. /* first try unused matching channels */
  283. for (i = 0; i < s->nb_channels; i++) {
  284. ChannelMap *ch = &s->channels[i];
  285. if (ch->input < 0)
  286. guess_map_matching(ctx, ch, inputs);
  287. }
  288. /* if the above failed, try to find _any_ unused input channel */
  289. for (i = 0; i < s->nb_channels; i++) {
  290. ChannelMap *ch = &s->channels[i];
  291. if (ch->input < 0)
  292. guess_map_any(ctx, ch, inputs);
  293. if (ch->input < 0) {
  294. av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
  295. "output channel '%s'.\n",
  296. av_get_channel_name(ch->out_channel));
  297. goto fail;
  298. }
  299. ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
  300. ch->in_channel);
  301. }
  302. /* print mappings */
  303. av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
  304. for (i = 0; i < s->nb_channels; i++) {
  305. ChannelMap *ch = &s->channels[i];
  306. av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
  307. av_get_channel_name(ch->in_channel),
  308. av_get_channel_name(ch->out_channel));
  309. }
  310. av_log(ctx, AV_LOG_VERBOSE, "\n");
  311. for (i = 0; i < ctx->nb_inputs; i++) {
  312. if (!inputs[i])
  313. av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
  314. "stream %d.\n", i);
  315. }
  316. fail:
  317. av_freep(&inputs);
  318. return ret;
  319. }
  320. static int join_request_frame(AVFilterLink *outlink)
  321. {
  322. AVFilterContext *ctx = outlink->src;
  323. JoinContext *s = ctx->priv;
  324. AVFrame *frame;
  325. int linesize = INT_MAX;
  326. int nb_samples = 0;
  327. int nb_buffers = 0;
  328. int i, j, ret;
  329. /* get a frame on each input */
  330. for (i = 0; i < ctx->nb_inputs; i++) {
  331. AVFilterLink *inlink = ctx->inputs[i];
  332. if (!s->input_frames[i] &&
  333. (ret = ff_request_frame(inlink)) < 0)
  334. return ret;
  335. /* request the same number of samples on all inputs */
  336. if (i == 0) {
  337. nb_samples = s->input_frames[0]->nb_samples;
  338. for (j = 1; !i && j < ctx->nb_inputs; j++)
  339. ctx->inputs[j]->request_samples = nb_samples;
  340. }
  341. }
  342. /* setup the output frame */
  343. frame = av_frame_alloc();
  344. if (!frame)
  345. return AVERROR(ENOMEM);
  346. if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
  347. frame->extended_data = av_mallocz(s->nb_channels *
  348. sizeof(*frame->extended_data));
  349. if (!frame->extended_data) {
  350. ret = AVERROR(ENOMEM);
  351. goto fail;
  352. }
  353. }
  354. /* copy the data pointers */
  355. for (i = 0; i < s->nb_channels; i++) {
  356. ChannelMap *ch = &s->channels[i];
  357. AVFrame *cur = s->input_frames[ch->input];
  358. AVBufferRef *buf;
  359. frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
  360. linesize = FFMIN(linesize, cur->linesize[0]);
  361. /* add the buffer where this plan is stored to the list if it's
  362. * not already there */
  363. buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
  364. if (!buf) {
  365. ret = AVERROR(EINVAL);
  366. goto fail;
  367. }
  368. for (j = 0; j < nb_buffers; j++)
  369. if (s->buffers[j]->buffer == buf->buffer)
  370. break;
  371. if (j == i)
  372. s->buffers[nb_buffers++] = buf;
  373. }
  374. /* create references to the buffers we copied to output */
  375. if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
  376. frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
  377. frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
  378. frame->nb_extended_buf);
  379. if (!frame->extended_buf) {
  380. frame->nb_extended_buf = 0;
  381. ret = AVERROR(ENOMEM);
  382. goto fail;
  383. }
  384. }
  385. for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
  386. frame->buf[i] = av_buffer_ref(s->buffers[i]);
  387. if (!frame->buf[i]) {
  388. ret = AVERROR(ENOMEM);
  389. goto fail;
  390. }
  391. }
  392. for (i = 0; i < frame->nb_extended_buf; i++) {
  393. frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
  394. FF_ARRAY_ELEMS(frame->buf)]);
  395. if (!frame->extended_buf[i]) {
  396. ret = AVERROR(ENOMEM);
  397. goto fail;
  398. }
  399. }
  400. frame->nb_samples = nb_samples;
  401. frame->channel_layout = outlink->channel_layout;
  402. frame->sample_rate = outlink->sample_rate;
  403. frame->format = outlink->format;
  404. frame->pts = s->input_frames[0]->pts;
  405. frame->linesize[0] = linesize;
  406. if (frame->data != frame->extended_data) {
  407. memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
  408. FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
  409. }
  410. ret = ff_filter_frame(outlink, frame);
  411. for (i = 0; i < ctx->nb_inputs; i++)
  412. av_frame_free(&s->input_frames[i]);
  413. return ret;
  414. fail:
  415. av_frame_free(&frame);
  416. return ret;
  417. }
  418. static const AVFilterPad avfilter_af_join_outputs[] = {
  419. {
  420. .name = "default",
  421. .type = AVMEDIA_TYPE_AUDIO,
  422. .config_props = join_config_output,
  423. .request_frame = join_request_frame,
  424. },
  425. { NULL }
  426. };
  427. AVFilter ff_af_join = {
  428. .name = "join",
  429. .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
  430. "multi-channel output"),
  431. .priv_size = sizeof(JoinContext),
  432. .priv_class = &join_class,
  433. .init = join_init,
  434. .uninit = join_uninit,
  435. .query_formats = join_query_formats,
  436. .inputs = NULL,
  437. .outputs = avfilter_af_join_outputs,
  438. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  439. };