2 * Microsoft Screen 3 (aka Microsoft ATC Screen) decoder
3 * Copyright (c) 2012 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
24 * Microsoft Screen 3 (aka Microsoft ATC Screen) decoder
27 #include "libavutil/mem.h"
29 #include "bytestream.h"
30 #include "codec_internal.h"
35 #define HEADER_SIZE 27
37 #define MODEL2_SCALE 13
38 #define MODEL_SCALE 15
39 #define MODEL256_SEC_SCALE 9
41 typedef struct Model2
{
42 int upd_val
, till_rescale
;
43 unsigned zero_freq
, zero_weight
;
44 unsigned total_freq
, total_weight
;
47 typedef struct Model
{
48 int weights
[16], freqs
[16];
51 int upd_val
, max_upd_val
, till_rescale
;
54 typedef struct Model256
{
55 int weights
[256], freqs
[256];
59 int upd_val
, max_upd_val
, till_rescale
;
62 #define RAC_BOTTOM 0x01000000
63 typedef struct RangeCoder
{
64 const uint8_t *src
, *src_end
;
78 typedef struct BlockTypeContext
{
83 typedef struct FillBlockCoder
{
88 typedef struct ImageBlockCoder
{
89 Model256 esc_model
, vec_entry_model
;
94 typedef struct DCTBlockCoder
{
96 ptrdiff_t prev_dc_stride
;
105 typedef struct HaarBlockCoder
{
111 typedef struct MSS3Context
{
112 AVCodecContext
*avctx
;
117 BlockTypeContext btype
[3];
118 FillBlockCoder fill_coder
[3];
119 ImageBlockCoder image_coder
[3];
120 DCTBlockCoder dct_coder
[3];
121 HaarBlockCoder haar_coder
[3];
128 static void model2_reset(Model2
*m
)
132 m
->zero_freq
= 0x1000;
133 m
->total_freq
= 0x2000;
138 static void model2_update(Model2
*m
, int bit
)
148 m
->total_weight
+= m
->upd_val
;
149 if (m
->total_weight
> 0x2000) {
150 m
->total_weight
= (m
->total_weight
+ 1) >> 1;
151 m
->zero_weight
= (m
->zero_weight
+ 1) >> 1;
152 if (m
->total_weight
== m
->zero_weight
)
153 m
->total_weight
= m
->zero_weight
+ 1;
155 m
->upd_val
= m
->upd_val
* 5 >> 2;
158 scale
= 0x80000000u
/ m
->total_weight
;
159 m
->zero_freq
= m
->zero_weight
* scale
>> 18;
160 m
->total_freq
= m
->total_weight
* scale
>> 18;
161 m
->till_rescale
= m
->upd_val
;
164 static void model_update(Model
*m
, int val
)
173 m
->tot_weight
+= m
->upd_val
;
175 if (m
->tot_weight
> 0x8000) {
177 for (i
= 0; i
< m
->num_syms
; i
++) {
178 m
->weights
[i
] = (m
->weights
[i
] + 1) >> 1;
179 m
->tot_weight
+= m
->weights
[i
];
182 scale
= 0x80000000u
/ m
->tot_weight
;
183 for (i
= 0; i
< m
->num_syms
; i
++) {
184 m
->freqs
[i
] = sum
* scale
>> 16;
185 sum
+= m
->weights
[i
];
188 m
->upd_val
= m
->upd_val
* 5 >> 2;
189 if (m
->upd_val
> m
->max_upd_val
)
190 m
->upd_val
= m
->max_upd_val
;
191 m
->till_rescale
= m
->upd_val
;
194 static void model_reset(Model
*m
)
199 for (i
= 0; i
< m
->num_syms
- 1; i
++)
201 m
->weights
[m
->num_syms
- 1] = 0;
203 m
->upd_val
= m
->num_syms
;
205 model_update(m
, m
->num_syms
- 1);
207 m
->upd_val
= (m
->num_syms
+ 6) >> 1;
210 static av_cold
void model_init(Model
*m
, int num_syms
)
212 m
->num_syms
= num_syms
;
213 m
->max_upd_val
= 8 * num_syms
+ 48;
218 static void model256_update(Model256
*m
, int val
)
228 m
->tot_weight
+= m
->upd_val
;
230 if (m
->tot_weight
> 0x8000) {
232 for (i
= 0; i
< 256; i
++) {
233 m
->weights
[i
] = (m
->weights
[i
] + 1) >> 1;
234 m
->tot_weight
+= m
->weights
[i
];
237 scale
= 0x80000000u
/ m
->tot_weight
;
239 for (i
= 0; i
< 256; i
++) {
240 m
->freqs
[i
] = sum
* scale
>> 16;
241 sum
+= m
->weights
[i
];
242 send
= m
->freqs
[i
] >> MODEL256_SEC_SCALE
;
244 m
->secondary
[sidx
++] = i
- 1;
246 while (sidx
< m
->sec_size
)
247 m
->secondary
[sidx
++] = 255;
249 m
->upd_val
= m
->upd_val
* 5 >> 2;
250 if (m
->upd_val
> m
->max_upd_val
)
251 m
->upd_val
= m
->max_upd_val
;
252 m
->till_rescale
= m
->upd_val
;
255 static void model256_reset(Model256
*m
)
259 for (i
= 0; i
< 255; i
++)
266 model256_update(m
, 255);
268 m
->upd_val
= (256 + 6) >> 1;
271 static av_cold
void model256_init(Model256
*m
)
273 m
->max_upd_val
= 8 * 256 + 48;
274 m
->sec_size
= (1 << 6) + 2;
279 static void rac_init(RangeCoder
*c
, const uint8_t *src
, int size
)
284 c
->src_end
= src
+ size
;
286 for (i
= 0; i
< FFMIN(size
, 4); i
++)
287 c
->low
= (c
->low
<< 8) | *c
->src
++;
288 c
->range
= 0xFFFFFFFF;
292 static void rac_normalise(RangeCoder
*c
)
297 if (c
->src
< c
->src_end
) {
299 } else if (!c
->low
) {
303 if (c
->low
> c
->range
) {
307 if (c
->range
>= RAC_BOTTOM
)
312 static int rac_get_bit(RangeCoder
*c
)
318 bit
= (c
->range
<= c
->low
);
322 if (c
->range
< RAC_BOTTOM
)
328 static int rac_get_bits(RangeCoder
*c
, int nbits
)
333 val
= c
->low
/ c
->range
;
334 c
->low
-= c
->range
* val
;
336 if (c
->range
< RAC_BOTTOM
)
342 static int rac_get_model2_sym(RangeCoder
*c
, Model2
*m
)
346 helper
= m
->zero_freq
* (c
->range
>> MODEL2_SCALE
);
347 bit
= (c
->low
>= helper
);
355 if (c
->range
< RAC_BOTTOM
)
358 model2_update(m
, bit
);
363 static int rac_get_model_sym(RangeCoder
*c
, Model
*m
)
367 unsigned prob
, prob2
, helper
;
371 c
->range
>>= MODEL_SCALE
;
373 end
= m
->num_syms
>> 1;
376 helper
= m
->freqs
[end
] * c
->range
;
377 if (helper
<= c
->low
) {
384 end
= (end2
+ val
) >> 1;
385 } while (end
!= val
);
387 c
->range
= prob2
- prob
;
388 if (c
->range
< RAC_BOTTOM
)
391 model_update(m
, val
);
396 static int rac_get_model256_sym(RangeCoder
*c
, Model256
*m
)
401 unsigned prob
, prob2
, helper
;
404 c
->range
>>= MODEL_SCALE
;
406 helper
= c
->low
/ c
->range
;
407 ssym
= helper
>> MODEL256_SEC_SCALE
;
408 val
= m
->secondary
[ssym
];
410 end
= start
= m
->secondary
[ssym
+ 1] + 1;
411 while (end
> val
+ 1) {
412 ssym
= (end
+ val
) >> 1;
413 if (m
->freqs
[ssym
] <= helper
) {
417 end
= (end
+ val
) >> 1;
421 prob
= m
->freqs
[val
] * c
->range
;
423 prob2
= m
->freqs
[val
+ 1] * c
->range
;
426 c
->range
= prob2
- prob
;
427 if (c
->range
< RAC_BOTTOM
)
430 model256_update(m
, val
);
435 static int decode_block_type(RangeCoder
*c
, BlockTypeContext
*bt
)
437 bt
->last_type
= rac_get_model_sym(c
, &bt
->bt_model
[bt
->last_type
]);
439 return bt
->last_type
;
442 static int decode_coeff(RangeCoder
*c
, Model
*m
)
446 val
= rac_get_model_sym(c
, m
);
448 sign
= rac_get_bit(c
);
451 val
= (1 << val
) + rac_get_bits(c
, val
);
460 static void decode_fill_block(RangeCoder
*c
, FillBlockCoder
*fc
,
461 uint8_t *dst
, ptrdiff_t stride
, int block_size
)
465 fc
->fill_val
+= decode_coeff(c
, &fc
->coef_model
);
467 for (i
= 0; i
< block_size
; i
++, dst
+= stride
)
468 memset(dst
, fc
->fill_val
, block_size
);
471 static void decode_image_block(RangeCoder
*c
, ImageBlockCoder
*ic
,
472 uint8_t *dst
, ptrdiff_t stride
, int block_size
)
480 vec_size
= rac_get_model_sym(c
, &ic
->vec_size_model
) + 2;
481 for (i
= 0; i
< vec_size
; i
++)
482 vec
[i
] = rac_get_model256_sym(c
, &ic
->vec_entry_model
);
485 memset(prev_line
, 0, sizeof(prev_line
));
487 for (j
= 0; j
< block_size
; j
++) {
490 for (i
= 0; i
< block_size
; i
++) {
493 A
= rac_get_model_sym(c
, &ic
->vq_model
[A
+ B
* 5 + C
* 25]);
499 dst
[i
] = rac_get_model256_sym(c
, &ic
->esc_model
);
505 static int decode_dct(RangeCoder
*c
, DCTBlockCoder
*bc
, int *block
,
508 int skip
, val
, sign
, pos
= 1, zz_pos
, dc
;
509 int blk_pos
= bx
+ by
* bc
->prev_dc_stride
;
511 memset(block
, 0, sizeof(*block
) * 64);
513 dc
= decode_coeff(c
, &bc
->dc_model
);
518 l
= bc
->prev_dc
[blk_pos
- 1];
519 tl
= bc
->prev_dc
[blk_pos
- 1 - bc
->prev_dc_stride
];
520 t
= bc
->prev_dc
[blk_pos
- bc
->prev_dc_stride
];
522 if (FFABS(t
- tl
) <= FFABS(l
- tl
))
527 dc
+= bc
->prev_dc
[blk_pos
- bc
->prev_dc_stride
];
530 dc
+= bc
->prev_dc
[bx
- 1];
532 bc
->prev_dc
[blk_pos
] = dc
;
533 block
[0] = dc
* bc
->qmat
[0];
536 val
= rac_get_model256_sym(c
, &bc
->ac_model
);
551 sign
= rac_get_model2_sym(c
, &bc
->sign_model
);
554 val
= (1 << val
) + rac_get_bits(c
, val
);
559 zz_pos
= ff_zigzag_direct
[pos
];
560 block
[zz_pos
] = val
* bc
->qmat
[zz_pos
];
564 return pos
== 64 ? 0 : -1;
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
)
573 int nblocks
= block_size
>> 3;
578 for (j
= 0; j
< nblocks
; j
++) {
579 for (i
= 0; i
< nblocks
; i
++) {
580 if (decode_dct(c
, bc
, block
, bx
+ i
, by
+ j
)) {
584 ff_mss34_dct_put(dst
+ i
* 8, stride
, block
);
590 static void decode_haar_block(RangeCoder
*c
, HaarBlockCoder
*hc
,
591 uint8_t *dst
, ptrdiff_t stride
,
592 int block_size
, int *block
)
594 const int hsize
= block_size
>> 1;
595 int A
, B
, C
, D
, t1
, t2
, t3
, t4
;
598 for (j
= 0; j
< block_size
; j
++) {
599 for (i
= 0; i
< block_size
; i
++) {
600 if (i
< hsize
&& j
< hsize
)
601 block
[i
] = rac_get_model256_sym(c
, &hc
->coef_model
);
603 block
[i
] = decode_coeff(c
, &hc
->coef_hi_model
);
604 block
[i
] *= hc
->scale
;
608 block
-= block_size
* block_size
;
610 for (j
= 0; j
< hsize
; j
++) {
611 for (i
= 0; i
< hsize
; i
++) {
613 B
= block
[i
+ hsize
];
614 C
= block
[i
+ hsize
* block_size
];
615 D
= block
[i
+ hsize
* block_size
+ hsize
];
621 dst
[i
* 2] = av_clip_uint8(t1
- t2
);
622 dst
[i
* 2 + stride
] = av_clip_uint8(t1
+ t2
);
623 dst
[i
* 2 + 1] = av_clip_uint8(t3
- t4
);
624 dst
[i
* 2 + 1 + stride
] = av_clip_uint8(t3
+ t4
);
631 static void reset_coders(MSS3Context
*ctx
, int quality
)
635 for (i
= 0; i
< 3; i
++) {
636 ctx
->btype
[i
].last_type
= SKIP_BLOCK
;
637 for (j
= 0; j
< 5; j
++)
638 model_reset(&ctx
->btype
[i
].bt_model
[j
]);
639 ctx
->fill_coder
[i
].fill_val
= 0;
640 model_reset(&ctx
->fill_coder
[i
].coef_model
);
641 model256_reset(&ctx
->image_coder
[i
].esc_model
);
642 model256_reset(&ctx
->image_coder
[i
].vec_entry_model
);
643 model_reset(&ctx
->image_coder
[i
].vec_size_model
);
644 for (j
= 0; j
< 125; j
++)
645 model_reset(&ctx
->image_coder
[i
].vq_model
[j
]);
646 if (ctx
->dct_coder
[i
].quality
!= quality
) {
647 ctx
->dct_coder
[i
].quality
= quality
;
648 ff_mss34_gen_quant_mat(ctx
->dct_coder
[i
].qmat
, quality
, !i
);
650 memset(ctx
->dct_coder
[i
].prev_dc
, 0,
651 sizeof(*ctx
->dct_coder
[i
].prev_dc
) *
652 ctx
->dct_coder
[i
].prev_dc_stride
*
653 ctx
->dct_coder
[i
].prev_dc_height
);
654 model_reset(&ctx
->dct_coder
[i
].dc_model
);
655 model2_reset(&ctx
->dct_coder
[i
].sign_model
);
656 model256_reset(&ctx
->dct_coder
[i
].ac_model
);
657 if (ctx
->haar_coder
[i
].quality
!= quality
) {
658 ctx
->haar_coder
[i
].quality
= quality
;
659 ctx
->haar_coder
[i
].scale
= 17 - 7 * quality
/ 50;
661 model_reset(&ctx
->haar_coder
[i
].coef_hi_model
);
662 model256_reset(&ctx
->haar_coder
[i
].coef_model
);
666 static av_cold
void init_coders(MSS3Context
*ctx
)
670 for (i
= 0; i
< 3; i
++) {
671 for (j
= 0; j
< 5; j
++)
672 model_init(&ctx
->btype
[i
].bt_model
[j
], 5);
673 model_init(&ctx
->fill_coder
[i
].coef_model
, 12);
674 model256_init(&ctx
->image_coder
[i
].esc_model
);
675 model256_init(&ctx
->image_coder
[i
].vec_entry_model
);
676 model_init(&ctx
->image_coder
[i
].vec_size_model
, 3);
677 for (j
= 0; j
< 125; j
++)
678 model_init(&ctx
->image_coder
[i
].vq_model
[j
], 5);
679 model_init(&ctx
->dct_coder
[i
].dc_model
, 12);
680 model256_init(&ctx
->dct_coder
[i
].ac_model
);
681 model_init(&ctx
->haar_coder
[i
].coef_hi_model
, 12);
682 model256_init(&ctx
->haar_coder
[i
].coef_model
);
686 static int mss3_decode_frame(AVCodecContext
*avctx
, AVFrame
*rframe
,
687 int *got_frame
, AVPacket
*avpkt
)
689 const uint8_t *buf
= avpkt
->data
;
690 int buf_size
= avpkt
->size
;
691 MSS3Context
*c
= avctx
->priv_data
;
692 RangeCoder
*acoder
= &c
->coder
;
695 int dec_width
, dec_height
, dec_x
, dec_y
, quality
, keyframe
;
696 int x
, y
, i
, mb_width
, mb_height
, blk_size
, btype
;
699 if (buf_size
< HEADER_SIZE
) {
700 av_log(avctx
, AV_LOG_ERROR
,
701 "Frame should have at least %d bytes, got %d instead\n",
702 HEADER_SIZE
, buf_size
);
703 return AVERROR_INVALIDDATA
;
706 bytestream2_init(&gb
, buf
, buf_size
);
707 keyframe
= bytestream2_get_be32(&gb
);
708 if (keyframe
& ~0x301) {
709 av_log(avctx
, AV_LOG_ERROR
, "Invalid frame type %X\n", keyframe
);
710 return AVERROR_INVALIDDATA
;
712 keyframe
= !(keyframe
& 1);
713 bytestream2_skip(&gb
, 6);
714 dec_x
= bytestream2_get_be16(&gb
);
715 dec_y
= bytestream2_get_be16(&gb
);
716 dec_width
= bytestream2_get_be16(&gb
);
717 dec_height
= bytestream2_get_be16(&gb
);
719 if (dec_x
+ dec_width
> avctx
->width
||
720 dec_y
+ dec_height
> avctx
->height
||
721 (dec_width
| dec_height
) & 0xF) {
722 av_log(avctx
, AV_LOG_ERROR
, "Invalid frame dimensions %dx%d +%d,%d\n",
723 dec_width
, dec_height
, dec_x
, dec_y
);
724 return AVERROR_INVALIDDATA
;
726 bytestream2_skip(&gb
, 4);
727 quality
= bytestream2_get_byte(&gb
);
728 if (quality
< 1 || quality
> 100) {
729 av_log(avctx
, AV_LOG_ERROR
, "Invalid quality setting %d\n", quality
);
730 return AVERROR_INVALIDDATA
;
732 bytestream2_skip(&gb
, 4);
734 if (keyframe
&& !bytestream2_get_bytes_left(&gb
)) {
735 av_log(avctx
, AV_LOG_ERROR
, "Keyframe without data found\n");
736 return AVERROR_INVALIDDATA
;
738 if (!keyframe
&& c
->got_error
)
742 if ((ret
= ff_reget_buffer(avctx
, c
->pic
, 0)) < 0)
745 c
->pic
->flags
|= AV_FRAME_FLAG_KEY
;
747 c
->pic
->flags
&= ~AV_FRAME_FLAG_KEY
;
748 c
->pic
->pict_type
= keyframe
? AV_PICTURE_TYPE_I
: AV_PICTURE_TYPE_P
;
749 if (!bytestream2_get_bytes_left(&gb
)) {
750 if ((ret
= av_frame_ref(rframe
, c
->pic
)) < 0)
757 reset_coders(c
, quality
);
759 rac_init(acoder
, buf
+ HEADER_SIZE
, buf_size
- HEADER_SIZE
);
761 mb_width
= dec_width
>> 4;
762 mb_height
= dec_height
>> 4;
763 dst
[0] = c
->pic
->data
[0] + dec_x
+ dec_y
* c
->pic
->linesize
[0];
764 dst
[1] = c
->pic
->data
[1] + dec_x
/ 2 + (dec_y
/ 2) * c
->pic
->linesize
[1];
765 dst
[2] = c
->pic
->data
[2] + dec_x
/ 2 + (dec_y
/ 2) * c
->pic
->linesize
[2];
766 for (y
= 0; y
< mb_height
; y
++) {
767 for (x
= 0; x
< mb_width
; x
++) {
768 for (i
= 0; i
< 3; i
++) {
771 btype
= decode_block_type(acoder
, c
->btype
+ i
);
774 decode_fill_block(acoder
, c
->fill_coder
+ i
,
775 dst
[i
] + x
* blk_size
,
776 c
->pic
->linesize
[i
], blk_size
);
779 decode_image_block(acoder
, c
->image_coder
+ i
,
780 dst
[i
] + x
* blk_size
,
781 c
->pic
->linesize
[i
], blk_size
);
784 decode_dct_block(acoder
, c
->dct_coder
+ i
,
785 dst
[i
] + x
* blk_size
,
786 c
->pic
->linesize
[i
], blk_size
,
790 decode_haar_block(acoder
, c
->haar_coder
+ i
,
791 dst
[i
] + x
* blk_size
,
792 c
->pic
->linesize
[i
], blk_size
,
796 if (c
->got_error
|| acoder
->got_error
) {
797 av_log(avctx
, AV_LOG_ERROR
, "Error decoding block %d,%d\n",
800 return AVERROR_INVALIDDATA
;
804 dst
[0] += c
->pic
->linesize
[0] * 16;
805 dst
[1] += c
->pic
->linesize
[1] * 8;
806 dst
[2] += c
->pic
->linesize
[2] * 8;
809 if ((ret
= av_frame_ref(rframe
, c
->pic
)) < 0)
817 static av_cold
int mss3_decode_end(AVCodecContext
*avctx
)
819 MSS3Context
* const c
= avctx
->priv_data
;
822 av_frame_free(&c
->pic
);
823 for (i
= 0; i
< 3; i
++)
824 av_freep(&c
->dct_coder
[i
].prev_dc
);
829 static av_cold
int mss3_decode_init(AVCodecContext
*avctx
)
831 MSS3Context
* const c
= avctx
->priv_data
;
836 if ((avctx
->width
& 0xF) || (avctx
->height
& 0xF)) {
837 av_log(avctx
, AV_LOG_ERROR
,
838 "Image dimensions should be a multiple of 16.\n");
839 return AVERROR_INVALIDDATA
;
843 for (i
= 0; i
< 3; i
++) {
844 int b_width
= avctx
->width
>> (2 + !!i
);
845 int b_height
= avctx
->height
>> (2 + !!i
);
846 c
->dct_coder
[i
].prev_dc_stride
= b_width
;
847 c
->dct_coder
[i
].prev_dc_height
= b_height
;
848 c
->dct_coder
[i
].prev_dc
= av_malloc(sizeof(*c
->dct_coder
[i
].prev_dc
) *
850 if (!c
->dct_coder
[i
].prev_dc
) {
851 av_log(avctx
, AV_LOG_ERROR
, "Cannot allocate buffer\n");
852 return AVERROR(ENOMEM
);
856 c
->pic
= av_frame_alloc();
858 return AVERROR(ENOMEM
);
860 avctx
->pix_fmt
= AV_PIX_FMT_YUV420P
;
867 const FFCodec ff_msa1_decoder
= {
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
,