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