[FFmpeg-devel] [RFC] Scalar color conversion utils (colorutils.[hc])?
Michael Niedermayer
michaelni
Sat May 2 04:32:32 CEST 2009
On Sat, May 02, 2009 at 12:48:11AM +0200, Stefano Sabatini wrote:
> On date Tuesday 2009-04-28 23:41:15 +0200, Michael Niedermayer encoded:
> > On Tue, Apr 28, 2009 at 10:28:56PM +0200, Stefano Sabatini wrote:
> > > Hi all,
> > >
> > > the need for such a facility mainly arises for libavfilter filters.
> > > For example we may need something to work like this:
> > >
> > > pad=padcolor=COLOR
> > >
> > > or
> > >
> > > drawbox=X:Y:COLOR
> > >
> > > and many other filters as well may need to take in input a textual
> > > representation of a color and convert it to a given colorspace.
> > >
> > > libavcodec/colorspace.h defines some useful macros for converting from
> > > RGB to YUV, but I think what we need here is a convenient API of the form:
> > >
> > > int av_parse_color(uint16_t color[4], enum PixelFormat dst_pix_fmt, const char *color_string, void *log_ctx)
> > >
> > > and
> > >
> > > int av_convert_color_to(uint16_t *dst_color[4], enum PixelFormat dst_pix_fmt,
> > > (uint16_t *src_color[4], enum PixelFormat src_pix_fmt,
> > > void *log_ctx);
> > >
> > > Support for HSV colorspace could be provided defining an HSV pixel format.
> > >
> > > Some of the questions which need a response:
> > >
> >
> > > * We may need a table containing all the colornames <-> color. In
> > > which colorspace should be specified the color? (An RGB32 variant
> > > could be the ideal solution.)
> >
> > doesnt matter ...
> >
> >
> > >
> > > * Where it should be implemented? lavfi or lsws seem the better
> > > candidates.
> >
> > probably lavfi
> >
> >
> > >
> > > * How should be done the color-spec string? It may contains a simple
> > > name but also a color specifications. Variants which come to mind:
> > > rgb=RR:GG::BB, hsv=HH:SS:VV, yuv=YY:UU:VV.
> >
> > html 0xRRGGBB must be supported
> > so must
> > red/gray/...
> >
> > i see no need for further variants in the near future.
>
> Attached stub, result of the test is:
>
> Cannot find color 'foo'
> Cannot find color 'red'
> Cannot find color 'Red '
> Cannot find color 'RED'
> Red -> R(255) G(0) B(0)
> 0x000000 -> R(0) G(0) B(0)
> 0x3e34ff -> R(62) G(52) B(255)
> Invalid RGB color string: '0xfoobar'
> Invalid RGB color string: '0xffffeeeeeeee'
>
> Conversion has to be implemented yet.
>
> I wonder if the case-insensitive matching of the name is a feature
> worth to be implemented, but as far as I understood it would require a
> linear search as opposed to a binary search as currently implemented.
no
>
> Regards.
> --
> FFmpeg = Faboulous Free Multipurpose Purposeless Extravagant Governor
> Makefile | 1
> colorutils.c | 261 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> colorutils.h | 61 +++++++++++++
> 3 files changed, 323 insertions(+)
> 1be93571a739e55f8114ee08c90f441fb43c358c implement-colorutils.patch
> Index: libavfilter-soc/ffmpeg/libavfilter/colorutils.c
> ===================================================================
> --- /dev/null 1970-01-01 00:00:00.000000000 +0000
> +++ libavfilter-soc/ffmpeg/libavfilter/colorutils.c 2009-05-02 00:44:19.000000000 +0200
> @@ -0,0 +1,261 @@
> +/*
> + * Stefano Sabatini 2009
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +/**
> + * @file libavfilter/colorutils.c
> + * color handling utils
> + */
> +
> +#include "colorutils.h"
> +#include "libavcodec/colorspace.h"
> +#include "libavutil/log.h"
> +
> +int av_convert_color(uint16_t *dst_color, enum PixelFormat dst_pix_fmt, enum AVColorspace dst_colorspace,
> + uint16_t *src_color, enum PixelFormat src_pix_fmt, enum AVColorspace src_colorspace,
> + void *log_ctx)
> +{
> + if (src_pix_fmt == dst_pix_fmt &&
> + dst_colorspace == src_colorspace) {
> + memcpy(dst_color, src_color, sizeof(*dst_color) * 4);
> + } else {
> + av_log(log_ctx, AV_LOG_ERROR, "Unsupported conversion\n");
> + return -1;
> + }
> +
> + return 0;
> +}
> +
> +typedef struct {
> + const char *name; ///< a string representing the name of the color
> + uint16_t color[4]; ///< RGB24 values for the color
> +} ColorEntry;
they all seem to fit in 8bit
> +
> +static ColorEntry color_table[] = {
> + { "AliceBlue", { 0xF0, 0xF8, 0xFF} },
> + { "AntiqueWhite", { 0xFA, 0xEB, 0xD7 } },
> + { "Aqua", { 0x00, 0xFF, 0xFF} },
> + { "Aquamarine", { 0x7F, 0xFF, 0xD4 } },
> + { "Azure", { 0xF0, 0xFF, 0xFF} },
> + { "Beige", { 0xF5, 0xF5, 0xDC } },
> + { "Bisque", { 0xFF, 0xE4, 0xC4 } },
vertical align
[...]
> +
> +static int color_table_compare(const void *lhs, const void *rhs)
> +{
> + return strcmp(lhs, ((const ColorEntry *)rhs)->name);
> +}
> +
> +int av_parse_color(uint16_t *color, enum PixelFormat dst_pix_fmt, enum AVColorspace dst_colorspace,
> + const char *color_string, void *log_ctx)
> +{
> + uint16_t rgb_color[4];
> +
> + if (!strncmp(color_string, "0x", 2)) {
> + char *tail;
> + int rgb = strtol(color_string, &tail, 16);
> +
> + if (*tail || strlen(color_string) != 8) {
> + av_log(log_ctx, AV_LOG_ERROR, "Invalid RGB color string: '%s'\n", color_string);
> + return -1;
> + }
> + rgb_color[0] = (uint16_t)(rgb >> 16);
> + rgb_color[1] = (uint16_t)((rgb >> 8) & 0xFF);
> + rgb_color[2] = (uint16_t)(rgb & 0xFF);
useless casts
> + rgb_color[3] = 255;
> + } else {
> + const ColorEntry *entry = NULL;
> + entry = bsearch(color_string,
> + color_table,
> + FF_ARRAY_ELEMS(color_table),
> + sizeof(ColorEntry),
> + color_table_compare);
> + if (!entry) {
> + av_log(log_ctx, AV_LOG_DEBUG, "Cannot find color '%s'\n", color_string);
> + return -1;
> + }
> + memcpy(rgb_color, entry->color, sizeof(*color) * 3);
> + }
> +
> + return av_convert_color(color, dst_pix_fmt, dst_colorspace,
> + rgb_color, PIX_FMT_RGB24, AV_COLORSPACE_RGB,
> + log_ctx);
i dont think it makes sense to mix color convert and color parse
[...]
> +#ifndef AVFILTER_COLORUTILS_H
> +#define AVFILTER_COLORUTILS_H
> +
> +#include "libavutil/pixfmt.h"
> +#include "libavcodec/pixdesc.h"
> +
> +enum AVColorspace {
> + AV_COLORSPACE_RGB,
> + AV_COLORSPACE_ITU709,
> + AV_COLORSPACE_FCC,
> + AV_COLORSPACE_ITU601,
> + AV_COLORSPACE_SMPTE240M,
> + AV_COLORSPACE_NB
> +};
let me try again
we need a AVColorspace in AVCodecContext not in a header inaccessible to
avcodec.h
Also they miss doxy comments and i hope there are no duplicates amongth
them
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
If a bugfix only changes things apparently unrelated to the bug with no
further explanation, that is a good sign that the bugfix is wrong.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20090502/fa734e60/attachment.pgp>
More information about the ffmpeg-devel
mailing list