Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: media/formats/webm/tracks_builder.cc

Issue 1534273002: Switch to standard integer types in media/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/logging.h" 7 #include "base/logging.h"
8 #include "media/formats/webm/webm_constants.h" 8 #include "media/formats/webm/webm_constants.h"
9 9
10 namespace media { 10 namespace media {
11 11
12 // Returns size of an integer, formatted using Matroska serialization. 12 // Returns size of an integer, formatted using Matroska serialization.
13 static int GetUIntMkvSize(uint64 value) { 13 static int GetUIntMkvSize(uint64_t value) {
14 if (value < 0x07FULL) 14 if (value < 0x07FULL)
15 return 1; 15 return 1;
16 if (value < 0x03FFFULL) 16 if (value < 0x03FFFULL)
17 return 2; 17 return 2;
18 if (value < 0x01FFFFFULL) 18 if (value < 0x01FFFFFULL)
19 return 3; 19 return 3;
20 if (value < 0x0FFFFFFFULL) 20 if (value < 0x0FFFFFFFULL)
21 return 4; 21 return 4;
22 if (value < 0x07FFFFFFFFULL) 22 if (value < 0x07FFFFFFFFULL)
23 return 5; 23 return 5;
24 if (value < 0x03FFFFFFFFFFULL) 24 if (value < 0x03FFFFFFFFFFULL)
25 return 6; 25 return 6;
26 if (value < 0x01FFFFFFFFFFFFULL) 26 if (value < 0x01FFFFFFFFFFFFULL)
27 return 7; 27 return 7;
28 return 8; 28 return 8;
29 } 29 }
30 30
31 // Returns the minimium size required to serialize an integer value. 31 // Returns the minimium size required to serialize an integer value.
32 static int GetUIntSize(uint64 value) { 32 static int GetUIntSize(uint64_t value) {
33 if (value < 0x0100ULL) 33 if (value < 0x0100ULL)
34 return 1; 34 return 1;
35 if (value < 0x010000ULL) 35 if (value < 0x010000ULL)
36 return 2; 36 return 2;
37 if (value < 0x01000000ULL) 37 if (value < 0x01000000ULL)
38 return 3; 38 return 3;
39 if (value < 0x0100000000ULL) 39 if (value < 0x0100000000ULL)
40 return 4; 40 return 4;
41 if (value < 0x010000000000ULL) 41 if (value < 0x010000000000ULL)
42 return 5; 42 return 5;
43 if (value < 0x01000000000000ULL) 43 if (value < 0x01000000000000ULL)
44 return 6; 44 return 6;
45 if (value < 0x0100000000000000ULL) 45 if (value < 0x0100000000000000ULL)
46 return 7; 46 return 7;
47 return 8; 47 return 8;
48 } 48 }
49 49
50 static int MasterElementSize(int element_id, int payload_size) { 50 static int MasterElementSize(int element_id, int payload_size) {
51 return GetUIntSize(element_id) + GetUIntMkvSize(payload_size) + payload_size; 51 return GetUIntSize(element_id) + GetUIntMkvSize(payload_size) + payload_size;
52 } 52 }
53 53
54 static int UIntElementSize(int element_id, uint64 value) { 54 static int UIntElementSize(int element_id, uint64_t value) {
55 return GetUIntSize(element_id) + 1 + GetUIntSize(value); 55 return GetUIntSize(element_id) + 1 + GetUIntSize(value);
56 } 56 }
57 57
58 static int DoubleElementSize(int element_id) { 58 static int DoubleElementSize(int element_id) {
59 return GetUIntSize(element_id) + 1 + 8; 59 return GetUIntSize(element_id) + 1 + 8;
60 } 60 }
61 61
62 static int StringElementSize(int element_id, const std::string& value) { 62 static int StringElementSize(int element_id, const std::string& value) {
63 return GetUIntSize(element_id) + 63 return GetUIntSize(element_id) +
64 GetUIntMkvSize(value.length()) + 64 GetUIntMkvSize(value.length()) +
65 value.length(); 65 value.length();
66 } 66 }
67 67
68 static void SerializeInt(uint8** buf_ptr, int* buf_size_ptr, 68 static void SerializeInt(uint8_t** buf_ptr,
69 int64 value, int size) { 69 int* buf_size_ptr,
70 uint8*& buf = *buf_ptr; 70 int64_t value,
71 int size) {
72 uint8_t*& buf = *buf_ptr;
71 int& buf_size = *buf_size_ptr; 73 int& buf_size = *buf_size_ptr;
72 74
73 for (int idx = 1; idx <= size; ++idx) { 75 for (int idx = 1; idx <= size; ++idx) {
74 *buf++ = static_cast<uint8>(value >> ((size - idx) * 8)); 76 *buf++ = static_cast<uint8_t>(value >> ((size - idx) * 8));
75 --buf_size; 77 --buf_size;
76 } 78 }
77 } 79 }
78 80
79 static void SerializeDouble(uint8** buf_ptr, int* buf_size_ptr, 81 static void SerializeDouble(uint8_t** buf_ptr,
82 int* buf_size_ptr,
80 double value) { 83 double value) {
81 // Use a union to convert |value| to native endian integer bit pattern. 84 // Use a union to convert |value| to native endian integer bit pattern.
82 union { 85 union {
83 double src; 86 double src;
84 int64 dst; 87 int64_t dst;
85 } tmp; 88 } tmp;
86 tmp.src = value; 89 tmp.src = value;
87 90
88 // Write the bytes from native endian |tmp.dst| to big-endian form in |buf|. 91 // Write the bytes from native endian |tmp.dst| to big-endian form in |buf|.
89 SerializeInt(buf_ptr, buf_size_ptr, tmp.dst, 8); 92 SerializeInt(buf_ptr, buf_size_ptr, tmp.dst, 8);
90 } 93 }
91 94
92 static void WriteElementId(uint8** buf, int* buf_size, int element_id) { 95 static void WriteElementId(uint8_t** buf, int* buf_size, int element_id) {
93 SerializeInt(buf, buf_size, element_id, GetUIntSize(element_id)); 96 SerializeInt(buf, buf_size, element_id, GetUIntSize(element_id));
94 } 97 }
95 98
96 static void WriteUInt(uint8** buf, int* buf_size, uint64 value) { 99 static void WriteUInt(uint8_t** buf, int* buf_size, uint64_t value) {
97 const int size = GetUIntMkvSize(value); 100 const int size = GetUIntMkvSize(value);
98 value |= (1ULL << (size * 7)); // Matroska formatting 101 value |= (1ULL << (size * 7)); // Matroska formatting
99 SerializeInt(buf, buf_size, value, size); 102 SerializeInt(buf, buf_size, value, size);
100 } 103 }
101 104
102 static void WriteMasterElement(uint8** buf, int* buf_size, 105 static void WriteMasterElement(uint8_t** buf,
103 int element_id, int payload_size) { 106 int* buf_size,
107 int element_id,
108 int payload_size) {
104 WriteElementId(buf, buf_size, element_id); 109 WriteElementId(buf, buf_size, element_id);
105 WriteUInt(buf, buf_size, payload_size); 110 WriteUInt(buf, buf_size, payload_size);
106 } 111 }
107 112
108 static void WriteUIntElement(uint8** buf, 113 static void WriteUIntElement(uint8_t** buf,
109 int* buf_size, 114 int* buf_size,
110 int element_id, 115 int element_id,
111 uint64 value) { 116 uint64_t value) {
112 WriteElementId(buf, buf_size, element_id); 117 WriteElementId(buf, buf_size, element_id);
113 118
114 const int size = GetUIntSize(value); 119 const int size = GetUIntSize(value);
115 WriteUInt(buf, buf_size, size); 120 WriteUInt(buf, buf_size, size);
116 121
117 SerializeInt(buf, buf_size, value, size); 122 SerializeInt(buf, buf_size, value, size);
118 } 123 }
119 124
120 static void WriteDoubleElement(uint8** buf, int* buf_size, 125 static void WriteDoubleElement(uint8_t** buf,
121 int element_id, double value) { 126 int* buf_size,
127 int element_id,
128 double value) {
122 WriteElementId(buf, buf_size, element_id); 129 WriteElementId(buf, buf_size, element_id);
123 WriteUInt(buf, buf_size, 8); 130 WriteUInt(buf, buf_size, 8);
124 SerializeDouble(buf, buf_size, value); 131 SerializeDouble(buf, buf_size, value);
125 } 132 }
126 133
127 static void WriteStringElement(uint8** buf_ptr, int* buf_size_ptr, 134 static void WriteStringElement(uint8_t** buf_ptr,
128 int element_id, const std::string& value) { 135 int* buf_size_ptr,
129 uint8*& buf = *buf_ptr; 136 int element_id,
137 const std::string& value) {
138 uint8_t*& buf = *buf_ptr;
130 int& buf_size = *buf_size_ptr; 139 int& buf_size = *buf_size_ptr;
131 140
132 WriteElementId(&buf, &buf_size, element_id); 141 WriteElementId(&buf, &buf_size, element_id);
133 142
134 const uint64 size = value.length(); 143 const uint64_t size = value.length();
135 WriteUInt(&buf, &buf_size, size); 144 WriteUInt(&buf, &buf_size, size);
136 145
137 memcpy(buf, value.data(), size); 146 memcpy(buf, value.data(), size);
138 buf += size; 147 buf += size;
139 buf_size -= size; 148 buf_size -= size;
140 } 149 }
141 150
142 TracksBuilder::TracksBuilder(bool allow_invalid_values) 151 TracksBuilder::TracksBuilder(bool allow_invalid_values)
143 : allow_invalid_values_(allow_invalid_values) {} 152 : allow_invalid_values_(allow_invalid_values) {}
144 TracksBuilder::TracksBuilder() 153 TracksBuilder::TracksBuilder()
145 : allow_invalid_values_(false) {} 154 : allow_invalid_values_(false) {}
146 TracksBuilder::~TracksBuilder() {} 155 TracksBuilder::~TracksBuilder() {}
147 156
148 void TracksBuilder::AddVideoTrack(int track_num, 157 void TracksBuilder::AddVideoTrack(int track_num,
149 uint64 track_uid, 158 uint64_t track_uid,
150 const std::string& codec_id, 159 const std::string& codec_id,
151 const std::string& name, 160 const std::string& name,
152 const std::string& language, 161 const std::string& language,
153 int default_duration, 162 int default_duration,
154 int video_pixel_width, 163 int video_pixel_width,
155 int video_pixel_height) { 164 int video_pixel_height) {
156 AddTrackInternal(track_num, kWebMTrackTypeVideo, track_uid, codec_id, name, 165 AddTrackInternal(track_num, kWebMTrackTypeVideo, track_uid, codec_id, name,
157 language, default_duration, video_pixel_width, 166 language, default_duration, video_pixel_width,
158 video_pixel_height, -1, -1); 167 video_pixel_height, -1, -1);
159 } 168 }
160 169
161 void TracksBuilder::AddAudioTrack(int track_num, 170 void TracksBuilder::AddAudioTrack(int track_num,
162 uint64 track_uid, 171 uint64_t track_uid,
163 const std::string& codec_id, 172 const std::string& codec_id,
164 const std::string& name, 173 const std::string& name,
165 const std::string& language, 174 const std::string& language,
166 int default_duration, 175 int default_duration,
167 int audio_channels, 176 int audio_channels,
168 double audio_sampling_frequency) { 177 double audio_sampling_frequency) {
169 AddTrackInternal(track_num, kWebMTrackTypeAudio, track_uid, codec_id, name, 178 AddTrackInternal(track_num, kWebMTrackTypeAudio, track_uid, codec_id, name,
170 language, default_duration, -1, -1, audio_channels, 179 language, default_duration, -1, -1, audio_channels,
171 audio_sampling_frequency); 180 audio_sampling_frequency);
172 } 181 }
173 182
174 void TracksBuilder::AddTextTrack(int track_num, 183 void TracksBuilder::AddTextTrack(int track_num,
175 uint64 track_uid, 184 uint64_t track_uid,
176 const std::string& codec_id, 185 const std::string& codec_id,
177 const std::string& name, 186 const std::string& name,
178 const std::string& language) { 187 const std::string& language) {
179 AddTrackInternal(track_num, kWebMTrackTypeSubtitlesOrCaptions, track_uid, 188 AddTrackInternal(track_num, kWebMTrackTypeSubtitlesOrCaptions, track_uid,
180 codec_id, name, language, -1, -1, -1, -1, -1); 189 codec_id, name, language, -1, -1, -1, -1, -1);
181 } 190 }
182 191
183 std::vector<uint8> TracksBuilder::Finish() { 192 std::vector<uint8_t> TracksBuilder::Finish() {
184 // Allocate the storage 193 // Allocate the storage
185 std::vector<uint8> buffer; 194 std::vector<uint8_t> buffer;
186 buffer.resize(GetTracksSize()); 195 buffer.resize(GetTracksSize());
187 196
188 // Populate the storage with a tracks header 197 // Populate the storage with a tracks header
189 WriteTracks(&buffer[0], buffer.size()); 198 WriteTracks(&buffer[0], buffer.size());
190 199
191 return buffer; 200 return buffer;
192 } 201 }
193 202
194 void TracksBuilder::AddTrackInternal(int track_num, 203 void TracksBuilder::AddTrackInternal(int track_num,
195 int track_type, 204 int track_type,
196 uint64 track_uid, 205 uint64_t track_uid,
197 const std::string& codec_id, 206 const std::string& codec_id,
198 const std::string& name, 207 const std::string& name,
199 const std::string& language, 208 const std::string& language,
200 int default_duration, 209 int default_duration,
201 int video_pixel_width, 210 int video_pixel_width,
202 int video_pixel_height, 211 int video_pixel_height,
203 int audio_channels, 212 int audio_channels,
204 double audio_sampling_frequency) { 213 double audio_sampling_frequency) {
205 tracks_.push_back(Track(track_num, track_type, track_uid, codec_id, name, 214 tracks_.push_back(Track(track_num, track_type, track_uid, codec_id, name,
206 language, default_duration, video_pixel_width, 215 language, default_duration, video_pixel_width,
207 video_pixel_height, audio_channels, 216 video_pixel_height, audio_channels,
208 audio_sampling_frequency, allow_invalid_values_)); 217 audio_sampling_frequency, allow_invalid_values_));
209 } 218 }
210 219
211 int TracksBuilder::GetTracksSize() const { 220 int TracksBuilder::GetTracksSize() const {
212 return MasterElementSize(kWebMIdTracks, GetTracksPayloadSize()); 221 return MasterElementSize(kWebMIdTracks, GetTracksPayloadSize());
213 } 222 }
214 223
215 int TracksBuilder::GetTracksPayloadSize() const { 224 int TracksBuilder::GetTracksPayloadSize() const {
216 int payload_size = 0; 225 int payload_size = 0;
217 226
218 for (TrackList::const_iterator itr = tracks_.begin(); 227 for (TrackList::const_iterator itr = tracks_.begin();
219 itr != tracks_.end(); ++itr) { 228 itr != tracks_.end(); ++itr) {
220 payload_size += itr->GetSize(); 229 payload_size += itr->GetSize();
221 } 230 }
222 231
223 return payload_size; 232 return payload_size;
224 } 233 }
225 234
226 void TracksBuilder::WriteTracks(uint8* buf, int buf_size) const { 235 void TracksBuilder::WriteTracks(uint8_t* buf, int buf_size) const {
227 WriteMasterElement(&buf, &buf_size, kWebMIdTracks, GetTracksPayloadSize()); 236 WriteMasterElement(&buf, &buf_size, kWebMIdTracks, GetTracksPayloadSize());
228 237
229 for (TrackList::const_iterator itr = tracks_.begin(); 238 for (TrackList::const_iterator itr = tracks_.begin();
230 itr != tracks_.end(); ++itr) { 239 itr != tracks_.end(); ++itr) {
231 itr->Write(&buf, &buf_size); 240 itr->Write(&buf, &buf_size);
232 } 241 }
233 } 242 }
234 243
235 TracksBuilder::Track::Track(int track_num, 244 TracksBuilder::Track::Track(int track_num,
236 int track_type, 245 int track_type,
237 uint64 track_uid, 246 uint64_t track_uid,
238 const std::string& codec_id, 247 const std::string& codec_id,
239 const std::string& name, 248 const std::string& name,
240 const std::string& language, 249 const std::string& language,
241 int default_duration, 250 int default_duration,
242 int video_pixel_width, 251 int video_pixel_width,
243 int video_pixel_height, 252 int video_pixel_height,
244 int audio_channels, 253 int audio_channels,
245 double audio_sampling_frequency, 254 double audio_sampling_frequency,
246 bool allow_invalid_values) 255 bool allow_invalid_values)
247 : track_num_(track_num), 256 : track_num_(track_num),
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 size += MasterElementSize(kWebMIdVideo, GetVideoPayloadSize()); 343 size += MasterElementSize(kWebMIdVideo, GetVideoPayloadSize());
335 } 344 }
336 345
337 if (GetAudioPayloadSize() > 0) { 346 if (GetAudioPayloadSize() > 0) {
338 size += MasterElementSize(kWebMIdAudio, GetAudioPayloadSize()); 347 size += MasterElementSize(kWebMIdAudio, GetAudioPayloadSize());
339 } 348 }
340 349
341 return size; 350 return size;
342 } 351 }
343 352
344 void TracksBuilder::Track::Write(uint8** buf, int* buf_size) const { 353 void TracksBuilder::Track::Write(uint8_t** buf, int* buf_size) const {
345 WriteMasterElement(buf, buf_size, kWebMIdTrackEntry, GetPayloadSize()); 354 WriteMasterElement(buf, buf_size, kWebMIdTrackEntry, GetPayloadSize());
346 355
347 WriteUIntElement(buf, buf_size, kWebMIdTrackNumber, track_num_); 356 WriteUIntElement(buf, buf_size, kWebMIdTrackNumber, track_num_);
348 WriteUIntElement(buf, buf_size, kWebMIdTrackType, track_type_); 357 WriteUIntElement(buf, buf_size, kWebMIdTrackType, track_type_);
349 WriteUIntElement(buf, buf_size, kWebMIdTrackUID, track_uid_); 358 WriteUIntElement(buf, buf_size, kWebMIdTrackUID, track_uid_);
350 359
351 if (default_duration_ >= 0) 360 if (default_duration_ >= 0)
352 WriteUIntElement(buf, buf_size, kWebMIdDefaultDuration, default_duration_); 361 WriteUIntElement(buf, buf_size, kWebMIdDefaultDuration, default_duration_);
353 362
354 if (!codec_id_.empty()) 363 if (!codec_id_.empty())
(...skipping 22 matching lines...) Expand all
377 WriteUIntElement(buf, buf_size, kWebMIdChannels, audio_channels_); 386 WriteUIntElement(buf, buf_size, kWebMIdChannels, audio_channels_);
378 387
379 if (audio_sampling_frequency_ >= 0) { 388 if (audio_sampling_frequency_ >= 0) {
380 WriteDoubleElement(buf, buf_size, kWebMIdSamplingFrequency, 389 WriteDoubleElement(buf, buf_size, kWebMIdSamplingFrequency,
381 audio_sampling_frequency_); 390 audio_sampling_frequency_);
382 } 391 }
383 } 392 }
384 } 393 }
385 394
386 } // namespace media 395 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698