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.

525 lines
16KB

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