[FFmpeg-devel] [PATCH 08/17] lavfi: add helpers to consume frames from link FIFOs.

Nicolas George george at nsup.org
Thu Dec 29 16:33:54 EET 2016


Signed-off-by: Nicolas George <george at nsup.org>
---
 libavfilter/avfilter.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++
 libavfilter/filters.h  | 42 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 94 insertions(+)


Changes in this commit: rename ff_link -> ff_inlink and move to filters.h,
remove the _sure variants and document practice.


diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index d31c9f738b..2183a84395 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -1498,6 +1498,58 @@ int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus)
     return 1;
 }
 
+int ff_inlink_check_available_frame(AVFilterLink *link)
+{
+    return ff_framequeue_queued_frames(&link->fifo) > 0;
+}
+
+int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min)
+{
+    uint64_t samples = ff_framequeue_queued_samples(&link->fifo);
+    av_assert1(min);
+    return samples >= min || (link->status_in && samples);
+}
+
+static void consume_update(AVFilterLink *link, const AVFrame *frame)
+{
+    ff_inlink_process_commands(link, frame);
+    ff_inlink_process_timeline(link, frame);
+    link->frame_count_out++;
+}
+
+int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
+{
+    AVFrame *frame;
+
+    *rframe = NULL;
+    if (!ff_inlink_check_available_frame(link))
+        return 0;
+    frame = ff_framequeue_take(&link->fifo);
+    consume_update(link, frame);
+    *rframe = frame;
+    return 1;
+}
+
+int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max,
+                            AVFrame **rframe)
+{
+    AVFrame *frame;
+    int ret;
+
+    av_assert1(min);
+    *rframe = NULL;
+    if (!ff_inlink_check_available_samples(link, min))
+        return 0;
+    if (link->status_in)
+        min = FFMIN(min, ff_framequeue_queued_samples(&link->fifo));
+    ret = take_samples(link, min, link->max_samples, &frame);
+    if (ret < 0)
+        return ret;
+    consume_update(link, frame);
+    *rframe = frame;
+    return 1;
+}
+
 int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe)
 {
     AVFrame *frame = *rframe;
diff --git a/libavfilter/filters.h b/libavfilter/filters.h
index 69a29c640f..95df86c911 100644
--- a/libavfilter/filters.h
+++ b/libavfilter/filters.h
@@ -45,6 +45,48 @@ void ff_inlink_process_commands(AVFilterLink *link, const AVFrame *frame);
 void ff_inlink_process_timeline(AVFilterLink *link, const AVFrame *frame);
 
 /**
+ * Test if a frame is available on the link.
+ * @return  >0 if a frame is available
+ */
+int ff_inlink_check_available_frame(AVFilterLink *link);
+
+/**
+ * Test if enough samples are available on the link.
+ * @return  >0 if enough samples are available
+ * @note  on EOF and error, min becomes 1
+ */
+int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min);
+
+/**
+ * Take a frame from the link's FIFO and update the link's stats.
+ *
+ * If ff_inlink_check_available_frame() was previously called, the
+ * preferred way of expressing it is "av_assert1(ret);" immediately after
+ * ff_inlink_consume_frame(). Negative error codes must still be checked.
+ *
+ * @note  May trigger process_command() and/or update is_disabled.
+ * @return  >0 if a frame is available,
+ *          0 and set rframe to NULL if no frame available,
+ *          or AVERROR code
+ */
+int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe);
+
+/**
+ * Take samples from the link's FIFO and update the link's stats.
+ *
+ * If ff_inlink_check_available_samples() was previously called, the
+ * preferred way of expressing it is "av_assert1(ret);" immediately after
+ * ff_inlink_consume_samples(). Negative error codes must still be checked.
+ *
+ * @note  May trigger process_command() and/or update is_disabled.
+ * @return  >0 if a frame is available,
+ *          0 and set rframe to NULL if no frame available,
+ *          or AVERROR code
+ */
+int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max,
+                            AVFrame **rframe);
+
+/**
  * Make sure a frame is writable.
  * This is similar to av_frame_make_writable() except it uses the link's
  * buffer allocation callback, and therefore allows direct rendering.
-- 
2.11.0



More information about the ffmpeg-devel mailing list