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