diff options
author | Ewald Snel <esnel@users.sourceforge.net> | 2002-04-30 08:28:09 +0000 |
---|---|---|
committer | Ewald Snel <esnel@users.sourceforge.net> | 2002-04-30 08:28:09 +0000 |
commit | d89b9f666c9a13affba7c24c4d82833947abeda0 (patch) | |
tree | 807e61cee37b30ba212b8bf049931c59b682f4cb | |
parent | 1b568a8bfd7e9d68f14296fc68d38cde45c3fd49 (diff) | |
download | xine-lib-d89b9f666c9a13affba7c24c4d82833947abeda0.tar.gz xine-lib-d89b9f666c9a13affba7c24c4d82833947abeda0.tar.bz2 |
Add libxinevdec video decoders
- Cinepak decoder moved into this directory
- New Creative YUV (CYUV) decoder by Mike Melanson
- New Microsoft Video-1 (msvc) decoder by Ewald Snel
CVS patchset: 1813
CVS date: 2002/04/30 08:28:09
-rw-r--r-- | configure.in | 5 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/libxinevdec/Makefile.am | 32 | ||||
-rw-r--r-- | src/libxinevdec/cinepak.c | 451 | ||||
-rw-r--r-- | src/libxinevdec/cyuv.c | 253 | ||||
-rw-r--r-- | src/libxinevdec/msvc.c | 361 |
6 files changed, 1101 insertions, 3 deletions
diff --git a/configure.in b/configure.in index f02f64afd..166e0b1df 100644 --- a/configure.in +++ b/configure.in @@ -949,7 +949,7 @@ src/libspucc/Makefile src/libvorbis/Makefile src/libxvid/Makefile src/libdivx4/Makefile -src/libcinepak/Makefile +src/libxinevdec/Makefile src/xine-utils/Makefile src/input/Makefile src/xine-engine/Makefile @@ -1068,7 +1068,8 @@ echo " - a52 - dts" echo " - mpeg2 - spu" echo " - ffmpeg - mad" echo " - vfill - divx4" -echo " - cinepak" +echo " - cinepak - msvc" +echo " - cyuv" if test x"$no_oggvorbis" = "xno"; then echo " - vorbis" fi diff --git a/src/Makefile.am b/src/Makefile.am index cbf57e019..a6df395a7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,7 +1,7 @@ SUBDIRS = xine-utils xine-engine audio_out video_out dxr3 input libmpeg2 libspudec demuxers \ liba52 libffmpeg liblpcm libw32dll libmad libdts \ - libvorbis libdivx4 libsputext libspucc libxvid libcinepak + libvorbis libdivx4 libsputext libspucc libxvid libxinevdec debug: @list='$(SUBDIRS)'; for subdir in $$list; do \ diff --git a/src/libxinevdec/Makefile.am b/src/libxinevdec/Makefile.am new file mode 100644 index 000000000..76a489dbe --- /dev/null +++ b/src/libxinevdec/Makefile.am @@ -0,0 +1,32 @@ +CFLAGS = @CFLAGS@ +DEBUG_CFLAGS = @DEBUG_CFLAGS@ + +LIBTOOL = $(SHELL) $(top_builddir)/libtool-nofpic + +libdir = $(XINE_PLUGINDIR) + +lib_LTLIBRARIES = xineplug_decode_cinepak.la xineplug_decode_cyuv.la \ + xineplug_decode_msvc.la + +xineplug_decode_cinepak_la_SOURCES = cinepak.c +xineplug_decode_cinepak_la_LDFLAGS = -avoid-version -module + +xineplug_decode_cyuv_la_SOURCES = cyuv.c +xineplug_decode_cyuv_la_LDFLAGS = -avoid-version -module + +xineplug_decode_msvc_la_SOURCES = msvc.c +xineplug_decode_msvc_la_LDFLAGS = -avoid-version -module + +debug: + @$(MAKE) CFLAGS="$(DEBUG_CFLAGS)" + +install-debug: debug + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +mostlyclean-generic: + -rm -f *~ \#* .*~ .\#* + +maintainer-clean-generic: + -@echo "This command is intended for maintainers to use;" + -@echo "it deletes files that may require special tools to rebuild." + -rm -f Makefile.in diff --git a/src/libxinevdec/cinepak.c b/src/libxinevdec/cinepak.c new file mode 100644 index 000000000..d472ef4d6 --- /dev/null +++ b/src/libxinevdec/cinepak.c @@ -0,0 +1,451 @@ +/* + * Copyright (C) 2002 the xine project + * + * This file is part of xine, a unix video player. + * + * xine is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * xine is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * by Ewald Snel <ewald@rambo.its.tudelft.nl> + * + * based on overview of Cinepak algorithm and example decoder + * by Tim Ferguson: http://www.csse.monash.edu.au/~timf/ + * + * $Id: cinepak.c,v 1.1 2002/04/30 08:28:10 esnel Exp $ + */ + +#include <stdlib.h> +#include <unistd.h> + +#include "video_out.h" +#include "buffer.h" +#include "bswap.h" +#include "xineutils.h" +#include "xine_internal.h" + +#define MAX_STRIPS 32 +#define VIDEOBUFSIZE 128 * 1024 + +/* now this is ripped of wine's vfw.h */ +typedef struct { + long biSize; + long biWidth; + long biHeight; + short biPlanes; + short biBitCount; + long biCompression; + long biSizeImage; + long biXPelsPerMeter; + long biYPelsPerMeter; + long biClrUsed; + long biClrImportant; +} BITMAPINFOHEADER; + +typedef struct { + uint8_t y0, y1, y2, y3; + uint8_t u, v; +} cvid_codebook_t; + +typedef struct { + uint16_t id; + uint16_t x1, y1; + uint16_t x2, y2; + cvid_codebook_t v4_codebook[256]; + cvid_codebook_t v1_codebook[256]; +} cvid_strip_t; + +typedef struct cvid_decoder_s { + video_decoder_t video_decoder; + + vo_instance_t *video_out; + int64_t video_step; + int decoder_ok; + + unsigned char *buf; + int bufsize; + int size; + + long biWidth; + long biHeight; + short biBitCount; + uint8_t *img_buffer; + + cvid_strip_t strips[MAX_STRIPS]; +} cvid_decoder_t; + + +static void cinepak_decode_codebook (cvid_codebook_t *codebook, + int chunk_id, int size, uint8_t *data) +{ + uint8_t *eod = (data + size); + uint32_t flag, mask; + int i, n; + + n = (chunk_id & 0x0400) ? 4 : 6; + flag = 0; + mask = 0; + + for (i=0; i < 256; i++) { + if ((chunk_id & 0x0100) && !(mask >>= 1)) { + if ((data + 4) > eod) + break; + + flag = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; + data += 4; + mask = 0x80000000; + } + + if (!(chunk_id & 0x0100) || (flag & mask)) { + if ((data + n) > eod) + break; + + codebook[i].y0 = *data++; + codebook[i].y1 = *data++; + codebook[i].y2 = *data++; + codebook[i].y3 = *data++; + codebook[i].u = 128 + ((n == 4) ? 0 : *data++); + codebook[i].v = 128 + ((n == 4) ? 0 : *data++); + } + } +} + +static void cinepak_decode_vectors (cvid_decoder_t *this, cvid_strip_t *strip, + int chunk_id, int size, uint8_t *data) +{ + uint8_t *eod = (data + size); + uint32_t flag, mask; + cvid_codebook_t *codebook; + int x, y; + int n = (this->biWidth * this->biHeight); + uint8_t *iy[4]; + uint8_t *iu[2]; + uint8_t *iv[2]; + + flag = 0; + mask = 0; + + for (y=strip->y1; y < strip->y2; y+=4) { + + iy[0] = this->img_buffer + (y * this->biWidth) + strip->x1; + iy[1] = iy[0] + this->biWidth; + iy[2] = iy[1] + this->biWidth; + iy[3] = iy[2] + this->biWidth; + iu[0] = this->img_buffer + n + ((y * this->biWidth) >> 2) + (strip->x1 >> 1); + iu[1] = iu[0] + (this->biWidth >> 1); + iv[0] = iu[0] + (n >> 2); + iv[1] = iv[0] + (this->biWidth >> 1); + + for (x=strip->x1; x < strip->x2; x+=4) { + if ((chunk_id & 0x0100) && !(mask >>= 1)) { + if ((data + 4) > eod) + return; + + flag = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; + data += 4; + mask = 0x80000000; + } + + if (!(chunk_id & 0x0100) || (flag & mask)) { + if (!(chunk_id & 0x0200) && !(mask >>= 1)) { + if ((data + 4) > eod) + return; + + flag = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; + data += 4; + mask = 0x80000000; + } + + if ((chunk_id & 0x0200) || (~flag & mask)) { + if (data >= eod) + return; + + codebook = &strip->v1_codebook[*data++]; + iy[0][0] = codebook->y0; iy[0][1] = codebook->y0; + iy[1][0] = codebook->y0; iy[1][1] = codebook->y0; + iu[0][0] = codebook->u; iv[0][0] = codebook->v; + + iy[0][2] = codebook->y1; iy[0][3] = codebook->y1; + iy[1][2] = codebook->y1; iy[1][3] = codebook->y1; + iu[0][1] = codebook->u; iv[0][1] = codebook->v; + + iy[2][0] = codebook->y2; iy[2][1] = codebook->y2; + iy[3][0] = codebook->y2; iy[3][1] = codebook->y2; + iu[1][0] = codebook->u; iv[1][0] = codebook->v; + + iy[2][2] = codebook->y3; iy[2][3] = codebook->y3; + iy[3][2] = codebook->y3; iy[3][3] = codebook->y3; + iu[1][1] = codebook->u; iv[1][1] = codebook->v; + + } else if (flag & mask) { + if ((data + 4) > eod) + return; + + codebook = &strip->v4_codebook[*data++]; + iy[0][0] = codebook->y0; iy[0][1] = codebook->y1; + iy[1][0] = codebook->y2; iy[1][1] = codebook->y3; + iu[0][0] = codebook->u; iv[0][0] = codebook->v; + + codebook = &strip->v4_codebook[*data++]; + iy[0][2] = codebook->y0; iy[0][3] = codebook->y1; + iy[1][2] = codebook->y2; iy[1][3] = codebook->y3; + iu[0][1] = codebook->u; iv[0][1] = codebook->v; + + codebook = &strip->v4_codebook[*data++]; + iy[2][0] = codebook->y0; iy[2][1] = codebook->y1; + iy[3][0] = codebook->y2; iy[3][1] = codebook->y3; + iu[1][0] = codebook->u; iv[1][0] = codebook->v; + + codebook = &strip->v4_codebook[*data++]; + iy[2][2] = codebook->y0; iy[2][3] = codebook->y1; + iy[3][2] = codebook->y2; iy[3][3] = codebook->y3; + iu[1][1] = codebook->u; iv[1][1] = codebook->v; + } + } + + iy[0] += 4; iy[1] += 4; + iy[2] += 4; iy[3] += 4; + iu[0] += 2; iu[1] += 2; + iv[0] += 2; iv[1] += 2; + } + } +} + +static void cinepak_decode_strip (cvid_decoder_t *this, + cvid_strip_t *strip, uint8_t *data, int size) +{ + uint8_t *eod = (data + size); + int chunk_id, chunk_size; + + if (strip->x1 >= this->biWidth || strip->x2 > this->biWidth || + strip->y1 >= this->biHeight || strip->y2 > this->biHeight || + strip->x1 >= strip->x2 || strip->y1 >= strip->y2) + return; + + while ((data + 4) <= eod) { + chunk_id = (data[0] << 8) | data[1]; + chunk_size = ((data[2] << 8) | data[3]) - 4; + data += 4; + chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size; + + switch (chunk_id) { + case 0x2000: + case 0x2100: + case 0x2400: + case 0x2500: + cinepak_decode_codebook (strip->v4_codebook, chunk_id, chunk_size, data); + break; + case 0x2200: + case 0x2300: + case 0x2600: + case 0x2700: + cinepak_decode_codebook (strip->v1_codebook, chunk_id, chunk_size, data); + break; + case 0x3000: + case 0x3100: + case 0x3200: + cinepak_decode_vectors (this, strip, chunk_id, chunk_size, data); + return; + } + + data += chunk_size; + } +} + +static void cinepak_decode_frame (cvid_decoder_t *this, uint8_t *data, int size) { + uint8_t *eod = (data + size); + int i, strip_size, frame_flags, num_strips; + int y0 = 0; + + if (size < 10) + return; + + frame_flags = data[0]; + num_strips = (data[8] << 8) | data[9]; + data += 10; + + if (num_strips > MAX_STRIPS) + num_strips = MAX_STRIPS; + + for (i=0; i < num_strips; i++) { + if ((data + 12) > eod) + break; + + this->strips[i].id = (data[0] << 8) | data[1]; + this->strips[i].y1 = y0; + this->strips[i].x1 = 0; + this->strips[i].y2 = y0 + ((data[8] << 8) | data[9]); + this->strips[i].x2 = this->biWidth; + + strip_size = (data[2] << 8) + data[3] - 12; + data += 12; + strip_size = ((data + strip_size) > eod) ? (eod - data) : strip_size; + + if ((i > 0) && !(frame_flags & 0x01)) { + xine_fast_memcpy (this->strips[i].v4_codebook, this->strips[i-1].v4_codebook, + sizeof(this->strips[i].v4_codebook)); + + xine_fast_memcpy (this->strips[i].v1_codebook, this->strips[i-1].v1_codebook, + sizeof(this->strips[i].v1_codebook)); + } + + cinepak_decode_strip (this, &this->strips[i], data, strip_size); + + data += strip_size; + y0 = this->strips[i].y2; + } +} + +static int cvid_can_handle (video_decoder_t *this_gen, int buf_type) { + return ((buf_type & 0xFFFF0000) == BUF_VIDEO_CINEPAK); +} + +static void cvid_init (video_decoder_t *this_gen, vo_instance_t *video_out) { + cvid_decoder_t *this = (cvid_decoder_t *) this_gen; + + this->video_out = video_out; + this->decoder_ok = 0; +} + +static void cvid_decode_data (video_decoder_t *this_gen, buf_element_t *buf) { + cvid_decoder_t *this = (cvid_decoder_t *) this_gen; + + if (buf->decoder_flags & BUF_FLAG_PREVIEW) + return; + + if (buf->decoder_flags & BUF_FLAG_HEADER) { + BITMAPINFOHEADER *bih; + + bih = (BITMAPINFOHEADER *) buf->content; + this->biWidth = (le2me_32 (bih->biWidth) + 3) & ~0x03; + this->biHeight = (le2me_32 (bih->biHeight) + 3) & ~0x03; + this->video_step = buf->decoder_info[1]; + + if (this->img_buffer) + free (this->img_buffer); + this->img_buffer = malloc((this->biWidth * this->biHeight * 3) >> 1); + + if (this->buf) + free (this->buf); + this->bufsize = VIDEOBUFSIZE; + this->buf = malloc(this->bufsize); + this->size = 0; + + this->video_out->open (this->video_out); + this->decoder_ok = 1; + + } else if (this->decoder_ok) { + + if (this->size + buf->size > this->bufsize) { + this->bufsize = this->size + 2 * buf->size; + this->buf = realloc (this->buf, this->bufsize); + } + + xine_fast_memcpy (&this->buf[this->size], buf->content, buf->size); + + this->size += buf->size; + + if (buf->decoder_flags & BUF_FLAG_FRAMERATE) + this->video_step = buf->decoder_info[0]; + + if (buf->decoder_flags & BUF_FLAG_FRAME_END) { + + vo_frame_t *img; + int n = (this->biWidth * this->biHeight); + + cinepak_decode_frame (this, this->buf, this->size); + + img = this->video_out->get_frame (this->video_out, + this->biWidth, this->biHeight, + 42, IMGFMT_YV12, VO_BOTH_FIELDS); + + img->duration = this->video_step; + img->pts = buf->pts; + img->bad_frame = 0; + + xine_fast_memcpy (img->base[0], this->img_buffer, n); + xine_fast_memcpy (img->base[1], this->img_buffer + n, (n >> 2)); + xine_fast_memcpy (img->base[2], this->img_buffer + n + (n >> 2), (n >> 2)); + + img->draw(img); + img->free(img); + + this->size = 0; + } + } +} + +static void cvid_flush (video_decoder_t *this_gen) { +} + +static void cvid_reset (video_decoder_t *this_gen) { + cvid_decoder_t *this = (cvid_decoder_t *) this_gen; + + this->size = 0; +} + +static void cvid_close (video_decoder_t *this_gen) { + cvid_decoder_t *this = (cvid_decoder_t *) this_gen; + + if (this->img_buffer) { + free (this->img_buffer); + this->img_buffer = NULL; + } + + if (this->buf) { + free (this->buf); + this->buf = NULL; + } + + if (this->decoder_ok) { + this->decoder_ok = 0; + this->video_out->close(this->video_out); + } +} + +static char *cvid_get_id(void) { + return "cinepak"; +} + +static void cvid_dispose (video_decoder_t *this_gen) { + free (this_gen); +} + +video_decoder_t *init_video_decoder_plugin (int iface_version, xine_t *xine) { + + cvid_decoder_t *this ; + + if (iface_version != 7) { + printf( "cinepak: plugin doesn't support plugin API version %d.\n" + "cinepak: this means there's a version mismatch between xine and this " + "cinepak: decoder plugin.\nInstalling current plugins should help.\n", + iface_version); + return NULL; + } + + this = (cvid_decoder_t *) malloc (sizeof (cvid_decoder_t)); + memset(this, 0, sizeof (cvid_decoder_t)); + + this->video_decoder.interface_version = iface_version; + this->video_decoder.can_handle = cvid_can_handle; + this->video_decoder.init = cvid_init; + this->video_decoder.decode_data = cvid_decode_data; + this->video_decoder.flush = cvid_flush; + this->video_decoder.reset = cvid_reset; + this->video_decoder.close = cvid_close; + this->video_decoder.get_identifier = cvid_get_id; + this->video_decoder.dispose = cvid_dispose; + this->video_decoder.priority = 5; + + return (video_decoder_t *) this; +} diff --git a/src/libxinevdec/cyuv.c b/src/libxinevdec/cyuv.c new file mode 100644 index 000000000..fd54686e1 --- /dev/null +++ b/src/libxinevdec/cyuv.c @@ -0,0 +1,253 @@ +/* This is the standard xine header: */ +/* + * Copyright (C) 2000-2001 the xine project + * + * This file is part of xine, a free video player. + * + * xine is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * xine is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * $Id: cyuv.c,v 1.1 2002/04/30 08:28:10 esnel Exp $ + */ + +/* And this is the header that came with the CYUV decoder: */ +/* ------------------------------------------------------------------------ + * Creative YUV Video Decoder + * + * Dr. Tim Ferguson, 2001. + * For more details on the algorithm: + * http://www.csse.monash.edu.au/~timf/videocodec.html + * + * This is a very simple predictive coder. A video frame is coded in YUV411 + * format. The first pixel of each scanline is coded using the upper four + * bits of its absolute value. Subsequent pixels for the scanline are coded + * using the difference between the last pixel and the current pixel (DPCM). + * The DPCM values are coded using a 16 entry table found at the start of the + * frame. Thus four bits per component are used and are as follows: + * UY VY YY UY VY YY UY VY... + * This code assumes the frame width will be a multiple of four pixels. This + * should probably be fixed. + * + * You may freely use this source code. I only ask that you reference its + * source in your projects documentation: + * Tim Ferguson: http://www.csse.monash.edu.au/~timf/ + * ------------------------------------------------------------------------ */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <unistd.h> + +#include "video_out.h" +#include "buffer.h" +#include "xine_internal.h" +#include "xineutils.h" + +#define VIDEOBUFSIZE 128*1024 + +typedef struct cyuv_decoder_s { + video_decoder_t video_decoder; + + vo_instance_t *video_out; + int video_step; + int skipframes; + unsigned char *buf; + int bufsize; + int size; + int width; + int height; +} cyuv_decoder_t; + +static int cyuv_can_handle (video_decoder_t *this_gen, int buf_type) { + return (buf_type == BUF_VIDEO_CYUV); +} + +static void cyuv_init (video_decoder_t *this_gen, vo_instance_t *video_out) { + cyuv_decoder_t *this = (cyuv_decoder_t *) this_gen; + + this->video_out = video_out; + this->buf = NULL; +} + +/* ------------------------------------------------------------------------ + * This function decodes a buffer containing a CYUV encoded frame. + * + * buf - the input buffer to be decoded + * size - the size of the input buffer + * frame - the output frame buffer (YUY2 format) + * width - the width of the output frame + * height - the height of the output frame + * bit_per_pixel - ignored for now: may be used later for conversions. + */ +void cyuv_decode(unsigned char *buf, int size, unsigned char *frame, + int width, int height, int bit_per_pixel) { + + int i, xpos, ypos, cur_Y = 0, cur_U = 0, cur_V = 0; + char *delta_y_tbl, *delta_c_tbl, *ptr; + + delta_y_tbl = buf + 16; + delta_c_tbl = buf + 32; + ptr = buf + (16 * 3); + + for(ypos = 0; ypos < height; ypos++) { + for(xpos = 0; xpos < width; xpos += 4) { + /* first pixels in scanline */ + if(xpos == 0) { + cur_U = *(ptr++); + cur_Y = (cur_U & 0x0f) << 4; + cur_U = cur_U & 0xf0; + *frame++ = cur_Y; + *frame++ = cur_U; + + cur_V = *(ptr++); + cur_Y = (cur_Y + delta_y_tbl[cur_V & 0x0f]) & 0xff; + cur_V = cur_V & 0xf0; + *frame++ = cur_Y; + *frame++ = cur_V; + } + /* subsequent pixels in scanline */ + else { + i = *(ptr++); + cur_U = (cur_U + delta_c_tbl[i >> 4]) & 0xff; + cur_Y = (cur_Y + delta_y_tbl[i & 0x0f]) & 0xff; + *frame++ = cur_Y; + *frame++ = cur_U; + + i = *(ptr++); + cur_V = (cur_V + delta_c_tbl[i >> 4]) & 0xff; + cur_Y = (cur_Y + delta_y_tbl[i & 0x0f]) & 0xff; + *frame++ = cur_Y; + *frame++ = cur_V; + } + + i = *(ptr++); + cur_Y = (cur_Y + delta_y_tbl[i & 0x0f]) & 0xff; + *frame++ = cur_Y; + *frame++ = cur_U; + + cur_Y = (cur_Y + delta_y_tbl[i >> 4]) & 0xff; + *frame++ = cur_Y; + *frame++ = cur_V; + } + } +} + +static void cyuv_decode_data (video_decoder_t *this_gen, + buf_element_t *buf) { + + cyuv_decoder_t *this = (cyuv_decoder_t *) this_gen; + vo_frame_t *img; /* video out frame */ + + if (buf->decoder_flags & BUF_FLAG_PREVIEW) + return; + + if (buf->decoder_flags & BUF_FLAG_HEADER) { /* need to initialize */ + this->video_out->open (this->video_out); + + if(this->buf) + free(this->buf); + + this->buf = malloc(VIDEOBUFSIZE); + this->bufsize = VIDEOBUFSIZE; + this->size = 0; + this->width = *(unsigned int *)&buf->content[4]; + this->height = *(unsigned int *)&buf->content[8]; + this->skipframes = 0; + this->video_step = buf->decoder_info[1]; + + return; + } + + if( this->size + buf->size > this->bufsize ) { + this->bufsize = this->size + 2 * buf->size; + printf("CYUV: increasing source buffer to %d to avoid overflow.\n", + this->bufsize); + this->buf = realloc( this->buf, this->bufsize ); + } + + xine_fast_memcpy (&this->buf[this->size], buf->content, buf->size); + this->size += buf->size; + + if (buf->decoder_flags & BUF_FLAG_FRAMERATE) + this->video_step = buf->decoder_info[0]; + + if (buf->decoder_flags & BUF_FLAG_FRAME_END) { /* time to decode a frame */ + img = this->video_out->get_frame (this->video_out, this->width, + this->height, XINE_ASPECT_RATIO_SQUARE, IMGFMT_YUY2, + VO_BOTH_FIELDS); + + img->pts = buf->pts; + img->duration = this->video_step; + + cyuv_decode(this->buf, this->size, img->base[0], + this->width, this->height, 0); + + this->skipframes = img->draw(img); + if( this->skipframes < 0 ) + this->skipframes = 0; + img->free(img); + + this->size = 0; + } +} + +static void cyuv_flush (video_decoder_t *this_gen) { +} + +static void cyuv_reset (video_decoder_t *this_gen) { +} + +static void cyuv_close (video_decoder_t *this_gen) { + + cyuv_decoder_t *this = (cyuv_decoder_t *) this_gen; + + this->video_out->close(this->video_out); +} + +static char *cyuv_get_id(void) { + return "CYUV"; +} + +static void cyuv_dispose (video_decoder_t *this_gen) { + free (this_gen); +} + +video_decoder_t *init_video_decoder_plugin (int iface_version, xine_t *xine) { + + cyuv_decoder_t *this ; + + if (iface_version != 7) { + printf( "CYUV: plugin doesn't support plugin API version %d.\n" + "CYUV: this means there's a version mismatch between xine and this " + "CYUV: decoder plugin.\nInstalling current plugins should help.\n", + iface_version); + return NULL; + } + + this = (cyuv_decoder_t *) malloc (sizeof (cyuv_decoder_t)); + + this->video_decoder.interface_version = iface_version; + this->video_decoder.can_handle = cyuv_can_handle; + this->video_decoder.init = cyuv_init; + this->video_decoder.decode_data = cyuv_decode_data; + this->video_decoder.flush = cyuv_flush; + this->video_decoder.reset = cyuv_reset; + this->video_decoder.close = cyuv_close; + this->video_decoder.get_identifier = cyuv_get_id; + this->video_decoder.dispose = cyuv_dispose; + this->video_decoder.priority = 1; + + return (video_decoder_t *) this; +} diff --git a/src/libxinevdec/msvc.c b/src/libxinevdec/msvc.c new file mode 100644 index 000000000..0db5dd6a9 --- /dev/null +++ b/src/libxinevdec/msvc.c @@ -0,0 +1,361 @@ +/* + * Copyright (C) 2002 the xine project + * + * This file is part of xine, a unix video player. + * + * xine is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * xine is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * by Ewald Snel <ewald@rambo.its.tudelft.nl> + * + * based on overview of Microsoft Video-1 algorithm + * by Mike Melanson: http://www.pcisys.net/~melanson/codecs/video1.txt + * + * $Id: msvc.c,v 1.1 2002/04/30 08:28:10 esnel Exp $ + */ + +#include <stdlib.h> +#include <unistd.h> + +#include "video_out.h" +#include "buffer.h" +#include "bswap.h" +#include "xineutils.h" +#include "xine_internal.h" + +#define VIDEOBUFSIZE 128 * 1024 + +/* now this is ripped of wine's vfw.h */ +typedef struct { + long biSize; + long biWidth; + long biHeight; + short biPlanes; + short biBitCount; + long biCompression; + long biSizeImage; + long biXPelsPerMeter; + long biYPelsPerMeter; + long biClrUsed; + long biClrImportant; +} BITMAPINFOHEADER; + +typedef struct { + uint16_t yu; + uint16_t yv; +} yuy2_t; + +typedef struct msvc_decoder_s { + video_decoder_t video_decoder; + + vo_instance_t *video_out; + int64_t video_step; + int decoder_ok; + + unsigned char *buf; + int bufsize; + int size; + + uint16_t biWidth; + uint16_t biHeight; + uint32_t biBitCount; + uint16_t *img_buffer; + yuy2_t color_table[256]; +} msvc_decoder_t; + +/* taken from libw32dll */ +#define MAXSAMPLE 255 +#define CENTERSAMPLE 128 + +#define SCALEBITS 16 +#define FIX(x) ( (int32_t) ( (x) * (1<<SCALEBITS) + 0.5 ) ) +#define ONE_HALF ( (int32_t) (1<< (SCALEBITS-1)) ) +#define CBCR_OFFSET (CENTERSAMPLE << SCALEBITS) + +static inline void rgb_to_yuy2 (const int bits, uint32_t rgb, yuy2_t *c) { + uint8_t r, g, b; + uint8_t y, u, v; + + if (bits == 15) { + b = (rgb & 0x001F) << 3; + g = (rgb & 0x03E0) >> 5 << 3; + r = (rgb & 0x7C00) >> 10 << 3; + } else { + b = (rgb & 0x0000FF); + g = (rgb & 0x00FF00) >> 8; + r = (rgb & 0xFF0000) >> 16; + } + + y = (FIX(0.299) * r + FIX(0.587) * g + FIX(0.114) * b + ONE_HALF) >> SCALEBITS; + u = (- FIX(0.16874) * r - FIX(0.33126) * g + FIX(0.5) * b + CBCR_OFFSET + ONE_HALF-1) >> SCALEBITS; + v = (FIX(0.5) * r - FIX(0.41869) * g - FIX(0.08131) * b + CBCR_OFFSET + ONE_HALF-1) >> SCALEBITS; + + c->yu = le2me_16 (y | (u << 8)); + c->yv = le2me_16 (y | (v << 8)); +} + +static void cram_decode_frame (msvc_decoder_t *this, uint8_t *data, int size) { + uint8_t *eod = (data + size); + uint32_t ctrl, clr8, skip; + int x, y, i, j; + uint16_t *out[4]; + yuy2_t c[2]; + + skip = 0; + + for (y=(this->biHeight - 4); y >= 0; y-=4) { + + out[0] = this->img_buffer + (y * this->biWidth); + out[1] = out[0] + this->biWidth; + out[2] = out[1] + this->biWidth; + out[3] = out[2] + this->biWidth; + + for (x=0; x < this->biWidth; x+=4) { + if (skip == 0 || --skip == 0) { + if ((data + 2) >= eod) + return; + + ctrl = data[0] | (data[1] << 8); + data += 2; + + if ((ctrl & ((this->biBitCount == 8) ? 0xF000 : 0x8000)) == 0x8000) { + if ((ctrl & ~0x3FF) == 0x8400) { + skip = (ctrl & 0x3FF); + } else { + if (this->biBitCount == 8) + c[0] = this->color_table[(ctrl & 0xFF)]; + else + rgb_to_yuy2 (15, ctrl, &c[0]); + + for (i=0; i < 4; i++) { + out[i][0] = c[0].yu; + out[i][1] = c[0].yv; + out[i][2] = c[0].yu; + out[i][3] = c[0].yv; + } + } + } else { + if (this->biBitCount == 8) { + if ((data + 2) >= eod) + return; + + c[1] = this->color_table[data[0]]; + c[0] = this->color_table[data[1]]; + clr8 = (ctrl >= 0x9000); + data += 2; + } else { + if ((data + 4) >= eod) + return; + + rgb_to_yuy2 (15, (data[0] | (data[1] << 8)), &c[1]); + rgb_to_yuy2 (15, (data[2] | (data[3] << 8)), &c[0]); + clr8 = (data[1] & 0x80); + data += 4; + } + + for (i=0; i < 4; i+=2) { + for (j=0; j < 4; j+=2) { + out[3-i][j] = c[(ctrl & 1) ].yu; + out[3-i][j+1] = c[(ctrl & 2) >> 1].yv; + out[2-i][j] = c[(ctrl & 16)>> 4].yu; + out[2-i][j+1] = c[(ctrl & 32)>> 5].yv; + ctrl >>= 2; + + if (clr8 && !(i & j)) { + if (this->biBitCount == 8) { + if ((data + 2) >= eod) + return; + + c[1] = this->color_table[data[0]]; + c[0] = this->color_table[data[1]]; + data += 2; + } else { + if ((data + 4) >= eod) + return; + + rgb_to_yuy2 (15, (data[0] | (data[1] << 8)), &c[1]); + rgb_to_yuy2 (15, (data[2] | (data[3] << 8)), &c[0]); + data += 4; + } + } + } + + ctrl >>= 4; + } + } + } + + out[0] += 4; + out[1] += 4; + out[2] += 4; + out[3] += 4; + } + } +} + +static int msvc_can_handle (video_decoder_t *this_gen, int buf_type) { + return ((buf_type & 0xFFFF0000) == BUF_VIDEO_MSVC); +} + +static void msvc_init (video_decoder_t *this_gen, vo_instance_t *video_out) { + msvc_decoder_t *this = (msvc_decoder_t *) this_gen; + + this->video_out = video_out; + this->decoder_ok = 0; +} + +static void msvc_decode_data (video_decoder_t *this_gen, buf_element_t *buf) { + msvc_decoder_t *this = (msvc_decoder_t *) this_gen; + + if (buf->decoder_flags & BUF_FLAG_PREVIEW) + return; + + if (buf->decoder_flags & BUF_FLAG_HEADER) { + BITMAPINFOHEADER *bih; + + bih = (BITMAPINFOHEADER *) buf->content; + this->biWidth = (le2me_32 (bih->biWidth) + 3) & ~0x03; + this->biHeight = (le2me_32 (bih->biHeight) + 3) & ~0x03; + this->biBitCount = le2me_32 (bih->biBitCount); + this->video_step = buf->decoder_info[1]; + + if (this->biBitCount != 8 && this->biBitCount != 16) { + fprintf (stderr, "Unsupported bit depth (%d)\n", this->biBitCount); + return; + } + + if (this->img_buffer) + free (this->img_buffer); + this->img_buffer = malloc((this->biWidth * this->biHeight) << 1); + + /* FIXME: Palette not loaded */ +#if 0 + for (i=0; i < 256; i++) { + rgb_to_yuy2 (32, le2me_32 (rgb[i]), &this->color_table[i]); + } +#endif + + if (this->buf) + free (this->buf); + this->bufsize = VIDEOBUFSIZE; + this->buf = malloc(this->bufsize); + this->size = 0; + + this->video_out->open (this->video_out); + this->decoder_ok = 1; + + } else if (this->decoder_ok) { + + if (this->size + buf->size > this->bufsize) { + this->bufsize = this->size + 2 * buf->size; + this->buf = realloc (this->buf, this->bufsize); + } + + xine_fast_memcpy (&this->buf[this->size], buf->content, buf->size); + + this->size += buf->size; + + if (buf->decoder_flags & BUF_FLAG_FRAMERATE) + this->video_step = buf->decoder_info[0]; + + if (buf->decoder_flags & BUF_FLAG_FRAME_END) { + + vo_frame_t *img; + int n = (this->biWidth * this->biHeight); + + cram_decode_frame (this, this->buf, this->size); + + img = this->video_out->get_frame (this->video_out, + this->biWidth, this->biHeight, + 42, IMGFMT_YUY2, VO_BOTH_FIELDS); + + img->duration = this->video_step; + img->pts = buf->pts; + img->bad_frame = 0; + + xine_fast_memcpy (img->base[0], this->img_buffer, (n << 1)); + + img->draw(img); + img->free(img); + + this->size = 0; + } + } +} + +static void msvc_flush (video_decoder_t *this_gen) { +} + +static void msvc_reset (video_decoder_t *this_gen) { + msvc_decoder_t *this = (msvc_decoder_t *) this_gen; + + this->size = 0; +} + +static void msvc_close (video_decoder_t *this_gen) { + + msvc_decoder_t *this = (msvc_decoder_t *) this_gen; + + if (this->img_buffer) { + free (this->img_buffer); + this->img_buffer = NULL; + } + + if (this->buf) { + free (this->buf); + this->buf = NULL; + } + + if (this->decoder_ok) { + this->decoder_ok = 0; + this->video_out->close(this->video_out); + } +} + +static char *msvc_get_id(void) { + return "msvc"; +} + +static void msvc_dispose (video_decoder_t *this_gen) { + free (this_gen); +} + +video_decoder_t *init_video_decoder_plugin (int iface_version, xine_t *xine) { + + msvc_decoder_t *this ; + + if (iface_version != 7) { + printf( "msvc: plugin doesn't support plugin API version %d.\n" + "msvc: this means there's a version mismatch between xine and this " + "msvc: decoder plugin.\nInstalling current plugins should help.\n", + iface_version); + return NULL; + } + + this = (msvc_decoder_t *) malloc (sizeof (msvc_decoder_t)); + memset(this, 0, sizeof (msvc_decoder_t)); + + this->video_decoder.interface_version = iface_version; + this->video_decoder.can_handle = msvc_can_handle; + this->video_decoder.init = msvc_init; + this->video_decoder.decode_data = msvc_decode_data; + this->video_decoder.flush = msvc_flush; + this->video_decoder.reset = msvc_reset; + this->video_decoder.close = msvc_close; + this->video_decoder.get_identifier = msvc_get_id; + this->video_decoder.dispose = msvc_dispose; + this->video_decoder.priority = 5; + + return (video_decoder_t *) this; +} |