diff --git a/include/mp4v2/file.h b/include/mp4v2/file.h index b2124d2..139e4d0 100644 --- a/include/mp4v2/file.h +++ b/include/mp4v2/file.h @@ -404,6 +404,18 @@ MP4V2_EXPORT MP4FileHandle MP4Read( const char* fileName ); +/** Generalized version of above MP4Read, supporting reading a MP4 file at + * the specific file offset. + * + * @param fileName As for MP4Read + * @param fileOffset seek offset in file that MP4 container structure starts + * at. + */ +MP4V2_EXPORT +MP4FileHandle MP4ReadFromOffset( + const char* fileName, + int64_t seekOffset); + /** Read an existing mp4 file. * * MP4ReadProvider is the first call that should be used when you want to just @@ -432,4 +444,18 @@ MP4FileHandle MP4ReadProvider( /** @} ***********************************************************************/ +/** Generalized version of above MP4Read, supporting reading a MP4 file at + * the specific file offset. + * + * @param fileName As for MP4Read + * @param fileOffset seek offset in file that MP4 container structure starts + * at. + */ + +MP4V2_EXPORT +MP4FileHandle MP4ReadProviderFromOffset( + const char* fileName, + const MP4FileProvider* fileProvider, + int64_t seekOffset ); + #endif /* MP4V2_FILE_H */ diff --git a/src/mp4.cpp b/src/mp4.cpp index 1016f79..156514c 100644 --- a/src/mp4.cpp +++ b/src/mp4.cpp @@ -89,6 +89,10 @@ const char* MP4GetFilename( MP4FileHandle hFile ) MP4FileHandle MP4Read( const char* fileName ) { + return MP4ReadFromOffset(fileName, 0); +} + +MP4FileHandle MP4ReadFromOffset( const char* fileName, int64_t seekOffset) { if (!fileName) return MP4_INVALID_FILE_HANDLE; @@ -99,6 +103,7 @@ MP4FileHandle MP4Read( const char* fileName ) try { ASSERT(pFile); + pFile->SetInitialSeekOffset( seekOffset ); pFile->Read( fileName, NULL ); return (MP4FileHandle)pFile; } @@ -117,6 +122,12 @@ MP4FileHandle MP4Read( const char* fileName ) } MP4FileHandle MP4ReadProvider( const char* fileName, const MP4FileProvider* fileProvider ) +{ + return MP4ReadProviderFromOffset(fileName, fileProvider, 0); +} + +MP4FileHandle MP4ReadProviderFromOffset( + const char* fileName, const MP4FileProvider* fileProvider, int64_t seekOffset) { if (!fileName) return MP4_INVALID_FILE_HANDLE; @@ -126,6 +137,7 @@ MP4FileHandle MP4ReadProvider( const char* fileName, const MP4FileProvider* file return MP4_INVALID_FILE_HANDLE; try { + pFile->SetInitialSeekOffset( seekOffset ); pFile->Read( fileName, fileProvider ); return (MP4FileHandle)pFile; } @@ -142,7 +154,7 @@ MP4FileHandle MP4ReadProvider( const char* fileName, const MP4FileProvider* file delete pFile; return MP4_INVALID_FILE_HANDLE; } - + /////////////////////////////////////////////////////////////////////////////// MP4FileHandle MP4Create (const char* fileName, diff --git a/src/mp4file.cpp b/src/mp4file.cpp index 86f386b..cf3da11 100644 --- a/src/mp4file.cpp +++ b/src/mp4file.cpp @@ -59,6 +59,7 @@ void MP4File::Init() m_memoryBuffer = NULL; m_memoryBufferSize = 0; m_memoryBufferPosition = 0; + m_initialSeekOffset = 0; m_numReadBits = 0; m_bufReadBits = 0; @@ -414,7 +415,6 @@ void MP4File::Open( const char* name, File::Mode mode, const MP4FileProvider* pr void MP4File::ReadFromFile() { - // ensure we start at beginning of file SetPosition(0); // create a new root atom @@ -624,6 +624,10 @@ void MP4File::Close(uint32_t options) m_file = NULL; } +void MP4File::SetInitialSeekOffset(int64_t seekOffset) { + m_initialSeekOffset = seekOffset; +} + void MP4File::Rename(const char* oldFileName, const char* newFileName) { if( FileSystem::rename( oldFileName, newFileName )) diff --git a/src/mp4file.h b/src/mp4file.h index 960505e..5c6bdaf 100644 --- a/src/mp4file.h +++ b/src/mp4file.h @@ -91,6 +91,7 @@ class MP4File bool CopyClose( const string& copyFileName ); void Dump( bool dumpImplicits = false ); void Close(uint32_t flags = 0); + void SetInitialSeekOffset(int64_t seekOffset); bool Use64Bits(const char *atomName); void Check64BitStatus(const char *atomName); @@ -954,6 +955,7 @@ class MP4File File* m_file; uint64_t m_fileOriginalSize; uint32_t m_createFlags; + int64_t m_initialSeekOffset; MP4Atom* m_pRootAtom; MP4Integer32Array m_trakIds; diff --git a/src/mp4file_io.cpp b/src/mp4file_io.cpp index d16d87f..da1299d 100644 --- a/src/mp4file_io.cpp +++ b/src/mp4file_io.cpp @@ -37,7 +37,7 @@ uint64_t MP4File::GetPosition( File* file ) file = m_file; ASSERT( file ); - return file->position; + return file->position - m_initialSeekOffset; } void MP4File::SetPosition( uint64_t pos, File* file ) @@ -53,7 +53,7 @@ void MP4File::SetPosition( uint64_t pos, File* file ) file = m_file; ASSERT( file ); - if( file->seek( pos )) + if( file->seek( pos + m_initialSeekOffset )) throw new PlatformException( "seek failed", sys::getLastError(), __FILE__, __LINE__, __FUNCTION__ ); } @@ -66,7 +66,7 @@ uint64_t MP4File::GetSize( File* file ) file = m_file; ASSERT( file ); - return file->size; + return file->size - m_initialSeekOffset; } void MP4File::ReadBytes( uint8_t* buf, uint32_t bufsiz, File* file )