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