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.10 2003/08/01 20:06:43 gbazin 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"
51 HMODULE WINAPI LoadLibraryA(LPCSTR);
52 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
53 int WINAPI FreeLibrary(HMODULE);
57 /*****************************************************************************
59 *****************************************************************************/
60 static int OpenDecoder ( vlc_object_t * );
62 static int RunDecoderAudio( decoder_fifo_t * );
63 static int RunDecoderVideo( decoder_fifo_t * );
65 /*****************************************************************************
67 *****************************************************************************/
70 set_description( _("QuickTime library decoder") );
71 set_capability( "decoder", 10 );
72 set_callbacks( OpenDecoder, NULL );
75 var_Create( p_module->p_libvlc, "qt_mutex", VLC_VAR_MUTEX );
78 #define FCC( a, b , c, d ) \
79 ((uint32_t)( ((a)<<24)|((b)<<16)|((c)<<8)|(d)))
82 typedef struct OpaqueSoundConverter* SoundConverter;
85 typedef unsigned long UnsignedFixed;
88 typedef struct SoundComponentData {
93 UnsignedFixed sampleRate;
99 #endif /* SYS_DARWIN */
103 /* Input properties */
104 decoder_fifo_t *p_fifo;
113 OSErr (*InitializeQTML) ( long flags );
114 OSErr (*TerminateQTML) ( void );
115 #endif /* SYS_DARWIN */
117 int (*SoundConverterOpen) ( const SoundComponentData *,
118 const SoundComponentData *,
120 int (*SoundConverterClose) ( SoundConverter );
121 int (*SoundConverterSetInfo) ( SoundConverter , OSType, void * );
122 int (*SoundConverterGetBufferSizes) ( SoundConverter, unsigned long,
123 unsigned long*, unsigned long*,
125 int (*SoundConverterBeginConversion)( SoundConverter );
126 int (*SoundConverterEndConversion) ( SoundConverter, void *,
127 unsigned long *, unsigned long *);
128 int (*SoundConverterConvertBuffer) ( SoundConverter, const void *,
129 unsigned long, void *,
130 unsigned long *, unsigned long * );
131 SoundConverter myConverter;
132 SoundComponentData InputFormatInfo, OutputFormatInfo;
135 unsigned int InFrameSize;
136 unsigned int OutFrameSize;
138 /* Output properties */
139 aout_instance_t * p_aout; /* opaque */
140 aout_input_t * p_aout_input; /* opaque */
141 audio_sample_format_t output_format;
147 unsigned int i_buffer;
148 unsigned int i_buffer_size;
151 uint8_t buffer_out[1000000]; /* FIXME */
158 /* Input properties */
159 decoder_fifo_t *p_fifo;
167 OSErr (*InitializeQTML) ( long flags );
169 #endif /* SYS_DARWIN */
170 Component (*FindNextComponent)
171 ( Component prev, ComponentDescription* desc );
173 ComponentInstance (*OpenComponent)
176 ComponentResult (*ImageCodecInitialize)
177 ( ComponentInstance ci, ImageSubCodecDecompressCapabilities * cap);
179 ComponentResult (*ImageCodecGetCodecInfo)
180 ( ComponentInstance ci, CodecInfo *info );
182 ComponentResult (*ImageCodecPreDecompress)
183 ( ComponentInstance ci, CodecDecompressParams * params );
185 ComponentResult (*ImageCodecBandDecompress)
186 ( ComponentInstance ci, CodecDecompressParams * params );
188 PixMapHandle (*GetGWorldPixMap)
189 ( GWorldPtr offscreenGWorld );
191 OSErr (*QTNewGWorldFromPtr)
192 ( GWorldPtr *gw, OSType pixelFormat, const Rect *boundsRect,
193 CTabHandle cTable, /*GDHandle*/ void *aGDevice, /*unused*/
194 GWorldFlags flags, void *baseAddr, long rowBytes );
196 OSErr (*NewHandleClear)
199 ComponentInstance ci;
200 Rect OutBufferRect; /* the dimensions of our GWorld */
201 GWorldPtr OutBufferGWorld; /* a GWorld is some kind of
202 description for a drawing
204 ImageDescriptionHandle framedescHandle;
206 CodecDecompressParams decpar; /* for ImageCodecPreDecompress()*/
207 CodecCapabilities codeccap; /* for decpar */
210 /* Output properties */
211 vout_thread_t * p_vout;
216 unsigned int i_buffer;
222 static int pi_channels_maps[6] =
226 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
227 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
228 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
229 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
230 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
234 static uint16_t GetWBE( uint8_t *p_buff )
236 return( (p_buff[0]<<8) + p_buff[1] );
239 static uint32_t GetDWBE( uint8_t *p_buff )
241 return( (p_buff[0] << 24) + ( p_buff[1] <<16 ) +
242 ( p_buff[2] <<8 ) + p_buff[3] );
246 static int GetPESData( uint8_t *p_buf, int i_max, pes_packet_t *p_pes )
251 data_packet_t *p_data;
254 p_data = p_pes->p_first;
255 while( p_data != NULL && i_count < i_max )
258 i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
264 p_data->p_payload_start,
268 p_data = p_data->p_next;
273 if( i_count < i_max )
275 memset( p_buf, 0, i_max - i_count );
280 /*****************************************************************************
281 * OpenDecoder: probe the decoder and return score
282 *****************************************************************************
283 * Tries to launch a decoder and return score so that the interface is able
285 *****************************************************************************/
286 static int OpenDecoder( vlc_object_t *p_this )
288 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
290 switch( p_fifo->i_fourcc )
292 case VLC_FOURCC('S','V','Q','3'): /* Sorenson v3 */
293 /* case VLC_FOURCC('S','V','Q','1'): Sorenson v1
294 case VLC_FOURCC('Z','y','G','o'):
295 case VLC_FOURCC('V','P','3','1'):
296 case VLC_FOURCC('3','I','V','1'): */
297 case VLC_FOURCC('r','l','e',' '): /* QuickTime animation (RLE) */
298 case VLC_FOURCC('r','p','z','a'): /* QuickTime Apple Video */
299 case VLC_FOURCC('a','z','p','r'): /* QuickTime animation (RLE) */
300 p_fifo->pf_run = RunDecoderVideo;
303 case VLC_FOURCC('m','p','4','a'): /* MPEG-4 audio */
304 case VLC_FOURCC('Q','D','M','C'): /* QDesign */
305 case VLC_FOURCC('Q','D','M','2'): /* QDesign* 2 */
306 case VLC_FOURCC('Q','c','l','p'): /* Qualcomm Purevoice Codec */
307 case VLC_FOURCC('Q','C','L','P'): /* Qualcomm Purevoice Codec */
308 case VLC_FOURCC('M','A','C','3'): /* MACE3 audio decoder */
309 case VLC_FOURCC('M','A','C','6'): /* MACE6 audio decoder */
310 case VLC_FOURCC('d','v','c','a'): /* DV Audio */
311 case VLC_FOURCC('s','o','w','t'): /* 16-bit Little Endian */
312 case VLC_FOURCC('t','w','o','s'): /* 16-bit Big Endian */
313 case VLC_FOURCC('a','l','a','w'): /* ALaw 2:1 */
314 case VLC_FOURCC('u','l','a','w'): /* mu-Law 2:1 */
315 case VLC_FOURCC('r','a','w',' '): /* 8-bit offset binaries */
316 case VLC_FOURCC('f','l','3','2'): /* 32-bit Floating Point */
317 case VLC_FOURCC('f','l','6','4'): /* 64-bit Floating Point */
318 case VLC_FOURCC('i','n','2','4'): /* 24-bit Interger */
319 case VLC_FOURCC('i','n','3','2'): /* 32-bit Integer */
320 case 0x0011: /* DVI IMA */
321 case 0x6D730002: /* Microsoft ADPCM-ACM */
322 case 0x6D730011: /* DVI Intel IMAADPCM-ACM */
324 p_fifo->pf_run = RunDecoderAudio;
331 /****************************************************************************
332 ****************************************************************************
336 ****************************************************************************
337 ****************************************************************************/
338 static int InitThreadAudio ( adec_thread_t * );
339 static void DecodeThreadAudio ( adec_thread_t * );
340 static void EndThreadAudio ( adec_thread_t * );
341 static int QTAudioInit ( adec_thread_t * );
343 static int RunDecoderAudio( decoder_fifo_t *p_fifo )
345 adec_thread_t *p_dec;
348 p_dec = malloc( sizeof( adec_thread_t ) );
351 msg_Err( p_fifo, "out of memory" );
352 DecoderError( p_fifo );
355 p_dec->p_fifo = p_fifo;
357 if( InitThreadAudio( p_dec ) != 0 )
359 DecoderError( p_fifo );
363 while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
365 DecodeThreadAudio( p_dec );
369 if( ( b_error = p_dec->p_fifo->b_error ) )
371 DecoderError( p_dec->p_fifo );
374 EndThreadAudio( p_dec );
383 static int InitThreadAudio( adec_thread_t *p_dec )
388 unsigned long WantedBufferSize;
389 unsigned long InputBufferSize = 0;
390 unsigned long OutputBufferSize = 0;
394 if( !( p_wf = (WAVEFORMATEX*)p_dec->p_fifo->p_waveformatex ) )
396 msg_Err( p_dec->p_fifo, "missing WAVEFORMATEX");
399 memcpy( fcc, &p_dec->p_fifo->i_fourcc, 4 );
401 /* get lock, avoid segfault */
402 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
403 vlc_mutex_lock( lockval.p_address );
409 if( QTAudioInit( p_dec ) != VLC_SUCCESS )
411 msg_Err( p_dec->p_fifo, "cannot initialize QT");
416 if( ( i_error = p_dec->InitializeQTML( 6 + 16 ) ) )
418 msg_Dbg( p_dec->p_fifo, "error while InitializeQTML = %d", i_error );
423 /* input format settings */
424 p_dec->InputFormatInfo.flags = 0;
425 p_dec->InputFormatInfo.sampleCount = 0;
426 p_dec->InputFormatInfo.buffer = NULL;
427 p_dec->InputFormatInfo.reserved = 0;
428 p_dec->InputFormatInfo.numChannels = p_wf->nChannels;
429 p_dec->InputFormatInfo.sampleSize = p_wf->wBitsPerSample;
430 p_dec->InputFormatInfo.sampleRate = p_wf->nSamplesPerSec;
431 p_dec->InputFormatInfo.format = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
433 /* output format settings */
434 p_dec->OutputFormatInfo.flags = 0;
435 p_dec->OutputFormatInfo.sampleCount = 0;
436 p_dec->OutputFormatInfo.buffer = NULL;
437 p_dec->OutputFormatInfo.reserved = 0;
438 p_dec->OutputFormatInfo.numChannels = p_wf->nChannels;
439 p_dec->OutputFormatInfo.sampleSize = 16;
440 p_dec->OutputFormatInfo.sampleRate = p_wf->nSamplesPerSec;
441 p_dec->OutputFormatInfo.format = FCC( 'N', 'O', 'N', 'E' );
444 /* on OS X QT is not threadsafe */
445 vlc_mutex_lock( &p_dec->p_fifo->p_vlc->quicktime_lock );
448 i_error = p_dec->SoundConverterOpen( &p_dec->InputFormatInfo,
449 &p_dec->OutputFormatInfo,
450 &p_dec->myConverter );
453 msg_Err( p_dec->p_fifo,
454 "error while SoundConverterOpen = %d", i_error );
459 /* tell the sound converter we accept VBR formats */
460 i_error = SoundConverterSetInfo( p_dec->myConverter, siClientAcceptsVBR,
464 if( p_wf->cbSize > 36 + 8 )
467 p_dec->SoundConverterSetInfo( p_dec->myConverter,
468 FCC( 'w', 'a', 'v', 'e' ),
469 ((uint8_t*)&p_wf[1]) + 36 + 8 );
470 msg_Dbg( p_dec->p_fifo,
471 "error while SoundConverterSetInfo = %d", i_error );
474 WantedBufferSize = p_dec->OutputFormatInfo.numChannels *
475 p_dec->OutputFormatInfo.sampleRate * 2;
476 p_dec->FramesToGet = 0;
478 i_error = p_dec->SoundConverterGetBufferSizes( p_dec->myConverter,
479 WantedBufferSize, &p_dec->FramesToGet,
480 &InputBufferSize, &OutputBufferSize );
482 msg_Dbg( p_dec->p_fifo, "WantedBufferSize=%li InputBufferSize=%li "
483 "OutputBufferSize=%li FramesToGet=%li", WantedBufferSize,
484 InputBufferSize, OutputBufferSize, p_dec->FramesToGet );
486 p_dec->InFrameSize = (InputBufferSize + p_dec->FramesToGet - 1 ) /
488 p_dec->OutFrameSize = OutputBufferSize / p_dec->FramesToGet;
490 msg_Dbg( p_dec->p_fifo, "frame size %d -> %d",
491 p_dec->InFrameSize, p_dec->OutFrameSize );
493 i_error = p_dec->SoundConverterBeginConversion( p_dec->myConverter );
496 msg_Err( p_dec->p_fifo,
497 "error while SoundConverterBeginConversion = %d", i_error );
502 vlc_mutex_unlock( &p_dec->p_fifo->p_vlc->quicktime_lock );
505 p_dec->output_format.i_format = AOUT_FMT_S16_NE;
506 p_dec->output_format.i_rate = p_wf->nSamplesPerSec;
507 p_dec->output_format.i_physical_channels =
508 p_dec->output_format.i_original_channels =
509 pi_channels_maps[p_wf->nChannels];
510 aout_DateInit( &p_dec->date, p_dec->output_format.i_rate );
511 p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
513 &p_dec->output_format );
514 if( !p_dec->p_aout_input )
516 msg_Err( p_dec->p_fifo, "cannot create aout" );
521 p_dec->i_buffer_size = 100*1000;
522 p_dec->p_buffer = malloc( p_dec->i_buffer_size );
524 vlc_mutex_unlock( lockval.p_address );
529 Restore_LDT_Keeper( p_dec->ldt_fs );
531 vlc_mutex_unlock( lockval.p_address );
535 static void DecodeThreadAudio( adec_thread_t *p_dec )
541 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
543 input_ExtractPES( p_dec->p_fifo, &p_pes );
546 msg_Err( p_dec->p_fifo, "cannot get PES" );
547 p_dec->p_fifo->b_error = 1;
551 p_dec->pts = p_pes->i_pts;
553 if( p_pes->i_pes_size > 0 && p_pes->i_pts > mdate() )
556 if( p_dec->i_buffer_size < p_dec->i_buffer + p_pes->i_pes_size )
558 p_dec->i_buffer_size = p_dec->i_buffer + p_pes->i_pes_size + 1024;
559 p_dec->p_buffer = realloc( p_dec->p_buffer,
560 p_dec->i_buffer_size );
563 GetPESData( &p_dec->p_buffer[p_dec->i_buffer],
564 p_dec->i_buffer_size - p_dec->i_buffer, p_pes );
565 p_dec->i_buffer += p_pes->i_pes_size;
567 if( p_dec->i_buffer > p_dec->InFrameSize )
569 int i_frames = p_dec->i_buffer / p_dec->InFrameSize;
570 long i_out_frames, i_out_bytes;
573 vlc_mutex_lock( lockval.p_address );
574 i_error = p_dec->SoundConverterConvertBuffer( p_dec->myConverter,
580 vlc_mutex_unlock( lockval.p_address );
583 msg_Dbg( p_dec->p_fifo,
584 "decoded %d frames -> %ld frames (error=%d)",
585 i_frames, i_out_frames, i_error );
587 msg_Dbg( p_dec->p_fifo, "decoded %ld frames = %ld bytes",
588 i_out_frames, i_out_bytes );
591 p_dec->i_buffer -= i_frames * p_dec->InFrameSize;
592 if( p_dec->i_buffer > 0 )
594 memmove( &p_dec->p_buffer[0],
595 &p_dec->p_buffer[i_frames * p_dec->InFrameSize],
599 if( !i_error && i_out_frames > 0 )
601 aout_buffer_t *p_aout_buffer;
602 uint8_t *p_buff = p_dec->buffer_out;
604 /*msg_Dbg( p_dec->p_fifo, "pts=%lld date=%lld dateget=%lld",
605 p_dec->pts, mdate(), aout_DateGet( &p_dec->date ) );*/
607 if( p_dec->pts != 0 &&
608 p_dec->pts != aout_DateGet( &p_dec->date ) )
610 aout_DateSet( &p_dec->date, p_dec->pts );
612 else if( !aout_DateGet( &p_dec->date ) )
614 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
618 while( i_out_frames > 0 )
622 i_frames = __MIN( i_out_frames, 1000 );
623 p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout,
628 msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
629 p_dec->p_fifo->b_error = 1;
632 p_aout_buffer->start_date = aout_DateGet( &p_dec->date );
633 p_aout_buffer->end_date = aout_DateIncrement( &p_dec->date,
636 memcpy( p_aout_buffer->p_buffer,
638 p_aout_buffer->i_nb_bytes );
641 msg_Dbg( p_dec->p_fifo,
642 "==> start=%lld end=%lld date=%lld",
643 p_aout_buffer->start_date,
644 p_aout_buffer->end_date, mdate() );
647 aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input,
651 msg_Dbg( p_dec->p_fifo, "s1=%d s2=%d", i_framesperchannels,
652 p_aout_buffer->i_nb_samples );
654 msg_Dbg( p_dec->p_fifo, "i_nb_bytes=%d i_nb_samples*4=%d",
655 p_aout_buffer->i_nb_bytes,
656 p_aout_buffer->i_nb_samples * 4 );
659 p_buff += i_frames * 4;
660 i_out_frames -= i_frames;
666 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
669 static void EndThreadAudio( adec_thread_t *p_dec )
673 unsigned long ConvertedFrames=0;
674 unsigned long ConvertedBytes=0;
676 /* get lock, avoid segfault */
677 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
678 vlc_mutex_lock( lockval.p_address );
680 i_error = p_dec->SoundConverterEndConversion( p_dec->myConverter, NULL,
683 msg_Dbg( p_dec->p_fifo, "SoundConverterEndConversion => %d", i_error );
685 i_error = p_dec->SoundConverterClose( p_dec->myConverter );
686 msg_Dbg( p_dec->p_fifo, "SoundConverterClose => %d", i_error );
689 FreeLibrary( p_dec->qtml );
690 msg_Dbg( p_dec->p_fifo, "FreeLibrary ok." );
692 vlc_mutex_unlock( lockval.p_address );
695 Restore_LDT_Keeper( p_dec->ldt_fs );
696 msg_Dbg( p_dec->p_fifo, "Restore_LDT_Keeper" );
701 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
704 static int QTAudioInit( adec_thread_t *p_dec )
708 p_dec->SoundConverterOpen = (void*)SoundConverterOpen;
709 p_dec->SoundConverterClose = (void*)SoundConverterClose;
710 p_dec->SoundConverterSetInfo = (void*)SoundConverterSetInfo;
711 p_dec->SoundConverterGetBufferSizes = (void*)SoundConverterGetBufferSizes;
712 p_dec->SoundConverterConvertBuffer = (void*)SoundConverterConvertBuffer;
713 p_dec->SoundConverterBeginConversion= (void*)SoundConverterBeginConversion;
714 p_dec->SoundConverterEndConversion = (void*)SoundConverterEndConversion;
718 p_dec->ldt_fs = Setup_LDT_Keeper();
721 p_dec->qtml = LoadLibraryA( "qtmlClient.dll" );
722 if( p_dec->qtml == NULL )
724 msg_Dbg( p_dec->p_fifo, "failed loading qtmlClient.dll" );
728 p_dec->InitializeQTML =
729 (void *)GetProcAddress( p_dec->qtml, "InitializeQTML" );
730 if( p_dec->InitializeQTML == NULL )
732 msg_Dbg( p_dec->p_fifo, "failed geting proc address InitializeQTML" );
736 p_dec->SoundConverterOpen =
737 (void *)GetProcAddress( p_dec->qtml, "SoundConverterOpen" );
738 if( p_dec->SoundConverterOpen == NULL )
740 msg_Dbg( p_dec->p_fifo,
741 "failed getting proc address SoundConverterOpen");
745 p_dec->SoundConverterClose =
746 (void *)GetProcAddress( p_dec->qtml, "SoundConverterClose" );
747 if( p_dec->SoundConverterClose == NULL )
749 msg_Dbg( p_dec->p_fifo,
750 "failed getting proc address SoundConverterClose");
754 p_dec->TerminateQTML =
755 (void *)GetProcAddress( p_dec->qtml, "TerminateQTML" );
756 if( p_dec->TerminateQTML == NULL )
758 msg_Dbg( p_dec->p_fifo, "failed getting proc address TerminateQTML");
762 p_dec->SoundConverterSetInfo =
763 (void *)GetProcAddress( p_dec->qtml, "SoundConverterSetInfo" );
764 if( p_dec->SoundConverterSetInfo == NULL )
766 msg_Dbg( p_dec->p_fifo,
767 "failed getting proc address SoundConverterSetInfo");
771 p_dec->SoundConverterGetBufferSizes =
772 (void *)GetProcAddress( p_dec->qtml, "SoundConverterGetBufferSizes" );
773 if( p_dec->SoundConverterGetBufferSizes == NULL )
775 msg_Dbg( p_dec->p_fifo,
776 "failed getting proc address SoundConverterGetBufferSizes");
780 p_dec->SoundConverterConvertBuffer =
781 (void *)GetProcAddress( p_dec->qtml, "SoundConverterConvertBuffer" );
782 if( p_dec->SoundConverterConvertBuffer == NULL )
784 msg_Dbg( p_dec->p_fifo,
785 "failed getting proc address SoundConverterConvertBuffer" );
789 p_dec->SoundConverterEndConversion =
790 (void *)GetProcAddress( p_dec->qtml, "SoundConverterEndConversion" );
791 if( p_dec->SoundConverterEndConversion == NULL )
793 msg_Dbg( p_dec->p_fifo,
794 "failed getting proc address SoundConverterEndConversion" );
798 p_dec->SoundConverterBeginConversion =
799 (void *)GetProcAddress( p_dec->qtml, "SoundConverterBeginConversion");
800 if( p_dec->SoundConverterBeginConversion == NULL )
802 msg_Dbg( p_dec->p_fifo,
803 "failed getting proc address SoundConverterBeginConversion" );
807 msg_Dbg( p_dec->p_fifo,
808 "Standard init done you may now call supported functions" );
809 #endif /* else SYS_DARWIN */
814 /****************************************************************************
815 ****************************************************************************
819 ****************************************************************************
820 ****************************************************************************/
823 static int RunDecoderVideo( decoder_fifo_t *p_fifo ){ return VLC_EGENERIC; }
826 static int InitThreadVideo ( vdec_thread_t * );
827 static void DecodeThreadVideo ( vdec_thread_t * );
828 static void EndThreadVideo ( vdec_thread_t * );
830 static int RunDecoderVideo( decoder_fifo_t *p_fifo )
832 vdec_thread_t *p_dec;
835 p_dec = malloc( sizeof( vdec_thread_t ) );
838 msg_Err( p_fifo, "out of memory" );
839 DecoderError( p_fifo );
842 p_dec->p_fifo = p_fifo;
844 if( InitThreadVideo( p_dec ) != 0 )
846 DecoderError( p_fifo );
850 while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
852 DecodeThreadVideo( p_dec );
856 if( ( b_error = p_dec->p_fifo->b_error ) )
858 DecoderError( p_dec->p_fifo );
861 EndThreadVideo( p_dec );
871 * InitThreadVideo: load and init library
874 static int InitThreadVideo( vdec_thread_t *p_dec )
878 ComponentDescription desc;
880 ComponentResult cres;
881 ImageSubCodecDecompressCapabilities icap; /* for ImageCodecInitialize() */
882 CodecInfo cinfo; /* for ImageCodecGetCodecInfo() */
883 ImageDescription *id;
885 BITMAPINFOHEADER *p_bih;
890 if( !( p_bih = (BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader ) )
892 msg_Err( p_dec->p_fifo, "missing BITMAPINFOHEADER !!" );
895 i_vide = p_bih->biSize - sizeof( BITMAPINFOHEADER );
896 p_vide = (uint8_t*)&p_bih[1];
897 if( i_vide <= 0 || p_vide == NULL )
899 msg_Err( p_dec->p_fifo, "invalid BITMAPINFOHEADER !!" );
902 memcpy( fcc, &p_dec->p_fifo->i_fourcc, 4 );
903 msg_Dbg( p_dec->p_fifo, "quicktime_video %4.4s %dx%d", fcc,
904 p_bih->biWidth, p_bih->biHeight );
906 /* get lock, avoid segfault */
907 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
908 vlc_mutex_lock( lockval.p_address );
913 p_dec->ldt_fs = Setup_LDT_Keeper();
915 msg_Dbg( p_dec->p_fifo, "trying to load `qtmlClient.dll'" );
916 if( !( p_dec->qtml = LoadLibraryA("qtmlClient.dll") ) )
918 msg_Err( p_dec->p_fifo, "cannot load qtmlClient.dll");
922 msg_Dbg( p_dec->p_fifo, "qtmlClient.dll loaded" );
924 /* (void*) to shut up gcc */
925 p_dec->InitializeQTML = (void*)InitializeQTML;
926 #endif /* SYS_DARWIN */
927 p_dec->FindNextComponent = (void*)FindNextComponent;
928 p_dec->OpenComponent = (void*)OpenComponent;
929 p_dec->ImageCodecInitialize = (void*)ImageCodecInitialize;
930 p_dec->ImageCodecGetCodecInfo = (void*)ImageCodecGetCodecInfo;
931 p_dec->ImageCodecPreDecompress = (void*)ImageCodecPreDecompress;
932 p_dec->ImageCodecBandDecompress = (void*)ImageCodecBandDecompress;
933 p_dec->GetGWorldPixMap = (void*)GetGWorldPixMap;
934 p_dec->QTNewGWorldFromPtr = (void*)QTNewGWorldFromPtr;
935 p_dec->NewHandleClear = (void*)NewHandleClear;
938 /* some sanity check */
939 if( !p_dec->InitializeQTML ||
940 !p_dec->FindNextComponent ||
941 !p_dec->OpenComponent ||
942 !p_dec->ImageCodecBandDecompress )
944 msg_Err( p_dec->p_fifo, "error getting qtmlClient.dll symbols");
948 if( ( i_result = p_dec->InitializeQTML( 6 + 16 ) ) )
950 msg_Dbg( p_dec->p_fifo, "error while InitializeQTML = %d", i_result );
955 /* init ComponentDescription */
956 memset( &desc, 0, sizeof( ComponentDescription ) );
957 desc.componentType = FCC( 'i', 'm', 'd', 'c' );
958 desc.componentSubType = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
959 desc.componentManufacturer = 0;
960 desc.componentFlags = 0;
961 desc.componentFlagsMask = 0;
963 if( !( prev = p_dec->FindNextComponent( NULL, &desc ) ) )
965 msg_Err( p_dec->p_fifo, "cannot find requested component" );
968 msg_Dbg( p_dec->p_fifo, "component id=0x%p", prev );
970 p_dec->ci = p_dec->OpenComponent( prev );
971 msg_Dbg( p_dec->p_fifo, "component instance p=0x%p", p_dec->ci );
973 memset( &icap, 0, sizeof( ImageSubCodecDecompressCapabilities ) );
974 cres = p_dec->ImageCodecInitialize( p_dec->ci, &icap );
975 /* msg_Dbg( p_dec->p_fifo, "ImageCodecInitialize->%p size=%d (%d)\n",cres,icap.recordSize,icap.decompressRecordSize); */
978 memset( &cinfo, 0, sizeof( CodecInfo ) );
979 cres = p_dec->ImageCodecGetCodecInfo( p_dec->ci, &cinfo );
980 msg_Dbg( p_dec->p_fifo,
981 "Flags: compr: 0x%lx decomp: 0x%lx format: 0x%lx\n",
982 cinfo.compressFlags, cinfo.decompressFlags, cinfo.formatFlags );
983 msg_Dbg( p_dec->p_fifo, "quicktime_video: Codec name: %.*s\n",
984 ((unsigned char*)&cinfo.typeName)[0],
985 ((unsigned char*)&cinfo.typeName)+1 );
987 /* make a yuy2 gworld */
988 p_dec->OutBufferRect.top = 0;
989 p_dec->OutBufferRect.left = 0;
990 p_dec->OutBufferRect.right = p_bih->biWidth;
991 p_dec->OutBufferRect.bottom = p_bih->biHeight;
994 /* codec data FIXME use codec not SVQ3 */
995 msg_Dbg( p_dec->p_fifo, "vide = %d", i_vide );
996 id = malloc( sizeof( ImageDescription ) + ( i_vide - 70 ) );
997 id->idSize = sizeof( ImageDescription ) + ( i_vide - 70 );
998 id->cType = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
999 id->version = GetWBE ( p_vide + 0 );
1000 id->revisionLevel = GetWBE ( p_vide + 2 );
1001 id->vendor = GetDWBE( p_vide + 4 );
1002 id->temporalQuality = GetDWBE( p_vide + 8 );
1003 id->spatialQuality = GetDWBE( p_vide + 12 );
1004 id->width = GetWBE ( p_vide + 16 );
1005 id->height = GetWBE ( p_vide + 18 );
1006 id->hRes = GetDWBE( p_vide + 20 );
1007 id->vRes = GetDWBE( p_vide + 24 );
1008 id->dataSize = GetDWBE( p_vide + 28 );
1009 id->frameCount = GetWBE ( p_vide + 32 );
1010 memcpy( &id->name, p_vide + 34, 32 );
1011 id->depth = GetWBE ( p_vide + 66 );
1012 id->clutID = GetWBE ( p_vide + 68 );
1015 memcpy( ((char*)&id->clutID) + 2, p_vide + 70, i_vide - 70 );
1018 msg_Dbg( p_dec->p_fifo, "idSize=%ld ver=%d rev=%d vendor=%ld tempQ=%d "
1019 "spaQ=%d w=%d h=%d dpi=%d%d dataSize=%d frameCount=%d clutID=%d",
1020 id->idSize, id->version, id->revisionLevel, id->vendor,
1021 (int)id->temporalQuality, (int)id->spatialQuality,
1022 id->width, id->height,
1023 (int)id->hRes, (int)id->vRes,
1028 p_dec->framedescHandle =
1029 (ImageDescriptionHandle) p_dec->NewHandleClear( id->idSize );
1030 memcpy( *p_dec->framedescHandle, id, id->idSize );
1032 p_dec->plane = malloc( p_bih->biWidth * p_bih->biHeight * 3 );
1034 i_result = p_dec->QTNewGWorldFromPtr( &p_dec->OutBufferGWorld,
1035 /*pixel format of new GWorld==YUY2 */
1037 /* we should benchmark if yvu9 is
1038 * faster for svq3, too */
1039 &p_dec->OutBufferRect,
1042 p_bih->biWidth * 2 );
1044 msg_Dbg( p_dec->p_fifo, "NewGWorldFromPtr returned:%ld\n",
1045 65536 - ( i_result&0xffff ) );
1047 memset( &p_dec->decpar, 0, sizeof( CodecDecompressParams ) );
1048 p_dec->decpar.imageDescription = p_dec->framedescHandle;
1049 p_dec->decpar.startLine = 0;
1050 p_dec->decpar.stopLine = ( **p_dec->framedescHandle ).height;
1051 p_dec->decpar.frameNumber = 1;
1052 p_dec->decpar.matrixFlags = 0;
1053 p_dec->decpar.matrixType = 0;
1054 p_dec->decpar.matrix = 0;
1055 p_dec->decpar.capabilities = &p_dec->codeccap;
1056 p_dec->decpar.accuracy = codecNormalQuality;
1057 p_dec->decpar.srcRect = p_dec->OutBufferRect;
1058 p_dec->decpar.transferMode = srcCopy;
1059 p_dec->decpar.dstPixMap = **p_dec->GetGWorldPixMap( p_dec->OutBufferGWorld );/*destPixmap; */
1061 cres = p_dec->ImageCodecPreDecompress( p_dec->ci, &p_dec->decpar );
1062 msg_Dbg( p_dec->p_fifo,
1063 "quicktime_video: ImageCodecPreDecompress cres=0x%X\n",
1066 p_dec->p_vout = vout_Request( p_dec->p_fifo, NULL,
1067 p_bih->biWidth, p_bih->biHeight,
1068 VLC_FOURCC( 'Y', 'U', 'Y', '2' ),
1069 VOUT_ASPECT_FACTOR * p_bih->biWidth /
1072 if( !p_dec->p_vout )
1074 msg_Err( p_dec->p_fifo, "cannot get a vout" );
1078 p_dec->i_buffer = 1000*1000;
1079 p_dec->p_buffer = malloc( p_dec->i_buffer );
1081 vlc_mutex_unlock( lockval.p_address );
1086 Restore_LDT_Keeper( p_dec->ldt_fs );
1088 vlc_mutex_unlock( lockval.p_address );
1089 return VLC_EGENERIC;
1093 static void DecodeThreadVideo( vdec_thread_t *p_dec )
1095 BITMAPINFOHEADER *p_bih =
1096 (BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader;
1098 pes_packet_t *p_pes;
1099 vlc_value_t lockval;
1101 ComponentResult cres;
1103 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
1105 input_ExtractPES( p_dec->p_fifo, &p_pes );
1108 msg_Err( p_dec->p_fifo, "cannot get PES" );
1109 p_dec->p_fifo->b_error = 1;
1113 if( p_pes->i_pes_size > p_dec->i_buffer )
1115 p_dec->i_buffer = 3 * p_pes->i_pes_size / 2;
1116 free( p_dec->p_buffer );
1117 p_dec->p_buffer = malloc( p_dec->i_buffer );
1120 if( p_pes->i_pes_size > 0 && p_pes->i_pts > mdate() )
1122 GetPESData( p_dec->p_buffer, p_dec->i_buffer, p_pes );
1124 while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
1126 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
1130 msleep( VOUT_OUTMEM_SLEEP );
1133 p_dec->decpar.data = p_dec->p_buffer;
1134 p_dec->decpar.bufferSize = p_pes->i_pes_size;
1135 (**p_dec->framedescHandle).dataSize = p_pes->i_pes_size;
1137 vlc_mutex_lock( lockval.p_address );
1138 cres = p_dec->ImageCodecBandDecompress( p_dec->ci, &p_dec->decpar );
1139 vlc_mutex_unlock( lockval.p_address );
1141 ++p_dec->decpar.frameNumber;
1145 msg_Dbg( p_dec->p_fifo, "quicktime_video: ImageCodecBandDecompress"
1146 " cres=0x%X (-0x%X) %d :(\n",
1147 (int)cres,(int)-cres, (int)cres );
1150 memcpy( p_pic->p[0].p_pixels,
1152 p_bih->biWidth * p_bih->biHeight * 2 );
1154 vout_DatePicture( p_dec->p_vout, p_pic, p_pes->i_pts );
1155 vout_DisplayPicture( p_dec->p_vout, p_pic );
1158 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
1161 static void EndThreadVideo( vdec_thread_t *p_dec )
1163 msg_Dbg( p_dec->p_fifo, "QuickTime library video decoder closing" );
1164 free( p_dec->plane );
1165 vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
1168 FreeLibrary( p_dec->qtml );
1169 msg_Dbg( p_dec->p_fifo, "FreeLibrary ok." );
1173 Restore_LDT_Keeper( p_dec->ldt_fs );
1174 msg_Dbg( p_dec->p_fifo, "Restore_LDT_Keeper" );