avfilter/vf_fade: simplify code to use pts timebase for time check

Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
This commit is contained in:
Limin Wang 2020-10-30 21:37:51 +08:00
parent c417f1a76c
commit 2399a7795b
1 changed files with 13 additions and 10 deletions

View File

@ -62,6 +62,7 @@ typedef struct FadeContext {
int alpha; int alpha;
int is_planar; int is_planar;
uint64_t start_time, duration; uint64_t start_time, duration;
uint64_t start_time_pts, duration_pts;
enum {VF_FADE_WAITING=0, VF_FADE_FADING, VF_FADE_DONE} fade_state; enum {VF_FADE_WAITING=0, VF_FADE_FADING, VF_FADE_DONE} fade_state;
uint8_t color_rgba[4]; ///< fade color uint8_t color_rgba[4]; ///< fade color
int black_fade; ///< if color_rgba is black int black_fade; ///< if color_rgba is black
@ -422,6 +423,11 @@ static int config_props(AVFilterLink *inlink)
s->is_rgb = pixdesc->flags & AV_PIX_FMT_FLAG_RGB; s->is_rgb = pixdesc->flags & AV_PIX_FMT_FLAG_RGB;
s->is_packed_rgb = !s->is_planar && s->is_rgb; s->is_packed_rgb = !s->is_planar && s->is_rgb;
if (s->duration)
s->duration_pts = av_rescale_q(s->duration, AV_TIME_BASE_Q, inlink->time_base);
if (s->start_time)
s->start_time_pts = av_rescale_q(s->start_time, AV_TIME_BASE_Q, inlink->time_base);
/* use CCIR601/709 black level for studio-level pixel non-alpha components */ /* use CCIR601/709 black level for studio-level pixel non-alpha components */
s->black_level = s->black_level =
ff_fmt_is_in(inlink->format, studio_level_pix_fmts) && !s->alpha ? 16 * (1 << (s->depth - 8)): 0; ff_fmt_is_in(inlink->format, studio_level_pix_fmts) && !s->alpha ? 16 * (1 << (s->depth - 8)): 0;
@ -440,29 +446,28 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
{ {
AVFilterContext *ctx = inlink->dst; AVFilterContext *ctx = inlink->dst;
FadeContext *s = ctx->priv; FadeContext *s = ctx->priv;
double frame_timestamp = frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base);
// Calculate Fade assuming this is a Fade In // Calculate Fade assuming this is a Fade In
if (s->fade_state == VF_FADE_WAITING) { if (s->fade_state == VF_FADE_WAITING) {
s->factor=0; s->factor=0;
if (frame_timestamp >= s->start_time/(double)AV_TIME_BASE if (frame->pts >= s->start_time_pts
&& inlink->frame_count_out >= s->start_frame) { && inlink->frame_count_out >= s->start_frame) {
// Time to start fading // Time to start fading
s->fade_state = VF_FADE_FADING; s->fade_state = VF_FADE_FADING;
// Save start time in case we are starting based on frames and fading based on time // Save start time in case we are starting based on frames and fading based on time
if (s->start_time == 0 && s->start_frame != 0) { if (s->start_time_pts == 0 && s->start_frame != 0) {
s->start_time = frame_timestamp*(double)AV_TIME_BASE; s->start_time_pts = frame->pts;
} }
// Save start frame in case we are starting based on time and fading based on frames // Save start frame in case we are starting based on time and fading based on frames
if (s->start_time != 0 && s->start_frame == 0) { if (s->start_time_pts != 0 && s->start_frame == 0) {
s->start_frame = inlink->frame_count_out; s->start_frame = inlink->frame_count_out;
} }
} }
} }
if (s->fade_state == VF_FADE_FADING) { if (s->fade_state == VF_FADE_FADING) {
if (s->duration == 0) { if (s->duration_pts == 0) {
// Fading based on frame count // Fading based on frame count
s->factor = (inlink->frame_count_out - s->start_frame) * s->fade_per_frame; s->factor = (inlink->frame_count_out - s->start_frame) * s->fade_per_frame;
if (inlink->frame_count_out > s->start_frame + s->nb_frames) { if (inlink->frame_count_out > s->start_frame + s->nb_frames) {
@ -471,10 +476,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
} else { } else {
// Fading based on duration // Fading based on duration
s->factor = (frame_timestamp - s->start_time/(double)AV_TIME_BASE) s->factor = (frame->pts - s->start_time_pts) * UINT16_MAX / s->duration_pts;
* (float) UINT16_MAX / (s->duration/(double)AV_TIME_BASE); if (frame->pts > s->start_time_pts + s->duration_pts) {
if (frame_timestamp > s->start_time/(double)AV_TIME_BASE
+ s->duration/(double)AV_TIME_BASE) {
s->fade_state = VF_FADE_DONE; s->fade_state = VF_FADE_DONE;
} }
} }