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