• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
6 
7 #ifndef H264_PARSER_H_
8 #define H264_PARSER_H_
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 #include <sys/types.h>
13 
14 #include <map>
15 #include <memory>
16 #include <vector>
17 
18 #include "base/macros.h"
19 #include "base/optional.h"
20 #include "h264_bit_reader.h"
21 #include "ranges.h"
22 #include "size.h"
23 #include "subsample_entry.h"
24 
25 namespace media {
26 
27 struct SubsampleEntry;
28 
29 // For explanations of each struct and its members, see H.264 specification
30 // at http://www.itu.int/rec/T-REC-H.264.
31 struct H264NALU {
32   H264NALU();
33 
34   enum Type {
35     kUnspecified = 0,
36     kNonIDRSlice = 1,
37     kSliceDataA = 2,
38     kSliceDataB = 3,
39     kSliceDataC = 4,
40     kIDRSlice = 5,
41     kSEIMessage = 6,
42     kSPS = 7,
43     kPPS = 8,
44     kAUD = 9,
45     kEOSeq = 10,
46     kEOStream = 11,
47     kFiller = 12,
48     kSPSExt = 13,
49     kReserved14 = 14,
50     kReserved15 = 15,
51     kReserved16 = 16,
52     kReserved17 = 17,
53     kReserved18 = 18,
54     kCodedSliceAux = 19,
55     kCodedSliceExtension = 20,
56   };
57 
58   // After (without) start code; we don't own the underlying memory
59   // and a shallow copy should be made when copying this struct.
60   const uint8_t* data;
61   off_t size;  // From after start code to start code of next NALU (or EOS).
62 
63   int nal_ref_idc;
64   int nal_unit_type;
65 };
66 
67 enum {
68   kH264ScalingList4x4Length = 16,
69   kH264ScalingList8x8Length = 64,
70 };
71 
72 struct H264SPS {
73   H264SPS();
74 
75   enum H264ProfileIDC {
76     kProfileIDCBaseline = 66,
77     kProfileIDCConstrainedBaseline = kProfileIDCBaseline,
78     kProfileIDCMain = 77,
79     kProfileIDScalableBaseline = 83,
80     kProfileIDScalableHigh = 86,
81     kProfileIDCHigh = 100,
82     kProfileIDHigh10 = 110,
83     kProfileIDSMultiviewHigh = 118,
84     kProfileIDHigh422 = 122,
85     kProfileIDStereoHigh = 128,
86     kProfileIDHigh444Predictive = 244,
87   };
88 
89   enum AspectRatioIdc {
90     kExtendedSar = 255,
91   };
92 
93   enum {
94     // Constants for HRD parameters (spec ch. E.2.2).
95     kBitRateScaleConstantTerm = 6,  // Equation E-37.
96     kCPBSizeScaleConstantTerm = 4,  // Equation E-38.
97     kDefaultInitialCPBRemovalDelayLength = 24,
98     kDefaultDPBOutputDelayLength = 24,
99     kDefaultTimeOffsetLength = 24,
100   };
101 
102   int profile_idc;
103   bool constraint_set0_flag;
104   bool constraint_set1_flag;
105   bool constraint_set2_flag;
106   bool constraint_set3_flag;
107   bool constraint_set4_flag;
108   bool constraint_set5_flag;
109   int level_idc;
110   int seq_parameter_set_id;
111 
112   int chroma_format_idc;
113   bool separate_colour_plane_flag;
114   int bit_depth_luma_minus8;
115   int bit_depth_chroma_minus8;
116   bool qpprime_y_zero_transform_bypass_flag;
117 
118   bool seq_scaling_matrix_present_flag;
119   int scaling_list4x4[6][kH264ScalingList4x4Length];
120   int scaling_list8x8[6][kH264ScalingList8x8Length];
121 
122   int log2_max_frame_num_minus4;
123   int pic_order_cnt_type;
124   int log2_max_pic_order_cnt_lsb_minus4;
125   bool delta_pic_order_always_zero_flag;
126   int offset_for_non_ref_pic;
127   int offset_for_top_to_bottom_field;
128   int num_ref_frames_in_pic_order_cnt_cycle;
129   int expected_delta_per_pic_order_cnt_cycle;  // calculated
130   int offset_for_ref_frame[255];
131   int max_num_ref_frames;
132   bool gaps_in_frame_num_value_allowed_flag;
133   int pic_width_in_mbs_minus1;
134   int pic_height_in_map_units_minus1;
135   bool frame_mbs_only_flag;
136   bool mb_adaptive_frame_field_flag;
137   bool direct_8x8_inference_flag;
138   bool frame_cropping_flag;
139   int frame_crop_left_offset;
140   int frame_crop_right_offset;
141   int frame_crop_top_offset;
142   int frame_crop_bottom_offset;
143 
144   bool vui_parameters_present_flag;
145   int sar_width;    // Set to 0 when not specified.
146   int sar_height;   // Set to 0 when not specified.
147   bool bitstream_restriction_flag;
148   int max_num_reorder_frames;
149   int max_dec_frame_buffering;
150   bool timing_info_present_flag;
151   int num_units_in_tick;
152   int time_scale;
153   bool fixed_frame_rate_flag;
154 
155   bool video_signal_type_present_flag;
156   int video_format;
157   bool video_full_range_flag;
158   bool colour_description_present_flag;
159   int colour_primaries;
160   int transfer_characteristics;
161   int matrix_coefficients;
162 
163   // TODO(posciak): actually parse these instead of ParseAndIgnoreHRDParameters.
164   bool nal_hrd_parameters_present_flag;
165   int cpb_cnt_minus1;
166   int bit_rate_scale;
167   int cpb_size_scale;
168   int bit_rate_value_minus1[32];
169   int cpb_size_value_minus1[32];
170   bool cbr_flag[32];
171   int initial_cpb_removal_delay_length_minus_1;
172   int cpb_removal_delay_length_minus1;
173   int dpb_output_delay_length_minus1;
174   int time_offset_length;
175 
176   bool low_delay_hrd_flag;
177 
178   int chroma_array_type;
179 
180   // Helpers to compute frequently-used values. These methods return
181   // base::nullopt if they encounter integer overflow. They do not verify that
182   // the results are in-spec for the given profile or level.
183   base::Optional<Size> GetCodedSize() const;
184 };
185 
186 struct H264PPS {
187   H264PPS();
188 
189   int pic_parameter_set_id;
190   int seq_parameter_set_id;
191   bool entropy_coding_mode_flag;
192   bool bottom_field_pic_order_in_frame_present_flag;
193   int num_slice_groups_minus1;
194   // TODO(posciak): Slice groups not implemented, could be added at some point.
195   int num_ref_idx_l0_default_active_minus1;
196   int num_ref_idx_l1_default_active_minus1;
197   bool weighted_pred_flag;
198   int weighted_bipred_idc;
199   int pic_init_qp_minus26;
200   int pic_init_qs_minus26;
201   int chroma_qp_index_offset;
202   bool deblocking_filter_control_present_flag;
203   bool constrained_intra_pred_flag;
204   bool redundant_pic_cnt_present_flag;
205   bool transform_8x8_mode_flag;
206 
207   bool pic_scaling_matrix_present_flag;
208   int scaling_list4x4[6][kH264ScalingList4x4Length];
209   int scaling_list8x8[6][kH264ScalingList8x8Length];
210 
211   int second_chroma_qp_index_offset;
212 };
213 
214 struct H264ModificationOfPicNum {
215   int modification_of_pic_nums_idc;
216   union {
217     int abs_diff_pic_num_minus1;
218     int long_term_pic_num;
219   };
220 };
221 
222 struct H264WeightingFactors {
223   bool luma_weight_flag;
224   bool chroma_weight_flag;
225   int luma_weight[32];
226   int luma_offset[32];
227   int chroma_weight[32][2];
228   int chroma_offset[32][2];
229 };
230 
231 struct H264DecRefPicMarking {
232   int memory_mgmnt_control_operation;
233   int difference_of_pic_nums_minus1;
234   int long_term_pic_num;
235   int long_term_frame_idx;
236   int max_long_term_frame_idx_plus1;
237 };
238 
239 struct H264SliceHeader {
240   H264SliceHeader();
241 
242   enum {
243     kRefListSize = 32,
244     kRefListModSize = kRefListSize
245   };
246 
247   enum Type {
248     kPSlice = 0,
249     kBSlice = 1,
250     kISlice = 2,
251     kSPSlice = 3,
252     kSISlice = 4,
253   };
254 
255   bool IsPSlice() const;
256   bool IsBSlice() const;
257   bool IsISlice() const;
258   bool IsSPSlice() const;
259   bool IsSISlice() const;
260 
261   bool idr_pic_flag;       // from NAL header
262   int nal_ref_idc;         // from NAL header
263   const uint8_t* nalu_data;  // from NAL header
264   off_t nalu_size;         // from NAL header
265   off_t header_bit_size;   // calculated
266 
267   int first_mb_in_slice;
268   int slice_type;
269   int pic_parameter_set_id;
270   int colour_plane_id;  // TODO(posciak): use this!  http://crbug.com/139878
271   int frame_num;
272   bool field_pic_flag;
273   bool bottom_field_flag;
274   int idr_pic_id;
275   int pic_order_cnt_lsb;
276   int delta_pic_order_cnt_bottom;
277   int delta_pic_order_cnt0;
278   int delta_pic_order_cnt1;
279   int redundant_pic_cnt;
280   bool direct_spatial_mv_pred_flag;
281 
282   bool num_ref_idx_active_override_flag;
283   int num_ref_idx_l0_active_minus1;
284   int num_ref_idx_l1_active_minus1;
285   bool ref_pic_list_modification_flag_l0;
286   bool ref_pic_list_modification_flag_l1;
287   H264ModificationOfPicNum ref_list_l0_modifications[kRefListModSize];
288   H264ModificationOfPicNum ref_list_l1_modifications[kRefListModSize];
289 
290   int luma_log2_weight_denom;
291   int chroma_log2_weight_denom;
292 
293   bool luma_weight_l0_flag;
294   bool chroma_weight_l0_flag;
295   H264WeightingFactors pred_weight_table_l0;
296 
297   bool luma_weight_l1_flag;
298   bool chroma_weight_l1_flag;
299   H264WeightingFactors pred_weight_table_l1;
300 
301   bool no_output_of_prior_pics_flag;
302   bool long_term_reference_flag;
303 
304   bool adaptive_ref_pic_marking_mode_flag;
305   H264DecRefPicMarking ref_pic_marking[kRefListSize];
306 
307   int cabac_init_idc;
308   int slice_qp_delta;
309   bool sp_for_switch_flag;
310   int slice_qs_delta;
311   int disable_deblocking_filter_idc;
312   int slice_alpha_c0_offset_div2;
313   int slice_beta_offset_div2;
314 
315   // Calculated.
316   // Size in bits of dec_ref_pic_marking() syntax element.
317   size_t dec_ref_pic_marking_bit_size;
318   size_t pic_order_cnt_bit_size;
319 };
320 
321 struct H264SEIRecoveryPoint {
322   int recovery_frame_cnt;
323   bool exact_match_flag;
324   bool broken_link_flag;
325   int changing_slice_group_idc;
326 };
327 
328 struct H264SEIMessage {
329   H264SEIMessage();
330 
331   enum Type {
332     kSEIRecoveryPoint = 6,
333   };
334 
335   int type;
336   int payload_size;
337   union {
338     // Placeholder; in future more supported types will contribute to more
339     // union members here.
340     H264SEIRecoveryPoint recovery_point;
341   };
342 };
343 
344 // Class to parse an Annex-B H.264 stream,
345 // as specified in chapters 7 and Annex B of the H.264 spec.
346 class H264Parser {
347  public:
348   enum Result {
349     kOk,
350     kInvalidStream,      // error in stream
351     kUnsupportedStream,  // stream not supported by the parser
352     kEOStream,           // end of stream
353   };
354 
355   // Find offset from start of data to next NALU start code
356   // and size of found start code (3 or 4 bytes).
357   // If no start code is found, offset is pointing to the first unprocessed byte
358   // (i.e. the first byte that was not considered as a possible start of a start
359   // code) and |*start_code_size| is set to 0.
360   // Preconditions:
361   // - |data_size| >= 0
362   // Postconditions:
363   // - |*offset| is between 0 and |data_size| included.
364   //   It is strictly less than |data_size| if |data_size| > 0.
365   // - |*start_code_size| is either 0, 3 or 4.
366   static bool FindStartCode(const uint8_t* data,
367                             off_t data_size,
368                             off_t* offset,
369                             off_t* start_code_size);
370 
371   // Wrapper for FindStartCode() that skips over start codes that
372   // may appear inside of |encrypted_ranges_|.
373   // Returns true if a start code was found. Otherwise returns false.
374   static bool FindStartCodeInClearRanges(const uint8_t* data,
375                                          off_t data_size,
376                                          const Ranges<const uint8_t*>& ranges,
377                                          off_t* offset,
378                                          off_t* start_code_size);
379 
380   H264Parser();
381   ~H264Parser();
382 
383   void Reset();
384   // Set current stream pointer to |stream| of |stream_size| in bytes,
385   // |stream| owned by caller.
386   // |subsamples| contains information about what parts of |stream| are
387   // encrypted.
388   void SetStream(const uint8_t* stream, off_t stream_size);
389   void SetEncryptedStream(const uint8_t* stream,
390                           off_t stream_size,
391                           const std::vector<SubsampleEntry>& subsamples);
392 
393   // Read the stream to find the next NALU, identify it and return
394   // that information in |*nalu|. This advances the stream to the beginning
395   // of this NALU, but not past it, so subsequent calls to NALU-specific
396   // parsing functions (ParseSPS, etc.)  will parse this NALU.
397   // If the caller wishes to skip the current NALU, it can call this function
398   // again, instead of any NALU-type specific parse functions below.
399   Result AdvanceToNextNALU(H264NALU* nalu);
400 
401   // NALU-specific parsing functions.
402   // These should be called after AdvanceToNextNALU().
403 
404   // SPSes and PPSes are owned by the parser class and the memory for their
405   // structures is managed here, not by the caller, as they are reused
406   // across NALUs.
407   //
408   // Parse an SPS/PPS NALU and save their data in the parser, returning id
409   // of the parsed structure in |*pps_id|/|*sps_id|.
410   // To get a pointer to a given SPS/PPS structure, use GetSPS()/GetPPS(),
411   // passing the returned |*sps_id|/|*pps_id| as parameter.
412   // TODO(posciak,fischman): consider replacing returning Result from Parse*()
413   // methods with a scoped_ptr and adding an AtEOS() function to check for EOS
414   // if Parse*() return NULL.
415   Result ParseSPS(int* sps_id);
416   Result ParsePPS(int* pps_id);
417 
418   // Return a pointer to SPS/PPS with given |sps_id|/|pps_id| or NULL if not
419   // present.
420   const H264SPS* GetSPS(int sps_id) const;
421   const H264PPS* GetPPS(int pps_id) const;
422 
423   // Slice headers and SEI messages are not used across NALUs by the parser
424   // and can be discarded after current NALU, so the parser does not store
425   // them, nor does it manage their memory.
426   // The caller has to provide and manage it instead.
427 
428   // Parse a slice header, returning it in |*shdr|. |*nalu| must be set to
429   // the NALU returned from AdvanceToNextNALU() and corresponding to |*shdr|.
430   Result ParseSliceHeader(const H264NALU& nalu, H264SliceHeader* shdr);
431 
432   // Parse a SEI message, returning it in |*sei_msg|, provided and managed
433   // by the caller.
434   Result ParseSEI(H264SEIMessage* sei_msg);
435 
436  private:
437   // Move the stream pointer to the beginning of the next NALU,
438   // i.e. pointing at the next start code.
439   // Return true if a NALU has been found.
440   // If a NALU is found:
441   // - its size in bytes is returned in |*nalu_size| and includes
442   //   the start code as well as the trailing zero bits.
443   // - the size in bytes of the start code is returned in |*start_code_size|.
444   bool LocateNALU(off_t* nalu_size, off_t* start_code_size);
445 
446   // Exp-Golomb code parsing as specified in chapter 9.1 of the spec.
447   // Read one unsigned exp-Golomb code from the stream and return in |*val|.
448   Result ReadUE(int* val);
449 
450   // Read one signed exp-Golomb code from the stream and return in |*val|.
451   Result ReadSE(int* val);
452 
453   // Parse scaling lists (see spec).
454   Result ParseScalingList(int size, int* scaling_list, bool* use_default);
455   Result ParseSPSScalingLists(H264SPS* sps);
456   Result ParsePPSScalingLists(const H264SPS& sps, H264PPS* pps);
457 
458   // Parse optional VUI parameters in SPS (see spec).
459   Result ParseVUIParameters(H264SPS* sps);
460   // Set |hrd_parameters_present| to true only if they are present.
461   Result ParseAndIgnoreHRDParameters(bool* hrd_parameters_present);
462 
463   // Parse reference picture lists' modifications (see spec).
464   Result ParseRefPicListModifications(H264SliceHeader* shdr);
465   Result ParseRefPicListModification(int num_ref_idx_active_minus1,
466                                      H264ModificationOfPicNum* ref_list_mods);
467 
468   // Parse prediction weight table (see spec).
469   Result ParsePredWeightTable(const H264SPS& sps, H264SliceHeader* shdr);
470 
471   // Parse weighting factors (see spec).
472   Result ParseWeightingFactors(int num_ref_idx_active_minus1,
473                                int chroma_array_type,
474                                int luma_log2_weight_denom,
475                                int chroma_log2_weight_denom,
476                                H264WeightingFactors* w_facts);
477 
478   // Parse decoded reference picture marking information (see spec).
479   Result ParseDecRefPicMarking(H264SliceHeader* shdr);
480 
481   // Pointer to the current NALU in the stream.
482   const uint8_t* stream_;
483 
484   // Bytes left in the stream after the current NALU.
485   off_t bytes_left_;
486 
487   H264BitReader br_;
488 
489   // PPSes and SPSes stored for future reference.
490   std::map<int, std::unique_ptr<H264SPS>> active_SPSes_;
491   std::map<int, std::unique_ptr<H264PPS>> active_PPSes_;
492 
493   // Ranges of encrypted bytes in the buffer passed to
494   // SetEncryptedStream().
495   Ranges<const uint8_t*> encrypted_ranges_;
496 
497   DISALLOW_COPY_AND_ASSIGN(H264Parser);
498 };
499 
500 }  // namespace media
501 
502 #endif  // H264_PARSER_H_
503