Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reading mp4 container structure from file offset #6

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions include/mp4v2/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
14 changes: 13 additions & 1 deletion src/mp4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -99,6 +103,7 @@ MP4FileHandle MP4Read( const char* fileName )
try
{
ASSERT(pFile);
pFile->SetInitialSeekOffset( seekOffset );
pFile->Read( fileName, NULL );
return (MP4FileHandle)pFile;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -142,7 +154,7 @@ MP4FileHandle MP4ReadProvider( const char* fileName, const MP4FileProvider* file
delete pFile;
return MP4_INVALID_FILE_HANDLE;
}

///////////////////////////////////////////////////////////////////////////////

MP4FileHandle MP4Create (const char* fileName,
Expand Down
6 changes: 5 additions & 1 deletion src/mp4file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ))
Expand Down
2 changes: 2 additions & 0 deletions src/mp4file.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/mp4file_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -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__ );
}

Expand All @@ -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 )
Expand Down