1/*
2 * Copyright (c) 2010 Stefano Sabatini
3 *
4 * This file is part of Libav.
5 *
6 * Libav 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 * Libav 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; 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 * Set timebase for the output link.
24 */
25
26#include "libavutil/avstring.h"
27#include "libavutil/eval.h"
28#include "libavutil/mathematics.h"
29#include "libavutil/rational.h"
30#include "avfilter.h"
31#include "internal.h"
32
33static const char *var_names[] = {
34    "E",
35    "PHI",
36    "PI",
37    "AVTB",   /* default timebase 1/AV_TIME_BASE */
38    "intb",   /* input timebase */
39    NULL
40};
41
42enum var_name {
43    VAR_E,
44    VAR_PHI,
45    VAR_PI,
46    VAR_AVTB,
47    VAR_INTB,
48    VAR_VARS_NB
49};
50
51typedef struct {
52    char tb_expr[256];
53    double var_values[VAR_VARS_NB];
54} SetTBContext;
55
56static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
57{
58    SetTBContext *settb = ctx->priv;
59    av_strlcpy(settb->tb_expr, "intb", sizeof(settb->tb_expr));
60
61    if (args)
62        sscanf(args, "%255[^:]", settb->tb_expr);
63
64    return 0;
65}
66
67static int config_output_props(AVFilterLink *outlink)
68{
69    AVFilterContext *ctx = outlink->src;
70    SetTBContext *settb = ctx->priv;
71    AVFilterLink *inlink = ctx->inputs[0];
72    AVRational time_base;
73    int ret;
74    double res;
75
76    settb->var_values[VAR_E]    = M_E;
77    settb->var_values[VAR_PHI]  = M_PHI;
78    settb->var_values[VAR_PI]   = M_PI;
79    settb->var_values[VAR_AVTB] = av_q2d(AV_TIME_BASE_Q);
80    settb->var_values[VAR_INTB] = av_q2d(inlink->time_base);
81
82    outlink->w = inlink->w;
83    outlink->h = inlink->h;
84
85    if ((ret = av_expr_parse_and_eval(&res, settb->tb_expr, var_names, settb->var_values,
86                                      NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
87        av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for timebase.\n", settb->tb_expr);
88        return ret;
89    }
90    time_base = av_d2q(res, INT_MAX);
91    if (time_base.num <= 0 || time_base.den <= 0) {
92        av_log(ctx, AV_LOG_ERROR,
93               "Invalid non-positive values for the timebase num:%d or den:%d.\n",
94               time_base.num, time_base.den);
95        return AVERROR(EINVAL);
96    }
97
98    outlink->time_base = time_base;
99    av_log(outlink->src, AV_LOG_INFO, "tb:%d/%d -> tb:%d/%d\n",
100           inlink ->time_base.num, inlink ->time_base.den,
101           outlink->time_base.num, outlink->time_base.den);
102
103    return 0;
104}
105
106static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
107{
108    AVFilterContext *ctx = inlink->dst;
109    AVFilterLink *outlink = ctx->outputs[0];
110    AVFilterBufferRef *picref2 = picref;
111
112    if (av_cmp_q(inlink->time_base, outlink->time_base)) {
113        picref2 = avfilter_ref_buffer(picref, ~0);
114        picref2->pts = av_rescale_q(picref->pts, inlink->time_base, outlink->time_base);
115        av_log(ctx, AV_LOG_DEBUG, "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
116               inlink ->time_base.num, inlink ->time_base.den, picref ->pts,
117               outlink->time_base.num, outlink->time_base.den, picref2->pts);
118        avfilter_unref_buffer(picref);
119    }
120
121    avfilter_start_frame(outlink, picref2);
122}
123
124AVFilter avfilter_vf_settb = {
125    .name      = "settb",
126    .description = NULL_IF_CONFIG_SMALL("Set timebase for the output link."),
127    .init      = init,
128
129    .priv_size = sizeof(SetTBContext),
130
131    .inputs    = (AVFilterPad[]) {{ .name             = "default",
132                                    .type             = AVMEDIA_TYPE_VIDEO,
133                                    .get_video_buffer = avfilter_null_get_video_buffer,
134                                    .start_frame      = start_frame,
135                                    .end_frame        = avfilter_null_end_frame },
136                                  { .name = NULL }},
137
138    .outputs   = (AVFilterPad[]) {{ .name            = "default",
139                                    .type            = AVMEDIA_TYPE_VIDEO,
140                                    .config_props    = config_output_props, },
141                                  { .name = NULL}},
142};
143