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];
76 for (int y = 0; y < avctx->height; y++) {
77 memset(dst, 0, avctx->width * s->bpp);
78 dst += frame->linesize[0];
82 static void copy_plane(AVCodecContext *avctx, AVFrame *src, AVFrame *dst)
84 RASCContext *s = avctx->priv_data;
85 uint8_t *srcp = src->data[0];
86 uint8_t *dstp = dst->data[0];
88 for (int y = 0; y < avctx->height; y++) {
89 memcpy(dstp, srcp, s->stride);
90 srcp += src->linesize[0];
91 dstp += dst->linesize[0];
95 static int init_frames(AVCodecContext *avctx)
97 RASCContext *s = avctx->priv_data;
100 av_frame_unref(s->frame1);
101 av_frame_unref(s->frame2);
102 if ((ret = ff_get_buffer(avctx, s->frame1, 0)) < 0)
105 if ((ret = ff_get_buffer(avctx, s->frame2, 0)) < 0)
108 clear_plane(avctx, s->frame2);
109 clear_plane(avctx, s->frame1);
114 static int decode_fint(AVCodecContext *avctx,
115 const AVPacket *avpkt, unsigned size)
117 RASCContext *s = avctx->priv_data;
118 GetByteContext *gb = &s->gb;
122 if (bytestream2_peek_le32(gb) != 0x65) {
123 if (!s->frame2->data[0] || !s->frame1->data[0])
124 return AVERROR_INVALIDDATA;
126 clear_plane(avctx, s->frame2);
127 clear_plane(avctx, s->frame1);
130 if (bytestream2_get_bytes_left(gb) < 72)
131 return AVERROR_INVALIDDATA;
133 bytestream2_skip(gb, 8);
134 w = bytestream2_get_le32(gb);
135 h = bytestream2_get_le32(gb);
136 bytestream2_skip(gb, 30);
137 fmt = bytestream2_get_le16(gb);
138 bytestream2_skip(gb, 24);
141 case 8: s->stride = FFALIGN(w, 4);
143 fmt = AV_PIX_FMT_PAL8; break;
144 case 16: s->stride = w * 2;
146 fmt = AV_PIX_FMT_RGB555LE; break;
147 case 32: s->stride = w * 4;
149 fmt = AV_PIX_FMT_BGR0; break;
150 default: return AVERROR_INVALIDDATA;
153 ret = ff_set_dimensions(avctx, w, h);
158 avctx->pix_fmt = fmt;
160 ret = init_frames(avctx);
164 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
165 uint32_t *pal = (uint32_t *)s->frame2->data[1];
167 for (int i = 0; i < 256; i++)
168 pal[i] = bytestream2_get_le32(gb) | 0xFF000000u;
174 static int decode_zlib(AVCodecContext *avctx, const AVPacket *avpkt,
175 unsigned size, unsigned uncompressed_size)
177 RASCContext *s = avctx->priv_data;
178 GetByteContext *gb = &s->gb;
181 zret = inflateReset(&s->zstream);
183 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
184 return AVERROR_EXTERNAL;
187 av_fast_padded_malloc(&s->delta, &s->delta_size, uncompressed_size);
189 return AVERROR(ENOMEM);
191 s->zstream.next_in = avpkt->data + bytestream2_tell(gb);
192 s->zstream.avail_in = FFMIN(size, bytestream2_get_bytes_left(gb));
194 s->zstream.next_out = s->delta;
195 s->zstream.avail_out = s->delta_size;
197 zret = inflate(&s->zstream, Z_FINISH);
198 if (zret != Z_STREAM_END) {
199 av_log(avctx, AV_LOG_ERROR,
200 "Inflate failed with return code: %d.\n", zret);
201 return AVERROR_INVALIDDATA;
207 static int decode_move(AVCodecContext *avctx,
208 const AVPacket *avpkt, unsigned size)
210 RASCContext *s = avctx->priv_data;
211 GetByteContext *gb = &s->gb;
213 unsigned pos, compression, nb_moves;
214 unsigned uncompressed_size;
217 pos = bytestream2_tell(gb);
218 bytestream2_skip(gb, 8);
219 nb_moves = bytestream2_get_le32(gb);
220 bytestream2_skip(gb, 8);
221 compression = bytestream2_get_le32(gb);
223 if (nb_moves > INT32_MAX / 16 || nb_moves > avctx->width * avctx->height)
224 return AVERROR_INVALIDDATA;
226 uncompressed_size = 16 * nb_moves;
228 if (compression == 1) {
229 ret = decode_zlib(avctx, avpkt,
230 size - (bytestream2_tell(gb) - pos),
234 bytestream2_init(&mc, s->delta, uncompressed_size);
235 } else if (compression == 0) {
236 bytestream2_init(&mc, avpkt->data + bytestream2_tell(gb),
237 bytestream2_get_bytes_left(gb));
238 } else if (compression == 2) {
239 avpriv_request_sample(avctx, "compression %d", compression);
240 return AVERROR_PATCHWELCOME;
242 return AVERROR_INVALIDDATA;
245 if (bytestream2_get_bytes_left(&mc) < uncompressed_size)
246 return AVERROR_INVALIDDATA;
248 for (int i = 0; i < nb_moves; i++) {
249 int type, start_x, start_y, end_x, end_y, mov_x, mov_y;
250 uint8_t *e2, *b1, *b2;
253 type = bytestream2_get_le16(&mc);
254 start_x = bytestream2_get_le16(&mc);
255 start_y = bytestream2_get_le16(&mc);
256 end_x = bytestream2_get_le16(&mc);
257 end_y = bytestream2_get_le16(&mc);
258 mov_x = bytestream2_get_le16(&mc);
259 mov_y = bytestream2_get_le16(&mc);
260 bytestream2_skip(&mc, 2);
262 if (start_x >= avctx->width || start_y >= avctx->height ||
263 end_x >= avctx->width || end_y >= avctx->height ||
264 mov_x >= avctx->width || mov_y >= avctx->height) {
268 if (start_x >= end_x || start_y >= end_y)
274 if (mov_x + w > avctx->width || mov_y + h > avctx->height)
277 if (!s->frame2->data[0] || !s->frame1->data[0])
278 return AVERROR_INVALIDDATA;
280 b1 = s->frame1->data[0] + s->frame1->linesize[0] * (start_y + h - 1) + start_x * s->bpp;
281 b2 = s->frame2->data[0] + s->frame2->linesize[0] * (start_y + h - 1) + start_x * s->bpp;
282 e2 = s->frame2->data[0] + s->frame2->linesize[0] * (mov_y + h - 1) + mov_x * s->bpp;
285 for (int j = 0; j < h; j++) {
286 memcpy(b1, b2, w * s->bpp);
287 b1 -= s->frame1->linesize[0];
288 b2 -= s->frame2->linesize[0];
290 } else if (type == 1) {
291 for (int j = 0; j < h; j++) {
292 memset(b2, 0, w * s->bpp);
293 b2 -= s->frame2->linesize[0];
295 } else if (type == 0) {
298 av_fast_padded_malloc(&s->delta, &s->delta_size, w * h * s->bpp);
301 return AVERROR(ENOMEM);
303 for (int j = 0; j < h; j++) {
304 memcpy(buffer + j * w * s->bpp, e2, w * s->bpp);
305 e2 -= s->frame2->linesize[0];
308 for (int j = 0; j < h; j++) {
309 memcpy(b2, buffer + j * w * s->bpp, w * s->bpp);
310 b2 -= s->frame2->linesize[0];
313 return AVERROR_INVALIDDATA;
317 bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
323 if (cx >= w * s->bpp) { \
326 b1 -= s->frame1->linesize[0]; \
327 b2 -= s->frame2->linesize[0]; \
331 static int decode_dlta(AVCodecContext *avctx,
332 const AVPacket *avpkt, unsigned size)
334 RASCContext *s = avctx->priv_data;
335 GetByteContext *gb = &s->gb;
337 unsigned uncompressed_size, pos;
339 int ret, cx, cy, compression;
342 pos = bytestream2_tell(gb);
343 bytestream2_skip(gb, 12);
344 uncompressed_size = bytestream2_get_le32(gb);
345 x = bytestream2_get_le32(gb);
346 y = bytestream2_get_le32(gb);
347 w = bytestream2_get_le32(gb);
348 h = bytestream2_get_le32(gb);
350 if (x >= avctx->width || y >= avctx->height ||
351 w > avctx->width || h > avctx->height)
352 return AVERROR_INVALIDDATA;
354 if (x + w > avctx->width || y + h > avctx->height)
355 return AVERROR_INVALIDDATA;
357 bytestream2_skip(gb, 4);
358 compression = bytestream2_get_le32(gb);
360 if (compression == 1) {
361 if (w * h * s->bpp * 3 < uncompressed_size)
362 return AVERROR_INVALIDDATA;
363 ret = decode_zlib(avctx, avpkt, size, uncompressed_size);
366 bytestream2_init(&dc, s->delta, uncompressed_size);
367 } else if (compression == 0) {
368 if (bytestream2_get_bytes_left(gb) < uncompressed_size)
369 return AVERROR_INVALIDDATA;
370 bytestream2_init(&dc, avpkt->data + bytestream2_tell(gb),
372 } else if (compression == 2) {
373 avpriv_request_sample(avctx, "compression %d", compression);
374 return AVERROR_PATCHWELCOME;
376 return AVERROR_INVALIDDATA;
379 if (!s->frame2->data[0] || !s->frame1->data[0])
380 return AVERROR_INVALIDDATA;
382 b1 = s->frame1->data[0] + s->frame1->linesize[0] * (y + h - 1) + x * s->bpp;
383 b2 = s->frame2->data[0] + s->frame2->linesize[0] * (y + h - 1) + x * s->bpp;
385 while (bytestream2_get_bytes_left(&dc) > 0) {
386 int type = bytestream2_get_byte(&dc);
387 int len = bytestream2_get_byte(&dc);
392 while (len > 0 && cy > 0) {
398 while (len > 0 && cy > 0) {
409 while (len > 0 && cy > 0) {
410 fill = bytestream2_get_byte(&dc);
418 fill = bytestream2_get_byte(&dc);
419 while (len > 0 && cy > 0) {
420 AV_WL32(b1 + cx, AV_RL32(b2 + cx));
421 AV_WL32(b2 + cx, fill);
427 fill = bytestream2_get_le32(&dc);
428 while (len > 0 && cy > 0) {
429 AV_WL32(b1 + cx, AV_RL32(b2 + cx));
430 AV_WL32(b2 + cx, fill);
436 while (len > 0 && cy > 0) {
442 while (len > 0 && cy > 0) {
445 v0 = AV_RL32(b2 + cx);
446 v1 = AV_RL32(b1 + cx);
447 AV_WL32(b2 + cx, v1);
448 AV_WL32(b1 + cx, v0);
454 while (len > 0 && cy > 0) {
455 fill = bytestream2_get_le32(&dc);
456 AV_WL32(b1 + cx, AV_RL32(b2 + cx));
457 AV_WL32(b2 + cx, fill);
463 avpriv_request_sample(avctx, "runlen %d", type);
464 return AVERROR_INVALIDDATA;
468 bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
473 static int decode_kfrm(AVCodecContext *avctx,
474 const AVPacket *avpkt, unsigned size)
476 RASCContext *s = avctx->priv_data;
477 GetByteContext *gb = &s->gb;
482 pos = bytestream2_tell(gb);
483 if (bytestream2_peek_le32(gb) == 0x65) {
484 ret = decode_fint(avctx, avpkt, size);
489 if (!s->frame2->data[0])
490 return AVERROR_INVALIDDATA;
492 zret = inflateReset(&s->zstream);
494 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
495 return AVERROR_EXTERNAL;
498 s->zstream.next_in = avpkt->data + bytestream2_tell(gb);
499 s->zstream.avail_in = bytestream2_get_bytes_left(gb);
501 dst = s->frame2->data[0] + (avctx->height - 1) * s->frame2->linesize[0];
502 for (int i = 0; i < avctx->height; i++) {
503 s->zstream.next_out = dst;
504 s->zstream.avail_out = s->stride;
506 zret = inflate(&s->zstream, Z_SYNC_FLUSH);
507 if (zret != Z_OK && zret != Z_STREAM_END) {
508 av_log(avctx, AV_LOG_ERROR,
509 "Inflate failed with return code: %d.\n", zret);
510 return AVERROR_INVALIDDATA;
513 dst -= s->frame2->linesize[0];
516 dst = s->frame1->data[0] + (avctx->height - 1) * s->frame1->linesize[0];
517 for (int i = 0; i < avctx->height; i++) {
518 s->zstream.next_out = dst;
519 s->zstream.avail_out = s->stride;
521 zret = inflate(&s->zstream, Z_SYNC_FLUSH);
522 if (zret != Z_OK && zret != Z_STREAM_END) {
523 av_log(avctx, AV_LOG_ERROR,
524 "Inflate failed with return code: %d.\n", zret);
525 return AVERROR_INVALIDDATA;
528 dst -= s->frame1->linesize[0];
531 bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
536 static int decode_mous(AVCodecContext *avctx,
537 const AVPacket *avpkt, unsigned size)
539 RASCContext *s = avctx->priv_data;
540 GetByteContext *gb = &s->gb;
541 unsigned w, h, pos, uncompressed_size;
544 pos = bytestream2_tell(gb);
545 bytestream2_skip(gb, 8);
546 w = bytestream2_get_le32(gb);
547 h = bytestream2_get_le32(gb);
548 bytestream2_skip(gb, 12);
549 uncompressed_size = bytestream2_get_le32(gb);
551 if (w > avctx->width || h > avctx->height)
552 return AVERROR_INVALIDDATA;
554 if (uncompressed_size != 3 * w * h)
555 return AVERROR_INVALIDDATA;
557 av_fast_padded_malloc(&s->cursor, &s->cursor_size, uncompressed_size);
559 return AVERROR(ENOMEM);
561 ret = decode_zlib(avctx, avpkt,
562 size - (bytestream2_tell(gb) - pos),
566 memcpy(s->cursor, s->delta, uncompressed_size);
568 bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
576 static int decode_mpos(AVCodecContext *avctx,
577 const AVPacket *avpkt, unsigned size)
579 RASCContext *s = avctx->priv_data;
580 GetByteContext *gb = &s->gb;
583 pos = bytestream2_tell(gb);
584 bytestream2_skip(gb, 8);
585 s->cursor_x = bytestream2_get_le32(gb);
586 s->cursor_y = bytestream2_get_le32(gb);
588 bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
593 static void draw_cursor(AVCodecContext *avctx)
595 RASCContext *s = avctx->priv_data;
601 if (s->cursor_x >= avctx->width || s->cursor_y >= avctx->height)
604 if (s->cursor_x + s->cursor_w > avctx->width ||
605 s->cursor_y + s->cursor_h > avctx->height)
608 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
609 pal = s->frame->data[1];
610 for (int i = 0; i < s->cursor_h; i++) {
611 for (int j = 0; j < s->cursor_w; j++) {
612 int cr = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 0];
613 int cg = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 1];
614 int cb = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 2];
619 if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
622 dst = s->frame->data[0] + s->frame->linesize[0] * (s->cursor_y + i) + (s->cursor_x + j);
623 for (int k = 0; k < 256; k++) {
624 int pr = pal[k * 4 + 0];
625 int pg = pal[k * 4 + 1];
626 int pb = pal[k * 4 + 2];
628 dist = FFABS(cr - pr) + FFABS(cg - pg) + FFABS(cb - pb);
637 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB555LE) {
638 for (int i = 0; i < s->cursor_h; i++) {
639 for (int j = 0; j < s->cursor_w; j++) {
640 int cr = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 0];
641 int cg = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 1];
642 int cb = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 2];
644 if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
647 cr >>= 3; cg >>=3; cb >>= 3;
648 dst = s->frame->data[0] + s->frame->linesize[0] * (s->cursor_y + i) + 2 * (s->cursor_x + j);
649 AV_WL16(dst, cr | cg << 5 | cb << 10);
652 } else if (avctx->pix_fmt == AV_PIX_FMT_BGR0) {
653 for (int i = 0; i < s->cursor_h; i++) {
654 for (int j = 0; j < s->cursor_w; j++) {
655 int cr = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 0];
656 int cg = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 1];
657 int cb = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 2];
659 if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
662 dst = s->frame->data[0] + s->frame->linesize[0] * (s->cursor_y + i) + 4 * (s->cursor_x + j);
671 static int decode_frame(AVCodecContext *avctx,
672 void *data, int *got_frame,
675 RASCContext *s = avctx->priv_data;
676 GetByteContext *gb = &s->gb;
678 AVFrame *frame = data;
680 bytestream2_init(gb, avpkt->data, avpkt->size);
682 if (bytestream2_peek_le32(gb) == EMPT)
687 while (bytestream2_get_bytes_left(gb) > 0) {
688 unsigned type, size = 0;
690 if (bytestream2_get_bytes_left(gb) < 8)
691 return AVERROR_INVALIDDATA;
693 type = bytestream2_get_le32(gb);
694 if (type == KBND || type == BNDL) {
695 intra = type == KBND;
696 type = bytestream2_get_le32(gb);
699 size = bytestream2_get_le32(gb);
700 if (bytestream2_get_bytes_left(gb) < size)
701 return AVERROR_INVALIDDATA;
706 ret = decode_fint(avctx, avpkt, size);
709 ret = decode_kfrm(avctx, avpkt, size);
712 ret = decode_dlta(avctx, avpkt, size);
715 ret = decode_move(avctx, avpkt, size);
718 ret = decode_mous(avctx, avpkt, size);
721 ret = decode_mpos(avctx, avpkt, size);
724 bytestream2_skip(gb, size);
731 if (!s->frame2->data[0] || !s->frame1->data[0])
732 return AVERROR_INVALIDDATA;
734 if ((ret = ff_get_buffer(avctx, s->frame, 0)) < 0)
737 copy_plane(avctx, s->frame2, s->frame);
738 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
739 memcpy(s->frame->data[1], s->frame2->data[1], 1024);
743 s->frame->key_frame = intra;
744 s->frame->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
751 static av_cold int decode_init(AVCodecContext *avctx)
753 RASCContext *s = avctx->priv_data;
756 s->zstream.zalloc = Z_NULL;
757 s->zstream.zfree = Z_NULL;
758 s->zstream.opaque = Z_NULL;
759 zret = inflateInit(&s->zstream);
761 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
762 return AVERROR_EXTERNAL;
765 s->frame1 = av_frame_alloc();
766 s->frame2 = av_frame_alloc();
767 if (!s->frame1 || !s->frame2)
768 return AVERROR(ENOMEM);
773 static av_cold int decode_close(AVCodecContext *avctx)
775 RASCContext *s = avctx->priv_data;
777 av_freep(&s->cursor);
781 av_frame_free(&s->frame1);
782 av_frame_free(&s->frame2);
783 inflateEnd(&s->zstream);
788 static void decode_flush(AVCodecContext *avctx)
790 RASCContext *s = avctx->priv_data;
792 clear_plane(avctx, s->frame1);
793 clear_plane(avctx, s->frame2);
796 static const AVOption options[] = {
797 { "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 },
801 static const AVClass rasc_decoder_class = {
802 .class_name = "rasc decoder",
803 .item_name = av_default_item_name,
805 .version = LIBAVUTIL_VERSION_INT,
808 AVCodec ff_rasc_decoder = {
810 .long_name = NULL_IF_CONFIG_SMALL("RemotelyAnywhere Screen Capture"),
811 .type = AVMEDIA_TYPE_VIDEO,
812 .id = AV_CODEC_ID_RASC,
813 .priv_data_size = sizeof(RASCContext),
815 .close = decode_close,
816 .decode = decode_frame,
817 .flush = decode_flush,
818 .capabilities = AV_CODEC_CAP_DR1,
819 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
820 FF_CODEC_CAP_INIT_CLEANUP,
821 .priv_class = &rasc_decoder_class,