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