demux_lavf: for avi, use packet .dts instead of .pts
[mplayer/glamo.git] / libmpcodecs / vd_qtvideo.c
blobe18a88a2590f384e90dacbbc7c7a6bb77467ecae
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;
101 int extradata_size = sh->bih ? sh->bih->biSize - sizeof(*sh->bih) : 0;
102 void *extradata = sh->bih + 1;
104 if (!sh->ImageDesc)
105 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"sh->ImageDesc not set, try -demuxer mov if this fails.\n");
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 && extradata_size >= sizeof(ImageDescription) &&
160 ((ImageDescription *)extradata)->idSize <= extradata_size)
161 sh->ImageDesc = extradata;
162 if (sh->ImageDesc) {
163 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"ImageDescription size: %d\n",((ImageDescription*)(sh->ImageDesc))->idSize);
164 framedescHandle=(ImageDescriptionHandle)NewHandleClear(((ImageDescription*)(sh->ImageDesc))->idSize);
165 memcpy(*framedescHandle,sh->ImageDesc,((ImageDescription*)(sh->ImageDesc))->idSize);
166 } else {
167 // assume extradata consists only of the atoms, build the other parts
168 ImageDescription *idesc;
169 int size = sizeof(*idesc) + extradata_size;
170 mp_msg(MSGT_DECVIDEO, MSGL_V, "Generating a ImageDescription\n");
171 framedescHandle=(ImageDescriptionHandle)NewHandleClear(size);
172 idesc = *framedescHandle;
173 memcpy(idesc + 1, extradata, extradata_size);
174 idesc->idSize = size;
175 idesc->width = sh->disp_w;
176 idesc->height = sh->disp_h;
178 dump_ImageDescription(*framedescHandle);
180 (**framedescHandle).cType = bswap_32(sh->format);
181 sh->context = (void *)kYUVSPixelFormat;
183 int imgfmt = sh->codec->outfmt[sh->outfmtidx];
184 int qt_imgfmt;
185 switch(imgfmt)
187 case IMGFMT_YUY2:
188 qt_imgfmt = kYUVSPixelFormat;
189 break;
190 case IMGFMT_YVU9:
191 qt_imgfmt = 0x73797639; //kYVU9PixelFormat;
192 break;
193 case IMGFMT_YV12:
194 qt_imgfmt = 0x79343230;
195 break;
196 case IMGFMT_UYVY:
197 qt_imgfmt = k2vuyPixelFormat;
198 break;
199 case IMGFMT_YVYU:
200 qt_imgfmt = kYVYU422PixelFormat;
201 imgfmt = IMGFMT_YUY2;
202 break;
203 case IMGFMT_RGB16:
204 qt_imgfmt = k16LE555PixelFormat;
205 break;
206 case IMGFMT_BGR24:
207 qt_imgfmt = k24BGRPixelFormat;
208 break;
209 case IMGFMT_BGR32:
210 qt_imgfmt = k32BGRAPixelFormat;
211 break;
212 case IMGFMT_RGB32:
213 qt_imgfmt = k32RGBAPixelFormat;
214 break;
215 default:
216 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Unknown requested csp\n");
217 return 0;
219 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"imgfmt: %s qt_imgfmt: %.4s\n", vo_format_name(imgfmt), (char *)&qt_imgfmt);
220 sh->context = (void *)qt_imgfmt;
221 if(!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,imgfmt)) return 0;
224 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_STATIC, MP_IMGFLAG_PRESERVE,
225 sh->disp_w, sh->disp_h);
226 if(!mpi) return 0;
228 result = QTNewGWorldFromPtr(
229 &OutBufferGWorld,
230 (OSType)sh->context,
231 &OutBufferRect, //we should benchmark if yvu9 is faster for svq3, too
235 mpi->planes[0],
236 mpi->stride[0]);
237 if (result) {
238 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"QTNewGWorldFromPtr result=%d\n",result);
239 return 0;
242 result = DecompressSequenceBegin(&imageSeq, framedescHandle, (CGrafPtr)OutBufferGWorld,
243 NULL, NULL, NULL, srcCopy, NULL, 0,
244 codecNormalQuality, 0);
245 if(result) {
246 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"DecompressSequenceBegin result=%d\n",result);
247 return 0;
250 return 1;
253 // uninit driver
254 static void uninit(sh_video_t *sh){
255 if(OutBufferGWorld) {
256 DisposeGWorld(OutBufferGWorld);
257 OutBufferGWorld = NULL;
259 if(framedescHandle) {
260 DisposeHandle((Handle)framedescHandle);
261 framedescHandle = NULL;
263 if(imageSeq) {
264 CDSequenceEnd(imageSeq);
265 imageSeq = 0;
267 ExitMovies();
270 // decode a frame
271 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
272 OSErr result = 1;
273 CodecFlags ignore;
275 if(len<=0) return NULL; // skipped frame
277 #if defined(WIN32_LOADER) && !defined(CONFIG_QUICKTIME)
278 Setup_FS_Segment();
279 #endif
281 result = DecompressSequenceFrameS(imageSeq, data, len, 0, &ignore, NULL);
282 if(result) {
283 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"DecompressSequenceFrameS result=0x%d\n",result);
284 return NULL;
287 if((int)sh->context==0x73797639){ // Sorenson 16-bit YUV -> std YVU9
288 int i;
290 PixMap dstPixMap = **GetGWorldPixMap(OutBufferGWorld);
291 short *src0=(short *)((char*)dstPixMap.baseAddr+0x20);
293 for(i=0;i<mpi->h;i++){
294 int x;
295 unsigned char* dst=mpi->planes[0]+i*mpi->stride[0];
296 unsigned short* src=src0+i*((mpi->w+15)&(~15));
297 for(x=0;x<mpi->w;x++) dst[x]=src[x];
299 src0+=((mpi->w+15)&(~15))*((mpi->h+15)&(~15));
300 for(i=0;i<mpi->h/4;i++){
301 int x;
302 unsigned char* dst=mpi->planes[1]+i*mpi->stride[1];
303 unsigned short* src=src0+i*(((mpi->w+63)&(~63))/4);
304 for(x=0;x<mpi->w/4;x++) dst[x]=src[x];
305 src+=((mpi->w+63)&(~63))/4;
307 src0+=(((mpi->w+63)&(~63))/4)*(((mpi->h+63)&(~63))/4);
308 for(i=0;i<mpi->h/4;i++){
309 int x;
310 unsigned char* dst=mpi->planes[2]+i*mpi->stride[2];
311 unsigned short* src=src0+i*(((mpi->w+63)&(~63))/4);
312 for(x=0;x<mpi->w/4;x++) dst[x]=src[x];
313 src+=((mpi->w+63)&(~63))/4;
319 return mpi;