Shaka Packager SDK
Loading...
Searching...
No Matches
webm_video_client.cc
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <packager/media/formats/webm/webm_video_client.h>
6
7#include <absl/log/log.h>
8
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>
16
17namespace {
18
19// Timestamps are represented in double in WebM. Convert to int64_t in us.
20const int32_t kWebMTimeScale = 1000000;
21
22} // namespace
23
24namespace shaka {
25namespace media {
26
27WebMVideoClient::WebMVideoClient() {}
28
29WebMVideoClient::~WebMVideoClient() {}
30
32 pixel_width_ = -1;
33 pixel_height_ = -1;
34 crop_bottom_ = -1;
35 crop_top_ = -1;
36 crop_left_ = -1;
37 crop_right_ = -1;
38 display_width_ = -1;
39 display_height_ = -1;
40 display_unit_ = -1;
41 alpha_mode_ = -1;
42
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;
49 color_range_ = -1;
50 transfer_characteristics_ = -1;
51 color_primaries_ = -1;
52}
53
54std::shared_ptr<VideoStreamInfo> WebMVideoClient::GetVideoStreamInfo(
55 int64_t track_num,
56 const std::string& codec_id,
57 const std::vector<uint8_t>& codec_private,
58 bool is_encrypted) {
59 std::string codec_string;
60 Codec video_codec = kUnknownCodec;
61 if (codec_id == "V_AV1") {
62 video_codec = kCodecAV1;
63
64 // CodecPrivate is mandatory per AV in Matroska / WebM specification.
65 // https://github.com/Matroska-Org/matroska-specification/blob/av1-mappin/codec/av1.md#codecprivate-1
67 if (!av1_config.Parse(codec_private)) {
68 LOG(ERROR) << "Failed to parse AV1 codec_private.";
69 return nullptr;
70 }
71 codec_string = av1_config.GetCodecString();
72 } else if (codec_id == "V_VP8") {
73 video_codec = kCodecVP8;
74 // codec_string for VP8 is parsed later.
75 } else if (codec_id == "V_VP9") {
76 video_codec = kCodecVP9;
77 // codec_string for VP9 is parsed later.
78 } else {
79 LOG(ERROR) << "Unsupported video codec_id " << codec_id;
80 return nullptr;
81 }
82
83 if (pixel_width_ <= 0 || pixel_height_ <= 0)
84 return nullptr;
85
86 // Set crop and display unit defaults if these elements are not present.
87 if (crop_bottom_ == -1)
88 crop_bottom_ = 0;
89
90 if (crop_top_ == -1)
91 crop_top_ = 0;
92
93 if (crop_left_ == -1)
94 crop_left_ = 0;
95
96 if (crop_right_ == -1)
97 crop_right_ = 0;
98
99 if (display_unit_ == -1)
100 display_unit_ = 0;
101
102 uint16_t width_after_crop = pixel_width_ - (crop_left_ + crop_right_);
103 uint16_t height_after_crop = pixel_height_ - (crop_top_ + crop_bottom_);
104
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)
112 return nullptr;
113 } else {
114 LOG(ERROR) << "Unsupported display unit type " << display_unit_;
115 return nullptr;
116 }
117
118 // Calculate sample aspect ratio.
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);
123
124 // |codec_private| may be overriden later for some codecs, e.g. VP9 since for
125 // VP9, the format for MP4 and WebM are different; MP4 format is used as the
126 // intermediate format.
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);
132
133 // Set colr box data for VP8/VP9 codecs
134 if ((video_codec == kCodecVP8 || video_codec == kCodecVP9) &&
135 HasColorInfo()) {
136 std::vector<uint8_t> colr_data = GenerateColrBoxData();
137 if (!colr_data.empty()) {
138 video_stream_info->set_colr_data(colr_data.data(), colr_data.size());
139 }
140 }
141
142 return video_stream_info;
143}
144
146 const std::vector<uint8_t>& codec_private) {
148 vp_config.ParseWebM(codec_private);
149 if (matrix_coefficients_ != -1) {
150 vp_config.set_matrix_coefficients(matrix_coefficients_);
151 }
152 if (bits_per_channel_ != -1) {
153 vp_config.set_bit_depth(bits_per_channel_);
154 }
155 if (chroma_subsampling_horz_ != -1 && chroma_subsampling_vert_ != -1) {
156 vp_config.SetChromaSubsampling(chroma_subsampling_horz_,
157 chroma_subsampling_vert_);
158 }
159 if (chroma_siting_horz_ != -1 && chroma_siting_vert_ != -1) {
160 vp_config.SetChromaLocation(chroma_siting_horz_, chroma_siting_vert_);
161 }
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);
167 // Ignore for other values.
168 }
169 if (transfer_characteristics_ != -1) {
170 vp_config.set_transfer_characteristics(transfer_characteristics_);
171 }
172 if (color_primaries_ != -1) {
173 vp_config.set_color_primaries(color_primaries_);
174 }
175 return vp_config;
176}
177
178WebMParserClient* WebMVideoClient::OnListStart(int id) {
179 return id == kWebMIdColor || id == kWebMIdProjection
180 ? this
181 : WebMParserClient::OnListStart(id);
182}
183
184bool WebMVideoClient::OnListEnd(int id) {
185 return id == kWebMIdColor || id == kWebMIdProjection
186 ? true
187 : WebMParserClient::OnListEnd(id);
188}
189
190bool WebMVideoClient::OnUInt(int id, int64_t val) {
191 int64_t* dst = nullptr;
192
193 switch (id) {
194 case kWebMIdPixelWidth:
195 dst = &pixel_width_;
196 break;
197 case kWebMIdPixelHeight:
198 dst = &pixel_height_;
199 break;
200 case kWebMIdPixelCropTop:
201 dst = &crop_top_;
202 break;
203 case kWebMIdPixelCropBottom:
204 dst = &crop_bottom_;
205 break;
206 case kWebMIdPixelCropLeft:
207 dst = &crop_left_;
208 break;
209 case kWebMIdPixelCropRight:
210 dst = &crop_right_;
211 break;
212 case kWebMIdDisplayWidth:
213 dst = &display_width_;
214 break;
215 case kWebMIdDisplayHeight:
216 dst = &display_height_;
217 break;
218 case kWebMIdDisplayUnit:
219 dst = &display_unit_;
220 break;
221 case kWebMIdAlphaMode:
222 dst = &alpha_mode_;
223 break;
224 case kWebMIdColorMatrixCoefficients:
225 dst = &matrix_coefficients_;
226 break;
227 case kWebMIdColorBitsPerChannel:
228 dst = &bits_per_channel_;
229 break;
230 case kWebMIdColorChromaSubsamplingHorz:
231 dst = &chroma_subsampling_horz_;
232 break;
233 case kWebMIdColorChromaSubsamplingVert:
234 dst = &chroma_subsampling_vert_;
235 break;
236 case kWebMIdColorChromaSitingHorz:
237 dst = &chroma_siting_horz_;
238 break;
239 case kWebMIdColorChromaSitingVert:
240 dst = &chroma_siting_vert_;
241 break;
242 case kWebMIdColorRange:
243 dst = &color_range_;
244 break;
245 case kWebMIdColorTransferCharacteristics:
246 dst = &transfer_characteristics_;
247 break;
248 case kWebMIdColorPrimaries:
249 dst = &color_primaries_;
250 break;
251 case kWebMIdColorMaxCLL:
252 case kWebMIdColorMaxFALL:
253 NOTIMPLEMENTED() << "HDR is not supported yet.";
254 return true;
255 case kWebMIdProjectionType:
256 LOG(WARNING) << "Ignoring ProjectionType with value " << val;
257 return true;
258 default:
259 return true;
260 }
261
262 if (*dst != -1) {
263 LOG(ERROR) << "Multiple values for id " << std::hex << id << " specified ("
264 << *dst << " and " << val << ")";
265 return false;
266 }
267
268 *dst = val;
269 return true;
270}
271
272bool WebMVideoClient::OnBinary(int /*id*/,
273 const uint8_t* /*data*/,
274 int /*size*/) {
275 // Accept binary fields we don't care about for now.
276 return true;
277}
278
279bool WebMVideoClient::OnFloat(int /*id*/, double /*val*/) {
280 // Accept float fields we don't care about for now.
281 return true;
282}
283
285 return color_range_ != -1 || color_primaries_ != -1 ||
286 transfer_characteristics_ != -1 || matrix_coefficients_ != -1;
287}
288
289std::vector<uint8_t> WebMVideoClient::GenerateColrBoxData() const {
290 BufferWriter writer;
291
292 writer.AppendInt(static_cast<uint32_t>(0)); // Size placeholder
293 writer.AppendInt(FOURCC_colr);
294 writer.AppendInt(FOURCC_nclx);
295
296 // Use BT.709 as default when not specified
297 uint16_t primaries =
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_)
301 : 1;
302 uint16_t matrix = (matrix_coefficients_ != -1)
303 ? static_cast<uint16_t>(matrix_coefficients_)
304 : 1;
305
306 writer.AppendInt(primaries);
307 writer.AppendInt(transfer);
308 writer.AppendInt(matrix);
309
310 // WebM color_range: 0 = limited range, 1 = full range
311 uint8_t full_range_flag = 0;
312 if (color_range_ != -1) {
313 full_range_flag = (color_range_ == 1) ? 1 : 0;
314 }
315 writer.AppendInt(full_range_flag);
316
317 const uint8_t* buffer = writer.Buffer();
318 size_t buffer_size = writer.Size();
319
320 std::vector<uint8_t> data(buffer, buffer + buffer_size);
321 uint32_t box_size = static_cast<uint32_t>(data.size());
322
323 // Update size field in big-endian
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;
328
329 return data;
330}
331
332} // namespace media
333} // namespace shaka
Class for parsing AV1 codec configuration record.
bool Parse(const std::vector< uint8_t > &data)
const uint8_t * Buffer() const
Class for parsing or writing VP codec configuration record.
bool ParseWebM(const std::vector< uint8_t > &data)
bool HasColorInfo() const
Check if color information is available.
std::vector< uint8_t > GenerateColrBoxData() const
Generate MP4 colr box data from color information.
std::shared_ptr< VideoStreamInfo > GetVideoStreamInfo(int64_t track_num, const std::string &codec_id, const std::vector< uint8_t > &codec_private, bool is_encrypted)
VPCodecConfigurationRecord GetVpCodecConfig(const std::vector< uint8_t > &codec_private)
void Reset()
Reset this object's state so it can process a new video track element.
All the methods that are virtual are virtual for mocking.