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.

925 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_byteu(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_byteu(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 = 0, min, max, x, y, cx = 0, cx1 = 0;
  441. int backstep = linesize - avctx->width;
  442. if (bytestream2_get_byte(gb) == 0)
  443. return 1;
  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 || min >= s->nbcount)
  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. if (count <= 0)
  464. return AVERROR_INVALIDDATA;
  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, z, bx = x * 16 + sx1, by = y * 16 + sy1;
  503. unsigned r, g, b, 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_units(s, &r, &g, &b, &cx, &cx1);
  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. if (run <= 0)
  520. return AVERROR_INVALIDDATA;
  521. switch (ptype) {
  522. case 0:
  523. while (run-- > 0) {
  524. if (by >= avctx->height)
  525. return AVERROR_INVALIDDATA;
  526. dst[by * linesize + bx] = clr;
  527. bx++;
  528. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  529. bx = x * 16 + sx1;
  530. by++;
  531. }
  532. }
  533. break;
  534. case 1:
  535. while (run-- > 0) {
  536. if (bx == 0) {
  537. if (by < 1)
  538. return AVERROR_INVALIDDATA;
  539. z = backstep;
  540. } else {
  541. z = 0;
  542. }
  543. if (by >= avctx->height)
  544. return AVERROR_INVALIDDATA;
  545. clr = dst[by * linesize + bx - 1 - z];
  546. dst[by * linesize + bx] = clr;
  547. bx++;
  548. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  549. bx = x * 16 + sx1;
  550. by++;
  551. }
  552. }
  553. break;
  554. case 2:
  555. while (run-- > 0) {
  556. if (by < 1 || by >= avctx->height)
  557. return AVERROR_INVALIDDATA;
  558. clr = dst[(by - 1) * linesize + bx];
  559. dst[by * linesize + bx] = clr;
  560. bx++;
  561. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  562. bx = x * 16 + sx1;
  563. by++;
  564. }
  565. }
  566. break;
  567. case 3:
  568. while (run-- > 0) {
  569. if (by >= avctx->height)
  570. return AVERROR_INVALIDDATA;
  571. clr = prev[by * plinesize + bx];
  572. dst[by * linesize + bx] = clr;
  573. bx++;
  574. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  575. bx = x * 16 + sx1;
  576. by++;
  577. }
  578. }
  579. break;
  580. case 4:
  581. while (run-- > 0) {
  582. uint8_t *odst = (uint8_t *)dst;
  583. if (by < 1 || by >= avctx->height)
  584. return AVERROR_INVALIDDATA;
  585. if (bx == 0) {
  586. if (by < 2)
  587. return AVERROR_INVALIDDATA;
  588. z = backstep;
  589. } else {
  590. z = 0;
  591. }
  592. r = odst[((by - 1) * linesize + bx) * 4] +
  593. odst[(by * linesize + bx - 1 - z) * 4] -
  594. odst[((by - 1) * linesize + bx - 1 - z) * 4];
  595. g = odst[((by - 1) * linesize + bx) * 4 + 1] +
  596. odst[(by * linesize + bx - 1 - z) * 4 + 1] -
  597. odst[((by - 1) * linesize + bx - 1 - z) * 4 + 1];
  598. b = odst[((by - 1) * linesize + bx) * 4 + 2] +
  599. odst[(by * linesize + bx - 1 - z) * 4 + 2] -
  600. odst[((by - 1) * linesize + bx - 1 - z) * 4 + 2];
  601. clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
  602. dst[by * linesize + bx] = clr;
  603. bx++;
  604. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  605. bx = x * 16 + sx1;
  606. by++;
  607. }
  608. }
  609. break;
  610. case 5:
  611. while (run-- > 0) {
  612. if (by < 1 || by >= avctx->height)
  613. return AVERROR_INVALIDDATA;
  614. if (bx == 0) {
  615. if (by < 2)
  616. return AVERROR_INVALIDDATA;
  617. z = backstep;
  618. } else {
  619. z = 0;
  620. }
  621. clr = dst[(by - 1) * linesize + bx - 1 - z];
  622. dst[by * linesize + bx] = clr;
  623. bx++;
  624. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  625. bx = x * 16 + sx1;
  626. by++;
  627. }
  628. }
  629. break;
  630. }
  631. if (avctx->bits_per_coded_sample == 16) {
  632. cx1 = (clr & 0x3F00) >> 2;
  633. cx = (clr & 0x3FFFFF) >> 16;
  634. } else {
  635. cx1 = (clr & 0xFC00) >> 4;
  636. cx = (clr & 0xFFFFFF) >> 18;
  637. }
  638. }
  639. }
  640. }
  641. }
  642. return 0;
  643. }
  644. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  645. AVPacket *avpkt)
  646. {
  647. SCPRContext *s = avctx->priv_data;
  648. GetByteContext *gb = &s->gb;
  649. AVFrame *frame = data;
  650. int ret, type;
  651. if (avctx->bits_per_coded_sample == 16) {
  652. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  653. return ret;
  654. }
  655. if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
  656. return ret;
  657. bytestream2_init(gb, avpkt->data, avpkt->size);
  658. type = bytestream2_peek_byte(gb);
  659. if (type == 2) {
  660. s->get_freq = get_freq0;
  661. s->decode = decode0;
  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 == 18) {
  666. s->get_freq = get_freq;
  667. s->decode = decode;
  668. frame->key_frame = 1;
  669. ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
  670. s->current_frame->linesize[0] / 4);
  671. } else if (type == 17) {
  672. uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
  673. int x, y;
  674. frame->key_frame = 1;
  675. bytestream2_skip(gb, 1);
  676. if (avctx->bits_per_coded_sample == 16) {
  677. uint16_t value = bytestream2_get_le16(gb);
  678. int r, g, b;
  679. r = (value ) & 31;
  680. g = (value >> 5) & 31;
  681. b = (value >> 10) & 31;
  682. clr = (r << 16) + (g << 8) + b;
  683. } else {
  684. clr = bytestream2_get_le24(gb);
  685. }
  686. for (y = 0; y < avctx->height; y++) {
  687. for (x = 0; x < avctx->width; x++) {
  688. dst[x] = clr;
  689. }
  690. dst += s->current_frame->linesize[0] / 4;
  691. }
  692. } else if (type == 0 || type == 1) {
  693. frame->key_frame = 0;
  694. ret = av_frame_copy(s->current_frame, s->last_frame);
  695. if (ret < 0)
  696. return ret;
  697. ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
  698. s->current_frame->linesize[0] / 4,
  699. (uint32_t *)s->last_frame->data[0],
  700. s->last_frame->linesize[0] / 4);
  701. if (ret == 1)
  702. return avpkt->size;
  703. } else {
  704. return AVERROR_PATCHWELCOME;
  705. }
  706. if (ret < 0)
  707. return ret;
  708. if (avctx->bits_per_coded_sample != 16) {
  709. ret = av_frame_ref(data, s->current_frame);
  710. if (ret < 0)
  711. return ret;
  712. } else {
  713. uint8_t *dst = frame->data[0];
  714. int x, y;
  715. ret = av_frame_copy(frame, s->current_frame);
  716. if (ret < 0)
  717. return ret;
  718. // scale up each sample by 8
  719. for (y = 0; y < avctx->height; y++) {
  720. // If the image is sufficiently aligned, compute 8 samples at once
  721. if (!(((uintptr_t)dst) & 7)) {
  722. uint64_t *dst64 = (uint64_t *)dst;
  723. int w = avctx->width>>1;
  724. for (x = 0; x < w; x++) {
  725. dst64[x] = (dst64[x] << 3) & 0xFCFCFCFCFCFCFCFCULL;
  726. }
  727. x *= 8;
  728. } else
  729. x = 0;
  730. for (; x < avctx->width * 4; x++) {
  731. dst[x] = dst[x] << 3;
  732. }
  733. dst += frame->linesize[0];
  734. }
  735. }
  736. frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  737. FFSWAP(AVFrame *, s->current_frame, s->last_frame);
  738. frame->data[0] += frame->linesize[0] * (avctx->height - 1);
  739. frame->linesize[0] *= -1;
  740. *got_frame = 1;
  741. return avpkt->size;
  742. }
  743. static av_cold int decode_init(AVCodecContext *avctx)
  744. {
  745. SCPRContext *s = avctx->priv_data;
  746. switch (avctx->bits_per_coded_sample) {
  747. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
  748. case 24:
  749. case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
  750. default:
  751. av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
  752. return AVERROR_INVALIDDATA;
  753. }
  754. s->get_freq = get_freq0;
  755. s->decode = decode0;
  756. s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
  757. s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
  758. s->nbx = (avctx->width + 15) / 16;
  759. s->nby = (avctx->height + 15) / 16;
  760. s->nbcount = s->nbx * s->nby;
  761. s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
  762. if (!s->blocks)
  763. return AVERROR(ENOMEM);
  764. s->last_frame = av_frame_alloc();
  765. s->current_frame = av_frame_alloc();
  766. if (!s->last_frame || !s->current_frame)
  767. return AVERROR(ENOMEM);
  768. return 0;
  769. }
  770. static av_cold int decode_close(AVCodecContext *avctx)
  771. {
  772. SCPRContext *s = avctx->priv_data;
  773. av_freep(&s->blocks);
  774. av_frame_free(&s->last_frame);
  775. av_frame_free(&s->current_frame);
  776. return 0;
  777. }
  778. AVCodec ff_scpr_decoder = {
  779. .name = "scpr",
  780. .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
  781. .type = AVMEDIA_TYPE_VIDEO,
  782. .id = AV_CODEC_ID_SCPR,
  783. .priv_data_size = sizeof(SCPRContext),
  784. .init = decode_init,
  785. .close = decode_close,
  786. .decode = decode_frame,
  787. .capabilities = AV_CODEC_CAP_DR1,
  788. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  789. FF_CODEC_CAP_INIT_CLEANUP,
  790. };