multithreading.txt 3.0 KB

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