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.

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