| OLD | NEW |
| 1 // Copyright (c) 2012 The WebM project authors. All Rights Reserved. | 1 // Copyright (c) 2012 The WebM project authors. All Rights Reserved. |
| 2 // | 2 // |
| 3 // Use of this source code is governed by a BSD-style license | 3 // Use of this source code is governed by a BSD-style license |
| 4 // that can be found in the LICENSE file in the root of the source | 4 // that can be found in the LICENSE file in the root of the source |
| 5 // tree. An additional intellectual property rights grant can be found | 5 // tree. An additional intellectual property rights grant can be found |
| 6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
| 7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
| 8 | 8 |
| 9 #include "mkvmuxerutil.hpp" | 9 #include "mkvmuxerutil.hpp" |
| 10 | 10 |
| 11 #ifdef __ANDROID__ | 11 #ifdef __ANDROID__ |
| 12 #include <fcntl.h> | 12 #include <fcntl.h> |
| 13 #endif | 13 #endif |
| 14 | 14 |
| 15 #include <cassert> | 15 #include <cassert> |
| 16 #include <cmath> | 16 #include <cmath> |
| 17 #include <cstdio> | 17 #include <cstdio> |
| 18 #ifdef _MSC_VER | |
| 19 #define _CRT_RAND_S | |
| 20 #endif | |
| 21 #include <cstdlib> | 18 #include <cstdlib> |
| 22 #include <cstring> | 19 #include <cstring> |
| 23 #include <ctime> | 20 #include <ctime> |
| 24 | |
| 25 #include <new> | 21 #include <new> |
| 26 | 22 |
| 27 #include "mkvwriter.hpp" | 23 #include "mkvwriter.hpp" |
| 28 #include "webmids.hpp" | 24 #include "webmids.hpp" |
| 29 | 25 |
| 26 #ifdef _MSC_VER |
| 27 // Disable MSVC warnings that suggest making code non-portable. |
| 28 #pragma warning(disable : 4996) |
| 29 #endif |
| 30 |
| 30 namespace mkvmuxer { | 31 namespace mkvmuxer { |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 // Date elements are always 8 octets in size. | 35 // Date elements are always 8 octets in size. |
| 35 const int kDateElementSize = 8; | 36 const int kDateElementSize = 8; |
| 36 | 37 |
| 38 uint64 WriteBlock(IMkvWriter* writer, const Frame* const frame, int64 timecode, |
| 39 uint64 timecode_scale) { |
| 40 uint64 block_additional_elem_size = 0; |
| 41 uint64 block_addid_elem_size = 0; |
| 42 uint64 block_more_payload_size = 0; |
| 43 uint64 block_more_elem_size = 0; |
| 44 uint64 block_additions_payload_size = 0; |
| 45 uint64 block_additions_elem_size = 0; |
| 46 if (frame->additional()) { |
| 47 block_additional_elem_size = EbmlElementSize( |
| 48 kMkvBlockAdditional, frame->additional(), frame->additional_length()); |
| 49 block_addid_elem_size = EbmlElementSize(kMkvBlockAddID, frame->add_id()); |
| 50 |
| 51 block_more_payload_size = |
| 52 block_addid_elem_size + block_additional_elem_size; |
| 53 block_more_elem_size = |
| 54 EbmlMasterElementSize(kMkvBlockMore, block_more_payload_size) + |
| 55 block_more_payload_size; |
| 56 block_additions_payload_size = block_more_elem_size; |
| 57 block_additions_elem_size = |
| 58 EbmlMasterElementSize(kMkvBlockAdditions, |
| 59 block_additions_payload_size) + |
| 60 block_additions_payload_size; |
| 61 } |
| 62 |
| 63 uint64 discard_padding_elem_size = 0; |
| 64 if (frame->discard_padding() != 0) { |
| 65 discard_padding_elem_size = |
| 66 EbmlElementSize(kMkvDiscardPadding, frame->discard_padding()); |
| 67 } |
| 68 |
| 69 const uint64 reference_block_timestamp = |
| 70 frame->reference_block_timestamp() / timecode_scale; |
| 71 uint64 reference_block_elem_size = 0; |
| 72 if (!frame->is_key()) { |
| 73 reference_block_elem_size = |
| 74 EbmlElementSize(kMkvReferenceBlock, reference_block_timestamp); |
| 75 } |
| 76 |
| 77 const uint64 duration = frame->duration() / timecode_scale; |
| 78 uint64 block_duration_elem_size = 0; |
| 79 if (duration > 0) |
| 80 block_duration_elem_size = EbmlElementSize(kMkvBlockDuration, duration); |
| 81 |
| 82 const uint64 block_payload_size = 4 + frame->length(); |
| 83 const uint64 block_elem_size = |
| 84 EbmlMasterElementSize(kMkvBlock, block_payload_size) + block_payload_size; |
| 85 |
| 86 const uint64 block_group_payload_size = |
| 87 block_elem_size + block_additions_elem_size + block_duration_elem_size + |
| 88 discard_padding_elem_size + reference_block_elem_size; |
| 89 |
| 90 if (!WriteEbmlMasterElement(writer, kMkvBlockGroup, |
| 91 block_group_payload_size)) { |
| 92 return 0; |
| 93 } |
| 94 |
| 95 if (!WriteEbmlMasterElement(writer, kMkvBlock, block_payload_size)) |
| 96 return 0; |
| 97 |
| 98 if (WriteUInt(writer, frame->track_number())) |
| 99 return 0; |
| 100 |
| 101 if (SerializeInt(writer, timecode, 2)) |
| 102 return 0; |
| 103 |
| 104 // For a Block, flags is always 0. |
| 105 if (SerializeInt(writer, 0, 1)) |
| 106 return 0; |
| 107 |
| 108 if (writer->Write(frame->frame(), static_cast<uint32>(frame->length()))) |
| 109 return 0; |
| 110 |
| 111 if (frame->additional()) { |
| 112 if (!WriteEbmlMasterElement(writer, kMkvBlockAdditions, |
| 113 block_additions_payload_size)) { |
| 114 return 0; |
| 115 } |
| 116 |
| 117 if (!WriteEbmlMasterElement(writer, kMkvBlockMore, block_more_payload_size)) |
| 118 return 0; |
| 119 |
| 120 if (!WriteEbmlElement(writer, kMkvBlockAddID, frame->add_id())) |
| 121 return 0; |
| 122 |
| 123 if (!WriteEbmlElement(writer, kMkvBlockAdditional, frame->additional(), |
| 124 frame->additional_length())) { |
| 125 return 0; |
| 126 } |
| 127 } |
| 128 |
| 129 if (frame->discard_padding() != 0 && |
| 130 !WriteEbmlElement(writer, kMkvDiscardPadding, frame->discard_padding())) { |
| 131 return false; |
| 132 } |
| 133 |
| 134 if (!frame->is_key() && |
| 135 !WriteEbmlElement(writer, kMkvReferenceBlock, |
| 136 reference_block_timestamp)) { |
| 137 return false; |
| 138 } |
| 139 |
| 140 if (duration > 0 && !WriteEbmlElement(writer, kMkvBlockDuration, duration)) { |
| 141 return false; |
| 142 } |
| 143 return EbmlMasterElementSize(kMkvBlockGroup, block_group_payload_size) + |
| 144 block_group_payload_size; |
| 145 } |
| 146 |
| 147 uint64 WriteSimpleBlock(IMkvWriter* writer, const Frame* const frame, |
| 148 int64 timecode) { |
| 149 if (WriteID(writer, kMkvSimpleBlock)) |
| 150 return 0; |
| 151 |
| 152 const int32 size = static_cast<int32>(frame->length()) + 4; |
| 153 if (WriteUInt(writer, size)) |
| 154 return 0; |
| 155 |
| 156 if (WriteUInt(writer, static_cast<uint64>(frame->track_number()))) |
| 157 return 0; |
| 158 |
| 159 if (SerializeInt(writer, timecode, 2)) |
| 160 return 0; |
| 161 |
| 162 uint64 flags = 0; |
| 163 if (frame->is_key()) |
| 164 flags |= 0x80; |
| 165 |
| 166 if (SerializeInt(writer, flags, 1)) |
| 167 return 0; |
| 168 |
| 169 if (writer->Write(frame->frame(), static_cast<uint32>(frame->length()))) |
| 170 return 0; |
| 171 |
| 172 return GetUIntSize(kMkvSimpleBlock) + GetCodedUIntSize(size) + 4 + |
| 173 frame->length(); |
| 174 } |
| 175 |
| 37 } // namespace | 176 } // namespace |
| 38 | 177 |
| 39 int32 GetCodedUIntSize(uint64 value) { | 178 int32 GetCodedUIntSize(uint64 value) { |
| 40 if (value < 0x000000000000007FULL) | 179 if (value < 0x000000000000007FULL) |
| 41 return 1; | 180 return 1; |
| 42 else if (value < 0x0000000000003FFFULL) | 181 else if (value < 0x0000000000003FFFULL) |
| 43 return 2; | 182 return 2; |
| 44 else if (value < 0x00000000001FFFFFULL) | 183 else if (value < 0x00000000001FFFFFULL) |
| 45 return 3; | 184 return 3; |
| 46 else if (value < 0x000000000FFFFFFFULL) | 185 else if (value < 0x000000000FFFFFFFULL) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 65 return 4; | 204 return 4; |
| 66 else if (value < 0x0000010000000000ULL) | 205 else if (value < 0x0000010000000000ULL) |
| 67 return 5; | 206 return 5; |
| 68 else if (value < 0x0001000000000000ULL) | 207 else if (value < 0x0001000000000000ULL) |
| 69 return 6; | 208 return 6; |
| 70 else if (value < 0x0100000000000000ULL) | 209 else if (value < 0x0100000000000000ULL) |
| 71 return 7; | 210 return 7; |
| 72 return 8; | 211 return 8; |
| 73 } | 212 } |
| 74 | 213 |
| 214 int32 GetIntSize(int64 value) { |
| 215 // Doubling the requested value ensures positive values with their high bit |
| 216 // set are written with 0-padding to avoid flipping the signedness. |
| 217 const uint64 v = (value < 0) ? value ^ -1LL : value; |
| 218 return GetUIntSize(2 * v); |
| 219 } |
| 220 |
| 75 uint64 EbmlMasterElementSize(uint64 type, uint64 value) { | 221 uint64 EbmlMasterElementSize(uint64 type, uint64 value) { |
| 76 // Size of EBML ID | 222 // Size of EBML ID |
| 77 int32 ebml_size = GetUIntSize(type); | 223 int32 ebml_size = GetUIntSize(type); |
| 78 | 224 |
| 79 // Datasize | 225 // Datasize |
| 80 ebml_size += GetCodedUIntSize(value); | 226 ebml_size += GetCodedUIntSize(value); |
| 81 | 227 |
| 82 return ebml_size; | 228 return ebml_size; |
| 83 } | 229 } |
| 84 | 230 |
| 85 uint64 EbmlElementSize(uint64 type, int64 value) { | 231 uint64 EbmlElementSize(uint64 type, int64 value) { |
| 86 return EbmlElementSize(type, static_cast<uint64>(value)); | 232 // Size of EBML ID |
| 233 int32 ebml_size = GetUIntSize(type); |
| 234 |
| 235 // Datasize |
| 236 ebml_size += GetIntSize(value); |
| 237 |
| 238 // Size of Datasize |
| 239 ebml_size++; |
| 240 |
| 241 return ebml_size; |
| 87 } | 242 } |
| 88 | 243 |
| 89 uint64 EbmlElementSize(uint64 type, uint64 value) { | 244 uint64 EbmlElementSize(uint64 type, uint64 value) { |
| 90 // Size of EBML ID | 245 // Size of EBML ID |
| 91 int32 ebml_size = GetUIntSize(type); | 246 int32 ebml_size = GetUIntSize(type); |
| 92 | 247 |
| 93 // Datasize | 248 // Datasize |
| 94 ebml_size += GetUIntSize(value); | 249 ebml_size += GetUIntSize(value); |
| 95 | 250 |
| 96 // Size of Datasize | 251 // Size of Datasize |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 292 |
| 138 // Datasize | 293 // Datasize |
| 139 ebml_size += size; | 294 ebml_size += size; |
| 140 | 295 |
| 141 // Size of Datasize | 296 // Size of Datasize |
| 142 ebml_size += GetCodedUIntSize(size); | 297 ebml_size += GetCodedUIntSize(size); |
| 143 | 298 |
| 144 return ebml_size; | 299 return ebml_size; |
| 145 } | 300 } |
| 146 | 301 |
| 147 uint64 EbmlDateElementSize(uint64 type, int64 value) { | 302 uint64 EbmlDateElementSize(uint64 type) { |
| 148 // Size of EBML ID | 303 // Size of EBML ID |
| 149 uint64 ebml_size = GetUIntSize(type); | 304 uint64 ebml_size = GetUIntSize(type); |
| 150 | 305 |
| 151 // Datasize | 306 // Datasize |
| 152 ebml_size += kDateElementSize; | 307 ebml_size += kDateElementSize; |
| 153 | 308 |
| 154 // Size of Datasize | 309 // Size of Datasize |
| 155 ebml_size++; | 310 ebml_size++; |
| 156 | 311 |
| 157 return ebml_size; | 312 return ebml_size; |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 const uint64 size = GetUIntSize(value); | 437 const uint64 size = GetUIntSize(value); |
| 283 if (WriteUInt(writer, size)) | 438 if (WriteUInt(writer, size)) |
| 284 return false; | 439 return false; |
| 285 | 440 |
| 286 if (SerializeInt(writer, value, static_cast<int32>(size))) | 441 if (SerializeInt(writer, value, static_cast<int32>(size))) |
| 287 return false; | 442 return false; |
| 288 | 443 |
| 289 return true; | 444 return true; |
| 290 } | 445 } |
| 291 | 446 |
| 447 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, int64 value) { |
| 448 if (!writer) |
| 449 return false; |
| 450 |
| 451 if (WriteID(writer, type)) |
| 452 return 0; |
| 453 |
| 454 const uint64 size = GetIntSize(value); |
| 455 if (WriteUInt(writer, size)) |
| 456 return false; |
| 457 |
| 458 if (SerializeInt(writer, value, static_cast<int32>(size))) |
| 459 return false; |
| 460 |
| 461 return true; |
| 462 } |
| 463 |
| 292 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, float value) { | 464 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, float value) { |
| 293 if (!writer) | 465 if (!writer) |
| 294 return false; | 466 return false; |
| 295 | 467 |
| 296 if (WriteID(writer, type)) | 468 if (WriteID(writer, type)) |
| 297 return false; | 469 return false; |
| 298 | 470 |
| 299 if (WriteUInt(writer, 4)) | 471 if (WriteUInt(writer, 4)) |
| 300 return false; | 472 return false; |
| 301 | 473 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 | 520 |
| 349 if (WriteUInt(writer, kDateElementSize)) | 521 if (WriteUInt(writer, kDateElementSize)) |
| 350 return false; | 522 return false; |
| 351 | 523 |
| 352 if (SerializeInt(writer, value, kDateElementSize)) | 524 if (SerializeInt(writer, value, kDateElementSize)) |
| 353 return false; | 525 return false; |
| 354 | 526 |
| 355 return true; | 527 return true; |
| 356 } | 528 } |
| 357 | 529 |
| 358 uint64 WriteSimpleBlock(IMkvWriter* writer, const uint8* data, uint64 length, | 530 uint64 WriteFrame(IMkvWriter* writer, const Frame* const frame, |
| 359 uint64 track_number, int64 timecode, uint64 is_key) { | 531 Cluster* cluster) { |
| 360 if (!writer) | 532 if (!writer || !frame || !frame->IsValid() || !cluster || |
| 361 return false; | 533 !cluster->timecode_scale()) |
| 362 | |
| 363 if (!data || length < 1) | |
| 364 return false; | |
| 365 | |
| 366 // Here we only permit track number values to be no greater than | |
| 367 // 126, which the largest value we can store having a Matroska | |
| 368 // integer representation of only 1 byte. | |
| 369 | |
| 370 if (track_number < 1 || track_number > 126) | |
| 371 return false; | |
| 372 | |
| 373 // Technically the timestamp for a block can be less than the | |
| 374 // timestamp for the cluster itself (remember that block timestamp | |
| 375 // is a signed, 16-bit integer). However, as a simplification we | |
| 376 // only permit non-negative cluster-relative timestamps for blocks. | |
| 377 | |
| 378 if (timecode < 0 || timecode > kMaxBlockTimecode) | |
| 379 return false; | |
| 380 | |
| 381 if (WriteID(writer, kMkvSimpleBlock)) | |
| 382 return 0; | 534 return 0; |
| 383 | 535 |
| 384 const int32 size = static_cast<int32>(length) + 4; | 536 // Technically the timecode for a block can be less than the |
| 385 if (WriteUInt(writer, size)) | 537 // timecode for the cluster itself (remember that block timecode |
| 538 // is a signed, 16-bit integer). However, as a simplification we |
| 539 // only permit non-negative cluster-relative timecodes for blocks. |
| 540 const int64 relative_timecode = cluster->GetRelativeTimecode( |
| 541 frame->timestamp() / cluster->timecode_scale()); |
| 542 if (relative_timecode < 0 || relative_timecode > kMaxBlockTimecode) |
| 386 return 0; | 543 return 0; |
| 387 | 544 |
| 388 if (WriteUInt(writer, static_cast<uint64>(track_number))) | 545 return frame->CanBeSimpleBlock() ? |
| 389 return 0; | 546 WriteSimpleBlock(writer, frame, relative_timecode) : |
| 390 | 547 WriteBlock(writer, frame, relative_timecode, |
| 391 if (SerializeInt(writer, timecode, 2)) | 548 cluster->timecode_scale()); |
| 392 return 0; | |
| 393 | |
| 394 uint64 flags = 0; | |
| 395 if (is_key) | |
| 396 flags |= 0x80; | |
| 397 | |
| 398 if (SerializeInt(writer, flags, 1)) | |
| 399 return 0; | |
| 400 | |
| 401 if (writer->Write(data, static_cast<uint32>(length))) | |
| 402 return 0; | |
| 403 | |
| 404 const uint64 element_size = | |
| 405 GetUIntSize(kMkvSimpleBlock) + GetCodedUIntSize(size) + 4 + length; | |
| 406 | |
| 407 return element_size; | |
| 408 } | |
| 409 | |
| 410 // We must write the metadata (key)frame as a BlockGroup element, | |
| 411 // because we need to specify a duration for the frame. The | |
| 412 // BlockGroup element comprises the frame itself and its duration, | |
| 413 // and is laid out as follows: | |
| 414 // | |
| 415 // BlockGroup tag | |
| 416 // BlockGroup size | |
| 417 // Block tag | |
| 418 // Block size | |
| 419 // (the frame is the block payload) | |
| 420 // Duration tag | |
| 421 // Duration size | |
| 422 // (duration payload) | |
| 423 // | |
| 424 uint64 WriteMetadataBlock(IMkvWriter* writer, const uint8* data, uint64 length, | |
| 425 uint64 track_number, int64 timecode, | |
| 426 uint64 duration) { | |
| 427 // We don't backtrack when writing to the stream, so we must | |
| 428 // pre-compute the BlockGroup size, by summing the sizes of each | |
| 429 // sub-element (the block and the duration). | |
| 430 | |
| 431 // We use a single byte for the track number of the block, which | |
| 432 // means the block header is exactly 4 bytes. | |
| 433 | |
| 434 // TODO(matthewjheaney): use EbmlMasterElementSize and WriteEbmlMasterElement | |
| 435 | |
| 436 const uint64 block_payload_size = 4 + length; | |
| 437 const int32 block_size = GetCodedUIntSize(block_payload_size); | |
| 438 const uint64 block_elem_size = 1 + block_size + block_payload_size; | |
| 439 | |
| 440 const int32 duration_payload_size = GetUIntSize(duration); | |
| 441 const int32 duration_size = GetCodedUIntSize(duration_payload_size); | |
| 442 const uint64 duration_elem_size = 1 + duration_size + duration_payload_size; | |
| 443 | |
| 444 const uint64 blockg_payload_size = block_elem_size + duration_elem_size; | |
| 445 const int32 blockg_size = GetCodedUIntSize(blockg_payload_size); | |
| 446 const uint64 blockg_elem_size = 1 + blockg_size + blockg_payload_size; | |
| 447 | |
| 448 if (WriteID(writer, kMkvBlockGroup)) // 1-byte ID size | |
| 449 return 0; | |
| 450 | |
| 451 if (WriteUInt(writer, blockg_payload_size)) | |
| 452 return 0; | |
| 453 | |
| 454 // Write Block element | |
| 455 | |
| 456 if (WriteID(writer, kMkvBlock)) // 1-byte ID size | |
| 457 return 0; | |
| 458 | |
| 459 if (WriteUInt(writer, block_payload_size)) | |
| 460 return 0; | |
| 461 | |
| 462 // Byte 1 of 4 | |
| 463 | |
| 464 if (WriteUInt(writer, track_number)) | |
| 465 return 0; | |
| 466 | |
| 467 // Bytes 2 & 3 of 4 | |
| 468 | |
| 469 if (SerializeInt(writer, timecode, 2)) | |
| 470 return 0; | |
| 471 | |
| 472 // Byte 4 of 4 | |
| 473 | |
| 474 const uint64 flags = 0; | |
| 475 | |
| 476 if (SerializeInt(writer, flags, 1)) | |
| 477 return 0; | |
| 478 | |
| 479 // Now write the actual frame (of metadata) | |
| 480 | |
| 481 if (writer->Write(data, static_cast<uint32>(length))) | |
| 482 return 0; | |
| 483 | |
| 484 // Write Duration element | |
| 485 | |
| 486 if (WriteID(writer, kMkvBlockDuration)) // 1-byte ID size | |
| 487 return 0; | |
| 488 | |
| 489 if (WriteUInt(writer, duration_payload_size)) | |
| 490 return 0; | |
| 491 | |
| 492 if (SerializeInt(writer, duration, duration_payload_size)) | |
| 493 return 0; | |
| 494 | |
| 495 // Note that we don't write a reference time as part of the block | |
| 496 // group; no reference time(s) indicates that this block is a | |
| 497 // keyframe. (Unlike the case for a SimpleBlock element, the header | |
| 498 // bits of the Block sub-element of a BlockGroup element do not | |
| 499 // indicate keyframe status. The keyframe status is inferred from | |
| 500 // the absence of reference time sub-elements.) | |
| 501 | |
| 502 return blockg_elem_size; | |
| 503 } | |
| 504 | |
| 505 // Writes a WebM BlockGroup with BlockAdditional data. The structure is as | |
| 506 // follows: | |
| 507 // Indentation shows sub-levels | |
| 508 // BlockGroup | |
| 509 // Block | |
| 510 // Data | |
| 511 // BlockAdditions | |
| 512 // BlockMore | |
| 513 // BlockAddID | |
| 514 // 1 (Denotes Alpha) | |
| 515 // BlockAdditional | |
| 516 // Data | |
| 517 uint64 WriteBlockWithAdditional(IMkvWriter* writer, const uint8* data, | |
| 518 uint64 length, const uint8* additional, | |
| 519 uint64 additional_length, uint64 add_id, | |
| 520 uint64 track_number, int64 timecode, | |
| 521 uint64 is_key) { | |
| 522 if (!data || !additional || length < 1 || additional_length < 1) | |
| 523 return 0; | |
| 524 | |
| 525 const uint64 block_payload_size = 4 + length; | |
| 526 const uint64 block_elem_size = | |
| 527 EbmlMasterElementSize(kMkvBlock, block_payload_size) + block_payload_size; | |
| 528 const uint64 block_additional_elem_size = | |
| 529 EbmlElementSize(kMkvBlockAdditional, additional, additional_length); | |
| 530 const uint64 block_addid_elem_size = EbmlElementSize(kMkvBlockAddID, add_id); | |
| 531 | |
| 532 const uint64 block_more_payload_size = | |
| 533 block_addid_elem_size + block_additional_elem_size; | |
| 534 const uint64 block_more_elem_size = | |
| 535 EbmlMasterElementSize(kMkvBlockMore, block_more_payload_size) + | |
| 536 block_more_payload_size; | |
| 537 const uint64 block_additions_payload_size = block_more_elem_size; | |
| 538 const uint64 block_additions_elem_size = | |
| 539 EbmlMasterElementSize(kMkvBlockAdditions, block_additions_payload_size) + | |
| 540 block_additions_payload_size; | |
| 541 const uint64 block_group_payload_size = | |
| 542 block_elem_size + block_additions_elem_size; | |
| 543 const uint64 block_group_elem_size = | |
| 544 EbmlMasterElementSize(kMkvBlockGroup, block_group_payload_size) + | |
| 545 block_group_payload_size; | |
| 546 | |
| 547 if (!WriteEbmlMasterElement(writer, kMkvBlockGroup, block_group_payload_size)) | |
| 548 return 0; | |
| 549 | |
| 550 if (!WriteEbmlMasterElement(writer, kMkvBlock, block_payload_size)) | |
| 551 return 0; | |
| 552 | |
| 553 if (WriteUInt(writer, track_number)) | |
| 554 return 0; | |
| 555 | |
| 556 if (SerializeInt(writer, timecode, 2)) | |
| 557 return 0; | |
| 558 | |
| 559 uint64 flags = 0; | |
| 560 if (is_key) | |
| 561 flags |= 0x80; | |
| 562 if (SerializeInt(writer, flags, 1)) | |
| 563 return 0; | |
| 564 | |
| 565 if (writer->Write(data, static_cast<uint32>(length))) | |
| 566 return 0; | |
| 567 | |
| 568 if (!WriteEbmlMasterElement(writer, kMkvBlockAdditions, | |
| 569 block_additions_payload_size)) | |
| 570 return 0; | |
| 571 | |
| 572 if (!WriteEbmlMasterElement(writer, kMkvBlockMore, block_more_payload_size)) | |
| 573 return 0; | |
| 574 | |
| 575 if (!WriteEbmlElement(writer, kMkvBlockAddID, add_id)) | |
| 576 return 0; | |
| 577 | |
| 578 if (!WriteEbmlElement(writer, kMkvBlockAdditional, additional, | |
| 579 additional_length)) | |
| 580 return 0; | |
| 581 | |
| 582 return block_group_elem_size; | |
| 583 } | |
| 584 | |
| 585 // Writes a WebM BlockGroup with DiscardPadding. The structure is as follows: | |
| 586 // Indentation shows sub-levels | |
| 587 // BlockGroup | |
| 588 // Block | |
| 589 // Data | |
| 590 // DiscardPadding | |
| 591 uint64 WriteBlockWithDiscardPadding(IMkvWriter* writer, const uint8* data, | |
| 592 uint64 length, int64 discard_padding, | |
| 593 uint64 track_number, int64 timecode, | |
| 594 uint64 is_key) { | |
| 595 if (!data || length < 1 || discard_padding <= 0) | |
| 596 return 0; | |
| 597 | |
| 598 const uint64 block_payload_size = 4 + length; | |
| 599 const uint64 block_elem_size = | |
| 600 EbmlMasterElementSize(kMkvBlock, block_payload_size) + block_payload_size; | |
| 601 const uint64 discard_padding_elem_size = | |
| 602 EbmlElementSize(kMkvDiscardPadding, discard_padding); | |
| 603 const uint64 block_group_payload_size = | |
| 604 block_elem_size + discard_padding_elem_size; | |
| 605 const uint64 block_group_elem_size = | |
| 606 EbmlMasterElementSize(kMkvBlockGroup, block_group_payload_size) + | |
| 607 block_group_payload_size; | |
| 608 | |
| 609 if (!WriteEbmlMasterElement(writer, kMkvBlockGroup, block_group_payload_size)) | |
| 610 return 0; | |
| 611 | |
| 612 if (!WriteEbmlMasterElement(writer, kMkvBlock, block_payload_size)) | |
| 613 return 0; | |
| 614 | |
| 615 if (WriteUInt(writer, track_number)) | |
| 616 return 0; | |
| 617 | |
| 618 if (SerializeInt(writer, timecode, 2)) | |
| 619 return 0; | |
| 620 | |
| 621 uint64 flags = 0; | |
| 622 if (is_key) | |
| 623 flags |= 0x80; | |
| 624 if (SerializeInt(writer, flags, 1)) | |
| 625 return 0; | |
| 626 | |
| 627 if (writer->Write(data, static_cast<uint32>(length))) | |
| 628 return 0; | |
| 629 | |
| 630 if (WriteID(writer, kMkvDiscardPadding)) | |
| 631 return 0; | |
| 632 | |
| 633 const uint64 size = GetUIntSize(discard_padding); | |
| 634 if (WriteUInt(writer, size)) | |
| 635 return false; | |
| 636 | |
| 637 if (SerializeInt(writer, discard_padding, static_cast<int32>(size))) | |
| 638 return false; | |
| 639 | |
| 640 return block_group_elem_size; | |
| 641 } | 549 } |
| 642 | 550 |
| 643 uint64 WriteVoidElement(IMkvWriter* writer, uint64 size) { | 551 uint64 WriteVoidElement(IMkvWriter* writer, uint64 size) { |
| 644 if (!writer) | 552 if (!writer) |
| 645 return false; | 553 return false; |
| 646 | 554 |
| 647 // Subtract one for the void ID and the coded size. | 555 // Subtract one for the void ID and the coded size. |
| 648 uint64 void_entry_size = size - 1 - GetCodedUIntSize(size - 1); | 556 uint64 void_entry_size = size - 1 - GetCodedUIntSize(size - 1); |
| 649 uint64 void_size = | 557 uint64 void_size = |
| 650 EbmlMasterElementSize(kMkvVoid, void_entry_size) + void_entry_size; | 558 EbmlMasterElementSize(kMkvVoid, void_entry_size) + void_entry_size; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 #ifdef __MINGW32__ | 599 #ifdef __MINGW32__ |
| 692 srand(*seed); | 600 srand(*seed); |
| 693 #endif | 601 #endif |
| 694 | 602 |
| 695 for (int i = 0; i < 7; ++i) { // avoid problems with 8-byte values | 603 for (int i = 0; i < 7; ++i) { // avoid problems with 8-byte values |
| 696 uid <<= 8; | 604 uid <<= 8; |
| 697 | 605 |
| 698 // TODO(fgalligan): Move random number generation to platform specific code. | 606 // TODO(fgalligan): Move random number generation to platform specific code. |
| 699 #ifdef _MSC_VER | 607 #ifdef _MSC_VER |
| 700 (void)seed; | 608 (void)seed; |
| 701 unsigned int random_value; | 609 const int32 nn = rand(); |
| 702 const errno_t e = rand_s(&random_value); | |
| 703 (void)e; | |
| 704 const int32 nn = random_value; | |
| 705 #elif __ANDROID__ | 610 #elif __ANDROID__ |
| 706 int32 temp_num = 1; | 611 int32 temp_num = 1; |
| 707 int fd = open("/dev/urandom", O_RDONLY); | 612 int fd = open("/dev/urandom", O_RDONLY); |
| 708 if (fd != -1) { | 613 if (fd != -1) { |
| 709 read(fd, &temp_num, sizeof(int32)); | 614 read(fd, &temp_num, sizeof(int32)); |
| 710 close(fd); | 615 close(fd); |
| 711 } | 616 } |
| 712 const int32 nn = temp_num; | 617 const int32 nn = temp_num; |
| 713 #elif defined __MINGW32__ | 618 #elif defined __MINGW32__ |
| 714 const int32 nn = rand(); | 619 const int32 nn = rand(); |
| 715 #else | 620 #else |
| 716 const int32 nn = rand_r(seed); | 621 const int32 nn = rand_r(seed); |
| 717 #endif | 622 #endif |
| 718 const int32 n = 0xFF & (nn >> 4); // throw away low-order bits | 623 const int32 n = 0xFF & (nn >> 4); // throw away low-order bits |
| 719 | 624 |
| 720 uid |= n; | 625 uid |= n; |
| 721 } | 626 } |
| 722 | 627 |
| 723 return uid; | 628 return uid; |
| 724 } | 629 } |
| OLD | NEW |