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.

520 lines
16KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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. static const AVOption join_options[] = {
  59. { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A },
  60. { "channel_layout", "Channel layout of the "
  61. "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A },
  62. { "map", "A comma-separated list of channels maps in the format "
  63. "'input_stream.input_channel-output_channel.",
  64. OFFSET(map), AV_OPT_TYPE_STRING, .flags = A },
  65. { NULL },
  66. };
  67. static const AVClass join_class = {
  68. .class_name = "join filter",
  69. .item_name = av_default_item_name,
  70. .option = join_options,
  71. .version = LIBAVUTIL_VERSION_INT,
  72. };
  73. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  74. {
  75. AVFilterContext *ctx = link->dst;
  76. JoinContext *s = ctx->priv;
  77. int i;
  78. for (i = 0; i < ctx->nb_inputs; i++)
  79. if (link == ctx->inputs[i])
  80. break;
  81. av_assert0(i < ctx->nb_inputs);
  82. av_assert0(!s->input_frames[i]);
  83. s->input_frames[i] = frame;
  84. return 0;
  85. }
  86. static int parse_maps(AVFilterContext *ctx)
  87. {
  88. JoinContext *s = ctx->priv;
  89. char separator = '|';
  90. char *cur = s->map;
  91. while (cur && *cur) {
  92. char *sep, *next, *p;
  93. uint64_t in_channel = 0, out_channel = 0;
  94. int input_idx, out_ch_idx, in_ch_idx;
  95. next = strchr(cur, separator);
  96. if (next)
  97. *next++ = 0;
  98. /* split the map into input and output parts */
  99. if (!(sep = strchr(cur, '-'))) {
  100. av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
  101. "map '%s'\n", cur);
  102. return AVERROR(EINVAL);
  103. }
  104. *sep++ = 0;
  105. #define PARSE_CHANNEL(str, var, inout) \
  106. if (!(var = av_get_channel_layout(str))) { \
  107. av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
  108. return AVERROR(EINVAL); \
  109. } \
  110. if (av_get_channel_layout_nb_channels(var) != 1) { \
  111. av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \
  112. inout " channel.\n"); \
  113. return AVERROR(EINVAL); \
  114. }
  115. /* parse output channel */
  116. PARSE_CHANNEL(sep, out_channel, "output");
  117. if (!(out_channel & s->channel_layout)) {
  118. av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
  119. "requested channel layout.\n", sep);
  120. return AVERROR(EINVAL);
  121. }
  122. out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
  123. out_channel);
  124. if (s->channels[out_ch_idx].input >= 0) {
  125. av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
  126. "'%s'.\n", sep);
  127. return AVERROR(EINVAL);
  128. }
  129. /* parse input channel */
  130. input_idx = strtol(cur, &cur, 0);
  131. if (input_idx < 0 || input_idx >= s->inputs) {
  132. av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
  133. input_idx);
  134. return AVERROR(EINVAL);
  135. }
  136. if (*cur)
  137. cur++;
  138. in_ch_idx = strtol(cur, &p, 0);
  139. if (p == cur) {
  140. /* channel specifier is not a number,
  141. * try to parse as channel name */
  142. PARSE_CHANNEL(cur, in_channel, "input");
  143. }
  144. s->channels[out_ch_idx].input = input_idx;
  145. if (in_channel)
  146. s->channels[out_ch_idx].in_channel = in_channel;
  147. else
  148. s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
  149. cur = next;
  150. }
  151. return 0;
  152. }
  153. static av_cold int join_init(AVFilterContext *ctx)
  154. {
  155. JoinContext *s = ctx->priv;
  156. int ret, i;
  157. if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
  158. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
  159. s->channel_layout_str);
  160. ret = AVERROR(EINVAL);
  161. goto fail;
  162. }
  163. s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
  164. s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
  165. s->buffers = av_mallocz(sizeof(*s->buffers) * s->nb_channels);
  166. s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
  167. if (!s->channels || !s->buffers|| !s->input_frames) {
  168. ret = AVERROR(ENOMEM);
  169. goto fail;
  170. }
  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. goto fail;
  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. fail:
  188. av_opt_free(s);
  189. return ret;
  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. frame->sample_rate = outlink->sample_rate;
  396. frame->format = outlink->format;
  397. frame->pts = s->input_frames[0]->pts;
  398. frame->linesize[0] = linesize;
  399. if (frame->data != frame->extended_data) {
  400. memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
  401. FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
  402. }
  403. ret = ff_filter_frame(outlink, frame);
  404. for (i = 0; i < ctx->nb_inputs; i++)
  405. av_frame_free(&s->input_frames[i]);
  406. return ret;
  407. fail:
  408. av_frame_free(&frame);
  409. return ret;
  410. }
  411. static const AVFilterPad avfilter_af_join_outputs[] = {
  412. {
  413. .name = "default",
  414. .type = AVMEDIA_TYPE_AUDIO,
  415. .config_props = join_config_output,
  416. .request_frame = join_request_frame,
  417. },
  418. { NULL }
  419. };
  420. AVFilter ff_af_join = {
  421. .name = "join",
  422. .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
  423. "multi-channel output"),
  424. .priv_size = sizeof(JoinContext),
  425. .priv_class = &join_class,
  426. .init = join_init,
  427. .uninit = join_uninit,
  428. .query_formats = join_query_formats,
  429. .inputs = NULL,
  430. .outputs = avfilter_af_join_outputs,
  431. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  432. };