You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

534 lines
17KB

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