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.

502 lines
15KB

  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/audioconvert.h"
  27. #include "libavutil/avassert.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. AVFilterBufferRef **input_frames;
  51. /**
  52. * Temporary storage for data pointers, for assembling the output buffer.
  53. */
  54. uint8_t **data;
  55. } JoinContext;
  56. /**
  57. * To avoid copying the data from input buffers, this filter creates
  58. * a custom output buffer that stores references to all inputs and
  59. * unrefs them on free.
  60. */
  61. typedef struct JoinBufferPriv {
  62. AVFilterBufferRef **in_buffers;
  63. int nb_in_buffers;
  64. } JoinBufferPriv;
  65. #define OFFSET(x) offsetof(JoinContext, x)
  66. #define A AV_OPT_FLAG_AUDIO_PARAM
  67. static const AVOption join_options[] = {
  68. { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { 2 }, 1, INT_MAX, A },
  69. { "channel_layout", "Channel layout of the "
  70. "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A },
  71. { "map", "A comma-separated list of channels maps in the format "
  72. "'input_stream.input_channel-output_channel.",
  73. OFFSET(map), AV_OPT_TYPE_STRING, .flags = A },
  74. { NULL },
  75. };
  76. static const AVClass join_class = {
  77. .class_name = "join filter",
  78. .item_name = av_default_item_name,
  79. .option = join_options,
  80. .version = LIBAVUTIL_VERSION_INT,
  81. };
  82. static int filter_samples(AVFilterLink *link, AVFilterBufferRef *buf)
  83. {
  84. AVFilterContext *ctx = link->dst;
  85. JoinContext *s = ctx->priv;
  86. int i;
  87. for (i = 0; i < ctx->nb_inputs; i++)
  88. if (link == ctx->inputs[i])
  89. break;
  90. av_assert0(i < ctx->nb_inputs);
  91. av_assert0(!s->input_frames[i]);
  92. s->input_frames[i] = buf;
  93. return 0;
  94. }
  95. static int parse_maps(AVFilterContext *ctx)
  96. {
  97. JoinContext *s = ctx->priv;
  98. char *cur = s->map;
  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, ',');
  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 int join_init(AVFilterContext *ctx, const char *args)
  162. {
  163. JoinContext *s = ctx->priv;
  164. int ret, i;
  165. s->class = &join_class;
  166. av_opt_set_defaults(s);
  167. if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
  168. return ret;
  169. if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
  170. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
  171. s->channel_layout_str);
  172. ret = AVERROR(EINVAL);
  173. goto fail;
  174. }
  175. s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
  176. s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
  177. s->data = av_mallocz(sizeof(*s->data) * s->nb_channels);
  178. s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
  179. if (!s->channels || !s->data || !s->input_frames) {
  180. ret = AVERROR(ENOMEM);
  181. goto fail;
  182. }
  183. for (i = 0; i < s->nb_channels; i++) {
  184. s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
  185. s->channels[i].input = -1;
  186. }
  187. if ((ret = parse_maps(ctx)) < 0)
  188. goto fail;
  189. for (i = 0; i < s->inputs; i++) {
  190. char name[32];
  191. AVFilterPad pad = { 0 };
  192. snprintf(name, sizeof(name), "input%d", i);
  193. pad.type = AVMEDIA_TYPE_AUDIO;
  194. pad.name = av_strdup(name);
  195. pad.filter_samples = filter_samples;
  196. pad.needs_fifo = 1;
  197. ff_insert_inpad(ctx, i, &pad);
  198. }
  199. fail:
  200. av_opt_free(s);
  201. return ret;
  202. }
  203. static void join_uninit(AVFilterContext *ctx)
  204. {
  205. JoinContext *s = ctx->priv;
  206. int i;
  207. for (i = 0; i < ctx->nb_inputs; i++) {
  208. av_freep(&ctx->input_pads[i].name);
  209. avfilter_unref_bufferp(&s->input_frames[i]);
  210. }
  211. av_freep(&s->channels);
  212. av_freep(&s->data);
  213. av_freep(&s->input_frames);
  214. }
  215. static int join_query_formats(AVFilterContext *ctx)
  216. {
  217. JoinContext *s = ctx->priv;
  218. AVFilterChannelLayouts *layouts = NULL;
  219. int i;
  220. ff_add_channel_layout(&layouts, s->channel_layout);
  221. ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
  222. for (i = 0; i < ctx->nb_inputs; i++)
  223. ff_channel_layouts_ref(ff_all_channel_layouts(),
  224. &ctx->inputs[i]->out_channel_layouts);
  225. ff_set_common_formats (ctx, ff_planar_sample_fmts());
  226. ff_set_common_samplerates(ctx, ff_all_samplerates());
  227. return 0;
  228. }
  229. static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
  230. uint64_t *inputs)
  231. {
  232. int i;
  233. for (i = 0; i < ctx->nb_inputs; i++) {
  234. AVFilterLink *link = ctx->inputs[i];
  235. if (ch->out_channel & link->channel_layout &&
  236. !(ch->out_channel & inputs[i])) {
  237. ch->input = i;
  238. ch->in_channel = ch->out_channel;
  239. inputs[i] |= ch->out_channel;
  240. return;
  241. }
  242. }
  243. }
  244. static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
  245. uint64_t *inputs)
  246. {
  247. int i;
  248. for (i = 0; i < ctx->nb_inputs; i++) {
  249. AVFilterLink *link = ctx->inputs[i];
  250. if ((inputs[i] & link->channel_layout) != link->channel_layout) {
  251. uint64_t unused = link->channel_layout & ~inputs[i];
  252. ch->input = i;
  253. ch->in_channel = av_channel_layout_extract_channel(unused, 0);
  254. inputs[i] |= ch->in_channel;
  255. return;
  256. }
  257. }
  258. }
  259. static int join_config_output(AVFilterLink *outlink)
  260. {
  261. AVFilterContext *ctx = outlink->src;
  262. JoinContext *s = ctx->priv;
  263. uint64_t *inputs; // nth element tracks which channels are used from nth input
  264. int i, ret = 0;
  265. /* initialize inputs to user-specified mappings */
  266. if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
  267. return AVERROR(ENOMEM);
  268. for (i = 0; i < s->nb_channels; i++) {
  269. ChannelMap *ch = &s->channels[i];
  270. AVFilterLink *inlink;
  271. if (ch->input < 0)
  272. continue;
  273. inlink = ctx->inputs[ch->input];
  274. if (!ch->in_channel)
  275. ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
  276. ch->in_channel_idx);
  277. if (!(ch->in_channel & inlink->channel_layout)) {
  278. av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
  279. "input stream #%d.\n", av_get_channel_name(ch->in_channel),
  280. ch->input);
  281. ret = AVERROR(EINVAL);
  282. goto fail;
  283. }
  284. inputs[ch->input] |= ch->in_channel;
  285. }
  286. /* guess channel maps when not explicitly defined */
  287. /* first try unused matching channels */
  288. for (i = 0; i < s->nb_channels; i++) {
  289. ChannelMap *ch = &s->channels[i];
  290. if (ch->input < 0)
  291. guess_map_matching(ctx, ch, inputs);
  292. }
  293. /* if the above failed, try to find _any_ unused input channel */
  294. for (i = 0; i < s->nb_channels; i++) {
  295. ChannelMap *ch = &s->channels[i];
  296. if (ch->input < 0)
  297. guess_map_any(ctx, ch, inputs);
  298. if (ch->input < 0) {
  299. av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
  300. "output channel '%s'.\n",
  301. av_get_channel_name(ch->out_channel));
  302. goto fail;
  303. }
  304. ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
  305. ch->in_channel);
  306. }
  307. /* print mappings */
  308. av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
  309. for (i = 0; i < s->nb_channels; i++) {
  310. ChannelMap *ch = &s->channels[i];
  311. av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
  312. av_get_channel_name(ch->in_channel),
  313. av_get_channel_name(ch->out_channel));
  314. }
  315. av_log(ctx, AV_LOG_VERBOSE, "\n");
  316. for (i = 0; i < ctx->nb_inputs; i++) {
  317. if (!inputs[i])
  318. av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
  319. "stream %d.\n", i);
  320. }
  321. fail:
  322. av_freep(&inputs);
  323. return ret;
  324. }
  325. static void join_free_buffer(AVFilterBuffer *buf)
  326. {
  327. JoinBufferPriv *priv = buf->priv;
  328. if (priv) {
  329. int i;
  330. for (i = 0; i < priv->nb_in_buffers; i++)
  331. avfilter_unref_bufferp(&priv->in_buffers[i]);
  332. av_freep(&priv->in_buffers);
  333. av_freep(&buf->priv);
  334. }
  335. if (buf->extended_data != buf->data)
  336. av_freep(&buf->extended_data);
  337. av_freep(&buf);
  338. }
  339. static int join_request_frame(AVFilterLink *outlink)
  340. {
  341. AVFilterContext *ctx = outlink->src;
  342. JoinContext *s = ctx->priv;
  343. AVFilterBufferRef *buf;
  344. JoinBufferPriv *priv;
  345. int linesize = INT_MAX;
  346. int perms = ~0;
  347. int nb_samples = 0;
  348. int i, j, ret;
  349. /* get a frame on each input */
  350. for (i = 0; i < ctx->nb_inputs; i++) {
  351. AVFilterLink *inlink = ctx->inputs[i];
  352. if (!s->input_frames[i] &&
  353. (ret = ff_request_frame(inlink)) < 0)
  354. return ret;
  355. /* request the same number of samples on all inputs */
  356. if (i == 0) {
  357. nb_samples = s->input_frames[0]->audio->nb_samples;
  358. for (j = 1; !i && j < ctx->nb_inputs; j++)
  359. ctx->inputs[j]->request_samples = nb_samples;
  360. }
  361. }
  362. for (i = 0; i < s->nb_channels; i++) {
  363. ChannelMap *ch = &s->channels[i];
  364. AVFilterBufferRef *cur_buf = s->input_frames[ch->input];
  365. s->data[i] = cur_buf->extended_data[ch->in_channel_idx];
  366. linesize = FFMIN(linesize, cur_buf->linesize[0]);
  367. perms &= cur_buf->perms;
  368. }
  369. av_assert0(nb_samples > 0);
  370. buf = avfilter_get_audio_buffer_ref_from_arrays(s->data, linesize, perms,
  371. nb_samples, outlink->format,
  372. outlink->channel_layout);
  373. if (!buf)
  374. return AVERROR(ENOMEM);
  375. buf->buf->free = join_free_buffer;
  376. buf->pts = s->input_frames[0]->pts;
  377. if (!(priv = av_mallocz(sizeof(*priv))))
  378. goto fail;
  379. if (!(priv->in_buffers = av_mallocz(sizeof(*priv->in_buffers) * ctx->nb_inputs)))
  380. goto fail;
  381. for (i = 0; i < ctx->nb_inputs; i++)
  382. priv->in_buffers[i] = s->input_frames[i];
  383. priv->nb_in_buffers = ctx->nb_inputs;
  384. buf->buf->priv = priv;
  385. ret = ff_filter_samples(outlink, buf);
  386. memset(s->input_frames, 0, sizeof(*s->input_frames) * ctx->nb_inputs);
  387. return ret;
  388. fail:
  389. avfilter_unref_buffer(buf);
  390. if (priv)
  391. av_freep(&priv->in_buffers);
  392. av_freep(&priv);
  393. return AVERROR(ENOMEM);
  394. }
  395. AVFilter avfilter_af_join = {
  396. .name = "join",
  397. .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
  398. "multi-channel output"),
  399. .priv_size = sizeof(JoinContext),
  400. .init = join_init,
  401. .uninit = join_uninit,
  402. .query_formats = join_query_formats,
  403. .inputs = (const AVFilterPad[]){{ NULL }},
  404. .outputs = (const AVFilterPad[]){{ .name = "default",
  405. .type = AVMEDIA_TYPE_AUDIO,
  406. .config_props = join_config_output,
  407. .request_frame = join_request_frame, },
  408. { NULL }},
  409. };