af_join.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. #define F AV_OPT_FLAG_FILTERING_PARAM
  59. static const AVOption join_options[] = {
  60. { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A|F },
  61. { "channel_layout", "Channel layout of the "
  62. "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A|F },
  63. { "map", "A comma-separated list of channels maps in the format "
  64. "'input_stream.input_channel-output_channel.",
  65. OFFSET(map), AV_OPT_TYPE_STRING, .flags = A|F },
  66. { NULL }
  67. };
  68. AVFILTER_DEFINE_CLASS(join);
  69. static int try_push_frame(AVFilterContext *ctx);
  70. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  71. {
  72. AVFilterContext *ctx = link->dst;
  73. JoinContext *s = ctx->priv;
  74. int i, j;
  75. for (i = 0; i < ctx->nb_inputs; i++)
  76. if (link == ctx->inputs[i])
  77. break;
  78. av_assert0(i < ctx->nb_inputs);
  79. av_assert0(!s->input_frames[i]);
  80. s->input_frames[i] = frame;
  81. /* request the same number of samples on all inputs */
  82. /* FIXME that means a frame arriving asynchronously on a different input
  83. will not have the requested number of samples */
  84. if (i == 0) {
  85. int nb_samples = s->input_frames[0]->nb_samples;
  86. for (j = 1; !i && j < ctx->nb_inputs; j++)
  87. ctx->inputs[j]->request_samples = nb_samples;
  88. }
  89. return try_push_frame(ctx);
  90. }
  91. static int parse_maps(AVFilterContext *ctx)
  92. {
  93. JoinContext *s = ctx->priv;
  94. char separator = '|';
  95. char *cur = s->map;
  96. #if FF_API_OLD_FILTER_OPTS
  97. if (cur && strchr(cur, ',')) {
  98. av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "
  99. "separate the mappings.\n");
  100. separator = ',';
  101. }
  102. #endif
  103. while (cur && *cur) {
  104. char *sep, *next, *p;
  105. uint64_t in_channel = 0, out_channel = 0;
  106. int input_idx, out_ch_idx, in_ch_idx;
  107. next = strchr(cur, separator);
  108. if (next)
  109. *next++ = 0;
  110. /* split the map into input and output parts */
  111. if (!(sep = strchr(cur, '-'))) {
  112. av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
  113. "map '%s'\n", cur);
  114. return AVERROR(EINVAL);
  115. }
  116. *sep++ = 0;
  117. #define PARSE_CHANNEL(str, var, inout) \
  118. if (!(var = av_get_channel_layout(str))) { \
  119. av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
  120. return AVERROR(EINVAL); \
  121. } \
  122. if (av_get_channel_layout_nb_channels(var) != 1) { \
  123. av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \
  124. inout " channel.\n"); \
  125. return AVERROR(EINVAL); \
  126. }
  127. /* parse output channel */
  128. PARSE_CHANNEL(sep, out_channel, "output");
  129. if (!(out_channel & s->channel_layout)) {
  130. av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
  131. "requested channel layout.\n", sep);
  132. return AVERROR(EINVAL);
  133. }
  134. out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
  135. out_channel);
  136. if (s->channels[out_ch_idx].input >= 0) {
  137. av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
  138. "'%s'.\n", sep);
  139. return AVERROR(EINVAL);
  140. }
  141. /* parse input channel */
  142. input_idx = strtol(cur, &cur, 0);
  143. if (input_idx < 0 || input_idx >= s->inputs) {
  144. av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
  145. input_idx);
  146. return AVERROR(EINVAL);
  147. }
  148. if (*cur)
  149. cur++;
  150. in_ch_idx = strtol(cur, &p, 0);
  151. if (p == cur) {
  152. /* channel specifier is not a number,
  153. * try to parse as channel name */
  154. PARSE_CHANNEL(cur, in_channel, "input");
  155. }
  156. s->channels[out_ch_idx].input = input_idx;
  157. if (in_channel)
  158. s->channels[out_ch_idx].in_channel = in_channel;
  159. else
  160. s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
  161. cur = next;
  162. }
  163. return 0;
  164. }
  165. static av_cold int join_init(AVFilterContext *ctx)
  166. {
  167. JoinContext *s = ctx->priv;
  168. int ret, i;
  169. if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
  170. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
  171. s->channel_layout_str);
  172. return AVERROR(EINVAL);
  173. }
  174. s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
  175. s->channels = av_mallocz_array(s->nb_channels, sizeof(*s->channels));
  176. s->buffers = av_mallocz_array(s->nb_channels, sizeof(*s->buffers));
  177. s->input_frames = av_mallocz_array(s->inputs, sizeof(*s->input_frames));
  178. if (!s->channels || !s->buffers|| !s->input_frames)
  179. return AVERROR(ENOMEM);
  180. for (i = 0; i < s->nb_channels; i++) {
  181. s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
  182. s->channels[i].input = -1;
  183. }
  184. if ((ret = parse_maps(ctx)) < 0)
  185. return ret;
  186. for (i = 0; i < s->inputs; i++) {
  187. char name[32];
  188. AVFilterPad pad = { 0 };
  189. snprintf(name, sizeof(name), "input%d", i);
  190. pad.type = AVMEDIA_TYPE_AUDIO;
  191. pad.name = av_strdup(name);
  192. if (!pad.name)
  193. return AVERROR(ENOMEM);
  194. pad.filter_frame = filter_frame;
  195. pad.needs_fifo = 1;
  196. ff_insert_inpad(ctx, i, &pad);
  197. }
  198. return 0;
  199. }
  200. static av_cold void join_uninit(AVFilterContext *ctx)
  201. {
  202. JoinContext *s = ctx->priv;
  203. int i;
  204. for (i = 0; i < ctx->nb_inputs; i++) {
  205. av_freep(&ctx->input_pads[i].name);
  206. av_frame_free(&s->input_frames[i]);
  207. }
  208. av_freep(&s->channels);
  209. av_freep(&s->buffers);
  210. av_freep(&s->input_frames);
  211. }
  212. static int join_query_formats(AVFilterContext *ctx)
  213. {
  214. JoinContext *s = ctx->priv;
  215. AVFilterChannelLayouts *layouts = NULL;
  216. int i, ret;
  217. if ((ret = ff_add_channel_layout(&layouts, s->channel_layout)) < 0 ||
  218. (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
  219. return ret;
  220. for (i = 0; i < ctx->nb_inputs; i++) {
  221. layouts = ff_all_channel_layouts();
  222. if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
  223. return ret;
  224. }
  225. if ((ret = ff_set_common_formats(ctx, ff_planar_sample_fmts())) < 0 ||
  226. (ret = ff_set_common_samplerates(ctx, ff_all_samplerates())) < 0)
  227. return ret;
  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_array(ctx->nb_inputs, sizeof(*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 int join_request_frame(AVFilterLink *outlink)
  327. {
  328. AVFilterContext *ctx = outlink->src;
  329. JoinContext *s = ctx->priv;
  330. int i;
  331. /* get a frame on each input */
  332. for (i = 0; i < ctx->nb_inputs; i++) {
  333. AVFilterLink *inlink = ctx->inputs[i];
  334. if (!s->input_frames[i])
  335. return ff_request_frame(inlink);
  336. }
  337. return 0;
  338. }
  339. static int try_push_frame(AVFilterContext *ctx)
  340. {
  341. AVFilterLink *outlink = ctx->outputs[0];
  342. JoinContext *s = ctx->priv;
  343. AVFrame *frame;
  344. int linesize = INT_MAX;
  345. int nb_samples = INT_MAX;
  346. int nb_buffers = 0;
  347. int i, j, ret;
  348. for (i = 0; i < ctx->nb_inputs; i++) {
  349. if (!s->input_frames[i])
  350. return 0;
  351. nb_samples = FFMIN(nb_samples, s->input_frames[i]->nb_samples);
  352. }
  353. /* setup the output frame */
  354. frame = av_frame_alloc();
  355. if (!frame)
  356. return AVERROR(ENOMEM);
  357. if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
  358. frame->extended_data = av_mallocz_array(s->nb_channels,
  359. sizeof(*frame->extended_data));
  360. if (!frame->extended_data) {
  361. ret = AVERROR(ENOMEM);
  362. goto fail;
  363. }
  364. }
  365. /* copy the data pointers */
  366. for (i = 0; i < s->nb_channels; i++) {
  367. ChannelMap *ch = &s->channels[i];
  368. AVFrame *cur = s->input_frames[ch->input];
  369. AVBufferRef *buf;
  370. frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
  371. linesize = FFMIN(linesize, cur->linesize[0]);
  372. /* add the buffer where this plan is stored to the list if it's
  373. * not already there */
  374. buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
  375. if (!buf) {
  376. ret = AVERROR(EINVAL);
  377. goto fail;
  378. }
  379. for (j = 0; j < nb_buffers; j++)
  380. if (s->buffers[j]->buffer == buf->buffer)
  381. break;
  382. if (j == i)
  383. s->buffers[nb_buffers++] = buf;
  384. }
  385. /* create references to the buffers we copied to output */
  386. if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
  387. frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
  388. frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
  389. sizeof(*frame->extended_buf));
  390. if (!frame->extended_buf) {
  391. frame->nb_extended_buf = 0;
  392. ret = AVERROR(ENOMEM);
  393. goto fail;
  394. }
  395. }
  396. for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
  397. frame->buf[i] = av_buffer_ref(s->buffers[i]);
  398. if (!frame->buf[i]) {
  399. ret = AVERROR(ENOMEM);
  400. goto fail;
  401. }
  402. }
  403. for (i = 0; i < frame->nb_extended_buf; i++) {
  404. frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
  405. FF_ARRAY_ELEMS(frame->buf)]);
  406. if (!frame->extended_buf[i]) {
  407. ret = AVERROR(ENOMEM);
  408. goto fail;
  409. }
  410. }
  411. frame->nb_samples = nb_samples;
  412. frame->channel_layout = outlink->channel_layout;
  413. av_frame_set_channels(frame, outlink->channels);
  414. frame->sample_rate = outlink->sample_rate;
  415. frame->format = outlink->format;
  416. frame->pts = s->input_frames[0]->pts;
  417. frame->linesize[0] = linesize;
  418. if (frame->data != frame->extended_data) {
  419. memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
  420. FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
  421. }
  422. ret = ff_filter_frame(outlink, frame);
  423. for (i = 0; i < ctx->nb_inputs; i++)
  424. av_frame_free(&s->input_frames[i]);
  425. return ret;
  426. fail:
  427. av_frame_free(&frame);
  428. return ret;
  429. }
  430. static const AVFilterPad avfilter_af_join_outputs[] = {
  431. {
  432. .name = "default",
  433. .type = AVMEDIA_TYPE_AUDIO,
  434. .config_props = join_config_output,
  435. .request_frame = join_request_frame,
  436. },
  437. { NULL }
  438. };
  439. AVFilter ff_af_join = {
  440. .name = "join",
  441. .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
  442. "multi-channel output."),
  443. .priv_size = sizeof(JoinContext),
  444. .priv_class = &join_class,
  445. .init = join_init,
  446. .uninit = join_uninit,
  447. .query_formats = join_query_formats,
  448. .inputs = NULL,
  449. .outputs = avfilter_af_join_outputs,
  450. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  451. };