1 /*
2  * MatchWare Screen Capture Codec decoder
3  *
4  * Copyright (c) 2018 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 MWSCContext {
36     unsigned int      decomp_size;
37     uint8_t          *decomp_buf;
38     AVFrame          *prev_frame;
39     FFZStream         zstream;
40 } MWSCContext;
41 
rle_uncompress(GetByteContext *gb, PutByteContext *pb, GetByteContext *gbp, int width, int height, int stride, int pb_linesize, int gbp_linesize)42 static int rle_uncompress(GetByteContext *gb, PutByteContext *pb, GetByteContext *gbp,
43                           int width, int height, int stride, int pb_linesize, int gbp_linesize)
44 {
45     int intra = 1, w = 0;
46 
47     bytestream2_seek_p(pb, (height - 1) * pb_linesize, SEEK_SET);
48 
49     while (bytestream2_get_bytes_left(gb) > 0) {
50         uint32_t fill = bytestream2_get_le24(gb);
51         unsigned run = bytestream2_get_byte(gb);
52 
53         if (run == 0) {
54             run = bytestream2_get_le32(gb);
55             for (int j = 0; j < run; j++, w++) {
56                 if (w == width) {
57                     w = 0;
58                     bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
59                 }
60                 bytestream2_put_le24(pb, fill);
61             }
62         } else if (run == 255) {
63             int pos = bytestream2_tell_p(pb);
64 
65             bytestream2_seek(gbp, pos, SEEK_SET);
66             for (int j = 0; j < fill; j++, w++) {
67                 if (w == width) {
68                     w = 0;
69                     bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
70                     bytestream2_seek(gbp, -(gbp_linesize + stride), SEEK_CUR);
71                 }
72                 bytestream2_put_le24(pb, bytestream2_get_le24(gbp));
73             }
74 
75             intra = 0;
76         } else {
77             for (int j = 0; j < run; j++, w++) {
78                 if (w == width) {
79                     w = 0;
80                     bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
81                 }
82                 bytestream2_put_le24(pb, fill);
83             }
84         }
85     }
86 
87     return intra;
88 }
89 
decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)90 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
91                         int *got_frame, AVPacket *avpkt)
92 {
93     MWSCContext *s = avctx->priv_data;
94     z_stream *const zstream = &s->zstream.zstream;
95     const uint8_t *buf = avpkt->data;
96     int buf_size = avpkt->size;
97     GetByteContext gb;
98     GetByteContext gbp;
99     PutByteContext pb;
100     int ret;
101 
102     ret = inflateReset(zstream);
103     if (ret != Z_OK) {
104         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
105         return AVERROR_EXTERNAL;
106     }
107     zstream->next_in   = buf;
108     zstream->avail_in  = buf_size;
109     zstream->next_out  = s->decomp_buf;
110     zstream->avail_out = s->decomp_size;
111     ret = inflate(zstream, Z_FINISH);
112     if (ret != Z_STREAM_END) {
113         av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
114         return AVERROR_EXTERNAL;
115     }
116 
117     if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
118         return ret;
119 
120     bytestream2_init(&gb, s->decomp_buf, zstream->total_out);
121     bytestream2_init(&gbp, s->prev_frame->data[0], avctx->height * s->prev_frame->linesize[0]);
122     bytestream2_init_writer(&pb, frame->data[0], avctx->height * frame->linesize[0]);
123 
124     frame->key_frame = rle_uncompress(&gb, &pb, &gbp, avctx->width, avctx->height, avctx->width * 3,
125                                       frame->linesize[0], s->prev_frame->linesize[0]);
126 
127     frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
128 
129     av_frame_unref(s->prev_frame);
130     if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
131         return ret;
132 
133     *got_frame = 1;
134 
135     return avpkt->size;
136 }
137 
decode_init(AVCodecContext *avctx)138 static av_cold int decode_init(AVCodecContext *avctx)
139 {
140     MWSCContext *s = avctx->priv_data;
141     int64_t size;
142 
143     avctx->pix_fmt = AV_PIX_FMT_BGR24;
144 
145     size = 32LL * avctx->height * avctx->width;
146     if (size >= INT32_MAX)
147         return AVERROR_INVALIDDATA;
148     s->decomp_size = size;
149     if (!(s->decomp_buf = av_malloc(s->decomp_size)))
150         return AVERROR(ENOMEM);
151 
152     s->prev_frame = av_frame_alloc();
153     if (!s->prev_frame)
154         return AVERROR(ENOMEM);
155 
156     return ff_inflate_init(&s->zstream, avctx);
157 }
158 
decode_close(AVCodecContext *avctx)159 static av_cold int decode_close(AVCodecContext *avctx)
160 {
161     MWSCContext *s = avctx->priv_data;
162 
163     av_frame_free(&s->prev_frame);
164     av_freep(&s->decomp_buf);
165     s->decomp_size = 0;
166     ff_inflate_end(&s->zstream);
167 
168     return 0;
169 }
170 
171 const FFCodec ff_mwsc_decoder = {
172     .p.name           = "mwsc",
173     .p.long_name      = NULL_IF_CONFIG_SMALL("MatchWare Screen Capture Codec"),
174     .p.type           = AVMEDIA_TYPE_VIDEO,
175     .p.id             = AV_CODEC_ID_MWSC,
176     .priv_data_size   = sizeof(MWSCContext),
177     .init             = decode_init,
178     .close            = decode_close,
179     FF_CODEC_DECODE_CB(decode_frame),
180     .p.capabilities   = AV_CODEC_CAP_DR1,
181     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
182                         FF_CODEC_CAP_INIT_CLEANUP,
183 };
184