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