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