af_amerge.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "internal.h"
  27. #define QUEUE_SIZE 16
  28. typedef struct {
  29. int nb_in_ch[2]; /**< number of channels for each input */
  30. int route[SWR_CH_MAX]; /**< channels routing, see copy_samples */
  31. int bps;
  32. struct amerge_queue {
  33. AVFilterBufferRef *buf[QUEUE_SIZE];
  34. int nb_buf, nb_samples, pos;
  35. } queue[2];
  36. } AMergeContext;
  37. static av_cold void uninit(AVFilterContext *ctx)
  38. {
  39. AMergeContext *am = ctx->priv;
  40. int i, j;
  41. for (i = 0; i < 2; i++)
  42. for (j = 0; j < am->queue[i].nb_buf; j++)
  43. avfilter_unref_buffer(am->queue[i].buf[j]);
  44. }
  45. static int query_formats(AVFilterContext *ctx)
  46. {
  47. AMergeContext *am = ctx->priv;
  48. int64_t inlayout[2], outlayout;
  49. const int packing_fmts[] = { AVFILTER_PACKED, -1 };
  50. AVFilterFormats *formats;
  51. int i;
  52. for (i = 0; i < 2; i++) {
  53. if (!ctx->inputs[i]->in_chlayouts ||
  54. !ctx->inputs[i]->in_chlayouts->format_count) {
  55. av_log(ctx, AV_LOG_ERROR,
  56. "No channel layout for input %d\n", i + 1);
  57. return AVERROR(EINVAL);
  58. }
  59. inlayout[i] = ctx->inputs[i]->in_chlayouts->formats[0];
  60. if (ctx->inputs[i]->in_chlayouts->format_count > 1) {
  61. char buf[256];
  62. av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
  63. av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
  64. }
  65. am->nb_in_ch[i] = av_get_channel_layout_nb_channels(inlayout[i]);
  66. }
  67. if (am->nb_in_ch[0] + am->nb_in_ch[1] > SWR_CH_MAX) {
  68. av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
  69. return AVERROR(EINVAL);
  70. }
  71. if (inlayout[0] & inlayout[1]) {
  72. av_log(ctx, AV_LOG_WARNING,
  73. "Inputs overlap: output layout will be meaningless\n");
  74. for (i = 0; i < am->nb_in_ch[0] + am->nb_in_ch[1]; i++)
  75. am->route[i] = i;
  76. outlayout = av_get_default_channel_layout(am->nb_in_ch[0] +
  77. am->nb_in_ch[1]);
  78. if (!outlayout)
  79. outlayout = ((int64_t)1 << (am->nb_in_ch[0] + am->nb_in_ch[1])) - 1;
  80. } else {
  81. int *route[2] = { am->route, am->route + am->nb_in_ch[0] };
  82. int c, out_ch_number = 0;
  83. outlayout = inlayout[0] | inlayout[1];
  84. for (c = 0; c < 64; c++)
  85. for (i = 0; i < 2; i++)
  86. if ((inlayout[i] >> c) & 1)
  87. *(route[i]++) = out_ch_number++;
  88. }
  89. formats = avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO);
  90. avfilter_set_common_sample_formats(ctx, formats);
  91. formats = avfilter_make_format_list(packing_fmts);
  92. avfilter_set_common_packing_formats(ctx, formats);
  93. for (i = 0; i < 2; i++) {
  94. formats = NULL;
  95. avfilter_add_format(&formats, inlayout[i]);
  96. avfilter_formats_ref(formats, &ctx->inputs[i]->out_chlayouts);
  97. }
  98. formats = NULL;
  99. avfilter_add_format(&formats, outlayout);
  100. avfilter_formats_ref(formats, &ctx->outputs[0]->in_chlayouts);
  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;
  133. for (i = 0; i < 2; i++)
  134. if (!am->queue[i].nb_samples)
  135. avfilter_request_frame(ctx->inputs[i]);
  136. return 0;
  137. }
  138. /**
  139. * Copy samples from two input streams to one output stream.
  140. * @param nb_in_ch number of channels in each input stream
  141. * @param route routing values;
  142. * input channel i goes to output channel route[i];
  143. * i < nb_in_ch[0] are the channels from the first output;
  144. * i >= nb_in_ch[0] are the channels from the second output
  145. * @param ins pointer to the samples of each inputs, in packed format;
  146. * will be left at the end of the copied samples
  147. * @param outs pointer to the samples of the output, in packet format;
  148. * must point to a buffer big enough;
  149. * will be left at the end of the copied samples
  150. * @param ns number of samples to copy
  151. * @param bps bytes per sample
  152. */
  153. static inline void copy_samples(int nb_in_ch[2], int *route, uint8_t *ins[2],
  154. uint8_t **outs, int ns, int bps)
  155. {
  156. int *route_cur;
  157. int i, c;
  158. while (ns--) {
  159. route_cur = route;
  160. for (i = 0; i < 2; i++) {
  161. for (c = 0; c < nb_in_ch[i]; c++) {
  162. memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
  163. ins[i] += bps;
  164. }
  165. }
  166. *outs += (nb_in_ch[0] + nb_in_ch[1]) * bps;
  167. }
  168. }
  169. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  170. {
  171. AVFilterContext *ctx = inlink->dst;
  172. AMergeContext *am = ctx->priv;
  173. int input_number = inlink == ctx->inputs[1];
  174. struct amerge_queue *inq = &am->queue[input_number];
  175. int nb_samples, ns, i;
  176. AVFilterBufferRef *outbuf, **inbuf[2];
  177. uint8_t *ins[2], *outs;
  178. if (inq->nb_buf == QUEUE_SIZE) {
  179. av_log(ctx, AV_LOG_ERROR, "Packet queue overflow; dropped\n");
  180. avfilter_unref_buffer(insamples);
  181. return;
  182. }
  183. inq->buf[inq->nb_buf++] = avfilter_ref_buffer(insamples, AV_PERM_READ |
  184. AV_PERM_PRESERVE);
  185. inq->nb_samples += insamples->audio->nb_samples;
  186. avfilter_unref_buffer(insamples);
  187. if (!am->queue[!input_number].nb_samples)
  188. return;
  189. nb_samples = FFMIN(am->queue[0].nb_samples,
  190. am->queue[1].nb_samples);
  191. outbuf = avfilter_get_audio_buffer(ctx->outputs[0], AV_PERM_WRITE,
  192. nb_samples);
  193. outs = outbuf->data[0];
  194. for (i = 0; i < 2; i++) {
  195. inbuf[i] = am->queue[i].buf;
  196. ins[i] = (*inbuf[i])->data[0] +
  197. am->queue[i].pos * am->nb_in_ch[i] * am->bps;
  198. }
  199. while (nb_samples) {
  200. ns = nb_samples;
  201. for (i = 0; i < 2; i++)
  202. ns = FFMIN(ns, (*inbuf[i])->audio->nb_samples - am->queue[i].pos);
  203. /* Unroll the most common sample formats: speed +~350% for the loop,
  204. +~13% overall (including two common decoders) */
  205. switch (am->bps) {
  206. case 1:
  207. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 1);
  208. break;
  209. case 2:
  210. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 2);
  211. break;
  212. case 4:
  213. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 4);
  214. break;
  215. default:
  216. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, am->bps);
  217. break;
  218. }
  219. nb_samples -= ns;
  220. for (i = 0; i < 2; i++) {
  221. am->queue[i].nb_samples -= ns;
  222. am->queue[i].pos += ns;
  223. if (am->queue[i].pos == (*inbuf[i])->audio->nb_samples) {
  224. am->queue[i].pos = 0;
  225. avfilter_unref_buffer(*inbuf[i]);
  226. *inbuf[i] = NULL;
  227. inbuf[i]++;
  228. ins[i] = *inbuf[i] ? (*inbuf[i])->data[0] : NULL;
  229. }
  230. }
  231. }
  232. for (i = 0; i < 2; i++) {
  233. int nbufused = inbuf[i] - am->queue[i].buf;
  234. if (nbufused) {
  235. am->queue[i].nb_buf -= nbufused;
  236. memmove(am->queue[i].buf, inbuf[i],
  237. am->queue[i].nb_buf * sizeof(**inbuf));
  238. }
  239. }
  240. avfilter_filter_samples(ctx->outputs[0], outbuf);
  241. }
  242. AVFilter avfilter_af_amerge = {
  243. .name = "amerge",
  244. .description = NULL_IF_CONFIG_SMALL("Merge two audio streams into "
  245. "a single multi-channel stream."),
  246. .priv_size = sizeof(AMergeContext),
  247. .uninit = uninit,
  248. .query_formats = query_formats,
  249. .inputs = (const AVFilterPad[]) {
  250. { .name = "in1",
  251. .type = AVMEDIA_TYPE_AUDIO,
  252. .filter_samples = filter_samples,
  253. .min_perms = AV_PERM_READ, },
  254. { .name = "in2",
  255. .type = AVMEDIA_TYPE_AUDIO,
  256. .filter_samples = filter_samples,
  257. .min_perms = AV_PERM_READ, },
  258. { .name = NULL }
  259. },
  260. .outputs = (const AVFilterPad[]) {
  261. { .name = "default",
  262. .type = AVMEDIA_TYPE_AUDIO,
  263. .config_props = config_output,
  264. .request_frame = request_frame, },
  265. { .name = NULL }
  266. },
  267. };