2 * Microsoft Screen 1 (aka Windows Media Video V7 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 1 (aka Windows Media Video V7 Screen) decoder
27 #include "libavutil/intfloat.h"
28 #include "libavutil/intreadwrite.h"
32 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 c->value |= get_bits1(c->gb);
63 static int arith_get_bit(ArithCoder *c)
65 int range = c->high - c->low + 1;
66 int bit = (((c->value - c->low) << 1) + 1) / range;
71 c->high = c->low + (range >> 1) - 1;
78 static int arith_get_bits(ArithCoder *c, int bits)
80 int range = c->high - c->low + 1;
81 int val = (((c->value - c->low + 1) << bits) - 1) / range;
82 int prob = range * val;
84 c->high = ((prob + range) >> bits) + c->low - 1;
85 c->low += prob >> bits;
92 static int arith_get_number(ArithCoder *c, int mod_val)
94 int range = c->high - c->low + 1;
95 int val = ((c->value - c->low + 1) * mod_val - 1) / range;
96 int prob = range * val;
98 c->high = (prob + range) / mod_val + c->low - 1;
99 c->low += prob / mod_val;
106 static int arith_get_prob(ArithCoder *c, int *probs)
108 int range = c->high - c->low + 1;
109 int val = ((c->value - c->low + 1) * probs[0] - 1) / range;
112 while (probs[sym] > val)
115 c->high = range * probs[sym - 1] / probs[0] + c->low - 1;
116 c->low += range * probs[sym] / probs[0];
121 static int arith_get_model_sym(ArithCoder *c, Model *m)
125 idx = arith_get_prob(c, m->cum_prob);
127 val = m->idx2sym[idx];
128 ff_mss12_model_update(m, idx);
135 static void arith_init(ArithCoder *c, GetBitContext *gb)
139 c->value = get_bits(gb, 16);
142 c->get_model_sym = arith_get_model_sym;
143 c->get_number = arith_get_number;
146 static int decode_pal(MSS1Context *ctx, ArithCoder *acoder)
148 int i, ncol, r, g, b;
149 uint32_t *pal = ctx->ctx.pal + 256 - ctx->ctx.free_colours;
151 if (!ctx->ctx.free_colours)
154 ncol = arith_get_number(acoder, ctx->ctx.free_colours + 1);
155 for (i = 0; i < ncol; i++) {
156 r = arith_get_bits(acoder, 8);
157 g = arith_get_bits(acoder, 8);
158 b = arith_get_bits(acoder, 8);
159 *pal++ = (r << 16) | (g << 8) | b;
165 static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
168 const uint8_t *buf = avpkt->data;
169 int buf_size = avpkt->size;
170 MSS1Context *c = avctx->priv_data;
176 init_get_bits(&gb, buf, buf_size * 8);
177 arith_init(&acoder, &gb);
179 c->pic.reference = 3;
180 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
181 FF_BUFFER_HINTS_REUSABLE;
182 if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
183 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
187 c->ctx.pic_start = c->pic.data[0] + c->pic.linesize[0] * (avctx->height - 1);
188 c->ctx.pic_stride = -c->pic.linesize[0];
189 c->ctx.keyframe = !arith_get_bit(&acoder);
190 if (c->ctx.keyframe) {
191 ff_mss12_codec_reset(&c->ctx);
192 pal_changed = decode_pal(c, &acoder);
193 c->pic.key_frame = 1;
194 c->pic.pict_type = AV_PICTURE_TYPE_I;
196 if (c->ctx.corrupted)
197 return AVERROR_INVALIDDATA;
198 c->pic.key_frame = 0;
199 c->pic.pict_type = AV_PICTURE_TYPE_P;
201 c->ctx.corrupted = ff_mss12_decode_rect(&c->ctx, &acoder, 0, 0,
202 avctx->width, avctx->height);
203 if (c->ctx.corrupted)
204 return AVERROR_INVALIDDATA;
205 memcpy(c->pic.data[1], c->ctx.pal, AVPALETTE_SIZE);
206 c->pic.palette_has_changed = pal_changed;
208 *data_size = sizeof(AVFrame);
209 *(AVFrame*)data = c->pic;
211 /* always report that the buffer was completely consumed */
215 static av_cold int mss1_decode_init(AVCodecContext *avctx)
217 MSS1Context * const c = avctx->priv_data;
219 c->ctx.avctx = avctx;
220 avctx->coded_frame = &c->pic;
222 return ff_mss12_decode_init(avctx, 0);
225 static av_cold int mss1_decode_end(AVCodecContext *avctx)
227 MSS1Context * const c = avctx->priv_data;
230 avctx->release_buffer(avctx, &c->pic);
231 ff_mss12_decode_end(avctx);
236 AVCodec ff_mss1_decoder = {
238 .type = AVMEDIA_TYPE_VIDEO,
239 .id = AV_CODEC_ID_MSS1,
240 .priv_data_size = sizeof(MSS1Context),
241 .init = mss1_decode_init,
242 .close = mss1_decode_end,
243 .decode = mss1_decode_frame,
244 .capabilities = CODEC_CAP_DR1,
245 .long_name = NULL_IF_CONFIG_SMALL("MS Screen 1"),