vo_tag: fix RGB with alpha output on little-endian
[mplayer/glamo.git] / libmpcodecs / vd_qtvideo.c
blob6032a57f8daaebb3654c16bd2f836e89feb092cd
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 <inttypes.h>
23 #include "config.h"
24 #include "mp_msg.h"
25 #include "mpbswap.h"
26 #include "vd_internal.h"
28 #ifdef CONFIG_QUICKTIME
29 #include <QuickTime/ImageCodec.h>
30 #define dump_ImageDescription(x)
31 #else
32 #include "loader/ldt_keeper.h"
33 #include "loader/qtx/qtxsdk/components.h"
34 #include "loader/wine/winbase.h"
35 #include "loader/wine/windef.h"
36 #endif
38 static const vd_info_t info = {
39 "Quicktime Video decoder",
40 "qtvideo",
41 "A'rpi",
42 "Sascha Sommer",
43 "win32"
46 LIBVD_EXTERN(qtvideo)
48 static mp_image_t* mpi;
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 ImageSequence imageSeq;
55 #ifndef CONFIG_QUICKTIME
56 static HINSTANCE qtime_qts; // handle to the preloaded quicktime.qts
57 static HMODULE handler;
58 static OSErr (*InitializeQTML)(long flags);
59 static OSErr (*EnterMovies)(void);
60 static void (*ExitMovies)(void);
61 static OSErr (*DecompressSequenceBegin)(ImageSequence *seqID,
62 ImageDescriptionHandle desc,
63 CGrafPtr port,
64 /*GDHandle*/void* gdh,
65 const Rect *srcRect,
66 MatrixRecordPtr matrix,
67 short mode,
68 RgnHandle mask,
69 CodecFlags flags,
70 CodecQ accuracy,
71 DecompressorComponent codec);
72 static OSErr (*DecompressSequenceFrameS)(ImageSequence seqID,
73 Ptr data,
74 long dataSize,
75 CodecFlags inFlags,
76 CodecFlags *outFlags,
77 ICMCompletionProcRecordPtr asyncCompletionProc);
78 static PixMapHandle (*GetGWorldPixMap)(GWorldPtr offscreenGWorld);
79 static OSErr (*QTNewGWorldFromPtr)(GWorldPtr *gw,
80 OSType pixelFormat,
81 const Rect *boundsRect,
82 CTabHandle cTable,
83 /*GDHandle*/void* aGDevice, //unused anyway
84 GWorldFlags flags,
85 void *baseAddr,
86 long rowBytes);
87 static Handle (*NewHandleClear)(Size byteCount);
88 static void (*DisposeHandle)(Handle h);
89 static void (*DisposeGWorld)(GWorldPtr offscreenGWorld);
90 static OSErr (*CDSequenceEnd)(ImageSequence seqID);
91 #endif /* #ifndef CONFIG_QUICKTIME */
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 // init driver
99 static int init(sh_video_t *sh){
100 OSErr result = 1;
102 if (sh->ImageDesc == NULL) {
103 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"sh->ImageDesc not set, cannot use binary QuickTime codecs (try -demuxer mov?)\n");
104 return 0;
107 #ifndef CONFIG_QUICKTIME
108 #ifdef WIN32_LOADER
109 Setup_LDT_Keeper();
110 #endif
112 //preload quicktime.qts to avoid the problems caused by the hardcoded path inside the dll
113 qtime_qts = LoadLibraryA("QuickTime.qts");
114 if(!qtime_qts){
115 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"unable to load QuickTime.qts\n" );
116 return 0;
119 handler = LoadLibraryA("qtmlClient.dll");
120 if(!handler){
121 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"unable to load qtmlClient.dll\n");
122 return 0;
125 InitializeQTML = (OSErr (*)(long))GetProcAddress(handler, "InitializeQTML");
126 EnterMovies = (OSErr (*)(void))GetProcAddress(handler, "EnterMovies");
127 ExitMovies = (void (*)(void))GetProcAddress(handler, "ExitMovies");
128 DecompressSequenceBegin = (OSErr (*)(ImageSequence*,ImageDescriptionHandle,CGrafPtr,void *,const Rect *,MatrixRecordPtr,short,RgnHandle,CodecFlags,CodecQ,DecompressorComponent))GetProcAddress(handler, "DecompressSequenceBegin");
129 DecompressSequenceFrameS = (OSErr (*)(ImageSequence,Ptr,long,CodecFlags,CodecFlags*,ICMCompletionProcRecordPtr))GetProcAddress(handler, "DecompressSequenceFrameS");
130 GetGWorldPixMap = (PixMapHandle (*)(GWorldPtr))GetProcAddress(handler, "GetGWorldPixMap");
131 QTNewGWorldFromPtr = (OSErr(*)(GWorldPtr *,OSType,const Rect *,CTabHandle,void*,GWorldFlags,void *,long))GetProcAddress(handler, "QTNewGWorldFromPtr");
132 NewHandleClear = (OSErr(*)(Size))GetProcAddress(handler, "NewHandleClear");
133 DisposeHandle = (void (*)(Handle))GetProcAddress(handler, "DisposeHandle");
134 DisposeGWorld = (void (*)(GWorldPtr))GetProcAddress(handler, "DisposeGWorld");
135 CDSequenceEnd = (OSErr (*)(ImageSequence))GetProcAddress(handler, "CDSequenceEnd");
137 if(!InitializeQTML || !EnterMovies || !DecompressSequenceBegin || !DecompressSequenceFrameS){
138 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"invalid qtmlClient.dll!\n");
139 return 0;
142 result=InitializeQTML(kInitializeQTMLDisableDirectSound |
143 kInitializeQTMLUseGDIFlag |
144 kInitializeQTMLDisableDDClippers);
145 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"InitializeQTML returned %d\n",result);
146 #endif /* CONFIG_QUICKTIME */
148 result=EnterMovies();
149 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"EnterMovies returned %d\n",result);
151 //make a yuy2 gworld
152 OutBufferRect.top=0;
153 OutBufferRect.left=0;
154 OutBufferRect.right=sh->disp_w;
155 OutBufferRect.bottom=sh->disp_h;
157 //Fill the imagedescription for our SVQ3 frame
158 //we can probably get this from Demuxer
159 if(!sh->ImageDesc) sh->ImageDesc=(sh->bih+1); // hack for SVQ3-in-AVI
160 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"ImageDescription size: %d\n",((ImageDescription*)(sh->ImageDesc))->idSize);
161 framedescHandle=(ImageDescriptionHandle)NewHandleClear(((ImageDescription*)(sh->ImageDesc))->idSize);
162 memcpy(*framedescHandle,sh->ImageDesc,((ImageDescription*)(sh->ImageDesc))->idSize);
163 dump_ImageDescription(*framedescHandle);
165 (**framedescHandle).cType = bswap_32(sh->format);
166 sh->context = (void *)kYUVSPixelFormat;
168 int imgfmt = sh->codec->outfmt[sh->outfmtidx];
169 int qt_imgfmt;
170 switch(imgfmt)
172 case IMGFMT_YUY2:
173 qt_imgfmt = kYUVSPixelFormat;
174 break;
175 case IMGFMT_YVU9:
176 qt_imgfmt = 0x73797639; //kYVU9PixelFormat;
177 break;
178 case IMGFMT_YV12:
179 qt_imgfmt = 0x79343230;
180 break;
181 case IMGFMT_UYVY:
182 qt_imgfmt = k2vuyPixelFormat;
183 break;
184 case IMGFMT_YVYU:
185 qt_imgfmt = kYVYU422PixelFormat;
186 imgfmt = IMGFMT_YUY2;
187 break;
188 case IMGFMT_RGB16:
189 qt_imgfmt = k16LE555PixelFormat;
190 break;
191 case IMGFMT_BGR24:
192 qt_imgfmt = k24BGRPixelFormat;
193 break;
194 case IMGFMT_BGR32:
195 qt_imgfmt = k32BGRAPixelFormat;
196 break;
197 case IMGFMT_RGB32:
198 qt_imgfmt = k32RGBAPixelFormat;
199 break;
200 default:
201 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Unknown requested csp\n");
202 return 0;
204 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"imgfmt: %s qt_imgfmt: %.4s\n", vo_format_name(imgfmt), (char *)&qt_imgfmt);
205 sh->context = (void *)qt_imgfmt;
206 if(!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,imgfmt)) return 0;
209 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_STATIC, MP_IMGFLAG_PRESERVE,
210 sh->disp_w, sh->disp_h);
211 if(!mpi) return 0;
213 result = QTNewGWorldFromPtr(
214 &OutBufferGWorld,
215 (OSType)sh->context,
216 &OutBufferRect, //we should benchmark if yvu9 is faster for svq3, too
220 mpi->planes[0],
221 mpi->stride[0]);
222 if (result) {
223 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"QTNewGWorldFromPtr result=%d\n",result);
224 return 0;
227 result = DecompressSequenceBegin(&imageSeq, framedescHandle, (CGrafPtr)OutBufferGWorld,
228 NULL, NULL, NULL, srcCopy, NULL, 0,
229 codecNormalQuality, 0);
230 if(result) {
231 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"DecompressSequenceBegin result=%d\n",result);
232 return 0;
235 return 1;
238 // uninit driver
239 static void uninit(sh_video_t *sh){
240 if(OutBufferGWorld) {
241 DisposeGWorld(OutBufferGWorld);
242 OutBufferGWorld = NULL;
244 if(framedescHandle) {
245 DisposeHandle((Handle)framedescHandle);
246 framedescHandle = NULL;
248 if(imageSeq) {
249 CDSequenceEnd(imageSeq);
250 imageSeq = 0;
252 ExitMovies();
255 // decode a frame
256 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
257 OSErr result = 1;
258 CodecFlags ignore;
260 if(len<=0) return NULL; // skipped frame
262 #ifdef WIN32_LOADER
263 Setup_FS_Segment();
264 #endif
266 result = DecompressSequenceFrameS(imageSeq, data, len, 0, &ignore, NULL);
267 if(result) {
268 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"DecompressSequenceFrameS result=0x%d\n",result);
269 return NULL;
272 if((int)sh->context==0x73797639){ // Sorenson 16-bit YUV -> std YVU9
273 int i;
275 PixMap dstPixMap = **GetGWorldPixMap(OutBufferGWorld);
276 short *src0=(short *)((char*)dstPixMap.baseAddr+0x20);
278 for(i=0;i<mpi->h;i++){
279 int x;
280 unsigned char* dst=mpi->planes[0]+i*mpi->stride[0];
281 unsigned short* src=src0+i*((mpi->w+15)&(~15));
282 for(x=0;x<mpi->w;x++) dst[x]=src[x];
284 src0+=((mpi->w+15)&(~15))*((mpi->h+15)&(~15));
285 for(i=0;i<mpi->h/4;i++){
286 int x;
287 unsigned char* dst=mpi->planes[1]+i*mpi->stride[1];
288 unsigned short* src=src0+i*(((mpi->w+63)&(~63))/4);
289 for(x=0;x<mpi->w/4;x++) dst[x]=src[x];
290 src+=((mpi->w+63)&(~63))/4;
292 src0+=(((mpi->w+63)&(~63))/4)*(((mpi->h+63)&(~63))/4);
293 for(i=0;i<mpi->h/4;i++){
294 int x;
295 unsigned char* dst=mpi->planes[2]+i*mpi->stride[2];
296 unsigned short* src=src0+i*(((mpi->w+63)&(~63))/4);
297 for(x=0;x<mpi->w/4;x++) dst[x]=src[x];
298 src+=((mpi->w+63)&(~63))/4;
304 return mpi;