libavcodec/mss1.c
005c80b6
 /*
  * Microsoft Screen 1 (aka Windows Media Video V7 Screen) decoder
  * Copyright (c) 2012 Konstantin Shishkov
  *
4da42ebe
  * This file is part of FFmpeg.
005c80b6
  *
4da42ebe
  * FFmpeg is free software; you can redistribute it and/or
005c80b6
  * 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.
  *
4da42ebe
  * FFmpeg is distributed in the hope that it will be useful,
005c80b6
  * 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
4da42ebe
  * License along with FFmpeg; if not, write to the Free Software
005c80b6
  * 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"
0de4a563
 #include "mss12.h"
005c80b6
 
 typedef struct MSS1Context {
0de4a563
     MSS12Context   ctx;
005c80b6
     AVFrame        pic;
a97ee41b
     SliceContext   sc;
005c80b6
 } 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;
ee769c6a
         c->value  |= get_bits1(c->gbc.gb);
005c80b6
     }
 }
 
ee769c6a
 ARITH_GET_BIT()
005c80b6
 
 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;
 }
 
626c1a33
 static int arith_get_prob(ArithCoder *c, int16_t *probs)
005c80b6
 {
     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;
 }
 
ee769c6a
 ARITH_GET_MODEL_SYM()
005c80b6
 
0de4a563
 static void arith_init(ArithCoder *c, GetBitContext *gb)
005c80b6
 {
ee769c6a
     c->low           = 0;
     c->high          = 0xFFFF;
     c->value         = get_bits(gb, 16);
     c->gbc.gb        = gb;
0de4a563
     c->get_model_sym = arith_get_model_sym;
     c->get_number    = arith_get_number;
005c80b6
 }
 
ee769c6a
 static int decode_pal(MSS12Context *ctx, ArithCoder *acoder)
005c80b6
 {
     int i, ncol, r, g, b;
ee769c6a
     uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
005c80b6
 
ee769c6a
     if (!ctx->free_colours)
005c80b6
         return 0;
 
ee769c6a
     ncol = arith_get_number(acoder, ctx->free_colours + 1);
005c80b6
     for (i = 0; i < ncol; i++) {
         r = arith_get_bits(acoder, 8);
         g = arith_get_bits(acoder, 8);
         b = arith_get_bits(acoder, 8);
b12d92ef
         *pal++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
005c80b6
     }
 
8f5d573a
     return !!ncol;
005c80b6
 }
 
df9b9567
 static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
005c80b6
                              AVPacket *avpkt)
 {
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
ee769c6a
     MSS1Context *ctx = avctx->priv_data;
     MSS12Context *c = &ctx->ctx;
005c80b6
     GetBitContext gb;
     ArithCoder acoder;
     int pal_changed = 0;
     int ret;
 
     init_get_bits(&gb, buf, buf_size * 8);
     arith_init(&acoder, &gb);
 
ee769c6a
     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) {
005c80b6
         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
         return ret;
     }
 
ee769c6a
     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) {
a97ee41b
         c->corrupted = 0;
         ff_mss12_slicecontext_reset(&ctx->sc);
ee769c6a
         pal_changed        = decode_pal(c, &acoder);
         ctx->pic.key_frame = 1;
         ctx->pic.pict_type = AV_PICTURE_TYPE_I;
005c80b6
     } else {
ee769c6a
         if (c->corrupted)
005c80b6
             return AVERROR_INVALIDDATA;
ee769c6a
         ctx->pic.key_frame = 0;
         ctx->pic.pict_type = AV_PICTURE_TYPE_P;
005c80b6
     }
a97ee41b
     c->corrupted = ff_mss12_decode_rect(&ctx->sc, &acoder, 0, 0,
ee769c6a
                                         avctx->width, avctx->height);
     if (c->corrupted)
005c80b6
         return AVERROR_INVALIDDATA;
ee769c6a
     memcpy(ctx->pic.data[1], c->pal, AVPALETTE_SIZE);
     ctx->pic.palette_has_changed = pal_changed;
005c80b6
 
df9b9567
     *got_frame      = 1;
ee769c6a
     *(AVFrame*)data = ctx->pic;
005c80b6
 
     /* always report that the buffer was completely consumed */
     return buf_size;
 }
 
 static av_cold int mss1_decode_init(AVCodecContext *avctx)
 {
     MSS1Context * const c = avctx->priv_data;
ede3d640
     int ret;
005c80b6
 
0de4a563
     c->ctx.avctx       = avctx;
005c80b6
     avctx->coded_frame = &c->pic;
 
a97ee41b
     ret = ff_mss12_decode_init(&c->ctx, 0, &c->sc, NULL);
ede3d640
 
716d413c
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
ede3d640
 
     return ret;
005c80b6
 }
 
 static av_cold int mss1_decode_end(AVCodecContext *avctx)
 {
ee769c6a
     MSS1Context * const ctx = avctx->priv_data;
005c80b6
 
ee769c6a
     if (ctx->pic.data[0])
         avctx->release_buffer(avctx, &ctx->pic);
     ff_mss12_decode_end(&ctx->ctx);
005c80b6
 
     return 0;
 }
 
 AVCodec ff_mss1_decoder = {
     .name           = "mss1",
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_MSS1,
005c80b6
     .priv_data_size = sizeof(MSS1Context),
     .init           = mss1_decode_init,
     .close          = mss1_decode_end,
     .decode         = mss1_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("MS Screen 1"),
 };