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 start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
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 = picref->pts;
00113 picref->pts = av_rescale_q(picref->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, picref->pts);
00117 }
00118 inlink->cur_buf = NULL;
00119
00120 return ff_start_frame(outlink, picref);
00121 }
00122
00123 static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
00124 {
00125 AVFilterContext *ctx = inlink->dst;
00126 AVFilterLink *outlink = ctx->outputs[0];
00127
00128 if (av_cmp_q(inlink->time_base, outlink->time_base)) {
00129 int64_t orig_pts = samplesref->pts;
00130 samplesref->pts = av_rescale_q(samplesref->pts, inlink->time_base, outlink->time_base);
00131 av_log(ctx, AV_LOG_DEBUG, "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
00132 inlink ->time_base.num, inlink ->time_base.den, orig_pts,
00133 outlink->time_base.num, outlink->time_base.den, samplesref->pts);
00134 }
00135
00136 return ff_filter_samples(outlink, samplesref);
00137 }
00138
00139 #if CONFIG_SETTB_FILTER
00140 AVFilter avfilter_vf_settb = {
00141 .name = "settb",
00142 .description = NULL_IF_CONFIG_SMALL("Set timebase for the video output link."),
00143 .init = init,
00144
00145 .priv_size = sizeof(SetTBContext),
00146
00147 .inputs = (const AVFilterPad[]) {
00148 { .name = "default",
00149 .type = AVMEDIA_TYPE_VIDEO,
00150 .get_video_buffer = ff_null_get_video_buffer,
00151 .start_frame = start_frame,
00152 .end_frame = ff_null_end_frame },
00153 { .name = NULL }
00154 },
00155 .outputs = (const AVFilterPad[]) {
00156 { .name = "default",
00157 .type = AVMEDIA_TYPE_VIDEO,
00158 .config_props = config_output_props, },
00159 { .name = NULL}
00160 },
00161 };
00162 #endif
00163
00164 #if CONFIG_ASETTB_FILTER
00165 AVFilter avfilter_af_asettb = {
00166 .name = "asettb",
00167 .description = NULL_IF_CONFIG_SMALL("Set timebase for the audio output link."),
00168 .init = init,
00169
00170 .priv_size = sizeof(SetTBContext),
00171
00172 .inputs = (const AVFilterPad[]) {
00173 { .name = "default",
00174 .type = AVMEDIA_TYPE_AUDIO,
00175 .get_audio_buffer = ff_null_get_audio_buffer,
00176 .filter_samples = filter_samples, },
00177 { .name = NULL }
00178 },
00179 .outputs = (const AVFilterPad[]) {
00180 { .name = "default",
00181 .type = AVMEDIA_TYPE_AUDIO,
00182 .config_props = config_output_props, },
00183 { .name = NULL}
00184 },
00185 };
00186 #endif