• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavfilter/avfilter.c

Go to the documentation of this file.
00001 /*
00002  * filter layer
00003  * Copyright (c) 2007 Bobby Bingham
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 /* #define DEBUG */
00023 
00024 #include "libavutil/pixdesc.h"
00025 #include "libavutil/rational.h"
00026 #include "libavutil/audioconvert.h"
00027 #include "libavutil/imgutils.h"
00028 #include "libavcodec/avcodec.h"
00029 #include "avfilter.h"
00030 #include "internal.h"
00031 
00032 unsigned avfilter_version(void) {
00033     return LIBAVFILTER_VERSION_INT;
00034 }
00035 
00036 const char *avfilter_configuration(void)
00037 {
00038     return LIBAV_CONFIGURATION;
00039 }
00040 
00041 const char *avfilter_license(void)
00042 {
00043 #define LICENSE_PREFIX "libavfilter license: "
00044     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
00045 }
00046 
00047 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
00048 {
00049     AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
00050     if (!ret)
00051         return NULL;
00052     *ret = *ref;
00053     if (ref->type == AVMEDIA_TYPE_VIDEO) {
00054         ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
00055         if (!ret->video) {
00056             av_free(ret);
00057             return NULL;
00058         }
00059         *ret->video = *ref->video;
00060     } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
00061         ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
00062         if (!ret->audio) {
00063             av_free(ret);
00064             return NULL;
00065         }
00066         *ret->audio = *ref->audio;
00067     }
00068     ret->perms &= pmask;
00069     ret->buf->refcount ++;
00070     return ret;
00071 }
00072 
00073 void avfilter_unref_buffer(AVFilterBufferRef *ref)
00074 {
00075     if (!ref)
00076         return;
00077     if (!(--ref->buf->refcount))
00078         ref->buf->free(ref->buf);
00079     av_free(ref->video);
00080     av_free(ref->audio);
00081     av_free(ref);
00082 }
00083 
00084 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
00085                          AVFilterPad **pads, AVFilterLink ***links,
00086                          AVFilterPad *newpad)
00087 {
00088     unsigned i;
00089 
00090     idx = FFMIN(idx, *count);
00091 
00092     *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
00093     *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
00094     memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
00095     memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
00096     memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
00097     (*links)[idx] = NULL;
00098 
00099     (*count)++;
00100     for (i = idx+1; i < *count; i++)
00101         if (*links[i])
00102             (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
00103 }
00104 
00105 int avfilter_link(AVFilterContext *src, unsigned srcpad,
00106                   AVFilterContext *dst, unsigned dstpad)
00107 {
00108     AVFilterLink *link;
00109 
00110     if (src->output_count <= srcpad || dst->input_count <= dstpad ||
00111         src->outputs[srcpad]        || dst->inputs[dstpad])
00112         return -1;
00113 
00114     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
00115         av_log(src, AV_LOG_ERROR,
00116                "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
00117                src->name, srcpad, dst->name, dstpad);
00118         return AVERROR(EINVAL);
00119     }
00120 
00121     src->outputs[srcpad] =
00122     dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
00123 
00124     link->src     = src;
00125     link->dst     = dst;
00126     link->srcpad  = &src->output_pads[srcpad];
00127     link->dstpad  = &dst->input_pads[dstpad];
00128     link->type    = src->output_pads[srcpad].type;
00129     assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
00130     link->format  = -1;
00131 
00132     return 0;
00133 }
00134 
00135 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
00136                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
00137 {
00138     int ret;
00139     unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
00140 
00141     av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
00142            "between the filter '%s' and the filter '%s'\n",
00143            filt->name, link->src->name, link->dst->name);
00144 
00145     link->dst->inputs[dstpad_idx] = NULL;
00146     if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
00147         /* failed to link output filter to new filter */
00148         link->dst->inputs[dstpad_idx] = link;
00149         return ret;
00150     }
00151 
00152     /* re-hookup the link to the new destination filter we inserted */
00153     link->dst = filt;
00154     link->dstpad = &filt->input_pads[filt_srcpad_idx];
00155     filt->inputs[filt_srcpad_idx] = link;
00156 
00157     /* if any information on supported media formats already exists on the
00158      * link, we need to preserve that */
00159     if (link->out_formats)
00160         avfilter_formats_changeref(&link->out_formats,
00161                                    &filt->outputs[filt_dstpad_idx]->out_formats);
00162 
00163     return 0;
00164 }
00165 
00166 int avfilter_config_links(AVFilterContext *filter)
00167 {
00168     int (*config_link)(AVFilterLink *);
00169     unsigned i;
00170     int ret;
00171 
00172     for (i = 0; i < filter->input_count; i ++) {
00173         AVFilterLink *link = filter->inputs[i];
00174 
00175         if (!link) continue;
00176 
00177         switch (link->init_state) {
00178         case AVLINK_INIT:
00179             continue;
00180         case AVLINK_STARTINIT:
00181             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
00182             return 0;
00183         case AVLINK_UNINIT:
00184             link->init_state = AVLINK_STARTINIT;
00185 
00186             if ((ret = avfilter_config_links(link->src)) < 0)
00187                 return ret;
00188 
00189             if (!(config_link = link->srcpad->config_props))
00190                 config_link  = avfilter_default_config_output_link;
00191             if ((ret = config_link(link)) < 0)
00192                 return ret;
00193 
00194             if (link->time_base.num == 0 && link->time_base.den == 0)
00195                 link->time_base = link->src && link->src->input_count ?
00196                     link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
00197 
00198             if (link->sample_aspect_ratio.num == 0 && link->sample_aspect_ratio.den == 0)
00199                 link->sample_aspect_ratio = link->src->input_count ?
00200                     link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
00201 
00202             if (link->sample_rate == 0 && link->src && link->src->input_count)
00203                 link->sample_rate = link->src->inputs[0]->sample_rate;
00204 
00205             if (link->channel_layout == 0 && link->src && link->src->input_count)
00206                 link->channel_layout = link->src->inputs[0]->channel_layout;
00207 
00208             if ((config_link = link->dstpad->config_props))
00209                 if ((ret = config_link(link)) < 0)
00210                     return ret;
00211 
00212             link->init_state = AVLINK_INIT;
00213         }
00214     }
00215 
00216     return 0;
00217 }
00218 
00219 #ifdef DEBUG
00220 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
00221 {
00222     snprintf(buf, buf_size, "%s%s%s%s%s%s",
00223              perms & AV_PERM_READ      ? "r" : "",
00224              perms & AV_PERM_WRITE     ? "w" : "",
00225              perms & AV_PERM_PRESERVE  ? "p" : "",
00226              perms & AV_PERM_REUSE     ? "u" : "",
00227              perms & AV_PERM_REUSE2    ? "U" : "",
00228              perms & AV_PERM_NEG_LINESIZES ? "n" : "");
00229     return buf;
00230 }
00231 #endif
00232 
00233 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
00234 {
00235     av_unused char buf[16];
00236     av_dlog(ctx,
00237             "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
00238             ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
00239             ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
00240             ref->pts, ref->pos);
00241 
00242     if (ref->video) {
00243         av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
00244                 ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
00245                 ref->video->w, ref->video->h,
00246                 !ref->video->interlaced     ? 'P' :         /* Progressive  */
00247                 ref->video->top_field_first ? 'T' : 'B',    /* Top / Bottom */
00248                 ref->video->key_frame,
00249                 av_get_picture_type_char(ref->video->pict_type));
00250     }
00251     if (ref->audio) {
00252         av_dlog(ctx, " cl:%"PRId64"d sn:%d s:%d sr:%d p:%d",
00253                 ref->audio->channel_layout,
00254                 ref->audio->nb_samples,
00255                 ref->audio->size,
00256                 ref->audio->sample_rate,
00257                 ref->audio->planar);
00258     }
00259 
00260     av_dlog(ctx, "]%s", end ? "\n" : "");
00261 }
00262 
00263 static void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
00264 {
00265     if (link->type == AVMEDIA_TYPE_VIDEO) {
00266         av_dlog(ctx,
00267                 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
00268                 link, link->w, link->h,
00269                 av_pix_fmt_descriptors[link->format].name,
00270                 link->src ? link->src->filter->name : "",
00271                 link->dst ? link->dst->filter->name : "",
00272                 end ? "\n" : "");
00273     } else {
00274         char buf[128];
00275         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
00276 
00277         av_dlog(ctx,
00278                 "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
00279                 link, link->sample_rate, buf,
00280                 av_get_sample_fmt_name(link->format),
00281                 link->src ? link->src->filter->name : "",
00282                 link->dst ? link->dst->filter->name : "",
00283                 end ? "\n" : "");
00284     }
00285 }
00286 
00287 #define FF_DPRINTF_START(ctx, func) av_dlog(NULL, "%-16s: ", #func)
00288 
00289 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
00290 {
00291     AVFilterBufferRef *ret = NULL;
00292 
00293     av_unused char buf[16];
00294     FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
00295     av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
00296 
00297     if (link->dstpad->get_video_buffer)
00298         ret = link->dstpad->get_video_buffer(link, perms, w, h);
00299 
00300     if (!ret)
00301         ret = avfilter_default_get_video_buffer(link, perms, w, h);
00302 
00303     if (ret)
00304         ret->type = AVMEDIA_TYPE_VIDEO;
00305 
00306     FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
00307 
00308     return ret;
00309 }
00310 
00311 AVFilterBufferRef *
00312 avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
00313                                           int w, int h, enum PixelFormat format)
00314 {
00315     AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
00316     AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
00317 
00318     if (!pic || !picref)
00319         goto fail;
00320 
00321     picref->buf = pic;
00322     picref->buf->free = ff_avfilter_default_free_buffer;
00323     if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
00324         goto fail;
00325 
00326     pic->w = picref->video->w = w;
00327     pic->h = picref->video->h = h;
00328 
00329     /* make sure the buffer gets read permission or it's useless for output */
00330     picref->perms = perms | AV_PERM_READ;
00331 
00332     pic->refcount = 1;
00333     picref->type = AVMEDIA_TYPE_VIDEO;
00334     pic->format = picref->format = format;
00335 
00336     memcpy(pic->data,        data,          4*sizeof(data[0]));
00337     memcpy(pic->linesize,    linesize,      4*sizeof(linesize[0]));
00338     memcpy(picref->data,     pic->data,     sizeof(picref->data));
00339     memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
00340 
00341     return picref;
00342 
00343 fail:
00344     if (picref && picref->video)
00345         av_free(picref->video);
00346     av_free(picref);
00347     av_free(pic);
00348     return NULL;
00349 }
00350 
00351 AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
00352                                              enum AVSampleFormat sample_fmt, int size,
00353                                              uint64_t channel_layout, int planar)
00354 {
00355     AVFilterBufferRef *ret = NULL;
00356 
00357     if (link->dstpad->get_audio_buffer)
00358         ret = link->dstpad->get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
00359 
00360     if (!ret)
00361         ret = avfilter_default_get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
00362 
00363     if (ret)
00364         ret->type = AVMEDIA_TYPE_AUDIO;
00365 
00366     return ret;
00367 }
00368 
00369 int avfilter_request_frame(AVFilterLink *link)
00370 {
00371     FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
00372 
00373     if (link->srcpad->request_frame)
00374         return link->srcpad->request_frame(link);
00375     else if (link->src->inputs[0])
00376         return avfilter_request_frame(link->src->inputs[0]);
00377     else return -1;
00378 }
00379 
00380 int avfilter_poll_frame(AVFilterLink *link)
00381 {
00382     int i, min = INT_MAX;
00383 
00384     if (link->srcpad->poll_frame)
00385         return link->srcpad->poll_frame(link);
00386 
00387     for (i = 0; i < link->src->input_count; i++) {
00388         int val;
00389         if (!link->src->inputs[i])
00390             return -1;
00391         val = avfilter_poll_frame(link->src->inputs[i]);
00392         min = FFMIN(min, val);
00393     }
00394 
00395     return min;
00396 }
00397 
00398 /* XXX: should we do the duplicating of the picture ref here, instead of
00399  * forcing the source filter to do it? */
00400 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00401 {
00402     void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
00403     AVFilterPad *dst = link->dstpad;
00404     int perms = picref->perms;
00405 
00406     FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
00407 
00408     if (!(start_frame = dst->start_frame))
00409         start_frame = avfilter_default_start_frame;
00410 
00411     if (picref->linesize[0] < 0)
00412         perms |= AV_PERM_NEG_LINESIZES;
00413     /* prepare to copy the picture if it has insufficient permissions */
00414     if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
00415         av_log(link->dst, AV_LOG_DEBUG,
00416                 "frame copy needed (have perms %x, need %x, reject %x)\n",
00417                 picref->perms,
00418                 link->dstpad->min_perms, link->dstpad->rej_perms);
00419 
00420         link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
00421         link->src_buf = picref;
00422         avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
00423     }
00424     else
00425         link->cur_buf = picref;
00426 
00427     start_frame(link, link->cur_buf);
00428 }
00429 
00430 void avfilter_end_frame(AVFilterLink *link)
00431 {
00432     void (*end_frame)(AVFilterLink *);
00433 
00434     if (!(end_frame = link->dstpad->end_frame))
00435         end_frame = avfilter_default_end_frame;
00436 
00437     end_frame(link);
00438 
00439     /* unreference the source picture if we're feeding the destination filter
00440      * a copied version dues to permission issues */
00441     if (link->src_buf) {
00442         avfilter_unref_buffer(link->src_buf);
00443         link->src_buf = NULL;
00444     }
00445 }
00446 
00447 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00448 {
00449     uint8_t *src[4], *dst[4];
00450     int i, j, vsub;
00451     void (*draw_slice)(AVFilterLink *, int, int, int);
00452 
00453     FF_DPRINTF_START(NULL, draw_slice); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
00454 
00455     /* copy the slice if needed for permission reasons */
00456     if (link->src_buf) {
00457         vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
00458 
00459         for (i = 0; i < 4; i++) {
00460             if (link->src_buf->data[i]) {
00461                 src[i] = link->src_buf-> data[i] +
00462                     (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
00463                 dst[i] = link->cur_buf->data[i] +
00464                     (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
00465             } else
00466                 src[i] = dst[i] = NULL;
00467         }
00468 
00469         for (i = 0; i < 4; i++) {
00470             int planew =
00471                 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
00472 
00473             if (!src[i]) continue;
00474 
00475             for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
00476                 memcpy(dst[i], src[i], planew);
00477                 src[i] += link->src_buf->linesize[i];
00478                 dst[i] += link->cur_buf->linesize[i];
00479             }
00480         }
00481     }
00482 
00483     if (!(draw_slice = link->dstpad->draw_slice))
00484         draw_slice = avfilter_default_draw_slice;
00485     draw_slice(link, y, h, slice_dir);
00486 }
00487 
00488 void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
00489 {
00490     void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
00491     AVFilterPad *dst = link->dstpad;
00492 
00493     FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
00494 
00495     if (!(filter_samples = dst->filter_samples))
00496         filter_samples = avfilter_default_filter_samples;
00497 
00498     /* prepare to copy the samples if the buffer has insufficient permissions */
00499     if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
00500         dst->rej_perms & samplesref->perms) {
00501 
00502         av_log(link->dst, AV_LOG_DEBUG,
00503                "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
00504                samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
00505 
00506         link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
00507                                                           samplesref->format,
00508                                                           samplesref->audio->size,
00509                                                           samplesref->audio->channel_layout,
00510                                                           samplesref->audio->planar);
00511         link->cur_buf->pts                = samplesref->pts;
00512         link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
00513 
00514         /* Copy actual data into new samples buffer */
00515         memcpy(link->cur_buf->data[0], samplesref->data[0], samplesref->audio->size);
00516 
00517         avfilter_unref_buffer(samplesref);
00518     } else
00519         link->cur_buf = samplesref;
00520 
00521     filter_samples(link, link->cur_buf);
00522 }
00523 
00524 #define MAX_REGISTERED_AVFILTERS_NB 64
00525 
00526 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
00527 
00528 static int next_registered_avfilter_idx = 0;
00529 
00530 AVFilter *avfilter_get_by_name(const char *name)
00531 {
00532     int i;
00533 
00534     for (i = 0; registered_avfilters[i]; i++)
00535         if (!strcmp(registered_avfilters[i]->name, name))
00536             return registered_avfilters[i];
00537 
00538     return NULL;
00539 }
00540 
00541 int avfilter_register(AVFilter *filter)
00542 {
00543     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
00544         return -1;
00545 
00546     registered_avfilters[next_registered_avfilter_idx++] = filter;
00547     return 0;
00548 }
00549 
00550 AVFilter **av_filter_next(AVFilter **filter)
00551 {
00552     return filter ? ++filter : &registered_avfilters[0];
00553 }
00554 
00555 void avfilter_uninit(void)
00556 {
00557     memset(registered_avfilters, 0, sizeof(registered_avfilters));
00558     next_registered_avfilter_idx = 0;
00559 }
00560 
00561 static int pad_count(const AVFilterPad *pads)
00562 {
00563     int count;
00564 
00565     for(count = 0; pads->name; count ++) pads ++;
00566     return count;
00567 }
00568 
00569 static const char *filter_name(void *p)
00570 {
00571     AVFilterContext *filter = p;
00572     return filter->filter->name;
00573 }
00574 
00575 static const AVClass avfilter_class = {
00576     "AVFilter",
00577     filter_name,
00578     NULL,
00579     LIBAVUTIL_VERSION_INT,
00580 };
00581 
00582 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
00583 {
00584     AVFilterContext *ret;
00585     *filter_ctx = NULL;
00586 
00587     if (!filter)
00588         return AVERROR(EINVAL);
00589 
00590     ret = av_mallocz(sizeof(AVFilterContext));
00591     if (!ret)
00592         return AVERROR(ENOMEM);
00593 
00594     ret->av_class = &avfilter_class;
00595     ret->filter   = filter;
00596     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
00597     if (filter->priv_size) {
00598         ret->priv     = av_mallocz(filter->priv_size);
00599         if (!ret->priv)
00600             goto err;
00601     }
00602 
00603     ret->input_count  = pad_count(filter->inputs);
00604     if (ret->input_count) {
00605         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
00606         if (!ret->input_pads)
00607             goto err;
00608         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
00609         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
00610         if (!ret->inputs)
00611             goto err;
00612     }
00613 
00614     ret->output_count = pad_count(filter->outputs);
00615     if (ret->output_count) {
00616         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
00617         if (!ret->output_pads)
00618             goto err;
00619         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
00620         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
00621         if (!ret->outputs)
00622             goto err;
00623     }
00624 
00625     *filter_ctx = ret;
00626     return 0;
00627 
00628 err:
00629     av_freep(&ret->inputs);
00630     av_freep(&ret->input_pads);
00631     ret->input_count = 0;
00632     av_freep(&ret->outputs);
00633     av_freep(&ret->output_pads);
00634     ret->output_count = 0;
00635     av_freep(&ret->priv);
00636     av_free(ret);
00637     return AVERROR(ENOMEM);
00638 }
00639 
00640 void avfilter_free(AVFilterContext *filter)
00641 {
00642     int i;
00643     AVFilterLink *link;
00644 
00645     if (filter->filter->uninit)
00646         filter->filter->uninit(filter);
00647 
00648     for (i = 0; i < filter->input_count; i++) {
00649         if ((link = filter->inputs[i])) {
00650             if (link->src)
00651                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
00652             avfilter_formats_unref(&link->in_formats);
00653             avfilter_formats_unref(&link->out_formats);
00654         }
00655         av_freep(&link);
00656     }
00657     for (i = 0; i < filter->output_count; i++) {
00658         if ((link = filter->outputs[i])) {
00659             if (link->dst)
00660                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
00661             avfilter_formats_unref(&link->in_formats);
00662             avfilter_formats_unref(&link->out_formats);
00663         }
00664         av_freep(&link);
00665     }
00666 
00667     av_freep(&filter->name);
00668     av_freep(&filter->input_pads);
00669     av_freep(&filter->output_pads);
00670     av_freep(&filter->inputs);
00671     av_freep(&filter->outputs);
00672     av_freep(&filter->priv);
00673     av_free(filter);
00674 }
00675 
00676 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
00677 {
00678     int ret=0;
00679 
00680     if (filter->filter->init)
00681         ret = filter->filter->init(filter, args, opaque);
00682     return ret;
00683 }
00684 
00685 int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
00686 {
00687     if (dst->type != AVMEDIA_TYPE_VIDEO)
00688         return AVERROR(EINVAL);
00689 
00690     dst->pts    = src->pts;
00691     dst->format = src->format;
00692 
00693     dst->video->w                   = src->width;
00694     dst->video->h                   = src->height;
00695     dst->video->pixel_aspect        = src->sample_aspect_ratio;
00696     dst->video->interlaced          = src->interlaced_frame;
00697     dst->video->top_field_first     = src->top_field_first;
00698     dst->video->key_frame           = src->key_frame;
00699     dst->video->pict_type           = src->pict_type;
00700 
00701     return 0;
00702 }
Generated on Thu Jul 11 2013 15:38:23 for Libav by doxygen 1.7.1