filter_design.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 AVFrame 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. query_formats can leave some formats unset and return AVERROR(EAGAIN) to
  24. cause the negotiation mechanism to try again later. That can be used by
  25. filters with complex requirements to use the format negotiated on one link
  26. to set the formats supported on another.
  27. Frame references ownership and permissions
  28. ==========================================
  29. Principle
  30. ---------
  31. Audio and video data are voluminous; the frame and frame reference
  32. mechanism is intended to avoid, as much as possible, expensive copies of
  33. that data while still allowing the filters to produce correct results.
  34. The data is stored in buffers represented by AVFrame structures.
  35. Several references can point to the same frame buffer; the buffer is
  36. automatically deallocated once all corresponding references have been
  37. destroyed.
  38. The characteristics of the data (resolution, sample rate, etc.) are
  39. stored in the reference; different references for the same buffer can
  40. show different characteristics. In particular, a video reference can
  41. point to only a part of a video buffer.
  42. A reference is usually obtained as input to the filter_frame method or
  43. requested using the ff_get_video_buffer or ff_get_audio_buffer
  44. functions. A new reference on an existing buffer can be created with
  45. av_frame_ref(). A reference is destroyed using
  46. the av_frame_free() function.
  47. Reference ownership
  48. -------------------
  49. At any time, a reference “belongs” to a particular piece of code,
  50. usually a filter. With a few caveats that will be explained below, only
  51. that piece of code is allowed to access it. It is also responsible for
  52. destroying it, although this is sometimes done automatically (see the
  53. section on link reference fields).
  54. Here are the (fairly obvious) rules for reference ownership:
  55. * A reference received by the filter_frame method belongs to the
  56. corresponding filter.
  57. * A reference passed to ff_filter_frame is given away and must no longer
  58. be used.
  59. * A reference created with av_frame_ref() 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 AVFrame fields.
  68. partial_buf is used by libavfilter internally and must not be accessed
  69. by filters.
  70. fifo contains frames queued in the filter's input. They belong to the
  71. framework until they are taken by the filter.
  72. Reference permissions
  73. ---------------------
  74. Since the same frame data can be shared by several frames, modifying may
  75. have unintended consequences. A frame is considered writable if only one
  76. reference to it exists. The code owning that reference it then allowed
  77. to modify the data.
  78. A filter can check if a frame is writable by using the
  79. av_frame_is_writable() function.
  80. A filter can ensure that a frame is writable at some point of the code
  81. by using the ff_inlink_make_frame_writable() function. It will duplicate
  82. the frame if needed.
  83. A filter can ensure that the frame passed to the filter_frame() callback
  84. is writable by setting the needs_writable flag on the corresponding
  85. input pad. It does not apply to the activate() callback.
  86. Frame scheduling
  87. ================
  88. The purpose of these rules is to ensure that frames flow in the filter
  89. graph without getting stuck and accumulating somewhere.
  90. Simple filters that output one frame for each input frame should not have
  91. to worry about it.
  92. There are two design for filters: one using the filter_frame() and
  93. request_frame() callbacks and the other using the activate() callback.
  94. The design using filter_frame() and request_frame() is legacy, but it is
  95. suitable for filters that have a single input and process one frame at a
  96. time. New filters with several inputs, that treat several frames at a time
  97. or that require a special treatment at EOF should probably use the design
  98. using activate().
  99. activate
  100. --------
  101. This method is called when something must be done in a filter; the
  102. definition of that "something" depends on the semantic of the filter.
  103. The callback must examine the status of the filter's links and proceed
  104. accordingly.
  105. The status of output links is stored in the frame_wanted_out, status_in
  106. and status_out fields and tested by the ff_outlink_frame_wanted()
  107. function. If this function returns true, then the processing requires a
  108. frame on this link and the filter is expected to make efforts in that
  109. direction.
  110. The status of input links is stored by the status_in, fifo and
  111. status_out fields; they must not be accessed directly. The fifo field
  112. contains the frames that are queued in the input for processing by the
  113. filter. The status_in and status_out fields contains the queued status
  114. (EOF or error) of the link; status_in is a status change that must be
  115. taken into account after all frames in fifo have been processed;
  116. status_out is the status that have been taken into account, it is final
  117. when it is not 0.
  118. The typical task of an activate callback is to first check the backward
  119. status of output links, and if relevant forward it to the corresponding
  120. input. Then, if relevant, for each input link: test the availability of
  121. frames in fifo and process them; if no frame is available, test and
  122. acknowledge a change of status using ff_inlink_acknowledge_status(); and
  123. forward the result (frame or status change) to the corresponding input.
  124. If nothing is possible, test the status of outputs and forward it to the
  125. corresponding input(s). If still not possible, return FFERROR_NOT_READY.
  126. If the filters stores internally one or a few frame for some input, it
  127. can consider them to be part of the FIFO and delay acknowledging a
  128. status change accordingly.
  129. Example code:
  130. ret = ff_outlink_get_status(outlink);
  131. if (ret) {
  132. ff_inlink_set_status(inlink, ret);
  133. return 0;
  134. }
  135. if (priv->next_frame) {
  136. /* use it */
  137. return 0;
  138. }
  139. ret = ff_inlink_consume_frame(inlink, &frame);
  140. if (ret < 0)
  141. return ret;
  142. if (ret) {
  143. /* use it */
  144. return 0;
  145. }
  146. ret = ff_inlink_acknowledge_status(inlink, &status, &pts);
  147. if (ret) {
  148. /* flush */
  149. ff_outlink_set_status(outlink, status, pts);
  150. return 0;
  151. }
  152. if (ff_outlink_frame_wanted(outlink)) {
  153. ff_inlink_request_frame(inlink);
  154. return 0;
  155. }
  156. return FFERROR_NOT_READY;
  157. The exact code depends on how similar the /* use it */ blocks are and
  158. how related they are to the /* flush */ block, and needs to apply these
  159. operations to the correct inlink or outlink if there are several.
  160. Macros are available to factor that when no extra processing is needed:
  161. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  162. FF_FILTER_FORWARD_STATUS_ALL(outlink, filter);
  163. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  164. FF_FILTER_FORWARD_STATUS_ALL(inlink, filter);
  165. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  166. filter_frame
  167. ------------
  168. For filters that do not use the activate() callback, this method is
  169. called when a frame is pushed to the filter's input. It can be called at
  170. any time except in a reentrant way.
  171. If the input frame is enough to produce output, then the filter should
  172. push the output frames on the output link immediately.
  173. As an exception to the previous rule, if the input frame is enough to
  174. produce several output frames, then the filter needs output only at
  175. least one per link. The additional frames can be left buffered in the
  176. filter; these buffered frames must be flushed immediately if a new input
  177. produces new output.
  178. (Example: frame rate-doubling filter: filter_frame must (1) flush the
  179. second copy of the previous frame, if it is still there, (2) push the
  180. first copy of the incoming frame, (3) keep the second copy for later.)
  181. If the input frame is not enough to produce output, the filter must not
  182. call request_frame to get more. It must just process the frame or queue
  183. it. The task of requesting more frames is left to the filter's
  184. request_frame method or the application.
  185. If a filter has several inputs, the filter must be ready for frames
  186. arriving randomly on any input. Therefore, any filter with several inputs
  187. will most likely require some kind of queuing mechanism. It is perfectly
  188. acceptable to have a limited queue and to drop frames when the inputs
  189. are too unbalanced.
  190. request_frame
  191. -------------
  192. For filters that do not use the activate() callback, this method is
  193. called when a frame is wanted on an output.
  194. For a source, it should directly call filter_frame on the corresponding
  195. output.
  196. For a filter, if there are queued frames already ready, one of these
  197. frames should be pushed. If not, the filter should request a frame on
  198. one of its inputs, repeatedly until at least one frame has been pushed.
  199. Return values:
  200. if request_frame could produce a frame, or at least make progress
  201. towards producing a frame, it should return 0;
  202. if it could not for temporary reasons, it should return AVERROR(EAGAIN);
  203. if it could not because there are no more frames, it should return
  204. AVERROR_EOF.
  205. The typical implementation of request_frame for a filter with several
  206. inputs will look like that:
  207. if (frames_queued) {
  208. push_one_frame();
  209. return 0;
  210. }
  211. input = input_where_a_frame_is_most_needed();
  212. ret = ff_request_frame(input);
  213. if (ret == AVERROR_EOF) {
  214. process_eof_on_input();
  215. } else if (ret < 0) {
  216. return ret;
  217. }
  218. return 0;
  219. Note that, except for filters that can have queued frames and sources,
  220. request_frame does not push frames: it requests them to its input, and
  221. as a reaction, the filter_frame method possibly will be called and do
  222. the work.