Libav
|
00001 /* 00002 * Filter layer - default implementations 00003 * copyright (c) 2007 Bobby Bingham 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include "libavcodec/imgconvert.h" 00023 #include "avfilter.h" 00024 00025 /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */ 00026 static void avfilter_default_free_video_buffer(AVFilterPic *pic) 00027 { 00028 av_free(pic->data[0]); 00029 av_free(pic); 00030 } 00031 00032 /* TODO: set the buffer's priv member to a context structure for the whole 00033 * filter chain. This will allow for a buffer pool instead of the constant 00034 * alloc & free cycle currently implemented. */ 00035 AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h) 00036 { 00037 AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic)); 00038 AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef)); 00039 int i, tempsize; 00040 char *buf; 00041 00042 ref->pic = pic; 00043 ref->w = pic->w = w; 00044 ref->h = pic->h = h; 00045 00046 /* make sure the buffer gets read permission or it's useless for output */ 00047 ref->perms = perms | AV_PERM_READ; 00048 00049 pic->refcount = 1; 00050 pic->format = link->format; 00051 pic->free = avfilter_default_free_video_buffer; 00052 ff_fill_linesize((AVPicture *)pic, pic->format, ref->w); 00053 00054 for (i=0; i<4;i++) 00055 pic->linesize[i] = FFALIGN(pic->linesize[i], 16); 00056 00057 tempsize = ff_fill_pointer((AVPicture *)pic, NULL, pic->format, ref->h); 00058 buf = av_malloc(tempsize); 00059 ff_fill_pointer((AVPicture *)pic, buf, pic->format, ref->h); 00060 00061 memcpy(ref->data, pic->data, sizeof(pic->data)); 00062 memcpy(ref->linesize, pic->linesize, sizeof(pic->linesize)); 00063 00064 return ref; 00065 } 00066 00067 void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref) 00068 { 00069 AVFilterLink *out = NULL; 00070 00071 if(link->dst->output_count) 00072 out = link->dst->outputs[0]; 00073 00074 if(out) { 00075 out->outpic = avfilter_get_video_buffer(out, AV_PERM_WRITE, out->w, out->h); 00076 out->outpic->pts = picref->pts; 00077 out->outpic->pos = picref->pos; 00078 out->outpic->pixel_aspect = picref->pixel_aspect; 00079 avfilter_start_frame(out, avfilter_ref_pic(out->outpic, ~0)); 00080 } 00081 } 00082 00083 void avfilter_default_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) 00084 { 00085 AVFilterLink *out = NULL; 00086 00087 if(link->dst->output_count) 00088 out = link->dst->outputs[0]; 00089 00090 if(out) 00091 avfilter_draw_slice(out, y, h, slice_dir); 00092 } 00093 00094 void avfilter_default_end_frame(AVFilterLink *link) 00095 { 00096 AVFilterLink *out = NULL; 00097 00098 if(link->dst->output_count) 00099 out = link->dst->outputs[0]; 00100 00101 avfilter_unref_pic(link->cur_pic); 00102 link->cur_pic = NULL; 00103 00104 if(out) { 00105 if(out->outpic) { 00106 avfilter_unref_pic(out->outpic); 00107 out->outpic = NULL; 00108 } 00109 avfilter_end_frame(out); 00110 } 00111 } 00112 00116 int avfilter_default_config_output_link(AVFilterLink *link) 00117 { 00118 if(link->src->input_count && link->src->inputs[0]) { 00119 link->w = link->src->inputs[0]->w; 00120 link->h = link->src->inputs[0]->h; 00121 } else { 00122 /* XXX: any non-simple filter which would cause this branch to be taken 00123 * really should implement its own config_props() for this link. */ 00124 return -1; 00125 } 00126 00127 return 0; 00128 } 00129 00138 void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats) 00139 { 00140 int count = 0, i; 00141 00142 for(i = 0; i < ctx->input_count; i ++) { 00143 if(ctx->inputs[i]) { 00144 avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats); 00145 count ++; 00146 } 00147 } 00148 for(i = 0; i < ctx->output_count; i ++) { 00149 if(ctx->outputs[i]) { 00150 avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats); 00151 count ++; 00152 } 00153 } 00154 00155 if(!count) { 00156 av_free(formats->formats); 00157 av_free(formats->refs); 00158 av_free(formats); 00159 } 00160 } 00161 00162 int avfilter_default_query_formats(AVFilterContext *ctx) 00163 { 00164 avfilter_set_common_formats(ctx, avfilter_all_colorspaces()); 00165 return 0; 00166 } 00167 00168 void avfilter_null_start_frame(AVFilterLink *link, AVFilterPicRef *picref) 00169 { 00170 avfilter_start_frame(link->dst->outputs[0], picref); 00171 } 00172 00173 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) 00174 { 00175 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir); 00176 } 00177 00178 void avfilter_null_end_frame(AVFilterLink *link) 00179 { 00180 avfilter_end_frame(link->dst->outputs[0]); 00181 } 00182 00183 AVFilterPicRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h) 00184 { 00185 return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h); 00186 } 00187