Mike Melanson | 2fdf638 | 2003-10-01 04:39:38 | [diff] [blame] | 1 | /* |
| 2 | * Microsoft Video-1 Decoder |
| 3 | * Copyright (C) 2003 the ffmpeg project |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * @file msvideo1.c |
| 23 | * Microsoft Video-1 Decoder by Mike Melanson ([email protected]) |
| 24 | * For more information about the MS Video-1 format, visit: |
| 25 | * http://www.pcisys.net/~melanson/codecs/ |
| 26 | * |
| 27 | * This decoder outputs either PAL8 or RGB555 data, depending on the |
Roberto Togni | 875efaf | 2003-11-02 21:57:55 | [diff] [blame^] | 28 | * whether a RGB palette was passed through palctrl; |
| 29 | * if it's present, then the data is PAL8; RGB555 otherwise. |
Mike Melanson | 2fdf638 | 2003-10-01 04:39:38 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | #include "common.h" |
| 38 | #include "avcodec.h" |
| 39 | #include "dsputil.h" |
| 40 | |
| 41 | #define PALETTE_COUNT 256 |
| 42 | #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0]) |
| 43 | #define CHECK_STREAM_PTR(n) \ |
| 44 | if ((stream_ptr + n) > s->size ) { \ |
| 45 | printf (" MS Video-1 warning: stream_ptr out of bounds (%d >= %d)\n", \ |
| 46 | stream_ptr + n, s->size); \ |
| 47 | return; \ |
| 48 | } |
| 49 | |
| 50 | #define COPY_PREV_BLOCK() \ |
| 51 | pixel_ptr = block_ptr; \ |
| 52 | for (pixel_y = 0; pixel_y < 4; pixel_y++) { \ |
| 53 | for (pixel_x = 0; pixel_x < 4; pixel_x++, pixel_ptr++) \ |
| 54 | pixels[pixel_ptr] = prev_pixels[pixel_ptr]; \ |
| 55 | pixel_ptr -= row_dec; \ |
| 56 | } |
| 57 | |
| 58 | typedef struct Msvideo1Context { |
| 59 | |
| 60 | AVCodecContext *avctx; |
| 61 | DSPContext dsp; |
| 62 | AVFrame frame; |
| 63 | AVFrame prev_frame; |
| 64 | |
| 65 | unsigned char *buf; |
| 66 | int size; |
| 67 | |
| 68 | int mode_8bit; /* if it's not 8-bit, it's 16-bit */ |
Mike Melanson | 2fdf638 | 2003-10-01 04:39:38 | [diff] [blame] | 69 | |
| 70 | } Msvideo1Context; |
| 71 | |
| 72 | static int msvideo1_decode_init(AVCodecContext *avctx) |
| 73 | { |
| 74 | Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data; |
Mike Melanson | 2fdf638 | 2003-10-01 04:39:38 | [diff] [blame] | 75 | |
| 76 | s->avctx = avctx; |
| 77 | |
Roberto Togni | 875efaf | 2003-11-02 21:57:55 | [diff] [blame^] | 78 | /* figure out the colorspace based on the presence of a palette */ |
| 79 | if (s->avctx->palctrl) { |
Mike Melanson | 2fdf638 | 2003-10-01 04:39:38 | [diff] [blame] | 80 | s->mode_8bit = 1; |
Mike Melanson | 2fdf638 | 2003-10-01 04:39:38 | [diff] [blame] | 81 | avctx->pix_fmt = PIX_FMT_PAL8; |
| 82 | } else { |
| 83 | s->mode_8bit = 0; |
| 84 | avctx->pix_fmt = PIX_FMT_RGB555; |
| 85 | } |
| 86 | |
| 87 | avctx->has_b_frames = 0; |
| 88 | dsputil_init(&s->dsp, avctx); |
| 89 | |
| 90 | s->frame.data[0] = s->prev_frame.data[0] = NULL; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static void msvideo1_decode_8bit(Msvideo1Context *s) |
| 96 | { |
| 97 | int block_ptr, pixel_ptr; |
| 98 | int total_blocks; |
| 99 | int pixel_x, pixel_y; /* pixel width and height iterators */ |
| 100 | int block_x, block_y; /* block width and height iterators */ |
| 101 | int blocks_wide, blocks_high; /* width and height in 4x4 blocks */ |
| 102 | int block_inc; |
| 103 | int row_dec; |
| 104 | |
| 105 | /* decoding parameters */ |
| 106 | int stream_ptr; |
| 107 | unsigned char byte_a, byte_b; |
| 108 | unsigned short flags; |
| 109 | int skip_blocks; |
| 110 | unsigned char colors[8]; |
| 111 | unsigned char *pixels = s->frame.data[0]; |
| 112 | unsigned char *prev_pixels = s->prev_frame.data[0]; |
| 113 | int stride = s->frame.linesize[0]; |
| 114 | |
| 115 | stream_ptr = 0; |
| 116 | skip_blocks = 0; |
| 117 | blocks_wide = s->avctx->width / 4; |
| 118 | blocks_high = s->avctx->height / 4; |
| 119 | total_blocks = blocks_wide * blocks_high; |
| 120 | block_inc = 4; |
| 121 | row_dec = stride + 4; |
| 122 | |
| 123 | for (block_y = blocks_high; block_y > 0; block_y--) { |
| 124 | block_ptr = ((block_y * 4) - 1) * stride; |
| 125 | for (block_x = blocks_wide; block_x > 0; block_x--) { |
| 126 | /* check if this block should be skipped */ |
| 127 | if (skip_blocks) { |
| 128 | COPY_PREV_BLOCK(); |
| 129 | block_ptr += block_inc; |
| 130 | skip_blocks--; |
| 131 | total_blocks--; |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | pixel_ptr = block_ptr; |
| 136 | |
| 137 | /* get the next two bytes in the encoded data stream */ |
| 138 | CHECK_STREAM_PTR(2); |
| 139 | byte_a = s->buf[stream_ptr++]; |
| 140 | byte_b = s->buf[stream_ptr++]; |
| 141 | |
| 142 | /* check if the decode is finished */ |
| 143 | if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) |
| 144 | return; |
| 145 | else if ((byte_b & 0xFC) == 0x84) { |
| 146 | /* skip code, but don't count the current block */ |
| 147 | skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1; |
| 148 | COPY_PREV_BLOCK(); |
| 149 | } else if (byte_b < 0x80) { |
| 150 | /* 2-color encoding */ |
| 151 | flags = (byte_b << 8) | byte_a; |
| 152 | |
| 153 | CHECK_STREAM_PTR(2); |
| 154 | colors[0] = s->buf[stream_ptr++]; |
| 155 | colors[1] = s->buf[stream_ptr++]; |
| 156 | |
| 157 | for (pixel_y = 0; pixel_y < 4; pixel_y++) { |
| 158 | for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1) |
| 159 | pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1]; |
| 160 | pixel_ptr -= row_dec; |
| 161 | } |
| 162 | } else if (byte_b >= 0x90) { |
| 163 | /* 8-color encoding */ |
| 164 | flags = (byte_b << 8) | byte_a; |
| 165 | |
| 166 | CHECK_STREAM_PTR(8); |
| 167 | memcpy(colors, &s->buf[stream_ptr], 8); |
| 168 | stream_ptr += 8; |
| 169 | |
| 170 | for (pixel_y = 0; pixel_y < 4; pixel_y++) { |
| 171 | for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1) |
| 172 | pixels[pixel_ptr++] = |
| 173 | colors[((pixel_y & 0x2) << 1) + |
| 174 | (pixel_x & 0x2) + ((flags & 0x1) ^ 1)]; |
| 175 | pixel_ptr -= row_dec; |
| 176 | } |
| 177 | } else { |
| 178 | /* 1-color encoding */ |
| 179 | colors[0] = byte_a; |
| 180 | |
| 181 | for (pixel_y = 0; pixel_y < 4; pixel_y++) { |
| 182 | for (pixel_x = 0; pixel_x < 4; pixel_x++) |
| 183 | pixels[pixel_ptr++] = colors[0]; |
| 184 | pixel_ptr -= row_dec; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | block_ptr += block_inc; |
| 189 | total_blocks--; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* make the palette available on the way out */ |
Roberto Togni | 875efaf | 2003-11-02 21:57:55 | [diff] [blame^] | 194 | if (s->avctx->pix_fmt == PIX_FMT_PAL8) { |
| 195 | memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE); |
| 196 | if (s->avctx->palctrl->palette_changed) { |
| 197 | s->frame.palette_has_changed = 1; |
| 198 | s->avctx->palctrl->palette_changed = 0; |
| 199 | } |
| 200 | } |
Mike Melanson | 2fdf638 | 2003-10-01 04:39:38 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static void msvideo1_decode_16bit(Msvideo1Context *s) |
| 204 | { |
| 205 | int block_ptr, pixel_ptr; |
| 206 | int total_blocks; |
| 207 | int pixel_x, pixel_y; /* pixel width and height iterators */ |
| 208 | int block_x, block_y; /* block width and height iterators */ |
| 209 | int blocks_wide, blocks_high; /* width and height in 4x4 blocks */ |
| 210 | int block_inc; |
| 211 | int row_dec; |
| 212 | |
| 213 | /* decoding parameters */ |
| 214 | int stream_ptr; |
| 215 | unsigned char byte_a, byte_b; |
| 216 | unsigned short flags; |
| 217 | int skip_blocks; |
| 218 | unsigned short colors[8]; |
| 219 | unsigned short *pixels = (unsigned short *)s->frame.data[0]; |
| 220 | unsigned short *prev_pixels = (unsigned short *)s->prev_frame.data[0]; |
| 221 | int stride = s->frame.linesize[0] / 2; |
| 222 | |
| 223 | stream_ptr = 0; |
| 224 | skip_blocks = 0; |
| 225 | blocks_wide = s->avctx->width / 4; |
| 226 | blocks_high = s->avctx->height / 4; |
| 227 | total_blocks = blocks_wide * blocks_high; |
| 228 | block_inc = 4; |
| 229 | row_dec = stride + 4; |
| 230 | |
| 231 | for (block_y = blocks_high; block_y > 0; block_y--) { |
| 232 | block_ptr = ((block_y * 4) - 1) * stride; |
| 233 | for (block_x = blocks_wide; block_x > 0; block_x--) { |
| 234 | /* check if this block should be skipped */ |
| 235 | if (skip_blocks) { |
| 236 | COPY_PREV_BLOCK(); |
| 237 | block_ptr += block_inc; |
| 238 | skip_blocks--; |
| 239 | total_blocks--; |
| 240 | continue; |
| 241 | } |
| 242 | |
| 243 | pixel_ptr = block_ptr; |
| 244 | |
| 245 | /* get the next two bytes in the encoded data stream */ |
| 246 | CHECK_STREAM_PTR(2); |
| 247 | byte_a = s->buf[stream_ptr++]; |
| 248 | byte_b = s->buf[stream_ptr++]; |
| 249 | |
| 250 | /* check if the decode is finished */ |
| 251 | if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) { |
| 252 | return; |
| 253 | } else if ((byte_b & 0xFC) == 0x84) { |
| 254 | /* skip code, but don't count the current block */ |
| 255 | skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1; |
| 256 | COPY_PREV_BLOCK(); |
| 257 | } else if (byte_b < 0x80) { |
| 258 | /* 2- or 8-color encoding modes */ |
| 259 | flags = (byte_b << 8) | byte_a; |
| 260 | |
| 261 | CHECK_STREAM_PTR(4); |
| 262 | colors[0] = LE_16(&s->buf[stream_ptr]); |
| 263 | stream_ptr += 2; |
| 264 | colors[1] = LE_16(&s->buf[stream_ptr]); |
| 265 | stream_ptr += 2; |
| 266 | |
| 267 | if (colors[0] & 0x8000) { |
| 268 | /* 8-color encoding */ |
| 269 | CHECK_STREAM_PTR(12); |
| 270 | colors[2] = LE_16(&s->buf[stream_ptr]); |
| 271 | stream_ptr += 2; |
| 272 | colors[3] = LE_16(&s->buf[stream_ptr]); |
| 273 | stream_ptr += 2; |
| 274 | colors[4] = LE_16(&s->buf[stream_ptr]); |
| 275 | stream_ptr += 2; |
| 276 | colors[5] = LE_16(&s->buf[stream_ptr]); |
| 277 | stream_ptr += 2; |
| 278 | colors[6] = LE_16(&s->buf[stream_ptr]); |
| 279 | stream_ptr += 2; |
| 280 | colors[7] = LE_16(&s->buf[stream_ptr]); |
| 281 | stream_ptr += 2; |
| 282 | |
| 283 | for (pixel_y = 0; pixel_y < 4; pixel_y++) { |
| 284 | for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1) |
| 285 | pixels[pixel_ptr++] = |
| 286 | colors[((pixel_y & 0x2) << 1) + |
| 287 | (pixel_x & 0x2) + ((flags & 0x1) ^ 1)]; |
| 288 | pixel_ptr -= row_dec; |
| 289 | } |
| 290 | } else { |
| 291 | /* 2-color encoding */ |
| 292 | for (pixel_y = 0; pixel_y < 4; pixel_y++) { |
| 293 | for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1) |
| 294 | pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1]; |
| 295 | pixel_ptr -= row_dec; |
| 296 | } |
| 297 | } |
| 298 | } else { |
| 299 | /* otherwise, it's a 1-color block */ |
| 300 | colors[0] = (byte_b << 8) | byte_a; |
| 301 | |
| 302 | for (pixel_y = 0; pixel_y < 4; pixel_y++) { |
| 303 | for (pixel_x = 0; pixel_x < 4; pixel_x++) |
| 304 | pixels[pixel_ptr++] = colors[0]; |
| 305 | pixel_ptr -= row_dec; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | block_ptr += block_inc; |
| 310 | total_blocks--; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | static int msvideo1_decode_frame(AVCodecContext *avctx, |
| 316 | void *data, int *data_size, |
| 317 | uint8_t *buf, int buf_size) |
| 318 | { |
| 319 | Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data; |
| 320 | |
| 321 | s->buf = buf; |
| 322 | s->size = buf_size; |
| 323 | |
| 324 | if (avctx->get_buffer(avctx, &s->frame)) { |
| 325 | printf (" MS Video-1 Video: get_buffer() failed\n"); |
| 326 | return -1; |
| 327 | } |
| 328 | |
Roberto Togni | 875efaf | 2003-11-02 21:57:55 | [diff] [blame^] | 329 | if (s->prev_frame.data[0] &&(s->frame.linesize[0] != s->prev_frame.linesize[0])) |
| 330 | printf (" MS Video-1: Buffer linesize changed: current %u, previous %u.\n" |
| 331 | " Expect wrong image and/or crash!\n", |
| 332 | s->frame.linesize[0], s->prev_frame.linesize[0]); |
| 333 | |
Mike Melanson | 2fdf638 | 2003-10-01 04:39:38 | [diff] [blame] | 334 | if (s->mode_8bit) |
| 335 | msvideo1_decode_8bit(s); |
| 336 | else |
| 337 | msvideo1_decode_16bit(s); |
| 338 | |
| 339 | if (s->prev_frame.data[0]) |
| 340 | avctx->release_buffer(avctx, &s->prev_frame); |
| 341 | |
| 342 | /* shuffle frames */ |
| 343 | s->prev_frame = s->frame; |
| 344 | |
| 345 | *data_size = sizeof(AVFrame); |
| 346 | *(AVFrame*)data = s->frame; |
| 347 | |
| 348 | /* report that the buffer was completely consumed */ |
| 349 | return buf_size; |
| 350 | } |
| 351 | |
| 352 | static int msvideo1_decode_end(AVCodecContext *avctx) |
| 353 | { |
| 354 | Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data; |
| 355 | |
| 356 | if (s->prev_frame.data[0]) |
| 357 | avctx->release_buffer(avctx, &s->prev_frame); |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | AVCodec msvideo1_decoder = { |
| 363 | "msvideo1", |
| 364 | CODEC_TYPE_VIDEO, |
| 365 | CODEC_ID_MSVIDEO1, |
| 366 | sizeof(Msvideo1Context), |
| 367 | msvideo1_decode_init, |
| 368 | NULL, |
| 369 | msvideo1_decode_end, |
| 370 | msvideo1_decode_frame, |
| 371 | CODEC_CAP_DR1, |
| 372 | }; |