2 * filehandler.cc -- saving DV data into different file formats
3 * Copyright (C) 2000 Arne Schirmacher <arne@schirmacher.de>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <framework/mlt_frame.h>
33 using std::ostringstream;
52 #include "filehandler.h"
57 FileTracker *FileTracker::instance = NULL;
59 FileTracker::FileTracker( ) : mode( CAPTURE_MOVIE_APPEND )
61 cerr << ">> Constructing File Capture tracker" << endl;
64 FileTracker::~FileTracker( )
66 cerr << ">> Destroying File Capture tracker" << endl;
69 FileTracker &FileTracker::GetInstance( )
71 if ( instance == NULL )
72 instance = new FileTracker();
77 void FileTracker::SetMode( FileCaptureMode mode )
82 FileCaptureMode FileTracker::GetMode( )
87 char *FileTracker::Get( int index )
92 void FileTracker::Add( const char *file )
94 if ( this->mode != CAPTURE_IGNORE )
96 cerr << ">>>> Registering " << file << " with the tracker" << endl;
97 list.push_back( strdup( file ) );
101 unsigned int FileTracker::Size( )
106 void FileTracker::Clear( )
110 free( list[ Size() - 1 ] );
113 this->mode = CAPTURE_MOVIE_APPEND;
116 FileHandler::FileHandler() : done( false ), autoSplit( false ), maxFrameCount( 999999 ),
117 framesWritten( 0 ), filename( "" )
123 FileHandler::~FileHandler()
129 bool FileHandler::GetAutoSplit() const
135 bool FileHandler::GetTimeStamp() const
141 string FileHandler::GetBaseName() const
147 string FileHandler::GetExtension() const
153 int FileHandler::GetMaxFrameCount() const
155 return maxFrameCount;
158 off_t FileHandler::GetMaxFileSize() const
163 string FileHandler::GetFilename() const
169 void FileHandler::SetAutoSplit( bool flag )
175 void FileHandler::SetTimeStamp( bool flag )
181 void FileHandler::SetBaseName( const string& s )
187 void FileHandler::SetMaxFrameCount( int count )
189 assert( count >= 0 );
190 maxFrameCount = count;
194 void FileHandler::SetEveryNthFrame( int every )
196 assert ( every > 0 );
198 everyNthFrame = every;
202 void FileHandler::SetMaxFileSize( off_t size )
204 assert ( size >= 0 );
210 void FileHandler::SetSampleFrame( const Frame& sample )
216 bool FileHandler::Done()
222 bool FileHandler::WriteFrame( const Frame& frame )
224 static TimeCode prevTimeCode;
227 /* If the user wants autosplit, start a new file if a
228 new recording is detected. */
229 prevTimeCode.sec = -1;
230 frame.GetTimeCode( timeCode );
231 int time_diff = timeCode.sec - prevTimeCode.sec;
232 bool discontinuity = prevTimeCode.sec != -1 && ( time_diff > 1 || ( time_diff < 0 && time_diff > -59 ) );
233 if ( FileIsOpen() && GetAutoSplit() == true && ( frame.IsNewRecording() || discontinuity ) )
238 if ( FileIsOpen() == false )
242 static int counter = 0;
244 if ( GetTimeStamp() == true )
246 ostringstream sb, sb2;
250 if ( ! frame.GetRecordingDate( date ) )
254 gettimeofday( &tv, &tz );
255 localtime_r( static_cast< const time_t * >( &tv.tv_sec ), &date );
258 << setw( 4 ) << date.tm_year + 1900 << '.'
259 << setw( 2 ) << date.tm_mon + 1 << '.'
260 << setw( 2 ) << date.tm_mday << '_'
261 << setw( 2 ) << date.tm_hour << '-'
262 << setw( 2 ) << date.tm_min << '-'
263 << setw( 2 ) << date.tm_sec;
265 sb2 << GetBaseName() << recDate << GetExtension();
266 filename = sb2.str();
267 cerr << ">>> Trying " << filename << endl;
275 sb << GetBaseName() << setfill( '0' ) << setw( 3 ) << ++ counter << GetExtension();
277 cerr << ">>> Trying " << filename << endl;
279 while ( stat( filename.c_str(), &stats ) == 0 );
282 SetSampleFrame( frame );
283 if ( Create( filename ) == false )
285 cerr << ">>> Error creating file!" << endl;
294 if ( framesToSkip == 0 )
296 if ( 0 > Write( frame ) )
298 cerr << ">>> Error writing frame!" << endl;
301 framesToSkip = everyNthFrame;
306 /* If the frame count is exceeded, close the current file.
307 If the autosplit flag is set, a new file will be created in the next iteration.
308 If the flag is not set, we are done. */
310 if ( ( GetMaxFrameCount() > 0 ) &&
311 ( framesWritten >= GetMaxFrameCount() ) )
314 done = !GetAutoSplit();
317 /* If the file size could be exceeded by another frame, close the current file.
318 If the autosplit flag is set, a new file will be created on the next iteration.
319 If the flag is not set, we are done. */
320 /* not exact, but should be good enough to prevent going over. */
324 frame.GetAudioInfo( info );
325 if ( ( GetFileSize() > 0 ) && ( GetMaxFileSize() > 0 ) &&
326 ( GetFileSize() + frame.GetFrameSize() + info.samples * 4 + 12 )
327 >= GetMaxFileSize() )
328 { // 12 = sizeof chunk metadata
330 done = !GetAutoSplit();
333 prevTimeCode.sec = timeCode.sec;
338 RawHandler::RawHandler() : fd( -1 )
344 RawHandler::~RawHandler()
350 bool RawHandler::FileIsOpen()
356 bool RawHandler::Create( const string& filename )
358 fd = open( filename.c_str(), O_CREAT | O_TRUNC | O_RDWR | O_NONBLOCK, 0644 );
361 FileTracker::GetInstance().Add( filename.c_str() );
362 this->filename = filename;
369 int RawHandler::Write( const Frame& frame )
371 int result = write( fd, frame.data, frame.GetFrameSize() );
376 int RawHandler::Close()
387 off_t RawHandler::GetFileSize()
389 struct stat file_status;
390 fstat( fd, &file_status );
391 return file_status.st_size;
394 int RawHandler::GetTotalFrames()
396 return GetFileSize() / ( 480 * numBlocks );
400 bool RawHandler::Open( const char *s )
402 unsigned char data[ 4 ];
404 fd = open( s, O_RDONLY | O_NONBLOCK );
407 if ( read( fd, data, 4 ) < 0 )
409 lseek( fd, 0, SEEK_SET );
410 numBlocks = ( ( data[ 3 ] & 0x80 ) == 0 ) ? 250 : 300;
416 int RawHandler::GetFrame( uint8_t *data, int frameNum )
419 int size = 480 * numBlocks;
422 off_t offset = ( ( off_t ) frameNum * ( off_t ) size );
423 fail_if( lseek( fd, offset, SEEK_SET ) == ( off_t ) - 1 );
424 if ( read( fd, data, size ) > 0 )
431 /***************************************************************************/
434 AVIHandler::AVIHandler( int format ) : avi( NULL ), aviFormat( format ), isOpenDML( false ),
435 fccHandler( make_fourcc( "dvsd" ) ), channels( 2 ), isFullyInitialized( false ),
439 for ( int c = 0; c < 4; c++ )
440 audioChannels[ c ] = NULL;
444 AVIHandler::~AVIHandler()
446 if ( audioBuffer != NULL )
451 for ( int c = 0; c < 4; c++ )
453 if ( audioChannels[ c ] != NULL )
455 delete audioChannels[ c ];
456 audioChannels[ c ] = NULL;
464 void AVIHandler::SetSampleFrame( const Frame& sample )
467 sample.GetAudioInfo( audioInfo );
468 sample.GetVideoInfo( videoInfo );
470 sample.GetAAUXPack( 0x50, pack );
471 dvinfo.dwDVAAuxSrc = *( DWORD* ) ( pack.data + 1 );
472 sample.GetAAUXPack( 0x51, pack );
473 dvinfo.dwDVAAuxCtl = *( DWORD* ) ( pack.data + 1 );
475 sample.GetAAUXPack( 0x52, pack );
476 dvinfo.dwDVAAuxSrc1 = *( DWORD* ) ( pack.data + 1 );
477 sample.GetAAUXPack( 0x53, pack );
478 dvinfo.dwDVAAuxCtl1 = *( DWORD* ) ( pack.data + 1 );
480 sample.GetVAUXPack( 0x60, pack );
481 dvinfo.dwDVVAuxSrc = *( DWORD* ) ( pack.data + 1 );
482 sample.GetVAUXPack( 0x61, pack );
483 dvinfo.dwDVVAuxCtl = *( DWORD* ) ( pack.data + 1 );
487 if ( sample.decoder->std == e_dv_std_smpte_314m )
488 fccHandler = make_fourcc( "dv25" );
493 bool AVIHandler::FileIsOpen()
499 bool AVIHandler::Create( const string& filename )
501 assert( avi == NULL );
507 fail_null( avi = new AVI1File );
508 if ( avi->Create( filename.c_str() ) == false )
510 //avi->Init( videoInfo.isPAL ? AVI_PAL : AVI_NTSC, audioInfo.frequency, AVI_LARGE_INDEX );
514 fail_null( avi = new AVI2File );
515 if ( avi->Create( filename.c_str() ) == false )
517 //if ( GetOpenDML() )
518 //avi->Init( videoInfo.isPAL ? AVI_PAL : AVI_NTSC, audioInfo.frequency,
519 //( AVI_SMALL_INDEX | AVI_LARGE_INDEX ) );
521 //avi->Init( videoInfo.isPAL ? AVI_PAL : AVI_NTSC, audioInfo.frequency,
522 //( AVI_SMALL_INDEX ) );
526 assert( aviFormat == AVI_DV1_FORMAT || aviFormat == AVI_DV2_FORMAT );
529 avi->setDVINFO( dvinfo );
530 avi->setFccHandler( make_fourcc( "iavs" ), fccHandler );
531 avi->setFccHandler( make_fourcc( "vids" ), fccHandler );
532 this->filename = filename;
533 FileTracker::GetInstance().Add( filename.c_str() );
534 return ( avi != NULL );
538 int AVIHandler::Write( const Frame& frame )
540 assert( avi != NULL );
543 return avi->WriteFrame( frame ) ? 0 : -1;
552 int AVIHandler::Close()
560 if ( audioBuffer != NULL )
565 for ( int c = 0; c < 4; c++ )
567 if ( audioChannels[ c ] != NULL )
569 delete audioChannels[ c ];
570 audioChannels[ c ] = NULL;
573 isFullyInitialized = false;
577 off_t AVIHandler::GetFileSize()
579 return avi->GetFileSize();
582 int AVIHandler::GetTotalFrames()
584 return avi->GetTotalFrames();
588 bool AVIHandler::Open( const char *s )
590 assert( avi == NULL );
591 fail_null( avi = new AVI1File );
592 if ( avi->Open( s ) )
596 avi->verifyStreamFormat( make_fourcc( "dvsd" ) ) ||
597 avi->verifyStreamFormat( make_fourcc( "DVSD" ) ) ||
598 avi->verifyStreamFormat( make_fourcc( "dvcs" ) ) ||
599 avi->verifyStreamFormat( make_fourcc( "DVCS" ) ) ||
600 avi->verifyStreamFormat( make_fourcc( "dvcp" ) ) ||
601 avi->verifyStreamFormat( make_fourcc( "DVCP" ) ) ||
602 avi->verifyStreamFormat( make_fourcc( "CDVC" ) ) ||
603 avi->verifyStreamFormat( make_fourcc( "cdvc" ) ) ||
604 avi->verifyStreamFormat( make_fourcc( "DV25" ) ) ||
605 avi->verifyStreamFormat( make_fourcc( "dv25" ) ) ) )
608 if ( avi->verifyStream( make_fourcc( "auds" ) ) )
609 aviFormat = AVI_DV2_FORMAT;
611 aviFormat = AVI_DV1_FORMAT;
612 isOpenDML = avi->isOpenDML();
621 int AVIHandler::GetFrame( uint8_t *data, int frameNum )
623 int result = avi->GetDVFrame( data, frameNum );
627 /* get the audio from the audio stream, if available */
628 if ( aviFormat == AVI_DV2_FORMAT )
632 if ( ! isFullyInitialized &&
633 avi->getStreamFormat( ( void* ) &wav, make_fourcc( "auds" ) ) )
635 if ( channels > 0 && channels < 5 )
637 // Allocate interleaved audio buffer
638 audioBuffer = new int16_t[ 2 * DV_AUDIO_MAX_SAMPLES * channels ];
640 // Allocate non-interleaved audio buffers
641 for ( int c = 0; c < channels; c++ )
642 audioChannels[ c ] = new int16_t[ 2 * DV_AUDIO_MAX_SAMPLES ];
644 // Get the audio parameters from AVI for subsequent calls to method
645 audioInfo.channels = wav.nChannels;
646 audioInfo.frequency = wav.nSamplesPerSec;
648 // Skip initialization on subsequent calls to method
649 isFullyInitialized = true;
650 cerr << ">>> using audio from separate AVI audio stream" << endl;
654 // Get the frame from AVI
655 int n = avi->getFrame( audioBuffer, frameNum, make_fourcc( "01wb" ) );
658 // Temporary pointer to audio scratch buffer
659 int16_t * s = audioBuffer;
661 // Determine samples in this frame
662 audioInfo.samples = n / audioInfo.channels / sizeof( int16_t );
664 // Convert interleaved audio into non-interleaved
665 for ( int n = 0; n < audioInfo.samples; ++n )
666 for ( int i = 0; i < audioInfo.channels; i++ )
667 audioChannels[ i ][ n ] = *s++;
669 // Write interleaved audio into frame
670 frame.EncodeAudio( audioInfo, audioChannels );
674 // Parse important metadata in DV bitstream
675 frame.ExtractHeader();
682 void AVIHandler::SetOpenDML( bool flag )
688 bool AVIHandler::GetOpenDML() const
694 /***************************************************************************/
696 #ifdef HAVE_LIBQUICKTIME
699 #define DV_AUDIO_MAX_SAMPLES 1944
702 // Missing fourcc's in libquicktime (allows compilation)
703 #ifndef QUICKTIME_DV_AVID
704 #define QUICKTIME_DV_AVID "AVdv"
707 #ifndef QUICKTIME_DV_AVID_A
708 #define QUICKTIME_DV_AVID_A "dvcp"
711 #ifndef QUICKTIME_DVCPRO
712 #define QUICKTIME_DVCPRO "dvpp"
715 QtHandler::QtHandler() : fd( NULL )
722 QtHandler::~QtHandler()
727 void QtHandler::Init()
734 samplesPerBuffer = 0;
737 audioChannelBuffer = NULL;
738 isFullyInitialized = false;
742 bool QtHandler::FileIsOpen()
748 bool QtHandler::Create( const string& filename )
752 if ( open( filename.c_str(), O_CREAT | O_TRUNC | O_RDWR | O_NONBLOCK, 0644 ) != -1 )
754 fd = quicktime_open( const_cast<char*>( filename.c_str() ), 0, 1 );
756 FileTracker::GetInstance().Add( filename.c_str() );
760 this->filename = filename;
764 void QtHandler::AllocateAudioBuffers()
766 if ( channels > 0 && channels < 5 )
768 audioBufferSize = DV_AUDIO_MAX_SAMPLES * 2;
769 audioBuffer = new int16_t[ audioBufferSize * channels ];
771 audioChannelBuffer = new short int * [ channels ];
772 for ( int c = 0; c < channels; c++ )
773 audioChannelBuffer[ c ] = new short int[ audioBufferSize ];
774 isFullyInitialized = true;
778 inline void QtHandler::DeinterlaceStereo16( void* pInput, int iBytes,
779 void* pLOutput, void* pROutput )
781 short int * piSampleInput = ( short int* ) pInput;
782 short int* piSampleLOutput = ( short int* ) pLOutput;
783 short int* piSampleROutput = ( short int* ) pROutput;
785 while ( ( char* ) piSampleInput < ( ( char* ) pInput + iBytes ) )
787 *piSampleLOutput++ = *piSampleInput++;
788 *piSampleROutput++ = *piSampleInput++;
793 int QtHandler::Write( const Frame& frame )
795 if ( ! isFullyInitialized )
799 if ( frame.GetAudioInfo( audio ) )
802 quicktime_set_audio( fd, channels, audio.frequency, 16, QUICKTIME_TWOS );
809 quicktime_set_video( fd, 1, 720, frame.IsPAL() ? 576 : 480,
810 frame.GetFrameRate(), QUICKTIME_DV );
811 AllocateAudioBuffers();
814 int result = quicktime_write_frame( fd, const_cast<unsigned char*>( frame.data ),
815 frame.GetFrameSize(), 0 );
820 if ( frame.GetAudioInfo( audio ) && ( unsigned int ) audio.samples < audioBufferSize )
822 long bytesRead = frame.ExtractAudio( audioBuffer );
824 DeinterlaceStereo16( audioBuffer, bytesRead,
825 audioChannelBuffer[ 0 ],
826 audioChannelBuffer[ 1 ] );
828 quicktime_encode_audio( fd, audioChannelBuffer, NULL, audio.samples );
835 int QtHandler::Close()
839 quicktime_close( fd );
842 if ( audioBuffer != NULL )
847 if ( audioChannelBuffer != NULL )
849 for ( int c = 0; c < channels; c++ )
850 delete audioChannelBuffer[ c ];
851 delete audioChannelBuffer;
852 audioChannelBuffer = NULL;
858 off_t QtHandler::GetFileSize()
860 struct stat file_status;
861 stat( filename.c_str(), &file_status );
862 return file_status.st_size;
866 int QtHandler::GetTotalFrames()
868 return ( int ) quicktime_video_length( fd, 0 );
872 bool QtHandler::Open( const char *s )
876 fd = quicktime_open( ( char * ) s, 1, 0 );
879 fprintf( stderr, "Error opening: %s\n", s );
883 if ( quicktime_has_video( fd ) <= 0 )
885 fprintf( stderr, "There must be at least one video track in the input file (%s).\n",
890 char * fcc = quicktime_video_compressor( fd, 0 );
891 if ( strncmp( fcc, QUICKTIME_DV, 4 ) != 0 &&
892 strncmp( fcc, QUICKTIME_DV_AVID, 4 ) != 0 &&
893 strncmp( fcc, QUICKTIME_DV_AVID_A, 4 ) != 0 &&
894 strncmp( fcc, QUICKTIME_DVCPRO, 4 ) != 0 )
899 if ( quicktime_has_audio( fd ) )
900 channels = quicktime_track_channels( fd, 0 );
905 int QtHandler::GetFrame( uint8_t *data, int frameNum )
907 assert( fd != NULL );
909 quicktime_set_video_position( fd, frameNum, 0 );
910 quicktime_read_frame( fd, data, 0 );
913 if ( quicktime_has_audio( fd ) )
915 if ( ! isFullyInitialized )
916 AllocateAudioBuffers();
918 // Fetch the frequency of the audio track and calc number of samples needed
919 int frequency = quicktime_sample_rate( fd, 0 );
920 float fps = ( data[ 3 ] & 0x80 ) ? 25.0f : 29.97f;
921 int samples = mlt_sample_calculator( fps, frequency, frameNum );
922 int64_t seek = mlt_sample_calculator_to_now( fps, frequency, frameNum );
924 // Obtain a dv encoder and initialise it with minimal info
925 dv_encoder_t *encoder = dv_encoder_new( 0, 0, 0 );
926 encoder->isPAL = ( data[ 3 ] & 0x80 );
927 encoder->samples_this_frame = samples;
929 // Seek to the calculated position and decode
930 quicktime_set_audio_position( fd, seek, 0 );
931 lqt_decode_audio( fd, audioChannelBuffer, NULL, (long) samples );
933 // Encode the audio on the frame and done
934 dv_encode_full_audio( encoder, audioChannelBuffer, channels, frequency, data );
935 dv_encoder_free( encoder );