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.

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