Commit ee769c6a authored by Alberto Delmás's avatar Alberto Delmás Committed by Kostya Shishkov

MSS2 decoder

Signed-off-by: default avatarKostya Shishkov <kostya.shishkov@gmail.com>
parent d96d6ba6
......@@ -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:
......
......@@ -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"
......
......@@ -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.
@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.
......
......@@ -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
......
......@@ -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);
......
......@@ -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
......
......@@ -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" */
{
......
......@@ -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;
......
......@@ -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);
......
......@@ -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;
}
......
This diff is collapsed.
......@@ -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 */
This diff is collapsed.
/*
* 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;
}
/*
* 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