Browse code

MSS2 decoder

Signed-off-by: Kostya Shishkov <kostya.shishkov@gmail.com>

Alberto Delmás authored on 2012/08/25 00:45:57
Showing 20 changed files
... ...
@@ -45,6 +45,7 @@ version <next>:
45 45
 - avconv -shortest option is now per-output file,
46 46
   -pass and -passlogfile are now per-output stream
47 47
 - Ut Video encoder
48
+- Microsoft Screen 2 decoder
48 49
 
49 50
 
50 51
 version 0.8:
... ...
@@ -1479,6 +1479,7 @@ msmpeg4v2_decoder_select="h263_decoder"
1479 1479
 msmpeg4v2_encoder_select="h263_encoder"
1480 1480
 msmpeg4v3_decoder_select="h263_decoder"
1481 1481
 msmpeg4v3_encoder_select="h263_encoder"
1482
+mss2_decoder_select="vc1_decoder"
1482 1483
 nellymoser_decoder_select="mdct sinewin"
1483 1484
 nellymoser_encoder_select="mdct sinewin"
1484 1485
 png_decoder_select="zlib"
... ...
@@ -547,6 +547,8 @@ following image formats are supported:
547 547
 @item Microsoft RLE          @tab     @tab  X
548 548
 @item Microsoft Screen 1     @tab     @tab  X
549 549
     @tab Also known as Windows Media Video V7 Screen.
550
+@item Microsoft Screen 2     @tab     @tab  X
551
+    @tab Also known as Windows Media Video V9 Screen.
550 552
 @item Microsoft Video 1      @tab     @tab  X
551 553
 @item Mimic                  @tab     @tab  X
552 554
     @tab Used in MSN Messenger Webcam streams.
... ...
@@ -271,6 +271,7 @@ OBJS-$(CONFIG_MSMPEG4V3_ENCODER)       += msmpeg4.o msmpeg4enc.o msmpeg4data.o \
271 271
 OBJS-$(CONFIG_MSRLE_DECODER)           += msrle.o msrledec.o
272 272
 OBJS-$(CONFIG_MSA1_DECODER)            += mss3.o mss34dsp.o
273 273
 OBJS-$(CONFIG_MSS1_DECODER)            += mss1.o mss12.o
274
+OBJS-$(CONFIG_MSS2_DECODER)            += mss2.o mss12.o mss2dsp.o
274 275
 OBJS-$(CONFIG_MSVIDEO1_DECODER)        += msvideo1.o
275 276
 OBJS-$(CONFIG_MSZH_DECODER)            += lcldec.o
276 277
 OBJS-$(CONFIG_MTS2_DECODER)            += mss4.o mss34dsp.o
... ...
@@ -160,6 +160,7 @@ void avcodec_register_all(void)
160 160
     REGISTER_ENCDEC  (MSMPEG4V3, msmpeg4v3);
161 161
     REGISTER_DECODER (MSRLE, msrle);
162 162
     REGISTER_DECODER (MSS1, mss1);
163
+    REGISTER_DECODER (MSS2, mss2);
163 164
     REGISTER_DECODER (MSVIDEO1, msvideo1);
164 165
     REGISTER_DECODER (MSZH, mszh);
165 166
     REGISTER_DECODER (MTS2, mts2);
... ...
@@ -264,6 +264,7 @@ enum AVCodecID {
264 264
     AV_CODEC_ID_TSCC2,
265 265
     AV_CODEC_ID_MTS2,
266 266
     AV_CODEC_ID_CLLC,
267
+    AV_CODEC_ID_MSS2,
267 268
 
268 269
     /* various PCM "codecs" */
269 270
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
... ...
@@ -1200,6 +1200,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
1200 1200
         .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
1201 1201
         .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
1202 1202
     },
1203
+    {
1204
+        .id        = AV_CODEC_ID_MSS2,
1205
+        .type      = AVMEDIA_TYPE_VIDEO,
1206
+        .name      = "mss2",
1207
+        .long_name = NULL_IF_CONFIG_SMALL("MS Windows Media Video V9 Screen"),
1208
+        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
1209
+    },
1203 1210
 
1204 1211
     /* various PCM "codecs" */
1205 1212
     {
... ...
@@ -58,7 +58,10 @@ av_cold int ff_h263_decode_init(AVCodecContext *avctx)
58 58
     s->quant_precision=5;
59 59
     s->decode_mb= ff_h263_decode_mb;
60 60
     s->low_delay= 1;
61
-    avctx->pix_fmt= avctx->get_format(avctx, avctx->codec->pix_fmts);
61
+    if (avctx->codec->id == AV_CODEC_ID_MSS2)
62
+        avctx->pix_fmt = PIX_FMT_YUV420P;
63
+    else
64
+        avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
62 65
     s->unrestricted_mv= 1;
63 66
 
64 67
     /* select sub codec */
... ...
@@ -93,6 +96,7 @@ av_cold int ff_h263_decode_init(AVCodecContext *avctx)
93 93
     case AV_CODEC_ID_WMV3:
94 94
     case AV_CODEC_ID_VC1IMAGE:
95 95
     case AV_CODEC_ID_WMV3IMAGE:
96
+    case AV_CODEC_ID_MSS2:
96 97
         s->h263_pred = 1;
97 98
         s->msmpeg4_version=6;
98 99
         avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
... ...
@@ -226,10 +226,11 @@ void ff_copy_picture(Picture *dst, Picture *src)
226 226
  */
227 227
 static void free_frame_buffer(MpegEncContext *s, Picture *pic)
228 228
 {
229
-    /* Windows Media Image codecs allocate internal buffers with different
230
-     * dimensions; ignore user defined callbacks for these
231
-     */
232
-    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE && s->codec_id != AV_CODEC_ID_VC1IMAGE)
229
+    /* WM Image / Screen codecs allocate internal buffers with different
230
+     * dimensions / colorspaces; ignore user-defined callbacks for these. */
231
+    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
232
+        s->codec_id != AV_CODEC_ID_VC1IMAGE  &&
233
+        s->codec_id != AV_CODEC_ID_MSS2)
233 234
         ff_thread_release_buffer(s->avctx, &pic->f);
234 235
     else
235 236
         avcodec_default_release_buffer(s->avctx, &pic->f);
... ...
@@ -254,7 +255,9 @@ static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
254 254
         }
255 255
     }
256 256
 
257
-    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE && s->codec_id != AV_CODEC_ID_VC1IMAGE)
257
+    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
258
+        s->codec_id != AV_CODEC_ID_VC1IMAGE  &&
259
+        s->codec_id != AV_CODEC_ID_MSS2)
258 260
         r = ff_thread_get_buffer(s->avctx, &pic->f);
259 261
     else
260 262
         r = avcodec_default_get_buffer(s->avctx, &pic->f);
... ...
@@ -24,14 +24,13 @@
24 24
  * Microsoft Screen 1 (aka Windows Media Video V7 Screen) decoder
25 25
  */
26 26
 
27
-#include "libavutil/intfloat.h"
28
-#include "libavutil/intreadwrite.h"
29 27
 #include "avcodec.h"
30 28
 #include "mss12.h"
31 29
 
32 30
 typedef struct MSS1Context {
33 31
     MSS12Context   ctx;
34 32
     AVFrame        pic;
33
+    SliceContext   sc[2];
35 34
 } MSS1Context;
36 35
 
37 36
 static void arith_normalise(ArithCoder *c)
... ...
@@ -56,24 +55,11 @@ static void arith_normalise(ArithCoder *c)
56 56
         c->low   <<= 1;
57 57
         c->high  <<= 1;
58 58
         c->high   |= 1;
59
-        c->value  |= get_bits1(c->gb);
59
+        c->value  |= get_bits1(c->gbc.gb);
60 60
     }
61 61
 }
62 62
 
63
-static int arith_get_bit(ArithCoder *c)
64
-{
65
-    int range = c->high - c->low + 1;
66
-    int bit   = (((c->value - c->low) << 1) + 1) / range;
67
-
68
-    if (bit)
69
-        c->low += range >> 1;
70
-    else
71
-        c->high = c->low + (range >> 1) - 1;
72
-
73
-    arith_normalise(c);
74
-
75
-    return bit;
76
-}
63
+ARITH_GET_BIT()
77 64
 
78 65
 static int arith_get_bits(ArithCoder *c, int bits)
79 66
 {
... ...
@@ -118,40 +104,27 @@ static int arith_get_prob(ArithCoder *c, int *probs)
118 118
     return sym;
119 119
 }
120 120
 
121
-static int arith_get_model_sym(ArithCoder *c, Model *m)
122
-{
123
-    int idx, val;
124
-
125
-    idx = arith_get_prob(c, m->cum_prob);
126
-
127
-    val = m->idx2sym[idx];
128
-    ff_mss12_model_update(m, idx);
129
-
130
-    arith_normalise(c);
131
-
132
-    return val;
133
-}
121
+ARITH_GET_MODEL_SYM()
134 122
 
135 123
 static void arith_init(ArithCoder *c, GetBitContext *gb)
136 124
 {
137
-    c->low   = 0;
138
-    c->high  = 0xFFFF;
139
-    c->value = get_bits(gb, 16);
140
-    c->gb    = gb;
141
-
125
+    c->low           = 0;
126
+    c->high          = 0xFFFF;
127
+    c->value         = get_bits(gb, 16);
128
+    c->gbc.gb        = gb;
142 129
     c->get_model_sym = arith_get_model_sym;
143 130
     c->get_number    = arith_get_number;
144 131
 }
145 132
 
146
-static int decode_pal(MSS1Context *ctx, ArithCoder *acoder)
133
+static int decode_pal(MSS12Context *ctx, ArithCoder *acoder)
147 134
 {
148 135
     int i, ncol, r, g, b;
149
-    uint32_t *pal = ctx->ctx.pal + 256 - ctx->ctx.free_colours;
136
+    uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
150 137
 
151
-    if (!ctx->ctx.free_colours)
138
+    if (!ctx->free_colours)
152 139
         return 0;
153 140
 
154
-    ncol = arith_get_number(acoder, ctx->ctx.free_colours + 1);
141
+    ncol = arith_get_number(acoder, ctx->free_colours + 1);
155 142
     for (i = 0; i < ncol; i++) {
156 143
         r = arith_get_bits(acoder, 8);
157 144
         g = arith_get_bits(acoder, 8);
... ...
@@ -167,7 +140,8 @@ static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
167 167
 {
168 168
     const uint8_t *buf = avpkt->data;
169 169
     int buf_size = avpkt->size;
170
-    MSS1Context *c = avctx->priv_data;
170
+    MSS1Context *ctx = avctx->priv_data;
171
+    MSS12Context *c = &ctx->ctx;
171 172
     GetBitContext gb;
172 173
     ArithCoder acoder;
173 174
     int pal_changed = 0;
... ...
@@ -176,37 +150,37 @@ static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
176 176
     init_get_bits(&gb, buf, buf_size * 8);
177 177
     arith_init(&acoder, &gb);
178 178
 
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) {
179
+    ctx->pic.reference    = 3;
180
+    ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
181
+                            FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
182
+    if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {
183 183
         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
184 184
         return ret;
185 185
     }
186 186
 
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;
187
+    c->pal_pic    =  ctx->pic.data[0] + ctx->pic.linesize[0] * (avctx->height - 1);
188
+    c->pal_stride = -ctx->pic.linesize[0];
189
+    c->keyframe   = !arith_get_bit(&acoder);
190
+    if (c->keyframe) {
191
+        ff_mss12_codec_reset(c);
192
+        pal_changed        = decode_pal(c, &acoder);
193
+        ctx->pic.key_frame = 1;
194
+        ctx->pic.pict_type = AV_PICTURE_TYPE_I;
195 195
     } else {
196
-        if (c->ctx.corrupted)
196
+        if (c->corrupted)
197 197
             return AVERROR_INVALIDDATA;
198
-        c->pic.key_frame = 0;
199
-        c->pic.pict_type = AV_PICTURE_TYPE_P;
198
+        ctx->pic.key_frame = 0;
199
+        ctx->pic.pict_type = AV_PICTURE_TYPE_P;
200 200
     }
201
-    c->ctx.corrupted = ff_mss12_decode_rect(&c->ctx, &acoder, 0, 0,
202
-                                            avctx->width, avctx->height);
203
-    if (c->ctx.corrupted)
201
+    c->corrupted = ff_mss12_decode_rect(&c->sc[0], &acoder, 0, 0,
202
+                                        avctx->width, avctx->height);
203
+    if (c->corrupted)
204 204
         return AVERROR_INVALIDDATA;
205
-    memcpy(c->pic.data[1], c->ctx.pal, AVPALETTE_SIZE);
206
-    c->pic.palette_has_changed = pal_changed;
205
+    memcpy(ctx->pic.data[1], c->pal, AVPALETTE_SIZE);
206
+    ctx->pic.palette_has_changed = pal_changed;
207 207
 
208 208
     *data_size = sizeof(AVFrame);
209
-    *(AVFrame*)data = c->pic;
209
+    *(AVFrame*)data = ctx->pic;
210 210
 
211 211
     /* always report that the buffer was completely consumed */
212 212
     return buf_size;
... ...
@@ -219,16 +193,16 @@ static av_cold int mss1_decode_init(AVCodecContext *avctx)
219 219
     c->ctx.avctx       = avctx;
220 220
     avctx->coded_frame = &c->pic;
221 221
 
222
-    return ff_mss12_decode_init(avctx, 0);
222
+    return ff_mss12_decode_init(&c->ctx, 0);
223 223
 }
224 224
 
225 225
 static av_cold int mss1_decode_end(AVCodecContext *avctx)
226 226
 {
227
-    MSS1Context * const c = avctx->priv_data;
227
+    MSS1Context * const ctx = avctx->priv_data;
228 228
 
229
-    if (c->pic.data[0])
230
-        avctx->release_buffer(avctx, &c->pic);
231
-    ff_mss12_decode_end(avctx);
229
+    if (ctx->pic.data[0])
230
+        avctx->release_buffer(avctx, &ctx->pic);
231
+    ff_mss12_decode_end(&ctx->ctx);
232 232
 
233 233
     return 0;
234 234
 }
... ...
@@ -47,12 +47,8 @@ static int model_calc_threshold(Model *m)
47 47
 {
48 48
     int thr;
49 49
 
50
-    if (m->thr_weight == -1) {
51
-        thr = 2 * m->weights[m->num_syms] - 1;
52
-        thr = ((thr >> 1) + 4 * m->cum_prob[0]) / thr;
53
-    } else {
54
-        thr = m->num_syms * m->thr_weight;
55
-    }
50
+    thr = 2 * m->weights[m->num_syms] - 1;
51
+    thr = ((thr >> 1) + 4 * m->cum_prob[0]) / thr;
56 52
 
57 53
     return FFMIN(thr, 0x3FFF);
58 54
 }
... ...
@@ -78,7 +74,7 @@ static av_cold void model_init(Model *m, int num_syms, int thr_weight)
78 78
 {
79 79
     m->num_syms   = num_syms;
80 80
     m->thr_weight = thr_weight;
81
-    m->threshold  = model_calc_threshold(m);
81
+    m->threshold  = num_syms * thr_weight;
82 82
     model_reset(m);
83 83
 }
84 84
 
... ...
@@ -87,7 +83,7 @@ static void model_rescale_weights(Model *m)
87 87
     int i;
88 88
     int cum_prob;
89 89
 
90
-    if (m->thr_weight == -1)
90
+    if (m->thr_weight == THRESH_ADAPTIVE)
91 91
         m->threshold = model_calc_threshold(m);
92 92
     while (m->cum_prob[0] > m->threshold) {
93 93
         cum_prob = 0;
... ...
@@ -129,8 +125,14 @@ static void pixctx_reset(PixContext *ctx)
129 129
 {
130 130
     int i, j, k;
131 131
 
132
-    for (i = 0; i < ctx->cache_size; i++)
133
-        ctx->cache[i] = i;
132
+    if (!ctx->special_initial_cache)
133
+        for (i = 0; i < ctx->cache_size; i++)
134
+            ctx->cache[i] = i;
135
+    else {
136
+        ctx->cache[0] = 1;
137
+        ctx->cache[1] = 2;
138
+        ctx->cache[2] = 4;
139
+    }
134 140
 
135 141
     model_reset(&ctx->cache_model);
136 142
     model_reset(&ctx->full_model);
... ...
@@ -141,27 +143,23 @@ static void pixctx_reset(PixContext *ctx)
141 141
                 model_reset(&ctx->sec_models[i][j][k]);
142 142
 }
143 143
 
144
-static av_cold void pixctx_init(PixContext *ctx, int cache_size)
144
+static av_cold void pixctx_init(PixContext *ctx, int cache_size,
145
+                                int full_model_syms, int special_initial_cache)
145 146
 {
146 147
     int i, j, k;
147 148
 
148
-    ctx->cache_size = cache_size + 4;
149
-    ctx->num_syms   = cache_size;
150
-
151
-    for (i = 0; i < ctx->cache_size; i++)
152
-        ctx->cache[i] = i;
149
+    ctx->cache_size            = cache_size + 4;
150
+    ctx->num_syms              = cache_size;
151
+    ctx->special_initial_cache = special_initial_cache;
153 152
 
154 153
     model_init(&ctx->cache_model, ctx->num_syms + 1, THRESH_LOW);
155
-    model_init(&ctx->full_model, 256, THRESH_HIGH);
154
+    model_init(&ctx->full_model, full_model_syms, THRESH_HIGH);
156 155
 
157
-    for (i = 0; i < 4; i++) {
158
-        for (j = 0; j < sec_order_sizes[i]; j++) {
159
-            for (k = 0; k < 4; k++) {
156
+    for (i = 0; i < 4; i++)
157
+        for (j = 0; j < sec_order_sizes[i]; j++)
158
+            for (k = 0; k < 4; k++)
160 159
                 model_init(&ctx->sec_models[i][j][k], 2 + i,
161 160
                            i ? THRESH_LOW : THRESH_ADAPTIVE);
162
-            }
163
-        }
164
-    }
165 161
 }
166 162
 
167 163
 static int decode_top_left_pixel(ArithCoder *acoder, PixContext *pctx)
... ...
@@ -196,7 +194,6 @@ static int decode_pixel(ArithCoder *acoder, PixContext *pctx,
196 196
     if (val < pctx->num_syms) {
197 197
         int idx, j;
198 198
 
199
-
200 199
         idx = 0;
201 200
         for (i = 0; i < pctx->cache_size; i++) {
202 201
             for (j = 0; j < num_ngb; j++)
... ...
@@ -309,195 +306,288 @@ static int decode_pixel_in_context(ArithCoder *acoder, PixContext *pctx,
309 309
         break;
310 310
     }
311 311
 
312
-    pix = acoder->get_model_sym(acoder, &pctx->sec_models[nlen - 1][layer][sub]);
312
+    pix = acoder->get_model_sym(acoder,
313
+                                &pctx->sec_models[nlen - 1][layer][sub]);
313 314
     if (pix < nlen)
314 315
         return ref_pix[pix];
315 316
     else
316 317
         return decode_pixel(acoder, pctx, ref_pix, nlen);
317 318
 }
318 319
 
319
-static int decode_region(MSS12Context *ctx, ArithCoder *acoder, uint8_t *dst,
320
+static int decode_region(ArithCoder *acoder, uint8_t *dst, uint8_t *rgb_pic,
320 321
                          int x, int y, int width, int height, int stride,
321
-                         PixContext *pctx)
322
+                         int rgb_stride, PixContext *pctx, const uint32_t *pal)
322 323
 {
323
-    int i, j;
324
+    int i, j, p;
325
+    uint8_t *rgb_dst = rgb_pic + x * 3 + y * rgb_stride;
324 326
 
325 327
     dst += x + y * stride;
326 328
 
327
-    dst[0] = decode_top_left_pixel(acoder, pctx);
328 329
     for (j = 0; j < height; j++) {
329 330
         for (i = 0; i < width; i++) {
330 331
             if (!i && !j)
331
-                continue;
332
+                p = decode_top_left_pixel(acoder, pctx);
333
+            else
334
+                p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
335
+                                            i, j, width - i - 1);
336
+            dst[i] = p;
332 337
 
333
-            dst[i] = decode_pixel_in_context(acoder, pctx, dst + i, stride,
334
-                                             i, j, width - i - 1);
338
+            if (rgb_pic)
339
+                AV_WB24(rgb_dst + i * 3, pal[p]);
335 340
         }
336
-        dst += stride;
341
+        dst     += stride;
342
+        rgb_dst += rgb_stride;
337 343
     }
338 344
 
339 345
     return 0;
340 346
 }
341 347
 
342
-static int decode_region_masked(MSS12Context *ctx, ArithCoder *acoder,
348
+static void copy_rectangles(MSS12Context const *c,
349
+                            int x, int y, int width, int height)
350
+{
351
+    int j;
352
+
353
+    if (c->last_rgb_pic)
354
+        for (j = y; j < y + height; j++) {
355
+            memcpy(c->rgb_pic      + j * c->rgb_stride + x * 3,
356
+                   c->last_rgb_pic + j * c->rgb_stride + x * 3,
357
+                   width * 3);
358
+            memcpy(c->pal_pic      + j * c->pal_stride + x,
359
+                   c->last_pal_pic + j * c->pal_stride + x,
360
+                   width);
361
+        }
362
+}
363
+
364
+static int motion_compensation(MSS12Context const *c,
365
+                               int x, int y, int width, int height)
366
+{
367
+    if (x + c->mvX < 0 || x + c->mvX + width  > c->avctx->width  ||
368
+        y + c->mvY < 0 || y + c->mvY + height > c->avctx->height ||
369
+        !c->rgb_pic)
370
+        return -1;
371
+    else {
372
+        uint8_t *dst     = c->pal_pic + x     + y * c->pal_stride;
373
+        uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
374
+        uint8_t *src;
375
+        uint8_t *rgb_src;
376
+        int j;
377
+        x += c->mvX;
378
+        y += c->mvY;
379
+        if (c->last_rgb_pic) {
380
+            src     = c->last_pal_pic + x +     y * c->pal_stride;
381
+            rgb_src = c->last_rgb_pic + x * 3 + y * c->rgb_stride;
382
+        } else {
383
+            src     = c->pal_pic + x     + y * c->pal_stride;
384
+            rgb_src = c->rgb_pic + x * 3 + y * c->rgb_stride;
385
+        }
386
+        for (j = 0; j < height; j++) {
387
+            memmove(dst, src, width);
388
+            memmove(rgb_dst, rgb_src, width * 3);
389
+            dst     += c->pal_stride;
390
+            src     += c->pal_stride;
391
+            rgb_dst += c->rgb_stride;
392
+            rgb_src += c->rgb_stride;
393
+        }
394
+    }
395
+    return 0;
396
+}
397
+
398
+static int decode_region_masked(MSS12Context const *c, ArithCoder *acoder,
343 399
                                 uint8_t *dst, int stride, uint8_t *mask,
344 400
                                 int mask_stride, int x, int y,
345 401
                                 int width, int height,
346 402
                                 PixContext *pctx)
347 403
 {
348
-    int i, j;
404
+    int i, j, p;
405
+    uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
349 406
 
350 407
     dst  += x + y * stride;
351 408
     mask += x + y * mask_stride;
352 409
 
353
-    if (mask[0] == 0xFF)
354
-        dst[0] = decode_top_left_pixel(acoder, pctx);
355 410
     for (j = 0; j < height; j++) {
356 411
         for (i = 0; i < width; i++) {
357
-            if (!i && !j || mask[i] != 0xFF)
358
-                continue;
359
-
360
-            dst[i] = decode_pixel_in_context(acoder, pctx, dst + i, stride,
361
-                                             i, j, width - i - 1);
412
+            if (c->avctx->err_recognition & AV_EF_EXPLODE &&
413
+                ( c->rgb_pic && mask[i] != 0x01 && mask[i] != 0x02 && mask[i] != 0x04 ||
414
+                 !c->rgb_pic && mask[i] != 0x80 && mask[i] != 0xFF))
415
+                return -1;
416
+
417
+            if (mask[i] == 0x02) {
418
+                copy_rectangles(c, x + i, y + j, 1, 1);
419
+            } else if (mask[i] == 0x04) {
420
+                if (motion_compensation(c, x + i, y + j, 1, 1))
421
+                    return -1;
422
+            } else if (mask[i] != 0x80) {
423
+                if (!i && !j)
424
+                    p = decode_top_left_pixel(acoder, pctx);
425
+                else
426
+                    p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
427
+                                                i, j, width - i - 1);
428
+                dst[i] = p;
429
+                if (c->rgb_pic)
430
+                    AV_WB24(rgb_dst + i * 3, c->pal[p]);
431
+            }
362 432
         }
363
-        dst  += stride;
364
-        mask += mask_stride;
433
+        dst     += stride;
434
+        mask    += mask_stride;
435
+        rgb_dst += c->rgb_stride;
365 436
     }
366 437
 
367 438
     return 0;
368 439
 }
369 440
 
370
-static av_cold void codec_init(MSS12Context *ctx)
441
+static av_cold void codec_init(MSS12Context *c, int version)
371 442
 {
372
-    model_init(&ctx->intra_region, 2, THRESH_ADAPTIVE);
373
-    model_init(&ctx->inter_region, 2, THRESH_ADAPTIVE);
374
-    model_init(&ctx->split_mode,   3, THRESH_HIGH);
375
-    model_init(&ctx->edge_mode,    2, THRESH_HIGH);
376
-    model_init(&ctx->pivot,        3, THRESH_LOW);
377
-    pixctx_init(&ctx->intra_pix_ctx, 8);
378
-    pixctx_init(&ctx->inter_pix_ctx, 2);
379
-    ctx->corrupted = 1;
443
+    int i;
444
+    for (i = 0; i < (c->slice_split ? 2 : 1); i++) {
445
+        c->sc[i].c = c;
446
+        model_init(&c->sc[i].intra_region, 2, THRESH_ADAPTIVE);
447
+        model_init(&c->sc[i].inter_region, 2, THRESH_ADAPTIVE);
448
+        model_init(&c->sc[i].split_mode,   3, THRESH_HIGH);
449
+        model_init(&c->sc[i].edge_mode,    2, THRESH_HIGH);
450
+        model_init(&c->sc[i].pivot,        3, THRESH_LOW);
451
+
452
+        pixctx_init(&c->sc[i].intra_pix_ctx, 8, c->full_model_syms, 0);
453
+
454
+        pixctx_init(&c->sc[i].inter_pix_ctx, version ? 3 : 2,
455
+                    c->full_model_syms, version ? 1 : 0);
456
+    }
457
+    c->corrupted = 1;
380 458
 }
381 459
 
382
-void ff_mss12_codec_reset(MSS12Context *ctx)
460
+void ff_mss12_codec_reset(MSS12Context *c)
383 461
 {
384
-    model_reset(&ctx->intra_region);
385
-    model_reset(&ctx->inter_region);
386
-    model_reset(&ctx->split_mode);
387
-    model_reset(&ctx->edge_mode);
388
-    model_reset(&ctx->pivot);
389
-    pixctx_reset(&ctx->intra_pix_ctx);
390
-    pixctx_reset(&ctx->inter_pix_ctx);
391
-
392
-    ctx->corrupted = 0;
462
+    int i;
463
+    for (i = 0; i < (c->slice_split ? 2 : 1); i++) {
464
+        model_reset(&c->sc[i].intra_region);
465
+        model_reset(&c->sc[i].inter_region);
466
+        model_reset(&c->sc[i].split_mode);
467
+        model_reset(&c->sc[i].edge_mode);
468
+        model_reset(&c->sc[i].pivot);
469
+        pixctx_reset(&c->sc[i].intra_pix_ctx);
470
+        pixctx_reset(&c->sc[i].inter_pix_ctx);
471
+    }
472
+
473
+    c->corrupted = 0;
393 474
 }
394 475
 
395
-static int decode_pivot(MSS12Context *ctx, ArithCoder *acoder, int base)
476
+static int decode_pivot(SliceContext *sc, ArithCoder *acoder, int base)
396 477
 {
397 478
     int val, inv;
398 479
 
399
-    inv = acoder->get_model_sym(acoder, &ctx->edge_mode);
400
-    val = acoder->get_model_sym(acoder, &ctx->pivot) + 1;
480
+    inv = acoder->get_model_sym(acoder, &sc->edge_mode);
481
+    val = acoder->get_model_sym(acoder, &sc->pivot) + 1;
401 482
 
402 483
     if (val > 2) {
403
-        if ((base + 1) / 2 - 2 <= 0) {
404
-            ctx->corrupted = 1;
405
-            return 0;
406
-        }
484
+        if ((base + 1) / 2 - 2 <= 0)
485
+            return -1;
486
+
407 487
         val = acoder->get_number(acoder, (base + 1) / 2 - 2) + 3;
408 488
     }
409 489
 
410
-    if (val == base) {
411
-        ctx->corrupted = 1;
412
-        return 0;
413
-    }
490
+    if (val >= base)
491
+        return -1;
414 492
 
415 493
     return inv ? base - val : val;
416 494
 }
417 495
 
418
-static int decode_region_intra(MSS12Context *ctx, ArithCoder *acoder,
496
+static int decode_region_intra(SliceContext *sc, ArithCoder *acoder,
419 497
                                int x, int y, int width, int height)
420 498
 {
499
+    MSS12Context const *c = sc->c;
421 500
     int mode;
422 501
 
423
-    mode = acoder->get_model_sym(acoder, &ctx->intra_region);
502
+    mode = acoder->get_model_sym(acoder, &sc->intra_region);
424 503
 
425 504
     if (!mode) {
426
-        int i, pix;
427
-        int stride = ctx->pic_stride;
428
-        uint8_t *dst = ctx->pic_start + x + y * stride;
429
-
430
-        pix = decode_top_left_pixel(acoder, &ctx->intra_pix_ctx);
431
-        for (i = 0; i < height; i++, dst += stride)
505
+        int i, j, pix, rgb_pix;
506
+        int stride       = c->pal_stride;
507
+        int rgb_stride   = c->rgb_stride;
508
+        uint8_t *dst     = c->pal_pic + x     + y * stride;
509
+        uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * rgb_stride;
510
+
511
+        pix     = decode_top_left_pixel(acoder, &sc->intra_pix_ctx);
512
+        rgb_pix = c->pal[pix];
513
+        for (i = 0; i < height; i++, dst += stride, rgb_dst += rgb_stride) {
432 514
             memset(dst, pix, width);
515
+            if (c->rgb_pic)
516
+                for (j = 0; j < width * 3; j += 3)
517
+                    AV_WB24(rgb_dst + j, rgb_pix);
518
+        }
433 519
     } else {
434
-        return decode_region(ctx, acoder, ctx->pic_start,
435
-                             x, y, width, height, ctx->pic_stride,
436
-                             &ctx->intra_pix_ctx);
520
+        return decode_region(acoder, c->pal_pic, c->rgb_pic,
521
+                             x, y, width, height, c->pal_stride, c->rgb_stride,
522
+                             &sc->intra_pix_ctx, &c->pal[0]);
437 523
     }
438 524
 
439 525
     return 0;
440 526
 }
441 527
 
442
-static int decode_region_inter(MSS12Context *ctx, ArithCoder *acoder,
528
+static int decode_region_inter(SliceContext *sc, ArithCoder *acoder,
443 529
                                int x, int y, int width, int height)
444 530
 {
531
+    MSS12Context const *c = sc->c;
445 532
     int mode;
446 533
 
447
-    mode = acoder->get_model_sym(acoder, &ctx->inter_region);
534
+    mode = acoder->get_model_sym(acoder, &sc->inter_region);
448 535
 
449 536
     if (!mode) {
450
-        mode = decode_top_left_pixel(acoder, &ctx->inter_pix_ctx);
451
-        if (mode != 0xFF) {
452
-            return 0;
453
-        } else {
454
-            return decode_region_intra(ctx, acoder, x, y, width, height);
455
-        }
537
+        mode = decode_top_left_pixel(acoder, &sc->inter_pix_ctx);
538
+
539
+        if (c->avctx->err_recognition & AV_EF_EXPLODE &&
540
+            ( c->rgb_pic && mode != 0x01 && mode != 0x02 && mode != 0x04 ||
541
+             !c->rgb_pic && mode != 0x80 && mode != 0xFF))
542
+            return -1;
543
+
544
+        if (mode == 0x02)
545
+            copy_rectangles(c, x, y, width, height);
546
+        else if (mode == 0x04)
547
+            return motion_compensation(c, x, y, width, height);
548
+        else if (mode != 0x80)
549
+            return decode_region_intra(sc, acoder, x, y, width, height);
456 550
     } else {
457
-        if (decode_region(ctx, acoder, ctx->mask,
458
-                          x, y, width, height, ctx->mask_linesize,
459
-                          &ctx->inter_pix_ctx) < 0)
551
+        if (decode_region(acoder, c->mask, NULL,
552
+                          x, y, width, height, c->mask_stride, 0,
553
+                          &sc->inter_pix_ctx, &c->pal[0]) < 0)
460 554
             return -1;
461
-        return decode_region_masked(ctx, acoder, ctx->pic_start,
462
-                                    ctx->pic_stride, ctx->mask,
463
-                                    ctx->mask_linesize,
555
+        return decode_region_masked(c, acoder, c->pal_pic,
556
+                                    c->pal_stride, c->mask,
557
+                                    c->mask_stride,
464 558
                                     x, y, width, height,
465
-                                    &ctx->intra_pix_ctx);
559
+                                    &sc->intra_pix_ctx);
466 560
     }
467 561
 
468 562
     return 0;
469 563
 }
470 564
 
471
-int ff_mss12_decode_rect(MSS12Context *ctx, ArithCoder *acoder,
565
+int ff_mss12_decode_rect(SliceContext *sc, ArithCoder *acoder,
472 566
                          int x, int y, int width, int height)
473 567
 {
474 568
     int mode, pivot;
475 569
 
476
-    if (ctx->corrupted)
477
-        return -1;
478
-
479
-    mode = acoder->get_model_sym(acoder, &ctx->split_mode);
570
+    mode = acoder->get_model_sym(acoder, &sc->split_mode);
480 571
 
481 572
     switch (mode) {
482 573
     case SPLIT_VERT:
483
-        pivot = decode_pivot(ctx, acoder, height);
484
-        if (ff_mss12_decode_rect(ctx, acoder, x, y, width, pivot))
574
+        if ((pivot = decode_pivot(sc, acoder, height)) < 1)
485 575
             return -1;
486
-        if (ff_mss12_decode_rect(ctx, acoder, x, y + pivot, width, height - pivot))
576
+        if (ff_mss12_decode_rect(sc, acoder, x, y, width, pivot))
577
+            return -1;
578
+        if (ff_mss12_decode_rect(sc, acoder, x, y + pivot, width, height - pivot))
487 579
             return -1;
488 580
         break;
489 581
     case SPLIT_HOR:
490
-        pivot = decode_pivot(ctx, acoder, width);
491
-        if (ff_mss12_decode_rect(ctx, acoder, x, y, pivot, height))
582
+        if ((pivot = decode_pivot(sc, acoder, width)) < 1)
583
+            return -1;
584
+        if (ff_mss12_decode_rect(sc, acoder, x, y, pivot, height))
492 585
             return -1;
493
-        if (ff_mss12_decode_rect(ctx, acoder, x + pivot, y, width - pivot, height))
586
+        if (ff_mss12_decode_rect(sc, acoder, x + pivot, y, width - pivot, height))
494 587
             return -1;
495 588
         break;
496 589
     case SPLIT_NONE:
497
-        if (ctx->keyframe)
498
-            return decode_region_intra(ctx, acoder, x, y, width, height);
590
+        if (sc->c->keyframe)
591
+            return decode_region_intra(sc, acoder, x, y, width, height);
499 592
         else
500
-            return decode_region_inter(ctx, acoder, x, y, width, height);
593
+            return decode_region_inter(sc, acoder, x, y, width, height);
501 594
     default:
502 595
         return -1;
503 596
     }
... ...
@@ -505,13 +595,11 @@ int ff_mss12_decode_rect(MSS12Context *ctx, ArithCoder *acoder,
505 505
     return 0;
506 506
 }
507 507
 
508
-av_cold int ff_mss12_decode_init(AVCodecContext *avctx, int version)
508
+av_cold int ff_mss12_decode_init(MSS12Context *c, int version)
509 509
 {
510
-    MSS12Context * const c = avctx->priv_data;
510
+    AVCodecContext *avctx = c->avctx;
511 511
     int i;
512 512
 
513
-    c->avctx = avctx;
514
-
515 513
     if (avctx->extradata_size < 52 + 256 * 3) {
516 514
         av_log(avctx, AV_LOG_ERROR, "Insufficient extradata size %d\n",
517 515
                avctx->extradata_size);
... ...
@@ -526,9 +614,23 @@ av_cold int ff_mss12_decode_init(AVCodecContext *avctx, int version)
526 526
         return AVERROR_INVALIDDATA;
527 527
     }
528 528
 
529
+    avctx->coded_width  = AV_RB32(avctx->extradata + 20);
530
+    avctx->coded_height = AV_RB32(avctx->extradata + 24);
531
+    if (avctx->coded_width > 4096 || avctx->coded_height > 4096) {
532
+        av_log(avctx, AV_LOG_ERROR, "Frame dimensions %dx%d too large",
533
+               avctx->coded_width, avctx->coded_height);
534
+        return AVERROR_INVALIDDATA;
535
+    }
536
+
529 537
     av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d\n",
530 538
            AV_RB32(avctx->extradata + 4), AV_RB32(avctx->extradata + 8));
531
-    c->free_colours     = AV_RB32(avctx->extradata + 48);
539
+    if (version != AV_RB32(avctx->extradata + 4) > 1) {
540
+        av_log(avctx, AV_LOG_ERROR,
541
+               "Header version doesn't match codec tag\n");
542
+        return -1;
543
+    }
544
+
545
+    c->free_colours = AV_RB32(avctx->extradata + 48);
532 546
     if ((unsigned)c->free_colours > 256) {
533 547
         av_log(avctx, AV_LOG_ERROR,
534 548
                "Incorrect number of changeable palette entries: %d\n",
... ...
@@ -536,8 +638,6 @@ av_cold int ff_mss12_decode_init(AVCodecContext *avctx, int version)
536 536
         return AVERROR_INVALIDDATA;
537 537
     }
538 538
     av_log(avctx, AV_LOG_DEBUG, "%d free colour(s)\n", c->free_colours);
539
-    avctx->coded_width  = AV_RB32(avctx->extradata + 20);
540
-    avctx->coded_height = AV_RB32(avctx->extradata + 24);
541 539
 
542 540
     av_log(avctx, AV_LOG_DEBUG, "Display dimensions %dx%d\n",
543 541
            AV_RB32(avctx->extradata + 12), AV_RB32(avctx->extradata + 16));
... ...
@@ -554,27 +654,53 @@ av_cold int ff_mss12_decode_init(AVCodecContext *avctx, int version)
554 554
     av_log(avctx, AV_LOG_DEBUG, "Max. seek time %g ms\n",
555 555
            av_int2float(AV_RB32(avctx->extradata + 44)));
556 556
 
557
-    for (i = 0; i < 256; i++)
558
-        c->pal[i] = AV_RB24(avctx->extradata + 52 + i * 3);
557
+    if (version) {
558
+        if (avctx->extradata_size < 60 + 256 * 3) {
559
+            av_log(avctx, AV_LOG_ERROR,
560
+                   "Insufficient extradata size %d for v2\n",
561
+                   avctx->extradata_size);
562
+            return AVERROR_INVALIDDATA;
563
+        }
564
+
565
+        c->slice_split = AV_RB32(avctx->extradata + 52);
566
+        av_log(avctx, AV_LOG_DEBUG, "Slice split %d\n", c->slice_split);
559 567
 
560
-    avctx->pix_fmt = PIX_FMT_PAL8;
568
+        c->full_model_syms = AV_RB32(avctx->extradata + 56);
569
+        if (c->full_model_syms < 2 || c->full_model_syms > 256) {
570
+            av_log(avctx, AV_LOG_ERROR,
571
+                   "Incorrect number of used colours %d\n",
572
+                   c->full_model_syms);
573
+            return AVERROR_INVALIDDATA;
574
+        }
575
+        av_log(avctx, AV_LOG_DEBUG, "Used colours %d\n",
576
+               c->full_model_syms);
577
+    } else {
578
+        c->slice_split     = 0;
579
+        c->full_model_syms = 256;
580
+    }
561 581
 
562
-    c->mask_linesize = FFALIGN(avctx->width, 16);
563
-    c->mask          = av_malloc(c->mask_linesize * avctx->height);
582
+    for (i = 0; i < 256; i++)
583
+        c->pal[i] = AV_RB24(avctx->extradata + 52 +
584
+                            (version ? 8 : 0) + i * 3);
585
+
586
+    c->mask_stride = FFALIGN(avctx->width, 16);
587
+    c->mask        = av_malloc(c->mask_stride * avctx->height);
564 588
     if (!c->mask) {
565 589
         av_log(avctx, AV_LOG_ERROR, "Cannot allocate mask plane\n");
566 590
         return AVERROR(ENOMEM);
567 591
     }
568 592
 
569
-    codec_init(c);
593
+    avctx->pix_fmt = version ? c->free_colours == 127 ? PIX_FMT_RGB555
594
+                                                      : PIX_FMT_RGB24
595
+                             : PIX_FMT_PAL8;
596
+
597
+    codec_init(c, version);
570 598
 
571 599
     return 0;
572 600
 }
573 601
 
574
-av_cold int ff_mss12_decode_end(AVCodecContext *avctx)
602
+av_cold int ff_mss12_decode_end(MSS12Context *c)
575 603
 {
576
-    MSS12Context * const c = avctx->priv_data;
577
-
578 604
     av_freep(&c->mask);
579 605
 
580 606
     return 0;
... ...
@@ -26,8 +26,10 @@
26 26
 #ifndef AVCODEC_MSS12_H
27 27
 #define AVCODEC_MSS12_H
28 28
 
29
+#include "libavutil/intreadwrite.h"
29 30
 #include "avcodec.h"
30 31
 #include "get_bits.h"
32
+#include "bytestream.h"
31 33
 
32 34
 #define MODEL_MIN_SYMS    2
33 35
 #define MODEL_MAX_SYMS  256
... ...
@@ -46,7 +48,10 @@ typedef struct Model {
46 46
 
47 47
 typedef struct ArithCoder {
48 48
     int low, high, value;
49
-    GetBitContext *gb;
49
+    union {
50
+        GetBitContext *gb;
51
+        GetByteContext *gB;
52
+    } gbc;
50 53
     int (*get_model_sym)(struct ArithCoder *c, Model *m);
51 54
     int (*get_number)   (struct ArithCoder *c, int n);
52 55
 } ArithCoder;
... ...
@@ -56,28 +61,77 @@ typedef struct PixContext {
56 56
     uint8_t cache[12];
57 57
     Model cache_model, full_model;
58 58
     Model sec_models[4][8][4];
59
+    int special_initial_cache;
59 60
 } PixContext;
60 61
 
62
+struct MSS12Context;
63
+
64
+typedef struct SliceContext {
65
+    struct MSS12Context *c;
66
+    Model      intra_region, inter_region;
67
+    Model      pivot, edge_mode, split_mode;
68
+    PixContext intra_pix_ctx, inter_pix_ctx;
69
+} SliceContext;
70
+
61 71
 typedef struct MSS12Context {
62 72
     AVCodecContext *avctx;
63
-    uint8_t        *pic_start;
64
-    int            pic_stride;
65
-    uint8_t        *mask;
66
-    int            mask_linesize;
67 73
     uint32_t       pal[256];
74
+    uint8_t        *pal_pic;
75
+    uint8_t        *last_pal_pic;
76
+    int            pal_stride;
77
+    uint8_t        *mask;
78
+    int            mask_stride;
79
+    uint8_t        *rgb_pic;
80
+    uint8_t        *last_rgb_pic;
81
+    int            rgb_stride;
68 82
     int            free_colours;
69 83
     int            keyframe;
70 84
     Model          intra_region, inter_region;
71 85
     Model          pivot, edge_mode, split_mode;
72 86
     PixContext     intra_pix_ctx, inter_pix_ctx;
87
+    int            mvX, mvY;
73 88
     int            corrupted;
89
+    int            slice_split;
90
+    int            full_model_syms;
91
+    SliceContext   sc[2];
74 92
 } MSS12Context;
75 93
 
76
-int ff_mss12_decode_rect(MSS12Context *ctx, ArithCoder *acoder,
94
+int ff_mss12_decode_rect(SliceContext *ctx, ArithCoder *acoder,
77 95
                          int x, int y, int width, int height);
78 96
 void ff_mss12_model_update(Model *m, int val);
79 97
 void ff_mss12_codec_reset(MSS12Context *ctx);
80
-av_cold int ff_mss12_decode_init(AVCodecContext *avctx, int version);
81
-av_cold int ff_mss12_decode_end(AVCodecContext *avctx);
98
+av_cold int ff_mss12_decode_init(MSS12Context *ctx, int version);
99
+av_cold int ff_mss12_decode_end(MSS12Context *ctx);
100
+
101
+#define ARITH_GET_BIT(VERSION)                                          \
102
+static int arith ## VERSION ## _get_bit(ArithCoder *c)                  \
103
+{                                                                       \
104
+    int range = c->high - c->low + 1;                                   \
105
+    int bit   = (((c->value - c->low) << 1) + 1) / range;               \
106
+                                                                        \
107
+    if (bit)                                                            \
108
+        c->low += range >> 1;                                           \
109
+    else                                                                \
110
+        c->high = c->low + (range >> 1) - 1;                            \
111
+                                                                        \
112
+    arith ## VERSION ## _normalise(c);                                  \
113
+                                                                        \
114
+    return bit;                                                         \
115
+}
116
+
117
+#define ARITH_GET_MODEL_SYM(VERSION)                                    \
118
+static int arith ## VERSION ## _get_model_sym(ArithCoder *c, Model *m)  \
119
+{                                                                       \
120
+    int idx, val;                                                       \
121
+                                                                        \
122
+    idx = arith ## VERSION ## _get_prob(c, m->cum_prob);                \
123
+                                                                        \
124
+    val = m->idx2sym[idx];                                              \
125
+    ff_mss12_model_update(m, idx);                                      \
126
+                                                                        \
127
+    arith ## VERSION ## _normalise(c);                                  \
128
+                                                                        \
129
+    return val;                                                         \
130
+}
82 131
 
83 132
 #endif /* AVCODEC_MSS12_H */
84 133
new file mode 100644
... ...
@@ -0,0 +1,860 @@
0
+/*
1
+ * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder
2
+ *
3
+ * This file is part of Libav.
4
+ *
5
+ * Libav is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * Libav is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with Libav; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder
23
+ */
24
+
25
+#include "libavutil/avassert.h"
26
+#include "msmpeg4data.h"
27
+#include "vc1.h"
28
+#include "mss12.h"
29
+#include "mss2dsp.h"
30
+
31
+typedef struct MSS2Context {
32
+    VC1Context     v;
33
+    int            split_position;
34
+    AVFrame        pic;
35
+    AVFrame        last_pic;
36
+    MSS12Context   c;
37
+    MSS2DSPContext dsp;
38
+    SliceContext   sc[2];
39
+} MSS2Context;
40
+
41
+static void arith2_normalise(ArithCoder *c)
42
+{
43
+    while ((c->high >> 15) - (c->low >> 15) < 2) {
44
+        if ((c->low ^ c->high) & 0x10000) {
45
+            c->high  ^= 0x8000;
46
+            c->value ^= 0x8000;
47
+            c->low   ^= 0x8000;
48
+        }
49
+        c->high  = c->high  << 8 & 0xFFFFFF | 0xFF;
50
+        c->value = c->value << 8 & 0xFFFFFF | bytestream2_get_byte(c->gbc.gB);
51
+        c->low   = c->low   << 8 & 0xFFFFFF;
52
+    }
53
+}
54
+
55
+ARITH_GET_BIT(2)
56
+
57
+/* L. Stuiver and A. Moffat: "Piecewise Integer Mapping for Arithmetic Coding."
58
+ * In Proc. 8th Data Compression Conference (DCC '98), pp. 3-12, Mar. 1998 */
59
+
60
+static int arith2_get_scaled_value(int value, int n, int range)
61
+{
62
+    int split = (n << 1) - range;
63
+
64
+    if (value > split)
65
+        return split + (value - split >> 1);
66
+    else
67
+        return value;
68
+}
69
+
70
+static void arith2_rescale_interval(ArithCoder *c, int range,
71
+                                    int low, int high, int n)
72
+{
73
+    int split = (n << 1) - range;
74
+
75
+    if (high > split)
76
+        c->high = split + (high - split << 1);
77
+    else
78
+        c->high = high;
79
+
80
+    c->high += c->low - 1;
81
+
82
+    if (low > split)
83
+        c->low += split + (low - split << 1);
84
+    else
85
+        c->low += low;
86
+}
87
+
88
+static int arith2_get_number(ArithCoder *c, int n)
89
+{
90
+    int range = c->high - c->low + 1;
91
+    int scale = av_log2(range) - av_log2(n);
92
+    int val;
93
+
94
+    if (n << scale > range)
95
+        scale--;
96
+
97
+    n <<= scale;
98
+
99
+    val = arith2_get_scaled_value(c->value - c->low, n, range) >> scale;
100
+
101
+    arith2_rescale_interval(c, range, val << scale, (val + 1) << scale, n);
102
+
103
+    arith2_normalise(c);
104
+
105
+    return val;
106
+}
107
+
108
+static int arith2_get_prob(ArithCoder *c, int *probs)
109
+{
110
+    int range = c->high - c->low + 1, n = *probs;
111
+    int scale = av_log2(range) - av_log2(n);
112
+    int i     = 0, val;
113
+
114
+    if (n << scale > range)
115
+        scale--;
116
+
117
+    n <<= scale;
118
+
119
+    val = arith2_get_scaled_value(c->value - c->low, n, range) >> scale;
120
+    while (probs[++i] > val) ;
121
+
122
+    arith2_rescale_interval(c, range,
123
+                            probs[i] << scale, probs[i - 1] << scale, n);
124
+
125
+    return i;
126
+}
127
+
128
+ARITH_GET_MODEL_SYM(2)
129
+
130
+static int arith2_get_consumed_bytes(ArithCoder *c)
131
+{
132
+    int diff = (c->high >> 16) - (c->low >> 16);
133
+    int bp   = bytestream2_tell(c->gbc.gB) - 3 << 3;
134
+    int bits = 1;
135
+
136
+    while (!(diff & 0x80)) {
137
+        bits++;
138
+        diff <<= 1;
139
+    }
140
+
141
+    return (bits + bp + 7 >> 3) + ((c->low >> 16) + 1 == c->high >> 16);
142
+}
143
+
144
+static void arith2_init(ArithCoder *c, GetByteContext *gB)
145
+{
146
+    c->low           = 0;
147
+    c->high          = 0xFFFFFF;
148
+    c->value         = bytestream2_get_be24(gB);
149
+    c->gbc.gB        = gB;
150
+    c->get_model_sym = arith2_get_model_sym;
151
+    c->get_number    = arith2_get_number;
152
+}
153
+
154
+static int decode_pal_v2(MSS12Context *ctx, const uint8_t *buf, int buf_size)
155
+{
156
+    int i, ncol;
157
+    uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
158
+
159
+    if (!ctx->free_colours)
160
+        return 0;
161
+
162
+    ncol = *buf++;
163
+    if (buf_size < 2 + ncol * 3)
164
+        return -1;
165
+    for (i = 0; i < ncol; i++)
166
+        *pal++ = AV_RB24(buf + 3 * i);
167
+
168
+    return 1 + ncol * 3;
169
+}
170
+
171
+static int decode_555(GetByteContext *gB, uint16_t *dst, int stride,
172
+                      int keyframe, int w, int h)
173
+{
174
+    int last_symbol = 0, repeat = 0, prev_avail = 0;
175
+
176
+    if (!keyframe) {
177
+        int x, y, endx, endy, t;
178
+
179
+#define READ_PAIR(a, b)                 \
180
+    a  = bytestream2_get_byte(gB) << 4; \
181
+    t  = bytestream2_get_byte(gB);      \
182
+    a |= t >> 4;                        \
183
+    b  = (t & 0xF) << 8;                \
184
+    b |= bytestream2_get_byte(gB);      \
185
+
186
+        READ_PAIR(x, endx)
187
+        READ_PAIR(y, endy)
188
+
189
+        if (endx >= w || endy >= h || x > endx || y > endy)
190
+            return -1;
191
+        dst += x + stride * y;
192
+        w    = endx - x + 1;
193
+        h    = endy - y + 1;
194
+        if (y)
195
+            prev_avail = 1;
196
+    }
197
+
198
+    do {
199
+        uint16_t *p = dst;
200
+        do {
201
+            if (repeat-- < 1) {
202
+                int b = bytestream2_get_byte(gB);
203
+                if (b < 128)
204
+                    last_symbol = b << 8 | bytestream2_get_byte(gB);
205
+                else if (b > 129) {
206
+                    repeat = 0;
207
+                    while (b-- > 130)
208
+                        repeat = (repeat << 8) + bytestream2_get_byte(gB) + 1;
209
+                    if (last_symbol == -2) {
210
+                        int skip = FFMIN((unsigned)repeat, dst + w - p);
211
+                        repeat -= skip;
212
+                        p      += skip;
213
+                    }
214
+                } else
215
+                    last_symbol = 127 - b;
216
+            }
217
+            if (last_symbol >= 0)
218
+                *p = last_symbol;
219
+            else if (last_symbol == -1 && prev_avail)
220
+                *p = *(p - stride);
221
+        } while (++p < dst + w);
222
+        dst       += stride;
223
+        prev_avail = 1;
224
+    } while (--h);
225
+
226
+    return 0;
227
+}
228
+
229
+static int decode_rle(GetBitContext *gb, uint8_t *pal_dst, int pal_stride,
230
+                      uint8_t *rgb_dst, int rgb_stride, uint32_t *pal,
231
+                      int keyframe, int kf_slipt, int slice, int w, int h)
232
+{
233
+    uint8_t bits[270] = { 0 };
234
+    uint32_t codes[270];
235
+    VLC vlc;
236
+
237
+    int current_length = 0, read_codes = 0, next_code = 0, current_codes = 0;
238
+    int remaining_codes, surplus_codes, i;
239
+
240
+    const int alphabet_size = 270 - keyframe;
241
+
242
+    int last_symbol = 0, repeat = 0, prev_avail = 0;
243
+
244
+    if (!keyframe) {
245
+        int x, y, clipw, cliph;
246
+
247
+        x     = get_bits(gb, 12);
248
+        y     = get_bits(gb, 12);
249
+        clipw = get_bits(gb, 12) + 1;
250
+        cliph = get_bits(gb, 12) + 1;
251
+
252
+        if (x + clipw > w || y + cliph > h)
253
+            return AVERROR_INVALIDDATA;
254
+        pal_dst += pal_stride * y + x;
255
+        rgb_dst += rgb_stride * y + x * 3;
256
+        w        = clipw;
257
+        h        = cliph;
258
+        if (y)
259
+            prev_avail = 1;
260
+    } else {
261
+        if (slice > 0) {
262
+            pal_dst   += pal_stride * kf_slipt;
263
+            rgb_dst   += rgb_stride * kf_slipt;
264
+            prev_avail = 1;
265
+            h         -= kf_slipt;
266
+        } else
267
+            h = kf_slipt;
268
+    }
269
+
270
+    /* read explicit codes */
271
+    do {
272
+        while (current_codes--) {
273
+            int symbol = get_bits(gb, 8);
274
+            if (symbol >= 204 - keyframe)
275
+                symbol += 14 - keyframe;
276
+            else if (symbol > 189)
277
+                symbol = get_bits1(gb) + (symbol << 1) - 190;
278
+            if (bits[symbol])
279
+                return AVERROR_INVALIDDATA;
280
+            bits[symbol]  = current_length;
281
+            codes[symbol] = next_code++;
282
+            read_codes++;
283
+        }
284
+        current_length++;
285
+        next_code     <<= 1;
286
+        remaining_codes = (1 << current_length) - next_code;
287
+        current_codes   = get_bits(gb, av_ceil_log2(remaining_codes + 1));
288
+        if (current_length > 22 || current_codes > remaining_codes)
289
+            return AVERROR_INVALIDDATA;
290
+    } while (current_codes != remaining_codes);
291
+
292
+    remaining_codes = alphabet_size - read_codes;
293
+
294
+    /* determine the minimum length to fit the rest of the alphabet */
295
+    while ((surplus_codes = (2 << current_length) -
296
+                            (next_code << 1) - remaining_codes) < 0) {
297
+        current_length++;
298
+        next_code <<= 1;
299
+    }
300
+
301
+    /* add the rest of the symbols lexicographically */
302
+    for (i = 0; i < alphabet_size; i++)
303
+        if (!bits[i]) {
304
+            if (surplus_codes-- == 0) {
305
+                current_length++;
306
+                next_code <<= 1;
307
+            }
308
+            bits[i]  = current_length;
309
+            codes[i] = next_code++;
310
+        }
311
+
312
+    if (next_code != 1 << current_length)
313
+        return AVERROR_INVALIDDATA;
314
+
315
+    if (i = init_vlc(&vlc, 9, alphabet_size, bits, 1, 1, codes, 4, 4, 0))
316
+        return i;
317
+
318
+    /* frame decode */
319
+    do {
320
+        uint8_t *pp = pal_dst;
321
+        uint8_t *rp = rgb_dst;
322
+        do {
323
+            if (repeat-- < 1) {
324
+                int b = get_vlc2(gb, vlc.table, 9, 3);
325
+                if (b < 256)
326
+                    last_symbol = b;
327
+                else if (b < 268) {
328
+                    b -= 256;
329
+                    if (b == 11)
330
+                        b = get_bits(gb, 4) + 10;
331
+
332
+                    if (!b)
333
+                        repeat = 0;
334
+                    else
335
+                        repeat = get_bits(gb, b);
336
+
337
+                    while (b--)
338
+                        repeat += 1 << b;
339
+
340
+                    if (last_symbol == -2) {
341
+                        int skip = FFMIN(repeat, pal_dst + w - pp);
342
+                        repeat -= skip;
343
+                        pp     += skip;
344
+                        rp     += skip * 3;
345
+                    }
346
+                } else
347
+                    last_symbol = 267 - b;
348
+            }
349
+            if (last_symbol >= 0) {
350
+                *pp = last_symbol;
351
+                AV_WB24(rp, pal[last_symbol]);
352
+            } else if (last_symbol == -1 && prev_avail) {
353
+                *pp = *(pp - pal_stride);
354
+                memcpy(rp, rp - rgb_stride, 3);
355
+            }
356
+            rp += 3;
357
+        } while (++pp < pal_dst + w);
358
+        pal_dst   += pal_stride;
359
+        rgb_dst   += rgb_stride;
360
+        prev_avail = 1;
361
+    } while (--h);
362
+
363
+    ff_free_vlc(&vlc);
364
+    return 0;
365
+}
366
+
367
+static int decode_wmv9(AVCodecContext *avctx, const uint8_t *buf, int buf_size,
368
+                       int x, int y, int w, int h, int wmv9_mask)
369
+{
370
+    MSS2Context *ctx  = avctx->priv_data;
371
+    MSS12Context *c   = &ctx->c;
372
+    VC1Context *v     = avctx->priv_data;
373
+    MpegEncContext *s = &v->s;
374
+    AVFrame *f;
375
+
376
+    ff_mpeg_flush(avctx);
377
+
378
+    if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
379
+        int i = ff_find_unused_picture(s, 0);
380
+        if (i < 0)
381
+            return -1;
382
+        s->current_picture_ptr = &s->picture[i];
383
+    }
384
+
385
+    init_get_bits(&s->gb, buf, buf_size * 8);
386
+
387
+    s->loop_filter = avctx->skip_loop_filter < AVDISCARD_ALL;
388
+
389
+    if (ff_vc1_parse_frame_header(v, &s->gb) == -1) {
390
+        av_log(v->s.avctx, AV_LOG_ERROR, "header error\n");
391
+        return AVERROR_INVALIDDATA;
392
+    }
393
+
394
+    if (s->pict_type != AV_PICTURE_TYPE_I) {
395
+        av_log(v->s.avctx, AV_LOG_ERROR, "expected I-frame\n");
396
+        return AVERROR_INVALIDDATA;
397
+    }
398
+
399
+    avctx->pix_fmt = PIX_FMT_YUV420P;
400
+
401
+    if (ff_MPV_frame_start(s, avctx) < 0) {
402
+        av_log(v->s.avctx, AV_LOG_ERROR, "ff_MPV_frame_start error\n");
403
+        avctx->pix_fmt = PIX_FMT_RGB24;
404
+        return -1;
405
+    }
406
+
407
+    ff_er_frame_start(s);
408
+
409
+    v->bits = buf_size * 8;
410
+
411
+    v->end_mb_x = (w + 15) >> 4;
412
+    s->end_mb_y = (h + 15) >> 4;
413
+    if (v->respic & 1)
414
+        v->end_mb_x = v->end_mb_x + 1 >> 1;
415
+    if (v->respic & 2)
416
+        s->end_mb_y = s->end_mb_y + 1 >> 1;
417
+
418
+    ff_vc1_decode_blocks(v);
419
+
420
+    ff_er_frame_end(s);
421
+
422
+    ff_MPV_frame_end(s);
423
+
424
+    f = &s->current_picture.f;
425
+
426
+    if (v->respic == 3) {
427
+        ctx->dsp.upsample_plane(f->data[0], f->linesize[0], w,      h);
428
+        ctx->dsp.upsample_plane(f->data[1], f->linesize[1], w >> 1, h >> 1);
429
+        ctx->dsp.upsample_plane(f->data[2], f->linesize[2], w >> 1, h >> 1);
430
+    } else if (v->respic)
431
+        av_log_ask_for_sample(v->s.avctx,
432
+                              "Asymmetric WMV9 rectangle subsampling\n");
433
+
434
+    av_assert0(f->linesize[1] == f->linesize[2]);
435
+
436
+    if (wmv9_mask != -1)
437
+        ctx->dsp.mss2_blit_wmv9_masked(c->rgb_pic + y * c->rgb_stride + x * 3,
438
+                                       c->rgb_stride, wmv9_mask,
439
+                                       c->pal_pic + y * c->pal_stride + x,
440
+                                       c->pal_stride,
441
+                                       f->data[0], f->linesize[0],
442
+                                       f->data[1], f->data[2], f->linesize[1],
443
+                                       w, h);
444
+    else
445
+        ctx->dsp.mss2_blit_wmv9(c->rgb_pic + y * c->rgb_stride + x * 3,
446
+                                c->rgb_stride,
447
+                                f->data[0], f->linesize[0],
448
+                                f->data[1], f->data[2], f->linesize[1],
449
+                                w, h);
450
+
451
+    avctx->pix_fmt = PIX_FMT_RGB24;
452
+
453
+    return 0;
454
+}
455
+
456
+typedef struct Rectangle {
457
+    int coded, x, y, w, h;
458
+} Rectangle;
459
+
460
+#define MAX_WMV9_RECTANGLES 20
461
+#define ARITH2_PADDING 2
462
+
463
+static int mss2_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
464
+                             AVPacket *avpkt)
465
+{
466
+    const uint8_t *buf = avpkt->data;
467
+    int buf_size       = avpkt->size;
468
+    MSS2Context *ctx = avctx->priv_data;
469
+    MSS12Context *c  = &ctx->c;
470
+    GetBitContext gb;
471
+    GetByteContext gB;
472
+    ArithCoder acoder;
473
+
474
+    int keyframe, has_wmv9, has_mv, is_rle, is_555, ret;
475
+
476
+    Rectangle wmv9rects[MAX_WMV9_RECTANGLES], *r;
477
+    int used_rects = 0, i, implicit_rect, av_uninit(wmv9_mask);
478
+
479
+    av_assert0(FF_INPUT_BUFFER_PADDING_SIZE >=
480
+               ARITH2_PADDING + (MIN_CACHE_BITS + 7) / 8);
481
+
482
+    init_get_bits(&gb, buf, buf_size * 8);
483
+
484
+    if (keyframe = get_bits1(&gb))
485
+        skip_bits(&gb, 7);
486
+    has_wmv9 = get_bits1(&gb);
487
+    has_mv   = keyframe ? 0 : get_bits1(&gb);
488
+    is_rle   = get_bits1(&gb);
489
+    is_555   = is_rle && get_bits1(&gb);
490
+    if (c->slice_split > 0)
491
+        ctx->split_position = c->slice_split;
492
+    else if (c->slice_split < 0) {
493
+        if (get_bits1(&gb)) {
494
+            if (get_bits1(&gb)) {
495
+                if (get_bits1(&gb))
496
+                    ctx->split_position = get_bits(&gb, 16);
497
+                else
498
+                    ctx->split_position = get_bits(&gb, 12);
499
+            } else
500
+                ctx->split_position = get_bits(&gb, 8) << 4;
501
+        } else {
502
+            if (keyframe)
503
+                ctx->split_position = avctx->height / 2;
504
+        }
505
+    } else
506
+        ctx->split_position = avctx->height;
507
+
508
+    if (c->slice_split && (ctx->split_position < 1 - is_555 ||
509
+                           ctx->split_position > avctx->height - 1))
510
+        return AVERROR_INVALIDDATA;
511
+
512
+    align_get_bits(&gb);
513
+    buf      += get_bits_count(&gb) >> 3;
514
+    buf_size -= get_bits_count(&gb) >> 3;
515
+
516
+    if (buf_size < 1)
517
+        return AVERROR_INVALIDDATA;
518
+
519
+    if (is_555 && (has_wmv9 || has_mv || c->slice_split && ctx->split_position))
520
+        return AVERROR_INVALIDDATA;
521
+
522
+    avctx->pix_fmt = is_555 ? PIX_FMT_RGB555 : PIX_FMT_RGB24;
523
+    if (ctx->pic.data[0] && ctx->pic.format != avctx->pix_fmt)
524
+        avctx->release_buffer(avctx, &ctx->pic);
525
+
526
+    if (has_wmv9) {
527
+        bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
528
+        arith2_init(&acoder, &gB);
529
+
530
+        implicit_rect = !arith2_get_bit(&acoder);
531
+
532
+        while (arith2_get_bit(&acoder)) {
533
+            if (used_rects == MAX_WMV9_RECTANGLES)
534
+                return AVERROR_INVALIDDATA;
535
+            r = &wmv9rects[used_rects];
536
+            if (!used_rects)
537
+                r->x = arith2_get_number(&acoder, avctx->width);
538
+            else
539
+                r->x = arith2_get_number(&acoder, avctx->width -
540
+                                         wmv9rects[used_rects - 1].x) +
541
+                       wmv9rects[used_rects - 1].x;
542
+            r->y = arith2_get_number(&acoder, avctx->height);
543
+            r->w = arith2_get_number(&acoder, avctx->width  - r->x) + 1;
544
+            r->h = arith2_get_number(&acoder, avctx->height - r->y) + 1;
545
+            used_rects++;
546
+        }
547
+
548
+        if (implicit_rect && used_rects) {
549
+            av_log(avctx, AV_LOG_ERROR, "implicit_rect && used_rects > 0\n");
550
+            return AVERROR_INVALIDDATA;
551
+        }
552
+
553
+        if (implicit_rect) {
554
+            wmv9rects[0].x = 0;
555
+            wmv9rects[0].y = 0;
556
+            wmv9rects[0].w = avctx->width;
557
+            wmv9rects[0].h = avctx->height;
558
+
559
+            used_rects = 1;
560
+        }
561
+        for (i = 0; i < used_rects; i++) {
562
+            if (!implicit_rect && arith2_get_bit(&acoder)) {
563
+                av_log(avctx, AV_LOG_ERROR, "Unexpected grandchildren\n");
564
+                return AVERROR_INVALIDDATA;
565
+            }
566
+            if (!i) {
567
+                wmv9_mask = arith2_get_bit(&acoder) - 1;
568
+                if (!wmv9_mask)
569
+                    wmv9_mask = arith2_get_number(&acoder, 256);
570
+            }
571
+            wmv9rects[i].coded = arith2_get_number(&acoder, 2);
572
+        }
573
+
574
+        buf      += arith2_get_consumed_bytes(&acoder);
575
+        buf_size -= arith2_get_consumed_bytes(&acoder);
576
+        if (buf_size < 1)
577
+            return AVERROR_INVALIDDATA;
578
+    }
579
+
580
+    c->mvX = c->mvY = 0;
581
+    if (keyframe && !is_555) {
582
+        if ((i = decode_pal_v2(c, buf, buf_size)) < 0)
583
+            return AVERROR_INVALIDDATA;
584
+        buf      += i;
585
+        buf_size -= i;
586
+    } else if (has_mv) {
587
+        buf      += 4;
588
+        buf_size -= 4;
589
+        if (buf_size < 1)
590
+            return AVERROR_INVALIDDATA;
591
+        c->mvX = AV_RB16(buf - 4) - avctx->width;
592
+        c->mvY = AV_RB16(buf - 2) - avctx->height;
593
+    }
594
+
595
+    if (c->mvX < 0 || c->mvY < 0) {
596
+        FFSWAP(AVFrame, ctx->pic, ctx->last_pic);
597
+        FFSWAP(uint8_t *, c->pal_pic, c->last_pal_pic);
598
+
599
+        if (ctx->pic.data[0])
600
+            avctx->release_buffer(avctx, &ctx->pic);
601
+
602
+        ctx->pic.reference    = 3;
603
+        ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID    |
604
+                                FF_BUFFER_HINTS_READABLE |
605
+                                FF_BUFFER_HINTS_PRESERVE |
606
+                                FF_BUFFER_HINTS_REUSABLE;
607
+
608
+        if ((ret = avctx->get_buffer(avctx, &ctx->pic)) < 0) {
609
+            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
610
+            return ret;
611
+        }
612
+
613
+        if (ctx->last_pic.data[0]) {
614
+            av_assert0(ctx->pic.linesize[0] == ctx->last_pic.linesize[0]);
615
+            c->last_rgb_pic = ctx->last_pic.data[0] +
616
+                              ctx->last_pic.linesize[0] * (avctx->height - 1);
617
+        } else {
618
+            av_log(avctx, AV_LOG_ERROR, "Missing keyframe\n");
619
+            return -1;
620
+        }
621
+    } else {
622
+        if (ctx->last_pic.data[0])
623
+            avctx->release_buffer(avctx, &ctx->last_pic);
624
+
625
+        ctx->pic.reference    = 3;
626
+        ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID    |
627
+                                FF_BUFFER_HINTS_READABLE |
628
+                                FF_BUFFER_HINTS_PRESERVE |
629
+                                FF_BUFFER_HINTS_REUSABLE;
630
+
631
+        if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {
632
+            av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
633
+            return ret;
634
+        }
635
+
636
+        c->last_rgb_pic = NULL;
637
+    }
638
+    c->rgb_pic    = ctx->pic.data[0] +
639
+                    ctx->pic.linesize[0] * (avctx->height - 1);
640
+    c->rgb_stride = -ctx->pic.linesize[0];
641
+
642
+    ctx->pic.key_frame = keyframe;
643
+    ctx->pic.pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
644
+
645
+    if (is_555) {
646
+        bytestream2_init(&gB, buf, buf_size);
647
+
648
+        if (decode_555(&gB, (uint16_t *)c->rgb_pic, c->rgb_stride >> 1,
649
+                       keyframe, avctx->width, avctx->height))
650
+            return AVERROR_INVALIDDATA;
651
+
652
+        buf_size -= bytestream2_tell(&gB);
653
+    } else if (is_rle) {
654
+        init_get_bits(&gb, buf, buf_size * 8);
655
+        if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
656
+                             c->rgb_pic, c->rgb_stride, c->pal, keyframe,
657
+                             ctx->split_position, 0,
658
+                             avctx->width, avctx->height))
659
+            return ret;
660
+        align_get_bits(&gb);
661
+
662
+        if (c->slice_split)
663
+            if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
664
+                                 c->rgb_pic, c->rgb_stride, c->pal, keyframe,
665
+                                 ctx->split_position, 1,
666
+                                 avctx->width, avctx->height))
667
+                return ret;
668
+
669
+        align_get_bits(&gb);
670
+        buf      += get_bits_count(&gb) >> 3;
671
+        buf_size -= get_bits_count(&gb) >> 3;
672
+    } else {
673
+        if (keyframe)
674
+            ff_mss12_codec_reset(c);
675
+        else if (c->corrupted)
676
+            return AVERROR_INVALIDDATA;
677
+        bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
678
+        arith2_init(&acoder, &gB);
679
+        c->keyframe = keyframe;
680
+        if (c->corrupted = ff_mss12_decode_rect(&c->sc[0], &acoder, 0, 0,
681
+                                                avctx->width,
682
+                                                ctx->split_position))
683
+            return AVERROR_INVALIDDATA;
684
+
685
+        buf      += arith2_get_consumed_bytes(&acoder);
686
+        buf_size -= arith2_get_consumed_bytes(&acoder);
687
+        if (c->slice_split) {
688
+            if (buf_size < 1)
689
+                return AVERROR_INVALIDDATA;
690
+            bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
691
+            arith2_init(&acoder, &gB);
692
+            if (c->corrupted = ff_mss12_decode_rect(&c->sc[1], &acoder, 0,
693
+                                                    ctx->split_position,
694
+                                                    avctx->width,
695
+                                                    avctx->height - ctx->split_position))
696
+                return AVERROR_INVALIDDATA;
697
+
698
+            buf      += arith2_get_consumed_bytes(&acoder);
699
+            buf_size -= arith2_get_consumed_bytes(&acoder);
700
+        }
701
+    }
702
+
703
+    if (has_wmv9) {
704
+        for (i = 0; i < used_rects; i++) {
705
+            int x = wmv9rects[i].x;
706
+            int y = wmv9rects[i].y;
707
+            int w = wmv9rects[i].w;
708
+            int h = wmv9rects[i].h;
709
+            if (wmv9rects[i].coded) {
710
+                int WMV9codedFrameSize;
711
+                if (buf_size < 4 || !(WMV9codedFrameSize = AV_RL24(buf)))
712
+                    return AVERROR_INVALIDDATA;
713
+                if (ret = decode_wmv9(avctx, buf + 3, buf_size - 3,
714
+                                      x, y, w, h, wmv9_mask))
715
+                    return ret;
716
+                buf      += WMV9codedFrameSize + 3;
717
+                buf_size -= WMV9codedFrameSize + 3;
718
+            } else {
719
+                uint8_t *dst = c->rgb_pic + y * c->rgb_stride + x * 3;
720
+                if (wmv9_mask != -1) {
721
+                    ctx->dsp.mss2_gray_fill_masked(dst, c->rgb_stride,
722
+                                                   wmv9_mask,
723
+                                                   c->pal_pic + y * c->pal_stride + x,
724
+                                                   c->pal_stride,
725
+                                                   w, h);
726
+                } else {
727
+                    do {
728
+                        memset(dst, 0x80, w * 3);
729
+                        dst += c->rgb_stride;
730
+                    } while (--h);
731
+                }
732
+            }
733
+        }
734
+    }
735
+
736
+    if (buf_size)
737
+        av_log(avctx, AV_LOG_WARNING, "buffer not fully consumed\n");
738
+
739
+    *data_size       = sizeof(AVFrame);
740
+    *(AVFrame *)data = ctx->pic;
741
+
742
+    return avpkt->size;
743
+}
744
+
745
+static av_cold int wmv9_init(AVCodecContext *avctx)
746
+{
747
+    VC1Context *v = avctx->priv_data;
748
+
749
+    v->s.avctx    = avctx;
750
+    avctx->flags |= CODEC_FLAG_EMU_EDGE;
751
+    v->s.flags   |= CODEC_FLAG_EMU_EDGE;
752
+
753
+    if (avctx->idct_algo == FF_IDCT_AUTO)
754
+        avctx->idct_algo = FF_IDCT_WMV2;
755
+
756
+    if (ff_vc1_init_common(v) < 0)
757
+        return -1;
758
+    ff_vc1dsp_init(&v->vc1dsp);
759
+
760
+    v->profile = PROFILE_MAIN;
761
+
762
+    v->zz_8x4     = ff_wmv2_scantableA;
763
+    v->zz_4x8     = ff_wmv2_scantableB;
764
+    v->res_y411   = 0;
765
+    v->res_sprite = 0;
766
+
767
+    v->frmrtq_postproc = 7;
768
+    v->bitrtq_postproc = 31;
769
+
770
+    v->res_x8          = 0;
771
+    v->multires        = 0;
772
+    v->res_fasttx      = 1;
773
+
774
+    v->fastuvmc        = 0;
775
+
776
+    v->extended_mv     = 0;
777
+
778
+    v->dquant          = 1;
779
+    v->vstransform     = 1;
780
+
781
+    v->res_transtab    = 0;
782
+
783
+    v->overlap         = 0;
784
+
785
+    v->s.resync_marker = 0;
786
+    v->rangered        = 0;
787
+
788
+    v->s.max_b_frames = avctx->max_b_frames = 0;
789
+    v->quantizer_mode = 0;
790
+
791
+    v->finterpflag = 0;
792
+
793
+    v->res_rtm_flag = 1;
794
+
795
+    ff_vc1_init_transposed_scantables(v);
796
+
797
+    if (ff_msmpeg4_decode_init(avctx) < 0 ||
798
+        ff_vc1_decode_init_alloc_tables(v) < 0)
799
+        return -1;
800
+
801
+    /* error concealment */
802
+    v->s.me.qpel_put = v->s.dsp.put_qpel_pixels_tab;
803
+    v->s.me.qpel_avg = v->s.dsp.avg_qpel_pixels_tab;
804
+
805
+    return 0;
806
+}
807
+
808
+static av_cold int mss2_decode_end(AVCodecContext *avctx)
809
+{
810
+    MSS2Context *const ctx = avctx->priv_data;
811
+
812
+    if (ctx->pic.data[0])
813
+        avctx->release_buffer(avctx, &ctx->pic);
814
+    if (ctx->last_pic.data[0])
815
+        avctx->release_buffer(avctx, &ctx->last_pic);
816
+
817
+    ff_mss12_decode_end(&ctx->c);
818
+    av_freep(&ctx->c.pal_pic);
819
+    av_freep(&ctx->c.last_pal_pic);
820
+    ff_vc1_decode_end(avctx);
821
+
822
+    return 0;
823
+}
824
+
825
+static av_cold int mss2_decode_init(AVCodecContext *avctx)
826
+{
827
+    MSS2Context * const ctx = avctx->priv_data;
828
+    MSS12Context *c = &ctx->c;
829
+    int ret;
830
+    c->avctx = avctx;
831
+    avctx->coded_frame = &ctx->pic;
832
+    if (ret = ff_mss12_decode_init(c, 1))
833
+        return ret;
834
+    c->pal_stride   = c->mask_stride;
835
+    c->pal_pic      = av_malloc(c->pal_stride * avctx->height);
836
+    c->last_pal_pic = av_malloc(c->pal_stride * avctx->height);
837
+    if (!c->pal_pic || !c->last_pal_pic) {
838
+        mss2_decode_end(avctx);
839
+        return AVERROR(ENOMEM);
840
+    }
841
+    if (ret = wmv9_init(avctx)) {
842
+        mss2_decode_end(avctx);
843
+        return ret;
844
+    }
845
+    ff_mss2dsp_init(&ctx->dsp);
846
+    return 0;
847
+}
848
+
849
+AVCodec ff_mss2_decoder = {
850
+    .name           = "mss2",
851
+    .type           = AVMEDIA_TYPE_VIDEO,
852
+    .id             = AV_CODEC_ID_MSS2,
853
+    .priv_data_size = sizeof(MSS2Context),
854
+    .init           = mss2_decode_init,
855
+    .close          = mss2_decode_end,
856
+    .decode         = mss2_decode_frame,
857
+    .capabilities   = CODEC_CAP_DR1,
858
+    .long_name      = NULL_IF_CONFIG_SMALL("MS Windows Media Video V9 Screen"),
859
+};
0 860
new file mode 100644
... ...
@@ -0,0 +1,153 @@
0
+/*
1
+ * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder
2
+ *
3
+ * This file is part of Libav.
4
+ *
5
+ * Libav is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * Libav is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with Libav; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder DSP routines
23
+ */
24
+
25
+#include "mss2dsp.h"
26
+#include "libavutil/common.h"
27
+
28
+static av_always_inline void mss2_blit_wmv9_template(uint8_t *dst,
29
+                                                     int dst_stride,
30
+                                                     int gray,
31
+                                                     int use_mask,
32
+                                                     int maskcolor,
33
+                                                     const uint8_t *mask,
34
+                                                     int mask_stride,
35
+                                                     const uint8_t *srcy,
36
+                                                     int srcy_stride,
37
+                                                     const uint8_t *srcu,
38
+                                                     const uint8_t *srcv,
39
+                                                     int srcuv_stride,
40
+                                                     int w, int h)
41
+{
42
+    int i, j, k, r = -1;
43
+    while (++r < h) {
44
+        for (i = 0, j = 0, k = 0; i < w; j += (i & 1), i++, k += 3) {
45
+            if (!use_mask || mask[i] == maskcolor) {
46
+                if (gray) {
47
+                    dst[k] = dst[k + 1] = dst[k + 2] = 0x80;
48
+                } else {
49
+                    int y = srcy[i];
50
+                    int u = srcu[j] - 128;
51
+                    int v = srcv[j] - 128;
52
+                    dst[k]     = av_clip_uint8(y + (             91881 * v + 32768 >> 16));
53
+                    dst[k + 1] = av_clip_uint8(y + (-22554 * u - 46802 * v + 32768 >> 16));
54
+                    dst[k + 2] = av_clip_uint8(y + (116130 * u             + 32768 >> 16));
55
+                }
56
+            }
57
+        }
58
+        mask +=  mask_stride;
59
+        dst  +=   dst_stride;
60
+        srcy +=  srcy_stride;
61
+        srcu += srcuv_stride * (r & 1);
62
+        srcv += srcuv_stride * (r & 1);
63
+    }
64
+}
65
+
66
+static void mss2_blit_wmv9_c(uint8_t *dst, int dst_stride,
67
+                             const uint8_t *srcy, int srcy_stride,
68
+                             const uint8_t *srcu, const uint8_t *srcv,
69
+                             int srcuv_stride, int w, int h)
70
+{
71
+    mss2_blit_wmv9_template(dst, dst_stride, 0, 0,
72
+                            0, NULL, 0,
73
+                            srcy, srcy_stride,
74
+                            srcu, srcv, srcuv_stride,
75
+                            w, h);
76
+}
77
+
78
+static void mss2_blit_wmv9_masked_c(uint8_t *dst, int dst_stride,
79
+                                    int maskcolor, const uint8_t *mask,
80
+                                    int mask_stride,
81
+                                    const uint8_t *srcy, int srcy_stride,
82
+                                    const uint8_t *srcu, const uint8_t *srcv,
83
+                                    int srcuv_stride, int w, int h)
84
+{
85
+    mss2_blit_wmv9_template(dst, dst_stride, 0, 1,
86
+                            maskcolor, mask, mask_stride,
87
+                            srcy, srcy_stride,
88
+                            srcu, srcv, srcuv_stride,
89
+                            w, h);
90
+}
91
+
92
+static void mss2_gray_fill_masked_c(uint8_t *dst, int dst_stride,
93
+                                    int maskcolor, const uint8_t *mask,
94
+                                    int mask_stride, int w, int h)
95
+{
96
+    mss2_blit_wmv9_template(dst, dst_stride, 1, 1,
97
+                            maskcolor, mask, mask_stride,
98
+                            NULL, 0,
99
+                            NULL, NULL, 0,
100
+                            w, h);
101
+}
102
+
103
+static void upsample_plane_c(uint8_t *plane, int plane_stride, int w, int h)
104
+{
105
+    uint8_t *src1, *src2, *dst1, *dst2, *p, a, b;
106
+    int i, j;
107
+
108
+    w += (w & 1);
109
+    h += (h & 1);
110
+
111
+    j = h - 1;
112
+
113
+    memcpy(plane + plane_stride *  j,
114
+           plane + plane_stride * (j >> 1),
115
+           w);
116
+
117
+    while ((j -= 2) > 0) {
118
+        dst1 = plane + plane_stride *  (j + 1);
119
+        dst2 = plane + plane_stride *   j;
120
+        src1 = plane + plane_stride * ((j + 1) >> 1);
121
+        src2 = plane + plane_stride * ( j      >> 1);
122
+
123
+        for (i = (w - 1) >> 1; i >= 0; i--) {
124
+            a = src1[i];
125
+            b = src2[i];
126
+            dst1[i] = (3 * a + b + 2) >> 2;
127
+            dst2[i] = (a + 3 * b + 2) >> 2;
128
+        }
129
+    }
130
+
131
+    for (j = h - 1; j >= 0; j--) {
132
+        p = plane + plane_stride * j;
133
+        i = w - 1;
134
+
135
+        p[i] = p[i >> 1];
136
+
137
+        while ((i -= 2) > 0) {
138
+            a = p[ i      >> 1];
139
+            b = p[(i + 1) >> 1];
140
+            p[i]     = (3 * a + b + 1) >> 2;
141
+            p[i + 1] = (a + 3 * b + 1) >> 2;
142
+        }
143
+    }
144
+}
145
+
146
+av_cold void ff_mss2dsp_init(MSS2DSPContext* dsp)
147
+{
148
+    dsp->mss2_blit_wmv9        = mss2_blit_wmv9_c;
149
+    dsp->mss2_blit_wmv9_masked = mss2_blit_wmv9_masked_c;
150
+    dsp->mss2_gray_fill_masked = mss2_gray_fill_masked_c;
151
+    dsp->upsample_plane        = upsample_plane_c;
152
+}
0 153
new file mode 100644
... ...
@@ -0,0 +1,50 @@
0
+/*
1
+ * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder
2
+ *
3
+ * This file is part of Libav.
4
+ *
5
+ * Libav is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * Libav is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with Libav; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder DSP routines
23
+ */
24
+
25
+#ifndef AVCODEC_MSS2DSP_H
26
+#define AVCODEC_MSS2DSP_H
27
+
28
+#include "dsputil.h"
29
+
30
+typedef struct MSS2DSPContext {
31
+    void (*mss2_blit_wmv9)(uint8_t *dst, int dst_stride,
32
+                           const uint8_t *srcy, int srcy_stride,
33
+                           const uint8_t *srcu, const uint8_t *srcv,
34
+                           int srcuv_stride, int w, int h);
35
+    void (*mss2_blit_wmv9_masked)(uint8_t *dst, int dst_stride,
36
+                                  int maskcolor, const uint8_t *mask,
37
+                                  int mask_stride,
38
+                                  const uint8_t *srcy, int srcy_stride,
39
+                                  const uint8_t *srcu, const uint8_t *srcv,
40
+                                  int srcuv_stride, int w, int h);
41
+    void (*mss2_gray_fill_masked)(uint8_t *dst, int dst_stride,
42
+                                  int maskcolor, const uint8_t *mask,
43
+                                  int mask_stride, int w, int h);
44
+    void (*upsample_plane)(uint8_t *plane, int plane_stride, int w, int h);
45
+} MSS2DSPContext;
46
+
47
+av_cold void ff_mss2dsp_init(MSS2DSPContext* dsp);
48
+
49
+#endif /* AVCODEC_MSS2DSP_H */
... ...
@@ -578,7 +578,12 @@ int ff_vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
578 578
 
579 579
     if (v->finterpflag)
580 580
         v->interpfrm = get_bits1(gb);
581
-    skip_bits(gb, 2); //framecnt unused
581
+    if (v->s.avctx->codec->id == AV_CODEC_ID_MSS2)
582
+        v->respic   =
583
+        v->rangered =
584
+        v->multires = get_bits(gb, 2) == 1;
585
+    else
586
+        skip_bits(gb, 2); //framecnt unused
582 587
     v->rangeredfrm = 0;
583 588
     if (v->rangered)
584 589
         v->rangeredfrm = get_bits1(gb);
... ...
@@ -394,6 +394,8 @@ typedef struct VC1Context{
394 394
     uint8_t broken_link;         ///< Broken link flag (BROKEN_LINK syntax element)
395 395
     uint8_t closed_entry;        ///< Closed entry point flag (CLOSED_ENTRY syntax element)
396 396
 
397
+    int end_mb_x;                ///< Horizontal macroblock limit (used only by mss2)
398
+
397 399
     int parse_only;              ///< Context is used within parser
398 400
 
399 401
     int warn_interlaced;
... ...
@@ -4348,10 +4348,10 @@ static void vc1_decode_i_blocks(VC1Context *v)
4348 4348
     s->mb_x = s->mb_y = 0;
4349 4349
     s->mb_intra         = 1;
4350 4350
     s->first_slice_line = 1;
4351
-    for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
4351
+    for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4352 4352
         s->mb_x = 0;
4353 4353
         ff_init_block_index(s);
4354
-        for (; s->mb_x < s->mb_width; s->mb_x++) {
4354
+        for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4355 4355
             uint8_t *dst[6];
4356 4356
             ff_update_block_index(s);
4357 4357
             dst[0] = s->dest[0];
... ...
@@ -4438,7 +4438,10 @@ static void vc1_decode_i_blocks(VC1Context *v)
4438 4438
         s->first_slice_line = 0;
4439 4439
     }
4440 4440
     if (v->s.loop_filter)
4441
-        ff_draw_horiz_band(s, (s->mb_height - 1) * 16, 16);
4441
+        ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4442
+
4443
+    /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4444
+     * profile, these only differ are when decoding MSS2 rectangles. */
4442 4445
     ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4443 4446
 }
4444 4447
 
... ...
@@ -5549,6 +5552,7 @@ static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5549 5549
         ff_er_frame_start(s);
5550 5550
 
5551 5551
         v->bits = buf_size * 8;
5552
+        v->end_mb_x = s->mb_width;
5552 5553
         if (v->field_mode) {
5553 5554
             uint8_t *tmp[2];
5554 5555
             s->current_picture.f.linesize[0] <<= 1;
... ...
@@ -27,8 +27,8 @@
27 27
  */
28 28
 
29 29
 #define LIBAVCODEC_VERSION_MAJOR 54
30
-#define LIBAVCODEC_VERSION_MINOR 26
31
-#define LIBAVCODEC_VERSION_MICRO  1
30
+#define LIBAVCODEC_VERSION_MINOR 27
31
+#define LIBAVCODEC_VERSION_MICRO  0
32 32
 
33 33
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
34 34
                                                LIBAVCODEC_VERSION_MINOR, \
... ...
@@ -289,6 +289,7 @@ const AVCodecTag ff_codec_bmp_tags[] = {
289 289
     { AV_CODEC_ID_TSCC2,        MKTAG('T', 'S', 'C', '2') },
290 290
     { AV_CODEC_ID_MTS2,         MKTAG('M', 'T', 'S', '2') },
291 291
     { AV_CODEC_ID_CLLC,         MKTAG('C', 'L', 'L', 'C') },
292
+    { AV_CODEC_ID_MSS2,         MKTAG('M', 'S', 'S', '2') },
292 293
     { AV_CODEC_ID_NONE,         0 }
293 294
 };
294 295