This source file includes following definitions.
- next_access_unit_pos_
- Flush
- Reset
- FindAUD
- ParseInternal
- EmitFrame
- UpdateVideoDecoderConfig
#include "media/formats/mp2t/es_parser_h264.h"
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "media/base/buffers.h"
#include "media/base/stream_parser_buffer.h"
#include "media/base/video_frame.h"
#include "media/filters/h264_parser.h"
#include "media/formats/common/offset_byte_queue.h"
#include "media/formats/mp2t/mp2t_common.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h"
namespace media {
namespace mp2t {
const int kMinAUDSize = 4;
EsParserH264::EsParserH264(
const NewVideoConfigCB& new_video_config_cb,
const EmitBufferCB& emit_buffer_cb)
: new_video_config_cb_(new_video_config_cb),
emit_buffer_cb_(emit_buffer_cb),
es_queue_(new media::OffsetByteQueue()),
h264_parser_(new H264Parser()),
current_access_unit_pos_(0),
next_access_unit_pos_(0) {
}
EsParserH264::~EsParserH264() {
}
bool EsParserH264::Parse(const uint8* buf, int size,
base::TimeDelta pts,
base::TimeDelta dts) {
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;
timing_desc_list_.push_back(
std::pair<int64, TimingDesc>(es_queue_->tail(), timing_desc));
}
es_queue_->Push(buf, size);
return ParseInternal();
}
void EsParserH264::Flush() {
DVLOG(1) << "EsParserH264::Flush";
if (!FindAUD(¤t_access_unit_pos_))
return;
uint8 aud[] = { 0x00, 0x00, 0x01, 0x09 };
es_queue_->Push(aud, sizeof(aud));
ParseInternal();
}
void EsParserH264::Reset() {
DVLOG(1) << "EsParserH264::Reset";
es_queue_.reset(new media::OffsetByteQueue());
h264_parser_.reset(new H264Parser());
current_access_unit_pos_ = 0;
next_access_unit_pos_ = 0;
timing_desc_list_.clear();
last_video_decoder_config_ = VideoDecoderConfig();
}
bool EsParserH264::FindAUD(int64* stream_pos) {
while (true) {
const uint8* es;
int size;
es_queue_->PeekAt(*stream_pos, &es, &size);
off_t start_code_offset;
off_t start_code_size;
bool start_code_found = H264Parser::FindStartCode(
es, size, &start_code_offset, &start_code_size);
*stream_pos += start_code_offset;
if (!start_code_found || start_code_offset + start_code_size >= size)
return false;
if (es[start_code_offset + start_code_size] == H264NALU::kAUD)
break;
*stream_pos += start_code_size;
}
return true;
}
bool EsParserH264::ParseInternal() {
DCHECK_LE(es_queue_->head(), current_access_unit_pos_);
DCHECK_LE(current_access_unit_pos_, next_access_unit_pos_);
DCHECK_LE(next_access_unit_pos_, es_queue_->tail());
bool aud_found = FindAUD(¤t_access_unit_pos_);
es_queue_->Trim(current_access_unit_pos_);
if (next_access_unit_pos_ < current_access_unit_pos_)
next_access_unit_pos_ = current_access_unit_pos_;
if (!aud_found)
return true;
if (next_access_unit_pos_ < current_access_unit_pos_ + kMinAUDSize) {
next_access_unit_pos_ = current_access_unit_pos_ + kMinAUDSize;
DCHECK_LE(next_access_unit_pos_, es_queue_->tail());
}
if (!FindAUD(&next_access_unit_pos_))
return true;
bool is_key_frame = false;
int pps_id_for_access_unit = -1;
const uint8* es;
int size;
es_queue_->PeekAt(current_access_unit_pos_, &es, &size);
int access_unit_size = base::checked_cast<int, int64>(
next_access_unit_pos_ - current_access_unit_pos_);
DCHECK_LE(access_unit_size, size);
h264_parser_->SetStream(es, access_unit_size);
while (true) {
bool is_eos = false;
H264NALU nalu;
switch (h264_parser_->AdvanceToNextNALU(&nalu)) {
case H264Parser::kOk:
break;
case H264Parser::kInvalidStream:
case H264Parser::kUnsupportedStream:
return false;
case H264Parser::kEOStream:
is_eos = true;
break;
}
if (is_eos)
break;
switch (nalu.nal_unit_type) {
case H264NALU::kAUD: {
DVLOG(LOG_LEVEL_ES) << "NALU: AUD";
break;
}
case H264NALU::kSPS: {
DVLOG(LOG_LEVEL_ES) << "NALU: SPS";
int sps_id;
if (h264_parser_->ParseSPS(&sps_id) != H264Parser::kOk)
return false;
break;
}
case H264NALU::kPPS: {
DVLOG(LOG_LEVEL_ES) << "NALU: PPS";
int pps_id;
if (h264_parser_->ParsePPS(&pps_id) != H264Parser::kOk)
return false;
break;
}
case H264NALU::kIDRSlice:
case H264NALU::kNonIDRSlice: {
is_key_frame = (nalu.nal_unit_type == H264NALU::kIDRSlice);
DVLOG(LOG_LEVEL_ES) << "NALU: slice IDR=" << is_key_frame;
H264SliceHeader shdr;
if (h264_parser_->ParseSliceHeader(nalu, &shdr) != H264Parser::kOk) {
if (last_video_decoder_config_.IsValidConfig())
return false;
} else {
pps_id_for_access_unit = shdr.pic_parameter_set_id;
}
break;
}
default: {
DVLOG(LOG_LEVEL_ES) << "NALU: " << nalu.nal_unit_type;
}
}
}
RCHECK(EmitFrame(current_access_unit_pos_, access_unit_size,
is_key_frame, pps_id_for_access_unit));
current_access_unit_pos_ = next_access_unit_pos_;
es_queue_->Trim(current_access_unit_pos_);
return true;
}
bool EsParserH264::EmitFrame(int64 access_unit_pos, int access_unit_size,
bool is_key_frame, int pps_id) {
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;
const H264PPS* pps = h264_parser_->GetPPS(pps_id);
if (!pps) {
if (last_video_decoder_config_.IsValidConfig())
return false;
} else {
const H264SPS* sps = h264_parser_->GetSPS(pps->seq_parameter_set_id);
if (!sps)
return false;
RCHECK(UpdateVideoDecoderConfig(sps));
}
DVLOG(LOG_LEVEL_ES) << "Emit frame: stream_pos=" << current_access_unit_pos_
<< " size=" << access_unit_size;
int es_size;
const uint8* es;
es_queue_->PeekAt(current_access_unit_pos_, &es, &es_size);
CHECK_GE(es_size, access_unit_size);
scoped_refptr<StreamParserBuffer> stream_parser_buffer =
StreamParserBuffer::CopyFrom(
es,
access_unit_size,
is_key_frame,
DemuxerStream::VIDEO,
0);
stream_parser_buffer->SetDecodeTimestamp(current_timing_desc.dts);
stream_parser_buffer->set_timestamp(current_timing_desc.pts);
emit_buffer_cb_.Run(stream_parser_buffer);
return true;
}
bool EsParserH264::UpdateVideoDecoderConfig(const H264SPS* sps) {
int sar_width = (sps->sar_width == 0) ? 1 : sps->sar_width;
int sar_height = (sps->sar_height == 0) ? 1 : sps->sar_height;
gfx::Size coded_size((sps->pic_width_in_mbs_minus1 + 1) * 16,
(sps->pic_height_in_map_units_minus1 + 1) * 16);
gfx::Rect visible_rect(
sps->frame_crop_left_offset,
sps->frame_crop_top_offset,
(coded_size.width() - sps->frame_crop_right_offset) -
sps->frame_crop_left_offset,
(coded_size.height() - sps->frame_crop_bottom_offset) -
sps->frame_crop_top_offset);
if (visible_rect.width() <= 0 || visible_rect.height() <= 0)
return false;
gfx::Size natural_size(
(visible_rect.width() * sar_width) / sar_height,
visible_rect.height());
if (natural_size.width() == 0)
return false;
VideoDecoderConfig video_decoder_config(
kCodecH264,
VIDEO_CODEC_PROFILE_UNKNOWN,
VideoFrame::YV12,
coded_size,
visible_rect,
natural_size,
NULL, 0,
false);
if (!video_decoder_config.Matches(last_video_decoder_config_)) {
DVLOG(1) << "Profile IDC: " << sps->profile_idc;
DVLOG(1) << "Level IDC: " << sps->level_idc;
DVLOG(1) << "Pic width: " << coded_size.width();
DVLOG(1) << "Pic height: " << coded_size.height();
DVLOG(1) << "log2_max_frame_num_minus4: "
<< sps->log2_max_frame_num_minus4;
DVLOG(1) << "SAR: width=" << sps->sar_width
<< " height=" << sps->sar_height;
last_video_decoder_config_ = video_decoder_config;
new_video_config_cb_.Run(video_decoder_config);
}
return true;
}
}
}