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