1 /* ***** BEGIN LICENSE BLOCK *****
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
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
36 * ***** END LICENSE BLOCK ***** */
45 #include <common/dirac.h>
46 #include <common/util.hpp>
47 #include <parser/StreamReader.hpp>
48 #include <parser/parser.h>
49 #include <parser/AccessUnit.hpp>
57 namespace parser_project
60 //! C++ wrapper for the parser API - aims to offer the same functionality
62 typedef int (*parser_getter) ( uint8 const**, size_t, size_t*);
65 explicit Parser ( std::auto_ptr<StreamReader> str ) : str_ ( str )
73 private: // compiler generated methods are not allowed
74 Parser ( Parser const& );
75 Parser& operator= ( Parser const& );
78 //! retrieves the next sequence header in the associated stream
79 //! au will contain a copy of the sequence header, if found
80 //! the cursor will not be moved past the beginning of the seqhdr, if found
81 bool getNextSequenceHeader ( AccessUnit& au)
84 return getAccessUnitFwd ( dirac_parser_get_sequence_header, au );
87 //! retrieves the next picture in the stream and copies content in au
88 //! the cursor will point to the start of the pic, if found
89 bool getNextPicture ( AccessUnit& au )
92 return getAccessUnitFwd ( dirac_parser_get_next_picture, au );
95 //! retrieves the next i picture in the stream and copies content in au
96 //! the cursor will point to the start of the i pic, if found
97 bool getNextIPicture ( AccessUnit& au )
100 return getAccessUnitFwd ( dirac_parser_get_next_i_picture, au );
103 //! retrieves the next access unit and copies content in au
104 //! the cursor will point to the start of the access unit, if found
105 bool getNextAccessUnit ( AccessUnit& au )
108 return getAccessUnitFwd ( dirac_parser_get_next_access_unit, au );
111 //! Retrieves I pics forward or backward in the bitstream and copies
112 //! content in au, if found
113 //! the cursor will point to the start of the requested i pic, if found
114 bool getNextIPictureSkipN ( AccessUnit& au, int n )
120 if ( !dirac_parser_get_i_picture_skip_n ( &str_->gptr()
126 if ( !str_->addDataBwd() ) return false;
130 if ( !str_->addDataFwd() ) return false;
135 au.setp ( str_->gptr(), au_size );
141 //! Retrieves I pics bwd in the bitstream and copies content in au
142 //! the cursor will point to the start of the requested i pic, if found
143 bool getPreviousIPicture ( AccessUnit& au )
148 if ( !dirac_parser_get_previous_i_picture ( &str_->gptr()
152 if ( !str_->addDataBwd() ) return false;
156 au.setp ( str_->gptr(), au_size );
162 //! Determines whether the cursor is pointing to the start of a dirac AU
163 bool isAccessUnit() const
166 return dirac_parser_is_access_unit ( str_->gptr(), str_->sizeFwd() );
169 //! Determines whether the cursor is pointing to the start of a dirac SeqHdr
170 bool isSequenceHeader() const
173 return dirac_parser_is_sequence_header ( str_->gptr(), str_->sizeFwd() );
176 //! Determines whether the cursor is pointing to the start of a picture
177 bool isPicture() const
180 return dirac_parser_is_picture ( str_->gptr(), str_->sizeFwd() );
183 //! Determines whether the cursor is pointing to the start of an intra pic
184 bool isIPicture() const
187 return dirac_parser_is_i_picture ( str_->gptr(), str_->sizeFwd() );
190 //! Determines whether the cursor is pointing to the start of aux data
191 bool isAuxiliaryData() const
194 return dirac_parser_is_auxiliary_data ( str_->gptr(), str_->sizeFwd() );
197 //! Determines whether the cursor is pointing to the start of padding data
198 bool isPaddingData() const
201 return dirac_parser_is_padding_data ( str_->gptr(), str_->sizeFwd() );
204 //! Determines whether the cursor is pointing to the start of the end of seq
205 bool isEndOfSequence() const
208 return dirac_parser_is_end_of_sequence ( str_->gptr(), str_->sizeFwd() );
211 //! Determines whether the cursor if pointing to the start of the last
212 //! end of sequence of the stream
213 bool isLastEndOfSequence() const
216 return dirac_parser_is_last_end_of_sequence ( str_->gptr(), str_->sizeFwd() );
219 //! Retrieves pic number, if cursor points to start of pic
220 bool getPictureNumber ( uint32& n ) const
223 return dirac_parser_get_picture_number ( str_->gptr()
228 //! Move cursor forward by one access unit - Note that the user must
229 //! ensure that the cursor points to start of AU or else undefined behaviour
234 if ( !dirac_parser_move_cursor_fwd ( &str_->gptr(), str_->sizeFwd() ) ) {
235 if ( !str_->addDataFwd() ) return false;
242 //! Rewind the cursor to the beginning of the stream, if possible
243 //! otherwise it has no effect
247 return str_->rewind();
250 //! Allows to swap two Parser objects, guaranteed not to throw
251 void swap ( Parser& rhs )
254 std::auto_ptr<StreamReader> tmp ( str_.release() );
259 //! Returns the major version number of this API
260 int getVersionMajor() const
262 return dirac_parser_get_version_major();
265 //! Returns the minor version number of this API
266 int getVersionMinor() const
268 return dirac_parser_get_version_minor();
271 //! Returns the revision number of this API
272 int getRevision() const
274 return dirac_parser_get_revision();
277 private: // helper methods
278 //! helper class used to implement getter methods
279 bool getAccessUnitFwd ( parser_getter getter, AccessUnit& au)
283 if ( !getter ( &str_->gptr(), str_->sizeFwd(), &au_size ) ) {
284 if ( !str_->addDataFwd() ) return false;
288 au.setp ( str_->gptr(), au_size );
294 void checkValidity() const
296 DIRAC_MESSAGE_ASSERT ( "stream reader cannot be null", 0 != str_.get() );
300 //! stream buffer used to hold the dirac bitstream
301 std::auto_ptr<StreamReader> str_;
303 } /* parser_project */
308 #endif /* _PARSER_HPP_ */