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.

833 lines
25KB

  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. } RangeCoder;
  34. typedef struct PixelModel {
  35. unsigned freq[256];
  36. unsigned lookup[16];
  37. unsigned total_freq;
  38. } PixelModel;
  39. typedef struct SCPRContext {
  40. AVFrame *last_frame;
  41. AVFrame *current_frame;
  42. GetByteContext gb;
  43. RangeCoder rc;
  44. PixelModel pixel_model[3][4096];
  45. unsigned op_model[6][7];
  46. unsigned run_model[6][257];
  47. unsigned range_model[257];
  48. unsigned count_model[257];
  49. unsigned fill_model[6];
  50. unsigned sxy_model[4][17];
  51. unsigned mv_model[2][513];
  52. unsigned nbx, nby;
  53. unsigned nbcount;
  54. unsigned *blocks;
  55. unsigned cbits;
  56. int cxshift;
  57. } SCPRContext;
  58. static void init_rangecoder(RangeCoder *rc, GetByteContext *gb)
  59. {
  60. rc->range = 0xFFFFFFFFU;
  61. rc->code = bytestream2_get_be32(gb);
  62. }
  63. static void reinit_tables(SCPRContext *s)
  64. {
  65. int comp, i, j;
  66. for (comp = 0; comp < 3; comp++) {
  67. for (j = 0; j < 4096; j++) {
  68. if (s->pixel_model[comp][j].total_freq != 256) {
  69. for (i = 0; i < 256; i++)
  70. s->pixel_model[comp][j].freq[i] = 1;
  71. for (i = 0; i < 16; i++)
  72. s->pixel_model[comp][j].lookup[i] = 16;
  73. s->pixel_model[comp][j].total_freq = 256;
  74. }
  75. }
  76. }
  77. for (j = 0; j < 6; j++) {
  78. unsigned *p = s->run_model[j];
  79. for (i = 0; i < 256; i++)
  80. p[i] = 1;
  81. p[256] = 256;
  82. }
  83. for (j = 0; j < 6; j++) {
  84. unsigned *op = s->op_model[j];
  85. for (i = 0; i < 6; i++)
  86. op[i] = 1;
  87. op[6] = 6;
  88. }
  89. for (i = 0; i < 256; i++) {
  90. s->range_model[i] = 1;
  91. s->count_model[i] = 1;
  92. }
  93. s->range_model[256] = 256;
  94. s->count_model[256] = 256;
  95. for (i = 0; i < 5; i++) {
  96. s->fill_model[i] = 1;
  97. }
  98. s->fill_model[5] = 5;
  99. for (j = 0; j < 4; j++) {
  100. for (i = 0; i < 16; i++) {
  101. s->sxy_model[j][i] = 1;
  102. }
  103. s->sxy_model[j][16] = 16;
  104. }
  105. for (i = 0; i < 512; i++) {
  106. s->mv_model[0][i] = 1;
  107. s->mv_model[1][i] = 1;
  108. }
  109. s->mv_model[0][512] = 512;
  110. s->mv_model[1][512] = 512;
  111. }
  112. static void decode(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
  113. {
  114. rc->code -= cumFreq * rc->range;
  115. rc->range *= freq;
  116. while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
  117. unsigned byte = bytestream2_get_byte(gb);
  118. rc->code = (rc->code << 8) | byte;
  119. rc->range <<= 8;
  120. }
  121. }
  122. static int get_freq(RangeCoder *rc, unsigned total_freq, unsigned *freq)
  123. {
  124. if (total_freq == 0)
  125. return AVERROR_INVALIDDATA;
  126. rc->range = rc->range / total_freq;
  127. if (rc->range == 0)
  128. return AVERROR_INVALIDDATA;
  129. *freq = rc->code / rc->range;
  130. return 0;
  131. }
  132. static int decode_value(SCPRContext *s, unsigned *cnt, unsigned maxc, unsigned step, unsigned *rval)
  133. {
  134. GetByteContext *gb = &s->gb;
  135. RangeCoder *rc = &s->rc;
  136. unsigned totfr = cnt[maxc];
  137. unsigned value;
  138. unsigned c = 0, cumfr = 0, cnt_c = 0;
  139. int i, ret;
  140. if ((ret = get_freq(rc, totfr, &value)) < 0)
  141. return ret;
  142. while (c < maxc) {
  143. cnt_c = cnt[c];
  144. if (value >= cumfr + cnt_c)
  145. cumfr += cnt_c;
  146. else
  147. break;
  148. c++;
  149. }
  150. decode(gb, rc, cumfr, cnt_c, totfr);
  151. cnt[c] = cnt_c + step;
  152. totfr += step;
  153. if (totfr > BOT) {
  154. totfr = 0;
  155. for (i = 0; i < maxc; i++) {
  156. unsigned nc = (cnt[i] >> 1) + 1;
  157. cnt[i] = nc;
  158. totfr += nc;
  159. }
  160. }
  161. cnt[maxc] = totfr;
  162. *rval = c;
  163. return 0;
  164. }
  165. static int decode_unit(SCPRContext *s, PixelModel *pixel, unsigned step, unsigned *rval)
  166. {
  167. GetByteContext *gb = &s->gb;
  168. RangeCoder *rc = &s->rc;
  169. unsigned totfr = pixel->total_freq;
  170. unsigned value, x = 0, cumfr = 0, cnt_x = 0;
  171. int i, j, ret, c, cnt_c;
  172. if ((ret = get_freq(rc, totfr, &value)) < 0)
  173. return ret;
  174. while (x < 16) {
  175. cnt_x = pixel->lookup[x];
  176. if (value >= cumfr + cnt_x)
  177. cumfr += cnt_x;
  178. else
  179. break;
  180. x++;
  181. }
  182. c = x * 16;
  183. cnt_c = 0;
  184. while (c < 256) {
  185. cnt_c = pixel->freq[c];
  186. if (value >= cumfr + cnt_c)
  187. cumfr += cnt_c;
  188. else
  189. break;
  190. c++;
  191. }
  192. decode(gb, rc, cumfr, cnt_c, totfr);
  193. pixel->freq[c] = cnt_c + step;
  194. pixel->lookup[x] = cnt_x + step;
  195. totfr += step;
  196. if (totfr > BOT) {
  197. totfr = 0;
  198. for (i = 0; i < 256; i++) {
  199. unsigned nc = (pixel->freq[i] >> 1) + 1;
  200. pixel->freq[i] = nc;
  201. totfr += nc;
  202. }
  203. for (i = 0; i < 16; i++) {
  204. unsigned sum = 0;
  205. unsigned i16_17 = i << 4;
  206. for (j = 0; j < 16; j++)
  207. sum += pixel->freq[i16_17 + j];
  208. pixel->lookup[i] = sum;
  209. }
  210. }
  211. pixel->total_freq = totfr;
  212. *rval = c & s->cbits;
  213. return 0;
  214. }
  215. static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
  216. {
  217. SCPRContext *s = avctx->priv_data;
  218. GetByteContext *gb = &s->gb;
  219. int cx = 0, cx1 = 0, k = 0, clr = 0;
  220. int run, r, g, b, off, y = 0, x = 0, ret;
  221. const int cxshift = s->cxshift;
  222. unsigned lx, ly, ptype;
  223. reinit_tables(s);
  224. bytestream2_skip(gb, 2);
  225. init_rangecoder(&s->rc, gb);
  226. while (k < avctx->width + 1) {
  227. ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
  228. if (ret < 0)
  229. return ret;
  230. cx1 = (cx << 6) & 0xFC0;
  231. cx = r >> cxshift;
  232. ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
  233. if (ret < 0)
  234. return ret;
  235. cx1 = (cx << 6) & 0xFC0;
  236. cx = g >> cxshift;
  237. ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
  238. if (ret < 0)
  239. return ret;
  240. cx1 = (cx << 6) & 0xFC0;
  241. cx = b >> cxshift;
  242. ret = decode_value(s, s->run_model[0], 256, 400, &run);
  243. if (ret < 0)
  244. return ret;
  245. clr = (b << 16) + (g << 8) + r;
  246. k += run;
  247. while (run-- > 0) {
  248. dst[y * linesize + x] = clr;
  249. lx = x;
  250. ly = y;
  251. x++;
  252. if (x >= avctx->width) {
  253. x = 0;
  254. y++;
  255. }
  256. }
  257. }
  258. off = -linesize - 1;
  259. ptype = 0;
  260. while (x < avctx->width && y < avctx->height) {
  261. ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
  262. if (ret < 0)
  263. return ret;
  264. if (ptype == 0) {
  265. ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
  266. if (ret < 0)
  267. return ret;
  268. cx1 = (cx << 6) & 0xFC0;
  269. cx = r >> cxshift;
  270. ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
  271. if (ret < 0)
  272. return ret;
  273. cx1 = (cx << 6) & 0xFC0;
  274. cx = g >> cxshift;
  275. ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
  276. if (ret < 0)
  277. return ret;
  278. cx1 = (cx << 6) & 0xFC0;
  279. cx = b >> cxshift;
  280. clr = (b << 16) + (g << 8) + r;
  281. }
  282. if (ptype > 5)
  283. return AVERROR_INVALIDDATA;
  284. ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
  285. if (ret < 0)
  286. return ret;
  287. switch (ptype) {
  288. case 0:
  289. while (run-- > 0) {
  290. if (y >= avctx->height)
  291. return AVERROR_INVALIDDATA;
  292. dst[y * linesize + x] = clr;
  293. lx = x;
  294. ly = y;
  295. x++;
  296. if (x >= avctx->width) {
  297. x = 0;
  298. y++;
  299. }
  300. }
  301. break;
  302. case 1:
  303. while (run-- > 0) {
  304. if (y >= avctx->height)
  305. return AVERROR_INVALIDDATA;
  306. dst[y * linesize + x] = dst[ly * linesize + lx];
  307. lx = x;
  308. ly = y;
  309. x++;
  310. if (x >= avctx->width) {
  311. x = 0;
  312. y++;
  313. }
  314. }
  315. clr = dst[ly * linesize + lx];
  316. break;
  317. case 2:
  318. while (run-- > 0) {
  319. if (y < 1 || y >= avctx->height)
  320. return AVERROR_INVALIDDATA;
  321. clr = dst[y * linesize + x + off + 1];
  322. dst[y * linesize + x] = clr;
  323. lx = x;
  324. ly = y;
  325. x++;
  326. if (x >= avctx->width) {
  327. x = 0;
  328. y++;
  329. }
  330. }
  331. break;
  332. case 4:
  333. while (run-- > 0) {
  334. uint8_t *odst = (uint8_t *)dst;
  335. if (y < 1 || y >= avctx->height)
  336. return AVERROR_INVALIDDATA;
  337. r = odst[(ly * linesize + lx) * 4] +
  338. odst[((y * linesize + x) + off) * 4 + 4] -
  339. odst[((y * linesize + x) + off) * 4];
  340. g = odst[(ly * linesize + lx) * 4 + 1] +
  341. odst[((y * linesize + x) + off) * 4 + 5] -
  342. odst[((y * linesize + x) + off) * 4 + 1];
  343. b = odst[(ly * linesize + lx) * 4 + 2] +
  344. odst[((y * linesize + x) + off) * 4 + 6] -
  345. odst[((y * linesize + x) + off) * 4 + 2];
  346. clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
  347. dst[y * linesize + x] = clr;
  348. lx = x;
  349. ly = y;
  350. x++;
  351. if (x >= avctx->width) {
  352. x = 0;
  353. y++;
  354. }
  355. }
  356. break;
  357. case 5:
  358. while (run-- > 0) {
  359. if (y < 1 || y >= avctx->height)
  360. return AVERROR_INVALIDDATA;
  361. clr = dst[y * linesize + x + off];
  362. dst[y * linesize + x] = clr;
  363. lx = x;
  364. ly = y;
  365. x++;
  366. if (x >= avctx->width) {
  367. x = 0;
  368. y++;
  369. }
  370. }
  371. break;
  372. }
  373. if (avctx->bits_per_coded_sample == 16) {
  374. cx1 = (clr & 0x3F00) >> 2;
  375. cx = (clr & 0xFFFFFF) >> 16;
  376. } else {
  377. cx1 = (clr & 0xFC00) >> 4;
  378. cx = (clr & 0xFFFFFF) >> 18;
  379. }
  380. }
  381. return 0;
  382. }
  383. static int decompress_p(AVCodecContext *avctx,
  384. uint32_t *dst, int linesize,
  385. uint32_t *prev, int plinesize)
  386. {
  387. SCPRContext *s = avctx->priv_data;
  388. GetByteContext *gb = &s->gb;
  389. int ret, temp, min, max, x, y, cx = 0, cx1 = 0;
  390. int backstep = linesize - avctx->width;
  391. const int cxshift = s->cxshift;
  392. if (bytestream2_get_byte(gb) == 0)
  393. return 0;
  394. bytestream2_skip(gb, 1);
  395. init_rangecoder(&s->rc, gb);
  396. ret = decode_value(s, s->range_model, 256, 1, &min);
  397. ret |= decode_value(s, s->range_model, 256, 1, &temp);
  398. min += temp << 8;
  399. ret |= decode_value(s, s->range_model, 256, 1, &max);
  400. ret |= decode_value(s, s->range_model, 256, 1, &temp);
  401. if (ret < 0)
  402. return ret;
  403. max += temp << 8;
  404. memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
  405. while (min <= max) {
  406. int fill, count;
  407. ret = decode_value(s, s->fill_model, 5, 10, &fill);
  408. ret |= decode_value(s, s->count_model, 256, 20, &count);
  409. if (ret < 0)
  410. return ret;
  411. while (min < s->nbcount && count-- > 0) {
  412. s->blocks[min++] = fill;
  413. }
  414. }
  415. for (y = 0; y < s->nby; y++) {
  416. for (x = 0; x < s->nbx; x++) {
  417. int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
  418. if (s->blocks[y * s->nbx + x] == 0)
  419. continue;
  420. if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
  421. ret = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
  422. ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
  423. ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
  424. ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
  425. if (ret < 0)
  426. return ret;
  427. sx2++;
  428. sy2++;
  429. }
  430. if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
  431. int i, j, by = y * 16, bx = x * 16;
  432. int mvx, mvy;
  433. ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
  434. ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
  435. if (ret < 0)
  436. return ret;
  437. mvx -= 256;
  438. mvy -= 256;
  439. if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
  440. by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
  441. return AVERROR_INVALIDDATA;
  442. for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height; i++) {
  443. for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width; j++) {
  444. dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
  445. }
  446. }
  447. } else {
  448. int run, r, g, b, z, bx = x * 16 + sx1, by = y * 16 + sy1;
  449. unsigned clr, ptype = 0;
  450. for (; by < y * 16 + sy2 && by < avctx->height;) {
  451. ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
  452. if (ptype == 0) {
  453. ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
  454. if (ret < 0)
  455. return ret;
  456. cx1 = (cx << 6) & 0xFC0;
  457. cx = r >> cxshift;
  458. ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
  459. if (ret < 0)
  460. return ret;
  461. cx1 = (cx << 6) & 0xFC0;
  462. cx = g >> cxshift;
  463. ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
  464. if (ret < 0)
  465. return ret;
  466. cx1 = (cx << 6) & 0xFC0;
  467. cx = b >> cxshift;
  468. clr = (b << 16) + (g << 8) + r;
  469. }
  470. if (ptype > 5)
  471. return AVERROR_INVALIDDATA;
  472. ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
  473. if (ret < 0)
  474. return ret;
  475. switch (ptype) {
  476. case 0:
  477. while (run-- > 0) {
  478. if (by >= avctx->height)
  479. return AVERROR_INVALIDDATA;
  480. dst[by * linesize + bx] = clr;
  481. bx++;
  482. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  483. bx = x * 16 + sx1;
  484. by++;
  485. }
  486. }
  487. break;
  488. case 1:
  489. while (run-- > 0) {
  490. if (bx == 0) {
  491. if (by < 1)
  492. return AVERROR_INVALIDDATA;
  493. z = backstep;
  494. } else {
  495. z = 0;
  496. }
  497. if (by >= avctx->height)
  498. return AVERROR_INVALIDDATA;
  499. clr = dst[by * linesize + bx - 1 - z];
  500. dst[by * linesize + bx] = clr;
  501. bx++;
  502. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  503. bx = x * 16 + sx1;
  504. by++;
  505. }
  506. }
  507. break;
  508. case 2:
  509. while (run-- > 0) {
  510. if (by < 1 || by >= avctx->height)
  511. return AVERROR_INVALIDDATA;
  512. clr = dst[(by - 1) * linesize + bx];
  513. dst[by * linesize + bx] = clr;
  514. bx++;
  515. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  516. bx = x * 16 + sx1;
  517. by++;
  518. }
  519. }
  520. break;
  521. case 3:
  522. while (run-- > 0) {
  523. if (by >= avctx->height)
  524. return AVERROR_INVALIDDATA;
  525. clr = prev[by * linesize + bx];
  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 4:
  535. while (run-- > 0) {
  536. uint8_t *odst = (uint8_t *)dst;
  537. if (by < 1 || by >= avctx->height)
  538. return AVERROR_INVALIDDATA;
  539. if (bx == 0) {
  540. z = backstep;
  541. } else {
  542. z = 0;
  543. }
  544. r = odst[((by - 1) * linesize + bx) * 4] +
  545. odst[(by * linesize + bx - 1 - z) * 4] -
  546. odst[((by - 1) * linesize + bx - 1 - z) * 4];
  547. g = odst[((by - 1) * linesize + bx) * 4 + 1] +
  548. odst[(by * linesize + bx - 1 - z) * 4 + 1] -
  549. odst[((by - 1) * linesize + bx - 1 - z) * 4 + 1];
  550. b = odst[((by - 1) * linesize + bx) * 4 + 2] +
  551. odst[(by * linesize + bx - 1 - z) * 4 + 2] -
  552. odst[((by - 1) * linesize + bx - 1 - z) * 4 + 2];
  553. clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
  554. dst[by * linesize + bx] = clr;
  555. bx++;
  556. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  557. bx = x * 16 + sx1;
  558. by++;
  559. }
  560. }
  561. break;
  562. case 5:
  563. while (run-- > 0) {
  564. if (by < 1 || by >= avctx->height)
  565. return AVERROR_INVALIDDATA;
  566. if (bx == 0) {
  567. z = backstep;
  568. } else {
  569. z = 0;
  570. }
  571. clr = dst[(by - 1) * linesize + bx - 1 - z];
  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. }
  581. if (avctx->bits_per_coded_sample == 16) {
  582. cx1 = (clr & 0x3F00) >> 2;
  583. cx = (clr & 0xFFFFFF) >> 16;
  584. } else {
  585. cx1 = (clr & 0xFC00) >> 4;
  586. cx = (clr & 0xFFFFFF) >> 18;
  587. }
  588. }
  589. }
  590. }
  591. }
  592. return 0;
  593. }
  594. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  595. AVPacket *avpkt)
  596. {
  597. SCPRContext *s = avctx->priv_data;
  598. GetByteContext *gb = &s->gb;
  599. AVFrame *frame = data;
  600. int ret, type;
  601. if (avctx->bits_per_coded_sample == 16) {
  602. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  603. return ret;
  604. }
  605. if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
  606. return ret;
  607. bytestream2_init(gb, avpkt->data, avpkt->size);
  608. type = bytestream2_peek_byte(gb);
  609. if (type == 18) {
  610. frame->key_frame = 1;
  611. ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
  612. s->current_frame->linesize[0] / 4);
  613. } else if (type == 17) {
  614. uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
  615. int x, y;
  616. frame->key_frame = 1;
  617. bytestream2_skip(gb, 1);
  618. if (avctx->bits_per_coded_sample == 16) {
  619. uint16_t value = bytestream2_get_le16(gb);
  620. int r, g, b;
  621. r = (value ) & 31;
  622. g = (value >> 5) & 31;
  623. b = (value >> 10) & 31;
  624. clr = (r << 16) + (g << 8) + b;
  625. } else {
  626. clr = bytestream2_get_le24(gb);
  627. }
  628. for (y = 0; y < avctx->height; y++) {
  629. for (x = 0; x < avctx->width; x++) {
  630. dst[x] = clr;
  631. }
  632. dst += s->current_frame->linesize[0] / 4;
  633. }
  634. } else if (type == 0 || type == 1) {
  635. frame->key_frame = 0;
  636. ret = av_frame_copy(s->current_frame, s->last_frame);
  637. if (ret < 0)
  638. return ret;
  639. ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
  640. s->current_frame->linesize[0] / 4,
  641. (uint32_t *)s->last_frame->data[0],
  642. s->last_frame->linesize[0] / 4);
  643. } else {
  644. return AVERROR_PATCHWELCOME;
  645. }
  646. if (ret < 0)
  647. return ret;
  648. if (avctx->bits_per_coded_sample != 16) {
  649. ret = av_frame_ref(data, s->current_frame);
  650. if (ret < 0)
  651. return ret;
  652. } else {
  653. uint8_t *dst = frame->data[0];
  654. int x, y;
  655. ret = av_frame_copy(frame, s->current_frame);
  656. if (ret < 0)
  657. return ret;
  658. for (y = 0; y < avctx->height; y++) {
  659. for (x = 0; x < avctx->width * 4; x++) {
  660. dst[x] = dst[x] << 3;
  661. }
  662. dst += frame->linesize[0];
  663. }
  664. }
  665. frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  666. FFSWAP(AVFrame *, s->current_frame, s->last_frame);
  667. frame->data[0] += frame->linesize[0] * (avctx->height - 1);
  668. frame->linesize[0] *= -1;
  669. *got_frame = 1;
  670. return avpkt->size;
  671. }
  672. static av_cold int decode_init(AVCodecContext *avctx)
  673. {
  674. SCPRContext *s = avctx->priv_data;
  675. switch (avctx->bits_per_coded_sample) {
  676. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
  677. case 24:
  678. case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
  679. default:
  680. av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
  681. return AVERROR_INVALIDDATA;
  682. }
  683. s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
  684. s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
  685. s->nbx = (avctx->width + 15) / 16;
  686. s->nby = (avctx->height + 15) / 16;
  687. s->nbcount = s->nbx * s->nby;
  688. s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
  689. if (!s->blocks)
  690. return AVERROR(ENOMEM);
  691. s->last_frame = av_frame_alloc();
  692. s->current_frame = av_frame_alloc();
  693. if (!s->last_frame || !s->current_frame)
  694. return AVERROR(ENOMEM);
  695. return 0;
  696. }
  697. static av_cold int decode_close(AVCodecContext *avctx)
  698. {
  699. SCPRContext *s = avctx->priv_data;
  700. av_freep(&s->blocks);
  701. av_frame_free(&s->last_frame);
  702. av_frame_free(&s->current_frame);
  703. return 0;
  704. }
  705. AVCodec ff_scpr_decoder = {
  706. .name = "scpr",
  707. .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
  708. .type = AVMEDIA_TYPE_VIDEO,
  709. .id = AV_CODEC_ID_SCPR,
  710. .priv_data_size = sizeof(SCPRContext),
  711. .init = decode_init,
  712. .close = decode_close,
  713. .decode = decode_frame,
  714. .capabilities = AV_CODEC_CAP_DR1,
  715. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  716. FF_CODEC_CAP_INIT_CLEANUP,
  717. };