]> git.sesse.net Git - vlc/blobdiff - modules/codec/quicktime.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / codec / quicktime.c
index 62f43b03e09a111d6e7343583cad164ffb96c835..ae154fa3c04a46d93b586335652d760544c32e1f 100644 (file)
@@ -1,11 +1,11 @@
 /*****************************************************************************
  * quicktime.c: a quicktime decoder that uses the QT library/dll
  *****************************************************************************
- * Copyright (C) 2003 VideoLAN
- * $Id: quicktime.c,v 1.16 2003/11/23 03:41:58 fenrir Exp $
+ * Copyright (C) 2003, 2008 - 2009 the VideoLAN team
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir at via.ecp.fr>
- *          Derk-Jan Hartman <thedj at users.sf.net>
+ *          Derk-Jan Hartman <hartman at videolan.org>>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
 
-#include <vlc/vlc.h>
-#include <vlc/aout.h>
-#include <vlc/vout.h>
-#include <vlc/decoder.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_aout.h>
+#include <vlc_codec.h>
+
+#if !defined (__APPLE__) && !defined(WIN32)
+# define LOADER 1
+#endif
 
-#ifdef SYS_DARWIN
+#ifdef __APPLE__
 #include <QuickTime/QuickTimeComponents.h>
 #include <QuickTime/Movies.h>
 #include <QuickTime/ImageCodec.h>
 #endif
 
 /* for windows do we require Quicktime compents header? */
-#define LOADER 1
 #ifdef LOADER
-#include "w32dll/loader/qtx/qtxsdk/components.h"
-#include "w32dll/loader/wine/windef.h"
-#include "w32dll/loader/ldt_keeper.h"
+#include "qtx/qtxsdk/components.h"
+#include "wine/windef.h"
+#include "ldt_keeper.h"
 
 HMODULE   WINAPI LoadLibraryA(LPCSTR);
 FARPROC   WINAPI GetProcAddress(HMODULE,LPCSTR);
@@ -56,14 +63,14 @@ int       WINAPI FreeLibrary(HMODULE);
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
-vlc_module_begin();
-    set_description( _("QuickTime library decoder") );
-    set_capability( "decoder", 10 );
-    set_callbacks( Open, Close );
+vlc_module_begin ()
+    set_description( N_("QuickTime library decoder") )
+    set_capability( "decoder", 0 )
+    set_category( CAT_INPUT )
+    set_subcategory( SUBCAT_INPUT_VCODEC )
+    set_callbacks( Open, Close )
 
-    /* create a mutex */
-    var_Create( p_module->p_libvlc, "qt_mutex", VLC_VAR_MUTEX );
-vlc_module_end();
+vlc_module_end ()
 
 
 /*****************************************************************************
@@ -73,12 +80,14 @@ static int           OpenAudio( decoder_t * );
 static int           OpenVideo( decoder_t * );
 
 static aout_buffer_t *DecodeAudio( decoder_t *, block_t ** );
+#ifndef WIN32
 static picture_t     *DecodeVideo( decoder_t *, block_t ** );
+#endif
 
 #define FCC( a, b , c, d ) \
     ((uint32_t)( ((a)<<24)|((b)<<16)|((c)<<8)|(d)))
 
-#ifndef SYS_DARWIN
+#ifndef __APPLE__
 typedef struct OpaqueSoundConverter*    SoundConverter;
 #ifndef LOADER
 typedef long                            OSType;
@@ -98,20 +107,21 @@ typedef struct SoundComponentData {
     long                            reserved;
 } SoundComponentData;
 
-#endif /* SYS_DARWIN */
+#endif /* __APPLE__ */
 
 struct decoder_sys_t
 {
     /* library */
-#ifndef SYS_DARWIN
+#ifndef __APPLE__
 #ifdef LOADER
     ldt_fs_t    *ldt_fs;
-
 #endif /* LOADER */
+
     HMODULE qtml;
+    HINSTANCE qts;
     OSErr (*InitializeQTML)             ( long flags );
     OSErr (*TerminateQTML)              ( void );
-#endif /* SYS_DARWIN */
+#endif /* __APPLE__ */
 
     /* Audio */
     int (*SoundConverterOpen)           ( const SoundComponentData *,
@@ -131,10 +141,11 @@ struct decoder_sys_t
     SoundConverter      myConverter;
     SoundComponentData  InputFormatInfo, OutputFormatInfo;
 
-    long            FramesToGet;
+    unsigned long   FramesToGet;
     unsigned int    InFrameSize;
     unsigned int    OutFrameSize;
 
+#ifndef WIN32
     /* Video */
     Component         (*FindNextComponent)
         ( Component prev, ComponentDescription* desc );
@@ -173,12 +184,12 @@ struct decoder_sys_t
 
     CodecDecompressParams   decpar;          /* for ImageCodecPreDecompress()*/
     CodecCapabilities       codeccap;        /* for decpar */
-
+#endif
 
     /* Output properties */
     uint8_t *           plane;
     mtime_t             pts;
-    audio_date_t        date;
+    date_t              date;
 
     int                 i_late; /* video */
 
@@ -193,7 +204,7 @@ struct decoder_sys_t
     int                 i_out;
 };
 
-static int pi_channels_maps[6] =
+static const int pi_channels_maps[6] =
 {
     0,
     AOUT_CHAN_CENTER,
@@ -205,7 +216,9 @@ static int pi_channels_maps[6] =
 };
 
 static int QTAudioInit( decoder_t * );
+#ifndef WIN32
 static int QTVideoInit( decoder_t * );
+#endif
 
 /*****************************************************************************
  * Open: probe the decoder and return score
@@ -217,51 +230,85 @@ static int Open( vlc_object_t *p_this )
 {
     decoder_t *p_dec = (decoder_t*)p_this;
 
+#ifdef __APPLE__
+    OSErr err;
+    SInt32 qtVersion, macosversion;
+
+    err = Gestalt(gestaltQuickTimeVersion, &qtVersion);
+    err = Gestalt(gestaltSystemVersion, &macosversion);
+#ifndef NDEBUG
+    msg_Dbg( p_this, "Mac OS version is %#lx", macosversion );
+    msg_Dbg( p_this, "Quicktime version is %#lx", qtVersion );
+#endif
+
+    /* bail out. This plugin is soo Carbon, that it can't be used on 10.5 at all */
+    msg_Info( p_dec, "Your Mac OS version is to new to use this plugin for anything." );
+    return VLC_EGENERIC;
+#endif
+
     switch( p_dec->fmt_in.i_codec )
     {
-        case VLC_FOURCC('S','V','Q','3'): /* Sorenson v3 */
-    /*    case VLC_FOURCC('S','V','Q','1'):  Sorenson v1 
+        case VLC_CODEC_H264:
+        case VLC_CODEC_CINEPAK:
+        case VLC_FOURCC('I','V','4','1'): /* Indeo Video IV */
+        case VLC_FOURCC('i','v','4','1'): /* dto. */
+#ifdef __APPLE__
+        case VLC_FOURCC('p','x','l','t'): /* Pixlet */
+#endif
+        case VLC_CODEC_DV:
+        case VLC_CODEC_SVQ3: /* Sorenson v3 */
+    /*    case VLC_CODEC_SVQ1:  Sorenson v1
         case VLC_FOURCC('Z','y','G','o'):
         case VLC_FOURCC('V','P','3','1'):
         case VLC_FOURCC('3','I','V','1'): */
-        case VLC_FOURCC('r','l','e',' '): /* QuickTime animation (RLE) */
-        case VLC_FOURCC('r','p','z','a'): /* QuickTime Apple Video */
-        case VLC_FOURCC('a','z','p','r'): /* QuickTime animation (RLE) */
+        case VLC_CODEC_QTRLE:
+        case VLC_CODEC_RPZA:
 #ifdef LOADER
-            p_dec->p_sys = NULL;
-            p_dec->pf_decode_video = DecodeVideo;
-            return VLC_SUCCESS;
+        p_dec->p_sys = NULL;
+        p_dec->pf_decode_video = DecodeVideo;
+        p_dec->fmt_out.i_cat = VIDEO_ES;
+        return VLC_SUCCESS;
 #else
-            return OpenVideo( p_dec );
+        return OpenVideo( p_dec );
 #endif
 
-        case VLC_FOURCC('s','a','m','r'): /* 3GPP AMR audio */
-        case VLC_FOURCC('m','p','4','a'): /* MPEG-4 audio */
+#ifdef __APPLE__
+        case VLC_FOURCC('I','L','B','C'): /* iLBC */
+            if ((err != noErr) || (qtVersion < 0x07500000)) 
+                return VLC_EGENERIC;
+        case VLC_FOURCC('i','l','b','c'): /* iLBC */
+            if ((err != noErr) || (qtVersion < 0x07500000)) 
+                return VLC_EGENERIC;
+#endif
+        case VLC_CODEC_AMR_NB: /* 3GPP AMR audio */
+        case VLC_FOURCC('s','a','m','b'): /* 3GPP AMR-WB audio */
+        case VLC_CODEC_MP4A: /* MPEG-4 audio */
         case VLC_FOURCC('Q','D','M','C'): /* QDesign */
-        case VLC_FOURCC('Q','D','M','2'): /* QDesign* 2 */
-        case VLC_FOURCC('Q','c','l','p'): /* Qualcomm Purevoice Codec */
+        case VLC_CODEC_QDM2: /* QDesign* 2 */
+        case VLC_CODEC_QCELP: /* Qualcomm Purevoice Codec */
         case VLC_FOURCC('Q','C','L','P'): /* Qualcomm Purevoice Codec */
-        case VLC_FOURCC('M','A','C','3'): /* MACE3 audio decoder */
-        case VLC_FOURCC('M','A','C','6'): /* MACE6 audio decoder */
+        case VLC_CODEC_MACE3: /* MACE3 audio decoder */
+        case VLC_CODEC_MACE6: /* MACE6 audio decoder */
         case VLC_FOURCC('d','v','c','a'): /* DV Audio */
         case VLC_FOURCC('s','o','w','t'): /* 16-bit Little Endian */
         case VLC_FOURCC('t','w','o','s'): /* 16-bit Big Endian */
-        case VLC_FOURCC('a','l','a','w'): /* ALaw 2:1 */
+        case VLC_CODEC_ALAW: /* ALaw 2:1 */
         case VLC_FOURCC('u','l','a','w'): /* mu-Law 2:1 */
         case VLC_FOURCC('r','a','w',' '): /* 8-bit offset binaries */
-        case VLC_FOURCC('f','l','3','2'): /* 32-bit Floating Point */
-        case VLC_FOURCC('f','l','6','4'): /* 64-bit Floating Point */
+        case VLC_CODEC_FL32: /* 32-bit Floating Point */
+        case VLC_CODEC_FL64: /* 64-bit Floating Point */
         case VLC_FOURCC('i','n','2','4'): /* 24-bit Interger */
         case VLC_FOURCC('i','n','3','2'): /* 32-bit Integer */
         case 0x0011:                            /* DVI IMA */
         case 0x6D730002:                        /* Microsoft ADPCM-ACM */
         case 0x6D730011:                        /* DVI Intel IMAADPCM-ACM */
 #ifdef LOADER
-            p_dec->p_sys = NULL;
-            p_dec->pf_decode_audio = DecodeAudio;
-            return VLC_SUCCESS;
+        p_dec->p_sys = NULL;
+        p_dec->pf_decode_audio = DecodeAudio;
+        p_dec->fmt_out.i_cat = AUDIO_ES;
+        return VLC_SUCCESS;
 #else
-            return OpenAudio( p_dec );
+        return OpenAudio( p_dec );
 #endif
 
         default:
@@ -269,6 +316,8 @@ static int Open( vlc_object_t *p_this )
     }
 }
 
+static vlc_mutex_t qt_mutex = VLC_STATIC_MUTEX;
+
 /*****************************************************************************
  * Close:
  *****************************************************************************/
@@ -276,15 +325,9 @@ static void Close( vlc_object_t *p_this )
 {
     decoder_t     *p_dec = (decoder_t*)p_this;
     decoder_sys_t *p_sys = p_dec->p_sys;
-    vlc_value_t   lockval;
 
     /* get lock, avoid segfault */
-    var_Get( p_dec->p_libvlc, "qt_mutex", &lockval );
-    vlc_mutex_lock( lockval.p_address );
-#ifdef SYS_DARWIN
-    /* on OS X QT is not threadsafe */
-    vlc_mutex_lock( &p_dec->p_vlc->quicktime_lock );
-#endif
+    vlc_mutex_lock( &qt_mutex );
 
     if( p_dec->fmt_out.i_cat == AUDIO_ES )
     {
@@ -307,12 +350,11 @@ static void Close( vlc_object_t *p_this )
         free( p_sys->plane );
     }
 
-#ifndef SYS_DARWIN
+#ifndef __APPLE__
     FreeLibrary( p_sys->qtml );
+    FreeLibrary( p_sys->qts );
     msg_Dbg( p_dec, "FreeLibrary ok." );
-#endif
-
-#ifdef SYS_DARWIN
+#else
     ExitMovies();
 #endif
 
@@ -324,10 +366,7 @@ static void Close( vlc_object_t *p_this )
 #endif
 #endif
 
-#ifdef SYS_DARWIN
-    vlc_mutex_unlock( &p_dec->p_vlc->quicktime_lock );
-#endif
-    vlc_mutex_unlock( lockval.p_address );
+    vlc_mutex_unlock( &qt_mutex );
 
     free( p_sys );
 }
@@ -337,31 +376,27 @@ static void Close( vlc_object_t *p_this )
  *****************************************************************************/
 static int OpenAudio( decoder_t *p_dec )
 {
-    decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) );
+    decoder_sys_t *p_sys;
 
-    vlc_value_t     lockval;
     int             i_error;
     char            fcc[4];
     unsigned long   WantedBufferSize;
     unsigned long   InputBufferSize = 0;
     unsigned long   OutputBufferSize = 0;
 
-    memset( p_sys, 0, sizeof( decoder_sys_t ) );
+    /* get lock, avoid segfault */
+    vlc_mutex_lock( &qt_mutex );
 
+    p_sys = calloc( 1, sizeof( decoder_sys_t ) );
     p_dec->p_sys = p_sys;
     p_dec->pf_decode_audio = DecodeAudio;
 
-    memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
-
-    /* get lock, avoid segfault */
-    var_Get( p_dec->p_libvlc, "qt_mutex", &lockval );
-    vlc_mutex_lock( lockval.p_address );
-#ifdef SYS_DARWIN
-    /* on OS X QT is not threadsafe */
-    vlc_mutex_lock( &p_dec->p_vlc->quicktime_lock );
-#endif
+    if( p_dec->fmt_in.i_original_fourcc )
+        memcpy( fcc, &p_dec->fmt_in.i_original_fourcc, 4 );
+    else
+        memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
 
-#ifdef SYS_DARWIN
+#ifdef __APPLE__
     EnterMovies();
 #endif
 
@@ -371,10 +406,10 @@ static int OpenAudio( decoder_t *p_dec )
         goto exit_error;
     }
 
-#ifndef SYS_DARWIN
+#ifndef __APPLE__
     if( ( i_error = p_sys->InitializeQTML( 6 + 16 ) ) )
     {
-        msg_Dbg( p_dec, "error while InitializeQTML = %d", i_error );
+        msg_Dbg( p_dec, "error on InitializeQTML = %d", i_error );
         goto exit_error;
     }
 #endif
@@ -405,7 +440,7 @@ static int OpenAudio( decoder_t *p_dec )
                                          &p_sys->myConverter );
     if( i_error )
     {
-        msg_Err( p_dec, "error while SoundConverterOpen = %d", i_error );
+        msg_Err( p_dec, "error on SoundConverterOpen = %d", i_error );
         goto exit_error;
     }
 
@@ -421,7 +456,7 @@ static int OpenAudio( decoder_t *p_dec )
                                                 FCC( 'w', 'a', 'v', 'e' ),
                                                 ((uint8_t*)p_dec->fmt_in.p_extra) + 36 + 8 );
 
-        msg_Dbg( p_dec, "error while SoundConverterSetInfo = %d", i_error );
+        msg_Dbg( p_dec, "error on SoundConverterSetInfo = %d", i_error );
     }
 
     WantedBufferSize = p_sys->OutputFormatInfo.numChannels *
@@ -449,31 +484,30 @@ static int OpenAudio( decoder_t *p_dec )
     if( (i_error = p_sys->SoundConverterBeginConversion(p_sys->myConverter)) )
     {
         msg_Err( p_dec,
-                 "error while SoundConverterBeginConversion = %d", i_error );
+                 "error on SoundConverterBeginConversion = %d", i_error );
         goto exit_error;
     }
 
 
-    es_format_Init( &p_dec->fmt_out, AUDIO_ES, AOUT_FMT_S16_NE );
+    es_format_Init( &p_dec->fmt_out, AUDIO_ES, VLC_CODEC_S16N );
     p_dec->fmt_out.audio.i_rate = p_sys->OutputFormatInfo.sampleRate;
     p_dec->fmt_out.audio.i_channels = p_sys->OutputFormatInfo.numChannels;
     p_dec->fmt_out.audio.i_physical_channels =
     p_dec->fmt_out.audio.i_original_channels =
         pi_channels_maps[p_sys->OutputFormatInfo.numChannels];
 
-    aout_DateInit( &p_sys->date, p_dec->fmt_out.audio.i_rate );
+    date_Init( &p_sys->date, p_dec->fmt_out.audio.i_rate, 1 );
 
     p_sys->i_buffer      = 0;
     p_sys->i_buffer_size = 100*1000;
     p_sys->p_buffer      = malloc( p_sys->i_buffer_size );
+    if( !p_sys->p_buffer )
+        goto exit_error;
 
     p_sys->i_out = 0;
     p_sys->i_out_frames = 0;
 
-#ifdef SYS_DARWIN
-    vlc_mutex_unlock( &p_dec->p_vlc->quicktime_lock );
-#endif
-    vlc_mutex_unlock( lockval.p_address );
+    vlc_mutex_unlock( &qt_mutex );
     return VLC_SUCCESS;
 
 exit_error:
@@ -481,10 +515,9 @@ exit_error:
 #ifdef LOADER
     Restore_LDT_Keeper( p_sys->ldt_fs );
 #endif
-#ifdef SYS_DARWIN
-    vlc_mutex_unlock( &p_dec->p_vlc->quicktime_lock );
-#endif
-    vlc_mutex_unlock( lockval.p_address );
+    vlc_mutex_unlock( &qt_mutex );
+
+    free( p_sys );
     return VLC_EGENERIC;
 }
 
@@ -495,7 +528,6 @@ static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
 
-    vlc_value_t lockval;
     block_t     *p_block;
     int         i_error;
 
@@ -507,7 +539,7 @@ static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
         if( OpenAudio( p_dec ) )
         {
             /* Fatal */
-            p_dec->b_error = VLC_TRUE;
+            p_dec->b_error = true;
             return NULL;
         }
 
@@ -533,7 +565,13 @@ static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
 
     if( p_sys->i_out_frames <= 0 )
     {
-        if( ( p_sys->pts = p_block->i_pts ) < mdate() )
+        p_sys->pts = p_block->i_pts;
+
+        mtime_t i_display_date = 0;
+        if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
+            i_display_date = decoder_GetDisplayDate( p_dec, p_block->i_pts );
+
+        if( i_display_date > 0 && i_display_date < mdate() )
         {
             block_Release( p_block );
             *pp_block = NULL;
@@ -544,7 +582,7 @@ static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
         if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
         {
             p_sys->i_buffer_size = p_sys->i_buffer + p_block->i_buffer + 1024;
-            p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
+            p_sys->p_buffer = xrealloc( p_sys->p_buffer, p_sys->i_buffer_size );
         }
         memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer,
                 p_block->i_buffer );
@@ -553,17 +591,16 @@ static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
         if( p_sys->i_buffer > p_sys->InFrameSize )
         {
             int i_frames = p_sys->i_buffer / p_sys->InFrameSize;
-            long i_out_frames, i_out_bytes;
+            unsigned long i_out_frames, i_out_bytes;
+            vlc_mutex_lock( &qt_mutex );
 
-            var_Get( p_dec->p_libvlc, "qt_mutex", &lockval );
-            vlc_mutex_lock( lockval.p_address );
             i_error = p_sys->SoundConverterConvertBuffer( p_sys->myConverter,
                                                           p_sys->p_buffer,
                                                           i_frames,
                                                           p_sys->out_buffer,
                                                           &i_out_frames,
                                                           &i_out_bytes );
-            vlc_mutex_unlock( lockval.p_address );
+            vlc_mutex_unlock( &qt_mutex );
 
             /*
             msg_Dbg( p_dec, "decoded %d frames -> %ld frames (error=%d)",
@@ -581,12 +618,12 @@ static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
                          p_sys->i_buffer );
             }
 
-            if( p_sys->pts != 0 &&
-                p_sys->pts != aout_DateGet( &p_sys->date ) )
+            if( p_sys->pts > VLC_TS_INVALID &&
+                p_sys->pts != date_Get( &p_sys->date ) )
             {
-                aout_DateSet( &p_sys->date, p_sys->pts );
+                date_Set( &p_sys->date, p_sys->pts );
             }
-            else if( !aout_DateGet( &p_sys->date ) )
+            else if( !date_Get( &p_sys->date ) )
             {
                 return NULL;
             }
@@ -605,16 +642,17 @@ static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
         aout_buffer_t *p_out;
         int  i_frames = __MIN( p_sys->i_out_frames - p_sys->i_out, 1000 );
 
-        p_out = p_dec->pf_aout_buffer_new( p_dec, i_frames );
+        p_out = decoder_NewAudioBuffer( p_dec, i_frames );
 
         if( p_out )
         {
-            p_out->start_date = aout_DateGet( &p_sys->date );
-            p_out->end_date = aout_DateIncrement( &p_sys->date, i_frames );
+            p_out->i_pts = date_Get( &p_sys->date );
+            p_out->i_length = date_Increment( &p_sys->date, i_frames )
+                              - p_out->i_pts;
 
             memcpy( p_out->p_buffer,
                     &p_sys->out_buffer[2 * p_sys->i_out * p_dec->fmt_out.audio.i_channels],
-                    p_out->i_nb_bytes );
+                    p_out->i_buffer );
 
             p_sys->i_out += i_frames;
         }
@@ -629,9 +667,11 @@ static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
  *****************************************************************************/
 static int OpenVideo( decoder_t *p_dec )
 {
+#ifndef WIN32
     decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) );
+    if( !p_sys )
+        return VLC_ENOMEM;
 
-    vlc_value_t                         lockval;
     long                                i_result;
     ComponentDescription                desc;
     Component                           prev;
@@ -655,15 +695,18 @@ static int OpenVideo( decoder_t *p_dec )
         return VLC_EGENERIC;
     }
 
-    memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
+    if( p_dec->fmt_in.i_original_fourcc )
+        memcpy( fcc, &p_dec->fmt_in.i_original_fourcc, 4 );
+    else
+        memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
+
     msg_Dbg( p_dec, "quicktime_video %4.4s %dx%d",
              fcc, p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
 
     /* get lock, avoid segfault */
-    var_Get( p_dec->p_libvlc, "qt_mutex", &lockval );
-    vlc_mutex_lock( lockval.p_address );
+    vlc_mutex_lock( &qt_mutex );
 
-#ifdef SYS_DARWIN
+#ifdef __APPLE__
     EnterMovies();
 #endif
 
@@ -673,10 +716,10 @@ static int OpenVideo( decoder_t *p_dec )
         goto exit_error;
     }
 
-#ifndef SYS_DARWIN
+#ifndef __APPLE__
     if( ( i_result = p_sys->InitializeQTML( 6 + 16 ) ) )
     {
-        msg_Dbg( p_dec, "error while InitializeQTML = %d", (int)i_result );
+        msg_Dbg( p_dec, "error on InitializeQTML = %d", (int)i_result );
         goto exit_error;
     }
 #endif
@@ -701,14 +744,17 @@ static int OpenVideo( decoder_t *p_dec )
 
     memset( &icap, 0, sizeof( ImageSubCodecDecompressCapabilities ) );
     cres =  p_sys->ImageCodecInitialize( p_sys->ci, &icap );
-/*    msg_Dbg( p_dec->p_fifo, "ImageCodecInitialize->%p  size=%d (%d)\n",cres,icap.recordSize,icap.decompressRecordSize); */
+    msg_Dbg( p_dec, "ImageCodecInitialize->0x%X size=%d (%d)",
+             (int)cres, (int)icap.recordSize, (int)icap.decompressRecordSize);
 
     memset( &cinfo, 0, sizeof( CodecInfo ) );
     cres =  p_sys->ImageCodecGetCodecInfo( p_sys->ci, &cinfo );
     msg_Dbg( p_dec,
-             "Flags: compr: 0x%lx  decomp: 0x%lx format: 0x%lx\n",
-             cinfo.compressFlags, cinfo.decompressFlags, cinfo.formatFlags );
-    msg_Dbg( p_dec, "quicktime_video: Codec name: %.*s\n",
+             "Flags: compr: 0x%x decomp: 0x%x format: 0x%x",
+             (unsigned int)cinfo.compressFlags,
+             (unsigned int)cinfo.decompressFlags,
+             (unsigned int)cinfo.formatFlags );
+    msg_Dbg( p_dec, "quicktime_video: Codec name: %.*s",
              ((unsigned char*)&cinfo.typeName)[0],
              ((unsigned char*)&cinfo.typeName)+1 );
 
@@ -722,6 +768,8 @@ static int OpenVideo( decoder_t *p_dec )
     /* codec data FIXME use codec not SVQ3 */
     msg_Dbg( p_dec, "vide = %d", i_vide  );
     id = malloc( sizeof( ImageDescription ) + ( i_vide - 70 ) );
+    if( !id )
+        goto exit_error;
     id->idSize          = sizeof( ImageDescription ) + ( i_vide - 70 );
     id->cType           = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
     id->version         = GetWBE ( p_vide +  0 );
@@ -743,21 +791,24 @@ static int OpenVideo( decoder_t *p_dec )
         memcpy( ((char*)&id->clutID) + 2, p_vide + 70, i_vide - 70 );
     }
 
-    msg_Dbg( p_dec, "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",
-             id->idSize, id->version, id->revisionLevel, id->vendor,
+    msg_Dbg( p_dec, "idSize=%d ver=%d rev=%d vendor=%d tempQ=%d "
+             "spaQ=%d w=%d h=%d dpi=%d%d dataSize=%d depth=%d frameCount=%d clutID=%d",
+             (int)id->idSize, id->version, id->revisionLevel, (int)id->vendor,
              (int)id->temporalQuality, (int)id->spatialQuality,
-             id->width, id->height,
+             (int)id->width, (int)id->height,
              (int)id->hRes, (int)id->vRes,
              (int)id->dataSize,
+             id->depth,
              id->frameCount,
              id->clutID );
 
-    p_sys->framedescHandle =
-        (ImageDescriptionHandle) p_sys->NewHandleClear( id->idSize );
+    p_sys->framedescHandle = (ImageDescriptionHandle) NewHandleClear( id->idSize );
     memcpy( *p_sys->framedescHandle, id, id->idSize );
 
-    p_sys->plane = malloc( p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height * 3 );
+    if( p_dec->fmt_in.video.i_width != 0 && p_dec->fmt_in.video.i_height != 0) 
+        p_sys->plane = malloc( p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height * 3 );
+    if( !p_sys->plane )
+        goto exit_error;
 
     i_result = p_sys->QTNewGWorldFromPtr( &p_sys->OutBufferGWorld,
                                           /*pixel format of new GWorld==YUY2 */
@@ -769,7 +820,7 @@ static int OpenVideo( decoder_t *p_dec )
                                           p_sys->plane,
                                           p_dec->fmt_in.video.i_width * 2 );
 
-    msg_Dbg( p_dec, "NewGWorldFromPtr returned:%ld\n",
+    msg_Dbg( p_dec, "NewGWorldFromPtr returned:%ld",
              65536 - ( i_result&0xffff ) );
 
     memset( &p_sys->decpar, 0, sizeof( CodecDecompressParams ) );
@@ -787,35 +838,42 @@ static int OpenVideo( decoder_t *p_dec )
     p_sys->decpar.dstPixMap        = **p_sys->GetGWorldPixMap( p_sys->OutBufferGWorld );/*destPixmap;  */
 
     cres =  p_sys->ImageCodecPreDecompress( p_sys->ci, &p_sys->decpar );
-    msg_Dbg( p_dec, "quicktime_video: ImageCodecPreDecompress cres=0x%X\n",
+    msg_Dbg( p_dec, "quicktime_video: ImageCodecPreDecompress cres=0x%X",
              (int)cres );
 
-    p_dec->fmt_out.i_codec = VLC_FOURCC( 'Y', 'U', 'Y', '2' );
+    es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_CODEC_YUYV);
     p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
     p_dec->fmt_out.video.i_height= p_dec->fmt_in.video.i_height;
-    p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height;
-
+    p_dec->fmt_out.video.i_sar_num = 1;
+    p_dec->fmt_out.video.i_sar_den = 1;
 
-    vlc_mutex_unlock( lockval.p_address );
+    vlc_mutex_unlock( &qt_mutex );
     return VLC_SUCCESS;
 
 exit_error:
 #ifdef LOADER
     Restore_LDT_Keeper( p_sys->ldt_fs );
 #endif
-    vlc_mutex_unlock( lockval.p_address );
+    vlc_mutex_unlock( &qt_mutex );
+
+#else
+    VLC_UNUSED( p_dec );
+#endif /* !WIN32 */
+
     return VLC_EGENERIC;
 }
 
+#ifndef WIN32
 /*****************************************************************************
  * DecodeVideo:
  *****************************************************************************/
 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
+    block_t       *p_block;
+    picture_t     *p_pic;
+    mtime_t       i_pts;
 
-    block_t         *p_block;
-    picture_t       *p_pic;
     ComponentResult cres;
 
 #ifdef LOADER
@@ -826,7 +884,7 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
         if( OpenVideo( p_dec ) )
         {
             /* Fatal */
-            p_dec->b_error = VLC_TRUE;
+            p_dec->b_error = true;
             return NULL;
         }
         p_sys = p_dec->p_sys;
@@ -839,8 +897,14 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
     }
     p_block = *pp_block;
     *pp_block = NULL;
+    i_pts = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;
 
-    if( p_block->i_pts < mdate() )
+    mtime_t i_display_date = 0;
+    if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
+        i_display_date = decoder_GetDisplayDate( p_dec, i_pts );
+
+    if( i_display_date > 0 && i_display_date < mdate() )
     {
         p_sys->i_late++;
     }
@@ -848,45 +912,47 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
     {
         p_sys->i_late = 0;
     }
+#ifndef NDEBUG
+    msg_Dbg( p_dec, "bufsize: %d", (int)p_block->i_buffer);
+#endif
 
     if( p_sys->i_late > 10 )
     {
-        msg_Dbg( p_dec, "too late buffer -> dropped" );
+        msg_Dbg( p_dec, "late buffer dropped (%"PRId64")", i_pts );
         block_Release( p_block );
         return NULL;
     }
+    vlc_mutex_lock( &qt_mutex );
 
-    if( ( p_pic = p_dec->pf_vout_buffer_new( p_dec ) ) )
+    if( ( p_pic = decoder_NewPicture( p_dec ) ) )
     {
-        vlc_value_t     lockval;
-
-        p_sys->decpar.data                  = p_block->p_buffer;
+        p_sys->decpar.data                  = (Ptr)p_block->p_buffer;
         p_sys->decpar.bufferSize            = p_block->i_buffer;
         (**p_sys->framedescHandle).dataSize = p_block->i_buffer;
 
-        var_Get( p_dec->p_libvlc, "qt_mutex", &lockval );
-        vlc_mutex_lock( lockval.p_address );
         cres = p_sys->ImageCodecBandDecompress( p_sys->ci, &p_sys->decpar );
-        vlc_mutex_unlock( lockval.p_address );
 
         ++p_sys->decpar.frameNumber;
 
         if( cres &0xFFFF )
         {
             msg_Dbg( p_dec, "quicktime_video: ImageCodecBandDecompress"
-                     " cres=0x%X (-0x%X) %d :(\n",
+                     " cres=0x%X (-0x%X) %d :(",
                      (int)cres,(int)-cres, (int)cres );
         }
 
-        memcpy( p_pic->p[0].p_pixels,
-                p_sys->plane,
+        memcpy( p_pic->p[0].p_pixels, p_sys->plane,
                 p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height * 2 );
-        p_pic->date = p_block->i_pts;
+        p_pic->date = i_pts;
     }
-    block_Release( p_block );
+    vlc_mutex_unlock( &qt_mutex );
 
+    block_Release( p_block );
     return p_pic;
 }
+#endif /* !WIN32 */
 
 /*****************************************************************************
  * QTAudioInit:
@@ -895,7 +961,7 @@ static int QTAudioInit( decoder_t *p_dec )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
 
-#ifdef SYS_DARWIN
+#ifdef __APPLE__
     p_sys->SoundConverterOpen       = (void*)SoundConverterOpen;
     p_sys->SoundConverterClose      = (void*)SoundConverterClose;
     p_sys->SoundConverterSetInfo    = (void*)SoundConverterSetInfo;
@@ -909,6 +975,12 @@ static int QTAudioInit( decoder_t *p_dec )
     p_sys->ldt_fs = Setup_LDT_Keeper();
 #endif /* LOADER */
 
+    p_sys->qts = LoadLibraryA( "QuickTime.qts" );
+    if( p_sys->qts == NULL )
+    {
+        msg_Dbg( p_dec, "failed loading QuickTime.qts" );
+        return VLC_EGENERIC;
+    }
     p_sys->qtml = LoadLibraryA( "qtmlClient.dll" );
     if( p_sys->qtml == NULL )
     {
@@ -928,7 +1000,7 @@ static int QTAudioInit( decoder_t *p_dec )
 
     if( p_sys->InitializeQTML == NULL )
     {
-        msg_Err( p_dec, "failed geting proc address InitializeQTML" );
+        msg_Err( p_dec, "failed getting proc address InitializeQTML" );
         return VLC_EGENERIC;
     }
     if( p_sys->SoundConverterOpen == NULL ||
@@ -939,17 +1011,17 @@ static int QTAudioInit( decoder_t *p_dec )
         p_sys->SoundConverterEndConversion == NULL ||
         p_sys->SoundConverterBeginConversion == NULL )
     {
-        msg_Err( p_dec, "failed geting proc address" );
+        msg_Err( p_dec, "failed getting proc address" );
         return VLC_EGENERIC;
     }
 
-    msg_Dbg( p_dec, "Standard init done" );
-#endif /* else SYS_DARWIN */
+    msg_Dbg( p_dec, "standard init done" );
+#endif /* else __APPLE__ */
 
     return VLC_SUCCESS;
 }
 
-
+#ifndef WIN32
 /*****************************************************************************
  * QTVideoInit:
  *****************************************************************************/
@@ -957,7 +1029,7 @@ static int QTVideoInit( decoder_t *p_dec )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
 
-#ifdef SYS_DARWIN
+#ifdef __APPLE__
     p_sys->FindNextComponent        = (void*)FindNextComponent;
     p_sys->OpenComponent            = (void*)OpenComponent;
     p_sys->ImageCodecInitialize     = (void*)ImageCodecInitialize;
@@ -972,6 +1044,13 @@ static int QTVideoInit( decoder_t *p_dec )
 #ifdef LOADER
     p_sys->ldt_fs = Setup_LDT_Keeper();
 #endif /* LOADER */
+    p_sys->qts = LoadLibraryA( "QuickTime.qts" );
+    if( p_sys->qts == NULL )
+    {
+        msg_Dbg( p_dec, "failed loading QuickTime.qts" );
+        return VLC_EGENERIC;
+    }
+    msg_Dbg( p_dec, "QuickTime.qts loaded" );
     p_sys->qtml = LoadLibraryA( "qtmlClient.dll" );
     if( p_sys->qtml == NULL )
     {
@@ -994,7 +1073,7 @@ static int QTVideoInit( decoder_t *p_dec )
 
     if( p_sys->InitializeQTML == NULL )
     {
-        msg_Dbg( p_dec, "failed geting proc address InitializeQTML" );
+        msg_Dbg( p_dec, "failed getting proc address InitializeQTML" );
         return VLC_EGENERIC;
     }
     if( p_sys->FindNextComponent == NULL ||
@@ -1007,12 +1086,11 @@ static int QTVideoInit( decoder_t *p_dec )
         p_sys->QTNewGWorldFromPtr == NULL ||
         p_sys->NewHandleClear == NULL )
     {
-        msg_Err( p_dec, "failed geting proc address" );
+        msg_Err( p_dec, "failed getting proc address" );
         return VLC_EGENERIC;
     }
-#endif /* SYS_DARWIN */
+#endif /* __APPLE__ */
 
     return VLC_SUCCESS;
 }
-
-
+#endif /* !WIN32 */