Prepare chromium for WorkerThread migration to WebThread.
[chromium-blink-merge.git] / media / filters / h264_parser.h
blob45020af61344b8228259bfc2a4868b1b7cfd8a9d
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.
4 //
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>
12 #include <map>
14 #include "base/basictypes.h"
15 #include "media/base/media_export.h"
16 #include "media/filters/h264_bit_reader.h"
18 namespace media {
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 {
23 H264NALU();
25 enum Type {
26 kUnspecified = 0,
27 kNonIDRSlice = 1,
28 kSliceDataA = 2,
29 kSliceDataB = 3,
30 kSliceDataC = 4,
31 kIDRSlice = 5,
32 kSEIMessage = 6,
33 kSPS = 7,
34 kPPS = 8,
35 kAUD = 9,
36 kEOSeq = 10,
37 kEOStream = 11,
38 kFiller = 12,
39 kSPSExt = 13,
40 kReserved14 = 14,
41 kReserved15 = 15,
42 kReserved16 = 16,
43 kReserved17 = 17,
44 kReserved18 = 18,
45 kCodedSliceAux = 19,
46 kCodedSliceExtension = 20,
49 // After (without) start code; we don't own the underlying memory
50 // and a shallow copy should be made when copying this struct.
51 const uint8* data;
52 off_t size; // From after start code to start code of next NALU (or EOS).
54 int nal_ref_idc;
55 int nal_unit_type;
58 enum {
59 kH264ScalingList4x4Length = 16,
60 kH264ScalingList8x8Length = 64,
63 struct MEDIA_EXPORT H264SPS {
64 H264SPS();
66 enum H264ProfileIDC {
67 kProfileIDCBaseline = 66,
68 kProfileIDCConstrainedBaseline = kProfileIDCBaseline,
69 kProfileIDCMain = 77,
70 kProfileIDCHigh = 100,
73 enum AspectRatioIdc {
74 kExtendedSar = 255,
77 enum {
78 // Constants for HRD parameters (spec ch. E.2.2).
79 kBitRateScaleConstantTerm = 6, // Equation E-37.
80 kCPBSizeScaleConstantTerm = 4, // Equation E-38.
81 kDefaultInitialCPBRemovalDelayLength = 24,
82 kDefaultDPBOutputDelayLength = 24,
83 kDefaultTimeOffsetLength = 24,
86 int profile_idc;
87 bool constraint_set0_flag;
88 bool constraint_set1_flag;
89 bool constraint_set2_flag;
90 bool constraint_set3_flag;
91 bool constraint_set4_flag;
92 bool constraint_set5_flag;
93 int level_idc;
94 int seq_parameter_set_id;
96 int chroma_format_idc;
97 bool separate_colour_plane_flag;
98 int bit_depth_luma_minus8;
99 int bit_depth_chroma_minus8;
100 bool qpprime_y_zero_transform_bypass_flag;
102 bool seq_scaling_matrix_present_flag;
103 int scaling_list4x4[6][kH264ScalingList4x4Length];
104 int scaling_list8x8[6][kH264ScalingList8x8Length];
106 int log2_max_frame_num_minus4;
107 int pic_order_cnt_type;
108 int log2_max_pic_order_cnt_lsb_minus4;
109 bool delta_pic_order_always_zero_flag;
110 int offset_for_non_ref_pic;
111 int offset_for_top_to_bottom_field;
112 int num_ref_frames_in_pic_order_cnt_cycle;
113 int expected_delta_per_pic_order_cnt_cycle; // calculated
114 int offset_for_ref_frame[255];
115 int max_num_ref_frames;
116 bool gaps_in_frame_num_value_allowed_flag;
117 int pic_width_in_mbs_minus1;
118 int pic_height_in_map_units_minus1;
119 bool frame_mbs_only_flag;
120 bool mb_adaptive_frame_field_flag;
121 bool direct_8x8_inference_flag;
122 bool frame_cropping_flag;
123 int frame_crop_left_offset;
124 int frame_crop_right_offset;
125 int frame_crop_top_offset;
126 int frame_crop_bottom_offset;
128 bool vui_parameters_present_flag;
129 int sar_width; // Set to 0 when not specified.
130 int sar_height; // Set to 0 when not specified.
131 bool bitstream_restriction_flag;
132 int max_num_reorder_frames;
133 int max_dec_frame_buffering;
134 bool timing_info_present_flag;
135 int num_units_in_tick;
136 int time_scale;
137 bool fixed_frame_rate_flag;
139 // TODO(posciak): actually parse these instead of ParseAndIgnoreHRDParameters.
140 bool nal_hrd_parameters_present_flag;
141 int cpb_cnt_minus1;
142 int bit_rate_scale;
143 int cpb_size_scale;
144 int bit_rate_value_minus1[32];
145 int cpb_size_value_minus1[32];
146 bool cbr_flag[32];
147 int initial_cpb_removal_delay_length_minus_1;
148 int cpb_removal_delay_length_minus1;
149 int dpb_output_delay_length_minus1;
150 int time_offset_length;
152 bool low_delay_hrd_flag;
154 int chroma_array_type;
157 struct MEDIA_EXPORT H264PPS {
158 H264PPS();
160 int pic_parameter_set_id;
161 int seq_parameter_set_id;
162 bool entropy_coding_mode_flag;
163 bool bottom_field_pic_order_in_frame_present_flag;
164 int num_slice_groups_minus1;
165 // TODO(posciak): Slice groups not implemented, could be added at some point.
166 int num_ref_idx_l0_default_active_minus1;
167 int num_ref_idx_l1_default_active_minus1;
168 bool weighted_pred_flag;
169 int weighted_bipred_idc;
170 int pic_init_qp_minus26;
171 int pic_init_qs_minus26;
172 int chroma_qp_index_offset;
173 bool deblocking_filter_control_present_flag;
174 bool constrained_intra_pred_flag;
175 bool redundant_pic_cnt_present_flag;
176 bool transform_8x8_mode_flag;
178 bool pic_scaling_matrix_present_flag;
179 int scaling_list4x4[6][kH264ScalingList4x4Length];
180 int scaling_list8x8[6][kH264ScalingList8x8Length];
182 int second_chroma_qp_index_offset;
185 struct MEDIA_EXPORT H264ModificationOfPicNum {
186 int modification_of_pic_nums_idc;
187 union {
188 int abs_diff_pic_num_minus1;
189 int long_term_pic_num;
193 struct MEDIA_EXPORT H264WeightingFactors {
194 bool luma_weight_flag;
195 bool chroma_weight_flag;
196 int luma_weight[32];
197 int luma_offset[32];
198 int chroma_weight[32][2];
199 int chroma_offset[32][2];
202 struct MEDIA_EXPORT H264DecRefPicMarking {
203 int memory_mgmnt_control_operation;
204 int difference_of_pic_nums_minus1;
205 int long_term_pic_num;
206 int long_term_frame_idx;
207 int max_long_term_frame_idx_plus1;
210 struct MEDIA_EXPORT H264SliceHeader {
211 H264SliceHeader();
213 enum {
214 kRefListSize = 32,
215 kRefListModSize = kRefListSize
218 enum Type {
219 kPSlice = 0,
220 kBSlice = 1,
221 kISlice = 2,
222 kSPSlice = 3,
223 kSISlice = 4,
226 bool IsPSlice() const;
227 bool IsBSlice() const;
228 bool IsISlice() const;
229 bool IsSPSlice() const;
230 bool IsSISlice() const;
232 bool idr_pic_flag; // from NAL header
233 int nal_ref_idc; // from NAL header
234 const uint8* nalu_data; // from NAL header
235 off_t nalu_size; // from NAL header
236 off_t header_bit_size; // calculated
238 int first_mb_in_slice;
239 int slice_type;
240 int pic_parameter_set_id;
241 int colour_plane_id; // TODO(posciak): use this! http://crbug.com/139878
242 int frame_num;
243 bool field_pic_flag;
244 bool bottom_field_flag;
245 int idr_pic_id;
246 int pic_order_cnt_lsb;
247 int delta_pic_order_cnt_bottom;
248 int delta_pic_order_cnt[2];
249 int redundant_pic_cnt;
250 bool direct_spatial_mv_pred_flag;
252 bool num_ref_idx_active_override_flag;
253 int num_ref_idx_l0_active_minus1;
254 int num_ref_idx_l1_active_minus1;
255 bool ref_pic_list_modification_flag_l0;
256 bool ref_pic_list_modification_flag_l1;
257 H264ModificationOfPicNum ref_list_l0_modifications[kRefListModSize];
258 H264ModificationOfPicNum ref_list_l1_modifications[kRefListModSize];
260 int luma_log2_weight_denom;
261 int chroma_log2_weight_denom;
263 bool luma_weight_l0_flag;
264 bool chroma_weight_l0_flag;
265 H264WeightingFactors pred_weight_table_l0;
267 bool luma_weight_l1_flag;
268 bool chroma_weight_l1_flag;
269 H264WeightingFactors pred_weight_table_l1;
271 bool no_output_of_prior_pics_flag;
272 bool long_term_reference_flag;
274 bool adaptive_ref_pic_marking_mode_flag;
275 H264DecRefPicMarking ref_pic_marking[kRefListSize];
277 int cabac_init_idc;
278 int slice_qp_delta;
279 bool sp_for_switch_flag;
280 int slice_qs_delta;
281 int disable_deblocking_filter_idc;
282 int slice_alpha_c0_offset_div2;
283 int slice_beta_offset_div2;
286 struct H264SEIRecoveryPoint {
287 int recovery_frame_cnt;
288 bool exact_match_flag;
289 bool broken_link_flag;
290 int changing_slice_group_idc;
293 struct MEDIA_EXPORT H264SEIMessage {
294 H264SEIMessage();
296 enum Type {
297 kSEIRecoveryPoint = 6,
300 int type;
301 int payload_size;
302 union {
303 // Placeholder; in future more supported types will contribute to more
304 // union members here.
305 H264SEIRecoveryPoint recovery_point;
309 // Class to parse an Annex-B H.264 stream,
310 // as specified in chapters 7 and Annex B of the H.264 spec.
311 class MEDIA_EXPORT H264Parser {
312 public:
313 enum Result {
314 kOk,
315 kInvalidStream, // error in stream
316 kUnsupportedStream, // stream not supported by the parser
317 kEOStream, // end of stream
320 // Find offset from start of data to next NALU start code
321 // and size of found start code (3 or 4 bytes).
322 // If no start code is found, offset is pointing to the first unprocessed byte
323 // (i.e. the first byte that was not considered as a possible start of a start
324 // code) and |*start_code_size| is set to 0.
325 // Preconditions:
326 // - |data_size| >= 0
327 // Postconditions:
328 // - |*offset| is between 0 and |data_size| included.
329 // It is strictly less than |data_size| if |data_size| > 0.
330 // - |*start_code_size| is either 0, 3 or 4.
331 static bool FindStartCode(const uint8* data, off_t data_size,
332 off_t* offset, off_t* start_code_size);
334 H264Parser();
335 ~H264Parser();
337 void Reset();
338 // Set current stream pointer to |stream| of |stream_size| in bytes,
339 // |stream| owned by caller.
340 void SetStream(const uint8* stream, off_t stream_size);
342 // Read the stream to find the next NALU, identify it and return
343 // that information in |*nalu|. This advances the stream to the beginning
344 // of this NALU, but not past it, so subsequent calls to NALU-specific
345 // parsing functions (ParseSPS, etc.) will parse this NALU.
346 // If the caller wishes to skip the current NALU, it can call this function
347 // again, instead of any NALU-type specific parse functions below.
348 Result AdvanceToNextNALU(H264NALU* nalu);
350 // NALU-specific parsing functions.
351 // These should be called after AdvanceToNextNALU().
353 // SPSes and PPSes are owned by the parser class and the memory for their
354 // structures is managed here, not by the caller, as they are reused
355 // across NALUs.
357 // Parse an SPS/PPS NALU and save their data in the parser, returning id
358 // of the parsed structure in |*pps_id|/|*sps_id|.
359 // To get a pointer to a given SPS/PPS structure, use GetSPS()/GetPPS(),
360 // passing the returned |*sps_id|/|*pps_id| as parameter.
361 // TODO(posciak,fischman): consider replacing returning Result from Parse*()
362 // methods with a scoped_ptr and adding an AtEOS() function to check for EOS
363 // if Parse*() return NULL.
364 Result ParseSPS(int* sps_id);
365 Result ParsePPS(int* pps_id);
367 // Return a pointer to SPS/PPS with given |sps_id|/|pps_id| or NULL if not
368 // present.
369 const H264SPS* GetSPS(int sps_id);
370 const H264PPS* GetPPS(int pps_id);
372 // Slice headers and SEI messages are not used across NALUs by the parser
373 // and can be discarded after current NALU, so the parser does not store
374 // them, nor does it manage their memory.
375 // The caller has to provide and manage it instead.
377 // Parse a slice header, returning it in |*shdr|. |*nalu| must be set to
378 // the NALU returned from AdvanceToNextNALU() and corresponding to |*shdr|.
379 Result ParseSliceHeader(const H264NALU& nalu, H264SliceHeader* shdr);
381 // Parse a SEI message, returning it in |*sei_msg|, provided and managed
382 // by the caller.
383 Result ParseSEI(H264SEIMessage* sei_msg);
385 private:
386 // Move the stream pointer to the beginning of the next NALU,
387 // i.e. pointing at the next start code.
388 // Return true if a NALU has been found.
389 // If a NALU is found:
390 // - its size in bytes is returned in |*nalu_size| and includes
391 // the start code as well as the trailing zero bits.
392 // - the size in bytes of the start code is returned in |*start_code_size|.
393 bool LocateNALU(off_t* nalu_size, off_t* start_code_size);
395 // Exp-Golomb code parsing as specified in chapter 9.1 of the spec.
396 // Read one unsigned exp-Golomb code from the stream and return in |*val|.
397 Result ReadUE(int* val);
399 // Read one signed exp-Golomb code from the stream and return in |*val|.
400 Result ReadSE(int* val);
402 // Parse scaling lists (see spec).
403 Result ParseScalingList(int size, int* scaling_list, bool* use_default);
404 Result ParseSPSScalingLists(H264SPS* sps);
405 Result ParsePPSScalingLists(const H264SPS& sps, H264PPS* pps);
407 // Parse optional VUI parameters in SPS (see spec).
408 Result ParseVUIParameters(H264SPS* sps);
409 // Set |hrd_parameters_present| to true only if they are present.
410 Result ParseAndIgnoreHRDParameters(bool* hrd_parameters_present);
412 // Parse reference picture lists' modifications (see spec).
413 Result ParseRefPicListModifications(H264SliceHeader* shdr);
414 Result ParseRefPicListModification(int num_ref_idx_active_minus1,
415 H264ModificationOfPicNum* ref_list_mods);
417 // Parse prediction weight table (see spec).
418 Result ParsePredWeightTable(const H264SPS& sps, H264SliceHeader* shdr);
420 // Parse weighting factors (see spec).
421 Result ParseWeightingFactors(int num_ref_idx_active_minus1,
422 int chroma_array_type,
423 int luma_log2_weight_denom,
424 int chroma_log2_weight_denom,
425 H264WeightingFactors* w_facts);
427 // Parse decoded reference picture marking information (see spec).
428 Result ParseDecRefPicMarking(H264SliceHeader* shdr);
430 // Pointer to the current NALU in the stream.
431 const uint8* stream_;
433 // Bytes left in the stream after the current NALU.
434 off_t bytes_left_;
436 H264BitReader br_;
438 // PPSes and SPSes stored for future reference.
439 typedef std::map<int, H264SPS*> SPSById;
440 typedef std::map<int, H264PPS*> PPSById;
441 SPSById active_SPSes_;
442 PPSById active_PPSes_;
444 DISALLOW_COPY_AND_ASSIGN(H264Parser);
447 } // namespace media
449 #endif // MEDIA_FILTERS_H264_PARSER_H_