[FFmpeg-devel] [PATCH 1/4] SGI RLE 8-bit decoder

Peter Ross pross at xvid.org
Tue Dec 11 11:25:34 CET 2012


Signed-off-by: Peter Ross <pross at xvid.org>
---
 Changelog               |   1 +
 doc/general.texi        |   1 +
 libavcodec/Makefile     |   1 +
 libavcodec/allcodecs.c  |   1 +
 libavcodec/avcodec.h    |   1 +
 libavcodec/codec_desc.c |   7 +++
 libavcodec/sgirledec.c  | 152 ++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 164 insertions(+)
 create mode 100644 libavcodec/sgirledec.c

diff --git a/Changelog b/Changelog
index 6be40bb..e8e6911 100644
--- a/Changelog
+++ b/Changelog
@@ -40,6 +40,7 @@ version <next>:
 - Virtual concatenation demuxer
 - VobSub demuxer
 - JSON captions for TED talks decoding support
+- SGI RLE 8-bit decoder
 
 
 version 1.0:
diff --git a/doc/general.texi b/doc/general.texi
index ed1269f..eb12364 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -664,6 +664,7 @@ following image formats are supported:
     @tab Texture dictionaries used by the Renderware Engine.
 @item RL2 video              @tab     @tab  X
     @tab used in some games by Entertainment Software Partners
+ at item SGI RLE 8-bit          @tab     @tab  X
 @item Sierra VMD video       @tab     @tab  X
     @tab Used in Sierra VMD files.
 @item Smacker video          @tab     @tab  X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 0507028..9e6edc1 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -376,6 +376,7 @@ OBJS-$(CONFIG_S302M_DECODER)           += s302m.o
 OBJS-$(CONFIG_SANM_DECODER)            += sanm.o
 OBJS-$(CONFIG_SGI_DECODER)             += sgidec.o
 OBJS-$(CONFIG_SGI_ENCODER)             += sgienc.o rle.o
+OBJS-$(CONFIG_SGIRLE_DECODER)          += sgirledec.o
 OBJS-$(CONFIG_SHORTEN_DECODER)         += shorten.o
 OBJS-$(CONFIG_SIPR_DECODER)            += sipr.o acelp_pitch_delay.o \
                                           celp_math.o acelp_vectors.o \
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 4743bb1..5dcd509 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -215,6 +215,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER (S302M, s302m);
     REGISTER_DECODER (SANM, sanm);
     REGISTER_ENCDEC  (SGI, sgi);
+    REGISTER_DECODER (SGIRLE, sgirle);
     REGISTER_DECODER (SMACKER, smacker);
     REGISTER_DECODER (SMC, smc);
     REGISTER_ENCDEC  (SNOW, snow);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index e432040..b9317f0 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -285,6 +285,7 @@ enum AVCodecID {
     AV_CODEC_ID_AVRN       = MKBETAG('A','V','R','n'),
     AV_CODEC_ID_CPIA       = MKBETAG('C','P','I','A'),
     AV_CODEC_ID_XFACE      = MKBETAG('X','F','A','C'),
+    AV_CODEC_ID_SGIRLE     = MKBETAG('S','G','I','R'),
 
     /* various PCM "codecs" */
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 8e8685c..4ba31e9 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -752,6 +752,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
         .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
     },
     {
+        .id        = AV_CODEC_ID_SGIRLE,
+        .type      = AVMEDIA_TYPE_VIDEO,
+        .name      = "sgirle",
+        .long_name = NULL_IF_CONFIG_SMALL("SGI RLE 8-bit"),
+        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
+    },
+    {
         .id        = AV_CODEC_ID_C93,
         .type      = AVMEDIA_TYPE_VIDEO,
         .name      = "c93",
diff --git a/libavcodec/sgirledec.c b/libavcodec/sgirledec.c
new file mode 100644
index 0000000..cd31e0f
--- /dev/null
+++ b/libavcodec/sgirledec.c
@@ -0,0 +1,152 @@
+/*
+ * SGI RLE 8-bit decoder
+ * Copyright (c) 2012 Peter Ross
+ *
+ * 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
+ * SGI RLE 8-bit decoder
+ */
+
+#include "libavutil/intreadwrite.h"
+#include "avcodec.h"
+#include "bytestream.h"
+
+typedef struct {
+    AVFrame frame;
+} SgiContext;
+
+static av_cold int sgirle_decode_init(AVCodecContext *avctx)
+{
+    SgiContext *s = avctx->priv_data;
+    avctx->pix_fmt = AV_PIX_FMT_BGR8;
+
+    avcodec_get_frame_defaults(&s->frame);
+    s->frame.data[0] = NULL;
+    return 0;
+}
+
+/**
+ * Convert SGI RGB332 pixel into PIX_FMT_BGR8
+ * SGI RGB332 is packed RGB 3:3:2, 8bpp, (msb)3R 2B 3G(lsb)
+ */
+#define RGB332_TO_BGR8(x) (((x << 3) & 0xC0) | ((x << 3) & 0x38) | ((x >> 5) & 7))
+
+static av_always_inline void memcpy_rgb332_to_bgr8(uint8_t *dst, const uint8_t *src, int size)
+{
+    int i;
+    for (i = 0; i < size; i++)
+        dst[i] = RGB332_TO_BGR8(src[i]);
+}
+
+/**
+ * @param[out] dst Destination buffer
+ * @param[in] src Source buffer
+ * @param src_size Source buffer size (bytes)
+ * @param width Width of destination buffer (pixels)
+ * @param height Height of destination buffer (pixels)
+ * @param linesize Line size of destination buffer (bytes)
+ * @return < 0 on unknown opcode
+ */
+static int decode_sgirle8(uint8_t *dst, const uint8_t *src, int src_size, int width, int height, int linesize)
+{
+    const uint8_t *src_end = src + src_size;
+    int x = 0, y = 0;
+
+#define INC_XY(n) \
+    x += n; \
+    if (x >= width) { \
+        y++; \
+        if (y >= height) \
+            return 0; \
+        x = 0; \
+    }
+
+    while (src_end - src >= 2) {
+        uint8_t v = *src++;
+        if (v > 0 && v < 0xC0) {
+            do {
+                int length = FFMIN(v, width - x);
+                memset(dst + y*linesize + x, RGB332_TO_BGR8(*src), length);
+                INC_XY(length);
+                v   -= length;
+            } while (v > 0);
+            src++;
+        } else if (v >= 0xC1) {
+            v -= 0xC0;
+            do {
+                int length = FFMIN3(v, width - x, src_end - src);
+                if (src_end - src < length)
+                    break;
+                memcpy_rgb332_to_bgr8(dst + y*linesize + x, src, length);
+                INC_XY(length);
+                src += length;
+                v   -= length;
+            } while (v > 0);
+        } else {
+            return -1;
+        }
+    }
+    return 0;
+}
+
+static int sgirle_decode_frame(AVCodecContext *avctx,
+                            void *data, int *data_size,
+                            AVPacket *avpkt)
+{
+    SgiContext *s = avctx->priv_data;
+
+    s->frame.reference = 3;
+    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
+                            FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
+    if (avctx->reget_buffer(avctx, &s->frame)) {
+        av_log (avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+        return -1;
+    }
+
+    if (decode_sgirle8(s->frame.data[0], avpkt->data, avpkt->size, avctx->width, avctx->height, s->frame.linesize[0]) < 0)
+        av_log_ask_for_sample(avctx, "unknown opcode\n");
+
+    *data_size = sizeof(AVFrame);
+    *(AVFrame*)data = s->frame;
+
+    return avpkt->size;
+}
+
+static av_cold int sgirle_decode_end(AVCodecContext *avctx)
+{
+    SgiContext *s = avctx->priv_data;
+
+    if (s->frame.data[0])
+        avctx->release_buffer(avctx, &s->frame);
+
+    return 0;
+}
+
+AVCodec ff_sgirle_decoder = {
+    .name           = "sgirle",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_SGIRLE,
+    .priv_data_size = sizeof(SgiContext),
+    .init           = sgirle_decode_init,
+    .close          = sgirle_decode_end,
+    .decode         = sgirle_decode_frame,
+    .capabilities   = CODEC_CAP_DR1,
+    .long_name      = NULL_IF_CONFIG_SMALL("SGI RLE 8-bit"),
+};
-- 
1.8.0

-- Peter
(A907 E02F A6E5 0CD2 34CD 20D2 6760 79C5 AC40 DD6B)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20121211/c509e4fc/attachment.asc>


More information about the ffmpeg-devel mailing list