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.
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
#include "packager/media/formats/webm/webm_video_client.h"
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2015-10-14 23:12:10 +00:00
|
|
|
#include "packager/base/logging.h"
|
2015-10-26 20:50:22 +00:00
|
|
|
#include "packager/base/stl_util.h"
|
2016-05-25 18:34:43 +00:00
|
|
|
#include "packager/media/codecs/vp_codec_configuration_record.h"
|
2015-10-14 22:46:23 +00:00
|
|
|
#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 {
|
|
|
|
|
|
|
|
// Timestamps are represented in double in WebM. Convert to uint64_t in us.
|
|
|
|
const uint32_t kWebMTimeScale = 1000000u;
|
|
|
|
|
|
|
|
int64_t GetGreatestCommonDivisor(int64_t a, int64_t b) {
|
|
|
|
while (b) {
|
|
|
|
int64_t temp = b;
|
|
|
|
b = a % b;
|
|
|
|
a = temp;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-10-08 21:48:07 +00:00
|
|
|
namespace media {
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
WebMVideoClient::WebMVideoClient() {
|
2015-10-08 21:48:07 +00:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
WebMVideoClient::~WebMVideoClient() {
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-14 23:12:10 +00:00
|
|
|
scoped_refptr<VideoStreamInfo> WebMVideoClient::GetVideoStreamInfo(
|
|
|
|
int64_t track_num,
|
2015-10-14 22:46:23 +00:00
|
|
|
const std::string& codec_id,
|
2016-05-25 18:34:43 +00:00
|
|
|
const std::vector<uint8_t>& codec_private_in,
|
2015-10-14 23:12:10 +00:00
|
|
|
bool is_encrypted) {
|
2016-05-25 18:34:43 +00:00
|
|
|
std::vector<uint8_t> codec_private = codec_private_in;
|
2015-10-08 21:48:07 +00:00
|
|
|
VideoCodec video_codec = kUnknownVideoCodec;
|
|
|
|
if (codec_id == "V_VP8") {
|
|
|
|
video_codec = kCodecVP8;
|
|
|
|
} else if (codec_id == "V_VP9") {
|
|
|
|
video_codec = kCodecVP9;
|
2016-05-25 18:34:43 +00:00
|
|
|
|
|
|
|
// Need to parse and convert the codec private data to MP4 format.
|
|
|
|
VPCodecConfigurationRecord vp_config;
|
|
|
|
if (!vp_config.ParseWebM(codec_private)) {
|
|
|
|
LOG(ERROR) << "Unable to parse VP9 codec configuration";
|
|
|
|
return scoped_refptr<VideoStreamInfo>();
|
|
|
|
}
|
|
|
|
vp_config.WriteMP4(&codec_private);
|
2015-10-26 20:50:22 +00:00
|
|
|
} else if (codec_id == "V_VP10") {
|
|
|
|
video_codec = kCodecVP10;
|
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;
|
2015-10-14 23:12:10 +00:00
|
|
|
return scoped_refptr<VideoStreamInfo>();
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pixel_width_ <= 0 || pixel_height_ <= 0)
|
2015-10-14 23:12:10 +00:00
|
|
|
return scoped_refptr<VideoStreamInfo>();
|
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)
|
2015-10-14 23:12:10 +00:00
|
|
|
return scoped_refptr<VideoStreamInfo>();
|
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_;
|
2015-10-14 23:12:10 +00:00
|
|
|
return scoped_refptr<VideoStreamInfo>();
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
2015-10-14 23:12:10 +00:00
|
|
|
// Calculate sample aspect ratio.
|
|
|
|
int64_t sar_x = display_width_ * height_after_crop;
|
|
|
|
int64_t sar_y = display_height_ * width_after_crop;
|
|
|
|
int64_t gcd = GetGreatestCommonDivisor(sar_x, sar_y);
|
|
|
|
sar_x /= gcd;
|
|
|
|
sar_y /= gcd;
|
|
|
|
|
2015-10-27 00:52:57 +00:00
|
|
|
return scoped_refptr<VideoStreamInfo>(new VideoStreamInfo(
|
2015-11-18 19:51:15 +00:00
|
|
|
track_num, kWebMTimeScale, 0, video_codec, std::string(), std::string(),
|
2016-05-25 18:34:43 +00:00
|
|
|
width_after_crop, height_after_crop, sar_x, sar_y, 0, 0,
|
|
|
|
codec_private.data(), codec_private.size(), is_encrypted));
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
bool WebMVideoClient::OnUInt(int id, int64_t val) {
|
|
|
|
int64_t* dst = NULL;
|
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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebMVideoClient::OnFloat(int id, double val) {
|
|
|
|
// Accept float fields we don't care about for now.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|