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
28 #include "bytestream.h"
33 #define HEADER_SIZE 27
35 #define MODEL2_SCALE 13
36 #define MODEL_SCALE 15
37 #define MODEL256_SEC_SCALE 9
39 typedef struct Model2 {
40 int upd_val, till_rescale;
41 unsigned zero_freq, zero_weight;
42 unsigned total_freq, total_weight;
45 typedef struct Model {
46 int weights[16], freqs[16];
49 int upd_val, max_upd_val, till_rescale;
52 typedef struct Model256 {
53 int weights[256], freqs[256];
57 int upd_val, max_upd_val, till_rescale;
60 #define RAC_BOTTOM 0x01000000
61 typedef struct RangeCoder {
62 const uint8_t *src, *src_end;
76 typedef struct BlockTypeContext {
81 typedef struct FillBlockCoder {
86 typedef struct ImageBlockCoder {
87 Model256 esc_model, vec_entry_model;
92 typedef struct DCTBlockCoder {
94 ptrdiff_t prev_dc_stride;
103 typedef struct HaarBlockCoder {
109 typedef struct MSS3Context {
110 AVCodecContext *avctx;
115 BlockTypeContext btype[3];
116 FillBlockCoder fill_coder[3];
117 ImageBlockCoder image_coder[3];
118 DCTBlockCoder dct_coder[3];
119 HaarBlockCoder haar_coder[3];
126 static void model2_reset(Model2 *m)
130 m->zero_freq = 0x1000;
131 m->total_freq = 0x2000;
136 static void model2_update(Model2 *m, int bit)
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;
153 m->upd_val = m->upd_val * 5 >> 2;
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;
162 static void model_update(Model *m, int val)
171 m->tot_weight += m->upd_val;
173 if (m->tot_weight > 0x8000) {
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];
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];
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;
192 static void model_reset(Model *m)
197 for (i = 0; i < m->num_syms - 1; i++)
199 m->weights[m->num_syms - 1] = 0;
201 m->upd_val = m->num_syms;
203 model_update(m, m->num_syms - 1);
205 m->upd_val = (m->num_syms + 6) >> 1;
208 static av_cold void model_init(Model *m, int num_syms)
210 m->num_syms = num_syms;
211 m->max_upd_val = 8 * num_syms + 48;
216 static void model256_update(Model256 *m, int val)
226 m->tot_weight += m->upd_val;
228 if (m->tot_weight > 0x8000) {
230 for (i = 0; i < 256; i++) {
231 m->weights[i] = (m->weights[i] + 1) >> 1;
232 m->tot_weight += m->weights[i];
235 scale = 0x80000000u / m->tot_weight;
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;
242 m->secondary[sidx++] = i - 1;
244 while (sidx < m->sec_size)
245 m->secondary[sidx++] = 255;
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;
253 static void model256_reset(Model256 *m)
257 for (i = 0; i < 255; i++)
264 model256_update(m, 255);
266 m->upd_val = (256 + 6) >> 1;
269 static av_cold void model256_init(Model256 *m)
271 m->max_upd_val = 8 * 256 + 48;
272 m->sec_size = (1 << 6) + 2;
277 static void rac_init(RangeCoder *c, const uint8_t *src, int size)
282 c->src_end = src + size;
284 for (i = 0; i < FFMIN(size, 4); i++)
285 c->low = (c->low << 8) | *c->src++;
286 c->range = 0xFFFFFFFF;
290 static void rac_normalise(RangeCoder *c)
295 if (c->src < c->src_end) {
297 } else if (!c->low) {
301 if (c->range >= RAC_BOTTOM)
306 static int rac_get_bit(RangeCoder *c)
312 bit = (c->range <= c->low);
316 if (c->range < RAC_BOTTOM)
322 static int rac_get_bits(RangeCoder *c, int nbits)
327 val = c->low / c->range;
328 c->low -= c->range * val;
330 if (c->range < RAC_BOTTOM)
336 static int rac_get_model2_sym(RangeCoder *c, Model2 *m)
340 helper = m->zero_freq * (c->range >> MODEL2_SCALE);
341 bit = (c->low >= helper);
349 if (c->range < RAC_BOTTOM)
352 model2_update(m, bit);
357 static int rac_get_model_sym(RangeCoder *c, Model *m)
361 unsigned prob, prob2, helper;
365 c->range >>= MODEL_SCALE;
367 end = m->num_syms >> 1;
370 helper = m->freqs[end] * c->range;
371 if (helper <= c->low) {
378 end = (end2 + val) >> 1;
379 } while (end != val);
381 c->range = prob2 - prob;
382 if (c->range < RAC_BOTTOM)
385 model_update(m, val);
390 static int rac_get_model256_sym(RangeCoder *c, Model256 *m)
395 unsigned prob, prob2, helper;
398 c->range >>= MODEL_SCALE;
400 helper = c->low / c->range;
401 ssym = helper >> MODEL256_SEC_SCALE;
402 val = m->secondary[ssym];
404 end = start = m->secondary[ssym + 1] + 1;
405 while (end > val + 1) {
406 ssym = (end + val) >> 1;
407 if (m->freqs[ssym] <= helper) {
411 end = (end + val) >> 1;
415 prob = m->freqs[val] * c->range;
417 prob2 = m->freqs[val + 1] * c->range;
420 c->range = prob2 - prob;
421 if (c->range < RAC_BOTTOM)
424 model256_update(m, val);
429 static int decode_block_type(RangeCoder *c, BlockTypeContext *bt)
431 bt->last_type = rac_get_model_sym(c, &bt->bt_model[bt->last_type]);
433 return bt->last_type;
436 static int decode_coeff(RangeCoder *c, Model *m)
440 val = rac_get_model_sym(c, m);
442 sign = rac_get_bit(c);
445 val = (1 << val) + rac_get_bits(c, val);
454 static void decode_fill_block(RangeCoder *c, FillBlockCoder *fc,
455 uint8_t *dst, ptrdiff_t stride, int block_size)
459 fc->fill_val += decode_coeff(c, &fc->coef_model);
461 for (i = 0; i < block_size; i++, dst += stride)
462 memset(dst, fc->fill_val, block_size);
465 static void decode_image_block(RangeCoder *c, ImageBlockCoder *ic,
466 uint8_t *dst, ptrdiff_t stride, int block_size)
474 vec_size = rac_get_model_sym(c, &ic->vec_size_model) + 2;
475 for (i = 0; i < vec_size; i++)
476 vec[i] = rac_get_model256_sym(c, &ic->vec_entry_model);
479 memset(prev_line, 0, sizeof(prev_line));
481 for (j = 0; j < block_size; j++) {
484 for (i = 0; i < block_size; i++) {
487 A = rac_get_model_sym(c, &ic->vq_model[A + B * 5 + C * 25]);
493 dst[i] = rac_get_model256_sym(c, &ic->esc_model);
499 static int decode_dct(RangeCoder *c, DCTBlockCoder *bc, int *block,
502 int skip, val, sign, pos = 1, zz_pos, dc;
503 int blk_pos = bx + by * bc->prev_dc_stride;
505 memset(block, 0, sizeof(*block) * 64);
507 dc = decode_coeff(c, &bc->dc_model);
512 l = bc->prev_dc[blk_pos - 1];
513 tl = bc->prev_dc[blk_pos - 1 - bc->prev_dc_stride];
514 t = bc->prev_dc[blk_pos - bc->prev_dc_stride];
516 if (FFABS(t - tl) <= FFABS(l - tl))
521 dc += bc->prev_dc[blk_pos - bc->prev_dc_stride];
524 dc += bc->prev_dc[bx - 1];
526 bc->prev_dc[blk_pos] = dc;
527 block[0] = dc * bc->qmat[0];
530 val = rac_get_model256_sym(c, &bc->ac_model);
545 sign = rac_get_model2_sym(c, &bc->sign_model);
548 val = (1 << val) + rac_get_bits(c, val);
553 zz_pos = ff_zigzag_direct[pos];
554 block[zz_pos] = val * bc->qmat[zz_pos];
558 return pos == 64 ? 0 : -1;
561 static void decode_dct_block(RangeCoder *c, DCTBlockCoder *bc,
562 uint8_t *dst, ptrdiff_t stride, int block_size,
563 int *block, int mb_x, int mb_y)
567 int nblocks = block_size >> 3;
572 for (j = 0; j < nblocks; j++) {
573 for (i = 0; i < nblocks; i++) {
574 if (decode_dct(c, bc, block, bx + i, by + j)) {
578 ff_mss34_dct_put(dst + i * 8, stride, block);
584 static void decode_haar_block(RangeCoder *c, HaarBlockCoder *hc,
585 uint8_t *dst, ptrdiff_t stride,
586 int block_size, int *block)
588 const int hsize = block_size >> 1;
589 int A, B, C, D, t1, t2, t3, t4;
592 for (j = 0; j < block_size; j++) {
593 for (i = 0; i < block_size; i++) {
594 if (i < hsize && j < hsize)
595 block[i] = rac_get_model256_sym(c, &hc->coef_model);
597 block[i] = decode_coeff(c, &hc->coef_hi_model);
598 block[i] *= hc->scale;
602 block -= block_size * block_size;
604 for (j = 0; j < hsize; j++) {
605 for (i = 0; i < hsize; i++) {
607 B = block[i + hsize];
608 C = block[i + hsize * block_size];
609 D = block[i + hsize * block_size + hsize];
615 dst[i * 2] = av_clip_uint8(t1 - t2);
616 dst[i * 2 + stride] = av_clip_uint8(t1 + t2);
617 dst[i * 2 + 1] = av_clip_uint8(t3 - t4);
618 dst[i * 2 + 1 + stride] = av_clip_uint8(t3 + t4);
625 static void reset_coders(MSS3Context *ctx, int quality)
629 for (i = 0; i < 3; i++) {
630 ctx->btype[i].last_type = SKIP_BLOCK;
631 for (j = 0; j < 5; j++)
632 model_reset(&ctx->btype[i].bt_model[j]);
633 ctx->fill_coder[i].fill_val = 0;
634 model_reset(&ctx->fill_coder[i].coef_model);
635 model256_reset(&ctx->image_coder[i].esc_model);
636 model256_reset(&ctx->image_coder[i].vec_entry_model);
637 model_reset(&ctx->image_coder[i].vec_size_model);
638 for (j = 0; j < 125; j++)
639 model_reset(&ctx->image_coder[i].vq_model[j]);
640 if (ctx->dct_coder[i].quality != quality) {
641 ctx->dct_coder[i].quality = quality;
642 ff_mss34_gen_quant_mat(ctx->dct_coder[i].qmat, quality, !i);
644 memset(ctx->dct_coder[i].prev_dc, 0,
645 sizeof(*ctx->dct_coder[i].prev_dc) *
646 ctx->dct_coder[i].prev_dc_stride *
647 ctx->dct_coder[i].prev_dc_height);
648 model_reset(&ctx->dct_coder[i].dc_model);
649 model2_reset(&ctx->dct_coder[i].sign_model);
650 model256_reset(&ctx->dct_coder[i].ac_model);
651 if (ctx->haar_coder[i].quality != quality) {
652 ctx->haar_coder[i].quality = quality;
653 ctx->haar_coder[i].scale = 17 - 7 * quality / 50;
655 model_reset(&ctx->haar_coder[i].coef_hi_model);
656 model256_reset(&ctx->haar_coder[i].coef_model);
660 static av_cold void init_coders(MSS3Context *ctx)
664 for (i = 0; i < 3; i++) {
665 for (j = 0; j < 5; j++)
666 model_init(&ctx->btype[i].bt_model[j], 5);
667 model_init(&ctx->fill_coder[i].coef_model, 12);
668 model256_init(&ctx->image_coder[i].esc_model);
669 model256_init(&ctx->image_coder[i].vec_entry_model);
670 model_init(&ctx->image_coder[i].vec_size_model, 3);
671 for (j = 0; j < 125; j++)
672 model_init(&ctx->image_coder[i].vq_model[j], 5);
673 model_init(&ctx->dct_coder[i].dc_model, 12);
674 model256_init(&ctx->dct_coder[i].ac_model);
675 model_init(&ctx->haar_coder[i].coef_hi_model, 12);
676 model256_init(&ctx->haar_coder[i].coef_model);
680 static int mss3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
683 const uint8_t *buf = avpkt->data;
684 int buf_size = avpkt->size;
685 MSS3Context *c = avctx->priv_data;
686 RangeCoder *acoder = &c->coder;
689 int dec_width, dec_height, dec_x, dec_y, quality, keyframe;
690 int x, y, i, mb_width, mb_height, blk_size, btype;
693 if (buf_size < HEADER_SIZE) {
694 av_log(avctx, AV_LOG_ERROR,
695 "Frame should have at least %d bytes, got %d instead\n",
696 HEADER_SIZE, buf_size);
697 return AVERROR_INVALIDDATA;
700 bytestream2_init(&gb, buf, buf_size);
701 keyframe = bytestream2_get_be32(&gb);
702 if (keyframe & ~0x301) {
703 av_log(avctx, AV_LOG_ERROR, "Invalid frame type %X\n", keyframe);
704 return AVERROR_INVALIDDATA;
706 keyframe = !(keyframe & 1);
707 bytestream2_skip(&gb, 6);
708 dec_x = bytestream2_get_be16(&gb);
709 dec_y = bytestream2_get_be16(&gb);
710 dec_width = bytestream2_get_be16(&gb);
711 dec_height = bytestream2_get_be16(&gb);
713 if (dec_x + dec_width > avctx->width ||
714 dec_y + dec_height > avctx->height ||
715 (dec_width | dec_height) & 0xF) {
716 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d +%d,%d\n",
717 dec_width, dec_height, dec_x, dec_y);
718 return AVERROR_INVALIDDATA;
720 bytestream2_skip(&gb, 4);
721 quality = bytestream2_get_byte(&gb);
722 if (quality < 1 || quality > 100) {
723 av_log(avctx, AV_LOG_ERROR, "Invalid quality setting %d\n", quality);
724 return AVERROR_INVALIDDATA;
726 bytestream2_skip(&gb, 4);
728 if (keyframe && !bytestream2_get_bytes_left(&gb)) {
729 av_log(avctx, AV_LOG_ERROR, "Keyframe without data found\n");
730 return AVERROR_INVALIDDATA;
732 if (!keyframe && c->got_error)
736 if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
738 c->pic->key_frame = keyframe;
739 c->pic->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
740 if (!bytestream2_get_bytes_left(&gb)) {
741 if ((ret = av_frame_ref(data, c->pic)) < 0)
748 reset_coders(c, quality);
750 rac_init(acoder, buf + HEADER_SIZE, buf_size - HEADER_SIZE);
752 mb_width = dec_width >> 4;
753 mb_height = dec_height >> 4;
754 dst[0] = c->pic->data[0] + dec_x + dec_y * c->pic->linesize[0];
755 dst[1] = c->pic->data[1] + dec_x / 2 + (dec_y / 2) * c->pic->linesize[1];
756 dst[2] = c->pic->data[2] + dec_x / 2 + (dec_y / 2) * c->pic->linesize[2];
757 for (y = 0; y < mb_height; y++) {
758 for (x = 0; x < mb_width; x++) {
759 for (i = 0; i < 3; i++) {
762 btype = decode_block_type(acoder, c->btype + i);
765 decode_fill_block(acoder, c->fill_coder + i,
766 dst[i] + x * blk_size,
767 c->pic->linesize[i], blk_size);
770 decode_image_block(acoder, c->image_coder + i,
771 dst[i] + x * blk_size,
772 c->pic->linesize[i], blk_size);
775 decode_dct_block(acoder, c->dct_coder + i,
776 dst[i] + x * blk_size,
777 c->pic->linesize[i], blk_size,
781 decode_haar_block(acoder, c->haar_coder + i,
782 dst[i] + x * blk_size,
783 c->pic->linesize[i], blk_size,
787 if (c->got_error || acoder->got_error) {
788 av_log(avctx, AV_LOG_ERROR, "Error decoding block %d,%d\n",
791 return AVERROR_INVALIDDATA;
795 dst[0] += c->pic->linesize[0] * 16;
796 dst[1] += c->pic->linesize[1] * 8;
797 dst[2] += c->pic->linesize[2] * 8;
800 if ((ret = av_frame_ref(data, c->pic)) < 0)
808 static av_cold int mss3_decode_end(AVCodecContext *avctx)
810 MSS3Context * const c = avctx->priv_data;
813 av_frame_free(&c->pic);
814 for (i = 0; i < 3; i++)
815 av_freep(&c->dct_coder[i].prev_dc);
820 static av_cold int mss3_decode_init(AVCodecContext *avctx)
822 MSS3Context * const c = avctx->priv_data;
827 if ((avctx->width & 0xF) || (avctx->height & 0xF)) {
828 av_log(avctx, AV_LOG_ERROR,
829 "Image dimensions should be a multiple of 16.\n");
830 return AVERROR_INVALIDDATA;
834 for (i = 0; i < 3; i++) {
835 int b_width = avctx->width >> (2 + !!i);
836 int b_height = avctx->height >> (2 + !!i);
837 c->dct_coder[i].prev_dc_stride = b_width;
838 c->dct_coder[i].prev_dc_height = b_height;
839 c->dct_coder[i].prev_dc = av_malloc(sizeof(*c->dct_coder[i].prev_dc) *
841 if (!c->dct_coder[i].prev_dc) {
842 av_log(avctx, AV_LOG_ERROR, "Cannot allocate buffer\n");
843 av_frame_free(&c->pic);
845 av_freep(&c->dct_coder[i].prev_dc);
848 return AVERROR(ENOMEM);
852 c->pic = av_frame_alloc();
854 mss3_decode_end(avctx);
855 return AVERROR(ENOMEM);
858 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
865 AVCodec ff_msa1_decoder = {
867 .long_name = NULL_IF_CONFIG_SMALL("MS ATC Screen"),
868 .type = AVMEDIA_TYPE_VIDEO,
869 .id = AV_CODEC_ID_MSA1,
870 .priv_data_size = sizeof(MSS3Context),
871 .init = mss3_decode_init,
872 .close = mss3_decode_end,
873 .decode = mss3_decode_frame,
874 .capabilities = AV_CODEC_CAP_DR1,