2 * RemotelyAnywhere Screen Capture decoder
4 * Copyright (c) 2018 Paul B Mahol
6 * This file is part of FFmpeg.
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.
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.
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
27 #include "libavutil/avassert.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/opt.h"
32 #include "bytestream.h"
37 #define KBND MKTAG('K', 'B', 'N', 'D')
38 #define FINT MKTAG('F', 'I', 'N', 'T')
39 #define INIT MKTAG('I', 'N', 'I', 'T')
40 #define BNDL MKTAG('B', 'N', 'D', 'L')
41 #define KFRM MKTAG('K', 'F', 'R', 'M')
42 #define DLTA MKTAG('D', 'L', 'T', 'A')
43 #define MOUS MKTAG('M', 'O', 'U', 'S')
44 #define MPOS MKTAG('M', 'P', 'O', 'S')
45 #define MOVE MKTAG('M', 'O', 'V', 'E')
46 #define EMPT MKTAG('E', 'M', 'P', 'T')
48 typedef struct RASCContext {
68 static void clear_plane(AVCodecContext *avctx, AVFrame *frame)
70 RASCContext *s = avctx->priv_data;
71 uint8_t *dst = frame->data[0];
73 for (int y = 0; y < avctx->height; y++) {
74 memset(dst, 0, avctx->width * s->bpp);
75 dst += frame->linesize[0];
79 static void copy_plane(AVCodecContext *avctx, AVFrame *src, AVFrame *dst)
81 RASCContext *s = avctx->priv_data;
82 uint8_t *srcp = src->data[0];
83 uint8_t *dstp = dst->data[0];
85 for (int y = 0; y < avctx->height; y++) {
86 memcpy(dstp, srcp, s->stride);
87 srcp += src->linesize[0];
88 dstp += dst->linesize[0];
92 static int init_frames(AVCodecContext *avctx)
94 RASCContext *s = avctx->priv_data;
97 av_frame_unref(s->frame1);
98 av_frame_unref(s->frame2);
99 if ((ret = ff_get_buffer(avctx, s->frame1, 0)) < 0)
102 if ((ret = ff_get_buffer(avctx, s->frame2, 0)) < 0)
105 clear_plane(avctx, s->frame2);
106 clear_plane(avctx, s->frame1);
111 static int decode_fint(AVCodecContext *avctx,
112 AVPacket *avpkt, unsigned size)
114 RASCContext *s = avctx->priv_data;
115 GetByteContext *gb = &s->gb;
119 if (bytestream2_peek_le32(gb) != 0x65) {
120 if (!s->frame2->data[0] || !s->frame1->data[0])
121 return AVERROR_INVALIDDATA;
123 clear_plane(avctx, s->frame2);
124 clear_plane(avctx, s->frame1);
128 bytestream2_skip(gb, 8);
129 w = bytestream2_get_le32(gb);
130 h = bytestream2_get_le32(gb);
131 bytestream2_skip(gb, 30);
132 fmt = bytestream2_get_le16(gb);
133 bytestream2_skip(gb, 24);
136 case 8: s->stride = FFALIGN(w, 4);
138 fmt = AV_PIX_FMT_PAL8; break;
139 case 16: s->stride = w * 2;
141 fmt = AV_PIX_FMT_RGB555LE; break;
142 case 32: s->stride = w * 4;
144 fmt = AV_PIX_FMT_BGR0; break;
145 default: return AVERROR_INVALIDDATA;
148 ret = ff_set_dimensions(avctx, w, h);
153 avctx->pix_fmt = fmt;
155 ret = init_frames(avctx);
159 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
160 uint32_t *pal = (uint32_t *)s->frame2->data[1];
162 for (int i = 0; i < 256; i++)
163 pal[i] = bytestream2_get_le32(gb) | 0xFF000000u;
169 static int decode_zlib(AVCodecContext *avctx, AVPacket *avpkt,
170 unsigned size, unsigned uncompressed_size)
172 RASCContext *s = avctx->priv_data;
173 GetByteContext *gb = &s->gb;
176 zret = inflateReset(&s->zstream);
178 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
179 return AVERROR_EXTERNAL;
182 av_fast_padded_malloc(&s->delta, &s->delta_size, uncompressed_size);
184 return AVERROR(ENOMEM);
186 s->zstream.next_in = avpkt->data + bytestream2_tell(gb);
187 s->zstream.avail_in = FFMIN(size, bytestream2_get_bytes_left(gb));
189 s->zstream.next_out = s->delta;
190 s->zstream.avail_out = s->delta_size;
192 zret = inflate(&s->zstream, Z_FINISH);
193 if (zret != Z_STREAM_END) {
194 av_log(avctx, AV_LOG_ERROR,
195 "Inflate failed with return code: %d.\n", zret);
196 return AVERROR_INVALIDDATA;
202 static int decode_move(AVCodecContext *avctx,
203 AVPacket *avpkt, unsigned size)
205 RASCContext *s = avctx->priv_data;
206 GetByteContext *gb = &s->gb;
208 unsigned pos, compression, nb_moves;
209 unsigned uncompressed_size;
212 pos = bytestream2_tell(gb);
213 bytestream2_skip(gb, 8);
214 nb_moves = bytestream2_get_le32(gb);
215 bytestream2_skip(gb, 8);
216 compression = bytestream2_get_le32(gb);
218 if (nb_moves > INT32_MAX / 16 || nb_moves > avctx->width * avctx->height)
219 return AVERROR_INVALIDDATA;
221 uncompressed_size = 16 * nb_moves;
223 if (compression == 1) {
224 ret = decode_zlib(avctx, avpkt,
225 size - (bytestream2_tell(gb) - pos),
229 bytestream2_init(&mc, s->delta, uncompressed_size);
230 } else if (compression == 0) {
231 bytestream2_init(&mc, avpkt->data + bytestream2_tell(gb),
232 bytestream2_get_bytes_left(gb));
233 } else if (compression == 2) {
234 avpriv_request_sample(avctx, "compression %d", compression);
235 return AVERROR_PATCHWELCOME;
237 return AVERROR_INVALIDDATA;
240 if (bytestream2_get_bytes_left(&mc) < uncompressed_size)
241 return AVERROR_INVALIDDATA;
243 for (int i = 0; i < nb_moves; i++) {
244 int type, start_x, start_y, end_x, end_y, mov_x, mov_y;
245 uint8_t *e2, *b1, *b2;
248 type = bytestream2_get_le16(&mc);
249 start_x = bytestream2_get_le16(&mc);
250 start_y = bytestream2_get_le16(&mc);
251 end_x = bytestream2_get_le16(&mc);
252 end_y = bytestream2_get_le16(&mc);
253 mov_x = bytestream2_get_le16(&mc);
254 mov_y = bytestream2_get_le16(&mc);
255 bytestream2_skip(&mc, 2);
257 if (start_x >= avctx->width || start_y >= avctx->height ||
258 end_x >= avctx->width || end_y >= avctx->height ||
259 mov_x >= avctx->width || mov_y >= avctx->height) {
263 if (start_x >= end_x || start_y >= end_y)
269 if (mov_x + w > avctx->width || mov_y + h > avctx->height)
272 if (!s->frame2->data[0] || !s->frame1->data[0])
273 return AVERROR_INVALIDDATA;
275 b1 = s->frame1->data[0] + s->frame1->linesize[0] * (start_y + h - 1) + start_x * s->bpp;
276 b2 = s->frame2->data[0] + s->frame2->linesize[0] * (start_y + h - 1) + start_x * s->bpp;
277 e2 = s->frame2->data[0] + s->frame2->linesize[0] * (mov_y + h - 1) + mov_x * s->bpp;
280 for (int j = 0; j < h; j++) {
281 memcpy(b1, b2, w * s->bpp);
282 b1 -= s->frame1->linesize[0];
283 b2 -= s->frame2->linesize[0];
285 } else if (type == 1) {
286 for (int j = 0; j < h; j++) {
287 memset(b2, 0, w * s->bpp);
288 b2 -= s->frame2->linesize[0];
290 } else if (type == 0) {
293 av_fast_padded_malloc(&s->delta, &s->delta_size, w * h * s->bpp);
296 return AVERROR(ENOMEM);
298 for (int j = 0; j < h; j++) {
299 memcpy(buffer + j * w * s->bpp, e2, w * s->bpp);
300 e2 -= s->frame2->linesize[0];
303 for (int j = 0; j < h; j++) {
304 memcpy(b2, buffer + j * w * s->bpp, w * s->bpp);
305 b2 -= s->frame2->linesize[0];
308 return AVERROR_INVALIDDATA;
312 bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
318 if (cx >= w * s->bpp) { \
321 b1 -= s->frame1->linesize[0]; \
322 b2 -= s->frame2->linesize[0]; \
326 static int decode_dlta(AVCodecContext *avctx,
327 AVPacket *avpkt, unsigned size)
329 RASCContext *s = avctx->priv_data;
330 GetByteContext *gb = &s->gb;
332 unsigned uncompressed_size, pos;
334 int ret, cx, cy, compression;
337 pos = bytestream2_tell(gb);
338 bytestream2_skip(gb, 12);
339 uncompressed_size = bytestream2_get_le32(gb);
340 x = bytestream2_get_le32(gb);
341 y = bytestream2_get_le32(gb);
342 w = bytestream2_get_le32(gb);
343 h = bytestream2_get_le32(gb);
345 if (x >= avctx->width || y >= avctx->height ||
346 w > avctx->width || h > avctx->height)
347 return AVERROR_INVALIDDATA;
349 if (x + w > avctx->width || y + h > avctx->height)
350 return AVERROR_INVALIDDATA;
352 bytestream2_skip(gb, 4);
353 compression = bytestream2_get_le32(gb);
355 if (compression == 1) {
356 if (w * h * s->bpp * 3 < uncompressed_size)
357 return AVERROR_INVALIDDATA;
358 ret = decode_zlib(avctx, avpkt, size, uncompressed_size);
361 bytestream2_init(&dc, s->delta, uncompressed_size);
362 } else if (compression == 0) {
363 if (bytestream2_get_bytes_left(gb) < uncompressed_size)
364 return AVERROR_INVALIDDATA;
365 bytestream2_init(&dc, avpkt->data + bytestream2_tell(gb),
367 } else if (compression == 2) {
368 avpriv_request_sample(avctx, "compression %d", compression);
369 return AVERROR_PATCHWELCOME;
371 return AVERROR_INVALIDDATA;
374 if (!s->frame2->data[0] || !s->frame1->data[0])
375 return AVERROR_INVALIDDATA;
377 b1 = s->frame1->data[0] + s->frame1->linesize[0] * (y + h - 1) + x * s->bpp;
378 b2 = s->frame2->data[0] + s->frame2->linesize[0] * (y + h - 1) + x * s->bpp;
380 while (bytestream2_get_bytes_left(&dc) > 0) {
381 int type = bytestream2_get_byte(&dc);
382 int len = bytestream2_get_byte(&dc);
387 while (len > 0 && cy > 0) {
393 while (len > 0 && cy > 0) {
404 while (len > 0 && cy > 0) {
405 fill = bytestream2_get_byte(&dc);
413 fill = bytestream2_get_byte(&dc);
414 while (len > 0 && cy > 0) {
415 AV_WL32(b1 + cx, AV_RL32(b2 + cx));
416 AV_WL32(b2 + cx, fill);
422 fill = bytestream2_get_le32(&dc);
423 while (len > 0 && cy > 0) {
424 AV_WL32(b1 + cx, AV_RL32(b2 + cx));
425 AV_WL32(b2 + cx, fill);
431 while (len > 0 && cy > 0) {
437 while (len > 0 && cy > 0) {
440 v0 = AV_RL32(b2 + cx);
441 v1 = AV_RL32(b1 + cx);
442 AV_WL32(b2 + cx, v1);
443 AV_WL32(b1 + cx, v0);
449 while (len > 0 && cy > 0) {
450 fill = bytestream2_get_le32(&dc);
451 AV_WL32(b1 + cx, AV_RL32(b2 + cx));
452 AV_WL32(b2 + cx, fill);
458 avpriv_request_sample(avctx, "runlen %d", type);
459 return AVERROR_INVALIDDATA;
463 bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
468 static int decode_kfrm(AVCodecContext *avctx,
469 AVPacket *avpkt, unsigned size)
471 RASCContext *s = avctx->priv_data;
472 GetByteContext *gb = &s->gb;
477 pos = bytestream2_tell(gb);
478 if (bytestream2_peek_le32(gb) == 0x65) {
479 ret = decode_fint(avctx, avpkt, size);
484 if (!s->frame2->data[0])
485 return AVERROR_INVALIDDATA;
487 zret = inflateReset(&s->zstream);
489 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
490 return AVERROR_EXTERNAL;
493 s->zstream.next_in = avpkt->data + bytestream2_tell(gb);
494 s->zstream.avail_in = bytestream2_get_bytes_left(gb);
496 dst = s->frame2->data[0] + (avctx->height - 1) * s->frame2->linesize[0];
497 for (int i = 0; i < avctx->height; i++) {
498 s->zstream.next_out = dst;
499 s->zstream.avail_out = s->stride;
501 zret = inflate(&s->zstream, Z_SYNC_FLUSH);
502 if (zret != Z_OK && zret != Z_STREAM_END) {
503 av_log(avctx, AV_LOG_ERROR,
504 "Inflate failed with return code: %d.\n", zret);
505 return AVERROR_INVALIDDATA;
508 dst -= s->frame2->linesize[0];
511 dst = s->frame1->data[0] + (avctx->height - 1) * s->frame1->linesize[0];
512 for (int i = 0; i < avctx->height; i++) {
513 s->zstream.next_out = dst;
514 s->zstream.avail_out = s->stride;
516 zret = inflate(&s->zstream, Z_SYNC_FLUSH);
517 if (zret != Z_OK && zret != Z_STREAM_END) {
518 av_log(avctx, AV_LOG_ERROR,
519 "Inflate failed with return code: %d.\n", zret);
520 return AVERROR_INVALIDDATA;
523 dst -= s->frame1->linesize[0];
526 bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
531 static int decode_mous(AVCodecContext *avctx,
532 AVPacket *avpkt, unsigned size)
534 RASCContext *s = avctx->priv_data;
535 GetByteContext *gb = &s->gb;
536 unsigned w, h, pos, uncompressed_size;
539 pos = bytestream2_tell(gb);
540 bytestream2_skip(gb, 8);
541 w = bytestream2_get_le32(gb);
542 h = bytestream2_get_le32(gb);
543 bytestream2_skip(gb, 12);
544 uncompressed_size = bytestream2_get_le32(gb);
546 if (w > avctx->width || h > avctx->height)
547 return AVERROR_INVALIDDATA;
549 if (uncompressed_size != 3 * w * h)
550 return AVERROR_INVALIDDATA;
552 av_fast_padded_malloc(&s->cursor, &s->cursor_size, uncompressed_size);
554 return AVERROR(ENOMEM);
556 ret = decode_zlib(avctx, avpkt,
557 size - (bytestream2_tell(gb) - pos),
561 memcpy(s->cursor, s->delta, uncompressed_size);
563 bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
571 static int decode_mpos(AVCodecContext *avctx,
572 AVPacket *avpkt, unsigned size)
574 RASCContext *s = avctx->priv_data;
575 GetByteContext *gb = &s->gb;
578 pos = bytestream2_tell(gb);
579 bytestream2_skip(gb, 8);
580 s->cursor_x = bytestream2_get_le32(gb);
581 s->cursor_y = bytestream2_get_le32(gb);
583 bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
588 static void draw_cursor(AVCodecContext *avctx)
590 RASCContext *s = avctx->priv_data;
596 if (s->cursor_x >= avctx->width || s->cursor_y >= avctx->height)
599 if (s->cursor_x + s->cursor_w > avctx->width ||
600 s->cursor_y + s->cursor_h > avctx->height)
603 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
604 pal = s->frame->data[1];
605 for (int i = 0; i < s->cursor_h; i++) {
606 for (int j = 0; j < s->cursor_w; j++) {
607 int cr = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 0];
608 int cg = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 1];
609 int cb = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 2];
614 if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
617 dst = s->frame->data[0] + s->frame->linesize[0] * (s->cursor_y + i) + (s->cursor_x + j);
618 for (int k = 0; k < 256; k++) {
619 int pr = pal[k * 4 + 0];
620 int pg = pal[k * 4 + 1];
621 int pb = pal[k * 4 + 2];
623 dist = FFABS(cr - pr) + FFABS(cg - pg) + FFABS(cb - pb);
632 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB555LE) {
633 for (int i = 0; i < s->cursor_h; i++) {
634 for (int j = 0; j < s->cursor_w; j++) {
635 int cr = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 0];
636 int cg = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 1];
637 int cb = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 2];
639 if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
642 cr >>= 3; cg >>=3; cb >>= 3;
643 dst = s->frame->data[0] + s->frame->linesize[0] * (s->cursor_y + i) + 2 * (s->cursor_x + j);
644 AV_WL16(dst, cr | cg << 5 | cb << 10);
647 } else if (avctx->pix_fmt == AV_PIX_FMT_BGR0) {
648 for (int i = 0; i < s->cursor_h; i++) {
649 for (int j = 0; j < s->cursor_w; j++) {
650 int cr = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 0];
651 int cg = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 1];
652 int cb = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 2];
654 if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
657 dst = s->frame->data[0] + s->frame->linesize[0] * (s->cursor_y + i) + 4 * (s->cursor_x + j);
666 static int decode_frame(AVCodecContext *avctx,
667 void *data, int *got_frame,
670 RASCContext *s = avctx->priv_data;
671 GetByteContext *gb = &s->gb;
673 AVFrame *frame = data;
675 bytestream2_init(gb, avpkt->data, avpkt->size);
677 if (bytestream2_peek_le32(gb) == EMPT)
682 while (bytestream2_get_bytes_left(gb) > 0) {
683 unsigned type, size = 0;
685 if (bytestream2_get_bytes_left(gb) < 8)
686 return AVERROR_INVALIDDATA;
688 type = bytestream2_get_le32(gb);
689 if (type == KBND || type == BNDL) {
690 intra = type == KBND;
691 type = bytestream2_get_le32(gb);
694 size = bytestream2_get_le32(gb);
695 if (bytestream2_get_bytes_left(gb) < size)
696 return AVERROR_INVALIDDATA;
701 ret = decode_fint(avctx, avpkt, size);
704 ret = decode_kfrm(avctx, avpkt, size);
707 ret = decode_dlta(avctx, avpkt, size);
710 ret = decode_move(avctx, avpkt, size);
713 ret = decode_mous(avctx, avpkt, size);
716 ret = decode_mpos(avctx, avpkt, size);
719 bytestream2_skip(gb, size);
726 if (!s->frame2->data[0] || !s->frame1->data[0])
727 return AVERROR_INVALIDDATA;
729 if ((ret = ff_get_buffer(avctx, s->frame, 0)) < 0)
732 copy_plane(avctx, s->frame2, s->frame);
733 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
734 memcpy(s->frame->data[1], s->frame2->data[1], 1024);
738 s->frame->key_frame = intra;
739 s->frame->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
746 static av_cold int decode_init(AVCodecContext *avctx)
748 RASCContext *s = avctx->priv_data;
751 s->zstream.zalloc = Z_NULL;
752 s->zstream.zfree = Z_NULL;
753 s->zstream.opaque = Z_NULL;
754 zret = inflateInit(&s->zstream);
756 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
757 return AVERROR_EXTERNAL;
760 s->frame1 = av_frame_alloc();
761 s->frame2 = av_frame_alloc();
762 if (!s->frame1 || !s->frame2)
763 return AVERROR(ENOMEM);
768 static av_cold int decode_close(AVCodecContext *avctx)
770 RASCContext *s = avctx->priv_data;
772 av_freep(&s->cursor);
776 av_frame_free(&s->frame1);
777 av_frame_free(&s->frame2);
778 inflateEnd(&s->zstream);
783 static void decode_flush(AVCodecContext *avctx)
785 RASCContext *s = avctx->priv_data;
787 clear_plane(avctx, s->frame1);
788 clear_plane(avctx, s->frame2);
791 static const AVOption options[] = {
792 { "skip_cursor", "skip the cursor", offsetof(RASCContext, skip_cursor), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
796 static const AVClass rasc_decoder_class = {
797 .class_name = "rasc decoder",
798 .item_name = av_default_item_name,
800 .version = LIBAVUTIL_VERSION_INT,
803 AVCodec ff_rasc_decoder = {
805 .long_name = NULL_IF_CONFIG_SMALL("RemotelyAnywhere Screen Capture"),
806 .type = AVMEDIA_TYPE_VIDEO,
807 .id = AV_CODEC_ID_RASC,
808 .priv_data_size = sizeof(RASCContext),
810 .close = decode_close,
811 .decode = decode_frame,
812 .flush = decode_flush,
813 .capabilities = AV_CODEC_CAP_DR1,
814 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
815 FF_CODEC_CAP_INIT_CLEANUP,
816 .priv_class = &rasc_decoder_class,