| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * 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 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-20 16:23:18 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file | 
					
						
							|  |  |  |  * memory buffer source filter | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | #include "avfilter.h"
 | 
					
						
							| 
									
										
										
										
											2011-05-01 14:47:05 +02:00
										 |  |  | #include "avcodec.h"
 | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | #include "vsrc_buffer.h"
 | 
					
						
							| 
									
										
										
										
											2011-02-07 14:37:08 +01:00
										 |  |  | #include "libavutil/imgutils.h"
 | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							| 
									
										
										
										
											2011-05-07 21:35:08 +02:00
										 |  |  |     AVFilterBufferRef *picref; | 
					
						
							| 
									
										
										
										
											2010-06-17 22:33:42 +00:00
										 |  |  |     int               h, w; | 
					
						
							|  |  |  |     enum PixelFormat  pix_fmt; | 
					
						
							| 
									
										
										
										
											2010-10-12 18:40:20 +00:00
										 |  |  |     AVRational        time_base;     ///< time_base to set in the output link
 | 
					
						
							| 
									
										
										
										
											2011-04-29 13:12:39 +02:00
										 |  |  |     AVRational        sample_aspect_ratio; | 
					
						
							| 
									
										
										
										
											2011-04-26 18:16:48 +02:00
										 |  |  |     char              sws_param[256]; | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | } BufferSourceContext; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-19 01:02:54 +02:00
										 |  |  | int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter, AVFilterBufferRef *picref) | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     BufferSourceContext *c = buffer_filter->priv; | 
					
						
							| 
									
										
										
										
											2011-05-07 21:35:08 +02:00
										 |  |  |     AVFilterLink *outlink = buffer_filter->outputs[0]; | 
					
						
							| 
									
										
										
										
											2011-03-09 15:13:59 +01:00
										 |  |  |     int ret; | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-07 21:35:08 +02:00
										 |  |  |     if (c->picref) { | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  |         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 -1;
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-07 21:35:08 +02:00
										 |  |  |     if (picref->video->w != c->w || picref->video->h != c->h || picref->format != c->pix_fmt) { | 
					
						
							| 
									
										
										
										
											2011-05-13 18:07:51 +02:00
										 |  |  |         AVFilterContext *scale = buffer_filter->outputs[0]->dst; | 
					
						
							| 
									
										
										
										
											2011-03-09 15:13:59 +01:00
										 |  |  |         AVFilterLink *link; | 
					
						
							| 
									
										
										
										
											2011-05-19 01:02:54 +02:00
										 |  |  |         char scale_param[1024]; | 
					
						
							| 
									
										
										
										
											2011-03-09 15:13:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-02 01:41:28 +02:00
										 |  |  |         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, | 
					
						
							| 
									
										
										
										
											2011-05-07 21:35:08 +02:00
										 |  |  |                picref->video->w, picref->video->h, av_pix_fmt_descriptors[picref->format].name); | 
					
						
							| 
									
										
										
										
											2011-03-09 15:13:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-13 18:07:51 +02:00
										 |  |  |         if (!scale || strcmp(scale->filter->name, "scale")) { | 
					
						
							|  |  |  |             AVFilter *f = avfilter_get_by_name("scale"); | 
					
						
							| 
									
										
										
										
											2011-03-09 15:13:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n"); | 
					
						
							| 
									
										
										
										
											2011-05-13 18:09:47 +02:00
										 |  |  |             if ((ret = avfilter_open(&scale, f, "Input equalizer")) < 0) | 
					
						
							|  |  |  |                 return ret; | 
					
						
							| 
									
										
										
										
											2011-03-09 15:13:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-19 01:02:54 +02:00
										 |  |  |             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) { | 
					
						
							| 
									
										
										
										
											2011-03-09 15:13:59 +01:00
										 |  |  |                 avfilter_free(scale); | 
					
						
							|  |  |  |                 return ret; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-13 18:07:51 +02:00
										 |  |  |             if ((ret = avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0)) < 0) { | 
					
						
							| 
									
										
										
										
											2011-03-09 15:13:59 +01:00
										 |  |  |                 avfilter_free(scale); | 
					
						
							|  |  |  |                 return ret; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2011-04-26 18:17:20 +02:00
										 |  |  |             scale->outputs[0]->time_base = scale->inputs[0]->time_base; | 
					
						
							| 
									
										
										
										
											2011-03-09 15:13:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             scale->outputs[0]->format= c->pix_fmt; | 
					
						
							| 
									
										
										
										
											2011-05-13 18:07:51 +02:00
										 |  |  |         } else if (!strcmp(scale->filter->name, "scale")) { | 
					
						
							| 
									
										
										
										
											2011-05-19 01:02:54 +02:00
										 |  |  |             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); | 
					
						
							| 
									
										
										
										
											2011-03-09 15:13:59 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-07 21:35:08 +02:00
										 |  |  |         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; | 
					
						
							| 
									
										
										
										
											2011-03-09 15:13:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-13 18:07:51 +02:00
										 |  |  |         link = scale->outputs[0]; | 
					
						
							| 
									
										
										
										
											2011-03-09 15:13:59 +01:00
										 |  |  |         if ((ret =  link->srcpad->config_props(link)) < 0) | 
					
						
							|  |  |  |             return ret; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-07 21:35:08 +02:00
										 |  |  |     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, | 
					
						
							|  |  |  |                   picref->data, picref->linesize, | 
					
						
							|  |  |  |                   picref->format, picref->video->w, picref->video->h); | 
					
						
							|  |  |  |     avfilter_copy_buffer_ref_props(c->picref, picref); | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-19 01:17:16 +02:00
										 |  |  | #if CONFIG_AVCODEC
 | 
					
						
							|  |  |  | #include "avcodec.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src, const AVFrame *frame) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     AVFilterBufferRef *picref = | 
					
						
							|  |  |  |         avfilter_get_video_buffer_ref_from_frame(frame, AV_PERM_WRITE); | 
					
						
							|  |  |  |     if (!picref) | 
					
						
							|  |  |  |         return AVERROR(ENOMEM); | 
					
						
							|  |  |  |     av_vsrc_buffer_add_video_buffer_ref(buffer_src, picref); | 
					
						
							|  |  |  |     picref->buf->data[0] = NULL; | 
					
						
							|  |  |  |     avfilter_unref_buffer(picref); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     BufferSourceContext *c = ctx->priv; | 
					
						
							| 
									
										
										
										
											2010-07-21 17:14:19 +00:00
										 |  |  |     char pix_fmt_str[128]; | 
					
						
							| 
									
										
										
										
											2010-07-21 17:14:13 +00:00
										 |  |  |     int n = 0; | 
					
						
							| 
									
										
										
										
											2011-05-19 01:02:54 +02:00
										 |  |  |     *c->sws_param = 0; | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 18:40:20 +00:00
										 |  |  |     if (!args || | 
					
						
							| 
									
										
										
										
											2011-05-19 01:02:54 +02:00
										 |  |  |         (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str, | 
					
						
							| 
									
										
										
										
											2011-01-31 20:48:35 +01:00
										 |  |  |                     &c->time_base.num, &c->time_base.den, | 
					
						
							| 
									
										
										
										
											2011-05-19 01:02:54 +02:00
										 |  |  |                     &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); | 
					
						
							| 
									
										
										
										
											2010-07-21 17:14:13 +00:00
										 |  |  |         return AVERROR(EINVAL); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-05-19 01:02:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-21 17:14:19 +00:00
										 |  |  |     if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) { | 
					
						
							|  |  |  |         char *tail; | 
					
						
							|  |  |  |         c->pix_fmt = strtol(pix_fmt_str, &tail, 10); | 
					
						
							|  |  |  |         if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) { | 
					
						
							|  |  |  |             av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str); | 
					
						
							|  |  |  |             return AVERROR(EINVAL); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-19 01:02:54 +02:00
										 |  |  |     av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n", | 
					
						
							| 
									
										
										
										
											2011-05-07 12:44:18 +02:00
										 |  |  |            c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name, | 
					
						
							|  |  |  |            c->time_base.num, c->time_base.den, | 
					
						
							| 
									
										
										
										
											2011-05-19 01:02:54 +02:00
										 |  |  |            c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param); | 
					
						
							| 
									
										
										
										
											2010-07-21 17:14:13 +00:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int query_formats(AVFilterContext *ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     BufferSourceContext *c = ctx->priv; | 
					
						
							|  |  |  |     enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts)); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int config_props(AVFilterLink *link) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     BufferSourceContext *c = link->src->priv; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     link->w = c->w; | 
					
						
							|  |  |  |     link->h = c->h; | 
					
						
							| 
									
										
										
										
											2011-04-29 13:12:39 +02:00
										 |  |  |     link->sample_aspect_ratio = c->sample_aspect_ratio; | 
					
						
							| 
									
										
										
										
											2010-10-12 18:40:20 +00:00
										 |  |  |     link->time_base = c->time_base; | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int request_frame(AVFilterLink *link) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     BufferSourceContext *c = link->src->priv; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-07 21:35:08 +02:00
										 |  |  |     if (!c->picref) { | 
					
						
							| 
									
										
										
										
											2011-05-20 01:00:59 +02:00
										 |  |  |         av_log(link->src, AV_LOG_WARNING, | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  |                "request_frame() called with no available frame!\n"); | 
					
						
							| 
									
										
										
										
											2011-05-20 01:00:59 +02:00
										 |  |  |         return AVERROR(EINVAL); | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-07 21:35:08 +02:00
										 |  |  |     avfilter_start_frame(link, avfilter_ref_buffer(c->picref, ~0)); | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  |     avfilter_draw_slice(link, 0, link->h, 1); | 
					
						
							|  |  |  |     avfilter_end_frame(link); | 
					
						
							| 
									
										
										
										
											2011-05-07 21:35:08 +02:00
										 |  |  |     avfilter_unref_buffer(c->picref); | 
					
						
							|  |  |  |     c->picref = NULL; | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int poll_frame(AVFilterLink *link) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     BufferSourceContext *c = link->src->priv; | 
					
						
							| 
									
										
										
										
											2011-05-07 21:35:08 +02:00
										 |  |  |     return !!(c->picref); | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-20 16:23:25 +00:00
										 |  |  | AVFilter avfilter_vsrc_buffer = { | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  |     .name      = "buffer", | 
					
						
							| 
									
										
										
										
											2010-07-21 17:14:08 +00:00
										 |  |  |     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."), | 
					
						
							| 
									
										
										
										
											2010-05-07 01:25:00 +00:00
										 |  |  |     .priv_size = sizeof(BufferSourceContext), | 
					
						
							|  |  |  |     .query_formats = query_formats, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     .init      = init, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     .inputs    = (AVFilterPad[]) {{ .name = NULL }}, | 
					
						
							|  |  |  |     .outputs   = (AVFilterPad[]) {{ .name            = "default", | 
					
						
							|  |  |  |                                     .type            = AVMEDIA_TYPE_VIDEO, | 
					
						
							|  |  |  |                                     .request_frame   = request_frame, | 
					
						
							|  |  |  |                                     .poll_frame      = poll_frame, | 
					
						
							|  |  |  |                                     .config_props    = config_props, }, | 
					
						
							|  |  |  |                                   { .name = NULL}}, | 
					
						
							|  |  |  | }; |