5#include <packager/media/formats/webm/webm_video_client.h>
7#include <absl/log/log.h>
9#include <packager/macros/logging.h>
10#include <packager/media/base/buffer_writer.h>
11#include <packager/media/base/fourccs.h>
12#include <packager/media/base/video_util.h>
13#include <packager/media/codecs/av1_codec_configuration_record.h>
14#include <packager/media/codecs/vp_codec_configuration_record.h>
15#include <packager/media/formats/webm/webm_constants.h>
20const int32_t kWebMTimeScale = 1000000;
27WebMVideoClient::WebMVideoClient() {}
29WebMVideoClient::~WebMVideoClient() {}
43 matrix_coefficients_ = -1;
44 bits_per_channel_ = -1;
45 chroma_subsampling_horz_ = -1;
46 chroma_subsampling_vert_ = -1;
47 chroma_siting_horz_ = -1;
48 chroma_siting_vert_ = -1;
50 transfer_characteristics_ = -1;
51 color_primaries_ = -1;
56 const std::string& codec_id,
57 const std::vector<uint8_t>& codec_private,
59 std::string codec_string;
60 Codec video_codec = kUnknownCodec;
61 if (codec_id ==
"V_AV1") {
62 video_codec = kCodecAV1;
67 if (!av1_config.
Parse(codec_private)) {
68 LOG(ERROR) <<
"Failed to parse AV1 codec_private.";
72 }
else if (codec_id ==
"V_VP8") {
73 video_codec = kCodecVP8;
75 }
else if (codec_id ==
"V_VP9") {
76 video_codec = kCodecVP9;
79 LOG(ERROR) <<
"Unsupported video codec_id " << codec_id;
83 if (pixel_width_ <= 0 || pixel_height_ <= 0)
87 if (crop_bottom_ == -1)
96 if (crop_right_ == -1)
99 if (display_unit_ == -1)
102 uint16_t width_after_crop = pixel_width_ - (crop_left_ + crop_right_);
103 uint16_t height_after_crop = pixel_height_ - (crop_top_ + crop_bottom_);
105 if (display_unit_ == 0) {
106 if (display_width_ <= 0)
107 display_width_ = width_after_crop;
108 if (display_height_ <= 0)
109 display_height_ = height_after_crop;
110 }
else if (display_unit_ == 3) {
111 if (display_width_ <= 0 || display_height_ <= 0)
114 LOG(ERROR) <<
"Unsupported display unit type " << display_unit_;
119 uint32_t pixel_width;
120 uint32_t pixel_height;
121 DerivePixelWidthHeight(width_after_crop, height_after_crop, display_width_,
122 display_height_, &pixel_width, &pixel_height);
127 auto video_stream_info = std::make_shared<VideoStreamInfo>(
128 track_num, kWebMTimeScale, 0, video_codec, H26xStreamFormat::kUnSpecified,
129 codec_string, codec_private.data(), codec_private.size(),
130 width_after_crop, height_after_crop, pixel_width, pixel_height, 0, 0, 0,
131 0, 0, std::string(), is_encrypted);
134 if ((video_codec == kCodecVP8 || video_codec == kCodecVP9) &&
137 if (!colr_data.empty()) {
138 video_stream_info->set_colr_data(colr_data.data(), colr_data.size());
142 return video_stream_info;
146 const std::vector<uint8_t>& codec_private) {
149 if (matrix_coefficients_ != -1) {
150 vp_config.set_matrix_coefficients(matrix_coefficients_);
152 if (bits_per_channel_ != -1) {
153 vp_config.set_bit_depth(bits_per_channel_);
155 if (chroma_subsampling_horz_ != -1 && chroma_subsampling_vert_ != -1) {
156 vp_config.SetChromaSubsampling(chroma_subsampling_horz_,
157 chroma_subsampling_vert_);
159 if (chroma_siting_horz_ != -1 && chroma_siting_vert_ != -1) {
160 vp_config.SetChromaLocation(chroma_siting_horz_, chroma_siting_vert_);
162 if (color_range_ != -1) {
163 vp_config.set_video_full_range_flag(color_range_ == 2);
165 if (transfer_characteristics_ != -1) {
166 vp_config.set_transfer_characteristics(transfer_characteristics_);
168 if (color_primaries_ != -1) {
169 vp_config.set_color_primaries(color_primaries_);
175 return id == kWebMIdColor ||
id == kWebMIdProjection
177 : WebMParserClient::OnListStart(
id);
180bool WebMVideoClient::OnListEnd(
int id) {
181 return id == kWebMIdColor ||
id == kWebMIdProjection
183 : WebMParserClient::OnListEnd(
id);
186bool WebMVideoClient::OnUInt(
int id, int64_t val) {
187 int64_t* dst =
nullptr;
190 case kWebMIdPixelWidth:
193 case kWebMIdPixelHeight:
194 dst = &pixel_height_;
196 case kWebMIdPixelCropTop:
199 case kWebMIdPixelCropBottom:
202 case kWebMIdPixelCropLeft:
205 case kWebMIdPixelCropRight:
208 case kWebMIdDisplayWidth:
209 dst = &display_width_;
211 case kWebMIdDisplayHeight:
212 dst = &display_height_;
214 case kWebMIdDisplayUnit:
215 dst = &display_unit_;
217 case kWebMIdAlphaMode:
220 case kWebMIdColorMatrixCoefficients:
221 dst = &matrix_coefficients_;
223 case kWebMIdColorBitsPerChannel:
224 dst = &bits_per_channel_;
226 case kWebMIdColorChromaSubsamplingHorz:
227 dst = &chroma_subsampling_horz_;
229 case kWebMIdColorChromaSubsamplingVert:
230 dst = &chroma_subsampling_vert_;
232 case kWebMIdColorChromaSitingHorz:
233 dst = &chroma_siting_horz_;
235 case kWebMIdColorChromaSitingVert:
236 dst = &chroma_siting_vert_;
238 case kWebMIdColorRange:
241 case kWebMIdColorTransferCharacteristics:
242 dst = &transfer_characteristics_;
244 case kWebMIdColorPrimaries:
245 dst = &color_primaries_;
247 case kWebMIdColorMaxCLL:
248 case kWebMIdColorMaxFALL:
249 NOTIMPLEMENTED() <<
"HDR is not supported yet.";
251 case kWebMIdProjectionType:
252 LOG(WARNING) <<
"Ignoring ProjectionType with value " << val;
259 LOG(ERROR) <<
"Multiple values for id " << std::hex <<
id <<
" specified ("
260 << *dst <<
" and " << val <<
")";
268bool WebMVideoClient::OnBinary(
int ,
275bool WebMVideoClient::OnFloat(
int ,
double ) {
281 return color_range_ != -1 || color_primaries_ != -1 ||
282 transfer_characteristics_ != -1 || matrix_coefficients_ != -1;
288 writer.
AppendInt(
static_cast<uint32_t
>(0));
294 (color_primaries_ != -1) ?
static_cast<uint16_t
>(color_primaries_) : 1;
295 uint16_t transfer = (transfer_characteristics_ != -1)
296 ?
static_cast<uint16_t
>(transfer_characteristics_)
298 uint16_t matrix = (matrix_coefficients_ != -1)
299 ?
static_cast<uint16_t
>(matrix_coefficients_)
307 uint8_t full_range_flag = 0;
308 if (color_range_ != -1) {
309 full_range_flag = (color_range_ == 2) ? 1 : 0;
313 const uint8_t* buffer = writer.
Buffer();
314 size_t buffer_size = writer.Size();
316 std::vector<uint8_t> data(buffer, buffer + buffer_size);
317 uint32_t box_size =
static_cast<uint32_t
>(data.size());
320 data[0] = (box_size >> 24) & 0xFF;
321 data[1] = (box_size >> 16) & 0xFF;
322 data[2] = (box_size >> 8) & 0xFF;
323 data[3] = box_size & 0xFF;
All the methods that are virtual are virtual for mocking.