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