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.

298 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 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 "internal.h"
  28. #define QUEUE_SIZE 16
  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_queue {
  34. AVFilterBufferRef *buf[QUEUE_SIZE];
  35. int nb_buf, nb_samples, pos;
  36. } queue[2];
  37. } AMergeContext;
  38. static av_cold void uninit(AVFilterContext *ctx)
  39. {
  40. AMergeContext *am = ctx->priv;
  41. int i, j;
  42. for (i = 0; i < 2; i++)
  43. for (j = 0; j < am->queue[i].nb_buf; j++)
  44. avfilter_unref_buffer(am->queue[i].buf[j]);
  45. }
  46. static int query_formats(AVFilterContext *ctx)
  47. {
  48. AMergeContext *am = ctx->priv;
  49. int64_t inlayout[2], outlayout;
  50. const int packing_fmts[] = { AVFILTER_PACKED, -1 };
  51. AVFilterFormats *formats;
  52. int i;
  53. for (i = 0; i < 2; i++) {
  54. if (!ctx->inputs[i]->in_chlayouts ||
  55. !ctx->inputs[i]->in_chlayouts->format_count) {
  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_chlayouts->formats[0];
  61. if (ctx->inputs[i]->in_chlayouts->format_count > 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_all_formats(AVMEDIA_TYPE_AUDIO);
  91. avfilter_set_common_sample_formats(ctx, formats);
  92. formats = avfilter_make_format_list(packing_fmts);
  93. avfilter_set_common_packing_formats(ctx, formats);
  94. for (i = 0; i < 2; i++) {
  95. formats = NULL;
  96. avfilter_add_format(&formats, inlayout[i]);
  97. avfilter_formats_ref(formats, &ctx->inputs[i]->out_chlayouts);
  98. }
  99. formats = NULL;
  100. avfilter_add_format(&formats, outlayout);
  101. avfilter_formats_ref(formats, &ctx->outputs[0]->in_chlayouts);
  102. return 0;
  103. }
  104. static int config_output(AVFilterLink *outlink)
  105. {
  106. AVFilterContext *ctx = outlink->src;
  107. AMergeContext *am = ctx->priv;
  108. int64_t layout;
  109. char name[3][256];
  110. int i;
  111. if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
  112. av_log(ctx, AV_LOG_ERROR,
  113. "Inputs must have the same sample rate "
  114. "(%"PRIi64" vs %"PRIi64")\n",
  115. ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
  116. return AVERROR(EINVAL);
  117. }
  118. am->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
  119. outlink->sample_rate = ctx->inputs[0]->sample_rate;
  120. outlink->time_base = ctx->inputs[0]->time_base;
  121. for (i = 0; i < 3; i++) {
  122. layout = (i < 2 ? ctx->inputs[i] : ctx->outputs[0])->channel_layout;
  123. av_get_channel_layout_string(name[i], sizeof(name[i]), -1, layout);
  124. }
  125. av_log(ctx, AV_LOG_INFO,
  126. "in1:%s + in2:%s -> out:%s\n", name[0], name[1], name[2]);
  127. return 0;
  128. }
  129. static int request_frame(AVFilterLink *outlink)
  130. {
  131. AVFilterContext *ctx = outlink->src;
  132. AMergeContext *am = ctx->priv;
  133. int i, ret;
  134. for (i = 0; i < 2; i++)
  135. if (!am->queue[i].nb_samples)
  136. if ((ret = avfilter_request_frame(ctx->inputs[i])) < 0)
  137. return ret;
  138. return 0;
  139. }
  140. /**
  141. * Copy samples from two input streams to one output stream.
  142. * @param nb_in_ch number of channels in each input stream
  143. * @param route routing values;
  144. * input channel i goes to output channel route[i];
  145. * i < nb_in_ch[0] are the channels from the first output;
  146. * i >= nb_in_ch[0] are the channels from the second output
  147. * @param ins pointer to the samples of each inputs, in packed format;
  148. * will be left at the end of the copied samples
  149. * @param outs pointer to the samples of the output, in packet format;
  150. * must point to a buffer big enough;
  151. * will be left at the end of the copied samples
  152. * @param ns number of samples to copy
  153. * @param bps bytes per sample
  154. */
  155. static inline void copy_samples(int nb_in_ch[2], int *route, uint8_t *ins[2],
  156. uint8_t **outs, int ns, int bps)
  157. {
  158. int *route_cur;
  159. int i, c;
  160. while (ns--) {
  161. route_cur = route;
  162. for (i = 0; i < 2; i++) {
  163. for (c = 0; c < nb_in_ch[i]; c++) {
  164. memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
  165. ins[i] += bps;
  166. }
  167. }
  168. *outs += (nb_in_ch[0] + nb_in_ch[1]) * bps;
  169. }
  170. }
  171. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  172. {
  173. AVFilterContext *ctx = inlink->dst;
  174. AMergeContext *am = ctx->priv;
  175. AVFilterLink *const outlink = ctx->outputs[0];
  176. int input_number = inlink == ctx->inputs[1];
  177. struct amerge_queue *inq = &am->queue[input_number];
  178. int nb_samples, ns, i;
  179. AVFilterBufferRef *outbuf, **inbuf[2];
  180. uint8_t *ins[2], *outs;
  181. if (inq->nb_buf == QUEUE_SIZE) {
  182. av_log(ctx, AV_LOG_ERROR, "Packet queue overflow; dropped\n");
  183. avfilter_unref_buffer(insamples);
  184. return;
  185. }
  186. inq->buf[inq->nb_buf++] = avfilter_ref_buffer(insamples, AV_PERM_READ |
  187. AV_PERM_PRESERVE);
  188. inq->nb_samples += insamples->audio->nb_samples;
  189. avfilter_unref_buffer(insamples);
  190. if (!am->queue[!input_number].nb_samples)
  191. return;
  192. nb_samples = FFMIN(am->queue[0].nb_samples,
  193. am->queue[1].nb_samples);
  194. outbuf = ff_get_audio_buffer(ctx->outputs[0], AV_PERM_WRITE,
  195. nb_samples);
  196. outs = outbuf->data[0];
  197. for (i = 0; i < 2; i++) {
  198. inbuf[i] = am->queue[i].buf;
  199. ins[i] = (*inbuf[i])->data[0] +
  200. am->queue[i].pos * am->nb_in_ch[i] * am->bps;
  201. }
  202. avfilter_copy_buffer_ref_props(outbuf, *inbuf[0]);
  203. outbuf->audio->nb_samples = nb_samples;
  204. outbuf->audio->channel_layout = outlink->channel_layout;
  205. outbuf->audio->planar = outlink->planar;
  206. while (nb_samples) {
  207. ns = nb_samples;
  208. for (i = 0; i < 2; i++)
  209. ns = FFMIN(ns, (*inbuf[i])->audio->nb_samples - am->queue[i].pos);
  210. /* Unroll the most common sample formats: speed +~350% for the loop,
  211. +~13% overall (including two common decoders) */
  212. switch (am->bps) {
  213. case 1:
  214. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 1);
  215. break;
  216. case 2:
  217. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 2);
  218. break;
  219. case 4:
  220. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 4);
  221. break;
  222. default:
  223. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, am->bps);
  224. break;
  225. }
  226. nb_samples -= ns;
  227. for (i = 0; i < 2; i++) {
  228. am->queue[i].nb_samples -= ns;
  229. am->queue[i].pos += ns;
  230. if (am->queue[i].pos == (*inbuf[i])->audio->nb_samples) {
  231. am->queue[i].pos = 0;
  232. avfilter_unref_buffer(*inbuf[i]);
  233. *inbuf[i] = NULL;
  234. inbuf[i]++;
  235. ins[i] = *inbuf[i] ? (*inbuf[i])->data[0] : NULL;
  236. }
  237. }
  238. }
  239. for (i = 0; i < 2; i++) {
  240. int nbufused = inbuf[i] - am->queue[i].buf;
  241. if (nbufused) {
  242. am->queue[i].nb_buf -= nbufused;
  243. memmove(am->queue[i].buf, inbuf[i],
  244. am->queue[i].nb_buf * sizeof(**inbuf));
  245. }
  246. }
  247. ff_filter_samples(ctx->outputs[0], outbuf);
  248. }
  249. AVFilter avfilter_af_amerge = {
  250. .name = "amerge",
  251. .description = NULL_IF_CONFIG_SMALL("Merge two audio streams into "
  252. "a single multi-channel stream."),
  253. .priv_size = sizeof(AMergeContext),
  254. .uninit = uninit,
  255. .query_formats = query_formats,
  256. .inputs = (const AVFilterPad[]) {
  257. { .name = "in1",
  258. .type = AVMEDIA_TYPE_AUDIO,
  259. .filter_samples = filter_samples,
  260. .min_perms = AV_PERM_READ, },
  261. { .name = "in2",
  262. .type = AVMEDIA_TYPE_AUDIO,
  263. .filter_samples = filter_samples,
  264. .min_perms = AV_PERM_READ, },
  265. { .name = NULL }
  266. },
  267. .outputs = (const AVFilterPad[]) {
  268. { .name = "default",
  269. .type = AVMEDIA_TYPE_AUDIO,
  270. .config_props = config_output,
  271. .request_frame = request_frame, },
  272. { .name = NULL }
  273. },
  274. };