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