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.8 2003/06/17 14:43:21 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 0x0011: /* DVI IMA */
293 case 0x6D730002: /* Microsoft ADPCM-ACM */
294 case 0x6D730011: /* DVI Intel IMAADPCM-ACM */
296 p_fifo->pf_run = RunDecoderAudio;
304 HMODULE WINAPI LoadLibraryA(LPCSTR);
305 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
306 int WINAPI FreeLibrary(HMODULE);
309 /****************************************************************************
310 ****************************************************************************
314 ****************************************************************************
315 ****************************************************************************/
317 static int InitThreadAudio ( adec_thread_t * );
318 static void DecodeThreadAudio ( adec_thread_t * );
319 static void EndThreadAudio ( adec_thread_t * );
321 static int RunDecoderAudio( decoder_fifo_t *p_fifo )
323 adec_thread_t *p_dec;
326 p_dec = malloc( sizeof( adec_thread_t ) );
329 msg_Err( p_fifo, "out of memory" );
330 DecoderError( p_fifo );
333 p_dec->p_fifo = p_fifo;
335 if( InitThreadAudio( p_dec ) != 0 )
337 DecoderError( p_fifo );
341 while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
343 DecodeThreadAudio( p_dec );
347 if( ( b_error = p_dec->p_fifo->b_error ) )
349 DecoderError( p_dec->p_fifo );
352 EndThreadAudio( p_dec );
361 static int InitThreadAudio( adec_thread_t *p_dec )
366 unsigned long WantedBufferSize;
367 unsigned long InputBufferSize = 0;
368 unsigned long OutputBufferSize = 0;
372 if( !( p_wf = (WAVEFORMATEX*)p_dec->p_fifo->p_waveformatex ) )
374 msg_Err( p_dec->p_fifo, "missing WAVEFORMATEX");
377 memcpy( fcc, &p_dec->p_fifo->i_fourcc, 4 );
379 /* get lock, avoid segfault */
380 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
381 vlc_mutex_lock( lockval.p_address );
386 p_dec->ldt_fs = Setup_LDT_Keeper();
388 msg_Dbg( p_dec->p_fifo, "trying to load `qtmlClient.dll'" );
389 if( !( p_dec->qtml = LoadLibraryA("qtmlClient.dll") ) )
391 msg_Err( p_dec->p_fifo, "cannot load qtmlClient.dll");
395 msg_Dbg( p_dec->p_fifo, "qtmlClient.dll loaded" );
397 /* (void*) to shut up gcc */
398 p_dec->InitializeQTML = (void*)InitializeQTML;
399 #endif /* SYS_DARWIN */
400 p_dec->SoundConverterOpen = (void*)SoundConverterOpen;
401 p_dec->SoundConverterClose = (void*)SoundConverterClose;
402 p_dec->SoundConverterSetInfo = (void*)SoundConverterSetInfo;
403 p_dec->SoundConverterGetBufferSizes = (void*)SoundConverterGetBufferSizes;
404 p_dec->SoundConverterConvertBuffer = (void*)SoundConverterConvertBuffer;
405 p_dec->SoundConverterBeginConversion= (void*)SoundConverterBeginConversion;
406 p_dec->SoundConverterEndConversion = (void*)SoundConverterEndConversion;
409 if( !p_dec->InitializeQTML ||
410 !p_dec->SoundConverterOpen || !p_dec->SoundConverterClose ||
411 !p_dec->SoundConverterSetInfo || !p_dec->SoundConverterGetBufferSizes ||
412 !p_dec->SoundConverterConvertBuffer ||
413 !p_dec->SoundConverterBeginConversion || !p_dec->SoundConverterEndConversion )
415 msg_Err( p_dec->p_fifo, "error getting qtmlClient.dll symbols");
419 if( ( i_error = p_dec->InitializeQTML( 6 + 16 ) ) )
421 msg_Dbg( p_dec->p_fifo, "error while InitializeQTML = %d", i_error );
426 /* input format settings */
427 p_dec->InputFormatInfo.flags = 0;
428 p_dec->InputFormatInfo.sampleCount = 0;
429 p_dec->InputFormatInfo.buffer = NULL;
430 p_dec->InputFormatInfo.reserved = 0;
431 p_dec->InputFormatInfo.numChannels = p_wf->nChannels;
432 p_dec->InputFormatInfo.sampleSize = p_wf->wBitsPerSample;
433 p_dec->InputFormatInfo.sampleRate = p_wf->nSamplesPerSec;
434 p_dec->InputFormatInfo.format = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
436 /* output format settings */
437 p_dec->OutputFormatInfo.flags = 0;
438 p_dec->OutputFormatInfo.sampleCount = 0;
439 p_dec->OutputFormatInfo.buffer = NULL;
440 p_dec->OutputFormatInfo.reserved = 0;
441 p_dec->OutputFormatInfo.numChannels = p_wf->nChannels;
442 p_dec->OutputFormatInfo.sampleSize = 16;
443 p_dec->OutputFormatInfo.sampleRate = p_wf->nSamplesPerSec;
444 p_dec->OutputFormatInfo.format = FCC( 'N', 'O', 'N', 'E' );
447 /* on OS X QT is not threadsafe */
448 vlc_mutex_lock( &p_dec->p_fifo->p_vlc->quicktime_lock );
451 i_error = p_dec->SoundConverterOpen( &p_dec->InputFormatInfo,
452 &p_dec->OutputFormatInfo,
453 &p_dec->myConverter );
456 msg_Err( p_dec->p_fifo, "error while SoundConverterOpen = %d", i_error );
460 if( p_wf->cbSize > 36 + 8 )
462 i_error = p_dec->SoundConverterSetInfo( p_dec->myConverter,
463 FCC( 'w', 'a', 'v', 'e' ),
464 ((uint8_t*)&p_wf[1]) + 36 + 8 );
465 msg_Dbg( p_dec->p_fifo, "error while SoundConverterSetInfo = %d", i_error );
468 WantedBufferSize = p_dec->OutputFormatInfo.numChannels * p_dec->OutputFormatInfo.sampleRate * 2;
469 p_dec->FramesToGet = 0;
470 i_error = p_dec->SoundConverterGetBufferSizes( p_dec->myConverter,
471 WantedBufferSize, &p_dec->FramesToGet,
472 &InputBufferSize, &OutputBufferSize );
474 msg_Dbg( p_dec->p_fifo, "WantedBufferSize=%li InputBufferSize=%li OutputBufferSize=%li FramesToGet=%li",
475 WantedBufferSize, InputBufferSize, OutputBufferSize, p_dec->FramesToGet );
477 p_dec->InFrameSize = (InputBufferSize + p_dec->FramesToGet - 1 ) / p_dec->FramesToGet;
478 p_dec->OutFrameSize = OutputBufferSize / p_dec->FramesToGet;
480 msg_Dbg( p_dec->p_fifo, "frame size %d -> %d", p_dec->InFrameSize, p_dec->OutFrameSize );
482 i_error = p_dec->SoundConverterBeginConversion( p_dec->myConverter );
485 msg_Err( p_dec->p_fifo, "error while SoundConverterBeginConversion = %d", i_error );
490 vlc_mutex_unlock( &p_dec->p_fifo->p_vlc->quicktime_lock );
493 p_dec->output_format.i_format = AOUT_FMT_S16_NE;
494 p_dec->output_format.i_rate = p_wf->nSamplesPerSec;
495 p_dec->output_format.i_physical_channels =
496 p_dec->output_format.i_original_channels =
497 pi_channels_maps[p_wf->nChannels];
498 aout_DateInit( &p_dec->date, p_dec->output_format.i_rate );
499 p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
501 &p_dec->output_format );
502 if( !p_dec->p_aout_input )
504 msg_Err( p_dec->p_fifo, "cannot create aout" );
509 p_dec->i_buffer_size = 100*1000;
510 p_dec->p_buffer = malloc( p_dec->i_buffer_size );
514 vlc_mutex_unlock( lockval.p_address );
519 Restore_LDT_Keeper( p_dec->ldt_fs );
521 vlc_mutex_unlock( lockval.p_address );
525 static void DecodeThreadAudio ( adec_thread_t *p_dec )
531 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
533 input_ExtractPES( p_dec->p_fifo, &p_pes );
536 msg_Err( p_dec->p_fifo, "cannot get PES" );
537 p_dec->p_fifo->b_error = 1;
540 /*if( p_dec->pts <= 0 )*/
542 p_dec->pts = p_pes->i_pts;
545 if( p_pes->i_pes_size > 0 && p_pes->i_pts > mdate() )
548 if( p_dec->i_buffer_size < p_dec->i_buffer + p_pes->i_pes_size )
550 p_dec->i_buffer_size = p_dec->i_buffer + p_pes->i_pes_size + 1024;
551 p_dec->p_buffer = realloc( p_dec->p_buffer,
552 p_dec->i_buffer_size );
555 GetPESData( &p_dec->p_buffer[p_dec->i_buffer],
556 p_dec->i_buffer_size - p_dec->i_buffer, p_pes );
557 p_dec->i_buffer += p_pes->i_pes_size;
559 if( p_dec->i_buffer > p_dec->InFrameSize )
561 int i_frames = p_dec->i_buffer / p_dec->InFrameSize;
562 long i_out_frames, i_out_bytes;
565 vlc_mutex_lock( lockval.p_address );
566 i_error = p_dec->SoundConverterConvertBuffer( p_dec->myConverter,
570 &i_out_frames, &i_out_bytes );
571 vlc_mutex_unlock( lockval.p_address );
573 /*msg_Dbg( p_dec->p_fifo, "decoded %d frames -> %ld frames (error=%d)",
574 i_frames, i_out_frames, i_error );
576 msg_Dbg( p_dec->p_fifo, "decoded %ld frames = %ld bytes", i_out_frames, i_out_bytes );*/
577 p_dec->i_buffer -= i_frames * p_dec->InFrameSize;
578 if( p_dec->i_buffer > 0 )
580 memmove( &p_dec->p_buffer[0],
581 &p_dec->p_buffer[i_frames * p_dec->InFrameSize],
585 if( i_out_frames > 0 )
587 aout_buffer_t *p_aout_buffer;
588 uint8_t *p_buff = p_dec->buffer_out;
590 /*msg_Dbg( p_dec->p_fifo, "pts=%lld date=%lld dateget=%lld",
591 p_dec->pts, mdate(), aout_DateGet( &p_dec->date ) );*/
593 if( p_dec->pts != 0 && p_dec->pts != aout_DateGet( &p_dec->date ) )
595 aout_DateSet( &p_dec->date, p_dec->pts );
597 else if( !aout_DateGet( &p_dec->date ) )
599 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
603 while( i_out_frames > 0 )
607 i_frames = __MIN( i_out_frames, 1000 );
608 p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout,
613 msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
614 p_dec->p_fifo->b_error = 1;
617 p_aout_buffer->start_date = aout_DateGet( &p_dec->date );
618 p_aout_buffer->end_date = aout_DateIncrement( &p_dec->date,
621 memcpy( p_aout_buffer->p_buffer,
623 p_aout_buffer->i_nb_bytes );
625 /*msg_Dbg( p_dec->p_fifo, "==> start=%lld end=%lld date=%lld",
626 p_aout_buffer->start_date, p_aout_buffer->end_date, mdate() );*/
627 aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_aout_buffer );
628 /*msg_Dbg( p_dec->p_fifo, "s1=%d s2=%d", i_framesperchannels, p_aout_buffer->i_nb_samples );
630 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 );*/
631 p_buff += i_frames * 4;
632 i_out_frames -= i_frames;
640 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
643 static void EndThreadAudio( adec_thread_t *p_dec )
647 unsigned long ConvertedFrames=0;
648 unsigned long ConvertedBytes=0;
650 /* get lock, avoid segfault */
651 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
652 vlc_mutex_lock( lockval.p_address );
654 i_error = p_dec->SoundConverterEndConversion( p_dec->myConverter, NULL, &ConvertedFrames, &ConvertedBytes );
655 msg_Dbg( p_dec->p_fifo, "SoundConverterEndConversion => %d", i_error );
657 i_error = p_dec->SoundConverterClose( p_dec->myConverter );
658 msg_Dbg( p_dec->p_fifo, "SoundConverterClose => %d", i_error );
661 FreeLibrary( p_dec->qtml );
662 msg_Dbg( p_dec->p_fifo, "FreeLibrary ok." ); */
664 vlc_mutex_unlock( lockval.p_address );
667 Restore_LDT_Keeper( p_dec->ldt_fs );
668 msg_Dbg( p_dec->p_fifo, "Restore_LDT_Keeper" ); */
673 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
677 /****************************************************************************
678 ****************************************************************************
682 ****************************************************************************
683 ****************************************************************************/
685 static int InitThreadVideo ( vdec_thread_t * );
686 static void DecodeThreadVideo ( vdec_thread_t * );
687 static void EndThreadVideo ( vdec_thread_t * );
689 static int RunDecoderVideo( decoder_fifo_t *p_fifo )
691 vdec_thread_t *p_dec;
694 p_dec = malloc( sizeof( vdec_thread_t ) );
697 msg_Err( p_fifo, "out of memory" );
698 DecoderError( p_fifo );
701 p_dec->p_fifo = p_fifo;
703 if( InitThreadVideo( p_dec ) != 0 )
705 DecoderError( p_fifo );
709 while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
711 DecodeThreadVideo( p_dec );
715 if( ( b_error = p_dec->p_fifo->b_error ) )
717 DecoderError( p_dec->p_fifo );
720 EndThreadVideo( p_dec );
730 * InitThreadVideo: load and init library
733 static int InitThreadVideo( vdec_thread_t *p_dec )
737 ComponentDescription desc;
739 ComponentResult cres;
740 ImageSubCodecDecompressCapabilities icap; /* for ImageCodecInitialize() */
741 CodecInfo cinfo; /* for ImageCodecGetCodecInfo() */
742 ImageDescription *id;
744 BITMAPINFOHEADER *p_bih;
749 if( !( p_bih = (BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader ) )
751 msg_Err( p_dec->p_fifo, "missing BITMAPINFOHEADER !!" );
754 i_vide = p_bih->biSize - sizeof( BITMAPINFOHEADER );
755 p_vide = (uint8_t*)&p_bih[1];
756 if( i_vide <= 0 || p_vide == NULL )
758 msg_Err( p_dec->p_fifo, "invalid BITMAPINFOHEADER !!" );
761 memcpy( fcc, &p_dec->p_fifo->i_fourcc, 4 );
762 msg_Dbg( p_dec->p_fifo, "quicktime_video %4.4s %dx%d", fcc, p_bih->biWidth, p_bih->biHeight );
764 /* get lock, avoid segfault */
765 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
766 vlc_mutex_lock( lockval.p_address );
771 p_dec->ldt_fs = Setup_LDT_Keeper();
773 msg_Dbg( p_dec->p_fifo, "trying to load `qtmlClient.dll'" );
774 if( !( p_dec->qtml = LoadLibraryA("qtmlClient.dll") ) )
776 msg_Err( p_dec->p_fifo, "cannot load qtmlClient.dll");
780 msg_Dbg( p_dec->p_fifo, "qtmlClient.dll loaded" );
782 /* (void*) to shut up gcc */
783 p_dec->InitializeQTML = (void*)InitializeQTML;
784 #endif /* SYS_DARWIN */
785 p_dec->FindNextComponent = (void*)FindNextComponent;
786 p_dec->OpenComponent = (void*)OpenComponent;
787 p_dec->ImageCodecInitialize = (void*)ImageCodecInitialize;
788 p_dec->ImageCodecGetCodecInfo = (void*)ImageCodecGetCodecInfo;
789 p_dec->ImageCodecPreDecompress = (void*)ImageCodecPreDecompress;
790 p_dec->ImageCodecBandDecompress = (void*)ImageCodecBandDecompress;
791 p_dec->GetGWorldPixMap = (void*)GetGWorldPixMap;
792 p_dec->QTNewGWorldFromPtr = (void*)QTNewGWorldFromPtr;
793 p_dec->NewHandleClear = (void*)NewHandleClear;
796 /* some sanity check */
797 if( !p_dec->InitializeQTML ||
798 !p_dec->FindNextComponent ||
799 !p_dec->OpenComponent ||
800 !p_dec->ImageCodecBandDecompress )
802 msg_Err( p_dec->p_fifo, "error getting qtmlClient.dll symbols");
806 if( ( i_result = p_dec->InitializeQTML( 6 + 16 ) ) )
808 msg_Dbg( p_dec->p_fifo, "error while InitializeQTML = %d", i_result );
813 /* init ComponentDescription */
814 memset( &desc, 0, sizeof( ComponentDescription ) );
815 desc.componentType = FCC( 'i', 'm', 'd', 'c' );
816 desc.componentSubType = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
817 desc.componentManufacturer = 0;
818 desc.componentFlags = 0;
819 desc.componentFlagsMask = 0;
821 if( !( prev = p_dec->FindNextComponent( NULL, &desc ) ) )
823 msg_Err( p_dec->p_fifo, "cannot find requested component" );
826 msg_Dbg( p_dec->p_fifo, "component id=0x%p", prev );
828 p_dec->ci = p_dec->OpenComponent( prev );
829 msg_Dbg( p_dec->p_fifo, "component instance p=0x%p", p_dec->ci );
831 memset( &icap, 0, sizeof( ImageSubCodecDecompressCapabilities ) );
832 cres = p_dec->ImageCodecInitialize( p_dec->ci, &icap );
833 /* msg_Dbg( p_dec->p_fifo, "ImageCodecInitialize->%p size=%d (%d)\n",cres,icap.recordSize,icap.decompressRecordSize); */
836 memset( &cinfo, 0, sizeof( CodecInfo ) );
837 cres = p_dec->ImageCodecGetCodecInfo( p_dec->ci, &cinfo );
838 msg_Dbg( p_dec->p_fifo, "Flags: compr: 0x%lx decomp: 0x%lx format: 0x%lx\n",
839 cinfo.compressFlags, cinfo.decompressFlags, cinfo.formatFlags);
840 msg_Dbg( p_dec->p_fifo, "quicktime_video: Codec name: %.*s\n", ((unsigned char*)&cinfo.typeName)[0],
841 ((unsigned char*)&cinfo.typeName)+1);
843 /* make a yuy2 gworld */
844 p_dec->OutBufferRect.top = 0;
845 p_dec->OutBufferRect.left = 0;
846 p_dec->OutBufferRect.right = p_bih->biWidth;
847 p_dec->OutBufferRect.bottom = p_bih->biHeight;
850 /* codec data FIXME use codec not SVQ3 */
851 msg_Dbg( p_dec->p_fifo, "vide = %d", i_vide );
852 id = malloc( sizeof( ImageDescription ) + ( i_vide - 70 ) );
853 id->idSize = sizeof( ImageDescription ) + ( i_vide - 70 );
854 id->cType = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
855 id->version = GetWBE ( p_vide + 0 );
856 id->revisionLevel = GetWBE ( p_vide + 2 );
857 id->vendor = GetDWBE( p_vide + 4 );
858 id->temporalQuality = GetDWBE( p_vide + 8 );
859 id->spatialQuality = GetDWBE( p_vide + 12 );
860 id->width = GetWBE ( p_vide + 16 );
861 id->height = GetWBE ( p_vide + 18 );
862 id->hRes = GetDWBE( p_vide + 20 );
863 id->vRes = GetDWBE( p_vide + 24 );
864 id->dataSize = GetDWBE( p_vide + 28 );
865 id->frameCount = GetWBE ( p_vide + 32 );
866 memcpy( &id->name, p_vide + 34, 32 );
867 id->depth = GetWBE ( p_vide + 66 );
868 id->clutID = GetWBE ( p_vide + 68 );
871 memcpy( ((char*)&id->clutID) + 2, p_vide + 70, i_vide - 70 );
874 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",
875 id->idSize, id->version, id->revisionLevel, id->vendor,
876 (int)id->temporalQuality, (int)id->spatialQuality,
877 id->width, id->height,
878 (int)id->hRes, (int)id->vRes,
883 p_dec->framedescHandle = (ImageDescriptionHandle) p_dec->NewHandleClear( id->idSize );
884 memcpy( *p_dec->framedescHandle, id, id->idSize );
886 p_dec->plane = malloc( p_bih->biWidth * p_bih->biHeight * 3 );
888 i_result = p_dec->QTNewGWorldFromPtr( &p_dec->OutBufferGWorld,
889 kYUVSPixelFormat, /*pixel format of new GWorld==YUY2 */
890 &p_dec->OutBufferRect, /*we should benchmark if yvu9 is faster for svq3, too */
893 p_bih->biWidth * 2 );
895 msg_Dbg( p_dec->p_fifo, "NewGWorldFromPtr returned:%ld\n", 65536-( i_result&0xffff ) );
897 memset( &p_dec->decpar, 0, sizeof( CodecDecompressParams ) );
898 p_dec->decpar.imageDescription = p_dec->framedescHandle;
899 p_dec->decpar.startLine = 0;
900 p_dec->decpar.stopLine = ( **p_dec->framedescHandle ).height;
901 p_dec->decpar.frameNumber = 1;
902 p_dec->decpar.matrixFlags = 0;
903 p_dec->decpar.matrixType = 0;
904 p_dec->decpar.matrix = 0;
905 p_dec->decpar.capabilities = &p_dec->codeccap;
906 p_dec->decpar.accuracy = codecNormalQuality;
907 p_dec->decpar.srcRect = p_dec->OutBufferRect;
908 p_dec->decpar.transferMode = srcCopy;
909 p_dec->decpar.dstPixMap = **p_dec->GetGWorldPixMap( p_dec->OutBufferGWorld );/*destPixmap; */
911 cres = p_dec->ImageCodecPreDecompress( p_dec->ci, &p_dec->decpar );
912 msg_Dbg( p_dec->p_fifo, "quicktime_video: ImageCodecPreDecompress cres=0x%X\n", (int)cres );
914 p_dec->p_vout = vout_Request( p_dec->p_fifo, NULL,
915 p_bih->biWidth, p_bih->biHeight,
916 VLC_FOURCC( 'Y', 'U', 'Y', '2' ),
917 VOUT_ASPECT_FACTOR * p_bih->biWidth / p_bih->biHeight );
921 msg_Err( p_dec->p_fifo, "cannot get a vout" );
925 p_dec->i_buffer = 1000*1000;
926 p_dec->p_buffer = malloc( p_dec->i_buffer );
928 vlc_mutex_unlock( lockval.p_address );
933 Restore_LDT_Keeper( p_dec->ldt_fs );
935 vlc_mutex_unlock( lockval.p_address );
940 static void DecodeThreadVideo( vdec_thread_t *p_dec )
942 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader;
946 ComponentResult cres;
948 var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
950 input_ExtractPES( p_dec->p_fifo, &p_pes );
953 msg_Err( p_dec->p_fifo, "cannot get PES" );
954 p_dec->p_fifo->b_error = 1;
958 if( p_pes->i_pes_size > p_dec->i_buffer )
960 p_dec->i_buffer = 3 * p_pes->i_pes_size / 2;
961 free( p_dec->p_buffer );
962 p_dec->p_buffer = malloc( p_dec->i_buffer );
965 if( p_pes->i_pes_size > 0 && p_pes->i_pts > mdate() )
967 GetPESData( p_dec->p_buffer, p_dec->i_buffer, p_pes );
969 while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
971 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
975 msleep( VOUT_OUTMEM_SLEEP );
978 p_dec->decpar.data = p_dec->p_buffer;
979 p_dec->decpar.bufferSize = p_pes->i_pes_size;
980 (**p_dec->framedescHandle).dataSize = p_pes->i_pes_size;
982 vlc_mutex_lock( lockval.p_address );
983 cres = p_dec->ImageCodecBandDecompress( p_dec->ci, &p_dec->decpar );
984 vlc_mutex_unlock( lockval.p_address );
986 ++p_dec->decpar.frameNumber;
990 msg_Dbg( p_dec->p_fifo,
991 "quicktime_video: ImageCodecBandDecompress cres=0x%X (-0x%X) %d :(\n",
992 (int)cres,(int)-cres, (int)cres );
995 memcpy( p_pic->p[0].p_pixels,
997 p_bih->biWidth * p_bih->biHeight * 2 );
999 vout_DatePicture( p_dec->p_vout, p_pic, p_pes->i_pts );
1000 vout_DisplayPicture( p_dec->p_vout, p_pic );
1003 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
1006 static void EndThreadVideo( vdec_thread_t *p_dec )
1008 msg_Dbg( p_dec->p_fifo, "QuickTime library video decoder closing" );
1009 free( p_dec->plane );
1010 vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
1013 FreeLibrary( p_dec->qtml );
1014 msg_Dbg( p_dec->p_fifo, "FreeLibrary ok." );
1018 Restore_LDT_Keeper( p_dec->ldt_fs );
1019 msg_Dbg( p_dec->p_fifo, "Restore_LDT_Keeper" );