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.

295 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 "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. return 0;
  101. }
  102. static int config_output(AVFilterLink *outlink)
  103. {
  104. AVFilterContext *ctx = outlink->src;
  105. AMergeContext *am = ctx->priv;
  106. int64_t layout;
  107. char name[3][256];
  108. int i;
  109. if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
  110. av_log(ctx, AV_LOG_ERROR,
  111. "Inputs must have the same sample rate "
  112. "(%"PRIi64" vs %"PRIi64")\n",
  113. ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
  114. return AVERROR(EINVAL);
  115. }
  116. am->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
  117. outlink->sample_rate = ctx->inputs[0]->sample_rate;
  118. outlink->time_base = ctx->inputs[0]->time_base;
  119. for (i = 0; i < 3; i++) {
  120. layout = (i < 2 ? ctx->inputs[i] : ctx->outputs[0])->channel_layout;
  121. av_get_channel_layout_string(name[i], sizeof(name[i]), -1, layout);
  122. }
  123. av_log(ctx, AV_LOG_INFO,
  124. "in1:%s + in2:%s -> out:%s\n", name[0], name[1], name[2]);
  125. return 0;
  126. }
  127. static int request_frame(AVFilterLink *outlink)
  128. {
  129. AVFilterContext *ctx = outlink->src;
  130. AMergeContext *am = ctx->priv;
  131. int i, ret;
  132. for (i = 0; i < 2; i++)
  133. if (!am->queue[i].nb_samples)
  134. if ((ret = avfilter_request_frame(ctx->inputs[i])) < 0)
  135. return ret;
  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. AVFilterLink *const outlink = ctx->outputs[0];
  174. int input_number = inlink == ctx->inputs[1];
  175. struct amerge_queue *inq = &am->queue[input_number];
  176. int nb_samples, ns, i;
  177. AVFilterBufferRef *outbuf, **inbuf[2];
  178. uint8_t *ins[2], *outs;
  179. if (inq->nb_buf == QUEUE_SIZE) {
  180. av_log(ctx, AV_LOG_ERROR, "Packet queue overflow; dropped\n");
  181. avfilter_unref_buffer(insamples);
  182. return;
  183. }
  184. inq->buf[inq->nb_buf++] = avfilter_ref_buffer(insamples, AV_PERM_READ |
  185. AV_PERM_PRESERVE);
  186. inq->nb_samples += insamples->audio->nb_samples;
  187. avfilter_unref_buffer(insamples);
  188. if (!am->queue[!input_number].nb_samples)
  189. return;
  190. nb_samples = FFMIN(am->queue[0].nb_samples,
  191. am->queue[1].nb_samples);
  192. outbuf = ff_get_audio_buffer(ctx->outputs[0], AV_PERM_WRITE,
  193. nb_samples);
  194. outs = outbuf->data[0];
  195. for (i = 0; i < 2; i++) {
  196. inbuf[i] = am->queue[i].buf;
  197. ins[i] = (*inbuf[i])->data[0] +
  198. am->queue[i].pos * am->nb_in_ch[i] * am->bps;
  199. }
  200. avfilter_copy_buffer_ref_props(outbuf, *inbuf[0]);
  201. outbuf->audio->nb_samples = nb_samples;
  202. outbuf->audio->channel_layout = outlink->channel_layout;
  203. while (nb_samples) {
  204. ns = nb_samples;
  205. for (i = 0; i < 2; i++)
  206. ns = FFMIN(ns, (*inbuf[i])->audio->nb_samples - am->queue[i].pos);
  207. /* Unroll the most common sample formats: speed +~350% for the loop,
  208. +~13% overall (including two common decoders) */
  209. switch (am->bps) {
  210. case 1:
  211. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 1);
  212. break;
  213. case 2:
  214. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 2);
  215. break;
  216. case 4:
  217. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 4);
  218. break;
  219. default:
  220. copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, am->bps);
  221. break;
  222. }
  223. nb_samples -= ns;
  224. for (i = 0; i < 2; i++) {
  225. am->queue[i].nb_samples -= ns;
  226. am->queue[i].pos += ns;
  227. if (am->queue[i].pos == (*inbuf[i])->audio->nb_samples) {
  228. am->queue[i].pos = 0;
  229. avfilter_unref_buffer(*inbuf[i]);
  230. *inbuf[i] = NULL;
  231. inbuf[i]++;
  232. ins[i] = *inbuf[i] ? (*inbuf[i])->data[0] : NULL;
  233. }
  234. }
  235. }
  236. for (i = 0; i < 2; i++) {
  237. int nbufused = inbuf[i] - am->queue[i].buf;
  238. if (nbufused) {
  239. am->queue[i].nb_buf -= nbufused;
  240. memmove(am->queue[i].buf, inbuf[i],
  241. am->queue[i].nb_buf * sizeof(**inbuf));
  242. }
  243. }
  244. ff_filter_samples(ctx->outputs[0], outbuf);
  245. }
  246. AVFilter avfilter_af_amerge = {
  247. .name = "amerge",
  248. .description = NULL_IF_CONFIG_SMALL("Merge two audio streams into "
  249. "a single multi-channel stream."),
  250. .priv_size = sizeof(AMergeContext),
  251. .uninit = uninit,
  252. .query_formats = query_formats,
  253. .inputs = (const AVFilterPad[]) {
  254. { .name = "in1",
  255. .type = AVMEDIA_TYPE_AUDIO,
  256. .filter_samples = filter_samples,
  257. .min_perms = AV_PERM_READ, },
  258. { .name = "in2",
  259. .type = AVMEDIA_TYPE_AUDIO,
  260. .filter_samples = filter_samples,
  261. .min_perms = AV_PERM_READ, },
  262. { .name = NULL }
  263. },
  264. .outputs = (const AVFilterPad[]) {
  265. { .name = "default",
  266. .type = AVMEDIA_TYPE_AUDIO,
  267. .config_props = config_output,
  268. .request_frame = request_frame, },
  269. { .name = NULL }
  270. },
  271. };