mss1: Add missing macro parameters to ARITH_GET_* macros
[libav.git] / libavcodec / mss1.c
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 Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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 "internal.h"
29 #include "mss12.h"
30
31 typedef struct MSS1Context {
32 MSS12Context ctx;
33 AVFrame *pic;
34 SliceContext sc;
35 } MSS1Context;
36
37 static void arith1_normalise(ArithCoder *c)
38 {
39 for (;;) {
40 if (c->high >= 0x8000) {
41 if (c->low < 0x8000) {
42 if (c->low >= 0x4000 && c->high < 0xC000) {
43 c->value -= 0x4000;
44 c->low -= 0x4000;
45 c->high -= 0x4000;
46 } else {
47 return;
48 }
49 } else {
50 c->value -= 0x8000;
51 c->low -= 0x8000;
52 c->high -= 0x8000;
53 }
54 }
55 c->value <<= 1;
56 c->low <<= 1;
57 c->high <<= 1;
58 c->high |= 1;
59 c->value |= get_bits1(c->gbc.gb);
60 }
61 }
62
63 ARITH_GET_BIT(1)
64
65 static int arith_get_bits(ArithCoder *c, int bits)
66 {
67 int range = c->high - c->low + 1;
68 int val = (((c->value - c->low + 1) << bits) - 1) / range;
69 int prob = range * val;
70
71 c->high = ((prob + range) >> bits) + c->low - 1;
72 c->low += prob >> bits;
73
74 arith1_normalise(c);
75
76 return val;
77 }
78
79 static int arith_get_number(ArithCoder *c, int mod_val)
80 {
81 int range = c->high - c->low + 1;
82 int val = ((c->value - c->low + 1) * mod_val - 1) / range;
83 int prob = range * val;
84
85 c->high = (prob + range) / mod_val + c->low - 1;
86 c->low += prob / mod_val;
87
88 arith1_normalise(c);
89
90 return val;
91 }
92
93 static int arith1_get_prob(ArithCoder *c, int16_t *probs)
94 {
95 int range = c->high - c->low + 1;
96 int val = ((c->value - c->low + 1) * probs[0] - 1) / range;
97 int sym = 1;
98
99 while (probs[sym] > val)
100 sym++;
101
102 c->high = range * probs[sym - 1] / probs[0] + c->low - 1;
103 c->low += range * probs[sym] / probs[0];
104
105 return sym;
106 }
107
108 ARITH_GET_MODEL_SYM(1)
109
110 static void arith_init(ArithCoder *c, GetBitContext *gb)
111 {
112 c->low = 0;
113 c->high = 0xFFFF;
114 c->value = get_bits(gb, 16);
115 c->gbc.gb = gb;
116 c->get_model_sym = arith1_get_model_sym;
117 c->get_number = arith_get_number;
118 }
119
120 static int decode_pal(MSS12Context *ctx, ArithCoder *acoder)
121 {
122 int i, ncol, r, g, b;
123 uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
124
125 if (!ctx->free_colours)
126 return 0;
127
128 ncol = arith_get_number(acoder, ctx->free_colours + 1);
129 for (i = 0; i < ncol; i++) {
130 r = arith_get_bits(acoder, 8);
131 g = arith_get_bits(acoder, 8);
132 b = arith_get_bits(acoder, 8);
133 *pal++ = (r << 16) | (g << 8) | b;
134 }
135
136 return !!ncol;
137 }
138
139 static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
140 AVPacket *avpkt)
141 {
142 const uint8_t *buf = avpkt->data;
143 int buf_size = avpkt->size;
144 MSS1Context *ctx = avctx->priv_data;
145 MSS12Context *c = &ctx->ctx;
146 GetBitContext gb;
147 ArithCoder acoder;
148 int pal_changed = 0;
149 int ret;
150
151 init_get_bits(&gb, buf, buf_size * 8);
152 arith_init(&acoder, &gb);
153
154 if ((ret = ff_reget_buffer(avctx, ctx->pic)) < 0) {
155 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
156 return ret;
157 }
158
159 c->pal_pic = ctx->pic->data[0] + ctx->pic->linesize[0] * (avctx->height - 1);
160 c->pal_stride = -ctx->pic->linesize[0];
161 c->keyframe = !arith1_get_bit(&acoder);
162 if (c->keyframe) {
163 c->corrupted = 0;
164 ff_mss12_slicecontext_reset(&ctx->sc);
165 pal_changed = decode_pal(c, &acoder);
166 ctx->pic->key_frame = 1;
167 ctx->pic->pict_type = AV_PICTURE_TYPE_I;
168 } else {
169 if (c->corrupted)
170 return AVERROR_INVALIDDATA;
171 ctx->pic->key_frame = 0;
172 ctx->pic->pict_type = AV_PICTURE_TYPE_P;
173 }
174 c->corrupted = ff_mss12_decode_rect(&ctx->sc, &acoder, 0, 0,
175 avctx->width, avctx->height);
176 if (c->corrupted)
177 return AVERROR_INVALIDDATA;
178 memcpy(ctx->pic->data[1], c->pal, AVPALETTE_SIZE);
179 ctx->pic->palette_has_changed = pal_changed;
180
181 if ((ret = av_frame_ref(data, ctx->pic)) < 0)
182 return ret;
183
184 *got_frame = 1;
185
186 /* always report that the buffer was completely consumed */
187 return buf_size;
188 }
189
190 static av_cold int mss1_decode_init(AVCodecContext *avctx)
191 {
192 MSS1Context * const c = avctx->priv_data;
193 int ret;
194
195 c->ctx.avctx = avctx;
196
197 c->pic = av_frame_alloc();
198 if (!c->pic)
199 return AVERROR(ENOMEM);
200
201 ret = ff_mss12_decode_init(&c->ctx, 0, &c->sc, NULL);
202
203 avctx->pix_fmt = AV_PIX_FMT_PAL8;
204
205 return ret;
206 }
207
208 static av_cold int mss1_decode_end(AVCodecContext *avctx)
209 {
210 MSS1Context * const ctx = avctx->priv_data;
211
212 av_frame_free(&ctx->pic);
213 ff_mss12_decode_end(&ctx->ctx);
214
215 return 0;
216 }
217
218 AVCodec ff_mss1_decoder = {
219 .name = "mss1",
220 .long_name = NULL_IF_CONFIG_SMALL("MS Screen 1"),
221 .type = AVMEDIA_TYPE_VIDEO,
222 .id = AV_CODEC_ID_MSS1,
223 .priv_data_size = sizeof(MSS1Context),
224 .init = mss1_decode_init,
225 .close = mss1_decode_end,
226 .decode = mss1_decode_frame,
227 .capabilities = AV_CODEC_CAP_DR1,
228 };