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.

517 lines
16KB

  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. pad.filter_frame = filter_frame;
  184. pad.needs_fifo = 1;
  185. ff_insert_inpad(ctx, i, &pad);
  186. }
  187. return 0;
  188. }
  189. static av_cold void join_uninit(AVFilterContext *ctx)
  190. {
  191. JoinContext *s = ctx->priv;
  192. int i;
  193. for (i = 0; i < ctx->nb_inputs; i++) {
  194. av_freep(&ctx->input_pads[i].name);
  195. av_frame_free(&s->input_frames[i]);
  196. }
  197. av_freep(&s->channels);
  198. av_freep(&s->buffers);
  199. av_freep(&s->input_frames);
  200. }
  201. static int join_query_formats(AVFilterContext *ctx)
  202. {
  203. JoinContext *s = ctx->priv;
  204. AVFilterChannelLayouts *layouts = NULL;
  205. int i;
  206. ff_add_channel_layout(&layouts, s->channel_layout);
  207. ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
  208. for (i = 0; i < ctx->nb_inputs; i++)
  209. ff_channel_layouts_ref(ff_all_channel_layouts(),
  210. &ctx->inputs[i]->out_channel_layouts);
  211. ff_set_common_formats (ctx, ff_planar_sample_fmts());
  212. ff_set_common_samplerates(ctx, ff_all_samplerates());
  213. return 0;
  214. }
  215. static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
  216. uint64_t *inputs)
  217. {
  218. int i;
  219. for (i = 0; i < ctx->nb_inputs; i++) {
  220. AVFilterLink *link = ctx->inputs[i];
  221. if (ch->out_channel & link->channel_layout &&
  222. !(ch->out_channel & inputs[i])) {
  223. ch->input = i;
  224. ch->in_channel = ch->out_channel;
  225. inputs[i] |= ch->out_channel;
  226. return;
  227. }
  228. }
  229. }
  230. static void guess_map_any(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 ((inputs[i] & link->channel_layout) != link->channel_layout) {
  237. uint64_t unused = link->channel_layout & ~inputs[i];
  238. ch->input = i;
  239. ch->in_channel = av_channel_layout_extract_channel(unused, 0);
  240. inputs[i] |= ch->in_channel;
  241. return;
  242. }
  243. }
  244. }
  245. static int join_config_output(AVFilterLink *outlink)
  246. {
  247. AVFilterContext *ctx = outlink->src;
  248. JoinContext *s = ctx->priv;
  249. uint64_t *inputs; // nth element tracks which channels are used from nth input
  250. int i, ret = 0;
  251. /* initialize inputs to user-specified mappings */
  252. if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
  253. return AVERROR(ENOMEM);
  254. for (i = 0; i < s->nb_channels; i++) {
  255. ChannelMap *ch = &s->channels[i];
  256. AVFilterLink *inlink;
  257. if (ch->input < 0)
  258. continue;
  259. inlink = ctx->inputs[ch->input];
  260. if (!ch->in_channel)
  261. ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
  262. ch->in_channel_idx);
  263. if (!(ch->in_channel & inlink->channel_layout)) {
  264. av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
  265. "input stream #%d.\n", av_get_channel_name(ch->in_channel),
  266. ch->input);
  267. ret = AVERROR(EINVAL);
  268. goto fail;
  269. }
  270. inputs[ch->input] |= ch->in_channel;
  271. }
  272. /* guess channel maps when not explicitly defined */
  273. /* first try unused matching channels */
  274. for (i = 0; i < s->nb_channels; i++) {
  275. ChannelMap *ch = &s->channels[i];
  276. if (ch->input < 0)
  277. guess_map_matching(ctx, ch, inputs);
  278. }
  279. /* if the above failed, try to find _any_ unused input channel */
  280. for (i = 0; i < s->nb_channels; i++) {
  281. ChannelMap *ch = &s->channels[i];
  282. if (ch->input < 0)
  283. guess_map_any(ctx, ch, inputs);
  284. if (ch->input < 0) {
  285. av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
  286. "output channel '%s'.\n",
  287. av_get_channel_name(ch->out_channel));
  288. goto fail;
  289. }
  290. ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
  291. ch->in_channel);
  292. }
  293. /* print mappings */
  294. av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
  295. for (i = 0; i < s->nb_channels; i++) {
  296. ChannelMap *ch = &s->channels[i];
  297. av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
  298. av_get_channel_name(ch->in_channel),
  299. av_get_channel_name(ch->out_channel));
  300. }
  301. av_log(ctx, AV_LOG_VERBOSE, "\n");
  302. for (i = 0; i < ctx->nb_inputs; i++) {
  303. if (!inputs[i])
  304. av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
  305. "stream %d.\n", i);
  306. }
  307. fail:
  308. av_freep(&inputs);
  309. return ret;
  310. }
  311. static int join_request_frame(AVFilterLink *outlink)
  312. {
  313. AVFilterContext *ctx = outlink->src;
  314. JoinContext *s = ctx->priv;
  315. AVFrame *frame;
  316. int linesize = INT_MAX;
  317. int nb_samples = 0;
  318. int nb_buffers = 0;
  319. int i, j, ret;
  320. /* get a frame on each input */
  321. for (i = 0; i < ctx->nb_inputs; i++) {
  322. AVFilterLink *inlink = ctx->inputs[i];
  323. if (!s->input_frames[i] &&
  324. (ret = ff_request_frame(inlink)) < 0)
  325. return ret;
  326. /* request the same number of samples on all inputs */
  327. if (i == 0) {
  328. nb_samples = s->input_frames[0]->nb_samples;
  329. for (j = 1; !i && j < ctx->nb_inputs; j++)
  330. ctx->inputs[j]->request_samples = nb_samples;
  331. }
  332. }
  333. /* setup the output frame */
  334. frame = av_frame_alloc();
  335. if (!frame)
  336. return AVERROR(ENOMEM);
  337. if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
  338. frame->extended_data = av_mallocz(s->nb_channels *
  339. sizeof(*frame->extended_data));
  340. if (!frame->extended_data) {
  341. ret = AVERROR(ENOMEM);
  342. goto fail;
  343. }
  344. }
  345. /* copy the data pointers */
  346. for (i = 0; i < s->nb_channels; i++) {
  347. ChannelMap *ch = &s->channels[i];
  348. AVFrame *cur = s->input_frames[ch->input];
  349. AVBufferRef *buf;
  350. frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
  351. linesize = FFMIN(linesize, cur->linesize[0]);
  352. /* add the buffer where this plan is stored to the list if it's
  353. * not already there */
  354. buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
  355. if (!buf) {
  356. ret = AVERROR(EINVAL);
  357. goto fail;
  358. }
  359. for (j = 0; j < nb_buffers; j++)
  360. if (s->buffers[j]->buffer == buf->buffer)
  361. break;
  362. if (j == i)
  363. s->buffers[nb_buffers++] = buf;
  364. }
  365. /* create references to the buffers we copied to output */
  366. if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
  367. frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
  368. frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
  369. frame->nb_extended_buf);
  370. if (!frame->extended_buf) {
  371. frame->nb_extended_buf = 0;
  372. ret = AVERROR(ENOMEM);
  373. goto fail;
  374. }
  375. }
  376. for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
  377. frame->buf[i] = av_buffer_ref(s->buffers[i]);
  378. if (!frame->buf[i]) {
  379. ret = AVERROR(ENOMEM);
  380. goto fail;
  381. }
  382. }
  383. for (i = 0; i < frame->nb_extended_buf; i++) {
  384. frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
  385. FF_ARRAY_ELEMS(frame->buf)]);
  386. if (!frame->extended_buf[i]) {
  387. ret = AVERROR(ENOMEM);
  388. goto fail;
  389. }
  390. }
  391. frame->nb_samples = nb_samples;
  392. frame->channel_layout = outlink->channel_layout;
  393. av_frame_set_channels(frame, outlink->channels);
  394. frame->format = outlink->format;
  395. frame->sample_rate = outlink->sample_rate;
  396. frame->pts = s->input_frames[0]->pts;
  397. frame->linesize[0] = linesize;
  398. if (frame->data != frame->extended_data) {
  399. memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
  400. FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
  401. }
  402. ret = ff_filter_frame(outlink, frame);
  403. for (i = 0; i < ctx->nb_inputs; i++)
  404. av_frame_free(&s->input_frames[i]);
  405. return ret;
  406. fail:
  407. av_frame_free(&frame);
  408. return ret;
  409. }
  410. static const AVFilterPad avfilter_af_join_outputs[] = {
  411. {
  412. .name = "default",
  413. .type = AVMEDIA_TYPE_AUDIO,
  414. .config_props = join_config_output,
  415. .request_frame = join_request_frame,
  416. },
  417. { NULL }
  418. };
  419. AVFilter ff_af_join = {
  420. .name = "join",
  421. .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
  422. "multi-channel output."),
  423. .priv_size = sizeof(JoinContext),
  424. .priv_class = &join_class,
  425. .init = join_init,
  426. .uninit = join_uninit,
  427. .query_formats = join_query_formats,
  428. .inputs = NULL,
  429. .outputs = avfilter_af_join_outputs,
  430. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  431. };