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.

883 lines
27KB

  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. int 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 ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
  179. return ret;
  180. cnt[c] = cnt_c + step;
  181. totfr += step;
  182. if (totfr > BOT) {
  183. totfr = 0;
  184. for (i = 0; i < maxc; i++) {
  185. unsigned nc = (cnt[i] >> 1) + 1;
  186. cnt[i] = nc;
  187. totfr += nc;
  188. }
  189. }
  190. cnt[maxc] = totfr;
  191. *rval = c;
  192. return 0;
  193. }
  194. static int decode_unit(SCPRContext *s, PixelModel *pixel, unsigned step, unsigned *rval)
  195. {
  196. GetByteContext *gb = &s->gb;
  197. RangeCoder *rc = &s->rc;
  198. unsigned totfr = pixel->total_freq;
  199. unsigned value, x = 0, cumfr = 0, cnt_x = 0;
  200. int i, j, ret, c, cnt_c;
  201. if ((ret = s->get_freq(rc, totfr, &value)) < 0)
  202. return ret;
  203. while (x < 16) {
  204. cnt_x = pixel->lookup[x];
  205. if (value >= cumfr + cnt_x)
  206. cumfr += cnt_x;
  207. else
  208. break;
  209. x++;
  210. }
  211. c = x * 16;
  212. cnt_c = 0;
  213. while (c < 256) {
  214. cnt_c = pixel->freq[c];
  215. if (value >= cumfr + cnt_c)
  216. cumfr += cnt_c;
  217. else
  218. break;
  219. c++;
  220. }
  221. if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
  222. return ret;
  223. pixel->freq[c] = cnt_c + step;
  224. pixel->lookup[x] = cnt_x + step;
  225. totfr += step;
  226. if (totfr > BOT) {
  227. totfr = 0;
  228. for (i = 0; i < 256; i++) {
  229. unsigned nc = (pixel->freq[i] >> 1) + 1;
  230. pixel->freq[i] = nc;
  231. totfr += nc;
  232. }
  233. for (i = 0; i < 16; i++) {
  234. unsigned sum = 0;
  235. unsigned i16_17 = i << 4;
  236. for (j = 0; j < 16; j++)
  237. sum += pixel->freq[i16_17 + j];
  238. pixel->lookup[i] = sum;
  239. }
  240. }
  241. pixel->total_freq = totfr;
  242. *rval = c & s->cbits;
  243. return 0;
  244. }
  245. static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
  246. {
  247. SCPRContext *s = avctx->priv_data;
  248. GetByteContext *gb = &s->gb;
  249. int cx = 0, cx1 = 0, k = 0, clr = 0;
  250. int run, r, g, b, off, y = 0, x = 0, ret;
  251. const int cxshift = s->cxshift;
  252. unsigned lx, ly, ptype;
  253. reinit_tables(s);
  254. bytestream2_skip(gb, 2);
  255. init_rangecoder(&s->rc, gb);
  256. while (k < avctx->width + 1) {
  257. ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
  258. if (ret < 0)
  259. return ret;
  260. cx1 = (cx << 6) & 0xFC0;
  261. cx = r >> cxshift;
  262. ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
  263. if (ret < 0)
  264. return ret;
  265. cx1 = (cx << 6) & 0xFC0;
  266. cx = g >> cxshift;
  267. ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
  268. if (ret < 0)
  269. return ret;
  270. cx1 = (cx << 6) & 0xFC0;
  271. cx = b >> cxshift;
  272. ret = decode_value(s, s->run_model[0], 256, 400, &run);
  273. if (ret < 0)
  274. return ret;
  275. clr = (b << 16) + (g << 8) + r;
  276. k += run;
  277. while (run-- > 0) {
  278. dst[y * linesize + x] = clr;
  279. lx = x;
  280. ly = y;
  281. x++;
  282. if (x >= avctx->width) {
  283. x = 0;
  284. y++;
  285. }
  286. }
  287. }
  288. off = -linesize - 1;
  289. ptype = 0;
  290. while (x < avctx->width && y < avctx->height) {
  291. ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
  292. if (ret < 0)
  293. return ret;
  294. if (ptype == 0) {
  295. ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
  296. if (ret < 0)
  297. return ret;
  298. cx1 = (cx << 6) & 0xFC0;
  299. cx = r >> cxshift;
  300. ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
  301. if (ret < 0)
  302. return ret;
  303. cx1 = (cx << 6) & 0xFC0;
  304. cx = g >> cxshift;
  305. ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
  306. if (ret < 0)
  307. return ret;
  308. clr = (b << 16) + (g << 8) + r;
  309. }
  310. if (ptype > 5)
  311. return AVERROR_INVALIDDATA;
  312. ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
  313. if (ret < 0)
  314. return ret;
  315. switch (ptype) {
  316. case 0:
  317. while (run-- > 0) {
  318. if (y >= avctx->height)
  319. return AVERROR_INVALIDDATA;
  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 1:
  331. while (run-- > 0) {
  332. if (y >= avctx->height)
  333. return AVERROR_INVALIDDATA;
  334. dst[y * linesize + x] = dst[ly * linesize + lx];
  335. lx = x;
  336. ly = y;
  337. x++;
  338. if (x >= avctx->width) {
  339. x = 0;
  340. y++;
  341. }
  342. }
  343. clr = dst[ly * linesize + lx];
  344. break;
  345. case 2:
  346. while (run-- > 0) {
  347. if (y < 1 || y >= avctx->height)
  348. return AVERROR_INVALIDDATA;
  349. clr = dst[y * linesize + x + off + 1];
  350. dst[y * linesize + x] = clr;
  351. lx = x;
  352. ly = y;
  353. x++;
  354. if (x >= avctx->width) {
  355. x = 0;
  356. y++;
  357. }
  358. }
  359. break;
  360. case 4:
  361. while (run-- > 0) {
  362. uint8_t *odst = (uint8_t *)dst;
  363. if (y < 1 || y >= avctx->height)
  364. return AVERROR_INVALIDDATA;
  365. r = odst[(ly * linesize + lx) * 4] +
  366. odst[((y * linesize + x) + off) * 4 + 4] -
  367. odst[((y * linesize + x) + off) * 4];
  368. g = odst[(ly * linesize + lx) * 4 + 1] +
  369. odst[((y * linesize + x) + off) * 4 + 5] -
  370. odst[((y * linesize + x) + off) * 4 + 1];
  371. b = odst[(ly * linesize + lx) * 4 + 2] +
  372. odst[((y * linesize + x) + off) * 4 + 6] -
  373. odst[((y * linesize + x) + off) * 4 + 2];
  374. clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
  375. dst[y * linesize + x] = clr;
  376. lx = x;
  377. ly = y;
  378. x++;
  379. if (x >= avctx->width) {
  380. x = 0;
  381. y++;
  382. }
  383. }
  384. break;
  385. case 5:
  386. while (run-- > 0) {
  387. if (y < 1 || y >= avctx->height)
  388. return AVERROR_INVALIDDATA;
  389. clr = dst[y * linesize + x + off];
  390. dst[y * linesize + x] = clr;
  391. lx = x;
  392. ly = y;
  393. x++;
  394. if (x >= avctx->width) {
  395. x = 0;
  396. y++;
  397. }
  398. }
  399. break;
  400. }
  401. if (avctx->bits_per_coded_sample == 16) {
  402. cx1 = (clr & 0x3F00) >> 2;
  403. cx = (clr & 0xFFFFFF) >> 16;
  404. } else {
  405. cx1 = (clr & 0xFC00) >> 4;
  406. cx = (clr & 0xFFFFFF) >> 18;
  407. }
  408. }
  409. return 0;
  410. }
  411. static int decompress_p(AVCodecContext *avctx,
  412. uint32_t *dst, int linesize,
  413. uint32_t *prev, int plinesize)
  414. {
  415. SCPRContext *s = avctx->priv_data;
  416. GetByteContext *gb = &s->gb;
  417. int ret, temp, min, max, x, y, cx = 0, cx1 = 0;
  418. int backstep = linesize - avctx->width;
  419. const int cxshift = s->cxshift;
  420. if (bytestream2_get_byte(gb) == 0)
  421. return 0;
  422. bytestream2_skip(gb, 1);
  423. init_rangecoder(&s->rc, gb);
  424. ret = decode_value(s, s->range_model, 256, 1, &min);
  425. ret |= decode_value(s, s->range_model, 256, 1, &temp);
  426. min += temp << 8;
  427. ret |= decode_value(s, s->range_model, 256, 1, &max);
  428. ret |= decode_value(s, s->range_model, 256, 1, &temp);
  429. if (ret < 0)
  430. return ret;
  431. max += temp << 8;
  432. memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
  433. while (min <= max) {
  434. int fill, count;
  435. ret = decode_value(s, s->fill_model, 5, 10, &fill);
  436. ret |= decode_value(s, s->count_model, 256, 20, &count);
  437. if (ret < 0)
  438. return ret;
  439. while (min < s->nbcount && count-- > 0) {
  440. s->blocks[min++] = fill;
  441. }
  442. }
  443. for (y = 0; y < s->nby; y++) {
  444. for (x = 0; x < s->nbx; x++) {
  445. int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
  446. if (s->blocks[y * s->nbx + x] == 0)
  447. continue;
  448. if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
  449. ret = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
  450. ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
  451. ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
  452. ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
  453. if (ret < 0)
  454. return ret;
  455. sx2++;
  456. sy2++;
  457. }
  458. if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
  459. int i, j, by = y * 16, bx = x * 16;
  460. int mvx, mvy;
  461. ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
  462. ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
  463. if (ret < 0)
  464. return ret;
  465. mvx -= 256;
  466. mvy -= 256;
  467. if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
  468. by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
  469. return AVERROR_INVALIDDATA;
  470. for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
  471. for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
  472. dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
  473. }
  474. }
  475. } else {
  476. int run, r, g, b, z, bx = x * 16 + sx1, by = y * 16 + sy1;
  477. unsigned clr, ptype = 0;
  478. for (; by < y * 16 + sy2 && by < avctx->height;) {
  479. ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
  480. if (ptype == 0) {
  481. ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
  482. if (ret < 0)
  483. return ret;
  484. cx1 = (cx << 6) & 0xFC0;
  485. cx = r >> cxshift;
  486. ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
  487. if (ret < 0)
  488. return ret;
  489. cx1 = (cx << 6) & 0xFC0;
  490. cx = g >> cxshift;
  491. ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
  492. if (ret < 0)
  493. return ret;
  494. clr = (b << 16) + (g << 8) + r;
  495. }
  496. if (ptype > 5)
  497. return AVERROR_INVALIDDATA;
  498. ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
  499. if (ret < 0)
  500. return ret;
  501. switch (ptype) {
  502. case 0:
  503. while (run-- > 0) {
  504. if (by >= avctx->height)
  505. return AVERROR_INVALIDDATA;
  506. dst[by * linesize + bx] = clr;
  507. bx++;
  508. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  509. bx = x * 16 + sx1;
  510. by++;
  511. }
  512. }
  513. break;
  514. case 1:
  515. while (run-- > 0) {
  516. if (bx == 0) {
  517. if (by < 1)
  518. return AVERROR_INVALIDDATA;
  519. z = backstep;
  520. } else {
  521. z = 0;
  522. }
  523. if (by >= avctx->height)
  524. return AVERROR_INVALIDDATA;
  525. clr = dst[by * linesize + bx - 1 - z];
  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 2:
  535. while (run-- > 0) {
  536. if (by < 1 || by >= avctx->height)
  537. return AVERROR_INVALIDDATA;
  538. clr = dst[(by - 1) * linesize + bx];
  539. dst[by * linesize + bx] = clr;
  540. bx++;
  541. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  542. bx = x * 16 + sx1;
  543. by++;
  544. }
  545. }
  546. break;
  547. case 3:
  548. while (run-- > 0) {
  549. if (by >= avctx->height)
  550. return AVERROR_INVALIDDATA;
  551. clr = prev[by * linesize + bx];
  552. dst[by * linesize + bx] = clr;
  553. bx++;
  554. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  555. bx = x * 16 + sx1;
  556. by++;
  557. }
  558. }
  559. break;
  560. case 4:
  561. while (run-- > 0) {
  562. uint8_t *odst = (uint8_t *)dst;
  563. if (by < 1 || by >= avctx->height)
  564. return AVERROR_INVALIDDATA;
  565. if (bx == 0) {
  566. z = backstep;
  567. } else {
  568. z = 0;
  569. }
  570. r = odst[((by - 1) * linesize + bx) * 4] +
  571. odst[(by * linesize + bx - 1 - z) * 4] -
  572. odst[((by - 1) * linesize + bx - 1 - z) * 4];
  573. g = odst[((by - 1) * linesize + bx) * 4 + 1] +
  574. odst[(by * linesize + bx - 1 - z) * 4 + 1] -
  575. odst[((by - 1) * linesize + bx - 1 - z) * 4 + 1];
  576. b = odst[((by - 1) * linesize + bx) * 4 + 2] +
  577. odst[(by * linesize + bx - 1 - z) * 4 + 2] -
  578. odst[((by - 1) * linesize + bx - 1 - z) * 4 + 2];
  579. clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
  580. dst[by * linesize + bx] = clr;
  581. bx++;
  582. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  583. bx = x * 16 + sx1;
  584. by++;
  585. }
  586. }
  587. break;
  588. case 5:
  589. while (run-- > 0) {
  590. if (by < 1 || by >= avctx->height)
  591. return AVERROR_INVALIDDATA;
  592. if (bx == 0) {
  593. z = backstep;
  594. } else {
  595. z = 0;
  596. }
  597. clr = dst[(by - 1) * linesize + bx - 1 - z];
  598. dst[by * linesize + bx] = clr;
  599. bx++;
  600. if (bx >= x * 16 + sx2 || bx >= avctx->width) {
  601. bx = x * 16 + sx1;
  602. by++;
  603. }
  604. }
  605. break;
  606. }
  607. if (avctx->bits_per_coded_sample == 16) {
  608. cx1 = (clr & 0x3F00) >> 2;
  609. cx = (clr & 0xFFFFFF) >> 16;
  610. } else {
  611. cx1 = (clr & 0xFC00) >> 4;
  612. cx = (clr & 0xFFFFFF) >> 18;
  613. }
  614. }
  615. }
  616. }
  617. }
  618. return 0;
  619. }
  620. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  621. AVPacket *avpkt)
  622. {
  623. SCPRContext *s = avctx->priv_data;
  624. GetByteContext *gb = &s->gb;
  625. AVFrame *frame = data;
  626. int ret, type;
  627. if (avctx->bits_per_coded_sample == 16) {
  628. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  629. return ret;
  630. }
  631. if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
  632. return ret;
  633. bytestream2_init(gb, avpkt->data, avpkt->size);
  634. type = bytestream2_peek_byte(gb);
  635. if (type == 2) {
  636. s->get_freq = get_freq0;
  637. s->decode = decode0;
  638. frame->key_frame = 1;
  639. ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
  640. s->current_frame->linesize[0] / 4);
  641. } else if (type == 18) {
  642. s->get_freq = get_freq;
  643. s->decode = decode;
  644. frame->key_frame = 1;
  645. ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
  646. s->current_frame->linesize[0] / 4);
  647. } else if (type == 17) {
  648. uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
  649. int x, y;
  650. frame->key_frame = 1;
  651. bytestream2_skip(gb, 1);
  652. if (avctx->bits_per_coded_sample == 16) {
  653. uint16_t value = bytestream2_get_le16(gb);
  654. int r, g, b;
  655. r = (value ) & 31;
  656. g = (value >> 5) & 31;
  657. b = (value >> 10) & 31;
  658. clr = (r << 16) + (g << 8) + b;
  659. } else {
  660. clr = bytestream2_get_le24(gb);
  661. }
  662. for (y = 0; y < avctx->height; y++) {
  663. for (x = 0; x < avctx->width; x++) {
  664. dst[x] = clr;
  665. }
  666. dst += s->current_frame->linesize[0] / 4;
  667. }
  668. } else if (type == 0 || type == 1) {
  669. frame->key_frame = 0;
  670. ret = av_frame_copy(s->current_frame, s->last_frame);
  671. if (ret < 0)
  672. return ret;
  673. ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
  674. s->current_frame->linesize[0] / 4,
  675. (uint32_t *)s->last_frame->data[0],
  676. s->last_frame->linesize[0] / 4);
  677. } else {
  678. return AVERROR_PATCHWELCOME;
  679. }
  680. if (ret < 0)
  681. return ret;
  682. if (avctx->bits_per_coded_sample != 16) {
  683. ret = av_frame_ref(data, s->current_frame);
  684. if (ret < 0)
  685. return ret;
  686. } else {
  687. uint8_t *dst = frame->data[0];
  688. int x, y;
  689. ret = av_frame_copy(frame, s->current_frame);
  690. if (ret < 0)
  691. return ret;
  692. for (y = 0; y < avctx->height; y++) {
  693. for (x = 0; x < avctx->width * 4; x++) {
  694. dst[x] = dst[x] << 3;
  695. }
  696. dst += frame->linesize[0];
  697. }
  698. }
  699. frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  700. FFSWAP(AVFrame *, s->current_frame, s->last_frame);
  701. frame->data[0] += frame->linesize[0] * (avctx->height - 1);
  702. frame->linesize[0] *= -1;
  703. *got_frame = 1;
  704. return avpkt->size;
  705. }
  706. static av_cold int decode_init(AVCodecContext *avctx)
  707. {
  708. SCPRContext *s = avctx->priv_data;
  709. switch (avctx->bits_per_coded_sample) {
  710. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
  711. case 24:
  712. case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
  713. default:
  714. av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
  715. return AVERROR_INVALIDDATA;
  716. }
  717. s->get_freq = get_freq0;
  718. s->decode = decode0;
  719. s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
  720. s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
  721. s->nbx = (avctx->width + 15) / 16;
  722. s->nby = (avctx->height + 15) / 16;
  723. s->nbcount = s->nbx * s->nby;
  724. s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
  725. if (!s->blocks)
  726. return AVERROR(ENOMEM);
  727. s->last_frame = av_frame_alloc();
  728. s->current_frame = av_frame_alloc();
  729. if (!s->last_frame || !s->current_frame)
  730. return AVERROR(ENOMEM);
  731. return 0;
  732. }
  733. static av_cold int decode_close(AVCodecContext *avctx)
  734. {
  735. SCPRContext *s = avctx->priv_data;
  736. av_freep(&s->blocks);
  737. av_frame_free(&s->last_frame);
  738. av_frame_free(&s->current_frame);
  739. return 0;
  740. }
  741. AVCodec ff_scpr_decoder = {
  742. .name = "scpr",
  743. .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
  744. .type = AVMEDIA_TYPE_VIDEO,
  745. .id = AV_CODEC_ID_SCPR,
  746. .priv_data_size = sizeof(SCPRContext),
  747. .init = decode_init,
  748. .close = decode_close,
  749. .decode = decode_frame,
  750. .capabilities = AV_CODEC_CAP_DR1,
  751. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  752. FF_CODEC_CAP_INIT_CLEANUP,
  753. };