2012-12-17 09:37:54 +00:00
|
|
|
/*
|
2012-12-17 09:37:54 +00:00
|
|
|
* Silicon Graphics RLE 8-bit video decoder
|
2012-12-17 09:37:54 +00:00
|
|
|
* 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
|
2012-12-17 09:37:54 +00:00
|
|
|
* 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.
|
2012-12-17 09:37:54 +00:00
|
|
|
*/
|
|
|
|
|
2012-12-17 09:37:54 +00:00
|
|
|
#include "libavutil/common.h"
|
|
|
|
|
2012-12-17 09:37:54 +00:00
|
|
|
#include "avcodec.h"
|
2013-03-12 02:20:18 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
2012-12-17 09:37:54 +00:00
|
|
|
static av_cold int sgirle_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_BGR8;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-17 09:37:54 +00:00
|
|
|
* Convert SGI RBG323 pixel into AV_PIX_FMT_BGR8
|
|
|
|
* SGI RGB data is packed as 8bpp, (msb)3R 2B 3G(lsb)
|
2012-12-17 09:37:54 +00:00
|
|
|
*/
|
2014-05-25 21:08:51 +00:00
|
|
|
#define RBG323_TO_BGR8(x) ((((x) << 3) & 0xC0) | \
|
|
|
|
(((x) << 3) & 0x38) | \
|
|
|
|
(((x) >> 5) & 7))
|
2012-12-17 09:37:54 +00:00
|
|
|
static av_always_inline
|
|
|
|
void rbg323_to_bgr8(uint8_t *dst, const uint8_t *src, int size)
|
2012-12-17 09:37:54 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < size; i++)
|
2012-12-17 09:37:54 +00:00
|
|
|
dst[i] = RBG323_TO_BGR8(src[i]);
|
2012-12-17 09:37:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param[out] dst Destination buffer
|
2012-12-17 09:37:54 +00:00
|
|
|
* @param[in] src Source buffer
|
2012-12-17 09:37:54 +00:00
|
|
|
* @param src_size Source buffer size (bytes)
|
2012-12-17 09:37:54 +00:00
|
|
|
* @param width Width of destination buffer (pixels)
|
|
|
|
* @param height Height of destination buffer (pixels)
|
2012-12-17 09:37:54 +00:00
|
|
|
* @param linesize Line size of destination buffer (bytes)
|
2012-12-17 09:37:54 +00:00
|
|
|
*
|
2012-12-17 09:37:54 +00:00
|
|
|
* @return <0 on error
|
|
|
|
*/
|
2012-12-17 09:37:54 +00:00
|
|
|
static int decode_sgirle8(AVCodecContext *avctx, uint8_t *dst,
|
|
|
|
const uint8_t *src, int src_size,
|
|
|
|
int width, int height, ptrdiff_t linesize)
|
2012-12-17 09:37:54 +00:00
|
|
|
{
|
|
|
|
const uint8_t *src_end = src + src_size;
|
|
|
|
int x = 0, y = 0;
|
|
|
|
|
2012-12-17 09:37:54 +00:00
|
|
|
#define INC_XY(n) \
|
|
|
|
x += n; \
|
|
|
|
if (x >= width) { \
|
|
|
|
y++; \
|
|
|
|
if (y >= height) \
|
|
|
|
return 0; \
|
|
|
|
x = 0; \
|
2012-12-17 09:37:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (src_end - src >= 2) {
|
|
|
|
uint8_t v = *src++;
|
|
|
|
if (v > 0 && v < 0xC0) {
|
|
|
|
do {
|
|
|
|
int length = FFMIN(v, width - x);
|
2013-09-22 17:22:51 +00:00
|
|
|
if (length <= 0)
|
|
|
|
break;
|
2012-12-17 09:37:54 +00:00
|
|
|
memset(dst + y * linesize + x, RBG323_TO_BGR8(*src), length);
|
2012-12-17 09:37:54 +00:00
|
|
|
INC_XY(length);
|
2012-12-17 09:37:54 +00:00
|
|
|
v -= length;
|
2012-12-17 09:37:54 +00:00
|
|
|
} while (v > 0);
|
|
|
|
src++;
|
|
|
|
} else if (v >= 0xC1) {
|
|
|
|
v -= 0xC0;
|
|
|
|
do {
|
|
|
|
int length = FFMIN3(v, width - x, src_end - src);
|
2013-09-22 17:22:51 +00:00
|
|
|
if (src_end - src < length || length <= 0)
|
2012-12-17 09:37:54 +00:00
|
|
|
break;
|
2012-12-17 09:37:54 +00:00
|
|
|
rbg323_to_bgr8(dst + y * linesize + x, src, length);
|
2012-12-17 09:37:54 +00:00
|
|
|
INC_XY(length);
|
|
|
|
src += length;
|
|
|
|
v -= length;
|
|
|
|
} while (v > 0);
|
|
|
|
} else {
|
2013-03-15 17:13:52 +00:00
|
|
|
avpriv_request_sample(avctx, "opcode %d", v);
|
2012-12-17 09:37:54 +00:00
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-17 09:37:54 +00:00
|
|
|
static int sgirle_decode_frame(AVCodecContext *avctx, void *data,
|
|
|
|
int *got_frame, AVPacket *avpkt)
|
2012-12-17 09:37:54 +00:00
|
|
|
{
|
2016-05-20 19:30:29 +00:00
|
|
|
AVFrame *frame = data;
|
2012-12-17 09:37:54 +00:00
|
|
|
int ret;
|
|
|
|
|
2016-05-20 19:30:29 +00:00
|
|
|
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
|
2012-12-17 09:37:54 +00:00
|
|
|
return ret;
|
|
|
|
|
2016-05-20 19:30:29 +00:00
|
|
|
ret = decode_sgirle8(avctx, frame->data[0], avpkt->data, avpkt->size,
|
|
|
|
avctx->width, avctx->height, frame->linesize[0]);
|
2012-12-17 09:37:54 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2016-05-20 19:30:29 +00:00
|
|
|
frame->pict_type = AV_PICTURE_TYPE_I;
|
|
|
|
frame->key_frame = 1;
|
|
|
|
|
2012-12-17 09:37:54 +00:00
|
|
|
*got_frame = 1;
|
2012-12-17 09:37:54 +00:00
|
|
|
|
|
|
|
return avpkt->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVCodec ff_sgirle_decoder = {
|
|
|
|
.name = "sgirle",
|
2012-12-17 09:37:54 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics RLE 8-bit video"),
|
2012-12-17 09:37:54 +00:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.id = AV_CODEC_ID_SGIRLE,
|
|
|
|
.init = sgirle_decode_init,
|
|
|
|
.decode = sgirle_decode_frame,
|
2015-07-07 00:41:27 +00:00
|
|
|
.capabilities = AV_CODEC_CAP_DR1,
|
2012-12-17 09:37:54 +00:00
|
|
|
};
|