AVRational dest_frame_rate; ///< output frames per second
int flags; ///< flags affecting frame rate conversion algorithm
double scene_score; ///< score that denotes a scene change has happened
- int interp_start; ///< start of range to apply linear interpolation
- int interp_end; ///< end of range to apply linear interpolation
+ int interp_start; ///< start of range to apply linear interpolation (same bitdepth as input)
+ int interp_end; ///< end of range to apply linear interpolation (same bitdepth as input)
+ int interp_start_param; ///< start of range to apply linear interpolation
+ int interp_end_param; ///< end of range to apply linear interpolation
int line_size[4]; ///< bytes of pixel data per line for each plane
int vsub;
AVFrame *srce[N_SRCE]; ///< buffered source frames
int64_t srce_pts_dest[N_SRCE]; ///< pts for source frames scaled to output timebase
+ double srce_score[N_SRCE]; ///< scene change score compared to the next srce frame
int64_t pts; ///< pts of frame we are working on
int max;
static const AVOption framerate_options[] = {
{"fps", "required output frames per second rate", OFFSET(dest_frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="50"}, 0, INT_MAX, V|F },
- {"interp_start", "point to start linear interpolation", OFFSET(interp_start), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, V|F },
- {"interp_end", "point to end linear interpolation", OFFSET(interp_end), AV_OPT_TYPE_INT, {.i64=240}, 0, 255, V|F },
- {"scene", "scene change level", OFFSET(scene_score), AV_OPT_TYPE_DOUBLE, {.dbl=7.0}, 0, INT_MAX, V|F },
+ {"interp_start", "point to start linear interpolation", OFFSET(interp_start_param),AV_OPT_TYPE_INT, {.i64=15}, 0, 255, V|F },
+ {"interp_end", "point to end linear interpolation", OFFSET(interp_end_param), AV_OPT_TYPE_INT, {.i64=240}, 0, 255, V|F },
+ {"scene", "scene change level", OFFSET(scene_score), AV_OPT_TYPE_DOUBLE, {.dbl=8.2}, 0, INT_MAX, V|F },
{"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=1}, 0, INT_MAX, V|F, "flags" },
{"scene_change_detect", "enable scene change detection", 0, AV_OPT_TYPE_CONST, {.i64=FRAMERATE_FLAG_SCD}, INT_MIN, INT_MAX, V|F, "flags" },
for (i = s->last; i > s->frst; i--) {
ff_dlog(ctx, "next_source() copy %d to %d\n", i - 1, i);
s->srce[i] = s->srce[i - 1];
+ s->srce_score[i] = s->srce_score[i - 1];
}
ff_dlog(ctx, "next_source() make %d null\n", s->frst);
s->srce[s->frst] = NULL;
+ s->srce_score[s->frst] = -1.0;
}
static av_always_inline int64_t sad_8x8_16(const uint16_t *src1, ptrdiff_t stride1,
return sum;
}
-static double get_scene_score16(AVFilterContext *ctx, AVFrame *crnt, AVFrame *next)
+static int64_t scene_sad16(FrameRateContext *s, const uint16_t *p1, int p1_linesize, const uint16_t* p2, int p2_linesize, const int width, const int height)
{
- FrameRateContext *s = ctx->priv;
- double ret = 0;
-
- ff_dlog(ctx, "get_scene_score16()\n");
+ int64_t sad;
+ int x, y;
+ for (sad = y = 0; y < height - 7; y += 8) {
+ for (x = 0; x < width - 7; x += 8) {
+ sad += sad_8x8_16(p1 + y * p1_linesize + x,
+ p1_linesize,
+ p2 + y * p2_linesize + x,
+ p2_linesize);
+ }
+ }
+ return sad;
+}
- if (crnt &&
- crnt->height == next->height &&
- crnt->width == next->width) {
- int x, y;
- int64_t sad;
- double mafd, diff;
- const uint16_t *p1 = (const uint16_t *)crnt->data[0];
- const uint16_t *p2 = (const uint16_t *)next->data[0];
- const int p1_linesize = crnt->linesize[0] / 2;
- const int p2_linesize = next->linesize[0] / 2;
-
- ff_dlog(ctx, "get_scene_score16() process\n");
-
- for (sad = y = 0; y < crnt->height; y += 8) {
- for (x = 0; x < p1_linesize; x += 8) {
- sad += sad_8x8_16(p1 + y * p1_linesize + x,
- p1_linesize,
- p2 + y * p2_linesize + x,
- p2_linesize);
- }
+static int64_t scene_sad8(FrameRateContext *s, uint8_t *p1, int p1_linesize, uint8_t* p2, int p2_linesize, const int width, const int height)
+{
+ int64_t sad;
+ int x, y;
+ for (sad = y = 0; y < height - 7; y += 8) {
+ for (x = 0; x < width - 7; x += 8) {
+ sad += s->sad(p1 + y * p1_linesize + x,
+ p1_linesize,
+ p2 + y * p2_linesize + x,
+ p2_linesize);
}
- mafd = sad / (crnt->height * crnt->width * 3);
- diff = fabs(mafd - s->prev_mafd);
- ret = av_clipf(FFMIN(mafd, diff), 0, 100.0);
- s->prev_mafd = mafd;
}
- ff_dlog(ctx, "get_scene_score16() result is:%f\n", ret);
- return ret;
+ emms_c();
+ return sad;
}
static double get_scene_score(AVFilterContext *ctx, AVFrame *crnt, AVFrame *next)
ff_dlog(ctx, "get_scene_score()\n");
- if (crnt &&
- crnt->height == next->height &&
+ if (crnt->height == next->height &&
crnt->width == next->width) {
- int x, y;
int64_t sad;
double mafd, diff;
- uint8_t *p1 = crnt->data[0];
- uint8_t *p2 = next->data[0];
- const int p1_linesize = crnt->linesize[0];
- const int p2_linesize = next->linesize[0];
ff_dlog(ctx, "get_scene_score() process\n");
+ if (s->bitdepth == 8)
+ sad = scene_sad8(s, crnt->data[0], crnt->linesize[0], next->data[0], next->linesize[0], crnt->width, crnt->height);
+ else
+ sad = scene_sad16(s, (const uint16_t*)crnt->data[0], crnt->linesize[0] / 2, (const uint16_t*)next->data[0], next->linesize[0] / 2, crnt->width, crnt->height);
- for (sad = y = 0; y < crnt->height; y += 8) {
- for (x = 0; x < p1_linesize; x += 8) {
- sad += s->sad(p1 + y * p1_linesize + x,
- p1_linesize,
- p2 + y * p2_linesize + x,
- p2_linesize);
- }
- }
- emms_c();
- mafd = sad / (crnt->height * crnt->width * 3);
+ mafd = (double)sad * 100.0 / FFMAX(1, (crnt->height & ~7) * (crnt->width & ~7)) / (1 << s->bitdepth);
diff = fabs(mafd - s->prev_mafd);
ret = av_clipf(FFMIN(mafd, diff), 0, 100.0);
s->prev_mafd = mafd;
}
- ff_dlog(ctx, "get_scene_score() result is:%f\n", ret);
+ ff_dlog(ctx, "get_scene_score() result is:%f\n", ret);
return ret;
}
return 0;
}
-static int blend_frames(AVFilterContext *ctx, float interpolate,
- AVFrame *copy_src1, AVFrame *copy_src2)
+static int blend_frames(AVFilterContext *ctx, int interpolate,
+ int src1, int src2)
{
FrameRateContext *s = ctx->priv;
AVFilterLink *outlink = ctx->outputs[0];
double interpolate_scene_score = 0;
- if ((s->flags & FRAMERATE_FLAG_SCD) && copy_src2) {
- interpolate_scene_score = s->bitdepth == 8 ? get_scene_score(ctx, copy_src1, copy_src2) : get_scene_score16(ctx, copy_src1, copy_src2);
+ if ((s->flags & FRAMERATE_FLAG_SCD) && s->srce[src1] && s->srce[src2]) {
+ int i1 = src1 < src2 ? src1 : src2;
+ int i2 = src1 < src2 ? src2 : src1;
+ if (i2 == i1 + 1 && s->srce_score[i1] >= 0.0)
+ interpolate_scene_score = s->srce_score[i1];
+ else
+ interpolate_scene_score = s->srce_score[i1] = get_scene_score(ctx, s->srce[i1], s->srce[i2]);
ff_dlog(ctx, "blend_frames() interpolate scene score:%f\n", interpolate_scene_score);
}
// decide if the shot-change detection allows us to blend two frames
- if (interpolate_scene_score < s->scene_score && copy_src2) {
+ if (interpolate_scene_score < s->scene_score && s->srce[src2]) {
ThreadData td;
- td.copy_src1 = copy_src1;
- td.copy_src2 = copy_src2;
- td.src2_factor = fabsf(interpolate) * (1 << (s->bitdepth - 8));
+ td.copy_src1 = s->srce[src1];
+ td.copy_src2 = s->srce[src2];
+ td.src2_factor = FFABS(interpolate);
td.src1_factor = s->max - td.src2_factor;
// get work-space for output frame
{
FrameRateContext *s = ctx->priv;
int64_t work_next_pts;
- AVFrame *copy_src1;
- float interpolate;
+ int interpolate;
+ int src1, src2;
ff_dlog(ctx, "process_work_frame()\n");
}
// calculate interpolation
- interpolate = ((s->pts - s->srce_pts_dest[s->crnt]) * 256.0 / s->average_srce_pts_dest_delta);
- ff_dlog(ctx, "process_work_frame() interpolate:%f/256\n", interpolate);
- copy_src1 = s->srce[s->crnt];
+ interpolate = av_rescale(s->pts - s->srce_pts_dest[s->crnt], s->max, s->average_srce_pts_dest_delta);
+ ff_dlog(ctx, "process_work_frame() interpolate:%d/%d\n", interpolate, s->max);
+ src1 = s->crnt;
if (interpolate > s->interp_end) {
ff_dlog(ctx, "process_work_frame() source is:NEXT\n");
- copy_src1 = s->srce[s->next];
+ src1 = s->next;
}
if (s->srce[s->prev] && interpolate < -s->interp_end) {
ff_dlog(ctx, "process_work_frame() source is:PREV\n");
- copy_src1 = s->srce[s->prev];
+ src1 = s->prev;
}
// decide whether to blend two frames
if ((interpolate >= s->interp_start && interpolate <= s->interp_end) || (interpolate <= -s->interp_start && interpolate >= -s->interp_end)) {
- AVFrame *copy_src2;
-
if (interpolate > 0) {
ff_dlog(ctx, "process_work_frame() interpolate source is:NEXT\n");
- copy_src2 = s->srce[s->next];
+ src2 = s->next;
} else {
ff_dlog(ctx, "process_work_frame() interpolate source is:PREV\n");
- copy_src2 = s->srce[s->prev];
+ src2 = s->prev;
}
- if (blend_frames(ctx, interpolate, copy_src1, copy_src2))
+ if (blend_frames(ctx, interpolate, src1, src2))
goto copy_done;
else
ff_dlog(ctx, "process_work_frame() CUT - DON'T INTERPOLATE\n");
ff_dlog(ctx, "process_work_frame() COPY to the work frame\n");
// copy the frame we decided is our base source
- s->work = av_frame_clone(copy_src1);
+ s->work = av_frame_clone(s->srce[src1]);
if (!s->work)
return AVERROR(ENOMEM);
static av_cold int init(AVFilterContext *ctx)
{
FrameRateContext *s = ctx->priv;
+ int i;
s->dest_frame_num = 0;
s->next = s->crnt - 1;
s->prev = s->crnt + 1;
+ for (i = 0; i < N_SRCE; i++)
+ s->srce_score[i] = -1.0;
+
return 0;
}
s->bitdepth = pix_desc->comp[0].depth;
s->vsub = pix_desc->log2_chroma_h;
+ s->interp_start = s->interp_start_param << (s->bitdepth - 8);
+ s->interp_end = s->interp_end_param << (s->bitdepth - 8);
s->sad = av_pixelutils_get_sad_fn(3, 3, 2, s); // 8x8 both sources aligned
if (!s->sad)