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