00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "libavutil/audioconvert.h"
00027 #include "libavutil/avassert.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[] = { -1 };
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[] = { -1 };
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 PixelFormat *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 AVFilter avfilter_vsink_ffbuffersink = {
00238 .name = "ffbuffersink",
00239 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
00240 .priv_size = sizeof(BufferSinkContext),
00241 .init_opaque = vsink_init,
00242 .uninit = vsink_uninit,
00243
00244 .query_formats = vsink_query_formats,
00245
00246 .inputs = (const AVFilterPad[]) {{ .name = "default",
00247 .type = AVMEDIA_TYPE_VIDEO,
00248 .end_frame = end_frame,
00249 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
00250 { .name = NULL }},
00251 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
00252 };
00253
00254 AVFilter avfilter_vsink_buffersink = {
00255 .name = "buffersink",
00256 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
00257 .priv_size = sizeof(BufferSinkContext),
00258 .init_opaque = vsink_init,
00259 .uninit = vsink_uninit,
00260
00261 .query_formats = vsink_query_formats,
00262
00263 .inputs = (const AVFilterPad[]) {{ .name = "default",
00264 .type = AVMEDIA_TYPE_VIDEO,
00265 .end_frame = end_frame,
00266 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
00267 { .name = NULL }},
00268 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
00269 };
00270
00271 static int filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
00272 {
00273 end_frame(link);
00274 return 0;
00275 }
00276
00277 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
00278 {
00279 BufferSinkContext *buf = ctx->priv;
00280 AVABufferSinkParams *params = opaque;
00281
00282 if (params && params->sample_fmts) {
00283 buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
00284 if (!buf->sample_fmts)
00285 goto fail_enomem;
00286 }
00287 if (params && params->channel_layouts) {
00288 buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
00289 if (!buf->channel_layouts)
00290 goto fail_enomem;
00291 }
00292 if (!common_init(ctx))
00293 return 0;
00294
00295 fail_enomem:
00296 av_freep(&buf->sample_fmts);
00297 av_freep(&buf->channel_layouts);
00298 return AVERROR(ENOMEM);
00299 }
00300
00301 static av_cold void asink_uninit(AVFilterContext *ctx)
00302 {
00303 BufferSinkContext *buf = ctx->priv;
00304
00305 av_freep(&buf->sample_fmts);
00306 av_freep(&buf->channel_layouts);
00307 common_uninit(ctx);
00308 }
00309
00310 static int asink_query_formats(AVFilterContext *ctx)
00311 {
00312 BufferSinkContext *buf = ctx->priv;
00313 AVFilterFormats *formats = NULL;
00314 AVFilterChannelLayouts *layouts = NULL;
00315
00316 if (buf->sample_fmts) {
00317 if (!(formats = ff_make_format_list(buf->sample_fmts)))
00318 return AVERROR(ENOMEM);
00319 ff_set_common_formats(ctx, formats);
00320 }
00321
00322 if (buf->channel_layouts) {
00323 if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
00324 return AVERROR(ENOMEM);
00325 ff_set_common_channel_layouts(ctx, layouts);
00326 }
00327
00328 return 0;
00329 }
00330
00331 AVFilter avfilter_asink_ffabuffersink = {
00332 .name = "ffabuffersink",
00333 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
00334 .init_opaque = asink_init,
00335 .uninit = asink_uninit,
00336 .priv_size = sizeof(BufferSinkContext),
00337 .query_formats = asink_query_formats,
00338
00339 .inputs = (const AVFilterPad[]) {{ .name = "default",
00340 .type = AVMEDIA_TYPE_AUDIO,
00341 .filter_samples = filter_samples,
00342 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
00343 { .name = NULL }},
00344 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
00345 };
00346
00347 AVFilter avfilter_asink_abuffersink = {
00348 .name = "abuffersink",
00349 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
00350 .init_opaque = asink_init,
00351 .uninit = asink_uninit,
00352 .priv_size = sizeof(BufferSinkContext),
00353 .query_formats = asink_query_formats,
00354
00355 .inputs = (const AVFilterPad[]) {{ .name = "default",
00356 .type = AVMEDIA_TYPE_AUDIO,
00357 .filter_samples = filter_samples,
00358 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
00359 { .name = NULL }},
00360 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
00361 };
00362
00363
00364
00365 extern AVFilter avfilter_vsink_buffer;
00366 extern AVFilter avfilter_asink_abuffer;
00367
00368 int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
00369 {
00370 AVFilterBufferRef *tbuf;
00371 int ret;
00372
00373 if (ctx->filter-> inputs[0].start_frame ==
00374 avfilter_vsink_buffer. inputs[0].start_frame ||
00375 ctx->filter-> inputs[0].filter_samples ==
00376 avfilter_asink_abuffer.inputs[0].filter_samples)
00377 return ff_buffersink_read_compat(ctx, buf);
00378 av_assert0(ctx->filter-> inputs[0].end_frame ==
00379 avfilter_vsink_ffbuffersink. inputs[0].end_frame ||
00380 ctx->filter-> inputs[0].filter_samples ==
00381 avfilter_asink_ffabuffersink.inputs[0].filter_samples);
00382
00383 ret = av_buffersink_get_buffer_ref(ctx, &tbuf,
00384 buf ? 0 : AV_BUFFERSINK_FLAG_PEEK);
00385 if (!buf)
00386 return ret >= 0;
00387 if (ret < 0)
00388 return ret;
00389 *buf = tbuf;
00390 return 0;
00391 }
00392
00393 int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
00394 int nb_samples)
00395 {
00396 BufferSinkContext *sink = ctx->priv;
00397 int ret = 0, have_samples = 0, need_samples;
00398 AVFilterBufferRef *tbuf, *in_buf;
00399 AVFilterLink *link = ctx->inputs[0];
00400 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
00401
00402 if (ctx->filter-> inputs[0].filter_samples ==
00403 avfilter_asink_abuffer.inputs[0].filter_samples)
00404 return ff_buffersink_read_samples_compat(ctx, buf, nb_samples);
00405 av_assert0(ctx->filter-> inputs[0].filter_samples ==
00406 avfilter_asink_ffabuffersink.inputs[0].filter_samples);
00407
00408 tbuf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples);
00409 if (!tbuf)
00410 return AVERROR(ENOMEM);
00411
00412 while (have_samples < nb_samples) {
00413 ret = av_buffersink_get_buffer_ref(ctx, &in_buf,
00414 AV_BUFFERSINK_FLAG_PEEK);
00415 if (ret < 0) {
00416 if (ret == AVERROR_EOF && have_samples) {
00417 nb_samples = have_samples;
00418 ret = 0;
00419 }
00420 break;
00421 }
00422
00423 need_samples = FFMIN(in_buf->audio->nb_samples,
00424 nb_samples - have_samples);
00425 av_samples_copy(tbuf->extended_data, in_buf->extended_data,
00426 have_samples, 0, need_samples,
00427 nb_channels, in_buf->format);
00428 have_samples += need_samples;
00429 if (need_samples < in_buf->audio->nb_samples) {
00430 in_buf->audio->nb_samples -= need_samples;
00431 av_samples_copy(in_buf->extended_data, in_buf->extended_data,
00432 0, need_samples, in_buf->audio->nb_samples,
00433 nb_channels, in_buf->format);
00434 } else {
00435 av_buffersink_get_buffer_ref(ctx, &in_buf, 0);
00436 avfilter_unref_buffer(in_buf);
00437 }
00438 }
00439 tbuf->audio->nb_samples = have_samples;
00440
00441 if (ret < 0) {
00442 av_assert0(!av_fifo_size(sink->fifo));
00443 if (have_samples)
00444 add_buffer_ref(ctx, tbuf);
00445 else
00446 avfilter_unref_buffer(tbuf);
00447 return ret;
00448 }
00449
00450 *buf = tbuf;
00451 return 0;
00452 }