Parse files consisting of multiple concatenated sequences.
[dirac_parser.git] / src / parser / .svn / text-base / parse_info.cpp.svn-base
blobb66b8145dae7d67df9779b0cea276ea8a4288dfd
1 /* ***** BEGIN LICENSE BLOCK *****
3 * $Id$
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 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) 2007.
21 * All Rights Reserved.
23 * Contributor(s): Andrea Gabriellini (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 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
42 #include <parser/parse_info.hpp>
44 #include <common/util.hpp>
46 namespace dirac
48 namespace parser_project
50   ParseInfo::ParseInfoPrefix::value_type const
51     ParseInfo::ParseInfoPrefix::prefix_[] = { 0x42, 0x42, 0x43, 0x44 };
52   ParseInfo::ParseInfoPrefix ParseInfo::prefix_;
53   
54   enum {
55       PI_HEADER_LENGTH = 13
56     , PI_PREFIX_LENGTH = 4
57     
58     // parse code
59     , CHECK_SEQUENCE_HEADER     = 0xFF
60     , SEQUENCE_HEADER_VALUE     = 0x00
62     , CHECK_END_OF_SEQUENCE     = 0xFF
63     , END_OF_SEQUENCE_VALUE     = 0x10
65     , CHECK_AUXILIARY_DATA      = 0xF8
66     , AUXILIARY_DATA_VALUE      = 0x20
68     , CHECK_PADDING_DATA        = 0xFF // I think, rather than F8, is appr. here
69     , PADDING_DATA_VALUE        = 0x30
70     
71     , CHECK_PICTURE             = 0x08
72     , PICTURE_VALUE             = 0x08
74     , CHECK_CORE_SYNTAX         = 0x88
75     , CORE_SYNTAX_VALUE         = 0x08
77     , CHECK_LOW_DELAY_SYNTAX    = 0x88
78     , LOW_DELAY_SYNTAX_VALUE    = 0x88
80     , CHECK_USING_AC            = 0x48
81     , USING_AC_VALUE            = 0x08
82     
83     , CHECK_IS_REFERENCE        = 0x0C
84     , IS_REFERENCE_VALUE        = 0x0C
85     
86     , CHECK_IS_NON_REFERENCE    = 0x0C
87     , IS_NON_REFERENCE_VALUE    = 0x08
88     
89     , NUMBER_OF_REFERENCES      = 0x03
91     // offsets
92     , PC_OFFSET                 = 4     // parse code offset
93     , NPO_OFFSET                = 5     // next parse offset's offset
94     , PPO_OFFSET                = 9     // previous parse offset's offset
95     , RPPO_OFFSET               = 5     // previous parse offset's offset
96                                         // from the reverse cursor's base
97     // other constants
98     , DATA_OK                   = 1
99     , NO_DATA                   = 0
100     
101   };
102   
103 } /* parser_project */
105 } /* dirac */
108 namespace parser = ::dirac::parser_project;
109 using namespace parser;
111 /*  ParseInfo::ParseInfoPrefix    */
113 ParseInfo::ParseInfoPrefix::const_iterator
114 ParseInfo::ParseInfoPrefix::begin()
116   return prefix_;
119 ParseInfo::ParseInfoPrefix::const_iterator
120 ParseInfo::ParseInfoPrefix::end()
122   return prefix_ + DIRAC_ARRAY_SIZE ( prefix_ );
125 ParseInfo::ParseInfoPrefix::size_type
126 ParseInfo::ParseInfoPrefix::size()
128   return PI_PREFIX_LENGTH;
131 ParseInfo::ParseInfoPrefix::const_reverse_iterator
132 ParseInfo::ParseInfoPrefix::rbegin() 
134   return std::reverse_iterator<const_iterator> ( end() );
137 ParseInfo::ParseInfoPrefix::const_reverse_iterator
138 ParseInfo::ParseInfoPrefix::rend() 
140   return std::reverse_iterator<const_iterator> ( begin() );
143 bool 
144 ParseInfo::ParseInfoPrefix::isParseInfo ( const_iterator cursor, size_type size )
146   DIRAC_MESSAGE_ASSERT ( "cursor cannot be null", 0 != cursor );
147   return (  !( PI_HEADER_LENGTH > size )
148         &&  prefix_[0] == cursor[0] && prefix_[1] == cursor[1] 
149         &&  prefix_[2] == cursor[2] && prefix_[3] == cursor[3] );
152 ParseInfo::const_iterator
153 ParseInfo::prefixBegin()
155   return prefix_.begin();
158 ParseInfo::const_iterator
159 ParseInfo::prefixEnd()
161   return prefix_.end();
164 ParseInfo::const_reverse_iterator
165 ParseInfo::prefixRbegin()
167   return prefix_.rbegin();
170 ParseInfo::const_reverse_iterator
171 ParseInfo::prefixRend()
173   return prefix_.rend();
176 ParseInfo::size_type
177 ParseInfo::prefixSize()
179   return prefix_.size();
183 /*  ParseInfo */
185 ParseInfo::ParseInfo ( const_iterator cursor )
186   : cursor_ ( cursor ) 
188   checkValidity();
191 ParseInfo::~ParseInfo()
193   checkValidity();
196 void
197 ParseInfo::checkValidity() const
199   DIRAC_MESSAGE_ASSERT  ( "parse info header cannot be null", 0 != cursor_ );
202 ParseInfo::value_type
203 ParseInfo::getParseCode() const
205   return cursor_[PC_OFFSET];
208 bool
209 ParseInfo::isParseInfo ( const_iterator buffer, size_type size )
211   DIRAC_MESSAGE_ASSERT ( "buffer cannot be null", 0 != buffer );
212   return prefix_.isParseInfo ( buffer, size );
215 bool
216 ParseInfo::isSequenceHeader() const
218   checkValidity();
219   return SEQUENCE_HEADER_VALUE == ( CHECK_SEQUENCE_HEADER & getParseCode() );
222 bool
223 ParseInfo::isEndOfSequence() const
225   checkValidity();
226   return END_OF_SEQUENCE_VALUE == ( CHECK_END_OF_SEQUENCE & getParseCode() );
229 bool
230 ParseInfo::isAuxiliaryData() const
232   checkValidity();
233   return AUXILIARY_DATA_VALUE == ( CHECK_AUXILIARY_DATA & getParseCode() );
236 bool
237 ParseInfo::isPaddingData() const
239   checkValidity();
240   return PADDING_DATA_VALUE == ( CHECK_PADDING_DATA & getParseCode() );
243 bool
244 ParseInfo::isPicture() const
246   checkValidity();
247   return PICTURE_VALUE == ( CHECK_PICTURE & getParseCode() );
250 bool
251 ParseInfo::isCoreSyntax() const
253   checkValidity();
254   return CORE_SYNTAX_VALUE == ( CHECK_CORE_SYNTAX & getParseCode()  );
257 bool
258 ParseInfo::isLowDelaySyntax() const
260   checkValidity();
261   return LOW_DELAY_SYNTAX_VALUE == ( CHECK_LOW_DELAY_SYNTAX & getParseCode() );
264 bool
265 ParseInfo::isUsingArithmeticCoding() const
267   checkValidity();
268   return USING_AC_VALUE == ( CHECK_USING_AC & getParseCode() );
271 bool
272 ParseInfo::isReference() const
274   checkValidity();
275   return IS_REFERENCE_VALUE == ( CHECK_IS_REFERENCE & getParseCode() );
278 bool
279 ParseInfo::isNotReference() const
281   checkValidity();
282   return IS_NON_REFERENCE_VALUE == ( CHECK_IS_NON_REFERENCE & getParseCode() );
285 ParseInfo::value_type
286 ParseInfo::getNumberOfReferences() const
288   checkValidity();
289   return NUMBER_OF_REFERENCES & getParseCode();
292 bool
293 ParseInfo::isIntra() const
295   checkValidity();
296   return isPicture() && 0 == getNumberOfReferences();
299 bool
300 ParseInfo::isInter() const
302   checkValidity();
303   return isPicture() && getNumberOfReferences();
306 bool
307 ParseInfo::hasPayload() const
309   checkValidity();
310   return END_OF_SEQUENCE_VALUE != getParseCode();
313 ParseInfo::const_iterator
314 ParseInfo::payload() const
316   checkValidity();
317   return cursor_ + PI_HEADER_LENGTH;
320 ParseInfo::const_iterator
321 ParseInfo::next() const
323   checkValidity();
324   
325   return cursor_ + readFourBytes ( cursor_ + NPO_OFFSET );
328 ParseInfo::const_iterator
329 ParseInfo::previous() const
331   checkValidity();
332   
333   return cursor_ - readFourBytes ( cursor_ + PPO_OFFSET );
336 ParseInfo::size_type
337 ParseInfo::size() const
339   checkValidity();
340   size_type next = readFourBytes ( cursor_ + NPO_OFFSET );
341   return 0 == next ? PI_HEADER_LENGTH : next;