]> git.sesse.net Git - ffmpeg/blob - libavcodec/mss2.c
mss2: do not try to read too many palette entries
[ffmpeg] / libavcodec / mss2.c
1 /*
2  * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder
24  */
25
26 #include "libavutil/avassert.h"
27 #include "msmpeg4data.h"
28 #include "vc1.h"
29 #include "mss12.h"
30 #include "mss2dsp.h"
31
32 typedef struct MSS2Context {
33     VC1Context     v;
34     int            split_position;
35     AVFrame        pic;
36     AVFrame        last_pic;
37     MSS12Context   c;
38     MSS2DSPContext dsp;
39     SliceContext   sc[2];
40 } MSS2Context;
41
42 static void arith2_normalise(ArithCoder *c)
43 {
44     while ((c->high >> 15) - (c->low >> 15) < 2) {
45         if ((c->low ^ c->high) & 0x10000) {
46             c->high  ^= 0x8000;
47             c->value ^= 0x8000;
48             c->low   ^= 0x8000;
49         }
50         c->high  = c->high  << 8 & 0xFFFFFF | 0xFF;
51         c->value = c->value << 8 & 0xFFFFFF | bytestream2_get_byte(c->gbc.gB);
52         c->low   = c->low   << 8 & 0xFFFFFF;
53     }
54 }
55
56 ARITH_GET_BIT(2)
57
58 /* L. Stuiver and A. Moffat: "Piecewise Integer Mapping for Arithmetic Coding."
59  * In Proc. 8th Data Compression Conference (DCC '98), pp. 3-12, Mar. 1998 */
60
61 static int arith2_get_scaled_value(int value, int n, int range)
62 {
63     int split = (n << 1) - range;
64
65     if (value > split)
66         return split + (value - split >> 1);
67     else
68         return value;
69 }
70
71 static void arith2_rescale_interval(ArithCoder *c, int range,
72                                     int low, int high, int n)
73 {
74     int split = (n << 1) - range;
75
76     if (high > split)
77         c->high = split + (high - split << 1);
78     else
79         c->high = high;
80
81     c->high += c->low - 1;
82
83     if (low > split)
84         c->low += split + (low - split << 1);
85     else
86         c->low += low;
87 }
88
89 static int arith2_get_number(ArithCoder *c, int n)
90 {
91     int range = c->high - c->low + 1;
92     int scale = av_log2(range) - av_log2(n);
93     int val;
94
95     if (n << scale > range)
96         scale--;
97
98     n <<= scale;
99
100     val = arith2_get_scaled_value(c->value - c->low, n, range) >> scale;
101
102     arith2_rescale_interval(c, range, val << scale, (val + 1) << scale, n);
103
104     arith2_normalise(c);
105
106     return val;
107 }
108
109 static int arith2_get_prob(ArithCoder *c, int16_t *probs)
110 {
111     int range = c->high - c->low + 1, n = *probs;
112     int scale = av_log2(range) - av_log2(n);
113     int i     = 0, val;
114
115     if (n << scale > range)
116         scale--;
117
118     n <<= scale;
119
120     val = arith2_get_scaled_value(c->value - c->low, n, range) >> scale;
121     while (probs[++i] > val) ;
122
123     arith2_rescale_interval(c, range,
124                             probs[i] << scale, probs[i - 1] << scale, n);
125
126     return i;
127 }
128
129 ARITH_GET_MODEL_SYM(2)
130
131 static int arith2_get_consumed_bytes(ArithCoder *c)
132 {
133     int diff = (c->high >> 16) - (c->low >> 16);
134     int bp   = bytestream2_tell(c->gbc.gB) - 3 << 3;
135     int bits = 1;
136
137     while (!(diff & 0x80)) {
138         bits++;
139         diff <<= 1;
140     }
141
142     return (bits + bp + 7 >> 3) + ((c->low >> 16) + 1 == c->high >> 16);
143 }
144
145 static void arith2_init(ArithCoder *c, GetByteContext *gB)
146 {
147     c->low           = 0;
148     c->high          = 0xFFFFFF;
149     c->value         = bytestream2_get_be24(gB);
150     c->gbc.gB        = gB;
151     c->get_model_sym = arith2_get_model_sym;
152     c->get_number    = arith2_get_number;
153 }
154
155 static int decode_pal_v2(MSS12Context *ctx, const uint8_t *buf, int buf_size)
156 {
157     int i, ncol;
158     uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
159
160     if (!ctx->free_colours)
161         return 0;
162
163     ncol = *buf++;
164     if (ncol > ctx->free_colours || buf_size < 2 + ncol * 3)
165         return -1;
166     for (i = 0; i < ncol; i++)
167         *pal++ = AV_RB24(buf + 3 * i);
168
169     return 1 + ncol * 3;
170 }
171
172 static int decode_555(GetByteContext *gB, uint16_t *dst, int stride,
173                       int keyframe, int w, int h)
174 {
175     int last_symbol = 0, repeat = 0, prev_avail = 0;
176
177     if (!keyframe) {
178         int x, y, endx, endy, t;
179
180 #define READ_PAIR(a, b)                 \
181     a  = bytestream2_get_byte(gB) << 4; \
182     t  = bytestream2_get_byte(gB);      \
183     a |= t >> 4;                        \
184     b  = (t & 0xF) << 8;                \
185     b |= bytestream2_get_byte(gB);      \
186
187         READ_PAIR(x, endx)
188         READ_PAIR(y, endy)
189
190         if (endx >= w || endy >= h || x > endx || y > endy)
191             return -1;
192         dst += x + stride * y;
193         w    = endx - x + 1;
194         h    = endy - y + 1;
195         if (y)
196             prev_avail = 1;
197     }
198
199     do {
200         uint16_t *p = dst;
201         do {
202             if (repeat-- < 1) {
203                 int b = bytestream2_get_byte(gB);
204                 if (b < 128)
205                     last_symbol = b << 8 | bytestream2_get_byte(gB);
206                 else if (b > 129) {
207                     repeat = 0;
208                     while (b-- > 130)
209                         repeat = (repeat << 8) + bytestream2_get_byte(gB) + 1;
210                     if (last_symbol == -2) {
211                         int skip = FFMIN((unsigned)repeat, dst + w - p);
212                         repeat -= skip;
213                         p      += skip;
214                     }
215                 } else
216                     last_symbol = 127 - b;
217             }
218             if (last_symbol >= 0)
219                 *p = last_symbol;
220             else if (last_symbol == -1 && prev_avail)
221                 *p = *(p - stride);
222         } while (++p < dst + w);
223         dst       += stride;
224         prev_avail = 1;
225     } while (--h);
226
227     return 0;
228 }
229
230 static int decode_rle(GetBitContext *gb, uint8_t *pal_dst, int pal_stride,
231                       uint8_t *rgb_dst, int rgb_stride, uint32_t *pal,
232                       int keyframe, int kf_slipt, int slice, int w, int h)
233 {
234     uint8_t bits[270] = { 0 };
235     uint32_t codes[270];
236     VLC vlc;
237
238     int current_length = 0, read_codes = 0, next_code = 0, current_codes = 0;
239     int remaining_codes, surplus_codes, i;
240
241     const int alphabet_size = 270 - keyframe;
242
243     int last_symbol = 0, repeat = 0, prev_avail = 0;
244
245     if (!keyframe) {
246         int x, y, clipw, cliph;
247
248         x     = get_bits(gb, 12);
249         y     = get_bits(gb, 12);
250         clipw = get_bits(gb, 12) + 1;
251         cliph = get_bits(gb, 12) + 1;
252
253         if (x + clipw > w || y + cliph > h)
254             return AVERROR_INVALIDDATA;
255         pal_dst += pal_stride * y + x;
256         rgb_dst += rgb_stride * y + x * 3;
257         w        = clipw;
258         h        = cliph;
259         if (y)
260             prev_avail = 1;
261     } else {
262         if (slice > 0) {
263             pal_dst   += pal_stride * kf_slipt;
264             rgb_dst   += rgb_stride * kf_slipt;
265             prev_avail = 1;
266             h         -= kf_slipt;
267         } else
268             h = kf_slipt;
269     }
270
271     /* read explicit codes */
272     do {
273         while (current_codes--) {
274             int symbol = get_bits(gb, 8);
275             if (symbol >= 204 - keyframe)
276                 symbol += 14 - keyframe;
277             else if (symbol > 189)
278                 symbol = get_bits1(gb) + (symbol << 1) - 190;
279             if (bits[symbol])
280                 return AVERROR_INVALIDDATA;
281             bits[symbol]  = current_length;
282             codes[symbol] = next_code++;
283             read_codes++;
284         }
285         current_length++;
286         next_code     <<= 1;
287         remaining_codes = (1 << current_length) - next_code;
288         current_codes   = get_bits(gb, av_ceil_log2(remaining_codes + 1));
289         if (current_length > 22 || current_codes > remaining_codes)
290             return AVERROR_INVALIDDATA;
291     } while (current_codes != remaining_codes);
292
293     remaining_codes = alphabet_size - read_codes;
294
295     /* determine the minimum length to fit the rest of the alphabet */
296     while ((surplus_codes = (2 << current_length) -
297                             (next_code << 1) - remaining_codes) < 0) {
298         current_length++;
299         next_code <<= 1;
300     }
301
302     /* add the rest of the symbols lexicographically */
303     for (i = 0; i < alphabet_size; i++)
304         if (!bits[i]) {
305             if (surplus_codes-- == 0) {
306                 current_length++;
307                 next_code <<= 1;
308             }
309             bits[i]  = current_length;
310             codes[i] = next_code++;
311         }
312
313     if (next_code != 1 << current_length)
314         return AVERROR_INVALIDDATA;
315
316     if (i = init_vlc(&vlc, 9, alphabet_size, bits, 1, 1, codes, 4, 4, 0))
317         return i;
318
319     /* frame decode */
320     do {
321         uint8_t *pp = pal_dst;
322         uint8_t *rp = rgb_dst;
323         do {
324             if (repeat-- < 1) {
325                 int b = get_vlc2(gb, vlc.table, 9, 3);
326                 if (b < 256)
327                     last_symbol = b;
328                 else if (b < 268) {
329                     b -= 256;
330                     if (b == 11)
331                         b = get_bits(gb, 4) + 10;
332
333                     if (!b)
334                         repeat = 0;
335                     else
336                         repeat = get_bits(gb, b);
337
338                     while (b--)
339                         repeat += 1 << b;
340
341                     if (last_symbol == -2) {
342                         int skip = FFMIN(repeat, pal_dst + w - pp);
343                         repeat -= skip;
344                         pp     += skip;
345                         rp     += skip * 3;
346                     }
347                 } else
348                     last_symbol = 267 - b;
349             }
350             if (last_symbol >= 0) {
351                 *pp = last_symbol;
352                 AV_WB24(rp, pal[last_symbol]);
353             } else if (last_symbol == -1 && prev_avail) {
354                 *pp = *(pp - pal_stride);
355                 memcpy(rp, rp - rgb_stride, 3);
356             }
357             rp += 3;
358         } while (++pp < pal_dst + w);
359         pal_dst   += pal_stride;
360         rgb_dst   += rgb_stride;
361         prev_avail = 1;
362     } while (--h);
363
364     ff_free_vlc(&vlc);
365     return 0;
366 }
367
368 static int decode_wmv9(AVCodecContext *avctx, const uint8_t *buf, int buf_size,
369                        int x, int y, int w, int h, int wmv9_mask)
370 {
371     MSS2Context *ctx  = avctx->priv_data;
372     MSS12Context *c   = &ctx->c;
373     VC1Context *v     = avctx->priv_data;
374     MpegEncContext *s = &v->s;
375     AVFrame *f;
376
377     ff_mpeg_flush(avctx);
378
379     if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
380         int i = ff_find_unused_picture(s, 0);
381         if (i < 0)
382             return -1;
383         s->current_picture_ptr = &s->picture[i];
384     }
385
386     init_get_bits(&s->gb, buf, buf_size * 8);
387
388     s->loop_filter = avctx->skip_loop_filter < AVDISCARD_ALL;
389
390     if (ff_vc1_parse_frame_header(v, &s->gb) == -1) {
391         av_log(v->s.avctx, AV_LOG_ERROR, "header error\n");
392         return AVERROR_INVALIDDATA;
393     }
394
395     if (s->pict_type != AV_PICTURE_TYPE_I) {
396         av_log(v->s.avctx, AV_LOG_ERROR, "expected I-frame\n");
397         return AVERROR_INVALIDDATA;
398     }
399
400     avctx->pix_fmt = PIX_FMT_YUV420P;
401
402     if (ff_MPV_frame_start(s, avctx) < 0) {
403         av_log(v->s.avctx, AV_LOG_ERROR, "ff_MPV_frame_start error\n");
404         avctx->pix_fmt = PIX_FMT_RGB24;
405         return -1;
406     }
407
408     ff_er_frame_start(s);
409
410     v->bits = buf_size * 8;
411
412     v->end_mb_x = (w + 15) >> 4;
413     s->end_mb_y = (h + 15) >> 4;
414     if (v->respic & 1)
415         v->end_mb_x = v->end_mb_x + 1 >> 1;
416     if (v->respic & 2)
417         s->end_mb_y = s->end_mb_y + 1 >> 1;
418
419     ff_vc1_decode_blocks(v);
420
421     ff_er_frame_end(s);
422
423     ff_MPV_frame_end(s);
424
425     f = &s->current_picture.f;
426
427     if (v->respic == 3) {
428         ctx->dsp.upsample_plane(f->data[0], f->linesize[0], w,      h);
429         ctx->dsp.upsample_plane(f->data[1], f->linesize[1], w >> 1, h >> 1);
430         ctx->dsp.upsample_plane(f->data[2], f->linesize[2], w >> 1, h >> 1);
431     } else if (v->respic)
432         av_log_ask_for_sample(v->s.avctx,
433                               "Asymmetric WMV9 rectangle subsampling\n");
434
435     av_assert0(f->linesize[1] == f->linesize[2]);
436
437     if (wmv9_mask != -1)
438         ctx->dsp.mss2_blit_wmv9_masked(c->rgb_pic + y * c->rgb_stride + x * 3,
439                                        c->rgb_stride, wmv9_mask,
440                                        c->pal_pic + y * c->pal_stride + x,
441                                        c->pal_stride,
442                                        f->data[0], f->linesize[0],
443                                        f->data[1], f->data[2], f->linesize[1],
444                                        w, h);
445     else
446         ctx->dsp.mss2_blit_wmv9(c->rgb_pic + y * c->rgb_stride + x * 3,
447                                 c->rgb_stride,
448                                 f->data[0], f->linesize[0],
449                                 f->data[1], f->data[2], f->linesize[1],
450                                 w, h);
451
452     avctx->pix_fmt = PIX_FMT_RGB24;
453
454     return 0;
455 }
456
457 typedef struct Rectangle {
458     int coded, x, y, w, h;
459 } Rectangle;
460
461 #define MAX_WMV9_RECTANGLES 20
462 #define ARITH2_PADDING 2
463
464 static int mss2_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
465                              AVPacket *avpkt)
466 {
467     const uint8_t *buf = avpkt->data;
468     int buf_size       = avpkt->size;
469     MSS2Context *ctx = avctx->priv_data;
470     MSS12Context *c  = &ctx->c;
471     GetBitContext gb;
472     GetByteContext gB;
473     ArithCoder acoder;
474
475     int keyframe, has_wmv9, has_mv, is_rle, is_555, ret;
476
477     Rectangle wmv9rects[MAX_WMV9_RECTANGLES], *r;
478     int used_rects = 0, i, implicit_rect, av_uninit(wmv9_mask);
479
480     av_assert0(FF_INPUT_BUFFER_PADDING_SIZE >=
481                ARITH2_PADDING + (MIN_CACHE_BITS + 7) / 8);
482
483     init_get_bits(&gb, buf, buf_size * 8);
484
485     if (keyframe = get_bits1(&gb))
486         skip_bits(&gb, 7);
487     has_wmv9 = get_bits1(&gb);
488     has_mv   = keyframe ? 0 : get_bits1(&gb);
489     is_rle   = get_bits1(&gb);
490     is_555   = is_rle && get_bits1(&gb);
491     if (c->slice_split > 0)
492         ctx->split_position = c->slice_split;
493     else if (c->slice_split < 0) {
494         if (get_bits1(&gb)) {
495             if (get_bits1(&gb)) {
496                 if (get_bits1(&gb))
497                     ctx->split_position = get_bits(&gb, 16);
498                 else
499                     ctx->split_position = get_bits(&gb, 12);
500             } else
501                 ctx->split_position = get_bits(&gb, 8) << 4;
502         } else {
503             if (keyframe)
504                 ctx->split_position = avctx->height / 2;
505         }
506     } else
507         ctx->split_position = avctx->height;
508
509     if (c->slice_split && (ctx->split_position < 1 - is_555 ||
510                            ctx->split_position > avctx->height - 1))
511         return AVERROR_INVALIDDATA;
512
513     align_get_bits(&gb);
514     buf      += get_bits_count(&gb) >> 3;
515     buf_size -= get_bits_count(&gb) >> 3;
516
517     if (buf_size < 1)
518         return AVERROR_INVALIDDATA;
519
520     if (is_555 && (has_wmv9 || has_mv || c->slice_split && ctx->split_position))
521         return AVERROR_INVALIDDATA;
522
523     avctx->pix_fmt = is_555 ? PIX_FMT_RGB555 : PIX_FMT_RGB24;
524     if (ctx->pic.data[0] && ctx->pic.format != avctx->pix_fmt)
525         avctx->release_buffer(avctx, &ctx->pic);
526
527     if (has_wmv9) {
528         bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
529         arith2_init(&acoder, &gB);
530
531         implicit_rect = !arith2_get_bit(&acoder);
532
533         while (arith2_get_bit(&acoder)) {
534             if (used_rects == MAX_WMV9_RECTANGLES)
535                 return AVERROR_INVALIDDATA;
536             r = &wmv9rects[used_rects];
537             if (!used_rects)
538                 r->x = arith2_get_number(&acoder, avctx->width);
539             else
540                 r->x = arith2_get_number(&acoder, avctx->width -
541                                          wmv9rects[used_rects - 1].x) +
542                        wmv9rects[used_rects - 1].x;
543             r->y = arith2_get_number(&acoder, avctx->height);
544             r->w = arith2_get_number(&acoder, avctx->width  - r->x) + 1;
545             r->h = arith2_get_number(&acoder, avctx->height - r->y) + 1;
546             used_rects++;
547         }
548
549         if (implicit_rect && used_rects) {
550             av_log(avctx, AV_LOG_ERROR, "implicit_rect && used_rects > 0\n");
551             return AVERROR_INVALIDDATA;
552         }
553
554         if (implicit_rect) {
555             wmv9rects[0].x = 0;
556             wmv9rects[0].y = 0;
557             wmv9rects[0].w = avctx->width;
558             wmv9rects[0].h = avctx->height;
559
560             used_rects = 1;
561         }
562         for (i = 0; i < used_rects; i++) {
563             if (!implicit_rect && arith2_get_bit(&acoder)) {
564                 av_log(avctx, AV_LOG_ERROR, "Unexpected grandchildren\n");
565                 return AVERROR_INVALIDDATA;
566             }
567             if (!i) {
568                 wmv9_mask = arith2_get_bit(&acoder) - 1;
569                 if (!wmv9_mask)
570                     wmv9_mask = arith2_get_number(&acoder, 256);
571             }
572             wmv9rects[i].coded = arith2_get_number(&acoder, 2);
573         }
574
575         buf      += arith2_get_consumed_bytes(&acoder);
576         buf_size -= arith2_get_consumed_bytes(&acoder);
577         if (buf_size < 1)
578             return AVERROR_INVALIDDATA;
579     }
580
581     c->mvX = c->mvY = 0;
582     if (keyframe && !is_555) {
583         if ((i = decode_pal_v2(c, buf, buf_size)) < 0)
584             return AVERROR_INVALIDDATA;
585         buf      += i;
586         buf_size -= i;
587     } else if (has_mv) {
588         buf      += 4;
589         buf_size -= 4;
590         if (buf_size < 1)
591             return AVERROR_INVALIDDATA;
592         c->mvX = AV_RB16(buf - 4) - avctx->width;
593         c->mvY = AV_RB16(buf - 2) - avctx->height;
594     }
595
596     if (c->mvX < 0 || c->mvY < 0) {
597         FFSWAP(AVFrame, ctx->pic, ctx->last_pic);
598         FFSWAP(uint8_t *, c->pal_pic, c->last_pal_pic);
599
600         if (ctx->pic.data[0])
601             avctx->release_buffer(avctx, &ctx->pic);
602
603         ctx->pic.reference    = 3;
604         ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID    |
605                                 FF_BUFFER_HINTS_READABLE |
606                                 FF_BUFFER_HINTS_PRESERVE |
607                                 FF_BUFFER_HINTS_REUSABLE;
608
609         if ((ret = avctx->get_buffer(avctx, &ctx->pic)) < 0) {
610             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
611             return ret;
612         }
613
614         if (ctx->last_pic.data[0]) {
615             av_assert0(ctx->pic.linesize[0] == ctx->last_pic.linesize[0]);
616             c->last_rgb_pic = ctx->last_pic.data[0] +
617                               ctx->last_pic.linesize[0] * (avctx->height - 1);
618         } else {
619             av_log(avctx, AV_LOG_ERROR, "Missing keyframe\n");
620             return -1;
621         }
622     } else {
623         if (ctx->last_pic.data[0])
624             avctx->release_buffer(avctx, &ctx->last_pic);
625
626         ctx->pic.reference    = 3;
627         ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID    |
628                                 FF_BUFFER_HINTS_READABLE |
629                                 FF_BUFFER_HINTS_PRESERVE |
630                                 FF_BUFFER_HINTS_REUSABLE;
631
632         if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {
633             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
634             return ret;
635         }
636
637         c->last_rgb_pic = NULL;
638     }
639     c->rgb_pic    = ctx->pic.data[0] +
640                     ctx->pic.linesize[0] * (avctx->height - 1);
641     c->rgb_stride = -ctx->pic.linesize[0];
642
643     ctx->pic.key_frame = keyframe;
644     ctx->pic.pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
645
646     if (is_555) {
647         bytestream2_init(&gB, buf, buf_size);
648
649         if (decode_555(&gB, (uint16_t *)c->rgb_pic, c->rgb_stride >> 1,
650                        keyframe, avctx->width, avctx->height))
651             return AVERROR_INVALIDDATA;
652
653         buf_size -= bytestream2_tell(&gB);
654     } else if (is_rle) {
655         init_get_bits(&gb, buf, buf_size * 8);
656         if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
657                              c->rgb_pic, c->rgb_stride, c->pal, keyframe,
658                              ctx->split_position, 0,
659                              avctx->width, avctx->height))
660             return ret;
661         align_get_bits(&gb);
662
663         if (c->slice_split)
664             if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
665                                  c->rgb_pic, c->rgb_stride, c->pal, keyframe,
666                                  ctx->split_position, 1,
667                                  avctx->width, avctx->height))
668                 return ret;
669
670         align_get_bits(&gb);
671         buf      += get_bits_count(&gb) >> 3;
672         buf_size -= get_bits_count(&gb) >> 3;
673     } else {
674         if (keyframe) {
675             c->corrupted = 0;
676             ff_mss12_slicecontext_reset(&ctx->sc[0]);
677             if (c->slice_split)
678                 ff_mss12_slicecontext_reset(&ctx->sc[1]);
679         }
680         else if (c->corrupted)
681             return AVERROR_INVALIDDATA;
682         bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
683         arith2_init(&acoder, &gB);
684         c->keyframe = keyframe;
685         if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[0], &acoder, 0, 0,
686                                                 avctx->width,
687                                                 ctx->split_position))
688             return AVERROR_INVALIDDATA;
689
690         buf      += arith2_get_consumed_bytes(&acoder);
691         buf_size -= arith2_get_consumed_bytes(&acoder);
692         if (c->slice_split) {
693             if (buf_size < 1)
694                 return AVERROR_INVALIDDATA;
695             bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
696             arith2_init(&acoder, &gB);
697             if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[1], &acoder, 0,
698                                                     ctx->split_position,
699                                                     avctx->width,
700                                                     avctx->height - ctx->split_position))
701                 return AVERROR_INVALIDDATA;
702
703             buf      += arith2_get_consumed_bytes(&acoder);
704             buf_size -= arith2_get_consumed_bytes(&acoder);
705         }
706     }
707
708     if (has_wmv9) {
709         for (i = 0; i < used_rects; i++) {
710             int x = wmv9rects[i].x;
711             int y = wmv9rects[i].y;
712             int w = wmv9rects[i].w;
713             int h = wmv9rects[i].h;
714             if (wmv9rects[i].coded) {
715                 int WMV9codedFrameSize;
716                 if (buf_size < 4 || !(WMV9codedFrameSize = AV_RL24(buf)))
717                     return AVERROR_INVALIDDATA;
718                 if (ret = decode_wmv9(avctx, buf + 3, buf_size - 3,
719                                       x, y, w, h, wmv9_mask))
720                     return ret;
721                 buf      += WMV9codedFrameSize + 3;
722                 buf_size -= WMV9codedFrameSize + 3;
723             } else {
724                 uint8_t *dst = c->rgb_pic + y * c->rgb_stride + x * 3;
725                 if (wmv9_mask != -1) {
726                     ctx->dsp.mss2_gray_fill_masked(dst, c->rgb_stride,
727                                                    wmv9_mask,
728                                                    c->pal_pic + y * c->pal_stride + x,
729                                                    c->pal_stride,
730                                                    w, h);
731                 } else {
732                     do {
733                         memset(dst, 0x80, w * 3);
734                         dst += c->rgb_stride;
735                     } while (--h);
736                 }
737             }
738         }
739     }
740
741     if (buf_size)
742         av_log(avctx, AV_LOG_WARNING, "buffer not fully consumed\n");
743
744     *data_size       = sizeof(AVFrame);
745     *(AVFrame *)data = ctx->pic;
746
747     return avpkt->size;
748 }
749
750 static av_cold int wmv9_init(AVCodecContext *avctx)
751 {
752     VC1Context *v = avctx->priv_data;
753
754     v->s.avctx    = avctx;
755     avctx->flags |= CODEC_FLAG_EMU_EDGE;
756     v->s.flags   |= CODEC_FLAG_EMU_EDGE;
757
758     if (avctx->idct_algo == FF_IDCT_AUTO)
759         avctx->idct_algo = FF_IDCT_WMV2;
760
761     if (ff_vc1_init_common(v) < 0)
762         return -1;
763     ff_vc1dsp_init(&v->vc1dsp);
764
765     v->profile = PROFILE_MAIN;
766
767     v->zz_8x4     = ff_wmv2_scantableA;
768     v->zz_4x8     = ff_wmv2_scantableB;
769     v->res_y411   = 0;
770     v->res_sprite = 0;
771
772     v->frmrtq_postproc = 7;
773     v->bitrtq_postproc = 31;
774
775     v->res_x8          = 0;
776     v->multires        = 0;
777     v->res_fasttx      = 1;
778
779     v->fastuvmc        = 0;
780
781     v->extended_mv     = 0;
782
783     v->dquant          = 1;
784     v->vstransform     = 1;
785
786     v->res_transtab    = 0;
787
788     v->overlap         = 0;
789
790     v->s.resync_marker = 0;
791     v->rangered        = 0;
792
793     v->s.max_b_frames = avctx->max_b_frames = 0;
794     v->quantizer_mode = 0;
795
796     v->finterpflag = 0;
797
798     v->res_rtm_flag = 1;
799
800     ff_vc1_init_transposed_scantables(v);
801
802     if (ff_msmpeg4_decode_init(avctx) < 0 ||
803         ff_vc1_decode_init_alloc_tables(v) < 0)
804         return -1;
805
806     /* error concealment */
807     v->s.me.qpel_put = v->s.dsp.put_qpel_pixels_tab;
808     v->s.me.qpel_avg = v->s.dsp.avg_qpel_pixels_tab;
809
810     return 0;
811 }
812
813 static av_cold int mss2_decode_end(AVCodecContext *avctx)
814 {
815     MSS2Context *const ctx = avctx->priv_data;
816
817     if (ctx->pic.data[0])
818         avctx->release_buffer(avctx, &ctx->pic);
819     if (ctx->last_pic.data[0])
820         avctx->release_buffer(avctx, &ctx->last_pic);
821
822     ff_mss12_decode_end(&ctx->c);
823     av_freep(&ctx->c.pal_pic);
824     av_freep(&ctx->c.last_pal_pic);
825     ff_vc1_decode_end(avctx);
826
827     return 0;
828 }
829
830 static av_cold int mss2_decode_init(AVCodecContext *avctx)
831 {
832     MSS2Context * const ctx = avctx->priv_data;
833     MSS12Context *c = &ctx->c;
834     int ret;
835     c->avctx = avctx;
836     avctx->coded_frame = &ctx->pic;
837     if (ret = ff_mss12_decode_init(c, 1, &ctx->sc[0], &ctx->sc[1]))
838         return ret;
839     c->pal_stride   = c->mask_stride;
840     c->pal_pic      = av_malloc(c->pal_stride * avctx->height);
841     c->last_pal_pic = av_malloc(c->pal_stride * avctx->height);
842     if (!c->pal_pic || !c->last_pal_pic) {
843         mss2_decode_end(avctx);
844         return AVERROR(ENOMEM);
845     }
846     if (ret = wmv9_init(avctx)) {
847         mss2_decode_end(avctx);
848         return ret;
849     }
850     ff_mss2dsp_init(&ctx->dsp);
851
852     avctx->pix_fmt = c->free_colours == 127 ? PIX_FMT_RGB555
853                                             : PIX_FMT_RGB24;
854
855     return 0;
856 }
857
858 AVCodec ff_mss2_decoder = {
859     .name           = "mss2",
860     .type           = AVMEDIA_TYPE_VIDEO,
861     .id             = AV_CODEC_ID_MSS2,
862     .priv_data_size = sizeof(MSS2Context),
863     .init           = mss2_decode_init,
864     .close          = mss2_decode_end,
865     .decode         = mss2_decode_frame,
866     .capabilities   = CODEC_CAP_DR1,
867     .long_name      = NULL_IF_CONFIG_SMALL("MS Windows Media Video V9 Screen"),
868 };