DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
es_parser_h265.cc
1 // Copyright 2016 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/formats/mp2t/es_parser_h265.h"
8 
9 #include <stdint.h>
10 
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"
20 
21 namespace shaka {
22 namespace media {
23 namespace mp2t {
24 
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()),
31  pid,
32  emit_sample_cb),
33  new_stream_info_cb_(new_stream_info_cb),
34  decoder_config_check_pending_(false),
35  h265_parser_(new H265Parser()) {}
36 
37 EsParserH265::~EsParserH265() {}
38 
39 void EsParserH265::Reset() {
40  DVLOG(1) << "EsParserH265::Reset";
41  h265_parser_.reset(new H265Parser());
42  last_video_decoder_config_ = scoped_refptr<StreamInfo>();
43  decoder_config_check_pending_ = false;
44  EsParserH26x::Reset();
45 }
46 
47 bool EsParserH265::ProcessNalu(const Nalu& nalu,
48  bool* is_key_frame,
49  int* pps_id_for_access_unit) {
50  switch (nalu.type()) {
51  case Nalu::H265_AUD: {
52  DVLOG(LOG_LEVEL_ES) << "Nalu: AUD";
53  break;
54  }
55  case Nalu::H265_SPS: {
56  DVLOG(LOG_LEVEL_ES) << "Nalu: SPS";
57  int sps_id;
58  if (h265_parser_->ParseSps(nalu, &sps_id) != H265Parser::kOk)
59  return false;
60  decoder_config_check_pending_ = true;
61  break;
62  }
63  case Nalu::H265_PPS: {
64  DVLOG(LOG_LEVEL_ES) << "Nalu: PPS";
65  int pps_id;
66  if (h265_parser_->ParsePps(nalu, &pps_id) != H265Parser::kOk) {
67  // Allow PPS parsing to fail if waiting for SPS.
68  if (last_video_decoder_config_)
69  return false;
70  } else {
71  decoder_config_check_pending_ = true;
72  }
73  break;
74  }
75  default: {
76  if (nalu.is_video_slice()) {
77  *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;
80  H265SliceHeader shdr;
81  if (h265_parser_->ParseSliceHeader(nalu, &shdr) != H265Parser::kOk) {
82  // Only accept an invalid SPS/PPS at the beginning when the stream
83  // does not necessarily start with an SPS/PPS/IDR.
84  if (last_video_decoder_config_)
85  return false;
86  } else {
87  *pps_id_for_access_unit = shdr.pic_parameter_set_id;
88  }
89  } else {
90  DVLOG(LOG_LEVEL_ES) << "Nalu: " << nalu.type();
91  }
92  }
93  }
94 
95  return true;
96 }
97 
98 bool EsParserH265::UpdateVideoDecoderConfig(int pps_id) {
99  // Update the video decoder configuration if needed.
100  if (!decoder_config_check_pending_)
101  return true;
102 
103  const H265Pps* pps = h265_parser_->GetPps(pps_id);
104  const H265Sps* sps;
105  if (!pps) {
106  // Only accept an invalid PPS at the beginning when the stream
107  // does not necessarily start with an SPS/PPS/IDR.
108  // In this case, the initial frames are conveyed to the upper layer with
109  // an invalid VideoDecoderConfig and it's up to the upper layer
110  // to process this kind of frame accordingly.
111  return last_video_decoder_config_ == nullptr;
112  } else {
113  sps = h265_parser_->GetSps(pps->seq_parameter_set_id);
114  if (!sps)
115  return false;
116  decoder_config_check_pending_ = false;
117  }
118 
119  std::vector<uint8_t> decoder_config_record;
120  HEVCDecoderConfigurationRecord decoder_config;
121  if (!stream_converter()->GetDecoderConfigurationRecord(
122  &decoder_config_record) ||
123  !decoder_config.Parse(decoder_config_record)) {
124  DLOG(ERROR) << "Failure to construct an HEVCDecoderConfigurationRecord";
125  return false;
126  }
127 
128  if (last_video_decoder_config_) {
129  if (last_video_decoder_config_->codec_config() != decoder_config_record) {
130  // Video configuration has changed. Issue warning.
131  // TODO(tinskip): Check the nature of the configuration change. Only
132  // minor configuration changes (such as frame ordering) can be handled
133  // gracefully by decoders without notification. Major changes (such as
134  // video resolution changes) should be treated as errors.
135  LOG(WARNING) << "H.265 decoder configuration has changed.";
136  last_video_decoder_config_->set_codec_config(decoder_config_record);
137  }
138  return true;
139  }
140 
141  uint32_t coded_width = 0;
142  uint32_t coded_height = 0;
143  uint32_t pixel_width = 0;
144  uint32_t pixel_height = 0;
145  if (!ExtractResolutionFromSps(*sps, &coded_width, &coded_height, &pixel_width,
146  &pixel_height)) {
147  LOG(ERROR) << "Failed to parse SPS.";
148  return false;
149  }
150 
151  last_video_decoder_config_ = scoped_refptr<StreamInfo>(new VideoStreamInfo(
152  pid(), kMpeg2Timescale, kInfiniteDuration, kCodecHVC1,
153  decoder_config.GetCodecString(kCodecHVC1), decoder_config_record.data(),
154  decoder_config_record.size(), coded_width, coded_height, pixel_width,
155  pixel_height, 0, H26xByteToUnitStreamConverter::kUnitStreamNaluLengthSize,
156  std::string(), false));
157 
158  // Video config notification.
159  new_stream_info_cb_.Run(last_video_decoder_config_);
160 
161  return true;
162 }
163 
164 } // namespace mp2t
165 } // namespace media
166 } // namespace shaka