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