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