00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "mss12.h"
00029
00030 typedef struct MSS1Context {
00031 MSS12Context ctx;
00032 AVFrame pic;
00033 SliceContext sc;
00034 } MSS1Context;
00035
00036 static void arith_normalise(ArithCoder *c)
00037 {
00038 for (;;) {
00039 if (c->high >= 0x8000) {
00040 if (c->low < 0x8000) {
00041 if (c->low >= 0x4000 && c->high < 0xC000) {
00042 c->value -= 0x4000;
00043 c->low -= 0x4000;
00044 c->high -= 0x4000;
00045 } else {
00046 return;
00047 }
00048 } else {
00049 c->value -= 0x8000;
00050 c->low -= 0x8000;
00051 c->high -= 0x8000;
00052 }
00053 }
00054 c->value <<= 1;
00055 c->low <<= 1;
00056 c->high <<= 1;
00057 c->high |= 1;
00058 c->value |= get_bits1(c->gbc.gb);
00059 }
00060 }
00061
00062 ARITH_GET_BIT()
00063
00064 static int arith_get_bits(ArithCoder *c, int bits)
00065 {
00066 int range = c->high - c->low + 1;
00067 int val = (((c->value - c->low + 1) << bits) - 1) / range;
00068 int prob = range * val;
00069
00070 c->high = ((prob + range) >> bits) + c->low - 1;
00071 c->low += prob >> bits;
00072
00073 arith_normalise(c);
00074
00075 return val;
00076 }
00077
00078 static int arith_get_number(ArithCoder *c, int mod_val)
00079 {
00080 int range = c->high - c->low + 1;
00081 int val = ((c->value - c->low + 1) * mod_val - 1) / range;
00082 int prob = range * val;
00083
00084 c->high = (prob + range) / mod_val + c->low - 1;
00085 c->low += prob / mod_val;
00086
00087 arith_normalise(c);
00088
00089 return val;
00090 }
00091
00092 static int arith_get_prob(ArithCoder *c, int16_t *probs)
00093 {
00094 int range = c->high - c->low + 1;
00095 int val = ((c->value - c->low + 1) * probs[0] - 1) / range;
00096 int sym = 1;
00097
00098 while (probs[sym] > val)
00099 sym++;
00100
00101 c->high = range * probs[sym - 1] / probs[0] + c->low - 1;
00102 c->low += range * probs[sym] / probs[0];
00103
00104 return sym;
00105 }
00106
00107 ARITH_GET_MODEL_SYM()
00108
00109 static void arith_init(ArithCoder *c, GetBitContext *gb)
00110 {
00111 c->low = 0;
00112 c->high = 0xFFFF;
00113 c->value = get_bits(gb, 16);
00114 c->gbc.gb = gb;
00115 c->get_model_sym = arith_get_model_sym;
00116 c->get_number = arith_get_number;
00117 }
00118
00119 static int decode_pal(MSS12Context *ctx, ArithCoder *acoder)
00120 {
00121 int i, ncol, r, g, b;
00122 uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
00123
00124 if (!ctx->free_colours)
00125 return 0;
00126
00127 ncol = arith_get_number(acoder, ctx->free_colours + 1);
00128 for (i = 0; i < ncol; i++) {
00129 r = arith_get_bits(acoder, 8);
00130 g = arith_get_bits(acoder, 8);
00131 b = arith_get_bits(acoder, 8);
00132 *pal++ = (0xFF << 24) | (r << 16) | (g << 8) | b;
00133 }
00134
00135 return !!ncol;
00136 }
00137
00138 static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00139 AVPacket *avpkt)
00140 {
00141 const uint8_t *buf = avpkt->data;
00142 int buf_size = avpkt->size;
00143 MSS1Context *ctx = avctx->priv_data;
00144 MSS12Context *c = &ctx->ctx;
00145 GetBitContext gb;
00146 ArithCoder acoder;
00147 int pal_changed = 0;
00148 int ret;
00149
00150 init_get_bits(&gb, buf, buf_size * 8);
00151 arith_init(&acoder, &gb);
00152
00153 ctx->pic.reference = 3;
00154 ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
00155 FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00156 if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {
00157 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00158 return ret;
00159 }
00160
00161 c->pal_pic = ctx->pic.data[0] + ctx->pic.linesize[0] * (avctx->height - 1);
00162 c->pal_stride = -ctx->pic.linesize[0];
00163 c->keyframe = !arith_get_bit(&acoder);
00164 if (c->keyframe) {
00165 c->corrupted = 0;
00166 ff_mss12_slicecontext_reset(&ctx->sc);
00167 pal_changed = decode_pal(c, &acoder);
00168 ctx->pic.key_frame = 1;
00169 ctx->pic.pict_type = AV_PICTURE_TYPE_I;
00170 } else {
00171 if (c->corrupted)
00172 return AVERROR_INVALIDDATA;
00173 ctx->pic.key_frame = 0;
00174 ctx->pic.pict_type = AV_PICTURE_TYPE_P;
00175 }
00176 c->corrupted = ff_mss12_decode_rect(&ctx->sc, &acoder, 0, 0,
00177 avctx->width, avctx->height);
00178 if (c->corrupted)
00179 return AVERROR_INVALIDDATA;
00180 memcpy(ctx->pic.data[1], c->pal, AVPALETTE_SIZE);
00181 ctx->pic.palette_has_changed = pal_changed;
00182
00183 *data_size = sizeof(AVFrame);
00184 *(AVFrame*)data = ctx->pic;
00185
00186
00187 return buf_size;
00188 }
00189
00190 static av_cold int mss1_decode_init(AVCodecContext *avctx)
00191 {
00192 MSS1Context * const c = avctx->priv_data;
00193 int ret;
00194
00195 c->ctx.avctx = avctx;
00196 avctx->coded_frame = &c->pic;
00197
00198 ret = ff_mss12_decode_init(&c->ctx, 0, &c->sc, NULL);
00199
00200 avctx->pix_fmt = PIX_FMT_PAL8;
00201
00202 return ret;
00203 }
00204
00205 static av_cold int mss1_decode_end(AVCodecContext *avctx)
00206 {
00207 MSS1Context * const ctx = avctx->priv_data;
00208
00209 if (ctx->pic.data[0])
00210 avctx->release_buffer(avctx, &ctx->pic);
00211 ff_mss12_decode_end(&ctx->ctx);
00212
00213 return 0;
00214 }
00215
00216 AVCodec ff_mss1_decoder = {
00217 .name = "mss1",
00218 .type = AVMEDIA_TYPE_VIDEO,
00219 .id = AV_CODEC_ID_MSS1,
00220 .priv_data_size = sizeof(MSS1Context),
00221 .init = mss1_decode_init,
00222 .close = mss1_decode_end,
00223 .decode = mss1_decode_frame,
00224 .capabilities = CODEC_CAP_DR1,
00225 .long_name = NULL_IF_CONFIG_SMALL("MS Screen 1"),
00226 };