5 #include <packager/media/formats/mp2t/es_parser_h264.h>
9 #include <absl/log/log.h>
11 #include <packager/macros/logging.h>
12 #include <packager/media/base/media_sample.h>
13 #include <packager/media/base/timestamp.h>
14 #include <packager/media/base/video_stream_info.h>
15 #include <packager/media/codecs/avc_decoder_configuration_record.h>
16 #include <packager/media/codecs/h264_byte_to_unit_stream_converter.h>
17 #include <packager/media/codecs/h264_parser.h>
18 #include <packager/media/formats/mp2t/mp2t_common.h>
24 EsParserH264::EsParserH264(uint32_t pid,
25 const NewStreamInfoCB& new_stream_info_cb,
26 const EmitSampleCB& emit_sample_cb)
27 : EsParserH26x(Nalu::kH264,
28 std::unique_ptr<H26xByteToUnitStreamConverter>(
29 new H264ByteToUnitStreamConverter()),
32 new_stream_info_cb_(new_stream_info_cb),
33 decoder_config_check_pending_(false),
34 h264_parser_(new H264Parser()) {}
36 EsParserH264::~EsParserH264() {}
38 void EsParserH264::Reset() {
39 DVLOG(1) <<
"EsParserH264::Reset";
40 h264_parser_.reset(
new H264Parser());
41 last_video_decoder_config_ = std::shared_ptr<StreamInfo>();
42 decoder_config_check_pending_ =
false;
43 EsParserH26x::Reset();
46 bool EsParserH264::ProcessNalu(
const Nalu& nalu,
47 VideoSliceInfo* video_slice_info) {
48 video_slice_info->valid =
false;
49 switch (nalu.type()) {
50 case Nalu::H264_AUD: {
51 DVLOG(LOG_LEVEL_ES) <<
"Nalu: AUD";
54 case Nalu::H264_SPS: {
55 DVLOG(LOG_LEVEL_ES) <<
"Nalu: SPS";
57 auto status = h264_parser_->ParseSps(nalu, &sps_id);
58 if (status == H264Parser::kOk)
59 decoder_config_check_pending_ =
true;
60 else if (status == H264Parser::kUnsupportedStream)
62 new_stream_info_cb_(
nullptr);
67 case Nalu::H264_PPS: {
68 DVLOG(LOG_LEVEL_ES) <<
"Nalu: PPS";
70 auto status = h264_parser_->ParsePps(nalu, &pps_id);
71 if (status == H264Parser::kOk) {
72 decoder_config_check_pending_ =
true;
73 }
else if (status == H264Parser::kUnsupportedStream) {
75 new_stream_info_cb_(
nullptr);
78 if (last_video_decoder_config_)
83 case Nalu::H264_IDRSlice:
84 case Nalu::H264_NonIDRSlice: {
85 const bool is_key_frame = (nalu.type() == Nalu::H264_IDRSlice);
86 DVLOG(LOG_LEVEL_ES) <<
"Nalu: slice IDR=" << is_key_frame;
88 auto status = h264_parser_->ParseSliceHeader(nalu, &shdr);
89 if (status == H264Parser::kOk) {
90 video_slice_info->valid =
true;
91 video_slice_info->is_key_frame = is_key_frame;
92 video_slice_info->frame_num = shdr.frame_num;
93 video_slice_info->pps_id = shdr.pic_parameter_set_id;
94 }
else if (status == H264Parser::kUnsupportedStream) {
96 new_stream_info_cb_(
nullptr);
100 if (last_video_decoder_config_)
106 DVLOG(LOG_LEVEL_ES) <<
"Nalu: " << nalu.type();
113 bool EsParserH264::UpdateVideoDecoderConfig(
int pps_id) {
115 if (!decoder_config_check_pending_)
118 const H264Pps* pps = h264_parser_->GetPps(pps_id);
126 return last_video_decoder_config_ ==
nullptr;
128 sps = h264_parser_->GetSps(pps->seq_parameter_set_id);
131 decoder_config_check_pending_ =
false;
134 std::vector<uint8_t> decoder_config_record;
135 if (!stream_converter()->GetDecoderConfigurationRecord(
136 &decoder_config_record)) {
137 DLOG(ERROR) <<
"Failure to construct an AVCDecoderConfigurationRecord";
141 if (last_video_decoder_config_) {
142 if (last_video_decoder_config_->codec_config() != decoder_config_record) {
148 LOG(WARNING) <<
"H.264 decoder configuration has changed.";
149 last_video_decoder_config_->set_codec_config(decoder_config_record);
154 uint32_t coded_width = 0;
155 uint32_t coded_height = 0;
156 uint32_t pixel_width = 0;
157 uint32_t pixel_height = 0;
158 if (!ExtractResolutionFromSps(*sps, &coded_width, &coded_height, &pixel_width,
160 LOG(ERROR) <<
"Failed to parse SPS.";
164 const uint8_t nalu_length_size =
165 H26xByteToUnitStreamConverter::kUnitStreamNaluLengthSize;
166 const H26xStreamFormat stream_format = stream_converter()->stream_format();
167 const FourCC codec_fourcc =
168 stream_format == H26xStreamFormat::kNalUnitStreamWithParameterSetNalus
171 last_video_decoder_config_ = std::make_shared<VideoStreamInfo>(
172 pid(), kMpeg2Timescale, kInfiniteDuration, kCodecH264, stream_format,
173 AVCDecoderConfigurationRecord::GetCodecString(
174 codec_fourcc, decoder_config_record[1], decoder_config_record[2],
175 decoder_config_record[3]),
176 decoder_config_record.data(), decoder_config_record.size(), coded_width,
177 coded_height, pixel_width, pixel_height, sps->color_primaries,
178 sps->matrix_coefficients, sps->transfer_characteristics, 0,
179 nalu_length_size, std::string(),
false);
180 DVLOG(1) <<
"Profile IDC: " << sps->profile_idc;
181 DVLOG(1) <<
"Level IDC: " << sps->level_idc;
182 DVLOG(1) <<
"log2_max_frame_num_minus4: " << sps->log2_max_frame_num_minus4;
185 new_stream_info_cb_(last_video_decoder_config_);
190 int64_t EsParserH264::CalculateSampleDuration(
int pps_id) {
191 auto pps = h264_parser_->GetPps(pps_id);
193 auto sps_id = pps->seq_parameter_set_id;
194 auto sps = h264_parser_->GetSps(sps_id);
195 if (sps && sps->timing_info_present_flag && sps->fixed_frame_rate_flag) {
196 return static_cast<int64_t
>(kMpeg2Timescale) * sps->num_units_in_tick *
200 LOG(WARNING) <<
"[MPEG-2 TS] PID " << pid()
201 <<
" Cannot calculate frame rate from SPS.";
203 return 0.001 * kMpeg2Timescale;
All the methods that are virtual are virtual for mocking.