Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

AVIHandler Class Reference

#include <filehandler.h>

Inheritance diagram for AVIHandler:

Inheritance graph
[legend]
Collaboration diagram for AVIHandler:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 AVIHandler (int format=AVI_DV1_FORMAT)
 ~AVIHandler ()
void SetSampleFrame (const Frame &sample)
bool FileIsOpen ()
bool Create (const string &filename)
int Write (const Frame &frame)
int Close ()
off_t GetFileSize ()
int GetTotalFrames ()
bool Open (const char *s)
int GetFrame (Frame &frame, int frameNum)
int16_t * SeekAudioFrame (Frame &frame, int frameNum, int &availableSize)
bool GetOpenDML () const
void SetOpenDML (bool)
int GetFormat () const

Protected Attributes

AVIFileavi
int aviFormat
AudioInfo audioInfo
VideoInfo videoInfo
bool isOpenDML
DVINFO dvinfo
FOURCC fccHandler
int channels
bool isFullyInitialized
int16_t * audioBuffer
int16_t * audioChannels [4]
bool isInterleave1to1
off_t audioOffset
int audioBufferSize

Constructor & Destructor Documentation

AVIHandler::AVIHandler int  format = AVI_DV1_FORMAT  ) 
 

Definition at line 428 of file filehandler.cc.

References audioChannels, and FileHandler::extension.

00428                                    : avi( NULL ), aviFormat( format ), isOpenDML( false ),
00429         fccHandler( make_fourcc( "dvsd" ) ), channels( 2 ), isFullyInitialized( false ),
00430         audioBuffer( NULL ), isInterleave1to1( true )
00431 {
00432     extension = ".avi";
00433     for ( int c = 0; c < 4; c++ )
00434         audioChannels[ c ] = NULL;
00435 }

AVIHandler::~AVIHandler  ) 
 

Definition at line 438 of file filehandler.cc.

References audioBuffer, audioChannels, and avi.

00439 {
00440     if ( audioBuffer != NULL )
00441     {
00442         delete[] audioBuffer;
00443         audioBuffer = NULL;
00444     }
00445     for ( int c = 0; c < 4; c++ )
00446     {
00447         if ( audioChannels[ c ] != NULL )
00448         {
00449             delete[] audioChannels[ c ];
00450             audioChannels[ c ] = NULL;
00451         }
00452     }
00453 
00454     delete avi;
00455 }


Member Function Documentation

int AVIHandler::Close  )  [virtual]
 

Implements FileHandler.

Definition at line 545 of file filehandler.cc.

References audioBuffer, audioChannels, avi, isFullyInitialized, and AVIFile::WriteRIFF().

00546 {
00547     if ( avi != NULL )
00548     {
00549         avi->WriteRIFF();
00550         delete avi;
00551         avi = NULL;
00552     }
00553     if ( audioBuffer != NULL )
00554     {
00555         delete audioBuffer;
00556         audioBuffer = NULL;
00557     }
00558     for ( int c = 0; c < 4; c++ )
00559     {
00560         if ( audioChannels[ c ] != NULL )
00561         {
00562             delete audioChannels[ c ];
00563             audioChannels[ c ] = NULL;
00564         }
00565     }
00566     isFullyInitialized = false;
00567     return 0;
00568 }

bool AVIHandler::Create const string &  filename  )  [virtual]
 

Implements FileHandler.

Definition at line 491 of file filehandler.cc.

References audioInfo, avi, AVI_DV1_FORMAT, AVI_DV2_FORMAT, AVI_LARGE_INDEX, AVI_NTSC, AVI_PAL, AVI_SMALL_INDEX, aviFormat, RIFFFile::Create(), dvinfo, fail_null, fccHandler, AudioInfo::frequency, FileTracker::GetInstance(), GetOpenDML(), AVIFile::Init(), VideoInfo::isPAL, make_fourcc(), AVIFile::setDVINFO(), AVIFile::setFccHandler(), and videoInfo.

00492 {
00493     assert( avi == NULL );
00494 
00495     switch ( aviFormat )
00496     {
00497 
00498     case AVI_DV1_FORMAT:
00499         fail_null( avi = new AVI1File );
00500         if ( avi->Create( filename.c_str() ) == false )
00501             return false;
00502         avi->Init( videoInfo.isPAL ? AVI_PAL : AVI_NTSC, audioInfo.frequency,
00503                    AVI_LARGE_INDEX );
00504         break;
00505 
00506     case AVI_DV2_FORMAT:
00507         fail_null( avi = new AVI2File );
00508         if ( avi->Create( filename.c_str() ) == false )
00509             return false;
00510         if ( GetOpenDML() )
00511             avi->Init( videoInfo.isPAL ? AVI_PAL : AVI_NTSC, audioInfo.frequency,
00512                        ( AVI_SMALL_INDEX | AVI_LARGE_INDEX ) );
00513         else
00514             avi->Init( videoInfo.isPAL ? AVI_PAL : AVI_NTSC, audioInfo.frequency,
00515                        ( AVI_SMALL_INDEX ) );
00516         break;
00517 
00518     default:
00519         assert( aviFormat == AVI_DV1_FORMAT || aviFormat == AVI_DV2_FORMAT );
00520     }
00521 
00522     avi->setDVINFO( dvinfo );
00523     avi->setFccHandler( make_fourcc( "iavs" ), fccHandler );
00524     avi->setFccHandler( make_fourcc( "vids" ), fccHandler );
00525     this->filename = filename;
00526     FileTracker::GetInstance().Add( filename.c_str() );
00527     return ( avi != NULL );
00528 }

bool AVIHandler::FileIsOpen  )  [virtual]
 

Implements FileHandler.

Definition at line 485 of file filehandler.cc.

References avi.

00486 {
00487     return avi != NULL;
00488 }

off_t AVIHandler::GetFileSize  )  [virtual]
 

Implements FileHandler.

Definition at line 570 of file filehandler.cc.

References avi, and RIFFFile::GetFileSize().

00571 {
00572     return avi->GetFileSize();
00573 }

int AVIHandler::GetFormat  )  const [inline]
 

Definition at line 158 of file filehandler.h.

References aviFormat.

Referenced by KinoCommon::showFrameMoreInfo().

00159     {
00160         return aviFormat;
00161     }

int AVIHandler::GetFrame Frame frame,
int  frameNum
[virtual]
 

Implements FileHandler.

Definition at line 619 of file filehandler.cc.

References audioBuffer, audioChannels, audioInfo, avi, AVI_AUDIO_BUFFER_SECS, AVI_DV2_FORMAT, aviFormat, AudioInfo::channels, channels, DV_AUDIO_MAX_SAMPLES, FOURCC, AudioInfo::frequency, AVIFile::GetDVFrame(), AVIFile::getFrame(), AVIFile::getStreamFormat(), isFullyInitialized, isInterleave1to1, make_fourcc(), AudioInfo::samples, and SeekAudioFrame().

00620 {
00621     int result = avi->GetDVFrame( frame, frameNum );
00622     if ( result == 0 )
00623     {
00624         /* get the audio from the audio stream, if available */
00625         if ( aviFormat == AVI_DV2_FORMAT )
00626         {
00627             WAVEFORMATEX wav;
00628             FOURCC fccWav = make_fourcc( "01wb" );
00629 
00630             if ( ! isFullyInitialized && 
00631                  avi->getStreamFormat( ( void* ) &wav, make_fourcc( "auds" ) ) )
00632             {
00633                 if ( channels > 0 && channels < 5 )
00634                 {
00635                     // Allocate interleaved audio buffer to accomodate large chunks
00636                     audioBuffer = new int16_t[ wav.nSamplesPerSec * channels * AVI_AUDIO_BUFFER_SECS ];
00637 
00638                     // Allocate non-interleaved audio buffers
00639                     for ( int c = 0; c < channels; c++ )
00640                         audioChannels[ c ] = new int16_t[ 2 * DV_AUDIO_MAX_SAMPLES ];
00641                     
00642                     // Get the audio parameters from AVI for subsequent calls to method
00643                     audioInfo.channels = wav.nChannels;
00644                     audioInfo.frequency = wav.nSamplesPerSec;
00645 
00646                     // Skip initialization on subsequent calls to method
00647                     isFullyInitialized = true;
00648                     cerr << ">>> using audio from separate AVI audio stream" << endl;
00649                 }
00650             }
00651 
00652             // Get the frame from AVI
00653             if ( isInterleave1to1 )
00654             {
00655                 int n = avi->getFrame( audioBuffer, frameNum, fccWav );
00656                 if ( n > 0 )
00657                 {
00658                     // Temporary pointer to audio scratch buffer
00659                     int16_t * s = audioBuffer;
00660     
00661                     // Determine samples in this frame
00662                     audioInfo.samples = n / audioInfo.channels / sizeof( int16_t );
00663                     if ( audioInfo.samples > DV_AUDIO_MAX_SAMPLES )
00664                         audioInfo.samples = DV_AUDIO_MAX_SAMPLES;
00665                     
00666                     // Convert interleaved audio into non-interleaved
00667                     for ( int n = 0; n < audioInfo.samples; ++n )
00668                         for ( int i = 0; i < audioInfo.channels; i++ )
00669                             audioChannels[ i ][ n ] = *s++;
00670                 }
00671             }
00672             else
00673             {
00674                 int16_t* s;
00675                 int availSamples = 0;
00676 
00677                 // Fetch correct audio chunk and get audio buffer offset
00678                 if ( ( s = SeekAudioFrame( frame, frameNum, availSamples ) ) )
00679                 {
00680                     if ( availSamples < audioInfo.samples )
00681                     {
00682                         // Not enough samples in audio buffer
00683                         int n;
00684 
00685                         // Convert interleaved samples remaining in the buffer
00686                         for ( n = 0; n < availSamples; ++n )
00687                             for ( int i = 0; i < audioInfo.channels; i++ )
00688                                 audioChannels[ i ][ n ] = *s++;
00689 
00690                         // Load the next audio chunk into buffer
00691                         if ( ( s = SeekAudioFrame( frame, frameNum + 1, availSamples ) ) )
00692                         {
00693                             // Convert interleaved audio into non-interleaved
00694                             for ( --n; n < audioInfo.samples; ++n )
00695                                 for ( int i = 0; i < audioInfo.channels; i++ )
00696                                     audioChannels[ i ][ n ] = *s++;
00697                         }
00698                         else if ( availSamples >= 0 )
00699                         {
00700                             // Just truncate number of samples in this frame
00701                             audioInfo.samples = availSamples;
00702                         }
00703                     }
00704                     else
00705                     {
00706                         // Convert interleaved audio into non-interleaved
00707                         for ( int n = 0; n < audioInfo.samples; ++n )
00708                             for ( int i = 0; i < audioInfo.channels; i++ )
00709                                 audioChannels[ i ][ n ] = *s++;
00710                     }   
00711                 }
00712                 // <0 signals an error in SeekAudioFrame
00713                 if ( availSamples < 0 )
00714                     return result;
00715             }
00716             // Write interleaved audio into frame
00717             frame.EncodeAudio( audioInfo, audioChannels );
00718         }
00719         // Parse important metadata in DV bitstream
00720         frame.ExtractHeader();
00721     }
00722     return result;
00723 }

bool AVIHandler::GetOpenDML  )  const
 

Definition at line 774 of file filehandler.cc.

References isOpenDML.

Referenced by Create(), and KinoCommon::showFrameMoreInfo().

00775 {
00776     return isOpenDML;
00777 }

int AVIHandler::GetTotalFrames  )  [virtual]
 

Implements FileHandler.

Definition at line 575 of file filehandler.cc.

References avi, and AVIFile::GetTotalFrames().

00576 {
00577     return avi->GetTotalFrames();
00578 }

bool AVIHandler::Open const char *  s  )  [virtual]
 

Implements FileHandler.

Definition at line 581 of file filehandler.cc.

References avi, AVI_DV1_FORMAT, AVI_DV2_FORMAT, aviFormat, fail_null, FileHandler::filename, AVIFile::isInterleave1to1(), isInterleave1to1, AVIFile::isOpenDML(), isOpenDML, make_fourcc(), RIFFFile::Open(), AVIFile::ParseRIFF(), AVIFile::ReadIndex(), AVIFile::verifyStream(), and AVIFile::verifyStreamFormat().

00582 {
00583     assert( avi == NULL );
00584     fail_null( avi = new AVI1File );
00585     if ( avi->Open( s ) )
00586     {
00587         avi->ParseRIFF();
00588         if ( ! (
00589                     avi->verifyStreamFormat( make_fourcc( "dvsd" ) ) ||
00590                     avi->verifyStreamFormat( make_fourcc( "DVSD" ) ) ||
00591                     avi->verifyStreamFormat( make_fourcc( "dvcs" ) ) ||
00592                     avi->verifyStreamFormat( make_fourcc( "DVCS" ) ) ||
00593                     avi->verifyStreamFormat( make_fourcc( "dvcp" ) ) ||
00594                     avi->verifyStreamFormat( make_fourcc( "DVCP" ) ) ||
00595                     avi->verifyStreamFormat( make_fourcc( "CDVC" ) ) ||
00596                     avi->verifyStreamFormat( make_fourcc( "cdvc" ) ) ||
00597                     avi->verifyStreamFormat( make_fourcc( "DV25" ) ) ||
00598                     avi->verifyStreamFormat( make_fourcc( "dv25" ) ) ) )
00599             return false;
00600         avi->ReadIndex();
00601         if ( avi->verifyStream( make_fourcc( "auds" ) ) )
00602         {
00603             aviFormat = AVI_DV2_FORMAT;
00604             isInterleave1to1 = avi->isInterleave1to1();
00605         }
00606         else
00607         {
00608             aviFormat = AVI_DV1_FORMAT;
00609         }
00610         isOpenDML = avi->isOpenDML();
00611         filename = s;
00612         return true;
00613     }
00614     else
00615         return false;
00616 
00617 }

int16_t * AVIHandler::SeekAudioFrame Frame frame,
int  frameNum,
int &  availableSize
 

Definition at line 726 of file filehandler.cc.

References audioBuffer, audioBufferSize, audioInfo, audioOffset, avi, AVI_AUDIO_BUFFER_SECS, AudioInfo::channels, FOURCC, AudioInfo::frequency, AVIFile::getFrame(), AVIFile::GetFrameInfo(), make_fourcc(), and AudioInfo::samples.

Referenced by GetFrame().

00727 {
00728     // Not very optimized - needs to seek to PCM sample offset
00729     FOURCC fccWav = make_fourcc( "01wb" );
00730     frame.ExtractHeader();
00731     off_t offsetBytes = off_t( 0.5 + (float) frameNum / frame.GetFrameRate() * audioInfo.frequency );
00732     offsetBytes *= audioInfo.channels * sizeof(int16_t);
00733 
00734     // If cache miss
00735     if ( offsetBytes < audioOffset || offsetBytes >= ( audioOffset + audioBufferSize ) )
00736     {
00737         int i = 0;
00738         off_t dummy;
00739 
00740         // Find chunk corresponding to offset
00741         for ( audioOffset = 0; avi->GetFrameInfo( dummy, audioBufferSize, i, fccWav ) != -1; ++i )
00742         {
00743             if ( audioBufferSize <= 0 )
00744             {
00745                 availableSize = -1;
00746                 return 0;
00747             }
00748             if ( audioOffset + audioBufferSize > offsetBytes )
00749                 break;
00750             audioOffset += audioBufferSize;
00751         }
00752         // Read chunk if buffer is large enough to hold it
00753         if ( audioBufferSize > ( off_t( audioInfo.frequency ) * audioInfo.channels * sizeof(int16_t) * AVI_AUDIO_BUFFER_SECS ) ||
00754              !avi->getFrame( audioBuffer, i, fccWav ) )
00755         {
00756             return 0;
00757         }
00758     }
00759     // Return how many samples in buffer are available after seeking
00760     availableSize = ( audioBufferSize - int( offsetBytes - audioOffset ) ) >> 2;
00761     // Set number of samples to use in this frame
00762     audioInfo.samples = frame.CalculateNumberSamples( audioInfo.frequency, frameNum );
00763     // Return offset into audio buffer
00764     return audioBuffer + ( ( offsetBytes - audioOffset ) >> 1 );
00765 }

void AVIHandler::SetOpenDML bool   ) 
 

Definition at line 768 of file filehandler.cc.

References isOpenDML.

Referenced by ExportAVI::doExport(), and PageCapture::startCapture().

00769 {
00770     isOpenDML = flag;
00771 }

void AVIHandler::SetSampleFrame const Frame sample  )  [virtual]
 

Reimplemented from FileHandler.

Definition at line 457 of file filehandler.cc.

References audioInfo, Pack::data, dvinfo, DWORD, fccHandler, make_fourcc(), and videoInfo.

00458 {
00459     Pack pack;
00460     sample.GetAudioInfo( audioInfo );
00461     sample.GetVideoInfo( videoInfo );
00462 
00463     sample.GetAAUXPack( 0x50, pack );
00464     dvinfo.dwDVAAuxSrc = *( DWORD* ) ( pack.data + 1 );
00465     sample.GetAAUXPack( 0x51, pack );
00466     dvinfo.dwDVAAuxCtl = *( DWORD* ) ( pack.data + 1 );
00467 
00468     sample.GetAAUXPack( 0x52, pack );
00469     dvinfo.dwDVAAuxSrc1 = *( DWORD* ) ( pack.data + 1 );
00470     sample.GetAAUXPack( 0x53, pack );
00471     dvinfo.dwDVAAuxCtl1 = *( DWORD* ) ( pack.data + 1 );
00472 
00473     sample.GetVAUXPack( 0x60, pack );
00474     dvinfo.dwDVVAuxSrc = *( DWORD* ) ( pack.data + 1 );
00475     sample.GetVAUXPack( 0x61, pack );
00476     dvinfo.dwDVVAuxCtl = *( DWORD* ) ( pack.data + 1 );
00477 
00478 #ifdef HAVE_LIBDV
00479     if ( sample.decoder->std == e_dv_std_smpte_314m )
00480         fccHandler = make_fourcc( "dv25" );
00481 #endif
00482 }

int AVIHandler::Write const Frame frame  )  [virtual]
 

Implements FileHandler.

Definition at line 531 of file filehandler.cc.

References avi, and AVIFile::WriteFrame().

00532 {
00533     assert( avi != NULL );
00534     try
00535     {
00536         return avi->WriteFrame( frame ) ? 0 : -1;
00537     }
00538     catch (...)
00539     {
00540         return -1;
00541     }
00542 }


Member Data Documentation

int16_t* AVIHandler::audioBuffer [protected]
 

Definition at line 173 of file filehandler.h.

Referenced by Close(), GetFrame(), SeekAudioFrame(), and ~AVIHandler().

int AVIHandler::audioBufferSize [protected]
 

Definition at line 177 of file filehandler.h.

Referenced by SeekAudioFrame().

int16_t* AVIHandler::audioChannels[4] [protected]
 

Definition at line 174 of file filehandler.h.

Referenced by AVIHandler(), Close(), GetFrame(), and ~AVIHandler().

AudioInfo AVIHandler::audioInfo [protected]
 

Definition at line 166 of file filehandler.h.

Referenced by Create(), GetFrame(), SeekAudioFrame(), and SetSampleFrame().

off_t AVIHandler::audioOffset [protected]
 

Definition at line 176 of file filehandler.h.

Referenced by SeekAudioFrame().

AVIFile* AVIHandler::avi [protected]
 

Definition at line 164 of file filehandler.h.

Referenced by Close(), Create(), FileIsOpen(), GetFileSize(), GetFrame(), GetTotalFrames(), Open(), SeekAudioFrame(), Write(), and ~AVIHandler().

int AVIHandler::aviFormat [protected]
 

Definition at line 165 of file filehandler.h.

Referenced by Create(), GetFormat(), GetFrame(), and Open().

int AVIHandler::channels [protected]
 

Definition at line 171 of file filehandler.h.

Referenced by GetFrame().

DVINFO AVIHandler::dvinfo [protected]
 

Definition at line 169 of file filehandler.h.

Referenced by Create(), and SetSampleFrame().

FOURCC AVIHandler::fccHandler [protected]
 

Definition at line 170 of file filehandler.h.

Referenced by Create(), and SetSampleFrame().

bool AVIHandler::isFullyInitialized [protected]
 

Definition at line 172 of file filehandler.h.

Referenced by Close(), and GetFrame().

bool AVIHandler::isInterleave1to1 [protected]
 

Definition at line 175 of file filehandler.h.

Referenced by GetFrame(), and Open().

bool AVIHandler::isOpenDML [protected]
 

Definition at line 168 of file filehandler.h.

Referenced by GetOpenDML(), Open(), and SetOpenDML().

VideoInfo AVIHandler::videoInfo [protected]
 

Definition at line 167 of file filehandler.h.

Referenced by Create(), and SetSampleFrame().


The documentation for this class was generated from the following files:
Generated on Sun Mar 11 22:12:47 2007 for Kino by  doxygen 1.4.2