[FFmpeg-devel] [PATCH 4/5] drawutils: new API.

Stefano Sabatini stefasab at gmail.com
Fri Mar 9 18:36:14 CET 2012


On date Thursday 2012-03-08 14:49:17 +0100, Nicolas George encoded:
> This new API stores useful data in a dedicated structure
> and has clearly delimited init functions.
> 
> Hopefully, uses of the old API can be replaced quickly.
> 
> Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
> ---
>  libavfilter/drawutils.c |  130 +++++++++++++++++++++++++++++++++++++++++++++++
>  libavfilter/drawutils.h |   58 +++++++++++++++++++++
>  2 files changed, 188 insertions(+), 0 deletions(-)
> 
> diff --git a/libavfilter/drawutils.c b/libavfilter/drawutils.c
> index bf308a1..584cfa6 100644
> --- a/libavfilter/drawutils.c
> +++ b/libavfilter/drawutils.c
> @@ -120,3 +120,133 @@ void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4],
>          }
>      }
>  }
> +
> +int ff_draw_init(FFDrawContext *draw, enum PixelFormat format, unsigned flags)
> +{
> +    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[format];
> +    const AVComponentDescriptor *c;
> +    unsigned i, nb_planes = 0;
> +    int pixelstep[MAX_PLANES] = { 0 };
> +
> +    if (desc->flags & ~(PIX_FMT_PLANAR | PIX_FMT_RGB))
> +        return AVERROR(ENOSYS);
> +    for (i = 0; i < desc->nb_components; i++) {
> +        c = &desc->comp[i];
> +        /* for now, only 8-bits formats */
> +        if (c->depth_minus1 != 8 - 1)
> +            return AVERROR(ENOSYS);
> +        if (c->plane >= MAX_PLANES)
> +            return AVERROR(ENOSYS);
> +        /* strange interleaving */
> +        if (pixelstep[c->plane] != 0 &&
> +            pixelstep[c->plane] != c->step_minus1 + 1)
> +            return AVERROR(ENOSYS);
> +        pixelstep[c->plane] = c->step_minus1 + 1;
> +        nb_planes = FFMAX(nb_planes, c->plane + 1);
> +    }
> +    if ((desc->log2_chroma_w || desc->log2_chroma_h) && nb_planes < 3)
> +        return AVERROR(ENOSYS); /* exclude NV12 and NV21 */
> +    memset(draw, 0, sizeof(*draw));
> +    draw->desc      = desc;
> +    draw->format    = format;
> +    draw->nb_planes = nb_planes;
> +    memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
> +    if (nb_planes >= 3 && !(desc->flags & PIX_FMT_RGB)) {
> +        draw->hsub[1] = draw->hsub[2] = desc->log2_chroma_w;
> +        draw->vsub[1] = draw->vsub[2] = desc->log2_chroma_h;
> +    }
> +    return 0;
> +}
> +

> +void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, uint8_t rgba[4])
> +{
> +    unsigned i;
> +    uint8_t rgba_map[4];
> +
> +    if ((draw->desc->flags & PIX_FMT_RGB) && draw->nb_planes == 1 &&
> +        ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
> +        for (i = 0; i < 4; i++)
> +            color->comp[0].u8[rgba_map[i]] = rgba[i];
> +    } else if (draw->nb_planes == 3 || draw->nb_planes == 4) {
> +        /* assume YUV */
> +        color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
> +        color->comp[1].u8[0] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
> +        color->comp[2].u8[0] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
> +        color->comp[3].u8[0] = rgba[3];
> +    } else {
> +        av_log(NULL, AV_LOG_WARNING,
> +               "Color conversion not implemented for %s\n", draw->desc->name);
> +        memset(color, 128, sizeof(*color));

uhmmm maybe we could create a special logging context for DrawContext,
drop the error message altogether and/or return an error code.

> +    }
> +}
> +

> +static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
> +                           int plane, int x, int y)
> +{
> +    return data[plane] +
> +           (y >> draw->vsub[plane]) * linesize[plane] +
> +           (x >> draw->hsub[plane]) * draw->pixelstep[plane];
> +}

maybe turn it to a macro or an inline function

> +
> +void ff_copy_rectangle2(FFDrawContext *draw,
> +                        uint8_t *dst[], int dst_linesize[],
> +                        uint8_t *src[], int src_linesize[], 
> +                        int dst_x, int dst_y, int src_x, int src_y,
> +                        int w, int h)
> +{
> +    int plane, y, wp, hp;
> +    uint8_t *p, *q;
> +
> +    for (plane = 0; plane < draw->nb_planes; plane++) {
> +        p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
> +        q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
> +        wp = (w >> draw->hsub[plane]) * draw->pixelstep[plane];
> +        hp = (h >> draw->vsub[plane]);
> +        for (y = 0; y < hp; y++) {
> +            memcpy(q, p, wp);
> +            p += src_linesize[plane];
> +            q += dst_linesize[plane];
> +        }
> +    }
> +}
> +
> +void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
> +                       uint8_t *dst[], int dst_linesize[],
> +                       int dst_x, int dst_y, int w, int h)
> +{
> +    int plane, x, y, wp, hp;
> +    uint8_t *p0, *p;
> +
> +    for (plane = 0; plane < draw->nb_planes; plane++) {
> +        p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
> +        wp = (w >> draw->hsub[plane]);
> +        hp = (h >> draw->vsub[plane]);
> +        p = p0;
> +        /* copy first line from color */
> +        for (x = 0; x < wp; x++) {
> +            memcpy(p, color->comp[plane].u8, draw->pixelstep[plane]);
> +            p += draw->pixelstep[plane];
> +        }
> +        wp *= draw->pixelstep[plane];
> +        /* copy next lines from first line */
> +        p = p0 + dst_linesize[plane];
> +        for (y = 1; y < hp; y++) {
> +            memcpy(p, p0, wp);
> +            p += dst_linesize[plane];
> +        }
> +    }
> +}
> +
> +AVFilterFormats *ff_draw_supported_pixel_formats(void)
> +{
> +    enum PixelFormat i, pix_fmts[PIX_FMT_NB + 1];
> +    unsigned n = 0;
> +    FFDrawContext draw;
> +
> +    for (i = 0; i < PIX_FMT_NB; i++)
> +        if (ff_draw_init(&draw, i, 0) >= 0)
> +            pix_fmts[n++] = i;
> +    pix_fmts[n++] = PIX_FMT_NONE;
> +    return avfilter_make_format_list(pix_fmts);
> +}
> +
> diff --git a/libavfilter/drawutils.h b/libavfilter/drawutils.h
> index 330b1cb..58448b8 100644
> --- a/libavfilter/drawutils.h
> +++ b/libavfilter/drawutils.h
> @@ -25,6 +25,7 @@
>   */
>  
>  #include <stdint.h>
> +#include "avfilter.h"
>  #include "libavutil/pixfmt.h"
>  
>  int ff_fill_rgba_map(uint8_t *rgba_map, enum PixelFormat pix_fmt);
> @@ -42,4 +43,61 @@ void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4],
>                         uint8_t *src[4], int src_linesize[4], int pixelstep[4],
>                         int hsub, int vsub, int x, int y, int y2, int w, int h);
>  
> +#define MAX_PLANES 4
> +
> +typedef struct FFDrawContext {
> +    const struct AVPixFmtDescriptor *desc;
> +    enum PixelFormat format;
> +    unsigned nb_planes;
> +    int pixelstep[MAX_PLANES]; /*< offset between pixels */
> +    uint8_t hsub[MAX_PLANES];  /*< horizontal subsamling */
> +    uint8_t vsub[MAX_PLANES];  /*< vertical subsamling */
> +} FFDrawContext;
> +
> +typedef struct FFDrawColor {
> +    union {
> +        uint32_t u32;
> +        uint16_t u16;
> +        uint8_t  u8[4];
> +    } comp[MAX_PLANES];
> +} FFDrawColor;
> +
> +/**
> + * Init a draw context.

Add something along the lines of:

Only a limited number of pixel formats is supported, if format is not
supported the function will return an error.

> + *
> + * No flags currently defined.
> + * @return  0 for success, < 0 for error
> + */
> +int ff_draw_init(FFDrawContext *draw, enum PixelFormat format, unsigned flags);
> +
> +/**
> + * Prepare a color.
> + */
> +void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, uint8_t rgba[4]);
> +
> +/**
> + * Copy a rectangle from an image to another.
> + *
> + * The coordinates must be as even as the subsampling requires.
> + */
> +void ff_copy_rectangle2(FFDrawContext *draw,
> +                        uint8_t *dst[], int dst_linesize[],
> +                        uint8_t *src[], int src_linesize[], 
> +                        int dst_x, int dst_y, int src_x, int src_y,
> +                        int w, int h);
> +

> +/**
> + * Copy a rectangle from an image to another.

copy&paste error

Mention how to "prepare" a color.

> + *
> + * The coordinates must be as even as the subsampling requires.

> + */
> +void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
> +                       uint8_t *dst[], int dst_linesize[],
> +                       int dst_x, int dst_y, int w, int h);
> +

> +/**
> + * Return the list of pixel formats supported by the draw functions.
> + */
> +AVFilterFormats *ff_draw_supported_pixel_formats(void);
> +
>  #endif /* AVFILTER_DRAWUTILS_H */

Looks fine otherwise, but testing this new API with FATE before to
commit it may help to spot eventual errors.
-- 
FFmpeg = Fantastic Faithless Merciful Pure Eccentric Gladiator


More information about the ffmpeg-devel mailing list