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 $
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 *,
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;
121 unsigned int InFrameSize;
122 unsigned int OutFrameSize;
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;
133 unsigned int i_buffer;
134 unsigned int i_buffer_size;
137 uint8_t buffer_out[1000000]; /* FIXME */
142 /* Input properties */
143 decoder_fifo_t *p_fifo;
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,
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,
165 const Rect *boundsRect,
167 /*GDHandle*/ void *aGDevice, /*unused*/
171 OSErr (*NewHandleClear) ( Size byteCount );
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
178 ImageDescriptionHandle framedescHandle;
180 CodecDecompressParams decpar; /* for ImageCodecPreDecompress() */
181 CodecCapabilities codeccap; /* for decpar */
184 /* Output properties */
185 vout_thread_t * p_vout;
190 unsigned int i_buffer;
195 static int pi_channels_maps[6] =
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
207 static uint16_t GetWBE( uint8_t *p_buff )
209 return( (p_buff[0]<<8) + p_buff[1] );
212 static uint32_t GetDWBE( uint8_t *p_buff )
214 return( (p_buff[0] << 24) + ( p_buff[1] <<16 ) +
215 ( p_buff[2] <<8 ) + p_buff[3] );
219 static int GetPESData( uint8_t *p_buf, int i_max, pes_packet_t *p_pes )
224 data_packet_t *p_data;
227 p_data = p_pes->p_first;
228 while( p_data != NULL && i_count < i_max )
231 i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
237 p_data->p_payload_start,
241 p_data = p_data->p_next;
246 if( i_count < i_max )
248 memset( p_buf, 0, i_max - i_count );
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
258 *****************************************************************************/
259 static int OpenDecoder( vlc_object_t *p_this )
261 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
263 switch( p_fifo->i_fourcc )
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;
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 */
298 p_fifo->pf_run = RunDecoderAudio;
306 HMODULE WINAPI LoadLibraryA(LPCSTR);
307 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
308 int WINAPI FreeLibrary(HMODULE);
311 /****************************************************************************
312 ****************************************************************************
316 ****************************************************************************
317 ****************************************************************************/
319 static int InitThreadAudio ( adec_thread_t * );
320 static void DecodeThreadAudio ( adec_thread_t * );
321 static void EndThreadAudio ( adec_thread_t * );
323 static int RunDecoderAudio( decoder_fifo_t *p_fifo )
325 adec_thread_t *p_dec;
328 p_dec = malloc( sizeof( adec_thread_t ) );
331 msg_Err( p_fifo, "out of memory" );
332 DecoderError( p_fifo );
335 p_dec->p_fifo = p_fifo;
337 if( InitThreadAudio( p_dec ) != 0 )
339 DecoderError( p_fifo );
343 while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
345 DecodeThreadAudio( p_dec );
349 if( ( b_error = p_dec->p_fifo->b_error ) )
351 DecoderError( p_dec->p_fifo );
354 EndThreadAudio( p_dec );
363 static int InitThreadAudio( adec_thread_t *p_dec )
368 unsigned long WantedBufferSize;
369 unsigned long InputBufferSize = 0;
370 unsigned long OutputBufferSize = 0;
374 if( !( p_wf = (WAVEFORMATEX*)p_dec->p_fifo->p_waveformatex ) )
376 msg_Err( p_dec->p_fifo, "missing WAVEFORMATEX");
379 memcpy( fcc, &p_dec->p_fifo->i_fourcc, 4 );
381 /* get lock, avoid segfault */
382 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
383 vlc_mutex_lock( lockval.p_address );
388 p_dec->ldt_fs = Setup_LDT_Keeper();
390 msg_Dbg( p_dec->p_fifo, "trying to load `qtmlClient.dll'" );
391 if( !( p_dec->qtml = LoadLibraryA("qtmlClient.dll") ) )
393 msg_Err( p_dec->p_fifo, "cannot load qtmlClient.dll");
397 msg_Dbg( p_dec->p_fifo, "qtmlClient.dll loaded" );
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;
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 )
417 msg_Err( p_dec->p_fifo, "error getting qtmlClient.dll symbols");
421 if( ( i_error = p_dec->InitializeQTML( 6 + 16 ) ) )
423 msg_Dbg( p_dec->p_fifo, "error while InitializeQTML = %d", i_error );
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] );
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' );
449 /* on OS X QT is not threadsafe */
450 vlc_mutex_lock( &p_dec->p_fifo->p_vlc->quicktime_lock );
453 i_error = p_dec->SoundConverterOpen( &p_dec->InputFormatInfo,
454 &p_dec->OutputFormatInfo,
455 &p_dec->myConverter );
458 msg_Dbg( p_dec->p_fifo, "error while SoundConverterOpen = %d", i_error );
462 if( p_wf->cbSize > 36 + 8 )
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 );
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 );
476 msg_Dbg( p_dec->p_fifo, "WantedBufferSize=%li InputBufferSize=%li OutputBufferSize=%li FramesToGet=%li",
477 WantedBufferSize, InputBufferSize, OutputBufferSize, p_dec->FramesToGet );
479 p_dec->InFrameSize = (InputBufferSize + p_dec->FramesToGet - 1 ) / p_dec->FramesToGet;
480 p_dec->OutFrameSize = OutputBufferSize / p_dec->FramesToGet;
482 msg_Dbg( p_dec->p_fifo, "frame size %d -> %d", p_dec->InFrameSize, p_dec->OutFrameSize );
484 i_error = p_dec->SoundConverterBeginConversion( p_dec->myConverter );
487 msg_Err( p_dec->p_fifo, "error while SoundConverterBeginConversion = %d", i_error );
492 vlc_mutex_unlock( &p_dec->p_fifo->p_vlc->quicktime_lock );
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,
503 &p_dec->output_format );
504 if( !p_dec->p_aout_input )
506 msg_Err( p_dec->p_fifo, "cannot create aout" );
511 p_dec->i_buffer_size = 100*1000;
512 p_dec->p_buffer = malloc( p_dec->i_buffer_size );
516 vlc_mutex_unlock( lockval.p_address );
521 Restore_LDT_Keeper( p_dec->ldt_fs );
523 vlc_mutex_unlock( lockval.p_address );
527 static void DecodeThreadAudio ( adec_thread_t *p_dec )
533 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
535 input_ExtractPES( p_dec->p_fifo, &p_pes );
538 msg_Err( p_dec->p_fifo, "cannot get PES" );
539 p_dec->p_fifo->b_error = 1;
542 /*if( p_dec->pts <= 0 )*/
544 p_dec->pts = p_pes->i_pts;
547 if( p_pes->i_pes_size > 0 && p_pes->i_pts > mdate() )
550 if( p_dec->i_buffer_size < p_dec->i_buffer + p_pes->i_pes_size )
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 );
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;
561 if( p_dec->i_buffer > p_dec->InFrameSize )
563 int i_frames = p_dec->i_buffer / p_dec->InFrameSize;
564 long i_out_frames, i_out_bytes;
567 vlc_mutex_lock( lockval.p_address );
568 i_error = p_dec->SoundConverterConvertBuffer( p_dec->myConverter,
572 &i_out_frames, &i_out_bytes );
573 vlc_mutex_unlock( lockval.p_address );
575 /*msg_Dbg( p_dec->p_fifo, "decoded %d frames -> %ld frames (error=%d)",
576 i_frames, i_out_frames, i_error );
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 )
582 memmove( &p_dec->p_buffer[0],
583 &p_dec->p_buffer[i_frames * p_dec->InFrameSize],
587 if( i_out_frames > 0 )
589 aout_buffer_t *p_aout_buffer;
590 uint8_t *p_buff = p_dec->buffer_out;
592 /*msg_Dbg( p_dec->p_fifo, "pts=%lld date=%lld dateget=%lld",
593 p_dec->pts, mdate(), aout_DateGet( &p_dec->date ) );*/
595 if( p_dec->pts != 0 && 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 );
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 );
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;
642 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
645 static void EndThreadAudio( adec_thread_t *p_dec )
649 unsigned long ConvertedFrames=0;
650 unsigned long ConvertedBytes=0;
652 /* get lock, avoid segfault */
653 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
654 vlc_mutex_lock( lockval.p_address );
656 i_error = p_dec->SoundConverterEndConversion( p_dec->myConverter, NULL, &ConvertedFrames, &ConvertedBytes );
657 msg_Dbg( p_dec->p_fifo, "SoundConverterEndConversion => %d", i_error );
659 i_error = p_dec->SoundConverterClose( p_dec->myConverter );
660 msg_Dbg( p_dec->p_fifo, "SoundConverterClose => %d", i_error );
663 FreeLibrary( p_dec->qtml );
664 msg_Dbg( p_dec->p_fifo, "FreeLibrary ok." ); */
666 vlc_mutex_unlock( lockval.p_address );
669 Restore_LDT_Keeper( p_dec->ldt_fs );
670 msg_Dbg( p_dec->p_fifo, "Restore_LDT_Keeper" ); */
675 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
679 /****************************************************************************
680 ****************************************************************************
684 ****************************************************************************
685 ****************************************************************************/
687 static int InitThreadVideo ( vdec_thread_t * );
688 static void DecodeThreadVideo ( vdec_thread_t * );
689 static void EndThreadVideo ( vdec_thread_t * );
691 static int RunDecoderVideo( decoder_fifo_t *p_fifo )
693 vdec_thread_t *p_dec;
696 p_dec = malloc( sizeof( vdec_thread_t ) );
699 msg_Err( p_fifo, "out of memory" );
700 DecoderError( p_fifo );
703 p_dec->p_fifo = p_fifo;
705 if( InitThreadVideo( p_dec ) != 0 )
707 DecoderError( p_fifo );
711 while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
713 DecodeThreadVideo( p_dec );
717 if( ( b_error = p_dec->p_fifo->b_error ) )
719 DecoderError( p_dec->p_fifo );
722 EndThreadVideo( p_dec );
732 * InitThreadVideo: load and init library
735 static int InitThreadVideo( vdec_thread_t *p_dec )
739 ComponentDescription desc;
741 ComponentResult cres;
742 ImageSubCodecDecompressCapabilities icap; /* for ImageCodecInitialize() */
743 CodecInfo cinfo; /* for ImageCodecGetCodecInfo() */
744 ImageDescription *id;
746 BITMAPINFOHEADER *p_bih;
751 if( !( p_bih = (BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader ) )
753 msg_Err( p_dec->p_fifo, "missing BITMAPINFOHEADER !!" );
756 i_vide = p_bih->biSize - sizeof( BITMAPINFOHEADER );
757 p_vide = (uint8_t*)&p_bih[1];
758 if( i_vide <= 0 || p_vide == NULL )
760 msg_Err( p_dec->p_fifo, "invalid BITMAPINFOHEADER !!" );
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 );
766 /* get lock, avoid segfault */
767 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
768 vlc_mutex_lock( lockval.p_address );
773 p_dec->ldt_fs = Setup_LDT_Keeper();
775 msg_Dbg( p_dec->p_fifo, "trying to load `qtmlClient.dll'" );
776 if( !( p_dec->qtml = LoadLibraryA("qtmlClient.dll") ) )
778 msg_Err( p_dec->p_fifo, "cannot load qtmlClient.dll");
782 msg_Dbg( p_dec->p_fifo, "qtmlClient.dll loaded" );
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;
798 /* some sanity check */
799 if( !p_dec->InitializeQTML ||
800 !p_dec->FindNextComponent ||
801 !p_dec->OpenComponent ||
802 !p_dec->ImageCodecBandDecompress )
804 msg_Err( p_dec->p_fifo, "error getting qtmlClient.dll symbols");
808 if( ( i_result = p_dec->InitializeQTML( 6 + 16 ) ) )
810 msg_Dbg( p_dec->p_fifo, "error while InitializeQTML = %d", i_result );
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;
823 if( !( prev = p_dec->FindNextComponent( NULL, &desc ) ) )
825 msg_Err( p_dec->p_fifo, "cannot find requested component" );
828 msg_Dbg( p_dec->p_fifo, "component id=0x%p", prev );
830 p_dec->ci = p_dec->OpenComponent( prev );
831 msg_Dbg( p_dec->p_fifo, "component instance p=0x%p", p_dec->ci );
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); */
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);
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;
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 );
873 memcpy( ((char*)&id->clutID) + 2, p_vide + 70, i_vide - 70 );
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,
885 p_dec->framedescHandle = (ImageDescriptionHandle) p_dec->NewHandleClear( id->idSize );
886 memcpy( *p_dec->framedescHandle, id, id->idSize );
888 p_dec->plane = malloc( p_bih->biWidth * p_bih->biHeight * 3 );
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 */
895 p_bih->biWidth * 2 );
897 msg_Dbg( p_dec->p_fifo, "NewGWorldFromPtr returned:%ld\n", 65536-( i_result&0xffff ) );
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; */
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 );
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 );
923 msg_Err( p_dec->p_fifo, "cannot get a vout" );
927 p_dec->i_buffer = 1000*1000;
928 p_dec->p_buffer = malloc( p_dec->i_buffer );
930 vlc_mutex_unlock( lockval.p_address );
935 Restore_LDT_Keeper( p_dec->ldt_fs );
937 vlc_mutex_unlock( lockval.p_address );
942 static void DecodeThreadVideo( vdec_thread_t *p_dec )
944 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader;
948 ComponentResult cres;
950 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
952 input_ExtractPES( p_dec->p_fifo, &p_pes );
955 msg_Err( p_dec->p_fifo, "cannot get PES" );
956 p_dec->p_fifo->b_error = 1;
960 if( p_pes->i_pes_size > p_dec->i_buffer )
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 );
967 if( p_pes->i_pes_size > 0 && p_pes->i_pts > mdate() )
969 GetPESData( p_dec->p_buffer, p_dec->i_buffer, p_pes );
971 while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
973 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
977 msleep( VOUT_OUTMEM_SLEEP );
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;
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 );
988 ++p_dec->decpar.frameNumber;
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 );
997 memcpy( p_pic->p[0].p_pixels,
999 p_bih->biWidth * p_bih->biHeight * 2 );
1001 vout_DatePicture( p_dec->p_vout, p_pic, p_pes->i_pts );
1002 vout_DisplayPicture( p_dec->p_vout, p_pic );
1005 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
1008 static void EndThreadVideo( vdec_thread_t *p_dec )
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 );
1015 FreeLibrary( p_dec->qtml );
1016 msg_Dbg( p_dec->p_fifo, "FreeLibrary ok." );
1020 Restore_LDT_Keeper( p_dec->ldt_fs );
1021 msg_Dbg( p_dec->p_fifo, "Restore_LDT_Keeper" );