filter_design.txt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. Filter design
  2. =============
  3. This document explains guidelines that should be observed (or ignored with
  4. good reason) when writing filters for libavfilter.
  5. In this document, the word “frame” indicates either a video frame or a group
  6. of audio samples, as stored in an AVFilterBuffer structure.
  7. Format negotiation
  8. ==================
  9. The query_formats method should set, for each input and each output links,
  10. the list supported formats.
  11. For video links, that means pixel format. For audio links, that means
  12. channel layout, and sample format (the sample packing is implied by the
  13. sample format).
  14. The lists are not just lists, they are references to shared objects. When
  15. the negotiation mechanism computes the intersection of the formats
  16. supported at each ends of a link, all references to both lists are
  17. replaced with a reference to the intersection. And when a single format is
  18. eventually chosen for a link amongst the remaining list, again, all
  19. references to the list are updated.
  20. That means that if a filter requires that its input and output have the
  21. same format amongst a supported list, all it have to do is use a reference
  22. to the same list of formats.
  23. Buffer references ownership and permissions
  24. ===========================================
  25. TODO
  26. Frame scheduling
  27. ================
  28. The purpose of these rules is to ensure that frames flow in the filter
  29. graph without getting stuck and accumulating somewhere.
  30. Simple filters that output one frame for each input frame should not have
  31. to worry about it.
  32. start_frame / filter_samples
  33. ----------------------------
  34. These methods are called when a frame is pushed to the filter's input.
  35. They can be called at any time except in a reentrant way.
  36. If the input frame is enough to produce output, then the filter should
  37. push the output frames on the output link immediately.
  38. As an exception to the previous rule, if the input frame is enough to
  39. produce several output frames, then the filter needs output only at
  40. least one per link. The additional frames can be left buffered in the
  41. filter; these buffered frames must be flushed immediately if a new input
  42. produces new output.
  43. (Example: framerate-doubling filter: start_frame must (1) flush the
  44. second copy of the previous frame, if it is still there, (2) push the
  45. first copy of the incoming frame, (3) keep the second copy for later.)
  46. If the input frame is not enough to produce output, the filter must not
  47. call request_frame to get more. It must just process the frame or queue
  48. it. The task of requesting more frames is left to the filter's
  49. request_frame method or the application.
  50. If a filter has several inputs, the filter must be ready for frames
  51. arriving randomly on any input. Therefore, any filter with several input
  52. will most likely require some kind of queuing mechanism. It is perfectly
  53. acceptable to have a limited queue and to drop frames when the inputs
  54. are too unbalanced.
  55. request_frame
  56. -------------
  57. This method is called when a frame is wanted on an output.
  58. For an input, it should directly call start_frame or filter_samples on
  59. the corresponding output.
  60. For a filter, if there are queued frames already ready, one of these
  61. frames should be pushed. If not, the filter should request a frame on
  62. one of its input, repeatedly until at least one frame has been pushed.
  63. Return values:
  64. if request_frame could produce a frame, it should return 0;
  65. if it could not for temporary reasons, it should return AVERROR(EAGAIN);
  66. if it could not because there are no more frames, it should return
  67. AVERROR_EOF.
  68. The typical implementation of request_frame for a filter with several
  69. inputs will look like that:
  70. if (frames_queued) {
  71. push_one_frame();
  72. return 0;
  73. }
  74. while (!frame_pushed) {
  75. input = input_where_a_frame_is_most_needed();
  76. ret = avfilter_request_frame(input);
  77. if (ret == AVERROR_EOF) {
  78. process_eof_on_input();
  79. } else if (ret < 0) {
  80. return ret;
  81. }
  82. }
  83. return 0;
  84. Note that, except for filters that can have queued frames, request_frame
  85. does not push frames: it requests them to its input, and as a reaction,
  86. the start_frame / filter_samples method will be called and do the work.