filter_design.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 of 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 has to do is use a reference
  22. to the same list of formats.
  23. Buffer references ownership and permissions
  24. ===========================================
  25. Principle
  26. ---------
  27. Audio and video data are voluminous; the buffer and buffer reference
  28. mechanism is intended to avoid, as much as possible, expensive copies of
  29. that data while still allowing the filters to produce correct results.
  30. The data is stored in buffers represented by AVFilterBuffer structures.
  31. They must not be accessed directly, but through references stored in
  32. AVFilterBufferRef structures. Several references can point to the
  33. same buffer; the buffer is automatically deallocated once all
  34. corresponding references have been destroyed.
  35. The characteristics of the data (resolution, sample rate, etc.) are
  36. stored in the reference; different references for the same buffer can
  37. show different characteristics. In particular, a video reference can
  38. point to only a part of a video buffer.
  39. A reference is usually obtained as input to the start_frame or
  40. filter_frame method or requested using the ff_get_video_buffer or
  41. ff_get_audio_buffer functions. A new reference on an existing buffer can
  42. be created with the avfilter_ref_buffer. A reference is destroyed using
  43. the avfilter_unref_bufferp function.
  44. Reference ownership
  45. -------------------
  46. At any time, a reference “belongs” to a particular piece of code,
  47. usually a filter. With a few caveats that will be explained below, only
  48. that piece of code is allowed to access it. It is also responsible for
  49. destroying it, although this is sometimes done automatically (see the
  50. section on link reference fields).
  51. Here are the (fairly obvious) rules for reference ownership:
  52. * A reference received by the start_frame or filter_frame method
  53. belong to the corresponding filter.
  54. Special exception: for video references: the reference may be used
  55. internally for automatic copying and must not be destroyed before
  56. end_frame; it can be given away to ff_start_frame.
  57. * A reference passed to ff_start_frame or ff_filter_frame is given
  58. away and must no longer be used.
  59. * A reference created with avfilter_ref_buffer belongs to the code that
  60. created it.
  61. * A reference obtained with ff_get_video_buffer or ff_get_audio_buffer
  62. belongs to the code that requested it.
  63. * A reference given as return value by the get_video_buffer or
  64. get_audio_buffer method is given away and must no longer be used.
  65. Link reference fields
  66. ---------------------
  67. The AVFilterLink structure has a few AVFilterBufferRef fields. Here are
  68. the rules to handle them:
  69. * cur_buf is set before the start_frame and filter_frame methods to
  70. the same reference given as argument to the methods and belongs to the
  71. destination filter of the link. If it has not been cleared after
  72. end_frame or filter_frame, libavfilter will automatically destroy
  73. the reference; therefore, any filter that needs to keep the reference
  74. for longer must set cur_buf to NULL.
  75. * out_buf belongs to the source filter of the link and can be used to
  76. store a reference to the buffer that has been sent to the destination.
  77. If it is not NULL after end_frame or filter_frame, libavfilter will
  78. automatically destroy the reference.
  79. If a video input pad does not have a start_frame method, the default
  80. method will request a buffer on the first output of the filter, store
  81. the reference in out_buf and push a second reference to the output.
  82. * src_buf, cur_buf_copy and partial_buf are used by libavfilter
  83. internally and must not be accessed by filters.
  84. Reference permissions
  85. ---------------------
  86. The AVFilterBufferRef structure has a perms field that describes what
  87. the code that owns the reference is allowed to do to the buffer data.
  88. Different references for the same buffer can have different permissions.
  89. For video filters, the permissions only apply to the parts of the buffer
  90. that have already been covered by the draw_slice method.
  91. The value is a binary OR of the following constants:
  92. * AV_PERM_READ: the owner can read the buffer data; this is essentially
  93. always true and is there for self-documentation.
  94. * AV_PERM_WRITE: the owner can modify the buffer data.
  95. * AV_PERM_PRESERVE: the owner can rely on the fact that the buffer data
  96. will not be modified by previous filters.
  97. * AV_PERM_REUSE: the owner can output the buffer several times, without
  98. modifying the data in between.
  99. * AV_PERM_REUSE2: the owner can output the buffer several times and
  100. modify the data in between (useless without the WRITE permissions).
  101. * AV_PERM_ALIGN: the owner can access the data using fast operations
  102. that require data alignment.
  103. The READ, WRITE and PRESERVE permissions are about sharing the same
  104. buffer between several filters to avoid expensive copies without them
  105. doing conflicting changes on the data.
  106. The REUSE and REUSE2 permissions are about special memory for direct
  107. rendering. For example a buffer directly allocated in video memory must
  108. not modified once it is displayed on screen, or it will cause tearing;
  109. it will therefore not have the REUSE2 permission.
  110. The ALIGN permission is about extracting part of the buffer, for
  111. copy-less padding or cropping for example.
  112. References received on input pads are guaranteed to have all the
  113. permissions stated in the min_perms field and none of the permissions
  114. stated in the rej_perms.
  115. References obtained by ff_get_video_buffer and ff_get_audio_buffer are
  116. guaranteed to have at least all the permissions requested as argument.
  117. References created by avfilter_ref_buffer have the same permissions as
  118. the original reference minus the ones explicitly masked; the mask is
  119. usually ~0 to keep the same permissions.
  120. Filters should remove permissions on reference they give to output
  121. whenever necessary. It can be automatically done by setting the
  122. rej_perms field on the output pad.
  123. Here are a few guidelines corresponding to common situations:
  124. * Filters that modify and forward their frame (like drawtext) need the
  125. WRITE permission.
  126. * Filters that read their input to produce a new frame on output (like
  127. scale) need the READ permission on input and and must request a buffer
  128. with the WRITE permission.
  129. * Filters that intend to keep a reference after the filtering process
  130. is finished (after end_frame or filter_frame returns) must have the
  131. PRESERVE permission on it and remove the WRITE permission if they
  132. create a new reference to give it away.
  133. * Filters that intend to modify a reference they have kept after the end
  134. of the filtering process need the REUSE2 permission and must remove
  135. the PRESERVE permission if they create a new reference to give it
  136. away.
  137. Frame scheduling
  138. ================
  139. The purpose of these rules is to ensure that frames flow in the filter
  140. graph without getting stuck and accumulating somewhere.
  141. Simple filters that output one frame for each input frame should not have
  142. to worry about it.
  143. start_frame / filter_frame
  144. ----------------------------
  145. These methods are called when a frame is pushed to the filter's input.
  146. They can be called at any time except in a reentrant way.
  147. If the input frame is enough to produce output, then the filter should
  148. push the output frames on the output link immediately.
  149. As an exception to the previous rule, if the input frame is enough to
  150. produce several output frames, then the filter needs output only at
  151. least one per link. The additional frames can be left buffered in the
  152. filter; these buffered frames must be flushed immediately if a new input
  153. produces new output.
  154. (Example: framerate-doubling filter: start_frame must (1) flush the
  155. second copy of the previous frame, if it is still there, (2) push the
  156. first copy of the incoming frame, (3) keep the second copy for later.)
  157. If the input frame is not enough to produce output, the filter must not
  158. call request_frame to get more. It must just process the frame or queue
  159. it. The task of requesting more frames is left to the filter's
  160. request_frame method or the application.
  161. If a filter has several inputs, the filter must be ready for frames
  162. arriving randomly on any input. Therefore, any filter with several inputs
  163. will most likely require some kind of queuing mechanism. It is perfectly
  164. acceptable to have a limited queue and to drop frames when the inputs
  165. are too unbalanced.
  166. request_frame
  167. -------------
  168. This method is called when a frame is wanted on an output.
  169. For an input, it should directly call start_frame or filter_frame on
  170. the corresponding output.
  171. For a filter, if there are queued frames already ready, one of these
  172. frames should be pushed. If not, the filter should request a frame on
  173. one of its inputs, repeatedly until at least one frame has been pushed.
  174. Return values:
  175. if request_frame could produce a frame, it should return 0;
  176. if it could not for temporary reasons, it should return AVERROR(EAGAIN);
  177. if it could not because there are no more frames, it should return
  178. AVERROR_EOF.
  179. The typical implementation of request_frame for a filter with several
  180. inputs will look like that:
  181. if (frames_queued) {
  182. push_one_frame();
  183. return 0;
  184. }
  185. while (!frame_pushed) {
  186. input = input_where_a_frame_is_most_needed();
  187. ret = avfilter_request_frame(input);
  188. if (ret == AVERROR_EOF) {
  189. process_eof_on_input();
  190. } else if (ret < 0) {
  191. return ret;
  192. }
  193. }
  194. return 0;
  195. Note that, except for filters that can have queued frames, request_frame
  196. does not push frames: it requests them to its input, and as a reaction,
  197. the start_frame / filter_frame method will be called and do the work.