Guard against int overflow when discarding samples

In `discard_samples()`, the rescaling operation can evaluate to
AV_NOPTS_VALUE if the rescaling operation overflows. This commit
prevents adjusting timestamp and durations by the invalid value.
This commit is contained in:
Thomas Guilbert
2026-06-17 03:18:51 +00:00
committed by michaelni
parent d7fe91e9c3
commit 005e963c85

View File

@@ -374,12 +374,18 @@ static int discard_samples(AVCodecContext *avctx, AVFrame *frame, int64_t *disca
int64_t diff_ts = av_rescale_q(avci->skip_samples,
(AVRational){1, avctx->sample_rate},
avctx->pkt_timebase);
if (frame->pts != AV_NOPTS_VALUE)
frame->pts += diff_ts;
if (frame->pkt_dts != AV_NOPTS_VALUE)
frame->pkt_dts += diff_ts;
if (frame->duration >= diff_ts)
frame->duration -= diff_ts;
if (diff_ts != AV_NOPTS_VALUE) {
if (frame->pts != AV_NOPTS_VALUE)
frame->pts = av_sat_add64(frame->pts, diff_ts);
if (frame->pkt_dts != AV_NOPTS_VALUE)
frame->pkt_dts = av_sat_add64(frame->pkt_dts, diff_ts);
if (frame->duration >= diff_ts)
frame->duration = av_sat_sub64(frame->duration, diff_ts);
} else {
frame->pts = AV_NOPTS_VALUE;
frame->pkt_dts = AV_NOPTS_VALUE;
frame->duration = 0;
}
} else
av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
@@ -400,7 +406,7 @@ static int discard_samples(AVCodecContext *avctx, AVFrame *frame, int64_t *disca
int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
(AVRational){1, avctx->sample_rate},
avctx->pkt_timebase);
frame->duration = diff_ts;
frame->duration = diff_ts == AV_NOPTS_VALUE ? 0 : diff_ts;
} else
av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");