Search code examples
cmultithreadingvideoffmpeglibavcodec

Compatibility of P-frames and multithreading in FFmpeg library in C


I am working on a video processing project using the FFmpeg library in C. I would like to inquire about the compatibility and support for utilizing P-frames (predictive frames) and multithreading simultaneously in FFmpeg. Currently, I am exploring two options for achieving multithreading with FFmpeg:

  1. Setting the i_threads parameter of FFmpeg to a value greater than 1.
  2. My application opens multiple threads, where each thread creates a codec context and performs parallel compression/decompression of frames from a shared FIFO.

I have tried option 1, but I did not observe any noticeable time difference compared to using only one thread. I am seeking an understanding of why this might be the case.

I have tried option 2, which has resulted in improved compression/decompression times for the frames. However, I would like to understand why does it work. Since I am utilizing P-Frames, where each P-frame depends on the latest I-Frame, how can I ensure that the decompression of P-Frames starts after the I-Frame has finished processing when dealing with multiple codec contexts?

I have two proceccses one for encodeing and one for decoding. I'd like to improve times using threads in both of them.

The encoder process code:

pt_handle->t_x264_param.i_threads = 24;

pt_handle->pt_x264_encoder = x264_encoder_open(&pt_handle->t_x264_param);

The decoder process code:

pt_handle->pt_avcodec_ctx->thread_count = 23;

pt_handle->pt_avcodec_ctx->thread_type = FF_THREAD_FRAME ;

i_retval = avcodec_open2(pt_handle->pt_avcodec_ctx, pt_h264_decoder, NULL);


Solution

  • Option 2 is not possible with FFmpeg.

    For option 1: you need to set thread_type along with thread_count. (There is no i_threads parameter.) The result will depend on what encoder you're using and whether it supports threading to begin with (see CODEC_CAP_*_THREADS). If you see no improvement, please mention what encoder you're using and what other parameters you set, i.e. please show code.