1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * Copyright (c) 2012 Konstantin Shishkov
3cabdff1aSopenharmony_ci *
4cabdff1aSopenharmony_ci * This file is part of FFmpeg.
5cabdff1aSopenharmony_ci *
6cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
7cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
8cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
9cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
10cabdff1aSopenharmony_ci *
11cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
12cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
13cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14cabdff1aSopenharmony_ci * Lesser General Public License for more details.
15cabdff1aSopenharmony_ci *
16cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
17cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
18cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19cabdff1aSopenharmony_ci */
20cabdff1aSopenharmony_ci
21cabdff1aSopenharmony_ci/**
22cabdff1aSopenharmony_ci * @file
23cabdff1aSopenharmony_ci * Common header for Microsoft Screen 1 and 2
24cabdff1aSopenharmony_ci */
25cabdff1aSopenharmony_ci
26cabdff1aSopenharmony_ci#ifndef AVCODEC_MSS12_H
27cabdff1aSopenharmony_ci#define AVCODEC_MSS12_H
28cabdff1aSopenharmony_ci
29cabdff1aSopenharmony_ci#include "libavutil/intreadwrite.h"
30cabdff1aSopenharmony_ci#include "avcodec.h"
31cabdff1aSopenharmony_ci#include "get_bits.h"
32cabdff1aSopenharmony_ci#include "bytestream.h"
33cabdff1aSopenharmony_ci
34cabdff1aSopenharmony_ci#define MODEL_MIN_SYMS    2
35cabdff1aSopenharmony_ci#define MODEL_MAX_SYMS  256
36cabdff1aSopenharmony_ci#define THRESH_ADAPTIVE  -1
37cabdff1aSopenharmony_ci#define THRESH_LOW       15
38cabdff1aSopenharmony_ci#define THRESH_HIGH      50
39cabdff1aSopenharmony_ci
40cabdff1aSopenharmony_citypedef struct Model {
41cabdff1aSopenharmony_ci    int16_t cum_prob[MODEL_MAX_SYMS + 1];
42cabdff1aSopenharmony_ci    int16_t weights[MODEL_MAX_SYMS + 1];
43cabdff1aSopenharmony_ci    uint8_t idx2sym[MODEL_MAX_SYMS + 1];
44cabdff1aSopenharmony_ci    int num_syms;
45cabdff1aSopenharmony_ci    int thr_weight, threshold;
46cabdff1aSopenharmony_ci} Model;
47cabdff1aSopenharmony_ci
48cabdff1aSopenharmony_citypedef struct ArithCoder {
49cabdff1aSopenharmony_ci    int low, high, value;
50cabdff1aSopenharmony_ci    int overread;
51cabdff1aSopenharmony_ci#define MAX_OVERREAD 16
52cabdff1aSopenharmony_ci    union {
53cabdff1aSopenharmony_ci        GetBitContext *gb;
54cabdff1aSopenharmony_ci        GetByteContext *gB;
55cabdff1aSopenharmony_ci    } gbc;
56cabdff1aSopenharmony_ci    int (*get_model_sym)(struct ArithCoder *c, Model *m);
57cabdff1aSopenharmony_ci    int (*get_number)   (struct ArithCoder *c, int n);
58cabdff1aSopenharmony_ci} ArithCoder;
59cabdff1aSopenharmony_ci
60cabdff1aSopenharmony_citypedef struct PixContext {
61cabdff1aSopenharmony_ci    int cache_size, num_syms;
62cabdff1aSopenharmony_ci    uint8_t cache[12];
63cabdff1aSopenharmony_ci    Model cache_model, full_model;
64cabdff1aSopenharmony_ci    Model sec_models[15][4];
65cabdff1aSopenharmony_ci    int special_initial_cache;
66cabdff1aSopenharmony_ci} PixContext;
67cabdff1aSopenharmony_ci
68cabdff1aSopenharmony_cistruct MSS12Context;
69cabdff1aSopenharmony_ci
70cabdff1aSopenharmony_citypedef struct SliceContext {
71cabdff1aSopenharmony_ci    const struct MSS12Context *c;
72cabdff1aSopenharmony_ci    Model      intra_region, inter_region;
73cabdff1aSopenharmony_ci    Model      pivot, edge_mode, split_mode;
74cabdff1aSopenharmony_ci    PixContext intra_pix_ctx, inter_pix_ctx;
75cabdff1aSopenharmony_ci} SliceContext;
76cabdff1aSopenharmony_ci
77cabdff1aSopenharmony_citypedef struct MSS12Context {
78cabdff1aSopenharmony_ci    AVCodecContext *avctx;
79cabdff1aSopenharmony_ci    uint32_t       pal[256];
80cabdff1aSopenharmony_ci    uint8_t        *pal_pic;
81cabdff1aSopenharmony_ci    uint8_t        *last_pal_pic;
82cabdff1aSopenharmony_ci    ptrdiff_t      pal_stride;
83cabdff1aSopenharmony_ci    uint8_t        *mask;
84cabdff1aSopenharmony_ci    ptrdiff_t      mask_stride;
85cabdff1aSopenharmony_ci    uint8_t        *rgb_pic;
86cabdff1aSopenharmony_ci    uint8_t        *last_rgb_pic;
87cabdff1aSopenharmony_ci    ptrdiff_t      rgb_stride;
88cabdff1aSopenharmony_ci    int            free_colours;
89cabdff1aSopenharmony_ci    int            keyframe;
90cabdff1aSopenharmony_ci    int            mvX, mvY;
91cabdff1aSopenharmony_ci    int            corrupted;
92cabdff1aSopenharmony_ci    int            slice_split;
93cabdff1aSopenharmony_ci    int            full_model_syms;
94cabdff1aSopenharmony_ci} MSS12Context;
95cabdff1aSopenharmony_ci
96cabdff1aSopenharmony_ciint ff_mss12_decode_rect(SliceContext *ctx, ArithCoder *acoder,
97cabdff1aSopenharmony_ci                         int x, int y, int width, int height);
98cabdff1aSopenharmony_civoid ff_mss12_model_update(Model *m, int val);
99cabdff1aSopenharmony_civoid ff_mss12_slicecontext_reset(SliceContext *sc);
100cabdff1aSopenharmony_ciint ff_mss12_decode_init(MSS12Context *c, int version,
101cabdff1aSopenharmony_ci                         SliceContext *sc1, SliceContext *sc2);
102cabdff1aSopenharmony_ciint ff_mss12_decode_end(MSS12Context *ctx);
103cabdff1aSopenharmony_ci
104cabdff1aSopenharmony_ci#define ARITH_GET_BIT(prefix)                                           \
105cabdff1aSopenharmony_cistatic int prefix ## _get_bit(ArithCoder *c)                            \
106cabdff1aSopenharmony_ci{                                                                       \
107cabdff1aSopenharmony_ci    int range = c->high - c->low + 1;                                   \
108cabdff1aSopenharmony_ci    int bit   = 2 * c->value - c->low >= c->high;                       \
109cabdff1aSopenharmony_ci                                                                        \
110cabdff1aSopenharmony_ci    if (bit)                                                            \
111cabdff1aSopenharmony_ci        c->low += range >> 1;                                           \
112cabdff1aSopenharmony_ci    else                                                                \
113cabdff1aSopenharmony_ci        c->high = c->low + (range >> 1) - 1;                            \
114cabdff1aSopenharmony_ci                                                                        \
115cabdff1aSopenharmony_ci    prefix ## _normalise(c);                                            \
116cabdff1aSopenharmony_ci                                                                        \
117cabdff1aSopenharmony_ci    return bit;                                                         \
118cabdff1aSopenharmony_ci}
119cabdff1aSopenharmony_ci
120cabdff1aSopenharmony_ci#define ARITH_GET_MODEL_SYM(prefix)                                     \
121cabdff1aSopenharmony_cistatic int prefix ## _get_model_sym(ArithCoder *c, Model *m)            \
122cabdff1aSopenharmony_ci{                                                                       \
123cabdff1aSopenharmony_ci    int idx, val;                                                       \
124cabdff1aSopenharmony_ci                                                                        \
125cabdff1aSopenharmony_ci    idx = prefix ## _get_prob(c, m->cum_prob);                          \
126cabdff1aSopenharmony_ci                                                                        \
127cabdff1aSopenharmony_ci    val = m->idx2sym[idx];                                              \
128cabdff1aSopenharmony_ci    ff_mss12_model_update(m, idx);                                      \
129cabdff1aSopenharmony_ci                                                                        \
130cabdff1aSopenharmony_ci    prefix ## _normalise(c);                                            \
131cabdff1aSopenharmony_ci                                                                        \
132cabdff1aSopenharmony_ci    return val;                                                         \
133cabdff1aSopenharmony_ci}
134cabdff1aSopenharmony_ci
135cabdff1aSopenharmony_ci#endif /* AVCODEC_MSS12_H */
136