]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_framerate.c
Merge commit '9d58639e270f7612874681e0ca30fa461e2667b7'
[ffmpeg] / libavfilter / vf_framerate.c
1 /*
2  * Copyright (C) 2012 Mark Himsley
3  *
4  * get_scene_score() Copyright (c) 2011 Stefano Sabatini
5  * taken from libavfilter/vf_select.c
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 /**
25  * @file
26  * filter for upsampling or downsampling a progressive source
27  */
28
29 #define DEBUG
30
31 #include "libavutil/avassert.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/pixelutils.h"
37
38 #include "avfilter.h"
39 #include "internal.h"
40 #include "video.h"
41
42 #define N_SRCE 3
43
44 typedef struct FrameRateContext {
45     const AVClass *class;
46     // parameters
47     AVRational dest_frame_rate;         ///< output frames per second
48     int flags;                          ///< flags affecting frame rate conversion algorithm
49     double scene_score;                 ///< score that denotes a scene change has happened
50     int interp_start;                   ///< start of range to apply linear interpolation
51     int interp_end;                     ///< end of range to apply linear interpolation
52
53     int line_size[4];                   ///< bytes of pixel data per line for each plane
54     int vsub;
55
56     int frst, next, prev, crnt, last;
57     int pending_srce_frames;            ///< how many input frames are still waiting to be processed
58     int flush;                          ///< are we flushing final frames
59     int pending_end_frame;              ///< flag indicating we are waiting to call filter_frame()
60
61     AVRational srce_time_base;          ///< timebase of source
62
63     AVRational dest_time_base;          ///< timebase of destination
64     int32_t dest_frame_num;
65     int64_t last_dest_frame_pts;        ///< pts of the last frame output
66     int64_t average_srce_pts_dest_delta;///< average input pts delta converted from input rate to output rate
67     int64_t average_dest_pts_delta;     ///< calculated average output pts delta
68
69     av_pixelutils_sad_fn sad;           ///< Sum of the absolute difference function (scene detect only)
70     double prev_mafd;                   ///< previous MAFD                           (scene detect only)
71
72     AVFrame *srce[N_SRCE];              ///< buffered source frames
73     int64_t srce_pts_dest[N_SRCE];      ///< pts for source frames scaled to output timebase
74     int64_t pts;                        ///< pts of frame we are working on
75 } FrameRateContext;
76
77 #define OFFSET(x) offsetof(FrameRateContext, x)
78 #define V AV_OPT_FLAG_VIDEO_PARAM
79 #define F AV_OPT_FLAG_FILTERING_PARAM
80 #define FRAMERATE_FLAG_SCD 01
81
82 static const AVOption framerate_options[] = {
83     {"fps",                 "required output frames per second rate", OFFSET(dest_frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="50"},             0,       INT_MAX, V|F },
84
85     {"interp_start",        "point to start linear interpolation",    OFFSET(interp_start),    AV_OPT_TYPE_INT,      {.i64=15},                 0,       255,     V|F },
86     {"interp_end",          "point to end linear interpolation",      OFFSET(interp_end),      AV_OPT_TYPE_INT,      {.i64=240},                0,       255,     V|F },
87     {"scene",               "scene change level",                     OFFSET(scene_score),     AV_OPT_TYPE_DOUBLE,   {.dbl=7.0},                0,       INT_MAX, V|F },
88
89     {"flags",               "set flags",                              OFFSET(flags),           AV_OPT_TYPE_FLAGS,    {.i64=1},                  0,       INT_MAX, V|F, "flags" },
90     {"scene_change_detect", "enable scene change detection",          0,                       AV_OPT_TYPE_CONST,    {.i64=FRAMERATE_FLAG_SCD}, INT_MIN, INT_MAX, V|F, "flags" },
91     {"scd",                 "enable scene change detection",          0,                       AV_OPT_TYPE_CONST,    {.i64=FRAMERATE_FLAG_SCD}, INT_MIN, INT_MAX, V|F, "flags" },
92
93     {NULL}
94 };
95
96 AVFILTER_DEFINE_CLASS(framerate);
97
98 static void next_source(AVFilterContext *ctx)
99 {
100     FrameRateContext *s = ctx->priv;
101     int i;
102
103     ff_dlog(ctx,  "next_source()\n");
104
105     if (s->srce[s->last] && s->srce[s->last] != s->srce[s->last-1]) {
106         ff_dlog(ctx, "next_source() unlink %d\n", s->last);
107         av_frame_free(&s->srce[s->last]);
108     }
109     for (i = s->last; i > s->frst; i--) {
110         ff_dlog(ctx, "next_source() copy %d to %d\n", i - 1, i);
111         s->srce[i] = s->srce[i - 1];
112     }
113     ff_dlog(ctx, "next_source() make %d null\n", s->frst);
114     s->srce[s->frst] = NULL;
115 }
116
117 static double get_scene_score(AVFilterContext *ctx, AVFrame *crnt, AVFrame *next)
118 {
119     FrameRateContext *s = ctx->priv;
120     double ret = 0;
121
122     ff_dlog(ctx, "get_scene_score()\n");
123
124     if (crnt &&
125         crnt->height == next->height &&
126         crnt->width  == next->width) {
127         int x, y;
128         int64_t sad;
129         double mafd, diff;
130         uint8_t *p1 = crnt->data[0];
131         uint8_t *p2 = next->data[0];
132         const int p1_linesize = crnt->linesize[0];
133         const int p2_linesize = next->linesize[0];
134
135         ff_dlog(ctx, "get_scene_score() process\n");
136
137         for (sad = y = 0; y < crnt->height; y += 8) {
138             for (x = 0; x < p1_linesize; x += 8) {
139                 sad += s->sad(p1 + y * p1_linesize + x,
140                               p1_linesize,
141                               p2 + y * p2_linesize + x,
142                               p2_linesize);
143             }
144         }
145         emms_c();
146         mafd = sad / (crnt->height * crnt->width * 3);
147         diff = fabs(mafd - s->prev_mafd);
148         ret  = av_clipf(FFMIN(mafd, diff), 0, 100.0);
149         s->prev_mafd = mafd;
150     }
151         ff_dlog(ctx, "get_scene_score() result is:%f\n", ret);
152     return ret;
153 }
154
155 static int process_work_frame(AVFilterContext *ctx, int stop)
156 {
157     FrameRateContext *s = ctx->priv;
158     AVFilterLink *outlink = ctx->outputs[0];
159     int64_t work_next_pts;
160     AVFrame *copy_src1, *copy_src2, *work;
161     int interpolate;
162
163     ff_dlog(ctx, "process_work_frame()\n");
164
165     ff_dlog(ctx, "process_work_frame() pending_input_frames %d\n", s->pending_srce_frames);
166
167     if (s->srce[s->prev]) ff_dlog(ctx, "process_work_frame() srce prev pts:%"PRId64"\n", s->srce[s->prev]->pts);
168     if (s->srce[s->crnt]) ff_dlog(ctx, "process_work_frame() srce crnt pts:%"PRId64"\n", s->srce[s->crnt]->pts);
169     if (s->srce[s->next]) ff_dlog(ctx, "process_work_frame() srce next pts:%"PRId64"\n", s->srce[s->next]->pts);
170
171     if (!s->srce[s->crnt]) {
172         // the filter cannot do anything
173         ff_dlog(ctx, "process_work_frame() no current frame cached: move on to next frame, do not output a frame\n");
174         next_source(ctx);
175         return 0;
176     }
177
178     work_next_pts = s->pts + s->average_dest_pts_delta;
179
180     ff_dlog(ctx, "process_work_frame() work crnt pts:%"PRId64"\n", s->pts);
181     ff_dlog(ctx, "process_work_frame() work next pts:%"PRId64"\n", work_next_pts);
182     if (s->srce[s->prev])
183         ff_dlog(ctx, "process_work_frame() srce prev pts:%"PRId64" at dest time base:%u/%u\n",
184             s->srce_pts_dest[s->prev], s->dest_time_base.num, s->dest_time_base.den);
185     if (s->srce[s->crnt])
186         ff_dlog(ctx, "process_work_frame() srce crnt pts:%"PRId64" at dest time base:%u/%u\n",
187             s->srce_pts_dest[s->crnt], s->dest_time_base.num, s->dest_time_base.den);
188     if (s->srce[s->next])
189         ff_dlog(ctx, "process_work_frame() srce next pts:%"PRId64" at dest time base:%u/%u\n",
190             s->srce_pts_dest[s->next], s->dest_time_base.num, s->dest_time_base.den);
191
192     av_assert0(s->srce[s->next]);
193
194     // should filter be skipping input frame (output frame rate is lower than input frame rate)
195     if (!s->flush && s->pts >= s->srce_pts_dest[s->next]) {
196         ff_dlog(ctx, "process_work_frame() work crnt pts >= srce next pts: SKIP FRAME, move on to next frame, do not output a frame\n");
197         next_source(ctx);
198         s->pending_srce_frames--;
199         return 0;
200     }
201
202     // calculate interpolation
203     interpolate = (int) ((s->pts - s->srce_pts_dest[s->crnt]) * 256.0 / s->average_srce_pts_dest_delta);
204     ff_dlog(ctx, "process_work_frame() interpolate:%d/256\n", interpolate);
205     copy_src1 = s->srce[s->crnt];
206     if (interpolate > s->interp_end) {
207         ff_dlog(ctx, "process_work_frame() source is:NEXT\n");
208         copy_src1 = s->srce[s->next];
209     }
210     if (s->srce[s->prev] && interpolate < -s->interp_end) {
211         ff_dlog(ctx, "process_work_frame() source is:PREV\n");
212         copy_src1 = s->srce[s->prev];
213     }
214
215     // decide whether to blend two frames
216     if ((interpolate >= s->interp_start && interpolate <= s->interp_end) || (interpolate <= -s->interp_start && interpolate >= -s->interp_end)) {
217         double interpolate_scene_score = 0;
218
219         if (interpolate > 0) {
220             ff_dlog(ctx, "process_work_frame() interpolate source is:NEXT\n");
221             copy_src2 = s->srce[s->next];
222         } else {
223             ff_dlog(ctx, "process_work_frame() interpolate source is:PREV\n");
224             copy_src2 = s->srce[s->prev];
225         }
226         if ((s->flags & FRAMERATE_FLAG_SCD) && copy_src2) {
227             interpolate_scene_score = get_scene_score(ctx, copy_src1, copy_src2);
228             ff_dlog(ctx, "process_work_frame() interpolate scene score:%f\n", interpolate_scene_score);
229         }
230         // decide if the shot-change detection allows us to blend two frames
231         if (interpolate_scene_score < s->scene_score && copy_src2) {
232             uint16_t src2_factor = abs(interpolate);
233             uint16_t src1_factor = 256 - src2_factor;
234             int plane, line, pixel;
235
236             // get work-space for output frame
237             work = ff_get_video_buffer(outlink, outlink->w, outlink->h);
238             if (!work)
239                 return AVERROR(ENOMEM);
240
241             av_frame_copy_props(work, s->srce[s->crnt]);
242
243             ff_dlog(ctx, "process_work_frame() INTERPOLATE to create work frame\n");
244             for (plane = 0; plane < 4 && copy_src1->data[plane] && copy_src2->data[plane]; plane++) {
245                 int cpy_line_width = s->line_size[plane];
246                 uint8_t *cpy_src1_data = copy_src1->data[plane];
247                 int cpy_src1_line_size = copy_src1->linesize[plane];
248                 uint8_t *cpy_src2_data = copy_src2->data[plane];
249                 int cpy_src2_line_size = copy_src2->linesize[plane];
250                 int cpy_src_h = (plane > 0 && plane < 3) ? (copy_src1->height >> s->vsub) : (copy_src1->height);
251                 uint8_t *cpy_dst_data = work->data[plane];
252                 int cpy_dst_line_size = work->linesize[plane];
253                 if (plane <1 || plane >2) {
254                     // luma or alpha
255                     for (line = 0; line < cpy_src_h; line++) {
256                         for (pixel = 0; pixel < cpy_line_width; pixel++) {
257                             // integer version of (src1 * src1_factor) + (src2 + src2_factor) + 0.5
258                             // 0.5 is for rounding
259                             // 128 is the integer representation of 0.5 << 8
260                             cpy_dst_data[pixel] = ((cpy_src1_data[pixel] * src1_factor) + (cpy_src2_data[pixel] * src2_factor) + 128) >> 8;
261                         }
262                         cpy_src1_data += cpy_src1_line_size;
263                         cpy_src2_data += cpy_src2_line_size;
264                         cpy_dst_data += cpy_dst_line_size;
265                     }
266                 } else {
267                     // chroma
268                     for (line = 0; line < cpy_src_h; line++) {
269                         for (pixel = 0; pixel < cpy_line_width; pixel++) {
270                             // as above
271                             // because U and V are based around 128 we have to subtract 128 from the components.
272                             // 32896 is the integer representation of 128.5 << 8
273                             cpy_dst_data[pixel] = (((cpy_src1_data[pixel] - 128) * src1_factor) + ((cpy_src2_data[pixel] - 128) * src2_factor) + 32896) >> 8;
274                         }
275                         cpy_src1_data += cpy_src1_line_size;
276                         cpy_src2_data += cpy_src2_line_size;
277                         cpy_dst_data += cpy_dst_line_size;
278                     }
279                 }
280             }
281             goto copy_done;
282         }
283         else {
284             ff_dlog(ctx, "process_work_frame() CUT - DON'T INTERPOLATE\n");
285         }
286     }
287
288     ff_dlog(ctx, "process_work_frame() COPY to the work frame\n");
289     // copy the frame we decided is our base source
290     work = av_frame_clone(copy_src1);
291     if (!work)
292         return AVERROR(ENOMEM);
293
294 copy_done:
295     work->pts = s->pts;
296
297     // should filter be re-using input frame (output frame rate is higher than input frame rate)
298     if (!s->flush && (work_next_pts + s->average_dest_pts_delta) < (s->srce_pts_dest[s->crnt] + s->average_srce_pts_dest_delta)) {
299         ff_dlog(ctx, "process_work_frame() REPEAT FRAME\n");
300     } else {
301         ff_dlog(ctx, "process_work_frame() CONSUME FRAME, move to next frame\n");
302         s->pending_srce_frames--;
303         next_source(ctx);
304     }
305     ff_dlog(ctx, "process_work_frame() output a frame\n");
306     s->dest_frame_num++;
307     if (stop)
308         s->pending_end_frame = 0;
309     s->last_dest_frame_pts = work->pts;
310
311     return ff_filter_frame(ctx->outputs[0], work);
312 }
313
314 static void set_srce_frame_dest_pts(AVFilterContext *ctx)
315 {
316     FrameRateContext *s = ctx->priv;
317
318     ff_dlog(ctx, "set_srce_frame_output_pts()\n");
319
320     // scale the input pts from the timebase difference between input and output
321     if (s->srce[s->prev])
322         s->srce_pts_dest[s->prev] = av_rescale_q(s->srce[s->prev]->pts, s->srce_time_base, s->dest_time_base);
323     if (s->srce[s->crnt])
324         s->srce_pts_dest[s->crnt] = av_rescale_q(s->srce[s->crnt]->pts, s->srce_time_base, s->dest_time_base);
325     if (s->srce[s->next])
326         s->srce_pts_dest[s->next] = av_rescale_q(s->srce[s->next]->pts, s->srce_time_base, s->dest_time_base);
327 }
328
329 static void set_work_frame_pts(AVFilterContext *ctx)
330 {
331     FrameRateContext *s = ctx->priv;
332     int64_t pts, average_srce_pts_delta = 0;
333
334     ff_dlog(ctx, "set_work_frame_pts()\n");
335
336     av_assert0(s->srce[s->next]);
337     av_assert0(s->srce[s->crnt]);
338
339     ff_dlog(ctx, "set_work_frame_pts() srce crnt pts:%"PRId64"\n", s->srce[s->crnt]->pts);
340     ff_dlog(ctx, "set_work_frame_pts() srce next pts:%"PRId64"\n", s->srce[s->next]->pts);
341     if (s->srce[s->prev])
342         ff_dlog(ctx, "set_work_frame_pts() srce prev pts:%"PRId64"\n", s->srce[s->prev]->pts);
343
344     average_srce_pts_delta = s->average_srce_pts_dest_delta;
345     ff_dlog(ctx, "set_work_frame_pts() initial average srce pts:%"PRId64"\n", average_srce_pts_delta);
346
347     // calculate the PTS delta
348     if ((pts = (s->srce[s->next]->pts - s->srce[s->crnt]->pts))) {
349         average_srce_pts_delta = average_srce_pts_delta?((average_srce_pts_delta+pts)>>1):pts;
350     } else if (s->srce[s->prev] && (pts = (s->srce[s->crnt]->pts - s->srce[s->prev]->pts))) {
351         average_srce_pts_delta = average_srce_pts_delta?((average_srce_pts_delta+pts)>>1):pts;
352     }
353
354     s->average_srce_pts_dest_delta = av_rescale_q(average_srce_pts_delta, s->srce_time_base, s->dest_time_base);
355     ff_dlog(ctx, "set_work_frame_pts() average srce pts:%"PRId64"\n", average_srce_pts_delta);
356     ff_dlog(ctx, "set_work_frame_pts() average srce pts:%"PRId64" at dest time base:%u/%u\n",
357             s->average_srce_pts_dest_delta, s->dest_time_base.num, s->dest_time_base.den);
358
359     set_srce_frame_dest_pts(ctx);
360
361     if (ctx->inputs[0] && !s->average_dest_pts_delta) {
362         int64_t d = av_q2d(av_inv_q(av_mul_q(s->srce_time_base, s->dest_frame_rate)));
363         if (d == 0) { // FIXME
364             av_log(ctx, AV_LOG_WARNING, "Buggy path reached, use settb filter before this filter!\n");
365             d = av_q2d(av_mul_q(ctx->inputs[0]->time_base, s->dest_frame_rate));
366         }
367         s->average_dest_pts_delta = av_rescale_q(d, s->srce_time_base, s->dest_time_base);
368         ff_dlog(ctx, "set_frame_pts() average output pts from input timebase\n");
369         ff_dlog(ctx, "set_work_frame_pts() average dest pts delta:%"PRId64"\n", s->average_dest_pts_delta);
370     }
371
372     if (!s->dest_frame_num) {
373         s->pts = s->last_dest_frame_pts = s->srce_pts_dest[s->crnt];
374     } else {
375         s->pts = s->last_dest_frame_pts + s->average_dest_pts_delta;
376     }
377
378     ff_dlog(ctx, "set_work_frame_pts() calculated pts:%"PRId64" at dest time base:%u/%u\n",
379             s->pts, s->dest_time_base.num, s->dest_time_base.den);
380 }
381
382 static av_cold int init(AVFilterContext *ctx)
383 {
384     FrameRateContext *s = ctx->priv;
385
386     s->dest_frame_num = 0;
387
388     s->crnt = (N_SRCE)>>1;
389     s->last = N_SRCE - 1;
390
391     s->next = s->crnt - 1;
392     s->prev = s->crnt + 1;
393
394     return 0;
395 }
396
397 static av_cold void uninit(AVFilterContext *ctx)
398 {
399     FrameRateContext *s = ctx->priv;
400     int i;
401
402     for (i = s->frst + 1; i > s->last; i++) {
403         if (s->srce[i] && (s->srce[i] != s->srce[i + 1]))
404             av_frame_free(&s->srce[i]);
405     }
406     av_frame_free(&s->srce[s->last]);
407 }
408
409 static int query_formats(AVFilterContext *ctx)
410 {
411     static const enum AVPixelFormat pix_fmts[] = {
412         AV_PIX_FMT_YUV410P,
413         AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVJ411P,
414         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
415         AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
416         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
417         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
418         AV_PIX_FMT_NONE
419     };
420
421     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
422     if (!fmts_list)
423         return AVERROR(ENOMEM);
424     return ff_set_common_formats(ctx, fmts_list);
425 }
426
427 static int config_input(AVFilterLink *inlink)
428 {
429     AVFilterContext *ctx = inlink->dst;
430     FrameRateContext *s = ctx->priv;
431     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
432     int plane;
433
434     for (plane = 0; plane < 4; plane++) {
435         s->line_size[plane] = av_image_get_linesize(inlink->format, inlink->w,
436                                                     plane);
437     }
438
439     s->vsub = pix_desc->log2_chroma_h;
440
441     s->sad = av_pixelutils_get_sad_fn(3, 3, 2, s); // 8x8 both sources aligned
442     if (!s->sad)
443         return AVERROR(EINVAL);
444
445     s->srce_time_base = inlink->time_base;
446
447     return 0;
448 }
449
450 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
451 {
452     AVFilterContext *ctx = inlink->dst;
453     FrameRateContext *s = ctx->priv;
454
455     // we have one new frame
456     s->pending_srce_frames++;
457
458     if (inpicref->interlaced_frame)
459         av_log(ctx, AV_LOG_WARNING, "Interlaced frame found - the output will not be correct.\n");
460
461     // store the pointer to the new frame
462     av_frame_free(&s->srce[s->frst]);
463     s->srce[s->frst] = inpicref;
464
465     if (!s->pending_end_frame && s->srce[s->crnt]) {
466         set_work_frame_pts(ctx);
467         s->pending_end_frame = 1;
468     } else {
469         set_srce_frame_dest_pts(ctx);
470     }
471
472     return process_work_frame(ctx, 1);
473 }
474
475 static int config_output(AVFilterLink *outlink)
476 {
477     AVFilterContext *ctx = outlink->src;
478     FrameRateContext *s = ctx->priv;
479     int exact;
480
481     ff_dlog(ctx, "config_output()\n");
482
483     ff_dlog(ctx,
484            "config_output() input time base:%u/%u (%f)\n",
485            ctx->inputs[0]->time_base.num,ctx->inputs[0]->time_base.den,
486            av_q2d(ctx->inputs[0]->time_base));
487
488     // make sure timebase is small enough to hold the framerate
489
490     exact = av_reduce(&s->dest_time_base.num, &s->dest_time_base.den,
491                       av_gcd((int64_t)s->srce_time_base.num * s->dest_frame_rate.num,
492                              (int64_t)s->srce_time_base.den * s->dest_frame_rate.den ),
493                       (int64_t)s->srce_time_base.den * s->dest_frame_rate.num, INT_MAX);
494
495     av_log(ctx, AV_LOG_INFO,
496            "time base:%u/%u -> %u/%u exact:%d\n",
497            s->srce_time_base.num, s->srce_time_base.den,
498            s->dest_time_base.num, s->dest_time_base.den, exact);
499     if (!exact) {
500         av_log(ctx, AV_LOG_WARNING, "Timebase conversion is not exact\n");
501     }
502
503     outlink->frame_rate = s->dest_frame_rate;
504     outlink->time_base = s->dest_time_base;
505     outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
506
507     ff_dlog(ctx,
508            "config_output() output time base:%u/%u (%f) w:%d h:%d\n",
509            outlink->time_base.num, outlink->time_base.den,
510            av_q2d(outlink->time_base),
511            outlink->w, outlink->h);
512
513
514     av_log(ctx, AV_LOG_INFO, "fps -> fps:%u/%u scene score:%f interpolate start:%d end:%d\n",
515             s->dest_frame_rate.num, s->dest_frame_rate.den,
516             s->scene_score, s->interp_start, s->interp_end);
517
518     return 0;
519 }
520
521 static int request_frame(AVFilterLink *outlink)
522 {
523     AVFilterContext *ctx = outlink->src;
524     FrameRateContext *s = ctx->priv;
525     int val, i;
526
527     ff_dlog(ctx, "request_frame()\n");
528
529     // if there is no "next" frame AND we are not in flush then get one from our input filter
530     if (!s->srce[s->frst] && !s->flush) {
531         ff_dlog(ctx, "request_frame() call source's request_frame()\n");
532         if ((val = ff_request_frame(outlink->src->inputs[0])) < 0) {
533             ff_dlog(ctx, "request_frame() source's request_frame() returned error:%d\n", val);
534             return val;
535         }
536         ff_dlog(ctx, "request_frame() source's request_frame() returned:%d\n", val);
537         return 0;
538     }
539
540     ff_dlog(ctx, "request_frame() REPEAT or FLUSH\n");
541
542     if (s->pending_srce_frames <= 0) {
543         ff_dlog(ctx, "request_frame() nothing else to do, return:EOF\n");
544         return AVERROR_EOF;
545     }
546
547     // otherwise, make brand-new frame and pass to our output filter
548     ff_dlog(ctx, "request_frame() FLUSH\n");
549
550     // back fill at end of file when source has no more frames
551     for (i = s->last; i > s->frst; i--) {
552         if (!s->srce[i - 1] && s->srce[i]) {
553             ff_dlog(ctx, "request_frame() copy:%d to:%d\n", i, i - 1);
554             s->srce[i - 1] = s->srce[i];
555         }
556     }
557
558     set_work_frame_pts(ctx);
559     return process_work_frame(ctx, 0);
560 }
561
562 static const AVFilterPad framerate_inputs[] = {
563     {
564         .name         = "default",
565         .type         = AVMEDIA_TYPE_VIDEO,
566         .config_props = config_input,
567         .filter_frame = filter_frame,
568     },
569     { NULL }
570 };
571
572 static const AVFilterPad framerate_outputs[] = {
573     {
574         .name          = "default",
575         .type          = AVMEDIA_TYPE_VIDEO,
576         .request_frame = request_frame,
577         .config_props  = config_output,
578     },
579     { NULL }
580 };
581
582 AVFilter ff_vf_framerate = {
583     .name          = "framerate",
584     .description   = NULL_IF_CONFIG_SMALL("Upsamples or downsamples progressive source between specified frame rates."),
585     .priv_size     = sizeof(FrameRateContext),
586     .priv_class    = &framerate_class,
587     .init          = init,
588     .uninit        = uninit,
589     .query_formats = query_formats,
590     .inputs        = framerate_inputs,
591     .outputs       = framerate_outputs,
592 };