00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "libavutil/common.h"
00029 #include "libavutil/fifo.h"
00030 #include "libavutil/mathematics.h"
00031 #include "libavutil/opt.h"
00032 #include "libavutil/parseutils.h"
00033
00034 #include "avfilter.h"
00035 #include "internal.h"
00036 #include "video.h"
00037
00038 typedef struct FPSContext {
00039 const AVClass *class;
00040
00041 AVFifoBuffer *fifo;
00042
00043
00044 int64_t first_pts;
00045 int64_t pts;
00046
00047 AVRational framerate;
00048 char *fps;
00049 int rounding;
00050
00051
00052 int frames_in;
00053 int frames_out;
00054 int dup;
00055 int drop;
00056 } FPSContext;
00057
00058 #define OFFSET(x) offsetof(FPSContext, x)
00059 #define V AV_OPT_FLAG_VIDEO_PARAM
00060 #define F AV_OPT_FLAG_FILTERING_PARAM
00061 static const AVOption fps_options[] = {
00062 { "fps", "A string describing desired output framerate", OFFSET(fps), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = V|F },
00063 { "round", "set rounding method for timestamps", OFFSET(rounding), AV_OPT_TYPE_INT, { .i64 = AV_ROUND_NEAR_INF }, 0, 5, V|F, "round" },
00064 { "zero", "round towards 0", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_ZERO }, 0, 5, V|F, "round" },
00065 { "inf", "round away from 0", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_INF }, 0, 5, V|F, "round" },
00066 { "down", "round towards -infty", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_DOWN }, 0, 5, V|F, "round" },
00067 { "up", "round towards +infty", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_UP }, 0, 5, V|F, "round" },
00068 { "near", "round to nearest", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_NEAR_INF }, 0, 5, V|F, "round" },
00069 { NULL },
00070 };
00071
00072 AVFILTER_DEFINE_CLASS(fps);
00073
00074 static av_cold int init(AVFilterContext *ctx, const char *args)
00075 {
00076 FPSContext *s = ctx->priv;
00077 const char *shorthand[] = { "fps", "round", NULL };
00078 int ret;
00079
00080 s->class = &fps_class;
00081 av_opt_set_defaults(s);
00082
00083 if ((ret = av_opt_set_from_string(s, args, shorthand, "=", ":")) < 0)
00084 return ret;
00085
00086 if ((ret = av_parse_video_rate(&s->framerate, s->fps)) < 0) {
00087 av_log(ctx, AV_LOG_ERROR, "Error parsing framerate %s.\n", s->fps);
00088 return ret;
00089 }
00090 av_opt_free(s);
00091
00092 if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFilterBufferRef*))))
00093 return AVERROR(ENOMEM);
00094
00095 av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den);
00096 return 0;
00097 }
00098
00099 static void flush_fifo(AVFifoBuffer *fifo)
00100 {
00101 while (av_fifo_size(fifo)) {
00102 AVFilterBufferRef *tmp;
00103 av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
00104 avfilter_unref_buffer(tmp);
00105 }
00106 }
00107
00108 static av_cold void uninit(AVFilterContext *ctx)
00109 {
00110 FPSContext *s = ctx->priv;
00111 if (s->fifo) {
00112 flush_fifo(s->fifo);
00113 av_fifo_free(s->fifo);
00114 }
00115
00116 av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
00117 "%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
00118 }
00119
00120 static int config_props(AVFilterLink* link)
00121 {
00122 FPSContext *s = link->src->priv;
00123
00124 link->time_base = av_inv_q(s->framerate);
00125 link->frame_rate= s->framerate;
00126 link->w = link->src->inputs[0]->w;
00127 link->h = link->src->inputs[0]->h;
00128 s->pts = AV_NOPTS_VALUE;
00129
00130 return 0;
00131 }
00132
00133 static int request_frame(AVFilterLink *outlink)
00134 {
00135 AVFilterContext *ctx = outlink->src;
00136 FPSContext *s = ctx->priv;
00137 int frames_out = s->frames_out;
00138 int ret = 0;
00139
00140 while (ret >= 0 && s->frames_out == frames_out)
00141 ret = ff_request_frame(ctx->inputs[0]);
00142
00143
00144 if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) {
00145 int i;
00146 for (i = 0; av_fifo_size(s->fifo); i++) {
00147 AVFilterBufferRef *buf;
00148
00149 av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
00150 buf->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base,
00151 outlink->time_base) + s->frames_out;
00152
00153 if ((ret = ff_filter_frame(outlink, buf)) < 0)
00154 return ret;
00155
00156 s->frames_out++;
00157 }
00158 return 0;
00159 }
00160
00161 return ret;
00162 }
00163
00164 static int write_to_fifo(AVFifoBuffer *fifo, AVFilterBufferRef *buf)
00165 {
00166 int ret;
00167
00168 if (!av_fifo_space(fifo) &&
00169 (ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo)))) {
00170 avfilter_unref_bufferp(&buf);
00171 return ret;
00172 }
00173
00174 av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL);
00175 return 0;
00176 }
00177
00178 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
00179 {
00180 AVFilterContext *ctx = inlink->dst;
00181 FPSContext *s = ctx->priv;
00182 AVFilterLink *outlink = ctx->outputs[0];
00183 int64_t delta;
00184 int i, ret;
00185
00186 s->frames_in++;
00187
00188 if (s->pts == AV_NOPTS_VALUE) {
00189 if (buf->pts != AV_NOPTS_VALUE) {
00190 ret = write_to_fifo(s->fifo, buf);
00191 if (ret < 0)
00192 return ret;
00193
00194 s->first_pts = s->pts = buf->pts;
00195 } else {
00196 av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
00197 "timestamp.\n");
00198 avfilter_unref_buffer(buf);
00199 s->drop++;
00200 }
00201 return 0;
00202 }
00203
00204
00205 if (buf->pts == AV_NOPTS_VALUE) {
00206 return write_to_fifo(s->fifo, buf);
00207 }
00208
00209
00210 delta = av_rescale_q_rnd(buf->pts - s->pts, inlink->time_base,
00211 outlink->time_base, s->rounding);
00212
00213 if (delta < 1) {
00214
00215 AVFilterBufferRef *tmp;
00216 int drop = av_fifo_size(s->fifo)/sizeof(AVFilterBufferRef*);
00217
00218 av_log(ctx, AV_LOG_DEBUG, "Dropping %d frame(s).\n", drop);
00219 s->drop += drop;
00220
00221 av_fifo_generic_read(s->fifo, &tmp, sizeof(tmp), NULL);
00222 flush_fifo(s->fifo);
00223 ret = write_to_fifo(s->fifo, tmp);
00224
00225 avfilter_unref_buffer(buf);
00226 return ret;
00227 }
00228
00229
00230 for (i = 0; i < delta; i++) {
00231 AVFilterBufferRef *buf_out;
00232 av_fifo_generic_read(s->fifo, &buf_out, sizeof(buf_out), NULL);
00233
00234
00235 if (!av_fifo_size(s->fifo) && i < delta - 1) {
00236 AVFilterBufferRef *dup = avfilter_ref_buffer(buf_out, ~0);
00237
00238 av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
00239 if (dup)
00240 ret = write_to_fifo(s->fifo, dup);
00241 else
00242 ret = AVERROR(ENOMEM);
00243
00244 if (ret < 0) {
00245 avfilter_unref_bufferp(&buf_out);
00246 avfilter_unref_bufferp(&buf);
00247 return ret;
00248 }
00249
00250 s->dup++;
00251 }
00252
00253 buf_out->pts = av_rescale_q(s->first_pts, inlink->time_base,
00254 outlink->time_base) + s->frames_out;
00255
00256 if ((ret = ff_filter_frame(outlink, buf_out)) < 0) {
00257 avfilter_unref_bufferp(&buf);
00258 return ret;
00259 }
00260
00261 s->frames_out++;
00262 }
00263 flush_fifo(s->fifo);
00264
00265 ret = write_to_fifo(s->fifo, buf);
00266 s->pts = s->first_pts + av_rescale_q(s->frames_out, outlink->time_base, inlink->time_base);
00267
00268 return ret;
00269 }
00270
00271 static const AVFilterPad avfilter_vf_fps_inputs[] = {
00272 {
00273 .name = "default",
00274 .type = AVMEDIA_TYPE_VIDEO,
00275 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
00276 .filter_frame = filter_frame,
00277 },
00278 { NULL }
00279 };
00280
00281 static const AVFilterPad avfilter_vf_fps_outputs[] = {
00282 {
00283 .name = "default",
00284 .type = AVMEDIA_TYPE_VIDEO,
00285 .rej_perms = AV_PERM_WRITE,
00286 .request_frame = request_frame,
00287 .config_props = config_props
00288 },
00289 { NULL }
00290 };
00291
00292 AVFilter avfilter_vf_fps = {
00293 .name = "fps",
00294 .description = NULL_IF_CONFIG_SMALL("Force constant framerate"),
00295
00296 .init = init,
00297 .uninit = uninit,
00298
00299 .priv_size = sizeof(FPSContext),
00300
00301 .inputs = avfilter_vf_fps_inputs,
00302 .outputs = avfilter_vf_fps_outputs,
00303 .priv_class = &fps_class,
00304 };