2012-06-15 18:37:37 +00:00
|
|
|
/*
|
|
|
|
* Microsoft Screen 1 (aka Windows Media Video V7 Screen) decoder
|
|
|
|
* Copyright (c) 2012 Konstantin Shishkov
|
|
|
|
*
|
2012-06-20 18:47:46 +00:00
|
|
|
* This file is part of FFmpeg.
|
2012-06-15 18:37:37 +00:00
|
|
|
*
|
2012-06-20 18:47:46 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-06-15 18:37:37 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2012-06-20 18:47:46 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-06-15 18:37:37 +00:00
|
|
|
* 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
|
2012-06-20 18:47:46 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-06-15 18:37:37 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Microsoft Screen 1 (aka Windows Media Video V7 Screen) decoder
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
2012-11-21 20:34:46 +00:00
|
|
|
#include "internal.h"
|
2012-08-26 12:16:07 +00:00
|
|
|
#include "mss12.h"
|
2012-06-15 18:37:37 +00:00
|
|
|
|
|
|
|
typedef struct MSS1Context {
|
2012-08-26 12:16:07 +00:00
|
|
|
MSS12Context ctx;
|
2013-11-09 09:14:46 +00:00
|
|
|
AVFrame *pic;
|
2012-09-02 10:42:01 +00:00
|
|
|
SliceContext sc;
|
2012-06-15 18:37:37 +00:00
|
|
|
} MSS1Context;
|
|
|
|
|
|
|
|
static void arith_normalise(ArithCoder *c)
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
if (c->high >= 0x8000) {
|
|
|
|
if (c->low < 0x8000) {
|
|
|
|
if (c->low >= 0x4000 && c->high < 0xC000) {
|
|
|
|
c->value -= 0x4000;
|
|
|
|
c->low -= 0x4000;
|
|
|
|
c->high -= 0x4000;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c->value -= 0x8000;
|
|
|
|
c->low -= 0x8000;
|
|
|
|
c->high -= 0x8000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c->value <<= 1;
|
|
|
|
c->low <<= 1;
|
|
|
|
c->high <<= 1;
|
|
|
|
c->high |= 1;
|
2012-08-24 15:45:57 +00:00
|
|
|
c->value |= get_bits1(c->gbc.gb);
|
2012-06-15 18:37:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 11:10:35 +00:00
|
|
|
ARITH_GET_BIT(arith)
|
2012-06-15 18:37:37 +00:00
|
|
|
|
|
|
|
static int arith_get_bits(ArithCoder *c, int bits)
|
|
|
|
{
|
|
|
|
int range = c->high - c->low + 1;
|
|
|
|
int val = (((c->value - c->low + 1) << bits) - 1) / range;
|
|
|
|
int prob = range * val;
|
|
|
|
|
|
|
|
c->high = ((prob + range) >> bits) + c->low - 1;
|
|
|
|
c->low += prob >> bits;
|
|
|
|
|
|
|
|
arith_normalise(c);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int arith_get_number(ArithCoder *c, int mod_val)
|
|
|
|
{
|
|
|
|
int range = c->high - c->low + 1;
|
|
|
|
int val = ((c->value - c->low + 1) * mod_val - 1) / range;
|
|
|
|
int prob = range * val;
|
|
|
|
|
|
|
|
c->high = (prob + range) / mod_val + c->low - 1;
|
|
|
|
c->low += prob / mod_val;
|
|
|
|
|
|
|
|
arith_normalise(c);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2012-09-02 10:44:21 +00:00
|
|
|
static int arith_get_prob(ArithCoder *c, int16_t *probs)
|
2012-06-15 18:37:37 +00:00
|
|
|
{
|
|
|
|
int range = c->high - c->low + 1;
|
|
|
|
int val = ((c->value - c->low + 1) * probs[0] - 1) / range;
|
|
|
|
int sym = 1;
|
|
|
|
|
|
|
|
while (probs[sym] > val)
|
|
|
|
sym++;
|
|
|
|
|
|
|
|
c->high = range * probs[sym - 1] / probs[0] + c->low - 1;
|
|
|
|
c->low += range * probs[sym] / probs[0];
|
|
|
|
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2013-12-30 11:10:35 +00:00
|
|
|
ARITH_GET_MODEL_SYM(arith)
|
2012-06-15 18:37:37 +00:00
|
|
|
|
2012-08-26 12:16:07 +00:00
|
|
|
static void arith_init(ArithCoder *c, GetBitContext *gb)
|
2012-06-15 18:37:37 +00:00
|
|
|
{
|
2012-08-24 15:45:57 +00:00
|
|
|
c->low = 0;
|
|
|
|
c->high = 0xFFFF;
|
|
|
|
c->value = get_bits(gb, 16);
|
|
|
|
c->gbc.gb = gb;
|
2012-08-26 12:16:07 +00:00
|
|
|
c->get_model_sym = arith_get_model_sym;
|
|
|
|
c->get_number = arith_get_number;
|
2012-06-15 18:37:37 +00:00
|
|
|
}
|
|
|
|
|
2012-08-24 15:45:57 +00:00
|
|
|
static int decode_pal(MSS12Context *ctx, ArithCoder *acoder)
|
2012-06-15 18:37:37 +00:00
|
|
|
{
|
|
|
|
int i, ncol, r, g, b;
|
2012-08-24 15:45:57 +00:00
|
|
|
uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
|
2012-06-15 18:37:37 +00:00
|
|
|
|
2012-08-24 15:45:57 +00:00
|
|
|
if (!ctx->free_colours)
|
2012-06-15 18:37:37 +00:00
|
|
|
return 0;
|
|
|
|
|
2012-08-24 15:45:57 +00:00
|
|
|
ncol = arith_get_number(acoder, ctx->free_colours + 1);
|
2012-06-15 18:37:37 +00:00
|
|
|
for (i = 0; i < ncol; i++) {
|
|
|
|
r = arith_get_bits(acoder, 8);
|
|
|
|
g = arith_get_bits(acoder, 8);
|
|
|
|
b = arith_get_bits(acoder, 8);
|
2012-10-14 01:17:13 +00:00
|
|
|
*pal++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
|
2012-06-15 18:37:37 +00:00
|
|
|
}
|
|
|
|
|
2012-06-27 08:07:47 +00:00
|
|
|
return !!ncol;
|
2012-06-15 18:37:37 +00:00
|
|
|
}
|
|
|
|
|
2012-11-13 18:35:22 +00:00
|
|
|
static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
|
2012-06-15 18:37:37 +00:00
|
|
|
AVPacket *avpkt)
|
|
|
|
{
|
2012-08-24 15:45:57 +00:00
|
|
|
MSS1Context *ctx = avctx->priv_data;
|
|
|
|
MSS12Context *c = &ctx->ctx;
|
2012-06-15 18:37:37 +00:00
|
|
|
GetBitContext gb;
|
|
|
|
ArithCoder acoder;
|
|
|
|
int pal_changed = 0;
|
|
|
|
int ret;
|
|
|
|
|
2013-11-05 12:50:43 +00:00
|
|
|
if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2012-06-15 18:37:37 +00:00
|
|
|
arith_init(&acoder, &gb);
|
|
|
|
|
2013-11-17 01:43:57 +00:00
|
|
|
if ((ret = ff_reget_buffer(avctx, ctx->pic)) < 0)
|
2012-06-15 18:37:37 +00:00
|
|
|
return ret;
|
|
|
|
|
2013-11-09 09:14:46 +00:00
|
|
|
c->pal_pic = ctx->pic->data[0] + ctx->pic->linesize[0] * (avctx->height - 1);
|
|
|
|
c->pal_stride = -ctx->pic->linesize[0];
|
2012-08-24 15:45:57 +00:00
|
|
|
c->keyframe = !arith_get_bit(&acoder);
|
|
|
|
if (c->keyframe) {
|
2012-09-02 10:42:01 +00:00
|
|
|
c->corrupted = 0;
|
|
|
|
ff_mss12_slicecontext_reset(&ctx->sc);
|
2012-08-24 15:45:57 +00:00
|
|
|
pal_changed = decode_pal(c, &acoder);
|
2013-11-09 09:14:46 +00:00
|
|
|
ctx->pic->key_frame = 1;
|
|
|
|
ctx->pic->pict_type = AV_PICTURE_TYPE_I;
|
2012-06-15 18:37:37 +00:00
|
|
|
} else {
|
2012-08-24 15:45:57 +00:00
|
|
|
if (c->corrupted)
|
2012-06-15 18:37:37 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2013-11-09 09:14:46 +00:00
|
|
|
ctx->pic->key_frame = 0;
|
|
|
|
ctx->pic->pict_type = AV_PICTURE_TYPE_P;
|
2012-06-15 18:37:37 +00:00
|
|
|
}
|
2012-09-02 10:42:01 +00:00
|
|
|
c->corrupted = ff_mss12_decode_rect(&ctx->sc, &acoder, 0, 0,
|
2012-08-24 15:45:57 +00:00
|
|
|
avctx->width, avctx->height);
|
|
|
|
if (c->corrupted)
|
2012-06-15 18:37:37 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2013-11-09 09:14:46 +00:00
|
|
|
memcpy(ctx->pic->data[1], c->pal, AVPALETTE_SIZE);
|
|
|
|
ctx->pic->palette_has_changed = pal_changed;
|
2012-06-15 18:37:37 +00:00
|
|
|
|
2013-11-09 09:14:46 +00:00
|
|
|
if ((ret = av_frame_ref(data, ctx->pic)) < 0)
|
2012-11-21 20:34:46 +00:00
|
|
|
return ret;
|
|
|
|
|
2012-11-13 18:35:22 +00:00
|
|
|
*got_frame = 1;
|
2012-06-15 18:37:37 +00:00
|
|
|
|
|
|
|
/* always report that the buffer was completely consumed */
|
2013-11-05 12:50:43 +00:00
|
|
|
return avpkt->size;
|
2012-06-15 18:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int mss1_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
MSS1Context * const c = avctx->priv_data;
|
2012-08-31 05:59:36 +00:00
|
|
|
int ret;
|
2012-06-15 18:37:37 +00:00
|
|
|
|
2012-08-26 12:16:07 +00:00
|
|
|
c->ctx.avctx = avctx;
|
2013-03-12 11:11:50 +00:00
|
|
|
|
2013-11-09 09:14:46 +00:00
|
|
|
c->pic = av_frame_alloc();
|
|
|
|
if (!c->pic)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-06-15 18:37:37 +00:00
|
|
|
|
2012-09-02 10:42:01 +00:00
|
|
|
ret = ff_mss12_decode_init(&c->ctx, 0, &c->sc, NULL);
|
2012-08-31 05:59:36 +00:00
|
|
|
|
2012-10-06 10:10:34 +00:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2012-08-31 05:59:36 +00:00
|
|
|
|
|
|
|
return ret;
|
2012-06-15 18:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int mss1_decode_end(AVCodecContext *avctx)
|
|
|
|
{
|
2012-08-24 15:45:57 +00:00
|
|
|
MSS1Context * const ctx = avctx->priv_data;
|
2012-06-15 18:37:37 +00:00
|
|
|
|
2013-11-09 09:14:46 +00:00
|
|
|
av_frame_free(&ctx->pic);
|
2012-08-24 15:45:57 +00:00
|
|
|
ff_mss12_decode_end(&ctx->ctx);
|
2012-06-15 18:37:37 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVCodec ff_mss1_decoder = {
|
|
|
|
.name = "mss1",
|
2013-10-03 20:57:53 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("MS Screen 1"),
|
2012-06-15 18:37:37 +00:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 09:11:04 +00:00
|
|
|
.id = AV_CODEC_ID_MSS1,
|
2012-06-15 18:37:37 +00:00
|
|
|
.priv_data_size = sizeof(MSS1Context),
|
|
|
|
.init = mss1_decode_init,
|
|
|
|
.close = mss1_decode_end,
|
|
|
|
.decode = mss1_decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DR1,
|
|
|
|
};
|