00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "libavutil/channel_layout.h"
00027 #include "libavutil/common.h"
00028 #include "libavutil/fifo.h"
00029 #include "libavutil/imgutils.h"
00030 #include "libavutil/opt.h"
00031 #include "libavutil/samplefmt.h"
00032 #include "audio.h"
00033 #include "avfilter.h"
00034 #include "buffersrc.h"
00035 #include "formats.h"
00036 #include "internal.h"
00037 #include "video.h"
00038 #include "avcodec.h"
00039
00040 typedef struct {
00041 const AVClass *class;
00042 AVFifoBuffer *fifo;
00043 AVRational time_base;
00044 AVRational frame_rate;
00045 unsigned nb_failed_requests;
00046 unsigned warning_limit;
00047
00048
00049 int w, h;
00050 enum AVPixelFormat pix_fmt;
00051 AVRational pixel_aspect;
00052 char *sws_param;
00053
00054
00055 int sample_rate;
00056 enum AVSampleFormat sample_fmt;
00057 char *sample_fmt_str;
00058 uint64_t channel_layout;
00059 char *channel_layout_str;
00060
00061 int eof;
00062 } BufferSourceContext;
00063
00064 #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
00065 if (c->w != width || c->h != height || c->pix_fmt != format) {\
00066 av_log(s, AV_LOG_INFO, "Changing frame properties on the fly is not supported by all filters.\n");\
00067 }
00068
00069 #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
00070 if (c->sample_fmt != format || c->sample_rate != srate ||\
00071 c->channel_layout != ch_layout) {\
00072 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
00073 return AVERROR(EINVAL);\
00074 }
00075
00076 int av_buffersrc_add_frame(AVFilterContext *buffer_src,
00077 const AVFrame *frame, int flags)
00078 {
00079 AVFilterBufferRef *picref;
00080 int ret;
00081
00082 if (!frame)
00083 return av_buffersrc_add_ref(buffer_src, NULL, flags);
00084
00085 picref = avfilter_get_buffer_ref_from_frame(buffer_src->outputs[0]->type,
00086 frame, AV_PERM_WRITE);
00087 if (!picref)
00088 return AVERROR(ENOMEM);
00089 ret = av_buffersrc_add_ref(buffer_src, picref, flags);
00090 picref->buf->data[0] = NULL;
00091 avfilter_unref_buffer(picref);
00092 return ret;
00093 }
00094
00095 int av_buffersrc_write_frame(AVFilterContext *buffer_filter, const AVFrame *frame)
00096 {
00097 return av_buffersrc_add_frame(buffer_filter, frame, 0);
00098 }
00099
00100 int av_buffersrc_add_ref(AVFilterContext *s, AVFilterBufferRef *buf, int flags)
00101 {
00102 BufferSourceContext *c = s->priv;
00103 AVFilterBufferRef *to_free = NULL;
00104 int ret;
00105
00106 if (!buf) {
00107 c->eof = 1;
00108 return 0;
00109 } else if (c->eof)
00110 return AVERROR(EINVAL);
00111
00112 if (!av_fifo_space(c->fifo) &&
00113 (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
00114 sizeof(buf))) < 0)
00115 return ret;
00116
00117 if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
00118 switch (s->outputs[0]->type) {
00119 case AVMEDIA_TYPE_VIDEO:
00120 CHECK_VIDEO_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
00121 break;
00122 case AVMEDIA_TYPE_AUDIO:
00123 CHECK_AUDIO_PARAM_CHANGE(s, c, buf->audio->sample_rate, buf->audio->channel_layout,
00124 buf->format);
00125 break;
00126 default:
00127 return AVERROR(EINVAL);
00128 }
00129 }
00130 if (!(flags & AV_BUFFERSRC_FLAG_NO_COPY))
00131 to_free = buf = ff_copy_buffer_ref(s->outputs[0], buf);
00132 if(!buf)
00133 return -1;
00134
00135 if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
00136 avfilter_unref_buffer(to_free);
00137 return ret;
00138 }
00139 c->nb_failed_requests = 0;
00140 if (c->warning_limit &&
00141 av_fifo_size(c->fifo) / sizeof(buf) >= c->warning_limit) {
00142 av_log(s, AV_LOG_WARNING,
00143 "%d buffers queued in %s, something may be wrong.\n",
00144 c->warning_limit,
00145 (char *)av_x_if_null(s->name, s->filter->name));
00146 c->warning_limit *= 10;
00147 }
00148
00149 if ((flags & AV_BUFFERSRC_FLAG_PUSH))
00150 if ((ret = s->output_pads[0].request_frame(s->outputs[0])) < 0)
00151 return ret;
00152
00153 return 0;
00154 }
00155
00156 #ifdef FF_API_BUFFERSRC_BUFFER
00157 int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
00158 {
00159 return av_buffersrc_add_ref(s, buf, AV_BUFFERSRC_FLAG_NO_COPY);
00160 }
00161 #endif
00162
00163 unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
00164 {
00165 return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
00166 }
00167
00168 #define OFFSET(x) offsetof(BufferSourceContext, x)
00169 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
00170 static const AVOption buffer_options[] = {
00171 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
00172 { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
00173 { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = FLAGS },
00174 { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, .flags = FLAGS },
00175 { "pixel_aspect", NULL, OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
00176 { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = FLAGS },
00177 { NULL },
00178 };
00179 #undef FLAGS
00180
00181 AVFILTER_DEFINE_CLASS(buffer);
00182
00183 static av_cold int init_video(AVFilterContext *ctx, const char *args)
00184 {
00185 BufferSourceContext *c = ctx->priv;
00186 char pix_fmt_str[128], sws_param[256] = "", *colon, *equal;
00187 int ret, n = 0;
00188
00189 c->class = &buffer_class;
00190
00191 if (!args) {
00192 av_log(ctx, AV_LOG_ERROR, "Arguments required\n");
00193 return AVERROR(EINVAL);
00194 }
00195 colon = strchr(args, ':');
00196 equal = strchr(args, '=');
00197 if (equal && (!colon || equal < colon)) {
00198 av_opt_set_defaults(c);
00199 ret = av_set_options_string(c, args, "=", ":");
00200 if (ret < 0)
00201 goto fail;
00202 } else {
00203 if ((n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
00204 &c->time_base.num, &c->time_base.den,
00205 &c->pixel_aspect.num, &c->pixel_aspect.den, sws_param)) < 7) {
00206 av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
00207 ret = AVERROR(EINVAL);
00208 goto fail;
00209 }
00210 av_log(ctx, AV_LOG_WARNING, "Flat options syntax is deprecated, use key=value pairs\n");
00211
00212 if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
00213 goto fail;
00214 c->sws_param = av_strdup(sws_param);
00215 if (!c->sws_param) {
00216 ret = AVERROR(ENOMEM);
00217 goto fail;
00218 }
00219 }
00220
00221 if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
00222 ret = AVERROR(ENOMEM);
00223 goto fail;
00224 }
00225
00226 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d fr:%d/%d sar:%d/%d sws_param:%s\n",
00227 c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
00228 c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
00229 c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
00230 c->warning_limit = 100;
00231 return 0;
00232
00233 fail:
00234 av_opt_free(c);
00235 return ret;
00236 }
00237
00238 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
00239 static const AVOption abuffer_options[] = {
00240 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
00241 { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
00242 { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
00243 { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
00244 { NULL },
00245 };
00246
00247 AVFILTER_DEFINE_CLASS(abuffer);
00248
00249 static av_cold int init_audio(AVFilterContext *ctx, const char *args)
00250 {
00251 BufferSourceContext *s = ctx->priv;
00252 int ret = 0;
00253
00254 s->class = &abuffer_class;
00255 av_opt_set_defaults(s);
00256
00257 if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
00258 goto fail;
00259
00260 s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
00261 if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
00262 av_log(ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n",
00263 s->sample_fmt_str);
00264 ret = AVERROR(EINVAL);
00265 goto fail;
00266 }
00267
00268 s->channel_layout = av_get_channel_layout(s->channel_layout_str);
00269 if (!s->channel_layout) {
00270 av_log(ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n",
00271 s->channel_layout_str);
00272 ret = AVERROR(EINVAL);
00273 goto fail;
00274 }
00275
00276 if (!(s->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
00277 ret = AVERROR(ENOMEM);
00278 goto fail;
00279 }
00280
00281 if (!s->time_base.num)
00282 s->time_base = (AVRational){1, s->sample_rate};
00283
00284 av_log(ctx, AV_LOG_VERBOSE,
00285 "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
00286 s->time_base.num, s->time_base.den, s->sample_fmt_str,
00287 s->sample_rate, s->channel_layout_str);
00288 s->warning_limit = 100;
00289
00290 fail:
00291 av_opt_free(s);
00292 return ret;
00293 }
00294
00295 static av_cold void uninit(AVFilterContext *ctx)
00296 {
00297 BufferSourceContext *s = ctx->priv;
00298 while (s->fifo && av_fifo_size(s->fifo)) {
00299 AVFilterBufferRef *buf;
00300 av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
00301 avfilter_unref_buffer(buf);
00302 }
00303 av_fifo_free(s->fifo);
00304 s->fifo = NULL;
00305 av_freep(&s->sws_param);
00306 }
00307
00308 static int query_formats(AVFilterContext *ctx)
00309 {
00310 BufferSourceContext *c = ctx->priv;
00311 AVFilterChannelLayouts *channel_layouts = NULL;
00312 AVFilterFormats *formats = NULL;
00313 AVFilterFormats *samplerates = NULL;
00314
00315 switch (ctx->outputs[0]->type) {
00316 case AVMEDIA_TYPE_VIDEO:
00317 ff_add_format(&formats, c->pix_fmt);
00318 ff_set_common_formats(ctx, formats);
00319 break;
00320 case AVMEDIA_TYPE_AUDIO:
00321 ff_add_format(&formats, c->sample_fmt);
00322 ff_set_common_formats(ctx, formats);
00323
00324 ff_add_format(&samplerates, c->sample_rate);
00325 ff_set_common_samplerates(ctx, samplerates);
00326
00327 ff_add_channel_layout(&channel_layouts, c->channel_layout);
00328 ff_set_common_channel_layouts(ctx, channel_layouts);
00329 break;
00330 default:
00331 return AVERROR(EINVAL);
00332 }
00333
00334 return 0;
00335 }
00336
00337 static int config_props(AVFilterLink *link)
00338 {
00339 BufferSourceContext *c = link->src->priv;
00340
00341 switch (link->type) {
00342 case AVMEDIA_TYPE_VIDEO:
00343 link->w = c->w;
00344 link->h = c->h;
00345 link->sample_aspect_ratio = c->pixel_aspect;
00346 break;
00347 case AVMEDIA_TYPE_AUDIO:
00348 link->channel_layout = c->channel_layout;
00349 link->sample_rate = c->sample_rate;
00350 break;
00351 default:
00352 return AVERROR(EINVAL);
00353 }
00354
00355 link->time_base = c->time_base;
00356 link->frame_rate = c->frame_rate;
00357 return 0;
00358 }
00359
00360 static int request_frame(AVFilterLink *link)
00361 {
00362 BufferSourceContext *c = link->src->priv;
00363 AVFilterBufferRef *buf;
00364 int ret = 0;
00365
00366 if (!av_fifo_size(c->fifo)) {
00367 if (c->eof)
00368 return AVERROR_EOF;
00369 c->nb_failed_requests++;
00370 return AVERROR(EAGAIN);
00371 }
00372 av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
00373
00374 ff_filter_frame(link, buf);
00375
00376 return ret;
00377 }
00378
00379 static int poll_frame(AVFilterLink *link)
00380 {
00381 BufferSourceContext *c = link->src->priv;
00382 int size = av_fifo_size(c->fifo);
00383 if (!size && c->eof)
00384 return AVERROR_EOF;
00385 return size/sizeof(AVFilterBufferRef*);
00386 }
00387
00388 static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
00389 {
00390 .name = "default",
00391 .type = AVMEDIA_TYPE_VIDEO,
00392 .request_frame = request_frame,
00393 .poll_frame = poll_frame,
00394 .config_props = config_props,
00395 },
00396 { NULL }
00397 };
00398
00399 AVFilter avfilter_vsrc_buffer = {
00400 .name = "buffer",
00401 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
00402 .priv_size = sizeof(BufferSourceContext),
00403 .query_formats = query_formats,
00404
00405 .init = init_video,
00406 .uninit = uninit,
00407
00408 .inputs = NULL,
00409 .outputs = avfilter_vsrc_buffer_outputs,
00410 .priv_class = &buffer_class,
00411 };
00412
00413 static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
00414 {
00415 .name = "default",
00416 .type = AVMEDIA_TYPE_AUDIO,
00417 .request_frame = request_frame,
00418 .poll_frame = poll_frame,
00419 .config_props = config_props,
00420 },
00421 { NULL }
00422 };
00423
00424 AVFilter avfilter_asrc_abuffer = {
00425 .name = "abuffer",
00426 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
00427 .priv_size = sizeof(BufferSourceContext),
00428 .query_formats = query_formats,
00429
00430 .init = init_audio,
00431 .uninit = uninit,
00432
00433 .inputs = NULL,
00434 .outputs = avfilter_asrc_abuffer_outputs,
00435 .priv_class = &abuffer_class,
00436 };