source: 3DVCSoftware/branches/0.1-poznan-univ/source/Lib/TLibEncoder/TEncFormattedStringParser.cpp

Last change on this file was 2, checked in by hhi, 13 years ago

inital import

  • Property svn:eol-style set to native
File size: 15.1 KB
Line 
1
2#include "TEncFormattedStringParser.h"
3#include <sstream>
4#include <algorithm>
5
6const std::string TEncFormattedStringParser::sm_cSetOfTypes      ("AIPBNipbn");
7const std::string TEncFormattedStringParser::sm_cSetOfDigits     ("0123456789");
8const std::string TEncFormattedStringParser::sm_cSetOfPartStart  ("AIPBNipbn*");
9
10ErrVal TEncFormattedStringParser::extractFrameType( const std::string &rcString,
11                                                SliceType& reSliceType,
12                                                bool &rbStoreForRef,
13                                                bool &rbIsIDR,
14                                                std::string::size_type &ruiStartPos )
15{
16  ROT( rcString.length() <= ruiStartPos );
17
18  rbIsIDR = rcString[ruiStartPos] == 'A';
19  rbStoreForRef = isupper( rcString[ruiStartPos] ) != 0;
20
21  const char cType = rcString[ruiStartPos++];
22  switch( toupper( cType ) )
23  {
24  case 'A':
25  case 'I':
26    reSliceType = I_SLICE ;
27    break;
28  case 'P':
29    reSliceType = P_SLICE ;
30    break;
31  case 'B':
32    reSliceType = B_SLICE ;
33    break;
34  default:
35    ROT( 1 );
36  }
37
38  return Err::m_nOK;
39}
40
41
42
43
44ErrVal TEncFormattedStringParser::extractFrameIncrement( const std::string &rcString,
45                                                     UInt &ruiIncrement,
46                                                     std::string::size_type &ruiStartPos )
47{
48  RNOK( xExtractUInt( rcString, ruiIncrement, ruiStartPos ) );
49  return Err::m_nOK;
50}
51
52ErrVal TEncFormattedStringParser::extractFrameLayer( const std::string &rcString,
53                                                 UInt &ruiLayer,
54                                                 std::string::size_type &ruiStartPos )
55{
56  ruiLayer = 0;
57  ROTRS( std::string::npos == ruiStartPos, Err::m_nOK );
58  ROTRS( rcString.length() <= ruiStartPos, Err::m_nOK );
59  ROTRS( rcString[ruiStartPos] != 'L', Err::m_nOK );
60  ruiStartPos++;
61  RNOK( xExtractUInt( rcString, ruiLayer, ruiStartPos ) );
62  return Err::m_nOK;
63}
64
65ErrVal TEncFormattedStringParser::xExtractRelRefPocAndRefViewIdx( const std::string &rcString,
66                                                                  std::vector<int> &raiAllowedRelativeRefPocs,
67                                                                  std::vector<int> &raiAllowedReferenceViewIdx,
68                                                                  std::string::size_type &ruiStartPos )
69{
70  if( rcString[ruiStartPos] == 'v' || rcString[ruiStartPos] == 'V' )
71  {
72    ruiStartPos++;
73    int iRefViewIdx = 0;
74    RNOK( xExtractInt( rcString, iRefViewIdx, ruiStartPos ) );
75    assert( iRefViewIdx >= 0 );
76    ROF( raiAllowedReferenceViewIdx.end() == std::find( raiAllowedReferenceViewIdx.begin(), raiAllowedReferenceViewIdx.end(), iRefViewIdx ) );
77    raiAllowedReferenceViewIdx.push_back( iRefViewIdx );
78    raiAllowedRelativeRefPocs.push_back( 0 );
79  }
80  else
81  {
82    int iRelRefPoc = 0;
83    RNOK( xExtractInt( rcString, iRelRefPoc, ruiStartPos ) );
84    ROF( raiAllowedRelativeRefPocs.end() == std::find( raiAllowedRelativeRefPocs.begin(), raiAllowedRelativeRefPocs.end(), iRelRefPoc ) );
85    raiAllowedRelativeRefPocs.push_back( iRelRefPoc );
86    raiAllowedReferenceViewIdx.push_back( -1 );
87  }
88  return Err::m_nOK;
89}
90
91ErrVal TEncFormattedStringParser::extractAllowedRelativeRefPocs( const std::string &rcString,
92                                                             std::vector<int> &raiAllowedRelativeRefPocs,
93                                                             std::vector<int> &raiAllowedReferenceViewIdx,
94                                                             std::string::size_type &ruiStartPos )
95{
96  ROF( raiAllowedRelativeRefPocs.empty() );
97  ROTRS( std::string::npos == ruiStartPos, Err::m_nOK );
98  ROTRS( rcString.length() <= ruiStartPos, Err::m_nOK );
99  RNOK( xEatChar( rcString, '(', ruiStartPos ) );
100
101  do
102  {
103    RNOK( xExtractRelRefPocAndRefViewIdx( rcString, raiAllowedRelativeRefPocs, raiAllowedReferenceViewIdx, ruiStartPos ) );
104  }
105  while( xEatChar( rcString, ',', ruiStartPos ) == Err::m_nOK );
106  RNOK( xEatChar( rcString, ')', ruiStartPos ) );
107
108  return Err::m_nOK;
109}
110
111ErrVal TEncFormattedStringParser::extractAllowedRelativeRefPocs( const std::string &rcString,
112                                                             std::vector<int> &raiAllowedRelativeRefPocsL0,
113                                                             std::vector<int> &raiAllowedRelativeRefPocsL1,
114                                                             std::vector<int> &raiAllowedReferenceViewIdxL0,
115                                                             std::vector<int> &raiAllowedReferenceViewIdxL1,
116                                                             std::string::size_type &ruiStartPos )
117{
118  ROF( raiAllowedRelativeRefPocsL0.empty() );
119  ROF( raiAllowedRelativeRefPocsL1.empty() );
120
121  ROTRS( std::string::npos == ruiStartPos, Err::m_nOK );
122  ROTRS( rcString.length() <= ruiStartPos, Err::m_nOK );
123
124  RNOK( xEatChar( rcString, '(', ruiStartPos ) );
125  do
126  {
127    RNOK( xExtractRelRefPocAndRefViewIdx( rcString, raiAllowedRelativeRefPocsL0, raiAllowedReferenceViewIdxL0, ruiStartPos ) );
128  }
129  while( xEatChar( rcString, ',', ruiStartPos ) == Err::m_nOK );
130  RNOK( xEatChar( rcString, ';', ruiStartPos ) );
131  do
132  {
133    RNOK( xExtractRelRefPocAndRefViewIdx( rcString, raiAllowedRelativeRefPocsL1, raiAllowedReferenceViewIdxL1, ruiStartPos ) );
134  }
135  while( xEatChar( rcString, ',', ruiStartPos ) == Err::m_nOK );
136  RNOK( xEatChar( rcString, ')', ruiStartPos ) );
137  return Err::m_nOK;
138}
139
140
141
142
143
144
145ErrVal TEncFormattedStringParser::xEatChar( const std::string &rcString,
146                                        char cChar,
147                                        std::string::size_type &ruiStartPos )
148{
149  ROTS( std::string::npos == ruiStartPos );
150  ROTS( rcString.length() <= ruiStartPos );
151  ROFS( rcString[ruiStartPos] == cChar );
152  ruiStartPos++;
153  return Err::m_nOK;
154}
155
156ErrVal TEncFormattedStringParser::xExtractUInt( const std::string &rcString,
157                                            UInt &ruiVal,
158                                            std::string::size_type &ruiStartPos )
159{
160  ROT( std::string::npos == ruiStartPos );
161  ROT( rcString.length() <= ruiStartPos );
162  const std::string::size_type uiPos = rcString.find_first_not_of( sm_cSetOfDigits, ruiStartPos + ( rcString[ruiStartPos] == '+' ? 1 : 0 ) );
163  ROT( ruiStartPos == uiPos );
164  std::stringstream( rcString.substr( ruiStartPos, std::string::npos == uiPos ? std::string::npos : uiPos - ruiStartPos ) ) >> ruiVal;
165  ruiStartPos = uiPos;
166  return Err::m_nOK;
167}
168
169ErrVal TEncFormattedStringParser::xExtractInt( const std::string &rcString,
170                                           int &riVal,
171                                           std::string::size_type &ruiStartPos )
172{
173  ROT( std::string::npos == ruiStartPos );
174  ROT( rcString.length() <= ruiStartPos );
175  const std::string::size_type uiPos = rcString.find_first_not_of( sm_cSetOfDigits, ruiStartPos + ( rcString[ruiStartPos] == '+' || rcString[ruiStartPos] == '-' ? 1 : 0 ) );
176  ROT( ruiStartPos == uiPos );
177  std::stringstream( rcString.substr( ruiStartPos, std::string::npos == uiPos ? std::string::npos : uiPos - ruiStartPos ) ) >> riVal;
178  ruiStartPos = uiPos;
179  return Err::m_nOK;
180}
181
182
183ErrVal
184TEncFormattedStringParser::separatString   ( const std::string&  rcString,
185                                         std::string& rcFDString,
186                                         std::string& rcMmcoString,
187                                         std::string& rcRplrStringL0,
188                                         std::string& rcRplrStringL1)
189{
190  size_t uiMPos  = rcString.find_first_of( "M" );
191  size_t uiR1Pos = rcString.find_first_of( "R" );
192  size_t uiR2Pos = rcString.find_last_of ( "R" );
193
194  size_t uiSize = rcString.size();
195
196  if( std::string::npos == uiMPos )
197  {
198    rcMmcoString = "";
199    if( std::string::npos == uiR1Pos )
200    {
201      rcFDString    = rcString;
202      rcRplrStringL0 = "";
203      rcRplrStringL1 = "";
204    }
205    else
206    {
207      rcFDString      = rcString.substr( 0,     uiR1Pos );
208      if( uiR1Pos == uiR2Pos )
209      {
210        rcRplrStringL0 = rcString.substr( uiR1Pos, uiSize - uiR1Pos );
211        rcRplrStringL1 = "";
212      }
213      else
214      {
215        rcRplrStringL0 = rcString.substr( uiR1Pos, uiR2Pos - uiR1Pos );
216        rcRplrStringL1 = rcString.substr( uiR2Pos, uiSize  - uiR2Pos );
217      }
218    }
219  }
220  else
221  {
222    if( std::string::npos == uiR1Pos )
223    {
224      rcFDString      = rcString.substr( 0,     uiMPos );
225      rcMmcoString    = rcString.substr( uiMPos, uiSize - uiMPos );
226      rcRplrStringL0 = "";
227      rcRplrStringL1 = "";
228    }
229    else
230    {
231      if( uiMPos < uiR1Pos )
232      {
233        rcFDString      = rcString.substr( 0,      uiMPos );
234        rcMmcoString    = rcString.substr( uiMPos,  uiR1Pos - uiMPos );
235        if( uiR1Pos == uiR2Pos )
236        {
237          rcRplrStringL0 = rcString.substr( uiR1Pos, uiSize - uiR1Pos );
238          rcRplrStringL1 = "";
239        }
240        else
241        {
242          rcRplrStringL0 = rcString.substr( uiR1Pos, uiR2Pos - uiR1Pos );
243          rcRplrStringL1 = rcString.substr( uiR2Pos, uiSize  - uiR2Pos );
244        }
245      }
246      else
247      {
248        rcFDString      = rcString.substr( 0,      uiR1Pos );
249
250        if( uiR1Pos == uiR2Pos )
251        {
252          rcRplrStringL0 = rcString.substr( uiR1Pos, uiMPos - uiR1Pos );
253          rcRplrStringL1 = "";
254          rcMmcoString    = rcString.substr( uiMPos,  uiSize - uiMPos );
255        }
256        else
257        {
258          if( uiMPos < uiR2Pos )
259          {
260            rcRplrStringL0 = rcString.substr( uiR1Pos, uiMPos  - uiR1Pos );
261            rcMmcoString    = rcString.substr( uiMPos, uiR2Pos - uiMPos );
262            rcRplrStringL1 = rcString.substr( uiR2Pos, uiSize  - uiR2Pos );
263
264          }
265          else
266          {
267            rcRplrStringL0 = rcString.substr( uiR1Pos, uiR2Pos - uiR1Pos );
268            rcRplrStringL1 = rcString.substr( uiR2Pos, uiMPos  - uiR2Pos );
269            rcMmcoString    = rcString.substr( uiMPos,  uiSize  - uiMPos );
270          }
271        }
272      }
273    }
274  }
275
276  if( rcRplrStringL0.size() == 1 )
277  {
278    rcRplrStringL0 = "";
279  }
280
281  if( rcRplrStringL1.size() == 1 )
282  {
283    rcRplrStringL1 = "";
284  }
285
286  return Err::m_nOK;
287}
288
289bool
290TEncFormattedStringParser::isFrameSequencePart( const std::string& rcString )
291{
292  return ( rcString.find( '*', 1 ) == std::string::npos );
293}
294
295
296ErrVal
297TEncFormattedStringParser::extractRepetitions( const std::string&  rcString,
298                                           std::string&        rcNoRepString,
299                                           UInt&               ruiNumberOfRepetitions )
300{
301  ROT( rcString.empty() );
302  if( rcString[0] != '*' )
303  {
304    ruiNumberOfRepetitions = 1;
305    rcNoRepString          = rcString;
306  }
307  else
308  {
309    size_t  uiLastPos   = rcString.length () - 1;
310    size_t  uiOpenPos   = rcString.find   ( '{' );
311    size_t  uiClosePos  = rcString.rfind  ( '}' );
312
313    ROTS(  uiOpenPos   == std::string::npos  );
314    ROFS(  uiClosePos  == uiLastPos          );
315
316    rcNoRepString     = rcString.substr( uiOpenPos+1, uiClosePos-uiOpenPos-1 ); //GT: abc  if rcString = *n100{abc}
317    std::string cNStr = rcString.substr( 1,           uiOpenPos - 1          ); //GT  n100 if rcString = *n100{abc}
318    ROT( cNStr.empty() );
319
320    if( uiOpenPos==2 && cNStr[0]=='n' )
321    {
322//      ruiNumberOfRepetitions = TypeLimits<UInt>::m_iMax; // reserved value representing "infinity"
323      ruiNumberOfRepetitions = MAX_UINT; // reserved value representing "infinity"
324    }
325    else
326    {
327      ROFS( cNStr.find_first_not_of( sm_cSetOfDigits ) == std::string::npos );
328      ruiNumberOfRepetitions  = atoi( cNStr.c_str() );
329    }
330  }
331
332  return Err::m_nOK;
333}
334
335ErrVal
336TEncFormattedStringParser::getNumberOfFrames( const std::string&  rcString,
337                                          UInt&               ruiNumberOfFrames )
338{
339  size_t  uiPos           = rcString.find_first_of( sm_cSetOfTypes );
340  size_t  uiUnderScorePos = 0 ;
341  ruiNumberOfFrames = 0;
342
343
344  while( uiPos != std::string::npos )
345  {
346    ruiNumberOfFrames++;
347    uiUnderScorePos = rcString.find_first_of( std::string("_"), uiPos+1 ) ;
348    ROT( uiUnderScorePos == std::string::npos ) ;
349    uiPos = rcString.find_first_of( sm_cSetOfTypes, uiUnderScorePos+1 );
350  }
351
352  return Err::m_nOK;
353}
354
355ErrVal
356TEncFormattedStringParser::extractNextFrameDescription( const std::string&  rcString,
357                                                    std::string&        rcFDString,
358                                                    size_t&        ruiStartPos )
359{
360  ROTS( ruiStartPos >= rcString.length()-1 );
361  size_t uiUnderScorePos = rcString.find_first_of( std::string("_"), ruiStartPos+1 ) ;
362  ROT( uiUnderScorePos == std::string::npos ) ;
363  size_t uiEndPos = rcString.find_first_of( sm_cSetOfTypes, uiUnderScorePos+1 );
364
365  rcFDString  = rcString.substr( ruiStartPos, uiEndPos - ruiStartPos );
366  ruiStartPos = uiEndPos;
367
368  return Err::m_nOK;
369}
370
371ErrVal
372TEncFormattedStringParser::getNumberOfParts( const std::string& rcString,
373                                         UInt&              ruiNumberOfParts )
374{
375  size_t  uiPos       = rcString.find_first_of( sm_cSetOfPartStart );
376  ruiNumberOfParts  = 0;
377
378  while( uiPos != std::string::npos )
379  {
380    ruiNumberOfParts++;
381
382    if( rcString[uiPos] == '*' ) //GT: Exclude sub-parts
383    {
384      size_t  uiEndPos        = rcString.find( '{', uiPos+1 );
385      UInt  uiOpenBrackets  = 1;
386      ROTS( uiEndPos == std::string::npos );
387
388      while( uiOpenBrackets )
389      {
390        uiEndPos  = rcString.find_first_of( "{}", uiEndPos+1 );
391        ROTS( uiEndPos == std::string::npos );
392
393        if( rcString[uiEndPos] == '{' )   uiOpenBrackets++;
394        else                              uiOpenBrackets--;
395      }
396
397      uiPos = rcString.find_first_of( sm_cSetOfPartStart, uiEndPos + 1 );
398    }
399    else
400    {
401      uiPos = rcString.find( '*', uiPos+1 );
402    }
403  }
404
405  return Err::m_nOK;
406}
407
408ErrVal
409TEncFormattedStringParser::extractPart( const std::string&  rcString,
410                                    std::string&        rcPartString,
411                                    size_t&        ruiStartPos )
412{
413  ROTS( ruiStartPos >= rcString.length()-1 );
414
415  size_t uiNextStartPos;
416
417  if( rcString[ruiStartPos] == '*' )
418  {
419    size_t  uiEndPos        = rcString.find( '{', ruiStartPos+1 );
420    UInt  uiOpenBrackets  = 1;
421    ROTS( uiEndPos == std::string::npos );
422
423    while( uiOpenBrackets )
424    {
425      uiEndPos  = rcString.find_first_of( "{}", uiEndPos+1 );
426      ROTS( uiEndPos == std::string::npos );
427
428      if( rcString[uiEndPos] == '{' )   uiOpenBrackets++;
429      else                              uiOpenBrackets--;
430    }
431
432    uiNextStartPos = rcString.find_first_of( sm_cSetOfPartStart, uiEndPos + 1 );
433  }
434  else
435  {
436    uiNextStartPos = rcString.find( '*', ruiStartPos+1 );
437  }
438
439  rcPartString  = rcString.substr( ruiStartPos, uiNextStartPos - ruiStartPos );
440  ruiStartPos   = uiNextStartPos;
441
442  return Err::m_nOK;
443}
444
445ErrVal TEncFormattedStringParser::extractHighestLayerOfGOPString( const std::string &rcString, UInt &ruiLayer )
446{
447  std::string::size_type uiPos = 0;
448  ruiLayer = 0;
449
450  while( ( uiPos = rcString.find_first_of( 'L', uiPos ) ) != std::string::npos )
451  {
452    uiPos++;
453
454    UInt ui = 0;
455    RNOK( xExtractUInt( rcString, ui, uiPos ) );
456    if( ui > ruiLayer )
457      ruiLayer = ui;
458  }
459  return Err::m_nOK;
460}
461
462
Note: See TracBrowser for help on using the repository browser.