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