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.

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