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.

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