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