Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / filters / h264_parser.h
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 MEDIA_FILTERS_H264_PARSER_H_
8 #define MEDIA_FILTERS_H264_PARSER_H_
9
10 #include <sys/types.h>
11
12 #include <map>
13
14 #include "base/basictypes.h"
15 #include "media/base/media_export.h"
16 #include "media/filters/h264_bit_reader.h"
17
18 namespace media {
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 MEDIA_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     kAUD = 9,
33     kEOSeq = 10,
34     kEOStream = 11,
35     kCodedSliceExtension = 20,
36   };
37
38   // After (without) start code; we don't own the underlying memory
39   // and a shallow copy should be made when copying this struct.
40   const uint8* data;
41   off_t size;  // From after start code to start code of next NALU (or EOS).
42
43   int nal_ref_idc;
44   int nal_unit_type;
45 };
46
47 enum {
48   kH264ScalingList4x4Length = 16,
49   kH264ScalingList8x8Length = 64,
50 };
51
52 struct MEDIA_EXPORT H264SPS {
53   H264SPS();
54
55   int profile_idc;
56   int constraint_setx_flag;
57   int level_idc;
58   int seq_parameter_set_id;
59
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;
65
66   bool seq_scaling_matrix_present_flag;
67   int scaling_list4x4[6][kH264ScalingList4x4Length];
68   int scaling_list8x8[6][kH264ScalingList8x8Length];
69
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.
95 };
96
97 struct MEDIA_EXPORT H264PPS {
98   H264PPS();
99
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;
117
118   bool pic_scaling_matrix_present_flag;
119   int scaling_list4x4[6][kH264ScalingList4x4Length];
120   int scaling_list8x8[6][kH264ScalingList8x8Length];
121
122   int second_chroma_qp_index_offset;
123 };
124
125 struct MEDIA_EXPORT H264ModificationOfPicNum {
126   int modification_of_pic_nums_idc;
127   union {
128     int abs_diff_pic_num_minus1;
129     int long_term_pic_num;
130   };
131 };
132
133 struct MEDIA_EXPORT H264WeightingFactors {
134   bool luma_weight_flag;
135   bool chroma_weight_flag;
136   int luma_weight[32];
137   int luma_offset[32];
138   int chroma_weight[32][2];
139   int chroma_offset[32][2];
140 };
141
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;
148 };
149
150 struct MEDIA_EXPORT H264SliceHeader {
151   H264SliceHeader();
152
153   enum {
154     kRefListSize = 32,
155     kRefListModSize = kRefListSize
156   };
157
158   enum Type {
159     kPSlice = 0,
160     kBSlice = 1,
161     kISlice = 2,
162     kSPSlice = 3,
163     kSISlice = 4,
164   };
165
166   bool IsPSlice() const;
167   bool IsBSlice() const;
168   bool IsISlice() const;
169   bool IsSPSlice() const;
170   bool IsSISlice() const;
171
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
177
178   int first_mb_in_slice;
179   int slice_type;
180   int pic_parameter_set_id;
181   int colour_plane_id;  // TODO(posciak): use this!  http://crbug.com/139878
182   int frame_num;
183   bool field_pic_flag;
184   bool bottom_field_flag;
185   int idr_pic_id;
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;
191
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];
199
200   int luma_log2_weight_denom;
201   int chroma_log2_weight_denom;
202
203   bool luma_weight_l0_flag;
204   bool chroma_weight_l0_flag;
205   H264WeightingFactors pred_weight_table_l0;
206
207   bool luma_weight_l1_flag;
208   bool chroma_weight_l1_flag;
209   H264WeightingFactors pred_weight_table_l1;
210
211   bool no_output_of_prior_pics_flag;
212   bool long_term_reference_flag;
213
214   bool adaptive_ref_pic_marking_mode_flag;
215   H264DecRefPicMarking ref_pic_marking[kRefListSize];
216
217   int cabac_init_idc;
218   int slice_qp_delta;
219   bool sp_for_switch_flag;
220   int slice_qs_delta;
221   int disable_deblocking_filter_idc;
222   int slice_alpha_c0_offset_div2;
223   int slice_beta_offset_div2;
224 };
225
226 struct H264SEIRecoveryPoint {
227   int recovery_frame_cnt;
228   bool exact_match_flag;
229   bool broken_link_flag;
230   int changing_slice_group_idc;
231 };
232
233 struct MEDIA_EXPORT H264SEIMessage {
234   H264SEIMessage();
235
236   enum Type {
237     kSEIRecoveryPoint = 6,
238   };
239
240   int type;
241   int payload_size;
242   union {
243     // Placeholder; in future more supported types will contribute to more
244     // union members here.
245     H264SEIRecoveryPoint recovery_point;
246   };
247 };
248
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 {
252  public:
253   enum Result {
254     kOk,
255     kInvalidStream,      // error in stream
256     kUnsupportedStream,  // stream not supported by the parser
257     kEOStream,           // end of stream
258   };
259
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.
265   // Preconditions:
266   // - |data_size| >= 0
267   // Postconditions:
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);
273
274   H264Parser();
275   ~H264Parser();
276
277   void Reset();
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);
281
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);
289
290   // NALU-specific parsing functions.
291   // These should be called after AdvanceToNextNALU().
292
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
295   // across NALUs.
296   //
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);
306
307   // Return a pointer to SPS/PPS with given |sps_id|/|pps_id| or NULL if not
308   // present.
309   const H264SPS* GetSPS(int sps_id);
310   const H264PPS* GetPPS(int pps_id);
311
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.
316
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);
320
321   // Parse a SEI message, returning it in |*sei_msg|, provided and managed
322   // by the caller.
323   Result ParseSEI(H264SEIMessage* sei_msg);
324
325  private:
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);
334
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);
338
339   // Read one signed exp-Golomb code from the stream and return in |*val|.
340   Result ReadSE(int* val);
341
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);
346
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);
351
352   // Parse prediction weight table (see spec).
353   Result ParsePredWeightTable(const H264SPS& sps, H264SliceHeader* shdr);
354
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);
361
362   // Parse decoded reference picture marking information (see spec).
363   Result ParseDecRefPicMarking(H264SliceHeader* shdr);
364
365   // Pointer to the current NALU in the stream.
366   const uint8* stream_;
367
368   // Bytes left in the stream after the current NALU.
369   off_t bytes_left_;
370
371   H264BitReader br_;
372
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_;
378
379   DISALLOW_COPY_AND_ASSIGN(H264Parser);
380 };
381
382 }  // namespace media
383
384 #endif  // MEDIA_FILTERS_H264_PARSER_H_