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.

680 lines
19KB

  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. #include "scpr.h"
  29. #include "scpr3.h"
  30. #define TOP 0x01000000
  31. #define BOT 0x010000
  32. #include "scpr3.c"
  33. static void init_rangecoder(RangeCoder *rc, GetByteContext *gb)
  34. {
  35. rc->code1 = 0;
  36. rc->range = 0xFFFFFFFFU;
  37. rc->code = bytestream2_get_be32(gb);
  38. }
  39. static void reinit_tables(SCPRContext *s)
  40. {
  41. int comp, i, j;
  42. for (comp = 0; comp < 3; comp++) {
  43. for (j = 0; j < 4096; j++) {
  44. if (s->pixel_model[comp][j].total_freq != 256) {
  45. for (i = 0; i < 256; i++)
  46. s->pixel_model[comp][j].freq[i] = 1;
  47. for (i = 0; i < 16; i++)
  48. s->pixel_model[comp][j].lookup[i] = 16;
  49. s->pixel_model[comp][j].total_freq = 256;
  50. }
  51. }
  52. }
  53. for (j = 0; j < 6; j++) {
  54. uint32_t *p = s->run_model[j];
  55. for (i = 0; i < 256; i++)
  56. p[i] = 1;
  57. p[256] = 256;
  58. }
  59. for (j = 0; j < 6; j++) {
  60. uint32_t *op = s->op_model[j];
  61. for (i = 0; i < 6; i++)
  62. op[i] = 1;
  63. op[6] = 6;
  64. }
  65. for (i = 0; i < 256; i++) {
  66. s->range_model[i] = 1;
  67. s->count_model[i] = 1;
  68. }
  69. s->range_model[256] = 256;
  70. s->count_model[256] = 256;
  71. for (i = 0; i < 5; i++) {
  72. s->fill_model[i] = 1;
  73. }
  74. s->fill_model[5] = 5;
  75. for (j = 0; j < 4; j++) {
  76. for (i = 0; i < 16; i++) {
  77. s->sxy_model[j][i] = 1;
  78. }
  79. s->sxy_model[j][16] = 16;
  80. }
  81. for (i = 0; i < 512; i++) {
  82. s->mv_model[0][i] = 1;
  83. s->mv_model[1][i] = 1;
  84. }
  85. s->mv_model[0][512] = 512;
  86. s->mv_model[1][512] = 512;
  87. }
  88. static int decode(GetByteContext *gb, RangeCoder *rc, uint32_t cumFreq, uint32_t freq, uint32_t total_freq)
  89. {
  90. rc->code -= cumFreq * rc->range;
  91. rc->range *= freq;
  92. while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
  93. uint32_t byte = bytestream2_get_byteu(gb);
  94. rc->code = (rc->code << 8) | byte;
  95. rc->range <<= 8;
  96. }
  97. return 0;
  98. }
  99. static int get_freq(RangeCoder *rc, uint32_t total_freq, uint32_t *freq)
  100. {
  101. if (total_freq == 0)
  102. return AVERROR_INVALIDDATA;
  103. rc->range = rc->range / total_freq;
  104. if (rc->range == 0)
  105. return AVERROR_INVALIDDATA;
  106. *freq = rc->code / rc->range;
  107. return 0;
  108. }
  109. static int decode0(GetByteContext *gb, RangeCoder *rc, uint32_t cumFreq, uint32_t freq, uint32_t total_freq)
  110. {
  111. uint32_t t;
  112. if (total_freq == 0)
  113. return AVERROR_INVALIDDATA;
  114. t = rc->range * (uint64_t)cumFreq / total_freq;
  115. rc->code1 += t + 1;
  116. rc->range = rc->range * (uint64_t)(freq + cumFreq) / total_freq - (t + 1);
  117. while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
  118. uint32_t byte = bytestream2_get_byteu(gb);
  119. rc->code = (rc->code << 8) | byte;
  120. rc->code1 <<= 8;
  121. rc->range <<= 8;
  122. }
  123. return 0;
  124. }
  125. static int get_freq0(RangeCoder *rc, uint32_t total_freq, uint32_t *freq)
  126. {
  127. if (rc->range == 0)
  128. return AVERROR_INVALIDDATA;
  129. *freq = total_freq * (uint64_t)(rc->code - rc->code1) / rc->range;
  130. return 0;
  131. }
  132. static int decode_value(SCPRContext *s, uint32_t *cnt, uint32_t maxc, uint32_t step, uint32_t *rval)
  133. {
  134. GetByteContext *gb = &s->gb;
  135. RangeCoder *rc = &s->rc;
  136. uint32_t totfr = cnt[maxc];
  137. uint32_t value;
  138. uint32_t c = 0, cumfr = 0, cnt_c = 0;
  139. int i, ret;
  140. if ((ret = s->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. if (c >= maxc)
  151. return AVERROR_INVALIDDATA;
  152. if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
  153. return ret;
  154. cnt[c] = cnt_c + step;
  155. totfr += step;
  156. if (totfr > BOT) {
  157. totfr = 0;
  158. for (i = 0; i < maxc; i++) {
  159. uint32_t nc = (cnt[i] >> 1) + 1;
  160. cnt[i] = nc;
  161. totfr += nc;
  162. }
  163. }
  164. cnt[maxc] = totfr;
  165. *rval = c;
  166. return 0;
  167. }
  168. static int decode_unit(SCPRContext *s, PixelModel *pixel, uint32_t step, uint32_t *rval)
  169. {
  170. GetByteContext *gb = &s->gb;
  171. RangeCoder *rc = &s->rc;
  172. uint32_t totfr = pixel->total_freq;
  173. uint32_t value, x = 0, cumfr = 0, cnt_x = 0;
  174. int i, j, ret, c, cnt_c;
  175. if ((ret = s->get_freq(rc, totfr, &value)) < 0)
  176. return ret;
  177. while (x < 16) {
  178. cnt_x = pixel->lookup[x];
  179. if (value >= cumfr + cnt_x)
  180. cumfr += cnt_x;
  181. else
  182. break;
  183. x++;
  184. }
  185. c = x * 16;
  186. cnt_c = 0;
  187. while (c < 256) {
  188. cnt_c = pixel->freq[c];
  189. if (value >= cumfr + cnt_c)
  190. cumfr += cnt_c;
  191. else
  192. break;
  193. c++;
  194. }
  195. if (x >= 16 || c >= 256) {
  196. return AVERROR_INVALIDDATA;
  197. }
  198. if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
  199. return ret;
  200. pixel->freq[c] = cnt_c + step;
  201. pixel->lookup[x] = cnt_x + step;
  202. totfr += step;
  203. if (totfr > BOT) {
  204. totfr = 0;
  205. for (i = 0; i < 256; i++) {
  206. uint32_t nc = (pixel->freq[i] >> 1) + 1;
  207. pixel->freq[i] = nc;
  208. totfr += nc;
  209. }
  210. for (i = 0; i < 16; i++) {
  211. uint32_t sum = 0;
  212. uint32_t i16_17 = i << 4;
  213. for (j = 0; j < 16; j++)
  214. sum += pixel->freq[i16_17 + j];
  215. pixel->lookup[i] = sum;
  216. }
  217. }
  218. pixel->total_freq = totfr;
  219. *rval = c & s->cbits;
  220. return 0;
  221. }
  222. static int decode_units(SCPRContext *s, uint32_t *r, uint32_t *g, uint32_t *b,
  223. int *cx, int *cx1)
  224. {
  225. const int cxshift = s->cxshift;
  226. int ret;
  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. return 0;
  243. }
  244. static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
  245. {
  246. SCPRContext *s = avctx->priv_data;
  247. GetByteContext *gb = &s->gb;
  248. int cx = 0, cx1 = 0, k = 0;
  249. int run, off, y = 0, x = 0, ret;
  250. uint32_t clr = 0, r, g, b, backstep = linesize - avctx->width;
  251. uint32_t lx, ly, ptype;
  252. reinit_tables(s);
  253. bytestream2_skip(gb, 2);
  254. init_rangecoder(&s->rc, gb);
  255. while (k < avctx->width + 1) {
  256. ret = decode_units(s, &r, &g, &b, &cx, &cx1);
  257. if (ret < 0)
  258. return ret;
  259. ret = decode_value(s, s->run_model[0], 256, 400, &run);
  260. if (ret < 0)
  261. return ret;
  262. if (run <= 0)
  263. return AVERROR_INVALIDDATA;
  264. clr = (b << 16) + (g << 8) + r;
  265. k += run;
  266. while (run-- > 0) {
  267. if (y >= avctx->height)
  268. return AVERROR_INVALIDDATA;
  269. dst[y * linesize + x] = clr;
  270. lx = x;
  271. ly = y;
  272. x++;
  273. if (x >= avctx->width) {
  274. x = 0;
  275. y++;
  276. }
  277. }
  278. }
  279. off = -linesize - 1;
  280. ptype = 0;
  281. while (x < avctx->width && y < avctx->height) {
  282. ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
  283. if (ret < 0)
  284. return ret;
  285. if (ptype == 0) {
  286. ret = decode_units(s, &r, &g, &b, &cx, &cx1);
  287. if (ret < 0)
  288. return ret;
  289. clr = (b << 16) + (g << 8) + r;
  290. }
  291. if (ptype > 5)
  292. return AVERROR_INVALIDDATA;
  293. ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
  294. if (ret < 0)
  295. return ret;
  296. if (run <= 0)
  297. return AVERROR_INVALIDDATA;
  298. ret = decode_run_i(avctx, ptype, run, &x, &y, clr,
  299. dst, linesize, &lx, &ly,
  300. backstep, off, &cx, &cx1);
  301. if (ret < 0)
  302. return ret;
  303. }
  304. return 0;
  305. }
  306. static int decompress_p(AVCodecContext *avctx,
  307. uint32_t *dst, int linesize,
  308. uint32_t *prev, int plinesize)
  309. {
  310. SCPRContext *s = avctx->priv_data;
  311. GetByteContext *gb = &s->gb;
  312. int ret, temp = 0, min, max, x, y, cx = 0, cx1 = 0;
  313. int backstep = linesize - avctx->width;
  314. if (bytestream2_get_byte(gb) == 0)
  315. return 1;
  316. bytestream2_skip(gb, 1);
  317. init_rangecoder(&s->rc, gb);
  318. ret = decode_value(s, s->range_model, 256, 1, &min);
  319. ret |= decode_value(s, s->range_model, 256, 1, &temp);
  320. if (ret < 0)
  321. return ret;
  322. min += temp << 8;
  323. ret = decode_value(s, s->range_model, 256, 1, &max);
  324. ret |= decode_value(s, s->range_model, 256, 1, &temp);
  325. if (ret < 0)
  326. return ret;
  327. max += temp << 8;
  328. if (min > max || min >= s->nbcount)
  329. return AVERROR_INVALIDDATA;
  330. memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
  331. while (min <= max) {
  332. int fill, count;
  333. ret = decode_value(s, s->fill_model, 5, 10, &fill);
  334. ret |= decode_value(s, s->count_model, 256, 20, &count);
  335. if (ret < 0)
  336. return ret;
  337. if (count <= 0)
  338. return AVERROR_INVALIDDATA;
  339. while (min < s->nbcount && count-- > 0) {
  340. s->blocks[min++] = fill;
  341. }
  342. }
  343. ret = av_frame_copy(s->current_frame, s->last_frame);
  344. if (ret < 0)
  345. return ret;
  346. for (y = 0; y < s->nby; y++) {
  347. for (x = 0; x < s->nbx; x++) {
  348. int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
  349. if (s->blocks[y * s->nbx + x] == 0)
  350. continue;
  351. if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
  352. ret = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
  353. ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
  354. ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
  355. ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
  356. if (ret < 0)
  357. return ret;
  358. sx2++;
  359. sy2++;
  360. }
  361. if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
  362. int i, j, by = y * 16, bx = x * 16;
  363. int mvx, mvy;
  364. ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
  365. ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
  366. if (ret < 0)
  367. return ret;
  368. mvx -= 256;
  369. mvy -= 256;
  370. if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
  371. by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
  372. return AVERROR_INVALIDDATA;
  373. for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
  374. for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
  375. dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
  376. }
  377. }
  378. } else {
  379. int run, bx = x * 16 + sx1, by = y * 16 + sy1;
  380. uint32_t r, g, b, clr, ptype = 0;
  381. for (; by < y * 16 + sy2 && by < avctx->height;) {
  382. ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
  383. if (ret < 0)
  384. return ret;
  385. if (ptype == 0) {
  386. ret = decode_units(s, &r, &g, &b, &cx, &cx1);
  387. if (ret < 0)
  388. return ret;
  389. clr = (b << 16) + (g << 8) + r;
  390. }
  391. if (ptype > 5)
  392. return AVERROR_INVALIDDATA;
  393. ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
  394. if (ret < 0)
  395. return ret;
  396. if (run <= 0)
  397. return AVERROR_INVALIDDATA;
  398. ret = decode_run_p(avctx, ptype, run, x, y, clr,
  399. dst, prev, linesize, plinesize, &bx, &by,
  400. backstep, sx1, sx2, &cx, &cx1);
  401. if (ret < 0)
  402. return ret;
  403. }
  404. }
  405. }
  406. }
  407. return 0;
  408. }
  409. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  410. AVPacket *avpkt)
  411. {
  412. SCPRContext *s = avctx->priv_data;
  413. GetByteContext *gb = &s->gb;
  414. AVFrame *frame = data;
  415. int ret, type;
  416. if (avctx->bits_per_coded_sample == 16) {
  417. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  418. return ret;
  419. }
  420. if ((ret = ff_reget_buffer(avctx, s->current_frame, 0)) < 0)
  421. return ret;
  422. bytestream2_init(gb, avpkt->data, avpkt->size);
  423. type = bytestream2_peek_byte(gb);
  424. if (type == 2) {
  425. s->version = 1;
  426. s->get_freq = get_freq0;
  427. s->decode = decode0;
  428. frame->key_frame = 1;
  429. ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
  430. s->current_frame->linesize[0] / 4);
  431. } else if (type == 18) {
  432. s->version = 2;
  433. s->get_freq = get_freq;
  434. s->decode = decode;
  435. frame->key_frame = 1;
  436. ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
  437. s->current_frame->linesize[0] / 4);
  438. } else if (type == 34) {
  439. frame->key_frame = 1;
  440. s->version = 3;
  441. ret = decompress_i3(avctx, (uint32_t *)s->current_frame->data[0],
  442. s->current_frame->linesize[0] / 4);
  443. } else if (type == 17 || type == 33) {
  444. uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
  445. int y;
  446. frame->key_frame = 1;
  447. bytestream2_skip(gb, 1);
  448. if (avctx->bits_per_coded_sample == 16) {
  449. uint16_t value = bytestream2_get_le16(gb);
  450. int r, g, b;
  451. r = (value ) & 31;
  452. g = (value >> 5) & 31;
  453. b = (value >> 10) & 31;
  454. clr = (r << 16) + (g << 8) + b;
  455. } else {
  456. clr = bytestream2_get_le24(gb);
  457. }
  458. for (y = 0; y < avctx->height; y++) {
  459. dst[0] = clr;
  460. av_memcpy_backptr((uint8_t*)(dst+1), 4, 4*avctx->width - 4);
  461. dst += s->current_frame->linesize[0] / 4;
  462. }
  463. } else if (type == 0 || type == 1) {
  464. frame->key_frame = 0;
  465. if (s->version == 1 || s->version == 2)
  466. ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
  467. s->current_frame->linesize[0] / 4,
  468. (uint32_t *)s->last_frame->data[0],
  469. s->last_frame->linesize[0] / 4);
  470. else
  471. ret = decompress_p3(avctx, (uint32_t *)s->current_frame->data[0],
  472. s->current_frame->linesize[0] / 4,
  473. (uint32_t *)s->last_frame->data[0],
  474. s->last_frame->linesize[0] / 4);
  475. if (ret == 1)
  476. return avpkt->size;
  477. } else {
  478. return AVERROR_PATCHWELCOME;
  479. }
  480. if (ret < 0)
  481. return ret;
  482. if (bytestream2_get_bytes_left(gb) > 5)
  483. return AVERROR_INVALIDDATA;
  484. if (avctx->bits_per_coded_sample != 16) {
  485. ret = av_frame_ref(data, s->current_frame);
  486. if (ret < 0)
  487. return ret;
  488. } else {
  489. uint8_t *dst = frame->data[0];
  490. int x, y;
  491. ret = av_frame_copy(frame, s->current_frame);
  492. if (ret < 0)
  493. return ret;
  494. // scale up each sample by 8
  495. for (y = 0; y < avctx->height; y++) {
  496. // If the image is sufficiently aligned, compute 8 samples at once
  497. if (!(((uintptr_t)dst) & 7)) {
  498. uint64_t *dst64 = (uint64_t *)dst;
  499. int w = avctx->width>>1;
  500. for (x = 0; x < w; x++) {
  501. dst64[x] = (dst64[x] << 3) & 0xFCFCFCFCFCFCFCFCULL;
  502. }
  503. x *= 8;
  504. } else
  505. x = 0;
  506. for (; x < avctx->width * 4; x++) {
  507. dst[x] = dst[x] << 3;
  508. }
  509. dst += frame->linesize[0];
  510. }
  511. }
  512. frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  513. FFSWAP(AVFrame *, s->current_frame, s->last_frame);
  514. frame->data[0] += frame->linesize[0] * (avctx->height - 1);
  515. frame->linesize[0] *= -1;
  516. *got_frame = 1;
  517. return avpkt->size;
  518. }
  519. static av_cold int decode_init(AVCodecContext *avctx)
  520. {
  521. SCPRContext *s = avctx->priv_data;
  522. switch (avctx->bits_per_coded_sample) {
  523. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
  524. case 24:
  525. case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
  526. default:
  527. av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
  528. return AVERROR_INVALIDDATA;
  529. }
  530. s->get_freq = get_freq0;
  531. s->decode = decode0;
  532. s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
  533. s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
  534. s->nbx = (avctx->width + 15) / 16;
  535. s->nby = (avctx->height + 15) / 16;
  536. s->nbcount = s->nbx * s->nby;
  537. s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
  538. if (!s->blocks)
  539. return AVERROR(ENOMEM);
  540. s->last_frame = av_frame_alloc();
  541. s->current_frame = av_frame_alloc();
  542. if (!s->last_frame || !s->current_frame)
  543. return AVERROR(ENOMEM);
  544. return 0;
  545. }
  546. static av_cold int decode_close(AVCodecContext *avctx)
  547. {
  548. SCPRContext *s = avctx->priv_data;
  549. av_freep(&s->blocks);
  550. av_frame_free(&s->last_frame);
  551. av_frame_free(&s->current_frame);
  552. return 0;
  553. }
  554. AVCodec ff_scpr_decoder = {
  555. .name = "scpr",
  556. .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
  557. .type = AVMEDIA_TYPE_VIDEO,
  558. .id = AV_CODEC_ID_SCPR,
  559. .priv_data_size = sizeof(SCPRContext),
  560. .init = decode_init,
  561. .close = decode_close,
  562. .decode = decode_frame,
  563. .capabilities = AV_CODEC_CAP_DR1,
  564. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  565. FF_CODEC_CAP_INIT_CLEANUP,
  566. };