Parse files consisting of multiple concatenated sequences.
[dirac_parser.git] / src / parser / .svn / text-base / AccessUnit.hpp.svn-base
blob6656a3bd4895b1815027449eabc4bbcc7ec8b3d9
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 #ifndef _ACCESSUNIT_HPP_
39 #define _ACCESSUNIT_HPP_
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
45 #include <common/dirac.h>
46 #include <common/util.hpp>
48 #include <parser/parser.h>
50 #include <vector>
51 #include <stdexcept>
52 #include <algorithm>
54 namespace dirac
56 namespace parser_project
58   struct AccessUnitException : public std::runtime_error 
59   {
60     AccessUnitException ( std::string const& msg ) 
61     : std::runtime_error ( msg ) {}
62   };
63   
64   class AccessUnit
65   {
66   friend class ValidityChecker<AccessUnit>;
67   
68   public: // type definitions
69     typedef AccessUnit                                  class_type;
70     typedef std::vector<uint8>                          Buffer;
71     typedef Buffer::value_type                          value_type;
72     typedef Buffer::pointer                             pointer;
73     typedef Buffer::const_pointer                       const_pointer;
74     typedef Buffer::size_type                           size_type;
75     
76   public: // constructors
77     AccessUnit() : buffer_() {}
78     AccessUnit ( const_pointer begin, size_type size ) 
79     : buffer_() 
80     {
81       setp ( begin, size );
82     }
83     ~AccessUnit() { checkValidity(); }
84   private: // compiler generated methods are not allowed
85     AccessUnit ( AccessUnit const& );
86     AccessUnit& operator= ( AccessUnit const& );
87     
88   public: // main interface
89     void                          setp ( const_pointer begin, size_type size )
90     {
91       ValidityChecker<class_type>  checker ( *this );
93       if ( !dirac_parser_is_access_unit ( begin, size ) )
94         throw AccessUnitException ( "this is not an access unit" );
95       Buffer tmp ( begin, begin + size );
96       buffer_.swap ( tmp );
97     }
98     
99     //! release internal buffer - it doesn't check whether the buffer is empty
100     Buffer*                       releaseBuffer()
101     {
102       ValidityChecker<class_type>  checker ( *this );
104       Buffer* buf = new Buffer;
105       std::swap ( buffer_, *buf );
106       return buf;
107     }
108     
109     const_pointer                 begin() const
110     { checkValidity(); return empty() ? 0 : &( *buffer_.begin() ); }
111     pointer                       begin() 
112     { checkValidity(); return empty() ? 0 : &( *buffer_.begin() ); }
113     const_pointer                 end() const
114     { checkValidity(); return empty() ? 0 : &(*buffer_.begin() ) + size(); }
115     size_type                     size() const
116     { checkValidity(); return buffer_.size(); }
117     bool                          empty() const
118     { checkValidity(); return buffer_.empty(); }
119     
120     bool                          isSequenceHeader() const
121     { 
122       checkValidity(); 
123       return dirac_parser_is_sequence_header ( begin(), size() );
124     }
125     bool                          isPicture() const
126     {
127       checkValidity();
128       return dirac_parser_is_picture ( begin(), size() );
129     }
130     bool                          isIPicture() const
131     {
132       checkValidity();
133       return dirac_parser_is_i_picture ( begin(), size() );
134     }
135     bool                          isAuxiliaryData() const
136     {
137       checkValidity();
138       return dirac_parser_is_auxiliary_data ( begin(), size() );
139     }
140     bool                          isPaddingData() const
141     {
142       checkValidity();
143       return dirac_parser_is_padding_data ( begin(), size() );
144     }
145     bool                          isEndOfSequence() const
146     {
147       checkValidity();
148       return dirac_parser_is_end_of_sequence ( begin(), size() );
149     }
150     bool                          isLastEndOfSequence() const
151     {
152       checkValidity();
153       return dirac_parser_is_last_end_of_sequence ( begin(), size() );
154     }
155         
156   private: // helper methods
157     void                          checkValidity() const
158     {
159       DIRAC_MESSAGE_ASSERT  ( "buffer empty or must hold access unit"
160       ,  buffer_.empty() 
161       || dirac_parser_is_access_unit ( &buffer_[0], buffer_.size() ) );
162     }
163   private: // members
164     Buffer                                      buffer_;
165   };
166 } /* parser_project */
168 } /* dirac */
171 #endif /* _ACCESSUNIT_HPP_ */