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