[FFmpeg-cvslog] MSS2 decoder
Alberto Delmás
git at videolan.org
Fri Aug 31 13:39:12 CEST 2012
ffmpeg | branch: master | Alberto Delmás <adelmas at gmail.com> | Fri Aug 24 17:45:57 2012 +0200| [ee769c6a7c1d4ec6560f5e5a6f457b770b10fb33] | committer: Kostya Shishkov
MSS2 decoder
Signed-off-by: Kostya Shishkov <kostya.shishkov at gmail.com>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=ee769c6a7c1d4ec6560f5e5a6f457b770b10fb33
---
Changelog | 1 +
configure | 1 +
doc/general.texi | 2 +
libavcodec/Makefile | 1 +
libavcodec/allcodecs.c | 1 +
libavcodec/avcodec.h | 1 +
libavcodec/codec_desc.c | 7 +
libavcodec/h263dec.c | 6 +-
libavcodec/mpegvideo.c | 13 +-
libavcodec/mss1.c | 106 +++---
libavcodec/mss12.c | 386 ++++++++++++++-------
libavcodec/mss12.h | 70 +++-
libavcodec/mss2.c | 860 +++++++++++++++++++++++++++++++++++++++++++++++
libavcodec/mss2dsp.c | 153 +++++++++
libavcodec/mss2dsp.h | 50 +++
libavcodec/vc1.c | 7 +-
libavcodec/vc1.h | 2 +
libavcodec/vc1dec.c | 10 +-
libavcodec/version.h | 4 +-
libavformat/riff.c | 1 +
20 files changed, 1466 insertions(+), 216 deletions(-)
diff --git a/Changelog b/Changelog
index 4fcce0e..fd67add 100644
--- a/Changelog
+++ b/Changelog
@@ -45,6 +45,7 @@ version <next>:
- avconv -shortest option is now per-output file,
-pass and -passlogfile are now per-output stream
- Ut Video encoder
+- Microsoft Screen 2 decoder
version 0.8:
diff --git a/configure b/configure
index b12df4e..ff051f5 100755
--- a/configure
+++ b/configure
@@ -1479,6 +1479,7 @@ msmpeg4v2_decoder_select="h263_decoder"
msmpeg4v2_encoder_select="h263_encoder"
msmpeg4v3_decoder_select="h263_decoder"
msmpeg4v3_encoder_select="h263_encoder"
+mss2_decoder_select="vc1_decoder"
nellymoser_decoder_select="mdct sinewin"
nellymoser_encoder_select="mdct sinewin"
png_decoder_select="zlib"
diff --git a/doc/general.texi b/doc/general.texi
index 7f22847..8415ca3 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -547,6 +547,8 @@ following image formats are supported:
@item Microsoft RLE @tab @tab X
@item Microsoft Screen 1 @tab @tab X
@tab Also known as Windows Media Video V7 Screen.
+ at item Microsoft Screen 2 @tab @tab X
+ @tab Also known as Windows Media Video V9 Screen.
@item Microsoft Video 1 @tab @tab X
@item Mimic @tab @tab X
@tab Used in MSN Messenger Webcam streams.
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index adbe1a3..7b3dc22 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -271,6 +271,7 @@ OBJS-$(CONFIG_MSMPEG4V3_ENCODER) += msmpeg4.o msmpeg4enc.o msmpeg4data.o \
OBJS-$(CONFIG_MSRLE_DECODER) += msrle.o msrledec.o
OBJS-$(CONFIG_MSA1_DECODER) += mss3.o mss34dsp.o
OBJS-$(CONFIG_MSS1_DECODER) += mss1.o mss12.o
+OBJS-$(CONFIG_MSS2_DECODER) += mss2.o mss12.o mss2dsp.o
OBJS-$(CONFIG_MSVIDEO1_DECODER) += msvideo1.o
OBJS-$(CONFIG_MSZH_DECODER) += lcldec.o
OBJS-$(CONFIG_MTS2_DECODER) += mss4.o mss34dsp.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index f7187d1..c6df818 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -160,6 +160,7 @@ void avcodec_register_all(void)
REGISTER_ENCDEC (MSMPEG4V3, msmpeg4v3);
REGISTER_DECODER (MSRLE, msrle);
REGISTER_DECODER (MSS1, mss1);
+ REGISTER_DECODER (MSS2, mss2);
REGISTER_DECODER (MSVIDEO1, msvideo1);
REGISTER_DECODER (MSZH, mszh);
REGISTER_DECODER (MTS2, mts2);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 742a2a3..8a091bd 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -264,6 +264,7 @@ enum AVCodecID {
AV_CODEC_ID_TSCC2,
AV_CODEC_ID_MTS2,
AV_CODEC_ID_CLLC,
+ AV_CODEC_ID_MSS2,
/* various PCM "codecs" */
AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 513a15c..51fc171 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1200,6 +1200,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
.long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
},
+ {
+ .id = AV_CODEC_ID_MSS2,
+ .type = AVMEDIA_TYPE_VIDEO,
+ .name = "mss2",
+ .long_name = NULL_IF_CONFIG_SMALL("MS Windows Media Video V9 Screen"),
+ .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
+ },
/* various PCM "codecs" */
{
diff --git a/libavcodec/h263dec.c b/libavcodec/h263dec.c
index 6bb6204..8e6085b 100644
--- a/libavcodec/h263dec.c
+++ b/libavcodec/h263dec.c
@@ -58,7 +58,10 @@ av_cold int ff_h263_decode_init(AVCodecContext *avctx)
s->quant_precision=5;
s->decode_mb= ff_h263_decode_mb;
s->low_delay= 1;
- avctx->pix_fmt= avctx->get_format(avctx, avctx->codec->pix_fmts);
+ if (avctx->codec->id == AV_CODEC_ID_MSS2)
+ avctx->pix_fmt = PIX_FMT_YUV420P;
+ else
+ avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
s->unrestricted_mv= 1;
/* select sub codec */
@@ -93,6 +96,7 @@ av_cold int ff_h263_decode_init(AVCodecContext *avctx)
case AV_CODEC_ID_WMV3:
case AV_CODEC_ID_VC1IMAGE:
case AV_CODEC_ID_WMV3IMAGE:
+ case AV_CODEC_ID_MSS2:
s->h263_pred = 1;
s->msmpeg4_version=6;
avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 13e2acc..e9aff3b 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -226,10 +226,11 @@ void ff_copy_picture(Picture *dst, Picture *src)
*/
static void free_frame_buffer(MpegEncContext *s, Picture *pic)
{
- /* Windows Media Image codecs allocate internal buffers with different
- * dimensions; ignore user defined callbacks for these
- */
- if (s->codec_id != AV_CODEC_ID_WMV3IMAGE && s->codec_id != AV_CODEC_ID_VC1IMAGE)
+ /* WM Image / Screen codecs allocate internal buffers with different
+ * dimensions / colorspaces; ignore user-defined callbacks for these. */
+ if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
+ s->codec_id != AV_CODEC_ID_VC1IMAGE &&
+ s->codec_id != AV_CODEC_ID_MSS2)
ff_thread_release_buffer(s->avctx, &pic->f);
else
avcodec_default_release_buffer(s->avctx, &pic->f);
@@ -254,7 +255,9 @@ static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
}
}
- if (s->codec_id != AV_CODEC_ID_WMV3IMAGE && s->codec_id != AV_CODEC_ID_VC1IMAGE)
+ if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
+ s->codec_id != AV_CODEC_ID_VC1IMAGE &&
+ s->codec_id != AV_CODEC_ID_MSS2)
r = ff_thread_get_buffer(s->avctx, &pic->f);
else
r = avcodec_default_get_buffer(s->avctx, &pic->f);
diff --git a/libavcodec/mss1.c b/libavcodec/mss1.c
index fe8ee10..ada479e 100644
--- a/libavcodec/mss1.c
+++ b/libavcodec/mss1.c
@@ -24,14 +24,13 @@
* Microsoft Screen 1 (aka Windows Media Video V7 Screen) decoder
*/
-#include "libavutil/intfloat.h"
-#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#include "mss12.h"
typedef struct MSS1Context {
MSS12Context ctx;
AVFrame pic;
+ SliceContext sc[2];
} MSS1Context;
static void arith_normalise(ArithCoder *c)
@@ -56,24 +55,11 @@ static void arith_normalise(ArithCoder *c)
c->low <<= 1;
c->high <<= 1;
c->high |= 1;
- c->value |= get_bits1(c->gb);
+ c->value |= get_bits1(c->gbc.gb);
}
}
-static int arith_get_bit(ArithCoder *c)
-{
- int range = c->high - c->low + 1;
- int bit = (((c->value - c->low) << 1) + 1) / range;
-
- if (bit)
- c->low += range >> 1;
- else
- c->high = c->low + (range >> 1) - 1;
-
- arith_normalise(c);
-
- return bit;
-}
+ARITH_GET_BIT()
static int arith_get_bits(ArithCoder *c, int bits)
{
@@ -118,40 +104,27 @@ static int arith_get_prob(ArithCoder *c, int *probs)
return sym;
}
-static int arith_get_model_sym(ArithCoder *c, Model *m)
-{
- int idx, val;
-
- idx = arith_get_prob(c, m->cum_prob);
-
- val = m->idx2sym[idx];
- ff_mss12_model_update(m, idx);
-
- arith_normalise(c);
-
- return val;
-}
+ARITH_GET_MODEL_SYM()
static void arith_init(ArithCoder *c, GetBitContext *gb)
{
- c->low = 0;
- c->high = 0xFFFF;
- c->value = get_bits(gb, 16);
- c->gb = gb;
-
+ c->low = 0;
+ c->high = 0xFFFF;
+ c->value = get_bits(gb, 16);
+ c->gbc.gb = gb;
c->get_model_sym = arith_get_model_sym;
c->get_number = arith_get_number;
}
-static int decode_pal(MSS1Context *ctx, ArithCoder *acoder)
+static int decode_pal(MSS12Context *ctx, ArithCoder *acoder)
{
int i, ncol, r, g, b;
- uint32_t *pal = ctx->ctx.pal + 256 - ctx->ctx.free_colours;
+ uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
- if (!ctx->ctx.free_colours)
+ if (!ctx->free_colours)
return 0;
- ncol = arith_get_number(acoder, ctx->ctx.free_colours + 1);
+ ncol = arith_get_number(acoder, ctx->free_colours + 1);
for (i = 0; i < ncol; i++) {
r = arith_get_bits(acoder, 8);
g = arith_get_bits(acoder, 8);
@@ -167,7 +140,8 @@ static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
- MSS1Context *c = avctx->priv_data;
+ MSS1Context *ctx = avctx->priv_data;
+ MSS12Context *c = &ctx->ctx;
GetBitContext gb;
ArithCoder acoder;
int pal_changed = 0;
@@ -176,37 +150,37 @@ static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
init_get_bits(&gb, buf, buf_size * 8);
arith_init(&acoder, &gb);
- c->pic.reference = 3;
- c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
- FF_BUFFER_HINTS_REUSABLE;
- if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
+ ctx->pic.reference = 3;
+ ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
+ FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
+ if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {
av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
return ret;
}
- c->ctx.pic_start = c->pic.data[0] + c->pic.linesize[0] * (avctx->height - 1);
- c->ctx.pic_stride = -c->pic.linesize[0];
- c->ctx.keyframe = !arith_get_bit(&acoder);
- if (c->ctx.keyframe) {
- ff_mss12_codec_reset(&c->ctx);
- pal_changed = decode_pal(c, &acoder);
- c->pic.key_frame = 1;
- c->pic.pict_type = AV_PICTURE_TYPE_I;
+ c->pal_pic = ctx->pic.data[0] + ctx->pic.linesize[0] * (avctx->height - 1);
+ c->pal_stride = -ctx->pic.linesize[0];
+ c->keyframe = !arith_get_bit(&acoder);
+ if (c->keyframe) {
+ ff_mss12_codec_reset(c);
+ pal_changed = decode_pal(c, &acoder);
+ ctx->pic.key_frame = 1;
+ ctx->pic.pict_type = AV_PICTURE_TYPE_I;
} else {
- if (c->ctx.corrupted)
+ if (c->corrupted)
return AVERROR_INVALIDDATA;
- c->pic.key_frame = 0;
- c->pic.pict_type = AV_PICTURE_TYPE_P;
+ ctx->pic.key_frame = 0;
+ ctx->pic.pict_type = AV_PICTURE_TYPE_P;
}
- c->ctx.corrupted = ff_mss12_decode_rect(&c->ctx, &acoder, 0, 0,
- avctx->width, avctx->height);
- if (c->ctx.corrupted)
+ c->corrupted = ff_mss12_decode_rect(&c->sc[0], &acoder, 0, 0,
+ avctx->width, avctx->height);
+ if (c->corrupted)
return AVERROR_INVALIDDATA;
- memcpy(c->pic.data[1], c->ctx.pal, AVPALETTE_SIZE);
- c->pic.palette_has_changed = pal_changed;
+ memcpy(ctx->pic.data[1], c->pal, AVPALETTE_SIZE);
+ ctx->pic.palette_has_changed = pal_changed;
*data_size = sizeof(AVFrame);
- *(AVFrame*)data = c->pic;
+ *(AVFrame*)data = ctx->pic;
/* always report that the buffer was completely consumed */
return buf_size;
@@ -219,16 +193,16 @@ static av_cold int mss1_decode_init(AVCodecContext *avctx)
c->ctx.avctx = avctx;
avctx->coded_frame = &c->pic;
- return ff_mss12_decode_init(avctx, 0);
+ return ff_mss12_decode_init(&c->ctx, 0);
}
static av_cold int mss1_decode_end(AVCodecContext *avctx)
{
- MSS1Context * const c = avctx->priv_data;
+ MSS1Context * const ctx = avctx->priv_data;
- if (c->pic.data[0])
- avctx->release_buffer(avctx, &c->pic);
- ff_mss12_decode_end(avctx);
+ if (ctx->pic.data[0])
+ avctx->release_buffer(avctx, &ctx->pic);
+ ff_mss12_decode_end(&ctx->ctx);
return 0;
}
diff --git a/libavcodec/mss12.c b/libavcodec/mss12.c
index 8b4f52b..f0f23c0 100644
--- a/libavcodec/mss12.c
+++ b/libavcodec/mss12.c
@@ -47,12 +47,8 @@ static int model_calc_threshold(Model *m)
{
int thr;
- if (m->thr_weight == -1) {
- thr = 2 * m->weights[m->num_syms] - 1;
- thr = ((thr >> 1) + 4 * m->cum_prob[0]) / thr;
- } else {
- thr = m->num_syms * m->thr_weight;
- }
+ thr = 2 * m->weights[m->num_syms] - 1;
+ thr = ((thr >> 1) + 4 * m->cum_prob[0]) / thr;
return FFMIN(thr, 0x3FFF);
}
@@ -78,7 +74,7 @@ static av_cold void model_init(Model *m, int num_syms, int thr_weight)
{
m->num_syms = num_syms;
m->thr_weight = thr_weight;
- m->threshold = model_calc_threshold(m);
+ m->threshold = num_syms * thr_weight;
model_reset(m);
}
@@ -87,7 +83,7 @@ static void model_rescale_weights(Model *m)
int i;
int cum_prob;
- if (m->thr_weight == -1)
+ if (m->thr_weight == THRESH_ADAPTIVE)
m->threshold = model_calc_threshold(m);
while (m->cum_prob[0] > m->threshold) {
cum_prob = 0;
@@ -129,8 +125,14 @@ static void pixctx_reset(PixContext *ctx)
{
int i, j, k;
- for (i = 0; i < ctx->cache_size; i++)
- ctx->cache[i] = i;
+ if (!ctx->special_initial_cache)
+ for (i = 0; i < ctx->cache_size; i++)
+ ctx->cache[i] = i;
+ else {
+ ctx->cache[0] = 1;
+ ctx->cache[1] = 2;
+ ctx->cache[2] = 4;
+ }
model_reset(&ctx->cache_model);
model_reset(&ctx->full_model);
@@ -141,27 +143,23 @@ static void pixctx_reset(PixContext *ctx)
model_reset(&ctx->sec_models[i][j][k]);
}
-static av_cold void pixctx_init(PixContext *ctx, int cache_size)
+static av_cold void pixctx_init(PixContext *ctx, int cache_size,
+ int full_model_syms, int special_initial_cache)
{
int i, j, k;
- ctx->cache_size = cache_size + 4;
- ctx->num_syms = cache_size;
-
- for (i = 0; i < ctx->cache_size; i++)
- ctx->cache[i] = i;
+ ctx->cache_size = cache_size + 4;
+ ctx->num_syms = cache_size;
+ ctx->special_initial_cache = special_initial_cache;
model_init(&ctx->cache_model, ctx->num_syms + 1, THRESH_LOW);
- model_init(&ctx->full_model, 256, THRESH_HIGH);
+ model_init(&ctx->full_model, full_model_syms, THRESH_HIGH);
- for (i = 0; i < 4; i++) {
- for (j = 0; j < sec_order_sizes[i]; j++) {
- for (k = 0; k < 4; k++) {
+ for (i = 0; i < 4; i++)
+ for (j = 0; j < sec_order_sizes[i]; j++)
+ for (k = 0; k < 4; k++)
model_init(&ctx->sec_models[i][j][k], 2 + i,
i ? THRESH_LOW : THRESH_ADAPTIVE);
- }
- }
- }
}
static int decode_top_left_pixel(ArithCoder *acoder, PixContext *pctx)
@@ -196,7 +194,6 @@ static int decode_pixel(ArithCoder *acoder, PixContext *pctx,
if (val < pctx->num_syms) {
int idx, j;
-
idx = 0;
for (i = 0; i < pctx->cache_size; i++) {
for (j = 0; j < num_ngb; j++)
@@ -309,195 +306,288 @@ static int decode_pixel_in_context(ArithCoder *acoder, PixContext *pctx,
break;
}
- pix = acoder->get_model_sym(acoder, &pctx->sec_models[nlen - 1][layer][sub]);
+ pix = acoder->get_model_sym(acoder,
+ &pctx->sec_models[nlen - 1][layer][sub]);
if (pix < nlen)
return ref_pix[pix];
else
return decode_pixel(acoder, pctx, ref_pix, nlen);
}
-static int decode_region(MSS12Context *ctx, ArithCoder *acoder, uint8_t *dst,
+static int decode_region(ArithCoder *acoder, uint8_t *dst, uint8_t *rgb_pic,
int x, int y, int width, int height, int stride,
- PixContext *pctx)
+ int rgb_stride, PixContext *pctx, const uint32_t *pal)
{
- int i, j;
+ int i, j, p;
+ uint8_t *rgb_dst = rgb_pic + x * 3 + y * rgb_stride;
dst += x + y * stride;
- dst[0] = decode_top_left_pixel(acoder, pctx);
for (j = 0; j < height; j++) {
for (i = 0; i < width; i++) {
if (!i && !j)
- continue;
+ p = decode_top_left_pixel(acoder, pctx);
+ else
+ p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
+ i, j, width - i - 1);
+ dst[i] = p;
- dst[i] = decode_pixel_in_context(acoder, pctx, dst + i, stride,
- i, j, width - i - 1);
+ if (rgb_pic)
+ AV_WB24(rgb_dst + i * 3, pal[p]);
}
- dst += stride;
+ dst += stride;
+ rgb_dst += rgb_stride;
}
return 0;
}
-static int decode_region_masked(MSS12Context *ctx, ArithCoder *acoder,
+static void copy_rectangles(MSS12Context const *c,
+ int x, int y, int width, int height)
+{
+ int j;
+
+ if (c->last_rgb_pic)
+ for (j = y; j < y + height; j++) {
+ memcpy(c->rgb_pic + j * c->rgb_stride + x * 3,
+ c->last_rgb_pic + j * c->rgb_stride + x * 3,
+ width * 3);
+ memcpy(c->pal_pic + j * c->pal_stride + x,
+ c->last_pal_pic + j * c->pal_stride + x,
+ width);
+ }
+}
+
+static int motion_compensation(MSS12Context const *c,
+ int x, int y, int width, int height)
+{
+ if (x + c->mvX < 0 || x + c->mvX + width > c->avctx->width ||
+ y + c->mvY < 0 || y + c->mvY + height > c->avctx->height ||
+ !c->rgb_pic)
+ return -1;
+ else {
+ uint8_t *dst = c->pal_pic + x + y * c->pal_stride;
+ uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
+ uint8_t *src;
+ uint8_t *rgb_src;
+ int j;
+ x += c->mvX;
+ y += c->mvY;
+ if (c->last_rgb_pic) {
+ src = c->last_pal_pic + x + y * c->pal_stride;
+ rgb_src = c->last_rgb_pic + x * 3 + y * c->rgb_stride;
+ } else {
+ src = c->pal_pic + x + y * c->pal_stride;
+ rgb_src = c->rgb_pic + x * 3 + y * c->rgb_stride;
+ }
+ for (j = 0; j < height; j++) {
+ memmove(dst, src, width);
+ memmove(rgb_dst, rgb_src, width * 3);
+ dst += c->pal_stride;
+ src += c->pal_stride;
+ rgb_dst += c->rgb_stride;
+ rgb_src += c->rgb_stride;
+ }
+ }
+ return 0;
+}
+
+static int decode_region_masked(MSS12Context const *c, ArithCoder *acoder,
uint8_t *dst, int stride, uint8_t *mask,
int mask_stride, int x, int y,
int width, int height,
PixContext *pctx)
{
- int i, j;
+ int i, j, p;
+ uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
dst += x + y * stride;
mask += x + y * mask_stride;
- if (mask[0] == 0xFF)
- dst[0] = decode_top_left_pixel(acoder, pctx);
for (j = 0; j < height; j++) {
for (i = 0; i < width; i++) {
- if (!i && !j || mask[i] != 0xFF)
- continue;
-
- dst[i] = decode_pixel_in_context(acoder, pctx, dst + i, stride,
- i, j, width - i - 1);
+ if (c->avctx->err_recognition & AV_EF_EXPLODE &&
+ ( c->rgb_pic && mask[i] != 0x01 && mask[i] != 0x02 && mask[i] != 0x04 ||
+ !c->rgb_pic && mask[i] != 0x80 && mask[i] != 0xFF))
+ return -1;
+
+ if (mask[i] == 0x02) {
+ copy_rectangles(c, x + i, y + j, 1, 1);
+ } else if (mask[i] == 0x04) {
+ if (motion_compensation(c, x + i, y + j, 1, 1))
+ return -1;
+ } else if (mask[i] != 0x80) {
+ if (!i && !j)
+ p = decode_top_left_pixel(acoder, pctx);
+ else
+ p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
+ i, j, width - i - 1);
+ dst[i] = p;
+ if (c->rgb_pic)
+ AV_WB24(rgb_dst + i * 3, c->pal[p]);
+ }
}
- dst += stride;
- mask += mask_stride;
+ dst += stride;
+ mask += mask_stride;
+ rgb_dst += c->rgb_stride;
}
return 0;
}
-static av_cold void codec_init(MSS12Context *ctx)
+static av_cold void codec_init(MSS12Context *c, int version)
{
- model_init(&ctx->intra_region, 2, THRESH_ADAPTIVE);
- model_init(&ctx->inter_region, 2, THRESH_ADAPTIVE);
- model_init(&ctx->split_mode, 3, THRESH_HIGH);
- model_init(&ctx->edge_mode, 2, THRESH_HIGH);
- model_init(&ctx->pivot, 3, THRESH_LOW);
- pixctx_init(&ctx->intra_pix_ctx, 8);
- pixctx_init(&ctx->inter_pix_ctx, 2);
- ctx->corrupted = 1;
+ int i;
+ for (i = 0; i < (c->slice_split ? 2 : 1); i++) {
+ c->sc[i].c = c;
+ model_init(&c->sc[i].intra_region, 2, THRESH_ADAPTIVE);
+ model_init(&c->sc[i].inter_region, 2, THRESH_ADAPTIVE);
+ model_init(&c->sc[i].split_mode, 3, THRESH_HIGH);
+ model_init(&c->sc[i].edge_mode, 2, THRESH_HIGH);
+ model_init(&c->sc[i].pivot, 3, THRESH_LOW);
+
+ pixctx_init(&c->sc[i].intra_pix_ctx, 8, c->full_model_syms, 0);
+
+ pixctx_init(&c->sc[i].inter_pix_ctx, version ? 3 : 2,
+ c->full_model_syms, version ? 1 : 0);
+ }
+ c->corrupted = 1;
}
-void ff_mss12_codec_reset(MSS12Context *ctx)
+void ff_mss12_codec_reset(MSS12Context *c)
{
- model_reset(&ctx->intra_region);
- model_reset(&ctx->inter_region);
- model_reset(&ctx->split_mode);
- model_reset(&ctx->edge_mode);
- model_reset(&ctx->pivot);
- pixctx_reset(&ctx->intra_pix_ctx);
- pixctx_reset(&ctx->inter_pix_ctx);
-
- ctx->corrupted = 0;
+ int i;
+ for (i = 0; i < (c->slice_split ? 2 : 1); i++) {
+ model_reset(&c->sc[i].intra_region);
+ model_reset(&c->sc[i].inter_region);
+ model_reset(&c->sc[i].split_mode);
+ model_reset(&c->sc[i].edge_mode);
+ model_reset(&c->sc[i].pivot);
+ pixctx_reset(&c->sc[i].intra_pix_ctx);
+ pixctx_reset(&c->sc[i].inter_pix_ctx);
+ }
+
+ c->corrupted = 0;
}
-static int decode_pivot(MSS12Context *ctx, ArithCoder *acoder, int base)
+static int decode_pivot(SliceContext *sc, ArithCoder *acoder, int base)
{
int val, inv;
- inv = acoder->get_model_sym(acoder, &ctx->edge_mode);
- val = acoder->get_model_sym(acoder, &ctx->pivot) + 1;
+ inv = acoder->get_model_sym(acoder, &sc->edge_mode);
+ val = acoder->get_model_sym(acoder, &sc->pivot) + 1;
if (val > 2) {
- if ((base + 1) / 2 - 2 <= 0) {
- ctx->corrupted = 1;
- return 0;
- }
+ if ((base + 1) / 2 - 2 <= 0)
+ return -1;
+
val = acoder->get_number(acoder, (base + 1) / 2 - 2) + 3;
}
- if (val == base) {
- ctx->corrupted = 1;
- return 0;
- }
+ if (val >= base)
+ return -1;
return inv ? base - val : val;
}
-static int decode_region_intra(MSS12Context *ctx, ArithCoder *acoder,
+static int decode_region_intra(SliceContext *sc, ArithCoder *acoder,
int x, int y, int width, int height)
{
+ MSS12Context const *c = sc->c;
int mode;
- mode = acoder->get_model_sym(acoder, &ctx->intra_region);
+ mode = acoder->get_model_sym(acoder, &sc->intra_region);
if (!mode) {
- int i, pix;
- int stride = ctx->pic_stride;
- uint8_t *dst = ctx->pic_start + x + y * stride;
-
- pix = decode_top_left_pixel(acoder, &ctx->intra_pix_ctx);
- for (i = 0; i < height; i++, dst += stride)
+ int i, j, pix, rgb_pix;
+ int stride = c->pal_stride;
+ int rgb_stride = c->rgb_stride;
+ uint8_t *dst = c->pal_pic + x + y * stride;
+ uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * rgb_stride;
+
+ pix = decode_top_left_pixel(acoder, &sc->intra_pix_ctx);
+ rgb_pix = c->pal[pix];
+ for (i = 0; i < height; i++, dst += stride, rgb_dst += rgb_stride) {
memset(dst, pix, width);
+ if (c->rgb_pic)
+ for (j = 0; j < width * 3; j += 3)
+ AV_WB24(rgb_dst + j, rgb_pix);
+ }
} else {
- return decode_region(ctx, acoder, ctx->pic_start,
- x, y, width, height, ctx->pic_stride,
- &ctx->intra_pix_ctx);
+ return decode_region(acoder, c->pal_pic, c->rgb_pic,
+ x, y, width, height, c->pal_stride, c->rgb_stride,
+ &sc->intra_pix_ctx, &c->pal[0]);
}
return 0;
}
-static int decode_region_inter(MSS12Context *ctx, ArithCoder *acoder,
+static int decode_region_inter(SliceContext *sc, ArithCoder *acoder,
int x, int y, int width, int height)
{
+ MSS12Context const *c = sc->c;
int mode;
- mode = acoder->get_model_sym(acoder, &ctx->inter_region);
+ mode = acoder->get_model_sym(acoder, &sc->inter_region);
if (!mode) {
- mode = decode_top_left_pixel(acoder, &ctx->inter_pix_ctx);
- if (mode != 0xFF) {
- return 0;
- } else {
- return decode_region_intra(ctx, acoder, x, y, width, height);
- }
+ mode = decode_top_left_pixel(acoder, &sc->inter_pix_ctx);
+
+ if (c->avctx->err_recognition & AV_EF_EXPLODE &&
+ ( c->rgb_pic && mode != 0x01 && mode != 0x02 && mode != 0x04 ||
+ !c->rgb_pic && mode != 0x80 && mode != 0xFF))
+ return -1;
+
+ if (mode == 0x02)
+ copy_rectangles(c, x, y, width, height);
+ else if (mode == 0x04)
+ return motion_compensation(c, x, y, width, height);
+ else if (mode != 0x80)
+ return decode_region_intra(sc, acoder, x, y, width, height);
} else {
- if (decode_region(ctx, acoder, ctx->mask,
- x, y, width, height, ctx->mask_linesize,
- &ctx->inter_pix_ctx) < 0)
+ if (decode_region(acoder, c->mask, NULL,
+ x, y, width, height, c->mask_stride, 0,
+ &sc->inter_pix_ctx, &c->pal[0]) < 0)
return -1;
- return decode_region_masked(ctx, acoder, ctx->pic_start,
- ctx->pic_stride, ctx->mask,
- ctx->mask_linesize,
+ return decode_region_masked(c, acoder, c->pal_pic,
+ c->pal_stride, c->mask,
+ c->mask_stride,
x, y, width, height,
- &ctx->intra_pix_ctx);
+ &sc->intra_pix_ctx);
}
return 0;
}
-int ff_mss12_decode_rect(MSS12Context *ctx, ArithCoder *acoder,
+int ff_mss12_decode_rect(SliceContext *sc, ArithCoder *acoder,
int x, int y, int width, int height)
{
int mode, pivot;
- if (ctx->corrupted)
- return -1;
-
- mode = acoder->get_model_sym(acoder, &ctx->split_mode);
+ mode = acoder->get_model_sym(acoder, &sc->split_mode);
switch (mode) {
case SPLIT_VERT:
- pivot = decode_pivot(ctx, acoder, height);
- if (ff_mss12_decode_rect(ctx, acoder, x, y, width, pivot))
+ if ((pivot = decode_pivot(sc, acoder, height)) < 1)
return -1;
- if (ff_mss12_decode_rect(ctx, acoder, x, y + pivot, width, height - pivot))
+ if (ff_mss12_decode_rect(sc, acoder, x, y, width, pivot))
+ return -1;
+ if (ff_mss12_decode_rect(sc, acoder, x, y + pivot, width, height - pivot))
return -1;
break;
case SPLIT_HOR:
- pivot = decode_pivot(ctx, acoder, width);
- if (ff_mss12_decode_rect(ctx, acoder, x, y, pivot, height))
+ if ((pivot = decode_pivot(sc, acoder, width)) < 1)
+ return -1;
+ if (ff_mss12_decode_rect(sc, acoder, x, y, pivot, height))
return -1;
- if (ff_mss12_decode_rect(ctx, acoder, x + pivot, y, width - pivot, height))
+ if (ff_mss12_decode_rect(sc, acoder, x + pivot, y, width - pivot, height))
return -1;
break;
case SPLIT_NONE:
- if (ctx->keyframe)
- return decode_region_intra(ctx, acoder, x, y, width, height);
+ if (sc->c->keyframe)
+ return decode_region_intra(sc, acoder, x, y, width, height);
else
- return decode_region_inter(ctx, acoder, x, y, width, height);
+ return decode_region_inter(sc, acoder, x, y, width, height);
default:
return -1;
}
@@ -505,13 +595,11 @@ int ff_mss12_decode_rect(MSS12Context *ctx, ArithCoder *acoder,
return 0;
}
-av_cold int ff_mss12_decode_init(AVCodecContext *avctx, int version)
+av_cold int ff_mss12_decode_init(MSS12Context *c, int version)
{
- MSS12Context * const c = avctx->priv_data;
+ AVCodecContext *avctx = c->avctx;
int i;
- c->avctx = avctx;
-
if (avctx->extradata_size < 52 + 256 * 3) {
av_log(avctx, AV_LOG_ERROR, "Insufficient extradata size %d\n",
avctx->extradata_size);
@@ -526,9 +614,23 @@ av_cold int ff_mss12_decode_init(AVCodecContext *avctx, int version)
return AVERROR_INVALIDDATA;
}
+ avctx->coded_width = AV_RB32(avctx->extradata + 20);
+ avctx->coded_height = AV_RB32(avctx->extradata + 24);
+ if (avctx->coded_width > 4096 || avctx->coded_height > 4096) {
+ av_log(avctx, AV_LOG_ERROR, "Frame dimensions %dx%d too large",
+ avctx->coded_width, avctx->coded_height);
+ return AVERROR_INVALIDDATA;
+ }
+
av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d\n",
AV_RB32(avctx->extradata + 4), AV_RB32(avctx->extradata + 8));
- c->free_colours = AV_RB32(avctx->extradata + 48);
+ if (version != AV_RB32(avctx->extradata + 4) > 1) {
+ av_log(avctx, AV_LOG_ERROR,
+ "Header version doesn't match codec tag\n");
+ return -1;
+ }
+
+ c->free_colours = AV_RB32(avctx->extradata + 48);
if ((unsigned)c->free_colours > 256) {
av_log(avctx, AV_LOG_ERROR,
"Incorrect number of changeable palette entries: %d\n",
@@ -536,8 +638,6 @@ av_cold int ff_mss12_decode_init(AVCodecContext *avctx, int version)
return AVERROR_INVALIDDATA;
}
av_log(avctx, AV_LOG_DEBUG, "%d free colour(s)\n", c->free_colours);
- avctx->coded_width = AV_RB32(avctx->extradata + 20);
- avctx->coded_height = AV_RB32(avctx->extradata + 24);
av_log(avctx, AV_LOG_DEBUG, "Display dimensions %dx%d\n",
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)
av_log(avctx, AV_LOG_DEBUG, "Max. seek time %g ms\n",
av_int2float(AV_RB32(avctx->extradata + 44)));
- for (i = 0; i < 256; i++)
- c->pal[i] = AV_RB24(avctx->extradata + 52 + i * 3);
+ if (version) {
+ if (avctx->extradata_size < 60 + 256 * 3) {
+ av_log(avctx, AV_LOG_ERROR,
+ "Insufficient extradata size %d for v2\n",
+ avctx->extradata_size);
+ return AVERROR_INVALIDDATA;
+ }
+
+ c->slice_split = AV_RB32(avctx->extradata + 52);
+ av_log(avctx, AV_LOG_DEBUG, "Slice split %d\n", c->slice_split);
- avctx->pix_fmt = PIX_FMT_PAL8;
+ c->full_model_syms = AV_RB32(avctx->extradata + 56);
+ if (c->full_model_syms < 2 || c->full_model_syms > 256) {
+ av_log(avctx, AV_LOG_ERROR,
+ "Incorrect number of used colours %d\n",
+ c->full_model_syms);
+ return AVERROR_INVALIDDATA;
+ }
+ av_log(avctx, AV_LOG_DEBUG, "Used colours %d\n",
+ c->full_model_syms);
+ } else {
+ c->slice_split = 0;
+ c->full_model_syms = 256;
+ }
- c->mask_linesize = FFALIGN(avctx->width, 16);
- c->mask = av_malloc(c->mask_linesize * avctx->height);
+ for (i = 0; i < 256; i++)
+ c->pal[i] = AV_RB24(avctx->extradata + 52 +
+ (version ? 8 : 0) + i * 3);
+
+ c->mask_stride = FFALIGN(avctx->width, 16);
+ c->mask = av_malloc(c->mask_stride * avctx->height);
if (!c->mask) {
av_log(avctx, AV_LOG_ERROR, "Cannot allocate mask plane\n");
return AVERROR(ENOMEM);
}
- codec_init(c);
+ avctx->pix_fmt = version ? c->free_colours == 127 ? PIX_FMT_RGB555
+ : PIX_FMT_RGB24
+ : PIX_FMT_PAL8;
+
+ codec_init(c, version);
return 0;
}
-av_cold int ff_mss12_decode_end(AVCodecContext *avctx)
+av_cold int ff_mss12_decode_end(MSS12Context *c)
{
- MSS12Context * const c = avctx->priv_data;
-
av_freep(&c->mask);
return 0;
diff --git a/libavcodec/mss12.h b/libavcodec/mss12.h
index 383d86c..93d1f61 100644
--- a/libavcodec/mss12.h
+++ b/libavcodec/mss12.h
@@ -26,8 +26,10 @@
#ifndef AVCODEC_MSS12_H
#define AVCODEC_MSS12_H
+#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#include "get_bits.h"
+#include "bytestream.h"
#define MODEL_MIN_SYMS 2
#define MODEL_MAX_SYMS 256
@@ -46,7 +48,10 @@ typedef struct Model {
typedef struct ArithCoder {
int low, high, value;
- GetBitContext *gb;
+ union {
+ GetBitContext *gb;
+ GetByteContext *gB;
+ } gbc;
int (*get_model_sym)(struct ArithCoder *c, Model *m);
int (*get_number) (struct ArithCoder *c, int n);
} ArithCoder;
@@ -56,28 +61,77 @@ typedef struct PixContext {
uint8_t cache[12];
Model cache_model, full_model;
Model sec_models[4][8][4];
+ int special_initial_cache;
} PixContext;
+struct MSS12Context;
+
+typedef struct SliceContext {
+ struct MSS12Context *c;
+ Model intra_region, inter_region;
+ Model pivot, edge_mode, split_mode;
+ PixContext intra_pix_ctx, inter_pix_ctx;
+} SliceContext;
+
typedef struct MSS12Context {
AVCodecContext *avctx;
- uint8_t *pic_start;
- int pic_stride;
- uint8_t *mask;
- int mask_linesize;
uint32_t pal[256];
+ uint8_t *pal_pic;
+ uint8_t *last_pal_pic;
+ int pal_stride;
+ uint8_t *mask;
+ int mask_stride;
+ uint8_t *rgb_pic;
+ uint8_t *last_rgb_pic;
+ int rgb_stride;
int free_colours;
int keyframe;
Model intra_region, inter_region;
Model pivot, edge_mode, split_mode;
PixContext intra_pix_ctx, inter_pix_ctx;
+ int mvX, mvY;
int corrupted;
+ int slice_split;
+ int full_model_syms;
+ SliceContext sc[2];
} MSS12Context;
-int ff_mss12_decode_rect(MSS12Context *ctx, ArithCoder *acoder,
+int ff_mss12_decode_rect(SliceContext *ctx, ArithCoder *acoder,
int x, int y, int width, int height);
void ff_mss12_model_update(Model *m, int val);
void ff_mss12_codec_reset(MSS12Context *ctx);
-av_cold int ff_mss12_decode_init(AVCodecContext *avctx, int version);
-av_cold int ff_mss12_decode_end(AVCodecContext *avctx);
+av_cold int ff_mss12_decode_init(MSS12Context *ctx, int version);
+av_cold int ff_mss12_decode_end(MSS12Context *ctx);
+
+#define ARITH_GET_BIT(VERSION) \
+static int arith ## VERSION ## _get_bit(ArithCoder *c) \
+{ \
+ int range = c->high - c->low + 1; \
+ int bit = (((c->value - c->low) << 1) + 1) / range; \
+ \
+ if (bit) \
+ c->low += range >> 1; \
+ else \
+ c->high = c->low + (range >> 1) - 1; \
+ \
+ arith ## VERSION ## _normalise(c); \
+ \
+ return bit; \
+}
+
+#define ARITH_GET_MODEL_SYM(VERSION) \
+static int arith ## VERSION ## _get_model_sym(ArithCoder *c, Model *m) \
+{ \
+ int idx, val; \
+ \
+ idx = arith ## VERSION ## _get_prob(c, m->cum_prob); \
+ \
+ val = m->idx2sym[idx]; \
+ ff_mss12_model_update(m, idx); \
+ \
+ arith ## VERSION ## _normalise(c); \
+ \
+ return val; \
+}
#endif /* AVCODEC_MSS12_H */
diff --git a/libavcodec/mss2.c b/libavcodec/mss2.c
new file mode 100644
index 0000000..c0c47dc
--- /dev/null
+++ b/libavcodec/mss2.c
@@ -0,0 +1,860 @@
+/*
+ * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder
+ *
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder
+ */
+
+#include "libavutil/avassert.h"
+#include "msmpeg4data.h"
+#include "vc1.h"
+#include "mss12.h"
+#include "mss2dsp.h"
+
+typedef struct MSS2Context {
+ VC1Context v;
+ int split_position;
+ AVFrame pic;
+ AVFrame last_pic;
+ MSS12Context c;
+ MSS2DSPContext dsp;
+ SliceContext sc[2];
+} MSS2Context;
+
+static void arith2_normalise(ArithCoder *c)
+{
+ while ((c->high >> 15) - (c->low >> 15) < 2) {
+ if ((c->low ^ c->high) & 0x10000) {
+ c->high ^= 0x8000;
+ c->value ^= 0x8000;
+ c->low ^= 0x8000;
+ }
+ c->high = c->high << 8 & 0xFFFFFF | 0xFF;
+ c->value = c->value << 8 & 0xFFFFFF | bytestream2_get_byte(c->gbc.gB);
+ c->low = c->low << 8 & 0xFFFFFF;
+ }
+}
+
+ARITH_GET_BIT(2)
+
+/* L. Stuiver and A. Moffat: "Piecewise Integer Mapping for Arithmetic Coding."
+ * In Proc. 8th Data Compression Conference (DCC '98), pp. 3-12, Mar. 1998 */
+
+static int arith2_get_scaled_value(int value, int n, int range)
+{
+ int split = (n << 1) - range;
+
+ if (value > split)
+ return split + (value - split >> 1);
+ else
+ return value;
+}
+
+static void arith2_rescale_interval(ArithCoder *c, int range,
+ int low, int high, int n)
+{
+ int split = (n << 1) - range;
+
+ if (high > split)
+ c->high = split + (high - split << 1);
+ else
+ c->high = high;
+
+ c->high += c->low - 1;
+
+ if (low > split)
+ c->low += split + (low - split << 1);
+ else
+ c->low += low;
+}
+
+static int arith2_get_number(ArithCoder *c, int n)
+{
+ int range = c->high - c->low + 1;
+ int scale = av_log2(range) - av_log2(n);
+ int val;
+
+ if (n << scale > range)
+ scale--;
+
+ n <<= scale;
+
+ val = arith2_get_scaled_value(c->value - c->low, n, range) >> scale;
+
+ arith2_rescale_interval(c, range, val << scale, (val + 1) << scale, n);
+
+ arith2_normalise(c);
+
+ return val;
+}
+
+static int arith2_get_prob(ArithCoder *c, int *probs)
+{
+ int range = c->high - c->low + 1, n = *probs;
+ int scale = av_log2(range) - av_log2(n);
+ int i = 0, val;
+
+ if (n << scale > range)
+ scale--;
+
+ n <<= scale;
+
+ val = arith2_get_scaled_value(c->value - c->low, n, range) >> scale;
+ while (probs[++i] > val) ;
+
+ arith2_rescale_interval(c, range,
+ probs[i] << scale, probs[i - 1] << scale, n);
+
+ return i;
+}
+
+ARITH_GET_MODEL_SYM(2)
+
+static int arith2_get_consumed_bytes(ArithCoder *c)
+{
+ int diff = (c->high >> 16) - (c->low >> 16);
+ int bp = bytestream2_tell(c->gbc.gB) - 3 << 3;
+ int bits = 1;
+
+ while (!(diff & 0x80)) {
+ bits++;
+ diff <<= 1;
+ }
+
+ return (bits + bp + 7 >> 3) + ((c->low >> 16) + 1 == c->high >> 16);
+}
+
+static void arith2_init(ArithCoder *c, GetByteContext *gB)
+{
+ c->low = 0;
+ c->high = 0xFFFFFF;
+ c->value = bytestream2_get_be24(gB);
+ c->gbc.gB = gB;
+ c->get_model_sym = arith2_get_model_sym;
+ c->get_number = arith2_get_number;
+}
+
+static int decode_pal_v2(MSS12Context *ctx, const uint8_t *buf, int buf_size)
+{
+ int i, ncol;
+ uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
+
+ if (!ctx->free_colours)
+ return 0;
+
+ ncol = *buf++;
+ if (buf_size < 2 + ncol * 3)
+ return -1;
+ for (i = 0; i < ncol; i++)
+ *pal++ = AV_RB24(buf + 3 * i);
+
+ return 1 + ncol * 3;
+}
+
+static int decode_555(GetByteContext *gB, uint16_t *dst, int stride,
+ int keyframe, int w, int h)
+{
+ int last_symbol = 0, repeat = 0, prev_avail = 0;
+
+ if (!keyframe) {
+ int x, y, endx, endy, t;
+
+#define READ_PAIR(a, b) \
+ a = bytestream2_get_byte(gB) << 4; \
+ t = bytestream2_get_byte(gB); \
+ a |= t >> 4; \
+ b = (t & 0xF) << 8; \
+ b |= bytestream2_get_byte(gB); \
+
+ READ_PAIR(x, endx)
+ READ_PAIR(y, endy)
+
+ if (endx >= w || endy >= h || x > endx || y > endy)
+ return -1;
+ dst += x + stride * y;
+ w = endx - x + 1;
+ h = endy - y + 1;
+ if (y)
+ prev_avail = 1;
+ }
+
+ do {
+ uint16_t *p = dst;
+ do {
+ if (repeat-- < 1) {
+ int b = bytestream2_get_byte(gB);
+ if (b < 128)
+ last_symbol = b << 8 | bytestream2_get_byte(gB);
+ else if (b > 129) {
+ repeat = 0;
+ while (b-- > 130)
+ repeat = (repeat << 8) + bytestream2_get_byte(gB) + 1;
+ if (last_symbol == -2) {
+ int skip = FFMIN((unsigned)repeat, dst + w - p);
+ repeat -= skip;
+ p += skip;
+ }
+ } else
+ last_symbol = 127 - b;
+ }
+ if (last_symbol >= 0)
+ *p = last_symbol;
+ else if (last_symbol == -1 && prev_avail)
+ *p = *(p - stride);
+ } while (++p < dst + w);
+ dst += stride;
+ prev_avail = 1;
+ } while (--h);
+
+ return 0;
+}
+
+static int decode_rle(GetBitContext *gb, uint8_t *pal_dst, int pal_stride,
+ uint8_t *rgb_dst, int rgb_stride, uint32_t *pal,
+ int keyframe, int kf_slipt, int slice, int w, int h)
+{
+ uint8_t bits[270] = { 0 };
+ uint32_t codes[270];
+ VLC vlc;
+
+ int current_length = 0, read_codes = 0, next_code = 0, current_codes = 0;
+ int remaining_codes, surplus_codes, i;
+
+ const int alphabet_size = 270 - keyframe;
+
+ int last_symbol = 0, repeat = 0, prev_avail = 0;
+
+ if (!keyframe) {
+ int x, y, clipw, cliph;
+
+ x = get_bits(gb, 12);
+ y = get_bits(gb, 12);
+ clipw = get_bits(gb, 12) + 1;
+ cliph = get_bits(gb, 12) + 1;
+
+ if (x + clipw > w || y + cliph > h)
+ return AVERROR_INVALIDDATA;
+ pal_dst += pal_stride * y + x;
+ rgb_dst += rgb_stride * y + x * 3;
+ w = clipw;
+ h = cliph;
+ if (y)
+ prev_avail = 1;
+ } else {
+ if (slice > 0) {
+ pal_dst += pal_stride * kf_slipt;
+ rgb_dst += rgb_stride * kf_slipt;
+ prev_avail = 1;
+ h -= kf_slipt;
+ } else
+ h = kf_slipt;
+ }
+
+ /* read explicit codes */
+ do {
+ while (current_codes--) {
+ int symbol = get_bits(gb, 8);
+ if (symbol >= 204 - keyframe)
+ symbol += 14 - keyframe;
+ else if (symbol > 189)
+ symbol = get_bits1(gb) + (symbol << 1) - 190;
+ if (bits[symbol])
+ return AVERROR_INVALIDDATA;
+ bits[symbol] = current_length;
+ codes[symbol] = next_code++;
+ read_codes++;
+ }
+ current_length++;
+ next_code <<= 1;
+ remaining_codes = (1 << current_length) - next_code;
+ current_codes = get_bits(gb, av_ceil_log2(remaining_codes + 1));
+ if (current_length > 22 || current_codes > remaining_codes)
+ return AVERROR_INVALIDDATA;
+ } while (current_codes != remaining_codes);
+
+ remaining_codes = alphabet_size - read_codes;
+
+ /* determine the minimum length to fit the rest of the alphabet */
+ while ((surplus_codes = (2 << current_length) -
+ (next_code << 1) - remaining_codes) < 0) {
+ current_length++;
+ next_code <<= 1;
+ }
+
+ /* add the rest of the symbols lexicographically */
+ for (i = 0; i < alphabet_size; i++)
+ if (!bits[i]) {
+ if (surplus_codes-- == 0) {
+ current_length++;
+ next_code <<= 1;
+ }
+ bits[i] = current_length;
+ codes[i] = next_code++;
+ }
+
+ if (next_code != 1 << current_length)
+ return AVERROR_INVALIDDATA;
+
+ if (i = init_vlc(&vlc, 9, alphabet_size, bits, 1, 1, codes, 4, 4, 0))
+ return i;
+
+ /* frame decode */
+ do {
+ uint8_t *pp = pal_dst;
+ uint8_t *rp = rgb_dst;
+ do {
+ if (repeat-- < 1) {
+ int b = get_vlc2(gb, vlc.table, 9, 3);
+ if (b < 256)
+ last_symbol = b;
+ else if (b < 268) {
+ b -= 256;
+ if (b == 11)
+ b = get_bits(gb, 4) + 10;
+
+ if (!b)
+ repeat = 0;
+ else
+ repeat = get_bits(gb, b);
+
+ while (b--)
+ repeat += 1 << b;
+
+ if (last_symbol == -2) {
+ int skip = FFMIN(repeat, pal_dst + w - pp);
+ repeat -= skip;
+ pp += skip;
+ rp += skip * 3;
+ }
+ } else
+ last_symbol = 267 - b;
+ }
+ if (last_symbol >= 0) {
+ *pp = last_symbol;
+ AV_WB24(rp, pal[last_symbol]);
+ } else if (last_symbol == -1 && prev_avail) {
+ *pp = *(pp - pal_stride);
+ memcpy(rp, rp - rgb_stride, 3);
+ }
+ rp += 3;
+ } while (++pp < pal_dst + w);
+ pal_dst += pal_stride;
+ rgb_dst += rgb_stride;
+ prev_avail = 1;
+ } while (--h);
+
+ ff_free_vlc(&vlc);
+ return 0;
+}
+
+static int decode_wmv9(AVCodecContext *avctx, const uint8_t *buf, int buf_size,
+ int x, int y, int w, int h, int wmv9_mask)
+{
+ MSS2Context *ctx = avctx->priv_data;
+ MSS12Context *c = &ctx->c;
+ VC1Context *v = avctx->priv_data;
+ MpegEncContext *s = &v->s;
+ AVFrame *f;
+
+ ff_mpeg_flush(avctx);
+
+ if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
+ int i = ff_find_unused_picture(s, 0);
+ if (i < 0)
+ return -1;
+ s->current_picture_ptr = &s->picture[i];
+ }
+
+ init_get_bits(&s->gb, buf, buf_size * 8);
+
+ s->loop_filter = avctx->skip_loop_filter < AVDISCARD_ALL;
+
+ if (ff_vc1_parse_frame_header(v, &s->gb) == -1) {
+ av_log(v->s.avctx, AV_LOG_ERROR, "header error\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ if (s->pict_type != AV_PICTURE_TYPE_I) {
+ av_log(v->s.avctx, AV_LOG_ERROR, "expected I-frame\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ avctx->pix_fmt = PIX_FMT_YUV420P;
+
+ if (ff_MPV_frame_start(s, avctx) < 0) {
+ av_log(v->s.avctx, AV_LOG_ERROR, "ff_MPV_frame_start error\n");
+ avctx->pix_fmt = PIX_FMT_RGB24;
+ return -1;
+ }
+
+ ff_er_frame_start(s);
+
+ v->bits = buf_size * 8;
+
+ v->end_mb_x = (w + 15) >> 4;
+ s->end_mb_y = (h + 15) >> 4;
+ if (v->respic & 1)
+ v->end_mb_x = v->end_mb_x + 1 >> 1;
+ if (v->respic & 2)
+ s->end_mb_y = s->end_mb_y + 1 >> 1;
+
+ ff_vc1_decode_blocks(v);
+
+ ff_er_frame_end(s);
+
+ ff_MPV_frame_end(s);
+
+ f = &s->current_picture.f;
+
+ if (v->respic == 3) {
+ ctx->dsp.upsample_plane(f->data[0], f->linesize[0], w, h);
+ ctx->dsp.upsample_plane(f->data[1], f->linesize[1], w >> 1, h >> 1);
+ ctx->dsp.upsample_plane(f->data[2], f->linesize[2], w >> 1, h >> 1);
+ } else if (v->respic)
+ av_log_ask_for_sample(v->s.avctx,
+ "Asymmetric WMV9 rectangle subsampling\n");
+
+ av_assert0(f->linesize[1] == f->linesize[2]);
+
+ if (wmv9_mask != -1)
+ ctx->dsp.mss2_blit_wmv9_masked(c->rgb_pic + y * c->rgb_stride + x * 3,
+ c->rgb_stride, wmv9_mask,
+ c->pal_pic + y * c->pal_stride + x,
+ c->pal_stride,
+ f->data[0], f->linesize[0],
+ f->data[1], f->data[2], f->linesize[1],
+ w, h);
+ else
+ ctx->dsp.mss2_blit_wmv9(c->rgb_pic + y * c->rgb_stride + x * 3,
+ c->rgb_stride,
+ f->data[0], f->linesize[0],
+ f->data[1], f->data[2], f->linesize[1],
+ w, h);
+
+ avctx->pix_fmt = PIX_FMT_RGB24;
+
+ return 0;
+}
+
+typedef struct Rectangle {
+ int coded, x, y, w, h;
+} Rectangle;
+
+#define MAX_WMV9_RECTANGLES 20
+#define ARITH2_PADDING 2
+
+static int mss2_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
+ AVPacket *avpkt)
+{
+ const uint8_t *buf = avpkt->data;
+ int buf_size = avpkt->size;
+ MSS2Context *ctx = avctx->priv_data;
+ MSS12Context *c = &ctx->c;
+ GetBitContext gb;
+ GetByteContext gB;
+ ArithCoder acoder;
+
+ int keyframe, has_wmv9, has_mv, is_rle, is_555, ret;
+
+ Rectangle wmv9rects[MAX_WMV9_RECTANGLES], *r;
+ int used_rects = 0, i, implicit_rect, av_uninit(wmv9_mask);
+
+ av_assert0(FF_INPUT_BUFFER_PADDING_SIZE >=
+ ARITH2_PADDING + (MIN_CACHE_BITS + 7) / 8);
+
+ init_get_bits(&gb, buf, buf_size * 8);
+
+ if (keyframe = get_bits1(&gb))
+ skip_bits(&gb, 7);
+ has_wmv9 = get_bits1(&gb);
+ has_mv = keyframe ? 0 : get_bits1(&gb);
+ is_rle = get_bits1(&gb);
+ is_555 = is_rle && get_bits1(&gb);
+ if (c->slice_split > 0)
+ ctx->split_position = c->slice_split;
+ else if (c->slice_split < 0) {
+ if (get_bits1(&gb)) {
+ if (get_bits1(&gb)) {
+ if (get_bits1(&gb))
+ ctx->split_position = get_bits(&gb, 16);
+ else
+ ctx->split_position = get_bits(&gb, 12);
+ } else
+ ctx->split_position = get_bits(&gb, 8) << 4;
+ } else {
+ if (keyframe)
+ ctx->split_position = avctx->height / 2;
+ }
+ } else
+ ctx->split_position = avctx->height;
+
+ if (c->slice_split && (ctx->split_position < 1 - is_555 ||
+ ctx->split_position > avctx->height - 1))
+ return AVERROR_INVALIDDATA;
+
+ align_get_bits(&gb);
+ buf += get_bits_count(&gb) >> 3;
+ buf_size -= get_bits_count(&gb) >> 3;
+
+ if (buf_size < 1)
+ return AVERROR_INVALIDDATA;
+
+ if (is_555 && (has_wmv9 || has_mv || c->slice_split && ctx->split_position))
+ return AVERROR_INVALIDDATA;
+
+ avctx->pix_fmt = is_555 ? PIX_FMT_RGB555 : PIX_FMT_RGB24;
+ if (ctx->pic.data[0] && ctx->pic.format != avctx->pix_fmt)
+ avctx->release_buffer(avctx, &ctx->pic);
+
+ if (has_wmv9) {
+ bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
+ arith2_init(&acoder, &gB);
+
+ implicit_rect = !arith2_get_bit(&acoder);
+
+ while (arith2_get_bit(&acoder)) {
+ if (used_rects == MAX_WMV9_RECTANGLES)
+ return AVERROR_INVALIDDATA;
+ r = &wmv9rects[used_rects];
+ if (!used_rects)
+ r->x = arith2_get_number(&acoder, avctx->width);
+ else
+ r->x = arith2_get_number(&acoder, avctx->width -
+ wmv9rects[used_rects - 1].x) +
+ wmv9rects[used_rects - 1].x;
+ r->y = arith2_get_number(&acoder, avctx->height);
+ r->w = arith2_get_number(&acoder, avctx->width - r->x) + 1;
+ r->h = arith2_get_number(&acoder, avctx->height - r->y) + 1;
+ used_rects++;
+ }
+
+ if (implicit_rect && used_rects) {
+ av_log(avctx, AV_LOG_ERROR, "implicit_rect && used_rects > 0\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ if (implicit_rect) {
+ wmv9rects[0].x = 0;
+ wmv9rects[0].y = 0;
+ wmv9rects[0].w = avctx->width;
+ wmv9rects[0].h = avctx->height;
+
+ used_rects = 1;
+ }
+ for (i = 0; i < used_rects; i++) {
+ if (!implicit_rect && arith2_get_bit(&acoder)) {
+ av_log(avctx, AV_LOG_ERROR, "Unexpected grandchildren\n");
+ return AVERROR_INVALIDDATA;
+ }
+ if (!i) {
+ wmv9_mask = arith2_get_bit(&acoder) - 1;
+ if (!wmv9_mask)
+ wmv9_mask = arith2_get_number(&acoder, 256);
+ }
+ wmv9rects[i].coded = arith2_get_number(&acoder, 2);
+ }
+
+ buf += arith2_get_consumed_bytes(&acoder);
+ buf_size -= arith2_get_consumed_bytes(&acoder);
+ if (buf_size < 1)
+ return AVERROR_INVALIDDATA;
+ }
+
+ c->mvX = c->mvY = 0;
+ if (keyframe && !is_555) {
+ if ((i = decode_pal_v2(c, buf, buf_size)) < 0)
+ return AVERROR_INVALIDDATA;
+ buf += i;
+ buf_size -= i;
+ } else if (has_mv) {
+ buf += 4;
+ buf_size -= 4;
+ if (buf_size < 1)
+ return AVERROR_INVALIDDATA;
+ c->mvX = AV_RB16(buf - 4) - avctx->width;
+ c->mvY = AV_RB16(buf - 2) - avctx->height;
+ }
+
+ if (c->mvX < 0 || c->mvY < 0) {
+ FFSWAP(AVFrame, ctx->pic, ctx->last_pic);
+ FFSWAP(uint8_t *, c->pal_pic, c->last_pal_pic);
+
+ if (ctx->pic.data[0])
+ avctx->release_buffer(avctx, &ctx->pic);
+
+ ctx->pic.reference = 3;
+ ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
+ FF_BUFFER_HINTS_READABLE |
+ FF_BUFFER_HINTS_PRESERVE |
+ FF_BUFFER_HINTS_REUSABLE;
+
+ if ((ret = avctx->get_buffer(avctx, &ctx->pic)) < 0) {
+ av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+ return ret;
+ }
+
+ if (ctx->last_pic.data[0]) {
+ av_assert0(ctx->pic.linesize[0] == ctx->last_pic.linesize[0]);
+ c->last_rgb_pic = ctx->last_pic.data[0] +
+ ctx->last_pic.linesize[0] * (avctx->height - 1);
+ } else {
+ av_log(avctx, AV_LOG_ERROR, "Missing keyframe\n");
+ return -1;
+ }
+ } else {
+ if (ctx->last_pic.data[0])
+ avctx->release_buffer(avctx, &ctx->last_pic);
+
+ ctx->pic.reference = 3;
+ ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
+ FF_BUFFER_HINTS_READABLE |
+ FF_BUFFER_HINTS_PRESERVE |
+ FF_BUFFER_HINTS_REUSABLE;
+
+ if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {
+ av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+ return ret;
+ }
+
+ c->last_rgb_pic = NULL;
+ }
+ c->rgb_pic = ctx->pic.data[0] +
+ ctx->pic.linesize[0] * (avctx->height - 1);
+ c->rgb_stride = -ctx->pic.linesize[0];
+
+ ctx->pic.key_frame = keyframe;
+ ctx->pic.pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
+
+ if (is_555) {
+ bytestream2_init(&gB, buf, buf_size);
+
+ if (decode_555(&gB, (uint16_t *)c->rgb_pic, c->rgb_stride >> 1,
+ keyframe, avctx->width, avctx->height))
+ return AVERROR_INVALIDDATA;
+
+ buf_size -= bytestream2_tell(&gB);
+ } else if (is_rle) {
+ init_get_bits(&gb, buf, buf_size * 8);
+ if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
+ c->rgb_pic, c->rgb_stride, c->pal, keyframe,
+ ctx->split_position, 0,
+ avctx->width, avctx->height))
+ return ret;
+ align_get_bits(&gb);
+
+ if (c->slice_split)
+ if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
+ c->rgb_pic, c->rgb_stride, c->pal, keyframe,
+ ctx->split_position, 1,
+ avctx->width, avctx->height))
+ return ret;
+
+ align_get_bits(&gb);
+ buf += get_bits_count(&gb) >> 3;
+ buf_size -= get_bits_count(&gb) >> 3;
+ } else {
+ if (keyframe)
+ ff_mss12_codec_reset(c);
+ else if (c->corrupted)
+ return AVERROR_INVALIDDATA;
+ bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
+ arith2_init(&acoder, &gB);
+ c->keyframe = keyframe;
+ if (c->corrupted = ff_mss12_decode_rect(&c->sc[0], &acoder, 0, 0,
+ avctx->width,
+ ctx->split_position))
+ return AVERROR_INVALIDDATA;
+
+ buf += arith2_get_consumed_bytes(&acoder);
+ buf_size -= arith2_get_consumed_bytes(&acoder);
+ if (c->slice_split) {
+ if (buf_size < 1)
+ return AVERROR_INVALIDDATA;
+ bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
+ arith2_init(&acoder, &gB);
+ if (c->corrupted = ff_mss12_decode_rect(&c->sc[1], &acoder, 0,
+ ctx->split_position,
+ avctx->width,
+ avctx->height - ctx->split_position))
+ return AVERROR_INVALIDDATA;
+
+ buf += arith2_get_consumed_bytes(&acoder);
+ buf_size -= arith2_get_consumed_bytes(&acoder);
+ }
+ }
+
+ if (has_wmv9) {
+ for (i = 0; i < used_rects; i++) {
+ int x = wmv9rects[i].x;
+ int y = wmv9rects[i].y;
+ int w = wmv9rects[i].w;
+ int h = wmv9rects[i].h;
+ if (wmv9rects[i].coded) {
+ int WMV9codedFrameSize;
+ if (buf_size < 4 || !(WMV9codedFrameSize = AV_RL24(buf)))
+ return AVERROR_INVALIDDATA;
+ if (ret = decode_wmv9(avctx, buf + 3, buf_size - 3,
+ x, y, w, h, wmv9_mask))
+ return ret;
+ buf += WMV9codedFrameSize + 3;
+ buf_size -= WMV9codedFrameSize + 3;
+ } else {
+ uint8_t *dst = c->rgb_pic + y * c->rgb_stride + x * 3;
+ if (wmv9_mask != -1) {
+ ctx->dsp.mss2_gray_fill_masked(dst, c->rgb_stride,
+ wmv9_mask,
+ c->pal_pic + y * c->pal_stride + x,
+ c->pal_stride,
+ w, h);
+ } else {
+ do {
+ memset(dst, 0x80, w * 3);
+ dst += c->rgb_stride;
+ } while (--h);
+ }
+ }
+ }
+ }
+
+ if (buf_size)
+ av_log(avctx, AV_LOG_WARNING, "buffer not fully consumed\n");
+
+ *data_size = sizeof(AVFrame);
+ *(AVFrame *)data = ctx->pic;
+
+ return avpkt->size;
+}
+
+static av_cold int wmv9_init(AVCodecContext *avctx)
+{
+ VC1Context *v = avctx->priv_data;
+
+ v->s.avctx = avctx;
+ avctx->flags |= CODEC_FLAG_EMU_EDGE;
+ v->s.flags |= CODEC_FLAG_EMU_EDGE;
+
+ if (avctx->idct_algo == FF_IDCT_AUTO)
+ avctx->idct_algo = FF_IDCT_WMV2;
+
+ if (ff_vc1_init_common(v) < 0)
+ return -1;
+ ff_vc1dsp_init(&v->vc1dsp);
+
+ v->profile = PROFILE_MAIN;
+
+ v->zz_8x4 = ff_wmv2_scantableA;
+ v->zz_4x8 = ff_wmv2_scantableB;
+ v->res_y411 = 0;
+ v->res_sprite = 0;
+
+ v->frmrtq_postproc = 7;
+ v->bitrtq_postproc = 31;
+
+ v->res_x8 = 0;
+ v->multires = 0;
+ v->res_fasttx = 1;
+
+ v->fastuvmc = 0;
+
+ v->extended_mv = 0;
+
+ v->dquant = 1;
+ v->vstransform = 1;
+
+ v->res_transtab = 0;
+
+ v->overlap = 0;
+
+ v->s.resync_marker = 0;
+ v->rangered = 0;
+
+ v->s.max_b_frames = avctx->max_b_frames = 0;
+ v->quantizer_mode = 0;
+
+ v->finterpflag = 0;
+
+ v->res_rtm_flag = 1;
+
+ ff_vc1_init_transposed_scantables(v);
+
+ if (ff_msmpeg4_decode_init(avctx) < 0 ||
+ ff_vc1_decode_init_alloc_tables(v) < 0)
+ return -1;
+
+ /* error concealment */
+ v->s.me.qpel_put = v->s.dsp.put_qpel_pixels_tab;
+ v->s.me.qpel_avg = v->s.dsp.avg_qpel_pixels_tab;
+
+ return 0;
+}
+
+static av_cold int mss2_decode_end(AVCodecContext *avctx)
+{
+ MSS2Context *const ctx = avctx->priv_data;
+
+ if (ctx->pic.data[0])
+ avctx->release_buffer(avctx, &ctx->pic);
+ if (ctx->last_pic.data[0])
+ avctx->release_buffer(avctx, &ctx->last_pic);
+
+ ff_mss12_decode_end(&ctx->c);
+ av_freep(&ctx->c.pal_pic);
+ av_freep(&ctx->c.last_pal_pic);
+ ff_vc1_decode_end(avctx);
+
+ return 0;
+}
+
+static av_cold int mss2_decode_init(AVCodecContext *avctx)
+{
+ MSS2Context * const ctx = avctx->priv_data;
+ MSS12Context *c = &ctx->c;
+ int ret;
+ c->avctx = avctx;
+ avctx->coded_frame = &ctx->pic;
+ if (ret = ff_mss12_decode_init(c, 1))
+ return ret;
+ c->pal_stride = c->mask_stride;
+ c->pal_pic = av_malloc(c->pal_stride * avctx->height);
+ c->last_pal_pic = av_malloc(c->pal_stride * avctx->height);
+ if (!c->pal_pic || !c->last_pal_pic) {
+ mss2_decode_end(avctx);
+ return AVERROR(ENOMEM);
+ }
+ if (ret = wmv9_init(avctx)) {
+ mss2_decode_end(avctx);
+ return ret;
+ }
+ ff_mss2dsp_init(&ctx->dsp);
+ return 0;
+}
+
+AVCodec ff_mss2_decoder = {
+ .name = "mss2",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .id = AV_CODEC_ID_MSS2,
+ .priv_data_size = sizeof(MSS2Context),
+ .init = mss2_decode_init,
+ .close = mss2_decode_end,
+ .decode = mss2_decode_frame,
+ .capabilities = CODEC_CAP_DR1,
+ .long_name = NULL_IF_CONFIG_SMALL("MS Windows Media Video V9 Screen"),
+};
diff --git a/libavcodec/mss2dsp.c b/libavcodec/mss2dsp.c
new file mode 100644
index 0000000..aa13577
--- /dev/null
+++ b/libavcodec/mss2dsp.c
@@ -0,0 +1,153 @@
+/*
+ * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder
+ *
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder DSP routines
+ */
+
+#include "mss2dsp.h"
+#include "libavutil/common.h"
+
+static av_always_inline void mss2_blit_wmv9_template(uint8_t *dst,
+ int dst_stride,
+ int gray,
+ int use_mask,
+ int maskcolor,
+ const uint8_t *mask,
+ int mask_stride,
+ const uint8_t *srcy,
+ int srcy_stride,
+ const uint8_t *srcu,
+ const uint8_t *srcv,
+ int srcuv_stride,
+ int w, int h)
+{
+ int i, j, k, r = -1;
+ while (++r < h) {
+ for (i = 0, j = 0, k = 0; i < w; j += (i & 1), i++, k += 3) {
+ if (!use_mask || mask[i] == maskcolor) {
+ if (gray) {
+ dst[k] = dst[k + 1] = dst[k + 2] = 0x80;
+ } else {
+ int y = srcy[i];
+ int u = srcu[j] - 128;
+ int v = srcv[j] - 128;
+ dst[k] = av_clip_uint8(y + ( 91881 * v + 32768 >> 16));
+ dst[k + 1] = av_clip_uint8(y + (-22554 * u - 46802 * v + 32768 >> 16));
+ dst[k + 2] = av_clip_uint8(y + (116130 * u + 32768 >> 16));
+ }
+ }
+ }
+ mask += mask_stride;
+ dst += dst_stride;
+ srcy += srcy_stride;
+ srcu += srcuv_stride * (r & 1);
+ srcv += srcuv_stride * (r & 1);
+ }
+}
+
+static void mss2_blit_wmv9_c(uint8_t *dst, int dst_stride,
+ const uint8_t *srcy, int srcy_stride,
+ const uint8_t *srcu, const uint8_t *srcv,
+ int srcuv_stride, int w, int h)
+{
+ mss2_blit_wmv9_template(dst, dst_stride, 0, 0,
+ 0, NULL, 0,
+ srcy, srcy_stride,
+ srcu, srcv, srcuv_stride,
+ w, h);
+}
+
+static void mss2_blit_wmv9_masked_c(uint8_t *dst, int dst_stride,
+ int maskcolor, const uint8_t *mask,
+ int mask_stride,
+ const uint8_t *srcy, int srcy_stride,
+ const uint8_t *srcu, const uint8_t *srcv,
+ int srcuv_stride, int w, int h)
+{
+ mss2_blit_wmv9_template(dst, dst_stride, 0, 1,
+ maskcolor, mask, mask_stride,
+ srcy, srcy_stride,
+ srcu, srcv, srcuv_stride,
+ w, h);
+}
+
+static void mss2_gray_fill_masked_c(uint8_t *dst, int dst_stride,
+ int maskcolor, const uint8_t *mask,
+ int mask_stride, int w, int h)
+{
+ mss2_blit_wmv9_template(dst, dst_stride, 1, 1,
+ maskcolor, mask, mask_stride,
+ NULL, 0,
+ NULL, NULL, 0,
+ w, h);
+}
+
+static void upsample_plane_c(uint8_t *plane, int plane_stride, int w, int h)
+{
+ uint8_t *src1, *src2, *dst1, *dst2, *p, a, b;
+ int i, j;
+
+ w += (w & 1);
+ h += (h & 1);
+
+ j = h - 1;
+
+ memcpy(plane + plane_stride * j,
+ plane + plane_stride * (j >> 1),
+ w);
+
+ while ((j -= 2) > 0) {
+ dst1 = plane + plane_stride * (j + 1);
+ dst2 = plane + plane_stride * j;
+ src1 = plane + plane_stride * ((j + 1) >> 1);
+ src2 = plane + plane_stride * ( j >> 1);
+
+ for (i = (w - 1) >> 1; i >= 0; i--) {
+ a = src1[i];
+ b = src2[i];
+ dst1[i] = (3 * a + b + 2) >> 2;
+ dst2[i] = (a + 3 * b + 2) >> 2;
+ }
+ }
+
+ for (j = h - 1; j >= 0; j--) {
+ p = plane + plane_stride * j;
+ i = w - 1;
+
+ p[i] = p[i >> 1];
+
+ while ((i -= 2) > 0) {
+ a = p[ i >> 1];
+ b = p[(i + 1) >> 1];
+ p[i] = (3 * a + b + 1) >> 2;
+ p[i + 1] = (a + 3 * b + 1) >> 2;
+ }
+ }
+}
+
+av_cold void ff_mss2dsp_init(MSS2DSPContext* dsp)
+{
+ dsp->mss2_blit_wmv9 = mss2_blit_wmv9_c;
+ dsp->mss2_blit_wmv9_masked = mss2_blit_wmv9_masked_c;
+ dsp->mss2_gray_fill_masked = mss2_gray_fill_masked_c;
+ dsp->upsample_plane = upsample_plane_c;
+}
diff --git a/libavcodec/mss2dsp.h b/libavcodec/mss2dsp.h
new file mode 100644
index 0000000..b3d67a1
--- /dev/null
+++ b/libavcodec/mss2dsp.h
@@ -0,0 +1,50 @@
+/*
+ * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder
+ *
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder DSP routines
+ */
+
+#ifndef AVCODEC_MSS2DSP_H
+#define AVCODEC_MSS2DSP_H
+
+#include "dsputil.h"
+
+typedef struct MSS2DSPContext {
+ void (*mss2_blit_wmv9)(uint8_t *dst, int dst_stride,
+ const uint8_t *srcy, int srcy_stride,
+ const uint8_t *srcu, const uint8_t *srcv,
+ int srcuv_stride, int w, int h);
+ void (*mss2_blit_wmv9_masked)(uint8_t *dst, int dst_stride,
+ int maskcolor, const uint8_t *mask,
+ int mask_stride,
+ const uint8_t *srcy, int srcy_stride,
+ const uint8_t *srcu, const uint8_t *srcv,
+ int srcuv_stride, int w, int h);
+ void (*mss2_gray_fill_masked)(uint8_t *dst, int dst_stride,
+ int maskcolor, const uint8_t *mask,
+ int mask_stride, int w, int h);
+ void (*upsample_plane)(uint8_t *plane, int plane_stride, int w, int h);
+} MSS2DSPContext;
+
+av_cold void ff_mss2dsp_init(MSS2DSPContext* dsp);
+
+#endif /* AVCODEC_MSS2DSP_H */
diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c
index 0e218af..de75aac 100644
--- a/libavcodec/vc1.c
+++ b/libavcodec/vc1.c
@@ -578,7 +578,12 @@ int ff_vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
if (v->finterpflag)
v->interpfrm = get_bits1(gb);
- skip_bits(gb, 2); //framecnt unused
+ if (v->s.avctx->codec->id == AV_CODEC_ID_MSS2)
+ v->respic =
+ v->rangered =
+ v->multires = get_bits(gb, 2) == 1;
+ else
+ skip_bits(gb, 2); //framecnt unused
v->rangeredfrm = 0;
if (v->rangered)
v->rangeredfrm = get_bits1(gb);
diff --git a/libavcodec/vc1.h b/libavcodec/vc1.h
index fe21f2f..13011ae 100644
--- a/libavcodec/vc1.h
+++ b/libavcodec/vc1.h
@@ -394,6 +394,8 @@ typedef struct VC1Context{
uint8_t broken_link; ///< Broken link flag (BROKEN_LINK syntax element)
uint8_t closed_entry; ///< Closed entry point flag (CLOSED_ENTRY syntax element)
+ int end_mb_x; ///< Horizontal macroblock limit (used only by mss2)
+
int parse_only; ///< Context is used within parser
int warn_interlaced;
diff --git a/libavcodec/vc1dec.c b/libavcodec/vc1dec.c
index 63c0949..6b2662e 100644
--- a/libavcodec/vc1dec.c
+++ b/libavcodec/vc1dec.c
@@ -4348,10 +4348,10 @@ static void vc1_decode_i_blocks(VC1Context *v)
s->mb_x = s->mb_y = 0;
s->mb_intra = 1;
s->first_slice_line = 1;
- for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
+ for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
s->mb_x = 0;
ff_init_block_index(s);
- for (; s->mb_x < s->mb_width; s->mb_x++) {
+ for (; s->mb_x < v->end_mb_x; s->mb_x++) {
uint8_t *dst[6];
ff_update_block_index(s);
dst[0] = s->dest[0];
@@ -4438,7 +4438,10 @@ static void vc1_decode_i_blocks(VC1Context *v)
s->first_slice_line = 0;
}
if (v->s.loop_filter)
- ff_draw_horiz_band(s, (s->mb_height - 1) * 16, 16);
+ ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
+
+ /* This is intentionally mb_height and not end_mb_y - unlike in advanced
+ * profile, these only differ are when decoding MSS2 rectangles. */
ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
}
@@ -5549,6 +5552,7 @@ static int vc1_decode_frame(AVCodecContext *avctx, void *data,
ff_er_frame_start(s);
v->bits = buf_size * 8;
+ v->end_mb_x = s->mb_width;
if (v->field_mode) {
uint8_t *tmp[2];
s->current_picture.f.linesize[0] <<= 1;
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 78f4ccf..b08f00d 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -27,8 +27,8 @@
*/
#define LIBAVCODEC_VERSION_MAJOR 54
-#define LIBAVCODEC_VERSION_MINOR 26
-#define LIBAVCODEC_VERSION_MICRO 1
+#define LIBAVCODEC_VERSION_MINOR 27
+#define LIBAVCODEC_VERSION_MICRO 0
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
LIBAVCODEC_VERSION_MINOR, \
diff --git a/libavformat/riff.c b/libavformat/riff.c
index 6920115..f963890 100644
--- a/libavformat/riff.c
+++ b/libavformat/riff.c
@@ -289,6 +289,7 @@ const AVCodecTag ff_codec_bmp_tags[] = {
{ AV_CODEC_ID_TSCC2, MKTAG('T', 'S', 'C', '2') },
{ AV_CODEC_ID_MTS2, MKTAG('M', 'T', 'S', '2') },
{ AV_CODEC_ID_CLLC, MKTAG('C', 'L', 'L', 'C') },
+ { AV_CODEC_ID_MSS2, MKTAG('M', 'S', 'S', '2') },
{ AV_CODEC_ID_NONE, 0 }
};
More information about the ffmpeg-cvslog
mailing list