af_join.c 16 KB

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