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

Unified · Split

libavfilter/vf_fade.c+13−10
@@ -62,6 +62,7 @@ typedef struct FadeContext { · 61 unmodified lines
62 int alpha;62 int alpha;
63 int is_planar;63 int is_planar;
64 uint64_t start_time, duration;64 uint64_t start_time, duration;
65 uint64_t start_time_pts, duration_pts;
65 enum {VF_FADE_WAITING=0, VF_FADE_FADING, VF_FADE_DONE} fade_state;66 enum {VF_FADE_WAITING=0, VF_FADE_FADING, VF_FADE_DONE} fade_state;
66 uint8_t color_rgba[4]; ///< fade color67 uint8_t color_rgba[4]; ///< fade color
67 int black_fade; ///< if color_rgba is black68 int black_fade; ///< if color_rgba is black
@@ -422,6 +423,11 @@ static int config_props(AVFilterLink *inlink) · 354 unmodified lines
422 s->is_rgb = pixdesc->flags & AV_PIX_FMT_FLAG_RGB;423 s->is_rgb = pixdesc->flags & AV_PIX_FMT_FLAG_RGB;
423 s->is_packed_rgb = !s->is_planar && s->is_rgb;424 s->is_packed_rgb = !s->is_planar && s->is_rgb;
424 425
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
425 /* use CCIR601/709 black level for studio-level pixel non-alpha components */431 /* use CCIR601/709 black level for studio-level pixel non-alpha components */
426 s->black_level =432 s->black_level =
427 ff_fmt_is_in(inlink->format, studio_level_pix_fmts) && !s->alpha ? 16 * (1 << (s->depth - 8)): 0;433 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
440{446{
441 AVFilterContext *ctx = inlink->dst;447 AVFilterContext *ctx = inlink->dst;
442 FadeContext *s = ctx->priv;448 FadeContext *s = ctx->priv;
443 double frame_timestamp = frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base);
444 449
445 // Calculate Fade assuming this is a Fade In450 // Calculate Fade assuming this is a Fade In
446 if (s->fade_state == VF_FADE_WAITING) {451 if (s->fade_state == VF_FADE_WAITING) {
447 s->factor=0;452 s->factor=0;
448 if (frame_timestamp >= s->start_time/(double)AV_TIME_BASE453 if (frame->pts >= s->start_time_pts
449 && inlink->frame_count_out >= s->start_frame) {454 && inlink->frame_count_out >= s->start_frame) {
450 // Time to start fading455 // Time to start fading
451 s->fade_state = VF_FADE_FADING;456 s->fade_state = VF_FADE_FADING;
452 457
453 // Save start time in case we are starting based on frames and fading based on time458 // 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) {459 if (s->start_time_pts == 0 && s->start_frame != 0) {
455 s->start_time = frame_timestamp*(double)AV_TIME_BASE;460 s->start_time_pts = frame->pts;
456 }461 }
457 462
458 // Save start frame in case we are starting based on time and fading based on frames463 // 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) {
460 s->start_frame = inlink->frame_count_out;465 s->start_frame = inlink->frame_count_out;
461 }466 }
462 }467 }
463 }468 }
464 if (s->fade_state == VF_FADE_FADING) {469 if (s->fade_state == VF_FADE_FADING) {
465 if (s->duration == 0) {470 if (s->duration_pts == 0) {
466 // Fading based on frame count471 // Fading based on frame count
467 s->factor = (inlink->frame_count_out - s->start_frame) * s->fade_per_frame;472 s->factor = (inlink->frame_count_out - s->start_frame) * s->fade_per_frame;
468 if (inlink->frame_count_out > s->start_frame + s->nb_frames) {473 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
471 476
472 } else {477 } else {
473 // Fading based on duration478 // Fading based on duration
474 s->factor = (frame_timestamp - s->start_time/(double)AV_TIME_BASE)479 s->factor = (frame->pts - s->start_time_pts) * UINT16_MAX / s->duration_pts;
475 * (float) UINT16_MAX / (s->duration/(double)AV_TIME_BASE);480 if (frame->pts > s->start_time_pts + s->duration_pts) {
476 if (frame_timestamp > s->start_time/(double)AV_TIME_BASE
477 + s->duration/(double)AV_TIME_BASE) {
478 s->fade_state = VF_FADE_DONE;481 s->fade_state = VF_FADE_DONE;
479 }482 }
480 }483 }

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