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.

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