MythTV  master
H264Parser.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 /*******************************************************************
3  * H264Parser
4  *
5  * Distributed as part of MythTV (www.mythtv.org)
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  ********************************************************************/
22 
23 #ifndef H264PARSER_H
24 #define H264PARSER_H
25 
26 // OMG this is a hack. Buried several layers down in FFmpeg includes
27 // is an include of unistd.h that using GCC will foricibly redefine
28 // NULL back to the wrong value. (Maybe just on ARM?) Include
29 // unistd.h up front so that the subsequent inclusions will be
30 // skipped, and then define NULL to the right value.
31 #include <unistd.h>
32 #undef NULL
33 #define NULL nullptr
34 
35 #include <QString>
36 #include <cstdint>
37 #include "mythconfig.h"
38 #include "compat.h" // for uint on Darwin, MinGW
39 
40 #ifndef INT_BIT
41 #define INT_BIT (CHAR_BIT * sizeof(int))
42 #endif
43 
44 // copied from libavutil/internal.h
45 extern "C" {
46 // Grr. NULL keeps getting redefined back to 0
47 #undef NULL
48 #define NULL nullptr
49 #include "libavutil/common.h" // for AV_GCC_VERSION_AT_LEAST()
50 }
51 #ifndef av_alias
52 #if HAVE_ATTRIBUTE_MAY_ALIAS && (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(3,3)
53 # define av_alias __attribute__((may_alias))
54 #else
55 # define av_alias
56 #endif
57 #endif
58 
59 extern "C" {
60 // Grr. NULL keeps getting redefined back to 0
61 #undef NULL
62 #define NULL nullptr
63 #include "libavcodec/get_bits.h"
64 }
65 
66 class FrameRate;
67 
68 class H264Parser {
69  public:
70 
71  enum {
73  };
74 
75  // ITU-T Rec. H.264 table 7-1
77  UNKNOWN = 0,
78  SLICE = 1, // 1 - 5 are VCL NAL units
79  SLICE_DPA = 2,
80  SLICE_DPB = 3,
81  SLICE_DPC = 4,
82  SLICE_IDR = 5,
83  SEI = 6,
84  SPS = 7,
85  PPS = 8,
88  END_STREAM = 11,
90  SPS_EXT = 13,
92  SPS_subset = 15,
95  };
96 
97  enum SEI_type {
102  };
103 
104  /*
105  slice_type values in the range 5..9 specify, in addition to the
106  coding type of the current slice, that all other slices of the
107  current coded picture shall have a value of slice_type equal to
108  the current value of slice_type or equal to the current value of
109  slice_type – 5.
110  */
111  enum SLICE_type {
112  SLICE_P = 0,
113  SLICE_B = 1,
114  SLICE_I = 2,
115  SLICE_SP = 3,
116  SLICE_SI = 4,
123  };
124 
125  enum frame_type {
126  FRAME = 'F',
127  FIELD_TOP = 'T',
129  };
130 
131  H264Parser(void);
132  H264Parser(const H264Parser& rhs);
133  ~H264Parser(void) {delete [] rbsp_buffer;}
134 
135  uint32_t addBytes(const uint8_t *bytes,
136  const uint32_t byte_count,
137  const uint64_t stream_offset);
138  void Reset(void);
139 
140  QString NAL_type_str(uint8_t type);
141 
142  bool stateChanged(void) const { return state_changed; }
143 
144  uint8_t lastNALtype(void) const { return nal_unit_type; }
145 
146  frame_type FieldType(void) const
147  {
148  if (bottom_field_flag == -1)
149  return FRAME;
150  else
152  }
153 
154  bool onFrameStart(void) const { return on_frame; }
155  bool onKeyFrameStart(void) const { return on_key_frame; }
156 
157  uint pictureWidth(void) const { return pic_width; }
158  uint pictureHeight(void) const { return pic_height; }
159  uint pictureWidthCropped(void) const;
160  uint pictureHeightCropped(void) const;
161 
164  uint aspectRatio(void) const;
165  double frameRate(void) const;
166  void getFrameRate(FrameRate &result) const;
167 
168  uint64_t frameAUstreamOffset(void) const {return frame_start_offset;}
169  uint64_t keyframeAUstreamOffset(void) const {return keyframe_start_offset;}
170  uint64_t SPSstreamOffset(void) const {return SPS_offset;}
171 
172  // == NAL_type AU_delimiter: primary_pic_type = 5
174  {
175  return (slice_type == SLICE_I ||
176  slice_type == SLICE_SI ||
177  slice_type == SLICE_I_a ||
179  }
180 
181  static bool NALisSlice(uint8_t nal_type)
182  {
183  return (nal_type == SLICE ||
184  nal_type == SLICE_DPA ||
185  nal_type == SLICE_IDR);
186  }
187 
188  void use_I_forKeyframes(bool val) { I_is_keyframe = val; }
189  bool using_I_forKeyframes(void) const { return I_is_keyframe; }
190 
191  uint32_t GetTimeScale(void) const { return timeScale; }
192 
193  uint32_t GetUnitsInTick(void) const { return unitsInTick; }
194 
195  void parse_SPS(uint8_t *sps, uint32_t sps_size,
196  bool& interlaced, int32_t& max_ref_frames);
197 
198  void reset_SPS(void) { seen_sps = false; }
199  bool seen_SPS(void) const { return seen_sps; }
200 
201  bool found_AU(void) const { return AU_pending; }
202 
203  private:
204  enum constants {EXTENDED_SAR = 255};
205 
206  inline void set_AU_pending(void)
207  {
208  if (!AU_pending)
209  {
210  AU_pending = true;
213  }
214  }
215 
216  bool new_AU(void);
217  void resetRBSP(void);
218  bool fillRBSP(const uint8_t *byteP, uint32_t byte_count,
219  bool found_start_code);
220  void processRBSP(bool rbsp_complete);
221  bool decode_Header(GetBitContext *gb);
222  void decode_SPS(GetBitContext *gb);
223  void decode_PPS(GetBitContext * gb);
224  void decode_SEI(GetBitContext * gb);
225  void vui_parameters(GetBitContext * gb);
226 
227  bool AU_pending {false};
228  bool state_changed {false};
229  bool seen_sps {false};
231  bool is_keyframe {false};
232  bool I_is_keyframe {true};
233 
234  uint32_t sync_accumulator {0xffffffff};
235  uint8_t *rbsp_buffer {nullptr};
236  uint32_t rbsp_buffer_size {188 * 2};
237  uint32_t rbsp_index {0};
238  uint32_t consecutive_zeros {0};
239  bool have_unfinished_NAL {false};
240 
241  int prev_frame_num {-1}, frame_num {-1};
246  uint8_t prev_nal_ref_idc {111}, nal_ref_idc {111}; // != [0|1|2|3]
252  uint prev_idr_pic_id {65536}, idr_pic_id {65536};
253 
256 
259  int8_t frame_mbs_only_flag {-1};
262  int8_t chroma_format_idc {1};
263 
266 // uint pic_width_in_mbs, pic_height_in_map_units;
272  uint8_t aspect_ratio_idc {0};
274  uint32_t unitsInTick {0}, timeScale {0};
275  bool fixedRate {false};
276 
278  uint64_t SPS_offset {0};
279  bool on_frame {false}, on_key_frame {false};
280 };
281 
282 #endif /* H264PARSER_H */
bool new_AU(void)
Definition: H264Parser.cpp:194
uint32_t unitsInTick
Definition: H264Parser.h:274
void decode_SEI(GetBitContext *gb)
uint pic_height
Definition: H264Parser.h:267
uint32_t GetUnitsInTick(void) const
Definition: H264Parser.h:193
int8_t chroma_format_idc
Definition: H264Parser.h:262
uint64_t SPS_offset
Definition: H264Parser.h:278
uint frame_crop_bottom_offset
Definition: H264Parser.h:271
bool I_is_keyframe
Definition: H264Parser.h:232
bool onKeyFrameStart(void) const
Definition: H264Parser.h:155
uint32_t addBytes(const uint8_t *bytes, const uint32_t byte_count, const uint64_t stream_offset)
Definition: H264Parser.cpp:398
void use_I_forKeyframes(bool val)
Definition: H264Parser.h:188
int8_t prev_field_pic_flag
Definition: H264Parser.h:244
uint8_t nal_ref_idc
Definition: H264Parser.h:246
uint num_ref_frames
Definition: H264Parser.h:264
uint8_t nal_unit_type
Definition: H264Parser.h:251
bool decode_Header(GetBitContext *gb)
Definition: H264Parser.cpp:612
int prev_delta_pic_order_cnt[2]
Definition: H264Parser.h:250
uint8_t pic_order_cnt_type
Definition: H264Parser.h:247
bool fixedRate
Definition: H264Parser.h:275
bool au_contains_keyframe_message
Definition: H264Parser.h:230
uint32_t sync_accumulator
Definition: H264Parser.h:234
unsigned int uint
Definition: compat.h:140
int8_t pic_order_present_flag
Definition: H264Parser.h:260
bool on_frame
Definition: H264Parser.h:279
uint redundant_pic_cnt
Definition: H264Parser.h:265
void parse_SPS(uint8_t *sps, uint32_t sps_size, bool &interlaced, int32_t &max_ref_frames)
uint log2_max_frame_num
Definition: H264Parser.h:254
uint aspectRatio(void) const
Computes aspect ratio from picture size and sample aspect ratio.
int8_t prev_bottom_field_flag
Definition: H264Parser.h:245
uint8_t separate_colour_plane_flag
Definition: H264Parser.h:258
int delta_pic_order_cnt[2]
Definition: H264Parser.h:250
int prev_pic_order_cnt_lsb
Definition: H264Parser.h:248
uint pictureWidthCropped(void) const
void Reset(void)
Definition: H264Parser.cpp:102
bool fillRBSP(const uint8_t *byteP, uint32_t byte_count, bool found_start_code)
Definition: H264Parser.cpp:321
uint sar_width
Definition: H264Parser.h:273
uint frame_crop_top_offset
Definition: H264Parser.h:270
int pic_parameter_set_id
Definition: H264Parser.h:243
uint64_t SPSstreamOffset(void) const
Definition: H264Parser.h:170
uint sar_height
Definition: H264Parser.h:273
void vui_parameters(GetBitContext *gb)
uint frame_crop_right_offset
Definition: H264Parser.h:269
int prev_delta_pic_order_cnt_bottom
Definition: H264Parser.h:249
uint32_t rbsp_index
Definition: H264Parser.h:237
~H264Parser(void)
Definition: H264Parser.h:133
void getFrameRate(FrameRate &result) const
uint8_t prev_pic_order_cnt_type
Definition: H264Parser.h:247
uint log2_max_pic_order_cnt_lsb
Definition: H264Parser.h:254
uint pic_width
Definition: H264Parser.h:267
uint64_t frameAUstreamOffset(void) const
Definition: H264Parser.h:168
uint8_t delta_pic_order_always_zero_flag
Definition: H264Parser.h:257
bool on_key_frame
Definition: H264Parser.h:279
void set_AU_pending(void)
Definition: H264Parser.h:206
uint slice_type
Definition: H264Parser.h:242
bool onFrameStart(void) const
Definition: H264Parser.h:154
bool stateChanged(void) const
Definition: H264Parser.h:142
static bool NALisSlice(uint8_t nal_type)
Definition: H264Parser.h:181
uint8_t lastNALtype(void) const
Definition: H264Parser.h:144
uint64_t keyframe_start_offset
Definition: H264Parser.h:277
uint8_t aspect_ratio_idc
Definition: H264Parser.h:272
uint idr_pic_id
Definition: H264Parser.h:252
int8_t redundant_pic_cnt_present_flag
Definition: H264Parser.h:261
uint32_t consecutive_zeros
Definition: H264Parser.h:238
int8_t bottom_field_flag
Definition: H264Parser.h:245
void decode_PPS(GetBitContext *gb)
void processRBSP(bool rbsp_complete)
Definition: H264Parser.cpp:531
uint64_t frame_start_offset
Definition: H264Parser.h:277
int pic_order_cnt_lsb
Definition: H264Parser.h:248
uint prev_idr_pic_id
Definition: H264Parser.h:252
uint64_t AU_offset
Definition: H264Parser.h:277
QString NAL_type_str(uint8_t type)
Definition: H264Parser.cpp:158
uint seq_parameter_set_id
Definition: H264Parser.h:255
bool state_changed
Definition: H264Parser.h:228
uint64_t pkt_offset
Definition: H264Parser.h:277
double frameRate(void) const
frame_type FieldType(void) const
Definition: H264Parser.h:146
void resetRBSP(void)
Definition: H264Parser.cpp:314
int8_t frame_mbs_only_flag
Definition: H264Parser.h:259
uint32_t rbsp_buffer_size
Definition: H264Parser.h:236
int prev_frame_num
Definition: H264Parser.h:241
int frame_num
Definition: H264Parser.h:241
static bool isKeySlice(uint slice_type)
Definition: H264Parser.h:173
void reset_SPS(void)
Definition: H264Parser.h:198
void decode_SPS(GetBitContext *gb)
Definition: H264Parser.cpp:808
uint32_t timeScale
Definition: H264Parser.h:274
uint frame_crop_left_offset
Definition: H264Parser.h:268
uint8_t prev_nal_ref_idc
Definition: H264Parser.h:246
uint pictureHeight(void) const
Definition: H264Parser.h:158
uint pictureWidth(void) const
Definition: H264Parser.h:157
bool AU_pending
Definition: H264Parser.h:227
uint8_t prev_nal_unit_type
Definition: H264Parser.h:251
int prev_pic_parameter_set_id
Definition: H264Parser.h:243
int8_t field_pic_flag
Definition: H264Parser.h:244
bool have_unfinished_NAL
Definition: H264Parser.h:239
uint32_t GetTimeScale(void) const
Definition: H264Parser.h:191
uint64_t keyframeAUstreamOffset(void) const
Definition: H264Parser.h:169
bool is_keyframe
Definition: H264Parser.h:231
bool found_AU(void) const
Definition: H264Parser.h:201
uint8_t * rbsp_buffer
Definition: H264Parser.h:235
bool using_I_forKeyframes(void) const
Definition: H264Parser.h:189
H264Parser(void)
Definition: H264Parser.cpp:93
bool seen_SPS(void) const
Definition: H264Parser.h:199
uint pictureHeightCropped(void) const
bool seen_sps
Definition: H264Parser.h:229
int delta_pic_order_cnt_bottom
Definition: H264Parser.h:249