summaryrefslogtreecommitdiff
path: root/libavcodec/msvideo1.c (plain)
blob: a49b9be364d7e4a110eafca8eabbe08a002fdc88
1/*
2 * Microsoft Video-1 Decoder
3 * Copyright (C) 2003 The FFmpeg project
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 Video-1 Decoder by Mike Melanson (melanson@pcisys.net)
25 * For more information about the MS Video-1 format, visit:
26 * http://www.pcisys.net/~melanson/codecs/
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33#include "libavutil/internal.h"
34#include "libavutil/intreadwrite.h"
35#include "avcodec.h"
36#include "internal.h"
37
38#define PALETTE_COUNT 256
39#define CHECK_STREAM_PTR(n) \
40 if ((stream_ptr + n) > s->size ) { \
41 av_log(s->avctx, AV_LOG_ERROR, " MS Video-1 warning: stream_ptr out of bounds (%d >= %d)\n", \
42 stream_ptr + n, s->size); \
43 return; \
44 }
45
46typedef struct Msvideo1Context {
47
48 AVCodecContext *avctx;
49 AVFrame *frame;
50
51 const unsigned char *buf;
52 int size;
53
54 int mode_8bit; /* if it's not 8-bit, it's 16-bit */
55
56 uint32_t pal[256];
57} Msvideo1Context;
58
59static av_cold int msvideo1_decode_init(AVCodecContext *avctx)
60{
61 Msvideo1Context *s = avctx->priv_data;
62
63 s->avctx = avctx;
64
65 /* figure out the colorspace based on the presence of a palette */
66 if (s->avctx->bits_per_coded_sample == 8) {
67 s->mode_8bit = 1;
68 avctx->pix_fmt = AV_PIX_FMT_PAL8;
69 if (avctx->extradata_size >= AVPALETTE_SIZE)
70 memcpy(s->pal, avctx->extradata, AVPALETTE_SIZE);
71 } else {
72 s->mode_8bit = 0;
73 avctx->pix_fmt = AV_PIX_FMT_RGB555;
74 }
75
76 s->frame = av_frame_alloc();
77 if (!s->frame)
78 return AVERROR(ENOMEM);
79
80 return 0;
81}
82
83static void msvideo1_decode_8bit(Msvideo1Context *s)
84{
85 int block_ptr, pixel_ptr;
86 int total_blocks;
87 int pixel_x, pixel_y; /* pixel width and height iterators */
88 int block_x, block_y; /* block width and height iterators */
89 int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
90 int block_inc;
91 int row_dec;
92
93 /* decoding parameters */
94 int stream_ptr;
95 unsigned char byte_a, byte_b;
96 unsigned short flags;
97 int skip_blocks;
98 unsigned char colors[8];
99 unsigned char *pixels = s->frame->data[0];
100 int stride = s->frame->linesize[0];
101
102 stream_ptr = 0;
103 skip_blocks = 0;
104 blocks_wide = s->avctx->width / 4;
105 blocks_high = s->avctx->height / 4;
106 total_blocks = blocks_wide * blocks_high;
107 block_inc = 4;
108 row_dec = stride + 4;
109
110 for (block_y = blocks_high; block_y > 0; block_y--) {
111 block_ptr = ((block_y * 4) - 1) * stride;
112 for (block_x = blocks_wide; block_x > 0; block_x--) {
113 /* check if this block should be skipped */
114 if (skip_blocks) {
115 block_ptr += block_inc;
116 skip_blocks--;
117 total_blocks--;
118 continue;
119 }
120
121 pixel_ptr = block_ptr;
122
123 /* get the next two bytes in the encoded data stream */
124 CHECK_STREAM_PTR(2);
125 byte_a = s->buf[stream_ptr++];
126 byte_b = s->buf[stream_ptr++];
127
128 /* check if the decode is finished */
129 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
130 return;
131 else if ((byte_b & 0xFC) == 0x84) {
132 /* skip code, but don't count the current block */
133 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
134 } else if (byte_b < 0x80) {
135 /* 2-color encoding */
136 flags = (byte_b << 8) | byte_a;
137
138 CHECK_STREAM_PTR(2);
139 colors[0] = s->buf[stream_ptr++];
140 colors[1] = s->buf[stream_ptr++];
141
142 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
143 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
144 pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
145 pixel_ptr -= row_dec;
146 }
147 } else if (byte_b >= 0x90) {
148 /* 8-color encoding */
149 flags = (byte_b << 8) | byte_a;
150
151 CHECK_STREAM_PTR(8);
152 memcpy(colors, &s->buf[stream_ptr], 8);
153 stream_ptr += 8;
154
155 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
156 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
157 pixels[pixel_ptr++] =
158 colors[((pixel_y & 0x2) << 1) +
159 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
160 pixel_ptr -= row_dec;
161 }
162 } else {
163 /* 1-color encoding */
164 colors[0] = byte_a;
165
166 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
167 for (pixel_x = 0; pixel_x < 4; pixel_x++)
168 pixels[pixel_ptr++] = colors[0];
169 pixel_ptr -= row_dec;
170 }
171 }
172
173 block_ptr += block_inc;
174 total_blocks--;
175 }
176 }
177
178 /* make the palette available on the way out */
179 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
180 memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
181}
182
183static void msvideo1_decode_16bit(Msvideo1Context *s)
184{
185 int block_ptr, pixel_ptr;
186 int total_blocks;
187 int pixel_x, pixel_y; /* pixel width and height iterators */
188 int block_x, block_y; /* block width and height iterators */
189 int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
190 int block_inc;
191 int row_dec;
192
193 /* decoding parameters */
194 int stream_ptr;
195 unsigned char byte_a, byte_b;
196 unsigned short flags;
197 int skip_blocks;
198 unsigned short colors[8];
199 unsigned short *pixels = (unsigned short *)s->frame->data[0];
200 int stride = s->frame->linesize[0] / 2;
201
202 stream_ptr = 0;
203 skip_blocks = 0;
204 blocks_wide = s->avctx->width / 4;
205 blocks_high = s->avctx->height / 4;
206 total_blocks = blocks_wide * blocks_high;
207 block_inc = 4;
208 row_dec = stride + 4;
209
210 for (block_y = blocks_high; block_y > 0; block_y--) {
211 block_ptr = ((block_y * 4) - 1) * stride;
212 for (block_x = blocks_wide; block_x > 0; block_x--) {
213 /* check if this block should be skipped */
214 if (skip_blocks) {
215 block_ptr += block_inc;
216 skip_blocks--;
217 total_blocks--;
218 continue;
219 }
220
221 pixel_ptr = block_ptr;
222
223 /* get the next two bytes in the encoded data stream */
224 CHECK_STREAM_PTR(2);
225 byte_a = s->buf[stream_ptr++];
226 byte_b = s->buf[stream_ptr++];
227
228 /* check if the decode is finished */
229 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
230 return;
231 } else if ((byte_b & 0xFC) == 0x84) {
232 /* skip code, but don't count the current block */
233 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
234 } else if (byte_b < 0x80) {
235 /* 2- or 8-color encoding modes */
236 flags = (byte_b << 8) | byte_a;
237
238 CHECK_STREAM_PTR(4);
239 colors[0] = AV_RL16(&s->buf[stream_ptr]);
240 stream_ptr += 2;
241 colors[1] = AV_RL16(&s->buf[stream_ptr]);
242 stream_ptr += 2;
243
244 if (colors[0] & 0x8000) {
245 /* 8-color encoding */
246 CHECK_STREAM_PTR(12);
247 colors[2] = AV_RL16(&s->buf[stream_ptr]);
248 stream_ptr += 2;
249 colors[3] = AV_RL16(&s->buf[stream_ptr]);
250 stream_ptr += 2;
251 colors[4] = AV_RL16(&s->buf[stream_ptr]);
252 stream_ptr += 2;
253 colors[5] = AV_RL16(&s->buf[stream_ptr]);
254 stream_ptr += 2;
255 colors[6] = AV_RL16(&s->buf[stream_ptr]);
256 stream_ptr += 2;
257 colors[7] = AV_RL16(&s->buf[stream_ptr]);
258 stream_ptr += 2;
259
260 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
261 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
262 pixels[pixel_ptr++] =
263 colors[((pixel_y & 0x2) << 1) +
264 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
265 pixel_ptr -= row_dec;
266 }
267 } else {
268 /* 2-color encoding */
269 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
270 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
271 pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
272 pixel_ptr -= row_dec;
273 }
274 }
275 } else {
276 /* otherwise, it's a 1-color block */
277 colors[0] = (byte_b << 8) | byte_a;
278
279 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
280 for (pixel_x = 0; pixel_x < 4; pixel_x++)
281 pixels[pixel_ptr++] = colors[0];
282 pixel_ptr -= row_dec;
283 }
284 }
285
286 block_ptr += block_inc;
287 total_blocks--;
288 }
289 }
290}
291
292static int msvideo1_decode_frame(AVCodecContext *avctx,
293 void *data, int *got_frame,
294 AVPacket *avpkt)
295{
296 const uint8_t *buf = avpkt->data;
297 int buf_size = avpkt->size;
298 Msvideo1Context *s = avctx->priv_data;
299 int ret;
300
301 s->buf = buf;
302 s->size = buf_size;
303
304 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
305 return ret;
306
307 if (s->mode_8bit) {
308 int size;
309 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
310
311 if (pal && size == AVPALETTE_SIZE) {
312 memcpy(s->pal, pal, AVPALETTE_SIZE);
313 s->frame->palette_has_changed = 1;
314 } else if (pal) {
315 av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
316 }
317 }
318
319 if (s->mode_8bit)
320 msvideo1_decode_8bit(s);
321 else
322 msvideo1_decode_16bit(s);
323
324 if ((ret = av_frame_ref(data, s->frame)) < 0)
325 return ret;
326
327 *got_frame = 1;
328
329 /* report that the buffer was completely consumed */
330 return buf_size;
331}
332
333static av_cold int msvideo1_decode_end(AVCodecContext *avctx)
334{
335 Msvideo1Context *s = avctx->priv_data;
336
337 av_frame_free(&s->frame);
338
339 return 0;
340}
341
342AVCodec ff_msvideo1_decoder = {
343 .name = "msvideo1",
344 .long_name = NULL_IF_CONFIG_SMALL("Microsoft Video 1"),
345 .type = AVMEDIA_TYPE_VIDEO,
346 .id = AV_CODEC_ID_MSVIDEO1,
347 .priv_data_size = sizeof(Msvideo1Context),
348 .init = msvideo1_decode_init,
349 .close = msvideo1_decode_end,
350 .decode = msvideo1_decode_frame,
351 .capabilities = AV_CODEC_CAP_DR1,
352};
353