2 * Mandsoft Screen Capture Codec decoder
4 * Copyright (c) 2017 Paul B Mahol
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "bytestream.h"
33 typedef struct MSCCContext {
35 unsigned int decomp_size;
37 unsigned int uncomp_size;
44 static int rle_uncompress(AVCodecContext *avctx, GetByteContext *gb, PutByteContext *pb)
46 MSCCContext *s = avctx->priv_data;
47 unsigned x = 0, y = 0;
49 while (bytestream2_get_bytes_left(gb) > 0) {
52 unsigned run = bytestream2_get_byte(gb);
55 switch (avctx->bits_per_coded_sample) {
57 fill = bytestream2_get_byte(gb);
60 fill = bytestream2_get_le16(gb);
63 fill = bytestream2_get_le24(gb);
66 fill = bytestream2_get_le32(gb);
70 for (j = 0; j < run; j++) {
71 switch (avctx->bits_per_coded_sample) {
73 bytestream2_put_byte(pb, fill);
76 bytestream2_put_le16(pb, fill);
79 bytestream2_put_le24(pb, fill);
82 bytestream2_put_le32(pb, fill);
88 unsigned copy = bytestream2_get_byte(gb);
93 bytestream2_seek_p(pb, y * avctx->width * s->bpp, SEEK_SET);
94 } else if (copy == 1) {
96 } else if (copy == 2) {
98 x += bytestream2_get_byte(gb);
99 y += bytestream2_get_byte(gb);
101 bytestream2_seek_p(pb, y * avctx->width * s->bpp + x * s->bpp, SEEK_SET);
103 for (j = 0; j < copy; j++) {
104 switch (avctx->bits_per_coded_sample) {
106 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
109 bytestream2_put_le16(pb, bytestream2_get_le16(gb));
112 bytestream2_put_le24(pb, bytestream2_get_le24(gb));
115 bytestream2_put_le32(pb, bytestream2_get_le32(gb));
120 if (s->bpp == 1 && (copy & 1))
121 bytestream2_skip(gb, 1);
127 return AVERROR_INVALIDDATA;
130 static int decode_frame(AVCodecContext *avctx,
131 void *data, int *got_frame,
134 MSCCContext *s = avctx->priv_data;
135 AVFrame *frame = data;
136 uint8_t *buf = avpkt->data;
137 int buf_size = avpkt->size;
145 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
148 if (avctx->codec_id == AV_CODEC_ID_MSCC) {
149 avpkt->data[2] ^= avpkt->data[0];
154 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
156 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
158 if (pal && size == AVPALETTE_SIZE) {
159 frame->palette_has_changed = 1;
160 for (j = 0; j < 256; j++)
161 s->pal[j] = 0xFF000000 | AV_RL32(pal + j * 4);
163 av_log(avctx, AV_LOG_ERROR,
164 "Palette size %"SIZE_SPECIFIER" is wrong\n", size);
166 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
169 ret = inflateReset(&s->zstream);
171 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
172 return AVERROR_UNKNOWN;
174 s->zstream.next_in = buf;
175 s->zstream.avail_in = buf_size;
176 s->zstream.next_out = s->decomp_buf;
177 s->zstream.avail_out = s->decomp_size;
178 ret = inflate(&s->zstream, Z_FINISH);
179 if (ret != Z_STREAM_END) {
180 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
181 return AVERROR_UNKNOWN;
184 bytestream2_init(&gb, s->decomp_buf, s->zstream.total_out);
185 bytestream2_init_writer(&pb, s->uncomp_buf, s->uncomp_size);
187 ret = rle_uncompress(avctx, &gb, &pb);
191 for (j = 0; j < avctx->height; j++) {
192 memcpy(frame->data[0] + (avctx->height - j - 1) * frame->linesize[0],
193 s->uncomp_buf + s->bpp * j * avctx->width, s->bpp * avctx->width);
196 frame->key_frame = 1;
197 frame->pict_type = AV_PICTURE_TYPE_I;
204 static av_cold int decode_init(AVCodecContext *avctx)
206 MSCCContext *s = avctx->priv_data;
209 switch (avctx->bits_per_coded_sample) {
210 case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
211 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
212 case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
213 case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
215 av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
216 return AVERROR_INVALIDDATA;
219 s->bpp = avctx->bits_per_coded_sample >> 3;
220 stride = 4 * ((avctx->width * avctx->bits_per_coded_sample + 31) / 32);
222 s->decomp_size = 2 * avctx->height * stride;
223 if (!(s->decomp_buf = av_malloc(s->decomp_size)))
224 return AVERROR(ENOMEM);
226 s->uncomp_size = avctx->height * stride;
227 if (!(s->uncomp_buf = av_malloc(s->uncomp_size)))
228 return AVERROR(ENOMEM);
230 s->zstream.zalloc = Z_NULL;
231 s->zstream.zfree = Z_NULL;
232 s->zstream.opaque = Z_NULL;
233 zret = inflateInit(&s->zstream);
235 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
236 return AVERROR_UNKNOWN;
242 static av_cold int decode_close(AVCodecContext *avctx)
244 MSCCContext *s = avctx->priv_data;
246 av_freep(&s->decomp_buf);
248 av_freep(&s->uncomp_buf);
250 inflateEnd(&s->zstream);
255 const AVCodec ff_mscc_decoder = {
257 .long_name = NULL_IF_CONFIG_SMALL("Mandsoft Screen Capture Codec"),
258 .type = AVMEDIA_TYPE_VIDEO,
259 .id = AV_CODEC_ID_MSCC,
260 .priv_data_size = sizeof(MSCCContext),
262 .close = decode_close,
263 .decode = decode_frame,
264 .capabilities = AV_CODEC_CAP_DR1,
265 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
268 const AVCodec ff_srgc_decoder = {
270 .long_name = NULL_IF_CONFIG_SMALL("Screen Recorder Gold Codec"),
271 .type = AVMEDIA_TYPE_VIDEO,
272 .id = AV_CODEC_ID_SRGC,
273 .priv_data_size = sizeof(MSCCContext),
275 .close = decode_close,
276 .decode = decode_frame,
277 .capabilities = AV_CODEC_CAP_DR1,
278 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,