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.

350 lines
10.0KB

  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 *x, int *y, uint32_t clr, uint32_t *dst,
  73. int linesize, uint32_t *lx, uint32_t *ly,
  74. uint32_t backstep, int off, int *cx, int *cx1)
  75. {
  76. uint32_t r, g, b;
  77. int z;
  78. switch (ptype) {
  79. case 0:
  80. while (run-- > 0) {
  81. if (*y >= avctx->height)
  82. return AVERROR_INVALIDDATA;
  83. dst[*y * linesize + *x] = clr;
  84. *lx = *x;
  85. *ly = *y;
  86. (*x)++;
  87. if (*x >= avctx->width) {
  88. *x = 0;
  89. (*y)++;
  90. }
  91. }
  92. break;
  93. case 1:
  94. while (run-- > 0) {
  95. if (*y >= avctx->height)
  96. return AVERROR_INVALIDDATA;
  97. dst[*y * linesize + *x] = dst[*ly * linesize + *lx];
  98. *lx = *x;
  99. *ly = *y;
  100. (*x)++;
  101. if (*x >= avctx->width) {
  102. *x = 0;
  103. (*y)++;
  104. }
  105. }
  106. clr = dst[*ly * linesize + *lx];
  107. break;
  108. case 2:
  109. while (run-- > 0) {
  110. if (*y < 1 || *y >= avctx->height)
  111. return AVERROR_INVALIDDATA;
  112. clr = dst[*y * linesize + *x + off + 1];
  113. dst[*y * linesize + *x] = clr;
  114. *lx = *x;
  115. *ly = *y;
  116. (*x)++;
  117. if (*x >= avctx->width) {
  118. *x = 0;
  119. (*y)++;
  120. }
  121. }
  122. break;
  123. case 4:
  124. while (run-- > 0) {
  125. uint8_t *odst = (uint8_t *)dst;
  126. if (*y < 1 || *y >= avctx->height ||
  127. (*y == 1 && *x == 0))
  128. return AVERROR_INVALIDDATA;
  129. if (*x == 0) {
  130. z = backstep;
  131. } else {
  132. z = 0;
  133. }
  134. r = odst[(*ly * linesize + *lx) * 4] +
  135. odst[((*y * linesize + *x) + off) * 4 + 4] -
  136. odst[((*y * linesize + *x) + off - z) * 4];
  137. g = odst[(*ly * linesize + *lx) * 4 + 1] +
  138. odst[((*y * linesize + *x) + off) * 4 + 5] -
  139. odst[((*y * linesize + *x) + off - z) * 4 + 1];
  140. b = odst[(*ly * linesize + *lx) * 4 + 2] +
  141. odst[((*y * linesize + *x) + off) * 4 + 6] -
  142. odst[((*y * linesize + *x) + off - z) * 4 + 2];
  143. clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
  144. dst[*y * linesize + *x] = clr;
  145. *lx = *x;
  146. *ly = *y;
  147. (*x)++;
  148. if (*x >= avctx->width) {
  149. *x = 0;
  150. (*y)++;
  151. }
  152. }
  153. break;
  154. case 5:
  155. while (run-- > 0) {
  156. if (*y < 1 || *y >= avctx->height ||
  157. (*y == 1 && *x == 0))
  158. return AVERROR_INVALIDDATA;
  159. if (*x == 0) {
  160. z = backstep;
  161. } else {
  162. z = 0;
  163. }
  164. clr = dst[*y * linesize + *x + off - z];
  165. dst[*y * linesize + *x] = clr;
  166. *lx = *x;
  167. *ly = *y;
  168. (*x)++;
  169. if (*x >= avctx->width) {
  170. *x = 0;
  171. (*y)++;
  172. }
  173. }
  174. break;
  175. }
  176. if (avctx->bits_per_coded_sample == 16) {
  177. *cx1 = (clr & 0x3F00) >> 2;
  178. *cx = (clr & 0x3FFFFF) >> 16;
  179. } else {
  180. *cx1 = (clr & 0xFC00) >> 4;
  181. *cx = (clr & 0xFFFFFF) >> 18;
  182. }
  183. return 0;
  184. }
  185. static int decode_run_p(AVCodecContext *avctx, uint32_t ptype, int run,
  186. int x, int y, uint32_t clr,
  187. uint32_t *dst, uint32_t *prev,
  188. int linesize, int plinesize,
  189. uint32_t *bx, uint32_t *by,
  190. uint32_t backstep, int sx1, int sx2,
  191. int *cx, int *cx1)
  192. {
  193. uint32_t r, g, b;
  194. int z;
  195. switch (ptype) {
  196. case 0:
  197. while (run-- > 0) {
  198. if (*by >= avctx->height)
  199. return AVERROR_INVALIDDATA;
  200. dst[*by * linesize + *bx] = clr;
  201. (*bx)++;
  202. if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
  203. *bx = x * 16 + sx1;
  204. (*by)++;
  205. }
  206. }
  207. break;
  208. case 1:
  209. while (run-- > 0) {
  210. if (*bx == 0) {
  211. if (*by < 1)
  212. return AVERROR_INVALIDDATA;
  213. z = backstep;
  214. } else {
  215. z = 0;
  216. }
  217. if (*by >= avctx->height)
  218. return AVERROR_INVALIDDATA;
  219. clr = dst[*by * linesize + *bx - 1 - z];
  220. dst[*by * linesize + *bx] = clr;
  221. (*bx)++;
  222. if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
  223. *bx = x * 16 + sx1;
  224. (*by)++;
  225. }
  226. }
  227. break;
  228. case 2:
  229. while (run-- > 0) {
  230. if (*by < 1 || *by >= avctx->height)
  231. return AVERROR_INVALIDDATA;
  232. clr = dst[(*by - 1) * linesize + *bx];
  233. dst[*by * linesize + *bx] = clr;
  234. (*bx)++;
  235. if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
  236. *bx = x * 16 + sx1;
  237. (*by)++;
  238. }
  239. }
  240. break;
  241. case 3:
  242. while (run-- > 0) {
  243. if (*by >= avctx->height)
  244. return AVERROR_INVALIDDATA;
  245. clr = prev[*by * plinesize + *bx];
  246. dst[*by * linesize + *bx] = clr;
  247. (*bx)++;
  248. if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
  249. *bx = x * 16 + sx1;
  250. (*by)++;
  251. }
  252. }
  253. break;
  254. case 4:
  255. while (run-- > 0) {
  256. uint8_t *odst = (uint8_t *)dst;
  257. if (*by < 1 || *by >= avctx->height)
  258. return AVERROR_INVALIDDATA;
  259. if (*bx == 0) {
  260. if (*by < 2)
  261. return AVERROR_INVALIDDATA;
  262. z = backstep;
  263. } else {
  264. z = 0;
  265. }
  266. r = odst[((*by - 1) * linesize + *bx) * 4] +
  267. odst[(*by * linesize + *bx - 1 - z) * 4] -
  268. odst[((*by - 1) * linesize + *bx - 1 - z) * 4];
  269. g = odst[((*by - 1) * linesize + *bx) * 4 + 1] +
  270. odst[(*by * linesize + *bx - 1 - z) * 4 + 1] -
  271. odst[((*by - 1) * linesize + *bx - 1 - z) * 4 + 1];
  272. b = odst[((*by - 1) * linesize + *bx) * 4 + 2] +
  273. odst[(*by * linesize + *bx - 1 - z) * 4 + 2] -
  274. odst[((*by - 1) * linesize + *bx - 1 - z) * 4 + 2];
  275. clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
  276. dst[*by * linesize + *bx] = clr;
  277. (*bx)++;
  278. if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
  279. *bx = x * 16 + sx1;
  280. (*by)++;
  281. }
  282. }
  283. break;
  284. case 5:
  285. while (run-- > 0) {
  286. if (*by < 1 || *by >= avctx->height)
  287. return AVERROR_INVALIDDATA;
  288. if (*bx == 0) {
  289. if (*by < 2)
  290. return AVERROR_INVALIDDATA;
  291. z = backstep;
  292. } else {
  293. z = 0;
  294. }
  295. clr = dst[(*by - 1) * linesize + *bx - 1 - z];
  296. dst[*by * linesize + *bx] = clr;
  297. (*bx)++;
  298. if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
  299. *bx = x * 16 + sx1;
  300. (*by)++;
  301. }
  302. }
  303. break;
  304. }
  305. if (avctx->bits_per_coded_sample == 16) {
  306. *cx1 = (clr & 0x3F00) >> 2;
  307. *cx = (clr & 0x3FFFFF) >> 16;
  308. } else {
  309. *cx1 = (clr & 0xFC00) >> 4;
  310. *cx = (clr & 0xFFFFFF) >> 18;
  311. }
  312. return 0;
  313. }
  314. #endif /* AVCODEC_SCPR_H */