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
32
33 #define HEADER_SIZE 27
34
35 #define MODEL2_SCALE 13
36 #define MODEL_SCALE 15
37 #define MODEL256_SEC_SCALE 9
38
44
45 typedef struct Model {
51
59
60 #define RAC_BOTTOM 0x01000000
63
67
74 };
75
80
85
91
102
108
112
120
124
125
127 {
134 }
135
137 {
139
140 if (!bit)
144 return;
145
152 }
160 }
161
163 {
164 int i, sum = 0;
166
170 return;
172
178 }
179 }
182 m->
freqs[i] = sum * scale >> 16;
184 }
185
190 }
191
193 {
194 int i;
195
197 for (i = 0; i < m->
num_syms - 1; i++)
200
206 }
207
209 {
212
214 }
215
217 {
218 int i, sum = 0;
220 int send, sidx = 1;
221
225 return;
227
230 for (i = 0; i < 256; i++) {
233 }
234 }
237 for (i = 0; i < 256; i++) {
238 m->
freqs[i] = sum * scale >> 16;
241 while (sidx <= send)
243 }
244 while (sidx < m->sec_size)
246
251 }
252
254 {
255 int i;
256
257 for (i = 0; i < 255; i++)
260
267 }
268
270 {
273
275 }
276
278 {
279 int i;
280
284 for (i = 0; i <
FFMIN(size, 4); i++)
286 c->
range = 0xFFFFFFFF;
288 }
289
291 {
292 for (;;) {
297 }
else if (!c->
low) {
300 }
302 return;
303 }
304 }
305
307 {
308 int bit;
309
311
313 if (bit)
315
318
319 return bit;
320 }
321
323 {
325
329
332
334 }
335
337 {
338 int bit, helper;
339
341 bit = (c->
low >= helper);
342 if (bit) {
345 } else {
347 }
348
351
353
354 return bit;
355 }
356
358 {
359 int prob, prob2, helper,
val;
361
362 prob = 0;
365 val = 0;
368 do {
370 if (helper <= c->low) {
372 prob = helper;
373 } else {
375 prob2 = helper;
376 }
377 end = (end2 +
val) >> 1;
378 } while (end != val);
380 c->
range = prob2 - prob;
383
385
387 }
388
390 {
391 int prob, prob2, helper,
val;
393 int ssym;
394
397
401
402 end = start = m->
secondary[ssym + 1] + 1;
403 while (end > val + 1) {
404 ssym = (end +
val) >> 1;
405 if (m->
freqs[ssym] <= helper) {
407 val = ssym;
408 } else {
409 end = (end +
val) >> 1;
410 start = ssym;
411 }
412 }
414 if (val != 255)
416
418 c->
range = prob2 - prob;
421
423
425 }
426
428 {
430
432 }
433
435 {
437
439 if (val) {
441 if (val > 1) {
442 val--;
444 }
445 if (!sign)
447 }
448
450 }
451
454 {
455 int i;
456
458
459 for (i = 0; i < block_size; i++, dst +=
stride)
460 memset(dst, fc->
fill_val, block_size);
461 }
462
465 {
466 int i, j;
467 int vec_size;
468 int vec[4];
469 int prev_line[16];
471
473 for (i = 0; i < vec_size; i++)
475 for (; i < 4; i++)
476 vec[i] = 0;
477 memset(prev_line, 0, sizeof(prev_line));
478
479 for (j = 0; j < block_size; j++) {
480 A = 0;
481 B = 0;
482 for (i = 0; i < block_size; i++) {
484 B = prev_line[i];
486
488 if (A < 4)
490 else
492 }
494 }
495 }
496
498 int bx, int by)
499 {
500 int skip,
val, sign, pos = 1, zz_pos,
dc;
502
503 memset(block, 0, sizeof(*block) * 64);
504
506 if (by) {
507 if (bx) {
509
513
515 dc += l;
516 else
518 } else {
520 }
521 } else if (bx) {
523 }
525 block[0] = dc * bc->
qmat[0];
526
527 while (pos < 64) {
529 if (!val)
530 return 0;
531 if (val == 0xF0) {
532 pos += 16;
533 continue;
534 }
535 skip = val >> 4;
536 val = val & 0xF;
537 if (!val)
538 return -1;
539 pos += skip;
540 if (pos >= 64)
541 return -1;
542
544 if (val > 1) {
545 val--;
547 }
548 if (!sign)
550
552 block[zz_pos] = val * bc->
qmat[zz_pos];
553 pos++;
554 }
555
556 return pos == 64 ? 0 : -1;
557 }
558
561 int *
block,
int mb_x,
int mb_y)
562 {
563 int i, j;
564 int bx, by;
565 int nblocks = block_size >> 3;
566
567 bx = mb_x * nblocks;
568 by = mb_y * nblocks;
569
570 for (j = 0; j < nblocks; j++) {
571 for (i = 0; i < nblocks; i++) {
572 if (
decode_dct(c, bc, block, bx + i, by + j)) {
574 return;
575 }
577 }
579 }
580 }
581
585 {
586 const int hsize = block_size >> 1;
588 int i, j;
589
590 for (j = 0; j < block_size; j++) {
591 for (i = 0; i < block_size; i++) {
592 if (i < hsize && j < hsize)
594 else
596 block[i] *= hc->
scale;
597 }
598 block += block_size;
599 }
600 block -= block_size * block_size;
601
602 for (j = 0; j < hsize; j++) {
603 for (i = 0; i < hsize; i++) {
604 A = block[i];
605 B = block[i + hsize];
606 C = block[i + hsize * block_size];
607 D = block[i + hsize * block_size + hsize];
608
613 dst[i * 2] = av_clip_uint8(t1 - t2);
614 dst[i * 2 +
stride] = av_clip_uint8(t1 + t2);
615 dst[i * 2 + 1] = av_clip_uint8(t3 - t4);
616 dst[i * 2 + 1 +
stride] = av_clip_uint8(t3 + t4);
617 }
618 block += block_size;
619 dst += stride * 2;
620 }
621 }
622
624 {
625 int i, j;
626
627 for (i = 0; i < 3; i++) {
629 for (j = 0; j < 5; j++)
636 for (j = 0; j < 125; j++)
641 }
652 }
655 }
656 }
657
659 {
660 int i, j;
661
662 for (i = 0; i < 3; i++) {
663 for (j = 0; j < 5; j++)
669 for (j = 0; j < 125; j++)
675 }
676 }
677
680 {
682 int buf_size = avpkt->
size;
687 int dec_width, dec_height, dec_x, dec_y, quality, keyframe;
688 int x,
y, i, mb_width, mb_height, blk_size, btype;
690
693 "Frame should have at least %d bytes, got %d instead\n",
696 }
697
699 keyframe = bytestream2_get_be32(&gb);
700 if (keyframe & ~0x301) {
703 }
704 keyframe = !(keyframe & 1);
706 dec_x = bytestream2_get_be16(&gb);
707 dec_y = bytestream2_get_be16(&gb);
708 dec_width = bytestream2_get_be16(&gb);
709 dec_height = bytestream2_get_be16(&gb);
710
711 if (dec_x + dec_width > avctx->
width ||
712 dec_y + dec_height > avctx->
height ||
713 (dec_width | dec_height) & 0xF) {
715 dec_width, dec_height, dec_x, dec_y);
717 }
719 quality = bytestream2_get_byte(&gb);
720 if (quality < 1 || quality > 100) {
723 }
725
729 }
731 return buf_size;
733
741 *got_frame = 1;
742
743 return buf_size;
744 }
745
747
749
750 mb_width = dec_width >> 4;
751 mb_height = dec_height >> 4;
755 for (y = 0; y < mb_height; y++) {
756 for (x = 0; x < mb_width; x++) {
757 for (i = 0; i < 3; i++) {
758 blk_size = 8 << !i;
759
761 switch (btype) {
764 dst[i] + x * blk_size,
766 break;
769 dst[i] + x * blk_size,
771 break;
774 dst[i] + x * blk_size,
777 break;
780 dst[i] + x * blk_size,
783 break;
784 }
787 x, y);
790 }
791 }
792 }
796 }
797
800
801 *got_frame = 1;
802
803 return buf_size;
804 }
805
807 {
809 int i;
810
812
813 if ((avctx->
width & 0xF) || (avctx->
height & 0xF)) {
815 "Image dimensions should be a multiple of 16.\n");
817 }
818
820 for (i = 0; i < 3; i++) {
821 int b_width = avctx->
width >> (2 + !!i);
822 int b_height = avctx->
height >> (2 + !!i);
826 b_width * b_height);
829 while (i >= 0) {
831 i--;
832 }
834 }
835 }
836
838
840
841 return 0;
842 }
843
845 {
847 int i;
848
850 for (i = 0; i < 3; i++)
852
853 return 0;
854 }
855
866 };