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;
42 static int rle_uncompress(AVCodecContext *avctx, GetByteContext *gb, PutByteContext *pb, int bpp)
44 while (bytestream2_get_bytes_left(gb) > 0) {
47 unsigned run = bytestream2_get_byte(gb);
50 switch (avctx->bits_per_coded_sample) {
52 fill = bytestream2_get_byte(gb);
55 fill = bytestream2_get_le16(gb);
58 fill = bytestream2_get_le24(gb);
61 fill = bytestream2_get_le32(gb);
65 for (j = 0; j < run; j++) {
66 switch (avctx->bits_per_coded_sample) {
68 bytestream2_put_byte(pb, fill);
71 bytestream2_put_le16(pb, fill);
74 bytestream2_put_le24(pb, fill);
77 bytestream2_put_le32(pb, fill);
82 unsigned copy = bytestream2_get_byte(gb);
86 } else if (copy == 2) {
89 x = bytestream2_get_byte(gb);
90 y = bytestream2_get_byte(gb);
92 bytestream2_skip_p(pb, x * bpp);
93 bytestream2_skip_p(pb, y * bpp * avctx->width);
95 for (j = 0; j < copy; j++) {
96 switch (avctx->bits_per_coded_sample) {
98 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
101 bytestream2_put_le16(pb, bytestream2_get_le16(gb));
104 bytestream2_put_le24(pb, bytestream2_get_le24(gb));
107 bytestream2_put_le32(pb, bytestream2_get_le32(gb));
115 return AVERROR_INVALIDDATA;
118 static int decode_frame(AVCodecContext *avctx,
119 void *data, int *got_frame,
122 MSCCContext *s = avctx->priv_data;
123 AVFrame *frame = data;
124 uint8_t *buf = avpkt->data;
125 int buf_size = avpkt->size;
131 return AVERROR_INVALIDDATA;
132 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
135 if (avctx->codec_id == AV_CODEC_ID_MSCC) {
136 avpkt->data[2] ^= avpkt->data[0];
141 ret = inflateReset(&s->zstream);
143 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
144 return AVERROR_UNKNOWN;
146 s->zstream.next_in = buf;
147 s->zstream.avail_in = buf_size;
148 s->zstream.next_out = s->decomp_buf;
149 s->zstream.avail_out = s->decomp_size;
150 ret = inflate(&s->zstream, Z_FINISH);
151 if (ret != Z_STREAM_END) {
152 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
153 return AVERROR_UNKNOWN;
156 bytestream2_init(&gb, s->decomp_buf, s->zstream.total_out);
157 bytestream2_init_writer(&pb, s->uncomp_buf, s->uncomp_size);
159 ret = rle_uncompress(avctx, &gb, &pb, s->bpp);
163 for (j = 0; j < avctx->height; j++) {
164 memcpy(frame->data[0] + (avctx->height - j - 1) * frame->linesize[0],
165 s->uncomp_buf + s->bpp * j * avctx->width, s->bpp * avctx->width);
168 frame->key_frame = 1;
169 frame->pict_type = AV_PICTURE_TYPE_I;
176 static av_cold int decode_init(AVCodecContext *avctx)
178 MSCCContext *s = avctx->priv_data;
181 switch (avctx->bits_per_coded_sample) {
182 case 8: avctx->pix_fmt = AV_PIX_FMT_GRAY8; break;
183 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
184 case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
185 case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
187 av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
188 return AVERROR_INVALIDDATA;
191 s->bpp = avctx->bits_per_coded_sample >> 3;
192 memset(&s->zstream, 0, sizeof(z_stream));
194 s->decomp_size = 4 * avctx->height * ((avctx->width * avctx->bits_per_coded_sample + 31) / 32);
195 if (!(s->decomp_buf = av_malloc(s->decomp_size)))
196 return AVERROR(ENOMEM);
198 s->uncomp_size = 4 * avctx->height * ((avctx->width * avctx->bits_per_coded_sample + 31) / 32);
199 if (!(s->uncomp_buf = av_malloc(s->uncomp_size)))
200 return AVERROR(ENOMEM);
202 s->zstream.zalloc = Z_NULL;
203 s->zstream.zfree = Z_NULL;
204 s->zstream.opaque = Z_NULL;
205 zret = inflateInit(&s->zstream);
207 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
208 return AVERROR_UNKNOWN;
214 static av_cold int decode_close(AVCodecContext *avctx)
216 MSCCContext *s = avctx->priv_data;
218 av_freep(&s->decomp_buf);
220 av_freep(&s->uncomp_buf);
222 inflateEnd(&s->zstream);
227 AVCodec ff_mscc_decoder = {
229 .long_name = NULL_IF_CONFIG_SMALL("Mandsoft Screen Capture Codec"),
230 .type = AVMEDIA_TYPE_VIDEO,
231 .id = AV_CODEC_ID_MSCC,
232 .priv_data_size = sizeof(MSCCContext),
234 .close = decode_close,
235 .decode = decode_frame,
236 .capabilities = AV_CODEC_CAP_DR1,
239 AVCodec ff_srgc_decoder = {
241 .long_name = NULL_IF_CONFIG_SMALL("Screen Recorder Gold Codec"),
242 .type = AVMEDIA_TYPE_VIDEO,
243 .id = AV_CODEC_ID_SRGC,
244 .priv_data_size = sizeof(MSCCContext),
246 .close = decode_close,
247 .decode = decode_frame,
248 .capabilities = AV_CODEC_CAP_DR1,