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"
32 static const char * const var_names[] = {
48 double var_values[VAR_NB];
50 AVFilterBufferRef *buf[QUEUE_SIZE];
52 /* buf[tail] is the oldest,
53 buf[(tail + nb) % QUEUE_SIZE] is where the next is added */
57 int eof; /* bitmask, one bit for each stream */
60 static const char *default_expr = "t1-t2";
62 static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
64 AStreamSyncContext *as = ctx->priv;
65 const char *expr = args0 ? args0 : default_expr;
68 r = av_expr_parse(&as->expr, expr, var_names,
69 NULL, NULL, NULL, NULL, 0, ctx);
71 av_log(ctx, AV_LOG_ERROR, "Error in expression \"%s\"\n", expr);
74 for (i = 0; i < 42; i++)
75 av_expr_eval(as->expr, as->var_values, NULL); /* exercize prng */
79 static int query_formats(AVFilterContext *ctx)
82 AVFilterFormats *formats;
84 for (i = 0; i < 2; i++) {
85 formats = ctx->inputs[i]->in_formats;
86 avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
87 avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
88 formats = ctx->inputs[i]->in_packing;
89 avfilter_formats_ref(formats, &ctx->inputs[i]->out_packing);
90 avfilter_formats_ref(formats, &ctx->outputs[i]->in_packing);
91 formats = ctx->inputs[i]->in_chlayouts;
92 avfilter_formats_ref(formats, &ctx->inputs[i]->out_chlayouts);
93 avfilter_formats_ref(formats, &ctx->outputs[i]->in_chlayouts);
98 static int config_output(AVFilterLink *outlink)
100 AVFilterContext *ctx = outlink->src;
101 int id = outlink == ctx->outputs[1];
103 outlink->sample_rate = ctx->inputs[id]->sample_rate;
104 outlink->time_base = ctx->inputs[id]->time_base;
108 static void send_out(AVFilterContext *ctx, int out_id)
110 AStreamSyncContext *as = ctx->priv;
111 struct buf_queue *queue = &as->queue[out_id];
112 AVFilterBufferRef *buf = queue->buf[queue->tail];
114 queue->buf[queue->tail] = NULL;
115 as->var_values[VAR_B1 + out_id]++;
116 as->var_values[VAR_S1 + out_id] += buf->audio->nb_samples;
117 if (buf->pts != AV_NOPTS_VALUE)
118 as->var_values[VAR_T1 + out_id] =
119 av_q2d(ctx->outputs[out_id]->time_base) * buf->pts;
120 as->var_values[VAR_T1 + out_id] += buf->audio->nb_samples /
121 (double)ctx->inputs[out_id]->sample_rate;
122 avfilter_filter_samples(ctx->outputs[out_id], buf);
124 queue->tail = (queue->tail + 1) % QUEUE_SIZE;
129 static void send_next(AVFilterContext *ctx)
131 AStreamSyncContext *as = ctx->priv;
135 if (!as->queue[as->next_out].nb)
137 send_out(ctx, as->next_out);
139 as->next_out = av_expr_eval(as->expr, as->var_values, NULL) >= 0;
141 for (i = 0; i < 2; i++)
142 if (as->queue[i].nb == QUEUE_SIZE)
146 static int request_frame(AVFilterLink *outlink)
148 AVFilterContext *ctx = outlink->src;
149 AStreamSyncContext *as = ctx->priv;
150 int id = outlink == ctx->outputs[1];
153 while (as->req[id] && !(as->eof & (1 << id))) {
154 if (as->queue[as->next_out].nb) {
157 as->eof |= 1 << as->next_out;
158 avfilter_request_frame(ctx->inputs[as->next_out]);
159 if (as->eof & (1 << as->next_out))
160 as->next_out = !as->next_out;
166 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
168 AVFilterContext *ctx = inlink->dst;
169 AStreamSyncContext *as = ctx->priv;
170 int id = inlink == ctx->inputs[1];
172 as->queue[id].buf[(as->queue[id].tail + as->queue[id].nb++) % QUEUE_SIZE] =
174 as->eof &= ~(1 << id);
178 AVFilter avfilter_af_astreamsync = {
179 .name = "astreamsync",
180 .description = NULL_IF_CONFIG_SMALL("Copy two streams of audio data "
181 "in a configurable order."),
182 .priv_size = sizeof(AStreamSyncContext),
184 .query_formats = query_formats,
186 .inputs = (const AVFilterPad[]) {
188 .type = AVMEDIA_TYPE_AUDIO,
189 .filter_samples = filter_samples,
190 .min_perms = AV_PERM_READ, },
192 .type = AVMEDIA_TYPE_AUDIO,
193 .filter_samples = filter_samples,
194 .min_perms = AV_PERM_READ, },
197 .outputs = (const AVFilterPad[]) {
199 .type = AVMEDIA_TYPE_AUDIO,
200 .config_props = config_output,
201 .request_frame = request_frame, },
203 .type = AVMEDIA_TYPE_AUDIO,
204 .config_props = config_output,
205 .request_frame = request_frame, },