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.

924 lines
28KB

  1. /*
  2. * ScreenPressor decoder
  3. *
  4. * Copyright (c) 2017 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 "avcodec.h"
  26. #include "bytestream.h"
  27. #include "internal.h"
  28. #define TOP 0x01000000
  29. #define BOT 0x010000
  30. typedef struct RangeCoder {
  31. unsigned code;
  32. unsigned range;
  33. unsigned code1;
  34. } RangeCoder;
  35. typedef struct PixelModel {
  36. unsigned freq[256];
  37. unsigned lookup[16];
  38. unsigned total_freq;
  39. } PixelModel;
  40. typedef struct SCPRContext {
  41. AVFrame *last_frame;
  42. AVFrame *current_frame;
  43. GetByteContext gb;
  44. RangeCoder rc;
  45. PixelModel pixel_model[3][4096];
  46. unsigned op_model[6][7];
  47. unsigned run_model[6][257];
  48. unsigned range_model[257];
  49. unsigned count_model[257];
  50. unsigned fill_model[6];
  51. unsigned sxy_model[4][17];
  52. unsigned mv_model[2][513];
  53. unsigned nbx, nby;
  54. unsigned nbcount;
  55. unsigned *blocks;
  56. unsigned cbits;
  57. int cxshift;
  58. int (*get_freq)(RangeCoder *rc, unsigned total_freq, unsigned *freq);
  59. int (*decode)(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq);
  60. } SCPRContext;
  61. static void init_rangecoder(RangeCoder *rc, GetByteContext *gb)
  62. {
  63. rc->code1 = 0;
  64. rc->range = 0xFFFFFFFFU;
  65. rc->code = bytestream2_get_be32(gb);
  66. }
  67. static void reinit_tables(SCPRContext *s)
  68. {
  69. int comp, i, j;
  70. for (comp = 0; comp < 3; comp++) {
  71. for (j = 0; j < 4096; j++) {
  72. if (s->pixel_model[comp][j].total_freq != 256) {
  73. for (i = 0; i < 256; i++)
  74. s->pixel_model[comp][j].freq[i] = 1;
  75. for (i = 0; i < 16; i++)
  76. s->pixel_model[comp][j].lookup[i] = 16;
  77. s->pixel_model[comp][j].total_freq = 256;
  78. }
  79. }
  80. }
  81. for (j = 0; j < 6; j++) {
  82. unsigned *p = s->run_model[j];
  83. for (i = 0; i < 256; i++)
  84. p[i] = 1;
  85. p[256] = 256;
  86. }
  87. for (j = 0; j < 6; j++) {
  88. unsigned *op = s->op_model[j];
  89. for (i = 0; i < 6; i++)
  90. op[i] = 1;
  91. op[6] = 6;
  92. }
  93. for (i = 0; i < 256; i++) {
  94. s->range_model[i] = 1;
  95. s->count_model[i] = 1;
  96. }
  97. s->range_model[256] = 256;
  98. s->count_model[256] = 256;
  99. for (i = 0; i < 5; i++) {
  100. s->fill_model[i] = 1;
  101. }
  102. s->fill_model[5] = 5;
  103. for (j = 0; j < 4; j++) {
  104. for (i = 0; i < 16; i++) {
  105. s->sxy_model[j][i] = 1;
  106. }
  107. s->sxy_model[j][16] = 16;
  108. }
  109. for (i = 0; i < 512; i++) {
  110. s->mv_model[0][i] = 1;
  111. s->mv_model[1][i] = 1;
  112. }
  113. s->mv_model[0][512] = 512;
  114. s->mv_model[1][512] = 512;
  115. }
  116. static int decode(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
  117. {
  118. rc->code -= cumFreq * rc->range;
  119. rc->range *= freq;
  120. while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
  121. unsigned byte = bytestream2_get_byte(gb);
  122. rc->code = (rc->code << 8) | byte;
  123. rc->range <<= 8;
  124. }
  125. return 0;
  126. }
  127. static int get_freq(RangeCoder *rc, unsigned total_freq, unsigned *freq)
  128. {
  129. if (total_freq == 0)
  130. return AVERROR_INVALIDDATA;
  131. rc->range = rc->range / total_freq;
  132. if (rc->range == 0)
  133. return AVERROR_INVALIDDATA;
  134. *freq = rc->code / rc->range;
  135. return 0;
  136. }
  137. static int decode0(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
  138. {
  139. unsigned t;
  140. if (total_freq == 0)
  141. return AVERROR_INVALIDDATA;
  142. t = rc->range * (uint64_t)cumFreq / total_freq;
  143. rc->code1 += t + 1;
  144. rc->range = rc->range * (uint64_t)(freq + cumFreq) / total_freq - (t + 1);
  145. while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
  146. unsigned byte = bytestream2_get_byte(gb);
  147. rc->code = (rc->code << 8) | byte;
  148. rc->code1 <<= 8;
  149. rc->range <<= 8;
  150. }
  151. return 0;
  152. }
  153. static int get_freq0(RangeCoder *rc, unsigned total_freq, unsigned *freq)
  154. {
  155. if (rc->range == 0)
  156. return AVERROR_INVALIDDATA;
  157. *freq = total_freq * (uint64_t)(rc->code - rc->code1) / rc->range;
  158. return 0;
  159. }
  160. static int decode_value(SCPRContext *s, unsigned *cnt, unsigned maxc, unsigned step, unsigned *rval)
  161. {
  162. GetByteContext *gb = &s->gb;
  163. RangeCoder *rc = &s->rc;
  164. unsigned totfr = cnt[maxc];
  165. unsigned value;
  166. unsigned c = 0, cumfr = 0, cnt_c = 0;
  167. int i, ret;
  168. if ((ret = s->get_freq(rc, totfr, &value)) < 0)
  169. return ret;
  170. while (c < maxc) {
  171. cnt_c = cnt[c];
  172. if (value >= cumfr + cnt_c)
  173. cumfr += cnt_c;
  174. else
  175. break;
  176. c++;
  177. }
  178. if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
  179. return ret;
  180. cnt[c] = cnt_c + step;
  181. totfr += step;
  182. if (totfr > BOT) {
  183. totfr = 0;
  184. for (i = 0; i < maxc; i++) {
  185. unsigned nc = (cnt[i] >> 1) + 1;
  186. cnt[i] = nc;
  187. totfr += nc;
  188. }
  189. }
  190. cnt[maxc] = totfr;
  191. *rval = c;
  192. return 0;
  193. }
  194. static int decode_unit(SCPRContext *s, PixelModel *pixel, unsigned step, unsigned *rval)
  195. {
  196. GetByteContext *gb = &s->gb;
  197. RangeCoder *rc = &s->rc;
  198. unsigned totfr = pixel->total_freq;
  199. unsigned value, x = 0, cumfr = 0, cnt_x = 0;
  200. int i, j, ret, c, cnt_c;
  201. if ((ret = s->get_freq(rc, totfr, &value)) < 0)
  202. return ret;
  203. while (x < 16) {
  204. cnt_x = pixel->lookup[x];
  205. if (value >= cumfr + cnt_x)
  206. cumfr += cnt_x;
  207. else
  208. break;
  209. x++;
  210. }
  211. c = x * 16;
  212. cnt_c = 0;
  213. while (c < 256) {
  214. cnt_c = pixel->freq[c];
  215. if (value >= cumfr + cnt_c)
  216. cumfr += cnt_c;
  217. else
  218. break;
  219. c++;
  220. }
  221. if (x >= 16 || c >= 256) {
  222. return AVERROR_INVALIDDATA;
  223. }
  224. if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
  225. return ret;
  226. pixel->freq[c] = cnt_c + step;
  227. pixel->lookup[x] = cnt_x + step;
  228. totfr += step;
  229. if (totfr > BOT) {
  230. totfr = 0;
  231. for (i = 0; i < 256; i++) {
  232. unsigned nc = (pixel->freq[i] >> 1) + 1;
  233. pixel->freq[i] = nc;
  234. totfr += nc;
  235. }
  236. for (i = 0; i < 16; i++) {
  237. unsigned sum = 0;
  238. unsigned i16_17 = i << 4;
  239. for (j = 0; j < 16; j++)
  240. sum += pixel->freq[i16_17 + j];
  241. pixel->lookup[i] = sum;
  242. }
  243. }
  244. pixel->total_freq = totfr;
  245. *rval = c & s->cbits;
  246. return 0;
  247. }
  248. static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
  249. {
  250. SCPRContext *s = avctx->priv_data;
  251. GetByteContext *gb = &s->gb;
  252. int cx = 0, cx1 = 0, k = 0, clr = 0;
  253. int run, r, g, b, off, y = 0, x = 0, z, ret;
  254. unsigned backstep = linesize - avctx->width;
  255. const int cxshift = s->cxshift;
  256. unsigned lx, ly, ptype;
  257. reinit_tables(s);
  258. bytestream2_skip(gb, 2);
  259. init_rangecoder(&s->rc, gb);
  260. while (k < avctx->width + 1) {
  261. ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
  262. if (ret < 0)
  263. return ret;
  264. cx1 = (cx << 6) & 0xFC0;
  265. cx = r >> cxshift;
  266. ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
  267. if (ret < 0)
  268. return ret;
  269. cx1 = (cx << 6) & 0xFC0;
  270. cx = g >> cxshift;
  271. ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
  272. if (ret < 0)
  273. return ret;
  274. cx1 = (cx << 6) & 0xFC0;
  275. cx = b >> cxshift;
  276. ret = decode_value(s, s->run_model[0], 256, 400, &run);
  277. if (ret < 0)
  278. return ret;
  279. clr = (b << 16) + (g << 8) + r;
  280. k += run;
  281. while (run-- > 0) {
  282. if (y >= avctx->height)
  283. return AVERROR_INVALIDDATA;
  284. dst[y * linesize + x] = clr;
  285. lx = x;
  286. ly = y;
  287. x++;
  288. if (x >= avctx->width) {
  289. x = 0;
  290. y++;
  291. }
  292. }
  293. }
  294. off = -linesize - 1;
  295. ptype = 0;
  296. while (x < avctx->width && y < avctx->height) {
  297. ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
  298. if (ret < 0)
  299. return ret;
  300. if (ptype == 0) {
  301. ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
  302. if (ret < 0)
  303. return ret;
  304. cx1 = (cx << 6) & 0xFC0;
  305. cx = r >> cxshift;
  306. ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
  307. if (ret < 0)
  308. return ret;
  309. cx1 = (cx << 6) & 0xFC0;
  310. cx = g >> cxshift;
  311. ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
  312. if (ret < 0)
  313. return ret;
  314. clr = (b << 16) + (g << 8) + r;
  315. }
  316. if (ptype > 5)
  317. return AVERROR_INVALIDDATA;
  318. ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
  319. if (ret < 0)
  320. return ret;
  321. switch (ptype) {
  322. case 0:
  323. while (run-- > 0) {
  324. if (y >= avctx->height)
  325. return AVERROR_INVALIDDATA;
  326. dst[y * linesize + x] = clr;
  327. lx = x;
  328. ly = y;
  329. x++;
  330. if (x >= avctx->width) {
  331. x = 0;
  332. y++;
  333. }
  334. }
  335. break;
  336. case 1:
  337. while (run-- > 0) {
  338. if (y >= avctx->height)
  339. return AVERROR_INVALIDDATA;
  340. dst[y * linesize + x] = dst[ly * linesize + lx];
  341. lx = x;
  342. ly = y;
  343. x++;
  344. if (x >= avctx->width) {
  345. x = 0;
  346. y++;
  347. }
  348. }
  349. clr = dst[ly * linesize + lx];
  350. break;
  351. case 2:
  352. while (run-- > 0) {
  353. if (y < 1 || y >= avctx->height)
  354. return AVERROR_INVALIDDATA;
  355. clr = dst[y * linesize + x + off + 1];
  356. dst[y * linesize + x] = clr;
  357. lx = x;
  358. ly = y;
  359. x++;
  360. if (x >= avctx->width) {
  361. x = 0;
  362. y++;
  363. }
  364. }
  365. break;
  366. case 4:
  367. while (run-- > 0) {
  368. uint8_t *odst = (uint8_t *)dst;
  369. if (y < 1 || y >= avctx->height ||
  370. (y == 1 && x == 0))
  371. return AVERROR_INVALIDDATA;
  372. if (x == 0) {
  373. z = backstep;
  374. } else {
  375. z = 0;
  376. }
  377. r = odst[(ly * linesize + lx) * 4] +
  378. odst[((y * linesize + x) + off - z) * 4 + 4] -
  379. odst[((y * linesize + x) + off - z) * 4];
  380. g = odst[(ly * linesize + lx) * 4 + 1] +
  381. odst[((y * linesize + x) + off - z) * 4 + 5] -
  382. odst[((y * linesize + x) + off - z) * 4 + 1];
  383. b = odst[(ly * linesize + lx) * 4 + 2] +
  384. odst[((y * linesize + x) + off - z) * 4 + 6] -
  385. odst[((y * linesize + x) + off - z) * 4 + 2];
  386. clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
  387. dst[y * linesize + x] = clr;
  388. lx = x;
  389. ly = y;
  390. x++;
  391. if (x >= avctx->width) {
  392. x = 0;
  393. y++;
  394. }
  395. }
  396. break;
  397. case 5:
  398. while (run-- > 0) {
  399. if (y < 1 || y >= avctx->height ||
  400. (y == 1 && x == 0))
  401. return AVERROR_INVALIDDATA;
  402. if (x == 0) {
  403. z = backstep;
  404. } else {
  405. z = 0;
  406. }
  407. clr = dst[y * linesize + x + off - z];
  408. dst[y * linesize + x] = clr;
  409. lx = x;
  410. ly = y;
  411. x++;
  412. if (x >= avctx->width) {
  413. x = 0;
  414. y++;
  415. }
  416. }
  417. break;
  418. }
  419. if (avctx->bits_per_coded_sample == 16) {
  420. cx1 = (clr & 0x3F00) >> 2;
  421. cx = (clr & 0x3FFFFF) >> 16;
  422. } else {
  423. cx1 = (clr & 0xFC00) >> 4;
  424. cx = (clr & 0xFFFFFF) >> 18;
  425. }
  426. }
  427. return 0;
  428. }
  429. static int decompress_p(AVCodecContext *avctx,
  430. uint32_t *dst, int linesize,
  431. uint32_t *prev, int plinesize)
  432. {
  433. SCPRContext *s = avctx->priv_data;
  434. GetByteContext *gb = &s->gb;
  435. int ret, temp = 0, min, max, x, y, cx = 0, cx1 = 0;
  436. int backstep = linesize - avctx->width;
  437. const int cxshift = s->cxshift;
  438. if (bytestream2_get_byte(gb) == 0)
  439. return 0;
  440. bytestream2_skip(gb, 1);
  441. init_rangecoder(&s->rc, gb);
  442. ret = decode_value(s, s->range_model, 256, 1, &min);
  443. ret |= decode_value(s, s->range_model, 256, 1, &temp);
  444. min += temp << 8;
  445. ret |= decode_value(s, s->range_model, 256, 1, &max);
  446. ret |= decode_value(s, s->range_model, 256, 1, &temp);
  447. if (ret < 0)
  448. return ret;
  449. max += temp << 8;
  450. if (min > max)
  451. return AVERROR_INVALIDDATA;
  452. memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
  453. while (min <= max) {
  454. int fill, count;
  455. ret = decode_value(s, s->fill_model, 5, 10, &fill);
  456. ret |= decode_value(s, s->count_model, 256, 20, &count);
  457. if (ret < 0)
  458. return ret;
  459. while (min < s->nbcount && count-- > 0) {
  460. s->blocks[min++] = fill;
  461. }
  462. }
  463. for (y = 0; y < s->nby; y++) {
  464. for (x = 0; x < s->nbx; x++) {
  465. int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
  466. if (s->blocks[y * s->nbx + x] == 0)
  467. continue;
  468. if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
  469. ret = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
  470. ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
  471. ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
  472. ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
  473. if (ret < 0)
  474. return ret;
  475. sx2++;
  476. sy2++;
  477. }
  478. if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
  479. int i, j, by = y * 16, bx = x * 16;
  480. int mvx, mvy;
  481. ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
  482. ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
  483. if (ret < 0)
  484. return ret;
  485. mvx -= 256;
  486. mvy -= 256;
  487. if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
  488. by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
  489. return AVERROR_INVALIDDATA;
  490. for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
  491. for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
  492. dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
  493. }
  494. }
  495. } else {
  496. int run, r, g, b, z, bx = x * 16 + sx1, by = y * 16 + sy1;
  497. unsigned clr, ptype = 0;
  498. for (; by < y * 16 + sy2 && by < avctx->height;) {
  499. ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
  500. if (ret < 0)
  501. return ret;
  502. if (ptype == 0) {
  503. ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
  504. if (ret < 0)
  505. return ret;
  506. cx1 = (cx << 6) & 0xFC0;
  507. cx = r >> cxshift;
  508. ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
  509. if (ret < 0)
  510. return ret;
  511. cx1 = (cx << 6) & 0xFC0;
  512. cx = g >> cxshift;
  513. ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
  514. if (ret < 0)
  515. return ret;
  516. clr = (b << 16) + (g << 8) + r;
  517. }
  518. if (ptype > 5)
  519. return AVERROR_INVALIDDATA;
  520. ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
  521. if (ret < 0)
  522. return ret;
  523. switch (ptype) {
  524. case 0:
  525. while (run-- > 0) {
  526. if (by >= avctx->height)
  527. return AVERROR_INVALIDDATA;
  528. dst[by * linesize + bx] = clr;
  529. bx++;
  530. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  531. bx = x * 16 + sx1;
  532. by++;
  533. }
  534. }
  535. break;
  536. case 1:
  537. while (run-- > 0) {
  538. if (bx == 0) {
  539. if (by < 1)
  540. return AVERROR_INVALIDDATA;
  541. z = backstep;
  542. } else {
  543. z = 0;
  544. }
  545. if (by >= avctx->height)
  546. return AVERROR_INVALIDDATA;
  547. clr = dst[by * linesize + bx - 1 - z];
  548. dst[by * linesize + bx] = clr;
  549. bx++;
  550. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  551. bx = x * 16 + sx1;
  552. by++;
  553. }
  554. }
  555. break;
  556. case 2:
  557. while (run-- > 0) {
  558. if (by < 1 || by >= avctx->height)
  559. return AVERROR_INVALIDDATA;
  560. clr = dst[(by - 1) * linesize + bx];
  561. dst[by * linesize + bx] = clr;
  562. bx++;
  563. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  564. bx = x * 16 + sx1;
  565. by++;
  566. }
  567. }
  568. break;
  569. case 3:
  570. while (run-- > 0) {
  571. if (by >= avctx->height)
  572. return AVERROR_INVALIDDATA;
  573. clr = prev[by * plinesize + bx];
  574. dst[by * linesize + bx] = clr;
  575. bx++;
  576. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  577. bx = x * 16 + sx1;
  578. by++;
  579. }
  580. }
  581. break;
  582. case 4:
  583. while (run-- > 0) {
  584. uint8_t *odst = (uint8_t *)dst;
  585. if (by < 1 || by >= avctx->height)
  586. return AVERROR_INVALIDDATA;
  587. if (bx == 0) {
  588. if (by < 2)
  589. return AVERROR_INVALIDDATA;
  590. z = backstep;
  591. } else {
  592. z = 0;
  593. }
  594. r = odst[((by - 1) * linesize + bx) * 4] +
  595. odst[(by * linesize + bx - 1 - z) * 4] -
  596. odst[((by - 1) * linesize + bx - 1 - z) * 4];
  597. g = odst[((by - 1) * linesize + bx) * 4 + 1] +
  598. odst[(by * linesize + bx - 1 - z) * 4 + 1] -
  599. odst[((by - 1) * linesize + bx - 1 - z) * 4 + 1];
  600. b = odst[((by - 1) * linesize + bx) * 4 + 2] +
  601. odst[(by * linesize + bx - 1 - z) * 4 + 2] -
  602. odst[((by - 1) * linesize + bx - 1 - z) * 4 + 2];
  603. clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
  604. dst[by * linesize + bx] = clr;
  605. bx++;
  606. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  607. bx = x * 16 + sx1;
  608. by++;
  609. }
  610. }
  611. break;
  612. case 5:
  613. while (run-- > 0) {
  614. if (by < 1 || by >= avctx->height)
  615. return AVERROR_INVALIDDATA;
  616. if (bx == 0) {
  617. if (by < 2)
  618. return AVERROR_INVALIDDATA;
  619. z = backstep;
  620. } else {
  621. z = 0;
  622. }
  623. clr = dst[(by - 1) * linesize + bx - 1 - z];
  624. dst[by * linesize + bx] = clr;
  625. bx++;
  626. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  627. bx = x * 16 + sx1;
  628. by++;
  629. }
  630. }
  631. break;
  632. }
  633. if (avctx->bits_per_coded_sample == 16) {
  634. cx1 = (clr & 0x3F00) >> 2;
  635. cx = (clr & 0x3FFFFF) >> 16;
  636. } else {
  637. cx1 = (clr & 0xFC00) >> 4;
  638. cx = (clr & 0xFFFFFF) >> 18;
  639. }
  640. }
  641. }
  642. }
  643. }
  644. return 0;
  645. }
  646. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  647. AVPacket *avpkt)
  648. {
  649. SCPRContext *s = avctx->priv_data;
  650. GetByteContext *gb = &s->gb;
  651. AVFrame *frame = data;
  652. int ret, type;
  653. if (avctx->bits_per_coded_sample == 16) {
  654. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  655. return ret;
  656. }
  657. if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
  658. return ret;
  659. bytestream2_init(gb, avpkt->data, avpkt->size);
  660. type = bytestream2_peek_byte(gb);
  661. if (type == 2) {
  662. s->get_freq = get_freq0;
  663. s->decode = decode0;
  664. frame->key_frame = 1;
  665. ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
  666. s->current_frame->linesize[0] / 4);
  667. } else if (type == 18) {
  668. s->get_freq = get_freq;
  669. s->decode = decode;
  670. frame->key_frame = 1;
  671. ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
  672. s->current_frame->linesize[0] / 4);
  673. } else if (type == 17) {
  674. uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
  675. int x, y;
  676. frame->key_frame = 1;
  677. bytestream2_skip(gb, 1);
  678. if (avctx->bits_per_coded_sample == 16) {
  679. uint16_t value = bytestream2_get_le16(gb);
  680. int r, g, b;
  681. r = (value ) & 31;
  682. g = (value >> 5) & 31;
  683. b = (value >> 10) & 31;
  684. clr = (r << 16) + (g << 8) + b;
  685. } else {
  686. clr = bytestream2_get_le24(gb);
  687. }
  688. for (y = 0; y < avctx->height; y++) {
  689. for (x = 0; x < avctx->width; x++) {
  690. dst[x] = clr;
  691. }
  692. dst += s->current_frame->linesize[0] / 4;
  693. }
  694. } else if (type == 0 || type == 1) {
  695. frame->key_frame = 0;
  696. ret = av_frame_copy(s->current_frame, s->last_frame);
  697. if (ret < 0)
  698. return ret;
  699. ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
  700. s->current_frame->linesize[0] / 4,
  701. (uint32_t *)s->last_frame->data[0],
  702. s->last_frame->linesize[0] / 4);
  703. } else {
  704. return AVERROR_PATCHWELCOME;
  705. }
  706. if (ret < 0)
  707. return ret;
  708. if (avctx->bits_per_coded_sample != 16) {
  709. ret = av_frame_ref(data, s->current_frame);
  710. if (ret < 0)
  711. return ret;
  712. } else {
  713. uint8_t *dst = frame->data[0];
  714. int x, y;
  715. ret = av_frame_copy(frame, s->current_frame);
  716. if (ret < 0)
  717. return ret;
  718. // scale up each sample by 8
  719. for (y = 0; y < avctx->height; y++) {
  720. // If the image is sufficiently aligned, compute 8 samples at once
  721. if (!(((uintptr_t)dst) & 7)) {
  722. uint64_t *dst64 = (uint64_t *)dst;
  723. int w = avctx->width>>1;
  724. for (x = 0; x < w; x++) {
  725. dst64[x] = (dst64[x] << 3) & 0xFCFCFCFCFCFCFCFCULL;
  726. }
  727. x *= 8;
  728. } else
  729. x = 0;
  730. for (; x < avctx->width * 4; x++) {
  731. dst[x] = dst[x] << 3;
  732. }
  733. dst += frame->linesize[0];
  734. }
  735. }
  736. frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  737. FFSWAP(AVFrame *, s->current_frame, s->last_frame);
  738. frame->data[0] += frame->linesize[0] * (avctx->height - 1);
  739. frame->linesize[0] *= -1;
  740. *got_frame = 1;
  741. return avpkt->size;
  742. }
  743. static av_cold int decode_init(AVCodecContext *avctx)
  744. {
  745. SCPRContext *s = avctx->priv_data;
  746. switch (avctx->bits_per_coded_sample) {
  747. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
  748. case 24:
  749. case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
  750. default:
  751. av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
  752. return AVERROR_INVALIDDATA;
  753. }
  754. s->get_freq = get_freq0;
  755. s->decode = decode0;
  756. s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
  757. s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
  758. s->nbx = (avctx->width + 15) / 16;
  759. s->nby = (avctx->height + 15) / 16;
  760. s->nbcount = s->nbx * s->nby;
  761. s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
  762. if (!s->blocks)
  763. return AVERROR(ENOMEM);
  764. s->last_frame = av_frame_alloc();
  765. s->current_frame = av_frame_alloc();
  766. if (!s->last_frame || !s->current_frame)
  767. return AVERROR(ENOMEM);
  768. return 0;
  769. }
  770. static av_cold int decode_close(AVCodecContext *avctx)
  771. {
  772. SCPRContext *s = avctx->priv_data;
  773. av_freep(&s->blocks);
  774. av_frame_free(&s->last_frame);
  775. av_frame_free(&s->current_frame);
  776. return 0;
  777. }
  778. AVCodec ff_scpr_decoder = {
  779. .name = "scpr",
  780. .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
  781. .type = AVMEDIA_TYPE_VIDEO,
  782. .id = AV_CODEC_ID_SCPR,
  783. .priv_data_size = sizeof(SCPRContext),
  784. .init = decode_init,
  785. .close = decode_close,
  786. .decode = decode_frame,
  787. .capabilities = AV_CODEC_CAP_DR1,
  788. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  789. FF_CODEC_CAP_INIT_CLEANUP,
  790. };