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