GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/mss12.c Lines: 354 399 88.7 %
Date: 2019-11-18 18:00:01 Branches: 210 281 74.7 %

Line Branch Exec Source
1
/*
2
 * Copyright (c) 2012 Konstantin Shishkov
3
 *
4
 * This file is part of FFmpeg.
5
 *
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.
10
 *
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.
15
 *
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
19
 */
20
21
/**
22
 * @file
23
 * Common functions for Microsoft Screen 1 and 2
24
 */
25
26
#include <inttypes.h>
27
28
#include "libavutil/intfloat.h"
29
#include "libavutil/intreadwrite.h"
30
#include "avcodec.h"
31
#include "mss12.h"
32
33
enum SplitMode {
34
    SPLIT_VERT = 0,
35
    SPLIT_HOR,
36
    SPLIT_NONE
37
};
38
39
static const int sec_order_sizes[4] = { 1, 7, 6, 1 };
40
41
enum ContextDirection {
42
    TOP_LEFT = 0,
43
    TOP,
44
    TOP_RIGHT,
45
    LEFT
46
};
47
48
3008834
static int model_calc_threshold(Model *m)
49
{
50
    int thr;
51
52
3008834
    thr = 2 * m->weights[m->num_syms] - 1;
53
3008834
    thr = ((thr >> 1) + 4 * m->cum_prob[0]) / thr;
54
55
3008834
    return FFMIN(thr, 0x3FFF);
56
}
57
58
903
static void model_reset(Model *m)
59
{
60
    int i;
61
62
8475
    for (i = 0; i <= m->num_syms; i++) {
63
7572
        m->weights[i]  = 1;
64
7572
        m->cum_prob[i] = m->num_syms - i;
65
    }
66
903
    m->weights[0] = 0;
67
7572
    for (i = 0; i < m->num_syms; i++)
68
6669
        m->idx2sym[i + 1] = i;
69
903
}
70
71
2064
static av_cold void model_init(Model *m, int num_syms, int thr_weight)
72
{
73
2064
    m->num_syms   = num_syms;
74
2064
    m->thr_weight = thr_weight;
75
2064
    m->threshold  = num_syms * thr_weight;
76
2064
}
77
78
3280337
static void model_rescale_weights(Model *m)
79
{
80
    int i;
81
    int cum_prob;
82
83
3280337
    if (m->thr_weight == THRESH_ADAPTIVE)
84
3008834
        m->threshold = model_calc_threshold(m);
85
3310190
    while (m->cum_prob[0] > m->threshold) {
86
29853
        cum_prob = 0;
87
133028
        for (i = m->num_syms; i >= 0; i--) {
88
103175
            m->cum_prob[i] = cum_prob;
89
103175
            m->weights[i]  = (m->weights[i] + 1) >> 1;
90
103175
            cum_prob      += m->weights[i];
91
        }
92
    }
93
3280337
}
94
95
3280337
void ff_mss12_model_update(Model *m, int val)
96
{
97
    int i;
98
99
3280337
    if (m->weights[val] == m->weights[val - 1]) {
100
42813
        for (i = val; m->weights[i - 1] == m->weights[val]; i--);
101
6459
        if (i != val) {
102
            int sym1, sym2;
103
104
6459
            sym1 = m->idx2sym[val];
105
6459
            sym2 = m->idx2sym[i];
106
107
6459
            m->idx2sym[val]  = sym2;
108
6459
            m->idx2sym[i]    = sym1;
109
110
6459
            val = i;
111
        }
112
    }
113
3280337
    m->weights[val]++;
114
6691619
    for (i = val - 1; i >= 0; i--)
115
3411282
        m->cum_prob[i]++;
116
3280337
    model_rescale_weights(m);
117
3280337
}
118
119
14
static void pixctx_reset(PixContext *ctx)
120
{
121
    int i, j;
122
123
14
    if (!ctx->special_initial_cache)
124
105
        for (i = 0; i < ctx->cache_size; i++)
125
96
            ctx->cache[i] = i;
126
    else {
127
5
        ctx->cache[0] = 1;
128
5
        ctx->cache[1] = 2;
129
5
        ctx->cache[2] = 4;
130
    }
131
132
14
    model_reset(&ctx->cache_model);
133
14
    model_reset(&ctx->full_model);
134
135
224
    for (i = 0; i < 15; i++)
136
1050
        for (j = 0; j < 4; j++)
137
840
            model_reset(&ctx->sec_models[i][j]);
138
14
}
139
140
32
static av_cold void pixctx_init(PixContext *ctx, int cache_size,
141
                                int full_model_syms, int special_initial_cache)
142
{
143
    int i, j, k, idx;
144
145
32
    ctx->cache_size            = cache_size + 4;
146
32
    ctx->num_syms              = cache_size;
147
32
    ctx->special_initial_cache = special_initial_cache;
148
149
32
    model_init(&ctx->cache_model, ctx->num_syms + 1, THRESH_LOW);
150
32
    model_init(&ctx->full_model, full_model_syms, THRESH_HIGH);
151
152
160
    for (i = 0, idx = 0; i < 4; i++)
153
608
        for (j = 0; j < sec_order_sizes[i]; j++, idx++)
154
2400
            for (k = 0; k < 4; k++)
155
1920
                model_init(&ctx->sec_models[idx][k], 2 + i,
156
                           i ? THRESH_LOW : THRESH_ADAPTIVE);
157
32
}
158
159
27806
static av_always_inline int decode_pixel(ArithCoder *acoder, PixContext *pctx,
160
                                         uint8_t *ngb, int num_ngb, int any_ngb)
161
{
162
    int i, val, pix;
163
164
27806
    if (acoder->overread > MAX_OVERREAD)
165
        return AVERROR_INVALIDDATA;
166
27806
    val = acoder->get_model_sym(acoder, &pctx->cache_model);
167
27806
    if (val < pctx->num_syms) {
168
26293
        if (any_ngb) {
169
            int idx, j;
170
171
23657
            idx = 0;
172
44146
            for (i = 0; i < pctx->cache_size; i++) {
173
89503
                for (j = 0; j < num_ngb; j++)
174
55784
                    if (pctx->cache[i] == ngb[j])
175
10427
                        break;
176
44146
                if (j == num_ngb) {
177
33719
                    if (idx == val)
178
23657
                        break;
179
10062
                    idx++;
180
                }
181
            }
182
23657
            val = FFMIN(i, pctx->cache_size - 1);
183
        }
184
26293
        pix = pctx->cache[val];
185
    } else {
186
1513
        pix = acoder->get_model_sym(acoder, &pctx->full_model);
187
17969
        for (i = 0; i < pctx->cache_size - 1; i++)
188
16550
            if (pctx->cache[i] == pix)
189
94
                break;
190
1513
        val = i;
191
    }
192
27806
    if (val) {
193
53478
        for (i = val; i > 0; i--)
194
39133
            pctx->cache[i] = pctx->cache[i - 1];
195
14345
        pctx->cache[0] = pix;
196
    }
197
198
27806
    return pix;
199
}
200
201
3241359
static int decode_pixel_in_context(ArithCoder *acoder, PixContext *pctx,
202
                                   uint8_t *src, ptrdiff_t stride, int x, int y,
203
                                   int has_right)
204
{
205
    uint8_t neighbours[4];
206
    uint8_t ref_pix[4];
207
    int nlen;
208
3241359
    int layer = 0, sub;
209
    int pix;
210
    int i, j;
211
212
3241359
    if (!y) {
213
49058
        memset(neighbours, src[-1], 4);
214
    } else {
215
3192301
        neighbours[TOP] = src[-stride];
216
3192301
        if (!x) {
217
27423
            neighbours[TOP_LEFT] = neighbours[LEFT] = neighbours[TOP];
218
        } else {
219
3164878
            neighbours[TOP_LEFT] = src[-stride - 1];
220
3164878
            neighbours[    LEFT] = src[-1];
221
        }
222
3192301
        if (has_right)
223
3164931
            neighbours[TOP_RIGHT] = src[-stride + 1];
224
        else
225
27370
            neighbours[TOP_RIGHT] = neighbours[TOP];
226
    }
227
228
3241359
    sub = 0;
229

3241359
    if (x >= 2 && src[-2] == neighbours[LEFT])
230
3072590
        sub  = 1;
231

3241359
    if (y >= 2 && src[-2 * stride] == neighbours[TOP])
232
3029273
        sub |= 2;
233
234
3241359
    nlen = 1;
235
3241359
    ref_pix[0] = neighbours[0];
236
12965436
    for (i = 1; i < 4; i++) {
237
10115065
        for (j = 0; j < nlen; j++)
238
9867896
            if (ref_pix[j] == neighbours[i])
239
9476908
                break;
240
9724077
        if (j == nlen)
241
247169
            ref_pix[nlen++] = neighbours[i];
242
    }
243
244

3241359
    switch (nlen) {
245
3006147
    case 1:
246
3006147
        layer = 0;
247
3006147
        break;
248
224320
    case 2:
249
224320
        if (neighbours[TOP] == neighbours[TOP_LEFT]) {
250
118747
            if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT])
251
55629
                layer = 1;
252
63118
            else if (neighbours[LEFT] == neighbours[TOP_LEFT])
253
48959
                layer = 2;
254
            else
255
14159
                layer = 3;
256
105573
        } else if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT]) {
257
43063
            if (neighbours[LEFT] == neighbours[TOP_LEFT])
258
15326
                layer = 4;
259
            else
260
27737
                layer = 5;
261
62510
        } else if (neighbours[LEFT] == neighbours[TOP_LEFT]) {
262
40477
            layer = 6;
263
        } else {
264
22033
            layer = 7;
265
        }
266
224320
        break;
267
9827
    case 3:
268
9827
        if (neighbours[TOP] == neighbours[TOP_LEFT])
269
680
            layer = 8;
270
9147
        else if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT])
271
3169
            layer = 9;
272
5978
        else if (neighbours[LEFT] == neighbours[TOP_LEFT])
273
1922
            layer = 10;
274
4056
        else if (neighbours[TOP_RIGHT] == neighbours[TOP])
275
1262
            layer = 11;
276
2794
        else if (neighbours[TOP] == neighbours[LEFT])
277
2492
            layer = 12;
278
        else
279
302
            layer = 13;
280
9827
        break;
281
1065
    case 4:
282
1065
        layer = 14;
283
1065
        break;
284
    }
285
286
3241359
    pix = acoder->get_model_sym(acoder,
287
                                &pctx->sec_models[layer][sub]);
288
3241359
    if (pix < nlen)
289
3216241
        return ref_pix[pix];
290
    else
291
25118
        return decode_pixel(acoder, pctx, ref_pix, nlen, 1);
292
}
293
294
601
static int decode_region(ArithCoder *acoder, uint8_t *dst, uint8_t *rgb_pic,
295
                         int x, int y, int width, int height, ptrdiff_t stride,
296
                         ptrdiff_t rgb_stride, PixContext *pctx,
297
                         const uint32_t *pal)
298
{
299
    int i, j, p;
300
601
    uint8_t *rgb_dst = rgb_pic + x * 3 + y * rgb_stride;
301
302
601
    dst += x + y * stride;
303
304
28519
    for (j = 0; j < height; j++) {
305
3269449
        for (i = 0; i < width; i++) {
306

3241531
            if (!i && !j)
307
601
                p = decode_pixel(acoder, pctx, NULL, 0, 0);
308
            else
309
3240930
                p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
310
3240930
                                            i, j, width - i - 1);
311
3241531
            if (p < 0)
312
                return p;
313
3241531
            dst[i] = p;
314
315
3241531
            if (rgb_pic)
316
3053651
                AV_WB24(rgb_dst + i * 3, pal[p]);
317
        }
318
27918
        dst     += stride;
319
27918
        rgb_dst += rgb_stride;
320
    }
321
322
601
    return 0;
323
}
324
325
2806
static void copy_rectangles(MSS12Context const *c,
326
                            int x, int y, int width, int height)
327
{
328
    int j;
329
330
2806
    if (c->last_rgb_pic)
331
2690
        for (j = y; j < y + height; j++) {
332
1345
            memcpy(c->rgb_pic      + j * c->rgb_stride + x * 3,
333
1345
                   c->last_rgb_pic + j * c->rgb_stride + x * 3,
334
1345
                   width * 3);
335
1345
            memcpy(c->pal_pic      + j * c->pal_stride + x,
336
1345
                   c->last_pal_pic + j * c->pal_stride + x,
337
                   width);
338
        }
339
2806
}
340
341
132
static int motion_compensation(MSS12Context const *c,
342
                               int x, int y, int width, int height)
343
{
344

132
    if (x + c->mvX < 0 || x + c->mvX + width  > c->avctx->width  ||
345

132
        y + c->mvY < 0 || y + c->mvY + height > c->avctx->height ||
346
132
        !c->rgb_pic)
347
        return -1;
348
    else {
349
132
        uint8_t *dst     = c->pal_pic + x     + y * c->pal_stride;
350
132
        uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
351
        uint8_t *src;
352
        uint8_t *rgb_src;
353
        int j;
354
132
        x += c->mvX;
355
132
        y += c->mvY;
356
132
        if (c->last_rgb_pic) {
357
75
            src     = c->last_pal_pic + x +     y * c->pal_stride;
358
75
            rgb_src = c->last_rgb_pic + x * 3 + y * c->rgb_stride;
359
        } else {
360
57
            src     = c->pal_pic + x     + y * c->pal_stride;
361
57
            rgb_src = c->rgb_pic + x * 3 + y * c->rgb_stride;
362
        }
363
264
        for (j = 0; j < height; j++) {
364
132
            memmove(dst, src, width);
365
132
            memmove(rgb_dst, rgb_src, width * 3);
366
132
            dst     += c->pal_stride;
367
132
            src     += c->pal_stride;
368
132
            rgb_dst += c->rgb_stride;
369
132
            rgb_src += c->rgb_stride;
370
        }
371
    }
372
132
    return 0;
373
}
374
375
5
static int decode_region_masked(MSS12Context const *c, ArithCoder *acoder,
376
                                uint8_t *dst, ptrdiff_t stride, uint8_t *mask,
377
                                ptrdiff_t mask_stride, int x, int y,
378
                                int width, int height,
379
                                PixContext *pctx)
380
{
381
    int i, j, p;
382
5
    uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
383
384
5
    dst  += x + y * stride;
385
5
    mask += x + y * mask_stride;
386
387
619
    for (j = 0; j < height; j++) {
388
3948
        for (i = 0; i < width; i++) {
389
3334
            if (c->avctx->err_recognition & AV_EF_EXPLODE &&
390
                ( c->rgb_pic && mask[i] != 0x01 && mask[i] != 0x02 && mask[i] != 0x04 ||
391
                 !c->rgb_pic && mask[i] != 0x80 && mask[i] != 0xFF))
392
                return -1;
393
394
3334
            if (mask[i] == 0x02) {
395
2772
                copy_rectangles(c, x + i, y + j, 1, 1);
396
562
            } else if (mask[i] == 0x04) {
397
132
                if (motion_compensation(c, x + i, y + j, 1, 1))
398
                    return -1;
399
430
            } else if (mask[i] != 0x80) {
400

430
                if (!i && !j)
401
1
                    p = decode_pixel(acoder, pctx, NULL, 0, 0);
402
                else
403
429
                    p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
404
429
                                                i, j, width - i - 1);
405
430
                if (p < 0)
406
                    return p;
407
430
                dst[i] = p;
408
430
                if (c->rgb_pic)
409
430
                    AV_WB24(rgb_dst + i * 3, c->pal[p]);
410
            }
411
        }
412
614
        dst     += stride;
413
614
        mask    += mask_stride;
414
614
        rgb_dst += c->rgb_stride;
415
    }
416
417
5
    return 0;
418
}
419
420
16
static av_cold void slicecontext_init(SliceContext *sc,
421
                                      int version, int full_model_syms)
422
{
423
16
    model_init(&sc->intra_region, 2, THRESH_ADAPTIVE);
424
16
    model_init(&sc->inter_region, 2, THRESH_ADAPTIVE);
425
16
    model_init(&sc->split_mode,   3, THRESH_HIGH);
426
16
    model_init(&sc->edge_mode,    2, THRESH_HIGH);
427
16
    model_init(&sc->pivot,        3, THRESH_LOW);
428
429
16
    pixctx_init(&sc->intra_pix_ctx, 8, full_model_syms, 0);
430
431
16
    pixctx_init(&sc->inter_pix_ctx, version ? 3 : 2,
432
                full_model_syms, version ? 1 : 0);
433
16
}
434
435
7
void ff_mss12_slicecontext_reset(SliceContext *sc)
436
{
437
7
    model_reset(&sc->intra_region);
438
7
    model_reset(&sc->inter_region);
439
7
    model_reset(&sc->split_mode);
440
7
    model_reset(&sc->edge_mode);
441
7
    model_reset(&sc->pivot);
442
7
    pixctx_reset(&sc->intra_pix_ctx);
443
7
    pixctx_reset(&sc->inter_pix_ctx);
444
7
}
445
446
1713
static int decode_pivot(SliceContext *sc, ArithCoder *acoder, int base)
447
{
448
    int val, inv;
449
450
1713
    inv = acoder->get_model_sym(acoder, &sc->edge_mode);
451
1713
    val = acoder->get_model_sym(acoder, &sc->pivot) + 1;
452
453
1713
    if (val > 2) {
454
1043
        if ((base + 1) / 2 - 2 <= 0)
455
            return -1;
456
457
1043
        val = acoder->get_number(acoder, (base + 1) / 2 - 2) + 3;
458
    }
459
460
1713
    if ((unsigned)val >= base)
461
        return -1;
462
463
1713
    return inv ? base - val : val;
464
}
465
466
1645
static int decode_region_intra(SliceContext *sc, ArithCoder *acoder,
467
                               int x, int y, int width, int height)
468
{
469
1645
    MSS12Context const *c = sc->c;
470
    int mode;
471
472
1645
    mode = acoder->get_model_sym(acoder, &sc->intra_region);
473
474
1645
    if (!mode) {
475
        int i, j, pix, rgb_pix;
476
1049
        ptrdiff_t stride     = c->pal_stride;
477
1049
        ptrdiff_t rgb_stride = c->rgb_stride;
478
1049
        uint8_t *dst     = c->pal_pic + x     + y * stride;
479
1049
        uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * rgb_stride;
480
481
1049
        pix     = decode_pixel(acoder, &sc->intra_pix_ctx, NULL, 0, 0);
482
1049
        if (pix < 0)
483
            return pix;
484
1049
        rgb_pix = c->pal[pix];
485
53944
        for (i = 0; i < height; i++, dst += stride, rgb_dst += rgb_stride) {
486
52895
            memset(dst, pix, width);
487
52895
            if (c->rgb_pic)
488
3606251
                for (j = 0; j < width * 3; j += 3)
489
3564623
                    AV_WB24(rgb_dst + j, rgb_pix);
490
        }
491
    } else {
492
596
        return decode_region(acoder, c->pal_pic, c->rgb_pic,
493
                             x, y, width, height, c->pal_stride, c->rgb_stride,
494
                             &sc->intra_pix_ctx, &c->pal[0]);
495
    }
496
497
1049
    return 0;
498
}
499
500
1042
static int decode_region_inter(SliceContext *sc, ArithCoder *acoder,
501
                               int x, int y, int width, int height)
502
{
503
1042
    MSS12Context const *c = sc->c;
504
    int mode;
505
506
1042
    mode = acoder->get_model_sym(acoder, &sc->inter_region);
507
508
1042
    if (!mode) {
509
1037
        mode = decode_pixel(acoder, &sc->inter_pix_ctx, NULL, 0, 0);
510
1037
        if (mode < 0)
511
            return mode;
512
513
1037
        if (c->avctx->err_recognition & AV_EF_EXPLODE &&
514
            ( c->rgb_pic && mode != 0x01 && mode != 0x02 && mode != 0x04 ||
515
             !c->rgb_pic && mode != 0x80 && mode != 0xFF))
516
            return -1;
517
518
1037
        if (mode == 0x02)
519
34
            copy_rectangles(c, x, y, width, height);
520
1003
        else if (mode == 0x04)
521
            return motion_compensation(c, x, y, width, height);
522
1003
        else if (mode != 0x80)
523
854
            return decode_region_intra(sc, acoder, x, y, width, height);
524
    } else {
525
5
        if (decode_region(acoder, c->mask, NULL,
526
                          x, y, width, height, c->mask_stride, 0,
527
                          &sc->inter_pix_ctx, &c->pal[0]) < 0)
528
            return -1;
529
5
        return decode_region_masked(c, acoder, c->pal_pic,
530
                                    c->pal_stride, c->mask,
531
                                    c->mask_stride,
532
                                    x, y, width, height,
533
                                    &sc->intra_pix_ctx);
534
    }
535
536
183
    return 0;
537
}
538
539
3546
int ff_mss12_decode_rect(SliceContext *sc, ArithCoder *acoder,
540
                         int x, int y, int width, int height)
541
{
542
    int mode, pivot;
543
3546
    if (acoder->overread > MAX_OVERREAD)
544
        return AVERROR_INVALIDDATA;
545
546
3546
    mode = acoder->get_model_sym(acoder, &sc->split_mode);
547
548

3546
    switch (mode) {
549
847
    case SPLIT_VERT:
550
847
        if ((pivot = decode_pivot(sc, acoder, height)) < 1)
551
            return -1;
552
847
        if (ff_mss12_decode_rect(sc, acoder, x, y, width, pivot))
553
            return -1;
554
847
        if (ff_mss12_decode_rect(sc, acoder, x, y + pivot, width, height - pivot))
555
            return -1;
556
847
        break;
557
866
    case SPLIT_HOR:
558
866
        if ((pivot = decode_pivot(sc, acoder, width)) < 1)
559
            return -1;
560
866
        if (ff_mss12_decode_rect(sc, acoder, x, y, pivot, height))
561
            return -1;
562
866
        if (ff_mss12_decode_rect(sc, acoder, x + pivot, y, width - pivot, height))
563
            return -1;
564
866
        break;
565
1833
    case SPLIT_NONE:
566
1833
        if (sc->c->keyframe)
567
791
            return decode_region_intra(sc, acoder, x, y, width, height);
568
        else
569
1042
            return decode_region_inter(sc, acoder, x, y, width, height);
570
    default:
571
        return -1;
572
    }
573
574
1713
    return 0;
575
}
576
577
12
av_cold int ff_mss12_decode_init(MSS12Context *c, int version,
578
                                 SliceContext* sc1, SliceContext *sc2)
579
{
580
12
    AVCodecContext *avctx = c->avctx;
581
    int i;
582
583
12
    if (avctx->extradata_size < 52 + 256 * 3) {
584
        av_log(avctx, AV_LOG_ERROR, "Insufficient extradata size %d\n",
585
               avctx->extradata_size);
586
        return AVERROR_INVALIDDATA;
587
    }
588
589
12
    if (AV_RB32(avctx->extradata) < avctx->extradata_size) {
590
        av_log(avctx, AV_LOG_ERROR,
591
               "Insufficient extradata size: expected %"PRIu32" got %d\n",
592
               AV_RB32(avctx->extradata),
593
               avctx->extradata_size);
594
        return AVERROR_INVALIDDATA;
595
    }
596
597
12
    avctx->coded_width  = FFMAX(AV_RB32(avctx->extradata + 20), avctx->width);
598
12
    avctx->coded_height = FFMAX(AV_RB32(avctx->extradata + 24), avctx->height);
599

12
    if (avctx->coded_width > 4096 || avctx->coded_height > 4096) {
600
        av_log(avctx, AV_LOG_ERROR, "Frame dimensions %dx%d too large",
601
               avctx->coded_width, avctx->coded_height);
602
        return AVERROR_INVALIDDATA;
603
    }
604

12
    if (avctx->coded_width < 1 || avctx->coded_height < 1) {
605
        av_log(avctx, AV_LOG_ERROR, "Frame dimensions %dx%d too small",
606
               avctx->coded_width, avctx->coded_height);
607
        return AVERROR_INVALIDDATA;
608
    }
609
610
12
    av_log(avctx, AV_LOG_DEBUG, "Encoder version %"PRIu32".%"PRIu32"\n",
611
12
           AV_RB32(avctx->extradata + 4), AV_RB32(avctx->extradata + 8));
612
12
    if (version != AV_RB32(avctx->extradata + 4) > 1) {
613
        av_log(avctx, AV_LOG_ERROR,
614
               "Header version doesn't match codec tag\n");
615
        return -1;
616
    }
617
618
12
    c->free_colours = AV_RB32(avctx->extradata + 48);
619
12
    if ((unsigned)c->free_colours > 256) {
620
        av_log(avctx, AV_LOG_ERROR,
621
               "Incorrect number of changeable palette entries: %d\n",
622
               c->free_colours);
623
        return AVERROR_INVALIDDATA;
624
    }
625
12
    av_log(avctx, AV_LOG_DEBUG, "%d free colour(s)\n", c->free_colours);
626
627
12
    av_log(avctx, AV_LOG_DEBUG, "Display dimensions %"PRIu32"x%"PRIu32"\n",
628
12
           AV_RB32(avctx->extradata + 12), AV_RB32(avctx->extradata + 16));
629
12
    av_log(avctx, AV_LOG_DEBUG, "Coded dimensions %dx%d\n",
630
           avctx->coded_width, avctx->coded_height);
631
12
    av_log(avctx, AV_LOG_DEBUG, "%g frames per second\n",
632
12
           av_int2float(AV_RB32(avctx->extradata + 28)));
633
12
    av_log(avctx, AV_LOG_DEBUG, "Bitrate %"PRIu32" bps\n",
634
12
           AV_RB32(avctx->extradata + 32));
635
12
    av_log(avctx, AV_LOG_DEBUG, "Max. lead time %g ms\n",
636
12
           av_int2float(AV_RB32(avctx->extradata + 36)));
637
12
    av_log(avctx, AV_LOG_DEBUG, "Max. lag time %g ms\n",
638
12
           av_int2float(AV_RB32(avctx->extradata + 40)));
639
12
    av_log(avctx, AV_LOG_DEBUG, "Max. seek time %g ms\n",
640
12
           av_int2float(AV_RB32(avctx->extradata + 44)));
641
642
12
    if (version) {
643
10
        if (avctx->extradata_size < 60 + 256 * 3) {
644
            av_log(avctx, AV_LOG_ERROR,
645
                   "Insufficient extradata size %d for v2\n",
646
                   avctx->extradata_size);
647
            return AVERROR_INVALIDDATA;
648
        }
649
650
10
        c->slice_split = AV_RB32(avctx->extradata + 52);
651
10
        av_log(avctx, AV_LOG_DEBUG, "Slice split %d\n", c->slice_split);
652
653
10
        c->full_model_syms = AV_RB32(avctx->extradata + 56);
654

10
        if (c->full_model_syms < 2 || c->full_model_syms > 256) {
655
            av_log(avctx, AV_LOG_ERROR,
656
                   "Incorrect number of used colours %d\n",
657
                   c->full_model_syms);
658
            return AVERROR_INVALIDDATA;
659
        }
660
10
        av_log(avctx, AV_LOG_DEBUG, "Used colours %d\n",
661
               c->full_model_syms);
662
    } else {
663
2
        c->slice_split     = 0;
664
2
        c->full_model_syms = 256;
665
    }
666
667
3084
    for (i = 0; i < 256; i++)
668

3072
        c->pal[i] = 0xFFU << 24 | AV_RB24(avctx->extradata + 52 +
669
                            (version ? 8 : 0) + i * 3);
670
671
12
    c->mask_stride = FFALIGN(avctx->width, 16);
672
12
    c->mask        = av_malloc_array(c->mask_stride, avctx->height);
673
12
    if (!c->mask) {
674
        av_log(avctx, AV_LOG_ERROR, "Cannot allocate mask plane\n");
675
        return AVERROR(ENOMEM);
676
    }
677
678
12
    sc1->c = c;
679
12
    slicecontext_init(sc1, version, c->full_model_syms);
680
12
    if (c->slice_split) {
681
4
        sc2->c = c;
682
4
        slicecontext_init(sc2, version, c->full_model_syms);
683
    }
684
12
    c->corrupted = 1;
685
686
12
    return 0;
687
}
688
689
12
av_cold int ff_mss12_decode_end(MSS12Context *c)
690
{
691
12
    av_freep(&c->mask);
692
693
12
    return 0;
694
}