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.

362 lines
11KB

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