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.

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