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
30
typedef struct MSS1Context {
36
static void arith_normalise(ArithCoder *c)
39
if (c->high >= 0x8000) {
40
if (c->low < 0x8000) {
41
if (c->low >= 0x4000 && c->high < 0xC000) {
58
c->value |= get_bits1(c->gbc.gb);
64
static int arith_get_bits(ArithCoder *c, int bits)
66
int range = c->high - c->low + 1;
67
int val = (((c->value - c->low + 1) << bits) - 1) / range;
68
int prob = range * val;
70
c->high = ((prob + range) >> bits) + c->low - 1;
71
c->low += prob >> bits;
78
static int arith_get_number(ArithCoder *c, int mod_val)
80
int range = c->high - c->low + 1;
81
int val = ((c->value - c->low + 1) * mod_val - 1) / range;
82
int prob = range * val;
84
c->high = (prob + range) / mod_val + c->low - 1;
85
c->low += prob / mod_val;
92
static int arith_get_prob(ArithCoder *c, int16_t *probs)
94
int range = c->high - c->low + 1;
95
int val = ((c->value - c->low + 1) * probs[0] - 1) / range;
98
while (probs[sym] > val)
101
c->high = range * probs[sym - 1] / probs[0] + c->low - 1;
102
c->low += range * probs[sym] / probs[0];
107
ARITH_GET_MODEL_SYM()
109
static void arith_init(ArithCoder *c, GetBitContext *gb)
113
c->value = get_bits(gb, 16);
115
c->get_model_sym = arith_get_model_sym;
116
c->get_number = arith_get_number;
119
static int decode_pal(MSS12Context *ctx, ArithCoder *acoder)
121
int i, ncol, r, g, b;
122
uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
124
if (!ctx->free_colours)
127
ncol = arith_get_number(acoder, ctx->free_colours + 1);
128
for (i = 0; i < ncol; i++) {
129
r = arith_get_bits(acoder, 8);
130
g = arith_get_bits(acoder, 8);
131
b = arith_get_bits(acoder, 8);
132
*pal++ = (r << 16) | (g << 8) | b;
138
static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
141
const uint8_t *buf = avpkt->data;
142
int buf_size = avpkt->size;
143
MSS1Context *ctx = avctx->priv_data;
144
MSS12Context *c = &ctx->ctx;
150
init_get_bits(&gb, buf, buf_size * 8);
151
arith_init(&acoder, &gb);
153
ctx->pic.reference = 3;
154
ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
155
FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
156
if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {
157
av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
161
c->pal_pic = ctx->pic.data[0] + ctx->pic.linesize[0] * (avctx->height - 1);
162
c->pal_stride = -ctx->pic.linesize[0];
163
c->keyframe = !arith_get_bit(&acoder);
166
ff_mss12_slicecontext_reset(&ctx->sc);
167
pal_changed = decode_pal(c, &acoder);
168
ctx->pic.key_frame = 1;
169
ctx->pic.pict_type = AV_PICTURE_TYPE_I;
172
return AVERROR_INVALIDDATA;
173
ctx->pic.key_frame = 0;
174
ctx->pic.pict_type = AV_PICTURE_TYPE_P;
176
c->corrupted = ff_mss12_decode_rect(&ctx->sc, &acoder, 0, 0,
177
avctx->width, avctx->height);
179
return AVERROR_INVALIDDATA;
180
memcpy(ctx->pic.data[1], c->pal, AVPALETTE_SIZE);
181
ctx->pic.palette_has_changed = pal_changed;
184
*(AVFrame*)data = ctx->pic;
186
/* always report that the buffer was completely consumed */
190
static av_cold int mss1_decode_init(AVCodecContext *avctx)
192
MSS1Context * const c = avctx->priv_data;
195
c->ctx.avctx = avctx;
196
avctx->coded_frame = &c->pic;
198
ret = ff_mss12_decode_init(&c->ctx, 0, &c->sc, NULL);
200
avctx->pix_fmt = AV_PIX_FMT_PAL8;
205
static av_cold int mss1_decode_end(AVCodecContext *avctx)
207
MSS1Context * const ctx = avctx->priv_data;
209
if (ctx->pic.data[0])
210
avctx->release_buffer(avctx, &ctx->pic);
211
ff_mss12_decode_end(&ctx->ctx);
216
AVCodec ff_mss1_decoder = {
218
.type = AVMEDIA_TYPE_VIDEO,
219
.id = AV_CODEC_ID_MSS1,
220
.priv_data_size = sizeof(MSS1Context),
221
.init = mss1_decode_init,
222
.close = mss1_decode_end,
223
.decode = mss1_decode_frame,
224
.capabilities = CODEC_CAP_DR1,
225
.long_name = NULL_IF_CONFIG_SMALL("MS Screen 1"),