FFmpeg  4.4.9
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 <stdlib.h>
25 #include <string.h>
26 
27 #include "libavutil/avassert.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/opt.h"
30 
31 #include "avcodec.h"
32 #include "bytestream.h"
33 #include "internal.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 
48 typedef struct RASCContext {
49  AVClass *class;
55  unsigned int mv_scratch_size;
58  unsigned cursor_w;
59  unsigned cursor_h;
60  unsigned cursor_x;
61  unsigned cursor_y;
62  int stride;
63  int bpp;
64  z_stream zstream;
68 } RASCContext;
69 
70 static void clear_plane(AVCodecContext *avctx, AVFrame *frame)
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 
84 static void copy_plane(AVCodecContext *avctx, AVFrame *src, AVFrame *dst)
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 
97 static 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 
116 static 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 
176 static int decode_zlib(AVCodecContext *avctx, const AVPacket *avpkt,
177  unsigned size, unsigned uncompressed_size)
178 {
179  RASCContext *s = avctx->priv_data;
180  GetByteContext *gb = &s->gb;
181  int zret;
182 
183  zret = inflateReset(&s->zstream);
184  if (zret != Z_OK) {
185  av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
186  return AVERROR_EXTERNAL;
187  }
188 
189  av_fast_padded_malloc(&s->delta, &s->delta_size, uncompressed_size);
190  if (!s->delta)
191  return AVERROR(ENOMEM);
192 
193  s->zstream.next_in = avpkt->data + bytestream2_tell(gb);
194  s->zstream.avail_in = FFMIN(size, bytestream2_get_bytes_left(gb));
195 
196  s->zstream.next_out = s->delta;
197  s->zstream.avail_out = s->delta_size;
198 
199  zret = inflate(&s->zstream, Z_FINISH);
200  if (zret != Z_STREAM_END) {
201  av_log(avctx, AV_LOG_ERROR,
202  "Inflate failed with return code: %d.\n", zret);
203  return AVERROR_INVALIDDATA;
204  }
205 
206  return 0;
207 }
208 
209 static int decode_move(AVCodecContext *avctx,
210  const AVPacket *avpkt, unsigned size)
211 {
212  RASCContext *s = avctx->priv_data;
213  GetByteContext *gb = &s->gb;
215  unsigned pos, compression, nb_moves;
216  unsigned uncompressed_size;
217  int ret;
218 
219  pos = bytestream2_tell(gb);
220  bytestream2_skip(gb, 8);
221  nb_moves = bytestream2_get_le32(gb);
222  bytestream2_skip(gb, 8);
223  compression = bytestream2_get_le32(gb);
224 
225  if (nb_moves > INT32_MAX / 16 || nb_moves > avctx->width * avctx->height)
226  return AVERROR_INVALIDDATA;
227 
228  uncompressed_size = 16 * nb_moves;
229 
230  if (compression == 1) {
231  ret = decode_zlib(avctx, avpkt,
232  size - (bytestream2_tell(gb) - pos),
233  uncompressed_size);
234  if (ret < 0)
235  return ret;
236  bytestream2_init(&mc, s->delta, uncompressed_size);
237  } else if (compression == 0) {
238  bytestream2_init(&mc, avpkt->data + bytestream2_tell(gb),
240  } else if (compression == 2) {
241  avpriv_request_sample(avctx, "compression %d", compression);
242  return AVERROR_PATCHWELCOME;
243  } else {
244  return AVERROR_INVALIDDATA;
245  }
246 
247  if (bytestream2_get_bytes_left(&mc) < uncompressed_size)
248  return AVERROR_INVALIDDATA;
249 
250  for (int i = 0; i < nb_moves; i++) {
251  int type, start_x, start_y, end_x, end_y, mov_x, mov_y;
252  uint8_t *e2, *b1, *b2;
253  int w, h;
254 
255  type = bytestream2_get_le16(&mc);
256  start_x = bytestream2_get_le16(&mc);
257  start_y = bytestream2_get_le16(&mc);
258  end_x = bytestream2_get_le16(&mc);
259  end_y = bytestream2_get_le16(&mc);
260  mov_x = bytestream2_get_le16(&mc);
261  mov_y = bytestream2_get_le16(&mc);
262  bytestream2_skip(&mc, 2);
263 
264  if (start_x >= avctx->width || start_y >= avctx->height ||
265  end_x >= avctx->width || end_y >= avctx->height ||
266  mov_x >= avctx->width || mov_y >= avctx->height) {
267  continue;
268  }
269 
270  if (start_x >= end_x || start_y >= end_y)
271  continue;
272 
273  w = end_x - start_x;
274  h = end_y - start_y;
275 
276  if (mov_x + w > avctx->width || mov_y + h > avctx->height)
277  continue;
278 
279  if (!s->frame2->data[0] || !s->frame1->data[0])
280  return AVERROR_INVALIDDATA;
281 
282  b1 = s->frame1->data[0] + s->frame1->linesize[0] * (start_y + h - 1) + start_x * s->bpp;
283  b2 = s->frame2->data[0] + s->frame2->linesize[0] * (start_y + h - 1) + start_x * s->bpp;
284  e2 = s->frame2->data[0] + s->frame2->linesize[0] * (mov_y + h - 1) + mov_x * s->bpp;
285 
286  if (type == 2) {
287  for (int j = 0; j < h; j++) {
288  memcpy(b1, b2, w * s->bpp);
289  b1 -= s->frame1->linesize[0];
290  b2 -= s->frame2->linesize[0];
291  }
292  } else if (type == 1) {
293  for (int j = 0; j < h; j++) {
294  memset(b2, 0, w * s->bpp);
295  b2 -= s->frame2->linesize[0];
296  }
297  } else if (type == 0) {
298  av_fast_padded_malloc(&s->mv_scratch, &s->mv_scratch_size, w * h * s->bpp);
299  uint8_t *buffer = s->mv_scratch;
300  if (!buffer)
301  return AVERROR(ENOMEM);
302 
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];
306  }
307 
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];
311  }
312  } else {
313  return AVERROR_INVALIDDATA;
314  }
315  }
316 
317  bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
318 
319  return 0;
320 }
321 
322 static inline int dlta_room(unsigned cx, unsigned w, unsigned bpp, unsigned need)
323 {
324  return cx + need <= w * bpp;
325 }
326 
327 #define NEXT_LINE \
328  if (cx >= w * s->bpp) { \
329  cx = 0; \
330  cy--; \
331  b1 -= s->frame1->linesize[0]; \
332  b2 -= s->frame2->linesize[0]; \
333  } \
334  len--;
335 
336 static int decode_dlta(AVCodecContext *avctx,
337  const AVPacket *avpkt, unsigned size)
338 {
339  RASCContext *s = avctx->priv_data;
340  GetByteContext *gb = &s->gb;
342  unsigned uncompressed_size, pos;
343  unsigned x, y, w, h;
344  int ret, cx, cy, compression;
345  uint8_t *b1, *b2;
346 
347  pos = bytestream2_tell(gb);
348  bytestream2_skip(gb, 12);
349  uncompressed_size = bytestream2_get_le32(gb);
350  x = bytestream2_get_le32(gb);
351  y = bytestream2_get_le32(gb);
352  w = bytestream2_get_le32(gb);
353  h = bytestream2_get_le32(gb);
354 
355  if (x >= avctx->width || y >= avctx->height ||
356  w > avctx->width || h > avctx->height)
357  return AVERROR_INVALIDDATA;
358 
359  if (x + w > avctx->width || y + h > avctx->height)
360  return AVERROR_INVALIDDATA;
361 
362  bytestream2_skip(gb, 4);
363  compression = bytestream2_get_le32(gb);
364 
365  if (compression == 1) {
366  if (w * h * s->bpp * 3 < uncompressed_size)
367  return AVERROR_INVALIDDATA;
368  ret = decode_zlib(avctx, avpkt, size, uncompressed_size);
369  if (ret < 0)
370  return ret;
371  bytestream2_init(&dc, s->delta, uncompressed_size);
372  } else if (compression == 0) {
373  if (bytestream2_get_bytes_left(gb) < uncompressed_size)
374  return AVERROR_INVALIDDATA;
375  bytestream2_init(&dc, avpkt->data + bytestream2_tell(gb),
376  uncompressed_size);
377  } else if (compression == 2) {
378  avpriv_request_sample(avctx, "compression %d", compression);
379  return AVERROR_PATCHWELCOME;
380  } else {
381  return AVERROR_INVALIDDATA;
382  }
383 
384  if (!s->frame2->data[0] || !s->frame1->data[0])
385  return AVERROR_INVALIDDATA;
386 
387  b1 = s->frame1->data[0] + s->frame1->linesize[0] * (int)(y + h - 1) + ((int)x) * s->bpp;
388  b2 = s->frame2->data[0] + s->frame2->linesize[0] * (int)(y + h - 1) + ((int)x) * s->bpp;
389  cx = 0, cy = h;
390  while (bytestream2_get_bytes_left(&dc) > 0) {
391  int type = bytestream2_get_byte(&dc);
392  int len = bytestream2_get_byte(&dc);
393  unsigned fill;
394 
395  switch (type) {
396  case 1:
397  while (len > 0 && cy > 0) {
398  cx++;
399  NEXT_LINE
400  }
401  break;
402  case 2:
403  while (len > 0 && cy > 0) {
404  int v0 = b1[cx];
405  int v1 = b2[cx];
406 
407  b2[cx] = v0;
408  b1[cx] = v1;
409  cx++;
410  NEXT_LINE
411  }
412  break;
413  case 3:
414  while (len > 0 && cy > 0) {
415  fill = bytestream2_get_byte(&dc);
416  b1[cx] = b2[cx];
417  b2[cx] = fill;
418  cx++;
419  NEXT_LINE
420  }
421  break;
422  case 4:
423  fill = bytestream2_get_byte(&dc);
424  while (len > 0 && cy > 0) {
425  if (!dlta_room(cx, w, s->bpp, 4))
426  return AVERROR_INVALIDDATA;
427  AV_WL32(b1 + cx, AV_RL32(b2 + cx));
428  AV_WL32(b2 + cx, fill);
429  cx++;
430  NEXT_LINE
431  }
432  break;
433  case 7:
434  fill = bytestream2_get_le32(&dc);
435  while (len > 0 && cy > 0) {
436  if (!dlta_room(cx, w, s->bpp, 4))
437  return AVERROR_INVALIDDATA;
438  AV_WL32(b1 + cx, AV_RL32(b2 + cx));
439  AV_WL32(b2 + cx, fill);
440  cx += 4;
441  NEXT_LINE
442  }
443  break;
444  case 10:
445  while (len > 0 && cy > 0) {
446  cx += 4;
447  NEXT_LINE
448  }
449  break;
450  case 12:
451  while (len > 0 && cy > 0) {
452  unsigned v0, v1;
453 
454  if (!dlta_room(cx, w, s->bpp, 4))
455  return AVERROR_INVALIDDATA;
456  v0 = AV_RL32(b2 + cx);
457  v1 = AV_RL32(b1 + cx);
458  AV_WL32(b2 + cx, v1);
459  AV_WL32(b1 + cx, v0);
460  cx += 4;
461  NEXT_LINE
462  }
463  break;
464  case 13:
465  while (len > 0 && cy > 0) {
466  fill = bytestream2_get_le32(&dc);
467  if (!dlta_room(cx, w, s->bpp, 4))
468  return AVERROR_INVALIDDATA;
469  AV_WL32(b1 + cx, AV_RL32(b2 + cx));
470  AV_WL32(b2 + cx, fill);
471  cx += 4;
472  NEXT_LINE
473  }
474  break;
475  default:
476  avpriv_request_sample(avctx, "runlen %d", type);
477  return AVERROR_INVALIDDATA;
478  }
479  }
480 
481  bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
482 
483  return 0;
484 }
485 
486 static int decode_kfrm(AVCodecContext *avctx,
487  const AVPacket *avpkt, unsigned size)
488 {
489  RASCContext *s = avctx->priv_data;
490  GetByteContext *gb = &s->gb;
491  uint8_t *dst;
492  unsigned pos;
493  int zret, ret;
494 
495  pos = bytestream2_tell(gb);
496  if (bytestream2_peek_le32(gb) == 0x65) {
497  ret = decode_fint(avctx, avpkt, size);
498  if (ret < 0)
499  return ret;
500  }
501 
502  if (!s->frame2->data[0])
503  return AVERROR_INVALIDDATA;
504 
505  zret = inflateReset(&s->zstream);
506  if (zret != Z_OK) {
507  av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
508  return AVERROR_EXTERNAL;
509  }
510 
511  s->zstream.next_in = avpkt->data + bytestream2_tell(gb);
512  s->zstream.avail_in = bytestream2_get_bytes_left(gb);
513 
514  dst = s->frame2->data[0] + (avctx->height - 1) * s->frame2->linesize[0];
515  for (int i = 0; i < avctx->height; i++) {
516  s->zstream.next_out = dst;
517  s->zstream.avail_out = s->stride;
518 
519  zret = inflate(&s->zstream, Z_SYNC_FLUSH);
520  if (zret != Z_OK && zret != Z_STREAM_END) {
521  av_log(avctx, AV_LOG_ERROR,
522  "Inflate failed with return code: %d.\n", zret);
523  return AVERROR_INVALIDDATA;
524  }
525 
526  dst -= s->frame2->linesize[0];
527  }
528 
529  dst = s->frame1->data[0] + (avctx->height - 1) * s->frame1->linesize[0];
530  for (int i = 0; i < avctx->height; i++) {
531  s->zstream.next_out = dst;
532  s->zstream.avail_out = s->stride;
533 
534  zret = inflate(&s->zstream, Z_SYNC_FLUSH);
535  if (zret != Z_OK && zret != Z_STREAM_END) {
536  av_log(avctx, AV_LOG_ERROR,
537  "Inflate failed with return code: %d.\n", zret);
538  return AVERROR_INVALIDDATA;
539  }
540 
541  dst -= s->frame1->linesize[0];
542  }
543 
544  bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
545 
546  return 0;
547 }
548 
549 static int decode_mous(AVCodecContext *avctx,
550  const AVPacket *avpkt, unsigned size)
551 {
552  RASCContext *s = avctx->priv_data;
553  GetByteContext *gb = &s->gb;
554  unsigned w, h, pos, uncompressed_size;
555  int ret;
556 
557  pos = bytestream2_tell(gb);
558  bytestream2_skip(gb, 8);
559  w = bytestream2_get_le32(gb);
560  h = bytestream2_get_le32(gb);
561  bytestream2_skip(gb, 12);
562  uncompressed_size = bytestream2_get_le32(gb);
563 
564  if (w > avctx->width || h > avctx->height)
565  return AVERROR_INVALIDDATA;
566 
567  if (uncompressed_size != 3 * w * h)
568  return AVERROR_INVALIDDATA;
569 
570  av_fast_padded_malloc(&s->cursor, &s->cursor_size, uncompressed_size);
571  if (!s->cursor)
572  return AVERROR(ENOMEM);
573 
574  ret = decode_zlib(avctx, avpkt,
575  size - (bytestream2_tell(gb) - pos),
576  uncompressed_size);
577  if (ret < 0)
578  return ret;
579  memcpy(s->cursor, s->delta, uncompressed_size);
580 
581  bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
582 
583  s->cursor_w = w;
584  s->cursor_h = h;
585 
586  return 0;
587 }
588 
589 static int decode_mpos(AVCodecContext *avctx,
590  const AVPacket *avpkt, unsigned size)
591 {
592  RASCContext *s = avctx->priv_data;
593  GetByteContext *gb = &s->gb;
594  unsigned pos;
595 
596  pos = bytestream2_tell(gb);
597  bytestream2_skip(gb, 8);
598  s->cursor_x = bytestream2_get_le32(gb);
599  s->cursor_y = bytestream2_get_le32(gb);
600 
601  bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos));
602 
603  return 0;
604 }
605 
606 static void draw_cursor(AVCodecContext *avctx)
607 {
608  RASCContext *s = avctx->priv_data;
609  uint8_t *dst, *pal;
610 
611  if (!s->cursor)
612  return;
613 
614  if (s->cursor_x >= avctx->width || s->cursor_y >= avctx->height)
615  return;
616 
617  if (s->cursor_x + s->cursor_w > avctx->width ||
618  s->cursor_y + s->cursor_h > avctx->height)
619  return;
620 
621  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
622  pal = s->frame->data[1];
623  for (int i = 0; i < s->cursor_h; i++) {
624  for (int j = 0; j < s->cursor_w; j++) {
625  int cr = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 0];
626  int cg = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 1];
627  int cb = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 2];
628  int best = INT_MAX;
629  int index = 0;
630  int dist;
631 
632  if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
633  continue;
634 
635  dst = s->frame->data[0] + s->frame->linesize[0] * (int)(s->cursor_y + i) + (int)(s->cursor_x + j);
636  for (int k = 0; k < 256; k++) {
637  int pr = pal[k * 4 + 0];
638  int pg = pal[k * 4 + 1];
639  int pb = pal[k * 4 + 2];
640 
641  dist = FFABS(cr - pr) + FFABS(cg - pg) + FFABS(cb - pb);
642  if (dist < best) {
643  best = dist;
644  index = k;
645  }
646  }
647  dst[0] = index;
648  }
649  }
650  } else if (avctx->pix_fmt == AV_PIX_FMT_RGB555LE) {
651  for (int i = 0; i < s->cursor_h; i++) {
652  for (int j = 0; j < s->cursor_w; j++) {
653  int cr = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 0];
654  int cg = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 1];
655  int cb = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 2];
656 
657  if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
658  continue;
659 
660  cr >>= 3; cg >>=3; cb >>= 3;
661  dst = s->frame->data[0] + s->frame->linesize[0] * (int)(s->cursor_y + i) + 2 * (s->cursor_x + j);
662  AV_WL16(dst, cr | cg << 5 | cb << 10);
663  }
664  }
665  } else if (avctx->pix_fmt == AV_PIX_FMT_BGR0) {
666  for (int i = 0; i < s->cursor_h; i++) {
667  for (int j = 0; j < s->cursor_w; j++) {
668  int cr = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 0];
669  int cg = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 1];
670  int cb = s->cursor[3 * s->cursor_w * (s->cursor_h - i - 1) + 3 * j + 2];
671 
672  if (cr == s->cursor[0] && cg == s->cursor[1] && cb == s->cursor[2])
673  continue;
674 
675  dst = s->frame->data[0] + s->frame->linesize[0] * (int)(s->cursor_y + i) + 4 * (s->cursor_x + j);
676  dst[0] = cb;
677  dst[1] = cg;
678  dst[2] = cr;
679  }
680  }
681  }
682 }
683 
684 static int decode_frame(AVCodecContext *avctx,
685  void *data, int *got_frame,
686  AVPacket *avpkt)
687 {
688  RASCContext *s = avctx->priv_data;
689  GetByteContext *gb = &s->gb;
690  int ret, intra = 0;
691  AVFrame *frame = data;
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:
737  bytestream2_skip(gb, size);
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  s->frame->key_frame = intra;
758  s->frame->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
759 
760  *got_frame = 1;
761 
762  return avpkt->size;
763 }
764 
766 {
767  RASCContext *s = avctx->priv_data;
768  int zret;
769 
770  s->zstream.zalloc = Z_NULL;
771  s->zstream.zfree = Z_NULL;
772  s->zstream.opaque = Z_NULL;
773  zret = inflateInit(&s->zstream);
774  if (zret != Z_OK) {
775  av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
776  return AVERROR_EXTERNAL;
777  }
778 
779  s->frame1 = av_frame_alloc();
780  s->frame2 = av_frame_alloc();
781  if (!s->frame1 || !s->frame2)
782  return AVERROR(ENOMEM);
783 
784  return 0;
785 }
786 
788 {
789  RASCContext *s = avctx->priv_data;
790 
791  av_freep(&s->cursor);
792  s->cursor_size = 0;
793  av_freep(&s->delta);
794  s->delta_size = 0;
795  av_freep(&s->mv_scratch);
796  s->mv_scratch_size = 0;
797  av_frame_free(&s->frame1);
798  av_frame_free(&s->frame2);
799  inflateEnd(&s->zstream);
800 
801  return 0;
802 }
803 
804 static void decode_flush(AVCodecContext *avctx)
805 {
806  RASCContext *s = avctx->priv_data;
807 
808  clear_plane(avctx, s->frame1);
809  clear_plane(avctx, s->frame2);
810 }
811 
812 static const AVOption options[] = {
813 { "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 },
814 { NULL },
815 };
816 
817 static const AVClass rasc_decoder_class = {
818  .class_name = "rasc decoder",
819  .item_name = av_default_item_name,
820  .option = options,
821  .version = LIBAVUTIL_VERSION_INT,
822 };
823 
825  .name = "rasc",
826  .long_name = NULL_IF_CONFIG_SMALL("RemotelyAnywhere Screen Capture"),
827  .type = AVMEDIA_TYPE_VIDEO,
828  .id = AV_CODEC_ID_RASC,
829  .priv_data_size = sizeof(RASCContext),
830  .init = decode_init,
831  .close = decode_close,
832  .decode = decode_frame,
833  .flush = decode_flush,
834  .capabilities = AV_CODEC_CAP_DR1,
835  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
837  .priv_class = &rasc_decoder_class,
838 };
static void flush(AVCodecContext *avctx)
#define av_cold
Definition: attributes.h:88
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> dc
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
Libavcodec external API header.
#define AV_RL32
Definition: intreadwrite.h:146
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
static av_always_inline int bytestream2_get_bytes_left(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(GetByteContext *g)
Definition: bytestream.h:192
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFMIN(a, b)
Definition: common.h:105
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#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:1911
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static AVFrame * frame
int
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
@ AV_CODEC_ID_RASC
Definition: codec_id.h:290
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:50
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int index
Definition: gxfenc.c:89
for(j=16;j >0;--j)
cl_device_type type
misc image utilities
int i
Definition: input.c:407
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:84
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
uint8_t w
Definition: llviddspenc.c:39
#define FFALIGN(x, a)
Definition: macros.h:48
const char data[16]
Definition: mxf.c:142
AVOptions.
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:279
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:281
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:240
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
Definition: pixfmt.h:108
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
static int decode_mpos(AVCodecContext *avctx, const AVPacket *avpkt, unsigned size)
Definition: rasc.c:589
static int decode_move(AVCodecContext *avctx, const AVPacket *avpkt, unsigned size)
Definition: rasc.c:209
#define DLTA
Definition: rasc.c:42
static int decode_kfrm(AVCodecContext *avctx, const AVPacket *avpkt, unsigned size)
Definition: rasc.c:486
#define MOVE
Definition: rasc.c:45
static const AVOption options[]
Definition: rasc.c:812
static av_cold int decode_close(AVCodecContext *avctx)
Definition: rasc.c:787
static int init_frames(AVCodecContext *avctx)
Definition: rasc.c:97
static const AVClass rasc_decoder_class
Definition: rasc.c:817
static int decode_mous(AVCodecContext *avctx, const AVPacket *avpkt, unsigned size)
Definition: rasc.c:549
static av_cold int decode_init(AVCodecContext *avctx)
Definition: rasc.c:765
#define BNDL
Definition: rasc.c:40
static int dlta_room(unsigned cx, unsigned w, unsigned bpp, unsigned need)
Definition: rasc.c:322
static void copy_plane(AVCodecContext *avctx, AVFrame *src, AVFrame *dst)
Definition: rasc.c:84
AVCodec ff_rasc_decoder
Definition: rasc.c:824
#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
static void decode_flush(AVCodecContext *avctx)
Definition: rasc.c:804
#define NEXT_LINE
Definition: rasc.c:327
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: rasc.c:684
#define FINT
Definition: rasc.c:38
static void draw_cursor(AVCodecContext *avctx)
Definition: rasc.c:606
#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:336
#define v0
Definition: regdef.h:26
static char buffer[20]
Definition: seek.c:32
unsigned int pos
Definition: spdifenc.c:412
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
AVFrame * frame2
Definition: rasc.c:67
unsigned cursor_w
Definition: rasc.c:58
z_stream zstream
Definition: rasc.c:64
GetByteContext gb
Definition: rasc.c:51
int cursor_size
Definition: rasc.c:57
AVFrame * frame
Definition: rasc.c:65
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:66
unsigned cursor_y
Definition: rasc.c:61
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
int size
#define mc
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:221
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:220
if(ret< 0)
Definition: vf_mcdeint.c:282
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
Definition: vf_neighbor.c:199
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:1668
static double b2(void *priv, double x, double y)
Definition: vf_xfade.c:1669
int len