You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
11KB

  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, sample format (the sample packing is implied by the sample
  13. format) and sample rate.
  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 end of a link, all references to both lists are replaced
  17. 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 filter_frame method (or its start_frame
  53. deprecated version) belongs 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_filter_frame (or the deprecated
  58. ff_start_frame) is given 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. The
  68. cur_buf and out_buf were used with the deprecated
  69. start_frame/draw_slice/end_frame API and should no longer be used.
  70. src_buf, cur_buf_copy and partial_buf are used by libavfilter internally
  71. and must not be accessed by filters.
  72. Reference permissions
  73. ---------------------
  74. The AVFilterBufferRef structure has a perms field that describes what
  75. the code that owns the reference is allowed to do to the buffer data.
  76. Different references for the same buffer can have different permissions.
  77. For video filters that implement the deprecated
  78. start_frame/draw_slice/end_frame API, the permissions only apply to the
  79. parts of the buffer that have already been covered by the draw_slice
  80. method.
  81. The value is a binary OR of the following constants:
  82. * AV_PERM_READ: the owner can read the buffer data; this is essentially
  83. always true and is there for self-documentation.
  84. * AV_PERM_WRITE: the owner can modify the buffer data.
  85. * AV_PERM_PRESERVE: the owner can rely on the fact that the buffer data
  86. will not be modified by previous filters.
  87. * AV_PERM_REUSE: the owner can output the buffer several times, without
  88. modifying the data in between.
  89. * AV_PERM_REUSE2: the owner can output the buffer several times and
  90. modify the data in between (useless without the WRITE permissions).
  91. * AV_PERM_ALIGN: the owner can access the data using fast operations
  92. that require data alignment.
  93. The READ, WRITE and PRESERVE permissions are about sharing the same
  94. buffer between several filters to avoid expensive copies without them
  95. doing conflicting changes on the data.
  96. The REUSE and REUSE2 permissions are about special memory for direct
  97. rendering. For example a buffer directly allocated in video memory must
  98. not modified once it is displayed on screen, or it will cause tearing;
  99. it will therefore not have the REUSE2 permission.
  100. The ALIGN permission is about extracting part of the buffer, for
  101. copy-less padding or cropping for example.
  102. References received on input pads are guaranteed to have all the
  103. permissions stated in the min_perms field and none of the permissions
  104. stated in the rej_perms.
  105. References obtained by ff_get_video_buffer and ff_get_audio_buffer are
  106. guaranteed to have at least all the permissions requested as argument.
  107. References created by avfilter_ref_buffer have the same permissions as
  108. the original reference minus the ones explicitly masked; the mask is
  109. usually ~0 to keep the same permissions.
  110. Filters should remove permissions on reference they give to output
  111. whenever necessary. It can be automatically done by setting the
  112. rej_perms field on the output pad.
  113. Here are a few guidelines corresponding to common situations:
  114. * Filters that modify and forward their frame (like drawtext) need the
  115. WRITE permission.
  116. * Filters that read their input to produce a new frame on output (like
  117. scale) need the READ permission on input and and must request a buffer
  118. with the WRITE permission.
  119. * Filters that intend to keep a reference after the filtering process
  120. is finished (after filter_frame returns) must have the PRESERVE
  121. permission on it and remove the WRITE permission if they create a new
  122. reference to give it away.
  123. * Filters that intend to modify a reference they have kept after the end
  124. of the filtering process need the REUSE2 permission and must remove
  125. the PRESERVE permission if they create a new reference to give it
  126. away.
  127. Frame scheduling
  128. ================
  129. The purpose of these rules is to ensure that frames flow in the filter
  130. graph without getting stuck and accumulating somewhere.
  131. Simple filters that output one frame for each input frame should not have
  132. to worry about it.
  133. filter_frame
  134. ------------
  135. This method is called when a frame is pushed to the filter's input. It
  136. can be called at any time except in a reentrant way.
  137. If the input frame is enough to produce output, then the filter should
  138. push the output frames on the output link immediately.
  139. As an exception to the previous rule, if the input frame is enough to
  140. produce several output frames, then the filter needs output only at
  141. least one per link. The additional frames can be left buffered in the
  142. filter; these buffered frames must be flushed immediately if a new input
  143. produces new output.
  144. (Example: framerate-doubling filter: filter_frame must (1) flush the
  145. second copy of the previous frame, if it is still there, (2) push the
  146. first copy of the incoming frame, (3) keep the second copy for later.)
  147. If the input frame is not enough to produce output, the filter must not
  148. call request_frame to get more. It must just process the frame or queue
  149. it. The task of requesting more frames is left to the filter's
  150. request_frame method or the application.
  151. If a filter has several inputs, the filter must be ready for frames
  152. arriving randomly on any input. Therefore, any filter with several inputs
  153. will most likely require some kind of queuing mechanism. It is perfectly
  154. acceptable to have a limited queue and to drop frames when the inputs
  155. are too unbalanced.
  156. request_frame
  157. -------------
  158. This method is called when a frame is wanted on an output.
  159. For an input, it should directly call filter_frame on the corresponding
  160. output.
  161. For a filter, if there are queued frames already ready, one of these
  162. frames should be pushed. If not, the filter should request a frame on
  163. one of its inputs, repeatedly until at least one frame has been pushed.
  164. Return values:
  165. if request_frame could produce a frame, it should return 0;
  166. if it could not for temporary reasons, it should return AVERROR(EAGAIN);
  167. if it could not because there are no more frames, it should return
  168. AVERROR_EOF.
  169. The typical implementation of request_frame for a filter with several
  170. inputs will look like that:
  171. if (frames_queued) {
  172. push_one_frame();
  173. return 0;
  174. }
  175. while (!frame_pushed) {
  176. input = input_where_a_frame_is_most_needed();
  177. ret = ff_request_frame(input);
  178. if (ret == AVERROR_EOF) {
  179. process_eof_on_input();
  180. } else if (ret < 0) {
  181. return ret;
  182. }
  183. }
  184. return 0;
  185. Note that, except for filters that can have queued frames, request_frame
  186. does not push frames: it requests them to its input, and as a reaction,
  187. the filter_frame method will be called and do the work.
  188. Legacy API
  189. ==========
  190. Until libavfilter 3.23, the filter_frame method was split:
  191. - for video filters, it was made of start_frame, draw_slice (that could be
  192. called several times on distinct parts of the frame) and end_frame;
  193. - for audio filters, it was called filter_samples.