00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavcodec/imgconvert.h"
00023 #include "avfilter.h"
00024
00025
00026 struct FilterList
00027 {
00028 AVFilter *filter;
00029 struct FilterList *next;
00030 } *filters = NULL;
00031
00032
00033 #define link_dpad(link) link->dst-> input_pads[link->dstpad]
00034 #define link_spad(link) link->src->output_pads[link->srcpad]
00035
00036 AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
00037 {
00038 AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
00039 *ret = *ref;
00040 ret->perms &= pmask;
00041 ret->pic->refcount ++;
00042 return ret;
00043 }
00044
00045 void avfilter_unref_pic(AVFilterPicRef *ref)
00046 {
00047 if(!(--ref->pic->refcount))
00048 ref->pic->free(ref->pic);
00049 av_free(ref);
00050 }
00051
00052 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
00053 AVFilterPad **pads, AVFilterLink ***links,
00054 AVFilterPad *newpad)
00055 {
00056 unsigned i;
00057
00058 idx = FFMIN(idx, *count);
00059
00060 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
00061 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
00062 memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
00063 memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
00064 memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
00065 (*links)[idx] = NULL;
00066
00067 (*count) ++;
00068 for(i = idx+1; i < *count; i ++)
00069 if(*links[i])
00070 (*(unsigned *)((uint8_t *) *links[i] + padidx_off)) ++;
00071 }
00072
00073 int avfilter_link(AVFilterContext *src, unsigned srcpad,
00074 AVFilterContext *dst, unsigned dstpad)
00075 {
00076 AVFilterLink *link;
00077
00078 if(src->output_count <= srcpad || dst->input_count <= dstpad ||
00079 src->outputs[srcpad] || dst->inputs[dstpad])
00080 return -1;
00081
00082 src->outputs[srcpad] =
00083 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
00084
00085 link->src = src;
00086 link->dst = dst;
00087 link->srcpad = srcpad;
00088 link->dstpad = dstpad;
00089 link->format = -1;
00090
00091 return 0;
00092 }
00093
00094 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
00095 unsigned in, unsigned out)
00096 {
00097 av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s'\n",
00098 filt->filter->name);
00099
00100 link->dst->inputs[link->dstpad] = NULL;
00101 if(avfilter_link(filt, out, link->dst, link->dstpad)) {
00102
00103 link->dst->inputs[link->dstpad] = link;
00104 return -1;
00105 }
00106
00107
00108 link->dst = filt;
00109 link->dstpad = in;
00110 filt->inputs[in] = link;
00111
00112
00113
00114 if(link->out_formats)
00115 avfilter_formats_changeref(&link->out_formats,
00116 &filt->outputs[out]->out_formats);
00117
00118 return 0;
00119 }
00120
00121 int avfilter_config_links(AVFilterContext *filter)
00122 {
00123 int (*config_link)(AVFilterLink *);
00124 unsigned i;
00125
00126 for(i = 0; i < filter->input_count; i ++) {
00127 AVFilterLink *link = filter->inputs[i];
00128
00129 if(!link) continue;
00130
00131 switch(link->init_state) {
00132 case AVLINK_INIT:
00133 continue;
00134 case AVLINK_STARTINIT:
00135 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
00136 return 0;
00137 case AVLINK_UNINIT:
00138 link->init_state = AVLINK_STARTINIT;
00139
00140 if(avfilter_config_links(link->src))
00141 return -1;
00142
00143 if(!(config_link = link_spad(link).config_props))
00144 config_link = avfilter_default_config_output_link;
00145 if(config_link(link))
00146 return -1;
00147
00148 if((config_link = link_dpad(link).config_props))
00149 if(config_link(link))
00150 return -1;
00151
00152 link->init_state = AVLINK_INIT;
00153 }
00154 }
00155
00156 return 0;
00157 }
00158
00159 AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
00160 {
00161 AVFilterPicRef *ret = NULL;
00162
00163 if(link_dpad(link).get_video_buffer)
00164 ret = link_dpad(link).get_video_buffer(link, perms);
00165
00166 if(!ret)
00167 ret = avfilter_default_get_video_buffer(link, perms);
00168
00169 return ret;
00170 }
00171
00172 int avfilter_request_frame(AVFilterLink *link)
00173 {
00174 if(link_spad(link).request_frame)
00175 return link_spad(link).request_frame(link);
00176 else if(link->src->inputs[0])
00177 return avfilter_request_frame(link->src->inputs[0]);
00178 else return -1;
00179 }
00180
00181 int avfilter_poll_frame(AVFilterLink *link)
00182 {
00183 int i, min=INT_MAX;
00184
00185 if(link_spad(link).poll_frame)
00186 return link_spad(link).poll_frame(link);
00187
00188 for (i=0; i<link->src->input_count; i++) {
00189 if(!link->src->inputs[i])
00190 return -1;
00191 min = FFMIN(min, avfilter_poll_frame(link->src->inputs[i]));
00192 }
00193
00194 return min;
00195 }
00196
00197
00198
00199 void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
00200 {
00201 void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
00202 AVFilterPad *dst = &link_dpad(link);
00203
00204 if(!(start_frame = dst->start_frame))
00205 start_frame = avfilter_default_start_frame;
00206
00207
00208 if((dst->min_perms & picref->perms) != dst->min_perms ||
00209 dst->rej_perms & picref->perms) {
00210
00211
00212
00213
00214
00215
00216
00217 link->cur_pic = avfilter_default_get_video_buffer(link, dst->min_perms);
00218 link->srcpic = picref;
00219 link->cur_pic->pts = link->srcpic->pts;
00220 }
00221 else
00222 link->cur_pic = picref;
00223
00224 start_frame(link, link->cur_pic);
00225 }
00226
00227 void avfilter_end_frame(AVFilterLink *link)
00228 {
00229 void (*end_frame)(AVFilterLink *);
00230
00231 if(!(end_frame = link_dpad(link).end_frame))
00232 end_frame = avfilter_default_end_frame;
00233
00234 end_frame(link);
00235
00236
00237
00238 if(link->srcpic) {
00239 avfilter_unref_pic(link->srcpic);
00240 link->srcpic = NULL;
00241 }
00242
00243 }
00244
00245 void avfilter_draw_slice(AVFilterLink *link, int y, int h)
00246 {
00247 uint8_t *src[4], *dst[4];
00248 int i, j, hsub, vsub;
00249
00250
00251 if(link->srcpic) {
00252 avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
00253
00254 for(i = 0; i < 4; i ++) {
00255 if(link->srcpic->data[i]) {
00256 src[i] = link->srcpic-> data[i] +
00257 (y >> (i==0 ? 0 : vsub)) * link->srcpic-> linesize[i];
00258 dst[i] = link->cur_pic->data[i] +
00259 (y >> (i==0 ? 0 : vsub)) * link->cur_pic->linesize[i];
00260 } else
00261 src[i] = dst[i] = NULL;
00262 }
00263
00264 for(i = 0; i < 4; i ++) {
00265 int planew =
00266 ff_get_plane_bytewidth(link->format, link->cur_pic->w, i);
00267
00268 if(!src[i]) continue;
00269
00270 for(j = 0; j < h >> (i==0 ? 0 : vsub); j ++) {
00271 memcpy(dst[i], src[i], planew);
00272 src[i] += link->srcpic ->linesize[i];
00273 dst[i] += link->cur_pic->linesize[i];
00274 }
00275 }
00276 }
00277
00278 if(link_dpad(link).draw_slice)
00279 link_dpad(link).draw_slice(link, y, h);
00280 }
00281
00282 AVFilter *avfilter_get_by_name(const char *name)
00283 {
00284 struct FilterList *filt;
00285
00286 for(filt = filters; filt; filt = filt->next)
00287 if(!strcmp(filt->filter->name, name))
00288 return filt->filter;
00289
00290 return NULL;
00291 }
00292
00293 void avfilter_register(AVFilter *filter)
00294 {
00295 struct FilterList *newfilt = av_malloc(sizeof(struct FilterList));
00296
00297 newfilt->filter = filter;
00298 newfilt->next = filters;
00299 filters = newfilt;
00300 }
00301
00302 void avfilter_uninit(void)
00303 {
00304 struct FilterList *tmp;
00305
00306 for(; filters; filters = tmp) {
00307 tmp = filters->next;
00308 av_free(filters);
00309 }
00310 }
00311
00312 static int pad_count(const AVFilterPad *pads)
00313 {
00314 int count;
00315
00316 for(count = 0; pads->name; count ++) pads ++;
00317 return count;
00318 }
00319
00320 static const char *filter_name(void *p)
00321 {
00322 AVFilterContext *filter = p;
00323 return filter->filter->name;
00324 }
00325
00326 static const AVClass avfilter_class = {
00327 "AVFilter",
00328 filter_name
00329 };
00330
00331 AVFilterContext *avfilter_open(AVFilter *filter, const char *inst_name)
00332 {
00333 AVFilterContext *ret;
00334
00335 if (!filter)
00336 return 0;
00337
00338 ret = av_malloc(sizeof(AVFilterContext));
00339
00340 ret->av_class = &avfilter_class;
00341 ret->filter = filter;
00342 ret->name = inst_name ? av_strdup(inst_name) : NULL;
00343 ret->priv = av_mallocz(filter->priv_size);
00344
00345 ret->input_count = pad_count(filter->inputs);
00346 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
00347 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
00348 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
00349
00350 ret->output_count = pad_count(filter->outputs);
00351 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
00352 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
00353 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
00354
00355 return ret;
00356 }
00357
00358 void avfilter_destroy(AVFilterContext *filter)
00359 {
00360 int i;
00361
00362 if(filter->filter->uninit)
00363 filter->filter->uninit(filter);
00364
00365 for(i = 0; i < filter->input_count; i ++) {
00366 if(filter->inputs[i])
00367 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
00368 av_freep(&filter->inputs[i]);
00369 }
00370 for(i = 0; i < filter->output_count; i ++) {
00371 if(filter->outputs[i])
00372 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
00373 av_freep(&filter->outputs[i]);
00374 }
00375
00376 av_freep(&filter->name);
00377 av_freep(&filter->input_pads);
00378 av_freep(&filter->output_pads);
00379 av_freep(&filter->inputs);
00380 av_freep(&filter->outputs);
00381 av_freep(&filter->priv);
00382 av_free(filter);
00383 }
00384
00385 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
00386 {
00387 int ret=0;
00388
00389 if(filter->filter->init)
00390 ret = filter->filter->init(filter, args, opaque);
00391 return ret;
00392 }
00393