2 * Copyright (c) 2012 Konstantin Shishkov
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Common functions for Microsoft Screen 1 and 2
26 #include "libavutil/intfloat.h"
27 #include "libavutil/intreadwrite.h"
37 static const int sec_order_sizes[4] = { 1, 7, 6, 1 };
39 enum ContextDirection {
46 static int model_calc_threshold(Model *m)
50 if (m->thr_weight == -1) {
51 thr = 2 * m->weights[m->num_syms] - 1;
52 thr = ((thr >> 1) + 4 * m->cum_prob[0]) / thr;
54 thr = m->num_syms * m->thr_weight;
57 return FFMIN(thr, 0x3FFF);
60 static void model_reset(Model *m)
64 for (i = 0; i <= m->num_syms; i++) {
66 m->cum_prob[i] = m->num_syms - i;
70 m->sym2idx[m->num_syms] = -1;
71 for (i = 0; i < m->num_syms; i++) {
72 m->sym2idx[i] = i + 1;
73 m->idx2sym[i + 1] = i;
77 static av_cold void model_init(Model *m, int num_syms, int thr_weight)
79 m->num_syms = num_syms;
80 m->thr_weight = thr_weight;
81 m->threshold = model_calc_threshold(m);
85 static void model_rescale_weights(Model *m)
90 if (m->thr_weight == -1)
91 m->threshold = model_calc_threshold(m);
92 while (m->cum_prob[0] > m->threshold) {
94 for (i = m->num_syms; i >= 0; i--) {
95 m->cum_prob[i] = cum_prob;
96 m->weights[i] = (m->weights[i] + 1) >> 1;
97 cum_prob += m->weights[i];
102 void ff_mss12_model_update(Model *m, int val)
106 if (m->weights[val] == m->weights[val - 1]) {
107 for (i = val; m->weights[i - 1] == m->weights[val]; i--);
111 sym1 = m->idx2sym[val];
112 sym2 = m->idx2sym[i];
114 m->idx2sym[val] = sym2;
115 m->idx2sym[i] = sym1;
116 m->sym2idx[sym1] = i;
117 m->sym2idx[sym2] = val;
123 for (i = val - 1; i >= 0; i--)
125 model_rescale_weights(m);
128 static void pixctx_reset(PixContext *ctx)
132 for (i = 0; i < ctx->cache_size; i++)
135 model_reset(&ctx->cache_model);
136 model_reset(&ctx->full_model);
138 for (i = 0; i < 4; i++)
139 for (j = 0; j < sec_order_sizes[i]; j++)
140 for (k = 0; k < 4; k++)
141 model_reset(&ctx->sec_models[i][j][k]);
144 static av_cold void pixctx_init(PixContext *ctx, int cache_size)
148 ctx->cache_size = cache_size + 4;
149 ctx->num_syms = cache_size;
151 for (i = 0; i < ctx->cache_size; i++)
154 model_init(&ctx->cache_model, ctx->num_syms + 1, THRESH_LOW);
155 model_init(&ctx->full_model, 256, THRESH_HIGH);
157 for (i = 0; i < 4; i++) {
158 for (j = 0; j < sec_order_sizes[i]; j++) {
159 for (k = 0; k < 4; k++) {
160 model_init(&ctx->sec_models[i][j][k], 2 + i,
161 i ? THRESH_LOW : THRESH_ADAPTIVE);
167 static int decode_top_left_pixel(ArithCoder *acoder, PixContext *pctx)
171 val = acoder->get_model_sym(acoder, &pctx->cache_model);
172 if (val < pctx->num_syms) {
173 pix = pctx->cache[val];
175 pix = acoder->get_model_sym(acoder, &pctx->full_model);
176 for (i = 0; i < pctx->cache_size - 1; i++)
177 if (pctx->cache[i] == pix)
182 for (i = val; i > 0; i--)
183 pctx->cache[i] = pctx->cache[i - 1];
184 pctx->cache[0] = pix;
190 static int decode_pixel(ArithCoder *acoder, PixContext *pctx,
191 uint8_t *ngb, int num_ngb)
195 val = acoder->get_model_sym(acoder, &pctx->cache_model);
196 if (val < pctx->num_syms) {
201 for (i = 0; i < pctx->cache_size; i++) {
202 for (j = 0; j < num_ngb; j++)
203 if (pctx->cache[i] == ngb[j])
211 val = FFMIN(i, pctx->cache_size - 1);
212 pix = pctx->cache[val];
214 pix = acoder->get_model_sym(acoder, &pctx->full_model);
215 for (i = 0; i < pctx->cache_size - 1; i++)
216 if (pctx->cache[i] == pix)
221 for (i = val; i > 0; i--)
222 pctx->cache[i] = pctx->cache[i - 1];
223 pctx->cache[0] = pix;
229 static int decode_pixel_in_context(ArithCoder *acoder, PixContext *pctx,
230 uint8_t *src, int stride, int x, int y,
233 uint8_t neighbours[4];
241 memset(neighbours, src[-1], 4);
243 neighbours[TOP] = src[-stride];
245 neighbours[TOP_LEFT] = neighbours[LEFT] = neighbours[TOP];
247 neighbours[TOP_LEFT] = src[-stride - 1];
248 neighbours[ LEFT] = src[-1];
251 neighbours[TOP_RIGHT] = src[-stride + 1];
253 neighbours[TOP_RIGHT] = neighbours[TOP];
257 if (x >= 2 && src[-2] == neighbours[LEFT])
259 if (y >= 2 && src[-2 * stride] == neighbours[TOP])
263 ref_pix[0] = neighbours[0];
264 for (i = 1; i < 4; i++) {
265 for (j = 0; j < nlen; j++)
266 if (ref_pix[j] == neighbours[i])
269 ref_pix[nlen++] = neighbours[i];
278 if (neighbours[TOP] == neighbours[TOP_LEFT]) {
279 if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT])
281 else if (neighbours[LEFT] == neighbours[TOP_LEFT])
285 } else if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT]) {
286 if (neighbours[LEFT] == neighbours[TOP_LEFT])
290 } else if (neighbours[LEFT] == neighbours[TOP_LEFT]) {
297 if (neighbours[TOP] == neighbours[TOP_LEFT])
299 else if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT])
301 else if (neighbours[LEFT] == neighbours[TOP_LEFT])
303 else if (neighbours[TOP_RIGHT] == neighbours[TOP])
305 else if (neighbours[TOP] == neighbours[LEFT])
312 pix = acoder->get_model_sym(acoder, &pctx->sec_models[nlen - 1][layer][sub]);
316 return decode_pixel(acoder, pctx, ref_pix, nlen);
319 static int decode_region(MSS12Context *ctx, ArithCoder *acoder, uint8_t *dst,
320 int x, int y, int width, int height, int stride,
325 dst += x + y * stride;
327 dst[0] = decode_top_left_pixel(acoder, pctx);
328 for (j = 0; j < height; j++) {
329 for (i = 0; i < width; i++) {
333 dst[i] = decode_pixel_in_context(acoder, pctx, dst + i, stride,
334 i, j, width - i - 1);
342 static int decode_region_masked(MSS12Context *ctx, ArithCoder *acoder,
343 uint8_t *dst, int stride, uint8_t *mask,
344 int mask_stride, int x, int y,
345 int width, int height,
350 dst += x + y * stride;
351 mask += x + y * mask_stride;
354 dst[0] = decode_top_left_pixel(acoder, pctx);
355 for (j = 0; j < height; j++) {
356 for (i = 0; i < width; i++) {
357 if (!i && !j || mask[i] != 0xFF)
360 dst[i] = decode_pixel_in_context(acoder, pctx, dst + i, stride,
361 i, j, width - i - 1);
370 static av_cold void codec_init(MSS12Context *ctx)
372 model_init(&ctx->intra_region, 2, THRESH_ADAPTIVE);
373 model_init(&ctx->inter_region, 2, THRESH_ADAPTIVE);
374 model_init(&ctx->split_mode, 3, THRESH_HIGH);
375 model_init(&ctx->edge_mode, 2, THRESH_HIGH);
376 model_init(&ctx->pivot, 3, THRESH_LOW);
377 pixctx_init(&ctx->intra_pix_ctx, 8);
378 pixctx_init(&ctx->inter_pix_ctx, 2);
382 void ff_mss12_codec_reset(MSS12Context *ctx)
384 model_reset(&ctx->intra_region);
385 model_reset(&ctx->inter_region);
386 model_reset(&ctx->split_mode);
387 model_reset(&ctx->edge_mode);
388 model_reset(&ctx->pivot);
389 pixctx_reset(&ctx->intra_pix_ctx);
390 pixctx_reset(&ctx->inter_pix_ctx);
395 static int decode_pivot(MSS12Context *ctx, ArithCoder *acoder, int base)
399 inv = acoder->get_model_sym(acoder, &ctx->edge_mode);
400 val = acoder->get_model_sym(acoder, &ctx->pivot) + 1;
403 if ((base + 1) / 2 - 2 <= 0) {
407 val = acoder->get_number(acoder, (base + 1) / 2 - 2) + 3;
410 if ((unsigned)val >= base) {
415 return inv ? base - val : val;
418 static int decode_region_intra(MSS12Context *ctx, ArithCoder *acoder,
419 int x, int y, int width, int height)
423 mode = acoder->get_model_sym(acoder, &ctx->intra_region);
427 int stride = ctx->pic_stride;
428 uint8_t *dst = ctx->pic_start + x + y * stride;
430 pix = decode_top_left_pixel(acoder, &ctx->intra_pix_ctx);
431 for (i = 0; i < height; i++, dst += stride)
432 memset(dst, pix, width);
434 return decode_region(ctx, acoder, ctx->pic_start,
435 x, y, width, height, ctx->pic_stride,
436 &ctx->intra_pix_ctx);
442 static int decode_region_inter(MSS12Context *ctx, ArithCoder *acoder,
443 int x, int y, int width, int height)
447 mode = acoder->get_model_sym(acoder, &ctx->inter_region);
450 mode = decode_top_left_pixel(acoder, &ctx->inter_pix_ctx);
454 return decode_region_intra(ctx, acoder, x, y, width, height);
457 if (decode_region(ctx, acoder, ctx->mask,
458 x, y, width, height, ctx->mask_linesize,
459 &ctx->inter_pix_ctx) < 0)
461 return decode_region_masked(ctx, acoder, ctx->pic_start,
462 ctx->pic_stride, ctx->mask,
465 &ctx->intra_pix_ctx);
471 int ff_mss12_decode_rect(MSS12Context *ctx, ArithCoder *acoder,
472 int x, int y, int width, int height)
479 mode = acoder->get_model_sym(acoder, &ctx->split_mode);
483 pivot = decode_pivot(ctx, acoder, height);
484 if (ff_mss12_decode_rect(ctx, acoder, x, y, width, pivot))
486 if (ff_mss12_decode_rect(ctx, acoder, x, y + pivot, width, height - pivot))
490 pivot = decode_pivot(ctx, acoder, width);
491 if (ff_mss12_decode_rect(ctx, acoder, x, y, pivot, height))
493 if (ff_mss12_decode_rect(ctx, acoder, x + pivot, y, width - pivot, height))
498 return decode_region_intra(ctx, acoder, x, y, width, height);
500 return decode_region_inter(ctx, acoder, x, y, width, height);
508 av_cold int ff_mss12_decode_init(AVCodecContext *avctx, int version)
510 MSS12Context * const c = avctx->priv_data;
515 if (avctx->extradata_size < 52 + 256 * 3) {
516 av_log(avctx, AV_LOG_ERROR, "Insufficient extradata size %d\n",
517 avctx->extradata_size);
518 return AVERROR_INVALIDDATA;
521 if (AV_RB32(avctx->extradata) < avctx->extradata_size) {
522 av_log(avctx, AV_LOG_ERROR,
523 "Insufficient extradata size: expected %d got %d\n",
524 AV_RB32(avctx->extradata),
525 avctx->extradata_size);
526 return AVERROR_INVALIDDATA;
529 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d\n",
530 AV_RB32(avctx->extradata + 4), AV_RB32(avctx->extradata + 8));
531 c->free_colours = AV_RB32(avctx->extradata + 48);
532 if ((unsigned)c->free_colours > 256) {
533 av_log(avctx, AV_LOG_ERROR,
534 "Incorrect number of changeable palette entries: %d\n",
536 return AVERROR_INVALIDDATA;
538 av_log(avctx, AV_LOG_DEBUG, "%d free colour(s)\n", c->free_colours);
539 avctx->coded_width = AV_RB32(avctx->extradata + 20);
540 avctx->coded_height = AV_RB32(avctx->extradata + 24);
542 av_log(avctx, AV_LOG_DEBUG, "Display dimensions %dx%d\n",
543 AV_RB32(avctx->extradata + 12), AV_RB32(avctx->extradata + 16));
544 av_log(avctx, AV_LOG_DEBUG, "Coded dimensions %dx%d\n",
545 avctx->coded_width, avctx->coded_height);
546 av_log(avctx, AV_LOG_DEBUG, "%g frames per second\n",
547 av_int2float(AV_RB32(avctx->extradata + 28)));
548 av_log(avctx, AV_LOG_DEBUG, "Bitrate %d bps\n",
549 AV_RB32(avctx->extradata + 32));
550 av_log(avctx, AV_LOG_DEBUG, "Max. lead time %g ms\n",
551 av_int2float(AV_RB32(avctx->extradata + 36)));
552 av_log(avctx, AV_LOG_DEBUG, "Max. lag time %g ms\n",
553 av_int2float(AV_RB32(avctx->extradata + 40)));
554 av_log(avctx, AV_LOG_DEBUG, "Max. seek time %g ms\n",
555 av_int2float(AV_RB32(avctx->extradata + 44)));
557 for (i = 0; i < 256; i++)
558 c->pal[i] = 0xFF << 24 | AV_RB24(avctx->extradata + 52 + i * 3);
560 avctx->pix_fmt = PIX_FMT_PAL8;
562 c->mask_linesize = FFALIGN(avctx->width, 16);
563 c->mask = av_malloc(c->mask_linesize * avctx->height);
565 av_log(avctx, AV_LOG_ERROR, "Cannot allocate mask plane\n");
566 return AVERROR(ENOMEM);
574 av_cold int ff_mss12_decode_end(AVCodecContext *avctx)
576 MSS12Context * const c = avctx->priv_data;