1 // Copyright 2014 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.
5 // This file contains an implementation of an H264 Annex-B video stream parser.
7 #ifndef MEDIA_FILTERS_H264_PARSER_H_
8 #define MEDIA_FILTERS_H264_PARSER_H_
10 #include <sys/types.h>
14 #include "base/basictypes.h"
15 #include "media/base/media_export.h"
16 #include "media/filters/h264_bit_reader.h"
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 MEDIA_EXPORT H264NALU {
35 kCodedSliceExtension = 20,
38 // After (without) start code; we don't own the underlying memory
39 // and a shallow copy should be made when copying this struct.
41 off_t size; // From after start code to start code of next NALU (or EOS).
48 kH264ScalingList4x4Length = 16,
49 kH264ScalingList8x8Length = 64,
52 struct MEDIA_EXPORT H264SPS {
56 int constraint_setx_flag;
58 int seq_parameter_set_id;
60 int chroma_format_idc;
61 bool separate_colour_plane_flag;
62 int bit_depth_luma_minus8;
63 int bit_depth_chroma_minus8;
64 bool qpprime_y_zero_transform_bypass_flag;
66 bool seq_scaling_matrix_present_flag;
67 int scaling_list4x4[6][kH264ScalingList4x4Length];
68 int scaling_list8x8[6][kH264ScalingList8x8Length];
70 int log2_max_frame_num_minus4;
71 int pic_order_cnt_type;
72 int log2_max_pic_order_cnt_lsb_minus4;
73 bool delta_pic_order_always_zero_flag;
74 int offset_for_non_ref_pic;
75 int offset_for_top_to_bottom_field;
76 int num_ref_frames_in_pic_order_cnt_cycle;
77 int expected_delta_per_pic_order_cnt_cycle; // calculated
78 int offset_for_ref_frame[255];
79 int max_num_ref_frames;
80 bool gaps_in_frame_num_value_allowed_flag;
81 int pic_width_in_mbs_minus1;
82 int pic_height_in_map_units_minus1;
83 bool frame_mbs_only_flag;
84 bool mb_adaptive_frame_field_flag;
85 bool direct_8x8_inference_flag;
86 bool frame_cropping_flag;
87 int frame_crop_left_offset;
88 int frame_crop_right_offset;
89 int frame_crop_top_offset;
90 int frame_crop_bottom_offset;
91 bool vui_parameters_present_flag;
92 int chroma_array_type;
93 int sar_width; // Set to 0 when not specified.
94 int sar_height; // Set to 0 when not specified.
97 struct MEDIA_EXPORT H264PPS {
100 int pic_parameter_set_id;
101 int seq_parameter_set_id;
102 bool entropy_coding_mode_flag;
103 bool bottom_field_pic_order_in_frame_present_flag;
104 int num_slice_groups_minus1;
105 // TODO(posciak): Slice groups not implemented, could be added at some point.
106 int num_ref_idx_l0_default_active_minus1;
107 int num_ref_idx_l1_default_active_minus1;
108 bool weighted_pred_flag;
109 int weighted_bipred_idc;
110 int pic_init_qp_minus26;
111 int pic_init_qs_minus26;
112 int chroma_qp_index_offset;
113 bool deblocking_filter_control_present_flag;
114 bool constrained_intra_pred_flag;
115 bool redundant_pic_cnt_present_flag;
116 bool transform_8x8_mode_flag;
118 bool pic_scaling_matrix_present_flag;
119 int scaling_list4x4[6][kH264ScalingList4x4Length];
120 int scaling_list8x8[6][kH264ScalingList8x8Length];
122 int second_chroma_qp_index_offset;
125 struct MEDIA_EXPORT H264ModificationOfPicNum {
126 int modification_of_pic_nums_idc;
128 int abs_diff_pic_num_minus1;
129 int long_term_pic_num;
133 struct MEDIA_EXPORT H264WeightingFactors {
134 bool luma_weight_flag;
135 bool chroma_weight_flag;
138 int chroma_weight[32][2];
139 int chroma_offset[32][2];
142 struct MEDIA_EXPORT H264DecRefPicMarking {
143 int memory_mgmnt_control_operation;
144 int difference_of_pic_nums_minus1;
145 int long_term_pic_num;
146 int long_term_frame_idx;
147 int max_long_term_frame_idx_plus1;
150 struct MEDIA_EXPORT H264SliceHeader {
155 kRefListModSize = kRefListSize
166 bool IsPSlice() const;
167 bool IsBSlice() const;
168 bool IsISlice() const;
169 bool IsSPSlice() const;
170 bool IsSISlice() const;
172 bool idr_pic_flag; // from NAL header
173 int nal_ref_idc; // from NAL header
174 const uint8* nalu_data; // from NAL header
175 off_t nalu_size; // from NAL header
176 off_t header_bit_size; // calculated
178 int first_mb_in_slice;
180 int pic_parameter_set_id;
181 int colour_plane_id; // TODO(posciak): use this! http://crbug.com/139878
184 bool bottom_field_flag;
186 int pic_order_cnt_lsb;
187 int delta_pic_order_cnt_bottom;
188 int delta_pic_order_cnt[2];
189 int redundant_pic_cnt;
190 bool direct_spatial_mv_pred_flag;
192 bool num_ref_idx_active_override_flag;
193 int num_ref_idx_l0_active_minus1;
194 int num_ref_idx_l1_active_minus1;
195 bool ref_pic_list_modification_flag_l0;
196 bool ref_pic_list_modification_flag_l1;
197 H264ModificationOfPicNum ref_list_l0_modifications[kRefListModSize];
198 H264ModificationOfPicNum ref_list_l1_modifications[kRefListModSize];
200 int luma_log2_weight_denom;
201 int chroma_log2_weight_denom;
203 bool luma_weight_l0_flag;
204 bool chroma_weight_l0_flag;
205 H264WeightingFactors pred_weight_table_l0;
207 bool luma_weight_l1_flag;
208 bool chroma_weight_l1_flag;
209 H264WeightingFactors pred_weight_table_l1;
211 bool no_output_of_prior_pics_flag;
212 bool long_term_reference_flag;
214 bool adaptive_ref_pic_marking_mode_flag;
215 H264DecRefPicMarking ref_pic_marking[kRefListSize];
219 bool sp_for_switch_flag;
221 int disable_deblocking_filter_idc;
222 int slice_alpha_c0_offset_div2;
223 int slice_beta_offset_div2;
226 struct H264SEIRecoveryPoint {
227 int recovery_frame_cnt;
228 bool exact_match_flag;
229 bool broken_link_flag;
230 int changing_slice_group_idc;
233 struct MEDIA_EXPORT H264SEIMessage {
237 kSEIRecoveryPoint = 6,
243 // Placeholder; in future more supported types will contribute to more
244 // union members here.
245 H264SEIRecoveryPoint recovery_point;
249 // Class to parse an Annex-B H.264 stream,
250 // as specified in chapters 7 and Annex B of the H.264 spec.
251 class MEDIA_EXPORT H264Parser {
255 kInvalidStream, // error in stream
256 kUnsupportedStream, // stream not supported by the parser
257 kEOStream, // end of stream
260 // Find offset from start of data to next NALU start code
261 // and size of found start code (3 or 4 bytes).
262 // If no start code is found, offset is pointing to the first unprocessed byte
263 // (i.e. the first byte that was not considered as a possible start of a start
264 // code) and |*start_code_size| is set to 0.
266 // - |data_size| >= 0
268 // - |*offset| is between 0 and |data_size| included.
269 // It is strictly less than |data_size| if |data_size| > 0.
270 // - |*start_code_size| is either 0, 3 or 4.
271 static bool FindStartCode(const uint8* data, off_t data_size,
272 off_t* offset, off_t* start_code_size);
278 // Set current stream pointer to |stream| of |stream_size| in bytes,
279 // |stream| owned by caller.
280 void SetStream(const uint8* stream, off_t stream_size);
282 // Read the stream to find the next NALU, identify it and return
283 // that information in |*nalu|. This advances the stream to the beginning
284 // of this NALU, but not past it, so subsequent calls to NALU-specific
285 // parsing functions (ParseSPS, etc.) will parse this NALU.
286 // If the caller wishes to skip the current NALU, it can call this function
287 // again, instead of any NALU-type specific parse functions below.
288 Result AdvanceToNextNALU(H264NALU* nalu);
290 // NALU-specific parsing functions.
291 // These should be called after AdvanceToNextNALU().
293 // SPSes and PPSes are owned by the parser class and the memory for their
294 // structures is managed here, not by the caller, as they are reused
297 // Parse an SPS/PPS NALU and save their data in the parser, returning id
298 // of the parsed structure in |*pps_id|/|*sps_id|.
299 // To get a pointer to a given SPS/PPS structure, use GetSPS()/GetPPS(),
300 // passing the returned |*sps_id|/|*pps_id| as parameter.
301 // TODO(posciak,fischman): consider replacing returning Result from Parse*()
302 // methods with a scoped_ptr and adding an AtEOS() function to check for EOS
303 // if Parse*() return NULL.
304 Result ParseSPS(int* sps_id);
305 Result ParsePPS(int* pps_id);
307 // Return a pointer to SPS/PPS with given |sps_id|/|pps_id| or NULL if not
309 const H264SPS* GetSPS(int sps_id);
310 const H264PPS* GetPPS(int pps_id);
312 // Slice headers and SEI messages are not used across NALUs by the parser
313 // and can be discarded after current NALU, so the parser does not store
314 // them, nor does it manage their memory.
315 // The caller has to provide and manage it instead.
317 // Parse a slice header, returning it in |*shdr|. |*nalu| must be set to
318 // the NALU returned from AdvanceToNextNALU() and corresponding to |*shdr|.
319 Result ParseSliceHeader(const H264NALU& nalu, H264SliceHeader* shdr);
321 // Parse a SEI message, returning it in |*sei_msg|, provided and managed
323 Result ParseSEI(H264SEIMessage* sei_msg);
326 // Move the stream pointer to the beginning of the next NALU,
327 // i.e. pointing at the next start code.
328 // Return true if a NALU has been found.
329 // If a NALU is found:
330 // - its size in bytes is returned in |*nalu_size| and includes
331 // the start code as well as the trailing zero bits.
332 // - the size in bytes of the start code is returned in |*start_code_size|.
333 bool LocateNALU(off_t* nalu_size, off_t* start_code_size);
335 // Exp-Golomb code parsing as specified in chapter 9.1 of the spec.
336 // Read one unsigned exp-Golomb code from the stream and return in |*val|.
337 Result ReadUE(int* val);
339 // Read one signed exp-Golomb code from the stream and return in |*val|.
340 Result ReadSE(int* val);
342 // Parse scaling lists (see spec).
343 Result ParseScalingList(int size, int* scaling_list, bool* use_default);
344 Result ParseSPSScalingLists(H264SPS* sps);
345 Result ParsePPSScalingLists(const H264SPS& sps, H264PPS* pps);
347 // Parse reference picture lists' modifications (see spec).
348 Result ParseRefPicListModifications(H264SliceHeader* shdr);
349 Result ParseRefPicListModification(int num_ref_idx_active_minus1,
350 H264ModificationOfPicNum* ref_list_mods);
352 // Parse prediction weight table (see spec).
353 Result ParsePredWeightTable(const H264SPS& sps, H264SliceHeader* shdr);
355 // Parse weighting factors (see spec).
356 Result ParseWeightingFactors(int num_ref_idx_active_minus1,
357 int chroma_array_type,
358 int luma_log2_weight_denom,
359 int chroma_log2_weight_denom,
360 H264WeightingFactors* w_facts);
362 // Parse decoded reference picture marking information (see spec).
363 Result ParseDecRefPicMarking(H264SliceHeader* shdr);
365 // Pointer to the current NALU in the stream.
366 const uint8* stream_;
368 // Bytes left in the stream after the current NALU.
373 // PPSes and SPSes stored for future reference.
374 typedef std::map<int, H264SPS*> SPSById;
375 typedef std::map<int, H264PPS*> PPSById;
376 SPSById active_SPSes_;
377 PPSById active_PPSes_;
379 DISALLOW_COPY_AND_ASSIGN(H264Parser);
384 #endif // MEDIA_FILTERS_H264_PARSER_H_