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