Skip to content

Commit

Permalink
skip_bytes use File Seek
Browse files Browse the repository at this point in the history
Currently for bytes skipping, tsMuxer reads 2 MB blocks at a time from the input file through a loop: this can take substantial time for large skips/files -especially when the file is on a NAS, accessed via WiFi.

This pull introduces file seeking so that only two blocks  (4MB) are left to be read.

Fixes issue #733.
  • Loading branch information
jcdr428 committed Sep 19, 2023
1 parent db58158 commit 3bd1492
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions tsMuxer/abstractReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AbstractReader
AbstractReader() : m_blockSize(0), m_allocSize(0), m_prereadThreshold(0) {}
virtual ~AbstractReader() = default;
virtual uint8_t* readBlock(int readerID, uint32_t& readCnt, int& rez, bool* firstBlockVar = nullptr) = 0;
virtual bool incSeek(int readerID, int64_t offset) = 0;
virtual void notify(int readerID, uint32_t dataReaded) = 0;
virtual int createReader(int readBuffOffset = 0) = 0;
virtual void deleteReader(int readerID) = 0;
Expand Down
2 changes: 1 addition & 1 deletion tsMuxer/bufferedReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class BufferedReader : public AbstractReader, TerminatableThread
uint32_t getReaderCount();
void terminate();
void setFileIterator(FileNameIterator* itr, int readerID);
bool incSeek(int readerID, int64_t offset);
bool incSeek(int readerID, int64_t offset) override;
bool gotoByte(int readerID, int64_t seekDist) override { return false; }

void setId(const uint32_t value) { m_id = value; }
Expand Down
37 changes: 25 additions & 12 deletions tsMuxer/ioContextDemuxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,17 @@ unsigned IOContextDemuxer::get_buffer(uint8_t* binary, unsigned size)
int readRez = 0;
uint8_t* dst = binary;
const uint8_t* dstEnd = dst + size;

if (m_curPos < m_bufEnd)
{
const unsigned copyLen = min((unsigned)(m_bufEnd - m_curPos), size);
const unsigned copyLen = min(static_cast<unsigned>(m_bufEnd - m_curPos), size);
memcpy(dst, m_curPos, copyLen);
dst += copyLen;
m_curPos += copyLen;
m_processedBytes += copyLen;
size -= copyLen;
}

while (dst < dstEnd)
{
uint8_t* data = m_bufferedReader->readBlock(m_readerID, readedBytes, readRez);
Expand All @@ -104,43 +106,54 @@ unsigned IOContextDemuxer::get_buffer(uint8_t* binary, unsigned size)

m_curPos = data + 188;
m_bufEnd = m_curPos + readedBytes;
const unsigned copyLen = min((unsigned)(m_bufEnd - m_curPos), size);
if (readedBytes == 0)
break;
const unsigned copyLen = min(readedBytes, size);
memcpy(dst, m_curPos, copyLen);
dst += copyLen;
m_curPos += copyLen;
size -= copyLen;
m_processedBytes += copyLen;
if (readedBytes == 0)
break;
}
return static_cast<uint32_t>(dst - binary);
}

void IOContextDemuxer::skip_bytes(const int64_t size)
{
if (size == 0)
return;
uint32_t readedBytes = 0;
int readRez = 0;
int64_t skipLeft = size;
if (m_curPos < m_bufEnd)

if (skipLeft < m_bufEnd - m_curPos)
{
const int64_t copyLen = min(m_bufEnd - m_curPos, skipLeft);
skipLeft -= copyLen;
m_curPos += copyLen;
m_processedBytes += copyLen;
m_curPos += skipLeft;
m_processedBytes += skipLeft;
return;
}

if (skipLeft > 2LL * m_fileBlockSize)
{
const int64_t offset = skipLeft - 2LL * m_fileBlockSize;
m_bufferedReader->incSeek(m_readerID, offset);
m_processedBytes += offset;
skipLeft = 2LL * m_fileBlockSize;
}

while (skipLeft > 0)
{
uint8_t* data = m_bufferedReader->readBlock(m_readerID, readedBytes, readRez);
if (readedBytes > 0 && readRez == 0)
m_bufferedReader->notify(m_readerID, readedBytes);
m_curPos = data + 188;
m_bufEnd = m_curPos + readedBytes;
const int64_t copyLen = min(m_bufEnd - m_curPos, skipLeft);
if (readedBytes == 0)
break;
const int64_t copyLen = min(readedBytes, skipLeft);
m_curPos += copyLen;
m_processedBytes += copyLen;
skipLeft -= copyLen;
if (readedBytes == 0)
break;
}
}

Expand Down
1 change: 1 addition & 0 deletions tsMuxer/metaDemuxer.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class ContainerToReaderWrapper final : public AbstractReader
m_terminated = false;
}
uint8_t* readBlock(int readerID, uint32_t& readCnt, int& rez, bool* firstBlockVar = nullptr) override;
bool incSeek(int readerID, int64_t offset) override { return false; }
void notify(int readerID, uint32_t dataReaded) override {}
int createReader(int readBuffOffset = 0) override;
void deleteReader(int readerID) override;
Expand Down

0 comments on commit 3bd1492

Please sign in to comment.