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.

344 lines
12KB

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