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