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 Libav.
00005  *
00006  * Libav 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  * Libav 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 Libav; 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 *var_names[] = {
00034     "E",
00035     "PHI",
00036     "PI",
00037     "AVTB",   /* default timebase 1/AV_TIME_BASE */
00038     "intb",   /* input timebase */
00039     NULL
00040 };
00041 
00042 enum var_name {
00043     VAR_E,
00044     VAR_PHI,
00045     VAR_PI,
00046     VAR_AVTB,
00047     VAR_INTB,
00048     VAR_VARS_NB
00049 };
00050 
00051 typedef struct {
00052     char tb_expr[256];
00053     double var_values[VAR_VARS_NB];
00054 } SetTBContext;
00055 
00056 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00057 {
00058     SetTBContext *settb = ctx->priv;
00059     av_strlcpy(settb->tb_expr, "intb", sizeof(settb->tb_expr));
00060 
00061     if (args)
00062         sscanf(args, "%255[^:]", settb->tb_expr);
00063 
00064     return 0;
00065 }
00066 
00067 static int config_output_props(AVFilterLink *outlink)
00068 {
00069     AVFilterContext *ctx = outlink->src;
00070     SetTBContext *settb = ctx->priv;
00071     AVFilterLink *inlink = ctx->inputs[0];
00072     AVRational time_base;
00073     int ret;
00074     double res;
00075 
00076     settb->var_values[VAR_E]    = M_E;
00077     settb->var_values[VAR_PHI]  = M_PHI;
00078     settb->var_values[VAR_PI]   = M_PI;
00079     settb->var_values[VAR_AVTB] = av_q2d(AV_TIME_BASE_Q);
00080     settb->var_values[VAR_INTB] = av_q2d(inlink->time_base);
00081 
00082     outlink->w = inlink->w;
00083     outlink->h = inlink->h;
00084 
00085     if ((ret = av_expr_parse_and_eval(&res, settb->tb_expr, var_names, settb->var_values,
00086                                       NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
00087         av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for timebase.\n", settb->tb_expr);
00088         return ret;
00089     }
00090     time_base = av_d2q(res, INT_MAX);
00091     if (time_base.num <= 0 || time_base.den <= 0) {
00092         av_log(ctx, AV_LOG_ERROR,
00093                "Invalid non-positive values for the timebase num:%d or den:%d.\n",
00094                time_base.num, time_base.den);
00095         return AVERROR(EINVAL);
00096     }
00097 
00098     outlink->time_base = time_base;
00099     av_log(outlink->src, AV_LOG_INFO, "tb:%d/%d -> tb:%d/%d\n",
00100            inlink ->time_base.num, inlink ->time_base.den,
00101            outlink->time_base.num, outlink->time_base.den);
00102 
00103     return 0;
00104 }
00105 
00106 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
00107 {
00108     AVFilterContext *ctx = inlink->dst;
00109     AVFilterLink *outlink = ctx->outputs[0];
00110     AVFilterBufferRef *picref2 = picref;
00111 
00112     if (av_cmp_q(inlink->time_base, outlink->time_base)) {
00113         picref2 = avfilter_ref_buffer(picref, ~0);
00114         picref2->pts = av_rescale_q(picref->pts, inlink->time_base, outlink->time_base);
00115         av_log(ctx, AV_LOG_DEBUG, "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
00116                inlink ->time_base.num, inlink ->time_base.den, picref ->pts,
00117                outlink->time_base.num, outlink->time_base.den, picref2->pts);
00118         avfilter_unref_buffer(picref);
00119     }
00120 
00121     avfilter_start_frame(outlink, picref2);
00122 }
00123 
00124 AVFilter avfilter_vf_settb = {
00125     .name      = "settb",
00126     .description = NULL_IF_CONFIG_SMALL("Set timebase for the output link."),
00127     .init      = init,
00128 
00129     .priv_size = sizeof(SetTBContext),
00130 
00131     .inputs    = (AVFilterPad[]) {{ .name             = "default",
00132                                     .type             = AVMEDIA_TYPE_VIDEO,
00133                                     .get_video_buffer = avfilter_null_get_video_buffer,
00134                                     .start_frame      = start_frame,
00135                                     .end_frame        = avfilter_null_end_frame },
00136                                   { .name = NULL }},
00137 
00138     .outputs   = (AVFilterPad[]) {{ .name            = "default",
00139                                     .type            = AVMEDIA_TYPE_VIDEO,
00140                                     .config_props    = config_output_props, },
00141                                   { .name = NULL}},
00142 };