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