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.

255 lines
8.5KB

  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. * Test if enough samples are available on the link.
  67. * @return >0 if enough samples are available
  68. * @note on EOF and error, min becomes 1
  69. */
  70. int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min);
  71. /**
  72. * Take a frame from the link's FIFO and update the link's stats.
  73. *
  74. * If ff_inlink_check_available_frame() was previously called, the
  75. * preferred way of expressing it is "av_assert1(ret);" immediately after
  76. * ff_inlink_consume_frame(). Negative error codes must still be checked.
  77. *
  78. * @note May trigger process_command() and/or update is_disabled.
  79. * @return >0 if a frame is available,
  80. * 0 and set rframe to NULL if no frame available,
  81. * or AVERROR code
  82. */
  83. int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe);
  84. /**
  85. * Take samples from the link's FIFO and update the link's stats.
  86. *
  87. * If ff_inlink_check_available_samples() was previously called, the
  88. * preferred way of expressing it is "av_assert1(ret);" immediately after
  89. * ff_inlink_consume_samples(). Negative error codes must still be checked.
  90. *
  91. * @note May trigger process_command() and/or update is_disabled.
  92. * @return >0 if a frame is available,
  93. * 0 and set rframe to NULL if no frame available,
  94. * or AVERROR code
  95. */
  96. int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max,
  97. AVFrame **rframe);
  98. /**
  99. * Access a frame in the link fifo without consuming it.
  100. * The first frame is numbered 0; the designated frame must exist.
  101. * @return the frame at idx position in the link fifo.
  102. */
  103. AVFrame *ff_inlink_peek_frame(AVFilterLink *link, size_t idx);
  104. /**
  105. * Make sure a frame is writable.
  106. * This is similar to av_frame_make_writable() except it uses the link's
  107. * buffer allocation callback, and therefore allows direct rendering.
  108. */
  109. int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe);
  110. /**
  111. * Test and acknowledge the change of status on the link.
  112. *
  113. * Status means EOF or an error condition; a change from the normal (0)
  114. * status to a non-zero status can be queued in a filter's input link, it
  115. * becomes relevant after the frames queued in the link's FIFO are
  116. * processed. This function tests if frames are still queued and if a queued
  117. * status change has not yet been processed. In that case it performs basic
  118. * treatment (updating the link's timestamp) and returns a positive value to
  119. * let the filter do its own treatments (flushing...).
  120. *
  121. * Filters implementing the activate callback should call this function when
  122. * they think it might succeed (usually after checking unsuccessfully for a
  123. * queued frame).
  124. * Filters implementing the filter_frame and request_frame callbacks do not
  125. * need to call that since the same treatment happens in ff_filter_frame().
  126. *
  127. * @param[out] rstatus new or current status
  128. * @param[out] rpts current timestamp of the link in link time base
  129. * @return >0 if status changed, <0 if status already acked, 0 otherwise
  130. */
  131. int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts);
  132. /**
  133. * Mark that a frame is wanted on the link.
  134. * Unlike ff_filter_frame(), it must not be called when the link has a
  135. * non-zero status, and thus does not acknowledge it.
  136. * Also it cannot fail.
  137. */
  138. void ff_inlink_request_frame(AVFilterLink *link);
  139. /**
  140. * Set the status on an input link.
  141. * Also discard all frames in the link's FIFO.
  142. */
  143. void ff_inlink_set_status(AVFilterLink *link, int status);
  144. /**
  145. * Test if a frame is wanted on an output link.
  146. */
  147. static inline int ff_outlink_frame_wanted(AVFilterLink *link)
  148. {
  149. return link->frame_wanted_out;
  150. }
  151. /**
  152. * Get the status on an output link.
  153. */
  154. int ff_outlink_get_status(AVFilterLink *link);
  155. /**
  156. * Set the status field of a link from the source filter.
  157. * The pts should reflect the timestamp of the status change,
  158. * in link time base and relative to the frames timeline.
  159. * In particular, for AVERROR_EOF, it should reflect the
  160. * end time of the last frame.
  161. */
  162. static inline void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
  163. {
  164. ff_avfilter_link_set_in_status(link, status, pts);
  165. }
  166. /**
  167. * Forward the status on an output link to an input link.
  168. * If the status is set, it will discard all queued frames and this macro
  169. * will return immediately.
  170. */
  171. #define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink) do { \
  172. int ret = ff_outlink_get_status(outlink); \
  173. if (ret) { \
  174. ff_inlink_set_status(inlink, ret); \
  175. return 0; \
  176. } \
  177. } while (0)
  178. /**
  179. * Forward the status on an output link to all input links.
  180. * If the status is set, it will discard all queued frames and this macro
  181. * will return immediately.
  182. */
  183. #define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter) do { \
  184. int ret = ff_outlink_get_status(outlink); \
  185. if (ret) { \
  186. unsigned i; \
  187. for (i = 0; i < filter->nb_inputs; i++) \
  188. ff_inlink_set_status(filter->inputs[i], ret); \
  189. return 0; \
  190. } \
  191. } while (0)
  192. /**
  193. * Acknowledge the status on an input link and forward it to an output link.
  194. * If the status is set, this macro will return immediately.
  195. */
  196. #define FF_FILTER_FORWARD_STATUS(inlink, outlink) do { \
  197. int status; \
  198. int64_t pts; \
  199. if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { \
  200. ff_outlink_set_status(outlink, status, pts); \
  201. return 0; \
  202. } \
  203. } while (0)
  204. /**
  205. * Acknowledge the status on an input link and forward it to an output link.
  206. * If the status is set, this macro will return immediately.
  207. */
  208. #define FF_FILTER_FORWARD_STATUS_ALL(inlink, filter) do { \
  209. int status; \
  210. int64_t pts; \
  211. if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { \
  212. unsigned i; \
  213. for (i = 0; i < filter->nb_outputs; i++) \
  214. ff_outlink_set_status(filter->outputs[i], status, pts); \
  215. return 0; \
  216. } \
  217. } while (0)
  218. /**
  219. * Forward the frame_wanted_out flag from an output link to an input link.
  220. * If the flag is set, this macro will return immediately.
  221. */
  222. #define FF_FILTER_FORWARD_WANTED(outlink, inlink) do { \
  223. if (ff_outlink_frame_wanted(outlink)) { \
  224. ff_inlink_request_frame(inlink); \
  225. return 0; \
  226. } \
  227. } while (0)
  228. #endif /* AVFILTER_FILTERS_H */