af_amerge.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. 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->queue[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. struct amerge_queue *inq = &am->queue[input_number];
  177. int nb_samples, ns, i;
  178. AVFilterBufferRef *outbuf, **inbuf[2];
  179. uint8_t *ins[2], *outs;
  180. if (inq->nb_buf == QUEUE_SIZE) {
  181. av_log(ctx, AV_LOG_ERROR, "Packet queue overflow; dropped\n");
  182. avfilter_unref_buffer(insamples);
  183. return;
  184. }
  185. inq->buf[inq->nb_buf++] = avfilter_ref_buffer(insamples, AV_PERM_READ |
  186. AV_PERM_PRESERVE);
  187. inq->nb_samples += insamples->audio->nb_samples;
  188. avfilter_unref_buffer(insamples);
  189. if (!am->queue[!input_number].nb_samples)
  190. return;
  191. nb_samples = FFMIN(am->queue[0].nb_samples,
  192. am->queue[1].nb_samples);
  193. outbuf = ff_get_audio_buffer(ctx->outputs[0], AV_PERM_WRITE,
  194. nb_samples);
  195. outs = outbuf->data[0];
  196. for (i = 0; i < 2; i++) {
  197. inbuf[i] = am->queue[i].buf;
  198. ins[i] = (*inbuf[i])->data[0] +
  199. am->queue[i].pos * am->nb_in_ch[i] * am->bps;
  200. }
  201. outbuf->pts = (*inbuf[0])->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
  202. (*inbuf[0])->pts +
  203. av_rescale_q(am->queue[0].pos,
  204. (AVRational){ 1, ctx->inputs[0]->sample_rate },
  205. ctx->outputs[0]->time_base);
  206. avfilter_copy_buffer_ref_props(outbuf, *inbuf[0]);
  207. outbuf->audio->nb_samples = nb_samples;
  208. outbuf->audio->channel_layout = outlink->channel_layout;
  209. while (nb_samples) {
  210. ns = nb_samples;
  211. for (i = 0; i < 2; i++)
  212. ns = FFMIN(ns, (*inbuf[i])->audio->nb_samples - am->queue[i].pos);
  213. /* Unroll the most common sample formats: speed +~350% for the loop,
  214. +~13% overall (including two common decoders) */
  215. switch (am->bps) {
  216. case 1:
  217. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 1);
  218. break;
  219. case 2:
  220. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 2);
  221. break;
  222. case 4:
  223. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 4);
  224. break;
  225. default:
  226. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, am->bps);
  227. break;
  228. }
  229. nb_samples -= ns;
  230. for (i = 0; i < 2; i++) {
  231. am->queue[i].nb_samples -= ns;
  232. am->queue[i].pos += ns;
  233. if (am->queue[i].pos == (*inbuf[i])->audio->nb_samples) {
  234. am->queue[i].pos = 0;
  235. avfilter_unref_buffer(*inbuf[i]);
  236. *inbuf[i] = NULL;
  237. inbuf[i]++;
  238. ins[i] = *inbuf[i] ? (*inbuf[i])->data[0] : NULL;
  239. }
  240. }
  241. }
  242. for (i = 0; i < 2; i++) {
  243. int nbufused = inbuf[i] - am->queue[i].buf;
  244. if (nbufused) {
  245. am->queue[i].nb_buf -= nbufused;
  246. memmove(am->queue[i].buf, inbuf[i],
  247. am->queue[i].nb_buf * sizeof(**inbuf));
  248. }
  249. }
  250. ff_filter_samples(ctx->outputs[0], outbuf);
  251. }
  252. AVFilter avfilter_af_amerge = {
  253. .name = "amerge",
  254. .description = NULL_IF_CONFIG_SMALL("Merge two audio streams into "
  255. "a single multi-channel stream."),
  256. .priv_size = sizeof(AMergeContext),
  257. .uninit = uninit,
  258. .query_formats = query_formats,
  259. .inputs = (const AVFilterPad[]) {
  260. { .name = "in1",
  261. .type = AVMEDIA_TYPE_AUDIO,
  262. .filter_samples = filter_samples,
  263. .min_perms = AV_PERM_READ, },
  264. { .name = "in2",
  265. .type = AVMEDIA_TYPE_AUDIO,
  266. .filter_samples = filter_samples,
  267. .min_perms = AV_PERM_READ, },
  268. { .name = NULL }
  269. },
  270. .outputs = (const AVFilterPad[]) {
  271. { .name = "default",
  272. .type = AVMEDIA_TYPE_AUDIO,
  273. .config_props = config_output,
  274. .request_frame = request_frame, },
  275. { .name = NULL }
  276. },
  277. };