af_amerge.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2011 Nicolas George <nicolas.george@normalesup.org>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Audio merging filter
  23. */
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/bprint.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/opt.h"
  28. #include "libswresample/swresample.h" // only for SWR_CH_MAX
  29. #include "avfilter.h"
  30. #include "audio.h"
  31. #include "bufferqueue.h"
  32. #include "internal.h"
  33. typedef struct {
  34. const AVClass *class;
  35. int nb_inputs;
  36. int route[SWR_CH_MAX]; /**< channels routing, see copy_samples */
  37. int bps;
  38. struct amerge_input {
  39. struct FFBufQueue queue;
  40. int nb_ch; /**< number of channels for the input */
  41. int nb_samples;
  42. int pos;
  43. } *in;
  44. } AMergeContext;
  45. #define OFFSET(x) offsetof(AMergeContext, x)
  46. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  47. static const AVOption amerge_options[] = {
  48. { "inputs", "specify the number of inputs", OFFSET(nb_inputs),
  49. AV_OPT_TYPE_INT, { .i64 = 2 }, 2, SWR_CH_MAX, FLAGS },
  50. { NULL }
  51. };
  52. AVFILTER_DEFINE_CLASS(amerge);
  53. static av_cold void uninit(AVFilterContext *ctx)
  54. {
  55. AMergeContext *am = ctx->priv;
  56. int i;
  57. for (i = 0; i < am->nb_inputs; i++) {
  58. if (am->in)
  59. ff_bufqueue_discard_all(&am->in[i].queue);
  60. if (ctx->input_pads)
  61. av_freep(&ctx->input_pads[i].name);
  62. }
  63. av_freep(&am->in);
  64. }
  65. static int query_formats(AVFilterContext *ctx)
  66. {
  67. AMergeContext *am = ctx->priv;
  68. int64_t inlayout[SWR_CH_MAX], outlayout = 0;
  69. AVFilterFormats *formats;
  70. AVFilterChannelLayouts *layouts;
  71. int i, overlap = 0, nb_ch = 0;
  72. for (i = 0; i < am->nb_inputs; i++) {
  73. if (!ctx->inputs[i]->in_channel_layouts ||
  74. !ctx->inputs[i]->in_channel_layouts->nb_channel_layouts) {
  75. av_log(ctx, AV_LOG_WARNING,
  76. "No channel layout for input %d\n", i + 1);
  77. return AVERROR(EAGAIN);
  78. }
  79. inlayout[i] = ctx->inputs[i]->in_channel_layouts->channel_layouts[0];
  80. if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
  81. char buf[256];
  82. av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
  83. av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
  84. }
  85. am->in[i].nb_ch = av_get_channel_layout_nb_channels(inlayout[i]);
  86. if (outlayout & inlayout[i])
  87. overlap++;
  88. outlayout |= inlayout[i];
  89. nb_ch += am->in[i].nb_ch;
  90. }
  91. if (nb_ch > SWR_CH_MAX) {
  92. av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
  93. return AVERROR(EINVAL);
  94. }
  95. if (overlap) {
  96. av_log(ctx, AV_LOG_WARNING,
  97. "Input channel layouts overlap: "
  98. "output layout will be determined by the number of distinct input channels\n");
  99. for (i = 0; i < nb_ch; i++)
  100. am->route[i] = i;
  101. outlayout = av_get_default_channel_layout(nb_ch);
  102. if (!outlayout)
  103. outlayout = ((int64_t)1 << nb_ch) - 1;
  104. } else {
  105. int *route[SWR_CH_MAX];
  106. int c, out_ch_number = 0;
  107. route[0] = am->route;
  108. for (i = 1; i < am->nb_inputs; i++)
  109. route[i] = route[i - 1] + am->in[i - 1].nb_ch;
  110. for (c = 0; c < 64; c++)
  111. for (i = 0; i < am->nb_inputs; i++)
  112. if ((inlayout[i] >> c) & 1)
  113. *(route[i]++) = out_ch_number++;
  114. }
  115. formats = ff_make_format_list(ff_packed_sample_fmts_array);
  116. ff_set_common_formats(ctx, formats);
  117. for (i = 0; i < am->nb_inputs; i++) {
  118. layouts = NULL;
  119. ff_add_channel_layout(&layouts, inlayout[i]);
  120. ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
  121. }
  122. layouts = NULL;
  123. ff_add_channel_layout(&layouts, outlayout);
  124. ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
  125. ff_set_common_samplerates(ctx, ff_all_samplerates());
  126. return 0;
  127. }
  128. static int config_output(AVFilterLink *outlink)
  129. {
  130. AVFilterContext *ctx = outlink->src;
  131. AMergeContext *am = ctx->priv;
  132. AVBPrint bp;
  133. int i;
  134. for (i = 1; i < am->nb_inputs; i++) {
  135. if (ctx->inputs[i]->sample_rate != ctx->inputs[0]->sample_rate) {
  136. av_log(ctx, AV_LOG_ERROR,
  137. "Inputs must have the same sample rate "
  138. "%d for in%d vs %d\n",
  139. ctx->inputs[i]->sample_rate, i, ctx->inputs[0]->sample_rate);
  140. return AVERROR(EINVAL);
  141. }
  142. }
  143. am->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
  144. outlink->sample_rate = ctx->inputs[0]->sample_rate;
  145. outlink->time_base = ctx->inputs[0]->time_base;
  146. av_bprint_init(&bp, 0, 1);
  147. for (i = 0; i < am->nb_inputs; i++) {
  148. av_bprintf(&bp, "%sin%d:", i ? " + " : "", i);
  149. av_bprint_channel_layout(&bp, -1, ctx->inputs[i]->channel_layout);
  150. }
  151. av_bprintf(&bp, " -> out:");
  152. av_bprint_channel_layout(&bp, -1, ctx->outputs[0]->channel_layout);
  153. av_log(ctx, AV_LOG_VERBOSE, "%s\n", bp.str);
  154. return 0;
  155. }
  156. static int request_frame(AVFilterLink *outlink)
  157. {
  158. AVFilterContext *ctx = outlink->src;
  159. AMergeContext *am = ctx->priv;
  160. int i, ret;
  161. for (i = 0; i < am->nb_inputs; i++)
  162. if (!am->in[i].nb_samples)
  163. if ((ret = ff_request_frame(ctx->inputs[i])) < 0)
  164. return ret;
  165. return 0;
  166. }
  167. /**
  168. * Copy samples from several input streams to one output stream.
  169. * @param nb_inputs number of inputs
  170. * @param in inputs; used only for the nb_ch field;
  171. * @param route routing values;
  172. * input channel i goes to output channel route[i];
  173. * i < in[0].nb_ch are the channels from the first output;
  174. * i >= in[0].nb_ch are the channels from the second output
  175. * @param ins pointer to the samples of each inputs, in packed format;
  176. * will be left at the end of the copied samples
  177. * @param outs pointer to the samples of the output, in packet format;
  178. * must point to a buffer big enough;
  179. * will be left at the end of the copied samples
  180. * @param ns number of samples to copy
  181. * @param bps bytes per sample
  182. */
  183. static inline void copy_samples(int nb_inputs, struct amerge_input in[],
  184. int *route, uint8_t *ins[],
  185. uint8_t **outs, int ns, int bps)
  186. {
  187. int *route_cur;
  188. int i, c, nb_ch = 0;
  189. for (i = 0; i < nb_inputs; i++)
  190. nb_ch += in[i].nb_ch;
  191. while (ns--) {
  192. route_cur = route;
  193. for (i = 0; i < nb_inputs; i++) {
  194. for (c = 0; c < in[i].nb_ch; c++) {
  195. memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
  196. ins[i] += bps;
  197. }
  198. }
  199. *outs += nb_ch * bps;
  200. }
  201. }
  202. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  203. {
  204. AVFilterContext *ctx = inlink->dst;
  205. AMergeContext *am = ctx->priv;
  206. AVFilterLink *const outlink = ctx->outputs[0];
  207. int input_number;
  208. int nb_samples, ns, i;
  209. AVFrame *outbuf, *inbuf[SWR_CH_MAX];
  210. uint8_t *ins[SWR_CH_MAX], *outs;
  211. for (input_number = 0; input_number < am->nb_inputs; input_number++)
  212. if (inlink == ctx->inputs[input_number])
  213. break;
  214. av_assert1(input_number < am->nb_inputs);
  215. if (ff_bufqueue_is_full(&am->in[input_number].queue)) {
  216. av_frame_free(&insamples);
  217. return AVERROR(ENOMEM);
  218. }
  219. ff_bufqueue_add(ctx, &am->in[input_number].queue, av_frame_clone(insamples));
  220. am->in[input_number].nb_samples += insamples->nb_samples;
  221. av_frame_free(&insamples);
  222. nb_samples = am->in[0].nb_samples;
  223. for (i = 1; i < am->nb_inputs; i++)
  224. nb_samples = FFMIN(nb_samples, am->in[i].nb_samples);
  225. if (!nb_samples)
  226. return 0;
  227. outbuf = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
  228. if (!outbuf)
  229. return AVERROR(ENOMEM);
  230. outs = outbuf->data[0];
  231. for (i = 0; i < am->nb_inputs; i++) {
  232. inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
  233. ins[i] = inbuf[i]->data[0] +
  234. am->in[i].pos * am->in[i].nb_ch * am->bps;
  235. }
  236. av_frame_copy_props(outbuf, inbuf[0]);
  237. outbuf->pts = inbuf[0]->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
  238. inbuf[0]->pts +
  239. av_rescale_q(am->in[0].pos,
  240. av_make_q(1, ctx->inputs[0]->sample_rate),
  241. ctx->outputs[0]->time_base);
  242. outbuf->nb_samples = nb_samples;
  243. outbuf->channel_layout = outlink->channel_layout;
  244. av_frame_set_channels(outbuf, outlink->channels);
  245. while (nb_samples) {
  246. ns = nb_samples;
  247. for (i = 0; i < am->nb_inputs; i++)
  248. ns = FFMIN(ns, inbuf[i]->nb_samples - am->in[i].pos);
  249. /* Unroll the most common sample formats: speed +~350% for the loop,
  250. +~13% overall (including two common decoders) */
  251. switch (am->bps) {
  252. case 1:
  253. copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 1);
  254. break;
  255. case 2:
  256. copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 2);
  257. break;
  258. case 4:
  259. copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 4);
  260. break;
  261. default:
  262. copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, am->bps);
  263. break;
  264. }
  265. nb_samples -= ns;
  266. for (i = 0; i < am->nb_inputs; i++) {
  267. am->in[i].nb_samples -= ns;
  268. am->in[i].pos += ns;
  269. if (am->in[i].pos == inbuf[i]->nb_samples) {
  270. am->in[i].pos = 0;
  271. av_frame_free(&inbuf[i]);
  272. ff_bufqueue_get(&am->in[i].queue);
  273. inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
  274. ins[i] = inbuf[i] ? inbuf[i]->data[0] : NULL;
  275. }
  276. }
  277. }
  278. return ff_filter_frame(ctx->outputs[0], outbuf);
  279. }
  280. static av_cold int init(AVFilterContext *ctx)
  281. {
  282. AMergeContext *am = ctx->priv;
  283. int i;
  284. am->in = av_calloc(am->nb_inputs, sizeof(*am->in));
  285. if (!am->in)
  286. return AVERROR(ENOMEM);
  287. for (i = 0; i < am->nb_inputs; i++) {
  288. char *name = av_asprintf("in%d", i);
  289. AVFilterPad pad = {
  290. .name = name,
  291. .type = AVMEDIA_TYPE_AUDIO,
  292. .filter_frame = filter_frame,
  293. };
  294. if (!name)
  295. return AVERROR(ENOMEM);
  296. ff_insert_inpad(ctx, i, &pad);
  297. }
  298. return 0;
  299. }
  300. static const AVFilterPad amerge_outputs[] = {
  301. {
  302. .name = "default",
  303. .type = AVMEDIA_TYPE_AUDIO,
  304. .config_props = config_output,
  305. .request_frame = request_frame,
  306. },
  307. { NULL }
  308. };
  309. AVFilter ff_af_amerge = {
  310. .name = "amerge",
  311. .description = NULL_IF_CONFIG_SMALL("Merge two or more audio streams into "
  312. "a single multi-channel stream."),
  313. .priv_size = sizeof(AMergeContext),
  314. .init = init,
  315. .uninit = uninit,
  316. .query_formats = query_formats,
  317. .inputs = NULL,
  318. .outputs = amerge_outputs,
  319. .priv_class = &amerge_class,
  320. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  321. };