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.

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