libavfilter/vf_settb.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Stefano Sabatini
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00026 #include "libavutil/avstring.h"
00027 #include "libavutil/eval.h"
00028 #include "libavutil/mathematics.h"
00029 #include "libavutil/rational.h"
00030 #include "avfilter.h"
00031 #include "internal.h"
00032 
00033 static const char * const var_names[] = {
00034     "AVTB",   /* default timebase 1/AV_TIME_BASE */
00035     "intb",   /* input timebase */
00036     NULL
00037 };
00038 
00039 enum var_name {
00040     VAR_AVTB,
00041     VAR_INTB,
00042     VAR_VARS_NB
00043 };
00044 
00045 typedef struct {
00046     char tb_expr[256];
00047     double var_values[VAR_VARS_NB];
00048 } SetTBContext;
00049 
00050 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00051 {
00052     SetTBContext *settb = ctx->priv;
00053     av_strlcpy(settb->tb_expr, "intb", sizeof(settb->tb_expr));
00054 
00055     if (args)
00056         sscanf(args, "%255[^:]", settb->tb_expr);
00057 
00058     return 0;
00059 }
00060 
00061 static int config_output_props(AVFilterLink *outlink)
00062 {
00063     AVFilterContext *ctx = outlink->src;
00064     SetTBContext *settb = ctx->priv;
00065     AVFilterLink *inlink = ctx->inputs[0];
00066     AVRational time_base;
00067     int ret;
00068     double res;
00069 
00070     settb->var_values[VAR_AVTB] = av_q2d(AV_TIME_BASE_Q);
00071     settb->var_values[VAR_INTB] = av_q2d(inlink->time_base);
00072 
00073     outlink->w = inlink->w;
00074     outlink->h = inlink->h;
00075 
00076     if ((ret = av_expr_parse_and_eval(&res, settb->tb_expr, var_names, settb->var_values,
00077                                       NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
00078         av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for timebase.\n", settb->tb_expr);
00079         return ret;
00080     }
00081     time_base = av_d2q(res, INT_MAX);
00082     if (time_base.num <= 0 || time_base.den <= 0) {
00083         av_log(ctx, AV_LOG_ERROR,
00084                "Invalid non-positive values for the timebase num:%d or den:%d.\n",
00085                time_base.num, time_base.den);
00086         return AVERROR(EINVAL);
00087     }
00088 
00089     outlink->time_base = time_base;
00090     av_log(outlink->src, AV_LOG_INFO, "tb:%d/%d -> tb:%d/%d\n",
00091            inlink ->time_base.num, inlink ->time_base.den,
00092            outlink->time_base.num, outlink->time_base.den);
00093 
00094     return 0;
00095 }
00096 
00097 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
00098 {
00099     AVFilterContext *ctx = inlink->dst;
00100     AVFilterLink *outlink = ctx->outputs[0];
00101     AVFilterBufferRef *picref2 = picref;
00102 
00103     if (av_cmp_q(inlink->time_base, outlink->time_base)) {
00104         picref2 = avfilter_ref_buffer(picref, ~0);
00105         picref2->pts = av_rescale_q(picref->pts, inlink->time_base, outlink->time_base);
00106         av_log(ctx, AV_LOG_DEBUG, "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
00107                inlink ->time_base.num, inlink ->time_base.den, picref ->pts,
00108                outlink->time_base.num, outlink->time_base.den, picref2->pts);
00109         avfilter_unref_buffer(picref);
00110     }
00111 
00112     avfilter_start_frame(outlink, picref2);
00113 }
00114 
00115 AVFilter avfilter_vf_settb = {
00116     .name      = "settb",
00117     .description = NULL_IF_CONFIG_SMALL("Set timebase for the output link."),
00118     .init      = init,
00119 
00120     .priv_size = sizeof(SetTBContext),
00121 
00122     .inputs    = (const AVFilterPad[]) {{ .name       = "default",
00123                                     .type             = AVMEDIA_TYPE_VIDEO,
00124                                     .get_video_buffer = avfilter_null_get_video_buffer,
00125                                     .start_frame      = start_frame,
00126                                     .end_frame        = avfilter_null_end_frame },
00127                                   { .name = NULL }},
00128 
00129     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
00130                                     .type            = AVMEDIA_TYPE_VIDEO,
00131                                     .config_props    = config_output_props, },
00132                                   { .name = NULL}},
00133 };