2 * Copyright (c) 2011 Nicolas George <nicolas.george@normalesup.org>
4 * This file is part of FFmpeg.
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.
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.
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
23 * Stream (de)synchronization filter
26 #include "libavutil/eval.h"
33 static const char * const var_names[] = {
49 double var_values[VAR_NB];
51 AVFilterBufferRef *buf[QUEUE_SIZE];
53 /* buf[tail] is the oldest,
54 buf[(tail + nb) % QUEUE_SIZE] is where the next is added */
58 int eof; /* bitmask, one bit for each stream */
61 static const char *default_expr = "t1-t2";
63 static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
65 AStreamSyncContext *as = ctx->priv;
66 const char *expr = args0 ? args0 : default_expr;
69 r = av_expr_parse(&as->expr, expr, var_names,
70 NULL, NULL, NULL, NULL, 0, ctx);
72 av_log(ctx, AV_LOG_ERROR, "Error in expression \"%s\"\n", expr);
75 for (i = 0; i < 42; i++)
76 av_expr_eval(as->expr, as->var_values, NULL); /* exercize prng */
80 static int query_formats(AVFilterContext *ctx)
83 AVFilterFormats *formats, *rates;
84 AVFilterChannelLayouts *layouts;
86 for (i = 0; i < 2; i++) {
87 formats = ctx->inputs[i]->in_formats;
88 avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
89 avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
90 rates = ff_all_samplerates();
91 avfilter_formats_ref(rates, &ctx->inputs[i]->out_samplerates);
92 avfilter_formats_ref(rates, &ctx->outputs[i]->in_samplerates);
93 layouts = ctx->inputs[i]->in_channel_layouts;
94 ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
95 ff_channel_layouts_ref(layouts, &ctx->outputs[i]->in_channel_layouts);
100 static int config_output(AVFilterLink *outlink)
102 AVFilterContext *ctx = outlink->src;
103 int id = outlink == ctx->outputs[1];
105 outlink->sample_rate = ctx->inputs[id]->sample_rate;
106 outlink->time_base = ctx->inputs[id]->time_base;
110 static void send_out(AVFilterContext *ctx, int out_id)
112 AStreamSyncContext *as = ctx->priv;
113 struct buf_queue *queue = &as->queue[out_id];
114 AVFilterBufferRef *buf = queue->buf[queue->tail];
116 queue->buf[queue->tail] = NULL;
117 as->var_values[VAR_B1 + out_id]++;
118 as->var_values[VAR_S1 + out_id] += buf->audio->nb_samples;
119 if (buf->pts != AV_NOPTS_VALUE)
120 as->var_values[VAR_T1 + out_id] =
121 av_q2d(ctx->outputs[out_id]->time_base) * buf->pts;
122 as->var_values[VAR_T1 + out_id] += buf->audio->nb_samples /
123 (double)ctx->inputs[out_id]->sample_rate;
124 ff_filter_samples(ctx->outputs[out_id], buf);
126 queue->tail = (queue->tail + 1) % QUEUE_SIZE;
131 static void send_next(AVFilterContext *ctx)
133 AStreamSyncContext *as = ctx->priv;
137 if (!as->queue[as->next_out].nb)
139 send_out(ctx, as->next_out);
141 as->next_out = av_expr_eval(as->expr, as->var_values, NULL) >= 0;
143 for (i = 0; i < 2; i++)
144 if (as->queue[i].nb == QUEUE_SIZE)
148 static int request_frame(AVFilterLink *outlink)
150 AVFilterContext *ctx = outlink->src;
151 AStreamSyncContext *as = ctx->priv;
152 int id = outlink == ctx->outputs[1];
155 while (as->req[id] && !(as->eof & (1 << id))) {
156 if (as->queue[as->next_out].nb) {
159 as->eof |= 1 << as->next_out;
160 avfilter_request_frame(ctx->inputs[as->next_out]);
161 if (as->eof & (1 << as->next_out))
162 as->next_out = !as->next_out;
168 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
170 AVFilterContext *ctx = inlink->dst;
171 AStreamSyncContext *as = ctx->priv;
172 int id = inlink == ctx->inputs[1];
174 as->queue[id].buf[(as->queue[id].tail + as->queue[id].nb++) % QUEUE_SIZE] =
176 as->eof &= ~(1 << id);
180 AVFilter avfilter_af_astreamsync = {
181 .name = "astreamsync",
182 .description = NULL_IF_CONFIG_SMALL("Copy two streams of audio data "
183 "in a configurable order."),
184 .priv_size = sizeof(AStreamSyncContext),
186 .query_formats = query_formats,
188 .inputs = (const AVFilterPad[]) {
190 .type = AVMEDIA_TYPE_AUDIO,
191 .filter_samples = filter_samples,
192 .min_perms = AV_PERM_READ, },
194 .type = AVMEDIA_TYPE_AUDIO,
195 .filter_samples = filter_samples,
196 .min_perms = AV_PERM_READ, },
199 .outputs = (const AVFilterPad[]) {
201 .type = AVMEDIA_TYPE_AUDIO,
202 .config_props = config_output,
203 .request_frame = request_frame, },
205 .type = AVMEDIA_TYPE_AUDIO,
206 .config_props = config_output,
207 .request_frame = request_frame, },