| @@ -25,7 +25,7 @@ · 24 unmodified lines |
| 25 | 25 | |
| 26 | 26 | #include "libavutil/audio_fifo.h" |
| 27 | 27 | #include "libavutil/audioconvert.h" |
| 28 | | −#include "libavutil/fifo.h" |
| 28 | +#include "libavutil/avassert.h" |
| 29 | 29 | #include "libavutil/mathematics.h" |
| 30 | 30 | |
| 31 | 31 | #include "audio.h" |
| @@ -34,86 +34,45 @@ · 2 unmodified lines |
| 34 | 34 | #include "internal.h" |
| 35 | 35 | |
| 36 | 36 | typedef struct { |
| 37 | | − AVFifoBuffer *fifo; ///< FIFO buffer of frame references |
| 38 | | − |
| 37 | + AVFilterBufferRef *cur_buf; ///< last buffer delivered on the sink |
| 39 | 38 | AVAudioFifo *audio_fifo; ///< FIFO for audio samples |
| 40 | 39 | int64_t next_pts; ///< interpolating audio pts |
| 41 | 40 | } BufferSinkContext; |
| 42 | 41 | |
| 43 | | −#define FIFO_INIT_SIZE 8 |
| 44 | | − |
| 45 | 42 | static av_cold void uninit(AVFilterContext *ctx) |
| 46 | 43 | { |
| 47 | 44 | BufferSinkContext *sink = ctx->priv; |
| 48 | 45 | |
| 49 | | − while (sink->fifo && av_fifo_size(sink->fifo)) { |
| 50 | | − AVFilterBufferRef *buf; |
| 51 | | − av_fifo_generic_read(sink->fifo, &buf, sizeof(buf), NULL); |
| 52 | | − avfilter_unref_buffer(buf); |
| 53 | | − } |
| 54 | | − av_fifo_free(sink->fifo); |
| 55 | | − |
| 56 | 46 | if (sink->audio_fifo) |
| 57 | 47 | av_audio_fifo_free(sink->audio_fifo); |
| 58 | 48 | } |
| 59 | 49 | |
| 60 | | −static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) |
| 61 | | −{ |
| 62 | | − BufferSinkContext *sink = ctx->priv; |
| 63 | | − |
| 64 | | − if (!(sink->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef*)))) { |
| 65 | | − av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n"); |
| 66 | | − return AVERROR(ENOMEM); |
| 67 | | − } |
| 68 | | − |
| 69 | | − return 0; |
| 70 | | −} |
| 71 | | − |
| 72 | | −static void write_buf(AVFilterContext *ctx, AVFilterBufferRef *buf) |
| 50 | +static void start_frame(AVFilterLink *link, AVFilterBufferRef *buf) |
| 73 | 51 | { |
| 74 | | − BufferSinkContext *sink = ctx->priv; |
| 75 | | − |
| 76 | | − if (av_fifo_space(sink->fifo) < sizeof(AVFilterBufferRef *) && |
| 77 | | − (av_fifo_realloc2(sink->fifo, av_fifo_size(sink->fifo) * 2) < 0)) { |
| 78 | | − av_log(ctx, AV_LOG_ERROR, "Error reallocating the FIFO.\n"); |
| 79 | | − return; |
| 80 | | − } |
| 52 | + BufferSinkContext *s = link->dst->priv; |
| 81 | 53 | |
| 82 | | − av_fifo_generic_write(sink->fifo, &buf, sizeof(buf), NULL); |
| 83 | | −} |
| 84 | | − |
| 85 | | −static void end_frame(AVFilterLink *link) |
| 86 | | −{ |
| 87 | | − write_buf(link->dst, link->cur_buf); |
| 54 | + av_assert0(!s->cur_buf); |
| 55 | + s->cur_buf = buf; |
| 88 | 56 | link->cur_buf = NULL; |
| 89 | | −} |
| 90 | | − |
| 91 | | −static void filter_samples(AVFilterLink *link, AVFilterBufferRef *buf) |
| 92 | | −{ |
| 93 | | − write_buf(link->dst, buf); |
| 94 | | −} |
| 57 | +}; |
| 95 | 58 | |
| 96 | 59 | int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf) |
| 97 | 60 | { |
| 98 | | − BufferSinkContext *sink = ctx->priv; |
| 61 | + BufferSinkContext *s = ctx->priv; |
| 99 | 62 | AVFilterLink *link = ctx->inputs[0]; |
| 100 | 63 | int ret; |
| 101 | 64 | |
| 102 | | − if (!buf) { |
| 103 | | − if (av_fifo_size(sink->fifo)) |
| 104 | | − return av_fifo_size(sink->fifo)/sizeof(*buf); |
| 105 | | − else |
| 106 | | − return ff_poll_frame(ctx->inputs[0]); |
| 107 | | − } |
| 65 | + if (!buf) |
| 66 | + return ff_poll_frame(ctx->inputs[0]); |
| 108 | 67 | |
| 109 | | − if (!av_fifo_size(sink->fifo) && |
| 110 | | − (ret = ff_request_frame(link)) < 0) |
| 68 | + if ((ret = ff_request_frame(link)) < 0) |
| 111 | 69 | return ret; |
| 112 | 70 | |
| 113 | | − if (!av_fifo_size(sink->fifo)) |
| 71 | + if (!s->cur_buf) |
| 114 | 72 | return AVERROR(EINVAL); |
| 115 | 73 | |
| 116 | | − av_fifo_generic_read(sink->fifo, buf, sizeof(*buf), NULL); |
| 74 | + *buf = s->cur_buf; |
| 75 | + s->cur_buf = NULL; |
| 117 | 76 | |
| 118 | 77 | return 0; |
| 119 | 78 | } |
| @@ -182,13 +141,13 @@ AVFilter avfilter_vsink_buffer = { · 62 unmodified lines |
| 182 | 141 | .name = "buffersink", |
| 183 | 142 | .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."), |
| 184 | 143 | .priv_size = sizeof(BufferSinkContext), |
| 185 | | − .init = init, |
| 186 | 144 | .uninit = uninit, |
| 187 | 145 | |
| 188 | 146 | .inputs = (AVFilterPad[]) {{ .name = "default", |
| 189 | 147 | .type = AVMEDIA_TYPE_VIDEO, |
| 190 | | − .end_frame = end_frame, |
| 191 | | − .min_perms = AV_PERM_READ, }, |
| 148 | + .start_frame = start_frame, |
| 149 | + .min_perms = AV_PERM_READ, |
| 150 | + .needs_fifo = 1 }, |
| 192 | 151 | { .name = NULL }}, |
| 193 | 152 | .outputs = (AVFilterPad[]) {{ .name = NULL }}, |
| 194 | 153 | }; |
| @@ -197,13 +156,13 @@ AVFilter avfilter_asink_abuffer = { · 2 unmodified lines |
| 197 | 156 | .name = "abuffersink", |
| 198 | 157 | .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."), |
| 199 | 158 | .priv_size = sizeof(BufferSinkContext), |
| 200 | | − .init = init, |
| 201 | 159 | .uninit = uninit, |
| 202 | 160 | |
| 203 | 161 | .inputs = (AVFilterPad[]) {{ .name = "default", |
| 204 | 162 | .type = AVMEDIA_TYPE_AUDIO, |
| 205 | | − .filter_samples = filter_samples, |
| 206 | | − .min_perms = AV_PERM_READ, }, |
| 163 | + .filter_samples = start_frame, |
| 164 | + .min_perms = AV_PERM_READ, |
| 165 | + .needs_fifo = 1 }, |
| 207 | 166 | { .name = NULL }}, |
| 208 | 167 | .outputs = (AVFilterPad[]) {{ .name = NULL }}, |
| 209 | 168 | }; |