2 * Copyright (C) 2012 Mark Himsley
4 * get_scene_score() Copyright (c) 2011 Stefano Sabatini
5 * taken from libavfilter/vf_select.c
7 * This file is part of FFmpeg.
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.
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.
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
26 * filter for upsampling or downsampling a progressive source
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"
44 typedef struct FrameRateContext {
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
53 int line_size[4]; ///< bytes of pixel data per line for each plane
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()
61 AVRational srce_time_base; ///< timebase of source
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
69 av_pixelutils_sad_fn sad; ///< Sum of the absolute difference function (scene detect only)
70 double prev_mafd; ///< previous MAFD (scene detect only)
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
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
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 },
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 },
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" },
96 AVFILTER_DEFINE_CLASS(framerate);
98 static void next_source(AVFilterContext *ctx)
100 FrameRateContext *s = ctx->priv;
103 ff_dlog(ctx, "next_source()\n");
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]);
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];
113 ff_dlog(ctx, "next_source() make %d null\n", s->frst);
114 s->srce[s->frst] = NULL;
117 static double get_scene_score(AVFilterContext *ctx, AVFrame *crnt, AVFrame *next)
119 FrameRateContext *s = ctx->priv;
122 ff_dlog(ctx, "get_scene_score()\n");
125 crnt->height == next->height &&
126 crnt->width == next->width) {
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];
135 ff_dlog(ctx, "get_scene_score() process\n");
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,
141 p2 + y * p2_linesize + x,
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);
151 ff_dlog(ctx, "get_scene_score() result is:%f\n", ret);
155 static int process_work_frame(AVFilterContext *ctx, int stop)
157 FrameRateContext *s = ctx->priv;
158 AVFilterLink *outlink = ctx->outputs[0];
159 int64_t work_next_pts;
160 AVFrame *copy_src1, *copy_src2, *work;
163 ff_dlog(ctx, "process_work_frame()\n");
165 ff_dlog(ctx, "process_work_frame() pending_input_frames %d\n", s->pending_srce_frames);
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);
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");
178 work_next_pts = s->pts + s->average_dest_pts_delta;
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);
192 av_assert0(s->srce[s->next]);
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");
198 s->pending_srce_frames--;
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];
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];
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;
219 if (interpolate > 0) {
220 ff_dlog(ctx, "process_work_frame() interpolate source is:NEXT\n");
221 copy_src2 = s->srce[s->next];
223 ff_dlog(ctx, "process_work_frame() interpolate source is:PREV\n");
224 copy_src2 = s->srce[s->prev];
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);
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;
236 // get work-space for output frame
237 work = ff_get_video_buffer(outlink, outlink->w, outlink->h);
239 return AVERROR(ENOMEM);
241 av_frame_copy_props(work, s->srce[s->crnt]);
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) {
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;
262 cpy_src1_data += cpy_src1_line_size;
263 cpy_src2_data += cpy_src2_line_size;
264 cpy_dst_data += cpy_dst_line_size;
268 for (line = 0; line < cpy_src_h; line++) {
269 for (pixel = 0; pixel < cpy_line_width; pixel++) {
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;
275 cpy_src1_data += cpy_src1_line_size;
276 cpy_src2_data += cpy_src2_line_size;
277 cpy_dst_data += cpy_dst_line_size;
284 ff_dlog(ctx, "process_work_frame() CUT - DON'T INTERPOLATE\n");
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);
292 return AVERROR(ENOMEM);
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");
301 ff_dlog(ctx, "process_work_frame() CONSUME FRAME, move to next frame\n");
302 s->pending_srce_frames--;
305 ff_dlog(ctx, "process_work_frame() output a frame\n");
308 s->pending_end_frame = 0;
309 s->last_dest_frame_pts = work->pts;
311 return ff_filter_frame(ctx->outputs[0], work);
314 static void set_srce_frame_dest_pts(AVFilterContext *ctx)
316 FrameRateContext *s = ctx->priv;
318 ff_dlog(ctx, "set_srce_frame_output_pts()\n");
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);
329 static void set_work_frame_pts(AVFilterContext *ctx)
331 FrameRateContext *s = ctx->priv;
332 int64_t pts, average_srce_pts_delta = 0;
334 ff_dlog(ctx, "set_work_frame_pts()\n");
336 av_assert0(s->srce[s->next]);
337 av_assert0(s->srce[s->crnt]);
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);
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);
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;
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);
359 set_srce_frame_dest_pts(ctx);
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));
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);
372 if (!s->dest_frame_num) {
373 s->pts = s->last_dest_frame_pts = s->srce_pts_dest[s->crnt];
375 s->pts = s->last_dest_frame_pts + s->average_dest_pts_delta;
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);
382 static av_cold int init(AVFilterContext *ctx)
384 FrameRateContext *s = ctx->priv;
386 s->dest_frame_num = 0;
388 s->crnt = (N_SRCE)>>1;
389 s->last = N_SRCE - 1;
391 s->next = s->crnt - 1;
392 s->prev = s->crnt + 1;
397 static av_cold void uninit(AVFilterContext *ctx)
399 FrameRateContext *s = ctx->priv;
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]);
406 av_frame_free(&s->srce[s->last]);
409 static int query_formats(AVFilterContext *ctx)
411 static const enum AVPixelFormat pix_fmts[] = {
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,
421 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
423 return AVERROR(ENOMEM);
424 return ff_set_common_formats(ctx, fmts_list);
427 static int config_input(AVFilterLink *inlink)
429 AVFilterContext *ctx = inlink->dst;
430 FrameRateContext *s = ctx->priv;
431 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
434 for (plane = 0; plane < 4; plane++) {
435 s->line_size[plane] = av_image_get_linesize(inlink->format, inlink->w,
439 s->vsub = pix_desc->log2_chroma_h;
441 s->sad = av_pixelutils_get_sad_fn(3, 3, 2, s); // 8x8 both sources aligned
443 return AVERROR(EINVAL);
445 s->srce_time_base = inlink->time_base;
450 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
452 AVFilterContext *ctx = inlink->dst;
453 FrameRateContext *s = ctx->priv;
455 // we have one new frame
456 s->pending_srce_frames++;
458 if (inpicref->interlaced_frame)
459 av_log(ctx, AV_LOG_WARNING, "Interlaced frame found - the output will not be correct.\n");
461 // store the pointer to the new frame
462 av_frame_free(&s->srce[s->frst]);
463 s->srce[s->frst] = inpicref;
465 if (!s->pending_end_frame && s->srce[s->crnt]) {
466 set_work_frame_pts(ctx);
467 s->pending_end_frame = 1;
469 set_srce_frame_dest_pts(ctx);
472 return process_work_frame(ctx, 1);
475 static int config_output(AVFilterLink *outlink)
477 AVFilterContext *ctx = outlink->src;
478 FrameRateContext *s = ctx->priv;
481 ff_dlog(ctx, "config_output()\n");
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));
488 // make sure timebase is small enough to hold the framerate
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);
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);
500 av_log(ctx, AV_LOG_WARNING, "Timebase conversion is not exact\n");
503 outlink->frame_rate = s->dest_frame_rate;
504 outlink->time_base = s->dest_time_base;
505 outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
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);
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);
521 static int request_frame(AVFilterLink *outlink)
523 AVFilterContext *ctx = outlink->src;
524 FrameRateContext *s = ctx->priv;
527 ff_dlog(ctx, "request_frame()\n");
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);
536 ff_dlog(ctx, "request_frame() source's request_frame() returned:%d\n", val);
540 ff_dlog(ctx, "request_frame() REPEAT or FLUSH\n");
542 if (s->pending_srce_frames <= 0) {
543 ff_dlog(ctx, "request_frame() nothing else to do, return:EOF\n");
547 // otherwise, make brand-new frame and pass to our output filter
548 ff_dlog(ctx, "request_frame() FLUSH\n");
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];
558 set_work_frame_pts(ctx);
559 return process_work_frame(ctx, 0);
562 static const AVFilterPad framerate_inputs[] = {
565 .type = AVMEDIA_TYPE_VIDEO,
566 .config_props = config_input,
567 .filter_frame = filter_frame,
572 static const AVFilterPad framerate_outputs[] = {
575 .type = AVMEDIA_TYPE_VIDEO,
576 .request_frame = request_frame,
577 .config_props = config_output,
582 AVFilter ff_vf_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,
589 .query_formats = query_formats,
590 .inputs = framerate_inputs,
591 .outputs = framerate_outputs,