2 * SGI RLE 8-bit decoder
3 * Copyright (c) 2012 Peter Ross
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * SGI RLE 8-bit decoder
30 typedef struct SGIRLEContext {
34 static av_cold int sgirle_decode_init(AVCodecContext *avctx)
36 SGIRLEContext *s = avctx->priv_data;
37 avctx->pix_fmt = AV_PIX_FMT_BGR8;
38 s->frame = av_frame_alloc();
40 return AVERROR(ENOMEM);
45 * Convert SGI RGB332 pixel into AV_PIX_FMT_BGR8
46 * SGI RGB332 is packed RGB 3:3:2, 8bpp, (msb)3R 2B 3G(lsb)
48 #define RGB332_TO_BGR8(x) (((x << 3) & 0xC0) | ((x << 3) & 0x38) | ((x >> 5) & 7))
50 static av_always_inline void memcpy_rgb332_to_bgr8(uint8_t *dst, const uint8_t *src, int size)
53 for (i = 0; i < size; i++)
54 dst[i] = RGB332_TO_BGR8(src[i]);
58 * @param[out] dst Destination buffer
59 * @param[in] src Source buffer
60 * @param src_size Source buffer size (bytes)
61 * @param width Width of destination buffer (pixels)
62 * @param height Height of destination buffer (pixels)
63 * @param linesize Line size of destination buffer (bytes)
66 static int decode_sgirle8(AVCodecContext *avctx, uint8_t *dst, const uint8_t *src, int src_size, int width, int height, int linesize)
68 const uint8_t *src_end = src + src_size;
80 while (src_end - src >= 2) {
82 if (v > 0 && v < 0xC0) {
84 int length = FFMIN(v, width - x);
87 memset(dst + y*linesize + x, RGB332_TO_BGR8(*src), length);
92 } else if (v >= 0xC1) {
95 int length = FFMIN3(v, width - x, src_end - src);
96 if (src_end - src < length || length <= 0)
98 memcpy_rgb332_to_bgr8(dst + y*linesize + x, src, length);
104 avpriv_request_sample(avctx, "opcode %d", v);
105 return AVERROR_PATCHWELCOME;
111 static int sgirle_decode_frame(AVCodecContext *avctx,
112 void *data, int *got_frame,
115 SGIRLEContext *s = avctx->priv_data;
118 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
121 ret = decode_sgirle8(avctx, s->frame->data[0], avpkt->data, avpkt->size, avctx->width, avctx->height, s->frame->linesize[0]);
126 if ((ret = av_frame_ref(data, s->frame)) < 0)
132 static av_cold int sgirle_decode_end(AVCodecContext *avctx)
134 SGIRLEContext *s = avctx->priv_data;
136 av_frame_free(&s->frame);
141 AVCodec ff_sgirle_decoder = {
143 .long_name = NULL_IF_CONFIG_SMALL("SGI RLE 8-bit"),
144 .type = AVMEDIA_TYPE_VIDEO,
145 .id = AV_CODEC_ID_SGIRLE,
146 .priv_data_size = sizeof(SGIRLEContext),
147 .init = sgirle_decode_init,
148 .close = sgirle_decode_end,
149 .decode = sgirle_decode_frame,
150 .capabilities = CODEC_CAP_DR1,