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