1 /*
2  * Mandsoft Screen Capture Codec decoder
3  *
4  * Copyright (c) 2017 Paul B Mahol
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "codec_internal.h"
30 #include "internal.h"
31 #include "zlib_wrapper.h"
32 
33 #include <zlib.h>
34 
35 typedef struct MSCCContext {
36     unsigned          bpp;
37     unsigned int      decomp_size;
38     uint8_t          *decomp_buf;
39     unsigned int      uncomp_size;
40     uint8_t          *uncomp_buf;
41     FFZStream         zstream;
42 
43     uint32_t          pal[256];
44 } MSCCContext;
45 
rle_uncompress(AVCodecContext *avctx, GetByteContext *gb, PutByteContext *pb)46 static int rle_uncompress(AVCodecContext *avctx, GetByteContext *gb, PutByteContext *pb)
47 {
48     MSCCContext *s = avctx->priv_data;
49     unsigned x = 0, y = 0;
50 
51     while (bytestream2_get_bytes_left(gb) > 0) {
52         uint32_t fill;
53         int j;
54         unsigned run = bytestream2_get_byte(gb);
55 
56         if (run) {
57             switch (avctx->bits_per_coded_sample) {
58             case 8:
59                 fill = bytestream2_get_byte(gb);
60                 break;
61             case 16:
62                 fill = bytestream2_get_le16(gb);
63                 break;
64             case 24:
65                 fill = bytestream2_get_le24(gb);
66                 break;
67             case 32:
68                 fill = bytestream2_get_le32(gb);
69                 break;
70             }
71 
72             for (j = 0; j < run; j++) {
73                 switch (avctx->bits_per_coded_sample) {
74                 case 8:
75                     bytestream2_put_byte(pb, fill);
76                     break;
77                 case 16:
78                     bytestream2_put_le16(pb, fill);
79                     break;
80                 case 24:
81                     bytestream2_put_le24(pb, fill);
82                     break;
83                 case 32:
84                     bytestream2_put_le32(pb, fill);
85                     break;
86                 }
87             }
88             x += run;
89         } else {
90             unsigned copy = bytestream2_get_byte(gb);
91 
92             if (copy == 0) {
93                 x = 0;
94                 y++;
95                 bytestream2_seek_p(pb, y * avctx->width * s->bpp, SEEK_SET);
96             } else if (copy == 1) {
97                 return 0;
98             } else if (copy == 2) {
99 
100                 x += bytestream2_get_byte(gb);
101                 y += bytestream2_get_byte(gb);
102 
103                 bytestream2_seek_p(pb, y * avctx->width * s->bpp + x * s->bpp, SEEK_SET);
104             } else {
105                 for (j = 0; j < copy; j++) {
106                     switch (avctx->bits_per_coded_sample) {
107                     case 8:
108                         bytestream2_put_byte(pb, bytestream2_get_byte(gb));
109                         break;
110                     case 16:
111                         bytestream2_put_le16(pb, bytestream2_get_le16(gb));
112                         break;
113                     case 24:
114                         bytestream2_put_le24(pb, bytestream2_get_le24(gb));
115                         break;
116                     case 32:
117                         bytestream2_put_le32(pb, bytestream2_get_le32(gb));
118                         break;
119                     }
120                 }
121 
122                 if (s->bpp == 1 && (copy & 1))
123                     bytestream2_skip(gb, 1);
124                 x += copy;
125             }
126         }
127     }
128 
129     return AVERROR_INVALIDDATA;
130 }
131 
decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)132 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
133                         int *got_frame, AVPacket *avpkt)
134 {
135     MSCCContext *s = avctx->priv_data;
136     z_stream *const zstream = &s->zstream.zstream;
137     const uint8_t *buf = avpkt->data;
138     int buf_size = avpkt->size;
139     GetByteContext gb;
140     PutByteContext pb;
141     int ret, j;
142 
143     if (avpkt->size < 3)
144         return buf_size;
145 
146     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
147         return ret;
148 
149     if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
150         size_t size;
151         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
152 
153         if (pal && size == AVPALETTE_SIZE) {
154             frame->palette_has_changed = 1;
155             for (j = 0; j < 256; j++)
156                 s->pal[j] = 0xFF000000 | AV_RL32(pal + j * 4);
157         } else if (pal) {
158             av_log(avctx, AV_LOG_ERROR,
159                    "Palette size %"SIZE_SPECIFIER" is wrong\n", size);
160         }
161         memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
162     }
163 
164     ret = inflateReset(zstream);
165     if (ret != Z_OK) {
166         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
167         return AVERROR_UNKNOWN;
168     }
169     zstream->next_out  = s->decomp_buf;
170     zstream->avail_out = s->decomp_size;
171     if (avctx->codec_id == AV_CODEC_ID_MSCC) {
172         const uint8_t start = avpkt->data[2] ^ avpkt->data[0];
173 
174         zstream->next_in  = &start;
175         zstream->avail_in = 1;
176         ret = inflate(zstream, Z_NO_FLUSH);
177         if (ret != Z_OK || zstream->avail_in != 0)
178             goto inflate_error;
179 
180         buf      += 3;
181         buf_size -= 3;
182     }
183     zstream->next_in   = buf;
184     zstream->avail_in  = buf_size;
185     ret = inflate(zstream, Z_FINISH);
186     if (ret != Z_STREAM_END) {
187 inflate_error:
188         av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
189         return AVERROR_UNKNOWN;
190     }
191 
192     bytestream2_init(&gb, s->decomp_buf, zstream->total_out);
193     bytestream2_init_writer(&pb, s->uncomp_buf, s->uncomp_size);
194 
195     ret = rle_uncompress(avctx, &gb, &pb);
196     if (ret)
197         return ret;
198 
199     for (j = 0; j < avctx->height; j++) {
200         memcpy(frame->data[0] + (avctx->height - j - 1) * frame->linesize[0],
201                s->uncomp_buf + s->bpp * j * avctx->width, s->bpp * avctx->width);
202     }
203 
204     frame->key_frame = 1;
205     frame->pict_type = AV_PICTURE_TYPE_I;
206 
207     *got_frame = 1;
208 
209     return avpkt->size;
210 }
211 
decode_init(AVCodecContext *avctx)212 static av_cold int decode_init(AVCodecContext *avctx)
213 {
214     MSCCContext *s = avctx->priv_data;
215     int stride;
216 
217     switch (avctx->bits_per_coded_sample) {
218     case  8: avctx->pix_fmt = AV_PIX_FMT_PAL8;   break;
219     case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
220     case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24;  break;
221     case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA;   break;
222     default:
223         av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
224         return AVERROR_INVALIDDATA;
225     }
226 
227     s->bpp = avctx->bits_per_coded_sample >> 3;
228     stride = 4 * ((avctx->width * avctx->bits_per_coded_sample + 31) / 32);
229 
230     s->decomp_size = 2 * avctx->height * stride;
231     if (!(s->decomp_buf = av_malloc(s->decomp_size)))
232         return AVERROR(ENOMEM);
233 
234     s->uncomp_size = avctx->height * stride;
235     if (!(s->uncomp_buf = av_malloc(s->uncomp_size)))
236         return AVERROR(ENOMEM);
237 
238     return ff_inflate_init(&s->zstream, avctx);
239 }
240 
decode_close(AVCodecContext *avctx)241 static av_cold int decode_close(AVCodecContext *avctx)
242 {
243     MSCCContext *s = avctx->priv_data;
244 
245     av_freep(&s->decomp_buf);
246     s->decomp_size = 0;
247     av_freep(&s->uncomp_buf);
248     s->uncomp_size = 0;
249     ff_inflate_end(&s->zstream);
250 
251     return 0;
252 }
253 
254 const FFCodec ff_mscc_decoder = {
255     .p.name           = "mscc",
256     .p.long_name      = NULL_IF_CONFIG_SMALL("Mandsoft Screen Capture Codec"),
257     .p.type           = AVMEDIA_TYPE_VIDEO,
258     .p.id             = AV_CODEC_ID_MSCC,
259     .priv_data_size   = sizeof(MSCCContext),
260     .init             = decode_init,
261     .close            = decode_close,
262     FF_CODEC_DECODE_CB(decode_frame),
263     .p.capabilities   = AV_CODEC_CAP_DR1,
264     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
265 };
266 
267 const FFCodec ff_srgc_decoder = {
268     .p.name           = "srgc",
269     .p.long_name      = NULL_IF_CONFIG_SMALL("Screen Recorder Gold Codec"),
270     .p.type           = AVMEDIA_TYPE_VIDEO,
271     .p.id             = AV_CODEC_ID_SRGC,
272     .priv_data_size   = sizeof(MSCCContext),
273     .init             = decode_init,
274     .close            = decode_close,
275     FF_CODEC_DECODE_CB(decode_frame),
276     .p.capabilities   = AV_CODEC_CAP_DR1,
277     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
278 };
279