mss12.c 21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*
 * Copyright (c) 2012 Konstantin Shishkov
 *
 * This file is part of Libav.
 *
 * Libav is free software; you can redistribute it and/or
 * 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.
 *
 * Libav is distributed in the hope that it will be useful,
 * 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
 * License along with Libav; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * Common functions for Microsoft Screen 1 and 2
 */

#include "libavutil/intfloat.h"
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#include "mss12.h"

enum SplitMode {
    SPLIT_VERT = 0,
    SPLIT_HOR,
    SPLIT_NONE
};

static const int sec_order_sizes[4] = { 1, 7, 6, 1 };

enum ContextDirection {
    TOP_LEFT = 0,
    TOP,
    TOP_RIGHT,
    LEFT
};

static int model_calc_threshold(Model *m)
{
    int thr;

Alberto Delmás's avatar
Alberto Delmás committed
50 51
    thr = 2 * m->weights[m->num_syms] - 1;
    thr = ((thr >> 1) + 4 * m->cum_prob[0]) / thr;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

    return FFMIN(thr, 0x3FFF);
}

static void model_reset(Model *m)
{
    int i;

    for (i = 0; i <= m->num_syms; i++) {
        m->weights[i]  = 1;
        m->cum_prob[i] = m->num_syms - i;
    }
    m->weights[0]           = -1;
    m->idx2sym[0]           = -1;
    m->sym2idx[m->num_syms] = -1;
    for (i = 0; i < m->num_syms; i++) {
        m->sym2idx[i]     = i + 1;
        m->idx2sym[i + 1] = i;
    }
}

static av_cold void model_init(Model *m, int num_syms, int thr_weight)
{
    m->num_syms   = num_syms;
    m->thr_weight = thr_weight;
Alberto Delmás's avatar
Alberto Delmás committed
77
    m->threshold  = num_syms * thr_weight;
78 79 80 81 82 83 84 85
    model_reset(m);
}

static void model_rescale_weights(Model *m)
{
    int i;
    int cum_prob;

Alberto Delmás's avatar
Alberto Delmás committed
86
    if (m->thr_weight == THRESH_ADAPTIVE)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        m->threshold = model_calc_threshold(m);
    while (m->cum_prob[0] > m->threshold) {
        cum_prob = 0;
        for (i = m->num_syms; i >= 0; i--) {
            m->cum_prob[i] = cum_prob;
            m->weights[i]  = (m->weights[i] + 1) >> 1;
            cum_prob      += m->weights[i];
        }
    }
}

void ff_mss12_model_update(Model *m, int val)
{
    int i;

    if (m->weights[val] == m->weights[val - 1]) {
        for (i = val; m->weights[i - 1] == m->weights[val]; i--);
        if (i != val) {
            int sym1, sym2;

            sym1 = m->idx2sym[val];
            sym2 = m->idx2sym[i];

            m->idx2sym[val]  = sym2;
            m->idx2sym[i]    = sym1;
            m->sym2idx[sym1] = i;
            m->sym2idx[sym2] = val;

            val = i;
        }
    }
    m->weights[val]++;
    for (i = val - 1; i >= 0; i--)
        m->cum_prob[i]++;
    model_rescale_weights(m);
}

static void pixctx_reset(PixContext *ctx)
{
    int i, j, k;

Alberto Delmás's avatar
Alberto Delmás committed
128 129 130 131 132 133 134 135
    if (!ctx->special_initial_cache)
        for (i = 0; i < ctx->cache_size; i++)
            ctx->cache[i] = i;
    else {
        ctx->cache[0] = 1;
        ctx->cache[1] = 2;
        ctx->cache[2] = 4;
    }
136 137 138 139 140 141 142 143 144 145

    model_reset(&ctx->cache_model);
    model_reset(&ctx->full_model);

    for (i = 0; i < 4; i++)
        for (j = 0; j < sec_order_sizes[i]; j++)
            for (k = 0; k < 4; k++)
                model_reset(&ctx->sec_models[i][j][k]);
}

Alberto Delmás's avatar
Alberto Delmás committed
146 147
static av_cold void pixctx_init(PixContext *ctx, int cache_size,
                                int full_model_syms, int special_initial_cache)
148 149 150
{
    int i, j, k;

Alberto Delmás's avatar
Alberto Delmás committed
151 152 153
    ctx->cache_size            = cache_size + 4;
    ctx->num_syms              = cache_size;
    ctx->special_initial_cache = special_initial_cache;
154 155

    model_init(&ctx->cache_model, ctx->num_syms + 1, THRESH_LOW);
Alberto Delmás's avatar
Alberto Delmás committed
156
    model_init(&ctx->full_model, full_model_syms, THRESH_HIGH);
157

Alberto Delmás's avatar
Alberto Delmás committed
158 159 160
    for (i = 0; i < 4; i++)
        for (j = 0; j < sec_order_sizes[i]; j++)
            for (k = 0; k < 4; k++)
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
                model_init(&ctx->sec_models[i][j][k], 2 + i,
                           i ? THRESH_LOW : THRESH_ADAPTIVE);
}

static int decode_top_left_pixel(ArithCoder *acoder, PixContext *pctx)
{
    int i, val, pix;

    val = acoder->get_model_sym(acoder, &pctx->cache_model);
    if (val < pctx->num_syms) {
        pix = pctx->cache[val];
    } else {
        pix = acoder->get_model_sym(acoder, &pctx->full_model);
        for (i = 0; i < pctx->cache_size - 1; i++)
            if (pctx->cache[i] == pix)
                break;
        val = i;
    }
    if (val) {
        for (i = val; i > 0; i--)
            pctx->cache[i] = pctx->cache[i - 1];
        pctx->cache[0] = pix;
    }

    return pix;
}

static int decode_pixel(ArithCoder *acoder, PixContext *pctx,
                        uint8_t *ngb, int num_ngb)
{
    int i, val, pix;

    val = acoder->get_model_sym(acoder, &pctx->cache_model);
    if (val < pctx->num_syms) {
        int idx, j;

        idx = 0;
        for (i = 0; i < pctx->cache_size; i++) {
            for (j = 0; j < num_ngb; j++)
                if (pctx->cache[i] == ngb[j])
                    break;
            if (j == num_ngb) {
                if (idx == val)
                    break;
                idx++;
            }
        }
        val = FFMIN(i, pctx->cache_size - 1);
        pix = pctx->cache[val];
    } else {
        pix = acoder->get_model_sym(acoder, &pctx->full_model);
        for (i = 0; i < pctx->cache_size - 1; i++)
            if (pctx->cache[i] == pix)
                break;
        val = i;
    }
    if (val) {
        for (i = val; i > 0; i--)
            pctx->cache[i] = pctx->cache[i - 1];
        pctx->cache[0] = pix;
    }

    return pix;
}

static int decode_pixel_in_context(ArithCoder *acoder, PixContext *pctx,
                                   uint8_t *src, int stride, int x, int y,
                                   int has_right)
{
    uint8_t neighbours[4];
    uint8_t ref_pix[4];
    int nlen;
    int layer = 0, sub;
    int pix;
    int i, j;

    if (!y) {
        memset(neighbours, src[-1], 4);
    } else {
        neighbours[TOP] = src[-stride];
        if (!x) {
            neighbours[TOP_LEFT] = neighbours[LEFT] = neighbours[TOP];
        } else {
            neighbours[TOP_LEFT] = src[-stride - 1];
            neighbours[    LEFT] = src[-1];
        }
        if (has_right)
            neighbours[TOP_RIGHT] = src[-stride + 1];
        else
            neighbours[TOP_RIGHT] = neighbours[TOP];
    }

    sub = 0;
    if (x >= 2 && src[-2] == neighbours[LEFT])
        sub  = 1;
    if (y >= 2 && src[-2 * stride] == neighbours[TOP])
        sub |= 2;

    nlen = 1;
    ref_pix[0] = neighbours[0];
    for (i = 1; i < 4; i++) {
        for (j = 0; j < nlen; j++)
            if (ref_pix[j] == neighbours[i])
                break;
        if (j == nlen)
            ref_pix[nlen++] = neighbours[i];
    }

    switch (nlen) {
    case 1:
    case 4:
        layer = 0;
        break;
    case 2:
        if (neighbours[TOP] == neighbours[TOP_LEFT]) {
            if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT])
                layer = 3;
            else if (neighbours[LEFT] == neighbours[TOP_LEFT])
                layer = 2;
            else
                layer = 4;
        } else if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT]) {
            if (neighbours[LEFT] == neighbours[TOP_LEFT])
                layer = 1;
            else
                layer = 5;
        } else if (neighbours[LEFT] == neighbours[TOP_LEFT]) {
            layer = 6;
        } else {
            layer = 0;
        }
        break;
    case 3:
        if (neighbours[TOP] == neighbours[TOP_LEFT])
            layer = 0;
        else if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT])
            layer = 1;
        else if (neighbours[LEFT] == neighbours[TOP_LEFT])
            layer = 2;
        else if (neighbours[TOP_RIGHT] == neighbours[TOP])
            layer = 3;
        else if (neighbours[TOP] == neighbours[LEFT])
            layer = 4;
        else
            layer = 5;
        break;
    }

Alberto Delmás's avatar
Alberto Delmás committed
309 310
    pix = acoder->get_model_sym(acoder,
                                &pctx->sec_models[nlen - 1][layer][sub]);
311 312 313 314 315 316
    if (pix < nlen)
        return ref_pix[pix];
    else
        return decode_pixel(acoder, pctx, ref_pix, nlen);
}

Alberto Delmás's avatar
Alberto Delmás committed
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,
Alberto Delmás's avatar
Alberto Delmás committed
319
                         int rgb_stride, PixContext *pctx, const uint32_t *pal)
320
{
Alberto Delmás's avatar
Alberto Delmás committed
321 322
    int i, j, p;
    uint8_t *rgb_dst = rgb_pic + x * 3 + y * rgb_stride;
323 324 325 326 327 328

    dst += x + y * stride;

    for (j = 0; j < height; j++) {
        for (i = 0; i < width; i++) {
            if (!i && !j)
Alberto Delmás's avatar
Alberto Delmás committed
329 330 331 332 333
                p = decode_top_left_pixel(acoder, pctx);
            else
                p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
                                            i, j, width - i - 1);
            dst[i] = p;
334

Alberto Delmás's avatar
Alberto Delmás committed
335 336
            if (rgb_pic)
                AV_WB24(rgb_dst + i * 3, pal[p]);
337
        }
Alberto Delmás's avatar
Alberto Delmás committed
338 339
        dst     += stride;
        rgb_dst += rgb_stride;
340 341 342 343 344
    }

    return 0;
}

Alberto Delmás's avatar
Alberto Delmás committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
static void copy_rectangles(MSS12Context const *c,
                            int x, int y, int width, int height)
{
    int j;

    if (c->last_rgb_pic)
        for (j = y; j < y + height; j++) {
            memcpy(c->rgb_pic      + j * c->rgb_stride + x * 3,
                   c->last_rgb_pic + j * c->rgb_stride + x * 3,
                   width * 3);
            memcpy(c->pal_pic      + j * c->pal_stride + x,
                   c->last_pal_pic + j * c->pal_stride + x,
                   width);
        }
}

static int motion_compensation(MSS12Context const *c,
                               int x, int y, int width, int height)
{
    if (x + c->mvX < 0 || x + c->mvX + width  > c->avctx->width  ||
        y + c->mvY < 0 || y + c->mvY + height > c->avctx->height ||
        !c->rgb_pic)
        return -1;
    else {
        uint8_t *dst     = c->pal_pic + x     + y * c->pal_stride;
        uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
        uint8_t *src;
        uint8_t *rgb_src;
        int j;
        x += c->mvX;
        y += c->mvY;
        if (c->last_rgb_pic) {
            src     = c->last_pal_pic + x +     y * c->pal_stride;
            rgb_src = c->last_rgb_pic + x * 3 + y * c->rgb_stride;
        } else {
            src     = c->pal_pic + x     + y * c->pal_stride;
            rgb_src = c->rgb_pic + x * 3 + y * c->rgb_stride;
        }
        for (j = 0; j < height; j++) {
            memmove(dst, src, width);
            memmove(rgb_dst, rgb_src, width * 3);
            dst     += c->pal_stride;
            src     += c->pal_stride;
            rgb_dst += c->rgb_stride;
            rgb_src += c->rgb_stride;
        }
    }
    return 0;
}

static int decode_region_masked(MSS12Context const *c, ArithCoder *acoder,
396 397 398 399 400
                                uint8_t *dst, int stride, uint8_t *mask,
                                int mask_stride, int x, int y,
                                int width, int height,
                                PixContext *pctx)
{
Alberto Delmás's avatar
Alberto Delmás committed
401 402
    int i, j, p;
    uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
403 404 405 406 407 408

    dst  += x + y * stride;
    mask += x + y * mask_stride;

    for (j = 0; j < height; j++) {
        for (i = 0; i < width; i++) {
Alberto Delmás's avatar
Alberto Delmás committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
            if (c->avctx->err_recognition & AV_EF_EXPLODE &&
                ( c->rgb_pic && mask[i] != 0x01 && mask[i] != 0x02 && mask[i] != 0x04 ||
                 !c->rgb_pic && mask[i] != 0x80 && mask[i] != 0xFF))
                return -1;

            if (mask[i] == 0x02) {
                copy_rectangles(c, x + i, y + j, 1, 1);
            } else if (mask[i] == 0x04) {
                if (motion_compensation(c, x + i, y + j, 1, 1))
                    return -1;
            } else if (mask[i] != 0x80) {
                if (!i && !j)
                    p = decode_top_left_pixel(acoder, pctx);
                else
                    p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
                                                i, j, width - i - 1);
                dst[i] = p;
                if (c->rgb_pic)
                    AV_WB24(rgb_dst + i * 3, c->pal[p]);
            }
429
        }
Alberto Delmás's avatar
Alberto Delmás committed
430 431 432
        dst     += stride;
        mask    += mask_stride;
        rgb_dst += c->rgb_stride;
433 434 435 436 437
    }

    return 0;
}

Alberto Delmás's avatar
Alberto Delmás committed
438
static av_cold void codec_init(MSS12Context *c, int version)
439
{
Alberto Delmás's avatar
Alberto Delmás committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
    int i;
    for (i = 0; i < (c->slice_split ? 2 : 1); i++) {
        c->sc[i].c = c;
        model_init(&c->sc[i].intra_region, 2, THRESH_ADAPTIVE);
        model_init(&c->sc[i].inter_region, 2, THRESH_ADAPTIVE);
        model_init(&c->sc[i].split_mode,   3, THRESH_HIGH);
        model_init(&c->sc[i].edge_mode,    2, THRESH_HIGH);
        model_init(&c->sc[i].pivot,        3, THRESH_LOW);

        pixctx_init(&c->sc[i].intra_pix_ctx, 8, c->full_model_syms, 0);

        pixctx_init(&c->sc[i].inter_pix_ctx, version ? 3 : 2,
                    c->full_model_syms, version ? 1 : 0);
    }
    c->corrupted = 1;
455 456
}

Alberto Delmás's avatar
Alberto Delmás committed
457
void ff_mss12_codec_reset(MSS12Context *c)
458
{
Alberto Delmás's avatar
Alberto Delmás committed
459 460 461 462 463 464 465 466 467 468 469 470
    int i;
    for (i = 0; i < (c->slice_split ? 2 : 1); i++) {
        model_reset(&c->sc[i].intra_region);
        model_reset(&c->sc[i].inter_region);
        model_reset(&c->sc[i].split_mode);
        model_reset(&c->sc[i].edge_mode);
        model_reset(&c->sc[i].pivot);
        pixctx_reset(&c->sc[i].intra_pix_ctx);
        pixctx_reset(&c->sc[i].inter_pix_ctx);
    }

    c->corrupted = 0;
471 472
}

Alberto Delmás's avatar
Alberto Delmás committed
473
static int decode_pivot(SliceContext *sc, ArithCoder *acoder, int base)
474 475 476
{
    int val, inv;

Alberto Delmás's avatar
Alberto Delmás committed
477 478
    inv = acoder->get_model_sym(acoder, &sc->edge_mode);
    val = acoder->get_model_sym(acoder, &sc->pivot) + 1;
479 480

    if (val > 2) {
Alberto Delmás's avatar
Alberto Delmás committed
481 482 483
        if ((base + 1) / 2 - 2 <= 0)
            return -1;

484 485 486
        val = acoder->get_number(acoder, (base + 1) / 2 - 2) + 3;
    }

Alberto Delmás's avatar
Alberto Delmás committed
487 488
    if (val >= base)
        return -1;
489 490 491 492

    return inv ? base - val : val;
}

Alberto Delmás's avatar
Alberto Delmás committed
493
static int decode_region_intra(SliceContext *sc, ArithCoder *acoder,
494 495
                               int x, int y, int width, int height)
{
Alberto Delmás's avatar
Alberto Delmás committed
496
    MSS12Context const *c = sc->c;
497 498
    int mode;

Alberto Delmás's avatar
Alberto Delmás committed
499
    mode = acoder->get_model_sym(acoder, &sc->intra_region);
500 501

    if (!mode) {
Alberto Delmás's avatar
Alberto Delmás committed
502 503 504 505 506 507 508 509 510
        int i, j, pix, rgb_pix;
        int stride       = c->pal_stride;
        int rgb_stride   = c->rgb_stride;
        uint8_t *dst     = c->pal_pic + x     + y * stride;
        uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * rgb_stride;

        pix     = decode_top_left_pixel(acoder, &sc->intra_pix_ctx);
        rgb_pix = c->pal[pix];
        for (i = 0; i < height; i++, dst += stride, rgb_dst += rgb_stride) {
511
            memset(dst, pix, width);
Alberto Delmás's avatar
Alberto Delmás committed
512 513 514 515
            if (c->rgb_pic)
                for (j = 0; j < width * 3; j += 3)
                    AV_WB24(rgb_dst + j, rgb_pix);
        }
516
    } else {
Alberto Delmás's avatar
Alberto Delmás committed
517 518 519
        return decode_region(acoder, c->pal_pic, c->rgb_pic,
                             x, y, width, height, c->pal_stride, c->rgb_stride,
                             &sc->intra_pix_ctx, &c->pal[0]);
520 521 522 523 524
    }

    return 0;
}

Alberto Delmás's avatar
Alberto Delmás committed
525
static int decode_region_inter(SliceContext *sc, ArithCoder *acoder,
526 527
                               int x, int y, int width, int height)
{
Alberto Delmás's avatar
Alberto Delmás committed
528
    MSS12Context const *c = sc->c;
529 530
    int mode;

Alberto Delmás's avatar
Alberto Delmás committed
531
    mode = acoder->get_model_sym(acoder, &sc->inter_region);
532 533

    if (!mode) {
Alberto Delmás's avatar
Alberto Delmás committed
534 535 536 537 538 539 540 541 542 543 544 545 546
        mode = decode_top_left_pixel(acoder, &sc->inter_pix_ctx);

        if (c->avctx->err_recognition & AV_EF_EXPLODE &&
            ( c->rgb_pic && mode != 0x01 && mode != 0x02 && mode != 0x04 ||
             !c->rgb_pic && mode != 0x80 && mode != 0xFF))
            return -1;

        if (mode == 0x02)
            copy_rectangles(c, x, y, width, height);
        else if (mode == 0x04)
            return motion_compensation(c, x, y, width, height);
        else if (mode != 0x80)
            return decode_region_intra(sc, acoder, x, y, width, height);
547
    } else {
Alberto Delmás's avatar
Alberto Delmás committed
548 549 550
        if (decode_region(acoder, c->mask, NULL,
                          x, y, width, height, c->mask_stride, 0,
                          &sc->inter_pix_ctx, &c->pal[0]) < 0)
551
            return -1;
Alberto Delmás's avatar
Alberto Delmás committed
552 553 554
        return decode_region_masked(c, acoder, c->pal_pic,
                                    c->pal_stride, c->mask,
                                    c->mask_stride,
555
                                    x, y, width, height,
Alberto Delmás's avatar
Alberto Delmás committed
556
                                    &sc->intra_pix_ctx);
557 558 559 560 561
    }

    return 0;
}

Alberto Delmás's avatar
Alberto Delmás committed
562
int ff_mss12_decode_rect(SliceContext *sc, ArithCoder *acoder,
563 564 565 566
                         int x, int y, int width, int height)
{
    int mode, pivot;

Alberto Delmás's avatar
Alberto Delmás committed
567
    mode = acoder->get_model_sym(acoder, &sc->split_mode);
568 569 570

    switch (mode) {
    case SPLIT_VERT:
Alberto Delmás's avatar
Alberto Delmás committed
571
        if ((pivot = decode_pivot(sc, acoder, height)) < 1)
572
            return -1;
Alberto Delmás's avatar
Alberto Delmás committed
573 574 575
        if (ff_mss12_decode_rect(sc, acoder, x, y, width, pivot))
            return -1;
        if (ff_mss12_decode_rect(sc, acoder, x, y + pivot, width, height - pivot))
576 577 578
            return -1;
        break;
    case SPLIT_HOR:
Alberto Delmás's avatar
Alberto Delmás committed
579 580 581
        if ((pivot = decode_pivot(sc, acoder, width)) < 1)
            return -1;
        if (ff_mss12_decode_rect(sc, acoder, x, y, pivot, height))
582
            return -1;
Alberto Delmás's avatar
Alberto Delmás committed
583
        if (ff_mss12_decode_rect(sc, acoder, x + pivot, y, width - pivot, height))
584 585 586
            return -1;
        break;
    case SPLIT_NONE:
Alberto Delmás's avatar
Alberto Delmás committed
587 588
        if (sc->c->keyframe)
            return decode_region_intra(sc, acoder, x, y, width, height);
589
        else
Alberto Delmás's avatar
Alberto Delmás committed
590
            return decode_region_inter(sc, acoder, x, y, width, height);
591 592 593 594 595 596 597
    default:
        return -1;
    }

    return 0;
}

Alberto Delmás's avatar
Alberto Delmás committed
598
av_cold int ff_mss12_decode_init(MSS12Context *c, int version)
599
{
Alberto Delmás's avatar
Alberto Delmás committed
600
    AVCodecContext *avctx = c->avctx;
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
    int i;

    if (avctx->extradata_size < 52 + 256 * 3) {
        av_log(avctx, AV_LOG_ERROR, "Insufficient extradata size %d\n",
               avctx->extradata_size);
        return AVERROR_INVALIDDATA;
    }

    if (AV_RB32(avctx->extradata) < avctx->extradata_size) {
        av_log(avctx, AV_LOG_ERROR,
               "Insufficient extradata size: expected %d got %d\n",
               AV_RB32(avctx->extradata),
               avctx->extradata_size);
        return AVERROR_INVALIDDATA;
    }

Alberto Delmás's avatar
Alberto Delmás committed
617 618 619 620 621 622 623 624
    avctx->coded_width  = AV_RB32(avctx->extradata + 20);
    avctx->coded_height = AV_RB32(avctx->extradata + 24);
    if (avctx->coded_width > 4096 || avctx->coded_height > 4096) {
        av_log(avctx, AV_LOG_ERROR, "Frame dimensions %dx%d too large",
               avctx->coded_width, avctx->coded_height);
        return AVERROR_INVALIDDATA;
    }

625 626
    av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d\n",
           AV_RB32(avctx->extradata + 4), AV_RB32(avctx->extradata + 8));
Alberto Delmás's avatar
Alberto Delmás committed
627 628 629 630 631 632 633
    if (version != AV_RB32(avctx->extradata + 4) > 1) {
        av_log(avctx, AV_LOG_ERROR,
               "Header version doesn't match codec tag\n");
        return -1;
    }

    c->free_colours = AV_RB32(avctx->extradata + 48);
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
    if ((unsigned)c->free_colours > 256) {
        av_log(avctx, AV_LOG_ERROR,
               "Incorrect number of changeable palette entries: %d\n",
               c->free_colours);
        return AVERROR_INVALIDDATA;
    }
    av_log(avctx, AV_LOG_DEBUG, "%d free colour(s)\n", c->free_colours);

    av_log(avctx, AV_LOG_DEBUG, "Display dimensions %dx%d\n",
           AV_RB32(avctx->extradata + 12), AV_RB32(avctx->extradata + 16));
    av_log(avctx, AV_LOG_DEBUG, "Coded dimensions %dx%d\n",
           avctx->coded_width, avctx->coded_height);
    av_log(avctx, AV_LOG_DEBUG, "%g frames per second\n",
           av_int2float(AV_RB32(avctx->extradata + 28)));
    av_log(avctx, AV_LOG_DEBUG, "Bitrate %d bps\n",
           AV_RB32(avctx->extradata + 32));
    av_log(avctx, AV_LOG_DEBUG, "Max. lead time %g ms\n",
           av_int2float(AV_RB32(avctx->extradata + 36)));
    av_log(avctx, AV_LOG_DEBUG, "Max. lag time %g ms\n",
           av_int2float(AV_RB32(avctx->extradata + 40)));
    av_log(avctx, AV_LOG_DEBUG, "Max. seek time %g ms\n",
           av_int2float(AV_RB32(avctx->extradata + 44)));

Alberto Delmás's avatar
Alberto Delmás committed
657 658 659 660 661 662 663 664 665 666
    if (version) {
        if (avctx->extradata_size < 60 + 256 * 3) {
            av_log(avctx, AV_LOG_ERROR,
                   "Insufficient extradata size %d for v2\n",
                   avctx->extradata_size);
            return AVERROR_INVALIDDATA;
        }

        c->slice_split = AV_RB32(avctx->extradata + 52);
        av_log(avctx, AV_LOG_DEBUG, "Slice split %d\n", c->slice_split);
667

Alberto Delmás's avatar
Alberto Delmás committed
668 669 670 671 672 673 674 675 676 677 678 679 680
        c->full_model_syms = AV_RB32(avctx->extradata + 56);
        if (c->full_model_syms < 2 || c->full_model_syms > 256) {
            av_log(avctx, AV_LOG_ERROR,
                   "Incorrect number of used colours %d\n",
                   c->full_model_syms);
            return AVERROR_INVALIDDATA;
        }
        av_log(avctx, AV_LOG_DEBUG, "Used colours %d\n",
               c->full_model_syms);
    } else {
        c->slice_split     = 0;
        c->full_model_syms = 256;
    }
681

Alberto Delmás's avatar
Alberto Delmás committed
682 683 684 685 686 687
    for (i = 0; i < 256; i++)
        c->pal[i] = AV_RB24(avctx->extradata + 52 +
                            (version ? 8 : 0) + i * 3);

    c->mask_stride = FFALIGN(avctx->width, 16);
    c->mask        = av_malloc(c->mask_stride * avctx->height);
688 689 690 691 692
    if (!c->mask) {
        av_log(avctx, AV_LOG_ERROR, "Cannot allocate mask plane\n");
        return AVERROR(ENOMEM);
    }

Alberto Delmás's avatar
Alberto Delmás committed
693
    codec_init(c, version);
694 695 696 697

    return 0;
}

Alberto Delmás's avatar
Alberto Delmás committed
698
av_cold int ff_mss12_decode_end(MSS12Context *c)
699 700 701 702 703
{
    av_freep(&c->mask);

    return 0;
}