Use static mutexes
[vlc.git] / 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 <hartman at videolan.org>>
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", 0 );
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 const 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 #ifdef __APPLE__
231     OSErr err;
232     SInt32 qtVersion, macosversion;
233     
234     err = Gestalt(gestaltQuickTimeVersion, &qtVersion);
235     err = Gestalt(gestaltSystemVersion, &macosversion);
236 #ifndef NDEBUG
237     msg_Dbg( p_this, "mac os version is %#lx", macosversion );
238     msg_Dbg( p_this, "quicktime version is %#lx", qtVersion );
239 #endif
240 #endif
241
242     switch( p_dec->fmt_in.i_codec )
243     {
244         case VLC_FOURCC('h','2','6','4'): /* H.264 */
245         case VLC_FOURCC('c','v','i','d'): /* Cinepak */
246         case VLC_FOURCC('I','V','4','1'): /* Indeo Video IV */
247         case VLC_FOURCC('i','v','4','1'): /* dto. */
248 #ifdef __APPLE__
249         case VLC_FOURCC('p','x','l','t'): /* Pixlet */
250 #endif
251         case VLC_FOURCC('d','v','1','n'): /* DVC Pro 100 NTSC */
252         case VLC_FOURCC('d','v','1','p'): /* DVC Pro 100 PAL */
253         case VLC_FOURCC('d','v','h','p'): /* DVC PRO HD 720p */
254         case VLC_FOURCC('d','v','h','6'): /* DVC PRO HD 1080i 60 */
255         case VLC_FOURCC('d','v','h','5'): /* DVC PRO HD 1080i 50 */
256
257         case VLC_FOURCC('S','V','Q','3'): /* Sorenson v3 */
258     /*    case VLC_FOURCC('S','V','Q','1'):  Sorenson v1
259         case VLC_FOURCC('Z','y','G','o'):
260         case VLC_FOURCC('V','P','3','1'):
261         case VLC_FOURCC('3','I','V','1'): */
262         case VLC_FOURCC('r','l','e',' '): /* QuickTime animation (RLE) */
263         case VLC_FOURCC('r','p','z','a'): /* QuickTime Apple Video */
264         case VLC_FOURCC('a','z','p','r'): /* QuickTime animation (RLE) */
265 #ifdef LOADER
266         p_dec->p_sys = NULL;
267         p_dec->pf_decode_video = DecodeVideo;
268         return VLC_SUCCESS;
269 #else
270         return OpenVideo( p_dec );
271 #endif
272
273 #ifdef __APPLE__
274         case VLC_FOURCC('I','L','B','C'): /* iLBC */
275             if ((err != noErr) || (qtVersion < 0x07500000)) 
276                 return VLC_EGENERIC;
277         case VLC_FOURCC('i','l','b','c'): /* iLBC */
278             if ((err != noErr) || (qtVersion < 0x07500000)) 
279                 return VLC_EGENERIC;
280 #endif
281         case VLC_FOURCC('s','a','m','r'): /* 3GPP AMR audio */
282         case VLC_FOURCC('s','a','m','b'): /* 3GPP AMR-WB audio */
283         case VLC_FOURCC('m','p','4','a'): /* MPEG-4 audio */
284         case VLC_FOURCC('Q','D','M','C'): /* QDesign */
285         case VLC_FOURCC('Q','D','M','2'): /* QDesign* 2 */
286         case VLC_FOURCC('Q','c','l','p'): /* Qualcomm Purevoice Codec */
287         case VLC_FOURCC('Q','C','L','P'): /* Qualcomm Purevoice Codec */
288         case VLC_FOURCC('M','A','C','3'): /* MACE3 audio decoder */
289         case VLC_FOURCC('M','A','C','6'): /* MACE6 audio decoder */
290         case VLC_FOURCC('d','v','c','a'): /* DV Audio */
291         case VLC_FOURCC('s','o','w','t'): /* 16-bit Little Endian */
292         case VLC_FOURCC('t','w','o','s'): /* 16-bit Big Endian */
293         case VLC_FOURCC('a','l','a','w'): /* ALaw 2:1 */
294         case VLC_FOURCC('u','l','a','w'): /* mu-Law 2:1 */
295         case VLC_FOURCC('r','a','w',' '): /* 8-bit offset binaries */
296         case VLC_FOURCC('f','l','3','2'): /* 32-bit Floating Point */
297         case VLC_FOURCC('f','l','6','4'): /* 64-bit Floating Point */
298         case VLC_FOURCC('i','n','2','4'): /* 24-bit Interger */
299         case VLC_FOURCC('i','n','3','2'): /* 32-bit Integer */
300         case 0x0011:                            /* DVI IMA */
301         case 0x6D730002:                        /* Microsoft ADPCM-ACM */
302         case 0x6D730011:                        /* DVI Intel IMAADPCM-ACM */
303 #ifdef LOADER
304         p_dec->p_sys = NULL;
305         p_dec->pf_decode_audio = DecodeAudio;
306         return VLC_SUCCESS;
307 #else
308
309 #ifdef __APPLE__
310         /* FIXME: right now, we don't support audio decoding on 10.5 and later
311          because we are still using the hardcore-outdated SoundManager API,
312          which was removed after 10.4 */
313
314         if( macosversion >= 0x1050 || err != noErr )
315         {
316             msg_Warn( p_dec, "Your Mac OS version doesn't have SoundManager anymore. "
317                      "You can't use this plugin for audio." );
318             return VLC_EGENERIC;
319         }
320 #endif
321         return OpenAudio( p_dec );
322 #endif
323
324         default:
325             return VLC_EGENERIC;
326     }
327 }
328
329 static vlc_mutex_t qt_mutex = VLC_STATIC_MUTEX;
330
331 /*****************************************************************************
332  * Close:
333  *****************************************************************************/
334 static void Close( vlc_object_t *p_this )
335 {
336     decoder_t     *p_dec = (decoder_t*)p_this;
337     decoder_sys_t *p_sys = p_dec->p_sys;
338
339     /* get lock, avoid segfault */
340     vlc_mutex_lock( &qt_mutex );
341
342     if( p_dec->fmt_out.i_cat == AUDIO_ES )
343     {
344         int i_error;
345         unsigned long ConvertedFrames=0;
346         unsigned long ConvertedBytes=0;
347
348         i_error = p_sys->SoundConverterEndConversion( p_sys->myConverter, NULL,
349                                                       &ConvertedFrames,
350                                                       &ConvertedBytes );
351         msg_Dbg( p_dec, "SoundConverterEndConversion => %d", i_error );
352
353         i_error = p_sys->SoundConverterClose( p_sys->myConverter );
354         msg_Dbg( p_dec, "SoundConverterClose => %d", i_error );
355
356         free( p_sys->p_buffer );
357     }
358     else if( p_dec->fmt_out.i_cat == VIDEO_ES )
359     {
360         free( p_sys->plane );
361     }
362
363 #ifndef __APPLE__
364     FreeLibrary( p_sys->qtml );
365     FreeLibrary( p_sys->qts );
366     msg_Dbg( p_dec, "FreeLibrary ok." );
367 #else
368     ExitMovies();
369 #endif
370
371 #if 0
372     /* Segfault */
373 #ifdef LOADER
374     Restore_LDT_Keeper( p_sys->ldt_fs );
375     msg_Dbg( p_dec, "Restore_LDT_Keeper" );
376 #endif
377 #endif
378
379     vlc_mutex_unlock( &qt_mutex );
380
381     free( p_sys );
382 }
383
384 /*****************************************************************************
385  * OpenAudio:
386  *****************************************************************************/
387 static int OpenAudio( decoder_t *p_dec )
388 {
389     decoder_sys_t *p_sys;
390
391     int             i_error;
392     char            fcc[4];
393     unsigned long   WantedBufferSize;
394     unsigned long   InputBufferSize = 0;
395     unsigned long   OutputBufferSize = 0;
396
397     /* get lock, avoid segfault */
398     vlc_mutex_lock( &qt_mutex );
399
400     p_sys = calloc( sizeof( decoder_sys_t ), 1 );
401     p_dec->p_sys = p_sys;
402     p_dec->pf_decode_audio = DecodeAudio;
403
404     memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
405
406 #ifdef __APPLE__
407     EnterMovies();
408 #endif
409
410     if( QTAudioInit( p_dec ) )
411     {
412         msg_Err( p_dec, "cannot initialize QT");
413         goto exit_error;
414     }
415
416 #ifndef __APPLE__
417     if( ( i_error = p_sys->InitializeQTML( 6 + 16 ) ) )
418     {
419         msg_Dbg( p_dec, "error on InitializeQTML = %d", i_error );
420         goto exit_error;
421     }
422 #endif
423
424     /* input format settings */
425     p_sys->InputFormatInfo.flags       = 0;
426     p_sys->InputFormatInfo.sampleCount = 0;
427     p_sys->InputFormatInfo.buffer      = NULL;
428     p_sys->InputFormatInfo.reserved    = 0;
429     p_sys->InputFormatInfo.numChannels = p_dec->fmt_in.audio.i_channels;
430     p_sys->InputFormatInfo.sampleSize  = p_dec->fmt_in.audio.i_bitspersample;
431     p_sys->InputFormatInfo.sampleRate  = p_dec->fmt_in.audio.i_rate;
432     p_sys->InputFormatInfo.format      = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
433
434     /* output format settings */
435     p_sys->OutputFormatInfo.flags       = 0;
436     p_sys->OutputFormatInfo.sampleCount = 0;
437     p_sys->OutputFormatInfo.buffer      = NULL;
438     p_sys->OutputFormatInfo.reserved    = 0;
439     p_sys->OutputFormatInfo.numChannels = p_dec->fmt_in.audio.i_channels;
440     p_sys->OutputFormatInfo.sampleSize  = 16;
441     p_sys->OutputFormatInfo.sampleRate  = p_dec->fmt_in.audio.i_rate;
442     p_sys->OutputFormatInfo.format      = FCC( 'N', 'O', 'N', 'E' );
443
444
445     i_error = p_sys->SoundConverterOpen( &p_sys->InputFormatInfo,
446                                          &p_sys->OutputFormatInfo,
447                                          &p_sys->myConverter );
448     if( i_error )
449     {
450         msg_Err( p_dec, "error on SoundConverterOpen = %d", i_error );
451         goto exit_error;
452     }
453
454 #if 0
455     /* tell the sound converter we accept VBR formats */
456     i_error = SoundConverterSetInfo( p_dec->myConverter, siClientAcceptsVBR,
457                                      (void *)true );
458 #endif
459
460     if( p_dec->fmt_in.i_extra > 36 + 8 )
461     {
462         i_error = p_sys->SoundConverterSetInfo( p_sys->myConverter,
463                                                 FCC( 'w', 'a', 'v', 'e' ),
464                                                 ((uint8_t*)p_dec->fmt_in.p_extra) + 36 + 8 );
465
466         msg_Dbg( p_dec, "error on SoundConverterSetInfo = %d", i_error );
467     }
468
469     WantedBufferSize = p_sys->OutputFormatInfo.numChannels *
470                        p_sys->OutputFormatInfo.sampleRate * 2;
471     p_sys->FramesToGet = 0;
472
473     i_error = p_sys->SoundConverterGetBufferSizes( p_sys->myConverter,
474                                                    WantedBufferSize,
475                                                    &p_sys->FramesToGet,
476                                                    &InputBufferSize,
477                                                    &OutputBufferSize );
478
479     msg_Dbg( p_dec, "WantedBufferSize=%li InputBufferSize=%li "
480              "OutputBufferSize=%li FramesToGet=%li",
481              WantedBufferSize, InputBufferSize, OutputBufferSize,
482              p_sys->FramesToGet );
483
484     p_sys->InFrameSize  = (InputBufferSize + p_sys->FramesToGet - 1 ) /
485                             p_sys->FramesToGet;
486     p_sys->OutFrameSize = OutputBufferSize / p_sys->FramesToGet;
487
488     msg_Dbg( p_dec, "frame size %d -> %d",
489              p_sys->InFrameSize, p_sys->OutFrameSize );
490
491     if( (i_error = p_sys->SoundConverterBeginConversion(p_sys->myConverter)) )
492     {
493         msg_Err( p_dec,
494                  "error on SoundConverterBeginConversion = %d", i_error );
495         goto exit_error;
496     }
497
498
499     es_format_Init( &p_dec->fmt_out, AUDIO_ES, AOUT_FMT_S16_NE );
500     p_dec->fmt_out.audio.i_rate = p_sys->OutputFormatInfo.sampleRate;
501     p_dec->fmt_out.audio.i_channels = p_sys->OutputFormatInfo.numChannels;
502     p_dec->fmt_out.audio.i_physical_channels =
503     p_dec->fmt_out.audio.i_original_channels =
504         pi_channels_maps[p_sys->OutputFormatInfo.numChannels];
505
506     aout_DateInit( &p_sys->date, p_dec->fmt_out.audio.i_rate );
507
508     p_sys->i_buffer      = 0;
509     p_sys->i_buffer_size = 100*1000;
510     p_sys->p_buffer      = malloc( p_sys->i_buffer_size );
511     if( !p_sys->p_buffer )
512         goto exit_error;
513
514     p_sys->i_out = 0;
515     p_sys->i_out_frames = 0;
516
517     vlc_mutex_unlock( &qt_mutex );
518     return VLC_SUCCESS;
519
520 exit_error:
521
522 #ifdef LOADER
523     Restore_LDT_Keeper( p_sys->ldt_fs );
524 #endif
525     vlc_mutex_unlock( &qt_mutex );
526
527     free( p_sys );
528     return VLC_EGENERIC;
529 }
530
531 /*****************************************************************************
532  * DecodeAudio:
533  *****************************************************************************/
534 static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
535 {
536     decoder_sys_t *p_sys = p_dec->p_sys;
537
538     block_t     *p_block;
539     int         i_error;
540
541 #ifdef LOADER
542     /* We must do open and close in the same thread (unless we do
543      * Setup_LDT_Keeper in the main thread before all others */
544     if( p_sys == NULL )
545     {
546         if( OpenAudio( p_dec ) )
547         {
548             /* Fatal */
549             p_dec->b_error = true;
550             return NULL;
551         }
552
553         p_sys = p_dec->p_sys;
554     }
555 #endif
556
557     if( pp_block == NULL || *pp_block == NULL )
558     {
559         return NULL;
560     }
561     p_block = *pp_block;
562
563     if( p_sys->i_out_frames > 0 && p_sys->i_out >= p_sys->i_out_frames )
564     {
565         /* Ask new data */
566         p_sys->i_out = 0;
567         p_sys->i_out_frames = 0;
568
569         *pp_block = NULL;
570         return NULL;
571     }
572
573     if( p_sys->i_out_frames <= 0 )
574     {
575         p_sys->pts = p_block->i_pts;
576         if( decoder_GetDisplayDate( p_dec, p_block->i_pts ) < mdate() )
577         {
578             block_Release( p_block );
579             *pp_block = NULL;
580             return NULL;
581         }
582
583         /* Append data */
584         if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
585         {
586             p_sys->i_buffer_size = p_sys->i_buffer + p_block->i_buffer + 1024;
587             p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
588         }
589         memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer,
590                 p_block->i_buffer );
591         p_sys->i_buffer += p_block->i_buffer;
592
593         if( p_sys->i_buffer > p_sys->InFrameSize )
594         {
595             int i_frames = p_sys->i_buffer / p_sys->InFrameSize;
596             unsigned long i_out_frames, i_out_bytes;
597             vlc_mutex_lock( &qt_mutex );
598
599             i_error = p_sys->SoundConverterConvertBuffer( p_sys->myConverter,
600                                                           p_sys->p_buffer,
601                                                           i_frames,
602                                                           p_sys->out_buffer,
603                                                           &i_out_frames,
604                                                           &i_out_bytes );
605             vlc_mutex_unlock( &qt_mutex );
606
607             /*
608             msg_Dbg( p_dec, "decoded %d frames -> %ld frames (error=%d)",
609                      i_frames, i_out_frames, i_error );
610
611             msg_Dbg( p_dec, "decoded %ld frames = %ld bytes",
612                      i_out_frames, i_out_bytes );
613             */
614
615             p_sys->i_buffer -= i_frames * p_sys->InFrameSize;
616             if( p_sys->i_buffer > 0 )
617             {
618                 memmove( &p_sys->p_buffer[0],
619                          &p_sys->p_buffer[i_frames * p_sys->InFrameSize],
620                          p_sys->i_buffer );
621             }
622
623             if( p_sys->pts != 0 &&
624                 p_sys->pts != aout_DateGet( &p_sys->date ) )
625             {
626                 aout_DateSet( &p_sys->date, p_sys->pts );
627             }
628             else if( !aout_DateGet( &p_sys->date ) )
629             {
630                 return NULL;
631             }
632
633             if( !i_error && i_out_frames > 0 )
634             {
635                 /* we have others samples */
636                 p_sys->i_out_frames = i_out_frames;
637                 p_sys->i_out = 0;
638             }
639         }
640     }
641
642     if( p_sys->i_out < p_sys->i_out_frames )
643     {
644         aout_buffer_t *p_out;
645         int  i_frames = __MIN( p_sys->i_out_frames - p_sys->i_out, 1000 );
646
647         p_out = p_dec->pf_aout_buffer_new( p_dec, i_frames );
648
649         if( p_out )
650         {
651             p_out->start_date = aout_DateGet( &p_sys->date );
652             p_out->end_date = aout_DateIncrement( &p_sys->date, i_frames );
653
654             memcpy( p_out->p_buffer,
655                     &p_sys->out_buffer[2 * p_sys->i_out * p_dec->fmt_out.audio.i_channels],
656                     p_out->i_nb_bytes );
657
658             p_sys->i_out += i_frames;
659         }
660         return p_out;
661     }
662
663     return NULL;
664 }
665
666 /*****************************************************************************
667  * OpenVideo:
668  *****************************************************************************/
669 static int OpenVideo( decoder_t *p_dec )
670 {
671     decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) );
672     if( !p_sys )
673         return VLC_ENOMEM;
674
675 #ifndef WIN32
676     long                                i_result;
677     ComponentDescription                desc;
678     Component                           prev;
679     ComponentResult                     cres;
680     ImageSubCodecDecompressCapabilities icap;   /* for ImageCodecInitialize() */
681     CodecInfo                           cinfo;  /* for ImageCodecGetCodecInfo() */
682     ImageDescription                    *id;
683
684     char                fcc[4];
685     int     i_vide = p_dec->fmt_in.i_extra;
686     uint8_t *p_vide = p_dec->fmt_in.p_extra;
687
688     p_dec->p_sys = p_sys;
689     p_dec->pf_decode_video = DecodeVideo;
690     p_sys->i_late = 0;
691
692     if( i_vide <= 0 )
693     {
694         msg_Err( p_dec, "missing extra info" );
695         free( p_sys );
696         return VLC_EGENERIC;
697     }
698
699     memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
700     msg_Dbg( p_dec, "quicktime_video %4.4s %dx%d",
701              fcc, p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
702
703     /* get lock, avoid segfault */
704     vlc_mutex_lock( &qt_mutex );
705
706 #ifdef __APPLE__
707     EnterMovies();
708 #endif
709
710     if( QTVideoInit( p_dec ) )
711     {
712         msg_Err( p_dec, "cannot initialize QT");
713         goto exit_error;
714     }
715
716 #ifndef __APPLE__
717     if( ( i_result = p_sys->InitializeQTML( 6 + 16 ) ) )
718     {
719         msg_Dbg( p_dec, "error on InitializeQTML = %d", (int)i_result );
720         goto exit_error;
721     }
722 #endif
723
724     /* init ComponentDescription */
725     memset( &desc, 0, sizeof( ComponentDescription ) );
726     desc.componentType      = FCC( 'i', 'm', 'd', 'c' );
727     desc.componentSubType   = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
728     desc.componentManufacturer = 0;
729     desc.componentFlags        = 0;
730     desc.componentFlagsMask    = 0;
731
732     if( !( prev = p_sys->FindNextComponent( NULL, &desc ) ) )
733     {
734         msg_Err( p_dec, "cannot find requested component" );
735         goto exit_error;
736     }
737     msg_Dbg( p_dec, "component id=0x%p", prev );
738
739     p_sys->ci =  p_sys->OpenComponent( prev );
740     msg_Dbg( p_dec, "component instance p=0x%p", p_sys->ci );
741
742     memset( &icap, 0, sizeof( ImageSubCodecDecompressCapabilities ) );
743     cres =  p_sys->ImageCodecInitialize( p_sys->ci, &icap );
744     msg_Dbg( p_dec, "ImageCodecInitialize->0x%X size=%d (%d)\n",
745              (int)cres, (int)icap.recordSize, (int)icap.decompressRecordSize);
746
747     memset( &cinfo, 0, sizeof( CodecInfo ) );
748     cres =  p_sys->ImageCodecGetCodecInfo( p_sys->ci, &cinfo );
749     msg_Dbg( p_dec,
750              "Flags: compr: 0x%x decomp: 0x%x format: 0x%x\n",
751              (unsigned int)cinfo.compressFlags,
752              (unsigned int)cinfo.decompressFlags,
753              (unsigned int)cinfo.formatFlags );
754     msg_Dbg( p_dec, "quicktime_video: Codec name: %.*s\n",
755              ((unsigned char*)&cinfo.typeName)[0],
756              ((unsigned char*)&cinfo.typeName)+1 );
757
758     /* make a yuy2 gworld */
759     p_sys->OutBufferRect.top    = 0;
760     p_sys->OutBufferRect.left   = 0;
761     p_sys->OutBufferRect.right  = p_dec->fmt_in.video.i_width;
762     p_sys->OutBufferRect.bottom = p_dec->fmt_in.video.i_height;
763
764
765     /* codec data FIXME use codec not SVQ3 */
766     msg_Dbg( p_dec, "vide = %d", i_vide  );
767     id = malloc( sizeof( ImageDescription ) + ( i_vide - 70 ) );
768     if( !id )
769         goto exit_error;
770     id->idSize          = sizeof( ImageDescription ) + ( i_vide - 70 );
771     id->cType           = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
772     id->version         = GetWBE ( p_vide +  0 );
773     id->revisionLevel   = GetWBE ( p_vide +  2 );
774     id->vendor          = GetDWBE( p_vide +  4 );
775     id->temporalQuality = GetDWBE( p_vide +  8 );
776     id->spatialQuality  = GetDWBE( p_vide + 12 );
777     id->width           = GetWBE ( p_vide + 16 );
778     id->height          = GetWBE ( p_vide + 18 );
779     id->hRes            = GetDWBE( p_vide + 20 );
780     id->vRes            = GetDWBE( p_vide + 24 );
781     id->dataSize        = GetDWBE( p_vide + 28 );
782     id->frameCount      = GetWBE ( p_vide + 32 );
783     memcpy( &id->name, p_vide + 34, 32 );
784     id->depth           = GetWBE ( p_vide + 66 );
785     id->clutID          = GetWBE ( p_vide + 68 );
786     if( i_vide > 70 )
787     {
788         memcpy( ((char*)&id->clutID) + 2, p_vide + 70, i_vide - 70 );
789     }
790
791     msg_Dbg( p_dec, "idSize=%d ver=%d rev=%d vendor=%d tempQ=%d "
792              "spaQ=%d w=%d h=%d dpi=%d%d dataSize=%d depth=%d frameCount=%d clutID=%d",
793              (int)id->idSize, id->version, id->revisionLevel, (int)id->vendor,
794              (int)id->temporalQuality, (int)id->spatialQuality,
795              (int)id->width, (int)id->height,
796              (int)id->hRes, (int)id->vRes,
797              (int)id->dataSize,
798              id->depth,
799              id->frameCount,
800              id->clutID );
801
802     p_sys->framedescHandle = (ImageDescriptionHandle) NewHandleClear( id->idSize );
803     memcpy( *p_sys->framedescHandle, id, id->idSize );
804
805     if( p_dec->fmt_in.video.i_width != 0 && p_dec->fmt_in.video.i_height != 0) 
806         p_sys->plane = malloc( p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height * 3 );
807     if( !p_sys->plane )
808         goto exit_error;
809
810     i_result = p_sys->QTNewGWorldFromPtr( &p_sys->OutBufferGWorld,
811                                           /*pixel format of new GWorld==YUY2 */
812                                           kYUVSPixelFormat,
813                                           /* we should benchmark if yvu9 is
814                                            * faster for svq3, too */
815                                           &p_sys->OutBufferRect,
816                                           0, 0, 0,
817                                           p_sys->plane,
818                                           p_dec->fmt_in.video.i_width * 2 );
819
820     msg_Dbg( p_dec, "NewGWorldFromPtr returned:%ld\n",
821              65536 - ( i_result&0xffff ) );
822
823     memset( &p_sys->decpar, 0, sizeof( CodecDecompressParams ) );
824     p_sys->decpar.imageDescription = p_sys->framedescHandle;
825     p_sys->decpar.startLine        = 0;
826     p_sys->decpar.stopLine         = ( **p_sys->framedescHandle ).height;
827     p_sys->decpar.frameNumber      = 1;
828     p_sys->decpar.matrixFlags      = 0;
829     p_sys->decpar.matrixType       = 0;
830     p_sys->decpar.matrix           = 0;
831     p_sys->decpar.capabilities     = &p_sys->codeccap;
832     p_sys->decpar.accuracy         = codecNormalQuality;
833     p_sys->decpar.srcRect          = p_sys->OutBufferRect;
834     p_sys->decpar.transferMode     = srcCopy;
835     p_sys->decpar.dstPixMap        = **p_sys->GetGWorldPixMap( p_sys->OutBufferGWorld );/*destPixmap;  */
836
837     cres =  p_sys->ImageCodecPreDecompress( p_sys->ci, &p_sys->decpar );
838     msg_Dbg( p_dec, "quicktime_video: ImageCodecPreDecompress cres=0x%X\n",
839              (int)cres );
840
841     es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC( 'Y', 'U', 'Y', '2' ));
842     p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
843     p_dec->fmt_out.video.i_height= p_dec->fmt_in.video.i_height;
844     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height;
845  
846     vlc_mutex_unlock( &qt_mutex );
847     return VLC_SUCCESS;
848
849 exit_error:
850 #ifdef LOADER
851     Restore_LDT_Keeper( p_sys->ldt_fs );
852 #endif
853     vlc_mutex_unlock( &qt_mutex );
854
855 #endif /* !WIN32 */
856
857     return VLC_EGENERIC;
858 }
859
860 #ifndef WIN32
861 /*****************************************************************************
862  * DecodeVideo:
863  *****************************************************************************/
864 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
865 {
866     decoder_sys_t *p_sys = p_dec->p_sys;
867     block_t       *p_block;
868     picture_t     *p_pic;
869     mtime_t       i_pts;
870
871     ComponentResult cres;
872
873 #ifdef LOADER
874     /* We must do open and close in the same thread (unless we do
875      * Setup_LDT_Keeper in the main thread before all others */
876     if( p_sys == NULL )
877     {
878         if( OpenVideo( p_dec ) )
879         {
880             /* Fatal */
881             p_dec->b_error = true;
882             return NULL;
883         }
884         p_sys = p_dec->p_sys;
885     }
886 #endif
887
888     if( pp_block == NULL || *pp_block == NULL )
889     {
890         return NULL;
891     }
892     p_block = *pp_block;
893     *pp_block = NULL;
894  
895     i_pts = p_block->i_pts ? p_block->i_pts : p_block->i_dts;
896
897     if( decoder_GetDisplayDate( p_dec, i_pts ) < mdate() )
898     {
899         p_sys->i_late++;
900     }
901     else
902     {
903         p_sys->i_late = 0;
904     }
905 #ifndef NDEBUG
906     msg_Dbg( p_dec, "bufsize: %d", (int)p_block->i_buffer);
907 #endif
908
909     if( p_sys->i_late > 10 )
910     {
911         msg_Dbg( p_dec, "late buffer dropped (%"PRId64")", i_pts );
912         block_Release( p_block );
913         return NULL;
914     }
915  
916     vlc_mutex_lock( &qt_mutex );
917
918     if( ( p_pic = p_dec->pf_vout_buffer_new( p_dec ) ) )
919     {
920         p_sys->decpar.data                  = (Ptr)p_block->p_buffer;
921         p_sys->decpar.bufferSize            = p_block->i_buffer;
922         (**p_sys->framedescHandle).dataSize = p_block->i_buffer;
923
924         cres = p_sys->ImageCodecBandDecompress( p_sys->ci, &p_sys->decpar );
925
926         ++p_sys->decpar.frameNumber;
927
928         if( cres &0xFFFF )
929         {
930             msg_Dbg( p_dec, "quicktime_video: ImageCodecBandDecompress"
931                      " cres=0x%X (-0x%X) %d :(\n",
932                      (int)cres,(int)-cres, (int)cres );
933         }
934
935         memcpy( p_pic->p[0].p_pixels, p_sys->plane,
936                 p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height * 2 );
937         p_pic->date = i_pts;
938     }
939  
940     vlc_mutex_unlock( &qt_mutex );
941
942     block_Release( p_block );
943     return p_pic;
944 }
945 #endif /* !WIN32 */
946
947 /*****************************************************************************
948  * QTAudioInit:
949  *****************************************************************************/
950 static int QTAudioInit( decoder_t *p_dec )
951 {
952     decoder_sys_t *p_sys = p_dec->p_sys;
953
954 #ifdef __APPLE__
955     p_sys->SoundConverterOpen       = (void*)SoundConverterOpen;
956     p_sys->SoundConverterClose      = (void*)SoundConverterClose;
957     p_sys->SoundConverterSetInfo    = (void*)SoundConverterSetInfo;
958     p_sys->SoundConverterGetBufferSizes = (void*)SoundConverterGetBufferSizes;
959     p_sys->SoundConverterConvertBuffer  = (void*)SoundConverterConvertBuffer;
960     p_sys->SoundConverterBeginConversion= (void*)SoundConverterBeginConversion;
961     p_sys->SoundConverterEndConversion  = (void*)SoundConverterEndConversion;
962 #else
963
964 #ifdef LOADER
965     p_sys->ldt_fs = Setup_LDT_Keeper();
966 #endif /* LOADER */
967
968     p_sys->qts = LoadLibraryA( "QuickTime.qts" );
969     if( p_sys->qts == NULL )
970     {
971         msg_Dbg( p_dec, "failed loading QuickTime.qts" );
972         return VLC_EGENERIC;
973     }
974     p_sys->qtml = LoadLibraryA( "qtmlClient.dll" );
975     if( p_sys->qtml == NULL )
976     {
977         msg_Dbg( p_dec, "failed loading qtmlClient.dll" );
978         return VLC_EGENERIC;
979     }
980
981     p_sys->InitializeQTML               = (void *)GetProcAddress( p_sys->qtml, "InitializeQTML" );
982     p_sys->TerminateQTML                = (void *)GetProcAddress( p_sys->qtml, "TerminateQTML" );
983     p_sys->SoundConverterOpen           = (void *)GetProcAddress( p_sys->qtml, "SoundConverterOpen" );
984     p_sys->SoundConverterClose          = (void *)GetProcAddress( p_sys->qtml, "SoundConverterClose" );
985     p_sys->SoundConverterSetInfo        = (void *)GetProcAddress( p_sys->qtml, "SoundConverterSetInfo" );
986     p_sys->SoundConverterGetBufferSizes = (void *)GetProcAddress( p_sys->qtml, "SoundConverterGetBufferSizes" );
987     p_sys->SoundConverterConvertBuffer  = (void *)GetProcAddress( p_sys->qtml, "SoundConverterConvertBuffer" );
988     p_sys->SoundConverterEndConversion  = (void *)GetProcAddress( p_sys->qtml, "SoundConverterEndConversion" );
989     p_sys->SoundConverterBeginConversion= (void *)GetProcAddress( p_sys->qtml, "SoundConverterBeginConversion");
990
991     if( p_sys->InitializeQTML == NULL )
992     {
993         msg_Err( p_dec, "failed getting proc address InitializeQTML" );
994         return VLC_EGENERIC;
995     }
996     if( p_sys->SoundConverterOpen == NULL ||
997         p_sys->SoundConverterClose == NULL ||
998         p_sys->SoundConverterSetInfo == NULL ||
999         p_sys->SoundConverterGetBufferSizes == NULL ||
1000         p_sys->SoundConverterConvertBuffer == NULL ||
1001         p_sys->SoundConverterEndConversion == NULL ||
1002         p_sys->SoundConverterBeginConversion == NULL )
1003     {
1004         msg_Err( p_dec, "failed getting proc address" );
1005         return VLC_EGENERIC;
1006     }
1007
1008     msg_Dbg( p_dec, "standard init done" );
1009 #endif /* else __APPLE__ */
1010
1011     return VLC_SUCCESS;
1012 }
1013
1014 #ifndef WIN32
1015 /*****************************************************************************
1016  * QTVideoInit:
1017  *****************************************************************************/
1018 static int QTVideoInit( decoder_t *p_dec )
1019 {
1020     decoder_sys_t *p_sys = p_dec->p_sys;
1021
1022 #ifdef __APPLE__
1023     p_sys->FindNextComponent        = (void*)FindNextComponent;
1024     p_sys->OpenComponent            = (void*)OpenComponent;
1025     p_sys->ImageCodecInitialize     = (void*)ImageCodecInitialize;
1026     p_sys->ImageCodecGetCodecInfo   = (void*)ImageCodecGetCodecInfo;
1027     p_sys->ImageCodecPreDecompress  = (void*)ImageCodecPreDecompress;
1028     p_sys->ImageCodecBandDecompress = (void*)ImageCodecBandDecompress;
1029     p_sys->GetGWorldPixMap          = (void*)GetGWorldPixMap;
1030     p_sys->QTNewGWorldFromPtr       = (void*)QTNewGWorldFromPtr;
1031     p_sys->NewHandleClear           = (void*)NewHandleClear;
1032 #else
1033
1034 #ifdef LOADER
1035     p_sys->ldt_fs = Setup_LDT_Keeper();
1036 #endif /* LOADER */
1037     p_sys->qts = LoadLibraryA( "QuickTime.qts" );
1038     if( p_sys->qts == NULL )
1039     {
1040         msg_Dbg( p_dec, "failed loading QuickTime.qts" );
1041         return VLC_EGENERIC;
1042     }
1043     msg_Dbg( p_dec, "QuickTime.qts loaded" );
1044     p_sys->qtml = LoadLibraryA( "qtmlClient.dll" );
1045     if( p_sys->qtml == NULL )
1046     {
1047         msg_Dbg( p_dec, "failed loading qtmlClient.dll" );
1048         return VLC_EGENERIC;
1049     }
1050     msg_Dbg( p_dec, "qtmlClient.dll loaded" );
1051
1052     /* (void*) to shut up gcc */
1053     p_sys->InitializeQTML           = (void*)GetProcAddress( p_sys->qtml, "InitializeQTML" );
1054     p_sys->FindNextComponent        = (void*)GetProcAddress( p_sys->qtml, "FindNextComponent" );
1055     p_sys->OpenComponent            = (void*)GetProcAddress( p_sys->qtml, "OpenComponent" );
1056     p_sys->ImageCodecInitialize     = (void*)GetProcAddress( p_sys->qtml, "ImageCodecInitialize" );
1057     p_sys->ImageCodecGetCodecInfo   = (void*)GetProcAddress( p_sys->qtml, "ImageCodecGetCodecInfo" );
1058     p_sys->ImageCodecPreDecompress  = (void*)GetProcAddress( p_sys->qtml, "ImageCodecPreDecompress" );
1059     p_sys->ImageCodecBandDecompress = (void*)GetProcAddress( p_sys->qtml, "ImageCodecBandDecompress" );
1060     p_sys->GetGWorldPixMap          = (void*)GetProcAddress( p_sys->qtml, "GetGWorldPixMap" );
1061     p_sys->QTNewGWorldFromPtr       = (void*)GetProcAddress( p_sys->qtml, "QTNewGWorldFromPtr" );
1062     p_sys->NewHandleClear           = (void*)GetProcAddress( p_sys->qtml, "NewHandleClear" );
1063
1064     if( p_sys->InitializeQTML == NULL )
1065     {
1066         msg_Dbg( p_dec, "failed getting proc address InitializeQTML" );
1067         return VLC_EGENERIC;
1068     }
1069     if( p_sys->FindNextComponent == NULL ||
1070         p_sys->OpenComponent == NULL ||
1071         p_sys->ImageCodecInitialize == NULL ||
1072         p_sys->ImageCodecGetCodecInfo == NULL ||
1073         p_sys->ImageCodecPreDecompress == NULL ||
1074         p_sys->ImageCodecBandDecompress == NULL ||
1075         p_sys->GetGWorldPixMap == NULL ||
1076         p_sys->QTNewGWorldFromPtr == NULL ||
1077         p_sys->NewHandleClear == NULL )
1078     {
1079         msg_Err( p_dec, "failed getting proc address" );
1080         return VLC_EGENERIC;
1081     }
1082 #endif /* __APPLE__ */
1083
1084     return VLC_SUCCESS;
1085 }
1086 #endif /* !WIN32 */