source: 3DVCSoftware/branches/0.2-HHI/source/Lib/TLibEncoder/TEncSeqStructure.cpp @ 21

Last change on this file since 21 was 5, checked in by hhi, 13 years ago

Clean version with cfg-files

  • Property svn:eol-style set to native
File size: 12.5 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license.
5 *
6 * Copyright (c) 2010-2011, ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34
35
36#include "TEncSeqStructure.h"
37#include "TEncFormattedStringParser.h"
38#include <algorithm>
39
40ErrVal FrameDescriptor::initFrameDescriptor( const std::string&  rcString, UInt &ruiIncrement, std::map<UInt, UInt>& rcColDirTracker)
41{
42  std::string::size_type uiPos = 0;
43
44  while( 1 )
45  {
46    SliceType eSliceType;
47    Bool bStoreForRef;
48    Bool bIsIDR;
49    UInt uiLayer;
50
51    RNOK( TEncFormattedStringParser::extractFrameType( rcString, eSliceType, bStoreForRef, bIsIDR, uiPos ) );
52    RNOK( TEncFormattedStringParser::extractFrameLayer( rcString, uiLayer, uiPos ) );
53
54    m_aeSliceType.push_back( eSliceType );
55    m_abStoreForRef.push_back( bStoreForRef );
56    m_abIsIDR.push_back( bIsIDR );
57    m_auiLayer.push_back( uiLayer );
58    m_aaiAllowedRelativeRefPocsL0.push_back( std::vector<int>() );
59    m_aaiAllowedRelativeRefPocsL1.push_back( std::vector<int>() );
60    m_aaiAllowedReferenceViewIdxL0.push_back( std::vector<int>() );
61    m_aaiAllowedReferenceViewIdxL1.push_back( std::vector<int>() );
62
63    if( eSliceType == P_SLICE )
64    {
65      RNOK( TEncFormattedStringParser::extractAllowedRelativeRefPocs( rcString, m_aaiAllowedRelativeRefPocsL0.back(), m_aaiAllowedReferenceViewIdxL0.back(), uiPos ) );
66    }
67    else if( eSliceType == B_SLICE )
68    {
69      RNOK( TEncFormattedStringParser::extractAllowedRelativeRefPocs( rcString, m_aaiAllowedRelativeRefPocsL0.back(), m_aaiAllowedRelativeRefPocsL1.back(),
70                                                                      m_aaiAllowedReferenceViewIdxL0.back(), m_aaiAllowedReferenceViewIdxL1.back(), uiPos ) );
71    }
72
73    // check that only available views are referenced
74    {
75      const Int iCurrViewIdx = Int( m_aeSliceType.size() - 1 );
76      for( UInt ui = 0; ui < m_aaiAllowedReferenceViewIdxL0.back().size(); ui++ )
77      {
78        const Int iRefViewIdx = m_aaiAllowedReferenceViewIdxL0.back()[ui];
79        ROF( iRefViewIdx == -1 || ( 0 <= iRefViewIdx && iRefViewIdx < iCurrViewIdx ) );
80      }
81      for( UInt ui = 0; ui < m_aaiAllowedReferenceViewIdxL1.back().size(); ui++ )
82      {
83        const Int iRefViewIdx = m_aaiAllowedReferenceViewIdxL1.back()[ui];
84        ROF( iRefViewIdx == -1 || ( 0 <= iRefViewIdx && iRefViewIdx < iCurrViewIdx ) );
85      }
86    }
87
88    ROT( eSliceType == I_SLICE && ! m_aaiAllowedRelativeRefPocsL0.back().empty() && ! m_aaiAllowedRelativeRefPocsL1.back().empty() );
89    ROT( eSliceType == P_SLICE && ! m_aaiAllowedRelativeRefPocsL1.back().empty() );
90    if( rcString[uiPos] == '_' )
91    {
92      uiPos++;
93      break;
94    }
95  }
96
97  RNOK( TEncFormattedStringParser::extractFrameIncrement( rcString, ruiIncrement, uiPos ) );
98
99  if(rcColDirTracker.find( m_auiLayer[0] )==rcColDirTracker.end())
100    rcColDirTracker.insert( std::make_pair(m_auiLayer[0],1) );
101
102  m_uiColDir = rcColDirTracker[ m_auiLayer[0] ];
103  rcColDirTracker[ m_auiLayer[0]  ] = 1 - rcColDirTracker[ m_auiLayer[0] ];
104
105  ROF( std::string::npos == uiPos || rcString.length() == uiPos );
106
107  return Err::m_nOK;
108}
109
110
111UInt64 TEncSeqStructure::FrameSequencePart::findIncrement( UInt64 uiIncrement, bool &rbSuccess ) const
112{
113  TOF( isInfinitelyLong() || uiIncrement < getSize() );
114  rbSuccess = false;
115  const UInt64 uiOffset = uiIncrement % xGetSize();
116  for( UInt ui = 0; ui < UInt( m_auiIncrements.size() ); ui++ )
117  {
118    if( uiOffset == m_auiIncrements[ui] )
119    {
120      rbSuccess = true;
121      return uiIncrement - uiOffset + ui;
122    }
123  }
124  return 0;
125}
126
127
128TEncSeqStructure::FrameSequencePart::FrameSequencePart( const std::string& rcString )
129{
130  std::string cNoRepString;
131
132  //----- get number of repetitions and number of frames -----
133  UInt uiNumRep = 0;
134  TNOK( TEncFormattedStringParser::extractRepetitions( rcString, cNoRepString, uiNumRep ) );
135  xSetNumRep( uiNumRep );
136  UInt uiNumberOfFrames = 0;
137  TNOK( TEncFormattedStringParser::getNumberOfFrames( cNoRepString, uiNumberOfFrames ) );
138  TOF( uiNumberOfFrames );
139  //----- create array -----
140  m_acFrameDescriptors.resize( uiNumberOfFrames );
141  TOF( m_acFrameDescriptors.size() == uiNumberOfFrames );
142  m_auiIncrements.resize( uiNumberOfFrames );
143
144  //----- initialize array -----
145  UInt uiIndex;
146  size_t uiPos;
147  for( uiPos = 0, uiIndex = 0; uiIndex < uiNumberOfFrames; uiIndex++ )
148  {
149    std::string cFDString;
150    TNOK( TEncFormattedStringParser::extractNextFrameDescription( cNoRepString, cFDString, uiPos ) );
151    UInt uiIncrement = 0;
152    TNOK( m_acFrameDescriptors[uiIndex].initFrameDescriptor( cFDString, uiIncrement, m_cColDirTracker ) );
153    m_auiIncrements[uiIndex] = uiIncrement;
154  }
155  TNOK( xCheck() );
156}
157
158
159ErrVal TEncSeqStructure::FrameSequencePart::xCheck()
160{
161  ROTS( m_acFrameDescriptors.empty() );
162
163  std::vector<bool> abCovered( m_acFrameDescriptors.size(), false );
164  for( UInt uiIndex = 0; uiIndex < m_acFrameDescriptors.size(); uiIndex++ )
165  {
166    const UInt uiInc = m_auiIncrements[uiIndex];
167    ROT( uiInc >= m_acFrameDescriptors.size() ); // error in config file parameter GOPFormatString
168    ROT( abCovered[ uiInc ] );
169    abCovered[uiInc] = true;
170  }
171
172  return Err::m_nOK;
173}
174
175
176TEncSeqStructure::GeneralSequencePart::GeneralSequencePart( const std::string& rcString )
177{
178  std::string cNoRepString;
179
180  UInt uiNumRep = 0;
181  TNOK( TEncFormattedStringParser::extractRepetitions( rcString, cNoRepString, uiNumRep ) );
182  xSetNumRep( uiNumRep );
183  UInt uiNumberOfParts = 0;
184  TNOK( TEncFormattedStringParser::getNumberOfParts( cNoRepString, uiNumberOfParts ) );
185  TOF( uiNumberOfParts );
186
187  //----- create array -----
188  m_apcSequenceParts.resize( uiNumberOfParts );
189
190  //----- initialize array -----
191  UInt uiIndex;
192  size_t uiPos;
193  for( uiPos = 0, uiIndex = 0; uiIndex < uiNumberOfParts; uiIndex++ )
194  {
195    std::string cPartString;
196
197    TNOK( TEncFormattedStringParser::extractPart( cNoRepString, cPartString, uiPos ) );
198
199    if( TEncFormattedStringParser::isFrameSequencePart( cPartString ) )
200    {
201      m_apcSequenceParts[uiIndex] = new FrameSequencePart( cPartString );
202    }
203    else
204    {
205      m_apcSequenceParts[uiIndex] = new GeneralSequencePart( cPartString );
206    }
207  }
208}
209
210
211TEncSeqStructure::TEncSeqStructure()
212: m_pcSequencePart( NULL )
213{
214}
215
216TEncSeqStructure::~TEncSeqStructure()
217{
218  delete m_pcSequencePart; m_pcSequencePart = NULL ;
219}
220
221ErrVal TEncSeqStructure::init( const std::string& rcString )
222{
223  std::string cStringWithoutWhitespace;
224  for( UInt ui = 0; ui < rcString.length(); ui++ )
225  {
226    if( rcString[ui] != ' ' && rcString[ui] != '\t' )
227    {
228      cStringWithoutWhitespace += rcString[ui];
229    }
230  }
231
232  if( cStringWithoutWhitespace.find( "*n{" ) == std::string::npos )
233  {
234    cStringWithoutWhitespace = "*n{" + cStringWithoutWhitespace + "}";
235  }
236  m_pcSequencePart = new GeneralSequencePart( cStringWithoutWhitespace );
237
238  return Err::m_nOK;
239}
240
241
242UInt TEncSeqStructure::getMaxAbsPocDiff( const UInt uiNumberOfFrames )
243{
244  TEncSeqStructure::Iterator cSeqIter = TEncSeqStructure::Iterator( *this, PicOrderCnt( 0 ), 0 );
245
246  UInt uiMaxAbsPocDiff = 0;
247  Int64 iLastPoc = cSeqIter.getPoc();
248  int iNumFramesLeft = uiNumberOfFrames;
249
250  for( int iIndex = 0; 0 != iNumFramesLeft; ++cSeqIter, iIndex++ )
251  {
252    Int64 iPoc = cSeqIter.getPoc();
253    if( iPoc >= uiNumberOfFrames )
254    {
255      // skiped due to frame behind end of sequence
256      continue;
257    }
258    iNumFramesLeft--;
259    //----- check POC differences -----
260    UInt uiAbsFrameDiffRef = (UInt) gAbs( iPoc - iLastPoc );
261    if( uiMaxAbsPocDiff < uiAbsFrameDiffRef  )
262    {
263      uiMaxAbsPocDiff = uiAbsFrameDiffRef;
264    }
265    iLastPoc = iPoc;
266  }
267
268  return uiMaxAbsPocDiff;
269}
270
271TEncSeqStructure::Iterator::Iterator( const TEncSeqStructure &r, PicOrderCnt cLayerChangeStartPoc, int iLayerOffset )
272: m_cBasePoc( 0 )
273, m_cLayerChangeStartPoc( cLayerChangeStartPoc )
274, m_iLayerOffset( iLayerOffset )
275{
276  m_acSeqPartPath.resize( 1 );
277  xGetCurr().m_pcSeqPart = r.m_pcSequencePart;
278  xGetCurr().m_uiCurrPos = 0;
279  xGoToLeaf( false );
280}
281
282void TEncSeqStructure::Iterator::xGoToLeaf( bool bGoToRightmostLeaf )
283{
284  while( !xGetCurr().m_pcSeqPart->isLeafNode() )
285  {
286    SequencePartWithPos cPartWithPos;
287    cPartWithPos.m_pcSeqPart = xGetCurr().m_pcSeqPart->getChildNode( xGetCurr().m_uiCurrPos );
288    cPartWithPos.m_uiCurrPos = 0;
289    if( bGoToRightmostLeaf )
290    {
291      TOT( cPartWithPos.m_pcSeqPart->isInfinitelyLong() );
292      cPartWithPos.m_uiCurrPos = cPartWithPos.m_pcSeqPart->getSize() - 1;
293    }
294    m_acSeqPartPath.push_back( cPartWithPos );
295  }
296}
297
298
299TEncSeqStructure::Iterator& TEncSeqStructure::Iterator::operator++()
300{
301  ++xGetCurr().m_uiCurrPos;
302  if( !xGetCurr().m_pcSeqPart->isInfinitelyLong() && xGetCurr().m_uiCurrPos == xGetCurr().m_pcSeqPart->getSize() )
303  {
304    xGoToNextFrameSequencePart();
305    xGetCurr().m_uiCurrPos = 0;
306  }
307  return *this;
308}
309
310
311TEncSeqStructure::Iterator& TEncSeqStructure::Iterator::traverseByPocDiff( Int64 iPocDiff )
312{
313  const PicOrderCnt cTargetPoc = getPoc() + iPocDiff;
314  const Int64 iIncrement = Int64( xGetCurr().m_pcSeqPart->getIncrement( xGetCurr().m_uiCurrPos ) );
315  if( iIncrement + iPocDiff < 0 )
316  {
317    xGoToPreviousFrameSequencePart();
318    const Int64 iNewIncrement = Int64( xGetCurr().m_pcSeqPart->getIncrement( xGetCurr().m_uiCurrPos ) );
319    const Int64 iNewSize = Int64( xGetCurr().m_pcSeqPart->getSize() );
320    traverseByPocDiff( iPocDiff + iIncrement + iNewSize - iNewIncrement );
321    TOF( cTargetPoc == getPoc() );
322    return *this;
323  }
324
325  if( !xGetCurr().m_pcSeqPart->isInfinitelyLong() )
326  {
327    const Int64 iSize = Int64( xGetCurr().m_pcSeqPart->getSize() );
328    if( iIncrement + iPocDiff >= iSize )
329    {
330      xGoToNextFrameSequencePart();
331      traverseByPocDiff( iPocDiff - ( iSize - iIncrement ) );
332      TOF( cTargetPoc == getPoc() );
333      return *this;
334    }
335  }
336
337  UInt64 uiPocDiffToFind = UInt64( iIncrement + iPocDiff );
338  bool bFound = false;
339  xGetCurr().m_uiCurrPos = xGetCurr().m_pcSeqPart->findIncrement( uiPocDiffToFind, bFound );
340  TOF( bFound );
341  TOF( cTargetPoc == getPoc() );
342  return *this;
343}
344
345
346TEncSeqStructure::Iterator TEncSeqStructure::Iterator::getIterByPocDiff( Int64 iPocDiff ) const
347{
348  Iterator cCopy = *this;
349  cCopy.traverseByPocDiff( iPocDiff );
350  return cCopy;
351}
352
353
354void TEncSeqStructure::Iterator::xGoToPreviousFrameSequencePart()
355{
356  TOF( xGetCurr().m_pcSeqPart->isLeafNode() );
357  TOF( m_acSeqPartPath.size() > 1 );
358  m_acSeqPartPath.pop_back();
359  while( xGetCurr().m_uiCurrPos == 0 )
360  {
361    TOF( m_acSeqPartPath.size() > 1 );
362    m_acSeqPartPath.pop_back();
363  }
364  xGetCurr().m_uiCurrPos--;
365  xGoToLeaf( true );
366  m_cBasePoc -= xGetCurr().m_pcSeqPart->getSize();
367}
368
369
370void TEncSeqStructure::Iterator::xGoToNextFrameSequencePart()
371{
372  TOF( xGetCurr().m_pcSeqPart->isLeafNode() );
373  TOT( xGetCurr().m_pcSeqPart->isInfinitelyLong() );
374  m_cBasePoc += xGetCurr().m_pcSeqPart->getSize();
375  m_acSeqPartPath.pop_back();
376  while( !xGetCurr().m_pcSeqPart->isInfinitelyLong() && xGetCurr().m_uiCurrPos == xGetCurr().m_pcSeqPart->getSize() - 1 )
377  {
378    TOF( m_acSeqPartPath.size() > 1 );
379    m_acSeqPartPath.pop_back();
380  }
381  xGetCurr().m_uiCurrPos++;
382  xGoToLeaf( false );
383}
384
385
386
Note: See TracBrowser for help on using the repository browser.