multithreading.txt 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. * Custom get_buffer2() and get_format() callbacks must be thread-safe.
  18. * There is one frame of delay added for every thread beyond the first one.
  19. Clients must be able to handle this; the pkt_dts and pkt_pts fields in
  20. AVFrame will work as usual.
  21. Restrictions on codec implementations
  22. ==============================================
  23. Slice threading -
  24. None except that there must be something worth executing in parallel.
  25. Frame threading -
  26. * Codecs can only accept entire pictures per packet.
  27. * Codecs similar to ffv1, whose streams don't reset across frames,
  28. will not work because their bitstreams cannot be decoded in parallel.
  29. * The contents of buffers must not be read before ff_progress_frame_await()
  30. has been called on them. reget_buffer() and buffer age optimizations no longer work.
  31. * The contents of buffers must not be written to after ff_progress_frame_report()
  32. has been called on them. This includes draw_edges().
  33. Porting codecs to frame threading
  34. ==============================================
  35. Find all context variables that are needed by the next frame. Move all
  36. code changing them, as well as code calling get_buffer(), up to before
  37. the decode process starts. Call ff_thread_finish_setup() afterwards. If
  38. some code can't be moved, have update_thread_context() run it in the next
  39. thread.
  40. Add AV_CODEC_CAP_FRAME_THREADS to the codec capabilities. There will be very little
  41. speed gain at this point but it should work.
  42. Use ff_thread_get_buffer() (or ff_progress_frame_get_buffer()
  43. in case you have inter-frame dependencies and use the ProgressFrame API)
  44. to allocate frame buffers.
  45. Call ff_progress_frame_report() after some part of the current picture has decoded.
  46. A good place to put this is where draw_horiz_band() is called - add this if it isn't
  47. called anywhere, as it's useful too and the implementation is trivial when you're
  48. doing this. Note that draw_edges() needs to be called before reporting progress.
  49. Before accessing a reference frame or its MVs, call ff_progress_frame_await().