2 * Microsoft Screen 1 (aka Windows Media Video V7 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 1 (aka Windows Media Video V7 Screen) decoder
31 typedef struct MSS1Context {
37 static void arith_normalise(ArithCoder *c)
40 if (c->high >= 0x8000) {
41 if (c->low < 0x8000) {
42 if (c->low >= 0x4000 && c->high < 0xC000) {
59 if (get_bits_left(c->gbc.gb) < 1)
61 c->value |= get_bits1(c->gbc.gb);
67 static int arith_get_bits(ArithCoder *c, int bits)
69 int range = c->high - c->low + 1;
70 int val = (((c->value - c->low + 1) << bits) - 1) / range;
71 int prob = range * val;
73 c->high = ((prob + range) >> bits) + c->low - 1;
74 c->low += prob >> bits;
81 static int arith_get_number(ArithCoder *c, int mod_val)
83 int range = c->high - c->low + 1;
84 int val = ((c->value - c->low + 1) * mod_val - 1) / range;
85 int prob = range * val;
87 c->high = (prob + range) / mod_val + c->low - 1;
88 c->low += prob / mod_val;
95 static int arith_get_prob(ArithCoder *c, int16_t *probs)
97 int range = c->high - c->low + 1;
98 int val = ((c->value - c->low + 1) * probs[0] - 1) / range;
101 while (probs[sym] > val)
104 c->high = range * probs[sym - 1] / probs[0] + c->low - 1;
105 c->low += range * probs[sym] / probs[0];
110 ARITH_GET_MODEL_SYM(arith)
112 static void arith_init(ArithCoder *c, GetBitContext *gb)
116 c->value = get_bits(gb, 16);
119 c->get_model_sym = arith_get_model_sym;
120 c->get_number = arith_get_number;
123 static int decode_pal(MSS12Context *ctx, ArithCoder *acoder)
125 int i, ncol, r, g, b;
126 uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
128 if (!ctx->free_colours)
131 ncol = arith_get_number(acoder, ctx->free_colours + 1);
132 for (i = 0; i < ncol; i++) {
133 r = arith_get_bits(acoder, 8);
134 g = arith_get_bits(acoder, 8);
135 b = arith_get_bits(acoder, 8);
136 *pal++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
142 static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
145 MSS1Context *ctx = avctx->priv_data;
146 MSS12Context *c = &ctx->ctx;
152 if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
155 arith_init(&acoder, &gb);
157 if ((ret = ff_reget_buffer(avctx, ctx->pic, 0)) < 0)
160 c->pal_pic = ctx->pic->data[0] + ctx->pic->linesize[0] * (avctx->height - 1);
161 c->pal_stride = -ctx->pic->linesize[0];
162 c->keyframe = !arith_get_bit(&acoder);
165 ff_mss12_slicecontext_reset(&ctx->sc);
166 pal_changed = decode_pal(c, &acoder);
167 ctx->pic->key_frame = 1;
168 ctx->pic->pict_type = AV_PICTURE_TYPE_I;
171 return AVERROR_INVALIDDATA;
172 ctx->pic->key_frame = 0;
173 ctx->pic->pict_type = AV_PICTURE_TYPE_P;
175 c->corrupted = ff_mss12_decode_rect(&ctx->sc, &acoder, 0, 0,
176 avctx->width, avctx->height);
178 return AVERROR_INVALIDDATA;
179 memcpy(ctx->pic->data[1], c->pal, AVPALETTE_SIZE);
180 ctx->pic->palette_has_changed = pal_changed;
182 if ((ret = av_frame_ref(data, ctx->pic)) < 0)
187 /* always report that the buffer was completely consumed */
191 static av_cold int mss1_decode_init(AVCodecContext *avctx)
193 MSS1Context * const c = avctx->priv_data;
196 c->ctx.avctx = avctx;
198 c->pic = av_frame_alloc();
200 return AVERROR(ENOMEM);
202 ret = ff_mss12_decode_init(&c->ctx, 0, &c->sc, NULL);
204 av_frame_free(&c->pic);
206 avctx->pix_fmt = AV_PIX_FMT_PAL8;
211 static av_cold int mss1_decode_end(AVCodecContext *avctx)
213 MSS1Context * const ctx = avctx->priv_data;
215 av_frame_free(&ctx->pic);
216 ff_mss12_decode_end(&ctx->ctx);
221 const AVCodec ff_mss1_decoder = {
223 .long_name = NULL_IF_CONFIG_SMALL("MS Screen 1"),
224 .type = AVMEDIA_TYPE_VIDEO,
225 .id = AV_CODEC_ID_MSS1,
226 .priv_data_size = sizeof(MSS1Context),
227 .init = mss1_decode_init,
228 .close = mss1_decode_end,
229 .decode = mss1_decode_frame,
230 .capabilities = AV_CODEC_CAP_DR1,