Updating Contact email
[BrunelResearch-dirac.git] / libdirac_encoder / enc_queue.cpp
blobda68bc629216ed61c3569df6781ff20a35e71933
1 /* ***** BEGIN LICENSE BLOCK *****
3 * $Id$ $Name$
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License
8 * Version 1.1 (the "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14 * the specific language governing rights and limitations under the License.
16 * The Original Code is BBC Research and Development code.
18 * The Initial Developer of the Original Code is the British Broadcasting
19 * Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2008.
21 * All Rights Reserved.
23 * Contributor(s): Thomas Davies (Original Author),
25 * Alternatively, the contents of this file may be used under the terms of
26 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
27 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
28 * the GPL or the LGPL are applicable instead of those above. If you wish to
29 * allow use of your version of this file only under the terms of the either
30 * the GPL or LGPL and not to allow others to use your version of this file
31 * under the MPL, indicate your decision by deleting the provisions above
32 * and replace them with the notice and other provisions required by the GPL
33 * or LGPL. If you do not delete the provisions above, a recipient may use
34 * your version of this file under the terms of any one of the MPL, the GPL
35 * or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include <libdirac_encoder/enc_queue.h>
39 #include <algorithm>
40 using namespace dirac;
42 //Simple constructor for decoder operation
43 EncQueue::EncQueue(){}
45 //Copy constructor. Why anyone would need this I don't know.
46 EncQueue::EncQueue(const EncQueue& cpy)
48 // first delete all frames in the current buffer
49 for (size_t i=0 ; i<m_pic_data.size() ; ++i)
51 delete m_pic_data[i];
52 }//i
54 // next create new arrays, copying from the initialising buffer
55 m_pic_data.resize(cpy.m_pic_data.size());
56 for (size_t i=0 ; i<m_pic_data.size() ; ++i){
57 m_pic_data[i] = new EncPicture( *(cpy.m_pic_data[i]) );
58 }//i
60 // now copy the map
61 m_pnum_map = cpy.m_pnum_map;
65 //Assignment=. Not sure why this would be used either.
66 EncQueue& EncQueue::operator=(const EncQueue& rhs){
67 if (&rhs!=this)
69 // delete all the frames in the lhs buffer
70 for (size_t i=0 ; i<m_pic_data.size() ; ++i)
72 delete m_pic_data[i];
73 }//i
75 // next create new arrays, copying from the rhs
76 m_pic_data.resize(rhs.m_pic_data.size());
77 for (size_t i=0 ; i<m_pic_data.size() ; ++i)
79 m_pic_data[i] = new EncPicture( *(rhs.m_pic_data[i]) );
80 }//i
82 // now copy the map
83 m_pnum_map = rhs.m_pnum_map;
86 return *this;
89 //Destructor
90 EncQueue::~EncQueue()
92 for (size_t i=0 ; i<m_pic_data.size() ;++i)
93 delete m_pic_data[i];
96 EncPicture& EncQueue::GetPicture( const unsigned int pnum )
97 {//get picture with a given picture number, NOT with a given position in the buffer.
98 //If the picture number does not occur, the first picture in the buffer is returned.
100 std::map<unsigned int,unsigned int>::iterator it = m_pnum_map.find(pnum);
102 unsigned int pos = 0;
103 if (it != m_pnum_map.end())
104 pos = it->second;
106 return *(m_pic_data[pos]);
109 const EncPicture& EncQueue::GetPicture( const unsigned int pnum ) const
110 { //as above, but const version
112 std::map<unsigned int,unsigned int>::const_iterator it = m_pnum_map.find(pnum);
114 unsigned int pos=0;
115 if (it != m_pnum_map.end())
116 pos = it->second;
118 return *(m_pic_data[pos]);
121 EncPicture& EncQueue::GetPicture( const unsigned int pnum, bool& is_present )
122 {//get picture with a given picture number, NOT with a given position in the buffer.
123 //If the picture number does not occur, the first picture in the buffer is returned.
125 std::map<unsigned int,unsigned int>::iterator it = m_pnum_map.find(pnum);
127 unsigned int pos = 0;
128 if (it != m_pnum_map.end())
130 is_present = true;
131 pos = it->second;
133 else
134 is_present=false;
136 return *(m_pic_data[pos]);
139 const EncPicture& EncQueue::GetPicture( const unsigned int pnum, bool& is_present ) const
140 { //as above, but const version
142 std::map<unsigned int,unsigned int>::const_iterator it = m_pnum_map.find(pnum);
144 unsigned int pos=0;
145 if (it != m_pnum_map.end())
147 is_present = true;
148 pos = it->second;
150 else
151 is_present=false;
153 return *(m_pic_data[pos]);
156 bool EncQueue::IsPictureAvail( const unsigned int pnum ) const
159 std::map<unsigned int,unsigned int>::const_iterator it = m_pnum_map.find(pnum);
161 if (it != m_pnum_map.end())
162 return true;
163 else
164 return false;
167 std::vector<int> EncQueue::Members() const
169 std::vector<int> members( 0 );
170 for (unsigned int i=0; i<m_pic_data.size(); ++i )
172 const PictureParams& pparams = m_pic_data[i]->GetPparams();
173 members.push_back( pparams.PictureNum() );
174 }// i
176 return members;
179 void EncQueue::PushPicture( const PictureParams& pp )
180 {// Put a new picture onto the top of the stack
182 // if picture is present - return
183 if (IsPictureAvail(pp.PictureNum()))
184 return;
186 // if ( pp.PicSort().IsRef() )
187 // m_ref_count++;
189 EncPicture* pptr = new EncPicture(pp);
190 // add the picture to the buffer
191 m_pic_data.push_back(pptr);
193 // put the picture number into the index table
194 std::pair<unsigned int,unsigned int> temp_pair(pp.PictureNum() , m_pic_data.size()-1);
195 m_pnum_map.insert(temp_pair);
198 void EncQueue::CopyPicture( const EncPicture& picture )
200 PushPicture(picture.GetPparams());
202 bool is_present;
204 EncPicture & p= GetPicture(picture.GetPparams().PictureNum(), is_present);
205 if(is_present)
206 p = picture;
209 void EncQueue::ClearSlot(const unsigned int pos)
211 // Clear a slot corresponding to position pos to take more data
213 std::pair<unsigned int,unsigned int>* tmp_pair;
215 if (pos<m_pic_data.size())
217 delete m_pic_data[pos];
219 m_pic_data.erase(m_pic_data.begin() + pos);
221 //make a new map
222 m_pnum_map.clear();
223 for (size_t i=0 ; i<m_pic_data.size() ; ++i)
225 tmp_pair = new std::pair<unsigned int,unsigned int>( m_pic_data[i]->GetPparams().PictureNum() , i);
226 m_pnum_map.insert(*tmp_pair);
227 delete tmp_pair;
228 }//i
233 void EncQueue::SetRetiredPictureNum(const int show_pnum, const int current_coded_pnum)
236 if ( IsPictureAvail(current_coded_pnum))
238 PictureParams &pparams = GetPicture(current_coded_pnum).GetPparams();
239 pparams.SetRetiredPictureNum(-1);
240 for (size_t i=0 ; i<m_pic_data.size() ; ++i)
242 if (m_pic_data[i]->GetPparams().PicSort().IsRef() )
244 if ( (m_pic_data[i]->GetPparams().PictureNum() + m_pic_data[i]->GetPparams().ExpiryTime() ) <= show_pnum)
246 pparams.SetRetiredPictureNum(m_pic_data[i]->GetPparams().PictureNum());
247 break;
250 }//i
254 void EncQueue::CleanAll(const int show_pnum, const int current_coded_pnum)
255 {// clean out all frames that have expired
256 if (IsPictureAvail(current_coded_pnum))
258 for (size_t i=0 ; i<m_pic_data.size() ; ++i)
260 if ( (m_pic_data[i]->GetPparams().PictureNum() + m_pic_data[i]->GetPparams().ExpiryTime() ) <= show_pnum)
261 ClearSlot(i);
262 }//i
266 void EncQueue::CleanRetired(const int show_pnum, const int current_coded_pnum)
267 {// clean out all frames that have expired
268 if ( IsPictureAvail(current_coded_pnum) )
270 PictureParams &pparams = GetPicture(current_coded_pnum).GetPparams();
271 // Remove Reference picture specified in retired picture number.
272 if (pparams.PicSort().IsRef() && pparams.RetiredPictureNum()>= 0)
273 Remove(pparams.RetiredPictureNum());
274 pparams.SetRetiredPictureNum(-1);
275 // Remove non-reference frames that have expired
276 for (size_t i=0 ; i<m_pic_data.size() ; ++i){
277 if ( (m_pic_data[i]->GetPparams().PictureNum()+
278 m_pic_data[i]->GetPparams().ExpiryTime() )<=show_pnum
279 && m_pic_data[i]->GetPparams().PicSort().IsNonRef() )
280 ClearSlot(i);
281 }//i
285 void EncQueue::Remove(const int pnum)
287 for (size_t i=0 ; i<m_pic_data.size() ; ++i){
288 if ( m_pic_data[i]->GetPparams().PictureNum() == pnum)
289 ClearSlot(i);
290 }//i