1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
|
/*
* RemotelyAnywhere Screen Capture decoder
*
* Copyright (c) 2018 Paul B Mahol
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include "libavutil/opt.h"
#include "avcodec.h"
#include "bytestream.h"
#include "codec_internal.h"
#include "decode.h"
#include "zlib_wrapper.h"
#include <zlib.h>
#define KBND MKTAG('K', 'B', 'N', 'D')
#define FINT MKTAG('F', 'I', 'N', 'T')
#define INIT MKTAG('I', 'N', 'I', 'T')
#define BNDL MKTAG('B', 'N', 'D', 'L')
#define KFRM MKTAG('K', 'F', 'R', 'M')
#define DLTA MKTAG('D', 'L', 'T', 'A')
#define MOUS MKTAG('M', 'O', 'U', 'S')
#define MPOS MKTAG('M', 'P', 'O', 'S')
#define MOVE MKTAG('M', 'O', 'V', 'E')
#define EMPT MKTAG('E', 'M', 'P', 'T')
typedef struct RASCContext {
AVClass *class;
int skip_cursor;
GetByteContext gb;
uint8_t *delta;
int delta_size;
uint8_t *cursor;
int cursor_size;
unsigned cursor_w;
unsigned cursor_h;
unsigned cursor_x;
unsigned cursor_y;
int stride;
int bpp;
AVFrame *frame;
AVFrame *frame1;
AVFrame *frame2;
FFZStream zstream;
} RASCContext;
static void clear_plane(AVCodecContext *avctx, AVFrame *frame)
{
RASCContext *s = avctx->priv_data;
uint8_t *dst = frame->data[0];
if (!dst)
return;
for (int y = 0; y < avctx->height; y++) {
memset(dst, 0, avctx->width * s->bpp);
dst += frame->linesize[0];
}
}
static void copy_plane(AVCodecContext *avctx, AVFrame *src, AVFrame *dst)
{
RASCContext *s = avctx->priv_data;
uint8_t *srcp = src->data[0];
uint8_t *dstp = dst->data[0];
for (int y = 0; y < avctx->height; y++) {
memcpy(dstp, srcp, s->stride);
srcp += src->linesize[0];
dstp += dst->linesize[0];
}
}
static int init_frames(AVCodecContext *avctx)
{
RASCContext *s = avctx->priv_data;
int ret;
av_frame_unref(s->frame1);
av_frame_unref(s->frame2);
if ((ret = ff_get_buffer(avctx, s->frame1, 0)) < 0)
return ret;
if ((ret = ff_get_buffer(avctx, s->frame2, 0)) < 0)
return ret;
clear_plane(avctx, s->frame2);
clear_plane(avctx, s->frame1);
return 0;
}
static int decode_fint(AVCodecContext *avctx,
const AVPacket *avpkt, unsigned size)
{
RASCContext *s = avctx->priv_data;
GetByteContext *gb = &s->gb;
unsigned w, h, fmt;
int ret;
if (bytestream2_peek_le32(gb) != 0x65) {
if (!s->frame2->data[0] || !s->frame1->data[0])
return AVERROR_INVALIDDATA;
clear_plane(avctx, s->frame2);
clear_plane(avctx, s->frame1);
return 0;
}
if (bytestream2_get_bytes_left(gb) < 72)
return AVERROR_INVALIDDATA;
bytestream2_skip(gb, 8);
w = bytestream2_get_le32(gb);
h = bytestream2_get_le32(gb);
bytestream2_skip(gb, 30);
fmt = bytestream2_get_le16(gb);
bytestream2_skip(gb, 24);
switch (fmt) {
case 8: s->stride = FFALIGN(w, 4);
s->bpp = 1;
fmt = AV_PIX_FMT_PAL8; break;
case 16: s->stride = w * 2;
s->bpp = 2;
fmt = AV_PIX_FMT_RGB555LE; break;
case 32: s->stride = w * 4;
s->bpp = 4;
fmt = AV_PIX_FMT_BGR0; break;
default: return AVERROR_INVALIDDATA;
}
ret = ff_set_dimensions(avctx, w, h);
if (ret < 0)
return ret;
avctx->width = w;
avctx->height = h;
avctx->pix_fmt = fmt;
ret = init_frames(avctx);
if (ret < 0)
return ret;
if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
uint32_t *pal = (uint32_t *)s->frame2->data[1];
for (int i = 0; i < 256; i++)
pal[i] = bytestream2_get_le32(gb) | 0xFF000000u;
}
return 0;
}
static int decode_zlib(AVCodecContext *avctx, const AVPacket *avpkt,
unsigned size, unsigned uncompressed_size)
{
RASCContext *s = avctx->priv_data;
z_stream *const zstream = &s->zstream.zstream;
GetByteContext *gb = &s->gb;
int zret;
zret = inflateReset(zstream);
if (zret != Z_OK) {
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
return AVERROR_EXTERNAL;
}
av_fast_padded_malloc(&s->delta, &s->delta_size, uncompressed_size);
if (!s->delta)
return AVERROR(ENOMEM);
zstream->next_in = avpkt->data + bytestream2_tell(gb);
zstream->avail_in = FFMIN(size, bytestream2_get_bytes_left(gb));
zstream->next_out = s->delta;
zstream->avail_out = s->delta_size;
zret = inflate(zstream, Z_FINISH);
if (zret != Z_STREAM_END) {
av_log(avctx, AV_LOG_ERROR,
"Inflate failed with return code: %d.\n", zret);
return AVERROR_INVALIDDATA;
}
return 0;
}
static int decode_move(AVCodecContext *avctx,
const AVPacket *avpkt, unsigned size)
{
RASCContext *s = avctx->priv_data;
GetByteContext *gb = &s->gb;
GetByteContext mc;
unsigned pos, compression, nb_moves;
unsigned uncompressed_size;
int ret;
pos = bytestream2_tell(gb);
bytestream2_skip(gb, 8);
nb_moves = bytestream2_get_le32(gb);
bytestream2_skip(gb, 8);
compression = bytestream2_get_le32(gb);
if (nb_moves > INT32_MAX / 16 || nb_moves > avctx->width * avctx->height)
return AVERROR_INVALIDDATA;
uncompressed_size = 16 * nb_moves;
if (compression == 1) {
ret = decode_zlib(avctx, avpkt,
size - (bytestream2_tell(gb) - pos),
uncompressed_size);
if (ret < 0)
return ret;
bytestream2_init(&mc, s->delta, uncompressed_size);
} else if (compression == 0) {
bytestream2_init(&mc, avpkt->data + bytestream2_tell(gb),
bytestream2_get_bytes_left(gb));
} else if (compression == 2) {
avpriv_request_sample(avctx, "compression %d", compression);
return AVERROR_PATCHWELCOME;
} else {
return AVERROR_INVALIDDATA;
}
if (bytestream2_get_bytes_left(&mc) < uncompressed_size)
return AVERROR_INVALIDDATA;
for (int i = 0; i < nb_moves; i++) {
int type, start_x, start_y, end_x, end_y, mov_x, mov_y;
uint8_t *e2, *b1, *b2;
int w, h;
type = bytestream2_get_le16(&mc);
start_x = bytestream2_get_le16(&mc);
start_y = bytestream2_get_le16(&mc);
end_x = bytestream2_get_le16(&mc);
end_y = bytestream2_get_le16(&mc);
mov_x = bytestream2_get_le16(&mc);
mov_y = bytestream2_get_le16(&mc);
bytestream2_skip(&mc, 2);
if (start_x >= avctx->width || start_y >= avctx->height ||
end_x >= avctx->width || end_y >= avctx->height ||
mov_x >= avctx->width || mov_y >= avctx->height) {
continue;
}
if (start_x >= end_x || start_y >= end_y)
continue;
w = end_x - start_x;
h = end_y - start_y;
if (mov_x + w > avctx->width || mov_y + h > avctx->height)
continue;
if (!s->frame2->data[0] || !s->frame1->data[0])
return AVERROR_INVALIDDATA;
b1 = s->frame1->data[0] + s->frame1->linesize[0] * (start_y + h - 1) + start_x * s->bpp;
b2 = s->frame2->data[0] + s->frame2->linesize[0] * (start_y + h - 1) + start_x * s->bpp;
e2 = s->frame2->data[0] + s->frame2->linesize[0] * (mov_y + h - 1) + mov_x * s->bpp;
if (type == 2) {
for (int j = 0; j < h; j++) {
memcpy(b1, b2, w * s->bpp);
b1 -= s->frame1->linesize[0];
b2 -= s->frame2->linesize[0];
}
} else if (type == 1) {
for (int j = 0; j < h; j++) {
memset(b2, 0, w * s->bpp);
b2 -= s->frame2->linesize[0];
}
} else if (type == 0) {
uint8_t *buffer;
av_fast_padded_malloc(&s->delta, &s->delta_size, w * h * s->bpp);
buffer = s->delta;
if (!buffer)
return AVERROR(ENOMEM);
for (int j = 0; j < h; j++) {
memcpy(buffer + j * w * s->bpp, e2, w * s->bpp);
e2 -= s->frame2->linesize[0];
}
for (int j = 0; j < h; j++) {
memcpy(b2, buffer + j * w * s->bpp, w * s->bpp);
b2 -= s->frame2->linesize[0];
}
} else {
return AVERROR_INVALIDDATA;
}
}
bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
return 0;
}
#define NEXT_LINE \
if (cx >= w * s->bpp) { \
cx = 0; \
cy--; \
b1 -= s->frame1->linesize[0]; \
b2 -= s->frame2->linesize[0]; \
} \
len--;
static int decode_dlta(AVCodecContext *avctx,
const AVPacket *avpkt, unsigned size)
{
RASCContext *s = avctx->priv_data;
GetByteContext *gb = &s->gb;
GetByteContext dc;
unsigned uncompressed_size, pos;
unsigned x, y, w, h;
int ret, cx, cy, compression;
uint8_t *b1, *b2;
pos = bytestream2_tell(gb);
bytestream2_skip(gb, 12);
uncompressed_size = bytestream2_get_le32(gb);
x = bytestream2_get_le32(gb);
y = bytestream2_get_le32(gb);
w = bytestream2_get_le32(gb);
h = bytestream2_get_le32(gb);
if (x >= avctx->width || y >= avctx->height ||
w > avctx->width || h > avctx->height)
return AVERROR_INVALIDDATA;
if (x + w > avctx->width || y + h > avctx->height)
return AVERROR_INVALIDDATA;
bytestream2_skip(gb, 4);
compression = bytestream2_get_le32(gb);
if (compression == 1) {
if (w * h * s->bpp * 3 < uncompressed_size)
return AVERROR_INVALIDDATA;
ret = decode_zlib(avctx, avpkt, size, uncompressed_size);
if (ret < 0)
return ret;
bytestream2_init(&dc, s->delta, uncompressed_size);
} else if (compression == 0) {
if (bytestream2_get_bytes_left(gb) < uncompressed_size)
return AVERROR_INVALIDDATA;
bytestream2_init(&dc, avpkt->data + bytestream2_tell(gb),
uncompressed_size);
} else if (compression == 2) {
avpriv_request_sample(avctx, "compression %d", compression);
return AVERROR_PATCHWELCOME;
} else {
return AVERROR_INVALIDDATA;
}
if (!s->frame2->data[0] || !s->frame1->data[0])
return AVERROR_INVALIDDATA;
b1 = s->frame1->data[0] + s->frame1->linesize[0] * (y + h - 1) + x * s->bpp;
b2 = s->frame2->data[0] + s->frame2->linesize[0] * (y + h - 1) + x * s->bpp;
cx = 0, cy = h;
while (bytestream2_get_bytes_left(&dc) > 0) {
int type = bytestream2_get_byte(&dc);
int len = bytestream2_get_byte(&dc);
unsigned fill;
switch (type) {
case 1:
while (len > 0 && cy > 0) {
cx++;
NEXT_LINE
}
break;
case 2:
while (len > 0 && cy > 0) {
int v0 = b1[cx];
int v1 = b2[cx];
b2[cx] = v0;
b1[cx] = v1;
cx++;
NEXT_LINE
}
break;
case 3:
while (len > 0 && cy > 0) {
fill = bytestream2_get_byte(&dc);
b1[cx] = b2[cx];
b2[cx] = fill;
cx++;
NEXT_LINE
}
break;
case 4:
fill = bytestream2_get_byte(&dc);
while (len > 0 && cy > 0) {
AV_WL32(b1 + cx, AV_RL32(b2 + cx));
AV_WL32(b2 + cx, fill);
cx++;
NEXT_LINE
}
break;
case 7:
fill = bytestream2_get_le32(&dc);
while (len > 0 && cy > 0) {
AV_WL32(b1 + cx, AV_RL32(b2 + cx));
AV_WL32(b2 + cx, fill);
cx += 4;
NEXT_LINE
}
break;
case 10:
while (len > 0 && cy > 0) {
cx += 4;
NEXT_LINE
}
break;
case 12:
while (len > 0 && cy > 0) {
unsigned v0, v1;
v0 = AV_RL32(b2 + cx);
v1 = AV_RL32(b1 + cx);
AV_WL32(b2 + cx, v1);
AV_WL32(b1 + cx, v0);
cx += 4;
NEXT_LINE
}
break;
case 13:
while (len > 0 && cy > 0) {
fill = bytestream2_get_le32(&dc);
AV_WL32(b1 + cx, AV_RL32(b2 + cx));
AV_WL32(b2 + cx, fill);
cx += 4;
NEXT_LINE
}
break;
default:
avpriv_request_sample(avctx, "runlen %d", type);
return AVERROR_INVALIDDATA;
}
}
bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
return 0;
}
static int decode_kfrm(AVCodecContext *avctx,
const AVPacket *avpkt, unsigned size)
{
RASCContext *s = avctx->priv_data;
z_stream *const zstream = &s->zstream.zstream;
GetByteContext *gb = &s->gb;
uint8_t *dst;
unsigned pos;
int zret, ret;
pos = bytestream2_tell(gb);
if (bytestream2_peek_le32(gb) == 0x65) {
ret = decode_fint(avctx, avpkt, size);
if (ret < 0)
return ret;
}
if (!s->frame2->data[0])
return AVERROR_INVALIDDATA;
zret = inflateReset(zstream);
if (zret != Z_OK) {
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
return AVERROR_EXTERNAL;
}
zstream->next_in = avpkt->data + bytestream2_tell(gb);
zstream->avail_in = bytestream2_get_bytes_left(gb);
dst = s->frame2->data[0] + (avctx->height - 1) * s->frame2->linesize[0];
for (int i = 0; i < avctx->height; i++) {
zstream->next_out = dst;
zstream->avail_out = s->stride;
zret = inflate(zstream, Z_SYNC_FLUSH);
if (zret != Z_OK && zret != Z_STREAM_END) {
av_log(avctx, AV_LOG_ERROR,
"Inflate failed with return code: %d.\n", zret);
return AVERROR_INVALIDDATA;
}
dst -= s->frame2->linesize[0];
}
dst = s->frame1->data[0] + (avctx->height - 1) * s->frame1->linesize[0];
for (int i = 0; i < avctx->height; i++) {
zstream->next_out = dst;
zstream->avail_out = s->stride;
zret = inflate(zstream, Z_SYNC_FLUSH);
if (zret != Z_OK && zret != Z_STREAM_END) {
av_log(avctx, AV_LOG_ERROR,
"Inflate failed with return code: %d.\n", zret);
return AVERROR_INVALIDDATA;
}
dst -= s->frame1->linesize[0];
}
bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
return 0;
}
static int decode_mous(AVCodecContext *avctx,
const AVPacket *avpkt, unsigned size)
{
RASCContext *s = avctx->priv_data;
GetByteContext *gb = &s->gb;
unsigned w, h, pos, uncompressed_size;
int ret;
pos = bytestream2_tell(gb);
bytestream2_skip(gb, 8);
w = bytestream2_get_le32(gb);
h = bytestream2_get_le32(gb);
bytestream2_skip(gb, 12);
uncompressed_size = bytestream2_get_le32(gb);
if (w > avctx->width || h > avctx->height)
return AVERROR_INVALIDDATA;
if (uncompressed_size != 3 * w * h)
return AVERROR_INVALIDDATA;
av_fast_padded_malloc(&s->cursor, &s->cursor_size, uncompressed_size);
if (!s->cursor)
return AVERROR(ENOMEM);
ret = decode_zlib(avctx, avpkt,
size - (bytestream2_tell(gb) - pos),
uncompressed_size);
if (ret < 0)
return ret;
memcpy(s->cursor, s->delta, uncompressed_size);
bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
s->cursor_w = w;
s->cursor_h = h;
return 0;
}
static int decode_mpos(AVCodecContext *avctx,
const AVPacket *avpkt, unsigned size)
{
RASCContext *s = avctx->priv_data;
GetByteContext *gb = &s->gb;
unsigned pos;
pos = bytestream2_tell(gb);
bytestream2_skip(gb, 8);
s->cursor_x = bytestream2_get_le32(gb);
s->cursor_y = bytestream2_get_le32(gb);
bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
return 0;
}
static void draw_cursor(AVCodecContext *avctx)
{
RASCContext *s = avctx->priv_data;
uint8_t *dst, *pal;
if (!s->cursor)
return;
if (s->cursor_x >= avctx->width || s->cursor_y >= avctx->height)
return;
if (s->cursor_x + s->cursor_w > avctx->width ||
s->cursor_y + s->cursor_h > avctx->height)
return;
if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
pal = s->frame->data[1];
for (int i = 0; i < s->cursor_h; i++) {
for (int j = 0; j < s->cursor_w; j++) {
int cr = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 0];
int cg = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 1];
int cb = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 2];
int best = INT_MAX;
int index = 0;
int dist;
if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
continue;
dst = s->frame->data[0] + s->frame->linesize[0] * (s->cursor_y + i) + (s->cursor_x + j);
for (int k = 0; k < 256; k++) {
int pr = pal[k * 4 + 0];
int pg = pal[k * 4 + 1];
int pb = pal[k * 4 + 2];
dist = FFABS(cr - pr) + FFABS(cg - pg) + FFABS(cb - pb);
if (dist < best) {
best = dist;
index = k;
}
}
dst[0] = index;
}
}
} else if (avctx->pix_fmt == AV_PIX_FMT_RGB555LE) {
for (int i = 0; i < s->cursor_h; i++) {
for (int j = 0; j < s->cursor_w; j++) {
int cr = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 0];
int cg = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 1];
int cb = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 2];
if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
continue;
cr >>= 3; cg >>=3; cb >>= 3;
dst = s->frame->data[0] + s->frame->linesize[0] * (s->cursor_y + i) + 2 * (s->cursor_x + j);
AV_WL16(dst, cr | cg << 5 | cb << 10);
}
}
} else if (avctx->pix_fmt == AV_PIX_FMT_BGR0) {
for (int i = 0; i < s->cursor_h; i++) {
for (int j = 0; j < s->cursor_w; j++) {
int cr = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 0];
int cg = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 1];
int cb = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 2];
if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
continue;
dst = s->frame->data[0] + s->frame->linesize[0] * (s->cursor_y + i) + 4 * (s->cursor_x + j);
dst[0] = cb;
dst[1] = cg;
dst[2] = cr;
}
}
}
}
static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
int *got_frame, AVPacket *avpkt)
{
RASCContext *s = avctx->priv_data;
GetByteContext *gb = &s->gb;
int ret, intra = 0;
bytestream2_init(gb, avpkt->data, avpkt->size);
if (bytestream2_peek_le32(gb) == EMPT)
return avpkt->size;
s->frame = frame;
while (bytestream2_get_bytes_left(gb) > 0) {
unsigned type, size = 0;
if (bytestream2_get_bytes_left(gb) < 8)
return AVERROR_INVALIDDATA;
type = bytestream2_get_le32(gb);
if (type == KBND || type == BNDL) {
intra = type == KBND;
type = bytestream2_get_le32(gb);
}
size = bytestream2_get_le32(gb);
if (bytestream2_get_bytes_left(gb) < size)
return AVERROR_INVALIDDATA;
switch (type) {
case FINT:
case INIT:
ret = decode_fint(avctx, avpkt, size);
break;
case KFRM:
ret = decode_kfrm(avctx, avpkt, size);
break;
case DLTA:
ret = decode_dlta(avctx, avpkt, size);
break;
case MOVE:
ret = decode_move(avctx, avpkt, size);
break;
case MOUS:
ret = decode_mous(avctx, avpkt, size);
break;
case MPOS:
ret = decode_mpos(avctx, avpkt, size);
break;
default:
bytestream2_skip(gb, size);
ret = 0;
}
if (ret < 0)
return ret;
}
if (!s->frame2->data[0] || !s->frame1->data[0])
return AVERROR_INVALIDDATA;
if ((ret = ff_get_buffer(avctx, s->frame, 0)) < 0)
return ret;
copy_plane(avctx, s->frame2, s->frame);
if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
memcpy(s->frame->data[1], s->frame2->data[1], 1024);
if (!s->skip_cursor)
draw_cursor(avctx);
s->frame->key_frame = intra;
s->frame->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
*got_frame = 1;
return avpkt->size;
}
static av_cold int decode_init(AVCodecContext *avctx)
{
RASCContext *s = avctx->priv_data;
s->frame1 = av_frame_alloc();
s->frame2 = av_frame_alloc();
if (!s->frame1 || !s->frame2)
return AVERROR(ENOMEM);
return ff_inflate_init(&s->zstream, avctx);
}
static av_cold int decode_close(AVCodecContext *avctx)
{
RASCContext *s = avctx->priv_data;
av_freep(&s->cursor);
s->cursor_size = 0;
av_freep(&s->delta);
s->delta_size = 0;
av_frame_free(&s->frame1);
av_frame_free(&s->frame2);
ff_inflate_end(&s->zstream);
return 0;
}
static void decode_flush(AVCodecContext *avctx)
{
RASCContext *s = avctx->priv_data;
clear_plane(avctx, s->frame1);
clear_plane(avctx, s->frame2);
}
static const AVOption options[] = {
{ "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 },
{ NULL },
};
static const AVClass rasc_decoder_class = {
.class_name = "rasc decoder",
.item_name = av_default_item_name,
.option = options,
.version = LIBAVUTIL_VERSION_INT,
};
const FFCodec ff_rasc_decoder = {
.p.name = "rasc",
CODEC_LONG_NAME("RemotelyAnywhere Screen Capture"),
.p.type = AVMEDIA_TYPE_VIDEO,
.p.id = AV_CODEC_ID_RASC,
.priv_data_size = sizeof(RASCContext),
.init = decode_init,
.close = decode_close,
FF_CODEC_DECODE_CB(decode_frame),
.flush = decode_flush,
.p.capabilities = AV_CODEC_CAP_DR1,
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
.p.priv_class = &rasc_decoder_class,
};
|