]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/buffersink.c
build: Generate pkg-config files from Make and not from configure
[ffmpeg] / libavfilter / buffersink.c
index e4cbe3be427f5bb2848eadca2def93a33684c5b5..3b4d285ffd6e90ef75a17e0a5200b694ec8e99d6 100644 (file)
  * buffer sink
  */
 
-#include "libavutil/fifo.h"
-
+#include "libavutil/audio_fifo.h"
+#include "libavutil/avassert.h"
+#include "libavutil/channel_layout.h"
+#include "libavutil/common.h"
+#include "libavutil/internal.h"
+#include "libavutil/mathematics.h"
+
+#include "audio.h"
 #include "avfilter.h"
 #include "buffersink.h"
+#include "internal.h"
 
-typedef struct {
-    AVFifoBuffer *fifo;          ///< FIFO buffer of video frame references
+typedef struct BufferSinkContext {
+    AVFrame *cur_frame;          ///< last frame delivered on the sink
+    AVAudioFifo *audio_fifo;     ///< FIFO for audio samples
+    int64_t next_pts;            ///< interpolating audio pts
 } BufferSinkContext;
 
-#define FIFO_INIT_SIZE 8
-
 static av_cold void uninit(AVFilterContext *ctx)
 {
     BufferSinkContext *sink = ctx->priv;
 
-    while (sink->fifo && av_fifo_size(sink->fifo)) {
-        AVFilterBufferRef *buf;
-        av_fifo_generic_read(sink->fifo, &buf, sizeof(buf), NULL);
-        avfilter_unref_buffer(buf);
-    }
-    av_fifo_free(sink->fifo);
+    if (sink->audio_fifo)
+        av_audio_fifo_free(sink->audio_fifo);
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+static int filter_frame(AVFilterLink *link, AVFrame *frame)
 {
-    BufferSinkContext *sink = ctx->priv;
+    BufferSinkContext *s = link->dst->priv;
 
-    if (!(sink->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef*)))) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
-        return AVERROR(ENOMEM);
-    }
+    av_assert0(!s->cur_frame);
+    s->cur_frame = frame;
 
     return 0;
 }
 
-static void end_frame(AVFilterLink *link)
+int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx,
+                                                AVFrame *frame)
 {
-    AVFilterContext   *ctx = link->dst;
-    BufferSinkContext *sink = ctx->priv;
+    BufferSinkContext *s    = ctx->priv;
+    AVFilterLink      *link = ctx->inputs[0];
+    int ret;
 
-    if (av_fifo_space(sink->fifo) < sizeof(AVFilterBufferRef *) &&
-        (av_fifo_realloc2(sink->fifo, av_fifo_size(sink->fifo) * 2) < 0)) {
-            av_log(ctx, AV_LOG_ERROR, "Error reallocating the FIFO.\n");
-            return;
-    }
+    if ((ret = ff_request_frame(link)) < 0)
+        return ret;
+
+    if (!s->cur_frame)
+        return AVERROR(EINVAL);
 
-    av_fifo_generic_write(sink->fifo, &link->cur_buf, sizeof(link->cur_buf), NULL);
-    link->cur_buf = NULL;
+    av_frame_move_ref(frame, s->cur_frame);
+    av_frame_free(&s->cur_frame);
+
+    return 0;
 }
 
-int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
+static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame,
+                          int nb_samples)
 {
-    BufferSinkContext *sink = ctx->priv;
-    AVFilterLink      *link = ctx->inputs[0];
-    int ret;
-
-    if (!buf) {
-        if (av_fifo_size(sink->fifo))
-            return av_fifo_size(sink->fifo)/sizeof(*buf);
-        else
-            return avfilter_poll_frame(ctx->inputs[0]);
-    }
+    BufferSinkContext *s = ctx->priv;
+    AVFilterLink   *link = ctx->inputs[0];
+    AVFrame *tmp;
 
-    if (!av_fifo_size(sink->fifo) &&
-        (ret = avfilter_request_frame(link)) < 0)
-        return ret;
+    if (!(tmp = ff_get_audio_buffer(link, nb_samples)))
+        return AVERROR(ENOMEM);
+    av_audio_fifo_read(s->audio_fifo, (void**)tmp->extended_data, nb_samples);
 
-    if (!av_fifo_size(sink->fifo))
-        return AVERROR(EINVAL);
+    tmp->pts = s->next_pts;
+    s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
+                                link->time_base);
 
-    av_fifo_generic_read(sink->fifo, buf, sizeof(*buf), NULL);
+    av_frame_move_ref(frame, tmp);
+    av_frame_free(&tmp);
 
     return 0;
 }
 
-AVFilter avfilter_vsink_buffer = {
-    .name      = "buffersink",
+int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx,
+                                                  AVFrame *frame, int nb_samples)
+{
+    BufferSinkContext *s = ctx->priv;
+    AVFilterLink   *link = ctx->inputs[0];
+    int ret = 0;
+
+    if (!s->audio_fifo) {
+        int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
+        if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
+            return AVERROR(ENOMEM);
+    }
+
+    while (ret >= 0) {
+        if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
+            return read_from_fifo(ctx, frame, nb_samples);
+
+        ret = ff_request_frame(link);
+        if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo))
+            return read_from_fifo(ctx, frame, av_audio_fifo_size(s->audio_fifo));
+        else if (ret < 0)
+            return ret;
+
+        if (s->cur_frame->pts != AV_NOPTS_VALUE) {
+            s->next_pts = s->cur_frame->pts -
+                          av_rescale_q(av_audio_fifo_size(s->audio_fifo),
+                                       (AVRational){ 1, link->sample_rate },
+                                       link->time_base);
+        }
+
+        ret = av_audio_fifo_write(s->audio_fifo, (void**)s->cur_frame->extended_data,
+                                  s->cur_frame->nb_samples);
+        av_frame_free(&s->cur_frame);
+    }
+
+    return ret;
+}
+
+static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+        .needs_fifo   = 1
+    },
+    { NULL }
+};
+
+AVFilter ff_vsink_buffer = {
+    .name        = "buffersink",
     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
-    .priv_size = sizeof(BufferSinkContext),
-    .init      = init,
-    .uninit    = uninit,
-
-    .inputs    = (AVFilterPad[]) {{ .name          = "default",
-                                    .type          = AVMEDIA_TYPE_VIDEO,
-                                    .end_frame     = end_frame,
-                                    .min_perms     = AV_PERM_READ, },
-                                  { .name = NULL }},
-    .outputs   = (AVFilterPad[]) {{ .name = NULL }},
+    .priv_size   = sizeof(BufferSinkContext),
+    .uninit      = uninit,
+
+    .inputs      = avfilter_vsink_buffer_inputs,
+    .outputs     = NULL,
+};
+
+static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_AUDIO,
+        .filter_frame = filter_frame,
+        .needs_fifo   = 1
+    },
+    { NULL }
+};
+
+AVFilter ff_asink_abuffer = {
+    .name        = "abuffersink",
+    .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
+    .priv_size   = sizeof(BufferSinkContext),
+    .uninit      = uninit,
+
+    .inputs      = avfilter_asink_abuffer_inputs,
+    .outputs     = NULL,
 };