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.

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