00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include <inttypes.h>
00027 #include <stdio.h>
00028
00029 #include "libavutil/avstring.h"
00030 #include "libavutil/eval.h"
00031 #include "libavutil/internal.h"
00032 #include "libavutil/mathematics.h"
00033 #include "libavutil/rational.h"
00034 #include "avfilter.h"
00035 #include "internal.h"
00036 #include "audio.h"
00037 #include "video.h"
00038
00039 static const char *const var_names[] = {
00040 "AVTB",
00041 "intb",
00042 "sr",
00043 NULL
00044 };
00045
00046 enum var_name {
00047 VAR_AVTB,
00048 VAR_INTB,
00049 VAR_SR,
00050 VAR_VARS_NB
00051 };
00052
00053 typedef struct {
00054 char tb_expr[256];
00055 double var_values[VAR_VARS_NB];
00056 } SetTBContext;
00057
00058 static av_cold int init(AVFilterContext *ctx, const char *args)
00059 {
00060 SetTBContext *settb = ctx->priv;
00061 av_strlcpy(settb->tb_expr, "intb", sizeof(settb->tb_expr));
00062
00063 if (args)
00064 sscanf(args, "%255[^:]", settb->tb_expr);
00065
00066 return 0;
00067 }
00068
00069 static int config_output_props(AVFilterLink *outlink)
00070 {
00071 AVFilterContext *ctx = outlink->src;
00072 SetTBContext *settb = ctx->priv;
00073 AVFilterLink *inlink = ctx->inputs[0];
00074 AVRational time_base;
00075 int ret;
00076 double res;
00077
00078 settb->var_values[VAR_AVTB] = av_q2d(AV_TIME_BASE_Q);
00079 settb->var_values[VAR_INTB] = av_q2d(inlink->time_base);
00080 settb->var_values[VAR_SR] = inlink->sample_rate;
00081
00082 outlink->w = inlink->w;
00083 outlink->h = inlink->h;
00084
00085 if ((ret = av_expr_parse_and_eval(&res, settb->tb_expr, var_names, settb->var_values,
00086 NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
00087 av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for timebase.\n", settb->tb_expr);
00088 return ret;
00089 }
00090 time_base = av_d2q(res, INT_MAX);
00091 if (time_base.num <= 0 || time_base.den <= 0) {
00092 av_log(ctx, AV_LOG_ERROR,
00093 "Invalid non-positive values for the timebase num:%d or den:%d.\n",
00094 time_base.num, time_base.den);
00095 return AVERROR(EINVAL);
00096 }
00097
00098 outlink->time_base = time_base;
00099 av_log(outlink->src, AV_LOG_VERBOSE, "tb:%d/%d -> tb:%d/%d\n",
00100 inlink ->time_base.num, inlink ->time_base.den,
00101 outlink->time_base.num, outlink->time_base.den);
00102
00103 return 0;
00104 }
00105
00106 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
00107 {
00108 AVFilterContext *ctx = inlink->dst;
00109 AVFilterLink *outlink = ctx->outputs[0];
00110
00111 if (av_cmp_q(inlink->time_base, outlink->time_base)) {
00112 int64_t orig_pts = frame->pts;
00113 frame->pts = av_rescale_q(frame->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, orig_pts,
00116 outlink->time_base.num, outlink->time_base.den, frame->pts);
00117 }
00118
00119 return ff_filter_frame(outlink, frame);
00120 }
00121
00122 #if CONFIG_SETTB_FILTER
00123 static const AVFilterPad avfilter_vf_settb_inputs[] = {
00124 {
00125 .name = "default",
00126 .type = AVMEDIA_TYPE_VIDEO,
00127 .get_video_buffer = ff_null_get_video_buffer,
00128 .filter_frame = filter_frame,
00129 },
00130 { NULL }
00131 };
00132
00133 static const AVFilterPad avfilter_vf_settb_outputs[] = {
00134 {
00135 .name = "default",
00136 .type = AVMEDIA_TYPE_VIDEO,
00137 .config_props = config_output_props,
00138 },
00139 { NULL }
00140 };
00141
00142 AVFilter avfilter_vf_settb = {
00143 .name = "settb",
00144 .description = NULL_IF_CONFIG_SMALL("Set timebase for the video output link."),
00145 .init = init,
00146
00147 .priv_size = sizeof(SetTBContext),
00148
00149 .inputs = avfilter_vf_settb_inputs,
00150 .outputs = avfilter_vf_settb_outputs,
00151 };
00152 #endif
00153
00154 #if CONFIG_ASETTB_FILTER
00155 static const AVFilterPad avfilter_af_asettb_inputs[] = {
00156 {
00157 .name = "default",
00158 .type = AVMEDIA_TYPE_AUDIO,
00159 .get_audio_buffer = ff_null_get_audio_buffer,
00160 .filter_frame = filter_frame,
00161 },
00162 { NULL }
00163 };
00164
00165 static const AVFilterPad avfilter_af_asettb_outputs[] = {
00166 {
00167 .name = "default",
00168 .type = AVMEDIA_TYPE_AUDIO,
00169 .config_props = config_output_props,
00170 },
00171 { NULL }
00172 };
00173
00174 AVFilter avfilter_af_asettb = {
00175 .name = "asettb",
00176 .description = NULL_IF_CONFIG_SMALL("Set timebase for the audio output link."),
00177 .init = init,
00178
00179 .priv_size = sizeof(SetTBContext),
00180 .inputs = avfilter_af_asettb_inputs,
00181 .outputs = avfilter_af_asettb_outputs,
00182 };
00183 #endif