00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "libavutil/fifo.h"
00027 #include "avfilter.h"
00028 #include "buffersink.h"
00029
00030 AVBufferSinkParams *av_buffersink_params_alloc(void)
00031 {
00032 static const int pixel_fmts[] = { -1 };
00033 AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
00034 if (!params)
00035 return NULL;
00036
00037 params->pixel_fmts = pixel_fmts;
00038 return params;
00039 }
00040
00041 AVABufferSinkParams *av_abuffersink_params_alloc(void)
00042 {
00043 static const int sample_fmts[] = { -1 };
00044 static const int packing_fmts[] = { -1 };
00045 static const int64_t channel_layouts[] = { -1 };
00046 AVABufferSinkParams *params = av_malloc(sizeof(AVABufferSinkParams));
00047
00048 if (!params)
00049 return NULL;
00050
00051 params->sample_fmts = sample_fmts;
00052 params->channel_layouts = channel_layouts;
00053 params->packing_fmts = packing_fmts;
00054 return params;
00055 }
00056
00057 typedef struct {
00058 AVFifoBuffer *fifo;
00059
00060
00061 const enum PixelFormat *pixel_fmts;
00062
00063
00064 const enum AVSampleFormat *sample_fmts;
00065 const int64_t *channel_layouts;
00066 const int *packing_fmts;
00067 } BufferSinkContext;
00068
00069 #define FIFO_INIT_SIZE 8
00070
00071 static av_cold int common_init(AVFilterContext *ctx)
00072 {
00073 BufferSinkContext *buf = ctx->priv;
00074
00075 buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
00076 if (!buf->fifo) {
00077 av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
00078 return AVERROR(ENOMEM);
00079 }
00080 return 0;
00081 }
00082
00083 static av_cold void common_uninit(AVFilterContext *ctx)
00084 {
00085 BufferSinkContext *buf = ctx->priv;
00086 AVFilterBufferRef *picref;
00087
00088 if (buf->fifo) {
00089 while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
00090 av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
00091 avfilter_unref_buffer(picref);
00092 }
00093 av_fifo_free(buf->fifo);
00094 buf->fifo = NULL;
00095 }
00096 }
00097
00098 static void end_frame(AVFilterLink *inlink)
00099 {
00100 AVFilterContext *ctx = inlink->dst;
00101 BufferSinkContext *buf = inlink->dst->priv;
00102
00103 if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
00104
00105 if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
00106 av_log(ctx, AV_LOG_ERROR,
00107 "Cannot buffer more frames. Consume some available frames "
00108 "before adding new ones.\n");
00109 return;
00110 }
00111 }
00112
00113
00114 av_fifo_generic_write(buf->fifo,
00115 &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
00116 }
00117
00118 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
00119 AVFilterBufferRef **bufref, int flags)
00120 {
00121 BufferSinkContext *buf = ctx->priv;
00122 AVFilterLink *inlink = ctx->inputs[0];
00123 int ret;
00124 *bufref = NULL;
00125
00126
00127 if (!av_fifo_size(buf->fifo)) {
00128 if ((ret = avfilter_request_frame(inlink)) < 0)
00129 return ret;
00130 }
00131
00132 if (!av_fifo_size(buf->fifo))
00133 return AVERROR(EINVAL);
00134
00135 if (flags & AV_BUFFERSINK_FLAG_PEEK)
00136 *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
00137 else
00138 av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
00139
00140 return 0;
00141 }
00142
00143 int av_buffersink_poll_frame(AVFilterContext *ctx)
00144 {
00145 BufferSinkContext *buf = ctx->priv;
00146 AVFilterLink *inlink = ctx->inputs[0];
00147
00148 return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + avfilter_poll_frame(inlink);
00149 }
00150
00151 #if FF_API_OLD_VSINK_API
00152 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
00153 AVFilterBufferRef **picref, int flags)
00154 {
00155 return av_buffersink_get_buffer_ref(ctx, picref, flags);
00156 }
00157 #endif
00158
00159 #if CONFIG_BUFFERSINK_FILTER
00160
00161 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
00162 {
00163 BufferSinkContext *buf = ctx->priv;
00164 av_unused AVBufferSinkParams *params;
00165
00166 if (!opaque) {
00167 av_log(ctx, AV_LOG_ERROR,
00168 "No opaque field provided\n");
00169 return AVERROR(EINVAL);
00170 } else {
00171 #if FF_API_OLD_VSINK_API
00172 buf->pixel_fmts = (const enum PixelFormat *)opaque;
00173 #else
00174 params = (AVBufferSinkParams *)opaque;
00175 buf->pixel_fmts = params->pixel_fmts;
00176 #endif
00177 }
00178
00179 return common_init(ctx);
00180 }
00181
00182 static int vsink_query_formats(AVFilterContext *ctx)
00183 {
00184 BufferSinkContext *buf = ctx->priv;
00185
00186 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
00187 return 0;
00188 }
00189
00190 AVFilter avfilter_vsink_buffersink = {
00191 .name = "buffersink",
00192 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
00193 .priv_size = sizeof(BufferSinkContext),
00194 .init = vsink_init,
00195 .uninit = common_uninit,
00196
00197 .query_formats = vsink_query_formats,
00198
00199 .inputs = (const AVFilterPad[]) {{ .name = "default",
00200 .type = AVMEDIA_TYPE_VIDEO,
00201 .end_frame = end_frame,
00202 .min_perms = AV_PERM_READ, },
00203 { .name = NULL }},
00204 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
00205 };
00206
00207 #endif
00208
00209 #if CONFIG_ABUFFERSINK_FILTER
00210
00211 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
00212 {
00213 end_frame(link);
00214 }
00215
00216 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
00217 {
00218 BufferSinkContext *buf = ctx->priv;
00219 AVABufferSinkParams *params;
00220
00221 if (!opaque) {
00222 av_log(ctx, AV_LOG_ERROR,
00223 "No opaque field provided, an AVABufferSinkParams struct is required\n");
00224 return AVERROR(EINVAL);
00225 } else
00226 params = (AVABufferSinkParams *)opaque;
00227
00228 buf->sample_fmts = params->sample_fmts;
00229 buf->channel_layouts = params->channel_layouts;
00230 buf->packing_fmts = params->packing_fmts;
00231
00232 return common_init(ctx);
00233 }
00234
00235 static int asink_query_formats(AVFilterContext *ctx)
00236 {
00237 BufferSinkContext *buf = ctx->priv;
00238 AVFilterFormats *formats = NULL;
00239
00240 if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
00241 return AVERROR(ENOMEM);
00242 avfilter_set_common_sample_formats(ctx, formats);
00243
00244 if (!(formats = avfilter_make_format64_list(buf->channel_layouts)))
00245 return AVERROR(ENOMEM);
00246 avfilter_set_common_channel_layouts(ctx, formats);
00247
00248 if (!(formats = avfilter_make_format_list(buf->packing_fmts)))
00249 return AVERROR(ENOMEM);
00250 avfilter_set_common_packing_formats(ctx, formats);
00251
00252 return 0;
00253 }
00254
00255 AVFilter avfilter_asink_abuffersink = {
00256 .name = "abuffersink",
00257 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
00258 .init = asink_init,
00259 .uninit = common_uninit,
00260 .priv_size = sizeof(BufferSinkContext),
00261 .query_formats = asink_query_formats,
00262
00263 .inputs = (const AVFilterPad[]) {{ .name = "default",
00264 .type = AVMEDIA_TYPE_AUDIO,
00265 .filter_samples = filter_samples,
00266 .min_perms = AV_PERM_READ, },
00267 { .name = NULL }},
00268 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
00269 };
00270
00271 #endif