ao_mpegpes.c: minor simplification
[mplayer.git] / libmpcodecs / ve_vfw.c
blob282550229347046f2248b9e1bd3c294799ac971a
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <inttypes.h>
24 #include <sys/stat.h>
26 #include "config.h"
28 #include "mp_msg.h"
30 #include "codec-cfg.h"
31 //#include "stream/stream.h"
32 //#include "libmpdemux/demuxer.h"
33 //#include "libmpdemux/stheader.h"
35 #include "loader/loader.h"
36 //#include "loader/wine/mmreg.h"
37 #include "loader/wine/vfw.h"
38 #include "libmpdemux/aviheader.h"
39 #include "loader/wine/winerror.h"
40 #include "loader/wine/objbase.h"
42 #include "img_format.h"
43 #include "mp_image.h"
44 #include "vf.h"
46 #include "stream/stream.h"
47 #include "libmpdemux/muxer.h"
49 //===========================================================================//
51 static char *vfw_param_codec = NULL;
52 static char *vfw_param_compdata = NULL;
53 static HRESULT CoInitRes = -1;
55 #include "m_option.h"
57 const m_option_t vfwopts_conf[]={
58 {"codec", &vfw_param_codec, CONF_TYPE_STRING, 0, 0, 0, NULL},
59 {"compdata", &vfw_param_compdata, CONF_TYPE_STRING, 0, 0, 0, NULL},
60 {NULL, NULL, 0, 0, 0, 0, NULL}
63 struct vf_priv_s {
64 muxer_stream_t* mux;
65 BITMAPINFOHEADER* bih;
68 static HIC encoder_hic;
69 static void* encoder_buf=NULL;
70 static int encoder_buf_size=0;
71 static int encoder_frameno=0;
73 //int init_vfw_encoder(char *dll_name, BITMAPINFOHEADER *input_bih, BITMAPINFOHEADER *output_bih)
74 static BITMAPINFOHEADER* vfw_open_encoder(char *dll_name, char *compdatafile, BITMAPINFOHEADER *input_bih,unsigned int out_fourcc)
76 HRESULT ret;
77 BITMAPINFOHEADER* output_bih=NULL;
78 int temp_len;
79 FILE *fd=NULL;
80 char *drvdata=NULL;
81 struct stat st;
83 //sh_video = malloc(sizeof(sh_video_t));
85 mp_msg(MSGT_WIN32,MSGL_V,"======= Win32 (VFW) VIDEO Encoder init =======\n");
86 CoInitRes = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
87 // memset(&sh_video->o_bih, 0, sizeof(BITMAPINFOHEADER));
88 // output_bih->biSize = sizeof(BITMAPINFOHEADER);
90 // encoder_hic = ICOpen( 0x63646976, out_fourcc, ICMODE_COMPRESS);
91 encoder_hic = ICOpen( (long) dll_name, out_fourcc, ICMODE_COMPRESS);
92 if(!encoder_hic){
93 mp_msg(MSGT_WIN32,MSGL_ERR,"ICOpen failed! unknown codec / wrong parameters?\n");
94 return NULL;
96 mp_msg(MSGT_WIN32,MSGL_INFO,"HIC: %x\n", encoder_hic);
99 ICINFO icinfo;
101 ret = ICGetInfo(encoder_hic, &icinfo, sizeof(ICINFO));
102 mp_msg(MSGT_WIN32,MSGL_INFO,"%ld - %ld - %d\n", ret, icinfo.dwSize, sizeof(ICINFO));
103 mp_tmsg(MSGT_WIN32,MSGL_INFO,"Compressor type: %.4lx\n", icinfo.fccType);
104 mp_tmsg(MSGT_WIN32,MSGL_INFO,"Compressor subtype: %.4lx\n", icinfo.fccHandler);
105 mp_tmsg(MSGT_WIN32,MSGL_INFO,"Compressor flags: %lu, version %lu, ICM version: %lu\n",
106 icinfo.dwFlags, icinfo.dwVersion, icinfo.dwVersionICM);
107 //printf("Compressor name: %s\n", icinfo.szName);
108 //printf("Compressor description: %s\n", icinfo.szDescription);
110 mp_tmsg(MSGT_WIN32,MSGL_INFO,"Flags:");
111 if (icinfo.dwFlags & VIDCF_QUALITY)
112 mp_tmsg(MSGT_WIN32,MSGL_INFO," quality");
113 if (icinfo.dwFlags & VIDCF_FASTTEMPORALD)
114 mp_msg(MSGT_WIN32,MSGL_INFO," fast-decompr");
115 if (icinfo.dwFlags & VIDCF_QUALITYTIME)
116 mp_msg(MSGT_WIN32,MSGL_INFO," temp-quality");
117 mp_msg(MSGT_WIN32,MSGL_INFO,"\n");
120 if(compdatafile){
121 if (!strncmp(compdatafile, "dialog", 6)){
122 if (ICSendMessage(encoder_hic, ICM_CONFIGURE, -1, 0) != ICERR_OK){
123 mp_msg(MSGT_WIN32,MSGL_ERR,"Compressor doesn't have a configure dialog!\n");
124 return NULL;
126 if (ICSendMessage(encoder_hic, ICM_CONFIGURE, 0, 0) != ICERR_OK){
127 mp_msg(MSGT_WIN32,MSGL_ERR,"Compressor configure dialog failed!\n");
128 return NULL;
131 else {
132 if (stat(compdatafile, &st) < 0){
133 mp_msg(MSGT_WIN32,MSGL_ERR,"Compressor data file not found!\n");
134 return NULL;
136 fd = fopen(compdatafile, "rb");
137 if (!fd){
138 mp_msg(MSGT_WIN32,MSGL_ERR,"Cannot open Compressor data file!\n");
139 return NULL;
141 drvdata = malloc(st.st_size);
142 if (fread(drvdata, st.st_size, 1, fd) != 1) {
143 mp_msg(MSGT_WIN32,MSGL_ERR,"Cannot read Compressor data file!\n");
144 fclose(fd);
145 free(drvdata);
146 return NULL;
148 fclose(fd);
149 mp_msg(MSGT_WIN32,MSGL_ERR,"Compressor data %d bytes\n", st.st_size);
150 if (!(temp_len = (unsigned int) ICSendMessage(encoder_hic, ICM_SETSTATE, (LPARAM) drvdata, (int) st.st_size))){
151 mp_msg(MSGT_WIN32,MSGL_ERR,"ICSetState failed!\n");
152 free(drvdata);
153 return NULL;
155 free(drvdata);
156 mp_msg(MSGT_WIN32,MSGL_INFO,"ICSetState ret: %d\n", temp_len);
160 temp_len = ICCompressGetFormatSize(encoder_hic, input_bih);
161 mp_msg(MSGT_WIN32,MSGL_INFO,"ICCompressGetFormatSize ret: %d\n", temp_len);
163 if (temp_len < sizeof(BITMAPINFOHEADER)) temp_len=sizeof(BITMAPINFOHEADER);
165 output_bih = malloc(temp_len+4);
166 memset(output_bih,0,temp_len);
167 output_bih->biSize = temp_len; //sizeof(BITMAPINFOHEADER);
169 return output_bih;
172 static int vfw_start_encoder(BITMAPINFOHEADER *input_bih, BITMAPINFOHEADER *output_bih){
173 HRESULT ret;
174 int temp_len=output_bih->biSize;
175 int i;
177 ret = ICCompressGetFormat(encoder_hic, input_bih, output_bih);
178 if(ret < 0){
179 unsigned char* temp=(unsigned char*)output_bih;
180 mp_msg(MSGT_WIN32,MSGL_ERR,"ICCompressGetFormat failed: Error %d (0x%X)\n", (int)ret, (int)ret);
181 for (i=0; i < temp_len; i++) mp_msg(MSGT_WIN32, MSGL_DBG2, "%02x ", temp[i]);
182 return 0;
184 mp_msg(MSGT_WIN32,MSGL_V,"ICCompressGetFormat OK\n");
186 if (temp_len > sizeof(BITMAPINFOHEADER))
188 unsigned char* temp=(unsigned char*)output_bih;
189 mp_msg(MSGT_WIN32, MSGL_V, "Extra info in o_bih (%d bytes)!\n",
190 temp_len-sizeof(BITMAPINFOHEADER));
191 for(i=sizeof(output_bih);i<temp_len;i++) mp_msg(MSGT_WIN32, MSGL_DBG2, "%02X ",temp[i]);
194 // if( mp_msg_test(MSGT_WIN32,MSGL_V) ) {
195 printf("Starting compression:\n");
196 printf(" Input format:\n");
197 printf(" biSize %ld\n", input_bih->biSize);
198 printf(" biWidth %ld\n", input_bih->biWidth);
199 printf(" biHeight %ld\n", input_bih->biHeight);
200 printf(" biPlanes %d\n", input_bih->biPlanes);
201 printf(" biBitCount %d\n", input_bih->biBitCount);
202 printf(" biCompression 0x%lx ('%.4s')\n", input_bih->biCompression, (char *)&input_bih->biCompression);
203 printf(" biSizeImage %ld\n", input_bih->biSizeImage);
204 printf(" Output format:\n");
205 printf(" biSize %ld\n", output_bih->biSize);
206 printf(" biWidth %ld\n", output_bih->biWidth);
207 printf(" biHeight %ld\n", output_bih->biHeight);
208 printf(" biPlanes %d\n", output_bih->biPlanes);
209 printf(" biBitCount %d\n", output_bih->biBitCount);
210 printf(" biCompression 0x%lx ('%.4s')\n", output_bih->biCompression, (char *)&output_bih->biCompression);
211 printf(" biSizeImage %ld\n", output_bih->biSizeImage);
212 // }
214 output_bih->biWidth=input_bih->biWidth;
215 output_bih->biHeight=input_bih->biHeight;
217 ret = ICCompressQuery(encoder_hic, input_bih, output_bih);
218 if(ret){
219 mp_msg(MSGT_WIN32,MSGL_ERR,"ICCompressQuery failed: Error %d\n", (int)ret);
220 return 0;
221 } else
222 mp_msg(MSGT_WIN32,MSGL_V,"ICCompressQuery OK\n");
224 ret = ICCompressBegin(encoder_hic, input_bih, output_bih);
225 if(ret){
226 mp_msg(MSGT_WIN32,MSGL_ERR,"ICCompressBegin failed: Error %d\n", (int)ret);
227 // return 0;
228 } else
229 mp_msg(MSGT_WIN32,MSGL_V,"ICCompressBegin OK\n");
230 mp_msg(MSGT_WIN32,MSGL_INFO," Output format after query/begin:\n");
231 mp_msg(MSGT_WIN32,MSGL_INFO," biSize %ld\n", output_bih->biSize);
232 mp_msg(MSGT_WIN32,MSGL_INFO," biWidth %ld\n", output_bih->biWidth);
233 mp_msg(MSGT_WIN32,MSGL_INFO," biHeight %ld\n", output_bih->biHeight);
234 mp_msg(MSGT_WIN32,MSGL_INFO," biPlanes %d\n", output_bih->biPlanes);
235 mp_msg(MSGT_WIN32,MSGL_INFO," biBitCount %d\n", output_bih->biBitCount);
236 mp_msg(MSGT_WIN32,MSGL_INFO," biCompression 0x%lx ('%.4s')\n", output_bih->biCompression, (char *)&output_bih->biCompression);
237 mp_msg(MSGT_WIN32,MSGL_INFO," biSizeImage %ld\n", output_bih->biSizeImage);
239 encoder_buf_size=input_bih->biSizeImage;
240 encoder_buf=malloc(encoder_buf_size);
241 encoder_frameno=0;
243 mp_msg(MSGT_WIN32,MSGL_V,"VIDEO CODEC Init OK!!! ;-)\n");
244 return 1;
247 static int vfw_encode_frame(BITMAPINFOHEADER* biOutput,void* OutBuf,
248 BITMAPINFOHEADER* biInput,void* Image,
249 long* keyframe, int quality){
250 HRESULT ret;
252 //long VFWAPIV ICCompress(
253 // HIC hic,long dwFlags,LPBITMAPINFOHEADER lpbiOutput,void* lpOutputBuf,
254 // LPBITMAPINFOHEADER lpbiInput,void* lpImage,long* lpckid,
255 // long* lpdwFlags,long lFrameNum,long dwFrameSize,long dwQuality,
256 // LPBITMAPINFOHEADER lpbiInputPrev,void* lpImagePrev
257 //);
259 // printf("vfw_encode_frame(%p,%p, %p,%p, %p,%d)\n",biOutput,OutBuf,biInput,Image,keyframe,quality);
261 ret=ICCompress(encoder_hic, 0,
262 biOutput, OutBuf,
263 biInput, Image,
264 NULL, keyframe, encoder_frameno, 0, quality,
265 biInput, encoder_buf);
267 // printf("ok. size=%ld\n",biOutput->biSizeImage);
269 memcpy(encoder_buf,Image,encoder_buf_size);
270 ++encoder_frameno;
272 return (int)ret;
274 #define mux_v (vf->priv->mux)
275 #define vfw_bih (vf->priv->bih)
277 static int config(struct vf_instance *vf,
278 int width, int height, int d_width, int d_height,
279 unsigned int flags, unsigned int outfmt){
281 vfw_bih->biWidth=width;
282 vfw_bih->biHeight=height;
283 vfw_bih->biSizeImage=width*height*((vfw_bih->biBitCount+7)/8);
284 mux_v->aspect = (float)d_width/d_height;
286 if(!vfw_start_encoder(vfw_bih, mux_v->bih)) return 0;
288 // mux_v->bih->biWidth=width;
289 // mux_v->bih->biHeight=height;
290 // mux_v->bih->biSizeImage=width*height*((mux_v->bih->biBitCount+7)/8);
292 return 1;
295 static int control(struct vf_instance *vf, int request, void* data){
297 return CONTROL_UNKNOWN;
300 static int query_format(struct vf_instance *vf, unsigned int fmt){
301 if(fmt==IMGFMT_BGR24) return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_FLIPPED;
302 return 0;
305 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
306 long flags=0;
307 int ret;
308 // flip_upside_down(vo_image_ptr,vo_image_ptr,3*vo_w,vo_h); // dirty hack
309 ret=vfw_encode_frame(mux_v->bih, mux_v->buffer, vfw_bih, mpi->planes[0], &flags, 10000);
310 // if (ret != ICERR_OK)
311 // return 0;
312 muxer_write_chunk(mux_v,mux_v->bih->biSizeImage,flags, pts, pts);
313 return 1;
316 static void uninit(struct vf_instance *vf)
318 HRESULT ret;
320 if(encoder_hic){
321 if(encoder_buf){
322 ret=ICCompressEnd(encoder_hic);
323 if(ret) mp_msg(MSGT_WIN32, MSGL_WARN, "ICCompressEnd failed: %ld\n", ret);
324 free(encoder_buf);
325 encoder_buf=NULL;
327 ret=ICClose(encoder_hic);
328 if(ret) mp_msg(MSGT_WIN32, MSGL_WARN, "ICClose failed: %ld\n", ret);
329 encoder_hic=0;
330 if ((CoInitRes == S_OK) || (CoInitRes == S_FALSE)) CoUninitialize();
334 //===========================================================================//
336 static int vf_open(vf_instance_t *vf, char* args){
337 vf->config=config;
338 vf->default_caps=VFCAP_CONSTANT;
339 vf->control=control;
340 vf->query_format=query_format;
341 vf->put_image=put_image;
342 vf->uninit=uninit;
343 vf->priv=malloc(sizeof(struct vf_priv_s));
344 memset(vf->priv,0,sizeof(struct vf_priv_s));
345 vf->priv->mux=(muxer_stream_t*)args;
347 vfw_bih=calloc(1, sizeof(BITMAPINFOHEADER));
348 vfw_bih->biSize=sizeof(BITMAPINFOHEADER);
349 vfw_bih->biWidth=0; // FIXME ?
350 vfw_bih->biHeight=0;
351 vfw_bih->biPlanes=1;
352 vfw_bih->biBitCount=24;
353 vfw_bih->biCompression=0;
354 // vfw_bih->biSizeImage=vo_w*vo_h*((vfw_bih->biBitCount+7)/8);
356 if (!vfw_param_codec)
358 mp_tmsg(MSGT_WIN32,MSGL_WARN, "[VE_RAW] Required VfW codec not specified!!\n");
359 return 0;
361 // mux_v->bih=vfw_open_encoder("divxc32.dll",vfw_bih,mmioFOURCC('D', 'I', 'V', '3'));
362 // mux_v->bih=vfw_open_encoder("AvidAVICodec.dll",vfw_bih, 0);
363 mux_v->bih = vfw_open_encoder(vfw_param_codec, vfw_param_compdata, vfw_bih, 0);
364 if(!mux_v->bih) return 0;
366 return 1;
369 vf_info_t ve_info_vfw = {
370 "Win32/VfW encoders",
371 "vfw",
372 "A'rpi",
373 "for internal use by mencoder",
374 vf_open
377 //===========================================================================//