1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 // 5 // This file contains an implementation of an H264 Annex-B video stream parser. 6 7 #ifndef CONTENT_COMMON_GPU_MEDIA_H264_PARSER_H_ 8 #define CONTENT_COMMON_GPU_MEDIA_H264_PARSER_H_ 9 10 #include <sys/types.h> 11 12 #include <map> 13 14 #include "base/basictypes.h" 15 #include "content/common/content_export.h" 16 #include "content/common/gpu/media/h264_bit_reader.h" 17 18 namespace content { 19 20 // For explanations of each struct and its members, see H.264 specification 21 // at http://www.itu.int/rec/T-REC-H.264. 22 struct CONTENT_EXPORT H264NALU { 23 H264NALU(); 24 25 enum Type { 26 kUnspecified = 0, 27 kNonIDRSlice = 1, 28 kIDRSlice = 5, 29 kSEIMessage = 6, 30 kSPS = 7, 31 kPPS = 8, 32 kEOSeq = 9, 33 kEOStream = 11, 34 kCodedSliceExtension = 20, 35 }; 36 37 // After (without) start code; we don't own the underlying memory 38 // and a shallow copy should be made when copying this struct. 39 const uint8* data; 40 off_t size; // From after start code to start code of next NALU (or EOS). 41 42 int nal_ref_idc; 43 int nal_unit_type; 44 }; 45 46 enum { kH264ScalingList4x4Length = 16, kH264ScalingList8x8Length = 64, }; 47 48 struct CONTENT_EXPORT H264SPS { 49 H264SPS(); 50 51 int profile_idc; 52 int constraint_setx_flag; 53 int level_idc; 54 int seq_parameter_set_id; 55 56 int chroma_format_idc; 57 bool separate_colour_plane_flag; 58 int bit_depth_luma_minus8; 59 int bit_depth_chroma_minus8; 60 bool qpprime_y_zero_transform_bypass_flag; 61 62 bool seq_scaling_matrix_present_flag; 63 int scaling_list4x4[6][kH264ScalingList4x4Length]; 64 int scaling_list8x8[6][kH264ScalingList8x8Length]; 65 66 int log2_max_frame_num_minus4; 67 int pic_order_cnt_type; 68 int log2_max_pic_order_cnt_lsb_minus4; 69 bool delta_pic_order_always_zero_flag; 70 int offset_for_non_ref_pic; 71 int offset_for_top_to_bottom_field; 72 int num_ref_frames_in_pic_order_cnt_cycle; 73 int expected_delta_per_pic_order_cnt_cycle; // calculated 74 int offset_for_ref_frame[255]; 75 int max_num_ref_frames; 76 bool gaps_in_frame_num_value_allowed_flag; 77 int pic_width_in_mbs_minus1; 78 int pic_height_in_map_units_minus1; 79 bool frame_mbs_only_flag; 80 bool mb_adaptive_frame_field_flag; 81 bool direct_8x8_inference_flag; 82 bool frame_cropping_flag; 83 int frame_crop_left_offset; 84 int frame_crop_right_offset; 85 int frame_crop_top_offset; 86 int frame_crop_bottom_offset; 87 bool vui_parameters_present_flag; 88 int chroma_array_type; 89 }; 90 91 struct CONTENT_EXPORT H264PPS { 92 H264PPS(); 93 94 int pic_parameter_set_id; 95 int seq_parameter_set_id; 96 bool entropy_coding_mode_flag; 97 bool bottom_field_pic_order_in_frame_present_flag; 98 int num_slice_groups_minus1; 99 // TODO(posciak): Slice groups not implemented, could be added at some point. 100 int num_ref_idx_l0_default_active_minus1; 101 int num_ref_idx_l1_default_active_minus1; 102 bool weighted_pred_flag; 103 int weighted_bipred_idc; 104 int pic_init_qp_minus26; 105 int pic_init_qs_minus26; 106 int chroma_qp_index_offset; 107 bool deblocking_filter_control_present_flag; 108 bool constrained_intra_pred_flag; 109 bool redundant_pic_cnt_present_flag; 110 bool transform_8x8_mode_flag; 111 112 bool pic_scaling_matrix_present_flag; 113 int scaling_list4x4[6][kH264ScalingList4x4Length]; 114 int scaling_list8x8[6][kH264ScalingList8x8Length]; 115 116 int second_chroma_qp_index_offset; 117 }; 118 119 struct CONTENT_EXPORT H264ModificationOfPicNum { 120 int modification_of_pic_nums_idc; 121 union { 122 int abs_diff_pic_num_minus1; 123 int long_term_pic_num; 124 }; 125 }; 126 127 struct CONTENT_EXPORT H264WeightingFactors { 128 bool luma_weight_flag; 129 bool chroma_weight_flag; 130 int luma_weight[32]; 131 int luma_offset[32]; 132 int chroma_weight[32][2]; 133 int chroma_offset[32][2]; 134 }; 135 136 struct CONTENT_EXPORT H264DecRefPicMarking { 137 int memory_mgmnt_control_operation; 138 int difference_of_pic_nums_minus1; 139 int long_term_pic_num; 140 int long_term_frame_idx; 141 int max_long_term_frame_idx_plus1; 142 }; 143 144 struct CONTENT_EXPORT H264SliceHeader { 145 H264SliceHeader(); 146 147 enum { 148 kRefListSize = 32, 149 kRefListModSize = kRefListSize 150 }; 151 152 enum Type { 153 kPSlice = 0, 154 kBSlice = 1, 155 kISlice = 2, 156 kSPSlice = 3, 157 kSISlice = 4, 158 }; 159 160 bool IsPSlice() const; 161 bool IsBSlice() const; 162 bool IsISlice() const; 163 bool IsSPSlice() const; 164 bool IsSISlice() const; 165 166 bool idr_pic_flag; // from NAL header 167 int nal_ref_idc; // from NAL header 168 const uint8* nalu_data; // from NAL header 169 off_t nalu_size; // from NAL header 170 off_t header_bit_size; // calculated 171 172 int first_mb_in_slice; 173 int slice_type; 174 int pic_parameter_set_id; 175 int colour_plane_id; // TODO(posciak): use this! http://crbug.com/139878 176 int frame_num; 177 bool field_pic_flag; 178 bool bottom_field_flag; 179 int idr_pic_id; 180 int pic_order_cnt_lsb; 181 int delta_pic_order_cnt_bottom; 182 int delta_pic_order_cnt[2]; 183 int redundant_pic_cnt; 184 bool direct_spatial_mv_pred_flag; 185 186 bool num_ref_idx_active_override_flag; 187 int num_ref_idx_l0_active_minus1; 188 int num_ref_idx_l1_active_minus1; 189 bool ref_pic_list_modification_flag_l0; 190 bool ref_pic_list_modification_flag_l1; 191 H264ModificationOfPicNum ref_list_l0_modifications[kRefListModSize]; 192 H264ModificationOfPicNum ref_list_l1_modifications[kRefListModSize]; 193 194 int luma_log2_weight_denom; 195 int chroma_log2_weight_denom; 196 197 bool luma_weight_l0_flag; 198 bool chroma_weight_l0_flag; 199 H264WeightingFactors pred_weight_table_l0; 200 201 bool luma_weight_l1_flag; 202 bool chroma_weight_l1_flag; 203 H264WeightingFactors pred_weight_table_l1; 204 205 bool no_output_of_prior_pics_flag; 206 bool long_term_reference_flag; 207 208 bool adaptive_ref_pic_marking_mode_flag; 209 H264DecRefPicMarking ref_pic_marking[kRefListSize]; 210 211 int cabac_init_idc; 212 int slice_qp_delta; 213 bool sp_for_switch_flag; 214 int slice_qs_delta; 215 int disable_deblocking_filter_idc; 216 int slice_alpha_c0_offset_div2; 217 int slice_beta_offset_div2; 218 }; 219 220 struct H264SEIRecoveryPoint { 221 int recovery_frame_cnt; 222 bool exact_match_flag; 223 bool broken_link_flag; 224 int changing_slice_group_idc; 225 }; 226 227 struct CONTENT_EXPORT H264SEIMessage { 228 H264SEIMessage(); 229 230 enum Type { 231 kSEIRecoveryPoint = 6, 232 }; 233 234 int type; 235 int payload_size; 236 union { 237 // Placeholder; in future more supported types will contribute to more 238 // union members here. 239 H264SEIRecoveryPoint recovery_point; 240 }; 241 }; 242 243 // Class to parse an Annex-B H.264 stream, 244 // as specified in chapters 7 and Annex B of the H.264 spec. 245 class CONTENT_EXPORT H264Parser { 246 public: 247 enum Result { 248 kOk, 249 kInvalidStream, // error in stream 250 kUnsupportedStream, // stream not supported by the parser 251 kEOStream, // end of stream 252 }; 253 254 H264Parser(); 255 ~H264Parser(); 256 257 void Reset(); 258 // Set current stream pointer to |stream| of |stream_size| in bytes, 259 // |stream| owned by caller. 260 void SetStream(const uint8* stream, off_t stream_size); 261 262 // Read the stream to find the next NALU, identify it and return 263 // that information in |*nalu|. This advances the stream to the beginning 264 // of this NALU, but not past it, so subsequent calls to NALU-specific 265 // parsing functions (ParseSPS, etc.) will parse this NALU. 266 // If the caller wishes to skip the current NALU, it can call this function 267 // again, instead of any NALU-type specific parse functions below. 268 Result AdvanceToNextNALU(H264NALU* nalu); 269 270 // NALU-specific parsing functions. 271 // These should be called after AdvanceToNextNALU(). 272 273 // SPSes and PPSes are owned by the parser class and the memory for their 274 // structures is managed here, not by the caller, as they are reused 275 // across NALUs. 276 // 277 // Parse an SPS/PPS NALU and save their data in the parser, returning id 278 // of the parsed structure in |*pps_id|/|*sps_id|. 279 // To get a pointer to a given SPS/PPS structure, use GetSPS()/GetPPS(), 280 // passing the returned |*sps_id|/|*pps_id| as parameter. 281 // TODO(posciak,fischman): consider replacing returning Result from Parse*() 282 // methods with a scoped_ptr and adding an AtEOS() function to check for EOS 283 // if Parse*() return NULL. 284 Result ParseSPS(int* sps_id); 285 Result ParsePPS(int* pps_id); 286 287 // Return a pointer to SPS/PPS with given |sps_id|/|pps_id| or NULL if not 288 // present. 289 const H264SPS* GetSPS(int sps_id); 290 const H264PPS* GetPPS(int pps_id); 291 292 // Slice headers and SEI messages are not used across NALUs by the parser 293 // and can be discarded after current NALU, so the parser does not store 294 // them, nor does it manage their memory. 295 // The caller has to provide and manage it instead. 296 297 // Parse a slice header, returning it in |*shdr|. |*nalu| must be set to 298 // the NALU returned from AdvanceToNextNALU() and corresponding to |*shdr|. 299 Result ParseSliceHeader(const H264NALU& nalu, H264SliceHeader* shdr); 300 301 // Parse a SEI message, returning it in |*sei_msg|, provided and managed 302 // by the caller. 303 Result ParseSEI(H264SEIMessage* sei_msg); 304 305 private: 306 // Exp-Golomb code parsing as specified in chapter 9.1 of the spec. 307 // Read one unsigned exp-Golomb code from the stream and return in |*val|. 308 Result ReadUE(int* val); 309 310 // Read one signed exp-Golomb code from the stream and return in |*val|. 311 Result ReadSE(int* val); 312 313 // Parse scaling lists (see spec). 314 Result ParseScalingList(int size, int* scaling_list, bool* use_default); 315 Result ParseSPSScalingLists(H264SPS* sps); 316 Result ParsePPSScalingLists(const H264SPS& sps, H264PPS* pps); 317 318 // Parse reference picture lists' modifications (see spec). 319 Result ParseRefPicListModifications(H264SliceHeader* shdr); 320 Result ParseRefPicListModification(int num_ref_idx_active_minus1, 321 H264ModificationOfPicNum* ref_list_mods); 322 323 // Parse prediction weight table (see spec). 324 Result ParsePredWeightTable(const H264SPS& sps, H264SliceHeader* shdr); 325 326 // Parse weighting factors (see spec). 327 Result ParseWeightingFactors(int num_ref_idx_active_minus1, 328 int chroma_array_type, 329 int luma_log2_weight_denom, 330 int chroma_log2_weight_denom, 331 H264WeightingFactors* w_facts); 332 333 // Parse decoded reference picture marking information (see spec). 334 Result ParseDecRefPicMarking(H264SliceHeader *shdr); 335 336 // Pointer to the current NALU in the stream. 337 const uint8* stream_; 338 339 // Bytes left in the stream after the current NALU. 340 off_t bytes_left_; 341 342 H264BitReader br_; 343 344 // PPSes and SPSes stored for future reference. 345 typedef std::map<int, H264SPS*> SPSById; 346 typedef std::map<int, H264PPS*> PPSById; 347 SPSById active_SPSes_; 348 PPSById active_PPSes_; 349 350 DISALLOW_COPY_AND_ASSIGN(H264Parser); 351 }; 352 353 } // namespace content 354 355 #endif // CONTENT_COMMON_GPU_MEDIA_H264_PARSER_H_ 356