Avcodec: deactivate DR for Camtasia
[vlc.git] / modules / codec / quicktime.c
blob55f9bf076355117a3ef3d841086f1dcf65a65533
1 /*****************************************************************************
2 * quicktime.c: a quicktime decoder that uses the QT library/dll
3 *****************************************************************************
4 * Copyright (C) 2003, 2008 - 2009 the VideoLAN team
5 * $Id$
7 * Authors: Laurent Aimar <fenrir at via.ecp.fr>
8 * Derk-Jan Hartman <hartman at videolan.org>>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_aout.h>
36 #include <vlc_codec.h>
38 #if !defined (__APPLE__) && !defined(WIN32)
39 # define LOADER 1
40 #endif
42 #ifdef __APPLE__
43 #include <QuickTime/QuickTimeComponents.h>
44 #include <QuickTime/Movies.h>
45 #include <QuickTime/ImageCodec.h>
46 #endif
48 /* for windows do we require Quicktime compents header? */
49 #ifdef LOADER
50 #include "qtx/qtxsdk/components.h"
51 #include "wine/windef.h"
52 #include "ldt_keeper.h"
54 HMODULE WINAPI LoadLibraryA(LPCSTR);
55 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
56 int WINAPI FreeLibrary(HMODULE);
58 #endif
60 /*****************************************************************************
61 * Module descriptor
62 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close( vlc_object_t * );
66 vlc_module_begin ()
67 set_description( N_("QuickTime library decoder") )
68 set_capability( "decoder", 0 )
69 set_category( CAT_INPUT )
70 set_subcategory( SUBCAT_INPUT_VCODEC )
71 set_callbacks( Open, Close )
73 vlc_module_end ()
76 /*****************************************************************************
77 * Local prototypes
78 *****************************************************************************/
79 static int OpenAudio( decoder_t * );
80 static int OpenVideo( decoder_t * );
82 static aout_buffer_t *DecodeAudio( decoder_t *, block_t ** );
83 #ifndef WIN32
84 static picture_t *DecodeVideo( decoder_t *, block_t ** );
85 #endif
87 #define FCC( a, b , c, d ) \
88 ((uint32_t)( ((a)<<24)|((b)<<16)|((c)<<8)|(d)))
90 #ifndef __APPLE__
91 typedef struct OpaqueSoundConverter* SoundConverter;
92 #ifndef LOADER
93 typedef long OSType;
94 typedef int OSErr;
95 #endif
96 typedef unsigned long UnsignedFixed;
97 typedef uint8_t Byte;
99 typedef struct SoundComponentData {
100 long flags;
101 OSType format;
102 short numChannels;
103 short sampleSize;
104 UnsignedFixed sampleRate;
105 long sampleCount;
106 Byte * buffer;
107 long reserved;
108 } SoundComponentData;
110 #endif /* __APPLE__ */
112 struct decoder_sys_t
114 /* library */
115 #ifndef __APPLE__
116 #ifdef LOADER
117 ldt_fs_t *ldt_fs;
118 #endif /* LOADER */
120 HMODULE qtml;
121 HINSTANCE qts;
122 OSErr (*InitializeQTML) ( long flags );
123 OSErr (*TerminateQTML) ( void );
124 #endif /* __APPLE__ */
126 /* Audio */
127 int (*SoundConverterOpen) ( const SoundComponentData *,
128 const SoundComponentData *,
129 SoundConverter* );
130 int (*SoundConverterClose) ( SoundConverter );
131 int (*SoundConverterSetInfo) ( SoundConverter , OSType, void * );
132 int (*SoundConverterGetBufferSizes) ( SoundConverter, unsigned long,
133 unsigned long*, unsigned long*,
134 unsigned long* );
135 int (*SoundConverterBeginConversion)( SoundConverter );
136 int (*SoundConverterEndConversion) ( SoundConverter, void *,
137 unsigned long *, unsigned long *);
138 int (*SoundConverterConvertBuffer) ( SoundConverter, const void *,
139 unsigned long, void *,
140 unsigned long *, unsigned long * );
141 SoundConverter myConverter;
142 SoundComponentData InputFormatInfo, OutputFormatInfo;
144 unsigned long FramesToGet;
145 unsigned int InFrameSize;
146 unsigned int OutFrameSize;
148 #ifndef WIN32
149 /* Video */
150 Component (*FindNextComponent)
151 ( Component prev, ComponentDescription* desc );
153 ComponentInstance (*OpenComponent)
154 ( Component c );
156 ComponentResult (*ImageCodecInitialize)
157 ( ComponentInstance ci, ImageSubCodecDecompressCapabilities * cap);
159 ComponentResult (*ImageCodecGetCodecInfo)
160 ( ComponentInstance ci, CodecInfo *info );
162 ComponentResult (*ImageCodecPreDecompress)
163 ( ComponentInstance ci, CodecDecompressParams * params );
165 ComponentResult (*ImageCodecBandDecompress)
166 ( ComponentInstance ci, CodecDecompressParams * params );
168 PixMapHandle (*GetGWorldPixMap)
169 ( GWorldPtr offscreenGWorld );
171 OSErr (*QTNewGWorldFromPtr)
172 ( GWorldPtr *gw, OSType pixelFormat, const Rect *boundsRect,
173 CTabHandle cTable, /*GDHandle*/ void *aGDevice, /*unused*/
174 GWorldFlags flags, void *baseAddr, long rowBytes );
176 OSErr (*NewHandleClear)( Size byteCount );
178 ComponentInstance ci;
179 Rect OutBufferRect; /* the dimensions of our GWorld */
180 GWorldPtr OutBufferGWorld; /* a GWorld is some kind of
181 description for a drawing
182 environment */
183 ImageDescriptionHandle framedescHandle;
185 CodecDecompressParams decpar; /* for ImageCodecPreDecompress()*/
186 CodecCapabilities codeccap; /* for decpar */
187 #endif
189 /* Output properties */
190 uint8_t * plane;
191 mtime_t pts;
192 date_t date;
194 int i_late; /* video */
196 /* buffer */
197 unsigned int i_buffer;
198 unsigned int i_buffer_size;
199 uint8_t *p_buffer;
201 /* Audio only */
202 uint8_t out_buffer[1000000]; /* FIXME */
203 int i_out_frames;
204 int i_out;
207 static const int pi_channels_maps[6] =
210 AOUT_CHAN_CENTER,
211 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
212 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
213 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
214 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
215 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
218 static int QTAudioInit( decoder_t * );
219 #ifndef WIN32
220 static int QTVideoInit( decoder_t * );
221 #endif
223 /*****************************************************************************
224 * Open: probe the decoder and return score
225 *****************************************************************************
226 * Tries to launch a decoder and return score so that the interface is able
227 * to choose.
228 *****************************************************************************/
229 static int Open( vlc_object_t *p_this )
231 decoder_t *p_dec = (decoder_t*)p_this;
233 #ifdef __APPLE__
234 OSErr err;
235 SInt32 qtVersion, macosversion;
237 err = Gestalt(gestaltQuickTimeVersion, &qtVersion);
238 err = Gestalt(gestaltSystemVersion, &macosversion);
239 #ifndef NDEBUG
240 msg_Dbg( p_this, "Mac OS version is %#lx", macosversion );
241 msg_Dbg( p_this, "Quicktime version is %#lx", qtVersion );
242 #endif
244 /* bail out. This plugin is soo Carbon, that it can't be used on 10.5 at all */
245 msg_Info( p_dec, "Your Mac OS version is to new to use this plugin for anything." );
246 return VLC_EGENERIC;
247 #endif
249 switch( p_dec->fmt_in.i_codec )
251 case VLC_CODEC_H264:
252 case VLC_CODEC_CINEPAK:
253 case VLC_FOURCC('I','V','4','1'): /* Indeo Video IV */
254 case VLC_FOURCC('i','v','4','1'): /* dto. */
255 #ifdef __APPLE__
256 case VLC_FOURCC('p','x','l','t'): /* Pixlet */
257 #endif
258 case VLC_CODEC_DV:
259 case VLC_CODEC_SVQ3: /* Sorenson v3 */
260 /* case VLC_CODEC_SVQ1: Sorenson v1
261 case VLC_FOURCC('Z','y','G','o'):
262 case VLC_FOURCC('V','P','3','1'):
263 case VLC_FOURCC('3','I','V','1'): */
264 case VLC_CODEC_QTRLE:
265 case VLC_CODEC_RPZA:
266 #ifdef LOADER
267 p_dec->p_sys = NULL;
268 p_dec->pf_decode_video = DecodeVideo;
269 p_dec->fmt_out.i_cat = VIDEO_ES;
270 return VLC_SUCCESS;
271 #else
272 return OpenVideo( p_dec );
273 #endif
275 #ifdef __APPLE__
276 case VLC_FOURCC('I','L','B','C'): /* iLBC */
277 if ((err != noErr) || (qtVersion < 0x07500000))
278 return VLC_EGENERIC;
279 case VLC_FOURCC('i','l','b','c'): /* iLBC */
280 if ((err != noErr) || (qtVersion < 0x07500000))
281 return VLC_EGENERIC;
282 #endif
283 case VLC_CODEC_AMR_NB: /* 3GPP AMR audio */
284 case VLC_FOURCC('s','a','m','b'): /* 3GPP AMR-WB audio */
285 case VLC_CODEC_MP4A: /* MPEG-4 audio */
286 case VLC_FOURCC('Q','D','M','C'): /* QDesign */
287 case VLC_CODEC_QDM2: /* QDesign* 2 */
288 case VLC_CODEC_QCELP: /* Qualcomm Purevoice Codec */
289 case VLC_FOURCC('Q','C','L','P'): /* Qualcomm Purevoice Codec */
290 case VLC_CODEC_MACE3: /* MACE3 audio decoder */
291 case VLC_CODEC_MACE6: /* MACE6 audio decoder */
292 case VLC_FOURCC('d','v','c','a'): /* DV Audio */
293 case VLC_FOURCC('s','o','w','t'): /* 16-bit Little Endian */
294 case VLC_FOURCC('t','w','o','s'): /* 16-bit Big Endian */
295 case VLC_CODEC_ALAW: /* ALaw 2:1 */
296 case VLC_FOURCC('u','l','a','w'): /* mu-Law 2:1 */
297 case VLC_FOURCC('r','a','w',' '): /* 8-bit offset binaries */
298 case VLC_CODEC_FL32: /* 32-bit Floating Point */
299 case VLC_CODEC_FL64: /* 64-bit Floating Point */
300 case VLC_FOURCC('i','n','2','4'): /* 24-bit Interger */
301 case VLC_FOURCC('i','n','3','2'): /* 32-bit Integer */
302 case 0x0011: /* DVI IMA */
303 case 0x6D730002: /* Microsoft ADPCM-ACM */
304 case 0x6D730011: /* DVI Intel IMAADPCM-ACM */
305 #ifdef LOADER
306 p_dec->p_sys = NULL;
307 p_dec->pf_decode_audio = DecodeAudio;
308 p_dec->fmt_out.i_cat = AUDIO_ES;
309 return VLC_SUCCESS;
310 #else
311 return OpenAudio( p_dec );
312 #endif
314 default:
315 return VLC_EGENERIC;
319 static vlc_mutex_t qt_mutex = VLC_STATIC_MUTEX;
321 /*****************************************************************************
322 * Close:
323 *****************************************************************************/
324 static void Close( vlc_object_t *p_this )
326 decoder_t *p_dec = (decoder_t*)p_this;
327 decoder_sys_t *p_sys = p_dec->p_sys;
329 /* get lock, avoid segfault */
330 vlc_mutex_lock( &qt_mutex );
332 if( p_dec->fmt_out.i_cat == AUDIO_ES )
334 int i_error;
335 unsigned long ConvertedFrames=0;
336 unsigned long ConvertedBytes=0;
338 i_error = p_sys->SoundConverterEndConversion( p_sys->myConverter, NULL,
339 &ConvertedFrames,
340 &ConvertedBytes );
341 msg_Dbg( p_dec, "SoundConverterEndConversion => %d", i_error );
343 i_error = p_sys->SoundConverterClose( p_sys->myConverter );
344 msg_Dbg( p_dec, "SoundConverterClose => %d", i_error );
346 free( p_sys->p_buffer );
348 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
350 free( p_sys->plane );
353 #ifndef __APPLE__
354 FreeLibrary( p_sys->qtml );
355 FreeLibrary( p_sys->qts );
356 msg_Dbg( p_dec, "FreeLibrary ok." );
357 #else
358 ExitMovies();
359 #endif
361 #if 0
362 /* Segfault */
363 #ifdef LOADER
364 Restore_LDT_Keeper( p_sys->ldt_fs );
365 msg_Dbg( p_dec, "Restore_LDT_Keeper" );
366 #endif
367 #endif
369 vlc_mutex_unlock( &qt_mutex );
371 free( p_sys );
374 /*****************************************************************************
375 * OpenAudio:
376 *****************************************************************************/
377 static int OpenAudio( decoder_t *p_dec )
379 decoder_sys_t *p_sys;
381 int i_error;
382 char fcc[4];
383 unsigned long WantedBufferSize;
384 unsigned long InputBufferSize = 0;
385 unsigned long OutputBufferSize = 0;
387 /* get lock, avoid segfault */
388 vlc_mutex_lock( &qt_mutex );
390 p_sys = calloc( 1, sizeof( decoder_sys_t ) );
391 p_dec->p_sys = p_sys;
392 p_dec->pf_decode_audio = DecodeAudio;
394 if( p_dec->fmt_in.i_original_fourcc )
395 memcpy( fcc, &p_dec->fmt_in.i_original_fourcc, 4 );
396 else
397 memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
399 #ifdef __APPLE__
400 EnterMovies();
401 #endif
403 if( QTAudioInit( p_dec ) )
405 msg_Err( p_dec, "cannot initialize QT");
406 goto exit_error;
409 #ifndef __APPLE__
410 if( ( i_error = p_sys->InitializeQTML( 6 + 16 ) ) )
412 msg_Dbg( p_dec, "error on InitializeQTML = %d", i_error );
413 goto exit_error;
415 #endif
417 /* input format settings */
418 p_sys->InputFormatInfo.flags = 0;
419 p_sys->InputFormatInfo.sampleCount = 0;
420 p_sys->InputFormatInfo.buffer = NULL;
421 p_sys->InputFormatInfo.reserved = 0;
422 p_sys->InputFormatInfo.numChannels = p_dec->fmt_in.audio.i_channels;
423 p_sys->InputFormatInfo.sampleSize = p_dec->fmt_in.audio.i_bitspersample;
424 p_sys->InputFormatInfo.sampleRate = p_dec->fmt_in.audio.i_rate;
425 p_sys->InputFormatInfo.format = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
427 /* output format settings */
428 p_sys->OutputFormatInfo.flags = 0;
429 p_sys->OutputFormatInfo.sampleCount = 0;
430 p_sys->OutputFormatInfo.buffer = NULL;
431 p_sys->OutputFormatInfo.reserved = 0;
432 p_sys->OutputFormatInfo.numChannels = p_dec->fmt_in.audio.i_channels;
433 p_sys->OutputFormatInfo.sampleSize = 16;
434 p_sys->OutputFormatInfo.sampleRate = p_dec->fmt_in.audio.i_rate;
435 p_sys->OutputFormatInfo.format = FCC( 'N', 'O', 'N', 'E' );
438 i_error = p_sys->SoundConverterOpen( &p_sys->InputFormatInfo,
439 &p_sys->OutputFormatInfo,
440 &p_sys->myConverter );
441 if( i_error )
443 msg_Err( p_dec, "error on SoundConverterOpen = %d", i_error );
444 goto exit_error;
447 #if 0
448 /* tell the sound converter we accept VBR formats */
449 i_error = SoundConverterSetInfo( p_dec->myConverter, siClientAcceptsVBR,
450 (void *)true );
451 #endif
453 if( p_dec->fmt_in.i_extra > 36 + 8 )
455 i_error = p_sys->SoundConverterSetInfo( p_sys->myConverter,
456 FCC( 'w', 'a', 'v', 'e' ),
457 ((uint8_t*)p_dec->fmt_in.p_extra) + 36 + 8 );
459 msg_Dbg( p_dec, "error on SoundConverterSetInfo = %d", i_error );
462 WantedBufferSize = p_sys->OutputFormatInfo.numChannels *
463 p_sys->OutputFormatInfo.sampleRate * 2;
464 p_sys->FramesToGet = 0;
466 i_error = p_sys->SoundConverterGetBufferSizes( p_sys->myConverter,
467 WantedBufferSize,
468 &p_sys->FramesToGet,
469 &InputBufferSize,
470 &OutputBufferSize );
472 msg_Dbg( p_dec, "WantedBufferSize=%li InputBufferSize=%li "
473 "OutputBufferSize=%li FramesToGet=%li",
474 WantedBufferSize, InputBufferSize, OutputBufferSize,
475 p_sys->FramesToGet );
477 p_sys->InFrameSize = (InputBufferSize + p_sys->FramesToGet - 1 ) /
478 p_sys->FramesToGet;
479 p_sys->OutFrameSize = OutputBufferSize / p_sys->FramesToGet;
481 msg_Dbg( p_dec, "frame size %d -> %d",
482 p_sys->InFrameSize, p_sys->OutFrameSize );
484 if( (i_error = p_sys->SoundConverterBeginConversion(p_sys->myConverter)) )
486 msg_Err( p_dec,
487 "error on SoundConverterBeginConversion = %d", i_error );
488 goto exit_error;
492 es_format_Init( &p_dec->fmt_out, AUDIO_ES, VLC_CODEC_S16N );
493 p_dec->fmt_out.audio.i_rate = p_sys->OutputFormatInfo.sampleRate;
494 p_dec->fmt_out.audio.i_channels = p_sys->OutputFormatInfo.numChannels;
495 p_dec->fmt_out.audio.i_physical_channels =
496 p_dec->fmt_out.audio.i_original_channels =
497 pi_channels_maps[p_sys->OutputFormatInfo.numChannels];
499 date_Init( &p_sys->date, p_dec->fmt_out.audio.i_rate, 1 );
501 p_sys->i_buffer = 0;
502 p_sys->i_buffer_size = 100*1000;
503 p_sys->p_buffer = malloc( p_sys->i_buffer_size );
504 if( !p_sys->p_buffer )
505 goto exit_error;
507 p_sys->i_out = 0;
508 p_sys->i_out_frames = 0;
510 vlc_mutex_unlock( &qt_mutex );
511 return VLC_SUCCESS;
513 exit_error:
515 #ifdef LOADER
516 Restore_LDT_Keeper( p_sys->ldt_fs );
517 #endif
518 vlc_mutex_unlock( &qt_mutex );
520 free( p_sys );
521 return VLC_EGENERIC;
524 /*****************************************************************************
525 * DecodeAudio:
526 *****************************************************************************/
527 static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
529 decoder_sys_t *p_sys = p_dec->p_sys;
531 block_t *p_block;
532 int i_error;
534 #ifdef LOADER
535 /* We must do open and close in the same thread (unless we do
536 * Setup_LDT_Keeper in the main thread before all others */
537 if( p_sys == NULL )
539 if( OpenAudio( p_dec ) )
541 /* Fatal */
542 p_dec->b_error = true;
543 return NULL;
546 p_sys = p_dec->p_sys;
548 #endif
550 if( pp_block == NULL || *pp_block == NULL )
552 return NULL;
554 p_block = *pp_block;
556 if( p_sys->i_out_frames > 0 && p_sys->i_out >= p_sys->i_out_frames )
558 /* Ask new data */
559 p_sys->i_out = 0;
560 p_sys->i_out_frames = 0;
562 *pp_block = NULL;
563 return NULL;
566 if( p_sys->i_out_frames <= 0 )
568 p_sys->pts = p_block->i_pts;
570 mtime_t i_display_date = 0;
571 if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
572 i_display_date = decoder_GetDisplayDate( p_dec, p_block->i_pts );
574 if( i_display_date > 0 && i_display_date < mdate() )
576 block_Release( p_block );
577 *pp_block = NULL;
578 return NULL;
581 /* Append data */
582 if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
584 p_sys->i_buffer_size = p_sys->i_buffer + p_block->i_buffer + 1024;
585 p_sys->p_buffer = xrealloc( p_sys->p_buffer, p_sys->i_buffer_size );
587 memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer,
588 p_block->i_buffer );
589 p_sys->i_buffer += p_block->i_buffer;
591 if( p_sys->i_buffer > p_sys->InFrameSize )
593 int i_frames = p_sys->i_buffer / p_sys->InFrameSize;
594 unsigned long i_out_frames, i_out_bytes;
595 vlc_mutex_lock( &qt_mutex );
597 i_error = p_sys->SoundConverterConvertBuffer( p_sys->myConverter,
598 p_sys->p_buffer,
599 i_frames,
600 p_sys->out_buffer,
601 &i_out_frames,
602 &i_out_bytes );
603 vlc_mutex_unlock( &qt_mutex );
606 msg_Dbg( p_dec, "decoded %d frames -> %ld frames (error=%d)",
607 i_frames, i_out_frames, i_error );
609 msg_Dbg( p_dec, "decoded %ld frames = %ld bytes",
610 i_out_frames, i_out_bytes );
613 p_sys->i_buffer -= i_frames * p_sys->InFrameSize;
614 if( p_sys->i_buffer > 0 )
616 memmove( &p_sys->p_buffer[0],
617 &p_sys->p_buffer[i_frames * p_sys->InFrameSize],
618 p_sys->i_buffer );
621 if( p_sys->pts > VLC_TS_INVALID &&
622 p_sys->pts != date_Get( &p_sys->date ) )
624 date_Set( &p_sys->date, p_sys->pts );
626 else if( !date_Get( &p_sys->date ) )
628 return NULL;
631 if( !i_error && i_out_frames > 0 )
633 /* we have others samples */
634 p_sys->i_out_frames = i_out_frames;
635 p_sys->i_out = 0;
640 if( p_sys->i_out < p_sys->i_out_frames )
642 aout_buffer_t *p_out;
643 int i_frames = __MIN( p_sys->i_out_frames - p_sys->i_out, 1000 );
645 p_out = decoder_NewAudioBuffer( p_dec, i_frames );
647 if( p_out )
649 p_out->i_pts = date_Get( &p_sys->date );
650 p_out->i_length = date_Increment( &p_sys->date, i_frames )
651 - p_out->i_pts;
653 memcpy( p_out->p_buffer,
654 &p_sys->out_buffer[2 * p_sys->i_out * p_dec->fmt_out.audio.i_channels],
655 p_out->i_buffer );
657 p_sys->i_out += i_frames;
659 return p_out;
662 return NULL;
665 /*****************************************************************************
666 * OpenVideo:
667 *****************************************************************************/
668 static int OpenVideo( decoder_t *p_dec )
670 #ifndef WIN32
671 decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) );
672 if( !p_sys )
673 return VLC_ENOMEM;
675 long i_result;
676 ComponentDescription desc;
677 Component prev;
678 ComponentResult cres;
679 ImageSubCodecDecompressCapabilities icap; /* for ImageCodecInitialize() */
680 CodecInfo cinfo; /* for ImageCodecGetCodecInfo() */
681 ImageDescription *id;
683 char fcc[4];
684 int i_vide = p_dec->fmt_in.i_extra;
685 uint8_t *p_vide = p_dec->fmt_in.p_extra;
687 p_dec->p_sys = p_sys;
688 p_dec->pf_decode_video = DecodeVideo;
689 p_sys->i_late = 0;
691 if( i_vide <= 0 )
693 msg_Err( p_dec, "missing extra info" );
694 free( p_sys );
695 return VLC_EGENERIC;
698 if( p_dec->fmt_in.i_original_fourcc )
699 memcpy( fcc, &p_dec->fmt_in.i_original_fourcc, 4 );
700 else
701 memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
703 msg_Dbg( p_dec, "quicktime_video %4.4s %dx%d",
704 fcc, p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
706 /* get lock, avoid segfault */
707 vlc_mutex_lock( &qt_mutex );
709 #ifdef __APPLE__
710 EnterMovies();
711 #endif
713 if( QTVideoInit( p_dec ) )
715 msg_Err( p_dec, "cannot initialize QT");
716 goto exit_error;
719 #ifndef __APPLE__
720 if( ( i_result = p_sys->InitializeQTML( 6 + 16 ) ) )
722 msg_Dbg( p_dec, "error on InitializeQTML = %d", (int)i_result );
723 goto exit_error;
725 #endif
727 /* init ComponentDescription */
728 memset( &desc, 0, sizeof( ComponentDescription ) );
729 desc.componentType = FCC( 'i', 'm', 'd', 'c' );
730 desc.componentSubType = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
731 desc.componentManufacturer = 0;
732 desc.componentFlags = 0;
733 desc.componentFlagsMask = 0;
735 if( !( prev = p_sys->FindNextComponent( NULL, &desc ) ) )
737 msg_Err( p_dec, "cannot find requested component" );
738 goto exit_error;
740 msg_Dbg( p_dec, "component id=0x%p", prev );
742 p_sys->ci = p_sys->OpenComponent( prev );
743 msg_Dbg( p_dec, "component instance p=0x%p", p_sys->ci );
745 memset( &icap, 0, sizeof( ImageSubCodecDecompressCapabilities ) );
746 cres = p_sys->ImageCodecInitialize( p_sys->ci, &icap );
747 msg_Dbg( p_dec, "ImageCodecInitialize->0x%X size=%d (%d)",
748 (int)cres, (int)icap.recordSize, (int)icap.decompressRecordSize);
750 memset( &cinfo, 0, sizeof( CodecInfo ) );
751 cres = p_sys->ImageCodecGetCodecInfo( p_sys->ci, &cinfo );
752 msg_Dbg( p_dec,
753 "Flags: compr: 0x%x decomp: 0x%x format: 0x%x",
754 (unsigned int)cinfo.compressFlags,
755 (unsigned int)cinfo.decompressFlags,
756 (unsigned int)cinfo.formatFlags );
757 msg_Dbg( p_dec, "quicktime_video: Codec name: %.*s",
758 ((unsigned char*)&cinfo.typeName)[0],
759 ((unsigned char*)&cinfo.typeName)+1 );
761 /* make a yuy2 gworld */
762 p_sys->OutBufferRect.top = 0;
763 p_sys->OutBufferRect.left = 0;
764 p_sys->OutBufferRect.right = p_dec->fmt_in.video.i_width;
765 p_sys->OutBufferRect.bottom = p_dec->fmt_in.video.i_height;
768 /* codec data FIXME use codec not SVQ3 */
769 msg_Dbg( p_dec, "vide = %d", i_vide );
770 id = malloc( sizeof( ImageDescription ) + ( i_vide - 70 ) );
771 if( !id )
772 goto exit_error;
773 id->idSize = sizeof( ImageDescription ) + ( i_vide - 70 );
774 id->cType = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
775 id->version = GetWBE ( p_vide + 0 );
776 id->revisionLevel = GetWBE ( p_vide + 2 );
777 id->vendor = GetDWBE( p_vide + 4 );
778 id->temporalQuality = GetDWBE( p_vide + 8 );
779 id->spatialQuality = GetDWBE( p_vide + 12 );
780 id->width = GetWBE ( p_vide + 16 );
781 id->height = GetWBE ( p_vide + 18 );
782 id->hRes = GetDWBE( p_vide + 20 );
783 id->vRes = GetDWBE( p_vide + 24 );
784 id->dataSize = GetDWBE( p_vide + 28 );
785 id->frameCount = GetWBE ( p_vide + 32 );
786 memcpy( &id->name, p_vide + 34, 32 );
787 id->depth = GetWBE ( p_vide + 66 );
788 id->clutID = GetWBE ( p_vide + 68 );
789 if( i_vide > 70 )
791 memcpy( ((char*)&id->clutID) + 2, p_vide + 70, i_vide - 70 );
794 msg_Dbg( p_dec, "idSize=%d ver=%d rev=%d vendor=%d tempQ=%d "
795 "spaQ=%d w=%d h=%d dpi=%d%d dataSize=%d depth=%d frameCount=%d clutID=%d",
796 (int)id->idSize, id->version, id->revisionLevel, (int)id->vendor,
797 (int)id->temporalQuality, (int)id->spatialQuality,
798 (int)id->width, (int)id->height,
799 (int)id->hRes, (int)id->vRes,
800 (int)id->dataSize,
801 id->depth,
802 id->frameCount,
803 id->clutID );
805 p_sys->framedescHandle = (ImageDescriptionHandle) NewHandleClear( id->idSize );
806 memcpy( *p_sys->framedescHandle, id, id->idSize );
808 if( p_dec->fmt_in.video.i_width != 0 && p_dec->fmt_in.video.i_height != 0)
809 p_sys->plane = malloc( p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height * 3 );
810 if( !p_sys->plane )
811 goto exit_error;
813 i_result = p_sys->QTNewGWorldFromPtr( &p_sys->OutBufferGWorld,
814 /*pixel format of new GWorld==YUY2 */
815 kYUVSPixelFormat,
816 /* we should benchmark if yvu9 is
817 * faster for svq3, too */
818 &p_sys->OutBufferRect,
819 0, 0, 0,
820 p_sys->plane,
821 p_dec->fmt_in.video.i_width * 2 );
823 msg_Dbg( p_dec, "NewGWorldFromPtr returned:%ld",
824 65536 - ( i_result&0xffff ) );
826 memset( &p_sys->decpar, 0, sizeof( CodecDecompressParams ) );
827 p_sys->decpar.imageDescription = p_sys->framedescHandle;
828 p_sys->decpar.startLine = 0;
829 p_sys->decpar.stopLine = ( **p_sys->framedescHandle ).height;
830 p_sys->decpar.frameNumber = 1;
831 p_sys->decpar.matrixFlags = 0;
832 p_sys->decpar.matrixType = 0;
833 p_sys->decpar.matrix = 0;
834 p_sys->decpar.capabilities = &p_sys->codeccap;
835 p_sys->decpar.accuracy = codecNormalQuality;
836 p_sys->decpar.srcRect = p_sys->OutBufferRect;
837 p_sys->decpar.transferMode = srcCopy;
838 p_sys->decpar.dstPixMap = **p_sys->GetGWorldPixMap( p_sys->OutBufferGWorld );/*destPixmap; */
840 cres = p_sys->ImageCodecPreDecompress( p_sys->ci, &p_sys->decpar );
841 msg_Dbg( p_dec, "quicktime_video: ImageCodecPreDecompress cres=0x%X",
842 (int)cres );
844 es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_CODEC_YUYV);
845 p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
846 p_dec->fmt_out.video.i_height= p_dec->fmt_in.video.i_height;
847 p_dec->fmt_out.video.i_sar_num = 1;
848 p_dec->fmt_out.video.i_sar_den = 1;
850 vlc_mutex_unlock( &qt_mutex );
851 return VLC_SUCCESS;
853 exit_error:
854 #ifdef LOADER
855 Restore_LDT_Keeper( p_sys->ldt_fs );
856 #endif
857 vlc_mutex_unlock( &qt_mutex );
859 #else
860 VLC_UNUSED( p_dec );
861 #endif /* !WIN32 */
863 return VLC_EGENERIC;
866 #ifndef WIN32
867 /*****************************************************************************
868 * DecodeVideo:
869 *****************************************************************************/
870 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
872 decoder_sys_t *p_sys = p_dec->p_sys;
873 block_t *p_block;
874 picture_t *p_pic;
875 mtime_t i_pts;
877 ComponentResult cres;
879 #ifdef LOADER
880 /* We must do open and close in the same thread (unless we do
881 * Setup_LDT_Keeper in the main thread before all others */
882 if( p_sys == NULL )
884 if( OpenVideo( p_dec ) )
886 /* Fatal */
887 p_dec->b_error = true;
888 return NULL;
890 p_sys = p_dec->p_sys;
892 #endif
894 if( pp_block == NULL || *pp_block == NULL )
896 return NULL;
898 p_block = *pp_block;
899 *pp_block = NULL;
901 i_pts = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;
903 mtime_t i_display_date = 0;
904 if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
905 i_display_date = decoder_GetDisplayDate( p_dec, i_pts );
907 if( i_display_date > 0 && i_display_date < mdate() )
909 p_sys->i_late++;
911 else
913 p_sys->i_late = 0;
915 #ifndef NDEBUG
916 msg_Dbg( p_dec, "bufsize: %zu", p_block->i_buffer);
917 #endif
919 if( p_sys->i_late > 10 )
921 msg_Dbg( p_dec, "late buffer dropped (%"PRId64")", i_pts );
922 block_Release( p_block );
923 return NULL;
926 vlc_mutex_lock( &qt_mutex );
928 if( ( p_pic = decoder_NewPicture( p_dec ) ) )
930 p_sys->decpar.data = (Ptr)p_block->p_buffer;
931 p_sys->decpar.bufferSize = p_block->i_buffer;
932 (**p_sys->framedescHandle).dataSize = p_block->i_buffer;
934 cres = p_sys->ImageCodecBandDecompress( p_sys->ci, &p_sys->decpar );
936 ++p_sys->decpar.frameNumber;
938 if( cres &0xFFFF )
940 msg_Dbg( p_dec, "quicktime_video: ImageCodecBandDecompress"
941 " cres=0x%X (-0x%X) %d :(",
942 (int)cres,(int)-cres, (int)cres );
945 memcpy( p_pic->p[0].p_pixels, p_sys->plane,
946 p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height * 2 );
947 p_pic->date = i_pts;
950 vlc_mutex_unlock( &qt_mutex );
952 block_Release( p_block );
953 return p_pic;
955 #endif /* !WIN32 */
957 /*****************************************************************************
958 * QTAudioInit:
959 *****************************************************************************/
960 static int QTAudioInit( decoder_t *p_dec )
962 decoder_sys_t *p_sys = p_dec->p_sys;
964 #ifdef __APPLE__
965 p_sys->SoundConverterOpen = (void*)SoundConverterOpen;
966 p_sys->SoundConverterClose = (void*)SoundConverterClose;
967 p_sys->SoundConverterSetInfo = (void*)SoundConverterSetInfo;
968 p_sys->SoundConverterGetBufferSizes = (void*)SoundConverterGetBufferSizes;
969 p_sys->SoundConverterConvertBuffer = (void*)SoundConverterConvertBuffer;
970 p_sys->SoundConverterBeginConversion= (void*)SoundConverterBeginConversion;
971 p_sys->SoundConverterEndConversion = (void*)SoundConverterEndConversion;
972 #else
974 #ifdef LOADER
975 p_sys->ldt_fs = Setup_LDT_Keeper();
976 #endif /* LOADER */
978 p_sys->qts = LoadLibraryA( "QuickTime.qts" );
979 if( p_sys->qts == NULL )
981 msg_Dbg( p_dec, "failed loading QuickTime.qts" );
982 return VLC_EGENERIC;
984 p_sys->qtml = LoadLibraryA( "qtmlClient.dll" );
985 if( p_sys->qtml == NULL )
987 msg_Dbg( p_dec, "failed loading qtmlClient.dll" );
988 return VLC_EGENERIC;
991 p_sys->InitializeQTML = (void *)GetProcAddress( p_sys->qtml, "InitializeQTML" );
992 p_sys->TerminateQTML = (void *)GetProcAddress( p_sys->qtml, "TerminateQTML" );
993 p_sys->SoundConverterOpen = (void *)GetProcAddress( p_sys->qtml, "SoundConverterOpen" );
994 p_sys->SoundConverterClose = (void *)GetProcAddress( p_sys->qtml, "SoundConverterClose" );
995 p_sys->SoundConverterSetInfo = (void *)GetProcAddress( p_sys->qtml, "SoundConverterSetInfo" );
996 p_sys->SoundConverterGetBufferSizes = (void *)GetProcAddress( p_sys->qtml, "SoundConverterGetBufferSizes" );
997 p_sys->SoundConverterConvertBuffer = (void *)GetProcAddress( p_sys->qtml, "SoundConverterConvertBuffer" );
998 p_sys->SoundConverterEndConversion = (void *)GetProcAddress( p_sys->qtml, "SoundConverterEndConversion" );
999 p_sys->SoundConverterBeginConversion= (void *)GetProcAddress( p_sys->qtml, "SoundConverterBeginConversion");
1001 if( p_sys->InitializeQTML == NULL )
1003 msg_Err( p_dec, "failed getting proc address InitializeQTML" );
1004 return VLC_EGENERIC;
1006 if( p_sys->SoundConverterOpen == NULL ||
1007 p_sys->SoundConverterClose == NULL ||
1008 p_sys->SoundConverterSetInfo == NULL ||
1009 p_sys->SoundConverterGetBufferSizes == NULL ||
1010 p_sys->SoundConverterConvertBuffer == NULL ||
1011 p_sys->SoundConverterEndConversion == NULL ||
1012 p_sys->SoundConverterBeginConversion == NULL )
1014 msg_Err( p_dec, "failed getting proc address" );
1015 return VLC_EGENERIC;
1018 msg_Dbg( p_dec, "standard init done" );
1019 #endif /* else __APPLE__ */
1021 return VLC_SUCCESS;
1024 #ifndef WIN32
1025 /*****************************************************************************
1026 * QTVideoInit:
1027 *****************************************************************************/
1028 static int QTVideoInit( decoder_t *p_dec )
1030 decoder_sys_t *p_sys = p_dec->p_sys;
1032 #ifdef __APPLE__
1033 p_sys->FindNextComponent = (void*)FindNextComponent;
1034 p_sys->OpenComponent = (void*)OpenComponent;
1035 p_sys->ImageCodecInitialize = (void*)ImageCodecInitialize;
1036 p_sys->ImageCodecGetCodecInfo = (void*)ImageCodecGetCodecInfo;
1037 p_sys->ImageCodecPreDecompress = (void*)ImageCodecPreDecompress;
1038 p_sys->ImageCodecBandDecompress = (void*)ImageCodecBandDecompress;
1039 p_sys->GetGWorldPixMap = (void*)GetGWorldPixMap;
1040 p_sys->QTNewGWorldFromPtr = (void*)QTNewGWorldFromPtr;
1041 p_sys->NewHandleClear = (void*)NewHandleClear;
1042 #else
1044 #ifdef LOADER
1045 p_sys->ldt_fs = Setup_LDT_Keeper();
1046 #endif /* LOADER */
1047 p_sys->qts = LoadLibraryA( "QuickTime.qts" );
1048 if( p_sys->qts == NULL )
1050 msg_Dbg( p_dec, "failed loading QuickTime.qts" );
1051 return VLC_EGENERIC;
1053 msg_Dbg( p_dec, "QuickTime.qts loaded" );
1054 p_sys->qtml = LoadLibraryA( "qtmlClient.dll" );
1055 if( p_sys->qtml == NULL )
1057 msg_Dbg( p_dec, "failed loading qtmlClient.dll" );
1058 return VLC_EGENERIC;
1060 msg_Dbg( p_dec, "qtmlClient.dll loaded" );
1062 /* (void*) to shut up gcc */
1063 p_sys->InitializeQTML = (void*)GetProcAddress( p_sys->qtml, "InitializeQTML" );
1064 p_sys->FindNextComponent = (void*)GetProcAddress( p_sys->qtml, "FindNextComponent" );
1065 p_sys->OpenComponent = (void*)GetProcAddress( p_sys->qtml, "OpenComponent" );
1066 p_sys->ImageCodecInitialize = (void*)GetProcAddress( p_sys->qtml, "ImageCodecInitialize" );
1067 p_sys->ImageCodecGetCodecInfo = (void*)GetProcAddress( p_sys->qtml, "ImageCodecGetCodecInfo" );
1068 p_sys->ImageCodecPreDecompress = (void*)GetProcAddress( p_sys->qtml, "ImageCodecPreDecompress" );
1069 p_sys->ImageCodecBandDecompress = (void*)GetProcAddress( p_sys->qtml, "ImageCodecBandDecompress" );
1070 p_sys->GetGWorldPixMap = (void*)GetProcAddress( p_sys->qtml, "GetGWorldPixMap" );
1071 p_sys->QTNewGWorldFromPtr = (void*)GetProcAddress( p_sys->qtml, "QTNewGWorldFromPtr" );
1072 p_sys->NewHandleClear = (void*)GetProcAddress( p_sys->qtml, "NewHandleClear" );
1074 if( p_sys->InitializeQTML == NULL )
1076 msg_Dbg( p_dec, "failed getting proc address InitializeQTML" );
1077 return VLC_EGENERIC;
1079 if( p_sys->FindNextComponent == NULL ||
1080 p_sys->OpenComponent == NULL ||
1081 p_sys->ImageCodecInitialize == NULL ||
1082 p_sys->ImageCodecGetCodecInfo == NULL ||
1083 p_sys->ImageCodecPreDecompress == NULL ||
1084 p_sys->ImageCodecBandDecompress == NULL ||
1085 p_sys->GetGWorldPixMap == NULL ||
1086 p_sys->QTNewGWorldFromPtr == NULL ||
1087 p_sys->NewHandleClear == NULL )
1089 msg_Err( p_dec, "failed getting proc address" );
1090 return VLC_EGENERIC;
1092 #endif /* __APPLE__ */
1094 return VLC_SUCCESS;
1096 #endif /* !WIN32 */