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.

523 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. static const AVOption join_options[] = {
  60. { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A },
  61. { "channel_layout", "Channel layout of the "
  62. "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A },
  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 },
  66. { NULL },
  67. };
  68. static const AVClass join_class = {
  69. .class_name = "join filter",
  70. .item_name = av_default_item_name,
  71. .option = join_options,
  72. .version = LIBAVUTIL_VERSION_INT,
  73. };
  74. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  75. {
  76. AVFilterContext *ctx = link->dst;
  77. JoinContext *s = ctx->priv;
  78. int i;
  79. for (i = 0; i < ctx->nb_inputs; i++)
  80. if (link == ctx->inputs[i])
  81. break;
  82. av_assert0(i < ctx->nb_inputs);
  83. av_assert0(!s->input_frames[i]);
  84. s->input_frames[i] = frame;
  85. return 0;
  86. }
  87. static int parse_maps(AVFilterContext *ctx)
  88. {
  89. JoinContext *s = ctx->priv;
  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, ',');
  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 int join_init(AVFilterContext *ctx, const char *args)
  154. {
  155. JoinContext *s = ctx->priv;
  156. int ret, i;
  157. s->class = &join_class;
  158. av_opt_set_defaults(s);
  159. if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
  160. av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
  161. return ret;
  162. }
  163. if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
  164. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
  165. s->channel_layout_str);
  166. ret = AVERROR(EINVAL);
  167. goto fail;
  168. }
  169. s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
  170. s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
  171. s->buffers = av_mallocz(sizeof(*s->buffers) * s->nb_channels);
  172. s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
  173. if (!s->channels || !s->buffers|| !s->input_frames) {
  174. ret = AVERROR(ENOMEM);
  175. goto fail;
  176. }
  177. for (i = 0; i < s->nb_channels; i++) {
  178. s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
  179. s->channels[i].input = -1;
  180. }
  181. if ((ret = parse_maps(ctx)) < 0)
  182. goto fail;
  183. for (i = 0; i < s->inputs; i++) {
  184. char name[32];
  185. AVFilterPad pad = { 0 };
  186. snprintf(name, sizeof(name), "input%d", i);
  187. pad.type = AVMEDIA_TYPE_AUDIO;
  188. pad.name = av_strdup(name);
  189. pad.filter_frame = filter_frame;
  190. pad.needs_fifo = 1;
  191. ff_insert_inpad(ctx, i, &pad);
  192. }
  193. fail:
  194. av_opt_free(s);
  195. return ret;
  196. }
  197. static void join_uninit(AVFilterContext *ctx)
  198. {
  199. JoinContext *s = ctx->priv;
  200. int i;
  201. for (i = 0; i < ctx->nb_inputs; i++) {
  202. av_freep(&ctx->input_pads[i].name);
  203. av_frame_free(&s->input_frames[i]);
  204. }
  205. av_freep(&s->channels);
  206. av_freep(&s->buffers);
  207. av_freep(&s->input_frames);
  208. }
  209. static int join_query_formats(AVFilterContext *ctx)
  210. {
  211. JoinContext *s = ctx->priv;
  212. AVFilterChannelLayouts *layouts = NULL;
  213. int i;
  214. ff_add_channel_layout(&layouts, s->channel_layout);
  215. ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
  216. for (i = 0; i < ctx->nb_inputs; i++)
  217. ff_channel_layouts_ref(ff_all_channel_layouts(),
  218. &ctx->inputs[i]->out_channel_layouts);
  219. ff_set_common_formats (ctx, ff_planar_sample_fmts());
  220. ff_set_common_samplerates(ctx, ff_all_samplerates());
  221. return 0;
  222. }
  223. static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
  224. uint64_t *inputs)
  225. {
  226. int i;
  227. for (i = 0; i < ctx->nb_inputs; i++) {
  228. AVFilterLink *link = ctx->inputs[i];
  229. if (ch->out_channel & link->channel_layout &&
  230. !(ch->out_channel & inputs[i])) {
  231. ch->input = i;
  232. ch->in_channel = ch->out_channel;
  233. inputs[i] |= ch->out_channel;
  234. return;
  235. }
  236. }
  237. }
  238. static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
  239. uint64_t *inputs)
  240. {
  241. int i;
  242. for (i = 0; i < ctx->nb_inputs; i++) {
  243. AVFilterLink *link = ctx->inputs[i];
  244. if ((inputs[i] & link->channel_layout) != link->channel_layout) {
  245. uint64_t unused = link->channel_layout & ~inputs[i];
  246. ch->input = i;
  247. ch->in_channel = av_channel_layout_extract_channel(unused, 0);
  248. inputs[i] |= ch->in_channel;
  249. return;
  250. }
  251. }
  252. }
  253. static int join_config_output(AVFilterLink *outlink)
  254. {
  255. AVFilterContext *ctx = outlink->src;
  256. JoinContext *s = ctx->priv;
  257. uint64_t *inputs; // nth element tracks which channels are used from nth input
  258. int i, ret = 0;
  259. /* initialize inputs to user-specified mappings */
  260. if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
  261. return AVERROR(ENOMEM);
  262. for (i = 0; i < s->nb_channels; i++) {
  263. ChannelMap *ch = &s->channels[i];
  264. AVFilterLink *inlink;
  265. if (ch->input < 0)
  266. continue;
  267. inlink = ctx->inputs[ch->input];
  268. if (!ch->in_channel)
  269. ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
  270. ch->in_channel_idx);
  271. if (!(ch->in_channel & inlink->channel_layout)) {
  272. av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
  273. "input stream #%d.\n", av_get_channel_name(ch->in_channel),
  274. ch->input);
  275. ret = AVERROR(EINVAL);
  276. goto fail;
  277. }
  278. inputs[ch->input] |= ch->in_channel;
  279. }
  280. /* guess channel maps when not explicitly defined */
  281. /* first try unused matching channels */
  282. for (i = 0; i < s->nb_channels; i++) {
  283. ChannelMap *ch = &s->channels[i];
  284. if (ch->input < 0)
  285. guess_map_matching(ctx, ch, inputs);
  286. }
  287. /* if the above failed, try to find _any_ unused input channel */
  288. for (i = 0; i < s->nb_channels; i++) {
  289. ChannelMap *ch = &s->channels[i];
  290. if (ch->input < 0)
  291. guess_map_any(ctx, ch, inputs);
  292. if (ch->input < 0) {
  293. av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
  294. "output channel '%s'.\n",
  295. av_get_channel_name(ch->out_channel));
  296. goto fail;
  297. }
  298. ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
  299. ch->in_channel);
  300. }
  301. /* print mappings */
  302. av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
  303. for (i = 0; i < s->nb_channels; i++) {
  304. ChannelMap *ch = &s->channels[i];
  305. av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
  306. av_get_channel_name(ch->in_channel),
  307. av_get_channel_name(ch->out_channel));
  308. }
  309. av_log(ctx, AV_LOG_VERBOSE, "\n");
  310. for (i = 0; i < ctx->nb_inputs; i++) {
  311. if (!inputs[i])
  312. av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
  313. "stream %d.\n", i);
  314. }
  315. fail:
  316. av_freep(&inputs);
  317. return ret;
  318. }
  319. static int join_request_frame(AVFilterLink *outlink)
  320. {
  321. AVFilterContext *ctx = outlink->src;
  322. JoinContext *s = ctx->priv;
  323. AVFrame *frame;
  324. int linesize = INT_MAX;
  325. int nb_samples = 0;
  326. int nb_buffers = 0;
  327. int i, j, ret;
  328. /* get a frame on each input */
  329. for (i = 0; i < ctx->nb_inputs; i++) {
  330. AVFilterLink *inlink = ctx->inputs[i];
  331. if (!s->input_frames[i] &&
  332. (ret = ff_request_frame(inlink)) < 0)
  333. return ret;
  334. /* request the same number of samples on all inputs */
  335. if (i == 0) {
  336. nb_samples = s->input_frames[0]->nb_samples;
  337. for (j = 1; !i && j < ctx->nb_inputs; j++)
  338. ctx->inputs[j]->request_samples = nb_samples;
  339. }
  340. }
  341. /* setup the output frame */
  342. frame = av_frame_alloc();
  343. if (!frame)
  344. return AVERROR(ENOMEM);
  345. if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
  346. frame->extended_data = av_mallocz(s->nb_channels *
  347. sizeof(*frame->extended_data));
  348. if (!frame->extended_data) {
  349. ret = AVERROR(ENOMEM);
  350. goto fail;
  351. }
  352. }
  353. /* copy the data pointers */
  354. for (i = 0; i < s->nb_channels; i++) {
  355. ChannelMap *ch = &s->channels[i];
  356. AVFrame *cur = s->input_frames[ch->input];
  357. AVBufferRef *buf;
  358. frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
  359. linesize = FFMIN(linesize, cur->linesize[0]);
  360. /* add the buffer where this plan is stored to the list if it's
  361. * not already there */
  362. buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
  363. if (!buf) {
  364. ret = AVERROR(EINVAL);
  365. goto fail;
  366. }
  367. for (j = 0; j < nb_buffers; j++)
  368. if (s->buffers[j]->buffer == buf->buffer)
  369. break;
  370. if (j == i)
  371. s->buffers[nb_buffers++] = buf;
  372. }
  373. /* create references to the buffers we copied to output */
  374. if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
  375. frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
  376. frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
  377. frame->nb_extended_buf);
  378. if (!frame->extended_buf) {
  379. frame->nb_extended_buf = 0;
  380. ret = AVERROR(ENOMEM);
  381. goto fail;
  382. }
  383. }
  384. for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
  385. frame->buf[i] = av_buffer_ref(s->buffers[i]);
  386. if (!frame->buf[i]) {
  387. ret = AVERROR(ENOMEM);
  388. goto fail;
  389. }
  390. }
  391. for (i = 0; i < frame->nb_extended_buf; i++) {
  392. frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
  393. FF_ARRAY_ELEMS(frame->buf)]);
  394. if (!frame->extended_buf[i]) {
  395. ret = AVERROR(ENOMEM);
  396. goto fail;
  397. }
  398. }
  399. frame->nb_samples = nb_samples;
  400. frame->channel_layout = outlink->channel_layout;
  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. .init = join_init,
  431. .uninit = join_uninit,
  432. .query_formats = join_query_formats,
  433. .inputs = NULL,
  434. .outputs = avfilter_af_join_outputs,
  435. };