[MPlayer-dev-eng] Open Source Video 1 Decoder
Mike Melanson
melanson at pcisys.net
Sun Nov 11 10:32:02 CET 2001
Hi there,
I'm pleased to contribute an open source decoder for
Microsoft Video 1 data. This is data that corresponds to the CRAM, WHAM,
and MSVC fourccs, and is normally handled by the msvidc32.dll codec. The
16-bit variant works just fine, but the 8-bit variant is still slightly
off. If anyone has some more 8-bit Video 1 files, or 16-bit ones that
don't work, send them my way.
Please note that this is my first attempt at making a patch
file. I think it should patch onto the current CVS version using
patch -p6 < video1.patches
And this is what my codecs.conf entry looks like:
;videocodec cram
videocodec video1
info "Microsoft Video 1"
status working
fourcc cram,CRAM
fourcc msvc,MSVC
fourcc wham,WHAM
driver video1
out BGR24
You'll probably want to move the decoder into a separate file so
it doesn't clutter up dec_video.c. And you may want to fix up the
little-endian reader/converters as well; I just used my own since I didn't
know what to use from the MPlayer codebase. Oh, and I was also lazy to the
point of only implementing BGR888 output data.
I was able to implement this by studying the XAnim source
code. For those who may be worried about code origin issues, I didn't
copy this code directly from XAnim. I reverse engineered a high-level
description of the algorithm that you can read here:
http://www.pcisys.net/~melanson/video1.txt
I wrote my implementation based on that description. I like to think that
my decoder is slightly more elegant than XAnim's.
Hopefully, this code will eliminate the need for at least one
binary-only codec. It's a start.
--
-Mike Melanson
-------------- next part --------------
--- ../../../ppc/mplayer-cvs/main/codec-cfg.h Tue Oct 23 08:01:13 2001
+++ codec-cfg.h Sat Nov 10 10:59:51 2001
@@ -42,6 +42,7 @@
#define VFM_RAW 8
#define VFM_RLE 9
#define VFM_XANIM 10
+#define VFM_VIDEO1 11
#ifndef GUID_TYPE
#define GUID_TYPE
--- ../../../ppc/mplayer-cvs/main/codec-cfg.c Sun Nov 4 07:00:53 2001
+++ codec-cfg.c Sat Nov 10 10:59:38 2001
@@ -223,6 +223,7 @@
"raw",
"rle",
"xanim",
+ "video1",
NULL
};
char **drv=audioflag?audiodrv:videodrv;
--- ../../../ppc/mplayer-cvs/main/dec_video.c Sat Nov 10 07:52:05 2001
+++ dec_video.c Sun Nov 11 02:15:12 2001
@@ -86,6 +86,23 @@
void AVI_Decode_RLE8(char *image,char *delta,int tdsize,
unsigned int *map,int imagex,int imagey,unsigned char x11_bytes_pixel);
+void AVI_Decode_Video1_16(
+ char *encoded,
+ int encoded_size,
+ char *decoded,
+ int width,
+ int height,
+ int bytes_per_pixel);
+
+void AVI_Decode_Video1_8(
+ char *encoded,
+ int encoded_size,
+ char *decoded,
+ int width,
+ int height,
+ unsigned char *palette_map,
+ int bytes_per_pixel);
+
//**************************************************************************//
// The OpenDivX stuff:
//**************************************************************************//
@@ -251,7 +268,7 @@
switch(sh_video->codec->driver){
case VFM_XANIM: {
#ifdef USE_XANIM
- int ret=xacodec_init_video(sh_video,out_fmt);
+ int ret=xacodec_init_video(sh_video,out_fmt);
if(!ret) return 0;
#else
// mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_NoXAnimSupport);
@@ -450,6 +467,12 @@
}
}
break;
+ case VFM_VIDEO1: {
+ int bpp=((out_fmt&255)+7)/8; // RGB only
+ sh_video->our_out_buffer =
+ (char*)memalign(64, sh_video->disp_w*sh_video->disp_h*bpp); // FIXME!!!
+ }
+ break;
}
}
sh_video->inited=1;
@@ -681,6 +704,19 @@
sh_video->disp_w,sh_video->disp_h,((out_fmt&255)+7)/8);
blit_frame=3;
break;
+ case VFM_VIDEO1:
+ if (sh_video->bih->biBitCount == 16)
+ AVI_Decode_Video1_16(
+ start, in_size, sh_video->our_out_buffer,
+ sh_video->disp_w, sh_video->disp_h,
+ ((out_fmt&255)+7)/8);
+ else
+ AVI_Decode_Video1_8(
+ start, in_size, sh_video->our_out_buffer,
+ sh_video->disp_w, sh_video->disp_h,
+ (char *)sh_video->bih+40, ((out_fmt&255)+7)/8);
+ blit_frame = 3;
+ break;
} // switch
//------------------------ frame decoded. --------------------
@@ -726,4 +762,355 @@
return blit_frame;
}
+
+#define LE_16(x) *(unsigned short *)(x)
+
+#define DECODE_BGR555_TO_BGR888(x) \
+ x.c1_b = (x.c1 >> 7) & 0xF8; \
+ x.c1_g = (x.c1 >> 2) & 0xF8; \
+ x.c1_r = (x.c1 << 3) & 0xF8; \
+ x.c2_b = (x.c2 >> 7) & 0xF8; \
+ x.c2_g = (x.c2 >> 2) & 0xF8; \
+ x.c2_r = (x.c2 << 3) & 0xF8;
+
+#define DECODE_PALETTE_TO_BGR888(x) \
+ x.c1_b = palette_map[x.c1 * 4 + 2]; \
+ x.c1_g = palette_map[x.c1 * 4 + 1]; \
+ x.c1_r = palette_map[x.c1 * 4 + 0]; \
+ x.c2_b = palette_map[x.c2 * 4 + 2]; \
+ x.c2_g = palette_map[x.c2 * 4 + 1]; \
+ x.c2_r = palette_map[x.c2 * 4 + 0];
+
+struct
+{
+ unsigned short c1, c2;
+ unsigned char c1_r, c1_g, c1_b;
+ unsigned char c2_r, c2_g, c2_b;
+} quad[2][2];
+
+void AVI_Decode_Video1_16(
+ char *encoded,
+ int encoded_size,
+ char *decoded,
+ int width,
+ int height,
+ int bytes_per_pixel)
+{
+ int block_ptr, pixel_ptr;
+ int pixel_x, pixel_y; // pixel width and height iterators
+ int block_x, block_y; // block width and height iterators
+ int blocks_wide, blocks_high; // width and height in 4x4 blocks
+ int block_inc;
+ int row_dec;
+
+ // decoding parameters
+ int stream_ptr;
+ unsigned char byte_a, byte_b;
+ unsigned short flags;
+ int skip_blocks;
+
+ stream_ptr = 0;
+ skip_blocks = 0;
+ blocks_wide = width / 4;
+ blocks_high = height / 4;
+ block_inc = 4 * bytes_per_pixel;
+ row_dec = (width + 4) * bytes_per_pixel;
+
+ for (block_y = blocks_high; block_y > 0; block_y--)
+ {
+ block_ptr = ((block_y * 4) - 1) * (width * bytes_per_pixel);
+ for (block_x = blocks_wide; block_x > 0; block_x--)
+ {
+ // check if this block should be skipped
+ if (skip_blocks)
+ {
+ block_ptr += block_inc;
+ skip_blocks--;
+ continue;
+ }
+
+ pixel_ptr = block_ptr;
+
+ // get the next two bytes in the encoded data stream
+ byte_a = encoded[stream_ptr++];
+ byte_b = encoded[stream_ptr++];
+
+ // check if the decode is finished
+ if ((byte_a == 0) && (byte_b == 0))
+ return;
+
+ // check if this is a skip code
+ else if ((byte_b & 0xFC) == 0x84)
+ {
+ // but don't count the current block
+ skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
+ }
+
+ // check if this is in the 2- or 8-color classes
+ else if (byte_b < 0x80)
+ {
+ flags = (byte_b << 8) | byte_a;
+
+ quad[0][0].c1 = LE_16(&encoded[stream_ptr]);
+ stream_ptr += 2;
+ quad[0][0].c2 = LE_16(&encoded[stream_ptr]);
+ stream_ptr += 2;
+
+ DECODE_BGR555_TO_BGR888(quad[0][0]);
+
+ if (quad[0][0].c1 & 0x8000)
+ {
+ // 8-color encoding
+ quad[1][0].c1 = LE_16(&encoded[stream_ptr]);
+ stream_ptr += 2;
+ quad[1][0].c2 = LE_16(&encoded[stream_ptr]);
+ stream_ptr += 2;
+ quad[0][1].c1 = LE_16(&encoded[stream_ptr]);
+ stream_ptr += 2;
+ quad[0][1].c2 = LE_16(&encoded[stream_ptr]);
+ stream_ptr += 2;
+ quad[1][1].c1 = LE_16(&encoded[stream_ptr]);
+ stream_ptr += 2;
+ quad[1][1].c2 = LE_16(&encoded[stream_ptr]);
+ stream_ptr += 2;
+
+ DECODE_BGR555_TO_BGR888(quad[0][1]);
+ DECODE_BGR555_TO_BGR888(quad[1][0]);
+ DECODE_BGR555_TO_BGR888(quad[1][1]);
+
+ for (pixel_y = 0; pixel_y < 4; pixel_y++)
+ {
+ for (pixel_x = 0; pixel_x < 4; pixel_x++)
+ {
+ if (flags & 1)
+ {
+ decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_r;
+ decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_g;
+ decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_b;
+ }
+ else
+ {
+ decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_r;
+ decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_g;
+ decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_b;
+ }
+
+ // get the next flag ready to go
+ flags >>= 1;
+ }
+ pixel_ptr -= row_dec;
+ }
+ }
+ else
+ {
+ // 2-color encoding
+ for (pixel_y = 0; pixel_y < 4; pixel_y++)
+ {
+ for (pixel_x = 0; pixel_x < 4; pixel_x++)
+ {
+ if (flags & 1)
+ {
+ decoded[pixel_ptr++] = quad[0][0].c1_r;
+ decoded[pixel_ptr++] = quad[0][0].c1_g;
+ decoded[pixel_ptr++] = quad[0][0].c1_b;
+ }
+ else
+ {
+ decoded[pixel_ptr++] = quad[0][0].c2_r;
+ decoded[pixel_ptr++] = quad[0][0].c2_g;
+ decoded[pixel_ptr++] = quad[0][0].c2_b;
+ }
+
+ // get the next flag ready to go
+ flags >>= 1;
+ }
+ pixel_ptr -= row_dec;
+ }
+ }
+ }
+
+ // otherwise, it's a 1-color block
+ else
+ {
+ quad[0][0].c1 = (byte_b << 8) | byte_a;
+ DECODE_BGR555_TO_BGR888(quad[0][0]);
+
+ for (pixel_y = 0; pixel_y < 4; pixel_y++)
+ {
+ for (pixel_x = 0; pixel_x < 4; pixel_x++)
+ {
+ decoded[pixel_ptr++] = quad[0][0].c1_r;
+ decoded[pixel_ptr++] = quad[0][0].c1_g;
+ decoded[pixel_ptr++] = quad[0][0].c1_b;
+ }
+ pixel_ptr -= row_dec;
+ }
+ }
+
+ block_ptr += block_inc;
+ }
+ }
+}
+
+void AVI_Decode_Video1_8(
+ char *encoded,
+ int encoded_size,
+ char *decoded,
+ int width,
+ int height,
+ unsigned char *palette_map,
+ int bytes_per_pixel)
+{
+ int block_ptr, pixel_ptr;
+ int pixel_x, pixel_y; // pixel width and height iterators
+ int block_x, block_y; // block width and height iterators
+ int blocks_wide, blocks_high; // width and height in 4x4 blocks
+ int block_inc;
+ int row_dec;
+
+ // decoding parameters
+ int stream_ptr;
+ unsigned char byte_a, byte_b;
+ unsigned short flags;
+ int skip_blocks;
+
+ stream_ptr = 0;
+ skip_blocks = 0;
+ blocks_wide = width / 4;
+ blocks_high = height / 4;
+ block_inc = 4 * bytes_per_pixel;
+ row_dec = (width + 4) * bytes_per_pixel;
+
+ for (block_y = blocks_high; block_y > 0; block_y--)
+ {
+ block_ptr = ((block_y * 4) - 1) * (width * bytes_per_pixel);
+ for (block_x = blocks_wide; block_x > 0; block_x--)
+ {
+ // check if this block should be skipped
+ if (skip_blocks)
+ {
+ block_ptr += block_inc;
+ skip_blocks--;
+ continue;
+ }
+
+ pixel_ptr = block_ptr;
+
+ // get the next two bytes in the encoded data stream
+ byte_a = encoded[stream_ptr++];
+ byte_b = encoded[stream_ptr++];
+
+ // check if the decode is finished
+ if ((byte_a == 0) && (byte_b == 0))
+ return;
+
+ // check if this is a skip code
+ else if ((byte_b & 0xFC) == 0x84)
+ {
+ // but don't count the current block
+ skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
+ }
+
+ // check if this is a 2-color block
+ else if (byte_b < 0x80)
+ {
+ flags = (byte_b << 8) | byte_a;
+
+ quad[0][0].c1 = (unsigned char)encoded[stream_ptr++];
+ quad[0][0].c2 = (unsigned char)encoded[stream_ptr++];
+
+ DECODE_PALETTE_TO_BGR888(quad[0][0]);
+
+ // 2-color encoding
+ for (pixel_y = 0; pixel_y < 4; pixel_y++)
+ {
+ for (pixel_x = 0; pixel_x < 4; pixel_x++)
+ {
+ if (flags & 1)
+ {
+ decoded[pixel_ptr++] = quad[0][0].c1_r;
+ decoded[pixel_ptr++] = quad[0][0].c1_g;
+ decoded[pixel_ptr++] = quad[0][0].c1_b;
+ }
+ else
+ {
+ decoded[pixel_ptr++] = quad[0][0].c2_r;
+ decoded[pixel_ptr++] = quad[0][0].c2_g;
+ decoded[pixel_ptr++] = quad[0][0].c2_b;
+ }
+
+ // get the next flag ready to go
+ flags >>= 1;
+ }
+ pixel_ptr -= row_dec;
+ }
+ }
+
+ // check if it's an 8-color block
+ else if (byte_b >= 0x90)
+ {
+ // 8-color encoding
+ quad[0][0].c1 = (unsigned char)encoded[stream_ptr++];
+ quad[0][0].c2 = (unsigned char)encoded[stream_ptr++];
+ quad[1][0].c1 = (unsigned char)encoded[stream_ptr++];
+ quad[1][0].c2 = (unsigned char)encoded[stream_ptr++];
+
+ quad[0][1].c1 = (unsigned char)encoded[stream_ptr++];
+ quad[0][1].c2 = (unsigned char)encoded[stream_ptr++];
+ quad[1][1].c1 = (unsigned char)encoded[stream_ptr++];
+ quad[1][1].c2 = (unsigned char)encoded[stream_ptr++];
+
+ DECODE_PALETTE_TO_BGR888(quad[0][0]);
+ DECODE_PALETTE_TO_BGR888(quad[0][1]);
+ DECODE_PALETTE_TO_BGR888(quad[1][0]);
+ DECODE_PALETTE_TO_BGR888(quad[1][1]);
+
+ for (pixel_y = 0; pixel_y < 4; pixel_y++)
+ {
+ for (pixel_x = 0; pixel_x < 4; pixel_x++)
+ {
+ if (flags & 1)
+ {
+ decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_r;
+ decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_g;
+ decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_b;
+ }
+ else
+ {
+ decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_r;
+ decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_g;
+ decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_b;
+ }
+
+ // get the next flag ready to go
+ flags >>= 1;
+ }
+ pixel_ptr -= row_dec;
+ }
+ }
+
+ // otherwise, it's a 1-color block
+ else
+ {
+ // init c2 along with c1 just so c2 is a known value for macro
+ quad[0][0].c1 = quad[0][0].c2 = byte_a;
+ DECODE_PALETTE_TO_BGR888(quad[0][0]);
+
+ for (pixel_y = 0; pixel_y < 4; pixel_y++)
+ {
+ for (pixel_x = 0; pixel_x < 4; pixel_x++)
+ {
+ decoded[pixel_ptr++] = quad[0][0].c1_r;
+ decoded[pixel_ptr++] = quad[0][0].c1_g;
+ decoded[pixel_ptr++] = quad[0][0].c1_b;
+ }
+ pixel_ptr -= row_dec;
+ }
+ }
+
+ block_ptr += block_inc;
+ }
+ }
+}
+
More information about the MPlayer-dev-eng
mailing list