1 |
|
|
/* |
2 |
|
|
* Microsoft Screen 3 (aka Microsoft ATC Screen) decoder |
3 |
|
|
* Copyright (c) 2012 Konstantin Shishkov |
4 |
|
|
* |
5 |
|
|
* This file is part of FFmpeg. |
6 |
|
|
* |
7 |
|
|
* FFmpeg is free software; you can redistribute it and/or |
8 |
|
|
* modify it under the terms of the GNU Lesser General Public |
9 |
|
|
* License as published by the Free Software Foundation; either |
10 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* FFmpeg is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
|
|
* Lesser General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU Lesser General Public |
18 |
|
|
* License along with FFmpeg; if not, write to the Free Software |
19 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
/** |
23 |
|
|
* @file |
24 |
|
|
* Microsoft Screen 3 (aka Microsoft ATC Screen) decoder |
25 |
|
|
*/ |
26 |
|
|
|
27 |
|
|
#include "avcodec.h" |
28 |
|
|
#include "bytestream.h" |
29 |
|
|
#include "internal.h" |
30 |
|
|
#include "mathops.h" |
31 |
|
|
#include "mss34dsp.h" |
32 |
|
|
|
33 |
|
|
#define HEADER_SIZE 27 |
34 |
|
|
|
35 |
|
|
#define MODEL2_SCALE 13 |
36 |
|
|
#define MODEL_SCALE 15 |
37 |
|
|
#define MODEL256_SEC_SCALE 9 |
38 |
|
|
|
39 |
|
|
typedef struct Model2 { |
40 |
|
|
int upd_val, till_rescale; |
41 |
|
|
unsigned zero_freq, zero_weight; |
42 |
|
|
unsigned total_freq, total_weight; |
43 |
|
|
} Model2; |
44 |
|
|
|
45 |
|
|
typedef struct Model { |
46 |
|
|
int weights[16], freqs[16]; |
47 |
|
|
int num_syms; |
48 |
|
|
int tot_weight; |
49 |
|
|
int upd_val, max_upd_val, till_rescale; |
50 |
|
|
} Model; |
51 |
|
|
|
52 |
|
|
typedef struct Model256 { |
53 |
|
|
int weights[256], freqs[256]; |
54 |
|
|
int tot_weight; |
55 |
|
|
int secondary[68]; |
56 |
|
|
int sec_size; |
57 |
|
|
int upd_val, max_upd_val, till_rescale; |
58 |
|
|
} Model256; |
59 |
|
|
|
60 |
|
|
#define RAC_BOTTOM 0x01000000 |
61 |
|
|
typedef struct RangeCoder { |
62 |
|
|
const uint8_t *src, *src_end; |
63 |
|
|
|
64 |
|
|
uint32_t range, low; |
65 |
|
|
int got_error; |
66 |
|
|
} RangeCoder; |
67 |
|
|
|
68 |
|
|
enum BlockType { |
69 |
|
|
FILL_BLOCK = 0, |
70 |
|
|
IMAGE_BLOCK, |
71 |
|
|
DCT_BLOCK, |
72 |
|
|
HAAR_BLOCK, |
73 |
|
|
SKIP_BLOCK |
74 |
|
|
}; |
75 |
|
|
|
76 |
|
|
typedef struct BlockTypeContext { |
77 |
|
|
int last_type; |
78 |
|
|
Model bt_model[5]; |
79 |
|
|
} BlockTypeContext; |
80 |
|
|
|
81 |
|
|
typedef struct FillBlockCoder { |
82 |
|
|
int fill_val; |
83 |
|
|
Model coef_model; |
84 |
|
|
} FillBlockCoder; |
85 |
|
|
|
86 |
|
|
typedef struct ImageBlockCoder { |
87 |
|
|
Model256 esc_model, vec_entry_model; |
88 |
|
|
Model vec_size_model; |
89 |
|
|
Model vq_model[125]; |
90 |
|
|
} ImageBlockCoder; |
91 |
|
|
|
92 |
|
|
typedef struct DCTBlockCoder { |
93 |
|
|
int *prev_dc; |
94 |
|
|
ptrdiff_t prev_dc_stride; |
95 |
|
|
int prev_dc_height; |
96 |
|
|
int quality; |
97 |
|
|
uint16_t qmat[64]; |
98 |
|
|
Model dc_model; |
99 |
|
|
Model2 sign_model; |
100 |
|
|
Model256 ac_model; |
101 |
|
|
} DCTBlockCoder; |
102 |
|
|
|
103 |
|
|
typedef struct HaarBlockCoder { |
104 |
|
|
int quality, scale; |
105 |
|
|
Model256 coef_model; |
106 |
|
|
Model coef_hi_model; |
107 |
|
|
} HaarBlockCoder; |
108 |
|
|
|
109 |
|
|
typedef struct MSS3Context { |
110 |
|
|
AVCodecContext *avctx; |
111 |
|
|
AVFrame *pic; |
112 |
|
|
|
113 |
|
|
int got_error; |
114 |
|
|
RangeCoder coder; |
115 |
|
|
BlockTypeContext btype[3]; |
116 |
|
|
FillBlockCoder fill_coder[3]; |
117 |
|
|
ImageBlockCoder image_coder[3]; |
118 |
|
|
DCTBlockCoder dct_coder[3]; |
119 |
|
|
HaarBlockCoder haar_coder[3]; |
120 |
|
|
|
121 |
|
|
int dctblock[64]; |
122 |
|
|
int hblock[16 * 16]; |
123 |
|
|
} MSS3Context; |
124 |
|
|
|
125 |
|
|
|
126 |
|
|
static void model2_reset(Model2 *m) |
127 |
|
|
{ |
128 |
|
|
m->zero_weight = 1; |
129 |
|
|
m->total_weight = 2; |
130 |
|
|
m->zero_freq = 0x1000; |
131 |
|
|
m->total_freq = 0x2000; |
132 |
|
|
m->upd_val = 4; |
133 |
|
|
m->till_rescale = 4; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
static void model2_update(Model2 *m, int bit) |
137 |
|
|
{ |
138 |
|
|
unsigned scale; |
139 |
|
|
|
140 |
|
|
if (!bit) |
141 |
|
|
m->zero_weight++; |
142 |
|
|
m->till_rescale--; |
143 |
|
|
if (m->till_rescale) |
144 |
|
|
return; |
145 |
|
|
|
146 |
|
|
m->total_weight += m->upd_val; |
147 |
|
|
if (m->total_weight > 0x2000) { |
148 |
|
|
m->total_weight = (m->total_weight + 1) >> 1; |
149 |
|
|
m->zero_weight = (m->zero_weight + 1) >> 1; |
150 |
|
|
if (m->total_weight == m->zero_weight) |
151 |
|
|
m->total_weight = m->zero_weight + 1; |
152 |
|
|
} |
153 |
|
|
m->upd_val = m->upd_val * 5 >> 2; |
154 |
|
|
if (m->upd_val > 64) |
155 |
|
|
m->upd_val = 64; |
156 |
|
|
scale = 0x80000000u / m->total_weight; |
157 |
|
|
m->zero_freq = m->zero_weight * scale >> 18; |
158 |
|
|
m->total_freq = m->total_weight * scale >> 18; |
159 |
|
|
m->till_rescale = m->upd_val; |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
static void model_update(Model *m, int val) |
163 |
|
|
{ |
164 |
|
|
int i, sum = 0; |
165 |
|
|
unsigned scale; |
166 |
|
|
|
167 |
|
|
m->weights[val]++; |
168 |
|
|
m->till_rescale--; |
169 |
|
|
if (m->till_rescale) |
170 |
|
|
return; |
171 |
|
|
m->tot_weight += m->upd_val; |
172 |
|
|
|
173 |
|
|
if (m->tot_weight > 0x8000) { |
174 |
|
|
m->tot_weight = 0; |
175 |
|
|
for (i = 0; i < m->num_syms; i++) { |
176 |
|
|
m->weights[i] = (m->weights[i] + 1) >> 1; |
177 |
|
|
m->tot_weight += m->weights[i]; |
178 |
|
|
} |
179 |
|
|
} |
180 |
|
|
scale = 0x80000000u / m->tot_weight; |
181 |
|
|
for (i = 0; i < m->num_syms; i++) { |
182 |
|
|
m->freqs[i] = sum * scale >> 16; |
183 |
|
|
sum += m->weights[i]; |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
m->upd_val = m->upd_val * 5 >> 2; |
187 |
|
|
if (m->upd_val > m->max_upd_val) |
188 |
|
|
m->upd_val = m->max_upd_val; |
189 |
|
|
m->till_rescale = m->upd_val; |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
static void model_reset(Model *m) |
193 |
|
|
{ |
194 |
|
|
int i; |
195 |
|
|
|
196 |
|
|
m->tot_weight = 0; |
197 |
|
|
for (i = 0; i < m->num_syms - 1; i++) |
198 |
|
|
m->weights[i] = 1; |
199 |
|
|
m->weights[m->num_syms - 1] = 0; |
200 |
|
|
|
201 |
|
|
m->upd_val = m->num_syms; |
202 |
|
|
m->till_rescale = 1; |
203 |
|
|
model_update(m, m->num_syms - 1); |
204 |
|
|
m->till_rescale = |
205 |
|
|
m->upd_val = (m->num_syms + 6) >> 1; |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
static av_cold void model_init(Model *m, int num_syms) |
209 |
|
|
{ |
210 |
|
|
m->num_syms = num_syms; |
211 |
|
|
m->max_upd_val = 8 * num_syms + 48; |
212 |
|
|
|
213 |
|
|
model_reset(m); |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
static void model256_update(Model256 *m, int val) |
217 |
|
|
{ |
218 |
|
|
int i, sum = 0; |
219 |
|
|
unsigned scale; |
220 |
|
|
int send, sidx = 1; |
221 |
|
|
|
222 |
|
|
m->weights[val]++; |
223 |
|
|
m->till_rescale--; |
224 |
|
|
if (m->till_rescale) |
225 |
|
|
return; |
226 |
|
|
m->tot_weight += m->upd_val; |
227 |
|
|
|
228 |
|
|
if (m->tot_weight > 0x8000) { |
229 |
|
|
m->tot_weight = 0; |
230 |
|
|
for (i = 0; i < 256; i++) { |
231 |
|
|
m->weights[i] = (m->weights[i] + 1) >> 1; |
232 |
|
|
m->tot_weight += m->weights[i]; |
233 |
|
|
} |
234 |
|
|
} |
235 |
|
|
scale = 0x80000000u / m->tot_weight; |
236 |
|
|
m->secondary[0] = 0; |
237 |
|
|
for (i = 0; i < 256; i++) { |
238 |
|
|
m->freqs[i] = sum * scale >> 16; |
239 |
|
|
sum += m->weights[i]; |
240 |
|
|
send = m->freqs[i] >> MODEL256_SEC_SCALE; |
241 |
|
|
while (sidx <= send) |
242 |
|
|
m->secondary[sidx++] = i - 1; |
243 |
|
|
} |
244 |
|
|
while (sidx < m->sec_size) |
245 |
|
|
m->secondary[sidx++] = 255; |
246 |
|
|
|
247 |
|
|
m->upd_val = m->upd_val * 5 >> 2; |
248 |
|
|
if (m->upd_val > m->max_upd_val) |
249 |
|
|
m->upd_val = m->max_upd_val; |
250 |
|
|
m->till_rescale = m->upd_val; |
251 |
|
|
} |
252 |
|
|
|
253 |
|
|
static void model256_reset(Model256 *m) |
254 |
|
|
{ |
255 |
|
|
int i; |
256 |
|
|
|
257 |
|
|
for (i = 0; i < 255; i++) |
258 |
|
|
m->weights[i] = 1; |
259 |
|
|
m->weights[255] = 0; |
260 |
|
|
|
261 |
|
|
m->tot_weight = 0; |
262 |
|
|
m->upd_val = 256; |
263 |
|
|
m->till_rescale = 1; |
264 |
|
|
model256_update(m, 255); |
265 |
|
|
m->till_rescale = |
266 |
|
|
m->upd_val = (256 + 6) >> 1; |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
static av_cold void model256_init(Model256 *m) |
270 |
|
|
{ |
271 |
|
|
m->max_upd_val = 8 * 256 + 48; |
272 |
|
|
m->sec_size = (1 << 6) + 2; |
273 |
|
|
|
274 |
|
|
model256_reset(m); |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
static void rac_init(RangeCoder *c, const uint8_t *src, int size) |
278 |
|
|
{ |
279 |
|
|
int i; |
280 |
|
|
|
281 |
|
|
c->src = src; |
282 |
|
|
c->src_end = src + size; |
283 |
|
|
c->low = 0; |
284 |
|
|
for (i = 0; i < FFMIN(size, 4); i++) |
285 |
|
|
c->low = (c->low << 8) | *c->src++; |
286 |
|
|
c->range = 0xFFFFFFFF; |
287 |
|
|
c->got_error = 0; |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
static void rac_normalise(RangeCoder *c) |
291 |
|
|
{ |
292 |
|
|
for (;;) { |
293 |
|
|
c->range <<= 8; |
294 |
|
|
c->low <<= 8; |
295 |
|
|
if (c->src < c->src_end) { |
296 |
|
|
c->low |= *c->src++; |
297 |
|
|
} else if (!c->low) { |
298 |
|
|
c->got_error = 1; |
299 |
|
|
c->low = 1; |
300 |
|
|
} |
301 |
|
|
if (c->low > c->range) { |
302 |
|
|
c->got_error = 1; |
303 |
|
|
c->low = 1; |
304 |
|
|
} |
305 |
|
|
if (c->range >= RAC_BOTTOM) |
306 |
|
|
return; |
307 |
|
|
} |
308 |
|
|
} |
309 |
|
|
|
310 |
|
|
static int rac_get_bit(RangeCoder *c) |
311 |
|
|
{ |
312 |
|
|
int bit; |
313 |
|
|
|
314 |
|
|
c->range >>= 1; |
315 |
|
|
|
316 |
|
|
bit = (c->range <= c->low); |
317 |
|
|
if (bit) |
318 |
|
|
c->low -= c->range; |
319 |
|
|
|
320 |
|
|
if (c->range < RAC_BOTTOM) |
321 |
|
|
rac_normalise(c); |
322 |
|
|
|
323 |
|
|
return bit; |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
static int rac_get_bits(RangeCoder *c, int nbits) |
327 |
|
|
{ |
328 |
|
|
int val; |
329 |
|
|
|
330 |
|
|
c->range >>= nbits; |
331 |
|
|
val = c->low / c->range; |
332 |
|
|
c->low -= c->range * val; |
333 |
|
|
|
334 |
|
|
if (c->range < RAC_BOTTOM) |
335 |
|
|
rac_normalise(c); |
336 |
|
|
|
337 |
|
|
return val; |
338 |
|
|
} |
339 |
|
|
|
340 |
|
|
static int rac_get_model2_sym(RangeCoder *c, Model2 *m) |
341 |
|
|
{ |
342 |
|
|
int bit, helper; |
343 |
|
|
|
344 |
|
|
helper = m->zero_freq * (c->range >> MODEL2_SCALE); |
345 |
|
|
bit = (c->low >= helper); |
346 |
|
|
if (bit) { |
347 |
|
|
c->low -= helper; |
348 |
|
|
c->range -= helper; |
349 |
|
|
} else { |
350 |
|
|
c->range = helper; |
351 |
|
|
} |
352 |
|
|
|
353 |
|
|
if (c->range < RAC_BOTTOM) |
354 |
|
|
rac_normalise(c); |
355 |
|
|
|
356 |
|
|
model2_update(m, bit); |
357 |
|
|
|
358 |
|
|
return bit; |
359 |
|
|
} |
360 |
|
|
|
361 |
|
|
static int rac_get_model_sym(RangeCoder *c, Model *m) |
362 |
|
|
{ |
363 |
|
|
int val; |
364 |
|
|
int end, end2; |
365 |
|
|
unsigned prob, prob2, helper; |
366 |
|
|
|
367 |
|
|
prob = 0; |
368 |
|
|
prob2 = c->range; |
369 |
|
|
c->range >>= MODEL_SCALE; |
370 |
|
|
val = 0; |
371 |
|
|
end = m->num_syms >> 1; |
372 |
|
|
end2 = m->num_syms; |
373 |
|
|
do { |
374 |
|
|
helper = m->freqs[end] * c->range; |
375 |
|
|
if (helper <= c->low) { |
376 |
|
|
val = end; |
377 |
|
|
prob = helper; |
378 |
|
|
} else { |
379 |
|
|
end2 = end; |
380 |
|
|
prob2 = helper; |
381 |
|
|
} |
382 |
|
|
end = (end2 + val) >> 1; |
383 |
|
|
} while (end != val); |
384 |
|
|
c->low -= prob; |
385 |
|
|
c->range = prob2 - prob; |
386 |
|
|
if (c->range < RAC_BOTTOM) |
387 |
|
|
rac_normalise(c); |
388 |
|
|
|
389 |
|
|
model_update(m, val); |
390 |
|
|
|
391 |
|
|
return val; |
392 |
|
|
} |
393 |
|
|
|
394 |
|
|
static int rac_get_model256_sym(RangeCoder *c, Model256 *m) |
395 |
|
|
{ |
396 |
|
|
int val; |
397 |
|
|
int start, end; |
398 |
|
|
int ssym; |
399 |
|
|
unsigned prob, prob2, helper; |
400 |
|
|
|
401 |
|
|
prob2 = c->range; |
402 |
|
|
c->range >>= MODEL_SCALE; |
403 |
|
|
|
404 |
|
|
helper = c->low / c->range; |
405 |
|
|
ssym = helper >> MODEL256_SEC_SCALE; |
406 |
|
|
val = m->secondary[ssym]; |
407 |
|
|
|
408 |
|
|
end = start = m->secondary[ssym + 1] + 1; |
409 |
|
|
while (end > val + 1) { |
410 |
|
|
ssym = (end + val) >> 1; |
411 |
|
|
if (m->freqs[ssym] <= helper) { |
412 |
|
|
end = start; |
413 |
|
|
val = ssym; |
414 |
|
|
} else { |
415 |
|
|
end = (end + val) >> 1; |
416 |
|
|
start = ssym; |
417 |
|
|
} |
418 |
|
|
} |
419 |
|
|
prob = m->freqs[val] * c->range; |
420 |
|
|
if (val != 255) |
421 |
|
|
prob2 = m->freqs[val + 1] * c->range; |
422 |
|
|
|
423 |
|
|
c->low -= prob; |
424 |
|
|
c->range = prob2 - prob; |
425 |
|
|
if (c->range < RAC_BOTTOM) |
426 |
|
|
rac_normalise(c); |
427 |
|
|
|
428 |
|
|
model256_update(m, val); |
429 |
|
|
|
430 |
|
|
return val; |
431 |
|
|
} |
432 |
|
|
|
433 |
|
|
static int decode_block_type(RangeCoder *c, BlockTypeContext *bt) |
434 |
|
|
{ |
435 |
|
|
bt->last_type = rac_get_model_sym(c, &bt->bt_model[bt->last_type]); |
436 |
|
|
|
437 |
|
|
return bt->last_type; |
438 |
|
|
} |
439 |
|
|
|
440 |
|
|
static int decode_coeff(RangeCoder *c, Model *m) |
441 |
|
|
{ |
442 |
|
|
int val, sign; |
443 |
|
|
|
444 |
|
|
val = rac_get_model_sym(c, m); |
445 |
|
|
if (val) { |
446 |
|
|
sign = rac_get_bit(c); |
447 |
|
|
if (val > 1) { |
448 |
|
|
val--; |
449 |
|
|
val = (1 << val) + rac_get_bits(c, val); |
450 |
|
|
} |
451 |
|
|
if (!sign) |
452 |
|
|
val = -val; |
453 |
|
|
} |
454 |
|
|
|
455 |
|
|
return val; |
456 |
|
|
} |
457 |
|
|
|
458 |
|
|
static void decode_fill_block(RangeCoder *c, FillBlockCoder *fc, |
459 |
|
|
uint8_t *dst, ptrdiff_t stride, int block_size) |
460 |
|
|
{ |
461 |
|
|
int i; |
462 |
|
|
|
463 |
|
|
fc->fill_val += decode_coeff(c, &fc->coef_model); |
464 |
|
|
|
465 |
|
|
for (i = 0; i < block_size; i++, dst += stride) |
466 |
|
|
memset(dst, fc->fill_val, block_size); |
467 |
|
|
} |
468 |
|
|
|
469 |
|
|
static void decode_image_block(RangeCoder *c, ImageBlockCoder *ic, |
470 |
|
|
uint8_t *dst, ptrdiff_t stride, int block_size) |
471 |
|
|
{ |
472 |
|
|
int i, j; |
473 |
|
|
int vec_size; |
474 |
|
|
int vec[4]; |
475 |
|
|
int prev_line[16]; |
476 |
|
|
int A, B, C; |
477 |
|
|
|
478 |
|
|
vec_size = rac_get_model_sym(c, &ic->vec_size_model) + 2; |
479 |
|
|
for (i = 0; i < vec_size; i++) |
480 |
|
|
vec[i] = rac_get_model256_sym(c, &ic->vec_entry_model); |
481 |
|
|
for (; i < 4; i++) |
482 |
|
|
vec[i] = 0; |
483 |
|
|
memset(prev_line, 0, sizeof(prev_line)); |
484 |
|
|
|
485 |
|
|
for (j = 0; j < block_size; j++) { |
486 |
|
|
A = 0; |
487 |
|
|
B = 0; |
488 |
|
|
for (i = 0; i < block_size; i++) { |
489 |
|
|
C = B; |
490 |
|
|
B = prev_line[i]; |
491 |
|
|
A = rac_get_model_sym(c, &ic->vq_model[A + B * 5 + C * 25]); |
492 |
|
|
|
493 |
|
|
prev_line[i] = A; |
494 |
|
|
if (A < 4) |
495 |
|
|
dst[i] = vec[A]; |
496 |
|
|
else |
497 |
|
|
dst[i] = rac_get_model256_sym(c, &ic->esc_model); |
498 |
|
|
} |
499 |
|
|
dst += stride; |
500 |
|
|
} |
501 |
|
|
} |
502 |
|
|
|
503 |
|
|
static int decode_dct(RangeCoder *c, DCTBlockCoder *bc, int *block, |
504 |
|
|
int bx, int by) |
505 |
|
|
{ |
506 |
|
|
int skip, val, sign, pos = 1, zz_pos, dc; |
507 |
|
|
int blk_pos = bx + by * bc->prev_dc_stride; |
508 |
|
|
|
509 |
|
|
memset(block, 0, sizeof(*block) * 64); |
510 |
|
|
|
511 |
|
|
dc = decode_coeff(c, &bc->dc_model); |
512 |
|
|
if (by) { |
513 |
|
|
if (bx) { |
514 |
|
|
int l, tl, t; |
515 |
|
|
|
516 |
|
|
l = bc->prev_dc[blk_pos - 1]; |
517 |
|
|
tl = bc->prev_dc[blk_pos - 1 - bc->prev_dc_stride]; |
518 |
|
|
t = bc->prev_dc[blk_pos - bc->prev_dc_stride]; |
519 |
|
|
|
520 |
|
|
if (FFABS(t - tl) <= FFABS(l - tl)) |
521 |
|
|
dc += l; |
522 |
|
|
else |
523 |
|
|
dc += t; |
524 |
|
|
} else { |
525 |
|
|
dc += bc->prev_dc[blk_pos - bc->prev_dc_stride]; |
526 |
|
|
} |
527 |
|
|
} else if (bx) { |
528 |
|
|
dc += bc->prev_dc[bx - 1]; |
529 |
|
|
} |
530 |
|
|
bc->prev_dc[blk_pos] = dc; |
531 |
|
|
block[0] = dc * bc->qmat[0]; |
532 |
|
|
|
533 |
|
|
while (pos < 64) { |
534 |
|
|
val = rac_get_model256_sym(c, &bc->ac_model); |
535 |
|
|
if (!val) |
536 |
|
|
return 0; |
537 |
|
|
if (val == 0xF0) { |
538 |
|
|
pos += 16; |
539 |
|
|
continue; |
540 |
|
|
} |
541 |
|
|
skip = val >> 4; |
542 |
|
|
val = val & 0xF; |
543 |
|
|
if (!val) |
544 |
|
|
return -1; |
545 |
|
|
pos += skip; |
546 |
|
|
if (pos >= 64) |
547 |
|
|
return -1; |
548 |
|
|
|
549 |
|
|
sign = rac_get_model2_sym(c, &bc->sign_model); |
550 |
|
|
if (val > 1) { |
551 |
|
|
val--; |
552 |
|
|
val = (1 << val) + rac_get_bits(c, val); |
553 |
|
|
} |
554 |
|
|
if (!sign) |
555 |
|
|
val = -val; |
556 |
|
|
|
557 |
|
|
zz_pos = ff_zigzag_direct[pos]; |
558 |
|
|
block[zz_pos] = val * bc->qmat[zz_pos]; |
559 |
|
|
pos++; |
560 |
|
|
} |
561 |
|
|
|
562 |
|
|
return pos == 64 ? 0 : -1; |
563 |
|
|
} |
564 |
|
|
|
565 |
|
|
static void decode_dct_block(RangeCoder *c, DCTBlockCoder *bc, |
566 |
|
|
uint8_t *dst, ptrdiff_t stride, int block_size, |
567 |
|
|
int *block, int mb_x, int mb_y) |
568 |
|
|
{ |
569 |
|
|
int i, j; |
570 |
|
|
int bx, by; |
571 |
|
|
int nblocks = block_size >> 3; |
572 |
|
|
|
573 |
|
|
bx = mb_x * nblocks; |
574 |
|
|
by = mb_y * nblocks; |
575 |
|
|
|
576 |
|
|
for (j = 0; j < nblocks; j++) { |
577 |
|
|
for (i = 0; i < nblocks; i++) { |
578 |
|
|
if (decode_dct(c, bc, block, bx + i, by + j)) { |
579 |
|
|
c->got_error = 1; |
580 |
|
|
return; |
581 |
|
|
} |
582 |
|
|
ff_mss34_dct_put(dst + i * 8, stride, block); |
583 |
|
|
} |
584 |
|
|
dst += 8 * stride; |
585 |
|
|
} |
586 |
|
|
} |
587 |
|
|
|
588 |
|
|
static void decode_haar_block(RangeCoder *c, HaarBlockCoder *hc, |
589 |
|
|
uint8_t *dst, ptrdiff_t stride, |
590 |
|
|
int block_size, int *block) |
591 |
|
|
{ |
592 |
|
|
const int hsize = block_size >> 1; |
593 |
|
|
int A, B, C, D, t1, t2, t3, t4; |
594 |
|
|
int i, j; |
595 |
|
|
|
596 |
|
|
for (j = 0; j < block_size; j++) { |
597 |
|
|
for (i = 0; i < block_size; i++) { |
598 |
|
|
if (i < hsize && j < hsize) |
599 |
|
|
block[i] = rac_get_model256_sym(c, &hc->coef_model); |
600 |
|
|
else |
601 |
|
|
block[i] = decode_coeff(c, &hc->coef_hi_model); |
602 |
|
|
block[i] *= hc->scale; |
603 |
|
|
} |
604 |
|
|
block += block_size; |
605 |
|
|
} |
606 |
|
|
block -= block_size * block_size; |
607 |
|
|
|
608 |
|
|
for (j = 0; j < hsize; j++) { |
609 |
|
|
for (i = 0; i < hsize; i++) { |
610 |
|
|
A = block[i]; |
611 |
|
|
B = block[i + hsize]; |
612 |
|
|
C = block[i + hsize * block_size]; |
613 |
|
|
D = block[i + hsize * block_size + hsize]; |
614 |
|
|
|
615 |
|
|
t1 = A - B; |
616 |
|
|
t2 = C - D; |
617 |
|
|
t3 = A + B; |
618 |
|
|
t4 = C + D; |
619 |
|
|
dst[i * 2] = av_clip_uint8(t1 - t2); |
620 |
|
|
dst[i * 2 + stride] = av_clip_uint8(t1 + t2); |
621 |
|
|
dst[i * 2 + 1] = av_clip_uint8(t3 - t4); |
622 |
|
|
dst[i * 2 + 1 + stride] = av_clip_uint8(t3 + t4); |
623 |
|
|
} |
624 |
|
|
block += block_size; |
625 |
|
|
dst += stride * 2; |
626 |
|
|
} |
627 |
|
|
} |
628 |
|
|
|
629 |
|
|
static void reset_coders(MSS3Context *ctx, int quality) |
630 |
|
|
{ |
631 |
|
|
int i, j; |
632 |
|
|
|
633 |
|
|
for (i = 0; i < 3; i++) { |
634 |
|
|
ctx->btype[i].last_type = SKIP_BLOCK; |
635 |
|
|
for (j = 0; j < 5; j++) |
636 |
|
|
model_reset(&ctx->btype[i].bt_model[j]); |
637 |
|
|
ctx->fill_coder[i].fill_val = 0; |
638 |
|
|
model_reset(&ctx->fill_coder[i].coef_model); |
639 |
|
|
model256_reset(&ctx->image_coder[i].esc_model); |
640 |
|
|
model256_reset(&ctx->image_coder[i].vec_entry_model); |
641 |
|
|
model_reset(&ctx->image_coder[i].vec_size_model); |
642 |
|
|
for (j = 0; j < 125; j++) |
643 |
|
|
model_reset(&ctx->image_coder[i].vq_model[j]); |
644 |
|
|
if (ctx->dct_coder[i].quality != quality) { |
645 |
|
|
ctx->dct_coder[i].quality = quality; |
646 |
|
|
ff_mss34_gen_quant_mat(ctx->dct_coder[i].qmat, quality, !i); |
647 |
|
|
} |
648 |
|
|
memset(ctx->dct_coder[i].prev_dc, 0, |
649 |
|
|
sizeof(*ctx->dct_coder[i].prev_dc) * |
650 |
|
|
ctx->dct_coder[i].prev_dc_stride * |
651 |
|
|
ctx->dct_coder[i].prev_dc_height); |
652 |
|
|
model_reset(&ctx->dct_coder[i].dc_model); |
653 |
|
|
model2_reset(&ctx->dct_coder[i].sign_model); |
654 |
|
|
model256_reset(&ctx->dct_coder[i].ac_model); |
655 |
|
|
if (ctx->haar_coder[i].quality != quality) { |
656 |
|
|
ctx->haar_coder[i].quality = quality; |
657 |
|
|
ctx->haar_coder[i].scale = 17 - 7 * quality / 50; |
658 |
|
|
} |
659 |
|
|
model_reset(&ctx->haar_coder[i].coef_hi_model); |
660 |
|
|
model256_reset(&ctx->haar_coder[i].coef_model); |
661 |
|
|
} |
662 |
|
|
} |
663 |
|
|
|
664 |
|
|
static av_cold void init_coders(MSS3Context *ctx) |
665 |
|
|
{ |
666 |
|
|
int i, j; |
667 |
|
|
|
668 |
|
|
for (i = 0; i < 3; i++) { |
669 |
|
|
for (j = 0; j < 5; j++) |
670 |
|
|
model_init(&ctx->btype[i].bt_model[j], 5); |
671 |
|
|
model_init(&ctx->fill_coder[i].coef_model, 12); |
672 |
|
|
model256_init(&ctx->image_coder[i].esc_model); |
673 |
|
|
model256_init(&ctx->image_coder[i].vec_entry_model); |
674 |
|
|
model_init(&ctx->image_coder[i].vec_size_model, 3); |
675 |
|
|
for (j = 0; j < 125; j++) |
676 |
|
|
model_init(&ctx->image_coder[i].vq_model[j], 5); |
677 |
|
|
model_init(&ctx->dct_coder[i].dc_model, 12); |
678 |
|
|
model256_init(&ctx->dct_coder[i].ac_model); |
679 |
|
|
model_init(&ctx->haar_coder[i].coef_hi_model, 12); |
680 |
|
|
model256_init(&ctx->haar_coder[i].coef_model); |
681 |
|
|
} |
682 |
|
|
} |
683 |
|
|
|
684 |
|
|
static int mss3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, |
685 |
|
|
AVPacket *avpkt) |
686 |
|
|
{ |
687 |
|
|
const uint8_t *buf = avpkt->data; |
688 |
|
|
int buf_size = avpkt->size; |
689 |
|
|
MSS3Context *c = avctx->priv_data; |
690 |
|
|
RangeCoder *acoder = &c->coder; |
691 |
|
|
GetByteContext gb; |
692 |
|
|
uint8_t *dst[3]; |
693 |
|
|
int dec_width, dec_height, dec_x, dec_y, quality, keyframe; |
694 |
|
|
int x, y, i, mb_width, mb_height, blk_size, btype; |
695 |
|
|
int ret; |
696 |
|
|
|
697 |
|
|
if (buf_size < HEADER_SIZE) { |
698 |
|
|
av_log(avctx, AV_LOG_ERROR, |
699 |
|
|
"Frame should have at least %d bytes, got %d instead\n", |
700 |
|
|
HEADER_SIZE, buf_size); |
701 |
|
|
return AVERROR_INVALIDDATA; |
702 |
|
|
} |
703 |
|
|
|
704 |
|
|
bytestream2_init(&gb, buf, buf_size); |
705 |
|
|
keyframe = bytestream2_get_be32(&gb); |
706 |
|
|
if (keyframe & ~0x301) { |
707 |
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid frame type %X\n", keyframe); |
708 |
|
|
return AVERROR_INVALIDDATA; |
709 |
|
|
} |
710 |
|
|
keyframe = !(keyframe & 1); |
711 |
|
|
bytestream2_skip(&gb, 6); |
712 |
|
|
dec_x = bytestream2_get_be16(&gb); |
713 |
|
|
dec_y = bytestream2_get_be16(&gb); |
714 |
|
|
dec_width = bytestream2_get_be16(&gb); |
715 |
|
|
dec_height = bytestream2_get_be16(&gb); |
716 |
|
|
|
717 |
|
|
if (dec_x + dec_width > avctx->width || |
718 |
|
|
dec_y + dec_height > avctx->height || |
719 |
|
|
(dec_width | dec_height) & 0xF) { |
720 |
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d +%d,%d\n", |
721 |
|
|
dec_width, dec_height, dec_x, dec_y); |
722 |
|
|
return AVERROR_INVALIDDATA; |
723 |
|
|
} |
724 |
|
|
bytestream2_skip(&gb, 4); |
725 |
|
|
quality = bytestream2_get_byte(&gb); |
726 |
|
|
if (quality < 1 || quality > 100) { |
727 |
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid quality setting %d\n", quality); |
728 |
|
|
return AVERROR_INVALIDDATA; |
729 |
|
|
} |
730 |
|
|
bytestream2_skip(&gb, 4); |
731 |
|
|
|
732 |
|
|
if (keyframe && !bytestream2_get_bytes_left(&gb)) { |
733 |
|
|
av_log(avctx, AV_LOG_ERROR, "Keyframe without data found\n"); |
734 |
|
|
return AVERROR_INVALIDDATA; |
735 |
|
|
} |
736 |
|
|
if (!keyframe && c->got_error) |
737 |
|
|
return buf_size; |
738 |
|
|
c->got_error = 0; |
739 |
|
|
|
740 |
|
|
if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0) |
741 |
|
|
return ret; |
742 |
|
|
c->pic->key_frame = keyframe; |
743 |
|
|
c->pic->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P; |
744 |
|
|
if (!bytestream2_get_bytes_left(&gb)) { |
745 |
|
|
if ((ret = av_frame_ref(data, c->pic)) < 0) |
746 |
|
|
return ret; |
747 |
|
|
*got_frame = 1; |
748 |
|
|
|
749 |
|
|
return buf_size; |
750 |
|
|
} |
751 |
|
|
|
752 |
|
|
reset_coders(c, quality); |
753 |
|
|
|
754 |
|
|
rac_init(acoder, buf + HEADER_SIZE, buf_size - HEADER_SIZE); |
755 |
|
|
|
756 |
|
|
mb_width = dec_width >> 4; |
757 |
|
|
mb_height = dec_height >> 4; |
758 |
|
|
dst[0] = c->pic->data[0] + dec_x + dec_y * c->pic->linesize[0]; |
759 |
|
|
dst[1] = c->pic->data[1] + dec_x / 2 + (dec_y / 2) * c->pic->linesize[1]; |
760 |
|
|
dst[2] = c->pic->data[2] + dec_x / 2 + (dec_y / 2) * c->pic->linesize[2]; |
761 |
|
|
for (y = 0; y < mb_height; y++) { |
762 |
|
|
for (x = 0; x < mb_width; x++) { |
763 |
|
|
for (i = 0; i < 3; i++) { |
764 |
|
|
blk_size = 8 << !i; |
765 |
|
|
|
766 |
|
|
btype = decode_block_type(acoder, c->btype + i); |
767 |
|
|
switch (btype) { |
768 |
|
|
case FILL_BLOCK: |
769 |
|
|
decode_fill_block(acoder, c->fill_coder + i, |
770 |
|
|
dst[i] + x * blk_size, |
771 |
|
|
c->pic->linesize[i], blk_size); |
772 |
|
|
break; |
773 |
|
|
case IMAGE_BLOCK: |
774 |
|
|
decode_image_block(acoder, c->image_coder + i, |
775 |
|
|
dst[i] + x * blk_size, |
776 |
|
|
c->pic->linesize[i], blk_size); |
777 |
|
|
break; |
778 |
|
|
case DCT_BLOCK: |
779 |
|
|
decode_dct_block(acoder, c->dct_coder + i, |
780 |
|
|
dst[i] + x * blk_size, |
781 |
|
|
c->pic->linesize[i], blk_size, |
782 |
|
|
c->dctblock, x, y); |
783 |
|
|
break; |
784 |
|
|
case HAAR_BLOCK: |
785 |
|
|
decode_haar_block(acoder, c->haar_coder + i, |
786 |
|
|
dst[i] + x * blk_size, |
787 |
|
|
c->pic->linesize[i], blk_size, |
788 |
|
|
c->hblock); |
789 |
|
|
break; |
790 |
|
|
} |
791 |
|
|
if (c->got_error || acoder->got_error) { |
792 |
|
|
av_log(avctx, AV_LOG_ERROR, "Error decoding block %d,%d\n", |
793 |
|
|
x, y); |
794 |
|
|
c->got_error = 1; |
795 |
|
|
return AVERROR_INVALIDDATA; |
796 |
|
|
} |
797 |
|
|
} |
798 |
|
|
} |
799 |
|
|
dst[0] += c->pic->linesize[0] * 16; |
800 |
|
|
dst[1] += c->pic->linesize[1] * 8; |
801 |
|
|
dst[2] += c->pic->linesize[2] * 8; |
802 |
|
|
} |
803 |
|
|
|
804 |
|
|
if ((ret = av_frame_ref(data, c->pic)) < 0) |
805 |
|
|
return ret; |
806 |
|
|
|
807 |
|
|
*got_frame = 1; |
808 |
|
|
|
809 |
|
|
return buf_size; |
810 |
|
|
} |
811 |
|
|
|
812 |
|
|
static av_cold int mss3_decode_end(AVCodecContext *avctx) |
813 |
|
|
{ |
814 |
|
|
MSS3Context * const c = avctx->priv_data; |
815 |
|
|
int i; |
816 |
|
|
|
817 |
|
|
av_frame_free(&c->pic); |
818 |
|
|
for (i = 0; i < 3; i++) |
819 |
|
|
av_freep(&c->dct_coder[i].prev_dc); |
820 |
|
|
|
821 |
|
|
return 0; |
822 |
|
|
} |
823 |
|
|
|
824 |
|
|
static av_cold int mss3_decode_init(AVCodecContext *avctx) |
825 |
|
|
{ |
826 |
|
|
MSS3Context * const c = avctx->priv_data; |
827 |
|
|
int i; |
828 |
|
|
|
829 |
|
|
c->avctx = avctx; |
830 |
|
|
|
831 |
|
|
if ((avctx->width & 0xF) || (avctx->height & 0xF)) { |
832 |
|
|
av_log(avctx, AV_LOG_ERROR, |
833 |
|
|
"Image dimensions should be a multiple of 16.\n"); |
834 |
|
|
return AVERROR_INVALIDDATA; |
835 |
|
|
} |
836 |
|
|
|
837 |
|
|
c->got_error = 0; |
838 |
|
|
for (i = 0; i < 3; i++) { |
839 |
|
|
int b_width = avctx->width >> (2 + !!i); |
840 |
|
|
int b_height = avctx->height >> (2 + !!i); |
841 |
|
|
c->dct_coder[i].prev_dc_stride = b_width; |
842 |
|
|
c->dct_coder[i].prev_dc_height = b_height; |
843 |
|
|
c->dct_coder[i].prev_dc = av_malloc(sizeof(*c->dct_coder[i].prev_dc) * |
844 |
|
|
b_width * b_height); |
845 |
|
|
if (!c->dct_coder[i].prev_dc) { |
846 |
|
|
av_log(avctx, AV_LOG_ERROR, "Cannot allocate buffer\n"); |
847 |
|
|
return AVERROR(ENOMEM); |
848 |
|
|
} |
849 |
|
|
} |
850 |
|
|
|
851 |
|
|
c->pic = av_frame_alloc(); |
852 |
|
|
if (!c->pic) |
853 |
|
|
return AVERROR(ENOMEM); |
854 |
|
|
|
855 |
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
856 |
|
|
|
857 |
|
|
init_coders(c); |
858 |
|
|
|
859 |
|
|
return 0; |
860 |
|
|
} |
861 |
|
|
|
862 |
|
|
AVCodec ff_msa1_decoder = { |
863 |
|
|
.name = "msa1", |
864 |
|
|
.long_name = NULL_IF_CONFIG_SMALL("MS ATC Screen"), |
865 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
866 |
|
|
.id = AV_CODEC_ID_MSA1, |
867 |
|
|
.priv_data_size = sizeof(MSS3Context), |
868 |
|
|
.init = mss3_decode_init, |
869 |
|
|
.close = mss3_decode_end, |
870 |
|
|
.decode = mss3_decode_frame, |
871 |
|
|
.capabilities = AV_CODEC_CAP_DR1, |
872 |
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
873 |
|
|
}; |