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.15 2003/11/22 23:39:14 fenrir 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 *****************************************************************************/
32 #include <vlc/decoder.h>
37 #include <QuickTime/QuickTimeComponents.h>
38 #include <QuickTime/Movies.h>
39 #include <QuickTime/ImageCodec.h>
42 /* for windows do we require Quicktime compents header? */
45 #include "w32dll/loader/qtx/qtxsdk/components.h"
46 #include "w32dll/loader/wine/windef.h"
47 #include "w32dll/loader/ldt_keeper.h"
49 HMODULE WINAPI LoadLibraryA(LPCSTR);
50 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
51 int WINAPI FreeLibrary(HMODULE);
55 /*****************************************************************************
57 *****************************************************************************/
58 static int OpenDecoder ( vlc_object_t * );
60 static int RunDecoderAudio( decoder_fifo_t * );
61 static int RunDecoderVideo( decoder_fifo_t * );
63 /*****************************************************************************
65 *****************************************************************************/
68 set_description( _("QuickTime library decoder") );
69 set_capability( "decoder", 10 );
70 set_callbacks( OpenDecoder, NULL );
73 var_Create( p_module->p_libvlc, "qt_mutex", VLC_VAR_MUTEX );
76 #define FCC( a, b , c, d ) \
77 ((uint32_t)( ((a)<<24)|((b)<<16)|((c)<<8)|(d)))
80 typedef struct OpaqueSoundConverter* SoundConverter;
83 typedef unsigned long UnsignedFixed;
86 typedef struct SoundComponentData {
91 UnsignedFixed sampleRate;
97 #endif /* SYS_DARWIN */
101 /* Input properties */
102 decoder_fifo_t *p_fifo;
111 OSErr (*InitializeQTML) ( long flags );
112 OSErr (*TerminateQTML) ( void );
113 #endif /* SYS_DARWIN */
115 int (*SoundConverterOpen) ( const SoundComponentData *,
116 const SoundComponentData *,
118 int (*SoundConverterClose) ( SoundConverter );
119 int (*SoundConverterSetInfo) ( SoundConverter , OSType, void * );
120 int (*SoundConverterGetBufferSizes) ( SoundConverter, unsigned long,
121 unsigned long*, unsigned long*,
123 int (*SoundConverterBeginConversion)( SoundConverter );
124 int (*SoundConverterEndConversion) ( SoundConverter, void *,
125 unsigned long *, unsigned long *);
126 int (*SoundConverterConvertBuffer) ( SoundConverter, const void *,
127 unsigned long, void *,
128 unsigned long *, unsigned long * );
129 SoundConverter myConverter;
130 SoundComponentData InputFormatInfo, OutputFormatInfo;
133 unsigned int InFrameSize;
134 unsigned int OutFrameSize;
136 /* Output properties */
137 aout_instance_t * p_aout; /* opaque */
138 aout_input_t * p_aout_input; /* opaque */
139 audio_sample_format_t output_format;
145 unsigned int i_buffer;
146 unsigned int i_buffer_size;
149 uint8_t buffer_out[1000000]; /* FIXME */
156 /* Input properties */
157 decoder_fifo_t *p_fifo;
165 OSErr (*InitializeQTML) ( long flags );
167 #endif /* SYS_DARWIN */
168 Component (*FindNextComponent)
169 ( Component prev, ComponentDescription* desc );
171 ComponentInstance (*OpenComponent)
174 ComponentResult (*ImageCodecInitialize)
175 ( ComponentInstance ci, ImageSubCodecDecompressCapabilities * cap);
177 ComponentResult (*ImageCodecGetCodecInfo)
178 ( ComponentInstance ci, CodecInfo *info );
180 ComponentResult (*ImageCodecPreDecompress)
181 ( ComponentInstance ci, CodecDecompressParams * params );
183 ComponentResult (*ImageCodecBandDecompress)
184 ( ComponentInstance ci, CodecDecompressParams * params );
186 PixMapHandle (*GetGWorldPixMap)
187 ( GWorldPtr offscreenGWorld );
189 OSErr (*QTNewGWorldFromPtr)
190 ( GWorldPtr *gw, OSType pixelFormat, const Rect *boundsRect,
191 CTabHandle cTable, /*GDHandle*/ void *aGDevice, /*unused*/
192 GWorldFlags flags, void *baseAddr, long rowBytes );
194 OSErr (*NewHandleClear)
197 ComponentInstance ci;
198 Rect OutBufferRect; /* the dimensions of our GWorld */
199 GWorldPtr OutBufferGWorld; /* a GWorld is some kind of
200 description for a drawing
202 ImageDescriptionHandle framedescHandle;
204 CodecDecompressParams decpar; /* for ImageCodecPreDecompress()*/
205 CodecCapabilities codeccap; /* for decpar */
208 /* Output properties */
209 vout_thread_t * p_vout;
214 unsigned int i_buffer;
220 static int pi_channels_maps[6] =
224 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
225 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
226 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
227 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
228 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
232 static int GetPESData( uint8_t *p_buf, int i_max, pes_packet_t *p_pes )
237 data_packet_t *p_data;
240 p_data = p_pes->p_first;
241 while( p_data != NULL && i_count < i_max )
244 i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
250 p_data->p_payload_start,
254 p_data = p_data->p_next;
259 if( i_count < i_max )
261 memset( p_buf, 0, i_max - i_count );
266 /*****************************************************************************
267 * OpenDecoder: probe the decoder and return score
268 *****************************************************************************
269 * Tries to launch a decoder and return score so that the interface is able
271 *****************************************************************************/
272 static int OpenDecoder( vlc_object_t *p_this )
274 decoder_t *p_dec = (decoder_t*)p_this;
276 switch( p_dec->p_fifo->i_fourcc )
278 case VLC_FOURCC('S','V','Q','3'): /* Sorenson v3 */
279 /* case VLC_FOURCC('S','V','Q','1'): Sorenson v1
280 case VLC_FOURCC('Z','y','G','o'):
281 case VLC_FOURCC('V','P','3','1'):
282 case VLC_FOURCC('3','I','V','1'): */
283 case VLC_FOURCC('r','l','e',' '): /* QuickTime animation (RLE) */
284 case VLC_FOURCC('r','p','z','a'): /* QuickTime Apple Video */
285 case VLC_FOURCC('a','z','p','r'): /* QuickTime animation (RLE) */
286 p_dec->pf_run = RunDecoderVideo;
289 case VLC_FOURCC('s','a','m','r'): /* 3GPP AMR audio */
290 case VLC_FOURCC('m','p','4','a'): /* MPEG-4 audio */
291 case VLC_FOURCC('Q','D','M','C'): /* QDesign */
292 case VLC_FOURCC('Q','D','M','2'): /* QDesign* 2 */
293 case VLC_FOURCC('Q','c','l','p'): /* Qualcomm Purevoice Codec */
294 case VLC_FOURCC('Q','C','L','P'): /* Qualcomm Purevoice Codec */
295 case VLC_FOURCC('M','A','C','3'): /* MACE3 audio decoder */
296 case VLC_FOURCC('M','A','C','6'): /* MACE6 audio decoder */
297 case VLC_FOURCC('d','v','c','a'): /* DV Audio */
298 case VLC_FOURCC('s','o','w','t'): /* 16-bit Little Endian */
299 case VLC_FOURCC('t','w','o','s'): /* 16-bit Big Endian */
300 case VLC_FOURCC('a','l','a','w'): /* ALaw 2:1 */
301 case VLC_FOURCC('u','l','a','w'): /* mu-Law 2:1 */
302 case VLC_FOURCC('r','a','w',' '): /* 8-bit offset binaries */
303 case VLC_FOURCC('f','l','3','2'): /* 32-bit Floating Point */
304 case VLC_FOURCC('f','l','6','4'): /* 64-bit Floating Point */
305 case VLC_FOURCC('i','n','2','4'): /* 24-bit Interger */
306 case VLC_FOURCC('i','n','3','2'): /* 32-bit Integer */
307 case 0x0011: /* DVI IMA */
308 case 0x6D730002: /* Microsoft ADPCM-ACM */
309 case 0x6D730011: /* DVI Intel IMAADPCM-ACM */
311 p_dec->pf_run = RunDecoderAudio;
318 /****************************************************************************
319 ****************************************************************************
323 ****************************************************************************
324 ****************************************************************************/
325 static int InitThreadAudio ( adec_thread_t * );
326 static void DecodeThreadAudio ( adec_thread_t * );
327 static void EndThreadAudio ( adec_thread_t * );
328 static int QTAudioInit ( adec_thread_t * );
330 static int RunDecoderAudio( decoder_fifo_t *p_fifo )
332 adec_thread_t *p_dec;
335 p_dec = malloc( sizeof( adec_thread_t ) );
338 msg_Err( p_fifo, "out of memory" );
339 DecoderError( p_fifo );
342 p_dec->p_fifo = p_fifo;
344 if( InitThreadAudio( p_dec ) != 0 )
346 DecoderError( p_fifo );
350 while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
352 DecodeThreadAudio( p_dec );
356 if( ( b_error = p_dec->p_fifo->b_error ) )
358 DecoderError( p_dec->p_fifo );
361 EndThreadAudio( p_dec );
370 static int InitThreadAudio( adec_thread_t *p_dec )
375 unsigned long WantedBufferSize;
376 unsigned long InputBufferSize = 0;
377 unsigned long OutputBufferSize = 0;
381 if( !( p_wf = (WAVEFORMATEX*)p_dec->p_fifo->p_waveformatex ) )
383 msg_Err( p_dec->p_fifo, "missing WAVEFORMATEX");
386 memcpy( fcc, &p_dec->p_fifo->i_fourcc, 4 );
388 /* get lock, avoid segfault */
389 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
390 vlc_mutex_lock( lockval.p_address );
396 if( QTAudioInit( p_dec ) != VLC_SUCCESS )
398 msg_Err( p_dec->p_fifo, "cannot initialize QT");
403 if( ( i_error = p_dec->InitializeQTML( 6 + 16 ) ) )
405 msg_Dbg( p_dec->p_fifo, "error while InitializeQTML = %d", i_error );
410 /* input format settings */
411 p_dec->InputFormatInfo.flags = 0;
412 p_dec->InputFormatInfo.sampleCount = 0;
413 p_dec->InputFormatInfo.buffer = NULL;
414 p_dec->InputFormatInfo.reserved = 0;
415 p_dec->InputFormatInfo.numChannels = p_wf->nChannels;
416 p_dec->InputFormatInfo.sampleSize = p_wf->wBitsPerSample;
417 p_dec->InputFormatInfo.sampleRate = p_wf->nSamplesPerSec;
418 p_dec->InputFormatInfo.format = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
420 /* output format settings */
421 p_dec->OutputFormatInfo.flags = 0;
422 p_dec->OutputFormatInfo.sampleCount = 0;
423 p_dec->OutputFormatInfo.buffer = NULL;
424 p_dec->OutputFormatInfo.reserved = 0;
425 p_dec->OutputFormatInfo.numChannels = p_wf->nChannels;
426 p_dec->OutputFormatInfo.sampleSize = 16;
427 p_dec->OutputFormatInfo.sampleRate = p_wf->nSamplesPerSec;
428 p_dec->OutputFormatInfo.format = FCC( 'N', 'O', 'N', 'E' );
431 /* on OS X QT is not threadsafe */
432 vlc_mutex_lock( &p_dec->p_fifo->p_vlc->quicktime_lock );
435 i_error = p_dec->SoundConverterOpen( &p_dec->InputFormatInfo,
436 &p_dec->OutputFormatInfo,
437 &p_dec->myConverter );
440 msg_Err( p_dec->p_fifo,
441 "error while SoundConverterOpen = %d", i_error );
446 /* tell the sound converter we accept VBR formats */
447 i_error = SoundConverterSetInfo( p_dec->myConverter, siClientAcceptsVBR,
451 if( p_wf->cbSize > 36 + 8 )
454 p_dec->SoundConverterSetInfo( p_dec->myConverter,
455 FCC( 'w', 'a', 'v', 'e' ),
456 ((uint8_t*)&p_wf[1]) + 36 + 8 );
457 msg_Dbg( p_dec->p_fifo,
458 "error while SoundConverterSetInfo = %d", i_error );
461 WantedBufferSize = p_dec->OutputFormatInfo.numChannels *
462 p_dec->OutputFormatInfo.sampleRate * 2;
463 p_dec->FramesToGet = 0;
465 i_error = p_dec->SoundConverterGetBufferSizes( p_dec->myConverter,
466 WantedBufferSize, &p_dec->FramesToGet,
467 &InputBufferSize, &OutputBufferSize );
469 msg_Dbg( p_dec->p_fifo, "WantedBufferSize=%li InputBufferSize=%li "
470 "OutputBufferSize=%li FramesToGet=%li", WantedBufferSize,
471 InputBufferSize, OutputBufferSize, p_dec->FramesToGet );
473 p_dec->InFrameSize = (InputBufferSize + p_dec->FramesToGet - 1 ) /
475 p_dec->OutFrameSize = OutputBufferSize / p_dec->FramesToGet;
477 msg_Dbg( p_dec->p_fifo, "frame size %d -> %d",
478 p_dec->InFrameSize, p_dec->OutFrameSize );
480 i_error = p_dec->SoundConverterBeginConversion( p_dec->myConverter );
483 msg_Err( p_dec->p_fifo,
484 "error while SoundConverterBeginConversion = %d", i_error );
489 vlc_mutex_unlock( &p_dec->p_fifo->p_vlc->quicktime_lock );
492 p_dec->output_format.i_format = AOUT_FMT_S16_NE;
493 p_dec->output_format.i_rate = p_dec->OutputFormatInfo.sampleRate;
494 p_dec->output_format.i_physical_channels =
495 p_dec->output_format.i_original_channels =
496 pi_channels_maps[p_dec->OutputFormatInfo.numChannels];
497 aout_DateInit( &p_dec->date, p_dec->output_format.i_rate );
498 p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
500 &p_dec->output_format );
501 if( !p_dec->p_aout_input )
503 msg_Err( p_dec->p_fifo, "cannot create aout" );
508 p_dec->i_buffer_size = 100*1000;
509 p_dec->p_buffer = malloc( p_dec->i_buffer_size );
511 vlc_mutex_unlock( lockval.p_address );
516 Restore_LDT_Keeper( p_dec->ldt_fs );
518 vlc_mutex_unlock( lockval.p_address );
522 static void DecodeThreadAudio( adec_thread_t *p_dec )
528 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
530 input_ExtractPES( p_dec->p_fifo, &p_pes );
533 msg_Err( p_dec->p_fifo, "cannot get PES" );
534 p_dec->p_fifo->b_error = 1;
538 p_dec->pts = p_pes->i_pts;
540 if( p_pes->i_pes_size > 0 && p_pes->i_pts > mdate() )
543 if( p_dec->i_buffer_size < p_dec->i_buffer + p_pes->i_pes_size )
545 p_dec->i_buffer_size = p_dec->i_buffer + p_pes->i_pes_size + 1024;
546 p_dec->p_buffer = realloc( p_dec->p_buffer,
547 p_dec->i_buffer_size );
550 GetPESData( &p_dec->p_buffer[p_dec->i_buffer],
551 p_dec->i_buffer_size - p_dec->i_buffer, p_pes );
552 p_dec->i_buffer += p_pes->i_pes_size;
554 if( p_dec->i_buffer > p_dec->InFrameSize )
556 int i_frames = p_dec->i_buffer / p_dec->InFrameSize;
557 long i_out_frames, i_out_bytes;
560 vlc_mutex_lock( lockval.p_address );
561 i_error = p_dec->SoundConverterConvertBuffer( p_dec->myConverter,
567 vlc_mutex_unlock( lockval.p_address );
570 msg_Dbg( p_dec->p_fifo,
571 "decoded %d frames -> %ld frames (error=%d)",
572 i_frames, i_out_frames, i_error );
574 msg_Dbg( p_dec->p_fifo, "decoded %ld frames = %ld bytes",
575 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_error && 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 &&
595 p_dec->pts != aout_DateGet( &p_dec->date ) )
597 aout_DateSet( &p_dec->date, p_dec->pts );
599 else if( !aout_DateGet( &p_dec->date ) )
601 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
605 while( i_out_frames > 0 )
609 i_frames = __MIN( i_out_frames, 1000 );
610 p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout,
615 msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
616 p_dec->p_fifo->b_error = 1;
619 p_aout_buffer->start_date = aout_DateGet( &p_dec->date );
620 p_aout_buffer->end_date = aout_DateIncrement( &p_dec->date,
623 memcpy( p_aout_buffer->p_buffer,
625 p_aout_buffer->i_nb_bytes );
628 msg_Dbg( p_dec->p_fifo,
629 "==> start=%lld end=%lld date=%lld",
630 p_aout_buffer->start_date,
631 p_aout_buffer->end_date, mdate() );
634 aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input,
638 msg_Dbg( p_dec->p_fifo, "s1=%d s2=%d", i_framesperchannels,
639 p_aout_buffer->i_nb_samples );
641 msg_Dbg( p_dec->p_fifo, "i_nb_bytes=%d i_nb_samples*4=%d",
642 p_aout_buffer->i_nb_bytes,
643 p_aout_buffer->i_nb_samples * 4 );
646 p_buff += i_frames * 4;
647 i_out_frames -= i_frames;
653 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
656 static void EndThreadAudio( adec_thread_t *p_dec )
660 unsigned long ConvertedFrames=0;
661 unsigned long ConvertedBytes=0;
663 /* get lock, avoid segfault */
664 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
665 vlc_mutex_lock( lockval.p_address );
667 i_error = p_dec->SoundConverterEndConversion( p_dec->myConverter, NULL,
670 msg_Dbg( p_dec->p_fifo, "SoundConverterEndConversion => %d", i_error );
672 i_error = p_dec->SoundConverterClose( p_dec->myConverter );
673 msg_Dbg( p_dec->p_fifo, "SoundConverterClose => %d", i_error );
676 FreeLibrary( p_dec->qtml );
677 msg_Dbg( p_dec->p_fifo, "FreeLibrary ok." );
679 vlc_mutex_unlock( lockval.p_address );
682 Restore_LDT_Keeper( p_dec->ldt_fs );
683 msg_Dbg( p_dec->p_fifo, "Restore_LDT_Keeper" );
688 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
691 static int QTAudioInit( adec_thread_t *p_dec )
695 p_dec->SoundConverterOpen = (void*)SoundConverterOpen;
696 p_dec->SoundConverterClose = (void*)SoundConverterClose;
697 p_dec->SoundConverterSetInfo = (void*)SoundConverterSetInfo;
698 p_dec->SoundConverterGetBufferSizes = (void*)SoundConverterGetBufferSizes;
699 p_dec->SoundConverterConvertBuffer = (void*)SoundConverterConvertBuffer;
700 p_dec->SoundConverterBeginConversion= (void*)SoundConverterBeginConversion;
701 p_dec->SoundConverterEndConversion = (void*)SoundConverterEndConversion;
705 p_dec->ldt_fs = Setup_LDT_Keeper();
708 p_dec->qtml = LoadLibraryA( "qtmlClient.dll" );
709 if( p_dec->qtml == NULL )
711 msg_Dbg( p_dec->p_fifo, "failed loading qtmlClient.dll" );
715 p_dec->InitializeQTML =
716 (void *)GetProcAddress( p_dec->qtml, "InitializeQTML" );
717 if( p_dec->InitializeQTML == NULL )
719 msg_Dbg( p_dec->p_fifo, "failed geting proc address InitializeQTML" );
723 p_dec->SoundConverterOpen =
724 (void *)GetProcAddress( p_dec->qtml, "SoundConverterOpen" );
725 if( p_dec->SoundConverterOpen == NULL )
727 msg_Dbg( p_dec->p_fifo,
728 "failed getting proc address SoundConverterOpen");
732 p_dec->SoundConverterClose =
733 (void *)GetProcAddress( p_dec->qtml, "SoundConverterClose" );
734 if( p_dec->SoundConverterClose == NULL )
736 msg_Dbg( p_dec->p_fifo,
737 "failed getting proc address SoundConverterClose");
741 p_dec->TerminateQTML =
742 (void *)GetProcAddress( p_dec->qtml, "TerminateQTML" );
743 if( p_dec->TerminateQTML == NULL )
745 msg_Dbg( p_dec->p_fifo, "failed getting proc address TerminateQTML");
749 p_dec->SoundConverterSetInfo =
750 (void *)GetProcAddress( p_dec->qtml, "SoundConverterSetInfo" );
751 if( p_dec->SoundConverterSetInfo == NULL )
753 msg_Dbg( p_dec->p_fifo,
754 "failed getting proc address SoundConverterSetInfo");
758 p_dec->SoundConverterGetBufferSizes =
759 (void *)GetProcAddress( p_dec->qtml, "SoundConverterGetBufferSizes" );
760 if( p_dec->SoundConverterGetBufferSizes == NULL )
762 msg_Dbg( p_dec->p_fifo,
763 "failed getting proc address SoundConverterGetBufferSizes");
767 p_dec->SoundConverterConvertBuffer =
768 (void *)GetProcAddress( p_dec->qtml, "SoundConverterConvertBuffer" );
769 if( p_dec->SoundConverterConvertBuffer == NULL )
771 msg_Dbg( p_dec->p_fifo,
772 "failed getting proc address SoundConverterConvertBuffer" );
776 p_dec->SoundConverterEndConversion =
777 (void *)GetProcAddress( p_dec->qtml, "SoundConverterEndConversion" );
778 if( p_dec->SoundConverterEndConversion == NULL )
780 msg_Dbg( p_dec->p_fifo,
781 "failed getting proc address SoundConverterEndConversion" );
785 p_dec->SoundConverterBeginConversion =
786 (void *)GetProcAddress( p_dec->qtml, "SoundConverterBeginConversion");
787 if( p_dec->SoundConverterBeginConversion == NULL )
789 msg_Dbg( p_dec->p_fifo,
790 "failed getting proc address SoundConverterBeginConversion" );
794 msg_Dbg( p_dec->p_fifo,
795 "Standard init done you may now call supported functions" );
796 #endif /* else SYS_DARWIN */
801 /****************************************************************************
802 ****************************************************************************
806 ****************************************************************************
807 ****************************************************************************/
810 static int RunDecoderVideo( decoder_fifo_t *p_fifo ){ return VLC_EGENERIC; }
813 static int InitThreadVideo ( vdec_thread_t * );
814 static void DecodeThreadVideo ( vdec_thread_t * );
815 static void EndThreadVideo ( vdec_thread_t * );
817 static int RunDecoderVideo( decoder_fifo_t *p_fifo )
819 vdec_thread_t *p_dec;
822 p_dec = malloc( sizeof( vdec_thread_t ) );
825 msg_Err( p_fifo, "out of memory" );
826 DecoderError( p_fifo );
829 p_dec->p_fifo = p_fifo;
831 if( InitThreadVideo( p_dec ) != 0 )
833 DecoderError( p_fifo );
837 while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
839 DecodeThreadVideo( p_dec );
843 if( ( b_error = p_dec->p_fifo->b_error ) )
845 DecoderError( p_dec->p_fifo );
848 EndThreadVideo( p_dec );
858 * InitThreadVideo: load and init library
861 static int InitThreadVideo( vdec_thread_t *p_dec )
865 ComponentDescription desc;
867 ComponentResult cres;
868 ImageSubCodecDecompressCapabilities icap; /* for ImageCodecInitialize() */
869 CodecInfo cinfo; /* for ImageCodecGetCodecInfo() */
870 ImageDescription *id;
872 BITMAPINFOHEADER *p_bih;
877 if( !( p_bih = (BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader ) )
879 msg_Err( p_dec->p_fifo, "missing BITMAPINFOHEADER !!" );
882 i_vide = p_bih->biSize - sizeof( BITMAPINFOHEADER );
883 p_vide = (uint8_t*)&p_bih[1];
884 if( i_vide <= 0 || p_vide == NULL )
886 msg_Err( p_dec->p_fifo, "invalid BITMAPINFOHEADER !!" );
889 memcpy( fcc, &p_dec->p_fifo->i_fourcc, 4 );
890 msg_Dbg( p_dec->p_fifo, "quicktime_video %4.4s %dx%d", fcc,
891 p_bih->biWidth, p_bih->biHeight );
893 /* get lock, avoid segfault */
894 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
895 vlc_mutex_lock( lockval.p_address );
900 p_dec->ldt_fs = Setup_LDT_Keeper();
902 msg_Dbg( p_dec->p_fifo, "trying to load `qtmlClient.dll'" );
903 if( !( p_dec->qtml = LoadLibraryA("qtmlClient.dll") ) )
905 msg_Err( p_dec->p_fifo, "cannot load qtmlClient.dll");
909 msg_Dbg( p_dec->p_fifo, "qtmlClient.dll loaded" );
911 /* (void*) to shut up gcc */
912 p_dec->InitializeQTML = (void*)InitializeQTML;
913 #endif /* SYS_DARWIN */
914 p_dec->FindNextComponent = (void*)FindNextComponent;
915 p_dec->OpenComponent = (void*)OpenComponent;
916 p_dec->ImageCodecInitialize = (void*)ImageCodecInitialize;
917 p_dec->ImageCodecGetCodecInfo = (void*)ImageCodecGetCodecInfo;
918 p_dec->ImageCodecPreDecompress = (void*)ImageCodecPreDecompress;
919 p_dec->ImageCodecBandDecompress = (void*)ImageCodecBandDecompress;
920 p_dec->GetGWorldPixMap = (void*)GetGWorldPixMap;
921 p_dec->QTNewGWorldFromPtr = (void*)QTNewGWorldFromPtr;
922 p_dec->NewHandleClear = (void*)NewHandleClear;
925 /* some sanity check */
926 if( !p_dec->InitializeQTML ||
927 !p_dec->FindNextComponent ||
928 !p_dec->OpenComponent ||
929 !p_dec->ImageCodecBandDecompress )
931 msg_Err( p_dec->p_fifo, "error getting qtmlClient.dll symbols");
935 if( ( i_result = p_dec->InitializeQTML( 6 + 16 ) ) )
937 msg_Dbg( p_dec->p_fifo, "error while InitializeQTML = %d", i_result );
942 /* init ComponentDescription */
943 memset( &desc, 0, sizeof( ComponentDescription ) );
944 desc.componentType = FCC( 'i', 'm', 'd', 'c' );
945 desc.componentSubType = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
946 desc.componentManufacturer = 0;
947 desc.componentFlags = 0;
948 desc.componentFlagsMask = 0;
950 if( !( prev = p_dec->FindNextComponent( NULL, &desc ) ) )
952 msg_Err( p_dec->p_fifo, "cannot find requested component" );
955 msg_Dbg( p_dec->p_fifo, "component id=0x%p", prev );
957 p_dec->ci = p_dec->OpenComponent( prev );
958 msg_Dbg( p_dec->p_fifo, "component instance p=0x%p", p_dec->ci );
960 memset( &icap, 0, sizeof( ImageSubCodecDecompressCapabilities ) );
961 cres = p_dec->ImageCodecInitialize( p_dec->ci, &icap );
962 /* msg_Dbg( p_dec->p_fifo, "ImageCodecInitialize->%p size=%d (%d)\n",cres,icap.recordSize,icap.decompressRecordSize); */
965 memset( &cinfo, 0, sizeof( CodecInfo ) );
966 cres = p_dec->ImageCodecGetCodecInfo( p_dec->ci, &cinfo );
967 msg_Dbg( p_dec->p_fifo,
968 "Flags: compr: 0x%lx decomp: 0x%lx format: 0x%lx\n",
969 cinfo.compressFlags, cinfo.decompressFlags, cinfo.formatFlags );
970 msg_Dbg( p_dec->p_fifo, "quicktime_video: Codec name: %.*s\n",
971 ((unsigned char*)&cinfo.typeName)[0],
972 ((unsigned char*)&cinfo.typeName)+1 );
974 /* make a yuy2 gworld */
975 p_dec->OutBufferRect.top = 0;
976 p_dec->OutBufferRect.left = 0;
977 p_dec->OutBufferRect.right = p_bih->biWidth;
978 p_dec->OutBufferRect.bottom = p_bih->biHeight;
981 /* codec data FIXME use codec not SVQ3 */
982 msg_Dbg( p_dec->p_fifo, "vide = %d", i_vide );
983 id = malloc( sizeof( ImageDescription ) + ( i_vide - 70 ) );
984 id->idSize = sizeof( ImageDescription ) + ( i_vide - 70 );
985 id->cType = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
986 id->version = GetWBE ( p_vide + 0 );
987 id->revisionLevel = GetWBE ( p_vide + 2 );
988 id->vendor = GetDWBE( p_vide + 4 );
989 id->temporalQuality = GetDWBE( p_vide + 8 );
990 id->spatialQuality = GetDWBE( p_vide + 12 );
991 id->width = GetWBE ( p_vide + 16 );
992 id->height = GetWBE ( p_vide + 18 );
993 id->hRes = GetDWBE( p_vide + 20 );
994 id->vRes = GetDWBE( p_vide + 24 );
995 id->dataSize = GetDWBE( p_vide + 28 );
996 id->frameCount = GetWBE ( p_vide + 32 );
997 memcpy( &id->name, p_vide + 34, 32 );
998 id->depth = GetWBE ( p_vide + 66 );
999 id->clutID = GetWBE ( p_vide + 68 );
1002 memcpy( ((char*)&id->clutID) + 2, p_vide + 70, i_vide - 70 );
1005 msg_Dbg( p_dec->p_fifo, "idSize=%ld ver=%d rev=%d vendor=%ld tempQ=%d "
1006 "spaQ=%d w=%d h=%d dpi=%d%d dataSize=%d frameCount=%d clutID=%d",
1007 id->idSize, id->version, id->revisionLevel, id->vendor,
1008 (int)id->temporalQuality, (int)id->spatialQuality,
1009 id->width, id->height,
1010 (int)id->hRes, (int)id->vRes,
1015 p_dec->framedescHandle =
1016 (ImageDescriptionHandle) p_dec->NewHandleClear( id->idSize );
1017 memcpy( *p_dec->framedescHandle, id, id->idSize );
1019 p_dec->plane = malloc( p_bih->biWidth * p_bih->biHeight * 3 );
1021 i_result = p_dec->QTNewGWorldFromPtr( &p_dec->OutBufferGWorld,
1022 /*pixel format of new GWorld==YUY2 */
1024 /* we should benchmark if yvu9 is
1025 * faster for svq3, too */
1026 &p_dec->OutBufferRect,
1029 p_bih->biWidth * 2 );
1031 msg_Dbg( p_dec->p_fifo, "NewGWorldFromPtr returned:%ld\n",
1032 65536 - ( i_result&0xffff ) );
1034 memset( &p_dec->decpar, 0, sizeof( CodecDecompressParams ) );
1035 p_dec->decpar.imageDescription = p_dec->framedescHandle;
1036 p_dec->decpar.startLine = 0;
1037 p_dec->decpar.stopLine = ( **p_dec->framedescHandle ).height;
1038 p_dec->decpar.frameNumber = 1;
1039 p_dec->decpar.matrixFlags = 0;
1040 p_dec->decpar.matrixType = 0;
1041 p_dec->decpar.matrix = 0;
1042 p_dec->decpar.capabilities = &p_dec->codeccap;
1043 p_dec->decpar.accuracy = codecNormalQuality;
1044 p_dec->decpar.srcRect = p_dec->OutBufferRect;
1045 p_dec->decpar.transferMode = srcCopy;
1046 p_dec->decpar.dstPixMap = **p_dec->GetGWorldPixMap( p_dec->OutBufferGWorld );/*destPixmap; */
1048 cres = p_dec->ImageCodecPreDecompress( p_dec->ci, &p_dec->decpar );
1049 msg_Dbg( p_dec->p_fifo,
1050 "quicktime_video: ImageCodecPreDecompress cres=0x%X\n",
1053 p_dec->p_vout = vout_Request( p_dec->p_fifo, NULL,
1054 p_bih->biWidth, p_bih->biHeight,
1055 VLC_FOURCC( 'Y', 'U', 'Y', '2' ),
1056 VOUT_ASPECT_FACTOR * p_bih->biWidth /
1059 if( !p_dec->p_vout )
1061 msg_Err( p_dec->p_fifo, "cannot get a vout" );
1065 p_dec->i_buffer = 1000*1000;
1066 p_dec->p_buffer = malloc( p_dec->i_buffer );
1068 vlc_mutex_unlock( lockval.p_address );
1073 Restore_LDT_Keeper( p_dec->ldt_fs );
1075 vlc_mutex_unlock( lockval.p_address );
1076 return VLC_EGENERIC;
1080 static void DecodeThreadVideo( vdec_thread_t *p_dec )
1082 BITMAPINFOHEADER *p_bih =
1083 (BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader;
1085 pes_packet_t *p_pes;
1086 vlc_value_t lockval;
1088 ComponentResult cres;
1090 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
1092 input_ExtractPES( p_dec->p_fifo, &p_pes );
1095 msg_Err( p_dec->p_fifo, "cannot get PES" );
1096 p_dec->p_fifo->b_error = 1;
1100 if( p_pes->i_pes_size > p_dec->i_buffer )
1102 p_dec->i_buffer = 3 * p_pes->i_pes_size / 2;
1103 free( p_dec->p_buffer );
1104 p_dec->p_buffer = malloc( p_dec->i_buffer );
1107 if( p_pes->i_pes_size > 0 && p_pes->i_pts > mdate() )
1109 GetPESData( p_dec->p_buffer, p_dec->i_buffer, p_pes );
1111 while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
1113 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
1117 msleep( VOUT_OUTMEM_SLEEP );
1120 p_dec->decpar.data = p_dec->p_buffer;
1121 p_dec->decpar.bufferSize = p_pes->i_pes_size;
1122 (**p_dec->framedescHandle).dataSize = p_pes->i_pes_size;
1124 vlc_mutex_lock( lockval.p_address );
1125 cres = p_dec->ImageCodecBandDecompress( p_dec->ci, &p_dec->decpar );
1126 vlc_mutex_unlock( lockval.p_address );
1128 ++p_dec->decpar.frameNumber;
1132 msg_Dbg( p_dec->p_fifo, "quicktime_video: ImageCodecBandDecompress"
1133 " cres=0x%X (-0x%X) %d :(\n",
1134 (int)cres,(int)-cres, (int)cres );
1137 memcpy( p_pic->p[0].p_pixels,
1139 p_bih->biWidth * p_bih->biHeight * 2 );
1141 vout_DatePicture( p_dec->p_vout, p_pic, p_pes->i_pts );
1142 vout_DisplayPicture( p_dec->p_vout, p_pic );
1145 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
1148 static void EndThreadVideo( vdec_thread_t *p_dec )
1150 msg_Dbg( p_dec->p_fifo, "QuickTime library video decoder closing" );
1151 free( p_dec->plane );
1152 vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
1155 FreeLibrary( p_dec->qtml );
1156 msg_Dbg( p_dec->p_fifo, "FreeLibrary ok." );
1160 Restore_LDT_Keeper( p_dec->ldt_fs );
1161 msg_Dbg( p_dec->p_fifo, "Restore_LDT_Keeper" );