1FFmpeg multithreading methods 2============================================== 3 4FFmpeg provides two methods for multithreading codecs. 5 6Slice threading decodes multiple parts of a frame at the same time, using 7AVCodecContext execute() and execute2(). 8 9Frame threading decodes multiple frames at the same time. 10It accepts N future frames and delays decoded pictures by N-1 frames. 11The later frames are decoded in separate threads while the user is 12displaying the current one. 13 14Restrictions on clients 15============================================== 16 17Slice threading - 18* The client's draw_horiz_band() must be thread-safe according to the comment 19 in avcodec.h. 20 21Frame threading - 22* Restrictions with slice threading also apply. 23* Custom get_buffer2() and get_format() callbacks must be thread-safe. 24* There is one frame of delay added for every thread beyond the first one. 25 Clients must be able to handle this; the pkt_dts and pkt_pts fields in 26 AVFrame will work as usual. 27 28Restrictions on codec implementations 29============================================== 30 31Slice threading - 32 None except that there must be something worth executing in parallel. 33 34Frame threading - 35* Codecs can only accept entire pictures per packet. 36* Codecs similar to ffv1, whose streams don't reset across frames, 37 will not work because their bitstreams cannot be decoded in parallel. 38 39* The contents of buffers must not be read before ff_thread_await_progress() 40 has been called on them. reget_buffer() and buffer age optimizations no longer work. 41* The contents of buffers must not be written to after ff_thread_report_progress() 42 has been called on them. This includes draw_edges(). 43 44Porting codecs to frame threading 45============================================== 46 47Find all context variables that are needed by the next frame. Move all 48code changing them, as well as code calling get_buffer(), up to before 49the decode process starts. Call ff_thread_finish_setup() afterwards. If 50some code can't be moved, have update_thread_context() run it in the next 51thread. 52 53Add AV_CODEC_CAP_FRAME_THREADS to the codec capabilities. There will be very little 54speed gain at this point but it should work. 55 56If there are inter-frame dependencies, so the codec calls 57ff_thread_report/await_progress(), set FF_CODEC_CAP_ALLOCATE_PROGRESS in 58AVCodec.caps_internal and use ff_thread_get_buffer() to allocate frames. The 59frames must then be freed with ff_thread_release_buffer(). 60Otherwise decode directly into the user-supplied frames. 61 62Call ff_thread_report_progress() after some part of the current picture has decoded. 63A good place to put this is where draw_horiz_band() is called - add this if it isn't 64called anywhere, as it's useful too and the implementation is trivial when you're 65doing this. Note that draw_edges() needs to be called before reporting progress. 66 67Before accessing a reference frame or its MVs, call ff_thread_await_progress(). 68