GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/mss1.c Lines: 104 112 92.9 %
Date: 2019-11-22 03:34:36 Branches: 26 34 76.5 %

Line Branch Exec 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 "internal.h"
29
#include "mss12.h"
30
31
typedef struct MSS1Context {
32
    MSS12Context   ctx;
33
    AVFrame       *pic;
34
    SliceContext   sc;
35
} MSS1Context;
36
37
290860
static void arith_normalise(ArithCoder *c)
38
{
39
    for (;;) {
40
290860
        if (c->high >= 0x8000) {
41
261731
            if (c->low < 0x8000) {
42

231213
                if (c->low >= 0x4000 && c->high < 0xC000) {
43
30249
                    c->value -= 0x4000;
44
30249
                    c->low   -= 0x4000;
45
30249
                    c->high  -= 0x4000;
46
                } else {
47
200964
                    return;
48
                }
49
            } else {
50
30518
                c->value -= 0x8000;
51
30518
                c->low   -= 0x8000;
52
30518
                c->high  -= 0x8000;
53
            }
54
        }
55
89896
        c->value <<= 1;
56
89896
        c->low   <<= 1;
57
89896
        c->high  <<= 1;
58
89896
        c->high   |= 1;
59
89896
        if (get_bits_left(c->gbc.gb) < 1)
60
224
            c->overread++;
61
89896
        c->value  |= get_bits1(c->gbc.gb);
62
    }
63
}
64
65
20
ARITH_GET_BIT(arith)
66
67
6
static int arith_get_bits(ArithCoder *c, int bits)
68
{
69
6
    int range = c->high - c->low + 1;
70
6
    int val   = (((c->value - c->low + 1) << bits) - 1) / range;
71
6
    int prob  = range * val;
72
73
6
    c->high   = ((prob + range) >> bits) + c->low - 1;
74
6
    c->low   += prob >> bits;
75
76
6
    arith_normalise(c);
77
78
6
    return val;
79
}
80
81
409
static int arith_get_number(ArithCoder *c, int mod_val)
82
{
83
409
    int range = c->high - c->low + 1;
84
409
    int val   = ((c->value - c->low + 1) * mod_val - 1) / range;
85
409
    int prob  = range * val;
86
87
409
    c->high   = (prob + range) / mod_val + c->low - 1;
88
409
    c->low   += prob / mod_val;
89
90
409
    arith_normalise(c);
91
92
409
    return val;
93
}
94
95
200529
static int arith_get_prob(ArithCoder *c, int16_t *probs)
96
{
97
200529
    int range = c->high - c->low + 1;
98
200529
    int val   = ((c->value - c->low + 1) * probs[0] - 1) / range;
99
200529
    int sym   = 1;
100
101
282720
    while (probs[sym] > val)
102
82191
        sym++;
103
104
200529
    c->high = range * probs[sym - 1] / probs[0] + c->low - 1;
105
200529
    c->low += range * probs[sym]     / probs[0];
106
107
200529
    return sym;
108
}
109
110
200529
ARITH_GET_MODEL_SYM(arith)
111
112
20
static void arith_init(ArithCoder *c, GetBitContext *gb)
113
{
114
20
    c->low           = 0;
115
20
    c->high          = 0xFFFF;
116
20
    c->value         = get_bits(gb, 16);
117
20
    c->overread      = 0;
118
20
    c->gbc.gb        = gb;
119
20
    c->get_model_sym = arith_get_model_sym;
120
20
    c->get_number    = arith_get_number;
121
20
}
122
123
2
static int decode_pal(MSS12Context *ctx, ArithCoder *acoder)
124
{
125
    int i, ncol, r, g, b;
126
2
    uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
127
128
2
    if (!ctx->free_colours)
129
        return 0;
130
131
2
    ncol = arith_get_number(acoder, ctx->free_colours + 1);
132
4
    for (i = 0; i < ncol; i++) {
133
2
        r = arith_get_bits(acoder, 8);
134
2
        g = arith_get_bits(acoder, 8);
135
2
        b = arith_get_bits(acoder, 8);
136
2
        *pal++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
137
    }
138
139
2
    return !!ncol;
140
}
141
142
20
static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
143
                             AVPacket *avpkt)
144
{
145
20
    MSS1Context *ctx = avctx->priv_data;
146
20
    MSS12Context *c = &ctx->ctx;
147
    GetBitContext gb;
148
    ArithCoder acoder;
149
20
    int pal_changed = 0;
150
    int ret;
151
152
20
    if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
153
        return ret;
154
155
20
    arith_init(&acoder, &gb);
156
157
20
    if ((ret = ff_reget_buffer(avctx, ctx->pic, 0)) < 0)
158
        return ret;
159
160
20
    c->pal_pic    =  ctx->pic->data[0] + ctx->pic->linesize[0] * (avctx->height - 1);
161
20
    c->pal_stride = -ctx->pic->linesize[0];
162
20
    c->keyframe   = !arith_get_bit(&acoder);
163
20
    if (c->keyframe) {
164
2
        c->corrupted = 0;
165
2
        ff_mss12_slicecontext_reset(&ctx->sc);
166
2
        pal_changed        = decode_pal(c, &acoder);
167
2
        ctx->pic->key_frame = 1;
168
2
        ctx->pic->pict_type = AV_PICTURE_TYPE_I;
169
    } else {
170
18
        if (c->corrupted)
171
            return AVERROR_INVALIDDATA;
172
18
        ctx->pic->key_frame = 0;
173
18
        ctx->pic->pict_type = AV_PICTURE_TYPE_P;
174
    }
175
20
    c->corrupted = ff_mss12_decode_rect(&ctx->sc, &acoder, 0, 0,
176
                                        avctx->width, avctx->height);
177
20
    if (c->corrupted)
178
        return AVERROR_INVALIDDATA;
179
20
    memcpy(ctx->pic->data[1], c->pal, AVPALETTE_SIZE);
180
20
    ctx->pic->palette_has_changed = pal_changed;
181
182
20
    if ((ret = av_frame_ref(data, ctx->pic)) < 0)
183
        return ret;
184
185
20
    *got_frame      = 1;
186
187
    /* always report that the buffer was completely consumed */
188
20
    return avpkt->size;
189
}
190
191
2
static av_cold int mss1_decode_init(AVCodecContext *avctx)
192
{
193
2
    MSS1Context * const c = avctx->priv_data;
194
    int ret;
195
196
2
    c->ctx.avctx       = avctx;
197
198
2
    c->pic = av_frame_alloc();
199
2
    if (!c->pic)
200
        return AVERROR(ENOMEM);
201
202
2
    ret = ff_mss12_decode_init(&c->ctx, 0, &c->sc, NULL);
203
2
    if (ret < 0)
204
        av_frame_free(&c->pic);
205
206
2
    avctx->pix_fmt = AV_PIX_FMT_PAL8;
207
208
2
    return ret;
209
}
210
211
2
static av_cold int mss1_decode_end(AVCodecContext *avctx)
212
{
213
2
    MSS1Context * const ctx = avctx->priv_data;
214
215
2
    av_frame_free(&ctx->pic);
216
2
    ff_mss12_decode_end(&ctx->ctx);
217
218
2
    return 0;
219
}
220
221
AVCodec ff_mss1_decoder = {
222
    .name           = "mss1",
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,
231
};