You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

823 lines
24KB

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