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