libavcodec/sgirledec.c
afa1617b
 /*
07761294
  * Silicon Graphics RLE 8-bit video decoder
afa1617b
  * 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
07761294
  * 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.
afa1617b
  */
 
07761294
 #include "libavutil/common.h"
 
afa1617b
 #include "avcodec.h"
80e9e63c
 #include "internal.h"
 
afa1617b
 static av_cold int sgirle_decode_init(AVCodecContext *avctx)
 {
     avctx->pix_fmt = AV_PIX_FMT_BGR8;
     return 0;
 }
 
 /**
07761294
  * Convert SGI RBG323 pixel into AV_PIX_FMT_BGR8
  * SGI RGB data is packed as 8bpp, (msb)3R 2B 3G(lsb)
afa1617b
  */
b0d0e29f
 #define RBG323_TO_BGR8(x) ((((x) << 3) & 0xC0) |                                \
                            (((x) << 3) & 0x38) |                                \
                            (((x) >> 5) & 7))
07761294
 static av_always_inline
 void rbg323_to_bgr8(uint8_t *dst, const uint8_t *src, int size)
afa1617b
 {
     int i;
     for (i = 0; i < size; i++)
07761294
         dst[i] = RBG323_TO_BGR8(src[i]);
afa1617b
 }
 
 /**
  * @param[out] dst Destination buffer
07761294
  * @param[in] src  Source buffer
afa1617b
  * @param src_size Source buffer size (bytes)
07761294
  * @param width    Width of destination buffer (pixels)
  * @param height   Height of destination buffer (pixels)
afa1617b
  * @param linesize Line size of destination buffer (bytes)
07761294
  *
afa1617b
  * @return <0 on error
  */
07761294
 static int decode_sgirle8(AVCodecContext *avctx, uint8_t *dst,
                           const uint8_t *src, int src_size,
                           int width, int height, ptrdiff_t linesize)
afa1617b
 {
     const uint8_t *src_end = src + src_size;
     int x = 0, y = 0;
 
07761294
 #define INC_XY(n)                                                             \
     x += n;                                                                   \
     if (x >= width) {                                                         \
         y++;                                                                  \
         if (y >= height)                                                      \
             return 0;                                                         \
         x = 0;                                                                \
afa1617b
     }
 
     while (src_end - src >= 2) {
         uint8_t v = *src++;
         if (v > 0 && v < 0xC0) {
             do {
                 int length = FFMIN(v, width - x);
b00fb157
                 if (length <= 0)
                     break;
07761294
                 memset(dst + y * linesize + x, RBG323_TO_BGR8(*src), length);
afa1617b
                 INC_XY(length);
07761294
                 v -= length;
afa1617b
             } while (v > 0);
             src++;
         } else if (v >= 0xC1) {
             v -= 0xC0;
             do {
                 int length = FFMIN3(v, width - x, src_end - src);
b00fb157
                 if (src_end - src < length || length <= 0)
afa1617b
                     break;
07761294
                 rbg323_to_bgr8(dst + y * linesize + x, src, length);
afa1617b
                 INC_XY(length);
                 src += length;
                 v   -= length;
             } while (v > 0);
         } else {
a9b42487
             avpriv_request_sample(avctx, "opcode %d", v);
afa1617b
             return AVERROR_PATCHWELCOME;
         }
     }
     return 0;
 }
 
07761294
 static int sgirle_decode_frame(AVCodecContext *avctx, void *data,
                                int *got_frame, AVPacket *avpkt)
afa1617b
 {
ccc3f4e7
     AVFrame *frame = data;
afa1617b
     int ret;
 
ccc3f4e7
     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
afa1617b
         return ret;
 
ccc3f4e7
     ret = decode_sgirle8(avctx, frame->data[0], avpkt->data, avpkt->size,
                          avctx->width, avctx->height, frame->linesize[0]);
afa1617b
     if (ret < 0)
         return ret;
 
ccc3f4e7
     frame->pict_type = AV_PICTURE_TYPE_I;
     frame->key_frame = 1;
 
07761294
     *got_frame = 1;
afa1617b
 
     return avpkt->size;
 }
 
 AVCodec ff_sgirle_decoder = {
     .name           = "sgirle",
07761294
     .long_name      = NULL_IF_CONFIG_SMALL("Silicon Graphics RLE 8-bit video"),
afa1617b
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_SGIRLE,
     .init           = sgirle_decode_init,
     .decode         = sgirle_decode_frame,
def97856
     .capabilities   = AV_CODEC_CAP_DR1,
afa1617b
 };