00001 /* 00002 * Copyright (c) 2008 Vitor Sessak 00003 * 00004 * This file is part of FFmpeg. 00005 * 00006 * FFmpeg is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * FFmpeg is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with FFmpeg; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00026 #include "avfilter.h" 00027 #include "internal.h" 00028 #include "avcodec.h" 00029 #include "vsrc_buffer.h" 00030 #include "libavutil/imgutils.h" 00031 00032 typedef struct { 00033 AVFilterBufferRef *picref; 00034 int h, w; 00035 enum PixelFormat pix_fmt; 00036 AVRational time_base; 00037 AVRational sample_aspect_ratio; 00038 char sws_param[256]; 00039 } BufferSourceContext; 00040 00041 int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter, 00042 AVFilterBufferRef *picref, int flags) 00043 { 00044 BufferSourceContext *c = buffer_filter->priv; 00045 AVFilterLink *outlink = buffer_filter->outputs[0]; 00046 int ret; 00047 00048 if (c->picref) { 00049 if (flags & AV_VSRC_BUF_FLAG_OVERWRITE) { 00050 avfilter_unref_buffer(c->picref); 00051 c->picref = NULL; 00052 } else { 00053 av_log(buffer_filter, AV_LOG_ERROR, 00054 "Buffering several frames is not supported. " 00055 "Please consume all available frames before adding a new one.\n"); 00056 return AVERROR(EINVAL); 00057 } 00058 } 00059 00060 if (picref->video->w != c->w || picref->video->h != c->h || picref->format != c->pix_fmt) { 00061 AVFilterContext *scale = buffer_filter->outputs[0]->dst; 00062 AVFilterLink *link; 00063 char scale_param[1024]; 00064 00065 av_log(buffer_filter, AV_LOG_INFO, 00066 "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n", 00067 c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name, 00068 picref->video->w, picref->video->h, av_pix_fmt_descriptors[picref->format].name); 00069 00070 if (!scale || strcmp(scale->filter->name, "scale")) { 00071 AVFilter *f = avfilter_get_by_name("scale"); 00072 00073 av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n"); 00074 if ((ret = avfilter_open(&scale, f, "Input equalizer")) < 0) 00075 return ret; 00076 00077 snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s", c->w, c->h, c->sws_param); 00078 if ((ret = avfilter_init_filter(scale, scale_param, NULL)) < 0) { 00079 avfilter_free(scale); 00080 return ret; 00081 } 00082 00083 if ((ret = avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0)) < 0) { 00084 avfilter_free(scale); 00085 return ret; 00086 } 00087 scale->outputs[0]->time_base = scale->inputs[0]->time_base; 00088 00089 scale->outputs[0]->format= c->pix_fmt; 00090 } else if (!strcmp(scale->filter->name, "scale")) { 00091 snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s", 00092 scale->outputs[0]->w, scale->outputs[0]->h, c->sws_param); 00093 scale->filter->init(scale, scale_param, NULL); 00094 } 00095 00096 c->pix_fmt = scale->inputs[0]->format = picref->format; 00097 c->w = scale->inputs[0]->w = picref->video->w; 00098 c->h = scale->inputs[0]->h = picref->video->h; 00099 00100 link = scale->outputs[0]; 00101 if ((ret = link->srcpad->config_props(link)) < 0) 00102 return ret; 00103 } 00104 00105 c->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, 00106 picref->video->w, picref->video->h); 00107 av_image_copy(c->picref->data, c->picref->linesize, 00108 (void*)picref->data, picref->linesize, 00109 picref->format, picref->video->w, picref->video->h); 00110 avfilter_copy_buffer_ref_props(c->picref, picref); 00111 00112 return 0; 00113 } 00114 00115 #if CONFIG_AVCODEC 00116 #include "avcodec.h" 00117 00118 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src, 00119 const AVFrame *frame, int flags) 00120 { 00121 int ret; 00122 AVFilterBufferRef *picref = 00123 avfilter_get_video_buffer_ref_from_frame(frame, AV_PERM_WRITE); 00124 if (!picref) 00125 return AVERROR(ENOMEM); 00126 ret = av_vsrc_buffer_add_video_buffer_ref(buffer_src, picref, flags); 00127 picref->buf->data[0] = NULL; 00128 avfilter_unref_buffer(picref); 00129 00130 return ret; 00131 } 00132 #endif 00133 00134 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) 00135 { 00136 BufferSourceContext *c = ctx->priv; 00137 char pix_fmt_str[128]; 00138 int ret, n = 0; 00139 *c->sws_param = 0; 00140 00141 if (!args || 00142 (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str, 00143 &c->time_base.num, &c->time_base.den, 00144 &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den, c->sws_param)) < 7) { 00145 av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args); 00146 return AVERROR(EINVAL); 00147 } 00148 00149 if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0) 00150 return ret; 00151 00152 av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n", 00153 c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name, 00154 c->time_base.num, c->time_base.den, 00155 c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param); 00156 return 0; 00157 } 00158 00159 static int query_formats(AVFilterContext *ctx) 00160 { 00161 BufferSourceContext *c = ctx->priv; 00162 enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE }; 00163 00164 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts)); 00165 return 0; 00166 } 00167 00168 static int config_props(AVFilterLink *link) 00169 { 00170 BufferSourceContext *c = link->src->priv; 00171 00172 link->w = c->w; 00173 link->h = c->h; 00174 link->sample_aspect_ratio = c->sample_aspect_ratio; 00175 link->time_base = c->time_base; 00176 00177 return 0; 00178 } 00179 00180 static int request_frame(AVFilterLink *link) 00181 { 00182 BufferSourceContext *c = link->src->priv; 00183 00184 if (!c->picref) { 00185 av_log(link->src, AV_LOG_WARNING, 00186 "request_frame() called with no available frame!\n"); 00187 return AVERROR(EINVAL); 00188 } 00189 00190 avfilter_start_frame(link, avfilter_ref_buffer(c->picref, ~0)); 00191 avfilter_draw_slice(link, 0, link->h, 1); 00192 avfilter_end_frame(link); 00193 avfilter_unref_buffer(c->picref); 00194 c->picref = NULL; 00195 00196 return 0; 00197 } 00198 00199 static int poll_frame(AVFilterLink *link) 00200 { 00201 BufferSourceContext *c = link->src->priv; 00202 return !!(c->picref); 00203 } 00204 00205 AVFilter avfilter_vsrc_buffer = { 00206 .name = "buffer", 00207 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."), 00208 .priv_size = sizeof(BufferSourceContext), 00209 .query_formats = query_formats, 00210 00211 .init = init, 00212 00213 .inputs = (const AVFilterPad[]) {{ .name = NULL }}, 00214 .outputs = (const AVFilterPad[]) {{ .name = "default", 00215 .type = AVMEDIA_TYPE_VIDEO, 00216 .request_frame = request_frame, 00217 .poll_frame = poll_frame, 00218 .config_props = config_props, }, 00219 { .name = NULL}}, 00220 };