10000l : fix a crash on x86 due to an horrible mistake in my x86_64 patch
[mplayer.git] / libmpcodecs / vd_vfw.c
blobf3f77da9a7c8e5a962b81061f691fa3a9c920dc4
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "config.h"
5 #include "mp_msg.h"
6 #include "help_mp.h"
8 #ifdef USE_WIN32DLL
10 #include "vd_internal.h"
12 #include "wine/driver.h"
13 #include "wine/vfw.h"
15 static vd_info_t info = {
16 #ifdef BUILD_VFWEX
17 "Win32/VfWex video codecs",
18 "vfwex",
19 #else
20 "Win32/VfW video codecs",
21 "vfw",
22 #endif
23 "A'rpi & Alex",
24 "avifile.sf.net",
25 "win32 codecs"
28 #ifdef BUILD_VFWEX
29 LIBVD_EXTERN(vfwex)
30 #else
31 LIBVD_EXTERN(vfw)
32 #endif
34 typedef struct {
35 BITMAPINFOHEADER *o_bih;
36 HIC handle;
37 unsigned char *palette;
38 } vd_vfw_ctx;
40 extern int divx_quality;
42 static int vfw_set_postproc(sh_video_t* sh, int quality)
44 vd_vfw_ctx *priv = sh->context;
45 // Works only with opendivx/divx4 based DLL
46 return ICSendMessage(priv->handle, ICM_USER+80, (long)(&quality), 0);
49 static void set_csp(BITMAPINFOHEADER *o_bih,unsigned int outfmt){
50 int yuv = 0;
52 switch (outfmt)
54 /* planar format */
55 case IMGFMT_YV12:
56 case IMGFMT_I420:
57 case IMGFMT_IYUV:
58 o_bih->biBitCount=12;
59 yuv=1;
60 break;
61 case IMGFMT_YVU9:
62 case IMGFMT_IF09:
63 o_bih->biBitCount=9;
64 yuv=1;
65 break;
66 /* packed format */
67 case IMGFMT_YUY2:
68 case IMGFMT_UYVY:
69 case IMGFMT_YVYU:
70 o_bih->biBitCount=16;
71 yuv=1;
72 break;
73 /* rgb/bgr format */
74 case IMGFMT_RGB8:
75 case IMGFMT_BGR8:
76 o_bih->biBitCount=8;
77 break;
78 case IMGFMT_RGB15:
79 case IMGFMT_RGB16:
80 case IMGFMT_BGR15:
81 case IMGFMT_BGR16:
82 o_bih->biBitCount=16;
83 break;
84 case IMGFMT_RGB24:
85 case IMGFMT_BGR24:
86 o_bih->biBitCount=24;
87 break;
88 case IMGFMT_RGB32:
89 case IMGFMT_BGR32:
90 o_bih->biBitCount=32;
91 break;
92 default:
93 mp_msg(MSGT_WIN32,MSGL_ERR,"Unsupported image format: %s\n", vo_format_name(outfmt));
94 return;
97 o_bih->biSizeImage = abs(o_bih->biWidth * o_bih->biHeight * (o_bih->biBitCount/8));
99 // Note: we cannot rely on sh->outfmtidx here, it's undefined at this stage!!!
100 // if (yuv && !(sh->codec->outflags[sh->outfmtidx] & CODECS_FLAG_YUVHACK))
101 if (yuv)
102 o_bih->biCompression = outfmt;
103 else
104 o_bih->biCompression = 0;
107 // to set/get/query special features/parameters
108 static int control(sh_video_t *sh,int cmd,void* arg,...){
109 vd_vfw_ctx *priv = sh->context;
110 switch(cmd){
111 case VDCTRL_QUERY_MAX_PP_LEVEL:
112 return 9;
113 case VDCTRL_SET_PP_LEVEL:
114 vfw_set_postproc(sh,10*(*((int*)arg)));
115 return CONTROL_OK;
116 #if 1
117 // FIXME: make this optional...
118 case VDCTRL_QUERY_FORMAT:
120 HRESULT ret;
121 if(!(sh->codec->outflags[sh->outfmtidx]&CODECS_FLAG_QUERY))
122 return CONTROL_UNKNOWN; // do not query!
123 set_csp(priv->o_bih,*((int*)arg));
124 #ifdef BUILD_VFWEX
125 ret = ICDecompressQueryEx(priv->handle, sh->bih, priv->o_bih);
126 #else
127 ret = ICDecompressQuery(priv->handle, sh->bih, priv->o_bih);
128 #endif
129 if (ret)
131 mp_msg(MSGT_WIN32, MSGL_DBG2, "ICDecompressQuery failed:: Error %d\n", (int)ret);
132 return CONTROL_FALSE;
134 return CONTROL_TRUE;
136 #endif
138 return CONTROL_UNKNOWN;
141 extern void print_video_header(BITMAPINFOHEADER *h);
143 // init driver
144 static int init(sh_video_t *sh){
145 HRESULT ret;
146 // unsigned int outfmt=sh->codec->outfmt[sh->outfmtidx];
147 int i, o_bih_len;
148 vd_vfw_ctx *priv;
150 priv = malloc(sizeof(vd_vfw_ctx));
151 if (!priv)
152 return 0;
153 memset(priv, 0, sizeof(vd_vfw_ctx));
154 sh->context = priv;
156 mp_msg(MSGT_WIN32,MSGL_V,"======= Win32 (VFW) VIDEO Codec init =======\n");
159 // win32_codec_name = sh->codec->dll;
160 // sh->hic = ICOpen( 0x63646976, sh->bih->biCompression, ICMODE_FASTDECOMPRESS);
161 // priv->handle = ICOpen( 0x63646976, sh->bih->biCompression, ICMODE_DECOMPRESS);
162 priv->handle = ICOpen( (long)(sh->codec->dll), sh->bih->biCompression, ICMODE_DECOMPRESS);
163 if(!priv->handle){
164 mp_msg(MSGT_WIN32,MSGL_ERR,"ICOpen failed! unknown codec / wrong parameters?\n");
165 return 0;
168 // sh->bih->biBitCount=32;
170 o_bih_len = ICDecompressGetFormatSize(priv->handle, sh->bih);
172 priv->o_bih = malloc(o_bih_len);
173 memset(priv->o_bih, 0, o_bih_len);
175 mp_msg(MSGT_WIN32,MSGL_V,"ICDecompressGetFormatSize ret: %d\n", o_bih_len);
177 ret = ICDecompressGetFormat(priv->handle, sh->bih, priv->o_bih);
178 if(ret < 0){
179 mp_msg(MSGT_WIN32,MSGL_ERR,"ICDecompressGetFormat failed: Error %d\n", (int)ret);
180 for (i=0; i < o_bih_len; i++) mp_msg(MSGT_WIN32, MSGL_DBG2, "%02x ", priv->o_bih[i]);
181 return 0;
183 mp_msg(MSGT_WIN32,MSGL_V,"ICDecompressGetFormat OK\n");
185 #if 0
186 // workaround for pegasus MJPEG:
187 if(!sh_video->o_bih.biWidth) sh_video->o_bih.biWidth=sh_video->bih->biWidth;
188 if(!sh_video->o_bih.biHeight) sh_video->o_bih.biHeight=sh_video->bih->biHeight;
189 if(!sh_video->o_bih.biPlanes) sh_video->o_bih.biPlanes=sh_video->bih->biPlanes;
190 #endif
192 // ok let libvo and vd core to handshake and decide the optimal csp:
193 if(!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YUY2)) return 0;
195 if (!(sh->codec->outflags[sh->outfmtidx]&CODECS_FLAG_FLIP)) {
196 priv->o_bih->biHeight=-sh->bih->biHeight; // flip image!
199 // ok, let's set the choosen colorspace:
200 set_csp(priv->o_bih,sh->codec->outfmt[sh->outfmtidx]);
202 // fake it to RGB for broken DLLs (divx3)
203 if(sh->codec->outflags[sh->outfmtidx] & CODECS_FLAG_YUVHACK)
204 priv->o_bih->biCompression = 0;
206 // sanity check:
207 #if 1
208 #ifdef BUILD_VFWEX
209 ret = ICDecompressQueryEx(priv->handle, sh->bih, priv->o_bih);
210 #else
211 ret = ICDecompressQuery(priv->handle, sh->bih, priv->o_bih);
212 #endif
213 if (ret)
215 mp_msg(MSGT_WIN32,MSGL_WARN,"ICDecompressQuery failed: Error %d\n", (int)ret);
216 // return 0;
217 } else
218 mp_msg(MSGT_WIN32,MSGL_V,"ICDecompressQuery OK\n");
219 #endif
221 #ifdef BUILD_VFWEX
222 ret = ICDecompressBeginEx(priv->handle, sh->bih, priv->o_bih);
223 #else
224 ret = ICDecompressBegin(priv->handle, sh->bih, priv->o_bih);
225 #endif
226 if (ret)
228 mp_msg(MSGT_WIN32,MSGL_WARN,"ICDecompressBegin failed: Error %d\n", (int)ret);
229 // return 0;
232 // for broken codecs set it again:
233 if(sh->codec->outflags[sh->outfmtidx] & CODECS_FLAG_YUVHACK)
234 set_csp(priv->o_bih,sh->codec->outfmt[sh->outfmtidx]);
236 mp_msg(MSGT_WIN32, MSGL_V, "Input format:\n");
237 if(verbose>0) print_video_header(sh->bih);
238 mp_msg(MSGT_WIN32, MSGL_V, "Output format:\n");
239 if(verbose>0) print_video_header(priv->o_bih);
241 // set postprocessing level in xvid/divx4 .dll
242 ICSendMessage(priv->handle, ICM_USER+80, (long)(&divx_quality), 0);
244 // don't do this palette mess always, it makes div3 dll crashing...
245 if(sh->codec->outfmt[sh->outfmtidx]==IMGFMT_BGR8){
246 if(ICDecompressGetPalette(priv->handle, sh->bih, priv->o_bih)){
247 priv->palette = (unsigned char*)(priv->o_bih+1);
248 mp_msg(MSGT_WIN32,MSGL_V,"ICDecompressGetPalette OK\n");
249 } else {
250 if(sh->bih->biSize>=40+4*4)
251 priv->palette = (unsigned char*)(sh->bih+1);
255 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: Win32 video codec init OK!\n");
256 return 1;
259 // uninit driver
260 static void uninit(sh_video_t *sh){
261 HRESULT ret;
262 vd_vfw_ctx *priv = sh->context;
264 #ifdef BUILD_VFWEX
265 ret = ICDecompressEndEx(priv->handle);
266 #else
267 ret = ICDecompressEnd(priv->handle);
268 #endif
269 if (ret)
271 mp_msg(MSGT_WIN32, MSGL_WARN, "ICDecompressEnd failed: %d\n", ret);
272 return;
275 ret = ICClose(priv->handle);
276 if (ret)
278 mp_msg(MSGT_WIN32, MSGL_WARN, "ICClose failed: %d\n", ret);
279 return;
282 free(priv->o_bih);
283 free(priv);
286 // decode a frame
287 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
288 vd_vfw_ctx *priv = sh->context;
289 mp_image_t* mpi;
290 HRESULT ret;
292 if(len<=0) return NULL; // skipped frame
294 mpi=mpcodecs_get_image(sh,
295 (sh->codec->outflags[sh->outfmtidx] & CODECS_FLAG_STATIC) ?
296 MP_IMGTYPE_STATIC : MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_WIDTH,
297 sh->disp_w, sh->disp_h);
298 if(!mpi){ // temporary!
299 printf("couldn't allocate image for cinepak codec\n");
300 return NULL;
303 // set stride: (trick discovered by Andreas Ackermann - thanx!)
304 sh->bih->biWidth=mpi->width; //mpi->stride[0]/(mpi->bpp/8);
305 priv->o_bih->biWidth=mpi->width; //mpi->stride[0]/(mpi->bpp/8);
307 sh->bih->biSizeImage = len;
309 #ifdef BUILD_VFWEX
310 ret = ICDecompressEx(priv->handle,
311 #else
312 ret = ICDecompress(priv->handle,
313 #endif
314 ( (sh->ds->flags&1) ? 0 : ICDECOMPRESS_NOTKEYFRAME ) |
315 ( ((flags&3)==2 && !(sh->ds->flags&1))?(ICDECOMPRESS_HURRYUP|ICDECOMPRESS_PREROL):0 ),
316 sh->bih, data, priv->o_bih, (flags&3) ? 0 : mpi->planes[0]);
318 if ((int)ret){
319 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error decompressing frame, err=%d\n",ret);
320 return NULL;
323 // export palette:
324 if(mpi->imgfmt==IMGFMT_RGB8 || mpi->imgfmt==IMGFMT_BGR8){
325 if (priv->palette)
327 mpi->planes[1] = priv->palette;
328 mpi->flags |= MP_IMGFLAG_RGB_PALETTE;
329 mp_dbg(MSGT_DECVIDEO, MSGL_DBG2, "Found and copied palette\n");
331 else
332 mpi->planes[1]=NULL;
335 return mpi;
337 #endif