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 {
23 EsParserH26x::EsParserH26x(
25 scoped_ptr<H26xByteToUnitStreamConverter> stream_converter,
27 const EmitSampleCB& emit_sample_cb)
29 emit_sample_cb_(emit_sample_cb),
31 es_queue_(new media::OffsetByteQueue()),
32 current_access_unit_pos_(0),
33 found_access_unit_(false),
34 stream_converter_(stream_converter.Pass()),
35 pending_sample_duration_(0),
36 waiting_for_key_frame_(true) {}
38 EsParserH26x::~EsParserH26x() {}
40 bool EsParserH26x::Parse(
const uint8_t* buf,
53 DVLOG_IF(1, pts == kNoTimestamp) <<
"Each video PES should have a PTS";
54 if (pts != kNoTimestamp) {
55 TimingDesc timing_desc;
56 timing_desc.pts = pts;
57 timing_desc.dts = (dts != kNoTimestamp) ? dts : pts;
60 timing_desc_list_.push_back(
61 std::pair<int64_t, TimingDesc>(es_queue_->tail(), timing_desc));
65 es_queue_->Push(buf, size);
68 if (!found_access_unit_) {
69 if (!FindNextAccessUnit(current_access_unit_pos_,
70 ¤t_access_unit_pos_)) {
73 es_queue_->Trim(current_access_unit_pos_);
74 found_access_unit_ =
true;
77 return ParseInternal();
80 void EsParserH26x::Flush() {
81 DVLOG(1) <<
"EsParserH26x::Flush";
85 if (type_ == Nalu::kH264) {
86 uint8_t aud[] = {0x00, 0x00, 0x01, 0x09};
87 es_queue_->Push(aud,
sizeof(aud));
89 DCHECK_EQ(Nalu::kH265, type_);
90 uint8_t aud[] = {0x00, 0x00, 0x01, 0x46, 0x01};
91 es_queue_->Push(aud,
sizeof(aud));
95 if (pending_sample_) {
97 DCHECK(pending_sample_duration_);
98 pending_sample_->set_duration(pending_sample_duration_);
99 emit_sample_cb_.Run(pid(), pending_sample_);
100 pending_sample_ = scoped_refptr<MediaSample>();
104 void EsParserH26x::Reset() {
105 es_queue_.reset(
new media::OffsetByteQueue());
106 current_access_unit_pos_ = 0;
107 found_access_unit_ =
false;
108 timing_desc_list_.clear();
109 pending_sample_ = scoped_refptr<MediaSample>();
110 pending_sample_duration_ = 0;
111 waiting_for_key_frame_ =
true;
114 bool EsParserH26x::FindNextAccessUnit(int64_t stream_pos,
115 int64_t* next_unit_pos) {
120 bool seen_vcl_nalu =
false;
124 es_queue_->PeekAt(stream_pos, &es, &size);
127 uint64_t start_code_offset;
128 uint8_t start_code_size;
129 bool start_code_found = NaluReader::FindStartCode(
130 es, size, &start_code_offset, &start_code_size);
131 stream_pos += start_code_offset;
134 if (!start_code_found ||
135 start_code_offset + start_code_size >= static_cast<uint64_t>(size)) {
140 const uint8_t* nalu_ptr = es + start_code_offset + start_code_size;
141 size_t nalu_size = size - (start_code_offset + start_code_size);
142 if (nalu.Initialize(type_, nalu_ptr, nalu_size)) {
159 if ((seen_vcl_nalu || !found_access_unit_) &&
160 nalu.can_start_access_unit()) {
163 bool is_vcl_nalu = nalu.is_video_slice() && nalu.nuh_layer_id() == 0;
164 seen_vcl_nalu |= is_vcl_nalu;
169 stream_pos += start_code_size;
172 *next_unit_pos = stream_pos;
176 bool EsParserH26x::ParseInternal() {
177 DCHECK_LE(es_queue_->head(), current_access_unit_pos_);
178 DCHECK_LE(current_access_unit_pos_, es_queue_->tail());
181 int64_t access_unit_end;
182 if (!FindNextAccessUnit(current_access_unit_pos_, &access_unit_end))
186 bool is_key_frame =
false;
187 int pps_id_for_access_unit = -1;
191 es_queue_->PeekAt(current_access_unit_pos_, &es, &size);
192 int access_unit_size = base::checked_cast<int, int64_t>(
193 access_unit_end - current_access_unit_pos_);
194 DCHECK_LE(access_unit_size, size);
195 NaluReader reader(type_, kIsAnnexbByteStream, es, access_unit_size);
202 switch (reader.Advance(&nalu)) {
203 case NaluReader::kOk:
205 case NaluReader::kEOStream:
214 if (!ProcessNalu(nalu, &is_key_frame, &pps_id_for_access_unit))
218 if (waiting_for_key_frame_) {
219 waiting_for_key_frame_ = !is_key_frame;
221 if (!waiting_for_key_frame_) {
223 RCHECK(EmitFrame(current_access_unit_pos_, access_unit_size,
224 is_key_frame, pps_id_for_access_unit));
226 current_access_unit_pos_ = access_unit_end;
227 es_queue_->Trim(current_access_unit_pos_);
232 bool EsParserH26x::EmitFrame(int64_t access_unit_pos,
233 int access_unit_size,
237 TimingDesc current_timing_desc = {kNoTimestamp, kNoTimestamp};
238 while (!timing_desc_list_.empty() &&
239 timing_desc_list_.front().first <= access_unit_pos) {
240 current_timing_desc = timing_desc_list_.front().second;
241 timing_desc_list_.pop_front();
243 if (current_timing_desc.pts == kNoTimestamp)
247 DVLOG(LOG_LEVEL_ES) <<
"Emit frame: stream_pos=" << current_access_unit_pos_
248 <<
" size=" << access_unit_size;
251 es_queue_->PeekAt(current_access_unit_pos_, &es, &es_size);
252 CHECK_GE(es_size, access_unit_size);
255 std::vector<uint8_t> converted_frame;
256 if (!stream_converter_->ConvertByteStreamToNalUnitStream(
257 es, access_unit_size, &converted_frame)) {
258 DLOG(ERROR) <<
"Failure to convert video frame to unit stream format.";
263 RCHECK(UpdateVideoDecoderConfig(pps_id));
268 converted_frame.data(), converted_frame.size(), is_key_frame);
269 media_sample->set_dts(current_timing_desc.dts);
270 media_sample->set_pts(current_timing_desc.pts);
271 if (pending_sample_) {
272 DCHECK_GT(media_sample->dts(), pending_sample_->dts());
273 pending_sample_duration_ = media_sample->dts() - pending_sample_->dts();
274 pending_sample_->set_duration(pending_sample_duration_);
275 emit_sample_cb_.Run(pid(), pending_sample_);
277 pending_sample_ = media_sample;