00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <unistd.h>
00036
00037 #include "common.h"
00038 #include "avcodec.h"
00039 #include "dsputil.h"
00040
00041 #define PALETTE_COUNT 256
00042 #define CHECK_STREAM_PTR(n) \
00043 if ((stream_ptr + n) > s->size ) { \
00044 av_log(s->avctx, AV_LOG_ERROR, " MS Video-1 warning: stream_ptr out of bounds (%d >= %d)\n", \
00045 stream_ptr + n, s->size); \
00046 return; \
00047 }
00048
00049 typedef struct Msvideo1Context {
00050
00051 AVCodecContext *avctx;
00052 DSPContext dsp;
00053 AVFrame frame;
00054
00055 unsigned char *buf;
00056 int size;
00057
00058 int mode_8bit;
00059
00060 } Msvideo1Context;
00061
00062 static int msvideo1_decode_init(AVCodecContext *avctx)
00063 {
00064 Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
00065
00066 s->avctx = avctx;
00067
00068
00069 if (s->avctx->palctrl) {
00070 s->mode_8bit = 1;
00071 avctx->pix_fmt = PIX_FMT_PAL8;
00072 } else {
00073 s->mode_8bit = 0;
00074 avctx->pix_fmt = PIX_FMT_RGB555;
00075 }
00076
00077 avctx->has_b_frames = 0;
00078 dsputil_init(&s->dsp, avctx);
00079
00080 s->frame.data[0] = NULL;
00081
00082 return 0;
00083 }
00084
00085 static void msvideo1_decode_8bit(Msvideo1Context *s)
00086 {
00087 int block_ptr, pixel_ptr;
00088 int total_blocks;
00089 int pixel_x, pixel_y;
00090 int block_x, block_y;
00091 int blocks_wide, blocks_high;
00092 int block_inc;
00093 int row_dec;
00094
00095
00096 int stream_ptr;
00097 unsigned char byte_a, byte_b;
00098 unsigned short flags;
00099 int skip_blocks;
00100 unsigned char colors[8];
00101 unsigned char *pixels = s->frame.data[0];
00102 int stride = s->frame.linesize[0];
00103
00104 stream_ptr = 0;
00105 skip_blocks = 0;
00106 blocks_wide = s->avctx->width / 4;
00107 blocks_high = s->avctx->height / 4;
00108 total_blocks = blocks_wide * blocks_high;
00109 block_inc = 4;
00110 row_dec = stride + 4;
00111
00112 for (block_y = blocks_high; block_y > 0; block_y--) {
00113 block_ptr = ((block_y * 4) - 1) * stride;
00114 for (block_x = blocks_wide; block_x > 0; block_x--) {
00115
00116 if (skip_blocks) {
00117 block_ptr += block_inc;
00118 skip_blocks--;
00119 total_blocks--;
00120 continue;
00121 }
00122
00123 pixel_ptr = block_ptr;
00124
00125
00126 CHECK_STREAM_PTR(2);
00127 byte_a = s->buf[stream_ptr++];
00128 byte_b = s->buf[stream_ptr++];
00129
00130
00131 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
00132 return;
00133 else if ((byte_b & 0xFC) == 0x84) {
00134
00135 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
00136 } else if (byte_b < 0x80) {
00137
00138 flags = (byte_b << 8) | byte_a;
00139
00140 CHECK_STREAM_PTR(2);
00141 colors[0] = s->buf[stream_ptr++];
00142 colors[1] = s->buf[stream_ptr++];
00143
00144 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00145 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
00146 pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
00147 pixel_ptr -= row_dec;
00148 }
00149 } else if (byte_b >= 0x90) {
00150
00151 flags = (byte_b << 8) | byte_a;
00152
00153 CHECK_STREAM_PTR(8);
00154 memcpy(colors, &s->buf[stream_ptr], 8);
00155 stream_ptr += 8;
00156
00157 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00158 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
00159 pixels[pixel_ptr++] =
00160 colors[((pixel_y & 0x2) << 1) +
00161 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
00162 pixel_ptr -= row_dec;
00163 }
00164 } else {
00165
00166 colors[0] = byte_a;
00167
00168 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00169 for (pixel_x = 0; pixel_x < 4; pixel_x++)
00170 pixels[pixel_ptr++] = colors[0];
00171 pixel_ptr -= row_dec;
00172 }
00173 }
00174
00175 block_ptr += block_inc;
00176 total_blocks--;
00177 }
00178 }
00179
00180
00181 if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
00182 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
00183 if (s->avctx->palctrl->palette_changed) {
00184 s->frame.palette_has_changed = 1;
00185 s->avctx->palctrl->palette_changed = 0;
00186 }
00187 }
00188 }
00189
00190 static void msvideo1_decode_16bit(Msvideo1Context *s)
00191 {
00192 int block_ptr, pixel_ptr;
00193 int total_blocks;
00194 int pixel_x, pixel_y;
00195 int block_x, block_y;
00196 int blocks_wide, blocks_high;
00197 int block_inc;
00198 int row_dec;
00199
00200
00201 int stream_ptr;
00202 unsigned char byte_a, byte_b;
00203 unsigned short flags;
00204 int skip_blocks;
00205 unsigned short colors[8];
00206 unsigned short *pixels = (unsigned short *)s->frame.data[0];
00207 int stride = s->frame.linesize[0] / 2;
00208
00209 stream_ptr = 0;
00210 skip_blocks = 0;
00211 blocks_wide = s->avctx->width / 4;
00212 blocks_high = s->avctx->height / 4;
00213 total_blocks = blocks_wide * blocks_high;
00214 block_inc = 4;
00215 row_dec = stride + 4;
00216
00217 for (block_y = blocks_high; block_y > 0; block_y--) {
00218 block_ptr = ((block_y * 4) - 1) * stride;
00219 for (block_x = blocks_wide; block_x > 0; block_x--) {
00220
00221 if (skip_blocks) {
00222 block_ptr += block_inc;
00223 skip_blocks--;
00224 total_blocks--;
00225 continue;
00226 }
00227
00228 pixel_ptr = block_ptr;
00229
00230
00231 CHECK_STREAM_PTR(2);
00232 byte_a = s->buf[stream_ptr++];
00233 byte_b = s->buf[stream_ptr++];
00234
00235
00236 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
00237 return;
00238 } else if ((byte_b & 0xFC) == 0x84) {
00239
00240 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
00241 } else if (byte_b < 0x80) {
00242
00243 flags = (byte_b << 8) | byte_a;
00244
00245 CHECK_STREAM_PTR(4);
00246 colors[0] = LE_16(&s->buf[stream_ptr]);
00247 stream_ptr += 2;
00248 colors[1] = LE_16(&s->buf[stream_ptr]);
00249 stream_ptr += 2;
00250
00251 if (colors[0] & 0x8000) {
00252
00253 CHECK_STREAM_PTR(12);
00254 colors[2] = LE_16(&s->buf[stream_ptr]);
00255 stream_ptr += 2;
00256 colors[3] = LE_16(&s->buf[stream_ptr]);
00257 stream_ptr += 2;
00258 colors[4] = LE_16(&s->buf[stream_ptr]);
00259 stream_ptr += 2;
00260 colors[5] = LE_16(&s->buf[stream_ptr]);
00261 stream_ptr += 2;
00262 colors[6] = LE_16(&s->buf[stream_ptr]);
00263 stream_ptr += 2;
00264 colors[7] = LE_16(&s->buf[stream_ptr]);
00265 stream_ptr += 2;
00266
00267 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00268 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
00269 pixels[pixel_ptr++] =
00270 colors[((pixel_y & 0x2) << 1) +
00271 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
00272 pixel_ptr -= row_dec;
00273 }
00274 } else {
00275
00276 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00277 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
00278 pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
00279 pixel_ptr -= row_dec;
00280 }
00281 }
00282 } else {
00283
00284 colors[0] = (byte_b << 8) | byte_a;
00285
00286 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00287 for (pixel_x = 0; pixel_x < 4; pixel_x++)
00288 pixels[pixel_ptr++] = colors[0];
00289 pixel_ptr -= row_dec;
00290 }
00291 }
00292
00293 block_ptr += block_inc;
00294 total_blocks--;
00295 }
00296 }
00297 }
00298
00299 static int msvideo1_decode_frame(AVCodecContext *avctx,
00300 void *data, int *data_size,
00301 uint8_t *buf, int buf_size)
00302 {
00303 Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
00304
00305 s->buf = buf;
00306 s->size = buf_size;
00307
00308 s->frame.reference = 1;
00309 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00310 if (avctx->reget_buffer(avctx, &s->frame)) {
00311 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00312 return -1;
00313 }
00314
00315 if (s->mode_8bit)
00316 msvideo1_decode_8bit(s);
00317 else
00318 msvideo1_decode_16bit(s);
00319
00320 *data_size = sizeof(AVFrame);
00321 *(AVFrame*)data = s->frame;
00322
00323
00324 return buf_size;
00325 }
00326
00327 static int msvideo1_decode_end(AVCodecContext *avctx)
00328 {
00329 Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
00330
00331 if (s->frame.data[0])
00332 avctx->release_buffer(avctx, &s->frame);
00333
00334 return 0;
00335 }
00336
00337 AVCodec msvideo1_decoder = {
00338 "msvideo1",
00339 CODEC_TYPE_VIDEO,
00340 CODEC_ID_MSVIDEO1,
00341 sizeof(Msvideo1Context),
00342 msvideo1_decode_init,
00343 NULL,
00344 msvideo1_decode_end,
00345 msvideo1_decode_frame,
00346 CODEC_CAP_DR1,
00347 };