DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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 "packager/base/logging.h"
8 #include "packager/base/stl_util.h"
9 #include "packager/media/formats/webm/webm_constants.h"
10 
11 namespace {
12 
13 // Timestamps are represented in double in WebM. Convert to uint64_t in us.
14 const uint32_t kWebMTimeScale = 1000000u;
15 
16 int64_t GetGreatestCommonDivisor(int64_t a, int64_t b) {
17  while (b) {
18  int64_t temp = b;
19  b = a % b;
20  a = temp;
21  }
22  return a;
23 }
24 
25 } // namespace
26 
27 namespace shaka {
28 namespace media {
29 
30 WebMVideoClient::WebMVideoClient() {
31  Reset();
32 }
33 
34 WebMVideoClient::~WebMVideoClient() {
35 }
36 
38  pixel_width_ = -1;
39  pixel_height_ = -1;
40  crop_bottom_ = -1;
41  crop_top_ = -1;
42  crop_left_ = -1;
43  crop_right_ = -1;
44  display_width_ = -1;
45  display_height_ = -1;
46  display_unit_ = -1;
47  alpha_mode_ = -1;
48 }
49 
50 scoped_refptr<VideoStreamInfo> WebMVideoClient::GetVideoStreamInfo(
51  int64_t track_num,
52  const std::string& codec_id,
53  const std::vector<uint8_t>& codec_private,
54  bool is_encrypted) {
55  VideoCodec video_codec = kUnknownVideoCodec;
56  if (codec_id == "V_VP8") {
57  video_codec = kCodecVP8;
58  } else if (codec_id == "V_VP9") {
59  video_codec = kCodecVP9;
60  } else if (codec_id == "V_VP10") {
61  video_codec = kCodecVP10;
62  } else {
63  LOG(ERROR) << "Unsupported video codec_id " << codec_id;
64  return scoped_refptr<VideoStreamInfo>();
65  }
66 
67  if (pixel_width_ <= 0 || pixel_height_ <= 0)
68  return scoped_refptr<VideoStreamInfo>();
69 
70  // Set crop and display unit defaults if these elements are not present.
71  if (crop_bottom_ == -1)
72  crop_bottom_ = 0;
73 
74  if (crop_top_ == -1)
75  crop_top_ = 0;
76 
77  if (crop_left_ == -1)
78  crop_left_ = 0;
79 
80  if (crop_right_ == -1)
81  crop_right_ = 0;
82 
83  if (display_unit_ == -1)
84  display_unit_ = 0;
85 
86  uint16_t width_after_crop = pixel_width_ - (crop_left_ + crop_right_);
87  uint16_t height_after_crop = pixel_height_ - (crop_top_ + crop_bottom_);
88 
89  if (display_unit_ == 0) {
90  if (display_width_ <= 0)
91  display_width_ = width_after_crop;
92  if (display_height_ <= 0)
93  display_height_ = height_after_crop;
94  } else if (display_unit_ == 3) {
95  if (display_width_ <= 0 || display_height_ <= 0)
96  return scoped_refptr<VideoStreamInfo>();
97  } else {
98  LOG(ERROR) << "Unsupported display unit type " << display_unit_;
99  return scoped_refptr<VideoStreamInfo>();
100  }
101  // Calculate sample aspect ratio.
102  int64_t sar_x = display_width_ * height_after_crop;
103  int64_t sar_y = display_height_ * width_after_crop;
104  int64_t gcd = GetGreatestCommonDivisor(sar_x, sar_y);
105  sar_x /= gcd;
106  sar_y /= gcd;
107 
108  return scoped_refptr<VideoStreamInfo>(new VideoStreamInfo(
109  track_num, kWebMTimeScale, 0, video_codec, std::string(), std::string(),
110  width_after_crop, height_after_crop, sar_x, sar_y, 0, 0, NULL, 0,
111  is_encrypted));
112 }
113 
114 bool WebMVideoClient::OnUInt(int id, int64_t val) {
115  int64_t* dst = NULL;
116 
117  switch (id) {
118  case kWebMIdPixelWidth:
119  dst = &pixel_width_;
120  break;
121  case kWebMIdPixelHeight:
122  dst = &pixel_height_;
123  break;
124  case kWebMIdPixelCropTop:
125  dst = &crop_top_;
126  break;
127  case kWebMIdPixelCropBottom:
128  dst = &crop_bottom_;
129  break;
130  case kWebMIdPixelCropLeft:
131  dst = &crop_left_;
132  break;
133  case kWebMIdPixelCropRight:
134  dst = &crop_right_;
135  break;
136  case kWebMIdDisplayWidth:
137  dst = &display_width_;
138  break;
139  case kWebMIdDisplayHeight:
140  dst = &display_height_;
141  break;
142  case kWebMIdDisplayUnit:
143  dst = &display_unit_;
144  break;
145  case kWebMIdAlphaMode:
146  dst = &alpha_mode_;
147  break;
148  default:
149  return true;
150  }
151 
152  if (*dst != -1) {
153  LOG(ERROR) << "Multiple values for id " << std::hex << id << " specified ("
154  << *dst << " and " << val << ")";
155  return false;
156  }
157 
158  *dst = val;
159  return true;
160 }
161 
162 bool WebMVideoClient::OnBinary(int id, const uint8_t* data, int size) {
163  // Accept binary fields we don't care about for now.
164  return true;
165 }
166 
167 bool WebMVideoClient::OnFloat(int id, double val) {
168  // Accept float fields we don't care about for now.
169  return true;
170 }
171 
172 } // namespace media
173 } // namespace shaka
void Reset()
Reset this object's state so it can process a new video track element.
scoped_refptr< VideoStreamInfo > GetVideoStreamInfo(int64_t track_num, const std::string &codec_id, const std::vector< uint8_t > &codec_private, bool is_encrypted)
Holds video stream information.