VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gravc.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gravc.h
19 ///
20 
21 #ifndef GR_AVC__H
22 #define GR_AVC__H
23 
24 #include "gdef.h"
25 #include "gbititerator.h"
26 #include "gbitstream.h"
27 #include <string>
28 #include <vector>
29 #include <iostream>
30 
31 namespace Gr
32 {
33  /// \namespace Gr::Avc
34  /// Classes for decoding H.264 parameter sets.
35  ///
36  /// H.264 is the standard for Advanced Video Coding (AVC).
37  ///
38  /// The AVC design separates out its configuration parameters from the
39  /// stream of video images (frames), with the configuration being
40  /// communicated completely out-of-band (eg. SDP) or in distinctly-typed
41  /// network packets (SPS and PPS NALUs) or in special file segments
42  /// (eg. "avcC" boxes).
43  ///
44  /// Once the parameter sets have been established the video frames can
45  /// simply refer to them by their id. In the simplest case there is one
46  /// sequence parameter set (SPS) and one picture parameter set (PPS)
47  /// used by all video frames.
48  ///
49  /// The job of decoding parameter sets can sometimes be delegated to a
50  /// H.264 decoder by presenting them as extra data at session setup, or
51  /// by adding them to the data stream as SPS and PPS NALUs.
52  ///
53  /// The H.264 standard is defined in terms of a bitstream syntax. A
54  /// bytestream format, RBSP, is also defined with a final stop bit
55  /// set to 1 followed by a set of padding bits set to 0. A RBSP bytestream
56  /// can be byte-stuffed in order that easily-identifiable markers can be
57  /// put inbetween the set of RBSP buffers that make up a higher-level
58  /// bytestream. The RBSP byte-stuffing replaces all 00-00-00, 00-00-01,
59  /// 00-00-02 or 00-00-03 sequences with 00-00-03-00, 00-00-03-01,
60  /// 00-00-03-02 or 00-00-03-03 respectively, thereby freeing up 00-00-00,
61  /// 00-00-01 and 00-00-02 to be used as inter-RBSP markers.
62  ///
63  /// H.264 NALUs are made up of a single byte-stuffed RBSP buffer with a
64  /// one-byte header to identify the NALU type. When NALUs are chained
65  /// together they are separated by 00-00-01 markers, with an additional
66  /// four-byte 00-00-00-01 start-code marker at the front. Other NALU packing
67  /// schemes use NALU-length fields between the separate NALUs (mp4-avcC,
68  /// rtp-avc), or use base64 encoding of each NALU with comma separators
69  /// (fmtp), but even in these cases the NALUs themselves are byte-stuffed.
70  ///
71  /// \see ISO/IEC 14496-15 (non-free) 5.2.4.1.1 5.2.3 5.3.4.1.2,
72  /// and libav's ff_isom_write_avcc().
73  ///
74  namespace Avc
75  {
76  class Configuration ;
77  class Sps ;
78  class Pps ;
79  class Rbsp ;
80  std::ostream & operator<<( std::ostream & , const Avc::Sps & ) ;
81  std::ostream & operator<<( std::ostream & , const Avc::Pps & ) ;
82  typedef G::bit_iterator<const char*> bit_iterator_t ;
83  typedef G::bit_stream<bit_iterator_t> bit_stream_t ;
84  }
85 }
86 
87 /// \class Gr::Avc::Configuration
88 /// Contains AVC configuration parameters, initialised from an "avcC" file
89 /// segment or from an SDP "fmtp" attribute string, held as lists of
90 /// SPS and PPS sub-structures. In practice there is often only one SPS
91 /// and one PPS.
92 ///
94 {
95 public:
96  static Configuration fromFmtp( const std::string & fmtp ) ;
97  ///< Factory function taking a SDP (Session Description Protocol) "fmtp" attribute string,
98  ///< something like "profile-level-id=...; ...; sprop-parameters-sets=Z00AKZpmA8==,aO48gA==".
99 
100  static Configuration fromAvcc( const std::string & binary_string ) ;
101  ///< Factory function taking an "avcC" binary string, including RBSP
102  ///< byte-stuffing (but no start code) -- see ISO/IEC 14496-10 7.4.1.1.
103 
104  Configuration() ;
105  ///< A default constructor for an in-valid() object.
106 
107  bool valid() const ;
108  ///< Returns true if a usable object.
109 
110  std::string reason() const ;
111  ///< Returns the in-valid() reason.
112 
113  unsigned int nalu_length_size() const ;
114  ///< Returns the size of the nalu length values, typically 2.
115 
116  const std::vector<std::string> & spsList() const ;
117  ///< Returns a list of binary strings for the sps structures.
118  ///< The strings are byte-stuffed and start with the nalu type
119  ///< in the low bits of the first character position.
120 
121  const std::vector<std::string> & ppsList() const ;
122  ///< Returns a list of binary strings for the pps structures.
123  ///< The strings are byte-stuffed and start with the nalu type
124  ///< in the low bits of the first character position.
125 
126  size_t spsCount() const ;
127  ///< Returns sps().size(). This will be smaller than spsList.size()
128  ///< if there are parsing errors.
129 
130  size_t ppsCount() const ;
131  ///< Returns pps().size(). This will be smaller than spsList.size()
132  ///< if there are parsing errors.
133 
134  const Sps & sps( size_t i ) const ;
135  ///< Returns a reference to the i-th sps structure.
136 
137  const Pps & pps( size_t i ) const ;
138  ///< Returns a reference to the i-th pps structure.
139 
140  std::string nalus() const ;
141  ///< Returns the NALU byte-stream comprising the four-byte 00-00-00-01
142  ///< start-code followed by the byte-stuffed NALUs separated by the
143  ///< three-byte 00-00-01 marker.
144 
145 private:
146  Configuration( bool , const std::string & ) ;
147  void initFmtp( const std::string & ) ;
148  void initAvcc( const std::string & ) ;
149  void checkFmtp() ;
150  void checkStream( bit_stream_t & ) ;
151  void spsListParse() ;
152  void ppsListParse() ;
153  void checkIds() ;
154  void commit() ;
155  void report( const std::string & ) ;
156 
157 private:
158  struct AvccInfo
159  {
160  unsigned int configuration_version ;
161  unsigned int profile ; // also in sps
162  unsigned int profile_compat ;
163  unsigned int level ; // also in sps
164  unsigned int length_size_minus_one ; // also in sps
165  } ;
166  struct FmtpInfo
167  {
168  unsigned int profile ; // also in sps
169  unsigned int level ; // also in sps
170  unsigned int packetisation_mode ;
171  } ;
172 
173 private:
174  AvccInfo m_avcc_info ;
175  FmtpInfo m_fmtp_info ;
176  std::string m_reason ;
177  std::vector<std::string> m_sps_list ;
178  std::vector<std::string> m_pps_list ;
179  std::vector<Sps> m_sps ;
180  std::vector<Pps> m_pps ;
181 } ;
182 
183 /// \class Gr::Avc::Sps
184 /// A Sequence Parameter Set (SPS) structure.
185 /// \see ISO/IEC 14496-10, esp. 7.3.2.1.1
186 ///
188 {
189 public:
190  explicit Sps( const std::string & binary_sps_buffer ) ;
191  ///< Constructor taking a binary byte-stuffed sps buffer.
192 
193  bool valid() const ;
194  ///< Returns true if a usable object.
195 
196  std::string reason() const ;
197  ///< Returns the in-valid() reason.
198 
199  void streamOut( std::ostream & ) const ;
200  ///< Streams out the sps info.
201 
202  unsigned int dx() const ;
203  ///< Returns the picture width in pixels.
204 
205  unsigned int dy() const ;
206  ///< Returns the picture height in pixels.
207 
208 public:
209  unsigned int m_nalu_type ; // 7
210  unsigned int m_profile_idc ; // eg. 66
211  bool m_constraint_set0_flag ; // see 7.4.2.1.1 and A.2.1
212  bool m_constraint_set1_flag ; // see A.2.2
213  bool m_constraint_set2_flag ;
214  bool m_constraint_set3_flag ;
215  bool m_constraint_set4_flag ;
216  bool m_constraint_set5_flag ;
217  unsigned int m_zero ; // 2 bits
218  unsigned int m_level_idc ;
219  unsigned int m_sequence_parameter_set_id ;
220  unsigned int m_chroma_format_idc ;
221  bool m_separate_colour_plane_flag ;
222  unsigned int m_bit_depth_luma_minus8 ;
223  unsigned int m_bit_depth_chroma_minus8 ;
224  bool m_qpprime_y_zero_transform_bypass_flag ;
225  bool m_seq_scaling_matrix_present_flag ;
226  unsigned int m_log2_max_frame_num_minus4 ;
227  unsigned int m_pic_order_cnt_type ;
228  unsigned int m_log2_max_pic_order_cnt_lsb_minus4 ;
229  bool m_delta_pic_order_always_zero_flag ;
230  int m_offset_for_non_ref_pic ;
231  int m_offset_for_top_to_bottom_field ;
232  unsigned int m_num_ref_frames_in_pic_order_cnt_cycle ;
233  unsigned int m_max_num_ref_frames ;
234  bool m_gaps_in_frame_num_value_allowed_flag ;
235  unsigned int m_pic_width_in_mbs_minus1 ;
236  unsigned int m_pic_height_in_map_units_minus1 ;
237  bool m_frame_mbs_only_flag ;
238  bool m_mb_adaptive_frame_field_flag ;
239  bool m_direct_8x8_inference_flag ;
240  bool m_frame_cropping_flag ;
241  unsigned int m_frame_crop_left_offset ;
242  unsigned int m_frame_crop_right_offset ;
243  unsigned int m_frame_crop_top_offset ;
244  unsigned int m_frame_crop_bottom_offset ;
245  bool m_vui_parameters_present_flag ;
246  bool m_nal_hrd_parameters_present_flag ;
247  bool m_vcl_hrd_parameters_present_flag ;
248 
249 private:
250  void skip_hrd_parameters( bit_stream_t & ) ;
251  void report( const std::string & , unsigned int = 0U , const std::string & = std::string() ) ;
252  void report_extra( const std::string & ) ;
253 
254 private:
255  std::string m_reason ;
256 } ;
257 
258 /// \class Gr::Avc::Pps
259 /// A Picture Sequence Parameter Set (PPS) structure.
260 /// \see ISO/IEC 14496-10, esp. 7.3.2.2
261 ///
263 {
264 public:
265  Pps( const std::string & binary_pps_buffer , int sps_chroma_format_idc ) ;
266  ///< Constructor taking a binary byte-stuffed pps buffer.
267 
268  bool valid() const ;
269  ///< Returns true if a usable object.
270 
271  std::string reason() const ;
272  ///< Returns the in-valid() reason.
273 
274  void streamOut( std::ostream & ) const ;
275  ///< Streams out the pps info.
276 
277 public:
278  unsigned int m_nalu_type ; // 8
279  unsigned int m_pic_parameter_set_id ;
280  unsigned int m_seq_parameter_set_id ;
281  bool m_entropy_coding_mode_flag ;
282  bool m_bottom_field_pic_order_in_frame_present_flag ;
283  unsigned int m_num_slice_groups_minus1 ;
284  unsigned int m_num_ref_idx_10_default_active_minus1 ;
285  unsigned int m_num_ref_idx_11_default_active_minus1 ;
286  bool m_weighted_pred_flag ;
287  unsigned int m_weighted_bipred_idc ; // 2 bits
288  int m_pic_init_qp_minus26 ;
289  int m_pic_init_qs_minus26 ;
290  int m_chroma_qp_index_offset ;
291  bool m_deblocking_filter_control_present_flag ;
292  bool m_constrained_intra_pred_flag ;
293  bool m_redundant_pic_cnt_present_flag ;
294  bool m_transform_8x8_mode_flag ;
295  bool m_pic_scaling_matrix_present_flag ;
296  int m_second_chroma_qp_index_offset ;
297 
298 private:
299  std::string m_reason ;
300 
301 private:
302  void report( const std::string & ) ;
303 } ;
304 
305 /// \class Gr::Avc::Rbsp
306 /// Provides static helper functions that relate to RBSP formatting.
307 ///
309 {
310 public:
311  static void addByteStuffing( std::string & s ) ;
312  ///< Adds byte-stuffing to the given unstuffed RBSP buffer.
313 
314  static std::string byteStuffed( const std::string & s ) ;
315  ///< Returns a byte-stuffed version of the given unstuffed RBSP buffer.
316 
317  static std::string removeByteStuffing( const std::string & s ) ;
318  ///< Removes byte stuffing from a byte-stuffed RBSP buffer.
319 
320  static bool hasMarkers( const std::string & s ) ;
321  ///< Returns true if the string contains one of the inter-RBSP
322  ///< markers (000, 001, 002).
323 
324  static size_t findStopBit( const std::string & s , size_t fail = 0U ) ;
325  ///< Returns the bit offset of the RBSP stop bit.
326  ///< Returns the fail value on error.
327 
328  static const std::string & _000() ;
329  ///< Returns the 00-00-00 string.
330 
331  static const std::string & _001() ;
332  ///< Returns the 00-00-01 string.
333 
334  static const std::string & _002() ;
335  ///< Returns the 00-00-02 string.
336 
337  static const std::string & _0001() ;
338  ///< Returns the 00-00-00-01 string.
339 
340 private:
341  Rbsp() ;
342 } ;
343 
344 #endif
const std::vector< std::string > & spsList() const
Returns a list of binary strings for the sps structures.
Definition: gravc.cpp:261
Sps(const std::string &binary_sps_buffer)
Constructor taking a binary byte-stuffed sps buffer.
Definition: gravc.cpp:311
static Configuration fromAvcc(const std::string &binary_string)
Factory function taking an "avcC" binary string, including RBSP byte-stuffing (but no start code) – s...
Definition: gravc.cpp:55
void streamOut(std::ostream &) const
Streams out the sps info.
Definition: gravc.cpp:509
size_t spsCount() const
Returns sps().size().
Definition: gravc.cpp:271
static const std::string & _002()
Returns the 00-00-02 string.
Definition: gravc.cpp:691
Contains AVC configuration parameters, initialised from an "avcC" file segment or from an SDP "fmtp" ...
Definition: gravc.h:93
size_t ppsCount() const
Returns pps().size().
Definition: gravc.cpp:276
Pps(const std::string &binary_pps_buffer, int sps_chroma_format_idc)
Constructor taking a binary byte-stuffed pps buffer.
Definition: gravc.cpp:548
void streamOut(std::ostream &) const
Streams out the pps info.
Definition: gravc.cpp:628
bool valid() const
Returns true if a usable object.
Definition: gravc.cpp:618
unsigned int dx() const
Returns the picture width in pixels.
Definition: gravc.cpp:462
A Picture Sequence Parameter Set (PPS) structure.
Definition: gravc.h:262
A Sequence Parameter Set (SPS) structure.
Definition: gravc.h:187
static const std::string & _001()
Returns the 00-00-01 string.
Definition: gravc.cpp:685
A class for pulling integer values of various widths out of a bit stream.
Definition: gbitstream.h:55
static Configuration fromFmtp(const std::string &fmtp)
Factory function taking a SDP (Session Description Protocol) "fmtp" attribute string, something like "profile-level-id=...; ...; sprop-parameters-sets=Z00AKZpmA8==,aO48gA==".
Definition: gravc.cpp:50
std::string reason() const
Returns the in-valid() reason.
Definition: gravc.cpp:251
A bit-by-bit input iterator that extracts bits in msb-to-lsb order from a sequence of bytes...
Definition: gbititerator.h:42
static std::string removeByteStuffing(const std::string &s)
Removes byte stuffing from a byte-stuffed RBSP buffer.
Definition: gravc.cpp:703
unsigned int dy() const
Returns the picture height in pixels.
Definition: gravc.cpp:473
bool valid() const
Returns true if a usable object.
Definition: gravc.cpp:246
bool valid() const
Returns true if a usable object.
Definition: gravc.cpp:494
static std::string byteStuffed(const std::string &s)
Returns a byte-stuffed version of the given unstuffed RBSP buffer.
Definition: gravc.cpp:728
const Pps & pps(size_t i) const
Returns a reference to the i-th pps structure.
Definition: gravc.cpp:286
static bool hasMarkers(const std::string &s)
Returns true if the string contains one of the inter-RBSP markers (000, 001, 002).
Definition: gravc.cpp:735
std::string reason() const
Returns the in-valid() reason.
Definition: gravc.cpp:623
static void addByteStuffing(std::string &s)
Adds byte-stuffing to the given unstuffed RBSP buffer.
Definition: gravc.cpp:710
const Sps & sps(size_t i) const
Returns a reference to the i-th sps structure.
Definition: gravc.cpp:281
const std::vector< std::string > & ppsList() const
Returns a list of binary strings for the pps structures.
Definition: gravc.cpp:266
Provides static helper functions that relate to RBSP formatting.
Definition: gravc.h:308
static size_t findStopBit(const std::string &s, size_t fail=0U)
Returns the bit offset of the RBSP stop bit.
Definition: gravc.cpp:667
std::string reason() const
Returns the in-valid() reason.
Definition: gravc.cpp:499
std::string nalus() const
Returns the NALU byte-stream comprising the four-byte 00-00-00-01 start-code followed by the byte-stu...
Definition: gravc.cpp:291
static const std::string & _000()
Returns the 00-00-00 string.
Definition: gravc.cpp:679
unsigned int nalu_length_size() const
Returns the size of the nalu length values, typically 2.
Definition: gravc.cpp:256
static const std::string & _0001()
Returns the 00-00-00-01 string.
Definition: gravc.cpp:697
Configuration()
A default constructor for an in-valid() object.
Definition: gravc.cpp:65