5 #include "packager/media/formats/mp2t/es_parser_h264.h"
9 #include "packager/base/logging.h"
10 #include "packager/media/base/media_sample.h"
11 #include "packager/media/base/timestamp.h"
12 #include "packager/media/base/video_stream_info.h"
13 #include "packager/media/codecs/avc_decoder_configuration_record.h"
14 #include "packager/media/codecs/h264_byte_to_unit_stream_converter.h"
15 #include "packager/media/codecs/h264_parser.h"
16 #include "packager/media/formats/mp2t/mp2t_common.h"
22 EsParserH264::EsParserH264(uint32_t pid,
23 const NewStreamInfoCB& new_stream_info_cb,
24 const EmitSampleCB& emit_sample_cb)
25 : EsParserH26x(Nalu::kH264,
26 std::unique_ptr<H26xByteToUnitStreamConverter>(
27 new H264ByteToUnitStreamConverter()),
30 new_stream_info_cb_(new_stream_info_cb),
31 decoder_config_check_pending_(false),
32 h264_parser_(new H264Parser()) {}
34 EsParserH264::~EsParserH264() {}
36 void EsParserH264::Reset() {
37 DVLOG(1) <<
"EsParserH264::Reset";
38 h264_parser_.reset(
new H264Parser());
39 last_video_decoder_config_ = std::shared_ptr<StreamInfo>();
40 decoder_config_check_pending_ =
false;
41 EsParserH26x::Reset();
44 bool EsParserH264::ProcessNalu(
const Nalu& nalu,
45 VideoSliceInfo* video_slice_info) {
46 video_slice_info->valid =
false;
47 switch (nalu.type()) {
48 case Nalu::H264_AUD: {
49 DVLOG(LOG_LEVEL_ES) <<
"Nalu: AUD";
52 case Nalu::H264_SPS: {
53 DVLOG(LOG_LEVEL_ES) <<
"Nalu: SPS";
55 if (h264_parser_->ParseSps(nalu, &sps_id) != H264Parser::kOk)
57 decoder_config_check_pending_ =
true;
60 case Nalu::H264_PPS: {
61 DVLOG(LOG_LEVEL_ES) <<
"Nalu: PPS";
63 if (h264_parser_->ParsePps(nalu, &pps_id) != H264Parser::kOk) {
65 if (last_video_decoder_config_)
68 decoder_config_check_pending_ =
true;
72 case Nalu::H264_IDRSlice:
73 case Nalu::H264_NonIDRSlice: {
74 const bool is_key_frame = (nalu.type() == Nalu::H264_IDRSlice);
75 DVLOG(LOG_LEVEL_ES) <<
"Nalu: slice IDR=" << is_key_frame;
77 if (h264_parser_->ParseSliceHeader(nalu, &shdr) != H264Parser::kOk) {
80 if (last_video_decoder_config_)
83 video_slice_info->valid =
true;
84 video_slice_info->is_key_frame = is_key_frame;
85 video_slice_info->frame_num = shdr.frame_num;
86 video_slice_info->pps_id = shdr.pic_parameter_set_id;
91 DVLOG(LOG_LEVEL_ES) <<
"Nalu: " << nalu.type();
98 bool EsParserH264::UpdateVideoDecoderConfig(
int pps_id) {
100 if (!decoder_config_check_pending_)
103 const H264Pps* pps = h264_parser_->GetPps(pps_id);
111 return last_video_decoder_config_ ==
nullptr;
113 sps = h264_parser_->GetSps(pps->seq_parameter_set_id);
116 decoder_config_check_pending_ =
false;
119 std::vector<uint8_t> decoder_config_record;
120 if (!stream_converter()->GetDecoderConfigurationRecord(
121 &decoder_config_record)) {
122 DLOG(ERROR) <<
"Failure to construct an AVCDecoderConfigurationRecord";
126 if (last_video_decoder_config_) {
127 if (last_video_decoder_config_->codec_config() != decoder_config_record) {
133 LOG(WARNING) <<
"H.264 decoder configuration has changed.";
134 last_video_decoder_config_->set_codec_config(decoder_config_record);
139 uint32_t coded_width = 0;
140 uint32_t coded_height = 0;
141 uint32_t pixel_width = 0;
142 uint32_t pixel_height = 0;
143 if (!ExtractResolutionFromSps(*sps, &coded_width, &coded_height, &pixel_width,
145 LOG(ERROR) <<
"Failed to parse SPS.";
149 const uint8_t nalu_length_size =
150 H26xByteToUnitStreamConverter::kUnitStreamNaluLengthSize;
151 const H26xStreamFormat stream_format = stream_converter()->stream_format();
152 const FourCC codec_fourcc =
153 stream_format == H26xStreamFormat::kNalUnitStreamWithParameterSetNalus
156 last_video_decoder_config_ = std::make_shared<VideoStreamInfo>(
157 pid(), kMpeg2Timescale, kInfiniteDuration, kCodecH264, stream_format,
159 codec_fourcc, decoder_config_record[1], decoder_config_record[2],
160 decoder_config_record[3]),
161 decoder_config_record.data(), decoder_config_record.size(), coded_width,
162 coded_height, pixel_width, pixel_height, 0, nalu_length_size,
163 std::string(),
false);
164 DVLOG(1) <<
"Profile IDC: " << sps->profile_idc;
165 DVLOG(1) <<
"Level IDC: " << sps->level_idc;
166 DVLOG(1) <<
"log2_max_frame_num_minus4: " << sps->log2_max_frame_num_minus4;
169 new_stream_info_cb_.Run(last_video_decoder_config_);