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

Generated on Wed Apr 11 2012 07:31:35 for FFmpeg by  doxygen 1.7.1