2016-03-31 19:48:16 +00:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
#include "packager/media/formats/mp2t/es_parser_h265.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/media/base/media_sample.h"
|
|
|
|
#include "packager/media/base/offset_byte_queue.h"
|
|
|
|
#include "packager/media/base/timestamp.h"
|
|
|
|
#include "packager/media/base/video_stream_info.h"
|
2016-05-25 18:03:17 +00:00
|
|
|
#include "packager/media/codecs/h265_byte_to_unit_stream_converter.h"
|
|
|
|
#include "packager/media/codecs/h265_parser.h"
|
|
|
|
#include "packager/media/codecs/hevc_decoder_configuration_record.h"
|
2016-03-31 19:48:16 +00:00
|
|
|
#include "packager/media/formats/mp2t/mp2t_common.h"
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-03-31 19:48:16 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp2t {
|
|
|
|
|
|
|
|
EsParserH265::EsParserH265(uint32_t pid,
|
|
|
|
const NewStreamInfoCB& new_stream_info_cb,
|
|
|
|
const EmitSampleCB& emit_sample_cb)
|
2016-05-04 21:19:40 +00:00
|
|
|
: EsParserH26x(Nalu::kH265,
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<H26xByteToUnitStreamConverter>(
|
2016-05-04 21:19:40 +00:00
|
|
|
new H265ByteToUnitStreamConverter()),
|
|
|
|
pid,
|
|
|
|
emit_sample_cb),
|
2016-03-31 19:48:16 +00:00
|
|
|
new_stream_info_cb_(new_stream_info_cb),
|
|
|
|
decoder_config_check_pending_(false),
|
|
|
|
h265_parser_(new H265Parser()) {}
|
|
|
|
|
|
|
|
EsParserH265::~EsParserH265() {}
|
|
|
|
|
|
|
|
void EsParserH265::Reset() {
|
|
|
|
DVLOG(1) << "EsParserH265::Reset";
|
|
|
|
h265_parser_.reset(new H265Parser());
|
2017-01-24 00:55:02 +00:00
|
|
|
last_video_decoder_config_ = std::shared_ptr<StreamInfo>();
|
2016-03-31 19:48:16 +00:00
|
|
|
decoder_config_check_pending_ = false;
|
|
|
|
EsParserH26x::Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EsParserH265::ProcessNalu(const Nalu& nalu,
|
2016-08-25 22:44:25 +00:00
|
|
|
VideoSliceInfo* video_slice_info) {
|
|
|
|
video_slice_info->valid = false;
|
2016-03-31 19:48:16 +00:00
|
|
|
switch (nalu.type()) {
|
|
|
|
case Nalu::H265_AUD: {
|
|
|
|
DVLOG(LOG_LEVEL_ES) << "Nalu: AUD";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Nalu::H265_SPS: {
|
|
|
|
DVLOG(LOG_LEVEL_ES) << "Nalu: SPS";
|
|
|
|
int sps_id;
|
|
|
|
if (h265_parser_->ParseSps(nalu, &sps_id) != H265Parser::kOk)
|
|
|
|
return false;
|
|
|
|
decoder_config_check_pending_ = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Nalu::H265_PPS: {
|
|
|
|
DVLOG(LOG_LEVEL_ES) << "Nalu: PPS";
|
|
|
|
int pps_id;
|
|
|
|
if (h265_parser_->ParsePps(nalu, &pps_id) != H265Parser::kOk) {
|
|
|
|
// Allow PPS parsing to fail if waiting for SPS.
|
|
|
|
if (last_video_decoder_config_)
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
decoder_config_check_pending_ = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2017-03-01 02:10:28 +00:00
|
|
|
if (nalu.is_vcl() && nalu.nuh_layer_id() == 0) {
|
2016-08-25 22:44:25 +00:00
|
|
|
const bool is_key_frame = nalu.type() == Nalu::H265_IDR_W_RADL ||
|
|
|
|
nalu.type() == Nalu::H265_IDR_N_LP;
|
2016-03-31 19:48:16 +00:00
|
|
|
DVLOG(LOG_LEVEL_ES) << "Nalu: slice KeyFrame=" << is_key_frame;
|
|
|
|
H265SliceHeader shdr;
|
|
|
|
if (h265_parser_->ParseSliceHeader(nalu, &shdr) != H265Parser::kOk) {
|
|
|
|
// Only accept an invalid SPS/PPS at the beginning when the stream
|
|
|
|
// does not necessarily start with an SPS/PPS/IDR.
|
|
|
|
if (last_video_decoder_config_)
|
|
|
|
return false;
|
|
|
|
} else {
|
2016-08-25 22:44:25 +00:00
|
|
|
video_slice_info->valid = true;
|
|
|
|
video_slice_info->is_key_frame = is_key_frame;
|
|
|
|
video_slice_info->frame_num = 0; // frame_num is only for H264.
|
|
|
|
video_slice_info->pps_id = shdr.pic_parameter_set_id;
|
2016-03-31 19:48:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DVLOG(LOG_LEVEL_ES) << "Nalu: " << nalu.type();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EsParserH265::UpdateVideoDecoderConfig(int pps_id) {
|
|
|
|
// Update the video decoder configuration if needed.
|
|
|
|
if (!decoder_config_check_pending_)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const H265Pps* pps = h265_parser_->GetPps(pps_id);
|
|
|
|
const H265Sps* sps;
|
|
|
|
if (!pps) {
|
|
|
|
// Only accept an invalid PPS at the beginning when the stream
|
|
|
|
// does not necessarily start with an SPS/PPS/IDR.
|
|
|
|
// In this case, the initial frames are conveyed to the upper layer with
|
|
|
|
// an invalid VideoDecoderConfig and it's up to the upper layer
|
|
|
|
// to process this kind of frame accordingly.
|
|
|
|
return last_video_decoder_config_ == nullptr;
|
|
|
|
} else {
|
|
|
|
sps = h265_parser_->GetSps(pps->seq_parameter_set_id);
|
|
|
|
if (!sps)
|
|
|
|
return false;
|
|
|
|
decoder_config_check_pending_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint8_t> decoder_config_record;
|
2016-05-25 17:33:53 +00:00
|
|
|
HEVCDecoderConfigurationRecord decoder_config;
|
2016-03-31 19:48:16 +00:00
|
|
|
if (!stream_converter()->GetDecoderConfigurationRecord(
|
|
|
|
&decoder_config_record) ||
|
|
|
|
!decoder_config.Parse(decoder_config_record)) {
|
|
|
|
DLOG(ERROR) << "Failure to construct an HEVCDecoderConfigurationRecord";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_video_decoder_config_) {
|
2016-06-27 19:30:32 +00:00
|
|
|
if (last_video_decoder_config_->codec_config() != decoder_config_record) {
|
2016-03-31 19:48:16 +00:00
|
|
|
// Video configuration has changed. Issue warning.
|
|
|
|
// TODO(tinskip): Check the nature of the configuration change. Only
|
|
|
|
// minor configuration changes (such as frame ordering) can be handled
|
|
|
|
// gracefully by decoders without notification. Major changes (such as
|
|
|
|
// video resolution changes) should be treated as errors.
|
|
|
|
LOG(WARNING) << "H.265 decoder configuration has changed.";
|
2016-06-27 19:30:32 +00:00
|
|
|
last_video_decoder_config_->set_codec_config(decoder_config_record);
|
2016-03-31 19:48:16 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t coded_width = 0;
|
|
|
|
uint32_t coded_height = 0;
|
|
|
|
uint32_t pixel_width = 0;
|
|
|
|
uint32_t pixel_height = 0;
|
|
|
|
if (!ExtractResolutionFromSps(*sps, &coded_width, &coded_height, &pixel_width,
|
|
|
|
&pixel_height)) {
|
|
|
|
LOG(ERROR) << "Failed to parse SPS.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-24 00:55:02 +00:00
|
|
|
const uint8_t nalu_length_size =
|
|
|
|
H26xByteToUnitStreamConverter::kUnitStreamNaluLengthSize;
|
2017-03-23 18:34:20 +00:00
|
|
|
const H26xStreamFormat stream_format = stream_converter()->stream_format();
|
|
|
|
const FourCC codec_fourcc =
|
|
|
|
stream_format == H26xStreamFormat::kNalUnitStreamWithParameterSetNalus
|
|
|
|
? FOURCC_hev1
|
|
|
|
: FOURCC_hvc1;
|
2017-01-24 00:55:02 +00:00
|
|
|
last_video_decoder_config_ = std::make_shared<VideoStreamInfo>(
|
2017-03-23 18:34:20 +00:00
|
|
|
pid(), kMpeg2Timescale, kInfiniteDuration, kCodecH265, stream_format,
|
|
|
|
decoder_config.GetCodecString(codec_fourcc), decoder_config_record.data(),
|
2016-07-27 00:51:08 +00:00
|
|
|
decoder_config_record.size(), coded_width, coded_height, pixel_width,
|
2017-01-24 00:55:02 +00:00
|
|
|
pixel_height, 0, nalu_length_size, std::string(), false);
|
2016-03-31 19:48:16 +00:00
|
|
|
|
|
|
|
// Video config notification.
|
|
|
|
new_stream_info_cb_.Run(last_video_decoder_config_);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp2t
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|