5 #include "packager/media/formats/mp2t/es_parser_h26x.h"
9 #include "packager/base/logging.h"
10 #include "packager/base/numerics/safe_conversions.h"
11 #include "packager/media/base/media_sample.h"
12 #include "packager/media/base/offset_byte_queue.h"
13 #include "packager/media/base/timestamp.h"
14 #include "packager/media/base/video_stream_info.h"
15 #include "packager/media/filters/h264_byte_to_unit_stream_converter.h"
16 #include "packager/media/filters/h265_byte_to_unit_stream_converter.h"
17 #include "packager/media/formats/mp2t/mp2t_common.h"
19 namespace edash_packager {
25 H26xByteToUnitStreamConverter* CreateStreamConverter(Nalu::CodecType type) {
26 if (type == Nalu::kH264) {
27 return new H264ByteToUnitStreamConverter();
29 DCHECK_EQ(Nalu::kH265, type);
30 return new H265ByteToUnitStreamConverter();
36 EsParserH26x::EsParserH26x(Nalu::CodecType type,
38 const EmitSampleCB& emit_sample_cb)
40 emit_sample_cb_(emit_sample_cb),
42 es_queue_(new media::OffsetByteQueue()),
43 current_access_unit_pos_(0),
44 found_access_unit_(false),
45 stream_converter_(CreateStreamConverter(type)),
46 pending_sample_duration_(0),
47 waiting_for_key_frame_(true) {
50 EsParserH26x::~EsParserH26x() {}
52 bool EsParserH26x::Parse(
const uint8_t* buf,
65 DVLOG_IF(1, pts == kNoTimestamp) <<
"Each video PES should have a PTS";
66 if (pts != kNoTimestamp) {
67 TimingDesc timing_desc;
68 timing_desc.pts = pts;
69 timing_desc.dts = (dts != kNoTimestamp) ? dts : pts;
72 timing_desc_list_.push_back(
73 std::pair<int64_t, TimingDesc>(es_queue_->tail(), timing_desc));
77 es_queue_->Push(buf, size);
80 if (!found_access_unit_) {
81 if (!FindNextAccessUnit(current_access_unit_pos_,
82 ¤t_access_unit_pos_)) {
85 es_queue_->Trim(current_access_unit_pos_);
86 found_access_unit_ =
true;
89 return ParseInternal();
92 void EsParserH26x::Flush() {
93 DVLOG(1) <<
"EsParserH26x::Flush";
97 if (type_ == Nalu::kH264) {
98 uint8_t aud[] = {0x00, 0x00, 0x01, 0x09};
99 es_queue_->Push(aud,
sizeof(aud));
101 DCHECK_EQ(Nalu::kH265, type_);
102 uint8_t aud[] = {0x00, 0x00, 0x01, 0x46, 0x01};
103 es_queue_->Push(aud,
sizeof(aud));
107 if (pending_sample_) {
109 DCHECK(pending_sample_duration_);
110 pending_sample_->set_duration(pending_sample_duration_);
111 emit_sample_cb_.Run(pid(), pending_sample_);
112 pending_sample_ = scoped_refptr<MediaSample>();
116 void EsParserH26x::Reset() {
117 es_queue_.reset(
new media::OffsetByteQueue());
118 current_access_unit_pos_ = 0;
119 found_access_unit_ =
false;
120 timing_desc_list_.clear();
121 pending_sample_ = scoped_refptr<MediaSample>();
122 pending_sample_duration_ = 0;
123 waiting_for_key_frame_ =
true;
126 bool EsParserH26x::FindNextAccessUnit(int64_t stream_pos,
127 int64_t* next_unit_pos) {
132 bool seen_vcl_nalu =
false;
136 es_queue_->PeekAt(stream_pos, &es, &size);
139 uint64_t start_code_offset;
140 uint8_t start_code_size;
141 bool start_code_found = NaluReader::FindStartCode(
142 es, size, &start_code_offset, &start_code_size);
143 stream_pos += start_code_offset;
146 if (!start_code_found ||
147 start_code_offset + start_code_size >= static_cast<uint64_t>(size)) {
152 const uint8_t* nalu_ptr = es + start_code_offset + start_code_size;
153 size_t nalu_size = size - (start_code_offset + start_code_size);
154 if (nalu.Initialize(type_, nalu_ptr, nalu_size)) {
171 if ((seen_vcl_nalu || !found_access_unit_) &&
172 nalu.can_start_access_unit()) {
175 bool is_vcl_nalu = nalu.is_video_slice() && nalu.nuh_layer_id() == 0;
176 seen_vcl_nalu |= is_vcl_nalu;
181 stream_pos += start_code_size;
184 *next_unit_pos = stream_pos;
188 bool EsParserH26x::ParseInternal() {
189 DCHECK_LE(es_queue_->head(), current_access_unit_pos_);
190 DCHECK_LE(current_access_unit_pos_, es_queue_->tail());
193 int64_t access_unit_end;
194 if (!FindNextAccessUnit(current_access_unit_pos_, &access_unit_end))
198 bool is_key_frame =
false;
199 int pps_id_for_access_unit = -1;
203 es_queue_->PeekAt(current_access_unit_pos_, &es, &size);
204 int access_unit_size = base::checked_cast<int, int64_t>(
205 access_unit_end - current_access_unit_pos_);
206 DCHECK_LE(access_unit_size, size);
207 NaluReader reader(type_, kIsAnnexbByteStream, es, access_unit_size);
214 switch (reader.Advance(&nalu)) {
215 case NaluReader::kOk:
217 case NaluReader::kEOStream:
226 if (!ProcessNalu(nalu, &is_key_frame, &pps_id_for_access_unit))
230 if (waiting_for_key_frame_) {
231 waiting_for_key_frame_ = !is_key_frame;
233 if (!waiting_for_key_frame_) {
235 RCHECK(EmitFrame(current_access_unit_pos_, access_unit_size,
236 is_key_frame, pps_id_for_access_unit));
238 current_access_unit_pos_ = access_unit_end;
239 es_queue_->Trim(current_access_unit_pos_);
244 bool EsParserH26x::EmitFrame(int64_t access_unit_pos,
245 int access_unit_size,
249 TimingDesc current_timing_desc = {kNoTimestamp, kNoTimestamp};
250 while (!timing_desc_list_.empty() &&
251 timing_desc_list_.front().first <= access_unit_pos) {
252 current_timing_desc = timing_desc_list_.front().second;
253 timing_desc_list_.pop_front();
255 if (current_timing_desc.pts == kNoTimestamp)
259 DVLOG(LOG_LEVEL_ES) <<
"Emit frame: stream_pos=" << current_access_unit_pos_
260 <<
" size=" << access_unit_size;
263 es_queue_->PeekAt(current_access_unit_pos_, &es, &es_size);
264 CHECK_GE(es_size, access_unit_size);
267 std::vector<uint8_t> converted_frame;
268 if (!stream_converter_->ConvertByteStreamToNalUnitStream(
269 es, access_unit_size, &converted_frame)) {
270 DLOG(ERROR) <<
"Failure to convert video frame to unit stream format.";
275 RCHECK(UpdateVideoDecoderConfig(pps_id));
280 converted_frame.data(), converted_frame.size(), is_key_frame);
281 media_sample->set_dts(current_timing_desc.dts);
282 media_sample->set_pts(current_timing_desc.pts);
283 if (pending_sample_) {
284 DCHECK_GT(media_sample->dts(), pending_sample_->dts());
285 pending_sample_duration_ = media_sample->dts() - pending_sample_->dts();
286 pending_sample_->set_duration(pending_sample_duration_);
287 emit_sample_cb_.Run(pid(), pending_sample_);
289 pending_sample_ = media_sample;