1 /*
2 * innoHeim/Rsupport Screen Capture Codec
3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * innoHeim/Rsupport Screen Capture Codec decoder
25 *
26 * Fourcc: ISCC, RSCC
27 *
28 * Lossless codec, data stored in tiles, with optional deflate compression.
29 *
30 * Header contains the number of tiles in a frame with the tile coordinates,
31 * and it can be deflated or not. Similarly, pixel data comes after the header
32 * and a variable size value, and it can be deflated or just raw.
33 *
34 * Supports: PAL8, BGRA, BGR24, RGB555
35 */
36
37 #include <stdint.h>
38 #include <string.h>
39 #include <zlib.h>
40
43
48
50
55
62
64
65 /* zlib interaction */
70
72 {
74
75 /* These needs to be set to estimate uncompressed buffer */
81 }
82
83 /* Allocate reference frame */
87
88 /* Get pixel format and the size of the pixel */
93 ctx->component_size = 4;
94 } else {
96 ctx->component_size = 3;
97 }
98 } else {
100 ctx->component_size = 4;
101 }
105 case 8:
107 break;
108 case 16:
110 break;
111 case 24:
113 break;
114 case 32:
116 break;
117 default:
121 }
122 } else {
124 ctx->component_size = 4;
126 }
127
128 /* Store the value to check for keyframes */
130
131 /* Allocate maximum size possible, a full frame */
133 if (!
ctx->inflated_buf)
135
136 return 0;
137 }
138
140 {
142
146
147 return 0;
148 }
149
152 {
156 const uint8_t *pixels, *raw;
157 uint8_t *inflated_tiles =
NULL;
158 int tiles_nb, packed_size, pixel_size = 0;
160
162
163 /* Size check */
167 }
168
169 /* Read number of tiles, and allocate the array */
170 tiles_nb = bytestream2_get_le16(gbc);
171
172 if (tiles_nb == 0) {
175 }
176
178 tiles_nb *
sizeof(*
ctx->tiles));
181 goto end;
182 }
183
185
186 /* When there are more than 5 tiles, they are packed together with
187 * a size header. When that size does not match the number of tiles
188 * times the tile size, it means it needs to be inflated as well */
189 if (tiles_nb > 5) {
190 uLongf packed_tiles_size;
191
192 if (tiles_nb < 32)
193 packed_tiles_size = bytestream2_get_byte(gbc);
194 else
195 packed_tiles_size = bytestream2_get_le16(gbc);
196
197 ff_dlog(avctx,
"packed tiles of size %lu.\n", packed_tiles_size);
198
199 /* If necessary, uncompress tiles, and hijack the bytestream reader */
200 if (packed_tiles_size != tiles_nb *
TILE_SIZE) {
202
205 goto end;
206 }
207
209 if (!inflated_tiles) {
211 goto end;
212 }
213
214 ret = uncompress(inflated_tiles, &length,
215 gbc->
buffer, packed_tiles_size);
219 goto end;
220 }
221
222 /* Skip the compressed tile section in the main byte reader,
223 * and point it to read the newly uncompressed data */
226 gbc = &tiles_gbc;
227 }
228 }
229
230 /* Fill in array of tiles, keeping track of how many pixels are updated */
231 for (
i = 0;
i < tiles_nb;
i++) {
232 ctx->tiles[
i].x = bytestream2_get_le16(gbc);
233 ctx->tiles[
i].w = bytestream2_get_le16(gbc);
234 ctx->tiles[
i].y = bytestream2_get_le16(gbc);
235 ctx->tiles[
i].h = bytestream2_get_le16(gbc);
236
237 if (pixel_size +
ctx->tiles[
i].w * (int64_t)
ctx->tiles[
i].h *
ctx->component_size > INT_MAX) {
240 goto end;
241 }
242
243 pixel_size +=
ctx->tiles[
i].w *
ctx->tiles[
i].h *
ctx->component_size;
244
245 ff_dlog(avctx,
"tile %d orig(%d,%d) %dx%d.\n",
i,
248
249 if (
ctx->tiles[
i].w == 0 ||
ctx->tiles[
i].h == 0) {
251 "invalid tile %d at (%d.%d) with size %dx%d.\n",
i,
255 goto end;
256 }
else if (
ctx->tiles[
i].x +
ctx->tiles[
i].w > avctx->
width ||
259 "out of bounds tile %d at (%d.%d) with size %dx%d.\n",
i,
263 goto end;
264 }
265 }
266
267 /* Reset the reader in case it had been modified before */
269
270 /* Extract how much pixel data the tiles contain */
271 if (pixel_size < 0x100)
272 packed_size = bytestream2_get_byte(gbc);
273 else if (pixel_size < 0x10000)
274 packed_size = bytestream2_get_le16(gbc);
275 else if (pixel_size < 0x1000000)
276 packed_size = bytestream2_get_le24(gbc);
277 else
278 packed_size = bytestream2_get_le32(gbc);
279
280 ff_dlog(avctx,
"pixel_size %d packed_size %d.\n", pixel_size, packed_size);
281
282 if (packed_size < 0) {
285 goto end;
286 }
287
288 /* Get pixels buffer, it may be deflated or just raw */
289 if (pixel_size == packed_size) {
293 goto end;
294 }
296 } else {
297 uLongf
len =
ctx->inflated_size;
301 goto end;
302 }
303 if (
ctx->inflated_size < pixel_size) {
305 goto end;
306 }
311 goto end;
312 }
313 pixels =
ctx->inflated_buf;
314 }
315
316 /* Allocate when needed */
319 goto end;
320
321 /* Pointer to actual pixels, will be updated when data is consumed */
322 raw = pixels;
323 for (
i = 0;
i < tiles_nb;
i++) {
324 uint8_t *dst =
ctx->reference->data[0] +
ctx->reference->linesize[0] *
326 ctx->tiles[
i].x *
ctx->component_size;
328 raw,
ctx->tiles[
i].w *
ctx->component_size,
329 ctx->tiles[
i].w *
ctx->component_size,
331 raw +=
ctx->tiles[
i].w *
ctx->component_size *
ctx->tiles[
i].h;
332 }
333
334 /* Frame is ready to be output */
337 goto end;
338
339 /* Keyframe when the number of pixels updated matches the whole surface */
340 if (pixel_size ==
ctx->inflated_size) {
342 frame->key_frame = 1;
343 } else {
345 }
346
347 /* Palette handling */
351 }
352 // We only return a picture when enough of it is undamaged, this avoids copying nearly broken frames around
353 if (
ctx->valid_pixels <
ctx->inflated_size)
354 ctx->valid_pixels += pixel_size;
356 *got_frame = 1;
357
359 end:
362 }
363
375 };