]> git.sesse.net Git - vlc/blob - modules/codec/quicktime.c
fd75b64ed25ab76b3427a271f2dc6734d39a1a5a
[vlc] / modules / codec / quicktime.c
1 /*****************************************************************************
2  * quicktime.c: a quicktime decoder that uses the QT library/dll
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir at via.ecp.fr>
8  *          Derk-Jan Hartman <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         p_dec->fmt_out.i_cat = VIDEO_ES;
269         return VLC_SUCCESS;
270 #else
271         return OpenVideo( p_dec );
272 #endif
273
274 #ifdef __APPLE__
275         case VLC_FOURCC('I','L','B','C'): /* iLBC */
276             if ((err != noErr) || (qtVersion < 0x07500000)) 
277                 return VLC_EGENERIC;
278         case VLC_FOURCC('i','l','b','c'): /* iLBC */
279             if ((err != noErr) || (qtVersion < 0x07500000)) 
280                 return VLC_EGENERIC;
281 #endif
282         case VLC_FOURCC('s','a','m','r'): /* 3GPP AMR audio */
283         case VLC_FOURCC('s','a','m','b'): /* 3GPP AMR-WB audio */
284         case VLC_FOURCC('m','p','4','a'): /* MPEG-4 audio */
285         case VLC_FOURCC('Q','D','M','C'): /* QDesign */
286         case VLC_FOURCC('Q','D','M','2'): /* QDesign* 2 */
287         case VLC_FOURCC('Q','c','l','p'): /* Qualcomm Purevoice Codec */
288         case VLC_FOURCC('Q','C','L','P'): /* Qualcomm Purevoice Codec */
289         case VLC_FOURCC('M','A','C','3'): /* MACE3 audio decoder */
290         case VLC_FOURCC('M','A','C','6'): /* MACE6 audio decoder */
291         case VLC_FOURCC('d','v','c','a'): /* DV Audio */
292         case VLC_FOURCC('s','o','w','t'): /* 16-bit Little Endian */
293         case VLC_FOURCC('t','w','o','s'): /* 16-bit Big Endian */
294         case VLC_FOURCC('a','l','a','w'): /* ALaw 2:1 */
295         case VLC_FOURCC('u','l','a','w'): /* mu-Law 2:1 */
296         case VLC_FOURCC('r','a','w',' '): /* 8-bit offset binaries */
297         case VLC_FOURCC('f','l','3','2'): /* 32-bit Floating Point */
298         case VLC_FOURCC('f','l','6','4'): /* 64-bit Floating Point */
299         case VLC_FOURCC('i','n','2','4'): /* 24-bit Interger */
300         case VLC_FOURCC('i','n','3','2'): /* 32-bit Integer */
301         case 0x0011:                            /* DVI IMA */
302         case 0x6D730002:                        /* Microsoft ADPCM-ACM */
303         case 0x6D730011:                        /* DVI Intel IMAADPCM-ACM */
304 #ifdef LOADER
305         p_dec->p_sys = NULL;
306         p_dec->pf_decode_audio = DecodeAudio;
307         p_dec->fmt_out.i_cat = AUDIO_ES;
308         return VLC_SUCCESS;
309 #else
310
311 #ifdef __APPLE__
312         /* FIXME: right now, we don't support audio decoding on 10.5 and later
313          because we are still using the hardcore-outdated SoundManager API,
314          which was removed after 10.4 */
315
316         if( macosversion >= 0x1050 || err != noErr )
317         {
318             msg_Warn( p_dec, "Your Mac OS version doesn't have SoundManager anymore. "
319                      "You can't use this plugin for audio." );
320             return VLC_EGENERIC;
321         }
322 #endif
323         return OpenAudio( p_dec );
324 #endif
325
326         default:
327             return VLC_EGENERIC;
328     }
329 }
330
331 static vlc_mutex_t qt_mutex = VLC_STATIC_MUTEX;
332
333 /*****************************************************************************
334  * Close:
335  *****************************************************************************/
336 static void Close( vlc_object_t *p_this )
337 {
338     decoder_t     *p_dec = (decoder_t*)p_this;
339     decoder_sys_t *p_sys = p_dec->p_sys;
340
341     /* get lock, avoid segfault */
342     vlc_mutex_lock( &qt_mutex );
343
344     if( p_dec->fmt_out.i_cat == AUDIO_ES )
345     {
346         int i_error;
347         unsigned long ConvertedFrames=0;
348         unsigned long ConvertedBytes=0;
349
350         i_error = p_sys->SoundConverterEndConversion( p_sys->myConverter, NULL,
351                                                       &ConvertedFrames,
352                                                       &ConvertedBytes );
353         msg_Dbg( p_dec, "SoundConverterEndConversion => %d", i_error );
354
355         i_error = p_sys->SoundConverterClose( p_sys->myConverter );
356         msg_Dbg( p_dec, "SoundConverterClose => %d", i_error );
357
358         free( p_sys->p_buffer );
359     }
360     else if( p_dec->fmt_out.i_cat == VIDEO_ES )
361     {
362         free( p_sys->plane );
363     }
364
365 #ifndef __APPLE__
366     FreeLibrary( p_sys->qtml );
367     FreeLibrary( p_sys->qts );
368     msg_Dbg( p_dec, "FreeLibrary ok." );
369 #else
370     ExitMovies();
371 #endif
372
373 #if 0
374     /* Segfault */
375 #ifdef LOADER
376     Restore_LDT_Keeper( p_sys->ldt_fs );
377     msg_Dbg( p_dec, "Restore_LDT_Keeper" );
378 #endif
379 #endif
380
381     vlc_mutex_unlock( &qt_mutex );
382
383     free( p_sys );
384 }
385
386 /*****************************************************************************
387  * OpenAudio:
388  *****************************************************************************/
389 static int OpenAudio( decoder_t *p_dec )
390 {
391     decoder_sys_t *p_sys;
392
393     int             i_error;
394     char            fcc[4];
395     unsigned long   WantedBufferSize;
396     unsigned long   InputBufferSize = 0;
397     unsigned long   OutputBufferSize = 0;
398
399     /* get lock, avoid segfault */
400     vlc_mutex_lock( &qt_mutex );
401
402     p_sys = calloc( 1, sizeof( decoder_sys_t ) );
403     p_dec->p_sys = p_sys;
404     p_dec->pf_decode_audio = DecodeAudio;
405
406     memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
407
408 #ifdef __APPLE__
409     EnterMovies();
410 #endif
411
412     if( QTAudioInit( p_dec ) )
413     {
414         msg_Err( p_dec, "cannot initialize QT");
415         goto exit_error;
416     }
417
418 #ifndef __APPLE__
419     if( ( i_error = p_sys->InitializeQTML( 6 + 16 ) ) )
420     {
421         msg_Dbg( p_dec, "error on InitializeQTML = %d", i_error );
422         goto exit_error;
423     }
424 #endif
425
426     /* input format settings */
427     p_sys->InputFormatInfo.flags       = 0;
428     p_sys->InputFormatInfo.sampleCount = 0;
429     p_sys->InputFormatInfo.buffer      = NULL;
430     p_sys->InputFormatInfo.reserved    = 0;
431     p_sys->InputFormatInfo.numChannels = p_dec->fmt_in.audio.i_channels;
432     p_sys->InputFormatInfo.sampleSize  = p_dec->fmt_in.audio.i_bitspersample;
433     p_sys->InputFormatInfo.sampleRate  = p_dec->fmt_in.audio.i_rate;
434     p_sys->InputFormatInfo.format      = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
435
436     /* output format settings */
437     p_sys->OutputFormatInfo.flags       = 0;
438     p_sys->OutputFormatInfo.sampleCount = 0;
439     p_sys->OutputFormatInfo.buffer      = NULL;
440     p_sys->OutputFormatInfo.reserved    = 0;
441     p_sys->OutputFormatInfo.numChannels = p_dec->fmt_in.audio.i_channels;
442     p_sys->OutputFormatInfo.sampleSize  = 16;
443     p_sys->OutputFormatInfo.sampleRate  = p_dec->fmt_in.audio.i_rate;
444     p_sys->OutputFormatInfo.format      = FCC( 'N', 'O', 'N', 'E' );
445
446
447     i_error = p_sys->SoundConverterOpen( &p_sys->InputFormatInfo,
448                                          &p_sys->OutputFormatInfo,
449                                          &p_sys->myConverter );
450     if( i_error )
451     {
452         msg_Err( p_dec, "error on SoundConverterOpen = %d", i_error );
453         goto exit_error;
454     }
455
456 #if 0
457     /* tell the sound converter we accept VBR formats */
458     i_error = SoundConverterSetInfo( p_dec->myConverter, siClientAcceptsVBR,
459                                      (void *)true );
460 #endif
461
462     if( p_dec->fmt_in.i_extra > 36 + 8 )
463     {
464         i_error = p_sys->SoundConverterSetInfo( p_sys->myConverter,
465                                                 FCC( 'w', 'a', 'v', 'e' ),
466                                                 ((uint8_t*)p_dec->fmt_in.p_extra) + 36 + 8 );
467
468         msg_Dbg( p_dec, "error on SoundConverterSetInfo = %d", i_error );
469     }
470
471     WantedBufferSize = p_sys->OutputFormatInfo.numChannels *
472                        p_sys->OutputFormatInfo.sampleRate * 2;
473     p_sys->FramesToGet = 0;
474
475     i_error = p_sys->SoundConverterGetBufferSizes( p_sys->myConverter,
476                                                    WantedBufferSize,
477                                                    &p_sys->FramesToGet,
478                                                    &InputBufferSize,
479                                                    &OutputBufferSize );
480
481     msg_Dbg( p_dec, "WantedBufferSize=%li InputBufferSize=%li "
482              "OutputBufferSize=%li FramesToGet=%li",
483              WantedBufferSize, InputBufferSize, OutputBufferSize,
484              p_sys->FramesToGet );
485
486     p_sys->InFrameSize  = (InputBufferSize + p_sys->FramesToGet - 1 ) /
487                             p_sys->FramesToGet;
488     p_sys->OutFrameSize = OutputBufferSize / p_sys->FramesToGet;
489
490     msg_Dbg( p_dec, "frame size %d -> %d",
491              p_sys->InFrameSize, p_sys->OutFrameSize );
492
493     if( (i_error = p_sys->SoundConverterBeginConversion(p_sys->myConverter)) )
494     {
495         msg_Err( p_dec,
496                  "error on SoundConverterBeginConversion = %d", i_error );
497         goto exit_error;
498     }
499
500
501     es_format_Init( &p_dec->fmt_out, AUDIO_ES, AOUT_FMT_S16_NE );
502     p_dec->fmt_out.audio.i_rate = p_sys->OutputFormatInfo.sampleRate;
503     p_dec->fmt_out.audio.i_channels = p_sys->OutputFormatInfo.numChannels;
504     p_dec->fmt_out.audio.i_physical_channels =
505     p_dec->fmt_out.audio.i_original_channels =
506         pi_channels_maps[p_sys->OutputFormatInfo.numChannels];
507
508     aout_DateInit( &p_sys->date, p_dec->fmt_out.audio.i_rate );
509
510     p_sys->i_buffer      = 0;
511     p_sys->i_buffer_size = 100*1000;
512     p_sys->p_buffer      = malloc( p_sys->i_buffer_size );
513     if( !p_sys->p_buffer )
514         goto exit_error;
515
516     p_sys->i_out = 0;
517     p_sys->i_out_frames = 0;
518
519     vlc_mutex_unlock( &qt_mutex );
520     return VLC_SUCCESS;
521
522 exit_error:
523
524 #ifdef LOADER
525     Restore_LDT_Keeper( p_sys->ldt_fs );
526 #endif
527     vlc_mutex_unlock( &qt_mutex );
528
529     free( p_sys );
530     return VLC_EGENERIC;
531 }
532
533 /*****************************************************************************
534  * DecodeAudio:
535  *****************************************************************************/
536 static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
537 {
538     decoder_sys_t *p_sys = p_dec->p_sys;
539
540     block_t     *p_block;
541     int         i_error;
542
543 #ifdef LOADER
544     /* We must do open and close in the same thread (unless we do
545      * Setup_LDT_Keeper in the main thread before all others */
546     if( p_sys == NULL )
547     {
548         if( OpenAudio( p_dec ) )
549         {
550             /* Fatal */
551             p_dec->b_error = true;
552             return NULL;
553         }
554
555         p_sys = p_dec->p_sys;
556     }
557 #endif
558
559     if( pp_block == NULL || *pp_block == NULL )
560     {
561         return NULL;
562     }
563     p_block = *pp_block;
564
565     if( p_sys->i_out_frames > 0 && p_sys->i_out >= p_sys->i_out_frames )
566     {
567         /* Ask new data */
568         p_sys->i_out = 0;
569         p_sys->i_out_frames = 0;
570
571         *pp_block = NULL;
572         return NULL;
573     }
574
575     if( p_sys->i_out_frames <= 0 )
576     {
577         p_sys->pts = p_block->i_pts;
578
579         mtime_t i_display_date = 0;
580         if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
581             i_display_date = decoder_GetDisplayDate( p_dec, p_block->i_pts );
582
583         if( i_display_date > 0 && i_display_date < mdate() )
584         {
585             block_Release( p_block );
586             *pp_block = NULL;
587             return NULL;
588         }
589
590         /* Append data */
591         if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
592         {
593             p_sys->i_buffer_size = p_sys->i_buffer + p_block->i_buffer + 1024;
594             p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
595         }
596         memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer,
597                 p_block->i_buffer );
598         p_sys->i_buffer += p_block->i_buffer;
599
600         if( p_sys->i_buffer > p_sys->InFrameSize )
601         {
602             int i_frames = p_sys->i_buffer / p_sys->InFrameSize;
603             unsigned long i_out_frames, i_out_bytes;
604             vlc_mutex_lock( &qt_mutex );
605
606             i_error = p_sys->SoundConverterConvertBuffer( p_sys->myConverter,
607                                                           p_sys->p_buffer,
608                                                           i_frames,
609                                                           p_sys->out_buffer,
610                                                           &i_out_frames,
611                                                           &i_out_bytes );
612             vlc_mutex_unlock( &qt_mutex );
613
614             /*
615             msg_Dbg( p_dec, "decoded %d frames -> %ld frames (error=%d)",
616                      i_frames, i_out_frames, i_error );
617
618             msg_Dbg( p_dec, "decoded %ld frames = %ld bytes",
619                      i_out_frames, i_out_bytes );
620             */
621
622             p_sys->i_buffer -= i_frames * p_sys->InFrameSize;
623             if( p_sys->i_buffer > 0 )
624             {
625                 memmove( &p_sys->p_buffer[0],
626                          &p_sys->p_buffer[i_frames * p_sys->InFrameSize],
627                          p_sys->i_buffer );
628             }
629
630             if( p_sys->pts != 0 &&
631                 p_sys->pts != aout_DateGet( &p_sys->date ) )
632             {
633                 aout_DateSet( &p_sys->date, p_sys->pts );
634             }
635             else if( !aout_DateGet( &p_sys->date ) )
636             {
637                 return NULL;
638             }
639
640             if( !i_error && i_out_frames > 0 )
641             {
642                 /* we have others samples */
643                 p_sys->i_out_frames = i_out_frames;
644                 p_sys->i_out = 0;
645             }
646         }
647     }
648
649     if( p_sys->i_out < p_sys->i_out_frames )
650     {
651         aout_buffer_t *p_out;
652         int  i_frames = __MIN( p_sys->i_out_frames - p_sys->i_out, 1000 );
653
654         p_out = decoder_NewAudioBuffer( p_dec, i_frames );
655
656         if( p_out )
657         {
658             p_out->start_date = aout_DateGet( &p_sys->date );
659             p_out->end_date = aout_DateIncrement( &p_sys->date, i_frames );
660
661             memcpy( p_out->p_buffer,
662                     &p_sys->out_buffer[2 * p_sys->i_out * p_dec->fmt_out.audio.i_channels],
663                     p_out->i_nb_bytes );
664
665             p_sys->i_out += i_frames;
666         }
667         return p_out;
668     }
669
670     return NULL;
671 }
672
673 /*****************************************************************************
674  * OpenVideo:
675  *****************************************************************************/
676 static int OpenVideo( decoder_t *p_dec )
677 {
678 #ifndef WIN32
679     decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) );
680     if( !p_sys )
681         return VLC_ENOMEM;
682
683     long                                i_result;
684     ComponentDescription                desc;
685     Component                           prev;
686     ComponentResult                     cres;
687     ImageSubCodecDecompressCapabilities icap;   /* for ImageCodecInitialize() */
688     CodecInfo                           cinfo;  /* for ImageCodecGetCodecInfo() */
689     ImageDescription                    *id;
690
691     char                fcc[4];
692     int     i_vide = p_dec->fmt_in.i_extra;
693     uint8_t *p_vide = p_dec->fmt_in.p_extra;
694
695     p_dec->p_sys = p_sys;
696     p_dec->pf_decode_video = DecodeVideo;
697     p_sys->i_late = 0;
698
699     if( i_vide <= 0 )
700     {
701         msg_Err( p_dec, "missing extra info" );
702         free( p_sys );
703         return VLC_EGENERIC;
704     }
705
706     memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
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_FOURCC( 'Y', 'U', 'Y', '2' ));
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 #endif /* !WIN32 */
863
864     return VLC_EGENERIC;
865 }
866
867 #ifndef WIN32
868 /*****************************************************************************
869  * DecodeVideo:
870  *****************************************************************************/
871 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
872 {
873     decoder_sys_t *p_sys = p_dec->p_sys;
874     block_t       *p_block;
875     picture_t     *p_pic;
876     mtime_t       i_pts;
877
878     ComponentResult cres;
879
880 #ifdef LOADER
881     /* We must do open and close in the same thread (unless we do
882      * Setup_LDT_Keeper in the main thread before all others */
883     if( p_sys == NULL )
884     {
885         if( OpenVideo( p_dec ) )
886         {
887             /* Fatal */
888             p_dec->b_error = true;
889             return NULL;
890         }
891         p_sys = p_dec->p_sys;
892     }
893 #endif
894
895     if( pp_block == NULL || *pp_block == NULL )
896     {
897         return NULL;
898     }
899     p_block = *pp_block;
900     *pp_block = NULL;
901  
902     i_pts = p_block->i_pts ? p_block->i_pts : p_block->i_dts;
903
904     mtime_t i_display_date = 0;
905     if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
906         i_display_date = decoder_GetDisplayDate( p_dec, i_pts );
907
908     if( i_display_date > 0 && i_display_date < mdate() )
909     {
910         p_sys->i_late++;
911     }
912     else
913     {
914         p_sys->i_late = 0;
915     }
916 #ifndef NDEBUG
917     msg_Dbg( p_dec, "bufsize: %d", (int)p_block->i_buffer);
918 #endif
919
920     if( p_sys->i_late > 10 )
921     {
922         msg_Dbg( p_dec, "late buffer dropped (%"PRId64")", i_pts );
923         block_Release( p_block );
924         return NULL;
925     }
926  
927     vlc_mutex_lock( &qt_mutex );
928
929     if( ( p_pic = decoder_NewPicture( p_dec ) ) )
930     {
931         p_sys->decpar.data                  = (Ptr)p_block->p_buffer;
932         p_sys->decpar.bufferSize            = p_block->i_buffer;
933         (**p_sys->framedescHandle).dataSize = p_block->i_buffer;
934
935         cres = p_sys->ImageCodecBandDecompress( p_sys->ci, &p_sys->decpar );
936
937         ++p_sys->decpar.frameNumber;
938
939         if( cres &0xFFFF )
940         {
941             msg_Dbg( p_dec, "quicktime_video: ImageCodecBandDecompress"
942                      " cres=0x%X (-0x%X) %d :(",
943                      (int)cres,(int)-cres, (int)cres );
944         }
945
946         memcpy( p_pic->p[0].p_pixels, p_sys->plane,
947                 p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height * 2 );
948         p_pic->date = i_pts;
949     }
950  
951     vlc_mutex_unlock( &qt_mutex );
952
953     block_Release( p_block );
954     return p_pic;
955 }
956 #endif /* !WIN32 */
957
958 /*****************************************************************************
959  * QTAudioInit:
960  *****************************************************************************/
961 static int QTAudioInit( decoder_t *p_dec )
962 {
963     decoder_sys_t *p_sys = p_dec->p_sys;
964
965 #ifdef __APPLE__
966     p_sys->SoundConverterOpen       = (void*)SoundConverterOpen;
967     p_sys->SoundConverterClose      = (void*)SoundConverterClose;
968     p_sys->SoundConverterSetInfo    = (void*)SoundConverterSetInfo;
969     p_sys->SoundConverterGetBufferSizes = (void*)SoundConverterGetBufferSizes;
970     p_sys->SoundConverterConvertBuffer  = (void*)SoundConverterConvertBuffer;
971     p_sys->SoundConverterBeginConversion= (void*)SoundConverterBeginConversion;
972     p_sys->SoundConverterEndConversion  = (void*)SoundConverterEndConversion;
973 #else
974
975 #ifdef LOADER
976     p_sys->ldt_fs = Setup_LDT_Keeper();
977 #endif /* LOADER */
978
979     p_sys->qts = LoadLibraryA( "QuickTime.qts" );
980     if( p_sys->qts == NULL )
981     {
982         msg_Dbg( p_dec, "failed loading QuickTime.qts" );
983         return VLC_EGENERIC;
984     }
985     p_sys->qtml = LoadLibraryA( "qtmlClient.dll" );
986     if( p_sys->qtml == NULL )
987     {
988         msg_Dbg( p_dec, "failed loading qtmlClient.dll" );
989         return VLC_EGENERIC;
990     }
991
992     p_sys->InitializeQTML               = (void *)GetProcAddress( p_sys->qtml, "InitializeQTML" );
993     p_sys->TerminateQTML                = (void *)GetProcAddress( p_sys->qtml, "TerminateQTML" );
994     p_sys->SoundConverterOpen           = (void *)GetProcAddress( p_sys->qtml, "SoundConverterOpen" );
995     p_sys->SoundConverterClose          = (void *)GetProcAddress( p_sys->qtml, "SoundConverterClose" );
996     p_sys->SoundConverterSetInfo        = (void *)GetProcAddress( p_sys->qtml, "SoundConverterSetInfo" );
997     p_sys->SoundConverterGetBufferSizes = (void *)GetProcAddress( p_sys->qtml, "SoundConverterGetBufferSizes" );
998     p_sys->SoundConverterConvertBuffer  = (void *)GetProcAddress( p_sys->qtml, "SoundConverterConvertBuffer" );
999     p_sys->SoundConverterEndConversion  = (void *)GetProcAddress( p_sys->qtml, "SoundConverterEndConversion" );
1000     p_sys->SoundConverterBeginConversion= (void *)GetProcAddress( p_sys->qtml, "SoundConverterBeginConversion");
1001
1002     if( p_sys->InitializeQTML == NULL )
1003     {
1004         msg_Err( p_dec, "failed getting proc address InitializeQTML" );
1005         return VLC_EGENERIC;
1006     }
1007     if( p_sys->SoundConverterOpen == NULL ||
1008         p_sys->SoundConverterClose == NULL ||
1009         p_sys->SoundConverterSetInfo == NULL ||
1010         p_sys->SoundConverterGetBufferSizes == NULL ||
1011         p_sys->SoundConverterConvertBuffer == NULL ||
1012         p_sys->SoundConverterEndConversion == NULL ||
1013         p_sys->SoundConverterBeginConversion == NULL )
1014     {
1015         msg_Err( p_dec, "failed getting proc address" );
1016         return VLC_EGENERIC;
1017     }
1018
1019     msg_Dbg( p_dec, "standard init done" );
1020 #endif /* else __APPLE__ */
1021
1022     return VLC_SUCCESS;
1023 }
1024
1025 #ifndef WIN32
1026 /*****************************************************************************
1027  * QTVideoInit:
1028  *****************************************************************************/
1029 static int QTVideoInit( decoder_t *p_dec )
1030 {
1031     decoder_sys_t *p_sys = p_dec->p_sys;
1032
1033 #ifdef __APPLE__
1034     p_sys->FindNextComponent        = (void*)FindNextComponent;
1035     p_sys->OpenComponent            = (void*)OpenComponent;
1036     p_sys->ImageCodecInitialize     = (void*)ImageCodecInitialize;
1037     p_sys->ImageCodecGetCodecInfo   = (void*)ImageCodecGetCodecInfo;
1038     p_sys->ImageCodecPreDecompress  = (void*)ImageCodecPreDecompress;
1039     p_sys->ImageCodecBandDecompress = (void*)ImageCodecBandDecompress;
1040     p_sys->GetGWorldPixMap          = (void*)GetGWorldPixMap;
1041     p_sys->QTNewGWorldFromPtr       = (void*)QTNewGWorldFromPtr;
1042     p_sys->NewHandleClear           = (void*)NewHandleClear;
1043 #else
1044
1045 #ifdef LOADER
1046     p_sys->ldt_fs = Setup_LDT_Keeper();
1047 #endif /* LOADER */
1048     p_sys->qts = LoadLibraryA( "QuickTime.qts" );
1049     if( p_sys->qts == NULL )
1050     {
1051         msg_Dbg( p_dec, "failed loading QuickTime.qts" );
1052         return VLC_EGENERIC;
1053     }
1054     msg_Dbg( p_dec, "QuickTime.qts loaded" );
1055     p_sys->qtml = LoadLibraryA( "qtmlClient.dll" );
1056     if( p_sys->qtml == NULL )
1057     {
1058         msg_Dbg( p_dec, "failed loading qtmlClient.dll" );
1059         return VLC_EGENERIC;
1060     }
1061     msg_Dbg( p_dec, "qtmlClient.dll loaded" );
1062
1063     /* (void*) to shut up gcc */
1064     p_sys->InitializeQTML           = (void*)GetProcAddress( p_sys->qtml, "InitializeQTML" );
1065     p_sys->FindNextComponent        = (void*)GetProcAddress( p_sys->qtml, "FindNextComponent" );
1066     p_sys->OpenComponent            = (void*)GetProcAddress( p_sys->qtml, "OpenComponent" );
1067     p_sys->ImageCodecInitialize     = (void*)GetProcAddress( p_sys->qtml, "ImageCodecInitialize" );
1068     p_sys->ImageCodecGetCodecInfo   = (void*)GetProcAddress( p_sys->qtml, "ImageCodecGetCodecInfo" );
1069     p_sys->ImageCodecPreDecompress  = (void*)GetProcAddress( p_sys->qtml, "ImageCodecPreDecompress" );
1070     p_sys->ImageCodecBandDecompress = (void*)GetProcAddress( p_sys->qtml, "ImageCodecBandDecompress" );
1071     p_sys->GetGWorldPixMap          = (void*)GetProcAddress( p_sys->qtml, "GetGWorldPixMap" );
1072     p_sys->QTNewGWorldFromPtr       = (void*)GetProcAddress( p_sys->qtml, "QTNewGWorldFromPtr" );
1073     p_sys->NewHandleClear           = (void*)GetProcAddress( p_sys->qtml, "NewHandleClear" );
1074
1075     if( p_sys->InitializeQTML == NULL )
1076     {
1077         msg_Dbg( p_dec, "failed getting proc address InitializeQTML" );
1078         return VLC_EGENERIC;
1079     }
1080     if( p_sys->FindNextComponent == NULL ||
1081         p_sys->OpenComponent == NULL ||
1082         p_sys->ImageCodecInitialize == NULL ||
1083         p_sys->ImageCodecGetCodecInfo == NULL ||
1084         p_sys->ImageCodecPreDecompress == NULL ||
1085         p_sys->ImageCodecBandDecompress == NULL ||
1086         p_sys->GetGWorldPixMap == NULL ||
1087         p_sys->QTNewGWorldFromPtr == NULL ||
1088         p_sys->NewHandleClear == NULL )
1089     {
1090         msg_Err( p_dec, "failed getting proc address" );
1091         return VLC_EGENERIC;
1092     }
1093 #endif /* __APPLE__ */
1094
1095     return VLC_SUCCESS;
1096 }
1097 #endif /* !WIN32 */