【VC开源代码栏目提醒】:网学会员VC开源代码为您提供mp4file_io.cpp参考,解决您在mp4file_io.cpp学习中工作中的难题,参考学习。
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is MPEG4IP.
*
* The Initial Developer of the Original Code is Cisco Systems Inc.
* Portions created by Cisco Systems Inc. are
* Copyright (C) Cisco Systems Inc. 2001. All Rights Reserved.
*
* Contributor(s):
* Dave Mackie dmackie@cisco.com
*/
#include "mp4common.h"
#ifdef HAVE_FPOS_T_POS
#define FPOS_TO_UINT64(x) ((u_int64_t)((x).__pos))
#define UINT64_TO_FPOS(x, y) ((x).__pos = (y))
#else
#define FPOS_TO_UINT64(x) ((u_int64_t)(x))
#define UINT64_TO_FPOS(x, y) ((x) = (fpos)(y))
#endif
// MP4File low level IO support
u_int64_t MP4File::GetPosition(FILE* pFile)
{
if (m_memoryBuffer == NULL) {
if (pFile == NULL) {
ASSERT(m_pFile);
pFile = m_pFile;
}
fpos_t fpos;
if (fgetpos(pFile, &fpos) < 0) {
throw new MP4Error(errno, "MP4GetPosition");
}
return FPOS_TO_UINT64(fpos);
} else {
return m_memoryBufferPosition;
}
}
void MP4File::SetPosition(u_int64_t pos, FILE* pFile)
{
if (m_memoryBuffer == NULL) {
if (pFile == NULL) {
ASSERT(m_pFile);
pFile = m_pFile;
}
fpos_t fpos;
VAR_TO_FPOS(fpos, pos);
if (fsetpos(pFile, &fpos) < 0) {
throw new MP4Error(errno, "MP4SetPosition");
}
} else {
if (pos >= m_memoryBufferSize) {
throw new MP4Error("position out of range", "MP4SetPosition");
}
m_memoryBufferPosition = pos;
}
}
u_int64_t MP4File::GetSize()
{
if (m_mode == 'w') {
// we're always positioned at the end of file in write mode
// except for short intervals in ReadSample and FinishWrite routines
// so we rely on the faster approach of GetPosition()
// instead of flushing to disk, and then stat'ing the file
m_fileSize = GetPosition();
} // else read mode, fileSize was determined at Open()
return m_fileSize;
}
u_int32_t MP4File::ReadBytes(u_int8_t* pBytes, u_int32_t numBytes, FILE* pFile)
{
// handle degenerate cases
if (numBytes == 0) {
return 0;
}
ASSERT(pBytes);
WARNING(m_numReadBits > 0);
if (pFile == NULL) {
pFile = m_pFile;
}
ASSERT(pFile);
if (m_memoryBuffer == NULL) {
if (fread(pBytes, 1, numBytes, pFile) != numBytes) {
if (feof(pFile)) {
throw new MP4Error(
"not enough bytes, reached end-of-file",
"MP4ReadBytes");
} else {
throw new MP4Error(errno, "MP4ReadBytes");
}
}
} else {
if (m_memoryBufferPosition + numBytes > m_memoryBufferSize) {
throw new MP4Error(
"not enough bytes, reached end-of-memory",
"MP4ReadBytes");
}
memcpy(pBytes, &m_memoryBuffer[m_memoryBufferPosition], numBytes);
m_memoryBufferPosition += numBytes;
}
return numBytes;
}
u_int32_t MP4File::PeekBytes(u_int8_t* pBytes, u_int32_t numBytes, FILE* pFile)
{
u_int64_t pos = GetPosition(pFile);
ReadBytes(pBytes, numBytes, pFile);
SetPosition(pos, pFile);
return numBytes;
}
void MP4File::EnableMemoryBuffer(u_int8_t* pBytes, u_int64_t numBytes)
{
ASSERT(m_memoryBuffer == NULL);
if (pBytes) {
m_memoryBuffer = pBytes;
m_memoryBufferSize = numBytes;
} else {
if (numBytes) {
m_memoryBufferSize = numBytes;
} else {
m_memoryBufferSize = 1024;
}
m_memoryBuffer = (u_int8_t*)MP4Malloc(m_memoryBufferSize);
}
m_memoryBufferPosition = 0;
}
void MP4File::DisableMemoryBuffer(u_int8_t** ppBytes, u_int64_t* pNumBytes)
{
ASSERT(m_memoryBuffer != NULL);
if (ppBytes) {
*ppBytes = m_memoryBuffer;
}
if (pNumBytes) {
*pNumBytes = m_memoryBufferPosition;
}
m_memoryBuffer = NULL;
m_memoryBufferSize = 0;
m_memoryBufferPosition = 0;
}
void MP4File::WriteBytes(u_int8_t* pBytes, u_int32_t numBytes, FILE* pFile)
{
ASSERT(m_numWriteBits == 0 || m_numWriteBits >= 8);
if (pBytes == NULL || numBytes == 0) {
return;
}
if (m_memoryBuffer == NULL) {
if (pFile == NULL) {
ASSERT(m_pFile);
pFile = m_pFile;
}
u_int32_t rc = fwrite(pBytes, 1, numBytes, pFile);
if (rc != numBytes) {
throw new MP4Error(errno, "MP4WriteBytes");
}
} else {
if (m_memoryBufferPosition + numBytes > m_memoryBufferSize) {
m_memoryBufferSize = 2 * (m_memoryBufferSize + numBytes);
m_m