]> git.sesse.net Git - vlc/blob - modules/codec/quicktime.c
* ALL: final improvements to the decoders/packetizers api.
[vlc] / modules / codec / quicktime.c
1 /*****************************************************************************
2  * quicktime.c: a quicktime decoder that uses the QT library/dll
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: quicktime.c,v 1.14 2003/10/01 20:57:34 hartman Exp $
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 #include <vlc/vlc.h>
29 #include <vlc/aout.h>
30 #include <vlc/vout.h>
31 #include <vlc/decoder.h>
32 #include <vlc/input.h>
33
34 #include <stdlib.h>                                      /* malloc(), free() */
35 #include <string.h>                                              /* strdup() */
36 #include "codecs.h"
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
46 #ifdef LOADER
47 #include "w32dll/loader/qtx/qtxsdk/components.h"
48 #include "w32dll/loader/wine/windef.h"
49 #include "w32dll/loader/ldt_keeper.h"
50
51 HMODULE   WINAPI LoadLibraryA(LPCSTR);
52 FARPROC   WINAPI GetProcAddress(HMODULE,LPCSTR);
53 int       WINAPI FreeLibrary(HMODULE);
54
55 #endif
56
57 /*****************************************************************************
58  * Local prototypes
59  *****************************************************************************/
60 static int  OpenDecoder    ( vlc_object_t * );
61
62 static int  RunDecoderAudio( decoder_fifo_t * );
63 static int  RunDecoderVideo( decoder_fifo_t * );
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68
69 vlc_module_begin();
70     set_description( _("QuickTime library decoder") );
71     set_capability( "decoder", 10 );
72     set_callbacks( OpenDecoder, NULL );
73
74     /* create a mutex */
75     var_Create( p_module->p_libvlc, "qt_mutex", VLC_VAR_MUTEX );
76 vlc_module_end();
77
78 #define FCC( a, b , c, d ) \
79     ((uint32_t)( ((a)<<24)|((b)<<16)|((c)<<8)|(d)))
80
81 #ifndef SYS_DARWIN
82 typedef struct OpaqueSoundConverter*    SoundConverter;
83 typedef long                            OSType;
84 typedef int                             OSErr;
85 typedef unsigned long                   UnsignedFixed;
86 typedef uint8_t                         Byte;
87
88 typedef struct SoundComponentData {
89     long                            flags;
90     OSType                          format;
91     short                           numChannels;
92     short                           sampleSize;
93     UnsignedFixed                   sampleRate;
94     long                            sampleCount;
95     Byte *                          buffer;
96     long                            reserved;
97 } SoundComponentData;
98
99 #endif /* SYS_DARWIN */
100
101 typedef struct
102 {
103     /* Input properties */
104     decoder_fifo_t *p_fifo;
105
106     /* library */
107 #ifndef SYS_DARWIN
108 #ifdef LOADER
109     ldt_fs_t    *ldt_fs;
110
111 #endif /* LOADER */
112     HMODULE qtml;
113     OSErr (*InitializeQTML)             ( long flags );
114     OSErr (*TerminateQTML)              ( void );
115 #endif /* SYS_DARWIN */
116
117     int (*SoundConverterOpen)           ( const SoundComponentData *,
118                                           const SoundComponentData *,
119                                           SoundConverter* );
120     int (*SoundConverterClose)          ( SoundConverter );
121     int (*SoundConverterSetInfo)        ( SoundConverter , OSType, void * );
122     int (*SoundConverterGetBufferSizes) ( SoundConverter, unsigned long,
123                                           unsigned long*, unsigned long*,
124                                           unsigned long* );
125     int (*SoundConverterBeginConversion)( SoundConverter );
126     int (*SoundConverterEndConversion)  ( SoundConverter, void *,
127                                           unsigned long *, unsigned long *);
128     int (*SoundConverterConvertBuffer)  ( SoundConverter, const void *,
129                                           unsigned long, void *,
130                                           unsigned long *, unsigned long * );
131     SoundConverter      myConverter;
132     SoundComponentData  InputFormatInfo, OutputFormatInfo;
133
134     long            FramesToGet;
135     unsigned int    InFrameSize;
136     unsigned int    OutFrameSize;
137
138     /* Output properties */
139     aout_instance_t *   p_aout;       /* opaque */
140     aout_input_t *      p_aout_input; /* opaque */
141     audio_sample_format_t output_format;
142
143     audio_date_t        date;
144     mtime_t             pts;
145
146     /* buffer */
147     unsigned int        i_buffer;
148     unsigned int        i_buffer_size;
149     uint8_t             *p_buffer;
150
151     uint8_t             buffer_out[1000000];    /* FIXME */
152
153 } adec_thread_t;
154
155 #ifndef WIN32
156 typedef struct
157 {
158     /* Input properties */
159     decoder_fifo_t *p_fifo;
160
161     /* library */
162 #ifndef SYS_DARWIN
163 #ifdef LOADER
164     ldt_fs_t          *ldt_fs;
165 #endif /* LOADER */
166     HMODULE           qtml;
167     OSErr             (*InitializeQTML)         ( long flags );
168
169 #endif /* SYS_DARWIN */
170     Component         (*FindNextComponent)
171         ( Component prev, ComponentDescription* desc );
172
173     ComponentInstance (*OpenComponent)
174         ( Component c );
175
176     ComponentResult   (*ImageCodecInitialize)
177         ( ComponentInstance ci, ImageSubCodecDecompressCapabilities * cap);
178
179     ComponentResult   (*ImageCodecGetCodecInfo)
180         ( ComponentInstance ci, CodecInfo *info );
181
182     ComponentResult   (*ImageCodecPreDecompress)
183         ( ComponentInstance ci, CodecDecompressParams * params );
184
185     ComponentResult   (*ImageCodecBandDecompress)
186         ( ComponentInstance ci, CodecDecompressParams * params );
187
188     PixMapHandle      (*GetGWorldPixMap)
189         ( GWorldPtr offscreenGWorld );
190
191     OSErr             (*QTNewGWorldFromPtr)
192         ( GWorldPtr *gw, OSType pixelFormat, const Rect *boundsRect,
193           CTabHandle cTable, /*GDHandle*/ void *aGDevice, /*unused*/
194           GWorldFlags flags, void *baseAddr, long rowBytes );
195
196     OSErr             (*NewHandleClear)
197         ( Size byteCount );
198
199     ComponentInstance       ci;
200     Rect                    OutBufferRect;   /* the dimensions of our GWorld */
201     GWorldPtr               OutBufferGWorld; /* a GWorld is some kind of
202                                                 description for a drawing
203                                                 environment */
204     ImageDescriptionHandle  framedescHandle;
205
206     CodecDecompressParams   decpar;          /* for ImageCodecPreDecompress()*/
207     CodecCapabilities       codeccap;        /* for decpar */
208
209
210     /* Output properties */
211     vout_thread_t *     p_vout;
212     uint8_t *           plane;
213     mtime_t             pts;
214
215     /* buffer */
216     unsigned int        i_buffer;
217     uint8_t             *p_buffer;
218
219 } vdec_thread_t;
220 #endif
221
222 static int pi_channels_maps[6] =
223 {
224     0,
225     AOUT_CHAN_CENTER,
226     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
227     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
228     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
229     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
230      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
231 };
232
233
234 static int GetPESData( uint8_t *p_buf, int i_max, pes_packet_t *p_pes )
235 {
236     int i_copy;
237     int i_count;
238
239     data_packet_t   *p_data;
240
241     i_count = 0;
242     p_data = p_pes->p_first;
243     while( p_data != NULL && i_count < i_max )
244     {
245
246         i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
247                         i_max - i_count );
248
249         if( i_copy > 0 )
250         {
251             memcpy( p_buf,
252                     p_data->p_payload_start,
253                     i_copy );
254         }
255
256         p_data = p_data->p_next;
257         i_count += i_copy;
258         p_buf   += i_copy;
259     }
260
261     if( i_count < i_max )
262     {
263         memset( p_buf, 0, i_max - i_count );
264     }
265     return( i_count );
266 }
267
268 /*****************************************************************************
269  * OpenDecoder: probe the decoder and return score
270  *****************************************************************************
271  * Tries to launch a decoder and return score so that the interface is able
272  * to choose.
273  *****************************************************************************/
274 static int OpenDecoder( vlc_object_t *p_this )
275 {
276     decoder_t *p_dec = (decoder_t*)p_this;
277
278     switch( p_dec->p_fifo->i_fourcc )
279     {
280         case VLC_FOURCC('S','V','Q','3'): /* Sorenson v3 */
281     /*    case VLC_FOURCC('S','V','Q','1'):  Sorenson v1 
282         case VLC_FOURCC('Z','y','G','o'):
283         case VLC_FOURCC('V','P','3','1'):
284         case VLC_FOURCC('3','I','V','1'): */
285         case VLC_FOURCC('r','l','e',' '): /* QuickTime animation (RLE) */
286         case VLC_FOURCC('r','p','z','a'): /* QuickTime Apple Video */
287         case VLC_FOURCC('a','z','p','r'): /* QuickTime animation (RLE) */
288             p_dec->pf_run = RunDecoderVideo;
289             return VLC_SUCCESS;
290
291         case VLC_FOURCC('s','a','m','r'): /* 3GPP AMR audio */
292         case VLC_FOURCC('m','p','4','a'): /* MPEG-4 audio */
293         case VLC_FOURCC('Q','D','M','C'): /* QDesign */
294         case VLC_FOURCC('Q','D','M','2'): /* QDesign* 2 */
295         case VLC_FOURCC('Q','c','l','p'): /* Qualcomm Purevoice Codec */
296         case VLC_FOURCC('Q','C','L','P'): /* Qualcomm Purevoice Codec */
297         case VLC_FOURCC('M','A','C','3'): /* MACE3 audio decoder */
298         case VLC_FOURCC('M','A','C','6'): /* MACE6 audio decoder */
299         case VLC_FOURCC('d','v','c','a'): /* DV Audio */
300         case VLC_FOURCC('s','o','w','t'): /* 16-bit Little Endian */
301         case VLC_FOURCC('t','w','o','s'): /* 16-bit Big Endian */
302         case VLC_FOURCC('a','l','a','w'): /* ALaw 2:1 */
303         case VLC_FOURCC('u','l','a','w'): /* mu-Law 2:1 */
304         case VLC_FOURCC('r','a','w',' '): /* 8-bit offset binaries */
305         case VLC_FOURCC('f','l','3','2'): /* 32-bit Floating Point */
306         case VLC_FOURCC('f','l','6','4'): /* 64-bit Floating Point */
307         case VLC_FOURCC('i','n','2','4'): /* 24-bit Interger */
308         case VLC_FOURCC('i','n','3','2'): /* 32-bit Integer */
309         case 0x0011:                            /* DVI IMA */
310         case 0x6D730002:                        /* Microsoft ADPCM-ACM */
311         case 0x6D730011:                        /* DVI Intel IMAADPCM-ACM */
312
313             p_dec->pf_run = RunDecoderAudio;
314             return VLC_SUCCESS;
315         default:
316             return VLC_EGENERIC;
317     }
318 }
319
320 /****************************************************************************
321  ****************************************************************************
322  **
323  **     audio part
324  **
325  **************************************************************************** 
326  ****************************************************************************/
327 static int  InitThreadAudio     ( adec_thread_t * );
328 static void DecodeThreadAudio   ( adec_thread_t * );
329 static void EndThreadAudio      ( adec_thread_t * );
330 static int  QTAudioInit         ( adec_thread_t * );
331
332 static int RunDecoderAudio( decoder_fifo_t *p_fifo )
333 {
334     adec_thread_t *p_dec;
335     vlc_bool_t    b_error;
336
337     p_dec = malloc( sizeof( adec_thread_t ) );
338     if( !p_dec )
339     {
340         msg_Err( p_fifo, "out of memory" );
341         DecoderError( p_fifo );
342         return VLC_EGENERIC;
343     }
344     p_dec->p_fifo = p_fifo;
345
346     if( InitThreadAudio( p_dec ) != 0 )
347     {
348         DecoderError( p_fifo );
349         return VLC_EGENERIC;
350     }
351
352     while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
353     {
354         DecodeThreadAudio( p_dec );
355     }
356
357
358     if( ( b_error = p_dec->p_fifo->b_error ) )
359     {
360         DecoderError( p_dec->p_fifo );
361     }
362
363     EndThreadAudio( p_dec );
364     if( b_error )
365     {
366         return VLC_EGENERIC;
367     }
368
369     return VLC_SUCCESS;
370 }
371
372 static int InitThreadAudio( adec_thread_t *p_dec )
373 {
374     vlc_value_t     lockval;
375     int             i_error;
376     char            fcc[4];
377     unsigned long   WantedBufferSize;
378     unsigned long   InputBufferSize = 0;
379     unsigned long   OutputBufferSize = 0;
380
381     WAVEFORMATEX    *p_wf;
382
383     if( !( p_wf = (WAVEFORMATEX*)p_dec->p_fifo->p_waveformatex ) )
384     {
385         msg_Err( p_dec->p_fifo, "missing WAVEFORMATEX");
386         return VLC_EGENERIC;
387     }
388     memcpy( fcc, &p_dec->p_fifo->i_fourcc, 4 );
389
390     /* get lock, avoid segfault */
391     var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
392     vlc_mutex_lock( lockval.p_address );
393
394 #ifdef SYS_DARWIN
395     EnterMovies();
396 #endif
397
398     if( QTAudioInit( p_dec ) != VLC_SUCCESS )
399     {
400         msg_Err( p_dec->p_fifo, "cannot initialize QT");
401         goto exit_error;
402     }
403
404 #ifndef SYS_DARWIN
405     if( ( i_error = p_dec->InitializeQTML( 6 + 16 ) ) )
406     {
407         msg_Dbg( p_dec->p_fifo, "error while InitializeQTML = %d", i_error );
408         goto exit_error;
409     }
410 #endif
411
412     /* input format settings */
413     p_dec->InputFormatInfo.flags       = 0;
414     p_dec->InputFormatInfo.sampleCount = 0;
415     p_dec->InputFormatInfo.buffer      = NULL;
416     p_dec->InputFormatInfo.reserved    = 0;
417     p_dec->InputFormatInfo.numChannels = p_wf->nChannels;
418     p_dec->InputFormatInfo.sampleSize  = p_wf->wBitsPerSample;
419     p_dec->InputFormatInfo.sampleRate  = p_wf->nSamplesPerSec;
420     p_dec->InputFormatInfo.format      = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
421
422     /* output format settings */
423     p_dec->OutputFormatInfo.flags       = 0;
424     p_dec->OutputFormatInfo.sampleCount = 0;
425     p_dec->OutputFormatInfo.buffer      = NULL;
426     p_dec->OutputFormatInfo.reserved    = 0;
427     p_dec->OutputFormatInfo.numChannels = p_wf->nChannels;
428     p_dec->OutputFormatInfo.sampleSize  = 16;
429     p_dec->OutputFormatInfo.sampleRate  = p_wf->nSamplesPerSec;
430     p_dec->OutputFormatInfo.format      = FCC( 'N', 'O', 'N', 'E' );
431
432 #ifdef SYS_DARWIN
433 /* on OS X QT is not threadsafe */
434     vlc_mutex_lock( &p_dec->p_fifo->p_vlc->quicktime_lock );
435 #endif
436
437     i_error = p_dec->SoundConverterOpen( &p_dec->InputFormatInfo,
438                                          &p_dec->OutputFormatInfo,
439                                          &p_dec->myConverter );
440     if( i_error )
441     {
442         msg_Err( p_dec->p_fifo,
443                  "error while SoundConverterOpen = %d", i_error );
444         goto exit_error;
445     }
446
447 #if 0
448     /* tell the sound converter we accept VBR formats */
449     i_error = SoundConverterSetInfo( p_dec->myConverter, siClientAcceptsVBR,
450                                      (void *)true );
451 #endif
452
453     if( p_wf->cbSize > 36 + 8 )
454     {
455         i_error =
456             p_dec->SoundConverterSetInfo( p_dec->myConverter,
457                                           FCC( 'w', 'a', 'v', 'e' ),
458                                           ((uint8_t*)&p_wf[1]) + 36 + 8 );
459         msg_Dbg( p_dec->p_fifo,
460                  "error while SoundConverterSetInfo = %d", i_error );
461     }
462
463     WantedBufferSize = p_dec->OutputFormatInfo.numChannels *
464                          p_dec->OutputFormatInfo.sampleRate * 2;
465     p_dec->FramesToGet = 0;
466
467     i_error = p_dec->SoundConverterGetBufferSizes( p_dec->myConverter,
468                   WantedBufferSize, &p_dec->FramesToGet,
469                   &InputBufferSize, &OutputBufferSize );
470
471     msg_Dbg( p_dec->p_fifo, "WantedBufferSize=%li InputBufferSize=%li "
472              "OutputBufferSize=%li FramesToGet=%li", WantedBufferSize,
473              InputBufferSize, OutputBufferSize, p_dec->FramesToGet );
474
475     p_dec->InFrameSize  = (InputBufferSize + p_dec->FramesToGet - 1 ) /
476                             p_dec->FramesToGet;
477     p_dec->OutFrameSize = OutputBufferSize / p_dec->FramesToGet;
478
479     msg_Dbg( p_dec->p_fifo, "frame size %d -> %d",
480              p_dec->InFrameSize, p_dec->OutFrameSize );
481
482     i_error = p_dec->SoundConverterBeginConversion( p_dec->myConverter );
483     if( i_error )
484     {
485         msg_Err( p_dec->p_fifo,
486                  "error while SoundConverterBeginConversion = %d", i_error );
487         goto exit_error;
488     }
489
490 #ifdef SYS_DARWIN
491     vlc_mutex_unlock( &p_dec->p_fifo->p_vlc->quicktime_lock );
492 #endif
493
494     p_dec->output_format.i_format   = AOUT_FMT_S16_NE;
495     p_dec->output_format.i_rate     = p_dec->OutputFormatInfo.sampleRate;
496     p_dec->output_format.i_physical_channels =
497         p_dec->output_format.i_original_channels =
498             pi_channels_maps[p_dec->OutputFormatInfo.numChannels];
499     aout_DateInit( &p_dec->date, p_dec->output_format.i_rate );
500     p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
501                                        &p_dec->p_aout,
502                                        &p_dec->output_format );
503     if( !p_dec->p_aout_input )
504     {
505         msg_Err( p_dec->p_fifo, "cannot create aout" );
506         goto exit_error;
507     }
508
509     p_dec->i_buffer      = 0;
510     p_dec->i_buffer_size = 100*1000;
511     p_dec->p_buffer      = malloc( p_dec->i_buffer_size );
512
513     vlc_mutex_unlock( lockval.p_address );
514     return VLC_SUCCESS;
515
516 exit_error:
517 #ifdef LOADER
518     Restore_LDT_Keeper( p_dec->ldt_fs );
519 #endif
520     vlc_mutex_unlock( lockval.p_address );
521     return VLC_EGENERIC;
522 }
523
524 static void DecodeThreadAudio( adec_thread_t *p_dec )
525 {
526     pes_packet_t    *p_pes;
527     vlc_value_t     lockval;
528     int             i_error;
529
530     var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
531
532     input_ExtractPES( p_dec->p_fifo, &p_pes );
533     if( !p_pes )
534     {
535         msg_Err( p_dec->p_fifo, "cannot get PES" );
536         p_dec->p_fifo->b_error = 1;
537         return;
538     }
539
540     p_dec->pts = p_pes->i_pts;
541
542     if( p_pes->i_pes_size > 0 && p_pes->i_pts > mdate() )
543     {
544
545         if( p_dec->i_buffer_size < p_dec->i_buffer + p_pes->i_pes_size )
546         {
547             p_dec->i_buffer_size = p_dec->i_buffer + p_pes->i_pes_size + 1024;
548             p_dec->p_buffer = realloc( p_dec->p_buffer,
549                                        p_dec->i_buffer_size );
550         }
551
552         GetPESData( &p_dec->p_buffer[p_dec->i_buffer],
553                     p_dec->i_buffer_size - p_dec->i_buffer, p_pes );
554         p_dec->i_buffer += p_pes->i_pes_size;
555
556         if( p_dec->i_buffer > p_dec->InFrameSize )
557         {
558             int i_frames = p_dec->i_buffer / p_dec->InFrameSize;
559             long i_out_frames, i_out_bytes;
560             /* enough data */
561
562             vlc_mutex_lock( lockval.p_address );
563             i_error = p_dec->SoundConverterConvertBuffer( p_dec->myConverter,
564                                                           p_dec->p_buffer,
565                                                           i_frames,
566                                                           p_dec->buffer_out,
567                                                           &i_out_frames,
568                                                           &i_out_bytes );
569             vlc_mutex_unlock( lockval.p_address );
570
571             /*
572             msg_Dbg( p_dec->p_fifo,
573                      "decoded %d frames -> %ld frames (error=%d)",
574                      i_frames, i_out_frames, i_error );
575
576             msg_Dbg( p_dec->p_fifo, "decoded %ld frames = %ld bytes",
577                      i_out_frames, i_out_bytes );
578             */
579
580             p_dec->i_buffer -= i_frames * p_dec->InFrameSize;
581             if( p_dec->i_buffer > 0 )
582             {
583                 memmove( &p_dec->p_buffer[0],
584                          &p_dec->p_buffer[i_frames * p_dec->InFrameSize],
585                          p_dec->i_buffer );
586             }
587
588             if( !i_error && i_out_frames > 0 )
589             {
590                 aout_buffer_t   *p_aout_buffer;
591                 uint8_t         *p_buff = p_dec->buffer_out;
592
593                 /*msg_Dbg( p_dec->p_fifo, "pts=%lld date=%lld dateget=%lld",
594                          p_dec->pts, mdate(), aout_DateGet( &p_dec->date ) );*/
595
596                 if( p_dec->pts != 0 &&
597                     p_dec->pts != aout_DateGet( &p_dec->date ) )
598                 {
599                     aout_DateSet( &p_dec->date, p_dec->pts );
600                 }
601                 else if( !aout_DateGet( &p_dec->date ) )
602                 {
603                     input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
604                     return;
605                 }
606
607                 while( i_out_frames > 0 )
608                 {
609                     int i_frames;
610
611                     i_frames = __MIN( i_out_frames, 1000 );
612                     p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout,
613                                                        p_dec->p_aout_input,
614                                                        i_frames );
615                     if( !p_aout_buffer )
616                     {
617                         msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
618                         p_dec->p_fifo->b_error = 1;
619                         return;
620                     }
621                     p_aout_buffer->start_date = aout_DateGet( &p_dec->date );
622                     p_aout_buffer->end_date = aout_DateIncrement( &p_dec->date,
623                                                                   i_frames );
624
625                     memcpy( p_aout_buffer->p_buffer,
626                             p_buff,
627                             p_aout_buffer->i_nb_bytes );
628
629                     /*
630                     msg_Dbg( p_dec->p_fifo,
631                              "==> start=%lld end=%lld date=%lld",
632                              p_aout_buffer->start_date,
633                              p_aout_buffer->end_date, mdate() );
634                     */
635
636                     aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input,
637                                   p_aout_buffer );
638
639                     /*
640                     msg_Dbg( p_dec->p_fifo, "s1=%d s2=%d", i_framesperchannels,
641                              p_aout_buffer->i_nb_samples );
642
643                     msg_Dbg( p_dec->p_fifo, "i_nb_bytes=%d i_nb_samples*4=%d",
644                              p_aout_buffer->i_nb_bytes,
645                              p_aout_buffer->i_nb_samples * 4 );
646                     */
647
648                     p_buff += i_frames * 4;
649                     i_out_frames -= i_frames;
650                 }
651             }
652         }
653     }
654
655     input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
656 }
657
658 static void EndThreadAudio( adec_thread_t *p_dec )
659 {
660     vlc_value_t             lockval;
661     int i_error;
662     unsigned long ConvertedFrames=0;
663     unsigned long ConvertedBytes=0;
664
665     /* get lock, avoid segfault */
666     var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
667     vlc_mutex_lock( lockval.p_address );
668
669     i_error = p_dec->SoundConverterEndConversion( p_dec->myConverter, NULL,
670                                                   &ConvertedFrames,
671                                                   &ConvertedBytes );
672     msg_Dbg( p_dec->p_fifo, "SoundConverterEndConversion => %d", i_error );
673
674     i_error = p_dec->SoundConverterClose( p_dec->myConverter );
675     msg_Dbg( p_dec->p_fifo, "SoundConverterClose => %d", i_error );
676
677 #ifndef SYS_DARWIN
678     FreeLibrary( p_dec->qtml );
679     msg_Dbg( p_dec->p_fifo, "FreeLibrary ok." );
680 #endif
681     vlc_mutex_unlock( lockval.p_address );
682
683 #ifdef LOADER
684     Restore_LDT_Keeper( p_dec->ldt_fs );
685     msg_Dbg( p_dec->p_fifo, "Restore_LDT_Keeper" );
686 #endif
687 #ifdef SYS_DARWIN
688     ExitMovies();
689 #endif
690     aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
691 }
692
693 static int QTAudioInit( adec_thread_t *p_dec )
694 {
695
696 #ifdef SYS_DARWIN
697     p_dec->SoundConverterOpen       = (void*)SoundConverterOpen;
698     p_dec->SoundConverterClose      = (void*)SoundConverterClose;
699     p_dec->SoundConverterSetInfo    = (void*)SoundConverterSetInfo;
700     p_dec->SoundConverterGetBufferSizes = (void*)SoundConverterGetBufferSizes;
701     p_dec->SoundConverterConvertBuffer  = (void*)SoundConverterConvertBuffer;
702     p_dec->SoundConverterBeginConversion= (void*)SoundConverterBeginConversion;
703     p_dec->SoundConverterEndConversion  = (void*)SoundConverterEndConversion;
704 #else
705
706 #ifdef LOADER
707     p_dec->ldt_fs = Setup_LDT_Keeper();
708 #endif /* LOADER */
709
710     p_dec->qtml = LoadLibraryA( "qtmlClient.dll" );
711     if( p_dec->qtml == NULL )
712     {
713         msg_Dbg( p_dec->p_fifo, "failed loading qtmlClient.dll" );
714         return VLC_EGENERIC;
715     }
716
717     p_dec->InitializeQTML =
718         (void *)GetProcAddress( p_dec->qtml, "InitializeQTML" );
719     if( p_dec->InitializeQTML == NULL )
720     {
721         msg_Dbg( p_dec->p_fifo, "failed geting proc address InitializeQTML" );
722         return VLC_EGENERIC;
723     }
724
725     p_dec->SoundConverterOpen =
726         (void *)GetProcAddress( p_dec->qtml, "SoundConverterOpen" );
727     if( p_dec->SoundConverterOpen == NULL )
728     {
729         msg_Dbg( p_dec->p_fifo,
730                  "failed getting proc address SoundConverterOpen");
731         return VLC_EGENERIC;
732     }
733
734     p_dec->SoundConverterClose =
735         (void *)GetProcAddress( p_dec->qtml, "SoundConverterClose" );
736     if( p_dec->SoundConverterClose == NULL )
737     {
738         msg_Dbg( p_dec->p_fifo,
739                  "failed getting proc address SoundConverterClose");
740         return VLC_EGENERIC;
741     }
742
743     p_dec->TerminateQTML =
744         (void *)GetProcAddress( p_dec->qtml, "TerminateQTML" );
745     if( p_dec->TerminateQTML == NULL )
746     {
747         msg_Dbg( p_dec->p_fifo, "failed getting proc address TerminateQTML");
748         return VLC_EGENERIC;
749     }
750
751     p_dec->SoundConverterSetInfo =
752         (void *)GetProcAddress( p_dec->qtml, "SoundConverterSetInfo" );
753     if( p_dec->SoundConverterSetInfo == NULL )
754     {
755         msg_Dbg( p_dec->p_fifo,
756                  "failed getting proc address SoundConverterSetInfo");
757         return VLC_EGENERIC;
758     }
759
760     p_dec->SoundConverterGetBufferSizes =
761         (void *)GetProcAddress( p_dec->qtml, "SoundConverterGetBufferSizes" );
762     if( p_dec->SoundConverterGetBufferSizes == NULL )
763     {
764         msg_Dbg( p_dec->p_fifo,
765                  "failed getting proc address SoundConverterGetBufferSizes");
766         return VLC_EGENERIC;
767     }
768
769     p_dec->SoundConverterConvertBuffer =
770         (void *)GetProcAddress( p_dec->qtml, "SoundConverterConvertBuffer" );
771     if( p_dec->SoundConverterConvertBuffer == NULL )
772     {
773         msg_Dbg( p_dec->p_fifo,
774                  "failed getting proc address SoundConverterConvertBuffer" );
775         return VLC_EGENERIC;
776     }
777
778     p_dec->SoundConverterEndConversion =
779         (void *)GetProcAddress( p_dec->qtml, "SoundConverterEndConversion" );
780     if( p_dec->SoundConverterEndConversion == NULL )
781     {
782         msg_Dbg( p_dec->p_fifo,
783                  "failed getting proc address SoundConverterEndConversion" );
784         return VLC_EGENERIC;
785     }
786
787     p_dec->SoundConverterBeginConversion =
788         (void *)GetProcAddress( p_dec->qtml, "SoundConverterBeginConversion");
789     if( p_dec->SoundConverterBeginConversion == NULL )
790     {
791         msg_Dbg( p_dec->p_fifo,
792                  "failed getting proc address SoundConverterBeginConversion" );
793         return VLC_EGENERIC;
794     }
795
796     msg_Dbg( p_dec->p_fifo,
797              "Standard init done you may now call supported functions" );
798 #endif /* else SYS_DARWIN */
799
800     return VLC_SUCCESS;
801 }
802
803 /****************************************************************************
804  ****************************************************************************
805  **
806  **     video part
807  **
808  **************************************************************************** 
809  ****************************************************************************/
810
811 #ifdef WIN32
812 static int  RunDecoderVideo( decoder_fifo_t *p_fifo ){ return VLC_EGENERIC; }
813
814 #else
815 static int  InitThreadVideo     ( vdec_thread_t * );
816 static void DecodeThreadVideo   ( vdec_thread_t * );
817 static void EndThreadVideo      ( vdec_thread_t * );
818
819 static int  RunDecoderVideo( decoder_fifo_t *p_fifo )
820 {
821     vdec_thread_t *p_dec;
822     vlc_bool_t    b_error;
823
824     p_dec = malloc( sizeof( vdec_thread_t ) );
825     if( !p_dec )
826     {
827         msg_Err( p_fifo, "out of memory" );
828         DecoderError( p_fifo );
829         return VLC_EGENERIC;
830     }
831     p_dec->p_fifo = p_fifo;
832
833     if( InitThreadVideo( p_dec ) != 0 )
834     {
835         DecoderError( p_fifo );
836         return VLC_EGENERIC;
837     }
838
839     while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
840     {
841         DecodeThreadVideo( p_dec );
842     }
843
844
845     if( ( b_error = p_dec->p_fifo->b_error ) )
846     {
847         DecoderError( p_dec->p_fifo );
848     }
849
850     EndThreadVideo( p_dec );
851     if( b_error )
852     {
853         return VLC_EGENERIC;
854     }
855
856     return VLC_SUCCESS;
857 }
858
859 /*
860  * InitThreadVideo: load and init library
861  *
862  */
863 static int InitThreadVideo( vdec_thread_t *p_dec )
864 {
865     vlc_value_t                         lockval;
866     long                                i_result;
867     ComponentDescription                desc;
868     Component                           prev;
869     ComponentResult                     cres;
870     ImageSubCodecDecompressCapabilities icap;   /* for ImageCodecInitialize() */
871     CodecInfo                           cinfo;  /* for ImageCodecGetCodecInfo() */
872     ImageDescription                    *id;
873
874     BITMAPINFOHEADER    *p_bih;
875     int                 i_vide;
876     uint8_t             *p_vide;
877     char                fcc[4];
878
879     if( !( p_bih  = (BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader ) )
880     {
881         msg_Err( p_dec->p_fifo, "missing BITMAPINFOHEADER !!" );
882         return VLC_EGENERIC;
883     }
884     i_vide = p_bih->biSize - sizeof( BITMAPINFOHEADER );
885     p_vide = (uint8_t*)&p_bih[1];
886     if( i_vide <= 0 || p_vide == NULL )
887     {
888         msg_Err( p_dec->p_fifo, "invalid BITMAPINFOHEADER !!" );
889         return VLC_EGENERIC;
890     }
891     memcpy( fcc, &p_dec->p_fifo->i_fourcc, 4 );
892     msg_Dbg( p_dec->p_fifo, "quicktime_video %4.4s %dx%d", fcc,
893              p_bih->biWidth, p_bih->biHeight );
894
895     /* get lock, avoid segfault */
896     var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
897     vlc_mutex_lock( lockval.p_address );
898 #ifdef SYS_DARWIN
899     EnterMovies();
900 #else
901 #ifdef LOADER
902     p_dec->ldt_fs = Setup_LDT_Keeper();
903 #endif /* LOADER */
904     msg_Dbg( p_dec->p_fifo, "trying to load `qtmlClient.dll'" );
905     if( !( p_dec->qtml = LoadLibraryA("qtmlClient.dll") ) )
906     {
907         msg_Err( p_dec->p_fifo, "cannot load qtmlClient.dll");
908         goto exit_error;
909     }
910
911     msg_Dbg( p_dec->p_fifo, "qtmlClient.dll loaded" );
912
913     /* (void*) to shut up gcc */
914     p_dec->InitializeQTML           = (void*)InitializeQTML;
915 #endif /* SYS_DARWIN */
916     p_dec->FindNextComponent        = (void*)FindNextComponent;
917     p_dec->OpenComponent            = (void*)OpenComponent;
918     p_dec->ImageCodecInitialize     = (void*)ImageCodecInitialize;
919     p_dec->ImageCodecGetCodecInfo   = (void*)ImageCodecGetCodecInfo;
920     p_dec->ImageCodecPreDecompress  = (void*)ImageCodecPreDecompress;
921     p_dec->ImageCodecBandDecompress = (void*)ImageCodecBandDecompress;
922     p_dec->GetGWorldPixMap          = (void*)GetGWorldPixMap;
923     p_dec->QTNewGWorldFromPtr       = (void*)QTNewGWorldFromPtr;
924     p_dec->NewHandleClear           = (void*)NewHandleClear;
925
926 #ifndef SYS_DARWIN
927     /* some sanity check */
928     if( !p_dec->InitializeQTML ||
929         !p_dec->FindNextComponent ||
930         !p_dec->OpenComponent ||
931         !p_dec->ImageCodecBandDecompress )
932     {
933         msg_Err( p_dec->p_fifo, "error getting qtmlClient.dll symbols");
934         goto exit_error;
935     }
936
937     if( ( i_result = p_dec->InitializeQTML( 6 + 16 ) ) )
938     {
939         msg_Dbg( p_dec->p_fifo, "error while InitializeQTML = %d", i_result );
940         goto exit_error;
941     }
942 #endif
943
944     /* init ComponentDescription */
945     memset( &desc, 0, sizeof( ComponentDescription ) );
946     desc.componentType      = FCC( 'i', 'm', 'd', 'c' );
947     desc.componentSubType   = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
948     desc.componentManufacturer = 0;
949     desc.componentFlags        = 0;
950     desc.componentFlagsMask    = 0;
951
952     if( !( prev = p_dec->FindNextComponent( NULL, &desc ) ) )
953     {
954         msg_Err( p_dec->p_fifo, "cannot find requested component" );
955         goto exit_error;
956     }
957     msg_Dbg( p_dec->p_fifo, "component id=0x%p", prev );
958
959     p_dec->ci =  p_dec->OpenComponent( prev );
960     msg_Dbg( p_dec->p_fifo, "component instance p=0x%p", p_dec->ci );
961
962     memset( &icap, 0, sizeof( ImageSubCodecDecompressCapabilities ) );
963     cres =  p_dec->ImageCodecInitialize( p_dec->ci, &icap );
964 /*    msg_Dbg( p_dec->p_fifo, "ImageCodecInitialize->%p  size=%d (%d)\n",cres,icap.recordSize,icap.decompressRecordSize); */
965
966
967     memset( &cinfo, 0, sizeof( CodecInfo ) );
968     cres =  p_dec->ImageCodecGetCodecInfo( p_dec->ci, &cinfo );
969     msg_Dbg( p_dec->p_fifo,
970              "Flags: compr: 0x%lx  decomp: 0x%lx format: 0x%lx\n",
971              cinfo.compressFlags, cinfo.decompressFlags, cinfo.formatFlags );
972     msg_Dbg( p_dec->p_fifo, "quicktime_video: Codec name: %.*s\n",
973              ((unsigned char*)&cinfo.typeName)[0],
974              ((unsigned char*)&cinfo.typeName)+1 );
975
976     /* make a yuy2 gworld */
977     p_dec->OutBufferRect.top    = 0;
978     p_dec->OutBufferRect.left   = 0;
979     p_dec->OutBufferRect.right  = p_bih->biWidth;
980     p_dec->OutBufferRect.bottom = p_bih->biHeight;
981
982
983     /* codec data FIXME use codec not SVQ3 */
984     msg_Dbg( p_dec->p_fifo, "vide = %d", i_vide );
985     id = malloc( sizeof( ImageDescription ) + ( i_vide - 70 ) );
986     id->idSize          = sizeof( ImageDescription ) + ( i_vide - 70 );
987     id->cType           = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
988     id->version         = GetWBE ( p_vide +  0 );
989     id->revisionLevel   = GetWBE ( p_vide +  2 );
990     id->vendor          = GetDWBE( p_vide +  4 );
991     id->temporalQuality = GetDWBE( p_vide +  8 );
992     id->spatialQuality  = GetDWBE( p_vide + 12 );
993     id->width           = GetWBE ( p_vide + 16 );
994     id->height          = GetWBE ( p_vide + 18 );
995     id->hRes            = GetDWBE( p_vide + 20 );
996     id->vRes            = GetDWBE( p_vide + 24 );
997     id->dataSize        = GetDWBE( p_vide + 28 );
998     id->frameCount      = GetWBE ( p_vide + 32 );
999     memcpy( &id->name, p_vide + 34, 32 );
1000     id->depth           = GetWBE ( p_vide + 66 );
1001     id->clutID          = GetWBE ( p_vide + 68 );
1002     if( i_vide > 70 )
1003     {
1004         memcpy( ((char*)&id->clutID) + 2, p_vide + 70, i_vide - 70 );
1005     }
1006
1007     msg_Dbg( p_dec->p_fifo, "idSize=%ld ver=%d rev=%d vendor=%ld tempQ=%d "
1008              "spaQ=%d w=%d h=%d dpi=%d%d dataSize=%d frameCount=%d clutID=%d",
1009              id->idSize, id->version, id->revisionLevel, id->vendor,
1010              (int)id->temporalQuality, (int)id->spatialQuality,
1011              id->width, id->height,
1012              (int)id->hRes, (int)id->vRes,
1013              (int)id->dataSize,
1014              id->frameCount,
1015              id->clutID );
1016
1017     p_dec->framedescHandle =
1018         (ImageDescriptionHandle) p_dec->NewHandleClear( id->idSize );
1019     memcpy( *p_dec->framedescHandle, id, id->idSize );
1020
1021     p_dec->plane = malloc( p_bih->biWidth * p_bih->biHeight * 3 );
1022
1023     i_result = p_dec->QTNewGWorldFromPtr( &p_dec->OutBufferGWorld,
1024                                           /*pixel format of new GWorld==YUY2 */
1025                                           kYUVSPixelFormat,
1026                                           /* we should benchmark if yvu9 is
1027                                            * faster for svq3, too */
1028                                           &p_dec->OutBufferRect,
1029                                           0, 0, 0,
1030                                           p_dec->plane,
1031                                           p_bih->biWidth * 2 );
1032
1033     msg_Dbg( p_dec->p_fifo, "NewGWorldFromPtr returned:%ld\n",
1034              65536 - ( i_result&0xffff ) );
1035
1036     memset( &p_dec->decpar, 0, sizeof( CodecDecompressParams ) );
1037     p_dec->decpar.imageDescription = p_dec->framedescHandle;
1038     p_dec->decpar.startLine        = 0;
1039     p_dec->decpar.stopLine         = ( **p_dec->framedescHandle ).height;
1040     p_dec->decpar.frameNumber      = 1;
1041     p_dec->decpar.matrixFlags      = 0;
1042     p_dec->decpar.matrixType       = 0;
1043     p_dec->decpar.matrix           = 0;
1044     p_dec->decpar.capabilities     = &p_dec->codeccap;
1045     p_dec->decpar.accuracy         = codecNormalQuality;
1046     p_dec->decpar.srcRect          = p_dec->OutBufferRect;
1047     p_dec->decpar.transferMode     = srcCopy;
1048     p_dec->decpar.dstPixMap        = **p_dec->GetGWorldPixMap( p_dec->OutBufferGWorld );/*destPixmap;  */
1049
1050     cres =  p_dec->ImageCodecPreDecompress( p_dec->ci, &p_dec->decpar );
1051     msg_Dbg( p_dec->p_fifo,
1052              "quicktime_video: ImageCodecPreDecompress cres=0x%X\n",
1053              (int)cres );
1054
1055     p_dec->p_vout = vout_Request( p_dec->p_fifo, NULL,
1056                                   p_bih->biWidth, p_bih->biHeight,
1057                                   VLC_FOURCC( 'Y', 'U', 'Y', '2' ),
1058                                   VOUT_ASPECT_FACTOR * p_bih->biWidth /
1059                                   p_bih->biHeight );
1060
1061     if( !p_dec->p_vout )
1062     {
1063         msg_Err( p_dec->p_fifo, "cannot get a vout" );
1064         goto exit_error;
1065     }
1066
1067     p_dec->i_buffer = 1000*1000;
1068     p_dec->p_buffer = malloc( p_dec->i_buffer );
1069
1070     vlc_mutex_unlock( lockval.p_address );
1071     return VLC_SUCCESS;
1072
1073 exit_error:
1074 #ifdef LOADER
1075     Restore_LDT_Keeper( p_dec->ldt_fs );
1076 #endif
1077     vlc_mutex_unlock( lockval.p_address );
1078     return VLC_EGENERIC;
1079
1080 }
1081
1082 static void DecodeThreadVideo( vdec_thread_t *p_dec )
1083 {
1084     BITMAPINFOHEADER *p_bih =
1085         (BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader;
1086
1087     pes_packet_t    *p_pes;
1088     vlc_value_t     lockval;
1089     picture_t       *p_pic;
1090     ComponentResult cres;
1091
1092     var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
1093
1094     input_ExtractPES( p_dec->p_fifo, &p_pes );
1095     if( !p_pes )
1096     {
1097         msg_Err( p_dec->p_fifo, "cannot get PES" );
1098         p_dec->p_fifo->b_error = 1;
1099         return;
1100     }
1101
1102     if( p_pes->i_pes_size > p_dec->i_buffer )
1103     {
1104         p_dec->i_buffer = 3 * p_pes->i_pes_size / 2;
1105         free( p_dec->p_buffer );
1106         p_dec->p_buffer = malloc( p_dec->i_buffer );
1107     }
1108
1109     if( p_pes->i_pes_size > 0 && p_pes->i_pts > mdate() )
1110     {
1111         GetPESData( p_dec->p_buffer, p_dec->i_buffer, p_pes );
1112
1113         while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
1114         {
1115             if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
1116             {
1117                 break;
1118             }
1119             msleep( VOUT_OUTMEM_SLEEP );
1120         }
1121
1122         p_dec->decpar.data                  = p_dec->p_buffer;
1123         p_dec->decpar.bufferSize            = p_pes->i_pes_size;
1124         (**p_dec->framedescHandle).dataSize = p_pes->i_pes_size;
1125
1126         vlc_mutex_lock( lockval.p_address );
1127         cres = p_dec->ImageCodecBandDecompress( p_dec->ci, &p_dec->decpar );
1128         vlc_mutex_unlock( lockval.p_address );
1129
1130         ++p_dec->decpar.frameNumber;
1131
1132         if( cres &0xFFFF )
1133         {
1134             msg_Dbg( p_dec->p_fifo, "quicktime_video: ImageCodecBandDecompress"
1135                      " cres=0x%X (-0x%X) %d :(\n",
1136                      (int)cres,(int)-cres, (int)cres );
1137         }
1138
1139         memcpy( p_pic->p[0].p_pixels,
1140                 p_dec->plane,
1141                 p_bih->biWidth * p_bih->biHeight * 2 );
1142
1143         vout_DatePicture( p_dec->p_vout, p_pic, p_pes->i_pts );
1144         vout_DisplayPicture( p_dec->p_vout, p_pic );
1145     }
1146
1147     input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
1148 }
1149
1150 static void EndThreadVideo( vdec_thread_t *p_dec )
1151 {
1152     msg_Dbg( p_dec->p_fifo, "QuickTime library video decoder closing" );
1153     free( p_dec->plane );
1154     vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
1155
1156 #ifndef SYS_DARWIN
1157     FreeLibrary( p_dec->qtml );
1158     msg_Dbg( p_dec->p_fifo, "FreeLibrary ok." );
1159 #endif
1160
1161 #ifdef LOADER
1162     Restore_LDT_Keeper( p_dec->ldt_fs );
1163     msg_Dbg( p_dec->p_fifo, "Restore_LDT_Keeper" );
1164 #endif
1165 #ifdef SYS_DARWIN
1166     ExitMovies();
1167 #endif
1168 }
1169 #endif