wavelength 1.0.0
Loading...
Searching...
No Matches
chunkstream.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
5#include "os/binstream.h"
6#include "os/file.h"
7
14extern const char *g_szChunkMark;
15
24class ChunkStream : public BinStream {
25public:
27 enum Mode {
30 };
31
33 static constexpr int kMaxBlocks = 50;
34
36 struct Header {
44 explicit Header(bool bCompressed);
45
46 unsigned int mMagic;
51 };
52
54 static constexpr unsigned int kMagicCompressed = 0xccbedeaf;
55
57 static constexpr unsigned int kMagicPlain = 0xcabedeaf;
58
71 const char *pszFile, Mode eMode, int nChunkSize, bool bCompressed, bool bLittleEndian);
72
79 ~ChunkStream() override;
80
91#ifdef VIDEO_STANDARD_PAL
92 void ReadImpl(void *pData, int nBytes) override;
93#else
94 void Read(void *pData, int nBytes) override;
95#endif
96
105#ifdef VIDEO_STANDARD_PAL
106 void WriteImpl(const void *pData, int nBytes) override;
107#else
108 void Write(const void *pData, int nBytes) override;
109#endif
110
117 void Flush() override {
118 }
119
128#ifdef VIDEO_STANDARD_PAL
129 void SeekImpl(int nOffset, SeekType eFrom) override;
130#else
131 void Seek(int nOffset, SeekType eFrom) override;
132#endif
133
141 int Tell() override;
142
150 bool Eof() override;
151
159 bool Fail() override;
160
168 void Mark(const char *pszMark) override;
169
170private:
178 void FlushBlock(bool bLast);
179
188 int WriteBlock(int nBytes);
189
196 void DecodeBlock();
197
198 std::unique_ptr<File> mFile;
199 int mFail;
200 Mode mMode;
201 Header mHeader;
202 int mCompressed;
203 int mChunkSize;
204 int mBufferSize;
205 int mBuffer;
206 int mBlockReady;
207 int mPosition;
208 int mHeaderPending;
209 int *mBlock;
210 int *mBlockEnd;
211 char *mBuffers[2];
212 char *mWork;
213 char *mData;
214 float mStallMs;
215};
216
BinStream & EndChunk(BinStream &stream)
Send g_szChunkMark to a stream.
const char * g_szChunkMark
The mark EndChunk() sends and ChunkStream::Mark() recognises, chunk.
Stream of raw bytes, the base of the memory, file, and network streams.
Definition binstream.h:22
SeekType
Origins of Seek().
Definition binstream.h:25
BinStream(bool bLittleEndian)
Construct a stream.
Definition binstream.h:38
static constexpr unsigned int kMagicCompressed
mMagic of a file with deflated blocks.
Definition chunkstream.h:54
bool Eof() override
Advance the background reads, and report whether the current block is still unavailable.
int Tell() override
Report that the stream cannot report a position.
static constexpr unsigned int kMagicPlain
mMagic of a file with plain blocks.
Definition chunkstream.h:57
void Seek(int nOffset, SeekType eFrom) override
Report that the stream cannot seek.
Mode
Directions a stream opens in.
Definition chunkstream.h:27
@ kModeRead
Definition chunkstream.h:28
@ kModeWrite
Definition chunkstream.h:29
void Read(void *pData, int nBytes) override
Copy bytes out of the current block.
void Flush() override
Do nothing.
Definition chunkstream.h:117
void Mark(const char *pszMark) override
End the block at a mark of g_szChunkMark once it has reached the chunk size.
void Write(const void *pData, int nBytes) override
Append bytes to the block being gathered.
static constexpr int kMaxBlocks
Blocks the header describes at most.
Definition chunkstream.h:33
bool Fail() override
Report whether the file failed to open or a block failed to write.
ChunkStream(const char *pszFile, Mode eMode, int nChunkSize, bool bCompressed, bool bLittleEndian)
Open a file and start reading its header, or write a fresh header.
~ChunkStream() override
Write the last block and the final header of a writer, and close the file.
The header at the start of the file.
Definition chunkstream.h:36
int mMaxBlockSize
Definition chunkstream.h:49
Header(bool bCompressed)
Construct the header of a file with one empty block.
int mHeaderSize
Definition chunkstream.h:47
int mBlockSizes[kMaxBlocks]
Definition chunkstream.h:50
int mBlockCount
Definition chunkstream.h:48
unsigned int mMagic
Definition chunkstream.h:46