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/fifo.h"
00029 #include "libavutil/mathematics.h"
00030 #include "libavutil/opt.h"
00031 #include "libavutil/parseutils.h"
00032
00033 #include "avfilter.h"
00034
00035 typedef struct FPSContext {
00036 const AVClass *class;
00037
00038 AVFifoBuffer *fifo;
00039
00040
00041 int64_t first_pts;
00042 int64_t pts;
00043
00044 AVRational framerate;
00045 char *fps;
00046
00047
00048 int frames_in;
00049 int frames_out;
00050 int dup;
00051 int drop;
00052 } FPSContext;
00053
00054 #define OFFSET(x) offsetof(FPSContext, x)
00055 #define V AV_OPT_FLAG_VIDEO_PARAM
00056 static const AVOption options[] = {
00057 { "fps", "A string describing desired output framerate", OFFSET(fps), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = V },
00058 { NULL },
00059 };
00060
00061 static const AVClass class = {
00062 .class_name = "FPS filter",
00063 .item_name = av_default_item_name,
00064 .option = options,
00065 .version = LIBAVUTIL_VERSION_INT,
00066 };
00067
00068 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00069 {
00070 FPSContext *s = ctx->priv;
00071 int ret;
00072
00073 s->class = &class;
00074 av_opt_set_defaults(s);
00075
00076 if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
00077 av_log(ctx, AV_LOG_ERROR, "Error parsing the options string %s.\n",
00078 args);
00079 return ret;
00080 }
00081
00082 if ((ret = av_parse_video_rate(&s->framerate, s->fps)) < 0) {
00083 av_log(ctx, AV_LOG_ERROR, "Error parsing framerate %s.\n", s->fps);
00084 return ret;
00085 }
00086 av_opt_free(s);
00087
00088 if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFilterBufferRef*))))
00089 return AVERROR(ENOMEM);
00090
00091 av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den);
00092 return 0;
00093 }
00094
00095 static void flush_fifo(AVFifoBuffer *fifo)
00096 {
00097 while (av_fifo_size(fifo)) {
00098 AVFilterBufferRef *tmp;
00099 av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
00100 avfilter_unref_buffer(tmp);
00101 }
00102 }
00103
00104 static av_cold void uninit(AVFilterContext *ctx)
00105 {
00106 FPSContext *s = ctx->priv;
00107 if (s->fifo) {
00108 flush_fifo(s->fifo);
00109 av_fifo_free(s->fifo);
00110 }
00111
00112 av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
00113 "%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
00114 }
00115
00116 static int config_props(AVFilterLink* link)
00117 {
00118 FPSContext *s = link->src->priv;
00119
00120 link->time_base = (AVRational){ s->framerate.den, s->framerate.num };
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 = avfilter_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 avfilter_start_frame(outlink, buf);
00149 avfilter_draw_slice(outlink, 0, outlink->h, 1);
00150 avfilter_end_frame(outlink);
00151 s->frames_out++;
00152 }
00153 return 0;
00154 }
00155
00156 return ret;
00157 }
00158
00159 static int write_to_fifo(AVFifoBuffer *fifo, AVFilterBufferRef *buf)
00160 {
00161 int ret;
00162
00163 if (!av_fifo_space(fifo) &&
00164 (ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo))))
00165 return ret;
00166
00167 av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL);
00168 return 0;
00169 }
00170
00171 static void end_frame(AVFilterLink *inlink)
00172 {
00173 AVFilterContext *ctx = inlink->dst;
00174 FPSContext *s = ctx->priv;
00175 AVFilterLink *outlink = ctx->outputs[0];
00176 AVFilterBufferRef *buf = inlink->cur_buf;
00177 int64_t delta;
00178 int i;
00179
00180 s->frames_in++;
00181
00182 if (s->pts == AV_NOPTS_VALUE) {
00183 if (buf->pts != AV_NOPTS_VALUE) {
00184 write_to_fifo(s->fifo, buf);
00185 s->first_pts = s->pts = buf->pts;
00186 } else {
00187 av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
00188 "timestamp.\n");
00189 avfilter_unref_buffer(buf);
00190 s->drop++;
00191 }
00192 return;
00193 }
00194
00195
00196 if (buf->pts == AV_NOPTS_VALUE) {
00197 write_to_fifo(s->fifo, buf);
00198 return;
00199 }
00200
00201
00202 delta = av_rescale_q(buf->pts - s->pts, inlink->time_base,
00203 outlink->time_base);
00204
00205 if (delta < 1) {
00206
00207 AVFilterBufferRef *tmp;
00208 int drop = av_fifo_size(s->fifo)/sizeof(AVFilterBufferRef*);
00209
00210 av_log(ctx, AV_LOG_DEBUG, "Dropping %d frame(s).\n", drop);
00211 s->drop += drop;
00212
00213 av_fifo_generic_read(s->fifo, &tmp, sizeof(tmp), NULL);
00214 flush_fifo(s->fifo);
00215 write_to_fifo(s->fifo, tmp);
00216
00217 avfilter_unref_buffer(buf);
00218 return;
00219 }
00220
00221
00222 for (i = 0; i < delta; i++) {
00223 AVFilterBufferRef *buf_out;
00224 av_fifo_generic_read(s->fifo, &buf_out, sizeof(buf_out), NULL);
00225
00226
00227 if (!av_fifo_size(s->fifo) && i < delta - 1) {
00228 av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
00229 write_to_fifo(s->fifo, avfilter_ref_buffer(buf_out, AV_PERM_READ));
00230 s->dup++;
00231 }
00232
00233 buf_out->pts = av_rescale_q(s->first_pts, inlink->time_base,
00234 outlink->time_base) + s->frames_out;
00235
00236 avfilter_start_frame(outlink, buf_out);
00237 avfilter_draw_slice(outlink, 0, outlink->h, 1);
00238 avfilter_end_frame(outlink);
00239 s->frames_out++;
00240 }
00241 flush_fifo(s->fifo);
00242
00243 write_to_fifo(s->fifo, buf);
00244 s->pts = s->first_pts + av_rescale_q(s->frames_out, outlink->time_base, inlink->time_base);
00245 }
00246
00247 static void null_start_frame(AVFilterLink *link, AVFilterBufferRef *buf)
00248 {
00249 }
00250
00251 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00252 {
00253 }
00254
00255 AVFilter avfilter_vf_fps = {
00256 .name = "fps",
00257 .description = NULL_IF_CONFIG_SMALL("Force constant framerate"),
00258
00259 .init = init,
00260 .uninit = uninit,
00261
00262 .priv_size = sizeof(FPSContext),
00263
00264 .inputs = (AVFilterPad[]) {{ .name = "default",
00265 .type = AVMEDIA_TYPE_VIDEO,
00266 .start_frame = null_start_frame,
00267 .draw_slice = null_draw_slice,
00268 .end_frame = end_frame, },
00269 { .name = NULL}},
00270 .outputs = (AVFilterPad[]) {{ .name = "default",
00271 .type = AVMEDIA_TYPE_VIDEO,
00272 .request_frame = request_frame,
00273 .config_props = config_props},
00274 { .name = NULL}},
00275 };