af_join.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 "filters.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. AVFrame **input_frames;
  52. /**
  53. * Temporary storage for buffer references, for assembling the output frame.
  54. */
  55. AVBufferRef **buffers;
  56. } JoinContext;
  57. #define OFFSET(x) offsetof(JoinContext, x)
  58. #define A AV_OPT_FLAG_AUDIO_PARAM
  59. #define F AV_OPT_FLAG_FILTERING_PARAM
  60. static const AVOption join_options[] = {
  61. { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A|F },
  62. { "channel_layout", "Channel layout of the "
  63. "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A|F },
  64. { "map", "A comma-separated list of channels maps in the format "
  65. "'input_stream.input_channel-output_channel.",
  66. OFFSET(map), AV_OPT_TYPE_STRING, .flags = A|F },
  67. { NULL }
  68. };
  69. AVFILTER_DEFINE_CLASS(join);
  70. static int parse_maps(AVFilterContext *ctx)
  71. {
  72. JoinContext *s = ctx->priv;
  73. char separator = '|';
  74. char *cur = s->map;
  75. while (cur && *cur) {
  76. char *sep, *next, *p;
  77. uint64_t in_channel = 0, out_channel = 0;
  78. int input_idx, out_ch_idx, in_ch_idx;
  79. next = strchr(cur, separator);
  80. if (next)
  81. *next++ = 0;
  82. /* split the map into input and output parts */
  83. if (!(sep = strchr(cur, '-'))) {
  84. av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
  85. "map '%s'\n", cur);
  86. return AVERROR(EINVAL);
  87. }
  88. *sep++ = 0;
  89. #define PARSE_CHANNEL(str, var, inout) \
  90. if (!(var = av_get_channel_layout(str))) { \
  91. av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
  92. return AVERROR(EINVAL); \
  93. } \
  94. if (av_get_channel_layout_nb_channels(var) != 1) { \
  95. av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \
  96. inout " channel.\n"); \
  97. return AVERROR(EINVAL); \
  98. }
  99. /* parse output channel */
  100. PARSE_CHANNEL(sep, out_channel, "output");
  101. if (!(out_channel & s->channel_layout)) {
  102. av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
  103. "requested channel layout.\n", sep);
  104. return AVERROR(EINVAL);
  105. }
  106. out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
  107. out_channel);
  108. if (s->channels[out_ch_idx].input >= 0) {
  109. av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
  110. "'%s'.\n", sep);
  111. return AVERROR(EINVAL);
  112. }
  113. /* parse input channel */
  114. input_idx = strtol(cur, &cur, 0);
  115. if (input_idx < 0 || input_idx >= s->inputs) {
  116. av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
  117. input_idx);
  118. return AVERROR(EINVAL);
  119. }
  120. if (*cur)
  121. cur++;
  122. in_ch_idx = strtol(cur, &p, 0);
  123. if (p == cur) {
  124. /* channel specifier is not a number,
  125. * try to parse as channel name */
  126. PARSE_CHANNEL(cur, in_channel, "input");
  127. }
  128. s->channels[out_ch_idx].input = input_idx;
  129. if (in_channel)
  130. s->channels[out_ch_idx].in_channel = in_channel;
  131. else
  132. s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
  133. cur = next;
  134. }
  135. return 0;
  136. }
  137. static av_cold int join_init(AVFilterContext *ctx)
  138. {
  139. JoinContext *s = ctx->priv;
  140. int ret, i;
  141. if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
  142. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
  143. s->channel_layout_str);
  144. return AVERROR(EINVAL);
  145. }
  146. s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
  147. s->channels = av_mallocz_array(s->nb_channels, sizeof(*s->channels));
  148. s->buffers = av_mallocz_array(s->nb_channels, sizeof(*s->buffers));
  149. s->input_frames = av_mallocz_array(s->inputs, sizeof(*s->input_frames));
  150. if (!s->channels || !s->buffers|| !s->input_frames)
  151. return AVERROR(ENOMEM);
  152. for (i = 0; i < s->nb_channels; i++) {
  153. s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
  154. s->channels[i].input = -1;
  155. }
  156. if ((ret = parse_maps(ctx)) < 0)
  157. return ret;
  158. for (i = 0; i < s->inputs; i++) {
  159. char name[32];
  160. AVFilterPad pad = { 0 };
  161. snprintf(name, sizeof(name), "input%d", i);
  162. pad.type = AVMEDIA_TYPE_AUDIO;
  163. pad.name = av_strdup(name);
  164. if (!pad.name)
  165. return AVERROR(ENOMEM);
  166. if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
  167. av_freep(&pad.name);
  168. return ret;
  169. }
  170. }
  171. return 0;
  172. }
  173. static av_cold void join_uninit(AVFilterContext *ctx)
  174. {
  175. JoinContext *s = ctx->priv;
  176. int i;
  177. for (i = 0; i < ctx->nb_inputs; i++) {
  178. av_freep(&ctx->input_pads[i].name);
  179. av_frame_free(&s->input_frames[i]);
  180. }
  181. av_freep(&s->channels);
  182. av_freep(&s->buffers);
  183. av_freep(&s->input_frames);
  184. }
  185. static int join_query_formats(AVFilterContext *ctx)
  186. {
  187. JoinContext *s = ctx->priv;
  188. AVFilterChannelLayouts *layouts = NULL;
  189. int i, ret;
  190. if ((ret = ff_add_channel_layout(&layouts, s->channel_layout)) < 0 ||
  191. (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
  192. return ret;
  193. for (i = 0; i < ctx->nb_inputs; i++) {
  194. layouts = ff_all_channel_layouts();
  195. if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
  196. return ret;
  197. }
  198. if ((ret = ff_set_common_formats(ctx, ff_planar_sample_fmts())) < 0 ||
  199. (ret = ff_set_common_samplerates(ctx, ff_all_samplerates())) < 0)
  200. return ret;
  201. return 0;
  202. }
  203. static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
  204. uint64_t *inputs)
  205. {
  206. int i;
  207. for (i = 0; i < ctx->nb_inputs; i++) {
  208. AVFilterLink *link = ctx->inputs[i];
  209. if (ch->out_channel & link->channel_layout &&
  210. !(ch->out_channel & inputs[i])) {
  211. ch->input = i;
  212. ch->in_channel = ch->out_channel;
  213. inputs[i] |= ch->out_channel;
  214. return;
  215. }
  216. }
  217. }
  218. static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
  219. uint64_t *inputs)
  220. {
  221. int i;
  222. for (i = 0; i < ctx->nb_inputs; i++) {
  223. AVFilterLink *link = ctx->inputs[i];
  224. if ((inputs[i] & link->channel_layout) != link->channel_layout) {
  225. uint64_t unused = link->channel_layout & ~inputs[i];
  226. ch->input = i;
  227. ch->in_channel = av_channel_layout_extract_channel(unused, 0);
  228. inputs[i] |= ch->in_channel;
  229. return;
  230. }
  231. }
  232. }
  233. static int join_config_output(AVFilterLink *outlink)
  234. {
  235. AVFilterContext *ctx = outlink->src;
  236. JoinContext *s = ctx->priv;
  237. uint64_t *inputs; // nth element tracks which channels are used from nth input
  238. int i, ret = 0;
  239. /* initialize inputs to user-specified mappings */
  240. if (!(inputs = av_mallocz_array(ctx->nb_inputs, sizeof(*inputs))))
  241. return AVERROR(ENOMEM);
  242. for (i = 0; i < s->nb_channels; i++) {
  243. ChannelMap *ch = &s->channels[i];
  244. AVFilterLink *inlink;
  245. if (ch->input < 0)
  246. continue;
  247. inlink = ctx->inputs[ch->input];
  248. if (!ch->in_channel)
  249. ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
  250. ch->in_channel_idx);
  251. if (!(ch->in_channel & inlink->channel_layout)) {
  252. av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
  253. "input stream #%d.\n", av_get_channel_name(ch->in_channel),
  254. ch->input);
  255. ret = AVERROR(EINVAL);
  256. goto fail;
  257. }
  258. inputs[ch->input] |= ch->in_channel;
  259. }
  260. /* guess channel maps when not explicitly defined */
  261. /* first try unused matching channels */
  262. for (i = 0; i < s->nb_channels; i++) {
  263. ChannelMap *ch = &s->channels[i];
  264. if (ch->input < 0)
  265. guess_map_matching(ctx, ch, inputs);
  266. }
  267. /* if the above failed, try to find _any_ unused input channel */
  268. for (i = 0; i < s->nb_channels; i++) {
  269. ChannelMap *ch = &s->channels[i];
  270. if (ch->input < 0)
  271. guess_map_any(ctx, ch, inputs);
  272. if (ch->input < 0) {
  273. av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
  274. "output channel '%s'.\n",
  275. av_get_channel_name(ch->out_channel));
  276. goto fail;
  277. }
  278. ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
  279. ch->in_channel);
  280. }
  281. /* print mappings */
  282. av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
  283. for (i = 0; i < s->nb_channels; i++) {
  284. ChannelMap *ch = &s->channels[i];
  285. av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
  286. av_get_channel_name(ch->in_channel),
  287. av_get_channel_name(ch->out_channel));
  288. }
  289. av_log(ctx, AV_LOG_VERBOSE, "\n");
  290. for (i = 0; i < ctx->nb_inputs; i++) {
  291. if (!inputs[i])
  292. av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
  293. "stream %d.\n", i);
  294. }
  295. fail:
  296. av_freep(&inputs);
  297. return ret;
  298. }
  299. static int try_push_frame(AVFilterContext *ctx)
  300. {
  301. AVFilterLink *outlink = ctx->outputs[0];
  302. JoinContext *s = ctx->priv;
  303. AVFrame *frame;
  304. int linesize = INT_MAX;
  305. int nb_samples = INT_MAX;
  306. int nb_buffers = 0;
  307. int i, j, ret;
  308. for (i = 0; i < ctx->nb_inputs; i++) {
  309. if (!s->input_frames[i])
  310. return 0;
  311. nb_samples = FFMIN(nb_samples, s->input_frames[i]->nb_samples);
  312. }
  313. if (!nb_samples)
  314. return 0;
  315. /* setup the output frame */
  316. frame = av_frame_alloc();
  317. if (!frame)
  318. return AVERROR(ENOMEM);
  319. if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
  320. frame->extended_data = av_mallocz_array(s->nb_channels,
  321. sizeof(*frame->extended_data));
  322. if (!frame->extended_data) {
  323. ret = AVERROR(ENOMEM);
  324. goto fail;
  325. }
  326. }
  327. /* copy the data pointers */
  328. for (i = 0; i < s->nb_channels; i++) {
  329. ChannelMap *ch = &s->channels[i];
  330. AVFrame *cur = s->input_frames[ch->input];
  331. AVBufferRef *buf;
  332. frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
  333. linesize = FFMIN(linesize, cur->linesize[0]);
  334. /* add the buffer where this plan is stored to the list if it's
  335. * not already there */
  336. buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
  337. if (!buf) {
  338. ret = AVERROR(EINVAL);
  339. goto fail;
  340. }
  341. for (j = 0; j < nb_buffers; j++)
  342. if (s->buffers[j]->buffer == buf->buffer)
  343. break;
  344. if (j == i)
  345. s->buffers[nb_buffers++] = buf;
  346. }
  347. /* create references to the buffers we copied to output */
  348. if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
  349. frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
  350. frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
  351. sizeof(*frame->extended_buf));
  352. if (!frame->extended_buf) {
  353. frame->nb_extended_buf = 0;
  354. ret = AVERROR(ENOMEM);
  355. goto fail;
  356. }
  357. }
  358. for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
  359. frame->buf[i] = av_buffer_ref(s->buffers[i]);
  360. if (!frame->buf[i]) {
  361. ret = AVERROR(ENOMEM);
  362. goto fail;
  363. }
  364. }
  365. for (i = 0; i < frame->nb_extended_buf; i++) {
  366. frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
  367. FF_ARRAY_ELEMS(frame->buf)]);
  368. if (!frame->extended_buf[i]) {
  369. ret = AVERROR(ENOMEM);
  370. goto fail;
  371. }
  372. }
  373. frame->nb_samples = nb_samples;
  374. frame->channel_layout = outlink->channel_layout;
  375. frame->channels = outlink->channels;
  376. frame->sample_rate = outlink->sample_rate;
  377. frame->format = outlink->format;
  378. frame->pts = s->input_frames[0]->pts;
  379. frame->linesize[0] = linesize;
  380. if (frame->data != frame->extended_data) {
  381. memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
  382. FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
  383. }
  384. ret = ff_filter_frame(outlink, frame);
  385. for (i = 0; i < ctx->nb_inputs; i++)
  386. av_frame_free(&s->input_frames[i]);
  387. return ret;
  388. fail:
  389. av_frame_free(&frame);
  390. return ret;
  391. }
  392. static int activate(AVFilterContext *ctx)
  393. {
  394. JoinContext *s = ctx->priv;
  395. int i, ret, status;
  396. int nb_samples = 0;
  397. int64_t pts;
  398. FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
  399. if (!s->input_frames[0]) {
  400. ret = ff_inlink_consume_frame(ctx->inputs[0], &s->input_frames[0]);
  401. if (ret < 0) {
  402. return ret;
  403. } else if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
  404. ff_outlink_set_status(ctx->outputs[0], status, pts);
  405. return 0;
  406. } else {
  407. if (ff_outlink_frame_wanted(ctx->outputs[0]) && !s->input_frames[0]) {
  408. ff_inlink_request_frame(ctx->inputs[0]);
  409. return 0;
  410. }
  411. }
  412. if (!s->input_frames[0]) {
  413. return 0;
  414. }
  415. }
  416. nb_samples = s->input_frames[0]->nb_samples;
  417. for (i = 1; i < ctx->nb_inputs && nb_samples > 0; i++) {
  418. if (s->input_frames[i])
  419. continue;
  420. if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
  421. ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->input_frames[i]);
  422. if (ret < 0) {
  423. return ret;
  424. } else if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
  425. ff_outlink_set_status(ctx->outputs[0], status, pts);
  426. return 0;
  427. }
  428. } else {
  429. if (ff_outlink_frame_wanted(ctx->outputs[0])) {
  430. ff_inlink_request_frame(ctx->inputs[i]);
  431. return 0;
  432. }
  433. }
  434. }
  435. return try_push_frame(ctx);
  436. }
  437. static const AVFilterPad avfilter_af_join_outputs[] = {
  438. {
  439. .name = "default",
  440. .type = AVMEDIA_TYPE_AUDIO,
  441. .config_props = join_config_output,
  442. },
  443. { NULL }
  444. };
  445. AVFilter ff_af_join = {
  446. .name = "join",
  447. .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
  448. "multi-channel output."),
  449. .priv_size = sizeof(JoinContext),
  450. .priv_class = &join_class,
  451. .init = join_init,
  452. .uninit = join_uninit,
  453. .activate = activate,
  454. .query_formats = join_query_formats,
  455. .inputs = NULL,
  456. .outputs = avfilter_af_join_outputs,
  457. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  458. };