00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "libavutil/avassert.h"
00027 #include "internal.h"
00028 #include "msmpeg4data.h"
00029 #include "vc1.h"
00030 #include "mss12.h"
00031 #include "mss2dsp.h"
00032
00033 typedef struct MSS2Context {
00034 VC1Context v;
00035 int split_position;
00036 AVFrame pic;
00037 AVFrame last_pic;
00038 MSS12Context c;
00039 MSS2DSPContext dsp;
00040 SliceContext sc[2];
00041 } MSS2Context;
00042
00043 static void arith2_normalise(ArithCoder *c)
00044 {
00045 while ((c->high >> 15) - (c->low >> 15) < 2) {
00046 if ((c->low ^ c->high) & 0x10000) {
00047 c->high ^= 0x8000;
00048 c->value ^= 0x8000;
00049 c->low ^= 0x8000;
00050 }
00051 c->high = c->high << 8 & 0xFFFFFF | 0xFF;
00052 c->value = c->value << 8 & 0xFFFFFF | bytestream2_get_byte(c->gbc.gB);
00053 c->low = c->low << 8 & 0xFFFFFF;
00054 }
00055 }
00056
00057 ARITH_GET_BIT(2)
00058
00059
00060
00061
00062 static int arith2_get_scaled_value(int value, int n, int range)
00063 {
00064 int split = (n << 1) - range;
00065
00066 if (value > split)
00067 return split + (value - split >> 1);
00068 else
00069 return value;
00070 }
00071
00072 static void arith2_rescale_interval(ArithCoder *c, int range,
00073 int low, int high, int n)
00074 {
00075 int split = (n << 1) - range;
00076
00077 if (high > split)
00078 c->high = split + (high - split << 1);
00079 else
00080 c->high = high;
00081
00082 c->high += c->low - 1;
00083
00084 if (low > split)
00085 c->low += split + (low - split << 1);
00086 else
00087 c->low += low;
00088 }
00089
00090 static int arith2_get_number(ArithCoder *c, int n)
00091 {
00092 int range = c->high - c->low + 1;
00093 int scale = av_log2(range) - av_log2(n);
00094 int val;
00095
00096 if (n << scale > range)
00097 scale--;
00098
00099 n <<= scale;
00100
00101 val = arith2_get_scaled_value(c->value - c->low, n, range) >> scale;
00102
00103 arith2_rescale_interval(c, range, val << scale, (val + 1) << scale, n);
00104
00105 arith2_normalise(c);
00106
00107 return val;
00108 }
00109
00110 static int arith2_get_prob(ArithCoder *c, int16_t *probs)
00111 {
00112 int range = c->high - c->low + 1, n = *probs;
00113 int scale = av_log2(range) - av_log2(n);
00114 int i = 0, val;
00115
00116 if (n << scale > range)
00117 scale--;
00118
00119 n <<= scale;
00120
00121 val = arith2_get_scaled_value(c->value - c->low, n, range) >> scale;
00122 while (probs[++i] > val) ;
00123
00124 arith2_rescale_interval(c, range,
00125 probs[i] << scale, probs[i - 1] << scale, n);
00126
00127 return i;
00128 }
00129
00130 ARITH_GET_MODEL_SYM(2)
00131
00132 static int arith2_get_consumed_bytes(ArithCoder *c)
00133 {
00134 int diff = (c->high >> 16) - (c->low >> 16);
00135 int bp = bytestream2_tell(c->gbc.gB) - 3 << 3;
00136 int bits = 1;
00137
00138 while (!(diff & 0x80)) {
00139 bits++;
00140 diff <<= 1;
00141 }
00142
00143 return (bits + bp + 7 >> 3) + ((c->low >> 16) + 1 == c->high >> 16);
00144 }
00145
00146 static void arith2_init(ArithCoder *c, GetByteContext *gB)
00147 {
00148 c->low = 0;
00149 c->high = 0xFFFFFF;
00150 c->value = bytestream2_get_be24(gB);
00151 c->gbc.gB = gB;
00152 c->get_model_sym = arith2_get_model_sym;
00153 c->get_number = arith2_get_number;
00154 }
00155
00156 static int decode_pal_v2(MSS12Context *ctx, const uint8_t *buf, int buf_size)
00157 {
00158 int i, ncol;
00159 uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
00160
00161 if (!ctx->free_colours)
00162 return 0;
00163
00164 ncol = *buf++;
00165 if (ncol > ctx->free_colours || buf_size < 2 + ncol * 3)
00166 return -1;
00167 for (i = 0; i < ncol; i++)
00168 *pal++ = AV_RB24(buf + 3 * i);
00169
00170 return 1 + ncol * 3;
00171 }
00172
00173 static int decode_555(GetByteContext *gB, uint16_t *dst, int stride,
00174 int keyframe, int w, int h)
00175 {
00176 int last_symbol = 0, repeat = 0, prev_avail = 0;
00177
00178 if (!keyframe) {
00179 int x, y, endx, endy, t;
00180
00181 #define READ_PAIR(a, b) \
00182 a = bytestream2_get_byte(gB) << 4; \
00183 t = bytestream2_get_byte(gB); \
00184 a |= t >> 4; \
00185 b = (t & 0xF) << 8; \
00186 b |= bytestream2_get_byte(gB); \
00187
00188 READ_PAIR(x, endx)
00189 READ_PAIR(y, endy)
00190
00191 if (endx >= w || endy >= h || x > endx || y > endy)
00192 return -1;
00193 dst += x + stride * y;
00194 w = endx - x + 1;
00195 h = endy - y + 1;
00196 if (y)
00197 prev_avail = 1;
00198 }
00199
00200 do {
00201 uint16_t *p = dst;
00202 do {
00203 if (repeat-- < 1) {
00204 int b = bytestream2_get_byte(gB);
00205 if (b < 128)
00206 last_symbol = b << 8 | bytestream2_get_byte(gB);
00207 else if (b > 129) {
00208 repeat = 0;
00209 while (b-- > 130)
00210 repeat = (repeat << 8) + bytestream2_get_byte(gB) + 1;
00211 if (last_symbol == -2) {
00212 int skip = FFMIN((unsigned)repeat, dst + w - p);
00213 repeat -= skip;
00214 p += skip;
00215 }
00216 } else
00217 last_symbol = 127 - b;
00218 }
00219 if (last_symbol >= 0)
00220 *p = last_symbol;
00221 else if (last_symbol == -1 && prev_avail)
00222 *p = *(p - stride);
00223 } while (++p < dst + w);
00224 dst += stride;
00225 prev_avail = 1;
00226 } while (--h);
00227
00228 return 0;
00229 }
00230
00231 static int decode_rle(GetBitContext *gb, uint8_t *pal_dst, int pal_stride,
00232 uint8_t *rgb_dst, int rgb_stride, uint32_t *pal,
00233 int keyframe, int kf_slipt, int slice, int w, int h)
00234 {
00235 uint8_t bits[270] = { 0 };
00236 uint32_t codes[270];
00237 VLC vlc;
00238
00239 int current_length = 0, read_codes = 0, next_code = 0, current_codes = 0;
00240 int remaining_codes, surplus_codes, i;
00241
00242 const int alphabet_size = 270 - keyframe;
00243
00244 int last_symbol = 0, repeat = 0, prev_avail = 0;
00245
00246 if (!keyframe) {
00247 int x, y, clipw, cliph;
00248
00249 x = get_bits(gb, 12);
00250 y = get_bits(gb, 12);
00251 clipw = get_bits(gb, 12) + 1;
00252 cliph = get_bits(gb, 12) + 1;
00253
00254 if (x + clipw > w || y + cliph > h)
00255 return AVERROR_INVALIDDATA;
00256 pal_dst += pal_stride * y + x;
00257 rgb_dst += rgb_stride * y + x * 3;
00258 w = clipw;
00259 h = cliph;
00260 if (y)
00261 prev_avail = 1;
00262 } else {
00263 if (slice > 0) {
00264 pal_dst += pal_stride * kf_slipt;
00265 rgb_dst += rgb_stride * kf_slipt;
00266 prev_avail = 1;
00267 h -= kf_slipt;
00268 } else
00269 h = kf_slipt;
00270 }
00271
00272
00273 do {
00274 while (current_codes--) {
00275 int symbol = get_bits(gb, 8);
00276 if (symbol >= 204 - keyframe)
00277 symbol += 14 - keyframe;
00278 else if (symbol > 189)
00279 symbol = get_bits1(gb) + (symbol << 1) - 190;
00280 if (bits[symbol])
00281 return AVERROR_INVALIDDATA;
00282 bits[symbol] = current_length;
00283 codes[symbol] = next_code++;
00284 read_codes++;
00285 }
00286 current_length++;
00287 next_code <<= 1;
00288 remaining_codes = (1 << current_length) - next_code;
00289 current_codes = get_bits(gb, av_ceil_log2(remaining_codes + 1));
00290 if (current_length > 22 || current_codes > remaining_codes)
00291 return AVERROR_INVALIDDATA;
00292 } while (current_codes != remaining_codes);
00293
00294 remaining_codes = alphabet_size - read_codes;
00295
00296
00297 while ((surplus_codes = (2 << current_length) -
00298 (next_code << 1) - remaining_codes) < 0) {
00299 current_length++;
00300 next_code <<= 1;
00301 }
00302
00303
00304 for (i = 0; i < alphabet_size; i++)
00305 if (!bits[i]) {
00306 if (surplus_codes-- == 0) {
00307 current_length++;
00308 next_code <<= 1;
00309 }
00310 bits[i] = current_length;
00311 codes[i] = next_code++;
00312 }
00313
00314 if (next_code != 1 << current_length)
00315 return AVERROR_INVALIDDATA;
00316
00317 if (i = init_vlc(&vlc, 9, alphabet_size, bits, 1, 1, codes, 4, 4, 0))
00318 return i;
00319
00320
00321 do {
00322 uint8_t *pp = pal_dst;
00323 uint8_t *rp = rgb_dst;
00324 do {
00325 if (repeat-- < 1) {
00326 int b = get_vlc2(gb, vlc.table, 9, 3);
00327 if (b < 256)
00328 last_symbol = b;
00329 else if (b < 268) {
00330 b -= 256;
00331 if (b == 11)
00332 b = get_bits(gb, 4) + 10;
00333
00334 if (!b)
00335 repeat = 0;
00336 else
00337 repeat = get_bits(gb, b);
00338
00339 repeat += (1 << b) - 1;
00340
00341 if (last_symbol == -2) {
00342 int skip = FFMIN(repeat, pal_dst + w - pp);
00343 repeat -= skip;
00344 pp += skip;
00345 rp += skip * 3;
00346 }
00347 } else
00348 last_symbol = 267 - b;
00349 }
00350 if (last_symbol >= 0) {
00351 *pp = last_symbol;
00352 AV_WB24(rp, pal[last_symbol]);
00353 } else if (last_symbol == -1 && prev_avail) {
00354 *pp = *(pp - pal_stride);
00355 memcpy(rp, rp - rgb_stride, 3);
00356 }
00357 rp += 3;
00358 } while (++pp < pal_dst + w);
00359 pal_dst += pal_stride;
00360 rgb_dst += rgb_stride;
00361 prev_avail = 1;
00362 } while (--h);
00363
00364 ff_free_vlc(&vlc);
00365 return 0;
00366 }
00367
00368 static int decode_wmv9(AVCodecContext *avctx, const uint8_t *buf, int buf_size,
00369 int x, int y, int w, int h, int wmv9_mask)
00370 {
00371 MSS2Context *ctx = avctx->priv_data;
00372 MSS12Context *c = &ctx->c;
00373 VC1Context *v = avctx->priv_data;
00374 MpegEncContext *s = &v->s;
00375 AVFrame *f;
00376
00377 ff_mpeg_flush(avctx);
00378
00379 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
00380 int i = ff_find_unused_picture(s, 0);
00381 if (i < 0)
00382 return -1;
00383 s->current_picture_ptr = &s->picture[i];
00384 }
00385
00386 init_get_bits(&s->gb, buf, buf_size * 8);
00387
00388 s->loop_filter = avctx->skip_loop_filter < AVDISCARD_ALL;
00389
00390 if (ff_vc1_parse_frame_header(v, &s->gb) == -1) {
00391 av_log(v->s.avctx, AV_LOG_ERROR, "header error\n");
00392 return AVERROR_INVALIDDATA;
00393 }
00394
00395 if (s->pict_type != AV_PICTURE_TYPE_I) {
00396 av_log(v->s.avctx, AV_LOG_ERROR, "expected I-frame\n");
00397 return AVERROR_INVALIDDATA;
00398 }
00399
00400 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
00401
00402 if (ff_MPV_frame_start(s, avctx) < 0) {
00403 av_log(v->s.avctx, AV_LOG_ERROR, "ff_MPV_frame_start error\n");
00404 avctx->pix_fmt = AV_PIX_FMT_RGB24;
00405 return -1;
00406 }
00407
00408 ff_er_frame_start(s);
00409
00410 v->bits = buf_size * 8;
00411
00412 v->end_mb_x = (w + 15) >> 4;
00413 s->end_mb_y = (h + 15) >> 4;
00414 if (v->respic & 1)
00415 v->end_mb_x = v->end_mb_x + 1 >> 1;
00416 if (v->respic & 2)
00417 s->end_mb_y = s->end_mb_y + 1 >> 1;
00418
00419 ff_vc1_decode_blocks(v);
00420
00421 ff_er_frame_end(s);
00422
00423 ff_MPV_frame_end(s);
00424
00425 f = &s->current_picture.f;
00426
00427 if (v->respic == 3) {
00428 ctx->dsp.upsample_plane(f->data[0], f->linesize[0], w, h);
00429 ctx->dsp.upsample_plane(f->data[1], f->linesize[1], w >> 1, h >> 1);
00430 ctx->dsp.upsample_plane(f->data[2], f->linesize[2], w >> 1, h >> 1);
00431 } else if (v->respic)
00432 av_log_ask_for_sample(v->s.avctx,
00433 "Asymmetric WMV9 rectangle subsampling\n");
00434
00435 av_assert0(f->linesize[1] == f->linesize[2]);
00436
00437 if (wmv9_mask != -1)
00438 ctx->dsp.mss2_blit_wmv9_masked(c->rgb_pic + y * c->rgb_stride + x * 3,
00439 c->rgb_stride, wmv9_mask,
00440 c->pal_pic + y * c->pal_stride + x,
00441 c->pal_stride,
00442 f->data[0], f->linesize[0],
00443 f->data[1], f->data[2], f->linesize[1],
00444 w, h);
00445 else
00446 ctx->dsp.mss2_blit_wmv9(c->rgb_pic + y * c->rgb_stride + x * 3,
00447 c->rgb_stride,
00448 f->data[0], f->linesize[0],
00449 f->data[1], f->data[2], f->linesize[1],
00450 w, h);
00451
00452 avctx->pix_fmt = AV_PIX_FMT_RGB24;
00453
00454 return 0;
00455 }
00456
00457 typedef struct Rectangle {
00458 int coded, x, y, w, h;
00459 } Rectangle;
00460
00461 #define MAX_WMV9_RECTANGLES 20
00462 #define ARITH2_PADDING 2
00463
00464 static int mss2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
00465 AVPacket *avpkt)
00466 {
00467 const uint8_t *buf = avpkt->data;
00468 int buf_size = avpkt->size;
00469 MSS2Context *ctx = avctx->priv_data;
00470 MSS12Context *c = &ctx->c;
00471 GetBitContext gb;
00472 GetByteContext gB;
00473 ArithCoder acoder;
00474
00475 int keyframe, has_wmv9, has_mv, is_rle, is_555, ret;
00476
00477 Rectangle wmv9rects[MAX_WMV9_RECTANGLES], *r;
00478 int used_rects = 0, i, implicit_rect = 0, av_uninit(wmv9_mask);
00479
00480 av_assert0(FF_INPUT_BUFFER_PADDING_SIZE >=
00481 ARITH2_PADDING + (MIN_CACHE_BITS + 7) / 8);
00482
00483 init_get_bits(&gb, buf, buf_size * 8);
00484
00485 if (keyframe = get_bits1(&gb))
00486 skip_bits(&gb, 7);
00487 has_wmv9 = get_bits1(&gb);
00488 has_mv = keyframe ? 0 : get_bits1(&gb);
00489 is_rle = get_bits1(&gb);
00490 is_555 = is_rle && get_bits1(&gb);
00491 if (c->slice_split > 0)
00492 ctx->split_position = c->slice_split;
00493 else if (c->slice_split < 0) {
00494 if (get_bits1(&gb)) {
00495 if (get_bits1(&gb)) {
00496 if (get_bits1(&gb))
00497 ctx->split_position = get_bits(&gb, 16);
00498 else
00499 ctx->split_position = get_bits(&gb, 12);
00500 } else
00501 ctx->split_position = get_bits(&gb, 8) << 4;
00502 } else {
00503 if (keyframe)
00504 ctx->split_position = avctx->height / 2;
00505 }
00506 } else
00507 ctx->split_position = avctx->height;
00508
00509 if (c->slice_split && (ctx->split_position < 1 - is_555 ||
00510 ctx->split_position > avctx->height - 1))
00511 return AVERROR_INVALIDDATA;
00512
00513 align_get_bits(&gb);
00514 buf += get_bits_count(&gb) >> 3;
00515 buf_size -= get_bits_count(&gb) >> 3;
00516
00517 if (buf_size < 1)
00518 return AVERROR_INVALIDDATA;
00519
00520 if (is_555 && (has_wmv9 || has_mv || c->slice_split && ctx->split_position))
00521 return AVERROR_INVALIDDATA;
00522
00523 avctx->pix_fmt = is_555 ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_RGB24;
00524 if (ctx->pic.data[0] && ctx->pic.format != avctx->pix_fmt)
00525 avctx->release_buffer(avctx, &ctx->pic);
00526
00527 if (has_wmv9) {
00528 bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
00529 arith2_init(&acoder, &gB);
00530
00531 implicit_rect = !arith2_get_bit(&acoder);
00532
00533 while (arith2_get_bit(&acoder)) {
00534 if (used_rects == MAX_WMV9_RECTANGLES)
00535 return AVERROR_INVALIDDATA;
00536 r = &wmv9rects[used_rects];
00537 if (!used_rects)
00538 r->x = arith2_get_number(&acoder, avctx->width);
00539 else
00540 r->x = arith2_get_number(&acoder, avctx->width -
00541 wmv9rects[used_rects - 1].x) +
00542 wmv9rects[used_rects - 1].x;
00543 r->y = arith2_get_number(&acoder, avctx->height);
00544 r->w = arith2_get_number(&acoder, avctx->width - r->x) + 1;
00545 r->h = arith2_get_number(&acoder, avctx->height - r->y) + 1;
00546 used_rects++;
00547 }
00548
00549 if (implicit_rect && used_rects) {
00550 av_log(avctx, AV_LOG_ERROR, "implicit_rect && used_rects > 0\n");
00551 return AVERROR_INVALIDDATA;
00552 }
00553
00554 if (implicit_rect) {
00555 wmv9rects[0].x = 0;
00556 wmv9rects[0].y = 0;
00557 wmv9rects[0].w = avctx->width;
00558 wmv9rects[0].h = avctx->height;
00559
00560 used_rects = 1;
00561 }
00562 for (i = 0; i < used_rects; i++) {
00563 if (!implicit_rect && arith2_get_bit(&acoder)) {
00564 av_log(avctx, AV_LOG_ERROR, "Unexpected grandchildren\n");
00565 return AVERROR_INVALIDDATA;
00566 }
00567 if (!i) {
00568 wmv9_mask = arith2_get_bit(&acoder) - 1;
00569 if (!wmv9_mask)
00570 wmv9_mask = arith2_get_number(&acoder, 256);
00571 }
00572 wmv9rects[i].coded = arith2_get_number(&acoder, 2);
00573 }
00574
00575 buf += arith2_get_consumed_bytes(&acoder);
00576 buf_size -= arith2_get_consumed_bytes(&acoder);
00577 if (buf_size < 1)
00578 return AVERROR_INVALIDDATA;
00579 }
00580
00581 c->mvX = c->mvY = 0;
00582 if (keyframe && !is_555) {
00583 if ((i = decode_pal_v2(c, buf, buf_size)) < 0)
00584 return AVERROR_INVALIDDATA;
00585 buf += i;
00586 buf_size -= i;
00587 } else if (has_mv) {
00588 buf += 4;
00589 buf_size -= 4;
00590 if (buf_size < 1)
00591 return AVERROR_INVALIDDATA;
00592 c->mvX = AV_RB16(buf - 4) - avctx->width;
00593 c->mvY = AV_RB16(buf - 2) - avctx->height;
00594 }
00595
00596 if (c->mvX < 0 || c->mvY < 0) {
00597 FFSWAP(AVFrame, ctx->pic, ctx->last_pic);
00598 FFSWAP(uint8_t *, c->pal_pic, c->last_pal_pic);
00599
00600 if (ctx->pic.data[0])
00601 avctx->release_buffer(avctx, &ctx->pic);
00602
00603 ctx->pic.reference = 3;
00604 ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
00605 FF_BUFFER_HINTS_READABLE |
00606 FF_BUFFER_HINTS_PRESERVE |
00607 FF_BUFFER_HINTS_REUSABLE;
00608
00609 if ((ret = ff_get_buffer(avctx, &ctx->pic)) < 0) {
00610 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00611 return ret;
00612 }
00613
00614 if (ctx->last_pic.data[0]) {
00615 av_assert0(ctx->pic.linesize[0] == ctx->last_pic.linesize[0]);
00616 c->last_rgb_pic = ctx->last_pic.data[0] +
00617 ctx->last_pic.linesize[0] * (avctx->height - 1);
00618 } else {
00619 av_log(avctx, AV_LOG_ERROR, "Missing keyframe\n");
00620 return -1;
00621 }
00622 } else {
00623 if (ctx->last_pic.data[0])
00624 avctx->release_buffer(avctx, &ctx->last_pic);
00625
00626 ctx->pic.reference = 3;
00627 ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
00628 FF_BUFFER_HINTS_READABLE |
00629 FF_BUFFER_HINTS_PRESERVE |
00630 FF_BUFFER_HINTS_REUSABLE;
00631
00632 if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {
00633 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00634 return ret;
00635 }
00636
00637 c->last_rgb_pic = NULL;
00638 }
00639 c->rgb_pic = ctx->pic.data[0] +
00640 ctx->pic.linesize[0] * (avctx->height - 1);
00641 c->rgb_stride = -ctx->pic.linesize[0];
00642
00643 ctx->pic.key_frame = keyframe;
00644 ctx->pic.pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
00645
00646 if (is_555) {
00647 bytestream2_init(&gB, buf, buf_size);
00648
00649 if (decode_555(&gB, (uint16_t *)c->rgb_pic, c->rgb_stride >> 1,
00650 keyframe, avctx->width, avctx->height))
00651 return AVERROR_INVALIDDATA;
00652
00653 buf_size -= bytestream2_tell(&gB);
00654 } else {
00655 if (keyframe) {
00656 c->corrupted = 0;
00657 ff_mss12_slicecontext_reset(&ctx->sc[0]);
00658 if (c->slice_split)
00659 ff_mss12_slicecontext_reset(&ctx->sc[1]);
00660 }
00661 if (is_rle) {
00662 init_get_bits(&gb, buf, buf_size * 8);
00663 if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
00664 c->rgb_pic, c->rgb_stride, c->pal, keyframe,
00665 ctx->split_position, 0,
00666 avctx->width, avctx->height))
00667 return ret;
00668 align_get_bits(&gb);
00669
00670 if (c->slice_split)
00671 if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
00672 c->rgb_pic, c->rgb_stride, c->pal, keyframe,
00673 ctx->split_position, 1,
00674 avctx->width, avctx->height))
00675 return ret;
00676
00677 align_get_bits(&gb);
00678 buf += get_bits_count(&gb) >> 3;
00679 buf_size -= get_bits_count(&gb) >> 3;
00680 } else if (!implicit_rect || wmv9_mask != -1) {
00681 if (c->corrupted)
00682 return AVERROR_INVALIDDATA;
00683 bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
00684 arith2_init(&acoder, &gB);
00685 c->keyframe = keyframe;
00686 if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[0], &acoder, 0, 0,
00687 avctx->width,
00688 ctx->split_position))
00689 return AVERROR_INVALIDDATA;
00690
00691 buf += arith2_get_consumed_bytes(&acoder);
00692 buf_size -= arith2_get_consumed_bytes(&acoder);
00693 if (c->slice_split) {
00694 if (buf_size < 1)
00695 return AVERROR_INVALIDDATA;
00696 bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
00697 arith2_init(&acoder, &gB);
00698 if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[1], &acoder, 0,
00699 ctx->split_position,
00700 avctx->width,
00701 avctx->height - ctx->split_position))
00702 return AVERROR_INVALIDDATA;
00703
00704 buf += arith2_get_consumed_bytes(&acoder);
00705 buf_size -= arith2_get_consumed_bytes(&acoder);
00706 }
00707 } else
00708 memset(c->pal_pic, 0, c->pal_stride * avctx->height);
00709 }
00710
00711 if (has_wmv9) {
00712 for (i = 0; i < used_rects; i++) {
00713 int x = wmv9rects[i].x;
00714 int y = wmv9rects[i].y;
00715 int w = wmv9rects[i].w;
00716 int h = wmv9rects[i].h;
00717 if (wmv9rects[i].coded) {
00718 int WMV9codedFrameSize;
00719 if (buf_size < 4 || !(WMV9codedFrameSize = AV_RL24(buf)))
00720 return AVERROR_INVALIDDATA;
00721 if (ret = decode_wmv9(avctx, buf + 3, buf_size - 3,
00722 x, y, w, h, wmv9_mask))
00723 return ret;
00724 buf += WMV9codedFrameSize + 3;
00725 buf_size -= WMV9codedFrameSize + 3;
00726 } else {
00727 uint8_t *dst = c->rgb_pic + y * c->rgb_stride + x * 3;
00728 if (wmv9_mask != -1) {
00729 ctx->dsp.mss2_gray_fill_masked(dst, c->rgb_stride,
00730 wmv9_mask,
00731 c->pal_pic + y * c->pal_stride + x,
00732 c->pal_stride,
00733 w, h);
00734 } else {
00735 do {
00736 memset(dst, 0x80, w * 3);
00737 dst += c->rgb_stride;
00738 } while (--h);
00739 }
00740 }
00741 }
00742 }
00743
00744 if (buf_size)
00745 av_log(avctx, AV_LOG_WARNING, "buffer not fully consumed\n");
00746
00747 *got_frame = 1;
00748 *(AVFrame *)data = ctx->pic;
00749
00750 return avpkt->size;
00751 }
00752
00753 static av_cold int wmv9_init(AVCodecContext *avctx)
00754 {
00755 VC1Context *v = avctx->priv_data;
00756
00757 v->s.avctx = avctx;
00758 avctx->flags |= CODEC_FLAG_EMU_EDGE;
00759 v->s.flags |= CODEC_FLAG_EMU_EDGE;
00760
00761 if (avctx->idct_algo == FF_IDCT_AUTO)
00762 avctx->idct_algo = FF_IDCT_WMV2;
00763
00764 if (ff_vc1_init_common(v) < 0)
00765 return -1;
00766 ff_vc1dsp_init(&v->vc1dsp);
00767
00768 v->profile = PROFILE_MAIN;
00769
00770 v->zz_8x4 = ff_wmv2_scantableA;
00771 v->zz_4x8 = ff_wmv2_scantableB;
00772 v->res_y411 = 0;
00773 v->res_sprite = 0;
00774
00775 v->frmrtq_postproc = 7;
00776 v->bitrtq_postproc = 31;
00777
00778 v->res_x8 = 0;
00779 v->multires = 0;
00780 v->res_fasttx = 1;
00781
00782 v->fastuvmc = 0;
00783
00784 v->extended_mv = 0;
00785
00786 v->dquant = 1;
00787 v->vstransform = 1;
00788
00789 v->res_transtab = 0;
00790
00791 v->overlap = 0;
00792
00793 v->s.resync_marker = 0;
00794 v->rangered = 0;
00795
00796 v->s.max_b_frames = avctx->max_b_frames = 0;
00797 v->quantizer_mode = 0;
00798
00799 v->finterpflag = 0;
00800
00801 v->res_rtm_flag = 1;
00802
00803 ff_vc1_init_transposed_scantables(v);
00804
00805 if (ff_msmpeg4_decode_init(avctx) < 0 ||
00806 ff_vc1_decode_init_alloc_tables(v) < 0)
00807 return -1;
00808
00809
00810 v->s.me.qpel_put = v->s.dsp.put_qpel_pixels_tab;
00811 v->s.me.qpel_avg = v->s.dsp.avg_qpel_pixels_tab;
00812
00813 return 0;
00814 }
00815
00816 static av_cold int mss2_decode_end(AVCodecContext *avctx)
00817 {
00818 MSS2Context *const ctx = avctx->priv_data;
00819
00820 if (ctx->pic.data[0])
00821 avctx->release_buffer(avctx, &ctx->pic);
00822 if (ctx->last_pic.data[0])
00823 avctx->release_buffer(avctx, &ctx->last_pic);
00824
00825 ff_mss12_decode_end(&ctx->c);
00826 av_freep(&ctx->c.pal_pic);
00827 av_freep(&ctx->c.last_pal_pic);
00828 ff_vc1_decode_end(avctx);
00829
00830 return 0;
00831 }
00832
00833 static av_cold int mss2_decode_init(AVCodecContext *avctx)
00834 {
00835 MSS2Context * const ctx = avctx->priv_data;
00836 MSS12Context *c = &ctx->c;
00837 int ret;
00838 c->avctx = avctx;
00839 avctx->coded_frame = &ctx->pic;
00840 if (ret = ff_mss12_decode_init(c, 1, &ctx->sc[0], &ctx->sc[1]))
00841 return ret;
00842 c->pal_stride = c->mask_stride;
00843 c->pal_pic = av_mallocz(c->pal_stride * avctx->height);
00844 c->last_pal_pic = av_mallocz(c->pal_stride * avctx->height);
00845 if (!c->pal_pic || !c->last_pal_pic) {
00846 mss2_decode_end(avctx);
00847 return AVERROR(ENOMEM);
00848 }
00849 if (ret = wmv9_init(avctx)) {
00850 mss2_decode_end(avctx);
00851 return ret;
00852 }
00853 ff_mss2dsp_init(&ctx->dsp);
00854
00855 avctx->pix_fmt = c->free_colours == 127 ? AV_PIX_FMT_RGB555
00856 : AV_PIX_FMT_RGB24;
00857
00858 return 0;
00859 }
00860
00861 AVCodec ff_mss2_decoder = {
00862 .name = "mss2",
00863 .type = AVMEDIA_TYPE_VIDEO,
00864 .id = AV_CODEC_ID_MSS2,
00865 .priv_data_size = sizeof(MSS2Context),
00866 .init = mss2_decode_init,
00867 .close = mss2_decode_end,
00868 .decode = mss2_decode_frame,
00869 .capabilities = CODEC_CAP_DR1,
00870 .long_name = NULL_IF_CONFIG_SMALL("MS Windows Media Video V9 Screen"),
00871 };