Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/formats/webm/tracks_builder.h" | 5 #include "media/formats/webm/tracks_builder.h" |
| 6 | 6 |
| 7 #include "media/formats/webm/webm_constants.h" | 7 #include "media/formats/webm/webm_constants.h" |
| 8 | 8 |
| 9 namespace media { | 9 namespace media { |
| 10 | 10 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 } | 47 } |
| 48 | 48 |
| 49 static int MasterElementSize(int element_id, int payload_size) { | 49 static int MasterElementSize(int element_id, int payload_size) { |
| 50 return GetUIntSize(element_id) + GetUIntMkvSize(payload_size) + payload_size; | 50 return GetUIntSize(element_id) + GetUIntMkvSize(payload_size) + payload_size; |
| 51 } | 51 } |
| 52 | 52 |
| 53 static int IntElementSize(int element_id, int value) { | 53 static int IntElementSize(int element_id, int value) { |
| 54 return GetUIntSize(element_id) + 1 + GetUIntSize(value); | 54 return GetUIntSize(element_id) + 1 + GetUIntSize(value); |
| 55 } | 55 } |
| 56 | 56 |
| 57 static int DoubleElementSize(int element_id) { | |
| 58 return GetUIntSize(element_id) + 1 + 8; | |
| 59 } | |
| 60 | |
| 57 static int StringElementSize(int element_id, const std::string& value) { | 61 static int StringElementSize(int element_id, const std::string& value) { |
| 58 return GetUIntSize(element_id) + | 62 return GetUIntSize(element_id) + |
| 59 GetUIntMkvSize(value.length()) + | 63 GetUIntMkvSize(value.length()) + |
| 60 value.length(); | 64 value.length(); |
| 61 } | 65 } |
| 62 | 66 |
| 63 static void SerializeInt(uint8** buf_ptr, int* buf_size_ptr, | 67 static void SerializeInt(uint8** buf_ptr, int* buf_size_ptr, |
| 64 int64 value, int size) { | 68 int64 value, int size) { |
| 65 uint8*& buf = *buf_ptr; | 69 uint8*& buf = *buf_ptr; |
| 66 int& buf_size = *buf_size_ptr; | 70 int& buf_size = *buf_size_ptr; |
| 67 | 71 |
| 68 for (int idx = 1; idx <= size; ++idx) { | 72 for (int idx = 1; idx <= size; ++idx) { |
| 69 *buf++ = static_cast<uint8>(value >> ((size - idx) * 8)); | 73 *buf++ = static_cast<uint8>(value >> ((size - idx) * 8)); |
| 70 --buf_size; | 74 --buf_size; |
| 71 } | 75 } |
| 72 } | 76 } |
| 73 | 77 |
| 78 static void SerializeDouble(uint8** buf_ptr, int* buf_size_ptr, | |
| 79 double value) { | |
| 80 uint8*& buf = *buf_ptr; | |
| 81 int& buf_size = *buf_size_ptr; | |
| 82 | |
| 83 // Use a union to convert |value| to native endian integer bit pattern. | |
| 84 union { | |
| 85 double src; | |
| 86 int64 dst; | |
| 87 } tmp; | |
| 88 tmp.src = value; | |
| 89 int64 pattern = tmp.dst; | |
| 90 | |
| 91 // Write the bytes from native endian |pattern| to big-endian form in buf. | |
| 92 for (int idx = 1; idx <= 8; ++idx) { | |
|
acolwell GONE FROM CHROMIUM
2014/03/26 22:25:32
nit: How about SerializeInt(buf_ptr, buf_size_ptr,
wolenetz
2014/03/27 01:21:43
Nice. Done.
| |
| 93 *buf++ = static_cast<uint8>(pattern >> ((8 - idx) * 8)); | |
| 94 --buf_size; | |
| 95 } | |
| 96 } | |
| 97 | |
| 74 static void WriteElementId(uint8** buf, int* buf_size, int element_id) { | 98 static void WriteElementId(uint8** buf, int* buf_size, int element_id) { |
| 75 SerializeInt(buf, buf_size, element_id, GetUIntSize(element_id)); | 99 SerializeInt(buf, buf_size, element_id, GetUIntSize(element_id)); |
| 76 } | 100 } |
| 77 | 101 |
| 78 static void WriteUInt(uint8** buf, int* buf_size, uint64 value) { | 102 static void WriteUInt(uint8** buf, int* buf_size, uint64 value) { |
| 79 const int size = GetUIntMkvSize(value); | 103 const int size = GetUIntMkvSize(value); |
| 80 value |= (1ULL << (size * 7)); // Matroska formatting | 104 value |= (1ULL << (size * 7)); // Matroska formatting |
| 81 SerializeInt(buf, buf_size, value, size); | 105 SerializeInt(buf, buf_size, value, size); |
| 82 } | 106 } |
| 83 | 107 |
| 84 static void WriteMasterElement(uint8** buf, int* buf_size, | 108 static void WriteMasterElement(uint8** buf, int* buf_size, |
| 85 int element_id, int payload_size) { | 109 int element_id, int payload_size) { |
| 86 WriteElementId(buf, buf_size, element_id); | 110 WriteElementId(buf, buf_size, element_id); |
| 87 WriteUInt(buf, buf_size, payload_size); | 111 WriteUInt(buf, buf_size, payload_size); |
| 88 } | 112 } |
| 89 | 113 |
| 90 static void WriteIntElement(uint8** buf, int* buf_size, | 114 static void WriteIntElement(uint8** buf, int* buf_size, |
| 91 int element_id, int value) { | 115 int element_id, int value) { |
| 92 WriteElementId(buf, buf_size, element_id); | 116 WriteElementId(buf, buf_size, element_id); |
| 93 | 117 |
| 94 const int size = GetUIntSize(value); | 118 const int size = GetUIntSize(value); |
| 95 WriteUInt(buf, buf_size, size); | 119 WriteUInt(buf, buf_size, size); |
| 96 | 120 |
| 97 SerializeInt(buf, buf_size, value, size); | 121 SerializeInt(buf, buf_size, value, size); |
| 98 } | 122 } |
| 99 | 123 |
| 124 static void WriteDoubleElement(uint8** buf, int* buf_size, | |
| 125 int element_id, double value) { | |
| 126 WriteElementId(buf, buf_size, element_id); | |
| 127 WriteUInt(buf, buf_size, 8); | |
| 128 SerializeDouble(buf, buf_size, value); | |
| 129 } | |
| 130 | |
| 100 static void WriteStringElement(uint8** buf_ptr, int* buf_size_ptr, | 131 static void WriteStringElement(uint8** buf_ptr, int* buf_size_ptr, |
| 101 int element_id, const std::string& value) { | 132 int element_id, const std::string& value) { |
| 102 uint8*& buf = *buf_ptr; | 133 uint8*& buf = *buf_ptr; |
| 103 int& buf_size = *buf_size_ptr; | 134 int& buf_size = *buf_size_ptr; |
| 104 | 135 |
| 105 WriteElementId(&buf, &buf_size, element_id); | 136 WriteElementId(&buf, &buf_size, element_id); |
| 106 | 137 |
| 107 const uint64 size = value.length(); | 138 const uint64 size = value.length(); |
| 108 WriteUInt(&buf, &buf_size, size); | 139 WriteUInt(&buf, &buf_size, size); |
| 109 | 140 |
| 110 memcpy(buf, value.data(), size); | 141 memcpy(buf, value.data(), size); |
| 111 buf += size; | 142 buf += size; |
| 112 buf_size -= size; | 143 buf_size -= size; |
| 113 } | 144 } |
| 114 | 145 |
| 115 TracksBuilder::TracksBuilder() {} | 146 TracksBuilder::TracksBuilder() {} |
| 116 TracksBuilder::~TracksBuilder() {} | 147 TracksBuilder::~TracksBuilder() {} |
| 117 | 148 |
| 118 void TracksBuilder::AddTrack( | 149 void TracksBuilder::AddTrack( |
| 119 int track_num, | 150 int track_num, |
| 120 int track_type, | 151 int track_type, |
| 121 int track_uid, | 152 int track_uid, |
| 122 const std::string& codec_id, | 153 const std::string& codec_id, |
| 123 const std::string& name, | 154 const std::string& name, |
| 124 const std::string& language) { | 155 const std::string& language, |
| 156 int default_duration, | |
| 157 int video_pixel_width, | |
| 158 int video_pixel_height, | |
| 159 int audio_channels, | |
| 160 double audio_sampling_frequency) { | |
| 125 tracks_.push_back(Track(track_num, track_type, track_uid, codec_id, name, | 161 tracks_.push_back(Track(track_num, track_type, track_uid, codec_id, name, |
| 126 language)); | 162 language, default_duration, video_pixel_width, |
| 163 video_pixel_height, audio_channels, | |
| 164 audio_sampling_frequency)); | |
| 127 } | 165 } |
| 128 | 166 |
| 129 std::vector<uint8> TracksBuilder::Finish() { | 167 std::vector<uint8> TracksBuilder::Finish() { |
| 130 // Allocate the storage | 168 // Allocate the storage |
| 131 std::vector<uint8> buffer; | 169 std::vector<uint8> buffer; |
| 132 buffer.resize(GetTracksSize()); | 170 buffer.resize(GetTracksSize()); |
| 133 | 171 |
| 134 // Populate the storage with a tracks header | 172 // Populate the storage with a tracks header |
| 135 WriteTracks(&buffer[0], buffer.size()); | 173 WriteTracks(&buffer[0], buffer.size()); |
| 136 | 174 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 157 | 195 |
| 158 for (TrackList::const_iterator itr = tracks_.begin(); | 196 for (TrackList::const_iterator itr = tracks_.begin(); |
| 159 itr != tracks_.end(); ++itr) { | 197 itr != tracks_.end(); ++itr) { |
| 160 itr->Write(&buf, &buf_size); | 198 itr->Write(&buf, &buf_size); |
| 161 } | 199 } |
| 162 } | 200 } |
| 163 | 201 |
| 164 TracksBuilder::Track::Track(int track_num, int track_type, int track_uid, | 202 TracksBuilder::Track::Track(int track_num, int track_type, int track_uid, |
| 165 const std::string& codec_id, | 203 const std::string& codec_id, |
| 166 const std::string& name, | 204 const std::string& name, |
| 167 const std::string& language) | 205 const std::string& language, |
| 206 int default_duration, | |
| 207 int video_pixel_width, int video_pixel_height, | |
| 208 int audio_channels, double audio_sampling_frequency) | |
| 168 : track_num_(track_num), | 209 : track_num_(track_num), |
| 169 track_type_(track_type), | 210 track_type_(track_type), |
| 170 track_uid_(track_uid), | 211 track_uid_(track_uid), |
| 171 codec_id_(codec_id), | 212 codec_id_(codec_id), |
| 172 name_(name), | 213 name_(name), |
| 173 language_(language) { | 214 language_(language), |
| 215 default_duration_(default_duration), | |
| 216 video_pixel_width_(video_pixel_width), | |
| 217 video_pixel_height_(video_pixel_height), | |
| 218 audio_channels_(audio_channels), | |
| 219 audio_sampling_frequency_(audio_sampling_frequency) { | |
| 174 } | 220 } |
| 175 | 221 |
| 176 int TracksBuilder::Track::GetSize() const { | 222 int TracksBuilder::Track::GetSize() const { |
| 177 return MasterElementSize(kWebMIdTrackEntry, GetPayloadSize()); | 223 return MasterElementSize(kWebMIdTrackEntry, GetPayloadSize()); |
| 178 } | 224 } |
| 179 | 225 |
| 226 int TracksBuilder::Track::GetVideoPayloadSize() const { | |
|
acolwell GONE FROM CHROMIUM
2014/03/26 22:25:32
nit: s/Payload/Element to match other naming here
wolenetz
2014/03/27 01:21:43
Hmm. I thought PayloadSize refers to the contents
| |
| 227 int payload_size = 0; | |
| 228 | |
| 229 if (video_pixel_width_ >= 0) | |
| 230 payload_size += IntElementSize(kWebMIdPixelWidth, video_pixel_width_); | |
| 231 if (video_pixel_height_ >= 0) | |
| 232 payload_size += IntElementSize(kWebMIdPixelHeight, video_pixel_height_); | |
| 233 | |
| 234 return payload_size; | |
| 235 } | |
| 236 | |
| 237 int TracksBuilder::Track::GetAudioPayloadSize() const { | |
|
acolwell GONE FROM CHROMIUM
2014/03/26 22:25:32
ditto
wolenetz
2014/03/27 01:21:43
ditto (confirmed offline that s/Payload/Element/ i
| |
| 238 int payload_size = 0; | |
| 239 | |
| 240 if (audio_channels_ >= 0) | |
| 241 payload_size += IntElementSize(kWebMIdChannels, audio_channels_); | |
| 242 if (audio_sampling_frequency_ >= 0) | |
| 243 payload_size += DoubleElementSize(kWebMIdSamplingFrequency); | |
| 244 | |
| 245 return payload_size; | |
| 246 } | |
| 247 | |
| 180 int TracksBuilder::Track::GetPayloadSize() const { | 248 int TracksBuilder::Track::GetPayloadSize() const { |
| 181 int size = 0; | 249 int size = 0; |
| 182 | 250 |
| 183 size += IntElementSize(kWebMIdTrackNumber, track_num_); | 251 size += IntElementSize(kWebMIdTrackNumber, track_num_); |
| 184 size += IntElementSize(kWebMIdTrackType, track_type_); | 252 size += IntElementSize(kWebMIdTrackType, track_type_); |
| 185 size += IntElementSize(kWebMIdTrackUID, track_uid_); | 253 size += IntElementSize(kWebMIdTrackUID, track_uid_); |
| 186 | 254 |
| 255 if (default_duration_ >= 0) | |
| 256 size += IntElementSize(kWebMIdDefaultDuration, default_duration_); | |
| 257 | |
| 187 if (!codec_id_.empty()) | 258 if (!codec_id_.empty()) |
| 188 size += StringElementSize(kWebMIdCodecID, codec_id_); | 259 size += StringElementSize(kWebMIdCodecID, codec_id_); |
| 189 | 260 |
| 190 if (!name_.empty()) | 261 if (!name_.empty()) |
| 191 size += StringElementSize(kWebMIdName, name_); | 262 size += StringElementSize(kWebMIdName, name_); |
| 192 | 263 |
| 193 if (!language_.empty()) | 264 if (!language_.empty()) |
| 194 size += StringElementSize(kWebMIdLanguage, language_); | 265 size += StringElementSize(kWebMIdLanguage, language_); |
| 195 | 266 |
| 267 if (GetVideoPayloadSize() > 0) { | |
| 268 size += MasterElementSize(kWebMIdVideo, GetVideoPayloadSize()); | |
| 269 } | |
| 270 | |
| 271 if (GetAudioPayloadSize() > 0) { | |
| 272 size += MasterElementSize(kWebMIdAudio, GetAudioPayloadSize()); | |
| 273 } | |
| 274 | |
| 196 return size; | 275 return size; |
| 197 } | 276 } |
| 198 | 277 |
| 199 void TracksBuilder::Track::Write(uint8** buf, int* buf_size) const { | 278 void TracksBuilder::Track::Write(uint8** buf, int* buf_size) const { |
| 200 WriteMasterElement(buf, buf_size, kWebMIdTrackEntry, GetPayloadSize()); | 279 WriteMasterElement(buf, buf_size, kWebMIdTrackEntry, GetPayloadSize()); |
| 201 | 280 |
| 202 WriteIntElement(buf, buf_size, kWebMIdTrackNumber, track_num_); | 281 WriteIntElement(buf, buf_size, kWebMIdTrackNumber, track_num_); |
| 203 WriteIntElement(buf, buf_size, kWebMIdTrackType, track_type_); | 282 WriteIntElement(buf, buf_size, kWebMIdTrackType, track_type_); |
| 204 WriteIntElement(buf, buf_size, kWebMIdTrackUID, track_uid_); | 283 WriteIntElement(buf, buf_size, kWebMIdTrackUID, track_uid_); |
| 205 | 284 |
| 285 if (default_duration_ >= 0) | |
|
acolwell GONE FROM CHROMIUM
2014/03/26 22:25:32
I wonder if we should have a "strict mode" that as
wolenetz
2014/03/27 01:21:43
Done (see new TracksBuilder |allow_invalid_values_
| |
| 286 WriteIntElement(buf, buf_size, kWebMIdDefaultDuration, default_duration_); | |
| 287 | |
| 206 if (!codec_id_.empty()) | 288 if (!codec_id_.empty()) |
| 207 WriteStringElement(buf, buf_size, kWebMIdCodecID, codec_id_); | 289 WriteStringElement(buf, buf_size, kWebMIdCodecID, codec_id_); |
| 208 | 290 |
| 209 if (!name_.empty()) | 291 if (!name_.empty()) |
| 210 WriteStringElement(buf, buf_size, kWebMIdName, name_); | 292 WriteStringElement(buf, buf_size, kWebMIdName, name_); |
| 211 | 293 |
| 212 if (!language_.empty()) | 294 if (!language_.empty()) |
| 213 WriteStringElement(buf, buf_size, kWebMIdLanguage, language_); | 295 WriteStringElement(buf, buf_size, kWebMIdLanguage, language_); |
| 296 | |
| 297 if (GetVideoPayloadSize() > 0) { | |
| 298 WriteMasterElement(buf, buf_size, kWebMIdVideo, GetVideoPayloadSize()); | |
| 299 | |
| 300 if (video_pixel_width_ >= 0) | |
| 301 WriteIntElement(buf, buf_size, kWebMIdPixelWidth, video_pixel_width_); | |
| 302 | |
| 303 if (video_pixel_height_ >= 0) | |
| 304 WriteIntElement(buf, buf_size, kWebMIdPixelHeight, video_pixel_height_); | |
| 305 } | |
| 306 | |
| 307 if (GetAudioPayloadSize() > 0) { | |
| 308 WriteMasterElement(buf, buf_size, kWebMIdAudio, GetAudioPayloadSize()); | |
| 309 | |
| 310 if (audio_channels_ >= 0) | |
| 311 WriteIntElement(buf, buf_size, kWebMIdChannels, audio_channels_); | |
| 312 | |
| 313 if (audio_sampling_frequency_ >= 0) { | |
| 314 WriteDoubleElement(buf, buf_size, kWebMIdSamplingFrequency, | |
| 315 audio_sampling_frequency_); | |
| 316 } | |
| 317 } | |
| 214 } | 318 } |
| 215 | 319 |
| 216 } // namespace media | 320 } // namespace media |
| OLD | NEW |