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