Rename AVFilterPicRef to AVFilterBufferRef.
[ffmpeg.git] / libavfilter / avfilter.c
index 283fd0a..bef0dd6 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+/* #define DEBUG */
+
 #include "libavcodec/imgconvert.h"
+#include "libavutil/pixdesc.h"
 #include "avfilter.h"
+#include "internal.h"
 
 unsigned avfilter_version(void) {
     return LIBAVFILTER_VERSION_INT;
 }
 
-/** list of registered filters */
-struct FilterList
+const char *avfilter_configuration(void)
+{
+    return FFMPEG_CONFIGURATION;
+}
+
+const char *avfilter_license(void)
 {
-    AVFilter *filter;
-    struct FilterList *next;
-} *filters = NULL;
+#define LICENSE_PREFIX "libavfilter license: "
+    return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
+}
 
 /** helper macros to get the in/out pad on the dst/src filter */
 #define link_dpad(link)     link->dst-> input_pads[link->dstpad]
 #define link_spad(link)     link->src->output_pads[link->srcpad]
 
-AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
+AVFilterBufferRef *avfilter_ref_pic(AVFilterBufferRef *ref, int pmask)
 {
-    AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
+    AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
     *ret = *ref;
     ret->perms &= pmask;
     ret->pic->refcount ++;
     return ret;
 }
 
-void avfilter_unref_pic(AVFilterPicRef *ref)
+void avfilter_unref_pic(AVFilterBufferRef *ref)
 {
     if(!(--ref->pic->refcount))
         ref->pic->free(ref->pic);
@@ -90,7 +98,9 @@ int avfilter_link(AVFilterContext *src, unsigned srcpad,
     link->dst     = dst;
     link->srcpad  = srcpad;
     link->dstpad  = dstpad;
-    link->format  = PIX_FMT_NONE;
+    link->type    = src->output_pads[srcpad].type;
+    assert(PIX_FMT_NONE == -1 && SAMPLE_FMT_NONE == -1);
+    link->format  = -1;
 
     return 0;
 }
@@ -98,8 +108,9 @@ int avfilter_link(AVFilterContext *src, unsigned srcpad,
 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
                            unsigned in, unsigned out)
 {
-    av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s'\n",
-            filt->filter->name);
+    av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
+           "between the filter '%s' and the filter '%s'\n",
+           filt->name, link->src->name, link->dst->name);
 
     link->dst->inputs[link->dstpad] = NULL;
     if(avfilter_link(filt, out, link->dst, link->dstpad)) {
@@ -113,7 +124,7 @@ int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
     link->dstpad = in;
     filt->inputs[in] = link;
 
-    /* if any information on supported colorspaces already exists on the
+    /* if any information on supported media formats already exists on the
      * link, we need to preserve that */
     if(link->out_formats)
         avfilter_formats_changeref(&link->out_formats,
@@ -160,21 +171,50 @@ int avfilter_config_links(AVFilterContext *filter)
     return 0;
 }
 
-AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
+void ff_dprintf_picref(void *ctx, AVFilterBufferRef *picref, int end)
+{
+    dprintf(ctx,
+            "picref[%p data[%p, %p, %p, %p] linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64" a:%d/%d s:%dx%d]%s",
+            picref,
+            picref->data    [0], picref->data    [1], picref->data    [2], picref->data    [3],
+            picref->linesize[0], picref->linesize[1], picref->linesize[2], picref->linesize[3],
+            picref->pts, picref->pos,
+            picref->pixel_aspect.num, picref->pixel_aspect.den, picref->w, picref->h,
+            end ? "\n" : "");
+}
+
+void ff_dprintf_link(void *ctx, AVFilterLink *link, int end)
 {
-    AVFilterPicRef *ret = NULL;
+    dprintf(ctx,
+            "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
+            link, link->w, link->h,
+            av_pix_fmt_descriptors[link->format].name,
+            link->src ? link->src->filter->name : "",
+            link->dst ? link->dst->filter->name : "",
+            end ? "\n" : "");
+}
+
+AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
+{
+    AVFilterBufferRef *ret = NULL;
+
+    FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " perms:%d w:%d h:%d\n", perms, w, h);
 
     if(link_dpad(link).get_video_buffer)
-        ret = link_dpad(link).get_video_buffer(link, perms);
+        ret = link_dpad(link).get_video_buffer(link, perms, w, h);
 
     if(!ret)
-        ret = avfilter_default_get_video_buffer(link, perms);
+        ret = avfilter_default_get_video_buffer(link, perms, w, h);
+
+    FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " returning "); ff_dprintf_picref(NULL, ret, 1);
 
     return ret;
 }
 
 int avfilter_request_frame(AVFilterLink *link)
 {
+    FF_DPRINTF_START(NULL, request_frame); ff_dprintf_link(NULL, link, 1);
+
     if(link_spad(link).request_frame)
         return link_spad(link).request_frame(link);
     else if(link->src->inputs[0])
@@ -190,9 +230,11 @@ int avfilter_poll_frame(AVFilterLink *link)
         return link_spad(link).poll_frame(link);
 
     for (i=0; i<link->src->input_count; i++) {
+        int val;
         if(!link->src->inputs[i])
             return -1;
-        min = FFMIN(min, avfilter_poll_frame(link->src->inputs[i]));
+        val = avfilter_poll_frame(link->src->inputs[i]);
+        min = FFMIN(min, val);
     }
 
     return min;
@@ -200,11 +242,13 @@ int avfilter_poll_frame(AVFilterLink *link)
 
 /* XXX: should we do the duplicating of the picture ref here, instead of
  * forcing the source filter to do it? */
-void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
+void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
 {
-    void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
+    void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
     AVFilterPad *dst = &link_dpad(link);
 
+    FF_DPRINTF_START(NULL, start_frame); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " "); ff_dprintf_picref(NULL, picref, 1);
+
     if(!(start_frame = dst->start_frame))
         start_frame = avfilter_default_start_frame;
 
@@ -218,9 +262,9 @@ void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
                 link_dpad(link).min_perms, link_dpad(link).rej_perms);
         */
 
-        link->cur_pic = avfilter_default_get_video_buffer(link, dst->min_perms);
+        link->cur_pic = avfilter_default_get_video_buffer(link, dst->min_perms, link->w, link->h);
         link->srcpic = picref;
-        link->cur_pic->pts = link->srcpic->pts;
+        avfilter_copy_picref_props(link->cur_pic, link->srcpic);
     }
     else
         link->cur_pic = picref;
@@ -246,15 +290,17 @@ void avfilter_end_frame(AVFilterLink *link)
 
 }
 
-void avfilter_draw_slice(AVFilterLink *link, int y, int h)
+void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
 {
     uint8_t *src[4], *dst[4];
-    int i, j, hsub, vsub;
-    void (*draw_slice)(AVFilterLink *, int, int);
+    int i, j, vsub;
+    void (*draw_slice)(AVFilterLink *, int, int, int);
+
+    FF_DPRINTF_START(NULL, draw_slice); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
 
     /* copy the slice if needed for permission reasons */
     if(link->srcpic) {
-        avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
+        vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
 
         for(i = 0; i < 4; i ++) {
             if(link->srcpic->data[i]) {
@@ -282,37 +328,44 @@ void avfilter_draw_slice(AVFilterLink *link, int y, int h)
 
     if(!(draw_slice = link_dpad(link).draw_slice))
         draw_slice = avfilter_default_draw_slice;
-    draw_slice(link, y, h);
+    draw_slice(link, y, h, slice_dir);
 }
 
+#define MAX_REGISTERED_AVFILTERS_NB 64
+
+static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
+
+static int next_registered_avfilter_idx = 0;
+
 AVFilter *avfilter_get_by_name(const char *name)
 {
-    struct FilterList *filt;
+    int i;
 
-    for(filt = filters; filt; filt = filt->next)
-        if(!strcmp(filt->filter->name, name))
-            return filt->filter;
+    for (i = 0; registered_avfilters[i]; i++)
+        if (!strcmp(registered_avfilters[i]->name, name))
+            return registered_avfilters[i];
 
     return NULL;
 }
 
-void avfilter_register(AVFilter *filter)
+int avfilter_register(AVFilter *filter)
 {
-    struct FilterList *newfilt = av_malloc(sizeof(struct FilterList));
+    if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
+        return -1;
 
-    newfilt->filter = filter;
-    newfilt->next   = filters;
-    filters         = newfilt;
+    registered_avfilters[next_registered_avfilter_idx++] = filter;
+    return 0;
 }
 
-void avfilter_uninit(void)
+AVFilter **av_filter_next(AVFilter **filter)
 {
-    struct FilterList *tmp;
+    return filter ? ++filter : &registered_avfilters[0];
+}
 
-    for(; filters; filters = tmp) {
-        tmp = filters->next;
-        av_free(filters);
-    }
+void avfilter_uninit(void)
+{
+    memset(registered_avfilters, 0, sizeof(registered_avfilters));
+    next_registered_avfilter_idx = 0;
 }
 
 static int pad_count(const AVFilterPad *pads)
@@ -331,7 +384,9 @@ static const char *filter_name(void *p)
 
 static const AVClass avfilter_class = {
     "AVFilter",
-    filter_name
+    filter_name,
+    NULL,
+    LIBAVUTIL_VERSION_INT,
 };
 
 AVFilterContext *avfilter_open(AVFilter *filter, const char *inst_name)
@@ -341,7 +396,7 @@ AVFilterContext *avfilter_open(AVFilter *filter, const char *inst_name)
     if (!filter)
         return 0;
 
-    ret = av_malloc(sizeof(AVFilterContext));
+    ret = av_mallocz(sizeof(AVFilterContext));
 
     ret->av_class = &avfilter_class;
     ret->filter   = filter;
@@ -349,14 +404,18 @@ AVFilterContext *avfilter_open(AVFilter *filter, const char *inst_name)
     ret->priv     = av_mallocz(filter->priv_size);
 
     ret->input_count  = pad_count(filter->inputs);
-    ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
-    memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
-    ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
+    if (ret->input_count) {
+        ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
+        memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
+        ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
+    }
 
     ret->output_count = pad_count(filter->outputs);
-    ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
-    memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
-    ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
+    if (ret->output_count) {
+        ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
+        memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
+        ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
+    }
 
     return ret;
 }
@@ -369,13 +428,21 @@ void avfilter_destroy(AVFilterContext *filter)
         filter->filter->uninit(filter);
 
     for(i = 0; i < filter->input_count; i ++) {
-        if(filter->inputs[i])
-            filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
+        if(filter->inputs[i]) {
+            if (filter->inputs[i]->src)
+                filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
+            avfilter_formats_unref(&filter->inputs[i]->in_formats);
+            avfilter_formats_unref(&filter->inputs[i]->out_formats);
+        }
         av_freep(&filter->inputs[i]);
     }
     for(i = 0; i < filter->output_count; i ++) {
-        if(filter->outputs[i])
-            filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
+        if(filter->outputs[i]) {
+            if (filter->outputs[i]->dst)
+                filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
+            avfilter_formats_unref(&filter->outputs[i]->in_formats);
+            avfilter_formats_unref(&filter->outputs[i]->out_formats);
+        }
         av_freep(&filter->outputs[i]);
     }