af_amerge.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/audioconvert.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/bprint.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. {0}
  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. ff_bufqueue_discard_all(&am->in[i].queue);
  59. av_freep(&ctx->input_pads[i].name);
  60. }
  61. av_freep(&am->in);
  62. }
  63. static int query_formats(AVFilterContext *ctx)
  64. {
  65. AMergeContext *am = ctx->priv;
  66. int64_t inlayout[SWR_CH_MAX], outlayout = 0;
  67. AVFilterFormats *formats;
  68. AVFilterChannelLayouts *layouts;
  69. int i, overlap = 0, nb_ch = 0;
  70. for (i = 0; i < am->nb_inputs; i++) {
  71. if (!ctx->inputs[i]->in_channel_layouts ||
  72. !ctx->inputs[i]->in_channel_layouts->nb_channel_layouts) {
  73. av_log(ctx, AV_LOG_ERROR,
  74. "No channel layout for input %d\n", i + 1);
  75. return AVERROR(EINVAL);
  76. }
  77. inlayout[i] = ctx->inputs[i]->in_channel_layouts->channel_layouts[0];
  78. if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
  79. char buf[256];
  80. av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
  81. av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
  82. }
  83. am->in[i].nb_ch = av_get_channel_layout_nb_channels(inlayout[i]);
  84. if (outlayout & inlayout[i])
  85. overlap++;
  86. outlayout |= inlayout[i];
  87. nb_ch += am->in[i].nb_ch;
  88. }
  89. if (nb_ch > SWR_CH_MAX) {
  90. av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
  91. return AVERROR(EINVAL);
  92. }
  93. if (overlap) {
  94. av_log(ctx, AV_LOG_WARNING,
  95. "Input channel layouts overlap: "
  96. "output layout will be determined by the number of distinct input channels\n");
  97. for (i = 0; i < nb_ch; i++)
  98. am->route[i] = i;
  99. outlayout = av_get_default_channel_layout(nb_ch);
  100. if (!outlayout)
  101. outlayout = ((int64_t)1 << nb_ch) - 1;
  102. } else {
  103. int *route[SWR_CH_MAX];
  104. int c, out_ch_number = 0;
  105. route[0] = am->route;
  106. for (i = 1; i < am->nb_inputs; i++)
  107. route[i] = route[i - 1] + am->in[i - 1].nb_ch;
  108. for (c = 0; c < 64; c++)
  109. for (i = 0; i < am->nb_inputs; i++)
  110. if ((inlayout[i] >> c) & 1)
  111. *(route[i]++) = out_ch_number++;
  112. }
  113. formats = ff_make_format_list(ff_packed_sample_fmts_array);
  114. ff_set_common_formats(ctx, formats);
  115. for (i = 0; i < am->nb_inputs; i++) {
  116. layouts = NULL;
  117. ff_add_channel_layout(&layouts, inlayout[i]);
  118. ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
  119. }
  120. layouts = NULL;
  121. ff_add_channel_layout(&layouts, outlayout);
  122. ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
  123. ff_set_common_samplerates(ctx, ff_all_samplerates());
  124. return 0;
  125. }
  126. static int config_output(AVFilterLink *outlink)
  127. {
  128. AVFilterContext *ctx = outlink->src;
  129. AMergeContext *am = ctx->priv;
  130. AVBPrint bp;
  131. int i;
  132. for (i = 1; i < am->nb_inputs; i++) {
  133. if (ctx->inputs[i]->sample_rate != ctx->inputs[0]->sample_rate) {
  134. av_log(ctx, AV_LOG_ERROR,
  135. "Inputs must have the same sample rate "
  136. "%d for in%d vs %d\n",
  137. ctx->inputs[i]->sample_rate, i, ctx->inputs[0]->sample_rate);
  138. return AVERROR(EINVAL);
  139. }
  140. }
  141. am->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
  142. outlink->sample_rate = ctx->inputs[0]->sample_rate;
  143. outlink->time_base = ctx->inputs[0]->time_base;
  144. av_bprint_init(&bp, 0, 1);
  145. for (i = 0; i < am->nb_inputs; i++) {
  146. av_bprintf(&bp, "%sin%d:", i ? " + " : "", i);
  147. av_bprint_channel_layout(&bp, -1, ctx->inputs[i]->channel_layout);
  148. }
  149. av_bprintf(&bp, " -> out:");
  150. av_bprint_channel_layout(&bp, -1, ctx->outputs[0]->channel_layout);
  151. av_log(ctx, AV_LOG_VERBOSE, "%s\n", bp.str);
  152. return 0;
  153. }
  154. static int request_frame(AVFilterLink *outlink)
  155. {
  156. AVFilterContext *ctx = outlink->src;
  157. AMergeContext *am = ctx->priv;
  158. int i, ret;
  159. for (i = 0; i < am->nb_inputs; i++)
  160. if (!am->in[i].nb_samples)
  161. if ((ret = ff_request_frame(ctx->inputs[i])) < 0)
  162. return ret;
  163. return 0;
  164. }
  165. /**
  166. * Copy samples from several input streams to one output stream.
  167. * @param nb_inputs number of inputs
  168. * @param in inputs; used only for the nb_ch field;
  169. * @param route routing values;
  170. * input channel i goes to output channel route[i];
  171. * i < in[0].nb_ch are the channels from the first output;
  172. * i >= in[0].nb_ch are the channels from the second output
  173. * @param ins pointer to the samples of each inputs, in packed format;
  174. * will be left at the end of the copied samples
  175. * @param outs pointer to the samples of the output, in packet format;
  176. * must point to a buffer big enough;
  177. * will be left at the end of the copied samples
  178. * @param ns number of samples to copy
  179. * @param bps bytes per sample
  180. */
  181. static inline void copy_samples(int nb_inputs, struct amerge_input in[],
  182. int *route, uint8_t *ins[],
  183. uint8_t **outs, int ns, int bps)
  184. {
  185. int *route_cur;
  186. int i, c, nb_ch = 0;
  187. for (i = 0; i < nb_inputs; i++)
  188. nb_ch += in[i].nb_ch;
  189. while (ns--) {
  190. route_cur = route;
  191. for (i = 0; i < nb_inputs; i++) {
  192. for (c = 0; c < in[i].nb_ch; c++) {
  193. memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
  194. ins[i] += bps;
  195. }
  196. }
  197. *outs += nb_ch * bps;
  198. }
  199. }
  200. static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  201. {
  202. AVFilterContext *ctx = inlink->dst;
  203. AMergeContext *am = ctx->priv;
  204. AVFilterLink *const outlink = ctx->outputs[0];
  205. int input_number;
  206. int nb_samples, ns, i;
  207. AVFilterBufferRef *outbuf, *inbuf[SWR_CH_MAX];
  208. uint8_t *ins[SWR_CH_MAX], *outs;
  209. for (input_number = 0; input_number < am->nb_inputs; input_number++)
  210. if (inlink == ctx->inputs[input_number])
  211. break;
  212. av_assert1(input_number < am->nb_inputs);
  213. ff_bufqueue_add(ctx, &am->in[input_number].queue, insamples);
  214. am->in[input_number].nb_samples += insamples->audio->nb_samples;
  215. nb_samples = am->in[0].nb_samples;
  216. for (i = 1; i < am->nb_inputs; i++)
  217. nb_samples = FFMIN(nb_samples, am->in[i].nb_samples);
  218. if (!nb_samples)
  219. return 0;
  220. outbuf = ff_get_audio_buffer(ctx->outputs[0], AV_PERM_WRITE, nb_samples);
  221. outs = outbuf->data[0];
  222. for (i = 0; i < am->nb_inputs; i++) {
  223. inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
  224. ins[i] = inbuf[i]->data[0] +
  225. am->in[i].pos * am->in[i].nb_ch * am->bps;
  226. }
  227. avfilter_copy_buffer_ref_props(outbuf, inbuf[0]);
  228. outbuf->pts = inbuf[0]->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
  229. inbuf[0]->pts +
  230. av_rescale_q(am->in[0].pos,
  231. (AVRational){ 1, ctx->inputs[0]->sample_rate },
  232. ctx->outputs[0]->time_base);
  233. outbuf->audio->nb_samples = nb_samples;
  234. outbuf->audio->channel_layout = outlink->channel_layout;
  235. while (nb_samples) {
  236. ns = nb_samples;
  237. for (i = 0; i < am->nb_inputs; i++)
  238. ns = FFMIN(ns, inbuf[i]->audio->nb_samples - am->in[i].pos);
  239. /* Unroll the most common sample formats: speed +~350% for the loop,
  240. +~13% overall (including two common decoders) */
  241. switch (am->bps) {
  242. case 1:
  243. copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 1);
  244. break;
  245. case 2:
  246. copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 2);
  247. break;
  248. case 4:
  249. copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 4);
  250. break;
  251. default:
  252. copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, am->bps);
  253. break;
  254. }
  255. nb_samples -= ns;
  256. for (i = 0; i < am->nb_inputs; i++) {
  257. am->in[i].nb_samples -= ns;
  258. am->in[i].pos += ns;
  259. if (am->in[i].pos == inbuf[i]->audio->nb_samples) {
  260. am->in[i].pos = 0;
  261. avfilter_unref_buffer(inbuf[i]);
  262. ff_bufqueue_get(&am->in[i].queue);
  263. inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
  264. ins[i] = inbuf[i] ? inbuf[i]->data[0] : NULL;
  265. }
  266. }
  267. }
  268. return ff_filter_samples(ctx->outputs[0], outbuf);
  269. }
  270. static av_cold int init(AVFilterContext *ctx, const char *args)
  271. {
  272. AMergeContext *am = ctx->priv;
  273. int ret, i;
  274. am->class = &amerge_class;
  275. av_opt_set_defaults(am);
  276. ret = av_set_options_string(am, args, "=", ":");
  277. if (ret < 0) {
  278. av_log(ctx, AV_LOG_ERROR, "Error parsing options: '%s'\n", args);
  279. return ret;
  280. }
  281. am->in = av_calloc(am->nb_inputs, sizeof(*am->in));
  282. if (!am->in)
  283. return AVERROR(ENOMEM);
  284. for (i = 0; i < am->nb_inputs; i++) {
  285. char *name = av_asprintf("in%d", i);
  286. AVFilterPad pad = {
  287. .name = name,
  288. .type = AVMEDIA_TYPE_AUDIO,
  289. .filter_samples = filter_samples,
  290. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  291. };
  292. if (!name)
  293. return AVERROR(ENOMEM);
  294. ff_insert_inpad(ctx, i, &pad);
  295. }
  296. return 0;
  297. }
  298. AVFilter avfilter_af_amerge = {
  299. .name = "amerge",
  300. .description = NULL_IF_CONFIG_SMALL("Merge two audio streams into "
  301. "a single multi-channel stream."),
  302. .priv_size = sizeof(AMergeContext),
  303. .init = init,
  304. .uninit = uninit,
  305. .query_formats = query_formats,
  306. .inputs = (const AVFilterPad[]) { { .name = NULL } },
  307. .outputs = (const AVFilterPad[]) {
  308. { .name = "default",
  309. .type = AVMEDIA_TYPE_AUDIO,
  310. .config_props = config_output,
  311. .request_frame = request_frame, },
  312. { .name = NULL }
  313. },
  314. .priv_class = &amerge_class,
  315. };