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_video_client.h>
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2023-10-13 19:42:47 +00:00
|
|
|
#include <absl/log/log.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
|
2023-10-14 16:36:01 +00:00
|
|
|
#include <packager/macros/logging.h>
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/video_util.h>
|
|
|
|
#include <packager/media/codecs/av1_codec_configuration_record.h>
|
|
|
|
#include <packager/media/codecs/vp_codec_configuration_record.h>
|
|
|
|
#include <packager/media/formats/webm/webm_constants.h>
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2015-10-14 23:12:10 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-08-04 18:56:44 +00:00
|
|
|
// Timestamps are represented in double in WebM. Convert to int64_t in us.
|
|
|
|
const int32_t kWebMTimeScale = 1000000;
|
2015-10-14 23:12:10 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-10-08 21:48:07 +00:00
|
|
|
namespace media {
|
|
|
|
|
2017-10-06 18:14:40 +00:00
|
|
|
WebMVideoClient::WebMVideoClient() {}
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2017-10-06 18:14:40 +00:00
|
|
|
WebMVideoClient::~WebMVideoClient() {}
|
2015-10-08 21:48:07 +00:00
|
|
|
|
|
|
|
void WebMVideoClient::Reset() {
|
|
|
|
pixel_width_ = -1;
|
|
|
|
pixel_height_ = -1;
|
|
|
|
crop_bottom_ = -1;
|
|
|
|
crop_top_ = -1;
|
|
|
|
crop_left_ = -1;
|
|
|
|
crop_right_ = -1;
|
|
|
|
display_width_ = -1;
|
|
|
|
display_height_ = -1;
|
|
|
|
display_unit_ = -1;
|
|
|
|
alpha_mode_ = -1;
|
2017-04-25 19:02:08 +00:00
|
|
|
|
2017-10-06 18:14:40 +00:00
|
|
|
matrix_coefficients_ = -1;
|
|
|
|
bits_per_channel_ = -1;
|
2017-04-25 19:02:08 +00:00
|
|
|
chroma_subsampling_horz_ = -1;
|
|
|
|
chroma_subsampling_vert_ = -1;
|
|
|
|
chroma_siting_horz_ = -1;
|
|
|
|
chroma_siting_vert_ = -1;
|
2017-10-06 18:14:40 +00:00
|
|
|
color_range_ = -1;
|
|
|
|
transfer_characteristics_ = -1;
|
|
|
|
color_primaries_ = -1;
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<VideoStreamInfo> WebMVideoClient::GetVideoStreamInfo(
|
2015-10-14 23:12:10 +00:00
|
|
|
int64_t track_num,
|
2015-10-14 22:46:23 +00:00
|
|
|
const std::string& codec_id,
|
2018-08-25 02:04:00 +00:00
|
|
|
const std::vector<uint8_t>& codec_private,
|
2015-10-14 23:12:10 +00:00
|
|
|
bool is_encrypted) {
|
2018-08-25 02:04:00 +00:00
|
|
|
std::string codec_string;
|
2016-07-27 00:51:08 +00:00
|
|
|
Codec video_codec = kUnknownCodec;
|
2018-08-25 02:04:00 +00:00
|
|
|
if (codec_id == "V_AV1") {
|
|
|
|
video_codec = kCodecAV1;
|
|
|
|
|
|
|
|
// CodecPrivate is mandatory per AV in Matroska / WebM specification.
|
|
|
|
// https://github.com/Matroska-Org/matroska-specification/blob/av1-mappin/codec/av1.md#codecprivate-1
|
|
|
|
AV1CodecConfigurationRecord av1_config;
|
|
|
|
if (!av1_config.Parse(codec_private)) {
|
|
|
|
LOG(ERROR) << "Failed to parse AV1 codec_private.";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
codec_string = av1_config.GetCodecString();
|
|
|
|
} else if (codec_id == "V_VP8") {
|
2015-10-08 21:48:07 +00:00
|
|
|
video_codec = kCodecVP8;
|
2018-08-25 02:04:00 +00:00
|
|
|
// codec_string for VP8 is parsed later.
|
2015-10-08 21:48:07 +00:00
|
|
|
} else if (codec_id == "V_VP9") {
|
|
|
|
video_codec = kCodecVP9;
|
2018-08-25 02:04:00 +00:00
|
|
|
// codec_string for VP9 is parsed later.
|
2015-10-08 21:48:07 +00:00
|
|
|
} else {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Unsupported video codec_id " << codec_id;
|
2018-08-25 02:04:00 +00:00
|
|
|
return nullptr;
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pixel_width_ <= 0 || pixel_height_ <= 0)
|
2018-08-25 02:04:00 +00:00
|
|
|
return nullptr;
|
2015-10-08 21:48:07 +00:00
|
|
|
|
|
|
|
// Set crop and display unit defaults if these elements are not present.
|
|
|
|
if (crop_bottom_ == -1)
|
|
|
|
crop_bottom_ = 0;
|
|
|
|
|
|
|
|
if (crop_top_ == -1)
|
|
|
|
crop_top_ = 0;
|
|
|
|
|
|
|
|
if (crop_left_ == -1)
|
|
|
|
crop_left_ = 0;
|
|
|
|
|
|
|
|
if (crop_right_ == -1)
|
|
|
|
crop_right_ = 0;
|
|
|
|
|
|
|
|
if (display_unit_ == -1)
|
|
|
|
display_unit_ = 0;
|
|
|
|
|
2015-10-14 23:12:10 +00:00
|
|
|
uint16_t width_after_crop = pixel_width_ - (crop_left_ + crop_right_);
|
|
|
|
uint16_t height_after_crop = pixel_height_ - (crop_top_ + crop_bottom_);
|
|
|
|
|
2015-10-08 21:48:07 +00:00
|
|
|
if (display_unit_ == 0) {
|
|
|
|
if (display_width_ <= 0)
|
2015-10-14 23:12:10 +00:00
|
|
|
display_width_ = width_after_crop;
|
2015-10-08 21:48:07 +00:00
|
|
|
if (display_height_ <= 0)
|
2015-10-14 23:12:10 +00:00
|
|
|
display_height_ = height_after_crop;
|
2015-10-08 21:48:07 +00:00
|
|
|
} else if (display_unit_ == 3) {
|
|
|
|
if (display_width_ <= 0 || display_height_ <= 0)
|
2018-08-25 02:04:00 +00:00
|
|
|
return nullptr;
|
2015-10-08 21:48:07 +00:00
|
|
|
} else {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Unsupported display unit type " << display_unit_;
|
2018-08-25 02:04:00 +00:00
|
|
|
return nullptr;
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
2019-08-03 00:19:13 +00:00
|
|
|
|
2015-10-14 23:12:10 +00:00
|
|
|
// Calculate sample aspect ratio.
|
2019-08-03 00:19:13 +00:00
|
|
|
uint32_t pixel_width;
|
|
|
|
uint32_t pixel_height;
|
|
|
|
DerivePixelWidthHeight(width_after_crop, height_after_crop, display_width_,
|
|
|
|
display_height_, &pixel_width, &pixel_height);
|
2015-10-14 23:12:10 +00:00
|
|
|
|
2018-08-25 02:04:00 +00:00
|
|
|
// |codec_private| may be overriden later for some codecs, e.g. VP9 since for
|
|
|
|
// VP9, the format for MP4 and WebM are different; MP4 format is used as the
|
|
|
|
// intermediate format.
|
2017-01-24 00:55:02 +00:00
|
|
|
return std::make_shared<VideoStreamInfo>(
|
2017-03-23 18:34:20 +00:00
|
|
|
track_num, kWebMTimeScale, 0, video_codec, H26xStreamFormat::kUnSpecified,
|
2018-08-25 02:04:00 +00:00
|
|
|
codec_string, codec_private.data(), codec_private.size(),
|
2019-08-03 00:19:13 +00:00
|
|
|
width_after_crop, height_after_crop, pixel_width, pixel_height, 0, 0,
|
2019-09-23 06:24:33 +00:00
|
|
|
0 /* transfer_characteristics */, std::string(), is_encrypted);
|
2017-04-25 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 18:14:40 +00:00
|
|
|
VPCodecConfigurationRecord WebMVideoClient::GetVpCodecConfig(
|
2017-04-25 19:02:08 +00:00
|
|
|
const std::vector<uint8_t>& codec_private) {
|
2017-10-06 18:14:40 +00:00
|
|
|
VPCodecConfigurationRecord vp_config;
|
|
|
|
vp_config.ParseWebM(codec_private);
|
|
|
|
if (matrix_coefficients_ != -1) {
|
|
|
|
vp_config.set_matrix_coefficients(matrix_coefficients_);
|
|
|
|
}
|
|
|
|
if (bits_per_channel_ != -1) {
|
|
|
|
vp_config.set_bit_depth(bits_per_channel_);
|
|
|
|
}
|
2017-04-25 19:02:08 +00:00
|
|
|
if (chroma_subsampling_horz_ != -1 && chroma_subsampling_vert_ != -1) {
|
2017-10-06 18:14:40 +00:00
|
|
|
vp_config.SetChromaSubsampling(chroma_subsampling_horz_,
|
|
|
|
chroma_subsampling_vert_);
|
2017-04-25 19:02:08 +00:00
|
|
|
}
|
|
|
|
if (chroma_siting_horz_ != -1 && chroma_siting_vert_ != -1) {
|
2017-10-06 18:14:40 +00:00
|
|
|
vp_config.SetChromaLocation(chroma_siting_horz_, chroma_siting_vert_);
|
2017-04-25 19:02:08 +00:00
|
|
|
}
|
2017-10-06 18:14:40 +00:00
|
|
|
if (color_range_ != -1) {
|
|
|
|
if (color_range_ == 0)
|
|
|
|
vp_config.set_video_full_range_flag(false);
|
|
|
|
else if (color_range_ == 1)
|
|
|
|
vp_config.set_video_full_range_flag(true);
|
|
|
|
// Ignore for other values.
|
|
|
|
}
|
|
|
|
if (transfer_characteristics_ != -1) {
|
|
|
|
vp_config.set_transfer_characteristics(transfer_characteristics_);
|
|
|
|
}
|
|
|
|
if (color_primaries_ != -1) {
|
|
|
|
vp_config.set_color_primaries(color_primaries_);
|
|
|
|
}
|
|
|
|
return vp_config;
|
2017-04-25 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebMParserClient* WebMVideoClient::OnListStart(int id) {
|
2021-05-07 17:13:02 +00:00
|
|
|
return id == kWebMIdColor || id == kWebMIdProjection
|
|
|
|
? this
|
|
|
|
: WebMParserClient::OnListStart(id);
|
2017-04-25 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebMVideoClient::OnListEnd(int id) {
|
2021-05-07 17:13:02 +00:00
|
|
|
return id == kWebMIdColor || id == kWebMIdProjection
|
|
|
|
? true
|
|
|
|
: WebMParserClient::OnListEnd(id);
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
bool WebMVideoClient::OnUInt(int id, int64_t val) {
|
2017-10-06 18:14:40 +00:00
|
|
|
int64_t* dst = nullptr;
|
2015-10-08 21:48:07 +00:00
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
case kWebMIdPixelWidth:
|
|
|
|
dst = &pixel_width_;
|
|
|
|
break;
|
|
|
|
case kWebMIdPixelHeight:
|
|
|
|
dst = &pixel_height_;
|
|
|
|
break;
|
|
|
|
case kWebMIdPixelCropTop:
|
|
|
|
dst = &crop_top_;
|
|
|
|
break;
|
|
|
|
case kWebMIdPixelCropBottom:
|
|
|
|
dst = &crop_bottom_;
|
|
|
|
break;
|
|
|
|
case kWebMIdPixelCropLeft:
|
|
|
|
dst = &crop_left_;
|
|
|
|
break;
|
|
|
|
case kWebMIdPixelCropRight:
|
|
|
|
dst = &crop_right_;
|
|
|
|
break;
|
|
|
|
case kWebMIdDisplayWidth:
|
|
|
|
dst = &display_width_;
|
|
|
|
break;
|
|
|
|
case kWebMIdDisplayHeight:
|
|
|
|
dst = &display_height_;
|
|
|
|
break;
|
|
|
|
case kWebMIdDisplayUnit:
|
|
|
|
dst = &display_unit_;
|
|
|
|
break;
|
|
|
|
case kWebMIdAlphaMode:
|
|
|
|
dst = &alpha_mode_;
|
|
|
|
break;
|
2017-04-25 19:02:08 +00:00
|
|
|
case kWebMIdColorMatrixCoefficients:
|
2017-10-06 18:14:40 +00:00
|
|
|
dst = &matrix_coefficients_;
|
2017-04-25 19:02:08 +00:00
|
|
|
break;
|
|
|
|
case kWebMIdColorBitsPerChannel:
|
2017-10-06 18:14:40 +00:00
|
|
|
dst = &bits_per_channel_;
|
2017-04-25 19:02:08 +00:00
|
|
|
break;
|
|
|
|
case kWebMIdColorChromaSubsamplingHorz:
|
|
|
|
dst = &chroma_subsampling_horz_;
|
|
|
|
break;
|
|
|
|
case kWebMIdColorChromaSubsamplingVert:
|
|
|
|
dst = &chroma_subsampling_vert_;
|
|
|
|
break;
|
|
|
|
case kWebMIdColorChromaSitingHorz:
|
|
|
|
dst = &chroma_siting_horz_;
|
|
|
|
break;
|
|
|
|
case kWebMIdColorChromaSitingVert:
|
|
|
|
dst = &chroma_siting_vert_;
|
|
|
|
break;
|
|
|
|
case kWebMIdColorRange:
|
2017-10-06 18:14:40 +00:00
|
|
|
dst = &color_range_;
|
2017-04-25 19:02:08 +00:00
|
|
|
break;
|
|
|
|
case kWebMIdColorTransferCharacteristics:
|
2017-10-06 18:14:40 +00:00
|
|
|
dst = &transfer_characteristics_;
|
2017-04-25 19:02:08 +00:00
|
|
|
break;
|
|
|
|
case kWebMIdColorPrimaries:
|
2017-10-06 18:14:40 +00:00
|
|
|
dst = &color_primaries_;
|
2017-04-25 19:02:08 +00:00
|
|
|
break;
|
|
|
|
case kWebMIdColorMaxCLL:
|
|
|
|
case kWebMIdColorMaxFALL:
|
|
|
|
NOTIMPLEMENTED() << "HDR is not supported yet.";
|
|
|
|
return true;
|
2021-05-07 17:13:02 +00:00
|
|
|
case kWebMIdProjectionType:
|
|
|
|
LOG(WARNING) << "Ignoring ProjectionType with value " << val;
|
|
|
|
return true;
|
2015-10-08 21:48:07 +00:00
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*dst != -1) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Multiple values for id " << std::hex << id << " specified ("
|
|
|
|
<< *dst << " and " << val << ")";
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-17 05:40:00 +00:00
|
|
|
bool WebMVideoClient::OnBinary(int /*id*/,
|
|
|
|
const uint8_t* /*data*/,
|
|
|
|
int /*size*/) {
|
2015-10-08 21:48:07 +00:00
|
|
|
// Accept binary fields we don't care about for now.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-17 05:40:00 +00:00
|
|
|
bool WebMVideoClient::OnFloat(int /*id*/, double /*val*/) {
|
2015-10-08 21:48:07 +00:00
|
|
|
// Accept float fields we don't care about for now.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|