History

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

Limin Wang committed 6 years ago

Commit 2399a771 parent c417f1a1 file · +13 −10Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/vf_fade.c+13−10
@@ -62,6 +62,7 @@ typedef struct FadeContext { · 61 unmodified lines
6262 int alpha;
6363 int is_planar;
6464 uint64_t start_time, duration;
65+ uint64_t start_time_pts, duration_pts;
6566 enum {VF_FADE_WAITING=0, VF_FADE_FADING, VF_FADE_DONE} fade_state;
6667 uint8_t color_rgba[4]; ///< fade color
6768 int black_fade; ///< if color_rgba is black
@@ -422,6 +423,11 @@ static int config_props(AVFilterLink *inlink) · 354 unmodified lines
422423 s->is_rgb = pixdesc->flags & AV_PIX_FMT_FLAG_RGB;
423424 s->is_packed_rgb = !s->is_planar && s->is_rgb;
424425
426+ if (s->duration)
427+ s->duration_pts = av_rescale_q(s->duration, AV_TIME_BASE_Q, inlink->time_base);
428+ if (s->start_time)
429+ s->start_time_pts = av_rescale_q(s->start_time, AV_TIME_BASE_Q, inlink->time_base);
430+
425431 /* use CCIR601/709 black level for studio-level pixel non-alpha components */
426432 s->black_level =
427433 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) · 12 unmodified lines
440446 {
441447 AVFilterContext *ctx = inlink->dst;
442448 FadeContext *s = ctx->priv;
443− double frame_timestamp = frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base);
444449
445450 // Calculate Fade assuming this is a Fade In
446451 if (s->fade_state == VF_FADE_WAITING) {
447452 s->factor=0;
448− if (frame_timestamp >= s->start_time/(double)AV_TIME_BASE
453+ if (frame->pts >= s->start_time_pts
449454 && inlink->frame_count_out >= s->start_frame) {
450455 // Time to start fading
451456 s->fade_state = VF_FADE_FADING;
452457
453458 // Save start time in case we are starting based on frames and fading based on time
454− if (s->start_time == 0 && s->start_frame != 0) {
455− s->start_time = frame_timestamp*(double)AV_TIME_BASE;
459+ if (s->start_time_pts == 0 && s->start_frame != 0) {
460+ s->start_time_pts = frame->pts;
456461 }
457462
458463 // Save start frame in case we are starting based on time and fading based on frames
459− if (s->start_time != 0 && s->start_frame == 0) {
464+ if (s->start_time_pts != 0 && s->start_frame == 0) {
460465 s->start_frame = inlink->frame_count_out;
461466 }
462467 }
463468 }
464469 if (s->fade_state == VF_FADE_FADING) {
465− if (s->duration == 0) {
470+ if (s->duration_pts == 0) {
466471 // Fading based on frame count
467472 s->factor = (inlink->frame_count_out - s->start_frame) * s->fade_per_frame;
468473 if (inlink->frame_count_out > s->start_frame + s->nb_frames) {
@@ -471,10 +476,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame) · 2 unmodified lines
471476
472477 } else {
473478 // Fading based on duration
474− s->factor = (frame_timestamp - s->start_time/(double)AV_TIME_BASE)
475− * (float) UINT16_MAX / (s->duration/(double)AV_TIME_BASE);
476− if (frame_timestamp > s->start_time/(double)AV_TIME_BASE
477− + s->duration/(double)AV_TIME_BASE) {
479+ s->factor = (frame->pts - s->start_time_pts) * UINT16_MAX / s->duration_pts;
480+ if (frame->pts > s->start_time_pts + s->duration_pts) {
478481 s->fade_state = VF_FADE_DONE;
479482 }
480483 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t