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.

675 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 (run < 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. min += temp << 8;
  321. ret |= decode_value(s, s->range_model, 256, 1, &max);
  322. ret |= decode_value(s, s->range_model, 256, 1, &temp);
  323. if (ret < 0)
  324. return ret;
  325. max += temp << 8;
  326. if (min > max || min >= s->nbcount)
  327. return AVERROR_INVALIDDATA;
  328. memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
  329. while (min <= max) {
  330. int fill, count;
  331. ret = decode_value(s, s->fill_model, 5, 10, &fill);
  332. ret |= decode_value(s, s->count_model, 256, 20, &count);
  333. if (ret < 0)
  334. return ret;
  335. if (count <= 0)
  336. return AVERROR_INVALIDDATA;
  337. while (min < s->nbcount && count-- > 0) {
  338. s->blocks[min++] = fill;
  339. }
  340. }
  341. ret = av_frame_copy(s->current_frame, s->last_frame);
  342. if (ret < 0)
  343. return ret;
  344. for (y = 0; y < s->nby; y++) {
  345. for (x = 0; x < s->nbx; x++) {
  346. int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
  347. if (s->blocks[y * s->nbx + x] == 0)
  348. continue;
  349. if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
  350. ret = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
  351. ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
  352. ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
  353. ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
  354. if (ret < 0)
  355. return ret;
  356. sx2++;
  357. sy2++;
  358. }
  359. if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
  360. int i, j, by = y * 16, bx = x * 16;
  361. int mvx, mvy;
  362. ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
  363. ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
  364. if (ret < 0)
  365. return ret;
  366. mvx -= 256;
  367. mvy -= 256;
  368. if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
  369. by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
  370. return AVERROR_INVALIDDATA;
  371. for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
  372. for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
  373. dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
  374. }
  375. }
  376. } else {
  377. int run, bx = x * 16 + sx1, by = y * 16 + sy1;
  378. uint32_t r, g, b, clr, ptype = 0;
  379. for (; by < y * 16 + sy2 && by < avctx->height;) {
  380. ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
  381. if (ret < 0)
  382. return ret;
  383. if (ptype == 0) {
  384. ret = decode_units(s, &r, &g, &b, &cx, &cx1);
  385. if (ret < 0)
  386. return ret;
  387. clr = (b << 16) + (g << 8) + r;
  388. }
  389. if (ptype > 5)
  390. return AVERROR_INVALIDDATA;
  391. ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
  392. if (ret < 0)
  393. return ret;
  394. if (run <= 0)
  395. return AVERROR_INVALIDDATA;
  396. ret = decode_run_p(avctx, ptype, run, x, y, clr,
  397. dst, prev, linesize, plinesize, &bx, &by,
  398. backstep, sx1, sx2, &cx, &cx1);
  399. if (ret < 0)
  400. return ret;
  401. }
  402. }
  403. }
  404. }
  405. return 0;
  406. }
  407. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  408. AVPacket *avpkt)
  409. {
  410. SCPRContext *s = avctx->priv_data;
  411. GetByteContext *gb = &s->gb;
  412. AVFrame *frame = data;
  413. int ret, type;
  414. if (avctx->bits_per_coded_sample == 16) {
  415. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  416. return ret;
  417. }
  418. if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
  419. return ret;
  420. bytestream2_init(gb, avpkt->data, avpkt->size);
  421. type = bytestream2_peek_byte(gb);
  422. if (type == 2) {
  423. s->version = 1;
  424. s->get_freq = get_freq0;
  425. s->decode = decode0;
  426. frame->key_frame = 1;
  427. ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
  428. s->current_frame->linesize[0] / 4);
  429. } else if (type == 18) {
  430. s->version = 2;
  431. s->get_freq = get_freq;
  432. s->decode = decode;
  433. frame->key_frame = 1;
  434. ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
  435. s->current_frame->linesize[0] / 4);
  436. } else if (type == 34) {
  437. frame->key_frame = 1;
  438. s->version = 3;
  439. ret = decompress_i3(avctx, (uint32_t *)s->current_frame->data[0],
  440. s->current_frame->linesize[0] / 4);
  441. } else if (type == 17 || type == 33) {
  442. uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
  443. int x, y;
  444. frame->key_frame = 1;
  445. bytestream2_skip(gb, 1);
  446. if (avctx->bits_per_coded_sample == 16) {
  447. uint16_t value = bytestream2_get_le16(gb);
  448. int r, g, b;
  449. r = (value ) & 31;
  450. g = (value >> 5) & 31;
  451. b = (value >> 10) & 31;
  452. clr = (r << 16) + (g << 8) + b;
  453. } else {
  454. clr = bytestream2_get_le24(gb);
  455. }
  456. for (y = 0; y < avctx->height; y++) {
  457. for (x = 0; x < avctx->width; x++) {
  458. dst[x] = clr;
  459. }
  460. dst += s->current_frame->linesize[0] / 4;
  461. }
  462. } else if (type == 0 || type == 1) {
  463. frame->key_frame = 0;
  464. if (s->version == 1 || s->version == 2)
  465. ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
  466. s->current_frame->linesize[0] / 4,
  467. (uint32_t *)s->last_frame->data[0],
  468. s->last_frame->linesize[0] / 4);
  469. else
  470. ret = decompress_p3(avctx, (uint32_t *)s->current_frame->data[0],
  471. s->current_frame->linesize[0] / 4,
  472. (uint32_t *)s->last_frame->data[0],
  473. s->last_frame->linesize[0] / 4);
  474. if (ret == 1)
  475. return avpkt->size;
  476. } else {
  477. return AVERROR_PATCHWELCOME;
  478. }
  479. if (ret < 0)
  480. return ret;
  481. if (avctx->bits_per_coded_sample != 16) {
  482. ret = av_frame_ref(data, s->current_frame);
  483. if (ret < 0)
  484. return ret;
  485. } else {
  486. uint8_t *dst = frame->data[0];
  487. int x, y;
  488. ret = av_frame_copy(frame, s->current_frame);
  489. if (ret < 0)
  490. return ret;
  491. // scale up each sample by 8
  492. for (y = 0; y < avctx->height; y++) {
  493. // If the image is sufficiently aligned, compute 8 samples at once
  494. if (!(((uintptr_t)dst) & 7)) {
  495. uint64_t *dst64 = (uint64_t *)dst;
  496. int w = avctx->width>>1;
  497. for (x = 0; x < w; x++) {
  498. dst64[x] = (dst64[x] << 3) & 0xFCFCFCFCFCFCFCFCULL;
  499. }
  500. x *= 8;
  501. } else
  502. x = 0;
  503. for (; x < avctx->width * 4; x++) {
  504. dst[x] = dst[x] << 3;
  505. }
  506. dst += frame->linesize[0];
  507. }
  508. }
  509. frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  510. FFSWAP(AVFrame *, s->current_frame, s->last_frame);
  511. frame->data[0] += frame->linesize[0] * (avctx->height - 1);
  512. frame->linesize[0] *= -1;
  513. *got_frame = 1;
  514. return avpkt->size;
  515. }
  516. static av_cold int decode_init(AVCodecContext *avctx)
  517. {
  518. SCPRContext *s = avctx->priv_data;
  519. switch (avctx->bits_per_coded_sample) {
  520. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
  521. case 24:
  522. case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
  523. default:
  524. av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
  525. return AVERROR_INVALIDDATA;
  526. }
  527. s->get_freq = get_freq0;
  528. s->decode = decode0;
  529. s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
  530. s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
  531. s->nbx = (avctx->width + 15) / 16;
  532. s->nby = (avctx->height + 15) / 16;
  533. s->nbcount = s->nbx * s->nby;
  534. s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
  535. if (!s->blocks)
  536. return AVERROR(ENOMEM);
  537. s->last_frame = av_frame_alloc();
  538. s->current_frame = av_frame_alloc();
  539. if (!s->last_frame || !s->current_frame)
  540. return AVERROR(ENOMEM);
  541. return 0;
  542. }
  543. static av_cold int decode_close(AVCodecContext *avctx)
  544. {
  545. SCPRContext *s = avctx->priv_data;
  546. av_freep(&s->blocks);
  547. av_frame_free(&s->last_frame);
  548. av_frame_free(&s->current_frame);
  549. return 0;
  550. }
  551. AVCodec ff_scpr_decoder = {
  552. .name = "scpr",
  553. .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
  554. .type = AVMEDIA_TYPE_VIDEO,
  555. .id = AV_CODEC_ID_SCPR,
  556. .priv_data_size = sizeof(SCPRContext),
  557. .init = decode_init,
  558. .close = decode_close,
  559. .decode = decode_frame,
  560. .capabilities = AV_CODEC_CAP_DR1,
  561. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  562. FF_CODEC_CAP_INIT_CLEANUP,
  563. };