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.

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