From kvasilye at amazon.com Mon Nov 9 21:44:41 2026 From: kvasilye at amazon.com (Vasilyev, Kostya) Date: Mon, 9 Nov 2026 21:44:41 +0000 Subject: [x264-devel] [PATCH] ratecontrol: Fix infinite loop with negative frame duration References: Message-ID: <010101a1c2454492-9e9cae30-7e5d-4225-b314-00f1364ea8a4-000000@us-west-2.amazonses.com> 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: From kvasilye at amazon.com Mon Nov 9 21:59:19 2026 From: kvasilye at amazon.com (Vasilyev, Kostya) Date: Mon, 9 Nov 2026 21:59:19 +0000 Subject: [x264-devel] [PATCH] ratecontrol: Fix infinite loop with negative frame duration In-Reply-To: <010101a1c2454492-9e9cae30-7e5d-4225-b314-00f1364ea8a4-000000@us-west-2.amazonses.com> References: <010101a1c2454492-9e9cae30-7e5d-4225-b314-00f1364ea8a4-000000@us-west-2.amazonses.com> Message-ID: Sorry - typo, I meant x264 (of course) not x265. Also attaching the reproducer inline, as our email system replaces attachments with links. Sorry in advance if it gets messed up too. Thanks, ~ K ----- #include #include #include #include #include /* Reproducer: VBV infinite loop in x264_ratecontrol_mb (clang -ffast-math builds, since d46938de). * * 1920x1020, preset medium, profile main, 4 frame threads, rc-lookahead 8, ABR 3777 kbps with * vbv-maxrate = vbv-bufsize = 3777, ratetol 0.02, no scenecut, VFR with timebase 1/1000. * Encodes 91 frames at 25 ms intervals from pts 1000, then a forced IDR at pts 3000 (pts goes * backwards) followed by 90 more frames. * * Usage: rep [back=1] [luma=0] * back=1 second segment starts at pts 3000 (backwards) -> hangs * back=0 second segment starts at pts 3275 (monotonic) -> completes / * int main( int argc, char argv ) { * int back = argc > 1 ? atoi( argv[1] ) : 1, fill = argc > 2 ? atoi( argv[2] ) : 0; x264_param_t p; x264_param_default_preset( &p, "medium", NULL ); p.i_threads = 4; p.rc.i_rc_method = X264_RC_ABR; p.rc.i_bitrate = 3777; p.rc.i_vbv_max_bitrate = 3777; p.rc.i_vbv_buffer_size = 3777; p.rc.f_rate_tolerance = 0.02f; p.rc.i_lookahead = 8; p.i_width = 1920; p.i_height = 1020; p.b_vfr_input = 1; p.i_timebase_num = 1; p.i_timebase_den = 1000; p.b_annexb = 0; p.b_repeat_headers = 0; p.b_open_gop = 0; p.i_scenecut_threshold = 0; p.i_keyint_max = 1073741824; p.i_keyint_min = 0; p.i_log_level = X264_LOG_ERROR; x264_param_apply_profile( &p, "main" ); x264_t h = x264_encoder_open( &p ); if( !h ) { puts( "open failed" ); return 1; } x264_picture_t in, out; x264_picture_alloc( &in, X264_CSP_I420, 1920, 1020 ); memset( in.img.plane[0], fill, 1920 * 1020 ); memset( in.img.plane[1], 128, 960 * 510 ); memset( in.img.plane[2], 128, 960 * 510 ); int n = 0; x264_nal_t nals; int nn; for( int seg = 0; seg < 2; seg++ ) { int64_t base = seg == 0 ? 1000 : ( back ? 3000 : 3275 ); for( int i = 0; i <= 90; i++ ) { in.i_pts = base + i * 25; in.i_type = i == 0 ? X264_TYPE_IDR : X264_TYPE_AUTO; if( x264_encoder_encode( h, &nals, &nn, &in, &out ) > 0 ) n++; } fprintf( stderr, "seg %d done, %d frames out * ", seg, n ); * } while( x264_encoder_delayed_frames( h ) ) if( x264_encoder_encode( h, &nals, &nn, NULL, &out ) > 0 ) n++; printf( "ok, %d frames * ", n ); * x264_encoder_close( h ); return 0; * } -------------- next part -------------- An HTML attachment was scrubbed... URL: