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