]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_astreamsync.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / af_astreamsync.c
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 /**
22  * @file
23  * Stream (de)synchronization filter
24  */
25
26 #include "libavutil/eval.h"
27 #include "avfilter.h"
28 #include "audio.h"
29 #include "internal.h"
30
31 #define QUEUE_SIZE 16
32
33 static const char * const var_names[] = {
34     "b1", "b2",
35     "s1", "s2",
36     "t1", "t2",
37     NULL
38 };
39
40 enum var_name {
41     VAR_B1, VAR_B2,
42     VAR_S1, VAR_S2,
43     VAR_T1, VAR_T2,
44     VAR_NB
45 };
46
47 typedef struct {
48     AVExpr *expr;
49     double var_values[VAR_NB];
50     struct buf_queue {
51         AVFilterBufferRef *buf[QUEUE_SIZE];
52         unsigned tail, nb;
53         /* buf[tail] is the oldest,
54            buf[(tail + nb) % QUEUE_SIZE] is where the next is added */
55     } queue[2];
56     int req[2];
57     int next_out;
58     int eof; /* bitmask, one bit for each stream */
59 } AStreamSyncContext;
60
61 static const char *default_expr = "t1-t2";
62
63 static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
64 {
65     AStreamSyncContext *as = ctx->priv;
66     const char *expr = args0 ? args0 : default_expr;
67     int r, i;
68
69     r = av_expr_parse(&as->expr, expr, var_names,
70                       NULL, NULL, NULL, NULL, 0, ctx);
71     if (r < 0) {
72         av_log(ctx, AV_LOG_ERROR, "Error in expression \"%s\"\n", expr);
73         return r;
74     }
75     for (i = 0; i < 42; i++)
76         av_expr_eval(as->expr, as->var_values, NULL); /* exercize prng */
77     return 0;
78 }
79
80 static int query_formats(AVFilterContext *ctx)
81 {
82     int i;
83     AVFilterFormats *formats;
84     AVFilterChannelLayouts *layouts;
85
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         layouts = ctx->inputs[i]->in_channel_layouts;
91         ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
92         ff_channel_layouts_ref(layouts, &ctx->outputs[i]->in_channel_layouts);
93     }
94     return 0;
95 }
96
97 static int config_output(AVFilterLink *outlink)
98 {
99     AVFilterContext *ctx = outlink->src;
100     int id = outlink == ctx->outputs[1];
101
102     outlink->sample_rate = ctx->inputs[id]->sample_rate;
103     outlink->time_base   = ctx->inputs[id]->time_base;
104     return 0;
105 }
106
107 static void send_out(AVFilterContext *ctx, int out_id)
108 {
109     AStreamSyncContext *as = ctx->priv;
110     struct buf_queue *queue = &as->queue[out_id];
111     AVFilterBufferRef *buf = queue->buf[queue->tail];
112
113     queue->buf[queue->tail] = NULL;
114     as->var_values[VAR_B1 + out_id]++;
115     as->var_values[VAR_S1 + out_id] += buf->audio->nb_samples;
116     if (buf->pts != AV_NOPTS_VALUE)
117         as->var_values[VAR_T1 + out_id] =
118             av_q2d(ctx->outputs[out_id]->time_base) * buf->pts;
119     as->var_values[VAR_T1 + out_id] += buf->audio->nb_samples /
120                                    (double)ctx->inputs[out_id]->sample_rate;
121     ff_filter_samples(ctx->outputs[out_id], buf);
122     queue->nb--;
123     queue->tail = (queue->tail + 1) % QUEUE_SIZE;
124     if (as->req[out_id])
125         as->req[out_id]--;
126 }
127
128 static void send_next(AVFilterContext *ctx)
129 {
130     AStreamSyncContext *as = ctx->priv;
131     int i;
132
133     while (1) {
134         if (!as->queue[as->next_out].nb)
135             break;
136         send_out(ctx, as->next_out);
137         if (!as->eof)
138             as->next_out = av_expr_eval(as->expr, as->var_values, NULL) >= 0;
139     }
140     for (i = 0; i < 2; i++)
141         if (as->queue[i].nb == QUEUE_SIZE)
142             send_out(ctx, i);
143 }
144
145 static int request_frame(AVFilterLink *outlink)
146 {
147     AVFilterContext *ctx = outlink->src;
148     AStreamSyncContext *as = ctx->priv;
149     int id = outlink == ctx->outputs[1];
150
151     as->req[id]++;
152     while (as->req[id] && !(as->eof & (1 << id))) {
153         if (as->queue[as->next_out].nb) {
154             send_next(ctx);
155         } else {
156             as->eof |= 1 << as->next_out;
157             avfilter_request_frame(ctx->inputs[as->next_out]);
158             if (as->eof & (1 << as->next_out))
159                 as->next_out = !as->next_out;
160         }
161     }
162     return 0;
163 }
164
165 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
166 {
167     AVFilterContext *ctx = inlink->dst;
168     AStreamSyncContext *as = ctx->priv;
169     int id = inlink == ctx->inputs[1];
170
171     as->queue[id].buf[(as->queue[id].tail + as->queue[id].nb++) % QUEUE_SIZE] =
172         insamples;
173     as->eof &= ~(1 << id);
174     send_next(ctx);
175 }
176
177 AVFilter avfilter_af_astreamsync = {
178     .name          = "astreamsync",
179     .description   = NULL_IF_CONFIG_SMALL("Copy two streams of audio data "
180                                           "in a configurable order."),
181     .priv_size     = sizeof(AStreamSyncContext),
182     .init          = init,
183     .query_formats = query_formats,
184
185     .inputs    = (const AVFilterPad[]) {
186         { .name             = "in1",
187           .type             = AVMEDIA_TYPE_AUDIO,
188           .filter_samples   = filter_samples,
189           .min_perms        = AV_PERM_READ, },
190         { .name             = "in2",
191           .type             = AVMEDIA_TYPE_AUDIO,
192           .filter_samples   = filter_samples,
193           .min_perms        = AV_PERM_READ, },
194         { .name = NULL }
195     },
196     .outputs   = (const AVFilterPad[]) {
197         { .name             = "out1",
198           .type             = AVMEDIA_TYPE_AUDIO,
199           .config_props     = config_output,
200           .request_frame    = request_frame, },
201         { .name             = "out2",
202           .type             = AVMEDIA_TYPE_AUDIO,
203           .config_props     = config_output,
204           .request_frame    = request_frame, },
205         { .name = NULL }
206     },
207 };