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.

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