8 #include "vd_internal.h"
10 #ifdef CONFIG_QUICKTIME
11 #include <QuickTime/ImageCodec.h>
12 #define dump_ImageDescription(x)
14 #include "loader/ldt_keeper.h"
15 #include "loader/qtx/qtxsdk/components.h"
16 #include "loader/wine/windef.h"
19 static vd_info_t info
= {
20 "Quicktime Video decoder",
29 //static ComponentDescription desc; // for FindNextComponent()
30 static ComponentInstance ci
=NULL
; // codec handle
31 //static CodecInfo cinfo; // for ImageCodecGetCodecInfo()
32 //Component prev=NULL;
33 //ComponentResult cres; //
34 static CodecCapabilities codeccap
; // for decpar
35 static CodecDecompressParams decpar
; // for ImageCodecPreDecompress()
36 //static ImageSubCodecDecompressCapabilities icap; // for ImageCodecInitialize()
37 static Rect OutBufferRect
; //the dimensions of our GWorld
39 static GWorldPtr OutBufferGWorld
= NULL
;//a GWorld is some kind of description for a drawing environment
40 static ImageDescriptionHandle framedescHandle
;
42 #ifndef CONFIG_QUICKTIME
43 HMODULE WINAPI
LoadLibraryA(LPCSTR
);
44 FARPROC WINAPI
GetProcAddress(HMODULE
,LPCSTR
);
45 int WINAPI
FreeLibrary(HMODULE
);
46 static HINSTANCE qtime_qts
; // handle to the preloaded quicktime.qts
47 static HMODULE handler
;
48 static Component (*FindNextComponent
)(Component prev
,ComponentDescription
* desc
);
49 static OSErr (*GetComponentInfo
)(Component prev
,ComponentDescription
* desc
,Handle h1
,Handle h2
,Handle h3
);
50 static long (*CountComponents
)(ComponentDescription
* desc
);
51 static OSErr (*InitializeQTML
)(long flags
);
52 static OSErr (*EnterMovies
)(void);
53 static ComponentInstance (*OpenComponent
)(Component c
);
54 static ComponentResult (*ImageCodecInitialize
)(ComponentInstance ci
,
55 ImageSubCodecDecompressCapabilities
* cap
);
56 static ComponentResult (*ImageCodecBeginBand
)(ComponentInstance ci
,
57 CodecDecompressParams
* params
,
58 ImageSubCodecDecompressRecord
* drp
,
60 static ComponentResult (*ImageCodecGetCodecInfo
)(ComponentInstance ci
,
62 static ComponentResult (*ImageCodecPreDecompress
)(ComponentInstance ci
,
63 CodecDecompressParams
* params
);
64 static ComponentResult (*ImageCodecBandDecompress
)(ComponentInstance ci
,
65 CodecDecompressParams
* params
);
66 static PixMapHandle (*GetGWorldPixMap
)(GWorldPtr offscreenGWorld
);
67 static OSErr (*QTNewGWorldFromPtr
)(GWorldPtr
*gw
,
69 const Rect
*boundsRect
,
71 /*GDHandle*/void* aGDevice
, //unused anyway
75 static OSErr (*NewHandleClear
)(Size byteCount
);
76 #endif /* #ifndef CONFIG_QUICKTIME */
78 // to set/get/query special features/parameters
79 static int control(sh_video_t
*sh
,int cmd
,void* arg
,...){
80 return CONTROL_UNKNOWN
;
83 static int codec_initialized
=0;
86 static int init(sh_video_t
*sh
){
87 #ifndef CONFIG_QUICKTIME
91 ComponentDescription desc
;
93 CodecInfo cinfo
; // for ImageCodecGetCodecInfo()
94 ImageSubCodecDecompressCapabilities icap
; // for ImageCodecInitialize()
96 codec_initialized
= 0;
97 #ifdef CONFIG_QUICKTIME
105 //preload quicktime.qts to avoid the problems caused by the hardcoded path inside the dll
106 qtime_qts
= LoadLibraryA("QuickTime.qts");
108 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
,"unable to load QuickTime.qts\n" );
112 handler
= LoadLibraryA("qtmlClient.dll");
114 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
,"unable to load qtmlClient.dll\n");
118 InitializeQTML
= (OSErr (*)(long))GetProcAddress(handler
, "InitializeQTML");
119 EnterMovies
= (OSErr (*)(void))GetProcAddress(handler
, "EnterMovies");
120 FindNextComponent
= (Component (*)(Component
,ComponentDescription
*))GetProcAddress(handler
, "FindNextComponent");
121 CountComponents
= (long (*)(ComponentDescription
*))GetProcAddress(handler
, "CountComponents");
122 GetComponentInfo
= (OSErr (*)(Component
,ComponentDescription
*,Handle
,Handle
,Handle
))GetProcAddress(handler
, "GetComponentInfo");
123 OpenComponent
= (ComponentInstance (*)(Component
))GetProcAddress(handler
, "OpenComponent");
124 ImageCodecInitialize
= (ComponentResult (*)(ComponentInstance
,ImageSubCodecDecompressCapabilities
*))GetProcAddress(handler
, "ImageCodecInitialize");
125 ImageCodecGetCodecInfo
= (ComponentResult (*)(ComponentInstance
,CodecInfo
*))GetProcAddress(handler
, "ImageCodecGetCodecInfo");
126 ImageCodecBeginBand
= (ComponentResult (*)(ComponentInstance
,CodecDecompressParams
*,ImageSubCodecDecompressRecord
*,long))GetProcAddress(handler
, "ImageCodecBeginBand");
127 ImageCodecPreDecompress
= (ComponentResult (*)(ComponentInstance
,CodecDecompressParams
*))GetProcAddress(handler
, "ImageCodecPreDecompress");
128 ImageCodecBandDecompress
= (ComponentResult (*)(ComponentInstance
,CodecDecompressParams
*))GetProcAddress(handler
, "ImageCodecBandDecompress");
129 GetGWorldPixMap
= (PixMapHandle (*)(GWorldPtr
))GetProcAddress(handler
, "GetGWorldPixMap");
130 QTNewGWorldFromPtr
= (OSErr(*)(GWorldPtr
*,OSType
,const Rect
*,CTabHandle
,void*,GWorldFlags
,void *,long))GetProcAddress(handler
, "QTNewGWorldFromPtr");
131 NewHandleClear
= (OSErr(*)(Size
))GetProcAddress(handler
, "NewHandleClear");
132 // = GetProcAddress(handler, "");
134 if(!InitializeQTML
|| !EnterMovies
|| !FindNextComponent
|| !ImageCodecBandDecompress
){
135 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
,"invalid qtmlClient.dll!\n");
139 result
=InitializeQTML(6+16);
140 // result=InitializeQTML(0);
141 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
,"InitializeQTML returned %li\n",result
);
142 // result=EnterMovies();
143 // printf("EnterMovies->%d\n",result);
144 #endif /* CONFIG_QUICKTIME */
147 memset(&desc
,0,sizeof(desc
));
148 while((prev
=FindNextComponent(prev
,&desc
))){
149 ComponentDescription desc2
;
150 unsigned char* c1
=&desc2
.componentType
;
151 unsigned char* c2
=&desc2
.componentSubType
;
152 memset(&desc2
,0,sizeof(desc2
));
153 // printf("juhee %p (%p)\n",prev,&desc);
154 GetComponentInfo(prev
,&desc2
,NULL
,NULL
,NULL
);
155 mp_msg(MSGT_DECVIDEO
,MSGL_DGB2
,"DESC: %c%c%c%c/%c%c%c%c [0x%X/0x%X] 0x%X\n",
156 c1
[3],c1
[2],c1
[1],c1
[0],
157 c2
[3],c2
[2],c2
[1],c2
[0],
158 desc2
.componentType
,desc2
.componentSubType
,
159 desc2
.componentFlags
);
164 memset(&desc
,0,sizeof(desc
));
165 desc
.componentType
= (((unsigned char)'i')<<24)|
166 (((unsigned char)'m')<<16)|
167 (((unsigned char)'d')<<8)|
168 (((unsigned char)'c'));
170 desc
.componentSubType
=
171 (((unsigned char)'S'<<24))|
172 (((unsigned char)'V')<<16)|
173 (((unsigned char)'Q')<<8)|
174 (((unsigned char)'3'));
176 desc
.componentSubType
= bswap_32(sh
->format
);
178 desc
.componentManufacturer
=0;
179 desc
.componentFlags
=0;
180 desc
.componentFlagsMask
=0;
182 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
,"Count = %ld\n",CountComponents(&desc
));
183 prev
=FindNextComponent(NULL
,&desc
);
185 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
,"Cannot find requested component\n");
188 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
,"Found it! ID = %p\n",prev
);
190 ci
=OpenComponent(prev
);
191 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
,"ci=%p\n",ci
);
193 memset(&icap
,0,sizeof(icap
));
194 cres
=ImageCodecInitialize(ci
,&icap
);
195 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
,"ImageCodecInitialize->%#x size=%d (%d)\n",cres
,icap
.recordSize
,icap
.decompressRecordSize
);
197 memset(&cinfo
,0,sizeof(cinfo
));
198 cres
=ImageCodecGetCodecInfo(ci
,&cinfo
);
199 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
,"Flags: compr: 0x%X decomp: 0x%X format: 0x%X\n",
200 cinfo
.compressFlags
, cinfo
.decompressFlags
, cinfo
.formatFlags
);
201 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
,"Codec name: %.*s\n",((unsigned char*)&cinfo
.typeName
)[0],
202 ((unsigned char*)&cinfo
.typeName
)+1);
206 OutBufferRect
.left
=0;
207 OutBufferRect
.right
=sh
->disp_w
;
208 OutBufferRect
.bottom
=sh
->disp_h
;
210 //Fill the imagedescription for our SVQ3 frame
211 //we can probably get this from Demuxer
213 framedescHandle
=(ImageDescriptionHandle
)NewHandleClear(sizeof(ImageDescription
)+200);
214 printf("framedescHandle=%p *p=%p\n",framedescHandle
,*framedescHandle
);
215 { FILE* f
=fopen("/root/.wine/fake_windows/IDesc","r");
216 if(!f
) printf("filenot found: IDesc\n");
217 fread(*framedescHandle
,sizeof(ImageDescription
)+200,1,f
);
221 if(!sh
->ImageDesc
) sh
->ImageDesc
=(sh
->bih
+1); // hack for SVQ3-in-AVI
222 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
,"ImageDescription size: %d\n",((ImageDescription
*)(sh
->ImageDesc
))->idSize
);
223 framedescHandle
=(ImageDescriptionHandle
)NewHandleClear(((ImageDescription
*)(sh
->ImageDesc
))->idSize
);
224 memcpy(*framedescHandle
,sh
->ImageDesc
,((ImageDescription
*)(sh
->ImageDesc
))->idSize
);
225 dump_ImageDescription(*framedescHandle
);
227 //Find codecscomponent for video decompression
228 // result = FindCodec ('SVQ1',anyCodec,&compressor,&decompressor );
229 // printf("FindCodec SVQ1 returned:%i compressor: 0x%X decompressor: 0x%X\n",result,compressor,decompressor);
231 sh
->context
= (void *)kYUVSPixelFormat
;
234 int imgfmt
= sh
->codec
->outfmt
[sh
->outfmtidx
];
239 qt_imgfmt
= kYUVSPixelFormat
;
242 qt_imgfmt
= 0x73797639; //kYVU9PixelFormat;
245 qt_imgfmt
= 0x79343230;
248 qt_imgfmt
= kUYVY422PixelFormat
;
251 qt_imgfmt
= kYVYU422PixelFormat
;
252 imgfmt
= IMGFMT_YUY2
;
255 qt_imgfmt
= k16LE555PixelFormat
;
258 qt_imgfmt
= k24BGRPixelFormat
;
261 qt_imgfmt
= k32BGRAPixelFormat
;
264 qt_imgfmt
= k32RGBAPixelFormat
;
267 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
,"Unknown requested csp\n");
270 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
,"imgfmt: %s qt_imgfmt: %.4s\n", vo_format_name(imgfmt
), (char *)&qt_imgfmt
);
271 sh
->context
= (void *)qt_imgfmt
;
272 if(!mpcodecs_config_vo(sh
,sh
->disp_w
,sh
->disp_h
,imgfmt
)) return 0;
275 if(!mpcodecs_config_vo(sh
,sh
->disp_w
,sh
->disp_h
,IMGFMT_YUY2
)) return 0;
282 static void uninit(sh_video_t
*sh
){
283 #ifdef CONFIG_QUICKTIME
289 static mp_image_t
* decode(sh_video_t
*sh
,void* data
,int len
,int flags
){
293 ComponentResult cres
;
295 if(len
<=0) return NULL
; // skipped frame
297 mpi
=mpcodecs_get_image(sh
, MP_IMGTYPE_STATIC
, MP_IMGFLAG_PRESERVE
,
298 sh
->disp_w
, sh
->disp_h
);
299 if(!mpi
) return NULL
;
301 decpar
.data
= (char*)data
;
302 decpar
.bufferSize
= len
;
303 (**framedescHandle
).dataSize
=len
;
305 if(!codec_initialized
){
306 result
= QTNewGWorldFromPtr(
308 // kYUVSPixelFormat, //pixel format of new GWorld == YUY2
310 &OutBufferRect
, //we should benchmark if yvu9 is faster for svq3, too
316 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
,"NewGWorldFromPtr returned:%ld\n",65536-(result
&0xffff));
317 // if (65536-(result&0xFFFF) != 10000)
320 // printf("IDesc=%d\n",sizeof(ImageDescription));
322 decpar
.imageDescription
= framedescHandle
;
324 decpar
.stopLine
=(**framedescHandle
).height
;
325 decpar
.frameNumber
= 1; //1
326 // decpar.conditionFlags=0xFFD; // first
327 // decpar.callerFlags=0x2001; // first
328 decpar
.matrixFlags
= 0;
329 decpar
.matrixType
= 0;
331 decpar
.capabilities
=&codeccap
;
332 // decpar.accuracy = 0x1680000; //codecNormalQuality;
333 decpar
.accuracy
= codecNormalQuality
;
334 // decpar.port = OutBufferGWorld;
335 // decpar.preferredOffscreenPixelSize=17207;
337 // decpar.sequenceID=malloc(1000);
338 // memset(decpar.sequenceID,0,1000);
340 // SrcRect.top=17207;
342 // SrcRect.right=0;//image_width;
343 // SrcRect.bottom=0;//image_height;
345 // decpar.srcRect = SrcRect;
346 decpar
.srcRect
= OutBufferRect
;
348 decpar
.transferMode
= srcCopy
;
349 decpar
.dstPixMap
= **GetGWorldPixMap( OutBufferGWorld
);//destPixmap;
351 cres
=ImageCodecPreDecompress(ci
,&decpar
);
352 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
,"ImageCodecPreDecompress cres=0x%X\n",cres
);
354 if(decpar
.wantedDestinationPixelTypes
)
355 { OSType
*p
=*(decpar
.wantedDestinationPixelTypes
);
357 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
,"supported csp: 0x%08X %.4s\n",*p
,(char *)p
);
363 // decpar.conditionFlags=0x10FFF; // first
364 // decpar.preferredOffscreenPixelSize=17207;
366 // decpar.conditionFlags=0x10FFD; // first
368 // cres=ImageCodecPreDecompress(ci,&decpar);
369 // printf("ImageCodecPreDecompress cres=0x%X\n",cres);
376 if(decpar
.frameNumber
==124){
377 decpar
.frameNumber
=1;
378 cres
=ImageCodecPreDecompress(ci
,&decpar
);
379 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
,"ImageCodecPreDecompress cres=0x%lX\n",cres
);
383 cres
=ImageCodecBandDecompress(ci
,&decpar
);
385 ++decpar
.frameNumber
;
388 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
,"ImageCodecBandDecompress cres=0x%X (-0x%X) %d\n",cres
,-cres
,cres
);
393 // printf("img_base[%d]=%p\n",i,((int*)decpar.dstPixMap.baseAddr)[i]);
395 if((int)sh
->context
==0x73797639){ // Sorenson 16-bit YUV -> std YVU9
397 short *src0
=(short *)((char*)decpar
.dstPixMap
.baseAddr
+0x20);
399 for(i
=0;i
<mpi
->h
;i
++){
401 unsigned char* dst
=mpi
->planes
[0]+i
*mpi
->stride
[0];
402 unsigned short* src
=src0
+i
*((mpi
->w
+15)&(~15));
403 for(x
=0;x
<mpi
->w
;x
++) dst
[x
]=src
[x
];
405 src0
+=((mpi
->w
+15)&(~15))*((mpi
->h
+15)&(~15));
406 for(i
=0;i
<mpi
->h
/4;i
++){
408 unsigned char* dst
=mpi
->planes
[1]+i
*mpi
->stride
[1];
409 unsigned short* src
=src0
+i
*(((mpi
->w
+63)&(~63))/4);
410 for(x
=0;x
<mpi
->w
/4;x
++) dst
[x
]=src
[x
];
411 src
+=((mpi
->w
+63)&(~63))/4;
413 src0
+=(((mpi
->w
+63)&(~63))/4)*(((mpi
->h
+63)&(~63))/4);
414 for(i
=0;i
<mpi
->h
/4;i
++){
416 unsigned char* dst
=mpi
->planes
[2]+i
*mpi
->stride
[2];
417 unsigned short* src
=src0
+i
*(((mpi
->w
+63)&(~63))/4);
418 for(x
=0;x
<mpi
->w
/4;x
++) dst
[x
]=src
[x
];
419 src
+=((mpi
->w
+63)&(~63))/4;