[FFmpeg-devel] [PATCH] XPM encoder nad decoder

Clément Bœsch ubitux at gmail.com
Fri Jun 29 23:24:58 CEST 2012


On Thu, Jun 28, 2012 at 05:28:47PM +0000, Paul B Mahol wrote:
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
>  doc/general.texi       |    2 +
>  libavcodec/Makefile    |    2 +
>  libavcodec/allcodecs.c |    1 +
>  libavcodec/avcodec.h   |    1 +
>  libavcodec/xpmdec.c    |  180 ++++++++++++++++
>  libavcodec/xpmenc.c    |  154 ++++++++++++++
>  libavformat/img2.c     |    1 +
>  libavformat/img2enc.c  |    2 +-
>  libavutil/parseutils.c |  544 +++++++++++++++++++++++++++++++++++++++++++++++-
>  9 files changed, 881 insertions(+), 6 deletions(-)
>  create mode 100644 libavcodec/xpmdec.c
>  create mode 100644 libavcodec/xpmenc.c
> 

Interesting; a XPM2 would be great too, to avoid the C stuff :)

[...]
> +        len = strcspn(ptr, "\" ");
> +        if ((ret = av_parse_color(rgba, ptr, len, avctx)) < 0)
> +            return ret;
> +        *(p->data[1] + i * 4    ) = rgba[2];
> +        *(p->data[1] + i * 4 + 1) = rgba[1];
> +        *(p->data[1] + i * 4 + 2) = rgba[0];
> +        *(p->data[1] + i * 4 + 3) = rgba[3];
> +

Any reason to prefer *(p + x) over p[x] here?

> +        if ((ret = ascii2index(index, cpp)) < 0)
> +            return ret;
> +        x->pixels[ret] = i;
> +
> +        ptr += strcspn(ptr, ",") + 1;
> +    }
> +
> +    for (i = 0; i < avctx->height; i++) {
> +        dst = p->data[0] + i * p->linesize[0];
> +        ptr += strcspn(ptr, "\"") + 1;
> +        for (j = 0; j < avctx->width; j++) {
> +            if (ptr + cpp > end)
> +                return AVERROR_INVALIDDATA;
> +            *dst++ = x->pixels[ascii2index(ptr, cpp)];

No risk of ascii2index() failure here?

> +            ptr += cpp;
> +        }
> +        ptr += strcspn(ptr, ",") + 1;
> +    }
> +
> +    p->key_frame = 1;
> +    p->pict_type = AV_PICTURE_TYPE_I;

Would it make sense to add it to is_intra_only()? (or this function has
only effects for video?)

[...]
> +
> +    av_fast_padded_malloc(&x->cpixels, &x->cpixels_size, ncolors * (cpp + 1));
> +    if (!x->cpixels)
> +        return AVERROR(ENOMEM);
> +
> +    size = 12 + 25 + 50 + ncolors * (cpp + 16) + avctx->height * (avctx->width * cpp + 5);
> +    if ((ret = ff_alloc_packet2(avctx, pkt, size)) < 0) {
> +        av_freep(&x->cpixels);
> +        return ret;
> +    }
> +
> +    buf = pkt->data;
> +    ptr = p->data[0];
> +
> +    buf += snprintf(buf, 12, "/* XPM */\n");
> +    buf += snprintf(buf, 25, "static char *out[] = {\n");
> +    buf += snprintf(buf, 50, "\"%u %u %u %u\",\n", avctx->width, avctx->height, ncolors, cpp);
> +

This and below looks like a very good candidate for av_bprint.

> +    for (i = 0; i < ncolors; i++) {
> +        uint8_t red, green, blue, alpha;
> +        uint32_t val;
> +
> +        val   = AV_RN32A(p->data[1] + i * 4);
> +        alpha = (val >> 24) & 0xFF;
> +        red   = (val >> 16) & 0xFF;
> +        green = (val >>  8) & 0xFF;
> +        blue  =  val        & 0xFF;
> +

pointless ( )

> +        index2ascii(i, cpp, x->cpixels + i * (cpp + 1));
> +        buf += snprintf(buf, 2 + cpp, "\"%s", x->cpixels + i * (cpp + 1));
> +        if (alpha)
> +            buf += snprintf(buf, 16, " c #%02X%02X%02X\",\n", red, green, blue);
> +        else
> +            buf += snprintf(buf, 12, " c none\",\n");
> +    }
> +    for (i = 0; i < avctx->height; i++) {
> +        buf += snprintf(buf, 2, "\"");
> +        for (j = 0; j < avctx->width; j++) {
> +            buf += snprintf(buf, 1 + cpp, "%s", x->cpixels + *ptr++ * (cpp + 1));
> +        }
> +        ptr += p->linesize[0] - avctx->width;
> +        buf += snprintf(buf, 4, "\",\n");
> +    }
> +    buf += snprintf(buf, 4, "};\n");
> +
[...]

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20120629/aee9d6a6/attachment.asc>


More information about the ffmpeg-devel mailing list