2 * Copyright (c) 2012 Konstantin Shishkov
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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 thr = 2 * m->weights[m->num_syms] - 1;
51 thr = ((thr >> 1) + 4 * m->cum_prob[0]) / thr;
53 return FFMIN(thr, 0x3FFF);
56 static void model_reset(Model *m)
60 for (i = 0; i <= m->num_syms; i++) {
62 m->cum_prob[i] = m->num_syms - i;
66 m->sym2idx[m->num_syms] = -1;
67 for (i = 0; i < m->num_syms; i++) {
68 m->sym2idx[i] = i + 1;
69 m->idx2sym[i + 1] = i;
73 static av_cold void model_init(Model *m, int num_syms, int thr_weight)
75 m->num_syms = num_syms;
76 m->thr_weight = thr_weight;
77 m->threshold = num_syms * thr_weight;
81 static void model_rescale_weights(Model *m)
86 if (m->thr_weight == THRESH_ADAPTIVE)
87 m->threshold = model_calc_threshold(m);
88 while (m->cum_prob[0] > m->threshold) {
90 for (i = m->num_syms; i >= 0; i--) {
91 m->cum_prob[i] = cum_prob;
92 m->weights[i] = (m->weights[i] + 1) >> 1;
93 cum_prob += m->weights[i];
98 void ff_mss12_model_update(Model *m, int val)
102 if (m->weights[val] == m->weights[val - 1]) {
103 for (i = val; m->weights[i - 1] == m->weights[val]; i--);
107 sym1 = m->idx2sym[val];
108 sym2 = m->idx2sym[i];
110 m->idx2sym[val] = sym2;
111 m->idx2sym[i] = sym1;
112 m->sym2idx[sym1] = i;
113 m->sym2idx[sym2] = val;
119 for (i = val - 1; i >= 0; i--)
121 model_rescale_weights(m);
124 static void pixctx_reset(PixContext *ctx)
128 if (!ctx->special_initial_cache)
129 for (i = 0; i < ctx->cache_size; i++)
137 model_reset(&ctx->cache_model);
138 model_reset(&ctx->full_model);
140 for (i = 0; i < 4; i++)
141 for (j = 0; j < sec_order_sizes[i]; j++)
142 for (k = 0; k < 4; k++)
143 model_reset(&ctx->sec_models[i][j][k]);
146 static av_cold void pixctx_init(PixContext *ctx, int cache_size,
147 int full_model_syms, int special_initial_cache)
151 ctx->cache_size = cache_size + 4;
152 ctx->num_syms = cache_size;
153 ctx->special_initial_cache = special_initial_cache;
155 model_init(&ctx->cache_model, ctx->num_syms + 1, THRESH_LOW);
156 model_init(&ctx->full_model, full_model_syms, THRESH_HIGH);
158 for (i = 0; i < 4; i++)
159 for (j = 0; j < sec_order_sizes[i]; j++)
160 for (k = 0; k < 4; k++)
161 model_init(&ctx->sec_models[i][j][k], 2 + i,
162 i ? THRESH_LOW : THRESH_ADAPTIVE);
165 static int decode_top_left_pixel(ArithCoder *acoder, PixContext *pctx)
169 val = acoder->get_model_sym(acoder, &pctx->cache_model);
170 if (val < pctx->num_syms) {
171 pix = pctx->cache[val];
173 pix = acoder->get_model_sym(acoder, &pctx->full_model);
174 for (i = 0; i < pctx->cache_size - 1; i++)
175 if (pctx->cache[i] == pix)
180 for (i = val; i > 0; i--)
181 pctx->cache[i] = pctx->cache[i - 1];
182 pctx->cache[0] = pix;
188 static int decode_pixel(ArithCoder *acoder, PixContext *pctx,
189 uint8_t *ngb, int num_ngb)
193 val = acoder->get_model_sym(acoder, &pctx->cache_model);
194 if (val < pctx->num_syms) {
198 for (i = 0; i < pctx->cache_size; i++) {
199 for (j = 0; j < num_ngb; j++)
200 if (pctx->cache[i] == ngb[j])
208 val = FFMIN(i, pctx->cache_size - 1);
209 pix = pctx->cache[val];
211 pix = acoder->get_model_sym(acoder, &pctx->full_model);
212 for (i = 0; i < pctx->cache_size - 1; i++)
213 if (pctx->cache[i] == pix)
218 for (i = val; i > 0; i--)
219 pctx->cache[i] = pctx->cache[i - 1];
220 pctx->cache[0] = pix;
226 static int decode_pixel_in_context(ArithCoder *acoder, PixContext *pctx,
227 uint8_t *src, int stride, int x, int y,
230 uint8_t neighbours[4];
238 memset(neighbours, src[-1], 4);
240 neighbours[TOP] = src[-stride];
242 neighbours[TOP_LEFT] = neighbours[LEFT] = neighbours[TOP];
244 neighbours[TOP_LEFT] = src[-stride - 1];
245 neighbours[ LEFT] = src[-1];
248 neighbours[TOP_RIGHT] = src[-stride + 1];
250 neighbours[TOP_RIGHT] = neighbours[TOP];
254 if (x >= 2 && src[-2] == neighbours[LEFT])
256 if (y >= 2 && src[-2 * stride] == neighbours[TOP])
260 ref_pix[0] = neighbours[0];
261 for (i = 1; i < 4; i++) {
262 for (j = 0; j < nlen; j++)
263 if (ref_pix[j] == neighbours[i])
266 ref_pix[nlen++] = neighbours[i];
275 if (neighbours[TOP] == neighbours[TOP_LEFT]) {
276 if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT])
278 else if (neighbours[LEFT] == neighbours[TOP_LEFT])
282 } else if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT]) {
283 if (neighbours[LEFT] == neighbours[TOP_LEFT])
287 } else if (neighbours[LEFT] == neighbours[TOP_LEFT]) {
294 if (neighbours[TOP] == neighbours[TOP_LEFT])
296 else if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT])
298 else if (neighbours[LEFT] == neighbours[TOP_LEFT])
300 else if (neighbours[TOP_RIGHT] == neighbours[TOP])
302 else if (neighbours[TOP] == neighbours[LEFT])
309 pix = acoder->get_model_sym(acoder,
310 &pctx->sec_models[nlen - 1][layer][sub]);
314 return decode_pixel(acoder, pctx, ref_pix, nlen);
317 static int decode_region(ArithCoder *acoder, uint8_t *dst, uint8_t *rgb_pic,
318 int x, int y, int width, int height, int stride,
319 int rgb_stride, PixContext *pctx, const uint32_t *pal)
322 uint8_t *rgb_dst = rgb_pic + x * 3 + y * rgb_stride;
324 dst += x + y * stride;
326 for (j = 0; j < height; j++) {
327 for (i = 0; i < width; i++) {
329 p = decode_top_left_pixel(acoder, pctx);
331 p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
332 i, j, width - i - 1);
336 AV_WB24(rgb_dst + i * 3, pal[p]);
339 rgb_dst += rgb_stride;
345 static void copy_rectangles(MSS12Context const *c,
346 int x, int y, int width, int height)
351 for (j = y; j < y + height; j++) {
352 memcpy(c->rgb_pic + j * c->rgb_stride + x * 3,
353 c->last_rgb_pic + j * c->rgb_stride + x * 3,
355 memcpy(c->pal_pic + j * c->pal_stride + x,
356 c->last_pal_pic + j * c->pal_stride + x,
361 static int motion_compensation(MSS12Context const *c,
362 int x, int y, int width, int height)
364 if (x + c->mvX < 0 || x + c->mvX + width > c->avctx->width ||
365 y + c->mvY < 0 || y + c->mvY + height > c->avctx->height ||
369 uint8_t *dst = c->pal_pic + x + y * c->pal_stride;
370 uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
376 if (c->last_rgb_pic) {
377 src = c->last_pal_pic + x + y * c->pal_stride;
378 rgb_src = c->last_rgb_pic + x * 3 + y * c->rgb_stride;
380 src = c->pal_pic + x + y * c->pal_stride;
381 rgb_src = c->rgb_pic + x * 3 + y * c->rgb_stride;
383 for (j = 0; j < height; j++) {
384 memmove(dst, src, width);
385 memmove(rgb_dst, rgb_src, width * 3);
386 dst += c->pal_stride;
387 src += c->pal_stride;
388 rgb_dst += c->rgb_stride;
389 rgb_src += c->rgb_stride;
395 static int decode_region_masked(MSS12Context const *c, ArithCoder *acoder,
396 uint8_t *dst, int stride, uint8_t *mask,
397 int mask_stride, int x, int y,
398 int width, int height,
402 uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
404 dst += x + y * stride;
405 mask += x + y * mask_stride;
407 for (j = 0; j < height; j++) {
408 for (i = 0; i < width; i++) {
409 if (c->avctx->err_recognition & AV_EF_EXPLODE &&
410 ( c->rgb_pic && mask[i] != 0x01 && mask[i] != 0x02 && mask[i] != 0x04 ||
411 !c->rgb_pic && mask[i] != 0x80 && mask[i] != 0xFF))
414 if (mask[i] == 0x02) {
415 copy_rectangles(c, x + i, y + j, 1, 1);
416 } else if (mask[i] == 0x04) {
417 if (motion_compensation(c, x + i, y + j, 1, 1))
419 } else if (mask[i] != 0x80) {
421 p = decode_top_left_pixel(acoder, pctx);
423 p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
424 i, j, width - i - 1);
427 AV_WB24(rgb_dst + i * 3, c->pal[p]);
432 rgb_dst += c->rgb_stride;
438 static av_cold void slicecontext_init(SliceContext *sc,
439 int version, int full_model_syms)
441 model_init(&sc->intra_region, 2, THRESH_ADAPTIVE);
442 model_init(&sc->inter_region, 2, THRESH_ADAPTIVE);
443 model_init(&sc->split_mode, 3, THRESH_HIGH);
444 model_init(&sc->edge_mode, 2, THRESH_HIGH);
445 model_init(&sc->pivot, 3, THRESH_LOW);
447 pixctx_init(&sc->intra_pix_ctx, 8, full_model_syms, 0);
449 pixctx_init(&sc->inter_pix_ctx, version ? 3 : 2,
450 full_model_syms, version ? 1 : 0);
453 void ff_mss12_slicecontext_reset(SliceContext *sc)
455 model_reset(&sc->intra_region);
456 model_reset(&sc->inter_region);
457 model_reset(&sc->split_mode);
458 model_reset(&sc->edge_mode);
459 model_reset(&sc->pivot);
460 pixctx_reset(&sc->intra_pix_ctx);
461 pixctx_reset(&sc->inter_pix_ctx);
464 static int decode_pivot(SliceContext *sc, ArithCoder *acoder, int base)
468 inv = acoder->get_model_sym(acoder, &sc->edge_mode);
469 val = acoder->get_model_sym(acoder, &sc->pivot) + 1;
472 if ((base + 1) / 2 - 2 <= 0)
475 val = acoder->get_number(acoder, (base + 1) / 2 - 2) + 3;
481 return inv ? base - val : val;
484 static int decode_region_intra(SliceContext *sc, ArithCoder *acoder,
485 int x, int y, int width, int height)
487 MSS12Context const *c = sc->c;
490 mode = acoder->get_model_sym(acoder, &sc->intra_region);
493 int i, j, pix, rgb_pix;
494 int stride = c->pal_stride;
495 int rgb_stride = c->rgb_stride;
496 uint8_t *dst = c->pal_pic + x + y * stride;
497 uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * rgb_stride;
499 pix = decode_top_left_pixel(acoder, &sc->intra_pix_ctx);
500 rgb_pix = c->pal[pix];
501 for (i = 0; i < height; i++, dst += stride, rgb_dst += rgb_stride) {
502 memset(dst, pix, width);
504 for (j = 0; j < width * 3; j += 3)
505 AV_WB24(rgb_dst + j, rgb_pix);
508 return decode_region(acoder, c->pal_pic, c->rgb_pic,
509 x, y, width, height, c->pal_stride, c->rgb_stride,
510 &sc->intra_pix_ctx, &c->pal[0]);
516 static int decode_region_inter(SliceContext *sc, ArithCoder *acoder,
517 int x, int y, int width, int height)
519 MSS12Context const *c = sc->c;
522 mode = acoder->get_model_sym(acoder, &sc->inter_region);
525 mode = decode_top_left_pixel(acoder, &sc->inter_pix_ctx);
527 if (c->avctx->err_recognition & AV_EF_EXPLODE &&
528 ( c->rgb_pic && mode != 0x01 && mode != 0x02 && mode != 0x04 ||
529 !c->rgb_pic && mode != 0x80 && mode != 0xFF))
533 copy_rectangles(c, x, y, width, height);
534 else if (mode == 0x04)
535 return motion_compensation(c, x, y, width, height);
536 else if (mode != 0x80)
537 return decode_region_intra(sc, acoder, x, y, width, height);
539 if (decode_region(acoder, c->mask, NULL,
540 x, y, width, height, c->mask_stride, 0,
541 &sc->inter_pix_ctx, &c->pal[0]) < 0)
543 return decode_region_masked(c, acoder, c->pal_pic,
544 c->pal_stride, c->mask,
553 int ff_mss12_decode_rect(SliceContext *sc, ArithCoder *acoder,
554 int x, int y, int width, int height)
558 mode = acoder->get_model_sym(acoder, &sc->split_mode);
562 if ((pivot = decode_pivot(sc, acoder, height)) < 1)
564 if (ff_mss12_decode_rect(sc, acoder, x, y, width, pivot))
566 if (ff_mss12_decode_rect(sc, acoder, x, y + pivot, width, height - pivot))
570 if ((pivot = decode_pivot(sc, acoder, width)) < 1)
572 if (ff_mss12_decode_rect(sc, acoder, x, y, pivot, height))
574 if (ff_mss12_decode_rect(sc, acoder, x + pivot, y, width - pivot, height))
579 return decode_region_intra(sc, acoder, x, y, width, height);
581 return decode_region_inter(sc, acoder, x, y, width, height);
589 av_cold int ff_mss12_decode_init(MSS12Context *c, int version,
590 SliceContext* sc1, SliceContext *sc2)
592 AVCodecContext *avctx = c->avctx;
595 if (avctx->extradata_size < 52 + 256 * 3) {
596 av_log(avctx, AV_LOG_ERROR, "Insufficient extradata size %d\n",
597 avctx->extradata_size);
598 return AVERROR_INVALIDDATA;
601 if (AV_RB32(avctx->extradata) < avctx->extradata_size) {
602 av_log(avctx, AV_LOG_ERROR,
603 "Insufficient extradata size: expected %d got %d\n",
604 AV_RB32(avctx->extradata),
605 avctx->extradata_size);
606 return AVERROR_INVALIDDATA;
609 avctx->coded_width = AV_RB32(avctx->extradata + 20);
610 avctx->coded_height = AV_RB32(avctx->extradata + 24);
611 if (avctx->coded_width > 4096 || avctx->coded_height > 4096) {
612 av_log(avctx, AV_LOG_ERROR, "Frame dimensions %dx%d too large",
613 avctx->coded_width, avctx->coded_height);
614 return AVERROR_INVALIDDATA;
617 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d\n",
618 AV_RB32(avctx->extradata + 4), AV_RB32(avctx->extradata + 8));
619 if (version != AV_RB32(avctx->extradata + 4) > 1) {
620 av_log(avctx, AV_LOG_ERROR,
621 "Header version doesn't match codec tag\n");
625 c->free_colours = AV_RB32(avctx->extradata + 48);
626 if ((unsigned)c->free_colours > 256) {
627 av_log(avctx, AV_LOG_ERROR,
628 "Incorrect number of changeable palette entries: %d\n",
630 return AVERROR_INVALIDDATA;
632 av_log(avctx, AV_LOG_DEBUG, "%d free colour(s)\n", c->free_colours);
634 av_log(avctx, AV_LOG_DEBUG, "Display dimensions %dx%d\n",
635 AV_RB32(avctx->extradata + 12), AV_RB32(avctx->extradata + 16));
636 av_log(avctx, AV_LOG_DEBUG, "Coded dimensions %dx%d\n",
637 avctx->coded_width, avctx->coded_height);
638 av_log(avctx, AV_LOG_DEBUG, "%g frames per second\n",
639 av_int2float(AV_RB32(avctx->extradata + 28)));
640 av_log(avctx, AV_LOG_DEBUG, "Bitrate %d bps\n",
641 AV_RB32(avctx->extradata + 32));
642 av_log(avctx, AV_LOG_DEBUG, "Max. lead time %g ms\n",
643 av_int2float(AV_RB32(avctx->extradata + 36)));
644 av_log(avctx, AV_LOG_DEBUG, "Max. lag time %g ms\n",
645 av_int2float(AV_RB32(avctx->extradata + 40)));
646 av_log(avctx, AV_LOG_DEBUG, "Max. seek time %g ms\n",
647 av_int2float(AV_RB32(avctx->extradata + 44)));
650 if (avctx->extradata_size < 60 + 256 * 3) {
651 av_log(avctx, AV_LOG_ERROR,
652 "Insufficient extradata size %d for v2\n",
653 avctx->extradata_size);
654 return AVERROR_INVALIDDATA;
657 c->slice_split = AV_RB32(avctx->extradata + 52);
658 av_log(avctx, AV_LOG_DEBUG, "Slice split %d\n", c->slice_split);
660 c->full_model_syms = AV_RB32(avctx->extradata + 56);
661 if (c->full_model_syms < 2 || c->full_model_syms > 256) {
662 av_log(avctx, AV_LOG_ERROR,
663 "Incorrect number of used colours %d\n",
665 return AVERROR_INVALIDDATA;
667 av_log(avctx, AV_LOG_DEBUG, "Used colours %d\n",
671 c->full_model_syms = 256;
674 for (i = 0; i < 256; i++)
675 c->pal[i] = AV_RB24(avctx->extradata + 52 +
676 (version ? 8 : 0) + i * 3);
678 c->mask_stride = FFALIGN(avctx->width, 16);
679 c->mask = av_malloc(c->mask_stride * avctx->height);
681 av_log(avctx, AV_LOG_ERROR, "Cannot allocate mask plane\n");
682 return AVERROR(ENOMEM);
686 slicecontext_init(sc1, version, c->full_model_syms);
687 if (c->slice_split) {
689 slicecontext_init(sc2, version, c->full_model_syms);
696 av_cold int ff_mss12_decode_end(MSS12Context *c)