00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "libavutil/avassert.h"
00027 #include "libavutil/channel_layout.h"
00028 #include "libavutil/fifo.h"
00029 #include "avfilter.h"
00030 #include "buffersink.h"
00031 #include "audio.h"
00032 #include "internal.h"
00033
00034 AVBufferSinkParams *av_buffersink_params_alloc(void)
00035 {
00036 static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
00037 AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
00038 if (!params)
00039 return NULL;
00040
00041 params->pixel_fmts = pixel_fmts;
00042 return params;
00043 }
00044
00045 AVABufferSinkParams *av_abuffersink_params_alloc(void)
00046 {
00047 static const int sample_fmts[] = { AV_SAMPLE_FMT_NONE };
00048 static const int64_t channel_layouts[] = { -1 };
00049 AVABufferSinkParams *params = av_malloc(sizeof(AVABufferSinkParams));
00050
00051 if (!params)
00052 return NULL;
00053
00054 params->sample_fmts = sample_fmts;
00055 params->channel_layouts = channel_layouts;
00056 return params;
00057 }
00058
00059 typedef struct {
00060 AVFifoBuffer *fifo;
00061 unsigned warning_limit;
00062
00063
00064 enum AVPixelFormat *pixel_fmts;
00065
00066
00067 enum AVSampleFormat *sample_fmts;
00068 int64_t *channel_layouts;
00069 } BufferSinkContext;
00070
00071 #define FIFO_INIT_SIZE 8
00072
00073 static av_cold int common_init(AVFilterContext *ctx)
00074 {
00075 BufferSinkContext *buf = ctx->priv;
00076
00077 buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
00078 if (!buf->fifo) {
00079 av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
00080 return AVERROR(ENOMEM);
00081 }
00082 buf->warning_limit = 100;
00083 return 0;
00084 }
00085
00086 static av_cold void common_uninit(AVFilterContext *ctx)
00087 {
00088 BufferSinkContext *buf = ctx->priv;
00089 AVFilterBufferRef *picref;
00090
00091 if (buf->fifo) {
00092 while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
00093 av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
00094 avfilter_unref_buffer(picref);
00095 }
00096 av_fifo_free(buf->fifo);
00097 buf->fifo = NULL;
00098 }
00099 }
00100
00101 static int add_buffer_ref(AVFilterContext *ctx, AVFilterBufferRef *ref)
00102 {
00103 BufferSinkContext *buf = ctx->priv;
00104
00105 if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
00106
00107 if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
00108 av_log(ctx, AV_LOG_ERROR,
00109 "Cannot buffer more frames. Consume some available frames "
00110 "before adding new ones.\n");
00111 return AVERROR(ENOMEM);
00112 }
00113 }
00114
00115
00116 av_fifo_generic_write(buf->fifo, &ref, sizeof(AVFilterBufferRef *), NULL);
00117 return 0;
00118 }
00119
00120 static int end_frame(AVFilterLink *inlink)
00121 {
00122 AVFilterContext *ctx = inlink->dst;
00123 BufferSinkContext *buf = inlink->dst->priv;
00124 int ret;
00125
00126 av_assert1(inlink->cur_buf);
00127 if ((ret = add_buffer_ref(ctx, inlink->cur_buf)) < 0)
00128 return ret;
00129 inlink->cur_buf = NULL;
00130 if (buf->warning_limit &&
00131 av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
00132 av_log(ctx, AV_LOG_WARNING,
00133 "%d buffers queued in %s, something may be wrong.\n",
00134 buf->warning_limit,
00135 (char *)av_x_if_null(ctx->name, ctx->filter->name));
00136 buf->warning_limit *= 10;
00137 }
00138 return 0;
00139 }
00140
00141 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
00142 {
00143 AVFilterLink *inlink = ctx->inputs[0];
00144
00145 inlink->min_samples = inlink->max_samples =
00146 inlink->partial_buf_size = frame_size;
00147 }
00148
00149 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
00150 AVFilterBufferRef **bufref, int flags)
00151 {
00152 BufferSinkContext *buf = ctx->priv;
00153 AVFilterLink *inlink = ctx->inputs[0];
00154 int ret;
00155 *bufref = NULL;
00156
00157 av_assert0( !strcmp(ctx->filter->name, "buffersink")
00158 || !strcmp(ctx->filter->name, "abuffersink")
00159 || !strcmp(ctx->filter->name, "ffbuffersink")
00160 || !strcmp(ctx->filter->name, "ffabuffersink"));
00161
00162
00163 if (!av_fifo_size(buf->fifo)) {
00164 if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
00165 return AVERROR(EAGAIN);
00166 if ((ret = ff_request_frame(inlink)) < 0)
00167 return ret;
00168 }
00169
00170 if (!av_fifo_size(buf->fifo))
00171 return AVERROR(EINVAL);
00172
00173 if (flags & AV_BUFFERSINK_FLAG_PEEK)
00174 *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
00175 else
00176 av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
00177
00178 return 0;
00179 }
00180
00181 AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
00182 {
00183 av_assert0( !strcmp(ctx->filter->name, "buffersink")
00184 || !strcmp(ctx->filter->name, "ffbuffersink"));
00185
00186 return ctx->inputs[0]->frame_rate;
00187 }
00188
00189 int av_buffersink_poll_frame(AVFilterContext *ctx)
00190 {
00191 BufferSinkContext *buf = ctx->priv;
00192 AVFilterLink *inlink = ctx->inputs[0];
00193
00194 av_assert0( !strcmp(ctx->filter->name, "buffersink")
00195 || !strcmp(ctx->filter->name, "abuffersink")
00196 || !strcmp(ctx->filter->name, "ffbuffersink")
00197 || !strcmp(ctx->filter->name, "ffabuffersink"));
00198
00199 return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
00200 }
00201
00202 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
00203 {
00204 BufferSinkContext *buf = ctx->priv;
00205 AVBufferSinkParams *params = opaque;
00206
00207 if (params && params->pixel_fmts) {
00208 const int *pixel_fmts = params->pixel_fmts;
00209
00210 buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
00211 if (!buf->pixel_fmts)
00212 return AVERROR(ENOMEM);
00213 }
00214
00215 return common_init(ctx);
00216 }
00217
00218 static av_cold void vsink_uninit(AVFilterContext *ctx)
00219 {
00220 BufferSinkContext *buf = ctx->priv;
00221 av_freep(&buf->pixel_fmts);
00222 common_uninit(ctx);
00223 }
00224
00225 static int vsink_query_formats(AVFilterContext *ctx)
00226 {
00227 BufferSinkContext *buf = ctx->priv;
00228
00229 if (buf->pixel_fmts)
00230 ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
00231 else
00232 ff_default_query_formats(ctx);
00233
00234 return 0;
00235 }
00236
00237 static const AVFilterPad ffbuffersink_inputs[] = {
00238 {
00239 .name = "default",
00240 .type = AVMEDIA_TYPE_VIDEO,
00241 .end_frame = end_frame,
00242 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
00243 },
00244 { NULL },
00245 };
00246
00247 AVFilter avfilter_vsink_ffbuffersink = {
00248 .name = "ffbuffersink",
00249 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
00250 .priv_size = sizeof(BufferSinkContext),
00251 .init_opaque = vsink_init,
00252 .uninit = vsink_uninit,
00253
00254 .query_formats = vsink_query_formats,
00255 .inputs = ffbuffersink_inputs,
00256 .outputs = NULL,
00257 };
00258
00259 static const AVFilterPad buffersink_inputs[] = {
00260 {
00261 .name = "default",
00262 .type = AVMEDIA_TYPE_VIDEO,
00263 .end_frame = end_frame,
00264 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
00265 },
00266 { NULL },
00267 };
00268
00269 AVFilter avfilter_vsink_buffersink = {
00270 .name = "buffersink",
00271 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
00272 .priv_size = sizeof(BufferSinkContext),
00273 .init_opaque = vsink_init,
00274 .uninit = vsink_uninit,
00275
00276 .query_formats = vsink_query_formats,
00277 .inputs = buffersink_inputs,
00278 .outputs = NULL,
00279 };
00280
00281 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *samplesref)
00282 {
00283 end_frame(link);
00284 return 0;
00285 }
00286
00287 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
00288 {
00289 BufferSinkContext *buf = ctx->priv;
00290 AVABufferSinkParams *params = opaque;
00291
00292 if (params && params->sample_fmts) {
00293 buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
00294 if (!buf->sample_fmts)
00295 goto fail_enomem;
00296 }
00297 if (params && params->channel_layouts) {
00298 buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
00299 if (!buf->channel_layouts)
00300 goto fail_enomem;
00301 }
00302 if (!common_init(ctx))
00303 return 0;
00304
00305 fail_enomem:
00306 av_freep(&buf->sample_fmts);
00307 av_freep(&buf->channel_layouts);
00308 return AVERROR(ENOMEM);
00309 }
00310
00311 static av_cold void asink_uninit(AVFilterContext *ctx)
00312 {
00313 BufferSinkContext *buf = ctx->priv;
00314
00315 av_freep(&buf->sample_fmts);
00316 av_freep(&buf->channel_layouts);
00317 common_uninit(ctx);
00318 }
00319
00320 static int asink_query_formats(AVFilterContext *ctx)
00321 {
00322 BufferSinkContext *buf = ctx->priv;
00323 AVFilterFormats *formats = NULL;
00324 AVFilterChannelLayouts *layouts = NULL;
00325
00326 if (buf->sample_fmts) {
00327 if (!(formats = ff_make_format_list(buf->sample_fmts)))
00328 return AVERROR(ENOMEM);
00329 ff_set_common_formats(ctx, formats);
00330 }
00331
00332 if (buf->channel_layouts) {
00333 if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
00334 return AVERROR(ENOMEM);
00335 ff_set_common_channel_layouts(ctx, layouts);
00336 }
00337
00338 return 0;
00339 }
00340
00341 static const AVFilterPad ffabuffersink_inputs[] = {
00342 {
00343 .name = "default",
00344 .type = AVMEDIA_TYPE_AUDIO,
00345 .filter_frame = filter_frame,
00346 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
00347 },
00348 { NULL },
00349 };
00350
00351 AVFilter avfilter_asink_ffabuffersink = {
00352 .name = "ffabuffersink",
00353 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
00354 .init_opaque = asink_init,
00355 .uninit = asink_uninit,
00356 .priv_size = sizeof(BufferSinkContext),
00357 .query_formats = asink_query_formats,
00358 .inputs = ffabuffersink_inputs,
00359 .outputs = NULL,
00360 };
00361
00362 static const AVFilterPad abuffersink_inputs[] = {
00363 {
00364 .name = "default",
00365 .type = AVMEDIA_TYPE_AUDIO,
00366 .filter_frame = filter_frame,
00367 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
00368 },
00369 { NULL },
00370 };
00371
00372 AVFilter avfilter_asink_abuffersink = {
00373 .name = "abuffersink",
00374 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
00375 .init_opaque = asink_init,
00376 .uninit = asink_uninit,
00377 .priv_size = sizeof(BufferSinkContext),
00378 .query_formats = asink_query_formats,
00379 .inputs = abuffersink_inputs,
00380 .outputs = NULL,
00381 };
00382
00383
00384
00385 extern AVFilter avfilter_vsink_buffer;
00386 extern AVFilter avfilter_asink_abuffer;
00387
00388 int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
00389 {
00390 AVFilterBufferRef *tbuf;
00391 int ret;
00392
00393 if (ctx->filter-> inputs[0].start_frame ==
00394 avfilter_vsink_buffer. inputs[0].start_frame ||
00395 ctx->filter-> inputs[0].filter_frame ==
00396 avfilter_asink_abuffer.inputs[0].filter_frame)
00397 return ff_buffersink_read_compat(ctx, buf);
00398 av_assert0(ctx->filter-> inputs[0].end_frame ==
00399 avfilter_vsink_ffbuffersink. inputs[0].end_frame ||
00400 ctx->filter-> inputs[0].filter_frame ==
00401 avfilter_asink_ffabuffersink.inputs[0].filter_frame);
00402
00403 ret = av_buffersink_get_buffer_ref(ctx, &tbuf,
00404 buf ? 0 : AV_BUFFERSINK_FLAG_PEEK);
00405 if (!buf)
00406 return ret >= 0;
00407 if (ret < 0)
00408 return ret;
00409 *buf = tbuf;
00410 return 0;
00411 }
00412
00413 int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
00414 int nb_samples)
00415 {
00416 BufferSinkContext *sink = ctx->priv;
00417 int ret = 0, have_samples = 0, need_samples;
00418 AVFilterBufferRef *tbuf, *in_buf;
00419 AVFilterLink *link = ctx->inputs[0];
00420 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
00421
00422 if (ctx->filter-> inputs[0].filter_frame ==
00423 avfilter_asink_abuffer.inputs[0].filter_frame)
00424 return ff_buffersink_read_samples_compat(ctx, buf, nb_samples);
00425 av_assert0(ctx->filter-> inputs[0].filter_frame ==
00426 avfilter_asink_ffabuffersink.inputs[0].filter_frame);
00427
00428 tbuf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples);
00429 if (!tbuf)
00430 return AVERROR(ENOMEM);
00431
00432 while (have_samples < nb_samples) {
00433 ret = av_buffersink_get_buffer_ref(ctx, &in_buf,
00434 AV_BUFFERSINK_FLAG_PEEK);
00435 if (ret < 0) {
00436 if (ret == AVERROR_EOF && have_samples) {
00437 nb_samples = have_samples;
00438 ret = 0;
00439 }
00440 break;
00441 }
00442
00443 need_samples = FFMIN(in_buf->audio->nb_samples,
00444 nb_samples - have_samples);
00445 av_samples_copy(tbuf->extended_data, in_buf->extended_data,
00446 have_samples, 0, need_samples,
00447 nb_channels, in_buf->format);
00448 have_samples += need_samples;
00449 if (need_samples < in_buf->audio->nb_samples) {
00450 in_buf->audio->nb_samples -= need_samples;
00451 av_samples_copy(in_buf->extended_data, in_buf->extended_data,
00452 0, need_samples, in_buf->audio->nb_samples,
00453 nb_channels, in_buf->format);
00454 } else {
00455 av_buffersink_get_buffer_ref(ctx, &in_buf, 0);
00456 avfilter_unref_buffer(in_buf);
00457 }
00458 }
00459 tbuf->audio->nb_samples = have_samples;
00460
00461 if (ret < 0) {
00462 av_assert0(!av_fifo_size(sink->fifo));
00463 if (have_samples)
00464 add_buffer_ref(ctx, tbuf);
00465 else
00466 avfilter_unref_buffer(tbuf);
00467 return ret;
00468 }
00469
00470 *buf = tbuf;
00471 return 0;
00472 }