2016-03-31 19:48:16 +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.
|
|
|
|
|
|
|
|
#include "packager/media/formats/mp2t/es_parser_h26x.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2023-08-31 23:59:46 +00:00
|
|
|
#include <glog/logging.h>
|
2016-03-31 19:48:16 +00:00
|
|
|
#include "packager/media/base/media_sample.h"
|
|
|
|
#include "packager/media/base/offset_byte_queue.h"
|
|
|
|
#include "packager/media/base/timestamp.h"
|
|
|
|
#include "packager/media/base/video_stream_info.h"
|
2016-05-03 22:05:45 +00:00
|
|
|
#include "packager/media/codecs/h26x_byte_to_unit_stream_converter.h"
|
2016-03-31 19:48:16 +00:00
|
|
|
#include "packager/media/formats/mp2t/mp2t_common.h"
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-03-31 19:48:16 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp2t {
|
|
|
|
|
2016-05-03 22:05:45 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const int kStartCodeSize = 3;
|
|
|
|
const int kH264NaluHeaderSize = 1;
|
|
|
|
const int kH265NaluHeaderSize = 2;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-05-04 21:19:40 +00:00
|
|
|
EsParserH26x::EsParserH26x(
|
|
|
|
Nalu::CodecType type,
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<H26xByteToUnitStreamConverter> stream_converter,
|
2016-05-04 21:19:40 +00:00
|
|
|
uint32_t pid,
|
|
|
|
const EmitSampleCB& emit_sample_cb)
|
2016-03-31 19:48:16 +00:00
|
|
|
: EsParser(pid),
|
|
|
|
emit_sample_cb_(emit_sample_cb),
|
|
|
|
type_(type),
|
|
|
|
es_queue_(new media::OffsetByteQueue()),
|
2016-08-25 22:44:25 +00:00
|
|
|
stream_converter_(std::move(stream_converter)) {}
|
2016-03-31 19:48:16 +00:00
|
|
|
|
|
|
|
EsParserH26x::~EsParserH26x() {}
|
|
|
|
|
|
|
|
bool EsParserH26x::Parse(const uint8_t* buf,
|
|
|
|
int size,
|
|
|
|
int64_t pts,
|
|
|
|
int64_t dts) {
|
|
|
|
// Note: Parse is invoked each time a PES packet has been reassembled.
|
|
|
|
// Unfortunately, a PES packet does not necessarily map
|
|
|
|
// to an h264/h265 access unit, although the HLS recommendation is to use one
|
|
|
|
// PES for each access unit (but this is just a recommendation and some
|
|
|
|
// streams do not comply with this recommendation).
|
|
|
|
|
|
|
|
// HLS recommendation: "In AVC video, you should have both a DTS and a
|
|
|
|
// PTS in each PES header".
|
|
|
|
// However, some streams do not comply with this recommendation.
|
|
|
|
DVLOG_IF(1, pts == kNoTimestamp) << "Each video PES should have a PTS";
|
|
|
|
if (pts != kNoTimestamp) {
|
|
|
|
TimingDesc timing_desc;
|
|
|
|
timing_desc.pts = pts;
|
|
|
|
timing_desc.dts = (dts != kNoTimestamp) ? dts : pts;
|
|
|
|
|
|
|
|
// Link the end of the byte queue with the incoming timing descriptor.
|
|
|
|
timing_desc_list_.push_back(
|
|
|
|
std::pair<int64_t, TimingDesc>(es_queue_->tail(), timing_desc));
|
2018-12-10 22:12:01 +00:00
|
|
|
|
|
|
|
// Warns if there are a large number of cached timestamps, which should be 1
|
|
|
|
// or 2 if everythings works as expected.
|
|
|
|
const size_t kWarningSize =
|
|
|
|
24; // An arbitrary number (it is 1 second for a fps of 24).
|
|
|
|
LOG_IF(WARNING, timing_desc_list_.size() >= kWarningSize)
|
|
|
|
<< "Unusually large number of cached timestamps ("
|
|
|
|
<< timing_desc_list_.size() << ").";
|
2016-03-31 19:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the incoming bytes to the ES queue.
|
|
|
|
es_queue_->Push(buf, size);
|
|
|
|
return ParseInternal();
|
|
|
|
}
|
|
|
|
|
2020-12-10 23:03:41 +00:00
|
|
|
bool EsParserH26x::Flush() {
|
2016-03-31 19:48:16 +00:00
|
|
|
DVLOG(1) << "EsParserH26x::Flush";
|
|
|
|
|
2016-08-25 22:44:25 +00:00
|
|
|
// Simulate two additional AUDs to force emitting the last access unit
|
2016-03-31 19:48:16 +00:00
|
|
|
// which is assumed to be complete at this point.
|
2016-08-25 22:44:25 +00:00
|
|
|
// Two AUDs are needed because the exact size of a NAL unit can only be
|
|
|
|
// determined after seeing the next NAL unit, so we need a second AUD to
|
|
|
|
// finish the parsing of the first AUD.
|
2016-03-31 19:48:16 +00:00
|
|
|
if (type_ == Nalu::kH264) {
|
2016-08-25 22:44:25 +00:00
|
|
|
const uint8_t aud[] = {0x00, 0x00, 0x01, 0x09, 0x00, 0x00, 0x01, 0x09};
|
2016-03-31 19:48:16 +00:00
|
|
|
es_queue_->Push(aud, sizeof(aud));
|
|
|
|
} else {
|
|
|
|
DCHECK_EQ(Nalu::kH265, type_);
|
2016-08-25 22:44:25 +00:00
|
|
|
const uint8_t aud[] = {0x00, 0x00, 0x01, 0x46, 0x01,
|
|
|
|
0x00, 0x00, 0x01, 0x46, 0x01};
|
2016-03-31 19:48:16 +00:00
|
|
|
es_queue_->Push(aud, sizeof(aud));
|
|
|
|
}
|
2016-05-03 22:05:45 +00:00
|
|
|
|
2020-12-10 23:03:41 +00:00
|
|
|
RCHECK(ParseInternal());
|
2016-05-03 22:05:45 +00:00
|
|
|
|
2016-03-31 19:48:16 +00:00
|
|
|
if (pending_sample_) {
|
|
|
|
// Flush pending sample.
|
2022-10-28 03:22:17 +00:00
|
|
|
if (!pending_sample_duration_) {
|
|
|
|
pending_sample_duration_ = CalculateSampleDuration(pending_sample_pps_id_);
|
|
|
|
}
|
2016-03-31 19:48:16 +00:00
|
|
|
pending_sample_->set_duration(pending_sample_duration_);
|
2023-08-31 23:59:46 +00:00
|
|
|
emit_sample_cb_(std::move(pending_sample_));
|
2016-03-31 19:48:16 +00:00
|
|
|
}
|
2020-12-10 23:03:41 +00:00
|
|
|
return true;
|
2016-03-31 19:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EsParserH26x::Reset() {
|
|
|
|
es_queue_.reset(new media::OffsetByteQueue());
|
2016-05-03 22:05:45 +00:00
|
|
|
current_search_position_ = 0;
|
2016-08-25 22:44:25 +00:00
|
|
|
current_access_unit_position_ = 0;
|
|
|
|
current_video_slice_info_.valid = false;
|
|
|
|
next_access_unit_position_set_ = false;
|
|
|
|
next_access_unit_position_ = 0;
|
|
|
|
current_nalu_info_.reset();
|
2016-03-31 19:48:16 +00:00
|
|
|
timing_desc_list_.clear();
|
2017-01-24 00:55:02 +00:00
|
|
|
pending_sample_ = std::shared_ptr<MediaSample>();
|
2016-03-31 19:48:16 +00:00
|
|
|
pending_sample_duration_ = 0;
|
|
|
|
waiting_for_key_frame_ = true;
|
|
|
|
}
|
|
|
|
|
2016-08-25 22:44:25 +00:00
|
|
|
bool EsParserH26x::SearchForNalu(uint64_t* position, Nalu* nalu) {
|
2016-05-03 22:05:45 +00:00
|
|
|
const uint8_t* es;
|
|
|
|
int es_size;
|
|
|
|
es_queue_->PeekAt(current_search_position_, &es, &es_size);
|
|
|
|
|
|
|
|
// Find a start code.
|
|
|
|
uint64_t start_code_offset;
|
|
|
|
uint8_t start_code_size;
|
|
|
|
const bool start_code_found = NaluReader::FindStartCode(
|
|
|
|
es, es_size, &start_code_offset, &start_code_size);
|
|
|
|
|
|
|
|
if (!start_code_found) {
|
|
|
|
// We didn't find a start code, so we don't have to search this data again.
|
|
|
|
if (es_size > kStartCodeSize)
|
|
|
|
current_search_position_ += es_size - kStartCodeSize;
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-31 19:48:16 +00:00
|
|
|
|
2016-05-03 22:05:45 +00:00
|
|
|
// Ensure the next NAL unit is a real NAL unit.
|
2016-08-25 22:44:25 +00:00
|
|
|
const uint8_t* next_nalu_ptr = es + start_code_offset + start_code_size;
|
2016-05-03 22:05:45 +00:00
|
|
|
// This size is likely inaccurate, this is just to get the header info.
|
|
|
|
const int64_t next_nalu_size = es_size - start_code_offset - start_code_size;
|
|
|
|
if (next_nalu_size <
|
|
|
|
(type_ == Nalu::kH264 ? kH264NaluHeaderSize : kH265NaluHeaderSize)) {
|
|
|
|
// There was not enough data, wait for more.
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-31 19:48:16 +00:00
|
|
|
|
2016-08-25 22:44:25 +00:00
|
|
|
// Update search position for next nalu.
|
2016-05-03 22:05:45 +00:00
|
|
|
current_search_position_ += start_code_offset + start_code_size;
|
|
|
|
|
2016-08-25 22:44:25 +00:00
|
|
|
// |next_nalu_info_| is made global intentionally to avoid repetitive memory
|
|
|
|
// allocation which could create memory fragments.
|
|
|
|
if (!next_nalu_info_)
|
|
|
|
next_nalu_info_.reset(new NaluInfo);
|
|
|
|
if (!next_nalu_info_->nalu.Initialize(type_, next_nalu_ptr, next_nalu_size)) {
|
|
|
|
// This NAL unit is invalid, skip it and search again.
|
|
|
|
return SearchForNalu(position, nalu);
|
2016-03-31 19:48:16 +00:00
|
|
|
}
|
2016-08-25 22:44:25 +00:00
|
|
|
next_nalu_info_->position = current_search_position_ - start_code_size;
|
|
|
|
next_nalu_info_->start_code_size = start_code_size;
|
|
|
|
|
|
|
|
const bool current_nalu_set = current_nalu_info_ ? true : false;
|
|
|
|
if (current_nalu_info_) {
|
|
|
|
// Starting position for the nalu including start code.
|
|
|
|
*position = current_nalu_info_->position;
|
|
|
|
// Update the NALU because the data pointer may have been invalidated.
|
|
|
|
const uint8_t* current_nalu_ptr =
|
|
|
|
next_nalu_ptr +
|
|
|
|
(current_nalu_info_->position + current_nalu_info_->start_code_size) -
|
|
|
|
current_search_position_;
|
|
|
|
const uint64_t current_nalu_size = next_nalu_info_->position -
|
|
|
|
current_nalu_info_->position -
|
|
|
|
current_nalu_info_->start_code_size;
|
|
|
|
CHECK(nalu->Initialize(type_, current_nalu_ptr, current_nalu_size));
|
2016-03-31 19:48:16 +00:00
|
|
|
}
|
2016-08-25 22:44:25 +00:00
|
|
|
current_nalu_info_.swap(next_nalu_info_);
|
|
|
|
return current_nalu_set ? true : SearchForNalu(position, nalu);
|
2016-03-31 19:48:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:05:45 +00:00
|
|
|
bool EsParserH26x::ParseInternal() {
|
2016-08-25 22:44:25 +00:00
|
|
|
uint64_t position;
|
|
|
|
Nalu nalu;
|
|
|
|
VideoSliceInfo video_slice_info;
|
|
|
|
while (SearchForNalu(&position, &nalu)) {
|
2016-05-03 22:05:45 +00:00
|
|
|
// ITU H.264 sec. 7.4.1.2.3
|
|
|
|
// H264: The first of the NAL units with |can_start_access_unit() == true|
|
|
|
|
// after the last VCL NAL unit of a primary coded picture specifies the
|
2016-08-25 22:44:25 +00:00
|
|
|
// start of a new access unit.
|
2016-05-03 22:05:45 +00:00
|
|
|
// ITU H.265 sec. 7.4.2.4.4
|
|
|
|
// H265: The first of the NAL units with |can_start_access_unit() == true|
|
|
|
|
// after the last VCL NAL unit preceding firstBlPicNalUnit (the first
|
|
|
|
// VCL NAL unit of a coded picture with nuh_layer_id equal to 0), if
|
|
|
|
// any, specifies the start of a new access unit.
|
2016-08-25 22:44:25 +00:00
|
|
|
if (nalu.can_start_access_unit()) {
|
|
|
|
if (!next_access_unit_position_set_) {
|
|
|
|
next_access_unit_position_set_ = true;
|
|
|
|
next_access_unit_position_ = position;
|
|
|
|
}
|
|
|
|
RCHECK(ProcessNalu(nalu, &video_slice_info));
|
2017-03-01 02:10:28 +00:00
|
|
|
if (nalu.is_vcl() && !video_slice_info.valid) {
|
2016-08-25 22:44:25 +00:00
|
|
|
// This could happen only if decoder config is not available yet. Drop
|
|
|
|
// this frame.
|
|
|
|
DCHECK(!current_video_slice_info_.valid);
|
|
|
|
next_access_unit_position_set_ = false;
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-01 02:10:28 +00:00
|
|
|
} else if (nalu.is_vcl()) {
|
2016-08-25 22:44:25 +00:00
|
|
|
// This isn't the first VCL NAL unit. Next access unit should start after
|
|
|
|
// this NAL unit.
|
|
|
|
next_access_unit_position_set_ = false;
|
2016-05-03 22:05:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-25 22:44:25 +00:00
|
|
|
// AUD shall be the first NAL unit if present. There shall be at most one
|
|
|
|
// AUD in any access unit. We can emit the current access unit which shall
|
|
|
|
// not contain the AUD.
|
2018-12-10 22:12:01 +00:00
|
|
|
if (nalu.is_aud()) {
|
|
|
|
RCHECK(EmitCurrentAccessUnit());
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-25 22:44:25 +00:00
|
|
|
|
|
|
|
// We can only determine if the current access unit ends after seeing
|
|
|
|
// another VCL NAL unit.
|
|
|
|
if (!video_slice_info.valid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check if it is the first VCL NAL unit of a primary coded picture. It is
|
|
|
|
// always true for H265 as nuh_layer_id shall be == 0 at this point.
|
|
|
|
bool is_first_vcl_nalu = true;
|
|
|
|
if (type_ == Nalu::kH264) {
|
|
|
|
if (current_video_slice_info_.valid) {
|
|
|
|
// ITU H.264 sec. 7.4.1.2.4 Detection of the first VCL NAL unit of a
|
|
|
|
// primary coded picture. Only pps_id and frame_num are checked here.
|
|
|
|
is_first_vcl_nalu =
|
|
|
|
video_slice_info.frame_num != current_video_slice_info_.frame_num ||
|
|
|
|
video_slice_info.pps_id != current_video_slice_info_.pps_id;
|
2016-05-03 22:05:45 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-25 22:44:25 +00:00
|
|
|
if (!is_first_vcl_nalu) {
|
|
|
|
// This isn't the first VCL NAL unit. Next access unit should start after
|
|
|
|
// this NAL unit.
|
|
|
|
next_access_unit_position_set_ = false;
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-03 22:05:45 +00:00
|
|
|
|
2016-08-25 22:44:25 +00:00
|
|
|
DCHECK(next_access_unit_position_set_);
|
|
|
|
RCHECK(EmitCurrentAccessUnit());
|
2016-05-03 22:05:45 +00:00
|
|
|
|
|
|
|
// Delete the data we have already processed.
|
2016-08-25 22:44:25 +00:00
|
|
|
es_queue_->Trim(next_access_unit_position_);
|
|
|
|
|
|
|
|
current_access_unit_position_ = next_access_unit_position_;
|
|
|
|
current_video_slice_info_ = video_slice_info;
|
|
|
|
next_access_unit_position_set_ = false;
|
2016-05-03 22:05:45 +00:00
|
|
|
}
|
2016-08-25 22:44:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EsParserH26x::EmitCurrentAccessUnit() {
|
|
|
|
if (current_video_slice_info_.valid) {
|
|
|
|
if (current_video_slice_info_.is_key_frame)
|
|
|
|
waiting_for_key_frame_ = false;
|
|
|
|
if (!waiting_for_key_frame_) {
|
|
|
|
RCHECK(
|
|
|
|
EmitFrame(current_access_unit_position_,
|
|
|
|
next_access_unit_position_ - current_access_unit_position_,
|
|
|
|
current_video_slice_info_.is_key_frame,
|
|
|
|
current_video_slice_info_.pps_id));
|
|
|
|
}
|
|
|
|
current_video_slice_info_.valid = false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-05-03 22:05:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 19:48:16 +00:00
|
|
|
bool EsParserH26x::EmitFrame(int64_t access_unit_pos,
|
|
|
|
int access_unit_size,
|
|
|
|
bool is_key_frame,
|
|
|
|
int pps_id) {
|
|
|
|
// Get the access unit timing info.
|
|
|
|
TimingDesc current_timing_desc = {kNoTimestamp, kNoTimestamp};
|
|
|
|
while (!timing_desc_list_.empty() &&
|
|
|
|
timing_desc_list_.front().first <= access_unit_pos) {
|
|
|
|
current_timing_desc = timing_desc_list_.front().second;
|
|
|
|
timing_desc_list_.pop_front();
|
|
|
|
}
|
|
|
|
if (current_timing_desc.pts == kNoTimestamp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Emit a frame.
|
2016-05-03 22:05:45 +00:00
|
|
|
DVLOG(LOG_LEVEL_ES) << "Emit frame: stream_pos=" << access_unit_pos
|
2018-12-10 22:12:01 +00:00
|
|
|
<< " size=" << access_unit_size << " pts "
|
|
|
|
<< current_timing_desc.pts << " timing_desc_list size "
|
|
|
|
<< timing_desc_list_.size();
|
2016-03-31 19:48:16 +00:00
|
|
|
int es_size;
|
|
|
|
const uint8_t* es;
|
2016-05-03 22:05:45 +00:00
|
|
|
es_queue_->PeekAt(access_unit_pos, &es, &es_size);
|
2016-03-31 19:48:16 +00:00
|
|
|
|
|
|
|
// Convert frame to unit stream format.
|
|
|
|
std::vector<uint8_t> converted_frame;
|
|
|
|
if (!stream_converter_->ConvertByteStreamToNalUnitStream(
|
|
|
|
es, access_unit_size, &converted_frame)) {
|
|
|
|
DLOG(ERROR) << "Failure to convert video frame to unit stream format.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the video decoder configuration if needed.
|
|
|
|
RCHECK(UpdateVideoDecoderConfig(pps_id));
|
|
|
|
|
|
|
|
// Create the media sample, emitting always the previous sample after
|
|
|
|
// calculating its duration.
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> media_sample = MediaSample::CopyFrom(
|
2016-03-31 19:48:16 +00:00
|
|
|
converted_frame.data(), converted_frame.size(), is_key_frame);
|
|
|
|
media_sample->set_dts(current_timing_desc.dts);
|
|
|
|
media_sample->set_pts(current_timing_desc.pts);
|
|
|
|
if (pending_sample_) {
|
2018-08-22 20:34:48 +00:00
|
|
|
if (media_sample->dts() <= pending_sample_->dts()) {
|
|
|
|
LOG(WARNING) << "[MPEG-2 TS] PID " << pid() << " dts "
|
|
|
|
<< media_sample->dts()
|
|
|
|
<< " less than or equal to previous dts "
|
|
|
|
<< pending_sample_->dts();
|
|
|
|
// Keep the sample but adjust the sample duration to a very small value,
|
|
|
|
// in case that the sample is still needed for the decoding afterwards.
|
|
|
|
const int64_t kArbitrarySmallDuration = 0.001 * kMpeg2Timescale; // 1ms.
|
|
|
|
pending_sample_->set_duration(kArbitrarySmallDuration);
|
|
|
|
} else {
|
2021-08-04 18:56:44 +00:00
|
|
|
int64_t sample_duration = media_sample->dts() - pending_sample_->dts();
|
2019-03-26 22:16:30 +00:00
|
|
|
pending_sample_->set_duration(sample_duration);
|
|
|
|
|
|
|
|
const int kArbitraryGapScale = 10;
|
2022-10-28 03:22:17 +00:00
|
|
|
if (pending_sample_duration_ &&
|
|
|
|
sample_duration > kArbitraryGapScale * pending_sample_duration_) {
|
2019-03-26 22:16:30 +00:00
|
|
|
LOG(WARNING) << "[MPEG-2 TS] PID " << pid() << " Possible GAP at dts "
|
|
|
|
<< pending_sample_->dts() << " with next sample at dts "
|
|
|
|
<< media_sample->dts() << " (difference "
|
|
|
|
<< sample_duration << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
pending_sample_duration_ = sample_duration;
|
2018-08-22 20:34:48 +00:00
|
|
|
}
|
2023-08-31 23:59:46 +00:00
|
|
|
emit_sample_cb_(std::move(pending_sample_));
|
2016-03-31 19:48:16 +00:00
|
|
|
}
|
|
|
|
pending_sample_ = media_sample;
|
2022-10-28 03:22:17 +00:00
|
|
|
pending_sample_pps_id_ = pps_id;
|
2016-03-31 19:48:16 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp2t
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|