1 /***************************************************************** 2 | 3 | AP4 - AVC Parser 4 | 5 | Copyright 2002-2014 Axiomatic Systems, LLC 6 | 7 | 8 | This file is part of Bento4/AP4 (MP4 Atom Processing Library). 9 | 10 | Unless you have obtained Bento4 under a difference license, 11 | this version of Bento4 is Bento4|GPL. 12 | Bento4|GPL is free software; you can redistribute it and/or modify 13 | it under the terms of the GNU General Public License as published by 14 | the Free Software Foundation; either version 2, or (at your option) 15 | any later version. 16 | 17 | Bento4|GPL is distributed in the hope that it will be useful, 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 | GNU General Public License for more details. 21 | 22 | You should have received a copy of the GNU General Public License 23 | along with Bento4|GPL; see the file COPYING. If not, write to the 24 | Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 25 | 02111-1307, USA. 26 | 27 ****************************************************************/ 28 29 #ifndef _AP4_AVC_PARSER_H_ 30 #define _AP4_AVC_PARSER_H_ 31 32 /*---------------------------------------------------------------------- 33 | includes 34 +---------------------------------------------------------------------*/ 35 #include "Ap4Types.h" 36 #include "Ap4Results.h" 37 #include "Ap4DataBuffer.h" 38 #include "Ap4NalParser.h" 39 #include "Ap4Array.h" 40 41 /*---------------------------------------------------------------------- 42 | constants 43 +---------------------------------------------------------------------*/ 44 const unsigned int AP4_AVC_NAL_UNIT_TYPE_UNSPECIFIED = 0; 45 const unsigned int AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_NON_IDR_PICTURE = 1; 46 const unsigned int AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_A = 2; 47 const unsigned int AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_B = 3; 48 const unsigned int AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_C = 4; 49 const unsigned int AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE = 5; 50 const unsigned int AP4_AVC_NAL_UNIT_TYPE_SEI = 6; 51 const unsigned int AP4_AVC_NAL_UNIT_TYPE_SPS = 7; 52 const unsigned int AP4_AVC_NAL_UNIT_TYPE_PPS = 8; 53 const unsigned int AP4_AVC_NAL_UNIT_TYPE_ACCESS_UNIT_DELIMITER = 9; 54 const unsigned int AP4_AVC_NAL_UNIT_TYPE_END_OF_SEQUENCE = 10; 55 const unsigned int AP4_AVC_NAL_UNIT_TYPE_END_OF_STREAM = 11; 56 const unsigned int AP4_AVC_NAL_UNIT_TYPE_FILLER_DATA = 12; 57 const unsigned int AP4_AVC_NAL_UNIT_TYPE_SPS_EXTENSION = 13; 58 const unsigned int AP4_AVC_NAL_UNIT_TYPE_PREFIX = 14; 59 const unsigned int AP4_AVC_NAL_UNIT_TYPE_SUBSET_SPS = 15; 60 const unsigned int AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_AUXILIARY_PICTURE = 19; 61 const unsigned int AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_IN_SCALABLE_EXTENSION = 20; 62 const unsigned int AP4_AVC_NAL_UNIT_TYPE_UNSPECIFIED28 = 28; 63 const unsigned int AP4_AVC_NAL_UNIT_TYPE_UNSPECIFIED30 = 30; 64 65 const unsigned int AP4_AVC_SLICE_TYPE_P = 0; 66 const unsigned int AP4_AVC_SLICE_TYPE_B = 1; 67 const unsigned int AP4_AVC_SLICE_TYPE_I = 2; 68 const unsigned int AP4_AVC_SLICE_TYPE_SP = 3; 69 const unsigned int AP4_AVC_SLICE_TYPE_SI = 4; 70 71 const unsigned int AP4_AVC_SPS_MAX_ID = 255; 72 const unsigned int AP4_AVC_SPS_MAX_NUM_REF_FRAMES_IN_PIC_ORDER_CNT_CYCLE = 256; 73 const unsigned int AP4_AVC_SPS_MAX_SCALING_LIST_COUNT = 12; 74 75 const unsigned int AP4_AVC_PPS_MAX_ID = 255; 76 const unsigned int AP4_AVC_PPS_MAX_SLICE_GROUPS = 256; 77 const unsigned int AP4_AVC_PPS_MAX_PIC_SIZE_IN_MAP_UNITS = 65536; 78 79 /*---------------------------------------------------------------------- 80 | types 81 +---------------------------------------------------------------------*/ 82 typedef struct { 83 int scale[16]; 84 } AP4_AvcScalingList4x4; 85 86 typedef struct { 87 int scale[64]; 88 } AP4_AvcScalingList8x8; 89 90 struct AP4_AvcSequenceParameterSet { 91 AP4_AvcSequenceParameterSet(); 92 93 void GetInfo(unsigned int& width, unsigned int& height); 94 95 AP4_DataBuffer raw_bytes; 96 97 unsigned int profile_idc; 98 unsigned int constraint_set0_flag; 99 unsigned int constraint_set1_flag; 100 unsigned int constraint_set2_flag; 101 unsigned int constraint_set3_flag; 102 unsigned int level_idc; 103 unsigned int seq_parameter_set_id; 104 unsigned int chroma_format_idc; 105 unsigned int separate_colour_plane_flag; 106 unsigned int bit_depth_luma_minus8; 107 unsigned int bit_depth_chroma_minus8; 108 unsigned int qpprime_y_zero_transform_bypass_flag; 109 unsigned int seq_scaling_matrix_present_flag; 110 AP4_AvcScalingList4x4 scaling_list_4x4[6]; 111 bool use_default_scaling_matrix_4x4[AP4_AVC_SPS_MAX_SCALING_LIST_COUNT]; 112 AP4_AvcScalingList8x8 scaling_list_8x8[6]; 113 unsigned char use_default_scaling_matrix_8x8[AP4_AVC_SPS_MAX_SCALING_LIST_COUNT]; 114 unsigned int log2_max_frame_num_minus4; 115 unsigned int pic_order_cnt_type; 116 unsigned int log2_max_pic_order_cnt_lsb_minus4; 117 unsigned int delta_pic_order_always_zero_flags; 118 int offset_for_non_ref_pic; 119 int offset_for_top_to_bottom_field; 120 unsigned int num_ref_frames_in_pic_order_cnt_cycle; 121 unsigned int offset_for_ref_frame[AP4_AVC_SPS_MAX_NUM_REF_FRAMES_IN_PIC_ORDER_CNT_CYCLE]; 122 unsigned int num_ref_frames; 123 unsigned int gaps_in_frame_num_value_allowed_flag; 124 unsigned int pic_width_in_mbs_minus1; 125 unsigned int pic_height_in_map_units_minus1; 126 unsigned int frame_mbs_only_flag; 127 unsigned int mb_adaptive_frame_field_flag; 128 unsigned int direct_8x8_inference_flag; 129 unsigned int frame_cropping_flag; 130 unsigned int frame_crop_left_offset; 131 unsigned int frame_crop_right_offset; 132 unsigned int frame_crop_top_offset; 133 unsigned int frame_crop_bottom_offset; 134 }; 135 136 struct AP4_AvcPictureParameterSet { 137 AP4_AvcPictureParameterSet(); 138 139 AP4_DataBuffer raw_bytes; 140 141 unsigned int pic_parameter_set_id; 142 unsigned int seq_parameter_set_id; 143 unsigned int entropy_coding_mode_flag; 144 unsigned int pic_order_present_flag; 145 unsigned int num_slice_groups_minus1; 146 unsigned int slice_group_map_type; 147 unsigned int run_length_minus1[AP4_AVC_PPS_MAX_SLICE_GROUPS]; 148 unsigned int top_left[AP4_AVC_PPS_MAX_SLICE_GROUPS]; 149 unsigned int bottom_right[AP4_AVC_PPS_MAX_SLICE_GROUPS]; 150 unsigned int slice_group_change_direction_flag; 151 unsigned int slice_group_change_rate_minus1; 152 unsigned int pic_size_in_map_units_minus1; 153 unsigned int num_ref_idx_10_active_minus1; 154 unsigned int num_ref_idx_11_active_minus1; 155 unsigned int weighted_pred_flag; 156 unsigned int weighted_bipred_idc; 157 int pic_init_qp_minus26; 158 int pic_init_qs_minus26; 159 int chroma_qp_index_offset; 160 unsigned int deblocking_filter_control_present_flag; 161 unsigned int constrained_intra_pred_flag; 162 unsigned int redundant_pic_cnt_present_flag; 163 }; 164 165 struct AP4_AvcSliceHeader { 166 AP4_AvcSliceHeader(); 167 168 unsigned int size; // not from the bitstream, this is computed after parsing 169 170 unsigned int first_mb_in_slice; 171 unsigned int slice_type; 172 unsigned int pic_parameter_set_id; 173 unsigned int colour_plane_id; 174 unsigned int frame_num; 175 unsigned int field_pic_flag; 176 unsigned int bottom_field_flag; 177 unsigned int idr_pic_id; 178 unsigned int pic_order_cnt_lsb; 179 int delta_pic_order_cnt[2]; 180 unsigned int redundant_pic_cnt; 181 unsigned int direct_spatial_mv_pred_flag; 182 unsigned int num_ref_idx_active_override_flag; 183 unsigned int num_ref_idx_l0_active_minus1; 184 unsigned int num_ref_idx_l1_active_minus1; 185 unsigned int ref_pic_list_reordering_flag_l0; 186 unsigned int reordering_of_pic_nums_idc; 187 unsigned int abs_diff_pic_num_minus1; 188 unsigned int long_term_pic_num; 189 unsigned int ref_pic_list_reordering_flag_l1; 190 unsigned int luma_log2_weight_denom; 191 unsigned int chroma_log2_weight_denom; 192 unsigned int cabac_init_idc; 193 unsigned int slice_qp_delta; 194 unsigned int sp_for_switch_flag; 195 int slice_qs_delta; 196 unsigned int disable_deblocking_filter_idc; 197 int slice_alpha_c0_offset_div2; 198 int slice_beta_offset_div2; 199 unsigned int slice_group_change_cycle; 200 unsigned int no_output_of_prior_pics_flag; 201 unsigned int long_term_reference_flag; 202 unsigned int difference_of_pic_nums_minus1; 203 unsigned int long_term_frame_idx; 204 unsigned int max_long_term_frame_idx_plus1; 205 }; 206 207 /*---------------------------------------------------------------------- 208 | AP4_AvcNalParser 209 +---------------------------------------------------------------------*/ 210 class AP4_AvcNalParser : public AP4_NalParser { 211 public: 212 static const char* NaluTypeName(unsigned int nalu_type); 213 static const char* PrimaryPicTypeName(unsigned int primary_pic_type); 214 static const char* SliceTypeName(unsigned int slice_type); 215 216 AP4_AvcNalParser(); 217 }; 218 219 /*---------------------------------------------------------------------- 220 | AP4_AvcFrameParser 221 +---------------------------------------------------------------------*/ 222 class AP4_AvcFrameParser { 223 public: 224 // types 225 struct AccessUnitInfo { 226 AP4_Array<AP4_DataBuffer*> nal_units; 227 bool is_idr; 228 AP4_UI32 decode_order; 229 AP4_UI32 display_order; 230 231 void Reset(); 232 }; 233 234 // methods 235 AP4_AvcFrameParser(); 236 ~AP4_AvcFrameParser(); 237 238 /** 239 * Feed some data to the parser and look for the next NAL Unit. 240 * 241 * @param data Pointer to the memory buffer with the data to feed. 242 * @param data_size Size in bytes of the buffer pointed to by the 243 * data pointer. 244 * @param bytes_consumed Number of bytes from the data buffer that were 245 * consumed and stored by the parser. 246 * @param access_unit_info Reference to a AccessUnitInfo structure that will 247 * contain information about any access unit found in the data. If no 248 * access unit was found, the nal_units field of this structure will be an 249 * empty array. 250 * @param eos Boolean flag that indicates if this buffer is the last 251 * buffer in the stream/file (End Of Stream). 252 * 253 * @result: AP4_SUCCESS is the call succeeds, or an error code if it 254 * fails. 255 * 256 * The caller must not feed the same data twice. When this method 257 * returns, the caller should inspect the value of bytes_consumed and 258 * advance the input stream source accordingly, such that the next 259 * buffer passed to this method will be exactly bytes_consumed bytes 260 * after what was passed in this call. After all the input data has 261 * been supplied to the parser (eos=true), the caller also should 262 * this method with an empty buffer (data=NULL and/or data_size=0) until 263 * no more data is returned, because there may be buffered data still 264 * available. 265 * 266 * When data is returned in the access_unit_info structure, the caller is 267 * responsible for freeing this data by calling AccessUnitInfo::Reset() 268 */ 269 AP4_Result Feed(const void* data, 270 AP4_Size data_size, 271 AP4_Size& bytes_consumed, 272 AccessUnitInfo& access_unit_info, 273 bool eos=false); 274 275 AP4_Result Feed(const AP4_UI08* nal_unit, 276 AP4_Size nal_unit_size, 277 AccessUnitInfo& access_unit_info, 278 bool last_unit=false); 279 GetSequenceParameterSets()280 AP4_AvcSequenceParameterSet** GetSequenceParameterSets() { return &m_SPS[0]; } GetPictureParameterSets()281 AP4_AvcPictureParameterSet** GetPictureParameterSets() { return &m_PPS[0]; } GetSliceHeader()282 const AP4_AvcSliceHeader* GetSliceHeader() { return m_SliceHeader; } 283 284 AP4_Result ParseSPS(const unsigned char* data, 285 unsigned int data_size, 286 AP4_AvcSequenceParameterSet& sps); 287 AP4_Result ParsePPS(const unsigned char* data, 288 unsigned int data_size, 289 AP4_AvcPictureParameterSet& pps); 290 AP4_Result ParseSliceHeader(const AP4_UI08* data, 291 unsigned int data_size, 292 unsigned int nal_unit_type, 293 unsigned int nal_ref_idc, 294 AP4_AvcSliceHeader& slice_header); 295 296 private: 297 // methods 298 bool SameFrame(unsigned int nal_unit_type_1, unsigned int nal_ref_idc_1, AP4_AvcSliceHeader& sh1, 299 unsigned int nal_unit_type_2, unsigned int nal_ref_idc_2, AP4_AvcSliceHeader& sh2); 300 AP4_AvcSequenceParameterSet* GetSliceSPS(AP4_AvcSliceHeader& sh); 301 void CheckIfAccessUnitIsCompleted(AccessUnitInfo& access_unit_info); 302 void AppendNalUnitData(const unsigned char* data, unsigned int data_size); 303 304 // members 305 AP4_AvcNalParser m_NalParser; 306 AP4_AvcSequenceParameterSet* m_SPS[AP4_AVC_SPS_MAX_ID+1]; 307 AP4_AvcPictureParameterSet* m_PPS[AP4_AVC_PPS_MAX_ID+1]; 308 309 // only updated on new VLC NAL Units 310 unsigned int m_NalUnitType; 311 unsigned int m_NalRefIdc; 312 AP4_AvcSliceHeader* m_SliceHeader; 313 unsigned int m_AccessUnitVclNalUnitCount; 314 315 // accumulator for NAL unit data 316 unsigned int m_TotalNalUnitCount; 317 unsigned int m_TotalAccessUnitCount; 318 AP4_Array<AP4_DataBuffer*> m_AccessUnitData; 319 320 // used to keep track of picture order count 321 unsigned int m_PrevFrameNum; 322 unsigned int m_PrevFrameNumOffset; 323 int m_PrevPicOrderCntMsb; 324 unsigned int m_PrevPicOrderCntLsb; 325 }; 326 327 #endif // _AP4_AVC_PARSER_H_ 328