libavfilter/vsrc_buffer.c
cf13f204
 /*
  * Copyright (c) 2008 Vitor Sessak
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
b5634e45
 /**
  * @file
  * memory buffer source filter
  */
 
cf13f204
 #include "avfilter.h"
e26782a9
 #include "internal.h"
566666ca
 #include "avcodec.h"
e1d9dbf2
 #include "buffersrc.h"
cf13f204
 #include "vsrc_buffer.h"
7ffe76e5
 #include "libavutil/imgutils.h"
cf13f204
 
 typedef struct {
6070b7e1
     AVFilterBufferRef *picref;
9164afcb
     int               h, w;
     enum PixelFormat  pix_fmt;
94498ec9
     AVRational        time_base;     ///< time_base to set in the output link
35fe66ab
     AVRational        sample_aspect_ratio;
a38cdfde
     char              sws_param[256];
cf13f204
 } BufferSourceContext;
 
5d25140f
 #define CHECK_PARAM_CHANGE(s, c, width, height, format)\
     if (c->w != width || c->h != height || c->pix_fmt != format) {\
         av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
         return AVERROR(EINVAL);\
     }
 
27bcf55f
 int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter,
                                         AVFilterBufferRef *picref, int flags)
cf13f204
 {
     BufferSourceContext *c = buffer_filter->priv;
6070b7e1
     AVFilterLink *outlink = buffer_filter->outputs[0];
7d948dc4
     int ret;
cf13f204
 
6070b7e1
     if (c->picref) {
27bcf55f
         if (flags & AV_VSRC_BUF_FLAG_OVERWRITE) {
             avfilter_unref_buffer(c->picref);
             c->picref = NULL;
         } else {
             av_log(buffer_filter, AV_LOG_ERROR,
                    "Buffering several frames is not supported. "
                    "Please consume all available frames before adding a new one.\n");
             return AVERROR(EINVAL);
         }
cf13f204
     }
 
6070b7e1
     if (picref->video->w != c->w || picref->video->h != c->h || picref->format != c->pix_fmt) {
3799805e
         AVFilterContext *scale = buffer_filter->outputs[0]->dst;
7d948dc4
         AVFilterLink *link;
50764e19
         char scale_param[1024];
7d948dc4
 
0f1ef8a3
         av_log(buffer_filter, AV_LOG_INFO,
                "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
                c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
6070b7e1
                picref->video->w, picref->video->h, av_pix_fmt_descriptors[picref->format].name);
7d948dc4
 
3799805e
         if (!scale || strcmp(scale->filter->name, "scale")) {
             AVFilter *f = avfilter_get_by_name("scale");
7d948dc4
 
             av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
509b32cf
             if ((ret = avfilter_open(&scale, f, "Input equalizer")) < 0)
                 return ret;
7d948dc4
 
50764e19
             snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s", c->w, c->h, c->sws_param);
             if ((ret = avfilter_init_filter(scale, scale_param, NULL)) < 0) {
7d948dc4
                 avfilter_free(scale);
                 return ret;
             }
 
3799805e
             if ((ret = avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0)) < 0) {
7d948dc4
                 avfilter_free(scale);
                 return ret;
             }
dc1de569
             scale->outputs[0]->time_base = scale->inputs[0]->time_base;
7d948dc4
 
             scale->outputs[0]->format= c->pix_fmt;
3799805e
         } else if (!strcmp(scale->filter->name, "scale")) {
50764e19
             snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s",
                      scale->outputs[0]->w, scale->outputs[0]->h, c->sws_param);
             scale->filter->init(scale, scale_param, NULL);
7d948dc4
         }
 
6070b7e1
         c->pix_fmt = scale->inputs[0]->format = picref->format;
         c->w       = scale->inputs[0]->w      = picref->video->w;
         c->h       = scale->inputs[0]->h      = picref->video->h;
7d948dc4
 
3799805e
         link = scale->outputs[0];
7d948dc4
         if ((ret =  link->srcpad->config_props(link)) < 0)
             return ret;
     }
 
6070b7e1
     c->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
                                           picref->video->w, picref->video->h);
     av_image_copy(c->picref->data, c->picref->linesize,
2982b02b
                   (void*)picref->data, picref->linesize,
6070b7e1
                   picref->format, picref->video->w, picref->video->h);
     avfilter_copy_buffer_ref_props(c->picref, picref);
cf13f204
 
     return 0;
 }
 
e1d9dbf2
 int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
 {
     BufferSourceContext *c = s->priv;
 
484e59a0
     if (c->picref) {
e1d9dbf2
         av_log(s, AV_LOG_ERROR,
                "Buffering several frames is not supported. "
                "Please consume all available frames before adding a new one.\n"
             );
         return AVERROR(EINVAL);
     }
 
0e7fc3ca
 //     CHECK_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
5d25140f
 
484e59a0
     c->picref = buf;
cf13f204
 
     return 0;
 }
 
c000a9f7
 #if CONFIG_AVCODEC
 #include "avcodec.h"
 
27bcf55f
 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src,
                              const AVFrame *frame, int flags)
c000a9f7
 {
9e66b64c
     int ret;
c000a9f7
     AVFilterBufferRef *picref =
         avfilter_get_video_buffer_ref_from_frame(frame, AV_PERM_WRITE);
     if (!picref)
         return AVERROR(ENOMEM);
27bcf55f
     ret = av_vsrc_buffer_add_video_buffer_ref(buffer_src, picref, flags);
c000a9f7
     picref->buf->data[0] = NULL;
     avfilter_unref_buffer(picref);
 
9e66b64c
     return ret;
c000a9f7
 }
 #endif
 
cf13f204
 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
 {
     BufferSourceContext *c = ctx->priv;
59775b3c
     char pix_fmt_str[128];
e26782a9
     int ret, n = 0;
50764e19
     *c->sws_param = 0;
cf13f204
 
94498ec9
     if (!args ||
50764e19
         (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
7b3ea550
                     &c->time_base.num, &c->time_base.den,
50764e19
                     &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den, c->sws_param)) < 7) {
         av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
b8dddebf
         return AVERROR(EINVAL);
     }
50764e19
 
e26782a9
     if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
         return ret;
cf13f204
 
50764e19
     av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
ea7f73c5
            c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
            c->time_base.num, c->time_base.den,
50764e19
            c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param);
b8dddebf
     return 0;
cf13f204
 }
 
43fe6a29
 static av_cold void uninit(AVFilterContext *ctx)
 {
     BufferSourceContext *s = ctx->priv;
b18e17ea
     if (s->picref)
         avfilter_unref_buffer(s->picref);
     s->picref = NULL;
43fe6a29
 }
 
cf13f204
 static int query_formats(AVFilterContext *ctx)
 {
     BufferSourceContext *c = ctx->priv;
     enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
 
fd2c0a5d
     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
cf13f204
     return 0;
 }
 
 static int config_props(AVFilterLink *link)
 {
     BufferSourceContext *c = link->src->priv;
 
     link->w = c->w;
     link->h = c->h;
35fe66ab
     link->sample_aspect_ratio = c->sample_aspect_ratio;
94498ec9
     link->time_base = c->time_base;
cf13f204
 
     return 0;
 }
 
 static int request_frame(AVFilterLink *link)
 {
     BufferSourceContext *c = link->src->priv;
 
6070b7e1
     if (!c->picref) {
cf06e3e4
         av_log(link->src, AV_LOG_WARNING,
cf13f204
                "request_frame() called with no available frame!\n");
cf06e3e4
         return AVERROR(EINVAL);
cf13f204
     }
 
6070b7e1
     avfilter_start_frame(link, avfilter_ref_buffer(c->picref, ~0));
cf13f204
     avfilter_draw_slice(link, 0, link->h, 1);
     avfilter_end_frame(link);
6070b7e1
     avfilter_unref_buffer(c->picref);
     c->picref = NULL;
cf13f204
 
     return 0;
 }
 
 static int poll_frame(AVFilterLink *link)
 {
     BufferSourceContext *c = link->src->priv;
75747383
     return !!c->picref;
cf13f204
 }
 
4055438b
 AVFilter avfilter_vsrc_buffer = {
cf13f204
     .name      = "buffer",
ce1f8536
     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
cf13f204
     .priv_size = sizeof(BufferSourceContext),
     .query_formats = query_formats,
 
     .init      = init,
43fe6a29
     .uninit    = uninit,
cf13f204
 
5af7daab
     .inputs    = (const AVFilterPad[]) {{ .name = NULL }},
     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
cf13f204
                                     .type            = AVMEDIA_TYPE_VIDEO,
                                     .request_frame   = request_frame,
                                     .poll_frame      = poll_frame,
                                     .config_props    = config_props, },
                                   { .name = NULL}},
 };