scpr.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. #ifndef AVCODEC_SCPR_H
  23. #define AVCODEC_SCPR_H
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "avcodec.h"
  28. #include "bytestream.h"
  29. #include "internal.h"
  30. #include "scpr3.h"
  31. typedef struct RangeCoder {
  32. uint32_t code;
  33. uint32_t range;
  34. uint32_t code1;
  35. } RangeCoder;
  36. typedef struct PixelModel {
  37. uint32_t freq[256];
  38. uint32_t lookup[16];
  39. uint32_t total_freq;
  40. } PixelModel;
  41. typedef struct SCPRContext {
  42. int version;
  43. AVFrame *last_frame;
  44. AVFrame *current_frame;
  45. GetByteContext gb;
  46. RangeCoder rc;
  47. PixelModel pixel_model[3][4096];
  48. uint32_t op_model[6][7];
  49. uint32_t run_model[6][257];
  50. uint32_t range_model[257];
  51. uint32_t count_model[257];
  52. uint32_t fill_model[6];
  53. uint32_t sxy_model[4][17];
  54. uint32_t mv_model[2][513];
  55. uint32_t nbx, nby;
  56. uint32_t nbcount;
  57. uint32_t *blocks;
  58. uint32_t cbits;
  59. int cxshift;
  60. PixelModel3 pixel_model3[3][4096];
  61. RunModel3 run_model3[6];
  62. RunModel3 range_model3;
  63. RunModel3 count_model3;
  64. FillModel3 fill_model3;
  65. SxyModel3 sxy_model3[4];
  66. MVModel3 mv_model3[2];
  67. OpModel3 op_model3[6];
  68. int (*get_freq)(RangeCoder *rc, uint32_t total_freq, uint32_t *freq);
  69. int (*decode)(GetByteContext *gb, RangeCoder *rc, uint32_t cumFreq, uint32_t freq, uint32_t total_freq);
  70. } SCPRContext;
  71. static int decode_run_i(AVCodecContext *avctx, uint32_t ptype, int run,
  72. int *px, int *py, uint32_t clr, uint32_t *dst,
  73. int linesize, uint32_t *plx, uint32_t *ply,
  74. uint32_t backstep, int off, int *cx, int *cx1)
  75. {
  76. uint32_t r, g, b;
  77. int z;
  78. int x = *px,
  79. y = *py;
  80. uint32_t lx = *plx,
  81. ly = *ply;
  82. if (y >= avctx->height)
  83. return AVERROR_INVALIDDATA;
  84. switch (ptype) {
  85. case 0:
  86. while (run-- > 0) {
  87. dst[y * linesize + x] = clr;
  88. lx = x;
  89. ly = y;
  90. (x)++;
  91. if (x >= avctx->width) {
  92. x = 0;
  93. (y)++;
  94. if (y >= avctx->height && run)
  95. return AVERROR_INVALIDDATA;
  96. }
  97. }
  98. break;
  99. case 1:
  100. while (run-- > 0) {
  101. dst[y * linesize + x] = dst[ly * linesize + lx];
  102. lx = x;
  103. ly = y;
  104. (x)++;
  105. if (x >= avctx->width) {
  106. x = 0;
  107. (y)++;
  108. if (y >= avctx->height && run)
  109. return AVERROR_INVALIDDATA;
  110. }
  111. }
  112. clr = dst[ly * linesize + lx];
  113. break;
  114. case 2:
  115. if (y < 1)
  116. return AVERROR_INVALIDDATA;
  117. while (run-- > 0) {
  118. clr = dst[y * linesize + x + off + 1];
  119. dst[y * linesize + x] = clr;
  120. lx = x;
  121. ly = y;
  122. (x)++;
  123. if (x >= avctx->width) {
  124. x = 0;
  125. (y)++;
  126. if (y >= avctx->height && run)
  127. return AVERROR_INVALIDDATA;
  128. }
  129. }
  130. break;
  131. case 4:
  132. if (y < 1 || (y == 1 && x == 0))
  133. return AVERROR_INVALIDDATA;
  134. while (run-- > 0) {
  135. uint8_t *odst = (uint8_t *)dst;
  136. int off1 = (ly * linesize + lx) * 4;
  137. int off2 = ((y * linesize + x) + off) * 4;
  138. if (x == 0) {
  139. z = backstep * 4;
  140. } else {
  141. z = 0;
  142. }
  143. r = odst[off1] +
  144. odst[off2 + 4] -
  145. odst[off2 - z ];
  146. g = odst[off1 + 1] +
  147. odst[off2 + 5] -
  148. odst[off2 - z + 1];
  149. b = odst[off1 + 2] +
  150. odst[off2 + 6] -
  151. odst[off2 - z + 2];
  152. clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
  153. dst[y * linesize + x] = clr;
  154. lx = x;
  155. ly = y;
  156. (x)++;
  157. if (x >= avctx->width) {
  158. x = 0;
  159. (y)++;
  160. if (y >= avctx->height && run)
  161. return AVERROR_INVALIDDATA;
  162. }
  163. }
  164. break;
  165. case 5:
  166. if (y < 1 || (y == 1 && x == 0))
  167. return AVERROR_INVALIDDATA;
  168. while (run-- > 0) {
  169. if (x == 0) {
  170. z = backstep;
  171. } else {
  172. z = 0;
  173. }
  174. clr = dst[y * linesize + x + off - z];
  175. dst[y * linesize + x] = clr;
  176. lx = x;
  177. ly = y;
  178. (x)++;
  179. if (x >= avctx->width) {
  180. x = 0;
  181. (y)++;
  182. if (y >= avctx->height && run)
  183. return AVERROR_INVALIDDATA;
  184. }
  185. }
  186. break;
  187. }
  188. *px = x;
  189. *py = y;
  190. *plx= lx;
  191. *ply= ly;
  192. if (avctx->bits_per_coded_sample == 16) {
  193. *cx1 = (clr & 0x3F00) >> 2;
  194. *cx = (clr & 0x3FFFFF) >> 16;
  195. } else {
  196. *cx1 = (clr & 0xFC00) >> 4;
  197. *cx = (clr & 0xFFFFFF) >> 18;
  198. }
  199. return 0;
  200. }
  201. static int decode_run_p(AVCodecContext *avctx, uint32_t ptype, int run,
  202. int x, int y, uint32_t clr,
  203. uint32_t *dst, uint32_t *prev,
  204. int linesize, int plinesize,
  205. uint32_t *bx, uint32_t *by,
  206. uint32_t backstep, int sx1, int sx2,
  207. int *cx, int *cx1)
  208. {
  209. uint32_t r, g, b;
  210. int z;
  211. switch (ptype) {
  212. case 0:
  213. while (run-- > 0) {
  214. if (*by >= avctx->height)
  215. return AVERROR_INVALIDDATA;
  216. dst[*by * linesize + *bx] = clr;
  217. (*bx)++;
  218. if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
  219. *bx = x * 16 + sx1;
  220. (*by)++;
  221. }
  222. }
  223. break;
  224. case 1:
  225. while (run-- > 0) {
  226. if (*bx == 0) {
  227. if (*by < 1)
  228. return AVERROR_INVALIDDATA;
  229. z = backstep;
  230. } else {
  231. z = 0;
  232. }
  233. if (*by >= avctx->height)
  234. return AVERROR_INVALIDDATA;
  235. clr = dst[*by * linesize + *bx - 1 - z];
  236. dst[*by * linesize + *bx] = clr;
  237. (*bx)++;
  238. if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
  239. *bx = x * 16 + sx1;
  240. (*by)++;
  241. }
  242. }
  243. break;
  244. case 2:
  245. while (run-- > 0) {
  246. if (*by < 1 || *by >= avctx->height)
  247. return AVERROR_INVALIDDATA;
  248. clr = dst[(*by - 1) * linesize + *bx];
  249. dst[*by * linesize + *bx] = clr;
  250. (*bx)++;
  251. if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
  252. *bx = x * 16 + sx1;
  253. (*by)++;
  254. }
  255. }
  256. break;
  257. case 3:
  258. while (run-- > 0) {
  259. if (*by >= avctx->height)
  260. return AVERROR_INVALIDDATA;
  261. clr = prev[*by * plinesize + *bx];
  262. dst[*by * linesize + *bx] = clr;
  263. (*bx)++;
  264. if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
  265. *bx = x * 16 + sx1;
  266. (*by)++;
  267. }
  268. }
  269. break;
  270. case 4:
  271. while (run-- > 0) {
  272. uint8_t *odst = (uint8_t *)dst;
  273. if (*by < 1 || *by >= avctx->height)
  274. return AVERROR_INVALIDDATA;
  275. if (*bx == 0) {
  276. if (*by < 2)
  277. return AVERROR_INVALIDDATA;
  278. z = backstep;
  279. } else {
  280. z = 0;
  281. }
  282. r = odst[((*by - 1) * linesize + *bx) * 4] +
  283. odst[(*by * linesize + *bx - 1 - z) * 4] -
  284. odst[((*by - 1) * linesize + *bx - 1 - z) * 4];
  285. g = odst[((*by - 1) * linesize + *bx) * 4 + 1] +
  286. odst[(*by * linesize + *bx - 1 - z) * 4 + 1] -
  287. odst[((*by - 1) * linesize + *bx - 1 - z) * 4 + 1];
  288. b = odst[((*by - 1) * linesize + *bx) * 4 + 2] +
  289. odst[(*by * linesize + *bx - 1 - z) * 4 + 2] -
  290. odst[((*by - 1) * linesize + *bx - 1 - z) * 4 + 2];
  291. clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
  292. dst[*by * linesize + *bx] = clr;
  293. (*bx)++;
  294. if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
  295. *bx = x * 16 + sx1;
  296. (*by)++;
  297. }
  298. }
  299. break;
  300. case 5:
  301. while (run-- > 0) {
  302. if (*by < 1 || *by >= avctx->height)
  303. return AVERROR_INVALIDDATA;
  304. if (*bx == 0) {
  305. if (*by < 2)
  306. return AVERROR_INVALIDDATA;
  307. z = backstep;
  308. } else {
  309. z = 0;
  310. }
  311. clr = dst[(*by - 1) * linesize + *bx - 1 - z];
  312. dst[*by * linesize + *bx] = clr;
  313. (*bx)++;
  314. if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
  315. *bx = x * 16 + sx1;
  316. (*by)++;
  317. }
  318. }
  319. break;
  320. }
  321. if (avctx->bits_per_coded_sample == 16) {
  322. *cx1 = (clr & 0x3F00) >> 2;
  323. *cx = (clr & 0x3FFFFF) >> 16;
  324. } else {
  325. *cx1 = (clr & 0xFC00) >> 4;
  326. *cx = (clr & 0xFFFFFF) >> 18;
  327. }
  328. return 0;
  329. }
  330. #endif /* AVCODEC_SCPR_H */