2015-10-08 21:48:07 +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.
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/formats/webm/webm_tracks_parser.h>
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2023-10-09 23:21:41 +00:00
|
|
|
#include <absl/strings/str_format.h>
|
|
|
|
#include <glog/logging.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/timestamp.h>
|
|
|
|
#include <packager/media/formats/webm/webm_constants.h>
|
|
|
|
#include <packager/media/formats/webm/webm_content_encodings.h>
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-10-08 21:48:07 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
static TextKind CodecIdToTextKind(const std::string& codec_id) {
|
|
|
|
if (codec_id == kWebMCodecSubtitles)
|
|
|
|
return kTextSubtitles;
|
|
|
|
|
|
|
|
if (codec_id == kWebMCodecCaptions)
|
|
|
|
return kTextCaptions;
|
|
|
|
|
|
|
|
if (codec_id == kWebMCodecDescriptions)
|
|
|
|
return kTextDescriptions;
|
|
|
|
|
|
|
|
if (codec_id == kWebMCodecMetadata)
|
|
|
|
return kTextMetadata;
|
|
|
|
|
|
|
|
return kTextNone;
|
|
|
|
}
|
|
|
|
|
2015-10-14 23:12:10 +00:00
|
|
|
static int64_t PrecisionCappedDefaultDuration(
|
2015-10-14 22:46:23 +00:00
|
|
|
const double timecode_scale_in_us,
|
|
|
|
const int64_t duration_in_ns) {
|
2015-10-08 21:48:07 +00:00
|
|
|
if (duration_in_ns <= 0)
|
2015-10-14 23:12:10 +00:00
|
|
|
return kNoTimestamp;
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
int64_t mult = duration_in_ns / 1000;
|
2015-10-08 21:48:07 +00:00
|
|
|
mult /= timecode_scale_in_us;
|
|
|
|
if (mult == 0)
|
2015-10-14 23:12:10 +00:00
|
|
|
return kNoTimestamp;
|
2015-10-08 21:48:07 +00:00
|
|
|
|
|
|
|
mult = static_cast<double>(mult) * timecode_scale_in_us;
|
2015-10-14 23:12:10 +00:00
|
|
|
return mult;
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
WebMTracksParser::WebMTracksParser(bool ignore_text_tracks)
|
2015-10-08 21:48:07 +00:00
|
|
|
: track_type_(-1),
|
|
|
|
track_num_(-1),
|
|
|
|
seek_preroll_(-1),
|
|
|
|
codec_delay_(-1),
|
|
|
|
default_duration_(-1),
|
|
|
|
audio_track_num_(-1),
|
|
|
|
audio_default_duration_(-1),
|
|
|
|
video_track_num_(-1),
|
|
|
|
video_default_duration_(-1),
|
|
|
|
ignore_text_tracks_(ignore_text_tracks),
|
2015-10-14 22:46:23 +00:00
|
|
|
audio_client_(),
|
|
|
|
video_client_() {
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebMTracksParser::~WebMTracksParser() {}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
int WebMTracksParser::Parse(const uint8_t* buf, int size) {
|
2015-10-08 21:48:07 +00:00
|
|
|
track_type_ =-1;
|
|
|
|
track_num_ = -1;
|
|
|
|
default_duration_ = -1;
|
|
|
|
track_name_.clear();
|
|
|
|
track_language_.clear();
|
|
|
|
audio_track_num_ = -1;
|
|
|
|
audio_default_duration_ = -1;
|
2015-10-14 23:12:10 +00:00
|
|
|
audio_stream_info_ = nullptr;
|
2015-10-08 21:48:07 +00:00
|
|
|
video_track_num_ = -1;
|
|
|
|
video_default_duration_ = -1;
|
2015-10-14 23:12:10 +00:00
|
|
|
video_stream_info_ = nullptr;
|
2015-10-08 21:48:07 +00:00
|
|
|
text_tracks_.clear();
|
|
|
|
ignored_tracks_.clear();
|
|
|
|
|
|
|
|
WebMListParser parser(kWebMIdTracks, this);
|
|
|
|
int result = parser.Parse(buf, size);
|
|
|
|
|
|
|
|
if (result <= 0)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
// For now we do all or nothing parsing.
|
|
|
|
return parser.IsParsingComplete() ? result : 0;
|
|
|
|
}
|
|
|
|
|
2015-10-14 23:12:10 +00:00
|
|
|
int64_t WebMTracksParser::GetAudioDefaultDuration(
|
2015-10-08 21:48:07 +00:00
|
|
|
const double timecode_scale_in_us) const {
|
|
|
|
return PrecisionCappedDefaultDuration(timecode_scale_in_us,
|
|
|
|
audio_default_duration_);
|
|
|
|
}
|
|
|
|
|
2015-10-14 23:12:10 +00:00
|
|
|
int64_t WebMTracksParser::GetVideoDefaultDuration(
|
2015-10-08 21:48:07 +00:00
|
|
|
const double timecode_scale_in_us) const {
|
|
|
|
return PrecisionCappedDefaultDuration(timecode_scale_in_us,
|
|
|
|
video_default_duration_);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebMParserClient* WebMTracksParser::OnListStart(int id) {
|
|
|
|
if (id == kWebMIdContentEncodings) {
|
|
|
|
DCHECK(!track_content_encodings_client_.get());
|
2015-10-14 22:46:23 +00:00
|
|
|
track_content_encodings_client_.reset(new WebMContentEncodingsClient());
|
2015-10-08 21:48:07 +00:00
|
|
|
return track_content_encodings_client_->OnListStart(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == kWebMIdTrackEntry) {
|
|
|
|
track_type_ = -1;
|
|
|
|
track_num_ = -1;
|
|
|
|
default_duration_ = -1;
|
|
|
|
track_name_.clear();
|
|
|
|
track_language_.clear();
|
|
|
|
codec_id_ = "";
|
|
|
|
codec_private_.clear();
|
|
|
|
audio_client_.Reset();
|
|
|
|
video_client_.Reset();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == kWebMIdAudio)
|
|
|
|
return &audio_client_;
|
|
|
|
|
|
|
|
if (id == kWebMIdVideo)
|
|
|
|
return &video_client_;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebMTracksParser::OnListEnd(int id) {
|
|
|
|
if (id == kWebMIdContentEncodings) {
|
|
|
|
DCHECK(track_content_encodings_client_.get());
|
|
|
|
return track_content_encodings_client_->OnListEnd(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == kWebMIdTrackEntry) {
|
|
|
|
if (track_type_ == -1 || track_num_ == -1) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Missing TrackEntry data for "
|
|
|
|
<< " TrackType " << track_type_ << " TrackNum " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (track_type_ != kWebMTrackTypeAudio &&
|
|
|
|
track_type_ != kWebMTrackTypeVideo &&
|
|
|
|
track_type_ != kWebMTrackTypeSubtitlesOrCaptions &&
|
|
|
|
track_type_ != kWebMTrackTypeDescriptionsOrMetadata) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Unexpected TrackType " << track_type_;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextKind text_track_kind = kTextNone;
|
|
|
|
if (track_type_ == kWebMTrackTypeSubtitlesOrCaptions) {
|
|
|
|
text_track_kind = CodecIdToTextKind(codec_id_);
|
|
|
|
if (text_track_kind == kTextNone) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Missing TrackEntry CodecID"
|
|
|
|
<< " TrackNum " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text_track_kind != kTextSubtitles &&
|
|
|
|
text_track_kind != kTextCaptions) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Wrong TrackEntry CodecID"
|
|
|
|
<< " TrackNum " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (track_type_ == kWebMTrackTypeDescriptionsOrMetadata) {
|
|
|
|
text_track_kind = CodecIdToTextKind(codec_id_);
|
|
|
|
if (text_track_kind == kTextNone) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Missing TrackEntry CodecID"
|
|
|
|
<< " TrackNum " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text_track_kind != kTextDescriptions &&
|
|
|
|
text_track_kind != kTextMetadata) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Wrong TrackEntry CodecID"
|
|
|
|
<< " TrackNum " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string encryption_key_id;
|
|
|
|
if (track_content_encodings_client_) {
|
|
|
|
DCHECK(!track_content_encodings_client_->content_encodings().empty());
|
|
|
|
// If we have multiple ContentEncoding in one track. Always choose the
|
|
|
|
// key id in the first ContentEncoding as the key id of the track.
|
|
|
|
encryption_key_id = track_content_encodings_client_->
|
|
|
|
content_encodings()[0]->encryption_key_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (track_type_ == kWebMTrackTypeAudio) {
|
|
|
|
if (audio_track_num_ == -1) {
|
|
|
|
audio_track_num_ = track_num_;
|
|
|
|
audio_encryption_key_id_ = encryption_key_id;
|
|
|
|
|
|
|
|
if (default_duration_ == 0) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Illegal 0ns audio TrackEntry "
|
|
|
|
"DefaultDuration";
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
audio_default_duration_ = default_duration_;
|
|
|
|
|
2015-10-14 23:12:10 +00:00
|
|
|
DCHECK(!audio_stream_info_);
|
|
|
|
audio_stream_info_ = audio_client_.GetAudioStreamInfo(
|
2016-05-05 22:26:48 +00:00
|
|
|
audio_track_num_, codec_id_, codec_private_, seek_preroll_,
|
|
|
|
codec_delay_, track_language_, !audio_encryption_key_id_.empty());
|
2015-10-14 23:12:10 +00:00
|
|
|
if (!audio_stream_info_)
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2015-10-14 22:46:23 +00:00
|
|
|
DLOG(INFO) << "Ignoring audio track " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
ignored_tracks_.insert(track_num_);
|
|
|
|
}
|
|
|
|
} else if (track_type_ == kWebMTrackTypeVideo) {
|
|
|
|
if (video_track_num_ == -1) {
|
|
|
|
video_track_num_ = track_num_;
|
|
|
|
video_encryption_key_id_ = encryption_key_id;
|
|
|
|
|
|
|
|
if (default_duration_ == 0) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Illegal 0ns video TrackEntry "
|
|
|
|
"DefaultDuration";
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
video_default_duration_ = default_duration_;
|
|
|
|
|
2015-10-14 23:12:10 +00:00
|
|
|
DCHECK(!video_stream_info_);
|
|
|
|
video_stream_info_ = video_client_.GetVideoStreamInfo(
|
2018-08-25 02:04:00 +00:00
|
|
|
video_track_num_, codec_id_, codec_private_,
|
|
|
|
!video_encryption_key_id_.empty());
|
2015-10-14 23:12:10 +00:00
|
|
|
if (!video_stream_info_)
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
2018-09-11 21:53:55 +00:00
|
|
|
|
|
|
|
if (codec_id_ == "V_VP8" || codec_id_ == "V_VP9") {
|
|
|
|
vp_config_ = video_client_.GetVpCodecConfig(codec_private_);
|
|
|
|
const double kNanosecondsPerSecond = 1000000000.0;
|
|
|
|
if (codec_id_ == "V_VP9" &&
|
|
|
|
(!vp_config_.is_level_set() || vp_config_.level() == 0)) {
|
|
|
|
vp_config_.SetVP9Level(
|
|
|
|
video_stream_info_->width(), video_stream_info_->height(),
|
|
|
|
video_default_duration_ / kNanosecondsPerSecond);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 21:48:07 +00:00
|
|
|
} else {
|
2015-10-14 22:46:23 +00:00
|
|
|
DLOG(INFO) << "Ignoring video track " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
ignored_tracks_.insert(track_num_);
|
|
|
|
}
|
|
|
|
} else if (track_type_ == kWebMTrackTypeSubtitlesOrCaptions ||
|
|
|
|
track_type_ == kWebMTrackTypeDescriptionsOrMetadata) {
|
|
|
|
if (ignore_text_tracks_) {
|
2015-10-14 22:46:23 +00:00
|
|
|
DLOG(INFO) << "Ignoring text track " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
ignored_tracks_.insert(track_num_);
|
|
|
|
} else {
|
2022-12-17 05:40:00 +00:00
|
|
|
std::string track_num = absl::StrFormat("%d", track_num_);
|
2015-10-08 21:48:07 +00:00
|
|
|
text_tracks_[track_num_] = TextTrackConfig(
|
|
|
|
text_track_kind, track_name_, track_language_, track_num);
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Unexpected TrackType " << track_type_;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
track_type_ = -1;
|
|
|
|
track_num_ = -1;
|
|
|
|
default_duration_ = -1;
|
|
|
|
track_name_.clear();
|
|
|
|
track_language_.clear();
|
|
|
|
codec_id_ = "";
|
|
|
|
codec_private_.clear();
|
|
|
|
track_content_encodings_client_.reset();
|
|
|
|
|
|
|
|
audio_client_.Reset();
|
|
|
|
video_client_.Reset();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
bool WebMTracksParser::OnUInt(int id, int64_t val) {
|
|
|
|
int64_t* dst = NULL;
|
2015-10-08 21:48:07 +00:00
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
case kWebMIdTrackNumber:
|
|
|
|
dst = &track_num_;
|
|
|
|
break;
|
|
|
|
case kWebMIdTrackType:
|
|
|
|
dst = &track_type_;
|
|
|
|
break;
|
|
|
|
case kWebMIdSeekPreRoll:
|
|
|
|
dst = &seek_preroll_;
|
|
|
|
break;
|
|
|
|
case kWebMIdCodecDelay:
|
|
|
|
dst = &codec_delay_;
|
|
|
|
break;
|
|
|
|
case kWebMIdDefaultDuration:
|
|
|
|
dst = &default_duration_;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*dst != -1) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Multiple values for id " << std::hex << id << " specified";
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-17 05:40:00 +00:00
|
|
|
bool WebMTracksParser::OnFloat(int /*id*/, double /*val*/) {
|
2015-10-08 21:48:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
bool WebMTracksParser::OnBinary(int id, const uint8_t* data, int size) {
|
2015-10-08 21:48:07 +00:00
|
|
|
if (id == kWebMIdCodecPrivate) {
|
|
|
|
if (!codec_private_.empty()) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Multiple CodecPrivate fields in a track.";
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
codec_private_.assign(data, data + size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebMTracksParser::OnString(int id, const std::string& str) {
|
|
|
|
if (id == kWebMIdCodecID) {
|
|
|
|
if (!codec_id_.empty()) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Multiple CodecID fields in a track";
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
codec_id_ = str;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == kWebMIdName) {
|
|
|
|
track_name_ = str;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == kWebMIdLanguage) {
|
|
|
|
track_language_ = str;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|