/src/ffmpeg/libavcodec/mss1.c
Line | Count | Source |
1 | | /* |
2 | | * Microsoft Screen 1 (aka Windows Media Video V7 Screen) decoder |
3 | | * Copyright (c) 2012 Konstantin Shishkov |
4 | | * |
5 | | * This file is part of FFmpeg. |
6 | | * |
7 | | * FFmpeg is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * FFmpeg is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with FFmpeg; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | /** |
23 | | * @file |
24 | | * Microsoft Screen 1 (aka Windows Media Video V7 Screen) decoder |
25 | | */ |
26 | | |
27 | | #include "avcodec.h" |
28 | | #include "codec_internal.h" |
29 | | #include "decode.h" |
30 | | #include "mss12.h" |
31 | | |
32 | | typedef struct MSS1Context { |
33 | | MSS12Context ctx; |
34 | | AVFrame *pic; |
35 | | SliceContext sc; |
36 | | } MSS1Context; |
37 | | |
38 | | static void arith_normalise(ArithCoder *c) |
39 | 57.4M | { |
40 | 224M | for (;;) { |
41 | 224M | if (c->high >= 0x8000) { |
42 | 199M | if (c->low < 0x8000) { |
43 | 185M | if (c->low >= 0x4000 && c->high < 0xC000) { |
44 | 128M | c->value -= 0x4000; |
45 | 128M | c->low -= 0x4000; |
46 | 128M | c->high -= 0x4000; |
47 | 128M | } else { |
48 | 57.4M | return; |
49 | 57.4M | } |
50 | 185M | } else { |
51 | 13.2M | c->value -= 0x8000; |
52 | 13.2M | c->low -= 0x8000; |
53 | 13.2M | c->high -= 0x8000; |
54 | 13.2M | } |
55 | 199M | } |
56 | 166M | c->value <<= 1; |
57 | 166M | c->low <<= 1; |
58 | 166M | c->high <<= 1; |
59 | 166M | c->high |= 1; |
60 | 166M | if (get_bits_left(c->gbc.gb) < 1) |
61 | 100M | c->overread++; |
62 | 166M | c->value |= get_bits1(c->gbc.gb); |
63 | 166M | } |
64 | 57.4M | } |
65 | | |
66 | | ARITH_GET_BIT(arith) |
67 | | |
68 | | static int arith_get_bits(ArithCoder *c, int bits) |
69 | 12.4M | { |
70 | 12.4M | int range = c->high - c->low + 1; |
71 | 12.4M | int val = (((c->value - c->low + 1) << bits) - 1) / range; |
72 | 12.4M | int prob = range * val; |
73 | | |
74 | 12.4M | c->high = ((prob + range) >> bits) + c->low - 1; |
75 | 12.4M | c->low += prob >> bits; |
76 | | |
77 | 12.4M | arith_normalise(c); |
78 | | |
79 | 12.4M | return val; |
80 | 12.4M | } |
81 | | |
82 | | static int arith_get_number(ArithCoder *c, int mod_val) |
83 | 177k | { |
84 | 177k | int range = c->high - c->low + 1; |
85 | 177k | int val = ((c->value - c->low + 1) * mod_val - 1) / range; |
86 | 177k | int prob = range * val; |
87 | | |
88 | 177k | c->high = (prob + range) / mod_val + c->low - 1; |
89 | 177k | c->low += prob / mod_val; |
90 | | |
91 | 177k | arith_normalise(c); |
92 | | |
93 | 177k | return val; |
94 | 177k | } |
95 | | |
96 | | static int arith_get_prob(ArithCoder *c, int16_t *probs) |
97 | 44.4M | { |
98 | 44.4M | int range = c->high - c->low + 1; |
99 | 44.4M | int val = ((c->value - c->low + 1) * probs[0] - 1) / range; |
100 | 44.4M | int sym = 1; |
101 | | |
102 | 167M | while (probs[sym] > val) |
103 | 122M | sym++; |
104 | | |
105 | 44.4M | c->high = range * probs[sym - 1] / probs[0] + c->low - 1; |
106 | 44.4M | c->low += range * probs[sym] / probs[0]; |
107 | | |
108 | 44.4M | return sym; |
109 | 44.4M | } |
110 | | |
111 | | ARITH_GET_MODEL_SYM(arith) |
112 | | |
113 | | static void arith_init(ArithCoder *c, GetBitContext *gb) |
114 | 339k | { |
115 | 339k | c->low = 0; |
116 | 339k | c->high = 0xFFFF; |
117 | 339k | c->value = get_bits(gb, 16); |
118 | 339k | c->overread = 0; |
119 | 339k | c->gbc.gb = gb; |
120 | 339k | c->get_model_sym = arith_get_model_sym; |
121 | 339k | c->get_number = arith_get_number; |
122 | 339k | } |
123 | | |
124 | | static void decode_pal(MSS12Context *ctx, ArithCoder *acoder) |
125 | 214k | { |
126 | 214k | int i, ncol, r, g, b; |
127 | 214k | uint32_t *pal = ctx->pal + 256 - ctx->free_colours; |
128 | | |
129 | 214k | if (!ctx->free_colours) |
130 | 94.6k | return; |
131 | | |
132 | 119k | ncol = arith_get_number(acoder, ctx->free_colours + 1); |
133 | 4.25M | for (i = 0; i < ncol; i++) { |
134 | 4.13M | r = arith_get_bits(acoder, 8); |
135 | 4.13M | g = arith_get_bits(acoder, 8); |
136 | 4.13M | b = arith_get_bits(acoder, 8); |
137 | 4.13M | *pal++ = (0xFFU << 24) | (r << 16) | (g << 8) | b; |
138 | 4.13M | } |
139 | 119k | } |
140 | | |
141 | | static int mss1_decode_frame(AVCodecContext *avctx, AVFrame *rframe, |
142 | | int *got_frame, AVPacket *avpkt) |
143 | 339k | { |
144 | 339k | MSS1Context *ctx = avctx->priv_data; |
145 | 339k | MSS12Context *c = &ctx->ctx; |
146 | 339k | GetBitContext gb; |
147 | 339k | ArithCoder acoder; |
148 | 339k | int ret; |
149 | | |
150 | 339k | if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0) |
151 | 0 | return ret; |
152 | | |
153 | 339k | arith_init(&acoder, &gb); |
154 | | |
155 | 339k | if ((ret = ff_reget_buffer(avctx, ctx->pic, 0)) < 0) |
156 | 2.28k | return ret; |
157 | | |
158 | 337k | c->pal_pic = ctx->pic->data[0] + ctx->pic->linesize[0] * (avctx->height - 1); |
159 | 337k | c->pal_stride = -ctx->pic->linesize[0]; |
160 | 337k | c->keyframe = !arith_get_bit(&acoder); |
161 | 337k | if (c->keyframe) { |
162 | 214k | c->corrupted = 0; |
163 | 214k | ff_mss12_slicecontext_reset(&ctx->sc); |
164 | 214k | decode_pal(c, &acoder); |
165 | 214k | ctx->pic->flags |= AV_FRAME_FLAG_KEY; |
166 | 214k | ctx->pic->pict_type = AV_PICTURE_TYPE_I; |
167 | 214k | } else { |
168 | 122k | if (c->corrupted) |
169 | 51.3k | return AVERROR_INVALIDDATA; |
170 | 71.5k | ctx->pic->flags &= ~AV_FRAME_FLAG_KEY; |
171 | 71.5k | ctx->pic->pict_type = AV_PICTURE_TYPE_P; |
172 | 71.5k | } |
173 | 285k | c->corrupted = ff_mss12_decode_rect(&ctx->sc, &acoder, 0, 0, |
174 | 285k | avctx->width, avctx->height); |
175 | 285k | if (c->corrupted) |
176 | 199k | return AVERROR_INVALIDDATA; |
177 | 85.8k | memcpy(ctx->pic->data[1], c->pal, AVPALETTE_SIZE); |
178 | | |
179 | 85.8k | if ((ret = av_frame_ref(rframe, ctx->pic)) < 0) |
180 | 0 | return ret; |
181 | | |
182 | 85.8k | *got_frame = 1; |
183 | | |
184 | | /* always report that the buffer was completely consumed */ |
185 | 85.8k | return avpkt->size; |
186 | 85.8k | } |
187 | | |
188 | | static av_cold int mss1_decode_init(AVCodecContext *avctx) |
189 | 1.05k | { |
190 | 1.05k | MSS1Context * const c = avctx->priv_data; |
191 | 1.05k | int ret; |
192 | | |
193 | 1.05k | c->ctx.avctx = avctx; |
194 | | |
195 | 1.05k | c->pic = av_frame_alloc(); |
196 | 1.05k | if (!c->pic) |
197 | 0 | return AVERROR(ENOMEM); |
198 | | |
199 | 1.05k | ret = ff_mss12_decode_init(&c->ctx, 0, &c->sc, NULL); |
200 | 1.05k | if (ret < 0) |
201 | 403 | return ret; |
202 | | |
203 | 652 | avctx->pix_fmt = AV_PIX_FMT_PAL8; |
204 | | |
205 | 652 | return ret; |
206 | 1.05k | } |
207 | | |
208 | | static av_cold int mss1_decode_end(AVCodecContext *avctx) |
209 | 1.05k | { |
210 | 1.05k | MSS1Context * const ctx = avctx->priv_data; |
211 | | |
212 | 1.05k | av_frame_free(&ctx->pic); |
213 | 1.05k | ff_mss12_decode_end(&ctx->ctx); |
214 | | |
215 | 1.05k | return 0; |
216 | 1.05k | } |
217 | | |
218 | | const FFCodec ff_mss1_decoder = { |
219 | | .p.name = "mss1", |
220 | | CODEC_LONG_NAME("MS Screen 1"), |
221 | | .p.type = AVMEDIA_TYPE_VIDEO, |
222 | | .p.id = AV_CODEC_ID_MSS1, |
223 | | .priv_data_size = sizeof(MSS1Context), |
224 | | .init = mss1_decode_init, |
225 | | .close = mss1_decode_end, |
226 | | FF_CODEC_DECODE_CB(mss1_decode_frame), |
227 | | .p.capabilities = AV_CODEC_CAP_DR1, |
228 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
229 | | }; |