Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/mss1.c
Line
Count
Source
1
/*
2
 * Microsoft Screen 1 (aka Windows Media Video V7 Screen) decoder
3
 * Copyright (c) 2012 Konstantin Shishkov
4
 *
5
 * This file is part of FFmpeg.
6
 *
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.
11
 *
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.
16
 *
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
20
 */
21
22
/**
23
 * @file
24
 * Microsoft Screen 1 (aka Windows Media Video V7 Screen) decoder
25
 */
26
27
#include "avcodec.h"
28
#include "codec_internal.h"
29
#include "decode.h"
30
#include "mss12.h"
31
32
typedef struct MSS1Context {
33
    MSS12Context   ctx;
34
    AVFrame       *pic;
35
    SliceContext   sc;
36
} MSS1Context;
37
38
static void arith_normalise(ArithCoder *c)
39
60.9M
{
40
234M
    for (;;) {
41
234M
        if (c->high >= 0x8000) {
42
206M
            if (c->low < 0x8000) {
43
190M
                if (c->low >= 0x4000 && c->high < 0xC000) {
44
129M
                    c->value -= 0x4000;
45
129M
                    c->low   -= 0x4000;
46
129M
                    c->high  -= 0x4000;
47
129M
                } else {
48
60.9M
                    return;
49
60.9M
                }
50
190M
            } else {
51
16.0M
                c->value -= 0x8000;
52
16.0M
                c->low   -= 0x8000;
53
16.0M
                c->high  -= 0x8000;
54
16.0M
            }
55
206M
        }
56
173M
        c->value <<= 1;
57
173M
        c->low   <<= 1;
58
173M
        c->high  <<= 1;
59
173M
        c->high   |= 1;
60
173M
        if (get_bits_left(c->gbc.gb) < 1)
61
92.4M
            c->overread++;
62
173M
        c->value  |= get_bits1(c->gbc.gb);
63
173M
    }
64
60.9M
}
65
66
ARITH_GET_BIT(arith)
67
68
static int arith_get_bits(ArithCoder *c, int bits)
69
11.3M
{
70
11.3M
    int range = c->high - c->low + 1;
71
11.3M
    int val   = (((c->value - c->low + 1) << bits) - 1) / range;
72
11.3M
    int prob  = range * val;
73
74
11.3M
    c->high   = ((prob + range) >> bits) + c->low - 1;
75
11.3M
    c->low   += prob >> bits;
76
77
11.3M
    arith_normalise(c);
78
79
11.3M
    return val;
80
11.3M
}
81
82
static int arith_get_number(ArithCoder *c, int mod_val)
83
161k
{
84
161k
    int range = c->high - c->low + 1;
85
161k
    int val   = ((c->value - c->low + 1) * mod_val - 1) / range;
86
161k
    int prob  = range * val;
87
88
161k
    c->high   = (prob + range) / mod_val + c->low - 1;
89
161k
    c->low   += prob / mod_val;
90
91
161k
    arith_normalise(c);
92
93
161k
    return val;
94
161k
}
95
96
static int arith_get_prob(ArithCoder *c, int16_t *probs)
97
49.0M
{
98
49.0M
    int range = c->high - c->low + 1;
99
49.0M
    int val   = ((c->value - c->low + 1) * probs[0] - 1) / range;
100
49.0M
    int sym   = 1;
101
102
193M
    while (probs[sym] > val)
103
144M
        sym++;
104
105
49.0M
    c->high = range * probs[sym - 1] / probs[0] + c->low - 1;
106
49.0M
    c->low += range * probs[sym]     / probs[0];
107
108
49.0M
    return sym;
109
49.0M
}
110
111
ARITH_GET_MODEL_SYM(arith)
112
113
static void arith_init(ArithCoder *c, GetBitContext *gb)
114
358k
{
115
358k
    c->low           = 0;
116
358k
    c->high          = 0xFFFF;
117
358k
    c->value         = get_bits(gb, 16);
118
358k
    c->overread      = 0;
119
358k
    c->gbc.gb        = gb;
120
358k
    c->get_model_sym = arith_get_model_sym;
121
358k
    c->get_number    = arith_get_number;
122
358k
}
123
124
static void decode_pal(MSS12Context *ctx, ArithCoder *acoder)
125
206k
{
126
206k
    int i, ncol, r, g, b;
127
206k
    uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
128
129
206k
    if (!ctx->free_colours)
130
99.1k
        return;
131
132
107k
    ncol = arith_get_number(acoder, ctx->free_colours + 1);
133
3.89M
    for (i = 0; i < ncol; i++) {
134
3.79M
        r = arith_get_bits(acoder, 8);
135
3.79M
        g = arith_get_bits(acoder, 8);
136
3.79M
        b = arith_get_bits(acoder, 8);
137
3.79M
        *pal++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
138
3.79M
    }
139
107k
}
140
141
static int mss1_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
142
                             int *got_frame, AVPacket *avpkt)
143
358k
{
144
358k
    MSS1Context *ctx = avctx->priv_data;
145
358k
    MSS12Context *c = &ctx->ctx;
146
358k
    GetBitContext gb;
147
358k
    ArithCoder acoder;
148
358k
    int ret;
149
150
358k
    if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
151
0
        return ret;
152
153
358k
    arith_init(&acoder, &gb);
154
155
358k
    if ((ret = ff_reget_buffer(avctx, ctx->pic, 0)) < 0)
156
2.18k
        return ret;
157
158
355k
    c->pal_pic    =  ctx->pic->data[0] + ctx->pic->linesize[0] * (avctx->height - 1);
159
355k
    c->pal_stride = -ctx->pic->linesize[0];
160
355k
    c->keyframe   = !arith_get_bit(&acoder);
161
355k
    if (c->keyframe) {
162
206k
        c->corrupted = 0;
163
206k
        ff_mss12_slicecontext_reset(&ctx->sc);
164
206k
        decode_pal(c, &acoder);
165
206k
        ctx->pic->flags |= AV_FRAME_FLAG_KEY;
166
206k
        ctx->pic->pict_type = AV_PICTURE_TYPE_I;
167
206k
    } else {
168
149k
        if (c->corrupted)
169
42.5k
            return AVERROR_INVALIDDATA;
170
107k
        ctx->pic->flags &= ~AV_FRAME_FLAG_KEY;
171
107k
        ctx->pic->pict_type = AV_PICTURE_TYPE_P;
172
107k
    }
173
313k
    c->corrupted = ff_mss12_decode_rect(&ctx->sc, &acoder, 0, 0,
174
313k
                                        avctx->width, avctx->height);
175
313k
    if (c->corrupted)
176
184k
        return AVERROR_INVALIDDATA;
177
128k
    memcpy(ctx->pic->data[1], c->pal, AVPALETTE_SIZE);
178
179
128k
    if ((ret = av_frame_ref(rframe, ctx->pic)) < 0)
180
0
        return ret;
181
182
128k
    *got_frame      = 1;
183
184
    /* always report that the buffer was completely consumed */
185
128k
    return avpkt->size;
186
128k
}
187
188
static av_cold int mss1_decode_init(AVCodecContext *avctx)
189
982
{
190
982
    MSS1Context * const c = avctx->priv_data;
191
982
    int ret;
192
193
982
    c->ctx.avctx       = avctx;
194
195
982
    c->pic = av_frame_alloc();
196
982
    if (!c->pic)
197
0
        return AVERROR(ENOMEM);
198
199
982
    ret = ff_mss12_decode_init(&c->ctx, 0, &c->sc, NULL);
200
982
    if (ret < 0)
201
391
        return ret;
202
203
591
    avctx->pix_fmt = AV_PIX_FMT_PAL8;
204
205
591
    return ret;
206
982
}
207
208
static av_cold int mss1_decode_end(AVCodecContext *avctx)
209
982
{
210
982
    MSS1Context * const ctx = avctx->priv_data;
211
212
982
    av_frame_free(&ctx->pic);
213
982
    ff_mss12_decode_end(&ctx->ctx);
214
215
982
    return 0;
216
982
}
217
218
const FFCodec ff_mss1_decoder = {
219
    .p.name         = "mss1",
220
    CODEC_LONG_NAME("MS Screen 1"),
221
    .p.type         = AVMEDIA_TYPE_VIDEO,
222
    .p.id           = AV_CODEC_ID_MSS1,
223
    .priv_data_size = sizeof(MSS1Context),
224
    .init           = mss1_decode_init,
225
    .close          = mss1_decode_end,
226
    FF_CODEC_DECODE_CB(mss1_decode_frame),
227
    .p.capabilities = AV_CODEC_CAP_DR1,
228
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
229
};