00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027
00028
00029 #include "libavutil/eval.h"
00030 #include "libavutil/mathematics.h"
00031 #include "avfilter.h"
00032
00033 static const char * const var_names[] = {
00034 "INTERLACED",
00035 "N",
00036 "POS",
00037 "PREV_INPTS",
00038 "PREV_OUTPTS",
00039 "PTS",
00040 "STARTPTS",
00041 "TB",
00042 NULL
00043 };
00044
00045 enum var_name {
00046 VAR_INTERLACED,
00047 VAR_N,
00048 VAR_POS,
00049 VAR_PREV_INPTS,
00050 VAR_PREV_OUTPTS,
00051 VAR_PTS,
00052 VAR_STARTPTS,
00053 VAR_TB,
00054 VAR_VARS_NB
00055 };
00056
00057 typedef struct {
00058 AVExpr *expr;
00059 double var_values[VAR_VARS_NB];
00060 } SetPTSContext;
00061
00062 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00063 {
00064 SetPTSContext *setpts = ctx->priv;
00065 int ret;
00066
00067 if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
00068 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
00069 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
00070 return ret;
00071 }
00072
00073 setpts->var_values[VAR_N ] = 0.0;
00074 setpts->var_values[VAR_PREV_INPTS ] = NAN;
00075 setpts->var_values[VAR_PREV_OUTPTS] = NAN;
00076 setpts->var_values[VAR_STARTPTS ] = NAN;
00077 return 0;
00078 }
00079
00080 static int config_input(AVFilterLink *inlink)
00081 {
00082 SetPTSContext *setpts = inlink->dst->priv;
00083
00084 setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
00085
00086 av_log(inlink->src, AV_LOG_INFO, "TB:%f\n", setpts->var_values[VAR_TB]);
00087 return 0;
00088 }
00089
00090 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
00091 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
00092
00093 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
00094 {
00095 SetPTSContext *setpts = inlink->dst->priv;
00096 double d;
00097 AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
00098
00099 if (isnan(setpts->var_values[VAR_STARTPTS]))
00100 setpts->var_values[VAR_STARTPTS] = TS2D(inpicref->pts);
00101
00102 setpts->var_values[VAR_INTERLACED] = inpicref->video->interlaced;
00103 setpts->var_values[VAR_PTS ] = TS2D(inpicref->pts);
00104 setpts->var_values[VAR_POS ] = inpicref->pos == -1 ? NAN : inpicref->pos;
00105
00106 d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
00107 outpicref->pts = D2TS(d);
00108
00109 #ifdef DEBUG
00110 av_log(inlink->dst, AV_LOG_DEBUG,
00111 "n:%"PRId64" interlaced:%d pos:%"PRId64" pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
00112 (int64_t)setpts->var_values[VAR_N],
00113 (int)setpts->var_values[VAR_INTERLACED],
00114 inpicref ->pos,
00115 inpicref ->pts, inpicref ->pts * av_q2d(inlink->time_base),
00116 outpicref->pts, outpicref->pts * av_q2d(inlink->time_base));
00117 #endif
00118
00119 setpts->var_values[VAR_N] += 1.0;
00120 setpts->var_values[VAR_PREV_INPTS ] = TS2D(inpicref ->pts);
00121 setpts->var_values[VAR_PREV_OUTPTS] = TS2D(outpicref->pts);
00122 avfilter_start_frame(inlink->dst->outputs[0], outpicref);
00123 }
00124
00125 static av_cold void uninit(AVFilterContext *ctx)
00126 {
00127 SetPTSContext *setpts = ctx->priv;
00128 av_expr_free(setpts->expr);
00129 setpts->expr = NULL;
00130 }
00131
00132 AVFilter avfilter_vf_setpts = {
00133 .name = "setpts",
00134 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
00135 .init = init,
00136 .uninit = uninit,
00137
00138 .priv_size = sizeof(SetPTSContext),
00139
00140 .inputs = (const AVFilterPad[]) {{ .name = "default",
00141 .type = AVMEDIA_TYPE_VIDEO,
00142 .get_video_buffer = avfilter_null_get_video_buffer,
00143 .config_props = config_input,
00144 .start_frame = start_frame, },
00145 { .name = NULL }},
00146 .outputs = (const AVFilterPad[]) {{ .name = "default",
00147 .type = AVMEDIA_TYPE_VIDEO, },
00148 { .name = NULL}},
00149 };