Alex Bikfalvi
SimStream Documentation
StreamBuffer.cpp
00001 #include "Headers.h" 00002 #include "StreamBuffer.h" 00003 00004 CStreamBuffer::CStreamBuffer( 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 CStreamBuffer::~CStreamBuffer() 00027 { 00028 delete[] this->buffer; 00029 delete[] this->bitmap; 00030 delete[] this->coder; 00031 } 00032 00033 void CStreamBuffer::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 CStreamBuffer::Add(CStreamFrame frame) 00053 { 00054 // Add the frame if it fits in the buffer 00055 if((frame.Index() > ((this->currentIndex >= this->sizeHistory)?(this->currentIndex - this->sizeHistory):0)) && (frame.Index() <= this->currentIndex + this->sizePlay)) 00056 { 00057 __uint32 idx = (this->size + this->ptrFirst + frame.Index() - this->currentIndex) % this->size; 00058 00059 // If the slot is empty 00060 if(0 == this->bitmap[idx]) 00061 { 00062 // Increase number of frames by 1 00063 this->count++; 00064 // Increase number of play frames by 1, if frame in the play region 00065 this->numPlay += ((frame.Index() >= this->currentIndex)?1:0); 00066 // Increase number of history frames by 1, if frame in the history region 00067 this->numHistory += ((frame.Index() < this->currentIndex)?1:0); 00068 00069 // Save the frame 00070 this->buffer[idx] = frame; 00071 00072 // Mark the frame in the bitmap 00073 this->bitmap[idx] = 1; 00074 00075 // Set coder to true if the anchor frames can be decoded (can decode I and P frames) 00076 this->coder[idx] = 00077 (frame.AnchorP()?this->coder[(this->size + idx + frame.AnchorP()) % this->size]:1) && 00078 (frame.AnchorB()?this->coder[(this->size + idx + frame.AnchorB()) % this->size]:1); 00079 00080 /* 00081 Do not perform these checks to increase simulation speed 00082 (frame.anchorB >= -this->sizeHistory) && 00083 (frame.anchorB < this->sizePlay) && 00084 (frame.anchorP >= -this->sizeHistory) && 00085 (frame.anchorP < this->sizePlay) && 00086 */ 00087 00088 assert(this->count <= this->size); 00089 00090 return true; 00091 } 00092 else return false; 00093 } 00094 else return false; 00095 } 00096 00097 void CStreamBuffer::Shift() 00098 { 00099 // Update the number of frames 00100 this->numPlay -= (this->bitmap[this->ptrFirst]?1:0); 00101 this->numHistory += (this->bitmap[this->ptrFirst]?1:0); 00102 00103 // Shift both the first and last pointers 00104 this->ptrFirst = (this->ptrFirst + 1) % this->size; 00105 this->ptrLast = (this->ptrLast + 1) % this->size; 00106 00107 // Increment the current frame 00108 this->currentIndex++; 00109 00110 // Update the number of frames 00111 this->count -= (this->bitmap[this->ptrLast]?1:0); 00112 this->numHistory -= (this->bitmap[this->ptrLast]?1:0); 00113 00114 // Clear the last entry 00115 this->bitmap[this->ptrLast] = 0; 00116 this->coder[this->ptrLast] = 0; 00117 } 00118 00119 bool CStreamBuffer::Has(__uint32 index) 00120 { 00121 if((index >= this->currentIndex - this->sizeHistory) && (index < this->currentIndex + this->sizePlay)) 00122 { 00123 return this->bitmap[(this->size + this->ptrFirst + index - this->currentIndex) % this->size]; 00124 } 00125 else return false; 00126 } 00127 00128 bool CStreamBuffer::IsCurrentDecodable() 00129 { 00130 // If the frame is already decoded, return true 00131 if(this->coder[this->ptrFirst]) { assert(this->bitmap[this->ptrFirst]); return true; } 00132 00133 // Else, if the current frame does not exist, return false 00134 if(0 == this->bitmap[this->ptrFirst]) { return false; } 00135 00136 // Else, verify if the anchor frame exist and are decoded 00137 this->coder[this->ptrFirst] = 00138 (this->buffer[this->ptrFirst].AnchorP()?this->coder[(this->size + this->ptrFirst + this->buffer[this->ptrFirst].AnchorP()) % this->size]:1) && 00139 (this->buffer[this->ptrFirst].AnchorB()?this->coder[(this->size + this->ptrFirst + this->buffer[this->ptrFirst].AnchorB()) % this->size]:1); 00140 00141 return this->coder[this->ptrFirst]; 00142 }
Last updated: February 8, 2011