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