7 #include "packager/media/formats/mp2t/es_parser_h265.h"
11 #include "packager/base/logging.h"
12 #include "packager/media/base/media_sample.h"
13 #include "packager/media/base/offset_byte_queue.h"
14 #include "packager/media/base/timestamp.h"
15 #include "packager/media/base/video_stream_info.h"
16 #include "packager/media/codecs/h265_byte_to_unit_stream_converter.h"
17 #include "packager/media/codecs/h265_parser.h"
18 #include "packager/media/codecs/hevc_decoder_configuration_record.h"
19 #include "packager/media/formats/mp2t/mp2t_common.h"
25 EsParserH265::EsParserH265(uint32_t pid,
26 const NewStreamInfoCB& new_stream_info_cb,
27 const EmitSampleCB& emit_sample_cb)
28 : EsParserH26x(Nalu::kH265,
29 std::unique_ptr<H26xByteToUnitStreamConverter>(
30 new H265ByteToUnitStreamConverter()),
33 new_stream_info_cb_(new_stream_info_cb),
34 decoder_config_check_pending_(false),
35 h265_parser_(new H265Parser()) {}
37 EsParserH265::~EsParserH265() {}
39 void EsParserH265::Reset() {
40 DVLOG(1) <<
"EsParserH265::Reset";
41 h265_parser_.reset(
new H265Parser());
42 last_video_decoder_config_ = std::shared_ptr<StreamInfo>();
43 decoder_config_check_pending_ =
false;
44 EsParserH26x::Reset();
47 bool EsParserH265::ProcessNalu(
const Nalu& nalu,
48 VideoSliceInfo* video_slice_info) {
49 video_slice_info->valid =
false;
50 switch (nalu.type()) {
51 case Nalu::H265_AUD: {
52 DVLOG(LOG_LEVEL_ES) <<
"Nalu: AUD";
55 case Nalu::H265_SPS: {
56 DVLOG(LOG_LEVEL_ES) <<
"Nalu: SPS";
58 if (h265_parser_->ParseSps(nalu, &sps_id) != H265Parser::kOk)
60 decoder_config_check_pending_ =
true;
63 case Nalu::H265_PPS: {
64 DVLOG(LOG_LEVEL_ES) <<
"Nalu: PPS";
66 if (h265_parser_->ParsePps(nalu, &pps_id) != H265Parser::kOk) {
68 if (last_video_decoder_config_)
71 decoder_config_check_pending_ =
true;
76 if (nalu.is_vcl() && nalu.nuh_layer_id() == 0) {
77 const bool is_key_frame = nalu.type() == Nalu::H265_IDR_W_RADL ||
78 nalu.type() == Nalu::H265_IDR_N_LP;
79 DVLOG(LOG_LEVEL_ES) <<
"Nalu: slice KeyFrame=" << is_key_frame;
81 if (h265_parser_->ParseSliceHeader(nalu, &shdr) != H265Parser::kOk) {
84 if (last_video_decoder_config_)
87 video_slice_info->valid =
true;
88 video_slice_info->is_key_frame = is_key_frame;
89 video_slice_info->frame_num = 0;
90 video_slice_info->pps_id = shdr.pic_parameter_set_id;
93 DVLOG(LOG_LEVEL_ES) <<
"Nalu: " << nalu.type();
101 bool EsParserH265::UpdateVideoDecoderConfig(
int pps_id) {
103 if (!decoder_config_check_pending_)
106 const H265Pps* pps = h265_parser_->GetPps(pps_id);
114 return last_video_decoder_config_ ==
nullptr;
116 sps = h265_parser_->GetSps(pps->seq_parameter_set_id);
119 decoder_config_check_pending_ =
false;
122 std::vector<uint8_t> decoder_config_record;
123 HEVCDecoderConfigurationRecord decoder_config;
124 if (!stream_converter()->GetDecoderConfigurationRecord(
125 &decoder_config_record) ||
126 !decoder_config.Parse(decoder_config_record)) {
127 DLOG(ERROR) <<
"Failure to construct an HEVCDecoderConfigurationRecord";
131 if (last_video_decoder_config_) {
132 if (last_video_decoder_config_->codec_config() != decoder_config_record) {
138 LOG(WARNING) <<
"H.265 decoder configuration has changed.";
139 last_video_decoder_config_->set_codec_config(decoder_config_record);
144 uint32_t coded_width = 0;
145 uint32_t coded_height = 0;
146 uint32_t pixel_width = 0;
147 uint32_t pixel_height = 0;
148 if (!ExtractResolutionFromSps(*sps, &coded_width, &coded_height, &pixel_width,
150 LOG(ERROR) <<
"Failed to parse SPS.";
154 const uint8_t nalu_length_size =
155 H26xByteToUnitStreamConverter::kUnitStreamNaluLengthSize;
156 const H26xStreamFormat stream_format = stream_converter()->stream_format();
157 const FourCC codec_fourcc =
158 stream_format == H26xStreamFormat::kNalUnitStreamWithParameterSetNalus
161 last_video_decoder_config_ = std::make_shared<VideoStreamInfo>(
162 pid(), kMpeg2Timescale, kInfiniteDuration, kCodecH265, stream_format,
163 decoder_config.GetCodecString(codec_fourcc), decoder_config_record.data(),
164 decoder_config_record.size(), coded_width, coded_height, pixel_width,
165 pixel_height, 0, nalu_length_size, std::string(),
false);
168 new_stream_info_cb_.Run(last_video_decoder_config_);