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/cluster_builder.h" | 5 #include "media/formats/webm/cluster_builder.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "media/base/data_buffer.h" | 8 #include "media/base/data_buffer.h" |
9 #include "media/formats/webm/webm_constants.h" | 9 #include "media/formats/webm/webm_constants.h" |
10 | 10 |
11 namespace media { | 11 namespace media { |
12 | 12 |
13 static const uint8 kClusterHeader[] = { | 13 static const uint8_t kClusterHeader[] = { |
14 0x1F, 0x43, 0xB6, 0x75, // CLUSTER ID | 14 0x1F, 0x43, 0xB6, 0x75, // CLUSTER ID |
15 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cluster(size = 0) | 15 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cluster(size = 0) |
16 0xE7, // Timecode ID | 16 0xE7, // Timecode ID |
17 0x88, // timecode(size=8) | 17 0x88, // timecode(size=8) |
18 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // timecode value | 18 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // timecode value |
19 }; | 19 }; |
20 | 20 |
21 static const uint8 kSimpleBlockHeader[] = { | 21 static const uint8_t kSimpleBlockHeader[] = { |
22 0xA3, // SimpleBlock ID | 22 0xA3, // SimpleBlock ID |
23 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SimpleBlock(size = 0) | 23 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SimpleBlock(size = 0) |
24 }; | 24 }; |
25 | 25 |
26 static const uint8 kBlockGroupHeader[] = { | 26 static const uint8_t kBlockGroupHeader[] = { |
27 0xA0, // BlockGroup ID | 27 0xA0, // BlockGroup ID |
28 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // BlockGroup(size = 0) | 28 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // BlockGroup(size = 0) |
29 0x9B, // BlockDuration ID | 29 0x9B, // BlockDuration ID |
30 0x88, // BlockDuration(size = 8) | 30 0x88, // BlockDuration(size = 8) |
31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // duration | 31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // duration |
32 0xA1, // Block ID | 32 0xA1, // Block ID |
33 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block(size = 0) | 33 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block(size = 0) |
34 }; | 34 }; |
35 | 35 |
36 static const uint8 kBlockGroupHeaderWithoutBlockDuration[] = { | 36 static const uint8_t kBlockGroupHeaderWithoutBlockDuration[] = { |
37 0xA0, // BlockGroup ID | 37 0xA0, // BlockGroup ID |
38 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // BlockGroup(size = 0) | 38 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // BlockGroup(size = 0) |
39 0xA1, // Block ID | 39 0xA1, // Block ID |
40 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block(size = 0) | 40 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block(size = 0) |
41 }; | 41 }; |
42 | 42 |
43 enum { | 43 enum { |
44 kClusterSizeOffset = 4, | 44 kClusterSizeOffset = 4, |
45 kClusterTimecodeOffset = 14, | 45 kClusterTimecodeOffset = 14, |
46 | 46 |
47 kSimpleBlockSizeOffset = 1, | 47 kSimpleBlockSizeOffset = 1, |
48 | 48 |
49 kBlockGroupSizeOffset = 1, | 49 kBlockGroupSizeOffset = 1, |
50 kBlockGroupWithoutBlockDurationBlockSizeOffset = 10, | 50 kBlockGroupWithoutBlockDurationBlockSizeOffset = 10, |
51 kBlockGroupDurationOffset = 11, | 51 kBlockGroupDurationOffset = 11, |
52 kBlockGroupBlockSizeOffset = 20, | 52 kBlockGroupBlockSizeOffset = 20, |
53 | 53 |
54 kInitialBufferSize = 32768, | 54 kInitialBufferSize = 32768, |
55 }; | 55 }; |
56 | 56 |
57 Cluster::Cluster(scoped_ptr<uint8[]> data, int size) | 57 Cluster::Cluster(scoped_ptr<uint8_t[]> data, int size) |
58 : data_(data.Pass()), size_(size) {} | 58 : data_(data.Pass()), size_(size) {} |
59 Cluster::~Cluster() {} | 59 Cluster::~Cluster() {} |
60 | 60 |
61 ClusterBuilder::ClusterBuilder() { Reset(); } | 61 ClusterBuilder::ClusterBuilder() { Reset(); } |
62 ClusterBuilder::~ClusterBuilder() {} | 62 ClusterBuilder::~ClusterBuilder() {} |
63 | 63 |
64 void ClusterBuilder::SetClusterTimecode(int64 cluster_timecode) { | 64 void ClusterBuilder::SetClusterTimecode(int64_t cluster_timecode) { |
65 DCHECK_EQ(cluster_timecode_, -1); | 65 DCHECK_EQ(cluster_timecode_, -1); |
66 | 66 |
67 cluster_timecode_ = cluster_timecode; | 67 cluster_timecode_ = cluster_timecode; |
68 | 68 |
69 // Write the timecode into the header. | 69 // Write the timecode into the header. |
70 uint8* buf = buffer_.get() + kClusterTimecodeOffset; | 70 uint8_t* buf = buffer_.get() + kClusterTimecodeOffset; |
71 for (int i = 7; i >= 0; --i) { | 71 for (int i = 7; i >= 0; --i) { |
72 buf[i] = cluster_timecode & 0xff; | 72 buf[i] = cluster_timecode & 0xff; |
73 cluster_timecode >>= 8; | 73 cluster_timecode >>= 8; |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 void ClusterBuilder::AddSimpleBlock(int track_num, int64 timecode, int flags, | 77 void ClusterBuilder::AddSimpleBlock(int track_num, |
78 const uint8* data, int size) { | 78 int64_t timecode, |
| 79 int flags, |
| 80 const uint8_t* data, |
| 81 int size) { |
79 int block_size = size + 4; | 82 int block_size = size + 4; |
80 int bytes_needed = sizeof(kSimpleBlockHeader) + block_size; | 83 int bytes_needed = sizeof(kSimpleBlockHeader) + block_size; |
81 if (bytes_needed > (buffer_size_ - bytes_used_)) | 84 if (bytes_needed > (buffer_size_ - bytes_used_)) |
82 ExtendBuffer(bytes_needed); | 85 ExtendBuffer(bytes_needed); |
83 | 86 |
84 uint8* buf = buffer_.get() + bytes_used_; | 87 uint8_t* buf = buffer_.get() + bytes_used_; |
85 int block_offset = bytes_used_; | 88 int block_offset = bytes_used_; |
86 memcpy(buf, kSimpleBlockHeader, sizeof(kSimpleBlockHeader)); | 89 memcpy(buf, kSimpleBlockHeader, sizeof(kSimpleBlockHeader)); |
87 UpdateUInt64(block_offset + kSimpleBlockSizeOffset, block_size); | 90 UpdateUInt64(block_offset + kSimpleBlockSizeOffset, block_size); |
88 buf += sizeof(kSimpleBlockHeader); | 91 buf += sizeof(kSimpleBlockHeader); |
89 | 92 |
90 WriteBlock(buf, track_num, timecode, flags, data, size); | 93 WriteBlock(buf, track_num, timecode, flags, data, size); |
91 | 94 |
92 bytes_used_ += bytes_needed; | 95 bytes_used_ += bytes_needed; |
93 } | 96 } |
94 | 97 |
95 void ClusterBuilder::AddBlockGroup(int track_num, int64 timecode, int duration, | 98 void ClusterBuilder::AddBlockGroup(int track_num, |
96 int flags, const uint8* data, int size) { | 99 int64_t timecode, |
| 100 int duration, |
| 101 int flags, |
| 102 const uint8_t* data, |
| 103 int size) { |
97 AddBlockGroupInternal(track_num, timecode, true, duration, flags, data, size); | 104 AddBlockGroupInternal(track_num, timecode, true, duration, flags, data, size); |
98 } | 105 } |
99 | 106 |
100 void ClusterBuilder::AddBlockGroupWithoutBlockDuration(int track_num, | 107 void ClusterBuilder::AddBlockGroupWithoutBlockDuration(int track_num, |
101 int64 timecode, | 108 int64_t timecode, |
102 int flags, | 109 int flags, |
103 const uint8* data, | 110 const uint8_t* data, |
104 int size) { | 111 int size) { |
105 AddBlockGroupInternal(track_num, timecode, false, 0, flags, data, size); | 112 AddBlockGroupInternal(track_num, timecode, false, 0, flags, data, size); |
106 } | 113 } |
107 | 114 |
108 | 115 void ClusterBuilder::AddBlockGroupInternal(int track_num, |
109 void ClusterBuilder::AddBlockGroupInternal(int track_num, int64 timecode, | 116 int64_t timecode, |
110 bool include_block_duration, | 117 bool include_block_duration, |
111 int duration, int flags, | 118 int duration, |
112 const uint8* data, int size) { | 119 int flags, |
| 120 const uint8_t* data, |
| 121 int size) { |
113 int block_size = size + 4; | 122 int block_size = size + 4; |
114 int bytes_needed = block_size; | 123 int bytes_needed = block_size; |
115 if (include_block_duration) { | 124 if (include_block_duration) { |
116 bytes_needed += sizeof(kBlockGroupHeader); | 125 bytes_needed += sizeof(kBlockGroupHeader); |
117 } else { | 126 } else { |
118 bytes_needed += sizeof(kBlockGroupHeaderWithoutBlockDuration); | 127 bytes_needed += sizeof(kBlockGroupHeaderWithoutBlockDuration); |
119 } | 128 } |
120 | 129 |
121 int block_group_size = bytes_needed - 9; | 130 int block_group_size = bytes_needed - 9; |
122 | 131 |
123 if (bytes_needed > (buffer_size_ - bytes_used_)) | 132 if (bytes_needed > (buffer_size_ - bytes_used_)) |
124 ExtendBuffer(bytes_needed); | 133 ExtendBuffer(bytes_needed); |
125 | 134 |
126 uint8* buf = buffer_.get() + bytes_used_; | 135 uint8_t* buf = buffer_.get() + bytes_used_; |
127 int block_group_offset = bytes_used_; | 136 int block_group_offset = bytes_used_; |
128 if (include_block_duration) { | 137 if (include_block_duration) { |
129 memcpy(buf, kBlockGroupHeader, sizeof(kBlockGroupHeader)); | 138 memcpy(buf, kBlockGroupHeader, sizeof(kBlockGroupHeader)); |
130 UpdateUInt64(block_group_offset + kBlockGroupDurationOffset, duration); | 139 UpdateUInt64(block_group_offset + kBlockGroupDurationOffset, duration); |
131 UpdateUInt64(block_group_offset + kBlockGroupBlockSizeOffset, block_size); | 140 UpdateUInt64(block_group_offset + kBlockGroupBlockSizeOffset, block_size); |
132 buf += sizeof(kBlockGroupHeader); | 141 buf += sizeof(kBlockGroupHeader); |
133 } else { | 142 } else { |
134 memcpy(buf, kBlockGroupHeaderWithoutBlockDuration, | 143 memcpy(buf, kBlockGroupHeaderWithoutBlockDuration, |
135 sizeof(kBlockGroupHeaderWithoutBlockDuration)); | 144 sizeof(kBlockGroupHeaderWithoutBlockDuration)); |
136 UpdateUInt64( | 145 UpdateUInt64( |
137 block_group_offset + kBlockGroupWithoutBlockDurationBlockSizeOffset, | 146 block_group_offset + kBlockGroupWithoutBlockDurationBlockSizeOffset, |
138 block_size); | 147 block_size); |
139 buf += sizeof(kBlockGroupHeaderWithoutBlockDuration); | 148 buf += sizeof(kBlockGroupHeaderWithoutBlockDuration); |
140 } | 149 } |
141 | 150 |
142 UpdateUInt64(block_group_offset + kBlockGroupSizeOffset, block_group_size); | 151 UpdateUInt64(block_group_offset + kBlockGroupSizeOffset, block_group_size); |
143 | 152 |
144 // Make sure the 4 most-significant bits are 0. | 153 // Make sure the 4 most-significant bits are 0. |
145 // http://www.matroska.org/technical/specs/index.html#block_structure | 154 // http://www.matroska.org/technical/specs/index.html#block_structure |
146 flags &= 0x0f; | 155 flags &= 0x0f; |
147 | 156 |
148 WriteBlock(buf, track_num, timecode, flags, data, size); | 157 WriteBlock(buf, track_num, timecode, flags, data, size); |
149 | 158 |
150 bytes_used_ += bytes_needed; | 159 bytes_used_ += bytes_needed; |
151 } | 160 } |
152 | 161 |
153 void ClusterBuilder::WriteBlock(uint8* buf, int track_num, int64 timecode, | 162 void ClusterBuilder::WriteBlock(uint8_t* buf, |
154 int flags, const uint8* data, int size) { | 163 int track_num, |
| 164 int64_t timecode, |
| 165 int flags, |
| 166 const uint8_t* data, |
| 167 int size) { |
155 DCHECK_GE(track_num, 0); | 168 DCHECK_GE(track_num, 0); |
156 DCHECK_LE(track_num, 126); | 169 DCHECK_LE(track_num, 126); |
157 DCHECK_GE(flags, 0); | 170 DCHECK_GE(flags, 0); |
158 DCHECK_LE(flags, 0xff); | 171 DCHECK_LE(flags, 0xff); |
159 DCHECK(data); | 172 DCHECK(data); |
160 DCHECK_GT(size, 0); | 173 DCHECK_GT(size, 0); |
161 DCHECK_NE(cluster_timecode_, -1); | 174 DCHECK_NE(cluster_timecode_, -1); |
162 | 175 |
163 int64 timecode_delta = timecode - cluster_timecode_; | 176 int64_t timecode_delta = timecode - cluster_timecode_; |
164 DCHECK_GE(timecode_delta, -32768); | 177 DCHECK_GE(timecode_delta, -32768); |
165 DCHECK_LE(timecode_delta, 32767); | 178 DCHECK_LE(timecode_delta, 32767); |
166 | 179 |
167 buf[0] = 0x80 | (track_num & 0x7F); | 180 buf[0] = 0x80 | (track_num & 0x7F); |
168 buf[1] = (timecode_delta >> 8) & 0xff; | 181 buf[1] = (timecode_delta >> 8) & 0xff; |
169 buf[2] = timecode_delta & 0xff; | 182 buf[2] = timecode_delta & 0xff; |
170 buf[3] = flags & 0xff; | 183 buf[3] = flags & 0xff; |
171 memcpy(buf + 4, data, size); | 184 memcpy(buf + 4, data, size); |
172 } | 185 } |
173 | 186 |
(...skipping 12 matching lines...) Expand all Loading... |
186 | 199 |
187 UpdateUInt64(kClusterSizeOffset, kWebMUnknownSize); | 200 UpdateUInt64(kClusterSizeOffset, kWebMUnknownSize); |
188 | 201 |
189 scoped_ptr<Cluster> ret(new Cluster(buffer_.Pass(), bytes_used_)); | 202 scoped_ptr<Cluster> ret(new Cluster(buffer_.Pass(), bytes_used_)); |
190 Reset(); | 203 Reset(); |
191 return ret.Pass(); | 204 return ret.Pass(); |
192 } | 205 } |
193 | 206 |
194 void ClusterBuilder::Reset() { | 207 void ClusterBuilder::Reset() { |
195 buffer_size_ = kInitialBufferSize; | 208 buffer_size_ = kInitialBufferSize; |
196 buffer_.reset(new uint8[buffer_size_]); | 209 buffer_.reset(new uint8_t[buffer_size_]); |
197 memcpy(buffer_.get(), kClusterHeader, sizeof(kClusterHeader)); | 210 memcpy(buffer_.get(), kClusterHeader, sizeof(kClusterHeader)); |
198 bytes_used_ = sizeof(kClusterHeader); | 211 bytes_used_ = sizeof(kClusterHeader); |
199 cluster_timecode_ = -1; | 212 cluster_timecode_ = -1; |
200 } | 213 } |
201 | 214 |
202 void ClusterBuilder::ExtendBuffer(int bytes_needed) { | 215 void ClusterBuilder::ExtendBuffer(int bytes_needed) { |
203 int new_buffer_size = 2 * buffer_size_; | 216 int new_buffer_size = 2 * buffer_size_; |
204 | 217 |
205 while ((new_buffer_size - bytes_used_) < bytes_needed) | 218 while ((new_buffer_size - bytes_used_) < bytes_needed) |
206 new_buffer_size *= 2; | 219 new_buffer_size *= 2; |
207 | 220 |
208 scoped_ptr<uint8[]> new_buffer(new uint8[new_buffer_size]); | 221 scoped_ptr<uint8_t[]> new_buffer(new uint8_t[new_buffer_size]); |
209 | 222 |
210 memcpy(new_buffer.get(), buffer_.get(), bytes_used_); | 223 memcpy(new_buffer.get(), buffer_.get(), bytes_used_); |
211 buffer_.reset(new_buffer.release()); | 224 buffer_.reset(new_buffer.release()); |
212 buffer_size_ = new_buffer_size; | 225 buffer_size_ = new_buffer_size; |
213 } | 226 } |
214 | 227 |
215 void ClusterBuilder::UpdateUInt64(int offset, int64 value) { | 228 void ClusterBuilder::UpdateUInt64(int offset, int64_t value) { |
216 DCHECK_LE(offset + 7, buffer_size_); | 229 DCHECK_LE(offset + 7, buffer_size_); |
217 uint8* buf = buffer_.get() + offset; | 230 uint8_t* buf = buffer_.get() + offset; |
218 | 231 |
219 // Fill the last 7 bytes of size field in big-endian order. | 232 // Fill the last 7 bytes of size field in big-endian order. |
220 for (int i = 7; i > 0; i--) { | 233 for (int i = 7; i > 0; i--) { |
221 buf[i] = value & 0xff; | 234 buf[i] = value & 0xff; |
222 value >>= 8; | 235 value >>= 8; |
223 } | 236 } |
224 } | 237 } |
225 | 238 |
226 } // namespace media | 239 } // namespace media |
OLD | NEW |