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.

526 lines
16KB

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