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.

904 lines
27KB

  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, 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. memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
  451. while (min <= max) {
  452. int fill, count;
  453. ret = decode_value(s, s->fill_model, 5, 10, &fill);
  454. ret |= decode_value(s, s->count_model, 256, 20, &count);
  455. if (ret < 0)
  456. return ret;
  457. while (min < s->nbcount && count-- > 0) {
  458. s->blocks[min++] = fill;
  459. }
  460. }
  461. for (y = 0; y < s->nby; y++) {
  462. for (x = 0; x < s->nbx; x++) {
  463. int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
  464. if (s->blocks[y * s->nbx + x] == 0)
  465. continue;
  466. if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
  467. ret = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
  468. ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
  469. ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
  470. ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
  471. if (ret < 0)
  472. return ret;
  473. sx2++;
  474. sy2++;
  475. }
  476. if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
  477. int i, j, by = y * 16, bx = x * 16;
  478. int mvx, mvy;
  479. ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
  480. ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
  481. if (ret < 0)
  482. return ret;
  483. mvx -= 256;
  484. mvy -= 256;
  485. if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
  486. by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
  487. return AVERROR_INVALIDDATA;
  488. for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
  489. for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
  490. dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
  491. }
  492. }
  493. } else {
  494. int run, r, g, b, z, bx = x * 16 + sx1, by = y * 16 + sy1;
  495. unsigned clr, ptype = 0;
  496. for (; by < y * 16 + sy2 && by < avctx->height;) {
  497. ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
  498. if (ptype == 0) {
  499. ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
  500. if (ret < 0)
  501. return ret;
  502. cx1 = (cx << 6) & 0xFC0;
  503. cx = r >> cxshift;
  504. ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
  505. if (ret < 0)
  506. return ret;
  507. cx1 = (cx << 6) & 0xFC0;
  508. cx = g >> cxshift;
  509. ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
  510. if (ret < 0)
  511. return ret;
  512. clr = (b << 16) + (g << 8) + r;
  513. }
  514. if (ptype > 5)
  515. return AVERROR_INVALIDDATA;
  516. ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
  517. if (ret < 0)
  518. return ret;
  519. switch (ptype) {
  520. case 0:
  521. while (run-- > 0) {
  522. if (by >= avctx->height)
  523. return AVERROR_INVALIDDATA;
  524. dst[by * linesize + bx] = clr;
  525. bx++;
  526. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  527. bx = x * 16 + sx1;
  528. by++;
  529. }
  530. }
  531. break;
  532. case 1:
  533. while (run-- > 0) {
  534. if (bx == 0) {
  535. if (by < 1)
  536. return AVERROR_INVALIDDATA;
  537. z = backstep;
  538. } else {
  539. z = 0;
  540. }
  541. if (by >= avctx->height)
  542. return AVERROR_INVALIDDATA;
  543. clr = dst[by * linesize + bx - 1 - z];
  544. dst[by * linesize + bx] = clr;
  545. bx++;
  546. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  547. bx = x * 16 + sx1;
  548. by++;
  549. }
  550. }
  551. break;
  552. case 2:
  553. while (run-- > 0) {
  554. if (by < 1 || by >= avctx->height)
  555. return AVERROR_INVALIDDATA;
  556. clr = dst[(by - 1) * linesize + bx];
  557. dst[by * linesize + bx] = clr;
  558. bx++;
  559. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  560. bx = x * 16 + sx1;
  561. by++;
  562. }
  563. }
  564. break;
  565. case 3:
  566. while (run-- > 0) {
  567. if (by >= avctx->height)
  568. return AVERROR_INVALIDDATA;
  569. clr = prev[by * plinesize + bx];
  570. dst[by * linesize + bx] = clr;
  571. bx++;
  572. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  573. bx = x * 16 + sx1;
  574. by++;
  575. }
  576. }
  577. break;
  578. case 4:
  579. while (run-- > 0) {
  580. uint8_t *odst = (uint8_t *)dst;
  581. if (by < 1 || by >= avctx->height)
  582. return AVERROR_INVALIDDATA;
  583. if (bx == 0) {
  584. z = backstep;
  585. } else {
  586. z = 0;
  587. }
  588. r = odst[((by - 1) * linesize + bx) * 4] +
  589. odst[(by * linesize + bx - 1 - z) * 4] -
  590. odst[((by - 1) * linesize + bx - 1 - z) * 4];
  591. g = odst[((by - 1) * linesize + bx) * 4 + 1] +
  592. odst[(by * linesize + bx - 1 - z) * 4 + 1] -
  593. odst[((by - 1) * linesize + bx - 1 - z) * 4 + 1];
  594. b = odst[((by - 1) * linesize + bx) * 4 + 2] +
  595. odst[(by * linesize + bx - 1 - z) * 4 + 2] -
  596. odst[((by - 1) * linesize + bx - 1 - z) * 4 + 2];
  597. clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
  598. dst[by * linesize + bx] = clr;
  599. bx++;
  600. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  601. bx = x * 16 + sx1;
  602. by++;
  603. }
  604. }
  605. break;
  606. case 5:
  607. while (run-- > 0) {
  608. if (by < 1 || by >= avctx->height)
  609. return AVERROR_INVALIDDATA;
  610. if (bx == 0) {
  611. z = backstep;
  612. } else {
  613. z = 0;
  614. }
  615. clr = dst[(by - 1) * linesize + bx - 1 - z];
  616. dst[by * linesize + bx] = clr;
  617. bx++;
  618. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  619. bx = x * 16 + sx1;
  620. by++;
  621. }
  622. }
  623. break;
  624. }
  625. if (avctx->bits_per_coded_sample == 16) {
  626. cx1 = (clr & 0x3F00) >> 2;
  627. cx = (clr & 0x3FFFFF) >> 16;
  628. } else {
  629. cx1 = (clr & 0xFC00) >> 4;
  630. cx = (clr & 0xFFFFFF) >> 18;
  631. }
  632. }
  633. }
  634. }
  635. }
  636. return 0;
  637. }
  638. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  639. AVPacket *avpkt)
  640. {
  641. SCPRContext *s = avctx->priv_data;
  642. GetByteContext *gb = &s->gb;
  643. AVFrame *frame = data;
  644. int ret, type;
  645. if (avctx->bits_per_coded_sample == 16) {
  646. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  647. return ret;
  648. }
  649. if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
  650. return ret;
  651. bytestream2_init(gb, avpkt->data, avpkt->size);
  652. type = bytestream2_peek_byte(gb);
  653. if (type == 2) {
  654. s->get_freq = get_freq0;
  655. s->decode = decode0;
  656. frame->key_frame = 1;
  657. ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
  658. s->current_frame->linesize[0] / 4);
  659. } else if (type == 18) {
  660. s->get_freq = get_freq;
  661. s->decode = decode;
  662. frame->key_frame = 1;
  663. ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
  664. s->current_frame->linesize[0] / 4);
  665. } else if (type == 17) {
  666. uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
  667. int x, y;
  668. frame->key_frame = 1;
  669. bytestream2_skip(gb, 1);
  670. if (avctx->bits_per_coded_sample == 16) {
  671. uint16_t value = bytestream2_get_le16(gb);
  672. int r, g, b;
  673. r = (value ) & 31;
  674. g = (value >> 5) & 31;
  675. b = (value >> 10) & 31;
  676. clr = (r << 16) + (g << 8) + b;
  677. } else {
  678. clr = bytestream2_get_le24(gb);
  679. }
  680. for (y = 0; y < avctx->height; y++) {
  681. for (x = 0; x < avctx->width; x++) {
  682. dst[x] = clr;
  683. }
  684. dst += s->current_frame->linesize[0] / 4;
  685. }
  686. } else if (type == 0 || type == 1) {
  687. frame->key_frame = 0;
  688. ret = av_frame_copy(s->current_frame, s->last_frame);
  689. if (ret < 0)
  690. return ret;
  691. ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
  692. s->current_frame->linesize[0] / 4,
  693. (uint32_t *)s->last_frame->data[0],
  694. s->last_frame->linesize[0] / 4);
  695. } else {
  696. return AVERROR_PATCHWELCOME;
  697. }
  698. if (ret < 0)
  699. return ret;
  700. if (avctx->bits_per_coded_sample != 16) {
  701. ret = av_frame_ref(data, s->current_frame);
  702. if (ret < 0)
  703. return ret;
  704. } else {
  705. uint8_t *dst = frame->data[0];
  706. int x, y;
  707. ret = av_frame_copy(frame, s->current_frame);
  708. if (ret < 0)
  709. return ret;
  710. for (y = 0; y < avctx->height; y++) {
  711. for (x = 0; x < avctx->width * 4; x++) {
  712. dst[x] = dst[x] << 3;
  713. }
  714. dst += frame->linesize[0];
  715. }
  716. }
  717. frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  718. FFSWAP(AVFrame *, s->current_frame, s->last_frame);
  719. frame->data[0] += frame->linesize[0] * (avctx->height - 1);
  720. frame->linesize[0] *= -1;
  721. *got_frame = 1;
  722. return avpkt->size;
  723. }
  724. static av_cold int decode_init(AVCodecContext *avctx)
  725. {
  726. SCPRContext *s = avctx->priv_data;
  727. switch (avctx->bits_per_coded_sample) {
  728. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
  729. case 24:
  730. case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
  731. default:
  732. av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
  733. return AVERROR_INVALIDDATA;
  734. }
  735. s->get_freq = get_freq0;
  736. s->decode = decode0;
  737. s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
  738. s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
  739. s->nbx = (avctx->width + 15) / 16;
  740. s->nby = (avctx->height + 15) / 16;
  741. s->nbcount = s->nbx * s->nby;
  742. s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
  743. if (!s->blocks)
  744. return AVERROR(ENOMEM);
  745. s->last_frame = av_frame_alloc();
  746. s->current_frame = av_frame_alloc();
  747. if (!s->last_frame || !s->current_frame)
  748. return AVERROR(ENOMEM);
  749. return 0;
  750. }
  751. static av_cold int decode_close(AVCodecContext *avctx)
  752. {
  753. SCPRContext *s = avctx->priv_data;
  754. av_freep(&s->blocks);
  755. av_frame_free(&s->last_frame);
  756. av_frame_free(&s->current_frame);
  757. return 0;
  758. }
  759. AVCodec ff_scpr_decoder = {
  760. .name = "scpr",
  761. .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
  762. .type = AVMEDIA_TYPE_VIDEO,
  763. .id = AV_CODEC_ID_SCPR,
  764. .priv_data_size = sizeof(SCPRContext),
  765. .init = decode_init,
  766. .close = decode_close,
  767. .decode = decode_frame,
  768. .capabilities = AV_CODEC_CAP_DR1,
  769. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  770. FF_CODEC_CAP_INIT_CLEANUP,
  771. };