2014-04-01 01:34:59 +00:00
|
|
|
// Copyright 2014 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/media/formats/mp2t/es_parser_h264.h>
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <cstdint>
|
2014-09-30 23:52:58 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <absl/log/log.h>
|
|
|
|
|
|
|
|
#include <packager/macros/logging.h>
|
|
|
|
#include <packager/media/base/media_sample.h>
|
|
|
|
#include <packager/media/base/timestamp.h>
|
|
|
|
#include <packager/media/base/video_stream_info.h>
|
|
|
|
#include <packager/media/codecs/avc_decoder_configuration_record.h>
|
|
|
|
#include <packager/media/codecs/h264_byte_to_unit_stream_converter.h>
|
|
|
|
#include <packager/media/codecs/h264_parser.h>
|
|
|
|
#include <packager/media/formats/mp2t/mp2t_common.h>
|
2014-04-08 00:39:14 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-04-01 01:34:59 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp2t {
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
EsParserH264::EsParserH264(uint32_t pid,
|
|
|
|
const NewStreamInfoCB& new_stream_info_cb,
|
|
|
|
const EmitSampleCB& emit_sample_cb)
|
2016-05-04 21:19:40 +00:00
|
|
|
: EsParserH26x(Nalu::kH264,
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<H26xByteToUnitStreamConverter>(
|
2016-05-04 21:19:40 +00:00
|
|
|
new H264ByteToUnitStreamConverter()),
|
|
|
|
pid,
|
|
|
|
emit_sample_cb),
|
2014-04-10 19:57:10 +00:00
|
|
|
new_stream_info_cb_(new_stream_info_cb),
|
2014-04-18 01:57:31 +00:00
|
|
|
decoder_config_check_pending_(false),
|
2016-03-31 19:48:16 +00:00
|
|
|
h264_parser_(new H264Parser()) {}
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2016-03-31 19:48:16 +00:00
|
|
|
EsParserH264::~EsParserH264() {}
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
void EsParserH264::Reset() {
|
|
|
|
DVLOG(1) << "EsParserH264::Reset";
|
|
|
|
h264_parser_.reset(new H264Parser());
|
2017-01-24 00:55:02 +00:00
|
|
|
last_video_decoder_config_ = std::shared_ptr<StreamInfo>();
|
2014-04-18 01:57:31 +00:00
|
|
|
decoder_config_check_pending_ = false;
|
2016-03-31 19:48:16 +00:00
|
|
|
EsParserH26x::Reset();
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 19:48:16 +00:00
|
|
|
bool EsParserH264::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::H264_AUD: {
|
|
|
|
DVLOG(LOG_LEVEL_ES) << "Nalu: AUD";
|
2014-04-01 01:34:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-03-31 19:48:16 +00:00
|
|
|
case Nalu::H264_SPS: {
|
|
|
|
DVLOG(LOG_LEVEL_ES) << "Nalu: SPS";
|
|
|
|
int sps_id;
|
2020-10-06 23:07:51 +00:00
|
|
|
auto status = h264_parser_->ParseSps(nalu, &sps_id);
|
|
|
|
if (status == H264Parser::kOk)
|
|
|
|
decoder_config_check_pending_ = true;
|
|
|
|
else if (status == H264Parser::kUnsupportedStream)
|
|
|
|
// Indicate the stream can't be parsed.
|
2023-12-01 17:32:19 +00:00
|
|
|
new_stream_info_cb_(nullptr);
|
2020-10-06 23:07:51 +00:00
|
|
|
else
|
2016-03-31 19:48:16 +00:00
|
|
|
return false;
|
2014-04-01 01:34:59 +00:00
|
|
|
break;
|
2016-03-31 19:48:16 +00:00
|
|
|
}
|
|
|
|
case Nalu::H264_PPS: {
|
|
|
|
DVLOG(LOG_LEVEL_ES) << "Nalu: PPS";
|
|
|
|
int pps_id;
|
2020-10-06 23:07:51 +00:00
|
|
|
auto status = h264_parser_->ParsePps(nalu, &pps_id);
|
|
|
|
if (status == H264Parser::kOk) {
|
|
|
|
decoder_config_check_pending_ = true;
|
|
|
|
} else if (status == H264Parser::kUnsupportedStream) {
|
|
|
|
// Indicate the stream can't be parsed.
|
2023-12-01 17:32:19 +00:00
|
|
|
new_stream_info_cb_(nullptr);
|
2020-10-06 23:07:51 +00:00
|
|
|
} else {
|
2016-03-31 19:48:16 +00:00
|
|
|
// Allow PPS parsing to fail if waiting for SPS.
|
|
|
|
if (last_video_decoder_config_)
|
2014-04-01 01:34:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-31 19:48:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Nalu::H264_IDRSlice:
|
|
|
|
case Nalu::H264_NonIDRSlice: {
|
2016-08-25 22:44:25 +00:00
|
|
|
const bool is_key_frame = (nalu.type() == Nalu::H264_IDRSlice);
|
2016-03-31 19:48:16 +00:00
|
|
|
DVLOG(LOG_LEVEL_ES) << "Nalu: slice IDR=" << is_key_frame;
|
|
|
|
H264SliceHeader shdr;
|
2020-10-06 23:07:51 +00:00
|
|
|
auto status = h264_parser_->ParseSliceHeader(nalu, &shdr);
|
|
|
|
if (status == H264Parser::kOk) {
|
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 = shdr.frame_num;
|
|
|
|
video_slice_info->pps_id = shdr.pic_parameter_set_id;
|
2020-10-06 23:07:51 +00:00
|
|
|
} else if (status == H264Parser::kUnsupportedStream) {
|
|
|
|
// Indicate the stream can't be parsed.
|
2023-12-01 17:32:19 +00:00
|
|
|
new_stream_info_cb_(nullptr);
|
2020-10-06 23:07:51 +00:00
|
|
|
} else {
|
|
|
|
// 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;
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
2016-03-31 19:48:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
DVLOG(LOG_LEVEL_ES) << "Nalu: " << nalu.type();
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-31 19:48:16 +00:00
|
|
|
bool EsParserH264::UpdateVideoDecoderConfig(int pps_id) {
|
|
|
|
// Update the video decoder configuration if needed.
|
|
|
|
if (!decoder_config_check_pending_)
|
|
|
|
return true;
|
2014-04-18 01:57:31 +00:00
|
|
|
|
2016-03-31 19:48:16 +00:00
|
|
|
const H264Pps* pps = h264_parser_->GetPps(pps_id);
|
|
|
|
const H264Sps* 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 = h264_parser_->GetSps(pps->seq_parameter_set_id);
|
|
|
|
if (!sps)
|
|
|
|
return false;
|
|
|
|
decoder_config_check_pending_ = false;
|
2014-04-18 01:57:31 +00:00
|
|
|
}
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> decoder_config_record;
|
2016-03-31 19:48:16 +00:00
|
|
|
if (!stream_converter()->GetDecoderConfigurationRecord(
|
2014-04-18 01:57:31 +00:00
|
|
|
&decoder_config_record)) {
|
|
|
|
DLOG(ERROR) << "Failure to construct an AVCDecoderConfigurationRecord";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-08 00:39:14 +00:00
|
|
|
if (last_video_decoder_config_) {
|
2016-06-27 19:30:32 +00:00
|
|
|
if (last_video_decoder_config_->codec_config() != decoder_config_record) {
|
2014-05-29 20:21:28 +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.264 decoder configuration has changed.";
|
2016-06-27 19:30:32 +00:00
|
|
|
last_video_decoder_config_->set_codec_config(decoder_config_record);
|
2014-04-18 01:57:31 +00:00
|
|
|
}
|
2014-05-29 20:21:28 +00:00
|
|
|
return true;
|
2014-04-08 00:39:14 +00:00
|
|
|
}
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2015-09-16 22:22:22 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-04-08 00:39:14 +00:00
|
|
|
|
2017-01-24 00:55:02 +00:00
|
|
|
const uint8_t nalu_length_size =
|
|
|
|
H26xByteToUnitStreamConverter::kUnitStreamNaluLengthSize;
|
2017-03-23 18:36:47 +00:00
|
|
|
const H26xStreamFormat stream_format = stream_converter()->stream_format();
|
|
|
|
const FourCC codec_fourcc =
|
|
|
|
stream_format == H26xStreamFormat::kNalUnitStreamWithParameterSetNalus
|
|
|
|
? FOURCC_avc3
|
|
|
|
: FOURCC_avc1;
|
2017-01-24 00:55:02 +00:00
|
|
|
last_video_decoder_config_ = std::make_shared<VideoStreamInfo>(
|
2017-03-23 18:36:47 +00:00
|
|
|
pid(), kMpeg2Timescale, kInfiniteDuration, kCodecH264, stream_format,
|
|
|
|
AVCDecoderConfigurationRecord::GetCodecString(
|
|
|
|
codec_fourcc, decoder_config_record[1], decoder_config_record[2],
|
|
|
|
decoder_config_record[3]),
|
2016-07-27 00:51:08 +00:00
|
|
|
decoder_config_record.data(), decoder_config_record.size(), coded_width,
|
2019-09-23 06:24:33 +00:00
|
|
|
coded_height, pixel_width, pixel_height, sps->transfer_characteristics, 0,
|
|
|
|
nalu_length_size, std::string(), false);
|
2014-04-08 00:39:14 +00:00
|
|
|
DVLOG(1) << "Profile IDC: " << sps->profile_idc;
|
|
|
|
DVLOG(1) << "Level IDC: " << sps->level_idc;
|
2015-09-16 22:22:22 +00:00
|
|
|
DVLOG(1) << "log2_max_frame_num_minus4: " << sps->log2_max_frame_num_minus4;
|
2014-04-08 00:39:14 +00:00
|
|
|
|
|
|
|
// Video config notification.
|
2023-12-01 17:32:19 +00:00
|
|
|
new_stream_info_cb_(last_video_decoder_config_);
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-28 03:22:17 +00:00
|
|
|
int64_t EsParserH264::CalculateSampleDuration(int pps_id) {
|
|
|
|
auto pps = h264_parser_->GetPps(pps_id);
|
|
|
|
if (pps) {
|
|
|
|
auto sps_id = pps->seq_parameter_set_id;
|
|
|
|
auto sps = h264_parser_->GetSps(sps_id);
|
|
|
|
if (sps && sps->timing_info_present_flag && sps->fixed_frame_rate_flag) {
|
|
|
|
return static_cast<int64_t>(kMpeg2Timescale) * sps->num_units_in_tick *
|
|
|
|
2 / sps->time_scale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG(WARNING) << "[MPEG-2 TS] PID " << pid()
|
|
|
|
<< " Cannot calculate frame rate from SPS.";
|
|
|
|
// Returns arbitrary safe duration
|
|
|
|
return 0.001 * kMpeg2Timescale; // 1ms.
|
|
|
|
}
|
|
|
|
|
2014-04-01 01:34:59 +00:00
|
|
|
} // namespace mp2t
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|