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