2 * Microsoft Screen 3 (aka Microsoft ATC Screen) decoder
3 * Copyright (c) 2012 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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"
31 #define HEADER_SIZE 27
33 #define MODEL2_SCALE 13
34 #define MODEL_SCALE 15
35 #define MODEL256_SEC_SCALE 9
37 typedef struct Model2 {
38 int upd_val, till_rescale;
39 unsigned zero_freq, zero_weight;
40 unsigned total_freq, total_weight;
43 typedef struct Model {
44 int weights[16], freqs[16];
47 int upd_val, max_upd_val, till_rescale;
50 typedef struct Model256 {
51 int weights[256], freqs[256];
55 int upd_val, max_upd_val, till_rescale;
58 #define RAC_BOTTOM 0x01000000
59 typedef struct RangeCoder {
60 const uint8_t *src, *src_end;
74 typedef struct BlockTypeContext {
79 typedef struct FillBlockCoder {
84 typedef struct ImageBlockCoder {
85 Model256 esc_model, vec_entry_model;
90 typedef struct DCTBlockCoder {
101 typedef struct HaarBlockCoder {
107 typedef struct MSS3Context {
108 AVCodecContext *avctx;
113 BlockTypeContext btype[3];
114 FillBlockCoder fill_coder[3];
115 ImageBlockCoder image_coder[3];
116 DCTBlockCoder dct_coder[3];
117 HaarBlockCoder haar_coder[3];
123 static const uint8_t zigzag_scan[64] = {
124 0, 1, 8, 16, 9, 2, 3, 10,
125 17, 24, 32, 25, 18, 11, 4, 5,
126 12, 19, 26, 33, 40, 48, 41, 34,
127 27, 20, 13, 6, 7, 14, 21, 28,
128 35, 42, 49, 56, 57, 50, 43, 36,
129 29, 22, 15, 23, 30, 37, 44, 51,
130 58, 59, 52, 45, 38, 31, 39, 46,
131 53, 60, 61, 54, 47, 55, 62, 63
135 static void model2_reset(Model2 *m)
139 m->zero_freq = 0x1000;
140 m->total_freq = 0x2000;
145 static void model2_update(Model2 *m, int bit)
155 m->total_weight += m->upd_val;
156 if (m->total_weight > 0x2000) {
157 m->total_weight = (m->total_weight + 1) >> 1;
158 m->zero_weight = (m->zero_weight + 1) >> 1;
159 if (m->total_weight == m->zero_weight)
160 m->total_weight = m->zero_weight + 1;
162 m->upd_val = m->upd_val * 5 >> 2;
165 scale = 0x80000000u / m->total_weight;
166 m->zero_freq = m->zero_weight * scale >> 18;
167 m->total_freq = m->total_weight * scale >> 18;
168 m->till_rescale = m->upd_val;
171 static void model_update(Model *m, int val)
180 m->tot_weight += m->upd_val;
182 if (m->tot_weight > 0x8000) {
184 for (i = 0; i < m->num_syms; i++) {
185 m->weights[i] = (m->weights[i] + 1) >> 1;
186 m->tot_weight += m->weights[i];
189 scale = 0x80000000u / m->tot_weight;
190 for (i = 0; i < m->num_syms; i++) {
191 m->freqs[i] = sum * scale >> 16;
192 sum += m->weights[i];
195 m->upd_val = m->upd_val * 5 >> 2;
196 if (m->upd_val > m->max_upd_val)
197 m->upd_val = m->max_upd_val;
198 m->till_rescale = m->upd_val;
201 static void model_reset(Model *m)
206 for (i = 0; i < m->num_syms - 1; i++)
208 m->weights[m->num_syms - 1] = 0;
210 m->upd_val = m->num_syms;
212 model_update(m, m->num_syms - 1);
214 m->upd_val = (m->num_syms + 6) >> 1;
217 static av_cold void model_init(Model *m, int num_syms)
219 m->num_syms = num_syms;
220 m->max_upd_val = 8 * num_syms + 48;
225 static void model256_update(Model256 *m, int val)
235 m->tot_weight += m->upd_val;
237 if (m->tot_weight > 0x8000) {
239 for (i = 0; i < 256; i++) {
240 m->weights[i] = (m->weights[i] + 1) >> 1;
241 m->tot_weight += m->weights[i];
244 scale = 0x80000000u / m->tot_weight;
246 for (i = 0; i < 256; i++) {
247 m->freqs[i] = sum * scale >> 16;
248 sum += m->weights[i];
249 send = m->freqs[i] >> MODEL256_SEC_SCALE;
251 m->secondary[sidx++] = i - 1;
253 while (sidx < m->sec_size)
254 m->secondary[sidx++] = 255;
256 m->upd_val = m->upd_val * 5 >> 2;
257 if (m->upd_val > m->max_upd_val)
258 m->upd_val = m->max_upd_val;
259 m->till_rescale = m->upd_val;
262 static void model256_reset(Model256 *m)
266 for (i = 0; i < 255; i++)
273 model256_update(m, 255);
275 m->upd_val = (256 + 6) >> 1;
278 static av_cold void model256_init(Model256 *m)
280 m->max_upd_val = 8 * 256 + 48;
281 m->sec_size = (1 << 6) + 2;
286 static void rac_init(RangeCoder *c, const uint8_t *src, int size)
291 c->src_end = src + size;
293 for (i = 0; i < FFMIN(size, 4); i++)
294 c->low = (c->low << 8) | *c->src++;
295 c->range = 0xFFFFFFFF;
299 static void rac_normalise(RangeCoder *c)
304 if (c->src < c->src_end) {
306 } else if (!c->low) {
310 if (c->range >= RAC_BOTTOM)
315 static int rac_get_bit(RangeCoder *c)
321 bit = (c->range <= c->low);
325 if (c->range < RAC_BOTTOM)
331 static int rac_get_bits(RangeCoder *c, int nbits)
336 val = c->low / c->range;
337 c->low -= c->range * val;
339 if (c->range < RAC_BOTTOM)
345 static int rac_get_model2_sym(RangeCoder *c, Model2 *m)
349 helper = m->zero_freq * (c->range >> MODEL2_SCALE);
350 bit = (c->low >= helper);
358 if (c->range < RAC_BOTTOM)
361 model2_update(m, bit);
366 static int rac_get_model_sym(RangeCoder *c, Model *m)
368 int prob, prob2, helper, val;
373 c->range >>= MODEL_SCALE;
375 end = m->num_syms >> 1;
378 helper = m->freqs[end] * c->range;
379 if (helper <= c->low) {
386 end = (end2 + val) >> 1;
387 } while (end != val);
389 c->range = prob2 - prob;
390 if (c->range < RAC_BOTTOM)
393 model_update(m, val);
398 static int rac_get_model256_sym(RangeCoder *c, Model256 *m)
400 int prob, prob2, helper, val;
405 c->range >>= MODEL_SCALE;
407 helper = c->low / c->range;
408 ssym = helper >> MODEL256_SEC_SCALE;
409 val = m->secondary[ssym];
411 end = start = m->secondary[ssym + 1] + 1;
412 while (end > val + 1) {
413 ssym = (end + val) >> 1;
414 if (m->freqs[ssym] <= helper) {
418 end = (end + val) >> 1;
422 prob = m->freqs[val] * c->range;
424 prob2 = m->freqs[val + 1] * c->range;
427 c->range = prob2 - prob;
428 if (c->range < RAC_BOTTOM)
431 model256_update(m, val);
436 static int decode_block_type(RangeCoder *c, BlockTypeContext *bt)
438 bt->last_type = rac_get_model_sym(c, &bt->bt_model[bt->last_type]);
440 return bt->last_type;
443 static int decode_coeff(RangeCoder *c, Model *m)
447 val = rac_get_model_sym(c, m);
449 sign = rac_get_bit(c);
452 val = (1 << val) + rac_get_bits(c, val);
461 static void decode_fill_block(RangeCoder *c, FillBlockCoder *fc,
462 uint8_t *dst, int stride, int block_size)
466 fc->fill_val += decode_coeff(c, &fc->coef_model);
468 for (i = 0; i < block_size; i++, dst += stride)
469 memset(dst, fc->fill_val, block_size);
472 static void decode_image_block(RangeCoder *c, ImageBlockCoder *ic,
473 uint8_t *dst, int stride, int block_size)
481 vec_size = rac_get_model_sym(c, &ic->vec_size_model) + 2;
482 for (i = 0; i < vec_size; i++)
483 vec[i] = rac_get_model256_sym(c, &ic->vec_entry_model);
486 memset(prev_line, 0, sizeof(prev_line));
488 for (j = 0; j < block_size; j++) {
491 for (i = 0; i < block_size; i++) {
494 A = rac_get_model_sym(c, &ic->vq_model[A + B * 5 + C * 25]);
500 dst[i] = rac_get_model256_sym(c, &ic->esc_model);
506 static int decode_dct(RangeCoder *c, DCTBlockCoder *bc, int *block,
509 int skip, val, sign, pos = 1, zz_pos, dc;
510 int blk_pos = bx + by * bc->prev_dc_stride;
512 memset(block, 0, sizeof(*block) * 64);
514 dc = decode_coeff(c, &bc->dc_model);
519 l = bc->prev_dc[blk_pos - 1];
520 tl = bc->prev_dc[blk_pos - 1 - bc->prev_dc_stride];
521 t = bc->prev_dc[blk_pos - bc->prev_dc_stride];
523 if (FFABS(t - tl) <= FFABS(l - tl))
528 dc += bc->prev_dc[blk_pos - bc->prev_dc_stride];
531 dc += bc->prev_dc[bx - 1];
533 bc->prev_dc[blk_pos] = dc;
534 block[0] = dc * bc->qmat[0];
537 val = rac_get_model256_sym(c, &bc->ac_model);
552 sign = rac_get_model2_sym(c, &bc->sign_model);
555 val = (1 << val) + rac_get_bits(c, val);
560 zz_pos = zigzag_scan[pos];
561 block[zz_pos] = val * bc->qmat[zz_pos];
565 return pos == 64 ? 0 : -1;
568 static void decode_dct_block(RangeCoder *c, DCTBlockCoder *bc,
569 uint8_t *dst, int stride, int block_size,
570 int *block, int mb_x, int mb_y)
574 int nblocks = block_size >> 3;
579 for (j = 0; j < nblocks; j++) {
580 for (i = 0; i < nblocks; i++) {
581 if (decode_dct(c, bc, block, bx + i, by + j)) {
585 ff_mss34_dct_put(dst + i * 8, stride, block);
591 static void decode_haar_block(RangeCoder *c, HaarBlockCoder *hc,
592 uint8_t *dst, int stride, int block_size,
595 const int hsize = block_size >> 1;
596 int A, B, C, D, t1, t2, t3, t4;
599 for (j = 0; j < block_size; j++) {
600 for (i = 0; i < block_size; i++) {
601 if (i < hsize && j < hsize)
602 block[i] = rac_get_model256_sym(c, &hc->coef_model);
604 block[i] = decode_coeff(c, &hc->coef_hi_model);
605 block[i] *= hc->scale;
609 block -= block_size * block_size;
611 for (j = 0; j < hsize; j++) {
612 for (i = 0; i < hsize; i++) {
614 B = block[i + hsize];
615 C = block[i + hsize * block_size];
616 D = block[i + hsize * block_size + hsize];
622 dst[i * 2] = av_clip_uint8(t1 - t2);
623 dst[i * 2 + stride] = av_clip_uint8(t1 + t2);
624 dst[i * 2 + 1] = av_clip_uint8(t3 - t4);
625 dst[i * 2 + 1 + stride] = av_clip_uint8(t3 + t4);
632 static void reset_coders(MSS3Context *ctx, int quality)
636 for (i = 0; i < 3; i++) {
637 ctx->btype[i].last_type = SKIP_BLOCK;
638 for (j = 0; j < 5; j++)
639 model_reset(&ctx->btype[i].bt_model[j]);
640 ctx->fill_coder[i].fill_val = 0;
641 model_reset(&ctx->fill_coder[i].coef_model);
642 model256_reset(&ctx->image_coder[i].esc_model);
643 model256_reset(&ctx->image_coder[i].vec_entry_model);
644 model_reset(&ctx->image_coder[i].vec_size_model);
645 for (j = 0; j < 125; j++)
646 model_reset(&ctx->image_coder[i].vq_model[j]);
647 if (ctx->dct_coder[i].quality != quality) {
648 ctx->dct_coder[i].quality = quality;
649 ff_mss34_gen_quant_mat(ctx->dct_coder[i].qmat, quality, !i);
651 memset(ctx->dct_coder[i].prev_dc, 0,
652 sizeof(*ctx->dct_coder[i].prev_dc) *
653 ctx->dct_coder[i].prev_dc_stride *
654 ctx->dct_coder[i].prev_dc_height);
655 model_reset(&ctx->dct_coder[i].dc_model);
656 model2_reset(&ctx->dct_coder[i].sign_model);
657 model256_reset(&ctx->dct_coder[i].ac_model);
658 if (ctx->haar_coder[i].quality != quality) {
659 ctx->haar_coder[i].quality = quality;
660 ctx->haar_coder[i].scale = 17 - 7 * quality / 50;
662 model_reset(&ctx->haar_coder[i].coef_hi_model);
663 model256_reset(&ctx->haar_coder[i].coef_model);
667 static av_cold void init_coders(MSS3Context *ctx)
671 for (i = 0; i < 3; i++) {
672 for (j = 0; j < 5; j++)
673 model_init(&ctx->btype[i].bt_model[j], 5);
674 model_init(&ctx->fill_coder[i].coef_model, 12);
675 model256_init(&ctx->image_coder[i].esc_model);
676 model256_init(&ctx->image_coder[i].vec_entry_model);
677 model_init(&ctx->image_coder[i].vec_size_model, 3);
678 for (j = 0; j < 125; j++)
679 model_init(&ctx->image_coder[i].vq_model[j], 5);
680 model_init(&ctx->dct_coder[i].dc_model, 12);
681 model256_init(&ctx->dct_coder[i].ac_model);
682 model_init(&ctx->haar_coder[i].coef_hi_model, 12);
683 model256_init(&ctx->haar_coder[i].coef_model);
687 static int mss3_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
690 const uint8_t *buf = avpkt->data;
691 int buf_size = avpkt->size;
692 MSS3Context *c = avctx->priv_data;
693 RangeCoder *acoder = &c->coder;
696 int dec_width, dec_height, dec_x, dec_y, quality, keyframe;
697 int x, y, i, mb_width, mb_height, blk_size, btype;
700 if (buf_size < HEADER_SIZE) {
701 av_log(avctx, AV_LOG_ERROR,
702 "Frame should have at least %d bytes, got %d instead\n",
703 HEADER_SIZE, buf_size);
704 return AVERROR_INVALIDDATA;
707 bytestream2_init(&gb, buf, buf_size);
708 keyframe = bytestream2_get_be32(&gb);
709 if (keyframe & ~0x301) {
710 av_log(avctx, AV_LOG_ERROR, "Invalid frame type %X\n", keyframe);
711 return AVERROR_INVALIDDATA;
713 keyframe = !(keyframe & 1);
714 bytestream2_skip(&gb, 6);
715 dec_x = bytestream2_get_be16(&gb);
716 dec_y = bytestream2_get_be16(&gb);
717 dec_width = bytestream2_get_be16(&gb);
718 dec_height = bytestream2_get_be16(&gb);
720 if (dec_x + dec_width > avctx->width ||
721 dec_y + dec_height > avctx->height ||
722 (dec_width | dec_height) & 0xF) {
723 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d +%d,%d\n",
724 dec_width, dec_height, dec_x, dec_y);
725 return AVERROR_INVALIDDATA;
727 bytestream2_skip(&gb, 4);
728 quality = bytestream2_get_byte(&gb);
729 if (quality < 1 || quality > 100) {
730 av_log(avctx, AV_LOG_ERROR, "Invalid quality setting %d\n", quality);
731 return AVERROR_INVALIDDATA;
733 bytestream2_skip(&gb, 4);
735 if (keyframe && !bytestream2_get_bytes_left(&gb)) {
736 av_log(avctx, AV_LOG_ERROR, "Keyframe without data found\n");
737 return AVERROR_INVALIDDATA;
739 if (!keyframe && c->got_error)
743 c->pic.reference = 3;
744 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
745 FF_BUFFER_HINTS_REUSABLE;
746 if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
747 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
750 c->pic.key_frame = keyframe;
751 c->pic.pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
752 if (!bytestream2_get_bytes_left(&gb)) {
753 *data_size = sizeof(AVFrame);
754 *(AVFrame*)data = c->pic;
759 reset_coders(c, quality);
761 rac_init(acoder, buf + HEADER_SIZE, buf_size - HEADER_SIZE);
763 mb_width = dec_width >> 4;
764 mb_height = dec_height >> 4;
765 dst[0] = c->pic.data[0] + dec_x + dec_y * c->pic.linesize[0];
766 dst[1] = c->pic.data[1] + dec_x / 2 + (dec_y / 2) * c->pic.linesize[1];
767 dst[2] = c->pic.data[2] + dec_x / 2 + (dec_y / 2) * c->pic.linesize[2];
768 for (y = 0; y < mb_height; y++) {
769 for (x = 0; x < mb_width; x++) {
770 for (i = 0; i < 3; i++) {
773 btype = decode_block_type(acoder, c->btype + i);
776 decode_fill_block(acoder, c->fill_coder + i,
777 dst[i] + x * blk_size,
778 c->pic.linesize[i], blk_size);
781 decode_image_block(acoder, c->image_coder + i,
782 dst[i] + x * blk_size,
783 c->pic.linesize[i], blk_size);
786 decode_dct_block(acoder, c->dct_coder + i,
787 dst[i] + x * blk_size,
788 c->pic.linesize[i], blk_size,
792 decode_haar_block(acoder, c->haar_coder + i,
793 dst[i] + x * blk_size,
794 c->pic.linesize[i], blk_size,
798 if (c->got_error || acoder->got_error) {
799 av_log(avctx, AV_LOG_ERROR, "Error decoding block %d,%d\n",
802 return AVERROR_INVALIDDATA;
806 dst[0] += c->pic.linesize[0] * 16;
807 dst[1] += c->pic.linesize[1] * 8;
808 dst[2] += c->pic.linesize[2] * 8;
811 *data_size = sizeof(AVFrame);
812 *(AVFrame*)data = c->pic;
817 static av_cold int mss3_decode_init(AVCodecContext *avctx)
819 MSS3Context * const c = avctx->priv_data;
824 if ((avctx->width & 0xF) || (avctx->height & 0xF)) {
825 av_log(avctx, AV_LOG_ERROR,
826 "Image dimensions should be a multiple of 16.\n");
827 return AVERROR_INVALIDDATA;
831 for (i = 0; i < 3; i++) {
832 int b_width = avctx->width >> (2 + !!i);
833 int b_height = avctx->height >> (2 + !!i);
834 c->dct_coder[i].prev_dc_stride = b_width;
835 c->dct_coder[i].prev_dc_height = b_height;
836 c->dct_coder[i].prev_dc = av_malloc(sizeof(*c->dct_coder[i].prev_dc) *
838 if (!c->dct_coder[i].prev_dc) {
839 av_log(avctx, AV_LOG_ERROR, "Cannot allocate buffer\n");
841 av_freep(&c->dct_coder[i].prev_dc);
844 return AVERROR(ENOMEM);
848 avctx->pix_fmt = PIX_FMT_YUV420P;
849 avctx->coded_frame = &c->pic;
856 static av_cold int mss3_decode_end(AVCodecContext *avctx)
858 MSS3Context * const c = avctx->priv_data;
862 avctx->release_buffer(avctx, &c->pic);
863 for (i = 0; i < 3; i++)
864 av_freep(&c->dct_coder[i].prev_dc);
869 AVCodec ff_msa1_decoder = {
871 .type = AVMEDIA_TYPE_VIDEO,
873 .priv_data_size = sizeof(MSS3Context),
874 .init = mss3_decode_init,
875 .close = mss3_decode_end,
876 .decode = mss3_decode_frame,
877 .capabilities = CODEC_CAP_DR1,
878 .long_name = NULL_IF_CONFIG_SMALL("MS ATC Screen"),