[x264-devel] Busy looping when flushing encoding operation?

Dimitry Andric dimitry at andric.com
Tue Oct 12 11:29:01 UTC 2021


Hi,

While integrating x264 into an encoding project, I noticed that the
logic for flushing any delayed frames out of the encoder basically
requires using a form of busy loop, e.g. something like:

    h = x264_encoder_open( param );
...
    while( !end_of_input() )
    {
        ...
        i_frame_size = x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out );
        ...
    }
    /* Flush delayed frames */
    while( x264_encoder_delayed_frames( h ) != 0 )
    {
        i_frame_size = x264_encoder_encode( h, &nal, &i_nal, NULL, &pic_out );
        ...
    }

What appears to happen is that in the latter loop, x264_encoder_encode()
sometimes has to be called quite a large number of times to have x264's
background threads deliver a frame (i.e. until i_frame_size > 0). So
effectively, in your calling code you then are busy-looping, eating up
one CPU core.

Is there any reason that x264_encoder_encode() with a NULL pic_in
parameter couldn't block for the delivery of a frame more efficiently?
As it can rely on x264's internal thread state, it could maybe wait on
some mutex or condition variable, which is a lot lower in CPU cost.

-Dimitry



More information about the x264-devel mailing list