1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

avfilter/yadif_common: fix timestamps with very small timebases

Yadif filter assumed that the output timebase is always half of the input
timebase. This is not true if halving the input time base is not representable
as an AVRational causing the output timestamps to be invalidly scaled in such a
case.

So let's use av_reduce instead of av_mul_q when calculating the output time
base and if the conversion is inexact then let's fall back to the original
timebase which probably makes more parctical sense than using x/INT_MAX.

Fixes invalidly scaled pts_time values in this command line:
ffmpeg -f lavfi -i testsrc -vf settb=tb=1/2000000000,yadif,showinfo -f null none

Signed-off-by: Marton Balint <cus@passwd.hu>
This commit is contained in:
Marton Balint 2024-01-28 02:45:35 +01:00
parent 268062fa15
commit 52cc89dc45
2 changed files with 21 additions and 5 deletions

View File

@ -86,6 +86,8 @@ typedef struct YADIFContext {
* the first field.
*/
int current_field; ///< YADIFCurrentField
int pts_multiplier;
} YADIFContext;
void ff_yadif_init_x86(YADIFContext *yadif);

View File

@ -62,6 +62,10 @@ FF_ENABLE_DEPRECATION_WARNINGS
if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
yadif->out->pts = cur_pts + next_pts;
if (yadif->pts_multiplier == 1) {
yadif->out->pts >>= 1;
yadif->out->duration >>= 1;
}
} else {
yadif->out->pts = AV_NOPTS_VALUE;
}
@ -150,8 +154,8 @@ int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame)
ff_ccfifo_inject(&yadif->cc_fifo, yadif->out);
av_frame_free(&yadif->prev);
if (yadif->out->pts != AV_NOPTS_VALUE)
yadif->out->pts *= 2;
yadif->out->duration *= 2;
yadif->out->pts *= yadif->pts_multiplier;
yadif->out->duration *= yadif->pts_multiplier;
return ff_filter_frame(ctx->outputs[0], yadif->out);
}
@ -168,9 +172,11 @@ FF_ENABLE_DEPRECATION_WARNINGS
yadif->out->flags &= ~AV_FRAME_FLAG_INTERLACED;
if (yadif->out->pts != AV_NOPTS_VALUE)
yadif->out->pts *= 2;
yadif->out->pts *= yadif->pts_multiplier;
if (!(yadif->mode & 1))
yadif->out->duration *= 2;
yadif->out->duration *= yadif->pts_multiplier;
else if (yadif->pts_multiplier == 1)
yadif->out->duration >>= 1;
return return_frame(ctx, 0);
}
@ -213,9 +219,17 @@ int ff_yadif_config_output_common(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
YADIFContext *yadif = ctx->priv;
AVRational tb = ctx->inputs[0]->time_base;
int ret;
outlink->time_base = av_mul_q(ctx->inputs[0]->time_base, (AVRational){1, 2});
if (av_reduce(&outlink->time_base.num, &outlink->time_base.den, tb.num, tb.den * 2LL, INT_MAX)) {
yadif->pts_multiplier = 2;
} else {
av_log(ctx, AV_LOG_WARNING, "Cannot use exact output timebase\n");
outlink->time_base = tb;
yadif->pts_multiplier = 1;
}
outlink->w = ctx->inputs[0]->w;
outlink->h = ctx->inputs[0]->h;