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.4 2003/05/23 00:00:48 hartman Exp $
7 * Authors: Laurent Aimar <fenrir at via.ecp.fr>
8 * Derk-Jan Hartman <thedj at users.sf.net>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <vlc/decoder.h>
32 #include <vlc/input.h>
34 #include <stdlib.h> /* malloc(), free() */
35 #include <string.h> /* strdup() */
39 #include <QuickTime/QuickTimeComponents.h>
40 #include <QuickTime/Movies.h>
41 #include <QuickTime/ImageCodec.h>
44 /* for windows do we require Quicktime compents header? */
47 #include "w32dll/loader/qtx/qtxsdk/components.h"
48 #include "w32dll/loader/wine/windef.h"
49 #include "w32dll/loader/ldt_keeper.h"
52 /*****************************************************************************
54 *****************************************************************************/
55 static int OpenDecoder ( vlc_object_t * );
57 static int RunDecoderAudio( decoder_fifo_t * );
58 static int RunDecoderVideo( decoder_fifo_t * );
60 /*****************************************************************************
62 *****************************************************************************/
65 set_description( _("QuickTime library decoder") );
66 set_capability( "decoder", 10 );
67 set_callbacks( OpenDecoder, NULL );
70 var_Create( p_module->p_libvlc, "qt_mutex", VLC_VAR_MUTEX );
75 #define FCC( a, b , c, d ) \
76 ((uint32_t)( ((a)<<24)|((b)<<16)|((c)<<8)|(d)))
79 typedef struct OpaqueSoundConverter* SoundConverter;
80 typedef unsigned long UnsignedFixed;
82 typedef struct SoundComponentData {
87 UnsignedFixed sampleRate;
92 #endif /* SYS_DARWIN */
96 /* Input properties */
97 decoder_fifo_t *p_fifo;
105 OSErr (*InitializeQTML) ( long flags );
106 #endif /* SYS_DARWIN */
107 int (*SoundConverterOpen) ( const SoundComponentData *, const SoundComponentData *, SoundConverter* );
108 int (*SoundConverterClose) ( SoundConverter );
109 int (*SoundConverterSetInfo) ( SoundConverter , OSType ,void * );
110 int (*SoundConverterGetBufferSizes) ( SoundConverter, unsigned long,
111 unsigned long*, unsigned long*, unsigned long* );
112 int (*SoundConverterBeginConversion)( SoundConverter );
113 int (*SoundConverterEndConversion) ( SoundConverter, void *, unsigned long *, unsigned long *);
114 int (*SoundConverterConvertBuffer) ( SoundConverter, const void *, unsigned long, void *,
115 unsigned long *,unsigned long * );
116 SoundConverter myConverter;
117 SoundComponentData InputFormatInfo, OutputFormatInfo;
120 unsigned int InFrameSize;
121 unsigned int OutFrameSize;
123 /* Output properties */
124 aout_instance_t * p_aout; /* opaque */
125 aout_input_t * p_aout_input; /* opaque */
126 audio_sample_format_t output_format;
132 unsigned int i_buffer;
133 unsigned int i_buffer_size;
136 uint8_t buffer_out[1000000]; /* FIXME */
141 /* Input properties */
142 decoder_fifo_t *p_fifo;
150 OSErr (*InitializeQTML) ( long flags );
151 #endif /* SYS_DARWIN */
152 Component (*FindNextComponent) ( Component prev, ComponentDescription* desc );
153 ComponentInstance (*OpenComponent) ( Component c );
154 ComponentResult (*ImageCodecInitialize) ( ComponentInstance ci, ImageSubCodecDecompressCapabilities * cap);
155 ComponentResult (*ImageCodecGetCodecInfo) ( ComponentInstance ci,
157 ComponentResult (*ImageCodecPreDecompress)( ComponentInstance ci,
158 CodecDecompressParams * params );
159 ComponentResult (*ImageCodecBandDecompress)( ComponentInstance ci,
160 CodecDecompressParams * params );
161 PixMapHandle (*GetGWorldPixMap) ( GWorldPtr offscreenGWorld );
162 OSErr (*QTNewGWorldFromPtr) ( GWorldPtr *gw,
164 const Rect *boundsRect,
166 /*GDHandle*/ void *aGDevice, /*unused*/
170 OSErr (*NewHandleClear) ( Size byteCount );
172 ComponentInstance ci;
173 Rect OutBufferRect; /* the dimensions of our GWorld */
174 GWorldPtr OutBufferGWorld; /* a GWorld is some kind of
175 description for a drawing
177 ImageDescriptionHandle framedescHandle;
179 CodecDecompressParams decpar; /* for ImageCodecPreDecompress() */
180 CodecCapabilities codeccap; /* for decpar */
183 /* Output properties */
184 vout_thread_t * p_vout;
189 unsigned int i_buffer;
194 static int pi_channels_maps[6] =
198 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
199 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
200 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
201 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
202 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
206 static uint16_t GetWBE( uint8_t *p_buff )
208 return( (p_buff[0]<<8) + p_buff[1] );
211 static uint32_t GetDWBE( uint8_t *p_buff )
213 return( (p_buff[0] << 24) + ( p_buff[1] <<16 ) +
214 ( p_buff[2] <<8 ) + p_buff[3] );
218 static int GetPESData( uint8_t *p_buf, int i_max, pes_packet_t *p_pes )
223 data_packet_t *p_data;
226 p_data = p_pes->p_first;
227 while( p_data != NULL && i_count < i_max )
230 i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
236 p_data->p_payload_start,
240 p_data = p_data->p_next;
245 if( i_count < i_max )
247 memset( p_buf, 0, i_max - i_count );
252 /*****************************************************************************
253 * OpenDecoder: probe the decoder and return score
254 *****************************************************************************
255 * Tries to launch a decoder and return score so that the interface is able
257 *****************************************************************************/
258 static int OpenDecoder( vlc_object_t *p_this )
260 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
262 switch( p_fifo->i_fourcc )
264 case VLC_FOURCC('S','V','Q','3'): /* Sorenson v3 */
265 case VLC_FOURCC('S','V','Q','1'): /* Sorenson v1 */
266 case VLC_FOURCC('Z','y','G','o'):
267 case VLC_FOURCC('V','P','3','1'):
268 case VLC_FOURCC('3','I','V','1'):
269 case VLC_FOURCC('r','l','e',' '): /* QuickTime animation (RLE) */
270 case VLC_FOURCC('r','p','z','a'): /* QuickTime Apple Video */
271 case VLC_FOURCC('a','z','p','r'): /* QuickTime animation (RLE) */
272 p_fifo->pf_run = RunDecoderVideo;
275 case VLC_FOURCC('Q','D','M','C'): /* QDesign */
276 case VLC_FOURCC('Q','D','M','2'): /* QDesign* 2 */
277 case VLC_FOURCC('Q','c','l','p'): /* Qualcomm Purevoice Codec */
278 case VLC_FOURCC('Q','C','L','P'): /* Qualcomm Purevoice Codec */
279 case VLC_FOURCC('M','A','C','3'): /* MACE3 audio decoder */
280 case VLC_FOURCC('M','A','C','6'): /* MACE6 audio decoder */
281 case VLC_FOURCC('d','v','c','a'): /* DV Audio */
282 case VLC_FOURCC('s','o','w','t'): /* 16-bit Little Endian */
283 case VLC_FOURCC('t','w','o','s'): /* 16-bit Big Endian */
284 case VLC_FOURCC('a','l','a','w'): /* ALaw 2:1 */
285 case VLC_FOURCC('u','l','a','w'): /* mu-Law 2:1 */
286 case VLC_FOURCC('r','a','w',' '): /* 8-bit offset binaries */
287 case VLC_FOURCC('f','l','3','2'): /* 32-bit Floating Point */
288 case VLC_FOURCC('f','l','6','4'): /* 64-bit Floating Point */
289 case VLC_FOURCC('i','n','2','4'): /* 24-bit Interger */
290 case VLC_FOURCC('i','n','3','2'): /* 32-bit Integer */
291 case 0x31: /* MS GSM */
292 case 0x32: /* MSN Audio */
293 case 0x0011: /* DVI IMA */
294 case 0x6D730002: /* Microsoft ADPCM-ACM */
295 case 0x6D730011: /* DVI Intel IMAADPCM-ACM */
297 p_fifo->pf_run = RunDecoderAudio;
305 HMODULE WINAPI LoadLibraryA(LPCSTR);
306 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
307 int WINAPI FreeLibrary(HMODULE);
310 /****************************************************************************
311 ****************************************************************************
315 ****************************************************************************
316 ****************************************************************************/
318 static int InitThreadAudio ( adec_thread_t * );
319 static void DecodeThreadAudio ( adec_thread_t * );
320 static void EndThreadAudio ( adec_thread_t * );
322 static int RunDecoderAudio( decoder_fifo_t *p_fifo )
324 adec_thread_t *p_dec;
327 p_dec = malloc( sizeof( adec_thread_t ) );
330 msg_Err( p_fifo, "out of memory" );
331 DecoderError( p_fifo );
334 p_dec->p_fifo = p_fifo;
336 if( InitThreadAudio( p_dec ) != 0 )
338 DecoderError( p_fifo );
342 while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
344 DecodeThreadAudio( p_dec );
348 if( ( b_error = p_dec->p_fifo->b_error ) )
350 DecoderError( p_dec->p_fifo );
353 EndThreadAudio( p_dec );
362 static int InitThreadAudio( adec_thread_t *p_dec )
367 unsigned long WantedBufferSize;
368 unsigned long InputBufferSize = 0;
369 unsigned long OutputBufferSize = 0;
373 if( !( p_wf = (WAVEFORMATEX*)p_dec->p_fifo->p_waveformatex ) )
375 msg_Err( p_dec->p_fifo, "missing WAVEFORMATEX");
378 memcpy( fcc, &p_dec->p_fifo->i_fourcc, 4 );
380 /* get lock, avoid segfault */
381 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
382 vlc_mutex_lock( lockval.p_address );
387 p_dec->ldt_fs = Setup_LDT_Keeper();
389 msg_Dbg( p_dec->p_fifo, "trying to load `qtmlClient.dll'" );
390 if( !( p_dec->qtml = LoadLibraryA("qtmlClient.dll") ) )
392 msg_Err( p_dec->p_fifo, "cannot load qtmlClient.dll");
396 msg_Dbg( p_dec->p_fifo, "qtmlClient.dll loaded" );
398 /* (void*) to shut up gcc */
399 p_dec->InitializeQTML = (void*)InitializeQTML;
400 #endif /* SYS_DARWIN */
401 p_dec->SoundConverterOpen = (void*)SoundConverterOpen;
402 p_dec->SoundConverterClose = (void*)SoundConverterClose;
403 p_dec->SoundConverterSetInfo = (void*)SoundConverterSetInfo;
404 p_dec->SoundConverterGetBufferSizes = (void*)SoundConverterGetBufferSizes;
405 p_dec->SoundConverterConvertBuffer = (void*)SoundConverterConvertBuffer;
406 p_dec->SoundConverterBeginConversion= (void*)SoundConverterBeginConversion;
407 p_dec->SoundConverterEndConversion = (void*)SoundConverterEndConversion;
410 if( !p_dec->InitializeQTML ||
411 !p_dec->SoundConverterOpen || !p_dec->SoundConverterClose ||
412 !p_dec->SoundConverterSetInfo || !p_dec->SoundConverterGetBufferSizes ||
413 !p_dec->SoundConverterConvertBuffer ||
414 !p_dec->SoundConverterBeginConversion || !p_dec->SoundConverterEndConversion )
416 msg_Err( p_dec->p_fifo, "error getting qtmlClient.dll symbols");
420 if( ( i_error = p_dec->InitializeQTML( 6 + 16 ) ) )
422 msg_Dbg( p_dec->p_fifo, "error while InitializeQTML = %d", i_error );
427 /* input format settings */
428 p_dec->InputFormatInfo.flags = 0;
429 p_dec->InputFormatInfo.sampleCount = 0;
430 p_dec->InputFormatInfo.buffer = NULL;
431 p_dec->InputFormatInfo.reserved = 0;
432 p_dec->InputFormatInfo.numChannels = p_wf->nChannels;
433 p_dec->InputFormatInfo.sampleSize = p_wf->wBitsPerSample;
434 p_dec->InputFormatInfo.sampleRate = p_wf->nSamplesPerSec;
435 p_dec->InputFormatInfo.format = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
437 /* output format settings */
438 p_dec->OutputFormatInfo.flags = 0;
439 p_dec->OutputFormatInfo.sampleCount = 0;
440 p_dec->OutputFormatInfo.buffer = NULL;
441 p_dec->OutputFormatInfo.reserved = 0;
442 p_dec->OutputFormatInfo.numChannels = p_wf->nChannels;
443 p_dec->OutputFormatInfo.sampleSize = 16;
444 p_dec->OutputFormatInfo.sampleRate = p_wf->nSamplesPerSec;
445 p_dec->OutputFormatInfo.format = FCC( 'N', 'O', 'N', 'E' );
448 /* on OS X QT is not threadsafe */
449 vlc_mutex_lock( &p_dec->p_fifo->p_vlc->quicktime_lock );
452 i_error = p_dec->SoundConverterOpen( &p_dec->InputFormatInfo,
453 &p_dec->OutputFormatInfo,
454 &p_dec->myConverter );
457 msg_Dbg( p_dec->p_fifo, "error while SoundConverterOpen = %d", i_error );
461 if( p_wf->cbSize > 36 + 8 )
463 i_error = p_dec->SoundConverterSetInfo( p_dec->myConverter,
464 FCC( 'w', 'a', 'v', 'e' ),
465 ((uint8_t*)&p_wf[1]) + 36 + 8 );
466 msg_Dbg( p_dec->p_fifo, "error while SoundConverterSetInfo = %d", i_error );
469 WantedBufferSize = p_dec->OutputFormatInfo.numChannels * p_dec->OutputFormatInfo.sampleRate * 2;
470 p_dec->FramesToGet = 0;
471 i_error = p_dec->SoundConverterGetBufferSizes( p_dec->myConverter,
472 WantedBufferSize, &p_dec->FramesToGet,
473 &InputBufferSize, &OutputBufferSize );
475 msg_Dbg( p_dec->p_fifo, "WantedBufferSize=%li InputBufferSize=%li OutputBufferSize=%li FramesToGet=%li",
476 WantedBufferSize, InputBufferSize, OutputBufferSize, p_dec->FramesToGet );
478 p_dec->InFrameSize = (InputBufferSize + p_dec->FramesToGet - 1 ) / p_dec->FramesToGet;
479 p_dec->OutFrameSize = OutputBufferSize / p_dec->FramesToGet;
481 msg_Dbg( p_dec->p_fifo, "frame size %d -> %d", p_dec->InFrameSize, p_dec->OutFrameSize );
483 i_error = p_dec->SoundConverterBeginConversion( p_dec->myConverter );
486 msg_Err( p_dec->p_fifo, "error while SoundConverterBeginConversion = %d", i_error );
491 vlc_mutex_unlock( &p_dec->p_fifo->p_vlc->quicktime_lock );
494 p_dec->output_format.i_format = AOUT_FMT_S16_NE;
495 p_dec->output_format.i_rate = p_wf->nSamplesPerSec;
496 p_dec->output_format.i_physical_channels =
497 p_dec->output_format.i_original_channels =
498 pi_channels_maps[p_wf->nChannels];
499 aout_DateInit( &p_dec->date, p_dec->output_format.i_rate );
500 p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
502 &p_dec->output_format );
503 if( !p_dec->p_aout_input )
505 msg_Err( p_dec->p_fifo, "cannot create aout" );
510 p_dec->i_buffer_size = 100*1000;
511 p_dec->p_buffer = malloc( p_dec->i_buffer_size );
515 vlc_mutex_unlock( lockval.p_address );
520 Restore_LDT_Keeper( p_dec->ldt_fs );
522 vlc_mutex_unlock( lockval.p_address );
526 static void DecodeThreadAudio ( adec_thread_t *p_dec )
532 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
534 input_ExtractPES( p_dec->p_fifo, &p_pes );
537 msg_Err( p_dec->p_fifo, "cannot get PES" );
538 p_dec->p_fifo->b_error = 1;
541 /*if( p_dec->pts <= 0 )*/
543 p_dec->pts = p_pes->i_pts;
546 if( p_pes->i_pes_size > 0 && p_pes->i_pts > mdate() )
549 if( p_dec->i_buffer_size < p_dec->i_buffer + p_pes->i_pes_size )
551 p_dec->i_buffer_size = p_dec->i_buffer + p_pes->i_pes_size + 1024;
552 p_dec->p_buffer = realloc( p_dec->p_buffer,
553 p_dec->i_buffer_size );
556 GetPESData( &p_dec->p_buffer[p_dec->i_buffer],
557 p_dec->i_buffer_size - p_dec->i_buffer, p_pes );
558 p_dec->i_buffer += p_pes->i_pes_size;
560 if( p_dec->i_buffer > p_dec->InFrameSize )
562 int i_frames = p_dec->i_buffer / p_dec->InFrameSize;
563 long i_out_frames, i_out_bytes;
566 vlc_mutex_lock( lockval.p_address );
567 i_error = p_dec->SoundConverterConvertBuffer( p_dec->myConverter,
571 &i_out_frames, &i_out_bytes );
572 vlc_mutex_unlock( lockval.p_address );
574 /*msg_Dbg( p_dec->p_fifo, "decoded %d frames -> %ld frames (error=%d)",
575 i_frames, i_out_frames, i_error );
577 msg_Dbg( p_dec->p_fifo, "decoded %ld frames = %ld bytes", i_out_frames, i_out_bytes );*/
578 p_dec->i_buffer -= i_frames * p_dec->InFrameSize;
579 if( p_dec->i_buffer > 0 )
581 memmove( &p_dec->p_buffer[0],
582 &p_dec->p_buffer[i_frames * p_dec->InFrameSize],
586 if( i_out_frames > 0 )
588 aout_buffer_t *p_aout_buffer;
589 uint8_t *p_buff = p_dec->buffer_out;
591 /*msg_Dbg( p_dec->p_fifo, "pts=%lld date=%lld dateget=%lld",
592 p_dec->pts, mdate(), aout_DateGet( &p_dec->date ) );*/
594 if( p_dec->pts != 0 && p_dec->pts != aout_DateGet( &p_dec->date ) )
596 aout_DateSet( &p_dec->date, p_dec->pts );
598 else if( !aout_DateGet( &p_dec->date ) )
600 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
604 while( i_out_frames > 0 )
608 i_frames = __MIN( i_out_frames, 1000 );
609 p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout,
614 msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
615 p_dec->p_fifo->b_error = 1;
618 p_aout_buffer->start_date = aout_DateGet( &p_dec->date );
619 p_aout_buffer->end_date = aout_DateIncrement( &p_dec->date,
622 memcpy( p_aout_buffer->p_buffer,
624 p_aout_buffer->i_nb_bytes );
626 /*msg_Dbg( p_dec->p_fifo, "==> start=%lld end=%lld date=%lld",
627 p_aout_buffer->start_date, p_aout_buffer->end_date, mdate() );*/
628 aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_aout_buffer );
629 /*msg_Dbg( p_dec->p_fifo, "s1=%d s2=%d", i_framesperchannels, p_aout_buffer->i_nb_samples );
631 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 );*/
632 p_buff += i_frames * 4;
633 i_out_frames -= i_frames;
641 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
644 static void EndThreadAudio( adec_thread_t *p_dec )
648 unsigned long ConvertedFrames=0;
649 unsigned long ConvertedBytes=0;
651 /* get lock, avoid segfault */
652 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
653 vlc_mutex_lock( lockval.p_address );
655 i_error = p_dec->SoundConverterEndConversion( p_dec->myConverter, NULL, &ConvertedFrames, &ConvertedBytes );
656 msg_Dbg( p_dec->p_fifo, "SoundConverterEndConversion => %d", i_error );
658 i_error = p_dec->SoundConverterClose( p_dec->myConverter );
659 msg_Dbg( p_dec->p_fifo, "SoundConverterClose => %d", i_error );
662 FreeLibrary( p_dec->qtml );
663 msg_Dbg( p_dec->p_fifo, "FreeLibrary ok." ); */
665 vlc_mutex_unlock( lockval.p_address );
668 Restore_LDT_Keeper( p_dec->ldt_fs );
669 msg_Dbg( p_dec->p_fifo, "Restore_LDT_Keeper" ); */
674 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
678 /****************************************************************************
679 ****************************************************************************
683 ****************************************************************************
684 ****************************************************************************/
686 static int InitThreadVideo ( vdec_thread_t * );
687 static void DecodeThreadVideo ( vdec_thread_t * );
688 static void EndThreadVideo ( vdec_thread_t * );
690 static int RunDecoderVideo( decoder_fifo_t *p_fifo )
692 vdec_thread_t *p_dec;
695 p_dec = malloc( sizeof( vdec_thread_t ) );
698 msg_Err( p_fifo, "out of memory" );
699 DecoderError( p_fifo );
702 p_dec->p_fifo = p_fifo;
704 if( InitThreadVideo( p_dec ) != 0 )
706 DecoderError( p_fifo );
710 while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
712 DecodeThreadVideo( p_dec );
716 if( ( b_error = p_dec->p_fifo->b_error ) )
718 DecoderError( p_dec->p_fifo );
721 EndThreadVideo( p_dec );
731 * InitThreadVideo: load and init library
734 static int InitThreadVideo( vdec_thread_t *p_dec )
738 ComponentDescription desc;
740 ComponentResult cres;
741 ImageSubCodecDecompressCapabilities icap; /* for ImageCodecInitialize() */
742 CodecInfo cinfo; /* for ImageCodecGetCodecInfo() */
743 ImageDescription *id;
745 BITMAPINFOHEADER *p_bih;
750 if( !( p_bih = (BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader ) )
752 msg_Err( p_dec->p_fifo, "missing BITMAPINFOHEADER !!" );
755 i_vide = p_bih->biSize - sizeof( BITMAPINFOHEADER );
756 p_vide = (uint8_t*)&p_bih[1];
757 if( i_vide <= 0 || p_vide == NULL )
759 msg_Err( p_dec->p_fifo, "invalid BITMAPINFOHEADER !!" );
762 memcpy( fcc, &p_dec->p_fifo->i_fourcc, 4 );
763 msg_Dbg( p_dec->p_fifo, "quicktime_video %4.4s %dx%d", fcc, p_bih->biWidth, p_bih->biHeight );
765 /* get lock, avoid segfault */
766 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
767 vlc_mutex_lock( lockval.p_address );
772 p_dec->ldt_fs = Setup_LDT_Keeper();
774 msg_Dbg( p_dec->p_fifo, "trying to load `qtmlClient.dll'" );
775 if( !( p_dec->qtml = LoadLibraryA("qtmlClient.dll") ) )
777 msg_Err( p_dec->p_fifo, "cannot load qtmlClient.dll");
781 msg_Dbg( p_dec->p_fifo, "qtmlClient.dll loaded" );
783 /* (void*) to shut up gcc */
784 p_dec->InitializeQTML = (void*)InitializeQTML;
785 #endif /* SYS_DARWIN */
786 p_dec->FindNextComponent = (void*)FindNextComponent;
787 p_dec->OpenComponent = (void*)OpenComponent;
788 p_dec->ImageCodecInitialize = (void*)ImageCodecInitialize;
789 p_dec->ImageCodecGetCodecInfo = (void*)ImageCodecGetCodecInfo;
790 p_dec->ImageCodecPreDecompress = (void*)ImageCodecPreDecompress;
791 p_dec->ImageCodecBandDecompress = (void*)ImageCodecBandDecompress;
792 p_dec->GetGWorldPixMap = (void*)GetGWorldPixMap;
793 p_dec->QTNewGWorldFromPtr = (void*)QTNewGWorldFromPtr;
794 p_dec->NewHandleClear = (void*)NewHandleClear;
797 /* some sanity check */
798 if( !p_dec->InitializeQTML ||
799 !p_dec->FindNextComponent ||
800 !p_dec->OpenComponent ||
801 !p_dec->ImageCodecBandDecompress )
803 msg_Err( p_dec->p_fifo, "error getting qtmlClient.dll symbols");
807 if( ( i_result = p_dec->InitializeQTML( 6 + 16 ) ) )
809 msg_Dbg( p_dec->p_fifo, "error while InitializeQTML = %d", i_result );
814 /* init ComponentDescription */
815 memset( &desc, 0, sizeof( ComponentDescription ) );
816 desc.componentType = FCC( 'i', 'm', 'd', 'c' );
817 desc.componentSubType = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
818 desc.componentManufacturer = 0;
819 desc.componentFlags = 0;
820 desc.componentFlagsMask = 0;
822 if( !( prev = p_dec->FindNextComponent( NULL, &desc ) ) )
824 msg_Err( p_dec->p_fifo, "cannot find requested component" );
827 msg_Dbg( p_dec->p_fifo, "component id=0x%p", prev );
829 p_dec->ci = p_dec->OpenComponent( prev );
830 msg_Dbg( p_dec->p_fifo, "component instance p=0x%p", p_dec->ci );
832 memset( &icap, 0, sizeof( ImageSubCodecDecompressCapabilities ) );
833 cres = p_dec->ImageCodecInitialize( p_dec->ci, &icap );
834 /* msg_Dbg( p_dec->p_fifo, "ImageCodecInitialize->%p size=%d (%d)\n",cres,icap.recordSize,icap.decompressRecordSize); */
837 memset( &cinfo, 0, sizeof( CodecInfo ) );
838 cres = p_dec->ImageCodecGetCodecInfo( p_dec->ci,&cinfo);
839 msg_Dbg( p_dec->p_fifo, "Flags: compr: 0x%lx decomp: 0x%lx format: 0x%lx\n",
840 cinfo.compressFlags, cinfo.decompressFlags, cinfo.formatFlags);
841 msg_Dbg( p_dec->p_fifo, "quicktime_video: Codec name: %.*s\n", ((unsigned char*)&cinfo.typeName)[0],
842 ((unsigned char*)&cinfo.typeName)+1);
844 /* make a yuy2 gworld */
845 p_dec->OutBufferRect.top = 0;
846 p_dec->OutBufferRect.left = 0;
847 p_dec->OutBufferRect.right = p_bih->biWidth;
848 p_dec->OutBufferRect.bottom = p_bih->biHeight;
851 /* codec data FIXME use codec not SVQ3 */
852 msg_Dbg( p_dec->p_fifo, "vide = %d", i_vide );
853 id = malloc( sizeof( ImageDescription ) + ( i_vide - 70 ) );
854 id->idSize = sizeof( ImageDescription ) + ( i_vide - 70 );
855 id->cType = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
856 id->version = GetWBE ( p_vide + 0 );
857 id->revisionLevel = GetWBE ( p_vide + 2 );
858 id->vendor = GetDWBE( p_vide + 4 );
859 id->temporalQuality = GetDWBE( p_vide + 8 );
860 id->spatialQuality = GetDWBE( p_vide + 12 );
861 id->width = GetWBE ( p_vide + 16 );
862 id->height = GetWBE ( p_vide + 18 );
863 id->hRes = GetDWBE( p_vide + 20 );
864 id->vRes = GetDWBE( p_vide + 24 );
865 id->dataSize = GetDWBE( p_vide + 28 );
866 id->frameCount = GetWBE ( p_vide + 32 );
867 memcpy( &id->name, p_vide + 34, 32 );
868 id->depth = GetWBE ( p_vide + 66 );
869 id->clutID = GetWBE ( p_vide + 68 );
872 memcpy( ((char*)&id->clutID) + 2, p_vide + 70, i_vide - 70 );
875 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",
876 id->idSize, id->version, id->revisionLevel, id->vendor,
877 (int)id->temporalQuality, (int)id->spatialQuality,
878 id->width, id->height,
879 (int)id->hRes, (int)id->vRes,
884 p_dec->framedescHandle = (ImageDescriptionHandle) p_dec->NewHandleClear (id->idSize);
885 memcpy (*p_dec->framedescHandle, id, id->idSize);
887 p_dec->plane = malloc( p_bih->biWidth * p_bih->biHeight * 3 );
889 i_result = p_dec->QTNewGWorldFromPtr(&p_dec->OutBufferGWorld,
890 kYUVSPixelFormat, /*pixel format of new GWorld==YUY2 */
891 &p_dec->OutBufferRect, /*we should benchmark if yvu9 is faster for svq3, too */
896 p_bih->biWidth * 2 );
898 msg_Dbg( p_dec->p_fifo, "NewGWorldFromPtr returned:%ld\n",65536-(i_result&0xffff));
900 memset( &p_dec->decpar, 0, sizeof( CodecDecompressParams ) );
901 p_dec->decpar.imageDescription = p_dec->framedescHandle;
902 p_dec->decpar.startLine = 0;
903 p_dec->decpar.stopLine = (**p_dec->framedescHandle).height;
904 p_dec->decpar.frameNumber = 1;
905 p_dec->decpar.matrixFlags = 0;
906 p_dec->decpar.matrixType = 0;
907 p_dec->decpar.matrix = 0;
908 p_dec->decpar.capabilities = &p_dec->codeccap;
909 p_dec->decpar.accuracy = codecNormalQuality;
910 p_dec->decpar.srcRect = p_dec->OutBufferRect;
911 p_dec->decpar.transferMode = srcCopy;
912 p_dec->decpar.dstPixMap = ** p_dec->GetGWorldPixMap (p_dec->OutBufferGWorld);/*destPixmap; */
914 msg_Dbg( p_dec->p_fifo, "will call: ImageCodecPreDecompress");
915 cres = p_dec->ImageCodecPreDecompress (p_dec->ci, &p_dec->decpar);
916 msg_Dbg( p_dec->p_fifo, "quicktime_video: ImageCodecPreDecompress cres=0x%X\n", (int)cres);
918 p_dec->p_vout = vout_Request( p_dec->p_fifo, NULL,
919 p_bih->biWidth, p_bih->biHeight,
920 VLC_FOURCC( 'Y', 'U', 'Y', '2' ),
921 VOUT_ASPECT_FACTOR * p_bih->biWidth / p_bih->biHeight );
925 msg_Err( p_dec->p_fifo, "cannot get a vout" );
929 p_dec->i_buffer = 1000*1000;
930 p_dec->p_buffer = malloc( p_dec->i_buffer );
932 vlc_mutex_unlock( lockval.p_address );
937 Restore_LDT_Keeper( p_dec->ldt_fs );
939 vlc_mutex_unlock( lockval.p_address );
944 static void DecodeThreadVideo ( vdec_thread_t *p_dec )
946 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader;
950 ComponentResult cres;
952 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
954 input_ExtractPES( p_dec->p_fifo, &p_pes );
957 msg_Err( p_dec->p_fifo, "cannot get PES" );
958 p_dec->p_fifo->b_error = 1;
962 if( p_pes->i_pes_size > p_dec->i_buffer )
964 p_dec->i_buffer = 3 * p_pes->i_pes_size / 2;
965 free( p_dec->p_buffer );
966 p_dec->p_buffer = malloc( p_dec->i_buffer );
969 if( p_pes->i_pes_size > 0 && p_pes->i_pts > mdate() )
971 GetPESData( p_dec->p_buffer, p_dec->i_buffer, p_pes );
973 while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
975 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
979 msleep( VOUT_OUTMEM_SLEEP );
982 p_dec->decpar.data = p_dec->p_buffer;
983 p_dec->decpar.bufferSize = p_pes->i_pes_size;
984 (**p_dec->framedescHandle).dataSize = p_pes->i_pes_size;
986 vlc_mutex_lock( lockval.p_address );
987 cres = p_dec->ImageCodecBandDecompress( p_dec->ci, &p_dec->decpar );
988 vlc_mutex_unlock( lockval.p_address );
990 ++p_dec->decpar.frameNumber;
994 msg_Dbg( p_dec->p_fifo,
995 "quicktime_video: ImageCodecBandDecompress cres=0x%X (-0x%X) %d :(\n",
996 (int)cres,(int)-cres, (int)cres );
999 memcpy( p_pic->p[0].p_pixels,
1001 p_bih->biWidth * p_bih->biHeight * 2 );
1003 vout_DatePicture( p_dec->p_vout, p_pic, p_pes->i_pts );
1004 vout_DisplayPicture( p_dec->p_vout, p_pic );
1007 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
1010 static void EndThreadVideo ( vdec_thread_t *p_dec )
1012 msg_Dbg( p_dec->p_fifo, "QuickTime library video decoder closing" );
1013 free( p_dec->plane );
1014 vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
1017 FreeLibrary( p_dec->qtml );
1018 msg_Dbg( p_dec->p_fifo, "FreeLibrary ok." );
1022 Restore_LDT_Keeper( p_dec->ldt_fs );
1023 msg_Dbg( p_dec->p_fifo, "Restore_LDT_Keeper" );