]> git.sesse.net Git - vlc/blob - modules/codec/quicktime.c
e36cd1872c7ba30cd9f5035fd834a514b958b3ac
[vlc] / modules / codec / quicktime.c
1 /*****************************************************************************
2  * quicktime.c: a quicktime decoder that uses the QT library/dll
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir at via.ecp.fr>
8  *          Derk-Jan Hartman <thedj at users.sf.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include <vlc/vlc.h>
30 #include <vlc_aout.h>
31 #include <vlc_vout.h>
32 #include <vlc_codec.h>
33
34 #if !defined (__APPLE__) && !defined(WIN32)
35 # define LOADER 1
36 #endif
37
38 #ifdef __APPLE__
39 #include <QuickTime/QuickTimeComponents.h>
40 #include <QuickTime/Movies.h>
41 #include <QuickTime/ImageCodec.h>
42 #endif
43
44 /* for windows do we require Quicktime compents header? */
45 #ifdef LOADER
46 #include "qtx/qtxsdk/components.h"
47 #include "wine/windef.h"
48 #include "ldt_keeper.h"
49
50 HMODULE   WINAPI LoadLibraryA(LPCSTR);
51 FARPROC   WINAPI GetProcAddress(HMODULE,LPCSTR);
52 int       WINAPI FreeLibrary(HMODULE);
53
54 #endif
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 static int  Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
61
62 vlc_module_begin();
63     set_description( _("QuickTime library decoder") );
64     set_capability( "decoder", 10 );
65     set_category( CAT_INPUT );
66     set_subcategory( SUBCAT_INPUT_VCODEC );
67     set_callbacks( Open, Close );
68
69 vlc_module_end();
70
71
72 /*****************************************************************************
73  * Local prototypes
74  *****************************************************************************/
75 static int           OpenAudio( decoder_t * );
76 static int           OpenVideo( decoder_t * );
77
78 static aout_buffer_t *DecodeAudio( decoder_t *, block_t ** );
79 static picture_t     *DecodeVideo( decoder_t *, block_t ** );
80
81 #define FCC( a, b , c, d ) \
82     ((uint32_t)( ((a)<<24)|((b)<<16)|((c)<<8)|(d)))
83
84 #ifndef __APPLE__
85 typedef struct OpaqueSoundConverter*    SoundConverter;
86 #ifndef LOADER
87 typedef long                            OSType;
88 typedef int                             OSErr;
89 #endif
90 typedef unsigned long                   UnsignedFixed;
91 typedef uint8_t                         Byte;
92
93 typedef struct SoundComponentData {
94     long                            flags;
95     OSType                          format;
96     short                           numChannels;
97     short                           sampleSize;
98     UnsignedFixed                   sampleRate;
99     long                            sampleCount;
100     Byte *                          buffer;
101     long                            reserved;
102 } SoundComponentData;
103
104 #endif /* __APPLE__ */
105
106 struct decoder_sys_t
107 {
108     /* library */
109 #ifndef __APPLE__
110 #ifdef LOADER
111     ldt_fs_t    *ldt_fs;
112 #endif /* LOADER */
113
114     HMODULE qtml;
115     HINSTANCE qts;
116     OSErr (*InitializeQTML)             ( long flags );
117     OSErr (*TerminateQTML)              ( void );
118 #endif /* __APPLE__ */
119
120     /* Audio */
121     int (*SoundConverterOpen)           ( const SoundComponentData *,
122                                           const SoundComponentData *,
123                                           SoundConverter* );
124     int (*SoundConverterClose)          ( SoundConverter );
125     int (*SoundConverterSetInfo)        ( SoundConverter , OSType, void * );
126     int (*SoundConverterGetBufferSizes) ( SoundConverter, unsigned long,
127                                           unsigned long*, unsigned long*,
128                                           unsigned long* );
129     int (*SoundConverterBeginConversion)( SoundConverter );
130     int (*SoundConverterEndConversion)  ( SoundConverter, void *,
131                                           unsigned long *, unsigned long *);
132     int (*SoundConverterConvertBuffer)  ( SoundConverter, const void *,
133                                           unsigned long, void *,
134                                           unsigned long *, unsigned long * );
135     SoundConverter      myConverter;
136     SoundComponentData  InputFormatInfo, OutputFormatInfo;
137
138     unsigned long   FramesToGet;
139     unsigned int    InFrameSize;
140     unsigned int    OutFrameSize;
141
142 #ifndef WIN32
143     /* Video */
144     Component         (*FindNextComponent)
145         ( Component prev, ComponentDescription* desc );
146
147     ComponentInstance (*OpenComponent)
148         ( Component c );
149
150     ComponentResult   (*ImageCodecInitialize)
151         ( ComponentInstance ci, ImageSubCodecDecompressCapabilities * cap);
152
153     ComponentResult   (*ImageCodecGetCodecInfo)
154         ( ComponentInstance ci, CodecInfo *info );
155
156     ComponentResult   (*ImageCodecPreDecompress)
157         ( ComponentInstance ci, CodecDecompressParams * params );
158
159     ComponentResult   (*ImageCodecBandDecompress)
160         ( ComponentInstance ci, CodecDecompressParams * params );
161
162     PixMapHandle      (*GetGWorldPixMap)
163         ( GWorldPtr offscreenGWorld );
164
165     OSErr             (*QTNewGWorldFromPtr)
166         ( GWorldPtr *gw, OSType pixelFormat, const Rect *boundsRect,
167           CTabHandle cTable, /*GDHandle*/ void *aGDevice, /*unused*/
168           GWorldFlags flags, void *baseAddr, long rowBytes );
169
170     OSErr             (*NewHandleClear)( Size byteCount );
171
172     ComponentInstance       ci;
173     Rect                    OutBufferRect;   /* the dimensions of our GWorld */
174     GWorldPtr               OutBufferGWorld; /* a GWorld is some kind of
175                                                 description for a drawing
176                                                 environment */
177     ImageDescriptionHandle  framedescHandle;
178
179     CodecDecompressParams   decpar;          /* for ImageCodecPreDecompress()*/
180     CodecCapabilities       codeccap;        /* for decpar */
181 #endif
182
183     /* Output properties */
184     uint8_t *           plane;
185     mtime_t             pts;
186     audio_date_t        date;
187
188     int                 i_late; /* video */
189
190     /* buffer */
191     unsigned int        i_buffer;
192     unsigned int        i_buffer_size;
193     uint8_t             *p_buffer;
194
195     /* Audio only */
196     uint8_t             out_buffer[1000000];    /* FIXME */
197     int                 i_out_frames;
198     int                 i_out;
199 };
200
201 static int pi_channels_maps[6] =
202 {
203     0,
204     AOUT_CHAN_CENTER,
205     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
206     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
207     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
208     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
209      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
210 };
211
212 static int QTAudioInit( decoder_t * );
213 static int QTVideoInit( decoder_t * );
214
215 /*****************************************************************************
216  * Open: probe the decoder and return score
217  *****************************************************************************
218  * Tries to launch a decoder and return score so that the interface is able
219  * to choose.
220  *****************************************************************************/
221 static int Open( vlc_object_t *p_this )
222 {
223     decoder_t *p_dec = (decoder_t*)p_this;
224     if( var_GetGlobalMutex( "qt_mutex" ) == NULL )
225         return VLC_EGENERIC;
226
227     /* create a mutex */
228     switch( p_dec->fmt_in.i_codec )
229     {
230         case VLC_FOURCC('S','V','Q','3'): /* Sorenson v3 */
231     /*    case VLC_FOURCC('S','V','Q','1'):  Sorenson v1 
232         case VLC_FOURCC('Z','y','G','o'):
233         case VLC_FOURCC('V','P','3','1'):
234         case VLC_FOURCC('3','I','V','1'): */
235         case VLC_FOURCC('r','l','e',' '): /* QuickTime animation (RLE) */
236         case VLC_FOURCC('r','p','z','a'): /* QuickTime Apple Video */
237         case VLC_FOURCC('a','z','p','r'): /* QuickTime animation (RLE) */
238 #ifdef LOADER
239             p_dec->p_sys = NULL;
240             p_dec->pf_decode_video = DecodeVideo;
241             return VLC_SUCCESS;
242 #else
243             return OpenVideo( p_dec );
244 #endif
245
246         case VLC_FOURCC('s','a','m','r'): /* 3GPP AMR audio */
247         case VLC_FOURCC('m','p','4','a'): /* MPEG-4 audio */
248         case VLC_FOURCC('Q','D','M','C'): /* QDesign */
249         case VLC_FOURCC('Q','D','M','2'): /* QDesign* 2 */
250         case VLC_FOURCC('Q','c','l','p'): /* Qualcomm Purevoice Codec */
251         case VLC_FOURCC('Q','C','L','P'): /* Qualcomm Purevoice Codec */
252         case VLC_FOURCC('M','A','C','3'): /* MACE3 audio decoder */
253         case VLC_FOURCC('M','A','C','6'): /* MACE6 audio decoder */
254         case VLC_FOURCC('d','v','c','a'): /* DV Audio */
255         case VLC_FOURCC('s','o','w','t'): /* 16-bit Little Endian */
256         case VLC_FOURCC('t','w','o','s'): /* 16-bit Big Endian */
257         case VLC_FOURCC('a','l','a','w'): /* ALaw 2:1 */
258         case VLC_FOURCC('u','l','a','w'): /* mu-Law 2:1 */
259         case VLC_FOURCC('r','a','w',' '): /* 8-bit offset binaries */
260         case VLC_FOURCC('f','l','3','2'): /* 32-bit Floating Point */
261         case VLC_FOURCC('f','l','6','4'): /* 64-bit Floating Point */
262         case VLC_FOURCC('i','n','2','4'): /* 24-bit Interger */
263         case VLC_FOURCC('i','n','3','2'): /* 32-bit Integer */
264         case 0x0011:                            /* DVI IMA */
265         case 0x6D730002:                        /* Microsoft ADPCM-ACM */
266         case 0x6D730011:                        /* DVI Intel IMAADPCM-ACM */
267 #ifdef LOADER
268             p_dec->p_sys = NULL;
269             p_dec->pf_decode_audio = DecodeAudio;
270             return VLC_SUCCESS;
271 #else
272             return OpenAudio( p_dec );
273 #endif
274
275         default:
276             return VLC_EGENERIC;
277     }
278 }
279
280 /*****************************************************************************
281  * Close:
282  *****************************************************************************/
283 static void Close( vlc_object_t *p_this )
284 {
285     decoder_t     *p_dec = (decoder_t*)p_this;
286     decoder_sys_t *p_sys = p_dec->p_sys;
287     vlc_mutex_t   *lock;
288
289     /* get lock, avoid segfault */
290     lock = var_GetGlobalMutex( "qt_mutex" );
291     vlc_mutex_lock( lock );
292
293     if( p_dec->fmt_out.i_cat == AUDIO_ES )
294     {
295         int i_error;
296         unsigned long ConvertedFrames=0;
297         unsigned long ConvertedBytes=0;
298
299         i_error = p_sys->SoundConverterEndConversion( p_sys->myConverter, NULL,
300                                                       &ConvertedFrames,
301                                                       &ConvertedBytes );
302         msg_Dbg( p_dec, "SoundConverterEndConversion => %d", i_error );
303
304         i_error = p_sys->SoundConverterClose( p_sys->myConverter );
305         msg_Dbg( p_dec, "SoundConverterClose => %d", i_error );
306
307         free( p_sys->p_buffer );
308     }
309     else if( p_dec->fmt_out.i_cat == VIDEO_ES )
310     {
311         free( p_sys->plane );
312     }
313
314 #ifndef __APPLE__
315     FreeLibrary( p_sys->qtml );
316     FreeLibrary( p_sys->qts );
317     msg_Dbg( p_dec, "FreeLibrary ok." );
318 #else
319     ExitMovies();
320 #endif
321
322 #if 0
323     /* Segfault */
324 #ifdef LOADER
325     Restore_LDT_Keeper( p_sys->ldt_fs );
326     msg_Dbg( p_dec, "Restore_LDT_Keeper" );
327 #endif
328 #endif
329
330     vlc_mutex_unlock( lock );
331
332     if( p_sys ) free( p_sys );
333 }
334
335 /*****************************************************************************
336  * OpenAudio:
337  *****************************************************************************/
338 static int OpenAudio( decoder_t *p_dec )
339 {
340     decoder_sys_t *p_sys;
341     vlc_mutex_t    *lock = var_GetGlobalMutex( "qt_mutex" );
342
343     int             i_error;
344     char            fcc[4];
345     unsigned long   WantedBufferSize;
346     unsigned long   InputBufferSize = 0;
347     unsigned long   OutputBufferSize = 0;
348
349     if( lock == NULL )
350         return VLC_EGENERIC;
351
352     p_sys = calloc( sizeof( decoder_sys_t ), 1 );
353     p_dec->p_sys = p_sys;
354     p_dec->pf_decode_audio = DecodeAudio;
355
356     memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
357
358     /* get lock, avoid segfault */
359     vlc_mutex_lock( lock );
360
361 #ifdef __APPLE__
362     EnterMovies();
363 #endif
364
365     if( QTAudioInit( p_dec ) )
366     {
367         msg_Err( p_dec, "cannot initialize QT");
368         goto exit_error;
369     }
370
371 #ifndef __APPLE__
372     if( ( i_error = p_sys->InitializeQTML( 6 + 16 ) ) )
373     {
374         msg_Dbg( p_dec, "error on InitializeQTML = %d", i_error );
375         goto exit_error;
376     }
377 #endif
378
379     /* input format settings */
380     p_sys->InputFormatInfo.flags       = 0;
381     p_sys->InputFormatInfo.sampleCount = 0;
382     p_sys->InputFormatInfo.buffer      = NULL;
383     p_sys->InputFormatInfo.reserved    = 0;
384     p_sys->InputFormatInfo.numChannels = p_dec->fmt_in.audio.i_channels;
385     p_sys->InputFormatInfo.sampleSize  = p_dec->fmt_in.audio.i_bitspersample;
386     p_sys->InputFormatInfo.sampleRate  = p_dec->fmt_in.audio.i_rate;
387     p_sys->InputFormatInfo.format      = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
388
389     /* output format settings */
390     p_sys->OutputFormatInfo.flags       = 0;
391     p_sys->OutputFormatInfo.sampleCount = 0;
392     p_sys->OutputFormatInfo.buffer      = NULL;
393     p_sys->OutputFormatInfo.reserved    = 0;
394     p_sys->OutputFormatInfo.numChannels = p_dec->fmt_in.audio.i_channels;
395     p_sys->OutputFormatInfo.sampleSize  = 16;
396     p_sys->OutputFormatInfo.sampleRate  = p_dec->fmt_in.audio.i_rate;
397     p_sys->OutputFormatInfo.format      = FCC( 'N', 'O', 'N', 'E' );
398
399
400     i_error = p_sys->SoundConverterOpen( &p_sys->InputFormatInfo,
401                                          &p_sys->OutputFormatInfo,
402                                          &p_sys->myConverter );
403     if( i_error )
404     {
405         msg_Err( p_dec, "error on SoundConverterOpen = %d", i_error );
406         goto exit_error;
407     }
408
409 #if 0
410     /* tell the sound converter we accept VBR formats */
411     i_error = SoundConverterSetInfo( p_dec->myConverter, siClientAcceptsVBR,
412                                      (void *)true );
413 #endif
414
415     if( p_dec->fmt_in.i_extra > 36 + 8 )
416     {
417         i_error = p_sys->SoundConverterSetInfo( p_sys->myConverter,
418                                                 FCC( 'w', 'a', 'v', 'e' ),
419                                                 ((uint8_t*)p_dec->fmt_in.p_extra) + 36 + 8 );
420
421         msg_Dbg( p_dec, "error on SoundConverterSetInfo = %d", i_error );
422     }
423
424     WantedBufferSize = p_sys->OutputFormatInfo.numChannels *
425                        p_sys->OutputFormatInfo.sampleRate * 2;
426     p_sys->FramesToGet = 0;
427
428     i_error = p_sys->SoundConverterGetBufferSizes( p_sys->myConverter,
429                                                    WantedBufferSize,
430                                                    &p_sys->FramesToGet,
431                                                    &InputBufferSize,
432                                                    &OutputBufferSize );
433
434     msg_Dbg( p_dec, "WantedBufferSize=%li InputBufferSize=%li "
435              "OutputBufferSize=%li FramesToGet=%li",
436              WantedBufferSize, InputBufferSize, OutputBufferSize,
437              p_sys->FramesToGet );
438
439     p_sys->InFrameSize  = (InputBufferSize + p_sys->FramesToGet - 1 ) /
440                             p_sys->FramesToGet;
441     p_sys->OutFrameSize = OutputBufferSize / p_sys->FramesToGet;
442
443     msg_Dbg( p_dec, "frame size %d -> %d",
444              p_sys->InFrameSize, p_sys->OutFrameSize );
445
446     if( (i_error = p_sys->SoundConverterBeginConversion(p_sys->myConverter)) )
447     {
448         msg_Err( p_dec,
449                  "error on SoundConverterBeginConversion = %d", i_error );
450         goto exit_error;
451     }
452
453
454     es_format_Init( &p_dec->fmt_out, AUDIO_ES, AOUT_FMT_S16_NE );
455     p_dec->fmt_out.audio.i_rate = p_sys->OutputFormatInfo.sampleRate;
456     p_dec->fmt_out.audio.i_channels = p_sys->OutputFormatInfo.numChannels;
457     p_dec->fmt_out.audio.i_physical_channels =
458     p_dec->fmt_out.audio.i_original_channels =
459         pi_channels_maps[p_sys->OutputFormatInfo.numChannels];
460
461     aout_DateInit( &p_sys->date, p_dec->fmt_out.audio.i_rate );
462
463     p_sys->i_buffer      = 0;
464     p_sys->i_buffer_size = 100*1000;
465     p_sys->p_buffer      = malloc( p_sys->i_buffer_size );
466
467     p_sys->i_out = 0;
468     p_sys->i_out_frames = 0;
469
470     vlc_mutex_unlock( lock );
471     return VLC_SUCCESS;
472
473 exit_error:
474
475 #ifdef LOADER
476     Restore_LDT_Keeper( p_sys->ldt_fs );
477 #endif
478     vlc_mutex_unlock( lock );
479
480     free( p_sys );
481     return VLC_EGENERIC;
482 }
483
484 /*****************************************************************************
485  * DecodeAudio:
486  *****************************************************************************/
487 static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
488 {
489     decoder_sys_t *p_sys = p_dec->p_sys;
490
491     block_t     *p_block;
492     int         i_error;
493
494 #ifdef LOADER
495     /* We must do open and close in the same thread (unless we do
496      * Setup_LDT_Keeper in the main thread before all others */
497     if( p_sys == NULL )
498     {
499         if( OpenAudio( p_dec ) )
500         {
501             /* Fatal */
502             p_dec->b_error = VLC_TRUE;
503             return NULL;
504         }
505
506         p_sys = p_dec->p_sys;
507     }
508 #endif
509
510     if( pp_block == NULL || *pp_block == NULL )
511     {
512         return NULL;
513     }
514     p_block = *pp_block;
515
516     if( p_sys->i_out_frames > 0 && p_sys->i_out >= p_sys->i_out_frames )
517     {
518         /* Ask new data */
519         p_sys->i_out = 0;
520         p_sys->i_out_frames = 0;
521
522         *pp_block = NULL;
523         return NULL;
524     }
525
526     if( p_sys->i_out_frames <= 0 )
527     {
528         if( ( p_sys->pts = p_block->i_pts ) < mdate() )
529         {
530             block_Release( p_block );
531             *pp_block = NULL;
532             return NULL;
533         }
534
535         /* Append data */
536         if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
537         {
538             p_sys->i_buffer_size = p_sys->i_buffer + p_block->i_buffer + 1024;
539             p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
540         }
541         memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer,
542                 p_block->i_buffer );
543         p_sys->i_buffer += p_block->i_buffer;
544
545         if( p_sys->i_buffer > p_sys->InFrameSize )
546         {
547             int i_frames = p_sys->i_buffer / p_sys->InFrameSize;
548             unsigned long i_out_frames, i_out_bytes;
549             vlc_mutex_t *lock = var_GetGlobalMutex( "qt_mutex ");
550
551             vlc_mutex_lock( lock );
552             i_error = p_sys->SoundConverterConvertBuffer( p_sys->myConverter,
553                                                           p_sys->p_buffer,
554                                                           i_frames,
555                                                           p_sys->out_buffer,
556                                                           &i_out_frames,
557                                                           &i_out_bytes );
558             vlc_mutex_unlock( lock );
559
560             /*
561             msg_Dbg( p_dec, "decoded %d frames -> %ld frames (error=%d)",
562                      i_frames, i_out_frames, i_error );
563
564             msg_Dbg( p_dec, "decoded %ld frames = %ld bytes",
565                      i_out_frames, i_out_bytes );
566             */
567
568             p_sys->i_buffer -= i_frames * p_sys->InFrameSize;
569             if( p_sys->i_buffer > 0 )
570             {
571                 memmove( &p_sys->p_buffer[0],
572                          &p_sys->p_buffer[i_frames * p_sys->InFrameSize],
573                          p_sys->i_buffer );
574             }
575
576             if( p_sys->pts != 0 &&
577                 p_sys->pts != aout_DateGet( &p_sys->date ) )
578             {
579                 aout_DateSet( &p_sys->date, p_sys->pts );
580             }
581             else if( !aout_DateGet( &p_sys->date ) )
582             {
583                 return NULL;
584             }
585
586             if( !i_error && i_out_frames > 0 )
587             {
588                 /* we have others samples */
589                 p_sys->i_out_frames = i_out_frames;
590                 p_sys->i_out = 0;
591             }
592         }
593     }
594
595     if( p_sys->i_out < p_sys->i_out_frames )
596     {
597         aout_buffer_t *p_out;
598         int  i_frames = __MIN( p_sys->i_out_frames - p_sys->i_out, 1000 );
599
600         p_out = p_dec->pf_aout_buffer_new( p_dec, i_frames );
601
602         if( p_out )
603         {
604             p_out->start_date = aout_DateGet( &p_sys->date );
605             p_out->end_date = aout_DateIncrement( &p_sys->date, i_frames );
606
607             memcpy( p_out->p_buffer,
608                     &p_sys->out_buffer[2 * p_sys->i_out * p_dec->fmt_out.audio.i_channels],
609                     p_out->i_nb_bytes );
610
611             p_sys->i_out += i_frames;
612         }
613         return p_out;
614     }
615
616     return NULL;
617 }
618
619 /*****************************************************************************
620  * OpenVideo:
621  *****************************************************************************/
622 static int OpenVideo( decoder_t *p_dec )
623 {
624     decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) );
625
626 #ifndef WIN32
627     vlc_mutex_t                        *lock;
628     long                                i_result;
629     ComponentDescription                desc;
630     Component                           prev;
631     ComponentResult                     cres;
632     ImageSubCodecDecompressCapabilities icap;   /* for ImageCodecInitialize() */
633     CodecInfo                           cinfo;  /* for ImageCodecGetCodecInfo() */
634     ImageDescription                    *id;
635
636     char                fcc[4];
637     int     i_vide = p_dec->fmt_in.i_extra;
638     uint8_t *p_vide = p_dec->fmt_in.p_extra;
639
640     p_dec->p_sys = p_sys;
641     p_dec->pf_decode_video = DecodeVideo;
642     p_sys->i_late = 0;
643
644     if( i_vide <= 0 )
645     {
646         msg_Err( p_dec, "missing extra info" );
647         free( p_sys );
648         return VLC_EGENERIC;
649     }
650
651     memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
652     msg_Dbg( p_dec, "quicktime_video %4.4s %dx%d",
653              fcc, p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
654
655     /* get lock, avoid segfault */
656     lock = var_GetGlobalMutex( "qt_mutex" );
657     vlc_mutex_lock( lock );
658
659 #ifdef __APPLE__
660     EnterMovies();
661 #endif
662
663     if( QTVideoInit( p_dec ) )
664     {
665         msg_Err( p_dec, "cannot initialize QT");
666         goto exit_error;
667     }
668
669 #ifndef __APPLE__
670     if( ( i_result = p_sys->InitializeQTML( 6 + 16 ) ) )
671     {
672         msg_Dbg( p_dec, "error on InitializeQTML = %d", (int)i_result );
673         goto exit_error;
674     }
675 #endif
676
677     /* init ComponentDescription */
678     memset( &desc, 0, sizeof( ComponentDescription ) );
679     desc.componentType      = FCC( 'i', 'm', 'd', 'c' );
680     desc.componentSubType   = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
681     desc.componentManufacturer = 0;
682     desc.componentFlags        = 0;
683     desc.componentFlagsMask    = 0;
684
685     if( !( prev = p_sys->FindNextComponent( NULL, &desc ) ) )
686     {
687         msg_Err( p_dec, "cannot find requested component" );
688         goto exit_error;
689     }
690     msg_Dbg( p_dec, "component id=0x%p", prev );
691
692     p_sys->ci =  p_sys->OpenComponent( prev );
693     msg_Dbg( p_dec, "component instance p=0x%p", p_sys->ci );
694
695     memset( &icap, 0, sizeof( ImageSubCodecDecompressCapabilities ) );
696     cres =  p_sys->ImageCodecInitialize( p_sys->ci, &icap );
697     msg_Dbg( p_dec, "ImageCodecInitialize->0x%X size=%d (%d)\n",
698              (int)cres, (int)icap.recordSize, (int)icap.decompressRecordSize);
699
700     memset( &cinfo, 0, sizeof( CodecInfo ) );
701     cres =  p_sys->ImageCodecGetCodecInfo( p_sys->ci, &cinfo );
702     msg_Dbg( p_dec,
703              "Flags: compr: 0x%x decomp: 0x%x format: 0x%x\n",
704              (unsigned int)cinfo.compressFlags,
705              (unsigned int)cinfo.decompressFlags,
706              (unsigned int)cinfo.formatFlags );
707     msg_Dbg( p_dec, "quicktime_video: Codec name: %.*s\n",
708              ((unsigned char*)&cinfo.typeName)[0],
709              ((unsigned char*)&cinfo.typeName)+1 );
710
711     /* make a yuy2 gworld */
712     p_sys->OutBufferRect.top    = 0;
713     p_sys->OutBufferRect.left   = 0;
714     p_sys->OutBufferRect.right  = p_dec->fmt_in.video.i_width;
715     p_sys->OutBufferRect.bottom = p_dec->fmt_in.video.i_height;
716
717
718     /* codec data FIXME use codec not SVQ3 */
719     msg_Dbg( p_dec, "vide = %d", i_vide  );
720     id = malloc( sizeof( ImageDescription ) + ( i_vide - 70 ) );
721     id->idSize          = sizeof( ImageDescription ) + ( i_vide - 70 );
722     id->cType           = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
723     id->version         = GetWBE ( p_vide +  0 );
724     id->revisionLevel   = GetWBE ( p_vide +  2 );
725     id->vendor          = GetDWBE( p_vide +  4 );
726     id->temporalQuality = GetDWBE( p_vide +  8 );
727     id->spatialQuality  = GetDWBE( p_vide + 12 );
728     id->width           = GetWBE ( p_vide + 16 );
729     id->height          = GetWBE ( p_vide + 18 );
730     id->hRes            = GetDWBE( p_vide + 20 );
731     id->vRes            = GetDWBE( p_vide + 24 );
732     id->dataSize        = GetDWBE( p_vide + 28 );
733     id->frameCount      = GetWBE ( p_vide + 32 );
734     memcpy( &id->name, p_vide + 34, 32 );
735     id->depth           = GetWBE ( p_vide + 66 );
736     id->clutID          = GetWBE ( p_vide + 68 );
737     if( i_vide > 70 )
738     {
739         memcpy( ((char*)&id->clutID) + 2, p_vide + 70, i_vide - 70 );
740     }
741
742     msg_Dbg( p_dec, "idSize=%d ver=%d rev=%d vendor=%d tempQ=%d "
743              "spaQ=%d w=%d h=%d dpi=%d%d dataSize=%d depth=%d frameCount=%d clutID=%d",
744              (int)id->idSize, id->version, id->revisionLevel, (int)id->vendor,
745              (int)id->temporalQuality, (int)id->spatialQuality,
746              (int)id->width, (int)id->height,
747              (int)id->hRes, (int)id->vRes,
748              (int)id->dataSize,
749              id->depth,
750              id->frameCount,
751              id->clutID );
752
753     p_sys->framedescHandle = (ImageDescriptionHandle) NewHandleClear( id->idSize );
754     memcpy( *p_sys->framedescHandle, id, id->idSize );
755
756     p_sys->plane = malloc( p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height * 3 );
757
758     i_result = p_sys->QTNewGWorldFromPtr( &p_sys->OutBufferGWorld,
759                                           /*pixel format of new GWorld==YUY2 */
760                                           kYUVSPixelFormat,
761                                           /* we should benchmark if yvu9 is
762                                            * faster for svq3, too */
763                                           &p_sys->OutBufferRect,
764                                           0, 0, 0,
765                                           p_sys->plane,
766                                           p_dec->fmt_in.video.i_width * 2 );
767
768     msg_Dbg( p_dec, "NewGWorldFromPtr returned:%ld\n",
769              65536 - ( i_result&0xffff ) );
770
771     memset( &p_sys->decpar, 0, sizeof( CodecDecompressParams ) );
772     p_sys->decpar.imageDescription = p_sys->framedescHandle;
773     p_sys->decpar.startLine        = 0;
774     p_sys->decpar.stopLine         = ( **p_sys->framedescHandle ).height;
775     p_sys->decpar.frameNumber      = 1;
776     p_sys->decpar.matrixFlags      = 0;
777     p_sys->decpar.matrixType       = 0;
778     p_sys->decpar.matrix           = 0;
779     p_sys->decpar.capabilities     = &p_sys->codeccap;
780     p_sys->decpar.accuracy         = codecNormalQuality;
781     p_sys->decpar.srcRect          = p_sys->OutBufferRect;
782     p_sys->decpar.transferMode     = srcCopy;
783     p_sys->decpar.dstPixMap        = **p_sys->GetGWorldPixMap( p_sys->OutBufferGWorld );/*destPixmap;  */
784
785     cres =  p_sys->ImageCodecPreDecompress( p_sys->ci, &p_sys->decpar );
786     msg_Dbg( p_dec, "quicktime_video: ImageCodecPreDecompress cres=0x%X\n",
787              (int)cres );
788
789     es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC( 'Y', 'U', 'Y', '2' ));
790     p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
791     p_dec->fmt_out.video.i_height= p_dec->fmt_in.video.i_height;
792     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height;
793     
794     vlc_mutex_unlock( lock );
795     return VLC_SUCCESS;
796
797 exit_error:
798 #ifdef LOADER
799     Restore_LDT_Keeper( p_sys->ldt_fs );
800 #endif
801     vlc_mutex_unlock( lock );
802
803 #endif /* !WIN32 */
804
805     return VLC_EGENERIC;
806 }
807
808 #ifndef WIN32
809 /*****************************************************************************
810  * DecodeVideo:
811  *****************************************************************************/
812 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
813 {
814     decoder_sys_t *p_sys = p_dec->p_sys;
815     vlc_mutex_t   *lock;
816     block_t       *p_block;
817     picture_t     *p_pic;
818     mtime_t       i_pts;
819
820     ComponentResult cres;
821
822 #ifdef LOADER
823     /* We must do open and close in the same thread (unless we do
824      * Setup_LDT_Keeper in the main thread before all others */
825     if( p_sys == NULL )
826     {
827         if( OpenVideo( p_dec ) )
828         {
829             /* Fatal */
830             p_dec->b_error = VLC_TRUE;
831             return NULL;
832         }
833         p_sys = p_dec->p_sys;
834     }
835 #endif
836
837     if( pp_block == NULL || *pp_block == NULL )
838     {
839         return NULL;
840     }
841     p_block = *pp_block;
842     *pp_block = NULL;
843     
844     i_pts = p_block->i_pts ? p_block->i_pts : p_block->i_dts;
845
846     if( i_pts < mdate() )
847     {
848         p_sys->i_late++;
849     }
850     else
851     {
852         p_sys->i_late = 0;
853     }
854     msg_Dbg( p_dec, "bufsize: %d", p_block->i_buffer);
855
856     if( p_sys->i_late > 10 )
857     {
858         msg_Dbg( p_dec, "too late buffer -> dropped" );
859         block_Release( p_block );
860         return NULL;
861     }
862     
863     lock = var_GetGlobalMutex( "qt_mutex" );
864     vlc_mutex_lock( lock );
865
866     if( ( p_pic = p_dec->pf_vout_buffer_new( p_dec ) ) )
867     {
868         p_sys->decpar.data                  = (Ptr)p_block->p_buffer;
869         p_sys->decpar.bufferSize            = p_block->i_buffer;
870         (**p_sys->framedescHandle).dataSize = p_block->i_buffer;
871
872         cres = p_sys->ImageCodecBandDecompress( p_sys->ci, &p_sys->decpar );
873
874         ++p_sys->decpar.frameNumber;
875
876         if( cres &0xFFFF )
877         {
878             msg_Dbg( p_dec, "quicktime_video: ImageCodecBandDecompress"
879                      " cres=0x%X (-0x%X) %d :(\n",
880                      (int)cres,(int)-cres, (int)cres );
881         }
882
883         memcpy( p_pic->p[0].p_pixels, p_sys->plane,
884                 p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height * 2 );
885         p_pic->date = i_pts;
886     }
887     
888     vlc_mutex_unlock( lock );
889
890     block_Release( p_block );
891     return p_pic;
892 }
893 #endif /* !WIN32 */
894
895 /*****************************************************************************
896  * QTAudioInit:
897  *****************************************************************************/
898 static int QTAudioInit( decoder_t *p_dec )
899 {
900     decoder_sys_t *p_sys = p_dec->p_sys;
901
902 #ifdef __APPLE__
903     p_sys->SoundConverterOpen       = (void*)SoundConverterOpen;
904     p_sys->SoundConverterClose      = (void*)SoundConverterClose;
905     p_sys->SoundConverterSetInfo    = (void*)SoundConverterSetInfo;
906     p_sys->SoundConverterGetBufferSizes = (void*)SoundConverterGetBufferSizes;
907     p_sys->SoundConverterConvertBuffer  = (void*)SoundConverterConvertBuffer;
908     p_sys->SoundConverterBeginConversion= (void*)SoundConverterBeginConversion;
909     p_sys->SoundConverterEndConversion  = (void*)SoundConverterEndConversion;
910 #else
911
912 #ifdef LOADER
913     p_sys->ldt_fs = Setup_LDT_Keeper();
914 #endif /* LOADER */
915
916     p_sys->qts = LoadLibraryA( "QuickTime.qts" );
917     if( p_sys->qts == NULL )
918     {
919         msg_Dbg( p_dec, "failed loading QuickTime.qts" );
920         return VLC_EGENERIC;
921     }
922     p_sys->qtml = LoadLibraryA( "qtmlClient.dll" );
923     if( p_sys->qtml == NULL )
924     {
925         msg_Dbg( p_dec, "failed loading qtmlClient.dll" );
926         return VLC_EGENERIC;
927     }
928
929     p_sys->InitializeQTML               = (void *)GetProcAddress( p_sys->qtml, "InitializeQTML" );
930     p_sys->TerminateQTML                = (void *)GetProcAddress( p_sys->qtml, "TerminateQTML" );
931     p_sys->SoundConverterOpen           = (void *)GetProcAddress( p_sys->qtml, "SoundConverterOpen" );
932     p_sys->SoundConverterClose          = (void *)GetProcAddress( p_sys->qtml, "SoundConverterClose" );
933     p_sys->SoundConverterSetInfo        = (void *)GetProcAddress( p_sys->qtml, "SoundConverterSetInfo" );
934     p_sys->SoundConverterGetBufferSizes = (void *)GetProcAddress( p_sys->qtml, "SoundConverterGetBufferSizes" );
935     p_sys->SoundConverterConvertBuffer  = (void *)GetProcAddress( p_sys->qtml, "SoundConverterConvertBuffer" );
936     p_sys->SoundConverterEndConversion  = (void *)GetProcAddress( p_sys->qtml, "SoundConverterEndConversion" );
937     p_sys->SoundConverterBeginConversion= (void *)GetProcAddress( p_sys->qtml, "SoundConverterBeginConversion");
938
939     if( p_sys->InitializeQTML == NULL )
940     {
941         msg_Err( p_dec, "failed getting proc address InitializeQTML" );
942         return VLC_EGENERIC;
943     }
944     if( p_sys->SoundConverterOpen == NULL ||
945         p_sys->SoundConverterClose == NULL ||
946         p_sys->SoundConverterSetInfo == NULL ||
947         p_sys->SoundConverterGetBufferSizes == NULL ||
948         p_sys->SoundConverterConvertBuffer == NULL ||
949         p_sys->SoundConverterEndConversion == NULL ||
950         p_sys->SoundConverterBeginConversion == NULL )
951     {
952         msg_Err( p_dec, "failed getting proc address" );
953         return VLC_EGENERIC;
954     }
955
956     msg_Dbg( p_dec, "standard init done" );
957 #endif /* else __APPLE__ */
958
959     return VLC_SUCCESS;
960 }
961
962 #ifndef WIN32
963 /*****************************************************************************
964  * QTVideoInit:
965  *****************************************************************************/
966 static int QTVideoInit( decoder_t *p_dec )
967 {
968     decoder_sys_t *p_sys = p_dec->p_sys;
969
970 #ifdef __APPLE__
971     p_sys->FindNextComponent        = (void*)FindNextComponent;
972     p_sys->OpenComponent            = (void*)OpenComponent;
973     p_sys->ImageCodecInitialize     = (void*)ImageCodecInitialize;
974     p_sys->ImageCodecGetCodecInfo   = (void*)ImageCodecGetCodecInfo;
975     p_sys->ImageCodecPreDecompress  = (void*)ImageCodecPreDecompress;
976     p_sys->ImageCodecBandDecompress = (void*)ImageCodecBandDecompress;
977     p_sys->GetGWorldPixMap          = (void*)GetGWorldPixMap;
978     p_sys->QTNewGWorldFromPtr       = (void*)QTNewGWorldFromPtr;
979     p_sys->NewHandleClear           = (void*)NewHandleClear;
980 #else
981
982 #ifdef LOADER
983     p_sys->ldt_fs = Setup_LDT_Keeper();
984 #endif /* LOADER */
985     p_sys->qts = LoadLibraryA( "QuickTime.qts" );
986     if( p_sys->qts == NULL )
987     {
988         msg_Dbg( p_dec, "failed loading QuickTime.qts" );
989         return VLC_EGENERIC;
990     }
991     msg_Dbg( p_dec, "QuickTime.qts loaded" );
992     p_sys->qtml = LoadLibraryA( "qtmlClient.dll" );
993     if( p_sys->qtml == NULL )
994     {
995         msg_Dbg( p_dec, "failed loading qtmlClient.dll" );
996         return VLC_EGENERIC;
997     }
998     msg_Dbg( p_dec, "qtmlClient.dll loaded" );
999
1000     /* (void*) to shut up gcc */
1001     p_sys->InitializeQTML           = (void*)GetProcAddress( p_sys->qtml, "InitializeQTML" );
1002     p_sys->FindNextComponent        = (void*)GetProcAddress( p_sys->qtml, "FindNextComponent" );
1003     p_sys->OpenComponent            = (void*)GetProcAddress( p_sys->qtml, "OpenComponent" );
1004     p_sys->ImageCodecInitialize     = (void*)GetProcAddress( p_sys->qtml, "ImageCodecInitialize" );
1005     p_sys->ImageCodecGetCodecInfo   = (void*)GetProcAddress( p_sys->qtml, "ImageCodecGetCodecInfo" );
1006     p_sys->ImageCodecPreDecompress  = (void*)GetProcAddress( p_sys->qtml, "ImageCodecPreDecompress" );
1007     p_sys->ImageCodecBandDecompress = (void*)GetProcAddress( p_sys->qtml, "ImageCodecBandDecompress" );
1008     p_sys->GetGWorldPixMap          = (void*)GetProcAddress( p_sys->qtml, "GetGWorldPixMap" );
1009     p_sys->QTNewGWorldFromPtr       = (void*)GetProcAddress( p_sys->qtml, "QTNewGWorldFromPtr" );
1010     p_sys->NewHandleClear           = (void*)GetProcAddress( p_sys->qtml, "NewHandleClear" );
1011
1012     if( p_sys->InitializeQTML == NULL )
1013     {
1014         msg_Dbg( p_dec, "failed getting proc address InitializeQTML" );
1015         return VLC_EGENERIC;
1016     }
1017     if( p_sys->FindNextComponent == NULL ||
1018         p_sys->OpenComponent == NULL ||
1019         p_sys->ImageCodecInitialize == NULL ||
1020         p_sys->ImageCodecGetCodecInfo == NULL ||
1021         p_sys->ImageCodecPreDecompress == NULL ||
1022         p_sys->ImageCodecBandDecompress == NULL ||
1023         p_sys->GetGWorldPixMap == NULL ||
1024         p_sys->QTNewGWorldFromPtr == NULL ||
1025         p_sys->NewHandleClear == NULL )
1026     {
1027         msg_Err( p_dec, "failed getting proc address" );
1028         return VLC_EGENERIC;
1029     }
1030 #endif /* __APPLE__ */
1031
1032     return VLC_SUCCESS;
1033 }
1034 #endif /* !WIN32 */