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.

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