Alex Bikfalvi
SimStream Documentation
StreamBufferLayer.cpp
00001 #include "Headers.h" 00002 #include "StreamBufferLayer.h" 00003 00004 CStreamBufferLayer::CStreamBufferLayer( 00005 __uint32 size, 00006 __uint32 sizeHistory 00007 ) 00008 { 00009 assert(size); 00010 assert(sizeHistory < size); 00011 00012 this->size = size; 00013 this->sizeHistory = sizeHistory; 00014 this->sizePlay = this->size - this->sizeHistory; 00015 00016 this->buffer = new CStreamFrame[this->size]; 00017 this->bitmap = new bool[this->size]; 00018 this->coder = new bool[this->size]; 00019 00020 this->currentIndex = 0; 00021 00022 this->ptrFirst = 0; 00023 this->ptrLast = 0; 00024 } 00025 00026 CStreamBufferLayer::~CStreamBufferLayer() 00027 { 00028 delete[] this->buffer; 00029 delete[] this->bitmap; 00030 delete[] this->coder; 00031 } 00032 00033 void CStreamBufferLayer::Reset(__uint32 index) 00034 { 00035 // Resets the buffer starting from the frame 00036 this->ptrLast = 0; 00037 this->ptrFirst = this->sizeHistory; 00038 this->currentIndex = index; 00039 00040 this->count = 0; 00041 this->numHistory = 0; 00042 this->numPlay = 0; 00043 00044 // Clear the buffer 00045 for(__uint32 idx = 0; idx < this->size; idx++) 00046 { 00047 this->bitmap[idx] = 0; 00048 this->coder[idx] = 0; 00049 } 00050 } 00051 00052 bool CStreamBufferLayer::Add(CStreamFrame frame, __uint32 layerIndex) 00053 { 00054 // Add the frame if it fits in the buffer 00055 if((layerIndex > ((this->currentIndex >= this->sizeHistory)?this->currentIndex - this->sizeHistory:0)) && (layerIndex <= this->currentIndex + this->sizePlay)) 00056 { 00057 __uint32 idx = (this->size + this->ptrFirst + layerIndex - this->currentIndex) % this->size; 00058 00059 // Increase number of frames by 1, if previously the slot was empty 00060 this->count += (this->bitmap[idx]?0:1); 00061 // Increase number of play frames by 1, if frame in the play region and previously the slot was empty 00062 this->numPlay += ((layerIndex >= this->currentIndex)?(this->bitmap[idx]?0:1):0); 00063 // Increase number of history frames by 1, if frame in the history region and previously the slot was empty 00064 this->numHistory += ((layerIndex < this->currentIndex)?(this->bitmap[idx]?0:1):0); 00065 00066 // Save the frame 00067 this->buffer[idx] = frame; 00068 00069 // Mark the frame in the bitmap 00070 this->bitmap[idx] = 1; 00071 00072 // Set coder to true if the frame is independent 00073 this->coder[idx] = (frame.AnchorP() == 0) && (frame.AnchorB() == 0); 00074 00075 assert(this->count <= this->size); 00076 00077 return true; 00078 } 00079 return false; 00080 } 00081 00082 void CStreamBufferLayer::Shift() 00083 { 00084 // Update the number of frames 00085 this->numPlay -= (this->bitmap[this->ptrFirst]?1:0); 00086 this->numHistory += (this->bitmap[this->ptrFirst]?1:0); 00087 00088 // Shift both the first and last pointers 00089 this->ptrFirst = (this->ptrFirst + 1) % this->size; 00090 this->ptrLast = (this->ptrLast + 1) % this->size; 00091 00092 // Increment the current frame 00093 this->currentIndex++; 00094 00095 // Update the number of frames 00096 this->count -= (this->bitmap[this->ptrLast]?1:0); 00097 this->numHistory -= (this->bitmap[this->ptrLast]?1:0); 00098 00099 // Clear the last entry 00100 this->bitmap[this->ptrLast] = 0; 00101 this->coder[this->ptrLast] = 0; 00102 } 00103 00104 bool CStreamBufferLayer::Has(__uint32 index) 00105 { 00106 if((index > ((this->currentIndex >= this->sizeHistory)?this->currentIndex - this->sizeHistory:0)) && (index <= this->currentIndex + this->sizePlay)) 00107 { 00108 return this->bitmap[(this->size + this->ptrFirst + index - this->currentIndex) % this->size]; 00109 } 00110 else return false; 00111 } 00112 00113 CStreamFrame* CStreamBufferLayer::Frame(__uint32 index) 00114 { 00115 if((index > ((this->currentIndex >= this->sizeHistory)?this->currentIndex - this->sizeHistory:0)) && (index <= this->currentIndex + this->sizePlay)) 00116 { 00117 return (this->bitmap[(this->size + this->ptrFirst + index - this->currentIndex) % this->size]? 00118 &this->buffer[(this->size + this->ptrFirst + index - this->currentIndex) % this->size]:NULL); 00119 } 00120 else return NULL; 00121 } 00122 00123 bool CStreamBufferLayer::IsCurrentDecodable() 00124 { 00125 return this->coder[this->ptrFirst]; 00126 } 00127 00128 bool CStreamBufferLayer::IsDecodable(__uint32 index) 00129 { 00130 if((index > ((this->currentIndex >= this->sizeHistory)?this->currentIndex - this->sizeHistory:0)) && (index <= this->currentIndex + this->sizePlay)) 00131 { 00132 return this->coder[(this->size + this->ptrFirst + index - this->currentIndex) % this->size]; 00133 } 00134 else return false; 00135 } 00136 00137 void CStreamBufferLayer::SetCurrentDecodable() 00138 { 00139 this->coder[this->ptrFirst] = true; 00140 } 00141 00142 void CStreamBufferLayer::SetDecodable(__uint32 index) 00143 { 00144 assert((index > ((this->currentIndex >= this->sizeHistory)?this->currentIndex - this->sizeHistory:0)) && (index <= this->currentIndex + this->sizePlay)); 00145 assert(this->bitmap[(this->size + this->ptrFirst + index - this->currentIndex) % this->size]); 00146 00147 this->coder[(this->size + this->ptrFirst + index - this->currentIndex) % this->size] = true; 00148 }
Last updated: February 8, 2011