typo fixes
[mplayer/greg.git] / libmpcodecs / vd_qtvideo.c
blobf96624bd1f7ad16522fc4565ad0f2e7d07750d9d
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "config.h"
6 #if defined(USE_QTX_CODECS) || defined(MACOSX)
7 #include "wine/windef.h"
9 #include "mp_msg.h"
10 #include "vd_internal.h"
12 #ifdef WIN32_LOADER
13 #include "ldt_keeper.h"
14 #endif
16 static vd_info_t info = {
17 "Quicktime Video decoder",
18 "qtvideo",
19 "A'rpi",
20 "Sascha Sommer",
21 "win32"
24 LIBVD_EXTERN(qtvideo)
26 #include "bswap.h"
28 #ifdef MACOSX
29 #include <QuickTime/ImageCodec.h>
30 #define dump_ImageDescription(x)
31 #else
32 #include "qtx/qtxsdk/components.h"
33 #endif
35 #if defined(USE_QTX_CODECS) && !defined(MACOSX)
36 //#include "wine/windef.h"
38 HMODULE WINAPI LoadLibraryA(LPCSTR);
39 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
40 int WINAPI FreeLibrary(HMODULE);
41 #endif
43 //static ComponentDescription desc; // for FindNextComponent()
44 static ComponentInstance ci=NULL; // codec handle
45 //static CodecInfo cinfo; // for ImageCodecGetCodecInfo()
46 //Component prev=NULL;
47 //ComponentResult cres; //
48 static CodecCapabilities codeccap; // for decpar
49 static CodecDecompressParams decpar; // for ImageCodecPreDecompress()
50 //static ImageSubCodecDecompressCapabilities icap; // for ImageCodecInitialize()
51 static Rect OutBufferRect; //the dimensions of our GWorld
53 static GWorldPtr OutBufferGWorld = NULL;//a GWorld is some kind of description for a drawing environment
54 static ImageDescriptionHandle framedescHandle;
55 static HINSTANCE qtime_qts; // handle to the preloaded quicktime.qts
56 static HMODULE handler;
58 #if defined(USE_QTX_CODECS) && !defined(MACOSX)
59 static Component (*FindNextComponent)(Component prev,ComponentDescription* desc);
60 static OSErr (*GetComponentInfo)(Component prev,ComponentDescription* desc,Handle h1,Handle h2,Handle h3);
61 static long (*CountComponents)(ComponentDescription* desc);
62 static OSErr (*InitializeQTML)(long flags);
63 static OSErr (*EnterMovies)(void);
64 static ComponentInstance (*OpenComponent)(Component c);
65 static ComponentResult (*ImageCodecInitialize)(ComponentInstance ci,
66 ImageSubCodecDecompressCapabilities * cap);
67 static ComponentResult (*ImageCodecBeginBand)(ComponentInstance ci,
68 CodecDecompressParams * params,
69 ImageSubCodecDecompressRecord * drp,
70 long flags);
71 static ComponentResult (*ImageCodecDrawBand)(ComponentInstance ci,
72 ImageSubCodecDecompressRecord * drp);
73 static ComponentResult (*ImageCodecEndBand)(ComponentInstance ci,
74 ImageSubCodecDecompressRecord * drp,
75 OSErr result,
76 long flags);
77 static ComponentResult (*ImageCodecGetCodecInfo)(ComponentInstance ci,
78 CodecInfo * info);
79 static ComponentResult (*ImageCodecPreDecompress)(ComponentInstance ci,
80 CodecDecompressParams * params);
81 static ComponentResult (*ImageCodecBandDecompress)(ComponentInstance ci,
82 CodecDecompressParams * params);
83 static PixMapHandle (*GetGWorldPixMap)(GWorldPtr offscreenGWorld);
84 static OSErr (*QTNewGWorldFromPtr)(GWorldPtr *gw,
85 OSType pixelFormat,
86 const Rect *boundsRect,
87 CTabHandle cTable,
88 /*GDHandle*/void* aGDevice, //unused anyway
89 GWorldFlags flags,
90 void *baseAddr,
91 long rowBytes);
92 static OSErr (*NewHandleClear)(Size byteCount);
93 #endif
95 // to set/get/query special features/parameters
96 static int control(sh_video_t *sh,int cmd,void* arg,...){
97 return CONTROL_UNKNOWN;
100 static int codec_inited=0;
102 // init driver
103 static int init(sh_video_t *sh){
104 long result = 1;
105 ComponentResult cres;
106 ComponentDescription desc;
107 Component prev=NULL;
108 CodecInfo cinfo; // for ImageCodecGetCodecInfo()
109 ImageSubCodecDecompressCapabilities icap; // for ImageCodecInitialize()
111 #ifdef MACOSX
112 EnterMovies();
113 #else
115 #ifdef WIN32_LOADER
116 Setup_LDT_Keeper();
117 #endif
119 //preload quicktime.qts to avoid the problems caused by the hardcoded path inside the dll
120 qtime_qts = LoadLibraryA("QuickTime.qts");
121 if(!qtime_qts){
122 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"unable to load QuickTime.qts\n" );
123 return 0;
126 handler = LoadLibraryA("qtmlClient.dll");
127 if(!handler){
128 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"unable to load qtmlClient.dll\n");
129 return 0;
132 InitializeQTML = (OSErr (*)(long))GetProcAddress(handler, "InitializeQTML");
133 EnterMovies = (OSErr (*)(void))GetProcAddress(handler, "EnterMovies");
134 FindNextComponent = (Component (*)(Component,ComponentDescription*))GetProcAddress(handler, "FindNextComponent");
135 CountComponents = (long (*)(ComponentDescription*))GetProcAddress(handler, "CountComponents");
136 GetComponentInfo = (OSErr (*)(Component,ComponentDescription*,Handle,Handle,Handle))GetProcAddress(handler, "GetComponentInfo");
137 OpenComponent = (ComponentInstance (*)(Component))GetProcAddress(handler, "OpenComponent");
138 ImageCodecInitialize = (ComponentResult (*)(ComponentInstance,ImageSubCodecDecompressCapabilities *))GetProcAddress(handler, "ImageCodecInitialize");
139 ImageCodecGetCodecInfo = (ComponentResult (*)(ComponentInstance,CodecInfo *))GetProcAddress(handler, "ImageCodecGetCodecInfo");
140 ImageCodecBeginBand = (ComponentResult (*)(ComponentInstance,CodecDecompressParams *,ImageSubCodecDecompressRecord *,long))GetProcAddress(handler, "ImageCodecBeginBand");
141 ImageCodecPreDecompress = (ComponentResult (*)(ComponentInstance,CodecDecompressParams *))GetProcAddress(handler, "ImageCodecPreDecompress");
142 ImageCodecBandDecompress = (ComponentResult (*)(ComponentInstance,CodecDecompressParams *))GetProcAddress(handler, "ImageCodecBandDecompress");
143 GetGWorldPixMap = (PixMapHandle (*)(GWorldPtr))GetProcAddress(handler, "GetGWorldPixMap");
144 QTNewGWorldFromPtr = (OSErr(*)(GWorldPtr *,OSType,const Rect *,CTabHandle,void*,GWorldFlags,void *,long))GetProcAddress(handler, "QTNewGWorldFromPtr");
145 NewHandleClear = (OSErr(*)(Size))GetProcAddress(handler, "NewHandleClear");
146 // = GetProcAddress(handler, "");
148 if(!InitializeQTML || !EnterMovies || !FindNextComponent || !ImageCodecBandDecompress){
149 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"invalid qtmlClient.dll!\n");
150 return 0;
153 result=InitializeQTML(6+16);
154 // result=InitializeQTML(0);
155 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"InitializeQTML returned %li\n",result);
156 // result=EnterMovies();
157 // printf("EnterMovies->%d\n",result);
158 #endif /* !MACOSX */
160 #if 0
161 memset(&desc,0,sizeof(desc));
162 while((prev=FindNextComponent(prev,&desc))){
163 ComponentDescription desc2;
164 unsigned char* c1=&desc2.componentType;
165 unsigned char* c2=&desc2.componentSubType;
166 memset(&desc2,0,sizeof(desc2));
167 // printf("juhee %p (%p)\n",prev,&desc);
168 GetComponentInfo(prev,&desc2,NULL,NULL,NULL);
169 mp_msg(MSGT_DECVIDEO,MSGL_DGB2,"DESC: %c%c%c%c/%c%c%c%c [0x%X/0x%X] 0x%X\n",
170 c1[3],c1[2],c1[1],c1[0],
171 c2[3],c2[2],c2[1],c2[0],
172 desc2.componentType,desc2.componentSubType,
173 desc2.componentFlags);
175 #endif
178 memset(&desc,0,sizeof(desc));
179 desc.componentType= (((unsigned char)'i')<<24)|
180 (((unsigned char)'m')<<16)|
181 (((unsigned char)'d')<<8)|
182 (((unsigned char)'c'));
183 #if 0
184 desc.componentSubType=
185 (((unsigned char)'S'<<24))|
186 (((unsigned char)'V')<<16)|
187 (((unsigned char)'Q')<<8)|
188 (((unsigned char)'3'));
189 #else
190 desc.componentSubType = bswap_32(sh->format);
191 #endif
192 desc.componentManufacturer=0;
193 desc.componentFlags=0;
194 desc.componentFlagsMask=0;
196 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"Count = %ld\n",CountComponents(&desc));
197 prev=FindNextComponent(NULL,&desc);
198 if(!prev){
199 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Cannot find requested component\n");
200 return(0);
202 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"Found it! ID = %p\n",prev);
204 ci=OpenComponent(prev);
205 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"ci=%p\n",ci);
207 memset(&icap,0,sizeof(icap));
208 cres=ImageCodecInitialize(ci,&icap);
209 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"ImageCodecInitialize->%#x size=%d (%d)\n",cres,icap.recordSize,icap.decompressRecordSize);
211 memset(&cinfo,0,sizeof(cinfo));
212 cres=ImageCodecGetCodecInfo(ci,&cinfo);
213 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"Flags: compr: 0x%X decomp: 0x%X format: 0x%X\n",
214 cinfo.compressFlags, cinfo.decompressFlags, cinfo.formatFlags);
215 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"Codec name: %.*s\n",((unsigned char*)&cinfo.typeName)[0],
216 ((unsigned char*)&cinfo.typeName)+1);
218 //make a yuy2 gworld
219 OutBufferRect.top=0;
220 OutBufferRect.left=0;
221 OutBufferRect.right=sh->disp_w;
222 OutBufferRect.bottom=sh->disp_h;
224 //Fill the imagedescription for our SVQ3 frame
225 //we can probably get this from Demuxer
226 #if 0
227 framedescHandle=(ImageDescriptionHandle)NewHandleClear(sizeof(ImageDescription)+200);
228 printf("framedescHandle=%p *p=%p\n",framedescHandle,*framedescHandle);
229 { FILE* f=fopen("/root/.wine/fake_windows/IDesc","r");
230 if(!f) printf("filenot found: IDesc\n");
231 fread(*framedescHandle,sizeof(ImageDescription)+200,1,f);
232 fclose(f);
234 #else
235 if(!sh->ImageDesc) sh->ImageDesc=(sh->bih+1); // hack for SVQ3-in-AVI
236 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"ImageDescription size: %d\n",((ImageDescription*)(sh->ImageDesc))->idSize);
237 framedescHandle=(ImageDescriptionHandle)NewHandleClear(((ImageDescription*)(sh->ImageDesc))->idSize);
238 memcpy(*framedescHandle,sh->ImageDesc,((ImageDescription*)(sh->ImageDesc))->idSize);
239 dump_ImageDescription(*framedescHandle);
240 #endif
241 //Find codecscomponent for video decompression
242 // result = FindCodec ('SVQ1',anyCodec,&compressor,&decompressor );
243 // printf("FindCodec SVQ1 returned:%i compressor: 0x%X decompressor: 0x%X\n",result,compressor,decompressor);
245 sh->context = (void *)kYUVSPixelFormat;
246 #if 1
248 int imgfmt = sh->codec->outfmt[sh->outfmtidx];
249 int qt_imgfmt;
250 switch(imgfmt)
252 case IMGFMT_YUY2:
253 qt_imgfmt = kYUVSPixelFormat;
254 break;
255 case IMGFMT_YVU9:
256 qt_imgfmt = 0x73797639; //kYVU9PixelFormat;
257 break;
258 case IMGFMT_YV12:
259 qt_imgfmt = 0x79343230;
260 break;
261 case IMGFMT_UYVY:
262 qt_imgfmt = kUYVY422PixelFormat;
263 break;
264 case IMGFMT_YVYU:
265 qt_imgfmt = kYVYU422PixelFormat;
266 imgfmt = IMGFMT_YUY2;
267 break;
268 case IMGFMT_RGB16:
269 qt_imgfmt = k16LE555PixelFormat;
270 break;
271 case IMGFMT_BGR24:
272 qt_imgfmt = k24BGRPixelFormat;
273 break;
274 case IMGFMT_BGR32:
275 qt_imgfmt = k32BGRAPixelFormat;
276 break;
277 case IMGFMT_RGB32:
278 qt_imgfmt = k32RGBAPixelFormat;
279 break;
280 default:
281 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Unknown requested csp\n");
282 return(0);
284 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"imgfmt: %s qt_imgfmt: %.4s\n", vo_format_name(imgfmt), (char *)&qt_imgfmt);
285 sh->context = (void *)qt_imgfmt;
286 if(!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,imgfmt)) return 0;
288 #else
289 if(!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YUY2)) return 0;
290 #endif
292 return 1;
295 // uninit driver
296 static void uninit(sh_video_t *sh){
297 #ifdef MACOSX
298 ExitMovies();
299 #endif
302 // decode a frame
303 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
304 long result = 1;
305 int i;
306 mp_image_t* mpi;
307 ComponentResult cres;
309 if(len<=0) return NULL; // skipped frame
311 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_STATIC, MP_IMGFLAG_PRESERVE,
312 sh->disp_w, sh->disp_h);
313 if(!mpi) return NULL;
315 decpar.data = (char*)data;
316 decpar.bufferSize = len;
317 (**framedescHandle).dataSize=len;
319 if(!codec_inited){
320 result = QTNewGWorldFromPtr(
321 &OutBufferGWorld,
322 // kYUVSPixelFormat, //pixel format of new GWorld == YUY2
323 (OSType)sh->context,
324 &OutBufferRect, //we should benchmark if yvu9 is faster for svq3, too
328 mpi->planes[0],
329 mpi->stride[0]);
330 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"NewGWorldFromPtr returned:%ld\n",65536-(result&0xffff));
331 // if (65536-(result&0xFFFF) != 10000)
332 // return NULL;
334 // printf("IDesc=%d\n",sizeof(ImageDescription));
336 decpar.imageDescription = framedescHandle;
337 decpar.startLine=0;
338 decpar.stopLine=(**framedescHandle).height;
339 decpar.frameNumber = 1; //1
340 // decpar.conditionFlags=0xFFD; // first
341 // decpar.callerFlags=0x2001; // first
342 decpar.matrixFlags = 0;
343 decpar.matrixType = 0;
344 decpar.matrix = 0;
345 decpar.capabilities=&codeccap;
346 // decpar.accuracy = 0x1680000; //codecNormalQuality;
347 decpar.accuracy = codecNormalQuality;
348 // decpar.port = OutBufferGWorld;
349 // decpar.preferredOffscreenPixelSize=17207;
351 // decpar.sequenceID=malloc(1000);
352 // memset(decpar.sequenceID,0,1000);
354 // SrcRect.top=17207;
355 // SrcRect.left=0;
356 // SrcRect.right=0;//image_width;
357 // SrcRect.bottom=0;//image_height;
359 // decpar.srcRect = SrcRect;
360 decpar.srcRect = OutBufferRect;
362 decpar.transferMode = srcCopy;
363 decpar.dstPixMap = **GetGWorldPixMap( OutBufferGWorld);//destPixmap;
365 cres=ImageCodecPreDecompress(ci,&decpar);
366 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"ImageCodecPreDecompress cres=0x%X\n",cres);
368 if(decpar.wantedDestinationPixelTypes)
369 { OSType *p=*(decpar.wantedDestinationPixelTypes);
370 if(p) while(*p){
371 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"supported csp: 0x%08X %.4s\n",*p,(char *)p);
372 ++p;
377 // decpar.conditionFlags=0x10FFF; // first
378 // decpar.preferredOffscreenPixelSize=17207;
380 // decpar.conditionFlags=0x10FFD; // first
382 // cres=ImageCodecPreDecompress(ci,&decpar);
383 // printf("ImageCodecPreDecompress cres=0x%X\n",cres);
386 codec_inited=1;
389 #if 0
390 if(decpar.frameNumber==124){
391 decpar.frameNumber=1;
392 cres=ImageCodecPreDecompress(ci,&decpar);
393 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"ImageCodecPreDecompress cres=0x%lX\n",cres);
395 #endif
397 cres=ImageCodecBandDecompress(ci,&decpar);
399 ++decpar.frameNumber;
401 if(cres&0xFFFF){
402 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"ImageCodecBandDecompress cres=0x%X (-0x%X) %d\n",cres,-cres,cres);
403 return NULL;
406 // for(i=0;i<8;i++)
407 // printf("img_base[%d]=%p\n",i,((int*)decpar.dstPixMap.baseAddr)[i]);
409 if((int)sh->context==0x73797639){ // Sorenson 16-bit YUV -> std YVU9
411 short *src0=(short *)((char*)decpar.dstPixMap.baseAddr+0x20);
413 for(i=0;i<mpi->h;i++){
414 int x;
415 unsigned char* dst=mpi->planes[0]+i*mpi->stride[0];
416 unsigned short* src=src0+i*((mpi->w+15)&(~15));
417 for(x=0;x<mpi->w;x++) dst[x]=src[x];
419 src0+=((mpi->w+15)&(~15))*((mpi->h+15)&(~15));
420 for(i=0;i<mpi->h/4;i++){
421 int x;
422 unsigned char* dst=mpi->planes[1]+i*mpi->stride[1];
423 unsigned short* src=src0+i*(((mpi->w+63)&(~63))/4);
424 for(x=0;x<mpi->w/4;x++) dst[x]=src[x];
425 src+=((mpi->w+63)&(~63))/4;
427 src0+=(((mpi->w+63)&(~63))/4)*(((mpi->h+63)&(~63))/4);
428 for(i=0;i<mpi->h/4;i++){
429 int x;
430 unsigned char* dst=mpi->planes[2]+i*mpi->stride[2];
431 unsigned short* src=src0+i*(((mpi->w+63)&(~63))/4);
432 for(x=0;x<mpi->w/4;x++) dst[x]=src[x];
433 src+=((mpi->w+63)&(~63))/4;
439 return mpi;
441 #endif