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
00050
00051 int frames_in;
00052 int frames_out;
00053 int dup;
00054 int drop;
00055 } FPSContext;
00056
00057 #define OFFSET(x) offsetof(FPSContext, x)
00058 #define V AV_OPT_FLAG_VIDEO_PARAM
00059 #define F AV_OPT_FLAG_FILTERING_PARAM
00060 static const AVOption fps_options[] = {
00061 { "fps", "A string describing desired output framerate", OFFSET(fps), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = V|F },
00062 { NULL },
00063 };
00064
00065 AVFILTER_DEFINE_CLASS(fps);
00066
00067 static av_cold int init(AVFilterContext *ctx, const char *args)
00068 {
00069 FPSContext *s = ctx->priv;
00070 int ret;
00071
00072 s->class = &fps_class;
00073 av_opt_set_defaults(s);
00074
00075 if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
00076 av_log(ctx, AV_LOG_ERROR, "Error parsing the options string %s.\n",
00077 args);
00078 return ret;
00079 }
00080
00081 if ((ret = av_parse_video_rate(&s->framerate, s->fps)) < 0) {
00082 av_log(ctx, AV_LOG_ERROR, "Error parsing framerate %s.\n", s->fps);
00083 return ret;
00084 }
00085 av_opt_free(s);
00086
00087 if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFilterBufferRef*))))
00088 return AVERROR(ENOMEM);
00089
00090 av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den);
00091 return 0;
00092 }
00093
00094 static void flush_fifo(AVFifoBuffer *fifo)
00095 {
00096 while (av_fifo_size(fifo)) {
00097 AVFilterBufferRef *tmp;
00098 av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
00099 avfilter_unref_buffer(tmp);
00100 }
00101 }
00102
00103 static av_cold void uninit(AVFilterContext *ctx)
00104 {
00105 FPSContext *s = ctx->priv;
00106 if (s->fifo) {
00107 flush_fifo(s->fifo);
00108 av_fifo_free(s->fifo);
00109 }
00110
00111 av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
00112 "%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
00113 }
00114
00115 static int config_props(AVFilterLink* link)
00116 {
00117 FPSContext *s = link->src->priv;
00118
00119 link->time_base = av_inv_q(s->framerate);
00120 link->frame_rate= s->framerate;
00121 link->w = link->src->inputs[0]->w;
00122 link->h = link->src->inputs[0]->h;
00123 s->pts = AV_NOPTS_VALUE;
00124
00125 return 0;
00126 }
00127
00128 static int request_frame(AVFilterLink *outlink)
00129 {
00130 AVFilterContext *ctx = outlink->src;
00131 FPSContext *s = ctx->priv;
00132 int frames_out = s->frames_out;
00133 int ret = 0;
00134
00135 while (ret >= 0 && s->frames_out == frames_out)
00136 ret = ff_request_frame(ctx->inputs[0]);
00137
00138
00139 if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) {
00140 int i;
00141 for (i = 0; av_fifo_size(s->fifo); i++) {
00142 AVFilterBufferRef *buf;
00143
00144 av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
00145 buf->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base,
00146 outlink->time_base) + s->frames_out;
00147
00148 if ((ret = ff_start_frame(outlink, buf)) < 0 ||
00149 (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
00150 (ret = ff_end_frame(outlink)) < 0)
00151 return ret;
00152
00153 s->frames_out++;
00154 }
00155 return 0;
00156 }
00157
00158 return ret;
00159 }
00160
00161 static int write_to_fifo(AVFifoBuffer *fifo, AVFilterBufferRef *buf)
00162 {
00163 int ret;
00164
00165 if (!av_fifo_space(fifo) &&
00166 (ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo)))) {
00167 avfilter_unref_bufferp(&buf);
00168 return ret;
00169 }
00170
00171 av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL);
00172 return 0;
00173 }
00174
00175 static int end_frame(AVFilterLink *inlink)
00176 {
00177 AVFilterContext *ctx = inlink->dst;
00178 FPSContext *s = ctx->priv;
00179 AVFilterLink *outlink = ctx->outputs[0];
00180 AVFilterBufferRef *buf = inlink->cur_buf;
00181 int64_t delta;
00182 int i, ret;
00183
00184 inlink->cur_buf = NULL;
00185 s->frames_in++;
00186
00187 if (s->pts == AV_NOPTS_VALUE) {
00188 if (buf->pts != AV_NOPTS_VALUE) {
00189 ret = write_to_fifo(s->fifo, buf);
00190 if (ret < 0)
00191 return ret;
00192
00193 s->first_pts = s->pts = buf->pts;
00194 } else {
00195 av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
00196 "timestamp.\n");
00197 avfilter_unref_buffer(buf);
00198 s->drop++;
00199 }
00200 return 0;
00201 }
00202
00203
00204 if (buf->pts == AV_NOPTS_VALUE) {
00205 return write_to_fifo(s->fifo, buf);
00206 }
00207
00208
00209 delta = av_rescale_q(buf->pts - s->pts, inlink->time_base,
00210 outlink->time_base);
00211
00212 if (delta < 1) {
00213
00214 AVFilterBufferRef *tmp;
00215 int drop = av_fifo_size(s->fifo)/sizeof(AVFilterBufferRef*);
00216
00217 av_log(ctx, AV_LOG_DEBUG, "Dropping %d frame(s).\n", drop);
00218 s->drop += drop;
00219
00220 av_fifo_generic_read(s->fifo, &tmp, sizeof(tmp), NULL);
00221 flush_fifo(s->fifo);
00222 ret = write_to_fifo(s->fifo, tmp);
00223
00224 avfilter_unref_buffer(buf);
00225 return ret;
00226 }
00227
00228
00229 for (i = 0; i < delta; i++) {
00230 AVFilterBufferRef *buf_out;
00231 av_fifo_generic_read(s->fifo, &buf_out, sizeof(buf_out), NULL);
00232
00233
00234 if (!av_fifo_size(s->fifo) && i < delta - 1) {
00235 AVFilterBufferRef *dup = avfilter_ref_buffer(buf_out, ~0);
00236
00237 av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
00238 if (dup)
00239 ret = write_to_fifo(s->fifo, dup);
00240 else
00241 ret = AVERROR(ENOMEM);
00242
00243 if (ret < 0) {
00244 avfilter_unref_bufferp(&buf_out);
00245 avfilter_unref_bufferp(&buf);
00246 return ret;
00247 }
00248
00249 s->dup++;
00250 }
00251
00252 buf_out->pts = av_rescale_q(s->first_pts, inlink->time_base,
00253 outlink->time_base) + s->frames_out;
00254
00255 if ((ret = ff_start_frame(outlink, buf_out)) < 0 ||
00256 (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
00257 (ret = ff_end_frame(outlink)) < 0) {
00258 avfilter_unref_bufferp(&buf);
00259 return ret;
00260 }
00261
00262 s->frames_out++;
00263 }
00264 flush_fifo(s->fifo);
00265
00266 ret = write_to_fifo(s->fifo, buf);
00267 s->pts = s->first_pts + av_rescale_q(s->frames_out, outlink->time_base, inlink->time_base);
00268
00269 return ret;
00270 }
00271
00272 static int null_start_frame(AVFilterLink *link, AVFilterBufferRef *buf)
00273 {
00274 return 0;
00275 }
00276
00277 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00278 {
00279 return 0;
00280 }
00281
00282 AVFilter avfilter_vf_fps = {
00283 .name = "fps",
00284 .description = NULL_IF_CONFIG_SMALL("Force constant framerate"),
00285
00286 .init = init,
00287 .uninit = uninit,
00288
00289 .priv_size = sizeof(FPSContext),
00290
00291 .inputs = (const AVFilterPad[]) {{ .name = "default",
00292 .type = AVMEDIA_TYPE_VIDEO,
00293 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
00294 .start_frame = null_start_frame,
00295 .draw_slice = null_draw_slice,
00296 .end_frame = end_frame, },
00297 { .name = NULL}},
00298 .outputs = (const AVFilterPad[]) {{ .name = "default",
00299 .type = AVMEDIA_TYPE_VIDEO,
00300 .rej_perms = AV_PERM_WRITE,
00301 .request_frame = request_frame,
00302 .config_props = config_props},
00303 { .name = NULL}},
00304 .priv_class = &fps_class,
00305 };