[FFmpeg-devel] [PATCH][TOY] XPM encoder

Paul B Mahol onemda at gmail.com
Sun Jun 24 02:51:53 CEST 2012


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---

Todo:
 - RGB24/BGR24.
 - count number of colors per image/palette

 doc/general.texi       |    2 +
 libavcodec/Makefile    |    1 +
 libavcodec/allcodecs.c |    1 +
 libavcodec/avcodec.h   |    1 +
 libavcodec/xpmenc.c    |  150 ++++++++++++++++++++++++++++++++++++++++++++++++
 libavformat/img2.c     |    1 +
 libavformat/img2enc.c  |    2 +-
 7 files changed, 157 insertions(+), 1 deletions(-)
 create mode 100644 libavcodec/xpmenc.c

diff --git a/doc/general.texi b/doc/general.texi
index 6217e54..971062f 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -422,6 +422,8 @@ following image formats are supported:
     @tab Targa (.TGA) image format
 @item XBM  @tab X @tab X
     @tab X BitMap image format
+ at item XPM  @tab X @tab
+    @tab X PixMap image format
 @item XWD  @tab X @tab X
     @tab X Window Dump image format
 @end multitable
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 95bcb17..de63420 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -509,6 +509,7 @@ OBJS-$(CONFIG_XBIN_DECODER)            += bintext.o cga_data.o
 OBJS-$(CONFIG_XBM_DECODER)             += xbmdec.o
 OBJS-$(CONFIG_XBM_ENCODER)             += xbmenc.o
 OBJS-$(CONFIG_XL_DECODER)              += xl.o
+OBJS-$(CONFIG_XPM_ENCODER)             += xpmenc.o
 OBJS-$(CONFIG_XSUB_DECODER)            += xsubdec.o
 OBJS-$(CONFIG_XSUB_ENCODER)            += xsubenc.o
 OBJS-$(CONFIG_XWD_DECODER)             += xwddec.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 8c2bd2b..41aa85a 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -255,6 +255,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER (XAN_WC4, xan_wc4);
     REGISTER_ENCDEC  (XBM, xbm);
     REGISTER_DECODER (XL, xl);
+    REGISTER_ENCODER (XPM, xpm);
     REGISTER_ENCDEC  (XWD, xwd);
     REGISTER_ENCDEC  (Y41P, y41p);
     REGISTER_DECODER (YOP, yop);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index d9da2ad..e84849e 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -270,6 +270,7 @@ enum CodecID {
     CODEC_ID_V408       = MKBETAG('V','4','0','8'),
     CODEC_ID_YUV4       = MKBETAG('Y','U','V','4'),
     CODEC_ID_SANM       = MKBETAG('S','A','N','M'),
+    CODEC_ID_XPM        = MKBETAG('X','P','M',' '),
 
     /* various PCM "codecs" */
     CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
diff --git a/libavcodec/xpmenc.c b/libavcodec/xpmenc.c
new file mode 100644
index 0000000..91485ae3
--- /dev/null
+++ b/libavcodec/xpmenc.c
@@ -0,0 +1,150 @@
+/*
+ * XPM image format
+ *
+ * Copyright (c) 2012 Paul B Mahol
+ *
+ * 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
+ */
+
+#include "libavutil/intreadwrite.h"
+#include "avcodec.h"
+#include "internal.h"
+
+typedef struct XPMEncContext {
+    AVFrame picture;
+    char    *cpixels;
+    int     cpixels_size;
+} XPMEncContext;
+
+static av_cold int xpm_encode_init(AVCodecContext *avctx)
+{
+    XPMEncContext *x = avctx->priv_data;
+
+    avcodec_get_frame_defaults(&x->picture);
+    avctx->coded_frame = &x->picture;
+
+    return 0;
+}
+
+static void index2ascii(int index, int cpp, char *cpixel)
+{
+    int n = index, charnum, i;
+    char *p = cpixel;
+
+    for (i = 0; i < cpp; i++) {
+        charnum = n % ('z' - '0');
+        n       = n / ('z' - '0');
+        *p++    = '0' + charnum;
+    }
+    *p = '\0';
+}
+
+static int xpm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
+                            const AVFrame *p, int *got_packet)
+{
+    XPMEncContext *x = avctx->priv_data;
+    int i, j, ret, ncolors, cpp, size;
+    uint8_t *buf, *ptr;
+
+    switch (avctx->pix_fmt) {
+    case PIX_FMT_RGB4_BYTE:
+    case PIX_FMT_BGR4_BYTE:
+        ncolors = 16;
+        cpp = 1;
+        break;
+    case PIX_FMT_PAL8:
+    case PIX_FMT_RGB8:
+    case PIX_FMT_BGR8:
+        ncolors = 256;
+        cpp = 2;
+        break;
+    default:
+        av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
+        return AVERROR(EINVAL);
+    }
+
+    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);
+
+    for (i = 0; i < ncolors; i++) {
+        uint8_t red, green, blue;
+        uint32_t val;
+
+        val   = AV_RN32A(p->data[1] + i * 4);
+        red   = (val >> 16) & 0xFF;
+        green = (val >>  8) & 0xFF;
+        blue  =  val        & 0xFF;
+
+        index2ascii(i, cpp, x->cpixels + i * (cpp + 1));
+        buf += snprintf(buf, 2 + cpp, "\"%s", x->cpixels + i * (cpp + 1));
+        buf += snprintf(buf, 16, " c #%02X%02X%02X\",\n", red, green, blue);
+    }
+    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");
+
+    pkt->size   = buf - pkt->data;
+    pkt->flags |= AV_PKT_FLAG_KEY;
+    *got_packet = 1;
+    return 0;
+}
+
+static av_cold int xpm_encode_close(AVCodecContext *avctx)
+{
+    XPMEncContext *x = avctx->priv_data;
+
+    av_freep(&x->cpixels);
+
+    return 0;
+}
+
+AVCodec ff_xpm_encoder = {
+    .name           = "xpm",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = CODEC_ID_XPM,
+    .priv_data_size = sizeof(XPMEncContext),
+    .init           = xpm_encode_init,
+    .encode2        = xpm_encode_frame,
+    .close          = xpm_encode_close,
+    .pix_fmts       = (const enum PixelFormat[]) { PIX_FMT_RGB8,
+                                                   PIX_FMT_BGR8,
+                                                   PIX_FMT_RGB4_BYTE,
+                                                   PIX_FMT_BGR4_BYTE,
+                                                   PIX_FMT_PAL8,
+                                                   PIX_FMT_NONE },
+    .long_name      = NULL_IF_CONFIG_SMALL("XPM (X PixMap) image"),
+};
diff --git a/libavformat/img2.c b/libavformat/img2.c
index 47dbb62..3dd83a2 100644
--- a/libavformat/img2.c
+++ b/libavformat/img2.c
@@ -73,6 +73,7 @@ static const IdStrMap img_tags[] = {
     { CODEC_ID_EXR       , "exr"},
     { CODEC_ID_PICTOR    , "pic"},
     { CODEC_ID_XBM       , "xbm"},
+    { CODEC_ID_XPM       , "xpm"},
     { CODEC_ID_XWD       , "xwd"},
     { CODEC_ID_NONE      , NULL}
 };
diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
index 5dc7e12..96c684b 100644
--- a/libavformat/img2enc.c
+++ b/libavformat/img2enc.c
@@ -153,7 +153,7 @@ AVOutputFormat ff_image2_muxer = {
     .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
     .extensions     = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
                       "ppm,sgi,tga,tif,tiff,jp2,j2c,xwd,sun,ras,rs,im1,im8,im24,"
-                      "sunras,xbm",
+                      "sunras,xbm,xpm",
     .priv_data_size = sizeof(VideoMuxData),
     .video_codec    = CODEC_ID_MJPEG,
     .write_header   = write_header,
-- 
1.7.7



More information about the ffmpeg-devel mailing list