1 // Copyright 2015 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 H265 Annex-B video stream parser. 6 7 #ifndef MEDIA_VIDEO_H265_PARSER_H_ 8 #define MEDIA_VIDEO_H265_PARSER_H_ 9 10 #include <stdint.h> 11 #include <sys/types.h> 12 13 #include <map> 14 #include <vector> 15 16 #include "base/macros.h" 17 #include "media/base/media_export.h" 18 #include "media/base/ranges.h" 19 #include "media/video/h264_bit_reader.h" 20 #include "media/video/h264_parser.h" 21 22 namespace media { 23 24 struct SubsampleEntry; 25 26 // For explanations of each struct and its members, see H.265 specification 27 // at http://www.itu.int/rec/T-REC-H.265. 28 struct MEDIA_EXPORT H265NALU { 29 H265NALU(); 30 31 // NAL Unit types are taken from Table 7-1 of HEVC/H265 standard 32 // http://www.itu.int/rec/T-REC-H.265-201410-I/en 33 enum Type { 34 TRAIL_N = 0, 35 TRAIL_R = 1, 36 TSA_N = 2, 37 TSA_R = 3, 38 STSA_N = 4, 39 STSA_R = 5, 40 RADL_N = 6, 41 RADL_R = 7, 42 RASL_N = 8, 43 RASL_R = 9, 44 RSV_VCL_N10 = 10, 45 RSV_VCL_R11 = 11, 46 RSV_VCL_N12 = 12, 47 RSV_VCL_R13 = 13, 48 RSV_VCL_N14 = 14, 49 RSV_VCL_R15 = 15, 50 BLA_W_LP = 16, 51 BLA_W_RADL = 17, 52 BLA_N_LP = 18, 53 IDR_W_RADL = 19, 54 IDR_N_LP = 20, 55 CRA_NUT = 21, 56 RSV_IRAP_VCL22 = 22, 57 RSV_IRAP_VCL23 = 23, 58 RSV_VCL24 = 24, 59 RSV_VCL25 = 25, 60 RSV_VCL26 = 26, 61 RSV_VCL27 = 27, 62 RSV_VCL28 = 28, 63 RSV_VCL29 = 29, 64 RSV_VCL30 = 30, 65 RSV_VCL31 = 31, 66 VPS_NUT = 32, 67 SPS_NUT = 33, 68 PPS_NUT = 34, 69 AUD_NUT = 35, 70 EOS_NUT = 36, 71 EOB_NUT = 37, 72 FD_NUT = 38, 73 PREFIX_SEI_NUT = 39, 74 SUFFIX_SEI_NUT = 40, 75 RSV_NVCL41 = 41, 76 RSV_NVCL42 = 42, 77 RSV_NVCL43 = 43, 78 RSV_NVCL44 = 44, 79 RSV_NVCL45 = 45, 80 RSV_NVCL46 = 46, 81 RSV_NVCL47 = 47, 82 UNSPEC48 = 48, 83 UNSPEC49 = 49, 84 UNSPEC50 = 50, 85 UNSPEC51 = 51, 86 UNSPEC52 = 52, 87 UNSPEC53 = 53, 88 UNSPEC54 = 54, 89 UNSPEC55 = 55, 90 UNSPEC56 = 56, 91 UNSPEC57 = 57, 92 UNSPEC58 = 58, 93 UNSPEC59 = 59, 94 UNSPEC60 = 60, 95 UNSPEC61 = 61, 96 UNSPEC62 = 62, 97 UNSPEC63 = 63, 98 }; 99 100 // After (without) start code; we don't own the underlying memory 101 // and a shallow copy should be made when copying this struct. 102 const uint8_t* data; 103 off_t size; // From after start code to start code of next NALU (or EOS). 104 105 int nal_unit_type; 106 int nuh_layer_id; 107 int nuh_temporal_id_plus1; 108 }; 109 110 // Class to parse an Annex-B H.265 stream. 111 class MEDIA_EXPORT H265Parser { 112 public: 113 enum Result { 114 kOk, 115 kInvalidStream, // error in stream 116 kUnsupportedStream, // stream not supported by the parser 117 kEOStream, // end of stream 118 }; 119 120 H265Parser(); 121 ~H265Parser(); 122 123 void Reset(); 124 // Set current stream pointer to |stream| of |stream_size| in bytes, 125 // |stream| owned by caller. 126 // |subsamples| contains information about what parts of |stream| are 127 // encrypted. 128 void SetStream(const uint8_t* stream, off_t stream_size); 129 void SetEncryptedStream(const uint8_t* stream, 130 off_t stream_size, 131 const std::vector<SubsampleEntry>& subsamples); 132 133 // Read the stream to find the next NALU, identify it and return 134 // that information in |*nalu|. This advances the stream to the beginning 135 // of this NALU, but not past it, so subsequent calls to NALU-specific 136 // parsing functions (ParseSPS, etc.) will parse this NALU. 137 // If the caller wishes to skip the current NALU, it can call this function 138 // again, instead of any NALU-type specific parse functions below. 139 Result AdvanceToNextNALU(H265NALU* nalu); 140 141 private: 142 // Move the stream pointer to the beginning of the next NALU, 143 // i.e. pointing at the next start code. 144 // Return true if a NALU has been found. 145 // If a NALU is found: 146 // - its size in bytes is returned in |*nalu_size| and includes 147 // the start code as well as the trailing zero bits. 148 // - the size in bytes of the start code is returned in |*start_code_size|. 149 bool LocateNALU(off_t* nalu_size, off_t* start_code_size); 150 151 // Pointer to the current NALU in the stream. 152 const uint8_t* stream_; 153 154 // Bytes left in the stream after the current NALU. 155 off_t bytes_left_; 156 157 H264BitReader br_; 158 159 // Ranges of encrypted bytes in the buffer passed to 160 // SetEncryptedStream(). 161 Ranges<const uint8_t*> encrypted_ranges_; 162 163 DISALLOW_COPY_AND_ASSIGN(H265Parser); 164 }; 165 166 } // namespace media 167 168 #endif // MEDIA_VIDEO_H265_PARSER_H_ 169