From 07761294fc3f08e139e8a406ef7d5b63aaf1ecee Mon Sep 17 00:00:00 2001 From: Peter Ross Date: Mon, 17 Dec 2012 20:37:54 +1100 Subject: Silicon Graphics RLE 8-bit video decoder Signed-off-by: Vittorio Giovara --- 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 | 160 ++++++++++++++++++++++++++++++++++++++++++++++++ libavcodec/version.h | 2 +- libavformat/isom.c | 1 + 9 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 libavcodec/sgirledec.c diff --git a/Changelog b/Changelog index 55216e8..0205ecd 100644 --- a/Changelog +++ b/Changelog @@ -19,6 +19,7 @@ version : - LucasArts SMUSH demuxer - MP2 encoding via TwoLAME - asettb filter +- Silicon Graphics RLE 8-bit video decoder version 10: diff --git a/doc/general.texi b/doc/general.texi index 2612eb1..ddf1c90 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -693,6 +693,7 @@ following image formats are supported: @tab used in some games by Entertainment Software Partners @item Sierra VMD video @tab @tab X @tab Used in Sierra VMD files. +@item Silicon Graphics RLE 8-bit video @tab @tab X @item Smacker video @tab @tab X @tab Video encoding used in Smacker. @item SMPTE VC-1 @tab @tab X diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 5bd9778..e920964 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -329,6 +329,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 47bbef5..8f44e88 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -228,6 +228,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_DECODER(SP5X, sp5x); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 622eac3..b11bffa 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -290,6 +290,7 @@ enum AVCodecID { AV_CODEC_ID_EXR, AV_CODEC_ID_VP7, AV_CODEC_ID_SANM, + AV_CODEC_ID_SGIRLE, /* 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 e5257fb..2474261 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1113,6 +1113,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("LucasArts SANM video"), .props = 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, + }, /* image codecs */ { diff --git a/libavcodec/sgirledec.c b/libavcodec/sgirledec.c new file mode 100644 index 0000000..d7bc5a9 --- /dev/null +++ b/libavcodec/sgirledec.c @@ -0,0 +1,160 @@ +/* + * Silicon Graphics RLE 8-bit video decoder + * Copyright (c) 2012 Peter Ross + * + * This file is part of Libav. + * + * Libav 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. + * + * Libav 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 Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Silicon Graphics RLE 8-bit video decoder + * @note Data is packed in rbg323 with rle, contained in mv or mov. + * The algorithm and pixfmt are subtly different from SGI images. + */ + +#include "libavutil/common.h" + +#include "avcodec.h" +#include "internal.h" + +typedef struct SGIRLEContext { + AVFrame *frame; +} SGIRLEContext; + +static av_cold int sgirle_decode_init(AVCodecContext *avctx) +{ + SGIRLEContext *s = avctx->priv_data; + avctx->pix_fmt = AV_PIX_FMT_BGR8; + s->frame = av_frame_alloc(); + if (!s->frame) + return AVERROR(ENOMEM); + return 0; +} + +/** + * Convert SGI RBG323 pixel into AV_PIX_FMT_BGR8 + * SGI RGB data is packed as 8bpp, (msb)3R 2B 3G(lsb) + */ +#define RBG323_TO_BGR8(x) (((x << 3) & 0xC0) | \ + ((x << 3) & 0x38) | \ + ((x >> 5) & 7)) +static av_always_inline +void rbg323_to_bgr8(uint8_t *dst, const uint8_t *src, int size) +{ + int i; + for (i = 0; i < size; i++) + dst[i] = RBG323_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 error + */ +static int decode_sgirle8(AVCodecContext *avctx, uint8_t *dst, + const uint8_t *src, int src_size, + int width, int height, ptrdiff_t 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); + if (length <= 0) + break; + memset(dst + y * linesize + x, RBG323_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 || length <= 0) + break; + rbg323_to_bgr8(dst + y * linesize + x, src, length); + INC_XY(length); + src += length; + v -= length; + } while (v > 0); + } else { + av_log(avctx, AV_LOG_ERROR, "Invalid opcode %d.\n", v); + return AVERROR_INVALIDDATA; + } + } + return 0; +} + +static int sgirle_decode_frame(AVCodecContext *avctx, void *data, + int *got_frame, AVPacket *avpkt) +{ + SGIRLEContext *s = avctx->priv_data; + int ret; + + if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) + return ret; + + ret = decode_sgirle8(avctx, s->frame->data[0], avpkt->data, avpkt->size, + avctx->width, avctx->height, s->frame->linesize[0]); + if (ret < 0) + return ret; + + *got_frame = 1; + if ((ret = av_frame_ref(data, s->frame)) < 0) + return ret; + + return avpkt->size; +} + +static av_cold int sgirle_decode_end(AVCodecContext *avctx) +{ + SGIRLEContext *s = avctx->priv_data; + + av_frame_free(&s->frame); + + return 0; +} + +AVCodec ff_sgirle_decoder = { + .name = "sgirle", + .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics RLE 8-bit video"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_SGIRLE, + .priv_data_size = sizeof(SGIRLEContext), + .init = sgirle_decode_init, + .close = sgirle_decode_end, + .decode = sgirle_decode_frame, + .capabilities = CODEC_CAP_DR1, +}; diff --git a/libavcodec/version.h b/libavcodec/version.h index 4a8febc..234d409 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -29,7 +29,7 @@ #include "libavutil/version.h" #define LIBAVCODEC_VERSION_MAJOR 55 -#define LIBAVCODEC_VERSION_MINOR 46 +#define LIBAVCODEC_VERSION_MINOR 47 #define LIBAVCODEC_VERSION_MICRO 0 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ diff --git a/libavformat/isom.c b/libavformat/isom.c index e8670bf..5d10dca 100644 --- a/libavformat/isom.c +++ b/libavformat/isom.c @@ -141,6 +141,7 @@ const AVCodecTag ff_codec_movvideo_tags[] = { { AV_CODEC_ID_8BPS, MKTAG('8', 'B', 'P', 'S') }, /* Planar RGB (8BPS) */ { AV_CODEC_ID_SMC, MKTAG('s', 'm', 'c', ' ') }, /* Apple Graphics (SMC) */ { AV_CODEC_ID_QTRLE, MKTAG('r', 'l', 'e', ' ') }, /* Apple Animation (RLE) */ + { AV_CODEC_ID_SGIRLE, MKTAG('r', 'l', 'e', '1') }, /* SGI RLE 8-bit */ { AV_CODEC_ID_MSRLE, MKTAG('W', 'R', 'L', 'E') }, { AV_CODEC_ID_QDRAW, MKTAG('q', 'd', 'r', 'w') }, /* QuickDraw */ -- cgit v1.1