]> git.sesse.net Git - ffmpeg/blob - libavcodec/mss1.c
mss1: move code that will be reused by MSS2 decoder into separate file
[ffmpeg] / 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 "libavutil/intfloat.h"
28 #include "libavutil/intreadwrite.h"
29 #include "avcodec.h"
30 #include "mss12.h"
31
32 typedef struct MSS1Context {
33     MSS12Context   ctx;
34     AVFrame        pic;
35 } MSS1Context;
36
37 static void arith_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->gb);
60     }
61 }
62
63 static int arith_get_bit(ArithCoder *c)
64 {
65     int range = c->high - c->low + 1;
66     int bit   = (((c->value - c->low) << 1) + 1) / range;
67
68     if (bit)
69         c->low += range >> 1;
70     else
71         c->high = c->low + (range >> 1) - 1;
72
73     arith_normalise(c);
74
75     return bit;
76 }
77
78 static int arith_get_bits(ArithCoder *c, int bits)
79 {
80     int range = c->high - c->low + 1;
81     int val   = (((c->value - c->low + 1) << bits) - 1) / range;
82     int prob  = range * val;
83
84     c->high   = ((prob + range) >> bits) + c->low - 1;
85     c->low   += prob >> bits;
86
87     arith_normalise(c);
88
89     return val;
90 }
91
92 static int arith_get_number(ArithCoder *c, int mod_val)
93 {
94     int range = c->high - c->low + 1;
95     int val   = ((c->value - c->low + 1) * mod_val - 1) / range;
96     int prob  = range * val;
97
98     c->high   = (prob + range) / mod_val + c->low - 1;
99     c->low   += prob / mod_val;
100
101     arith_normalise(c);
102
103     return val;
104 }
105
106 static int arith_get_prob(ArithCoder *c, int *probs)
107 {
108     int range = c->high - c->low + 1;
109     int val   = ((c->value - c->low + 1) * probs[0] - 1) / range;
110     int sym   = 1;
111
112     while (probs[sym] > val)
113         sym++;
114
115     c->high = range * probs[sym - 1] / probs[0] + c->low - 1;
116     c->low += range * probs[sym]     / probs[0];
117
118     return sym;
119 }
120
121 static int arith_get_model_sym(ArithCoder *c, Model *m)
122 {
123     int idx, val;
124
125     idx = arith_get_prob(c, m->cum_prob);
126
127     val = m->idx2sym[idx];
128     ff_mss12_model_update(m, idx);
129
130     arith_normalise(c);
131
132     return val;
133 }
134
135 static void arith_init(ArithCoder *c, GetBitContext *gb)
136 {
137     c->low   = 0;
138     c->high  = 0xFFFF;
139     c->value = get_bits(gb, 16);
140     c->gb    = gb;
141
142     c->get_model_sym = arith_get_model_sym;
143     c->get_number    = arith_get_number;
144 }
145
146 static int decode_pal(MSS1Context *ctx, ArithCoder *acoder)
147 {
148     int i, ncol, r, g, b;
149     uint32_t *pal = ctx->ctx.pal + 256 - ctx->ctx.free_colours;
150
151     if (!ctx->ctx.free_colours)
152         return 0;
153
154     ncol = arith_get_number(acoder, ctx->ctx.free_colours + 1);
155     for (i = 0; i < ncol; i++) {
156         r = arith_get_bits(acoder, 8);
157         g = arith_get_bits(acoder, 8);
158         b = arith_get_bits(acoder, 8);
159         *pal++ = (r << 16) | (g << 8) | b;
160     }
161
162     return !!ncol;
163 }
164
165 static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
166                              AVPacket *avpkt)
167 {
168     const uint8_t *buf = avpkt->data;
169     int buf_size = avpkt->size;
170     MSS1Context *c = avctx->priv_data;
171     GetBitContext gb;
172     ArithCoder acoder;
173     int pal_changed = 0;
174     int ret;
175
176     init_get_bits(&gb, buf, buf_size * 8);
177     arith_init(&acoder, &gb);
178
179     c->pic.reference    = 3;
180     c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
181                           FF_BUFFER_HINTS_REUSABLE;
182     if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
183         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
184         return ret;
185     }
186
187     c->ctx.pic_start  = c->pic.data[0] + c->pic.linesize[0] * (avctx->height - 1);
188     c->ctx.pic_stride = -c->pic.linesize[0];
189     c->ctx.keyframe   = !arith_get_bit(&acoder);
190     if (c->ctx.keyframe) {
191         ff_mss12_codec_reset(&c->ctx);
192         pal_changed      = decode_pal(c, &acoder);
193         c->pic.key_frame = 1;
194         c->pic.pict_type = AV_PICTURE_TYPE_I;
195     } else {
196         if (c->ctx.corrupted)
197             return AVERROR_INVALIDDATA;
198         c->pic.key_frame = 0;
199         c->pic.pict_type = AV_PICTURE_TYPE_P;
200     }
201     c->ctx.corrupted = ff_mss12_decode_rect(&c->ctx, &acoder, 0, 0,
202                                             avctx->width, avctx->height);
203     if (c->ctx.corrupted)
204         return AVERROR_INVALIDDATA;
205     memcpy(c->pic.data[1], c->ctx.pal, AVPALETTE_SIZE);
206     c->pic.palette_has_changed = pal_changed;
207
208     *data_size = sizeof(AVFrame);
209     *(AVFrame*)data = c->pic;
210
211     /* always report that the buffer was completely consumed */
212     return buf_size;
213 }
214
215 static av_cold int mss1_decode_init(AVCodecContext *avctx)
216 {
217     MSS1Context * const c = avctx->priv_data;
218
219     c->ctx.avctx       = avctx;
220     avctx->coded_frame = &c->pic;
221
222     return ff_mss12_decode_init(avctx, 0);
223 }
224
225 static av_cold int mss1_decode_end(AVCodecContext *avctx)
226 {
227     MSS1Context * const c = avctx->priv_data;
228
229     if (c->pic.data[0])
230         avctx->release_buffer(avctx, &c->pic);
231     ff_mss12_decode_end(avctx);
232
233     return 0;
234 }
235
236 AVCodec ff_mss1_decoder = {
237     .name           = "mss1",
238     .type           = AVMEDIA_TYPE_VIDEO,
239     .id             = AV_CODEC_ID_MSS1,
240     .priv_data_size = sizeof(MSS1Context),
241     .init           = mss1_decode_init,
242     .close          = mss1_decode_end,
243     .decode         = mss1_decode_frame,
244     .capabilities   = CODEC_CAP_DR1,
245     .long_name      = NULL_IF_CONFIG_SMALL("MS Screen 1"),
246 };