FFmpeg
Loading...
Searching...
No Matches
rasc.c
Go to the documentation of this file.
1/*
2 * RemotelyAnywhere Screen Capture 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#include <string.h>
25
26#include "libavutil/mem.h"
27#include "libavutil/opt.h"
28
29#include "avcodec.h"
30#include "bytestream.h"
31#include "codec_internal.h"
32#include "decode.h"
33#include "zlib_wrapper.h"
34
35#include <zlib.h>
36
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')
47
69
71{
72 RASCContext *s = avctx->priv_data;
73 uint8_t *dst = frame->data[0];
74
75 if (!dst)
76 return;
77
78 for (int y = 0; y < avctx->height; y++) {
79 memset(dst, 0, avctx->width * s->bpp);
80 dst += frame->linesize[0];
81 }
82}
83
85{
86 RASCContext *s = avctx->priv_data;
87 uint8_t *srcp = src->data[0];
88 uint8_t *dstp = dst->data[0];
89
90 for (int y = 0; y < avctx->height; y++) {
91 memcpy(dstp, srcp, s->stride);
92 srcp += src->linesize[0];
93 dstp += dst->linesize[0];
94 }
95}
96
97static int init_frames(AVCodecContext *avctx)
98{
99 RASCContext *s = avctx->priv_data;
100 int ret;
101
102 av_frame_unref(s->frame1);
103 av_frame_unref(s->frame2);
104 if ((ret = ff_get_buffer(avctx, s->frame1, 0)) < 0)
105 return ret;
106
107 if ((ret = ff_get_buffer(avctx, s->frame2, 0)) < 0)
108 return ret;
109
110 clear_plane(avctx, s->frame2);
111 clear_plane(avctx, s->frame1);
112
113 return 0;
114}
115
116static int decode_fint(AVCodecContext *avctx,
117 const AVPacket *avpkt, unsigned size)
118{
119 RASCContext *s = avctx->priv_data;
120 GetByteContext *gb = &s->gb;
121 unsigned w, h, fmt;
122 int ret;
123
124 if (bytestream2_peek_le32(gb) != 0x65) {
125 if (!s->frame2->data[0] || !s->frame1->data[0])
126 return AVERROR_INVALIDDATA;
127
128 clear_plane(avctx, s->frame2);
129 clear_plane(avctx, s->frame1);
130 return 0;
131 }
132 if (bytestream2_get_bytes_left(gb) < 72)
133 return AVERROR_INVALIDDATA;
134
135 bytestream2_skip(gb, 8);
136 w = bytestream2_get_le32(gb);
137 h = bytestream2_get_le32(gb);
138 bytestream2_skip(gb, 30);
139 fmt = bytestream2_get_le16(gb);
140 bytestream2_skip(gb, 24);
141
142 switch (fmt) {
143 case 8: s->stride = FFALIGN(w, 4);
144 s->bpp = 1;
145 fmt = AV_PIX_FMT_PAL8; break;
146 case 16: s->stride = w * 2;
147 s->bpp = 2;
148 fmt = AV_PIX_FMT_RGB555LE; break;
149 case 32: s->stride = w * 4;
150 s->bpp = 4;
151 fmt = AV_PIX_FMT_BGR0; break;
152 default: return AVERROR_INVALIDDATA;
153 }
154
155 ret = ff_set_dimensions(avctx, w, h);
156 if (ret < 0)
157 return ret;
158 avctx->width = w;
159 avctx->height = h;
160 avctx->pix_fmt = fmt;
161
162 ret = init_frames(avctx);
163 if (ret < 0)
164 return ret;
165
166 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
167 uint32_t *pal = (uint32_t *)s->frame2->data[1];
168
169 for (int i = 0; i < 256; i++)
170 pal[i] = bytestream2_get_le32(gb) | 0xFF000000u;
171 }
172
173 return 0;
174}
175
176static int decode_zlib(AVCodecContext *avctx, const AVPacket *avpkt,
177 unsigned size, unsigned uncompressed_size)
178{
179 RASCContext *s = avctx->priv_data;
180 z_stream *const zstream = &s->zstream.zstream;
181 GetByteContext *gb = &s->gb;
182 int zret;
183
184 zret = inflateReset(zstream);
185 if (zret != Z_OK) {
186 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
187 return AVERROR_EXTERNAL;
188 }
189
190 av_fast_padded_malloc(&s->delta, &s->delta_size, uncompressed_size);
191 if (!s->delta)
192 return AVERROR(ENOMEM);
193
194 zstream->next_in = avpkt->data + bytestream2_tell(gb);
195 zstream->avail_in = FFMIN(size, bytestream2_get_bytes_left(gb));
196
197 zstream->next_out = s->delta;
198 zstream->avail_out = s->delta_size;
199
200 zret = inflate(zstream, Z_FINISH);
201 if (zret != Z_STREAM_END) {
202 av_log(avctx, AV_LOG_ERROR,
203 "Inflate failed with return code: %d.\n", zret);
204 return AVERROR_INVALIDDATA;
205 }
206
207 return 0;
208}
209
210static int decode_move(AVCodecContext *avctx,
211 const AVPacket *avpkt, unsigned size)
212{
213 RASCContext *s = avctx->priv_data;
214 GetByteContext *gb = &s->gb;
216 unsigned pos, compression, nb_moves;
217 unsigned uncompressed_size;
218 int ret;
219
220 pos = bytestream2_tell(gb);
221 bytestream2_skip(gb, 8);
222 nb_moves = bytestream2_get_le32(gb);
223 bytestream2_skip(gb, 8);
224 compression = bytestream2_get_le32(gb);
225
226 if (nb_moves > INT32_MAX / 16 || nb_moves > avctx->width * avctx->height)
227 return AVERROR_INVALIDDATA;
228
229 uncompressed_size = 16 * nb_moves;
230
231 if (compression == 1) {
232 ret = decode_zlib(avctx, avpkt,
233 size - (bytestream2_tell(gb) - pos),
234 uncompressed_size);
235 if (ret < 0)
236 return ret;
237 bytestream2_init(&mc, s->delta, uncompressed_size);
238 } else if (compression == 0) {
241 } else if (compression == 2) {
242 avpriv_request_sample(avctx, "compression %d", compression);
244 } else {
245 return AVERROR_INVALIDDATA;
246 }
247
248 if (bytestream2_get_bytes_left(&mc) < uncompressed_size)
249 return AVERROR_INVALIDDATA;
250
251 for (int i = 0; i < nb_moves; i++) {
252 int type, start_x, start_y, end_x, end_y, mov_x, mov_y;
253 uint8_t *e2, *b1, *b2;
254 int w, h;
255
256 type = bytestream2_get_le16(&mc);
257 start_x = bytestream2_get_le16(&mc);
258 start_y = bytestream2_get_le16(&mc);
259 end_x = bytestream2_get_le16(&mc);
260 end_y = bytestream2_get_le16(&mc);
261 mov_x = bytestream2_get_le16(&mc);
262 mov_y = bytestream2_get_le16(&mc);
263 bytestream2_skip(&mc, 2);
264
265 if (start_x >= avctx->width || start_y >= avctx->height ||
266 end_x >= avctx->width || end_y >= avctx->height ||
267 mov_x >= avctx->width || mov_y >= avctx->height) {
268 continue;
269 }
270
271 if (start_x >= end_x || start_y >= end_y)
272 continue;
273
274 w = end_x - start_x;
275 h = end_y - start_y;
276
277 if (mov_x + w > avctx->width || mov_y + h > avctx->height)
278 continue;
279
280 if (!s->frame2->data[0] || !s->frame1->data[0])
281 return AVERROR_INVALIDDATA;
282
283 b1 = s->frame1->data[0] + s->frame1->linesize[0] * (start_y + h - 1) + start_x * s->bpp;
284 b2 = s->frame2->data[0] + s->frame2->linesize[0] * (start_y + h - 1) + start_x * s->bpp;
285 e2 = s->frame2->data[0] + s->frame2->linesize[0] * (mov_y + h - 1) + mov_x * s->bpp;
286
287 if (type == 2) {
288 for (int j = 0; j < h; j++) {
289 memcpy(b1, b2, w * s->bpp);
290 b1 -= s->frame1->linesize[0];
291 b2 -= s->frame2->linesize[0];
292 }
293 } else if (type == 1) {
294 for (int j = 0; j < h; j++) {
295 memset(b2, 0, w * s->bpp);
296 b2 -= s->frame2->linesize[0];
297 }
298 } else if (type == 0) {
299 av_fast_padded_malloc(&s->mv_scratch, &s->mv_scratch_size, w * h * s->bpp);
300 uint8_t *buffer = s->mv_scratch;
301 if (!buffer)
302 return AVERROR(ENOMEM);
303
304 for (int j = 0; j < h; j++) {
305 memcpy(buffer + j * w * s->bpp, e2, w * s->bpp);
306 e2 -= s->frame2->linesize[0];
307 }
308
309 for (int j = 0; j < h; j++) {
310 memcpy(b2, buffer + j * w * s->bpp, w * s->bpp);
311 b2 -= s->frame2->linesize[0];
312 }
313 } else {
314 return AVERROR_INVALIDDATA;
315 }
316 }
317
319
320 return 0;
321}
322
323static inline int dlta_room(unsigned cx, unsigned w, unsigned bpp, unsigned need)
324{
325 return cx + need <= w * bpp;
326}
327
328#define NEXT_LINE \
329 if (cx >= w * s->bpp) { \
330 cx = 0; \
331 cy--; \
332 b1 -= s->frame1->linesize[0]; \
333 b2 -= s->frame2->linesize[0]; \
334 } \
335 len--;
336
337static int decode_dlta(AVCodecContext *avctx,
338 const AVPacket *avpkt, unsigned size)
339{
340 RASCContext *s = avctx->priv_data;
341 GetByteContext *gb = &s->gb;
343 unsigned uncompressed_size, pos;
344 unsigned x, y, w, h;
345 int ret, cx, cy, compression;
346 uint8_t *b1, *b2;
347
348 pos = bytestream2_tell(gb);
349 bytestream2_skip(gb, 12);
350 uncompressed_size = bytestream2_get_le32(gb);
351 x = bytestream2_get_le32(gb);
352 y = bytestream2_get_le32(gb);
353 w = bytestream2_get_le32(gb);
354 h = bytestream2_get_le32(gb);
355
356 if (x >= avctx->width || y >= avctx->height ||
357 w > avctx->width || h > avctx->height)
358 return AVERROR_INVALIDDATA;
359
360 if (x + w > avctx->width || y + h > avctx->height)
361 return AVERROR_INVALIDDATA;
362
363 bytestream2_skip(gb, 4);
364 compression = bytestream2_get_le32(gb);
365
366 if (compression == 1) {
367 if (w * h * s->bpp * 3 < uncompressed_size)
368 return AVERROR_INVALIDDATA;
369 ret = decode_zlib(avctx, avpkt, size, uncompressed_size);
370 if (ret < 0)
371 return ret;
372 bytestream2_init(&dc, s->delta, uncompressed_size);
373 } else if (compression == 0) {
374 if (bytestream2_get_bytes_left(gb) < uncompressed_size)
375 return AVERROR_INVALIDDATA;
376 bytestream2_init(&dc, avpkt->data + bytestream2_tell(gb),
377 uncompressed_size);
378 } else if (compression == 2) {
379 avpriv_request_sample(avctx, "compression %d", compression);
381 } else {
382 return AVERROR_INVALIDDATA;
383 }
384
385 if (!s->frame2->data[0] || !s->frame1->data[0])
386 return AVERROR_INVALIDDATA;
387
388 b1 = s->frame1->data[0] + s->frame1->linesize[0] * (int)(y + h - 1) + ((int)x) * s->bpp;
389 b2 = s->frame2->data[0] + s->frame2->linesize[0] * (int)(y + h - 1) + ((int)x) * s->bpp;
390 cx = 0, cy = h;
391 while (bytestream2_get_bytes_left(&dc) > 0) {
392 int type = bytestream2_get_byte(&dc);
393 int len = bytestream2_get_byte(&dc);
394 unsigned fill;
395
396 switch (type) {
397 case 1:
398 while (len > 0 && cy > 0) {
399 cx++;
401 }
402 break;
403 case 2:
404 while (len > 0 && cy > 0) {
405 int v0 = b1[cx];
406 int v1 = b2[cx];
407
408 b2[cx] = v0;
409 b1[cx] = v1;
410 cx++;
412 }
413 break;
414 case 3:
415 while (len > 0 && cy > 0) {
416 fill = bytestream2_get_byte(&dc);
417 b1[cx] = b2[cx];
418 b2[cx] = fill;
419 cx++;
421 }
422 break;
423 case 4:
424 fill = bytestream2_get_byte(&dc);
425 while (len > 0 && cy > 0) {
426 if (!dlta_room(cx, w, s->bpp, 4))
427 return AVERROR_INVALIDDATA;
428 AV_WL32(b1 + cx, AV_RL32(b2 + cx));
429 AV_WL32(b2 + cx, fill);
430 cx++;
432 }
433 break;
434 case 7:
435 fill = bytestream2_get_le32(&dc);
436 while (len > 0 && cy > 0) {
437 if (!dlta_room(cx, w, s->bpp, 4))
438 return AVERROR_INVALIDDATA;
439 AV_WL32(b1 + cx, AV_RL32(b2 + cx));
440 AV_WL32(b2 + cx, fill);
441 cx += 4;
443 }
444 break;
445 case 10:
446 while (len > 0 && cy > 0) {
447 cx += 4;
449 }
450 break;
451 case 12:
452 while (len > 0 && cy > 0) {
453 unsigned v0, v1;
454
455 if (!dlta_room(cx, w, s->bpp, 4))
456 return AVERROR_INVALIDDATA;
457 v0 = AV_RL32(b2 + cx);
458 v1 = AV_RL32(b1 + cx);
459 AV_WL32(b2 + cx, v1);
460 AV_WL32(b1 + cx, v0);
461 cx += 4;
463 }
464 break;
465 case 13:
466 while (len > 0 && cy > 0) {
467 fill = bytestream2_get_le32(&dc);
468 if (!dlta_room(cx, w, s->bpp, 4))
469 return AVERROR_INVALIDDATA;
470 AV_WL32(b1 + cx, AV_RL32(b2 + cx));
471 AV_WL32(b2 + cx, fill);
472 cx += 4;
474 }
475 break;
476 default:
477 avpriv_request_sample(avctx, "runlen %d", type);
478 return AVERROR_INVALIDDATA;
479 }
480 }
481
483
484 return 0;
485}
486
487static int decode_kfrm(AVCodecContext *avctx,
488 const AVPacket *avpkt, unsigned size)
489{
490 RASCContext *s = avctx->priv_data;
491 z_stream *const zstream = &s->zstream.zstream;
492 GetByteContext *gb = &s->gb;
493 uint8_t *dst;
494 unsigned pos;
495 int zret, ret;
496
497 pos = bytestream2_tell(gb);
498 if (bytestream2_peek_le32(gb) == 0x65) {
499 ret = decode_fint(avctx, avpkt, size);
500 if (ret < 0)
501 return ret;
502 }
503
504 if (!s->frame2->data[0])
505 return AVERROR_INVALIDDATA;
506
507 zret = inflateReset(zstream);
508 if (zret != Z_OK) {
509 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
510 return AVERROR_EXTERNAL;
511 }
512
513 zstream->next_in = avpkt->data + bytestream2_tell(gb);
514 zstream->avail_in = bytestream2_get_bytes_left(gb);
515
516 dst = s->frame2->data[0] + (avctx->height - 1) * s->frame2->linesize[0];
517 for (int i = 0; i < avctx->height; i++) {
518 zstream->next_out = dst;
519 zstream->avail_out = s->stride;
520
521 zret = inflate(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;
526 }
527
528 dst -= s->frame2->linesize[0];
529 }
530
531 dst = s->frame1->data[0] + (avctx->height - 1) * s->frame1->linesize[0];
532 for (int i = 0; i < avctx->height; i++) {
533 zstream->next_out = dst;
534 zstream->avail_out = s->stride;
535
536 zret = inflate(zstream, Z_SYNC_FLUSH);
537 if (zret != Z_OK && zret != Z_STREAM_END) {
538 av_log(avctx, AV_LOG_ERROR,
539 "Inflate failed with return code: %d.\n", zret);
540 return AVERROR_INVALIDDATA;
541 }
542
543 dst -= s->frame1->linesize[0];
544 }
545
547
548 return 0;
549}
550
551static int decode_mous(AVCodecContext *avctx,
552 const AVPacket *avpkt, unsigned size)
553{
554 RASCContext *s = avctx->priv_data;
555 GetByteContext *gb = &s->gb;
556 unsigned w, h, pos, uncompressed_size;
557 int ret;
558
559 pos = bytestream2_tell(gb);
560 bytestream2_skip(gb, 8);
561 w = bytestream2_get_le32(gb);
562 h = bytestream2_get_le32(gb);
563 bytestream2_skip(gb, 12);
564 uncompressed_size = bytestream2_get_le32(gb);
565
566 if (w > avctx->width || h > avctx->height)
567 return AVERROR_INVALIDDATA;
568
569 if (uncompressed_size != 3 * w * h)
570 return AVERROR_INVALIDDATA;
571
572 av_fast_padded_malloc(&s->cursor, &s->cursor_size, uncompressed_size);
573 if (!s->cursor)
574 return AVERROR(ENOMEM);
575
576 ret = decode_zlib(avctx, avpkt,
577 size - (bytestream2_tell(gb) - pos),
578 uncompressed_size);
579 if (ret < 0)
580 return ret;
581 memcpy(s->cursor, s->delta, uncompressed_size);
582
584
585 s->cursor_w = w;
586 s->cursor_h = h;
587
588 return 0;
589}
590
591static int decode_mpos(AVCodecContext *avctx,
592 const AVPacket *avpkt, unsigned size)
593{
594 RASCContext *s = avctx->priv_data;
595 GetByteContext *gb = &s->gb;
596 unsigned pos;
597
598 pos = bytestream2_tell(gb);
599 bytestream2_skip(gb, 8);
600 s->cursor_x = bytestream2_get_le32(gb);
601 s->cursor_y = bytestream2_get_le32(gb);
602
604
605 return 0;
606}
607
608static void draw_cursor(AVCodecContext *avctx)
609{
610 RASCContext *s = avctx->priv_data;
611 uint8_t *dst, *pal;
612
613 if (!s->cursor)
614 return;
615
616 if (s->cursor_x >= avctx->width || s->cursor_y >= avctx->height)
617 return;
618
619 if (s->cursor_x + s->cursor_w > avctx->width ||
620 s->cursor_y + s->cursor_h > avctx->height)
621 return;
622
623 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
624 pal = s->frame->data[1];
625 for (int i = 0; i < s->cursor_h; i++) {
626 for (int j = 0; j < s->cursor_w; j++) {
627 int cr = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 0];
628 int cg = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 1];
629 int cb = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 2];
630 int best = INT_MAX;
631 int index = 0;
632 int dist;
633
634 if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
635 continue;
636
637 dst = s->frame->data[0] + s->frame->linesize[0] * (int)(s->cursor_y + i) + (int)(s->cursor_x + j);
638 for (int k = 0; k < 256; k++) {
639 int pr = pal[k * 4 + 0];
640 int pg = pal[k * 4 + 1];
641 int pb = pal[k * 4 + 2];
642
643 dist = FFABS(cr - pr) + FFABS(cg - pg) + FFABS(cb - pb);
644 if (dist < best) {
645 best = dist;
646 index = k;
647 }
648 }
649 dst[0] = index;
650 }
651 }
652 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB555LE) {
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];
658
659 if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
660 continue;
661
662 cr >>= 3; cg >>=3; cb >>= 3;
663 dst = s->frame->data[0] + s->frame->linesize[0] * (int)(s->cursor_y + i) + 2 * (s->cursor_x + j);
664 AV_WL16(dst, cr | cg << 5 | cb << 10);
665 }
666 }
667 } else if (avctx->pix_fmt == AV_PIX_FMT_BGR0) {
668 for (int i = 0; i < s->cursor_h; i++) {
669 for (int j = 0; j < s->cursor_w; j++) {
670 int cr = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 0];
671 int cg = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 1];
672 int cb = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 2];
673
674 if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
675 continue;
676
677 dst = s->frame->data[0] + s->frame->linesize[0] * (int)(s->cursor_y + i) + 4 * (s->cursor_x + j);
678 dst[0] = cb;
679 dst[1] = cg;
680 dst[2] = cr;
681 }
682 }
683 }
684}
685
687 int *got_frame, AVPacket *avpkt)
688{
689 RASCContext *s = avctx->priv_data;
690 GetByteContext *gb = &s->gb;
691 int ret, intra = 0;
692
693 bytestream2_init(gb, avpkt->data, avpkt->size);
694
695 if (bytestream2_peek_le32(gb) == EMPT)
696 return avpkt->size;
697
698 s->frame = frame;
699
700 while (bytestream2_get_bytes_left(gb) > 0) {
701 unsigned type, size = 0;
702
703 if (bytestream2_get_bytes_left(gb) < 8)
704 return AVERROR_INVALIDDATA;
705
706 type = bytestream2_get_le32(gb);
707 if (type == KBND || type == BNDL) {
708 intra = type == KBND;
709 type = bytestream2_get_le32(gb);
710 }
711
712 size = bytestream2_get_le32(gb);
714 return AVERROR_INVALIDDATA;
715
716 switch (type) {
717 case FINT:
718 case INIT:
719 ret = decode_fint(avctx, avpkt, size);
720 break;
721 case KFRM:
722 ret = decode_kfrm(avctx, avpkt, size);
723 break;
724 case DLTA:
725 ret = decode_dlta(avctx, avpkt, size);
726 break;
727 case MOVE:
728 ret = decode_move(avctx, avpkt, size);
729 break;
730 case MOUS:
731 ret = decode_mous(avctx, avpkt, size);
732 break;
733 case MPOS:
734 ret = decode_mpos(avctx, avpkt, size);
735 break;
736 default:
738 ret = 0;
739 }
740
741 if (ret < 0)
742 return ret;
743 }
744
745 if (!s->frame2->data[0] || !s->frame1->data[0])
746 return AVERROR_INVALIDDATA;
747
748 if ((ret = ff_get_buffer(avctx, s->frame, 0)) < 0)
749 return ret;
750
751 copy_plane(avctx, s->frame2, s->frame);
752 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
753 memcpy(s->frame->data[1], s->frame2->data[1], 1024);
754 if (!s->skip_cursor)
755 draw_cursor(avctx);
756
757 if (intra)
758 s->frame->flags |= AV_FRAME_FLAG_KEY;
759 else
760 s->frame->flags &= ~AV_FRAME_FLAG_KEY;
761 s->frame->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
762
763 *got_frame = 1;
764
765 return avpkt->size;
766}
767
769{
770 RASCContext *s = avctx->priv_data;
771
772 s->frame1 = av_frame_alloc();
773 s->frame2 = av_frame_alloc();
774 if (!s->frame1 || !s->frame2)
775 return AVERROR(ENOMEM);
776
777 return ff_inflate_init(&s->zstream, avctx);
778}
779
781{
782 RASCContext *s = avctx->priv_data;
783
784 av_freep(&s->cursor);
785 s->cursor_size = 0;
786 av_freep(&s->delta);
787 s->delta_size = 0;
788 av_freep(&s->mv_scratch);
789 s->mv_scratch_size = 0;
790 av_frame_free(&s->frame1);
791 av_frame_free(&s->frame2);
792 ff_inflate_end(&s->zstream);
793
794 return 0;
795}
796
798{
799 RASCContext *s = avctx->priv_data;
800
801 clear_plane(avctx, s->frame1);
802 clear_plane(avctx, s->frame2);
803}
804
805static const AVOption options[] = {
806{ "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 },
807{ NULL },
808};
809
811 .class_name = "rasc decoder",
812 .item_name = av_default_item_name,
813 .option = options,
814 .version = LIBAVUTIL_VERSION_INT,
815};
816
818 .p.name = "rasc",
819 CODEC_LONG_NAME("RemotelyAnywhere Screen Capture"),
820 .p.type = AVMEDIA_TYPE_VIDEO,
821 .p.id = AV_CODEC_ID_RASC,
822 .priv_data_size = sizeof(RASCContext),
823 .init = decode_init,
826 .flush = decode_flush,
827 .p.capabilities = AV_CODEC_CAP_DR1,
828 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
829 .p.priv_class = &rasc_decoder_class,
830};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static av_cold void decode_flush(AVCodecContext *avctx)
Definition agm.c:1253
const FFCodec ff_rasc_decoder
Definition rasc.c:817
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition opt.h:355
#define AV_OPT_FLAG_VIDEO_PARAM
Definition opt.h:357
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_RASC
Definition codec_id.h:286
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition utils.c:53
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:687
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:496
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int index
Definition gxfenc.c:90
cl_device_type type
#define AV_WL32(p, v)
#define AV_RL32(p)
#define AV_WL16(p, v)
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
static av_cold int decode_close(AVCodecContext *avctx)
Definition aacdec.c:1220
#define av_cold
Definition attributes.h:117
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
AVOptions.
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition pixfmt.h:265
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
Definition pixfmt.h:115
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
static int decode_mpos(AVCodecContext *avctx, const AVPacket *avpkt, unsigned size)
Definition rasc.c:591
static int decode_move(AVCodecContext *avctx, const AVPacket *avpkt, unsigned size)
Definition rasc.c:210
static av_cold void decode_flush(AVCodecContext *avctx)
Definition rasc.c:797
#define DLTA
Definition rasc.c:42
static int decode_kfrm(AVCodecContext *avctx, const AVPacket *avpkt, unsigned size)
Definition rasc.c:487
#define MOVE
Definition rasc.c:45
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition rasc.c:686
static av_cold int decode_close(AVCodecContext *avctx)
Definition rasc.c:780
static int init_frames(AVCodecContext *avctx)
Definition rasc.c:97
static const AVClass rasc_decoder_class
Definition rasc.c:810
static int decode_mous(AVCodecContext *avctx, const AVPacket *avpkt, unsigned size)
Definition rasc.c:551
static av_cold int decode_init(AVCodecContext *avctx)
Definition rasc.c:768
#define BNDL
Definition rasc.c:40
static int dlta_room(unsigned cx, unsigned w, unsigned bpp, unsigned need)
Definition rasc.c:323
static void copy_plane(AVCodecContext *avctx, AVFrame *src, AVFrame *dst)
Definition rasc.c:84
#define EMPT
Definition rasc.c:46
static int decode_fint(AVCodecContext *avctx, const AVPacket *avpkt, unsigned size)
Definition rasc.c:116
#define MPOS
Definition rasc.c:44
static int decode_zlib(AVCodecContext *avctx, const AVPacket *avpkt, unsigned size, unsigned uncompressed_size)
Definition rasc.c:176
#define INIT
Definition rasc.c:39
static void clear_plane(AVCodecContext *avctx, AVFrame *frame)
Definition rasc.c:70
#define KFRM
Definition rasc.c:41
#define NEXT_LINE
Definition rasc.c:328
#define FINT
Definition rasc.c:38
static void draw_cursor(AVCodecContext *avctx)
Definition rasc.c:608
#define MOUS
Definition rasc.c:43
#define KBND
Definition rasc.c:37
static int decode_dlta(AVCodecContext *avctx, const AVPacket *avpkt, unsigned size)
Definition rasc.c:337
unsigned int pos
Definition spdifenc.c:431
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
AVFrame * frame2
Definition rasc.c:66
unsigned cursor_w
Definition rasc.c:58
GetByteContext gb
Definition rasc.c:51
int cursor_size
Definition rasc.c:57
AVFrame * frame
Definition rasc.c:64
int delta_size
Definition rasc.c:53
unsigned cursor_x
Definition rasc.c:60
int stride
Definition rasc.c:62
int skip_cursor
Definition rasc.c:50
uint8_t * cursor
Definition rasc.c:56
uint8_t * mv_scratch
Definition rasc.c:54
int bpp
Definition rasc.c:63
unsigned int mv_scratch_size
Definition rasc.c:55
uint8_t * delta
Definition rasc.c:52
unsigned cursor_h
Definition rasc.c:59
AVFrame * frame1
Definition rasc.c:65
unsigned cursor_y
Definition rasc.c:61
FFZStream zstream
Definition rasc.c:67
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static char buffer[20]
Definition seek.c:32
int size
#define mc
static double cr(void *priv, double x, double y)
Definition vf_geq.c:248
static double cb(void *priv, double x, double y)
Definition vf_geq.c:247
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
static double b1(void *priv, double x, double y)
Definition vf_xfade.c:2034
static double b2(void *priv, double x, double y)
Definition vf_xfade.c:2035
int len
void ff_inflate_end(FFZStream *zstream)
Wrapper around inflateEnd().
int ff_inflate_init(FFZStream *zstream, void *logctx)
Wrapper around inflateInit().