]> git.sesse.net Git - vlc/blob - modules/codec/quicktime.c
Copyright fixes
[vlc] / modules / codec / quicktime.c
1 /*****************************************************************************
2  * quicktime.c: a quicktime decoder that uses the QT library/dll
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN (Centrale Réseaux) and its contributors
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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
115 #endif /* LOADER */
116     HMODULE qtml;
117     OSErr (*InitializeQTML)             ( long flags );
118     OSErr (*TerminateQTML)              ( void );
119 #endif /* SYS_DARWIN */
120
121     /* Audio */
122     int (*SoundConverterOpen)           ( const SoundComponentData *,
123                                           const SoundComponentData *,
124                                           SoundConverter* );
125     int (*SoundConverterClose)          ( SoundConverter );
126     int (*SoundConverterSetInfo)        ( SoundConverter , OSType, void * );
127     int (*SoundConverterGetBufferSizes) ( SoundConverter, unsigned long,
128                                           unsigned long*, unsigned long*,
129                                           unsigned long* );
130     int (*SoundConverterBeginConversion)( SoundConverter );
131     int (*SoundConverterEndConversion)  ( SoundConverter, void *,
132                                           unsigned long *, unsigned long *);
133     int (*SoundConverterConvertBuffer)  ( SoundConverter, const void *,
134                                           unsigned long, void *,
135                                           unsigned long *, unsigned long * );
136     SoundConverter      myConverter;
137     SoundComponentData  InputFormatInfo, OutputFormatInfo;
138
139     long            FramesToGet;
140     unsigned int    InFrameSize;
141     unsigned int    OutFrameSize;
142
143 #ifndef WIN32
144     /* Video */
145     Component         (*FindNextComponent)
146         ( Component prev, ComponentDescription* desc );
147
148     ComponentInstance (*OpenComponent)
149         ( Component c );
150
151     ComponentResult   (*ImageCodecInitialize)
152         ( ComponentInstance ci, ImageSubCodecDecompressCapabilities * cap);
153
154     ComponentResult   (*ImageCodecGetCodecInfo)
155         ( ComponentInstance ci, CodecInfo *info );
156
157     ComponentResult   (*ImageCodecPreDecompress)
158         ( ComponentInstance ci, CodecDecompressParams * params );
159
160     ComponentResult   (*ImageCodecBandDecompress)
161         ( ComponentInstance ci, CodecDecompressParams * params );
162
163     PixMapHandle      (*GetGWorldPixMap)
164         ( GWorldPtr offscreenGWorld );
165
166     OSErr             (*QTNewGWorldFromPtr)
167         ( GWorldPtr *gw, OSType pixelFormat, const Rect *boundsRect,
168           CTabHandle cTable, /*GDHandle*/ void *aGDevice, /*unused*/
169           GWorldFlags flags, void *baseAddr, long rowBytes );
170
171     OSErr             (*NewHandleClear)( Size byteCount );
172
173     ComponentInstance       ci;
174     Rect                    OutBufferRect;   /* the dimensions of our GWorld */
175     GWorldPtr               OutBufferGWorld; /* a GWorld is some kind of
176                                                 description for a drawing
177                                                 environment */
178     ImageDescriptionHandle  framedescHandle;
179
180     CodecDecompressParams   decpar;          /* for ImageCodecPreDecompress()*/
181     CodecCapabilities       codeccap;        /* for decpar */
182 #endif
183
184     /* Output properties */
185     uint8_t *           plane;
186     mtime_t             pts;
187     audio_date_t        date;
188
189     int                 i_late; /* video */
190
191     /* buffer */
192     unsigned int        i_buffer;
193     unsigned int        i_buffer_size;
194     uint8_t             *p_buffer;
195
196     /* Audio only */
197     uint8_t             out_buffer[1000000];    /* FIXME */
198     int                 i_out_frames;
199     int                 i_out;
200 };
201
202 static int pi_channels_maps[6] =
203 {
204     0,
205     AOUT_CHAN_CENTER,
206     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
207     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
208     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
209     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
210      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
211 };
212
213 static int QTAudioInit( decoder_t * );
214 static int QTVideoInit( decoder_t * );
215
216 /*****************************************************************************
217  * Open: probe the decoder and return score
218  *****************************************************************************
219  * Tries to launch a decoder and return score so that the interface is able
220  * to choose.
221  *****************************************************************************/
222 static int Open( vlc_object_t *p_this )
223 {
224     decoder_t *p_dec = (decoder_t*)p_this;
225
226     switch( p_dec->fmt_in.i_codec )
227     {
228         case VLC_FOURCC('S','V','Q','3'): /* Sorenson v3 */
229     /*    case VLC_FOURCC('S','V','Q','1'):  Sorenson v1 
230         case VLC_FOURCC('Z','y','G','o'):
231         case VLC_FOURCC('V','P','3','1'):
232         case VLC_FOURCC('3','I','V','1'): */
233         case VLC_FOURCC('r','l','e',' '): /* QuickTime animation (RLE) */
234         case VLC_FOURCC('r','p','z','a'): /* QuickTime Apple Video */
235         case VLC_FOURCC('a','z','p','r'): /* QuickTime animation (RLE) */
236 #ifdef LOADER
237             p_dec->p_sys = NULL;
238             p_dec->pf_decode_video = DecodeVideo;
239             return VLC_SUCCESS;
240 #else
241             return OpenVideo( p_dec );
242 #endif
243
244         case VLC_FOURCC('s','a','m','r'): /* 3GPP AMR audio */
245         case VLC_FOURCC('m','p','4','a'): /* MPEG-4 audio */
246         case VLC_FOURCC('Q','D','M','C'): /* QDesign */
247         case VLC_FOURCC('Q','D','M','2'): /* QDesign* 2 */
248         case VLC_FOURCC('Q','c','l','p'): /* Qualcomm Purevoice Codec */
249         case VLC_FOURCC('Q','C','L','P'): /* Qualcomm Purevoice Codec */
250         case VLC_FOURCC('M','A','C','3'): /* MACE3 audio decoder */
251         case VLC_FOURCC('M','A','C','6'): /* MACE6 audio decoder */
252         case VLC_FOURCC('d','v','c','a'): /* DV Audio */
253         case VLC_FOURCC('s','o','w','t'): /* 16-bit Little Endian */
254         case VLC_FOURCC('t','w','o','s'): /* 16-bit Big Endian */
255         case VLC_FOURCC('a','l','a','w'): /* ALaw 2:1 */
256         case VLC_FOURCC('u','l','a','w'): /* mu-Law 2:1 */
257         case VLC_FOURCC('r','a','w',' '): /* 8-bit offset binaries */
258         case VLC_FOURCC('f','l','3','2'): /* 32-bit Floating Point */
259         case VLC_FOURCC('f','l','6','4'): /* 64-bit Floating Point */
260         case VLC_FOURCC('i','n','2','4'): /* 24-bit Interger */
261         case VLC_FOURCC('i','n','3','2'): /* 32-bit Integer */
262         case 0x0011:                            /* DVI IMA */
263         case 0x6D730002:                        /* Microsoft ADPCM-ACM */
264         case 0x6D730011:                        /* DVI Intel IMAADPCM-ACM */
265 #ifdef LOADER
266             p_dec->p_sys = NULL;
267             p_dec->pf_decode_audio = DecodeAudio;
268             return VLC_SUCCESS;
269 #else
270             return OpenAudio( p_dec );
271 #endif
272
273         default:
274             return VLC_EGENERIC;
275     }
276 }
277
278 /*****************************************************************************
279  * Close:
280  *****************************************************************************/
281 static void Close( vlc_object_t *p_this )
282 {
283     decoder_t     *p_dec = (decoder_t*)p_this;
284     decoder_sys_t *p_sys = p_dec->p_sys;
285     vlc_value_t   lockval;
286
287     /* get lock, avoid segfault */
288     var_Get( p_dec->p_libvlc, "qt_mutex", &lockval );
289     vlc_mutex_lock( lockval.p_address );
290 #ifdef SYS_DARWIN
291     /* on OS X QT is not threadsafe */
292     vlc_mutex_lock( &p_dec->p_vlc->quicktime_lock );
293 #endif
294
295     if( p_dec->fmt_out.i_cat == AUDIO_ES )
296     {
297         int i_error;
298         unsigned long ConvertedFrames=0;
299         unsigned long ConvertedBytes=0;
300
301         i_error = p_sys->SoundConverterEndConversion( p_sys->myConverter, NULL,
302                                                       &ConvertedFrames,
303                                                       &ConvertedBytes );
304         msg_Dbg( p_dec, "SoundConverterEndConversion => %d", i_error );
305
306         i_error = p_sys->SoundConverterClose( p_sys->myConverter );
307         msg_Dbg( p_dec, "SoundConverterClose => %d", i_error );
308
309         free( p_sys->p_buffer );
310     }
311     else if( p_dec->fmt_out.i_cat == VIDEO_ES )
312     {
313         free( p_sys->plane );
314     }
315
316 #ifndef SYS_DARWIN
317     FreeLibrary( p_sys->qtml );
318     msg_Dbg( p_dec, "FreeLibrary ok." );
319 #endif
320
321 #ifdef SYS_DARWIN
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             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->p_fifo, "ImageCodecInitialize->%p  size=%d (%d)\n",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 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->frameCount,
763              id->clutID );
764
765     p_sys->framedescHandle =
766         (ImageDescriptionHandle) p_sys->NewHandleClear( id->idSize );
767     memcpy( *p_sys->framedescHandle, id, id->idSize );
768
769     p_sys->plane = malloc( p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height * 3 );
770
771     i_result = p_sys->QTNewGWorldFromPtr( &p_sys->OutBufferGWorld,
772                                           /*pixel format of new GWorld==YUY2 */
773                                           kYUVSPixelFormat,
774                                           /* we should benchmark if yvu9 is
775                                            * faster for svq3, too */
776                                           &p_sys->OutBufferRect,
777                                           0, 0, 0,
778                                           p_sys->plane,
779                                           p_dec->fmt_in.video.i_width * 2 );
780
781     msg_Dbg( p_dec, "NewGWorldFromPtr returned:%ld\n",
782              65536 - ( i_result&0xffff ) );
783
784     memset( &p_sys->decpar, 0, sizeof( CodecDecompressParams ) );
785     p_sys->decpar.imageDescription = p_sys->framedescHandle;
786     p_sys->decpar.startLine        = 0;
787     p_sys->decpar.stopLine         = ( **p_sys->framedescHandle ).height;
788     p_sys->decpar.frameNumber      = 1;
789     p_sys->decpar.matrixFlags      = 0;
790     p_sys->decpar.matrixType       = 0;
791     p_sys->decpar.matrix           = 0;
792     p_sys->decpar.capabilities     = &p_sys->codeccap;
793     p_sys->decpar.accuracy         = codecNormalQuality;
794     p_sys->decpar.srcRect          = p_sys->OutBufferRect;
795     p_sys->decpar.transferMode     = srcCopy;
796     p_sys->decpar.dstPixMap        = **p_sys->GetGWorldPixMap( p_sys->OutBufferGWorld );/*destPixmap;  */
797
798     cres =  p_sys->ImageCodecPreDecompress( p_sys->ci, &p_sys->decpar );
799     msg_Dbg( p_dec, "quicktime_video: ImageCodecPreDecompress cres=0x%X\n",
800              (int)cres );
801
802     p_dec->fmt_out.i_codec = VLC_FOURCC( 'Y', 'U', 'Y', '2' );
803     p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
804     p_dec->fmt_out.video.i_height= p_dec->fmt_in.video.i_height;
805     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height;
806
807
808     vlc_mutex_unlock( lockval.p_address );
809     return VLC_SUCCESS;
810
811 exit_error:
812 #ifdef LOADER
813     Restore_LDT_Keeper( p_sys->ldt_fs );
814 #endif
815     vlc_mutex_unlock( lockval.p_address );
816
817 #endif /* !WIN32 */
818
819     return VLC_EGENERIC;
820 }
821
822 #ifndef WIN32
823 /*****************************************************************************
824  * DecodeVideo:
825  *****************************************************************************/
826 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
827 {
828     decoder_sys_t *p_sys = p_dec->p_sys;
829     block_t       *p_block;
830     picture_t     *p_pic;
831     mtime_t       i_pts;
832
833     ComponentResult cres;
834
835 #ifdef LOADER
836     /* We must do open and close in the same thread (unless we do
837      * Setup_LDT_Keeper in the main thread before all others */
838     if( p_sys == NULL )
839     {
840         if( OpenVideo( p_dec ) )
841         {
842             /* Fatal */
843             p_dec->b_error = VLC_TRUE;
844             return NULL;
845         }
846         p_sys = p_dec->p_sys;
847     }
848 #endif
849
850     if( pp_block == NULL || *pp_block == NULL )
851     {
852         return NULL;
853     }
854     p_block = *pp_block;
855     *pp_block = NULL;
856
857     i_pts = p_block->i_pts ? p_block->i_pts : p_block->i_dts;
858
859     if( i_pts < mdate() )
860     {
861         p_sys->i_late++;
862     }
863     else
864     {
865         p_sys->i_late = 0;
866     }
867
868     if( p_sys->i_late > 10 )
869     {
870         msg_Dbg( p_dec, "too late buffer -> dropped" );
871         block_Release( p_block );
872         return NULL;
873     }
874
875     if( ( p_pic = p_dec->pf_vout_buffer_new( p_dec ) ) )
876     {
877         vlc_value_t     lockval;
878
879         p_sys->decpar.data                  = p_block->p_buffer;
880         p_sys->decpar.bufferSize            = p_block->i_buffer;
881         (**p_sys->framedescHandle).dataSize = p_block->i_buffer;
882
883         var_Get( p_dec->p_libvlc, "qt_mutex", &lockval );
884         vlc_mutex_lock( lockval.p_address );
885         cres = p_sys->ImageCodecBandDecompress( p_sys->ci, &p_sys->decpar );
886         vlc_mutex_unlock( lockval.p_address );
887
888         ++p_sys->decpar.frameNumber;
889
890         if( cres &0xFFFF )
891         {
892             msg_Dbg( p_dec, "quicktime_video: ImageCodecBandDecompress"
893                      " cres=0x%X (-0x%X) %d :(\n",
894                      (int)cres,(int)-cres, (int)cres );
895         }
896
897         memcpy( p_pic->p[0].p_pixels, p_sys->plane,
898                 p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height * 2 );
899         p_pic->date = i_pts;
900     }
901     block_Release( p_block );
902
903     return p_pic;
904 }
905 #endif /* !WIN32 */
906
907 /*****************************************************************************
908  * QTAudioInit:
909  *****************************************************************************/
910 static int QTAudioInit( decoder_t *p_dec )
911 {
912     decoder_sys_t *p_sys = p_dec->p_sys;
913
914 #ifdef SYS_DARWIN
915     p_sys->SoundConverterOpen       = (void*)SoundConverterOpen;
916     p_sys->SoundConverterClose      = (void*)SoundConverterClose;
917     p_sys->SoundConverterSetInfo    = (void*)SoundConverterSetInfo;
918     p_sys->SoundConverterGetBufferSizes = (void*)SoundConverterGetBufferSizes;
919     p_sys->SoundConverterConvertBuffer  = (void*)SoundConverterConvertBuffer;
920     p_sys->SoundConverterBeginConversion= (void*)SoundConverterBeginConversion;
921     p_sys->SoundConverterEndConversion  = (void*)SoundConverterEndConversion;
922 #else
923
924 #ifdef LOADER
925     p_sys->ldt_fs = Setup_LDT_Keeper();
926 #endif /* LOADER */
927
928     p_sys->qtml = LoadLibraryA( "qtmlClient.dll" );
929     if( p_sys->qtml == NULL )
930     {
931         msg_Dbg( p_dec, "failed loading qtmlClient.dll" );
932         return VLC_EGENERIC;
933     }
934
935     p_sys->InitializeQTML               = (void *)GetProcAddress( p_sys->qtml, "InitializeQTML" );
936     p_sys->TerminateQTML                = (void *)GetProcAddress( p_sys->qtml, "TerminateQTML" );
937     p_sys->SoundConverterOpen           = (void *)GetProcAddress( p_sys->qtml, "SoundConverterOpen" );
938     p_sys->SoundConverterClose          = (void *)GetProcAddress( p_sys->qtml, "SoundConverterClose" );
939     p_sys->SoundConverterSetInfo        = (void *)GetProcAddress( p_sys->qtml, "SoundConverterSetInfo" );
940     p_sys->SoundConverterGetBufferSizes = (void *)GetProcAddress( p_sys->qtml, "SoundConverterGetBufferSizes" );
941     p_sys->SoundConverterConvertBuffer  = (void *)GetProcAddress( p_sys->qtml, "SoundConverterConvertBuffer" );
942     p_sys->SoundConverterEndConversion  = (void *)GetProcAddress( p_sys->qtml, "SoundConverterEndConversion" );
943     p_sys->SoundConverterBeginConversion= (void *)GetProcAddress( p_sys->qtml, "SoundConverterBeginConversion");
944
945     if( p_sys->InitializeQTML == NULL )
946     {
947         msg_Err( p_dec, "failed getting proc address InitializeQTML" );
948         return VLC_EGENERIC;
949     }
950     if( p_sys->SoundConverterOpen == NULL ||
951         p_sys->SoundConverterClose == NULL ||
952         p_sys->SoundConverterSetInfo == NULL ||
953         p_sys->SoundConverterGetBufferSizes == NULL ||
954         p_sys->SoundConverterConvertBuffer == NULL ||
955         p_sys->SoundConverterEndConversion == NULL ||
956         p_sys->SoundConverterBeginConversion == NULL )
957     {
958         msg_Err( p_dec, "failed getting proc address" );
959         return VLC_EGENERIC;
960     }
961
962     msg_Dbg( p_dec, "Standard init done" );
963 #endif /* else SYS_DARWIN */
964
965     return VLC_SUCCESS;
966 }
967
968 #ifndef WIN32
969 /*****************************************************************************
970  * QTVideoInit:
971  *****************************************************************************/
972 static int QTVideoInit( decoder_t *p_dec )
973 {
974     decoder_sys_t *p_sys = p_dec->p_sys;
975
976 #ifdef SYS_DARWIN
977     p_sys->FindNextComponent        = (void*)FindNextComponent;
978     p_sys->OpenComponent            = (void*)OpenComponent;
979     p_sys->ImageCodecInitialize     = (void*)ImageCodecInitialize;
980     p_sys->ImageCodecGetCodecInfo   = (void*)ImageCodecGetCodecInfo;
981     p_sys->ImageCodecPreDecompress  = (void*)ImageCodecPreDecompress;
982     p_sys->ImageCodecBandDecompress = (void*)ImageCodecBandDecompress;
983     p_sys->GetGWorldPixMap          = (void*)GetGWorldPixMap;
984     p_sys->QTNewGWorldFromPtr       = (void*)QTNewGWorldFromPtr;
985     p_sys->NewHandleClear           = (void*)NewHandleClear;
986 #else
987
988 #ifdef LOADER
989     p_sys->ldt_fs = Setup_LDT_Keeper();
990 #endif /* LOADER */
991     p_sys->qtml = LoadLibraryA( "qtmlClient.dll" );
992     if( p_sys->qtml == NULL )
993     {
994         msg_Dbg( p_dec, "failed loading qtmlClient.dll" );
995         return VLC_EGENERIC;
996     }
997     msg_Dbg( p_dec, "qtmlClient.dll loaded" );
998
999     /* (void*) to shut up gcc */
1000     p_sys->InitializeQTML           = (void*)GetProcAddress( p_sys->qtml, "InitializeQTML" );
1001     p_sys->FindNextComponent        = (void*)GetProcAddress( p_sys->qtml, "FindNextComponent" );
1002     p_sys->OpenComponent            = (void*)GetProcAddress( p_sys->qtml, "OpenComponent" );
1003     p_sys->ImageCodecInitialize     = (void*)GetProcAddress( p_sys->qtml, "ImageCodecInitialize" );
1004     p_sys->ImageCodecGetCodecInfo   = (void*)GetProcAddress( p_sys->qtml, "ImageCodecGetCodecInfo" );
1005     p_sys->ImageCodecPreDecompress  = (void*)GetProcAddress( p_sys->qtml, "ImageCodecPreDecompress" );
1006     p_sys->ImageCodecBandDecompress = (void*)GetProcAddress( p_sys->qtml, "ImageCodecBandDecompress" );
1007     p_sys->GetGWorldPixMap          = (void*)GetProcAddress( p_sys->qtml, "GetGWorldPixMap" );
1008     p_sys->QTNewGWorldFromPtr       = (void*)GetProcAddress( p_sys->qtml, "QTNewGWorldFromPtr" );
1009     p_sys->NewHandleClear           = (void*)GetProcAddress( p_sys->qtml, "NewHandleClear" );
1010
1011     if( p_sys->InitializeQTML == NULL )
1012     {
1013         msg_Dbg( p_dec, "failed getting proc address InitializeQTML" );
1014         return VLC_EGENERIC;
1015     }
1016     if( p_sys->FindNextComponent == NULL ||
1017         p_sys->OpenComponent == NULL ||
1018         p_sys->ImageCodecInitialize == NULL ||
1019         p_sys->ImageCodecGetCodecInfo == NULL ||
1020         p_sys->ImageCodecPreDecompress == NULL ||
1021         p_sys->ImageCodecBandDecompress == NULL ||
1022         p_sys->GetGWorldPixMap == NULL ||
1023         p_sys->QTNewGWorldFromPtr == NULL ||
1024         p_sys->NewHandleClear == NULL )
1025     {
1026         msg_Err( p_dec, "failed getting proc address" );
1027         return VLC_EGENERIC;
1028     }
1029 #endif /* SYS_DARWIN */
1030
1031     return VLC_SUCCESS;
1032 }
1033 #endif /* !WIN32 */