filters.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Filters implementation helper functions
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVFILTER_FILTERS_H
  21. #define AVFILTER_FILTERS_H
  22. /**
  23. * Filters implementation helper functions
  24. */
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. /**
  28. * Special return code when activate() did not do anything.
  29. */
  30. #define FFERROR_NOT_READY FFERRTAG('N','R','D','Y')
  31. /**
  32. * Mark a filter ready and schedule it for activation.
  33. *
  34. * This is automatically done when something happens to the filter (queued
  35. * frame, status change, request on output).
  36. * Filters implementing the activate callback can call it directly to
  37. * perform one more round of processing later.
  38. * It is also useful for filters reacting to external or asynchronous
  39. * events.
  40. */
  41. void ff_filter_set_ready(AVFilterContext *filter, unsigned priority);
  42. /**
  43. * Process the commands queued in the link up to the time of the frame.
  44. * Commands will trigger the process_command() callback.
  45. * @return >= 0 or AVERROR code.
  46. */
  47. int ff_inlink_process_commands(AVFilterLink *link, const AVFrame *frame);
  48. /**
  49. * Evaluate the timeline expression of the link for the time and properties
  50. * of the frame.
  51. * @return >0 if enabled, 0 if disabled
  52. * @note It does not update link->dst->is_disabled.
  53. */
  54. int ff_inlink_evaluate_timeline_at_frame(AVFilterLink *link, const AVFrame *frame);
  55. /**
  56. * Get the number of frames available on the link.
  57. * @return the number of frames available in the link fifo.
  58. */
  59. size_t ff_inlink_queued_frames(AVFilterLink *link);
  60. /**
  61. * Test if a frame is available on the link.
  62. * @return >0 if a frame is available
  63. */
  64. int ff_inlink_check_available_frame(AVFilterLink *link);
  65. /***
  66. * Get the number of samples available on the link.
  67. * @return the numer of samples available on the link.
  68. */
  69. int ff_inlink_queued_samples(AVFilterLink *link);
  70. /**
  71. * Test if enough samples are available on the link.
  72. * @return >0 if enough samples are available
  73. * @note on EOF and error, min becomes 1
  74. */
  75. int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min);
  76. /**
  77. * Take a frame from the link's FIFO and update the link's stats.
  78. *
  79. * If ff_inlink_check_available_frame() was previously called, the
  80. * preferred way of expressing it is "av_assert1(ret);" immediately after
  81. * ff_inlink_consume_frame(). Negative error codes must still be checked.
  82. *
  83. * @note May trigger process_command() and/or update is_disabled.
  84. * @return >0 if a frame is available,
  85. * 0 and set rframe to NULL if no frame available,
  86. * or AVERROR code
  87. */
  88. int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe);
  89. /**
  90. * Take samples from the link's FIFO and update the link's stats.
  91. *
  92. * If ff_inlink_check_available_samples() was previously called, the
  93. * preferred way of expressing it is "av_assert1(ret);" immediately after
  94. * ff_inlink_consume_samples(). Negative error codes must still be checked.
  95. *
  96. * @note May trigger process_command() and/or update is_disabled.
  97. * @return >0 if a frame is available,
  98. * 0 and set rframe to NULL if no frame available,
  99. * or AVERROR code
  100. */
  101. int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max,
  102. AVFrame **rframe);
  103. /**
  104. * Access a frame in the link fifo without consuming it.
  105. * The first frame is numbered 0; the designated frame must exist.
  106. * @return the frame at idx position in the link fifo.
  107. */
  108. AVFrame *ff_inlink_peek_frame(AVFilterLink *link, size_t idx);
  109. /**
  110. * Make sure a frame is writable.
  111. * This is similar to av_frame_make_writable() except it uses the link's
  112. * buffer allocation callback, and therefore allows direct rendering.
  113. */
  114. int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe);
  115. /**
  116. * Test and acknowledge the change of status on the link.
  117. *
  118. * Status means EOF or an error condition; a change from the normal (0)
  119. * status to a non-zero status can be queued in a filter's input link, it
  120. * becomes relevant after the frames queued in the link's FIFO are
  121. * processed. This function tests if frames are still queued and if a queued
  122. * status change has not yet been processed. In that case it performs basic
  123. * treatment (updating the link's timestamp) and returns a positive value to
  124. * let the filter do its own treatments (flushing...).
  125. *
  126. * Filters implementing the activate callback should call this function when
  127. * they think it might succeed (usually after checking unsuccessfully for a
  128. * queued frame).
  129. * Filters implementing the filter_frame and request_frame callbacks do not
  130. * need to call that since the same treatment happens in ff_filter_frame().
  131. *
  132. * @param[out] rstatus new or current status
  133. * @param[out] rpts current timestamp of the link in link time base
  134. * @return >0 if status changed, <0 if status already acked, 0 otherwise
  135. */
  136. int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts);
  137. /**
  138. * Mark that a frame is wanted on the link.
  139. * Unlike ff_filter_frame(), it must not be called when the link has a
  140. * non-zero status, and thus does not acknowledge it.
  141. * Also it cannot fail.
  142. */
  143. void ff_inlink_request_frame(AVFilterLink *link);
  144. /**
  145. * Set the status on an input link.
  146. * Also discard all frames in the link's FIFO.
  147. */
  148. void ff_inlink_set_status(AVFilterLink *link, int status);
  149. /**
  150. * Test if a frame is wanted on an output link.
  151. */
  152. static inline int ff_outlink_frame_wanted(AVFilterLink *link)
  153. {
  154. return link->frame_wanted_out;
  155. }
  156. /**
  157. * Get the status on an output link.
  158. */
  159. int ff_outlink_get_status(AVFilterLink *link);
  160. /**
  161. * Set the status field of a link from the source filter.
  162. * The pts should reflect the timestamp of the status change,
  163. * in link time base and relative to the frames timeline.
  164. * In particular, for AVERROR_EOF, it should reflect the
  165. * end time of the last frame.
  166. */
  167. static inline void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
  168. {
  169. ff_avfilter_link_set_in_status(link, status, pts);
  170. }
  171. /**
  172. * Forward the status on an output link to an input link.
  173. * If the status is set, it will discard all queued frames and this macro
  174. * will return immediately.
  175. */
  176. #define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink) do { \
  177. int ret = ff_outlink_get_status(outlink); \
  178. if (ret) { \
  179. ff_inlink_set_status(inlink, ret); \
  180. return 0; \
  181. } \
  182. } while (0)
  183. /**
  184. * Forward the status on an output link to all input links.
  185. * If the status is set, it will discard all queued frames and this macro
  186. * will return immediately.
  187. */
  188. #define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter) do { \
  189. int ret = ff_outlink_get_status(outlink); \
  190. if (ret) { \
  191. unsigned i; \
  192. for (i = 0; i < filter->nb_inputs; i++) \
  193. ff_inlink_set_status(filter->inputs[i], ret); \
  194. return 0; \
  195. } \
  196. } while (0)
  197. /**
  198. * Acknowledge the status on an input link and forward it to an output link.
  199. * If the status is set, this macro will return immediately.
  200. */
  201. #define FF_FILTER_FORWARD_STATUS(inlink, outlink) do { \
  202. int status; \
  203. int64_t pts; \
  204. if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { \
  205. ff_outlink_set_status(outlink, status, pts); \
  206. return 0; \
  207. } \
  208. } while (0)
  209. /**
  210. * Acknowledge the status on an input link and forward it to an output link.
  211. * If the status is set, this macro will return immediately.
  212. */
  213. #define FF_FILTER_FORWARD_STATUS_ALL(inlink, filter) do { \
  214. int status; \
  215. int64_t pts; \
  216. if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { \
  217. unsigned i; \
  218. for (i = 0; i < filter->nb_outputs; i++) \
  219. ff_outlink_set_status(filter->outputs[i], status, pts); \
  220. return 0; \
  221. } \
  222. } while (0)
  223. /**
  224. * Forward the frame_wanted_out flag from an output link to an input link.
  225. * If the flag is set, this macro will return immediately.
  226. */
  227. #define FF_FILTER_FORWARD_WANTED(outlink, inlink) do { \
  228. if (ff_outlink_frame_wanted(outlink)) { \
  229. ff_inlink_request_frame(inlink); \
  230. return 0; \
  231. } \
  232. } while (0)
  233. #endif /* AVFILTER_FILTERS_H */