Alex Bikfalvi
SimStream Documentation
StreamBufferMulti.cpp
00001 #include "Headers.h" 00002 #include "StreamBufferMulti.h" 00003 00004 CStreamBufferMulti::CStreamBufferMulti( 00005 __uint32 numLayers, 00006 __uint32 size, 00007 __uint32 sizeHistory, 00008 __byte sizeGop 00009 ) 00010 { 00011 assert(numLayers); 00012 assert(size); 00013 assert(sizeHistory < size); 00014 00015 this->numLayers = numLayers; 00016 this->size = size; 00017 this->sizeHistory = sizeHistory; 00018 this->sizePlay = this->size - this->sizeHistory; 00019 this->sizeGop = sizeGop; 00020 this->sizeGopLayer = this->sizeGop * this->numLayers; 00021 00022 // Create layer buffers 00023 this->buffers = new CStreamBufferLayer*[this->numLayers]; 00024 for(__uint32 index = 0; index < this->numLayers; index++) 00025 { 00026 this->buffers[index] = new CStreamBufferLayer( 00027 this->size, 00028 this->sizeHistory 00029 ); 00030 } 00031 } 00032 00033 CStreamBufferMulti::~CStreamBufferMulti() 00034 { 00035 for(__uint32 index = 0; index < this->numLayers; index++) 00036 delete this->buffers[index]; 00037 delete[] this->buffers; 00038 } 00039 00040 void CStreamBufferMulti::Reset(__uint32 index) 00041 { 00042 // Calculate the base frame for the current layer 00043 __uint32 layer = this->IndexToLayer(index); 00044 __uint32 layerBaseIndex = this->IndexToLayerBaseIndex(index); 00045 00046 // Reset the layer corresponding to the index 00047 this->buffers[layer]->Reset(this->IndexToLayerIndex(index)); 00048 00049 // Reset all other layers for subsequent frames 00050 for(__uint32 idx = 1; idx < this->numLayers; idx++) 00051 { 00052 // Calculate the next layer 00053 __uint32 nextLayer = (layer + idx) % this->numLayers; 00054 00055 // Calculate the next base frame for that layer 00056 __uint32 baseIndex = layerBaseIndex + idx*this->sizeGop; 00057 00058 // Reset the layer buffer to the base index 00059 this->buffers[nextLayer]->Reset(this->IndexToLayerIndex(baseIndex)); 00060 } 00061 00062 // Set the frame index 00063 this->currentIndex = index; 00064 } 00065 00066 bool CStreamBufferMulti::Add(__uint32 layer, CStreamFrame frame) 00067 { 00068 // Add the frame to the buffer layer 00069 assert(layer == this->IndexToLayer(frame.Index())); 00070 return this->buffers[layer]->Add(frame, this->IndexToLayerIndex(frame.Index())); 00071 } 00072 00073 void CStreamBufferMulti::Shift() 00074 { 00075 // Shift the layer buffer of the current index 00076 assert(this->IndexToLayer(this->currentIndex) < this->numLayers); 00077 this->buffers[this->IndexToLayer(this->currentIndex)]->Shift(); 00078 00079 // Increment the current frame 00080 this->currentIndex++; 00081 } 00082 00083 bool CStreamBufferMulti::Has(__uint32 index) 00084 { 00085 assert(this->IndexToLayer(index) < this->numLayers); 00086 return this->buffers[this->IndexToLayer(index)]->Has(IndexToLayerIndex(index)); 00087 } 00088 00089 bool CStreamBufferMulti::HasCurrentIndex() 00090 { 00091 assert(this->IndexToLayer(this->currentIndex) < this->numLayers); 00092 return this->buffers[this->IndexToLayer(this->currentIndex)]->HasCurrentIndex(); 00093 } 00094 00095 CStreamFrame* CStreamBufferMulti::CurrentFrame() 00096 { 00097 assert(this->IndexToLayer(this->currentIndex) < this->numLayers); 00098 assert(this->buffers[this->IndexToLayer(this->currentIndex)]->CurrentFrame()->Index() == this->currentIndex); 00099 return this->buffers[this->IndexToLayer(this->currentIndex)]->CurrentFrame(); 00100 } 00101 00102 bool CStreamBufferMulti::IsCurrentDecodable() 00103 { 00104 assert(this->IndexToLayer(this->currentIndex) < this->numLayers); 00105 00106 // If the frame does not exist, return false 00107 if(!this->buffers[this->IndexToLayer(this->currentIndex)]->HasCurrentIndex()) return false; 00108 00109 // If the frame is already decoded, return true 00110 if(this->buffers[this->IndexToLayer(this->currentIndex)]->IsCurrentDecodable()) return true; 00111 00112 // Else, try to decode it 00113 CStreamFrame* frame = this->buffers[this->IndexToLayer(this->currentIndex)]->CurrentFrame(); 00114 assert((frame->AnchorB() != 0) || (frame->AnchorP() != 0)); 00115 assert(frame->Index() == this->currentIndex); 00116 00117 if((frame->AnchorP()?this->IsDecodable(frame->Index() + frame->AnchorP()):true) && 00118 (frame->AnchorB()?this->IsDecodable(frame->Index() + frame->AnchorB()):true)) 00119 { 00120 this->buffers[this->IndexToLayer(this->currentIndex)]->SetCurrentDecodable(); 00121 return true; 00122 } 00123 return false; 00124 } 00125 00126 bool CStreamBufferMulti::IsDecodable(__uint32 index) 00127 { 00128 assert(this->IndexToLayer(index) < this->numLayers); 00129 00130 // If the frame does not exist, return false 00131 if(!this->buffers[this->IndexToLayer(index)]->Has(this->IndexToLayerIndex(index))) return false; 00132 00133 // If the frame is already decoded, return true 00134 if(this->buffers[this->IndexToLayer(index)]->IsDecodable(this->IndexToLayerIndex(index))) return true; 00135 00136 // Else, try to decode it 00137 CStreamFrame* frame = this->buffers[this->IndexToLayer(index)]->Frame(this->IndexToLayerIndex(index)); 00138 assert(frame); 00139 assert((frame->AnchorP() != 0) || (frame->AnchorB() != 0)); 00140 00141 if((frame->AnchorP()?this->IsDecodable(frame->Index() + frame->AnchorP()):true) && 00142 (frame->AnchorB()?this->IsDecodable(frame->Index() + frame->AnchorB()):true)) 00143 { 00144 this->buffers[this->IndexToLayer(index)]->SetDecodable(this->IndexToLayerIndex(index)); 00145 return true; 00146 } 00147 return false; 00148 } 00149 00150 bool CStreamBufferMulti::IsCurrentIndependent() 00151 { 00152 assert(this->IndexToLayer(this->currentIndex) < this->numLayers); 00153 return this->buffers[this->IndexToLayer(this->currentIndex)]->IsCurrentIndependent(); 00154 } 00155 00156 void CStreamBufferMulti::Log() 00157 { 00158 printf("\n\tBuffer @ %u", this->currentIndex); 00159 for(__uint32 index = 0; index < numLayers; index++) 00160 { 00161 printf("\n\t\t%u @ %u [%u]", index, this->buffers[index]->CurrentIndex(), this->LayerIndexToIndex(this->buffers[index]->CurrentIndex(), index)); 00162 } 00163 }
Last updated: February 8, 2011