af_amerge.c 11 KB

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