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 if (color_range_ == 0)
164 vp_config.set_video_full_range_flag(
false);
165 else if (color_range_ == 1)
166 vp_config.set_video_full_range_flag(
true);
169 if (transfer_characteristics_ != -1) {
170 vp_config.set_transfer_characteristics(transfer_characteristics_);
172 if (color_primaries_ != -1) {
173 vp_config.set_color_primaries(color_primaries_);
179 return id == kWebMIdColor ||
id == kWebMIdProjection
181 : WebMParserClient::OnListStart(
id);
184bool WebMVideoClient::OnListEnd(
int id) {
185 return id == kWebMIdColor ||
id == kWebMIdProjection
187 : WebMParserClient::OnListEnd(
id);
190bool WebMVideoClient::OnUInt(
int id, int64_t val) {
191 int64_t* dst =
nullptr;
194 case kWebMIdPixelWidth:
197 case kWebMIdPixelHeight:
198 dst = &pixel_height_;
200 case kWebMIdPixelCropTop:
203 case kWebMIdPixelCropBottom:
206 case kWebMIdPixelCropLeft:
209 case kWebMIdPixelCropRight:
212 case kWebMIdDisplayWidth:
213 dst = &display_width_;
215 case kWebMIdDisplayHeight:
216 dst = &display_height_;
218 case kWebMIdDisplayUnit:
219 dst = &display_unit_;
221 case kWebMIdAlphaMode:
224 case kWebMIdColorMatrixCoefficients:
225 dst = &matrix_coefficients_;
227 case kWebMIdColorBitsPerChannel:
228 dst = &bits_per_channel_;
230 case kWebMIdColorChromaSubsamplingHorz:
231 dst = &chroma_subsampling_horz_;
233 case kWebMIdColorChromaSubsamplingVert:
234 dst = &chroma_subsampling_vert_;
236 case kWebMIdColorChromaSitingHorz:
237 dst = &chroma_siting_horz_;
239 case kWebMIdColorChromaSitingVert:
240 dst = &chroma_siting_vert_;
242 case kWebMIdColorRange:
245 case kWebMIdColorTransferCharacteristics:
246 dst = &transfer_characteristics_;
248 case kWebMIdColorPrimaries:
249 dst = &color_primaries_;
251 case kWebMIdColorMaxCLL:
252 case kWebMIdColorMaxFALL:
253 NOTIMPLEMENTED() <<
"HDR is not supported yet.";
255 case kWebMIdProjectionType:
256 LOG(WARNING) <<
"Ignoring ProjectionType with value " << val;
263 LOG(ERROR) <<
"Multiple values for id " << std::hex <<
id <<
" specified ("
264 << *dst <<
" and " << val <<
")";
272bool WebMVideoClient::OnBinary(
int ,
279bool WebMVideoClient::OnFloat(
int ,
double ) {
285 return color_range_ != -1 || color_primaries_ != -1 ||
286 transfer_characteristics_ != -1 || matrix_coefficients_ != -1;
292 writer.
AppendInt(
static_cast<uint32_t
>(0));
298 (color_primaries_ != -1) ?
static_cast<uint16_t
>(color_primaries_) : 1;
299 uint16_t transfer = (transfer_characteristics_ != -1)
300 ?
static_cast<uint16_t
>(transfer_characteristics_)
302 uint16_t matrix = (matrix_coefficients_ != -1)
303 ?
static_cast<uint16_t
>(matrix_coefficients_)
311 uint8_t full_range_flag = 0;
312 if (color_range_ != -1) {
313 full_range_flag = (color_range_ == 1) ? 1 : 0;
317 const uint8_t* buffer = writer.
Buffer();
318 size_t buffer_size = writer.Size();
320 std::vector<uint8_t> data(buffer, buffer + buffer_size);
321 uint32_t box_size =
static_cast<uint32_t
>(data.size());
324 data[0] = (box_size >> 24) & 0xFF;
325 data[1] = (box_size >> 16) & 0xFF;
326 data[2] = (box_size >> 8) & 0xFF;
327 data[3] = box_size & 0xFF;
All the methods that are virtual are virtual for mocking.