00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "libavutil/audioconvert.h"
00027 #include "libavutil/avstring.h"
00028 #include "libavutil/bprint.h"
00029 #include "libavutil/opt.h"
00030 #include "libswresample/swresample.h"
00031 #include "avfilter.h"
00032 #include "audio.h"
00033 #include "bufferqueue.h"
00034 #include "internal.h"
00035
00036 typedef struct {
00037 const AVClass *class;
00038 int nb_inputs;
00039 int route[SWR_CH_MAX];
00040 int bps;
00041 struct amerge_input {
00042 struct FFBufQueue queue;
00043 int nb_ch;
00044 int nb_samples;
00045 int pos;
00046 } *in;
00047 } AMergeContext;
00048
00049 #define OFFSET(x) offsetof(AMergeContext, x)
00050 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
00051
00052 static const AVOption amerge_options[] = {
00053 { "inputs", "specify the number of inputs", OFFSET(nb_inputs),
00054 AV_OPT_TYPE_INT, { .i64 = 2 }, 2, SWR_CH_MAX, FLAGS },
00055 {0}
00056 };
00057
00058 AVFILTER_DEFINE_CLASS(amerge);
00059
00060 static av_cold void uninit(AVFilterContext *ctx)
00061 {
00062 AMergeContext *am = ctx->priv;
00063 int i;
00064
00065 for (i = 0; i < am->nb_inputs; i++) {
00066 ff_bufqueue_discard_all(&am->in[i].queue);
00067 av_freep(&ctx->input_pads[i].name);
00068 }
00069 av_freep(&am->in);
00070 }
00071
00072 static int query_formats(AVFilterContext *ctx)
00073 {
00074 AMergeContext *am = ctx->priv;
00075 int64_t inlayout[SWR_CH_MAX], outlayout = 0;
00076 AVFilterFormats *formats;
00077 AVFilterChannelLayouts *layouts;
00078 int i, overlap = 0, nb_ch = 0;
00079
00080 for (i = 0; i < am->nb_inputs; i++) {
00081 if (!ctx->inputs[i]->in_channel_layouts ||
00082 !ctx->inputs[i]->in_channel_layouts->nb_channel_layouts) {
00083 av_log(ctx, AV_LOG_ERROR,
00084 "No channel layout for input %d\n", i + 1);
00085 return AVERROR(EINVAL);
00086 }
00087 inlayout[i] = ctx->inputs[i]->in_channel_layouts->channel_layouts[0];
00088 if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
00089 char buf[256];
00090 av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
00091 av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
00092 }
00093 am->in[i].nb_ch = av_get_channel_layout_nb_channels(inlayout[i]);
00094 if (outlayout & inlayout[i])
00095 overlap++;
00096 outlayout |= inlayout[i];
00097 nb_ch += am->in[i].nb_ch;
00098 }
00099 if (nb_ch > SWR_CH_MAX) {
00100 av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
00101 return AVERROR(EINVAL);
00102 }
00103 if (overlap) {
00104 av_log(ctx, AV_LOG_WARNING,
00105 "Input channel layouts overlap: "
00106 "output layout will be determined by the number of distinct input channels\n");
00107 for (i = 0; i < nb_ch; i++)
00108 am->route[i] = i;
00109 outlayout = av_get_default_channel_layout(nb_ch);
00110 if (!outlayout)
00111 outlayout = ((int64_t)1 << nb_ch) - 1;
00112 } else {
00113 int *route[SWR_CH_MAX];
00114 int c, out_ch_number = 0;
00115
00116 route[0] = am->route;
00117 for (i = 1; i < am->nb_inputs; i++)
00118 route[i] = route[i - 1] + am->in[i - 1].nb_ch;
00119 for (c = 0; c < 64; c++)
00120 for (i = 0; i < am->nb_inputs; i++)
00121 if ((inlayout[i] >> c) & 1)
00122 *(route[i]++) = out_ch_number++;
00123 }
00124 formats = ff_make_format_list(ff_packed_sample_fmts_array);
00125 ff_set_common_formats(ctx, formats);
00126 for (i = 0; i < am->nb_inputs; i++) {
00127 layouts = NULL;
00128 ff_add_channel_layout(&layouts, inlayout[i]);
00129 ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
00130 }
00131 layouts = NULL;
00132 ff_add_channel_layout(&layouts, outlayout);
00133 ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
00134 ff_set_common_samplerates(ctx, ff_all_samplerates());
00135 return 0;
00136 }
00137
00138 static int config_output(AVFilterLink *outlink)
00139 {
00140 AVFilterContext *ctx = outlink->src;
00141 AMergeContext *am = ctx->priv;
00142 AVBPrint bp;
00143 int i;
00144
00145 for (i = 1; i < am->nb_inputs; i++) {
00146 if (ctx->inputs[i]->sample_rate != ctx->inputs[0]->sample_rate) {
00147 av_log(ctx, AV_LOG_ERROR,
00148 "Inputs must have the same sample rate "
00149 "%d for in%d vs %d\n",
00150 ctx->inputs[i]->sample_rate, i, ctx->inputs[0]->sample_rate);
00151 return AVERROR(EINVAL);
00152 }
00153 }
00154 am->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
00155 outlink->sample_rate = ctx->inputs[0]->sample_rate;
00156 outlink->time_base = ctx->inputs[0]->time_base;
00157
00158 av_bprint_init(&bp, 0, 1);
00159 for (i = 0; i < am->nb_inputs; i++) {
00160 av_bprintf(&bp, "%sin%d:", i ? " + " : "", i);
00161 av_bprint_channel_layout(&bp, -1, ctx->inputs[i]->channel_layout);
00162 }
00163 av_bprintf(&bp, " -> out:");
00164 av_bprint_channel_layout(&bp, -1, ctx->outputs[0]->channel_layout);
00165 av_log(ctx, AV_LOG_VERBOSE, "%s\n", bp.str);
00166
00167 return 0;
00168 }
00169
00170 static int request_frame(AVFilterLink *outlink)
00171 {
00172 AVFilterContext *ctx = outlink->src;
00173 AMergeContext *am = ctx->priv;
00174 int i, ret;
00175
00176 for (i = 0; i < am->nb_inputs; i++)
00177 if (!am->in[i].nb_samples)
00178 if ((ret = ff_request_frame(ctx->inputs[i])) < 0)
00179 return ret;
00180 return 0;
00181 }
00182
00199 static inline void copy_samples(int nb_inputs, struct amerge_input in[],
00200 int *route, uint8_t *ins[],
00201 uint8_t **outs, int ns, int bps)
00202 {
00203 int *route_cur;
00204 int i, c, nb_ch = 0;
00205
00206 for (i = 0; i < nb_inputs; i++)
00207 nb_ch += in[i].nb_ch;
00208 while (ns--) {
00209 route_cur = route;
00210 for (i = 0; i < nb_inputs; i++) {
00211 for (c = 0; c < in[i].nb_ch; c++) {
00212 memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
00213 ins[i] += bps;
00214 }
00215 }
00216 *outs += nb_ch * bps;
00217 }
00218 }
00219
00220 static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
00221 {
00222 AVFilterContext *ctx = inlink->dst;
00223 AMergeContext *am = ctx->priv;
00224 AVFilterLink *const outlink = ctx->outputs[0];
00225 int input_number;
00226 int nb_samples, ns, i;
00227 AVFilterBufferRef *outbuf, *inbuf[SWR_CH_MAX];
00228 uint8_t *ins[SWR_CH_MAX], *outs;
00229
00230 for (input_number = 0; input_number < am->nb_inputs; input_number++)
00231 if (inlink == ctx->inputs[input_number])
00232 break;
00233 av_assert1(input_number < am->nb_inputs);
00234 ff_bufqueue_add(ctx, &am->in[input_number].queue, insamples);
00235 am->in[input_number].nb_samples += insamples->audio->nb_samples;
00236 nb_samples = am->in[0].nb_samples;
00237 for (i = 1; i < am->nb_inputs; i++)
00238 nb_samples = FFMIN(nb_samples, am->in[i].nb_samples);
00239 if (!nb_samples)
00240 return 0;
00241
00242 outbuf = ff_get_audio_buffer(ctx->outputs[0], AV_PERM_WRITE, nb_samples);
00243 outs = outbuf->data[0];
00244 for (i = 0; i < am->nb_inputs; i++) {
00245 inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
00246 ins[i] = inbuf[i]->data[0] +
00247 am->in[i].pos * am->in[i].nb_ch * am->bps;
00248 }
00249 avfilter_copy_buffer_ref_props(outbuf, inbuf[0]);
00250 outbuf->pts = inbuf[0]->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
00251 inbuf[0]->pts +
00252 av_rescale_q(am->in[0].pos,
00253 (AVRational){ 1, ctx->inputs[0]->sample_rate },
00254 ctx->outputs[0]->time_base);
00255
00256 outbuf->audio->nb_samples = nb_samples;
00257 outbuf->audio->channel_layout = outlink->channel_layout;
00258
00259 while (nb_samples) {
00260 ns = nb_samples;
00261 for (i = 0; i < am->nb_inputs; i++)
00262 ns = FFMIN(ns, inbuf[i]->audio->nb_samples - am->in[i].pos);
00263
00264
00265 switch (am->bps) {
00266 case 1:
00267 copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 1);
00268 break;
00269 case 2:
00270 copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 2);
00271 break;
00272 case 4:
00273 copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 4);
00274 break;
00275 default:
00276 copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, am->bps);
00277 break;
00278 }
00279
00280 nb_samples -= ns;
00281 for (i = 0; i < am->nb_inputs; i++) {
00282 am->in[i].nb_samples -= ns;
00283 am->in[i].pos += ns;
00284 if (am->in[i].pos == inbuf[i]->audio->nb_samples) {
00285 am->in[i].pos = 0;
00286 avfilter_unref_buffer(inbuf[i]);
00287 ff_bufqueue_get(&am->in[i].queue);
00288 inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
00289 ins[i] = inbuf[i] ? inbuf[i]->data[0] : NULL;
00290 }
00291 }
00292 }
00293 return ff_filter_samples(ctx->outputs[0], outbuf);
00294 }
00295
00296 static av_cold int init(AVFilterContext *ctx, const char *args)
00297 {
00298 AMergeContext *am = ctx->priv;
00299 int ret, i;
00300
00301 am->class = &amerge_class;
00302 av_opt_set_defaults(am);
00303 ret = av_set_options_string(am, args, "=", ":");
00304 if (ret < 0) {
00305 av_log(ctx, AV_LOG_ERROR, "Error parsing options: '%s'\n", args);
00306 return ret;
00307 }
00308 am->in = av_calloc(am->nb_inputs, sizeof(*am->in));
00309 if (!am->in)
00310 return AVERROR(ENOMEM);
00311 for (i = 0; i < am->nb_inputs; i++) {
00312 char *name = av_asprintf("in%d", i);
00313 AVFilterPad pad = {
00314 .name = name,
00315 .type = AVMEDIA_TYPE_AUDIO,
00316 .filter_samples = filter_samples,
00317 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
00318 };
00319 if (!name)
00320 return AVERROR(ENOMEM);
00321 ff_insert_inpad(ctx, i, &pad);
00322 }
00323 return 0;
00324 }
00325
00326 AVFilter avfilter_af_amerge = {
00327 .name = "amerge",
00328 .description = NULL_IF_CONFIG_SMALL("Merge two audio streams into "
00329 "a single multi-channel stream."),
00330 .priv_size = sizeof(AMergeContext),
00331 .init = init,
00332 .uninit = uninit,
00333 .query_formats = query_formats,
00334
00335 .inputs = (const AVFilterPad[]) { { .name = NULL } },
00336 .outputs = (const AVFilterPad[]) {
00337 { .name = "default",
00338 .type = AVMEDIA_TYPE_AUDIO,
00339 .config_props = config_output,
00340 .request_frame = request_frame, },
00341 { .name = NULL }
00342 },
00343 .priv_class = &amerge_class,
00344 };