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.

283 lines
10KB

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