[x264-devel] [PATCH] ratecontrol: Fix infinite loop with negative frame duration
Vasilyev, Kostya
kvasilye at amazon.com
Mon Nov 9 21:44:41 UTC 2026
Amazon has replaced the attachments in this email with download links.
Important: These links may contain content that is confidential. Please exercise appropriate caution when sharing.
2 Attachments [Download all] | Passcode required yJQfHY
https://us-west-2.secure-attach.amazon.com/all/347c6be4-a0ae-4336-aa63-42e57d2dc6f8/d96d2ca6-23f6-4cda-b5e2-db241a0038bc
[rep.c]
https://us-west-2.secure-attach.amazon.com/d96d2ca6-23f6-4cda-b5e2-db241a0038bc/50e68313-b010-4fc5-bf58-be4858500d6c
[upstream-report.md]
https://us-west-2.secure-attach.amazon.com/d96d2ca6-23f6-4cda-b5e2-db241a0038bc/0dee9fa4-1312-4bc1-ab9d-834606b9ef63
Attachments Available Until: December 09, 2026, 21:44 (UTC+00:00)
Help us improve: How was your experience? [Share Your Feedback]
https://amazonexteu.qualtrics.com/jfe/form/SV_ehuz6zGo8YnsRKK
Need assistance? [Visit Here] for FAQs and support resources.
https://docs.secure-attach.amazon.com/guide
Hello,
Found a bug in the stable branch of x265.
If the pts goes backwards (e.g. a timestamp discontinuity with VFR input),
the frame duration and therefore frame_size_maximum become negative.
Since d46938de the MinCR / buffer fill restriction in rate_estimate_qscale()
is applied after vbv_pass1() and is no longer followed by
X264_MAX( q0, q ) and the final qscale clipping, so the negative limit turns
q negative. The resulting NaN QP makes the row loops in
x264_ratecontrol_mb() spin forever when compiled with -ffast-math
(observed with clang 15, 18 and 20 on x86_64).
**Reproducer**
Attached `rep.c` (standalone, uses only libx264):
```
CC=clang-20 ./configure --disable-cli --enable-static --enable-pic --disable-opencl --disable-lavf
make libx264.a
gcc -O1 -I. rep.c libx264.a -lpthread -lm -ldl -o rep
./rep 1 0 # hangs (pts goes backwards)
./rep 0 0 # completes: "ok, 182 frames"
```
Results:
| Build | ./rep 1 0 (3 runs) |
|---|---|
| clang 18.1.3, d46938de~1 | ok ok ok |
| clang 18.1.3, d46938de | hang hang hang |
| clang 18.1.3, b35605ac (stable) | hang hang hang |
| clang 18.1.3, b35605ac + `--extra-cflags=-fno-finite-math-only` | ok ok ok |
| clang 20.1.2, d46938de~1 | ok ok ok |
| clang 20.1.2, d46938de | hang hang hang |
| clang 20.1.2, b35605ac (stable) | hang hang hang |
| clang 20.1.2, b35605ac + `--extra-cflags=-fno-finite-math-only` | ok ok ok |
| clang 20.1.2, master 0480cb05 | hang hang hang |
| clang 15.0.7, b35605ac | hang hang hang |
| GCC, b35605ac | ok ok ok |
**Backtrace (hung state)**
```
Thread (worker): x264_8_ratecontrol_mb <- slices_write <- threadpool_thread [spinning]
Thread (worker): predict_row_size_to_end <- x264_8_ratecontrol_mb <- slices_write [spinning]
Thread (worker): x264_8_frame_cond_wait <- x264_8_macroblock_analyse <- slices_write
Thread (main): x264_8_threadpool_wait <- encoder_frame_end <- x264_8_encoder_encode
```
---
encoder/ratecontrol.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c
index a42305c5..2c3c45fa 100644
--- a/encoder/ratecontrol.c
+++ b/encoder/ratecontrol.c
@@ -2460,7 +2460,8 @@ static float rate_estimate_qscale( x264_t *h )
if( rcc->b_vbv )
{
double frame_size_maximum = X264_MIN( rcc->frame_size_maximum, X264_MAX( rcc->buffer_fill, 0.001 ) );
- if( rcc->frame_size_planned > frame_size_maximum )
+ /* frame_size_maximum can be negative if the frame duration is negative (pts going backwards). */
+ if( frame_size_maximum > 0 && rcc->frame_size_planned > frame_size_maximum )
{
q *= rcc->frame_size_planned / frame_size_maximum;
rcc->frame_size_planned = frame_size_maximum;
@@ -2646,7 +2647,8 @@ static float rate_estimate_qscale( x264_t *h )
if( rcc->b_vbv )
{
double frame_size_maximum = X264_MIN( rcc->frame_size_maximum, X264_MAX( rcc->buffer_fill, 0.001 ) );
- if( rcc->frame_size_planned > frame_size_maximum )
+ /* frame_size_maximum can be negative if the frame duration is negative (pts going backwards). */
+ if( frame_size_maximum > 0 && rcc->frame_size_planned > frame_size_maximum )
{
q *= rcc->frame_size_planned / frame_size_maximum;
rcc->frame_size_planned = frame_size_maximum;
--
2.43.0
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/x264-devel/attachments/20261109/8c251e8b/attachment.htm>
More information about the x264-devel
mailing list