Merge ../mlt
[melted] / src / modules / kino / filehandler.cc
index 67412fd..e5e552c 100644 (file)
 
 #include "config.h"
 
+extern "C" {
+#include <framework/mlt_frame.h>
+}
+
 #include <string>
 #include <iostream>
 #include <sstream>
@@ -37,6 +41,8 @@ using std::setfill;
 #include <assert.h>
 #include <time.h>
 #include <sys/time.h>
+#include <string.h>
+#include <stdlib.h>
 
 // libdv header files
 #ifdef HAVE_LIBDV
@@ -693,6 +699,19 @@ bool AVIHandler::GetOpenDML() const
 #define DV_AUDIO_MAX_SAMPLES 1944
 #endif
 
+// Missing fourcc's in libquicktime (allows compilation)
+#ifndef QUICKTIME_DV_AVID
+#define QUICKTIME_DV_AVID "AVdv"
+#endif
+
+#ifndef QUICKTIME_DV_AVID_A
+#define QUICKTIME_DV_AVID_A "dvcp"
+#endif
+
+#ifndef QUICKTIME_DVCPRO
+#define QUICKTIME_DVCPRO "dvpp"
+#endif
+
 QtHandler::QtHandler() : fd( NULL )
 {
        extension = ".mov";
@@ -839,7 +858,7 @@ int QtHandler::Close()
 off_t QtHandler::GetFileSize()
 {
        struct stat file_status;
-       fstat( fileno( fd->stream ), &file_status );
+       stat( filename.c_str(), &file_status );
        return file_status.st_size;
 }
 
@@ -854,7 +873,7 @@ bool QtHandler::Open( const char *s )
 {
        Init();
 
-       fd = quicktime_open( ( char * ) s, 1, 0 );
+       fd = quicktime_open( s, 1, 0 );
        if ( fd == NULL )
        {
                fprintf( stderr, "Error opening: %s\n", s );
@@ -871,7 +890,8 @@ bool QtHandler::Open( const char *s )
        char * fcc = quicktime_video_compressor( fd, 0 );
        if ( strncmp( fcc, QUICKTIME_DV, 4 ) != 0 &&
             strncmp( fcc, QUICKTIME_DV_AVID, 4 ) != 0 &&
-            strncmp( fcc, QUICKTIME_DV_AVID_A, 4 ) != 0 )
+            strncmp( fcc, QUICKTIME_DV_AVID_A, 4 ) != 0 && 
+            strncmp( fcc, QUICKTIME_DVCPRO, 4 ) != 0 )
        {
                Close();
                return false;
@@ -895,15 +915,22 @@ int QtHandler::GetFrame( uint8_t *data, int frameNum )
                if ( ! isFullyInitialized )
                        AllocateAudioBuffers();
 
+               // Fetch the frequency of the audio track and calc number of samples needed
                int frequency = quicktime_sample_rate( fd, 0 );
-               int samples = ( int )( frequency / quicktime_frame_rate( fd, 0 ) );
-               for ( int i = 0; i < channels; i++ )
-               {
-                       quicktime_set_audio_position( fd, ( int64_t )( frameNum * samples ), 0 );
-                       quicktime_decode_audio( fd, audioChannelBuffer[ i ], NULL, (long) samples, i );
-               }
+               float fps = ( data[ 3 ] & 0x80 ) ? 25.0f : 29.97f;
+               int samples = mlt_sample_calculator( fps, frequency, frameNum );
+               int64_t seek = mlt_sample_calculator_to_now( fps, frequency, frameNum );
+
+               // Obtain a dv encoder and initialise it with minimal info
                dv_encoder_t *encoder = dv_encoder_new( 0, 0, 0 );
+               encoder->isPAL = ( data[ 3 ] & 0x80 );
                encoder->samples_this_frame = samples;
+
+               // Seek to the calculated position and decode
+               quicktime_set_audio_position( fd, seek, 0 );
+               lqt_decode_audio( fd, audioChannelBuffer, NULL, (long) samples );
+
+               // Encode the audio on the frame and done
                dv_encode_full_audio( encoder, audioChannelBuffer, channels, frequency, data );
                dv_encoder_free( encoder );
        }