DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
box_definitions.cc
1 // Copyright (c) 2012 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/mp4/box_definitions.h"
6 
7 #include <limits>
8 
9 #include "packager/base/logging.h"
10 #include "packager/media/base/bit_reader.h"
11 #include "packager/media/base/macros.h"
12 #include "packager/media/base/rcheck.h"
13 #include "packager/media/formats/mp4/box_buffer.h"
14 
15 namespace {
16 const uint32_t kFourCCSize = 4;
17 
18 // Key Id size as defined in CENC spec.
19 const uint32_t kCencKeyIdSize = 16;
20 
21 // 9 uint32_t in big endian formatted array.
22 const uint8_t kUnityMatrix[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23  0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
24  0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0};
25 
26 // Default entries for HandlerReference box.
27 const char kVideoHandlerName[] = "VideoHandler";
28 const char kAudioHandlerName[] = "SoundHandler";
29 const char kTextHandlerName[] = "TextHandler";
30 
31 // Default values for VideoSampleEntry box.
32 const uint32_t kVideoResolution = 0x00480000; // 72 dpi.
33 const uint16_t kVideoFrameCount = 1;
34 const uint16_t kVideoDepth = 0x0018;
35 
36 const uint32_t kCompressorNameSize = 32u;
37 const char kAvcCompressorName[] = "\012AVC Coding";
38 const char kHevcCompressorName[] = "\013HEVC Coding";
39 const char kVpcCompressorName[] = "\012VPC Coding";
40 
41 // Using negative value as "not set". It is very unlikely that 2^31 cues happen
42 // at once.
43 const int kCueSourceIdNotSet = -1;
44 
45 const size_t kInvalidIvSize = 1;
46 // According to ISO/IEC FDIS 23001-7: CENC spec, IV should be either
47 // 64-bit (8-byte) or 128-bit (16-byte).
48 // |per_sample_iv_size| of 0 means constant_iv is used.
49 bool IsIvSizeValid(uint8_t per_sample_iv_size) {
50  return per_sample_iv_size == 0 || per_sample_iv_size == 8 ||
51  per_sample_iv_size == 16;
52 }
53 
54 // Default values to construct the following fields in ddts box. Values are set
55 // according to FFMPEG.
56 // bit(2) FrameDuration; // 3 = 4096
57 // bit(5) StreamConstruction; // 18
58 // bit(1) CoreLFEPresent; // 0 = none
59 // bit(6) CoreLayout; // 31 = ignore core layout
60 // bit(14) CoreSize; // 0
61 // bit(1) StereoDownmix // 0 = none
62 // bit(3) RepresentationType; // 4
63 // bit(16) ChannelLayout; // 0xf = 5.1 channel layout.
64 // bit(1) MultiAssetFlag // 0 = single asset
65 // bit(1) LBRDurationMod // 0 = ignore
66 // bit(1) ReservedBoxPresent // 0 = none
67 // bit(5) Reserved // 0
68 const uint8_t kDdtsExtraData[] = {0xe4, 0x7c, 0, 4, 0, 0x0f, 0};
69 
70 // ID3v2 header: http://id3.org/id3v2.4.0-structure
71 const uint32_t kID3v2HeaderSize = 10;
72 const char kID3v2Identifier[] = "ID3";
73 const uint16_t kID3v2Version = 0x0400; // id3v2.4.0
74 
75 // Utility functions to check if the 64bit integers can fit in 32bit integer.
76 bool IsFitIn32Bits(uint64_t a) {
77  return a <= std::numeric_limits<uint32_t>::max();
78 }
79 
80 bool IsFitIn32Bits(int64_t a) {
81  return a <= std::numeric_limits<int32_t>::max() &&
82  a >= std::numeric_limits<int32_t>::min();
83 }
84 
85 template <typename T1, typename T2>
86 bool IsFitIn32Bits(T1 a1, T2 a2) {
87  return IsFitIn32Bits(a1) && IsFitIn32Bits(a2);
88 }
89 
90 template <typename T1, typename T2, typename T3>
91 bool IsFitIn32Bits(T1 a1, T2 a2, T3 a3) {
92  return IsFitIn32Bits(a1) && IsFitIn32Bits(a2) && IsFitIn32Bits(a3);
93 }
94 
95 } // namespace
96 
97 namespace shaka {
98 namespace media {
99 namespace mp4 {
100 
101 namespace {
102 
103 TrackType FourCCToTrackType(FourCC fourcc) {
104  switch (fourcc) {
105  case FOURCC_vide:
106  return kVideo;
107  case FOURCC_soun:
108  return kAudio;
109  case FOURCC_text:
110  return kText;
111  default:
112  return kInvalid;
113  }
114 }
115 
116 FourCC TrackTypeToFourCC(TrackType track_type) {
117  switch (track_type) {
118  case kVideo:
119  return FOURCC_vide;
120  case kAudio:
121  return FOURCC_soun;
122  case kText:
123  return FOURCC_text;
124  default:
125  return FOURCC_NULL;
126  }
127 }
128 
129 bool IsProtectionSchemeSupported(FourCC scheme) {
130  return scheme == FOURCC_cenc || scheme == FOURCC_cens ||
131  scheme == FOURCC_cbc1 || scheme == FOURCC_cbcs;
132 }
133 
134 } // namespace
135 
136 FileType::FileType() : major_brand(FOURCC_NULL), minor_version(0) {}
137 FileType::~FileType() {}
138 FourCC FileType::BoxType() const { return FOURCC_ftyp; }
139 
140 bool FileType::ReadWriteInternal(BoxBuffer* buffer) {
141  RCHECK(ReadWriteHeaderInternal(buffer) &&
142  buffer->ReadWriteFourCC(&major_brand) &&
143  buffer->ReadWriteUInt32(&minor_version));
144  size_t num_brands;
145  if (buffer->Reading()) {
146  RCHECK(buffer->BytesLeft() % sizeof(FourCC) == 0);
147  num_brands = buffer->BytesLeft() / sizeof(FourCC);
148  compatible_brands.resize(num_brands);
149  } else {
150  num_brands = compatible_brands.size();
151  }
152  for (size_t i = 0; i < num_brands; ++i)
153  RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
154  return true;
155 }
156 
157 size_t FileType::ComputeSizeInternal() {
158  return HeaderSize() + kFourCCSize + sizeof(minor_version) +
159  kFourCCSize * compatible_brands.size();
160 }
161 
162 FourCC SegmentType::BoxType() const { return FOURCC_styp; }
163 
164 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() {}
165 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() {}
166 FourCC ProtectionSystemSpecificHeader::BoxType() const { return FOURCC_pssh; }
167 
168 bool ProtectionSystemSpecificHeader::ReadWriteInternal(BoxBuffer* buffer) {
169  if (buffer->Reading()) {
170  BoxReader* reader = buffer->reader();
171  DCHECK(reader);
172  raw_box.assign(reader->data(), reader->data() + reader->size());
173  } else {
174  DCHECK(!raw_box.empty());
175  buffer->writer()->AppendVector(raw_box);
176  }
177 
178  return true;
179 }
180 
181 size_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
182  return raw_box.size();
183 }
184 
185 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
186 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
187 FourCC SampleAuxiliaryInformationOffset::BoxType() const { return FOURCC_saio; }
188 
189 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(BoxBuffer* buffer) {
190  RCHECK(ReadWriteHeaderInternal(buffer));
191  if (flags & 1)
192  RCHECK(buffer->IgnoreBytes(8)); // aux_info_type and parameter.
193 
194  uint32_t count = static_cast<uint32_t>(offsets.size());
195  RCHECK(buffer->ReadWriteUInt32(&count));
196  offsets.resize(count);
197 
198  size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
199  for (uint32_t i = 0; i < count; ++i)
200  RCHECK(buffer->ReadWriteUInt64NBytes(&offsets[i], num_bytes));
201  return true;
202 }
203 
204 size_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
205  // This box is optional. Skip it if it is empty.
206  if (offsets.size() == 0)
207  return 0;
208  size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
209  return HeaderSize() + sizeof(uint32_t) + num_bytes * offsets.size();
210 }
211 
212 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
213  : default_sample_info_size(0), sample_count(0) {}
214 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
215 FourCC SampleAuxiliaryInformationSize::BoxType() const { return FOURCC_saiz; }
216 
217 bool SampleAuxiliaryInformationSize::ReadWriteInternal(BoxBuffer* buffer) {
218  RCHECK(ReadWriteHeaderInternal(buffer));
219  if (flags & 1)
220  RCHECK(buffer->IgnoreBytes(8));
221 
222  RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
223  buffer->ReadWriteUInt32(&sample_count));
224  if (default_sample_info_size == 0)
225  RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
226  return true;
227 }
228 
229 size_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
230  // This box is optional. Skip it if it is empty.
231  if (sample_count == 0)
232  return 0;
233  return HeaderSize() + sizeof(default_sample_info_size) +
234  sizeof(sample_count) +
235  (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
236 }
237 
238 SampleEncryptionEntry::SampleEncryptionEntry() {}
239 SampleEncryptionEntry::~SampleEncryptionEntry() {}
240 
241 bool SampleEncryptionEntry::ReadWrite(uint8_t iv_size,
242  bool has_subsamples,
243  BoxBuffer* buffer) {
244  DCHECK(IsIvSizeValid(iv_size));
245  DCHECK(buffer);
246 
247  RCHECK(buffer->ReadWriteVector(&initialization_vector, iv_size));
248 
249  if (!has_subsamples) {
250  subsamples.clear();
251  return true;
252  }
253 
254  uint16_t subsample_count = static_cast<uint16_t>(subsamples.size());
255  RCHECK(buffer->ReadWriteUInt16(&subsample_count));
256  RCHECK(subsample_count > 0);
257  subsamples.resize(subsample_count);
258  for (auto& subsample : subsamples) {
259  RCHECK(buffer->ReadWriteUInt16(&subsample.clear_bytes) &&
260  buffer->ReadWriteUInt32(&subsample.cipher_bytes));
261  }
262  return true;
263 }
264 
266  bool has_subsamples,
267  BufferReader* reader) {
268  DCHECK(IsIvSizeValid(iv_size));
269  DCHECK(reader);
270 
271  initialization_vector.resize(iv_size);
272  RCHECK(reader->ReadToVector(&initialization_vector, iv_size));
273 
274  if (!has_subsamples) {
275  subsamples.clear();
276  return true;
277  }
278 
279  uint16_t subsample_count;
280  RCHECK(reader->Read2(&subsample_count));
281  RCHECK(subsample_count > 0);
282  subsamples.resize(subsample_count);
283  for (auto& subsample : subsamples) {
284  RCHECK(reader->Read2(&subsample.clear_bytes) &&
285  reader->Read4(&subsample.cipher_bytes));
286  }
287  return true;
288 }
289 
291  const uint32_t subsample_entry_size = sizeof(uint16_t) + sizeof(uint32_t);
292  const uint16_t subsample_count = static_cast<uint16_t>(subsamples.size());
293  return static_cast<uint32_t>(
294  initialization_vector.size() +
295  (subsample_count > 0
296  ? (sizeof(subsample_count) + subsample_entry_size * subsample_count)
297  : 0));
298 }
299 
301  uint32_t size = 0;
302  for (uint32_t i = 0; i < subsamples.size(); ++i)
303  size += subsamples[i].clear_bytes + subsamples[i].cipher_bytes;
304  return size;
305 }
306 
307 SampleEncryption::SampleEncryption() : iv_size(kInvalidIvSize) {}
308 SampleEncryption::~SampleEncryption() {}
309 FourCC SampleEncryption::BoxType() const { return FOURCC_senc; }
310 
311 bool SampleEncryption::ReadWriteInternal(BoxBuffer* buffer) {
312  RCHECK(ReadWriteHeaderInternal(buffer));
313 
314  // If we don't know |iv_size|, store sample encryption data to parse later
315  // after we know iv_size.
316  if (buffer->Reading() && iv_size == kInvalidIvSize) {
317  RCHECK(
318  buffer->ReadWriteVector(&sample_encryption_data, buffer->BytesLeft()));
319  return true;
320  }
321 
322  if (!IsIvSizeValid(iv_size)) {
323  LOG(ERROR)
324  << "IV_size can only be 8 or 16 or 0 for constant iv, but seeing "
325  << iv_size;
326  return false;
327  }
328 
329  uint32_t sample_count =
330  static_cast<uint32_t>(sample_encryption_entries.size());
331  RCHECK(buffer->ReadWriteUInt32(&sample_count));
332 
333  sample_encryption_entries.resize(sample_count);
334  for (auto& sample_encryption_entry : sample_encryption_entries) {
335  RCHECK(sample_encryption_entry.ReadWrite(
336  iv_size, (flags & kUseSubsampleEncryption) != 0, buffer) != 0);
337  }
338  return true;
339 }
340 
341 size_t SampleEncryption::ComputeSizeInternal() {
342  const uint32_t sample_count =
343  static_cast<uint32_t>(sample_encryption_entries.size());
344  if (sample_count == 0) {
345  // Sample encryption box is optional. Skip it if it is empty.
346  return 0;
347  }
348 
349  DCHECK(IsIvSizeValid(iv_size));
350  size_t box_size = HeaderSize() + sizeof(sample_count);
351  if (flags & kUseSubsampleEncryption) {
352  for (const SampleEncryptionEntry& sample_encryption_entry :
353  sample_encryption_entries) {
354  box_size += sample_encryption_entry.ComputeSize();
355  }
356  } else {
357  box_size += sample_count * iv_size;
358  }
359  return box_size;
360 }
361 
363  uint8_t iv_size,
364  std::vector<SampleEncryptionEntry>* sample_encryption_entries) const {
365  DCHECK(IsIvSizeValid(iv_size));
366 
367  BufferReader reader(sample_encryption_data.data(),
368  sample_encryption_data.size());
369  uint32_t sample_count = 0;
370  RCHECK(reader.Read4(&sample_count));
371 
372  sample_encryption_entries->resize(sample_count);
373  for (auto& sample_encryption_entry : *sample_encryption_entries) {
374  RCHECK(sample_encryption_entry.ParseFromBuffer(
375  iv_size, (flags & kUseSubsampleEncryption) != 0, &reader) != 0);
376  }
377  return true;
378 }
379 
380 OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
381 OriginalFormat::~OriginalFormat() {}
382 FourCC OriginalFormat::BoxType() const { return FOURCC_frma; }
383 
384 bool OriginalFormat::ReadWriteInternal(BoxBuffer* buffer) {
385  return ReadWriteHeaderInternal(buffer) && buffer->ReadWriteFourCC(&format);
386 }
387 
388 size_t OriginalFormat::ComputeSizeInternal() {
389  return HeaderSize() + kFourCCSize;
390 }
391 
392 SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
393 SchemeType::~SchemeType() {}
394 FourCC SchemeType::BoxType() const { return FOURCC_schm; }
395 
396 bool SchemeType::ReadWriteInternal(BoxBuffer* buffer) {
397  RCHECK(ReadWriteHeaderInternal(buffer) &&
398  buffer->ReadWriteFourCC(&type) &&
399  buffer->ReadWriteUInt32(&version));
400  return true;
401 }
402 
403 size_t SchemeType::ComputeSizeInternal() {
404  return HeaderSize() + kFourCCSize + sizeof(version);
405 }
406 
407 TrackEncryption::TrackEncryption()
408  : default_is_protected(0),
409  default_per_sample_iv_size(0),
410  default_kid(16, 0),
411  default_crypt_byte_block(0),
412  default_skip_byte_block(0) {}
413 TrackEncryption::~TrackEncryption() {}
414 FourCC TrackEncryption::BoxType() const { return FOURCC_tenc; }
415 
416 bool TrackEncryption::ReadWriteInternal(BoxBuffer* buffer) {
417  if (!buffer->Reading()) {
418  if (default_kid.size() != kCencKeyIdSize) {
419  LOG(WARNING) << "CENC defines key id length of " << kCencKeyIdSize
420  << " bytes; got " << default_kid.size()
421  << ". Resized accordingly.";
422  default_kid.resize(kCencKeyIdSize);
423  }
424  RCHECK(default_crypt_byte_block < 16 && default_skip_byte_block < 16);
425  if (default_crypt_byte_block != 0 && default_skip_byte_block != 0) {
426  // Version 1 box is needed for pattern-based encryption.
427  version = 1;
428  }
429  }
430 
431  RCHECK(ReadWriteHeaderInternal(buffer) &&
432  buffer->IgnoreBytes(1)); // reserved.
433 
434  uint8_t pattern = default_crypt_byte_block << 4 | default_skip_byte_block;
435  RCHECK(buffer->ReadWriteUInt8(&pattern));
436  default_crypt_byte_block = pattern >> 4;
437  default_skip_byte_block = pattern & 0x0F;
438 
439  RCHECK(buffer->ReadWriteUInt8(&default_is_protected) &&
440  buffer->ReadWriteUInt8(&default_per_sample_iv_size) &&
441  buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
442 
443  if (default_is_protected == 1) {
444  if (default_per_sample_iv_size == 0) { // For constant iv.
445  uint8_t default_constant_iv_size =
446  static_cast<uint8_t>(default_constant_iv.size());
447  RCHECK(buffer->ReadWriteUInt8(&default_constant_iv_size));
448  RCHECK(default_constant_iv_size == 8 || default_constant_iv_size == 16);
449  RCHECK(buffer->ReadWriteVector(&default_constant_iv,
450  default_constant_iv_size));
451  } else {
452  RCHECK(default_per_sample_iv_size == 8 ||
453  default_per_sample_iv_size == 16);
454  RCHECK(default_constant_iv.empty());
455  }
456  } else {
457  // Expect |default_is_protected| to be 0, i.e. not protected. Other values
458  // of |default_is_protected| is not supported.
459  RCHECK(default_is_protected == 0);
460  RCHECK(default_per_sample_iv_size == 0);
461  RCHECK(default_constant_iv.empty());
462  }
463  return true;
464 }
465 
466 size_t TrackEncryption::ComputeSizeInternal() {
467  return HeaderSize() + sizeof(uint32_t) + kCencKeyIdSize +
468  (default_constant_iv.empty() ? 0 : (sizeof(uint8_t) +
469  default_constant_iv.size()));
470 }
471 
472 SchemeInfo::SchemeInfo() {}
473 SchemeInfo::~SchemeInfo() {}
474 FourCC SchemeInfo::BoxType() const { return FOURCC_schi; }
475 
476 bool SchemeInfo::ReadWriteInternal(BoxBuffer* buffer) {
477  RCHECK(ReadWriteHeaderInternal(buffer) && buffer->PrepareChildren() &&
478  buffer->ReadWriteChild(&track_encryption));
479  return true;
480 }
481 
482 size_t SchemeInfo::ComputeSizeInternal() {
483  return HeaderSize() + track_encryption.ComputeSize();
484 }
485 
486 ProtectionSchemeInfo::ProtectionSchemeInfo() {}
487 ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
488 FourCC ProtectionSchemeInfo::BoxType() const { return FOURCC_sinf; }
489 
490 bool ProtectionSchemeInfo::ReadWriteInternal(BoxBuffer* buffer) {
491  RCHECK(ReadWriteHeaderInternal(buffer) &&
492  buffer->PrepareChildren() &&
493  buffer->ReadWriteChild(&format) &&
494  buffer->ReadWriteChild(&type));
495  if (IsProtectionSchemeSupported(type.type)) {
496  RCHECK(buffer->ReadWriteChild(&info));
497  } else {
498  DLOG(WARNING) << "Ignore unsupported protection scheme: "
499  << FourCCToString(type.type);
500  }
501  // Other protection schemes are silently ignored. Since the protection scheme
502  // type can't be determined until this box is opened, we return 'true' for
503  // non-CENC protection scheme types. It is the parent box's responsibility to
504  // ensure that this scheme type is a supported one.
505  return true;
506 }
507 
508 size_t ProtectionSchemeInfo::ComputeSizeInternal() {
509  // Skip sinf box if it is not initialized.
510  if (format.format == FOURCC_NULL)
511  return 0;
512  return HeaderSize() + format.ComputeSize() + type.ComputeSize() +
513  info.ComputeSize();
514 }
515 
516 MovieHeader::MovieHeader()
517  : creation_time(0),
518  modification_time(0),
519  timescale(0),
520  duration(0),
521  rate(1 << 16),
522  volume(1 << 8),
523  next_track_id(0) {}
524 MovieHeader::~MovieHeader() {}
525 FourCC MovieHeader::BoxType() const { return FOURCC_mvhd; }
526 
527 bool MovieHeader::ReadWriteInternal(BoxBuffer* buffer) {
528  RCHECK(ReadWriteHeaderInternal(buffer));
529 
530  size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
531  RCHECK(buffer->ReadWriteUInt64NBytes(&creation_time, num_bytes) &&
532  buffer->ReadWriteUInt64NBytes(&modification_time, num_bytes) &&
533  buffer->ReadWriteUInt32(&timescale) &&
534  buffer->ReadWriteUInt64NBytes(&duration, num_bytes));
535 
536  std::vector<uint8_t> matrix(kUnityMatrix,
537  kUnityMatrix + arraysize(kUnityMatrix));
538  RCHECK(buffer->ReadWriteInt32(&rate) &&
539  buffer->ReadWriteInt16(&volume) &&
540  buffer->IgnoreBytes(10) && // reserved
541  buffer->ReadWriteVector(&matrix, matrix.size()) &&
542  buffer->IgnoreBytes(24) && // predefined zero
543  buffer->ReadWriteUInt32(&next_track_id));
544  return true;
545 }
546 
547 size_t MovieHeader::ComputeSizeInternal() {
548  version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
549  return HeaderSize() + sizeof(uint32_t) * (1 + version) * 3 +
550  sizeof(timescale) + sizeof(rate) + sizeof(volume) +
551  sizeof(next_track_id) + sizeof(kUnityMatrix) + 10 +
552  24; // 10 bytes reserved, 24 bytes predefined.
553 }
554 
555 TrackHeader::TrackHeader()
556  : creation_time(0),
557  modification_time(0),
558  track_id(0),
559  duration(0),
560  layer(0),
561  alternate_group(0),
562  volume(-1),
563  width(0),
564  height(0) {
565  flags = kTrackEnabled | kTrackInMovie;
566 }
567 TrackHeader::~TrackHeader() {}
568 FourCC TrackHeader::BoxType() const { return FOURCC_tkhd; }
569 
570 bool TrackHeader::ReadWriteInternal(BoxBuffer* buffer) {
571  RCHECK(ReadWriteHeaderInternal(buffer));
572 
573  size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
574  RCHECK(buffer->ReadWriteUInt64NBytes(&creation_time, num_bytes) &&
575  buffer->ReadWriteUInt64NBytes(&modification_time, num_bytes) &&
576  buffer->ReadWriteUInt32(&track_id) &&
577  buffer->IgnoreBytes(4) && // reserved
578  buffer->ReadWriteUInt64NBytes(&duration, num_bytes));
579 
580  if (!buffer->Reading()) {
581  // Set default value for volume, if track is audio, 0x100 else 0.
582  if (volume == -1)
583  volume = (width != 0 && height != 0) ? 0 : 0x100;
584  }
585  std::vector<uint8_t> matrix(kUnityMatrix,
586  kUnityMatrix + arraysize(kUnityMatrix));
587  RCHECK(buffer->IgnoreBytes(8) && // reserved
588  buffer->ReadWriteInt16(&layer) &&
589  buffer->ReadWriteInt16(&alternate_group) &&
590  buffer->ReadWriteInt16(&volume) &&
591  buffer->IgnoreBytes(2) && // reserved
592  buffer->ReadWriteVector(&matrix, matrix.size()) &&
593  buffer->ReadWriteUInt32(&width) &&
594  buffer->ReadWriteUInt32(&height));
595  return true;
596 }
597 
598 size_t TrackHeader::ComputeSizeInternal() {
599  version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
600  return HeaderSize() + sizeof(track_id) +
601  sizeof(uint32_t) * (1 + version) * 3 + sizeof(layer) +
602  sizeof(alternate_group) + sizeof(volume) + sizeof(width) +
603  sizeof(height) + sizeof(kUnityMatrix) + 14; // 14 bytes reserved.
604 }
605 
606 SampleDescription::SampleDescription() : type(kInvalid) {}
607 SampleDescription::~SampleDescription() {}
608 FourCC SampleDescription::BoxType() const { return FOURCC_stsd; }
609 
610 bool SampleDescription::ReadWriteInternal(BoxBuffer* buffer) {
611  uint32_t count = 0;
612  switch (type) {
613  case kVideo:
614  count = static_cast<uint32_t>(video_entries.size());
615  break;
616  case kAudio:
617  count = static_cast<uint32_t>(audio_entries.size());
618  break;
619  case kText:
620  count = static_cast<uint32_t>(text_entries.size());
621  break;
622  default:
623  NOTIMPLEMENTED() << "SampleDecryption type " << type
624  << " is not handled. Skipping.";
625  }
626  RCHECK(ReadWriteHeaderInternal(buffer) &&
627  buffer->ReadWriteUInt32(&count));
628 
629  if (buffer->Reading()) {
630  BoxReader* reader = buffer->reader();
631  DCHECK(reader);
632  video_entries.clear();
633  audio_entries.clear();
634  // Note: this value is preset before scanning begins. See comments in the
635  // Parse(Media*) function.
636  if (type == kVideo) {
637  RCHECK(reader->ReadAllChildren(&video_entries));
638  RCHECK(video_entries.size() == count);
639  } else if (type == kAudio) {
640  RCHECK(reader->ReadAllChildren(&audio_entries));
641  RCHECK(audio_entries.size() == count);
642  } else if (type == kText) {
643  RCHECK(reader->ReadAllChildren(&text_entries));
644  RCHECK(text_entries.size() == count);
645  }
646  } else {
647  DCHECK_LT(0u, count);
648  if (type == kVideo) {
649  for (uint32_t i = 0; i < count; ++i)
650  RCHECK(buffer->ReadWriteChild(&video_entries[i]));
651  } else if (type == kAudio) {
652  for (uint32_t i = 0; i < count; ++i)
653  RCHECK(buffer->ReadWriteChild(&audio_entries[i]));
654  } else if (type == kText) {
655  for (uint32_t i = 0; i < count; ++i)
656  RCHECK(buffer->ReadWriteChild(&text_entries[i]));
657  } else {
658  NOTIMPLEMENTED();
659  }
660  }
661  return true;
662 }
663 
664 size_t SampleDescription::ComputeSizeInternal() {
665  size_t box_size = HeaderSize() + sizeof(uint32_t);
666  if (type == kVideo) {
667  for (uint32_t i = 0; i < video_entries.size(); ++i)
668  box_size += video_entries[i].ComputeSize();
669  } else if (type == kAudio) {
670  for (uint32_t i = 0; i < audio_entries.size(); ++i)
671  box_size += audio_entries[i].ComputeSize();
672  } else if (type == kText) {
673  for (uint32_t i = 0; i < text_entries.size(); ++i)
674  box_size += text_entries[i].ComputeSize();
675  }
676  return box_size;
677 }
678 
679 DecodingTimeToSample::DecodingTimeToSample() {}
680 DecodingTimeToSample::~DecodingTimeToSample() {}
681 FourCC DecodingTimeToSample::BoxType() const { return FOURCC_stts; }
682 
683 bool DecodingTimeToSample::ReadWriteInternal(BoxBuffer* buffer) {
684  uint32_t count = static_cast<uint32_t>(decoding_time.size());
685  RCHECK(ReadWriteHeaderInternal(buffer) &&
686  buffer->ReadWriteUInt32(&count));
687 
688  decoding_time.resize(count);
689  for (uint32_t i = 0; i < count; ++i) {
690  RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
691  buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
692  }
693  return true;
694 }
695 
696 size_t DecodingTimeToSample::ComputeSizeInternal() {
697  return HeaderSize() + sizeof(uint32_t) +
698  sizeof(DecodingTime) * decoding_time.size();
699 }
700 
701 CompositionTimeToSample::CompositionTimeToSample() {}
702 CompositionTimeToSample::~CompositionTimeToSample() {}
703 FourCC CompositionTimeToSample::BoxType() const { return FOURCC_ctts; }
704 
705 bool CompositionTimeToSample::ReadWriteInternal(BoxBuffer* buffer) {
706  uint32_t count = static_cast<uint32_t>(composition_offset.size());
707  if (!buffer->Reading()) {
708  // Determine whether version 0 or version 1 should be used.
709  // Use version 0 if possible, use version 1 if there is a negative
710  // sample_offset value.
711  version = 0;
712  for (uint32_t i = 0; i < count; ++i) {
713  if (composition_offset[i].sample_offset < 0) {
714  version = 1;
715  break;
716  }
717  }
718  }
719 
720  RCHECK(ReadWriteHeaderInternal(buffer) &&
721  buffer->ReadWriteUInt32(&count));
722 
723  composition_offset.resize(count);
724  for (uint32_t i = 0; i < count; ++i) {
725  RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
726 
727  if (version == 0) {
728  uint32_t sample_offset = composition_offset[i].sample_offset;
729  RCHECK(buffer->ReadWriteUInt32(&sample_offset));
730  composition_offset[i].sample_offset = sample_offset;
731  } else {
732  int32_t sample_offset = composition_offset[i].sample_offset;
733  RCHECK(buffer->ReadWriteInt32(&sample_offset));
734  composition_offset[i].sample_offset = sample_offset;
735  }
736  }
737  return true;
738 }
739 
740 size_t CompositionTimeToSample::ComputeSizeInternal() {
741  // This box is optional. Skip it if it is empty.
742  if (composition_offset.empty())
743  return 0;
744  // Structure CompositionOffset contains |sample_offset| (uint32_t) and
745  // |sample_offset| (int64_t). The actual size of |sample_offset| is
746  // 4 bytes (uint32_t for version 0 and int32_t for version 1).
747  const size_t kCompositionOffsetSize = sizeof(uint32_t) * 2;
748  return HeaderSize() + sizeof(uint32_t) +
749  kCompositionOffsetSize * composition_offset.size();
750 }
751 
752 SampleToChunk::SampleToChunk() {}
753 SampleToChunk::~SampleToChunk() {}
754 FourCC SampleToChunk::BoxType() const { return FOURCC_stsc; }
755 
756 bool SampleToChunk::ReadWriteInternal(BoxBuffer* buffer) {
757  uint32_t count = static_cast<uint32_t>(chunk_info.size());
758  RCHECK(ReadWriteHeaderInternal(buffer) &&
759  buffer->ReadWriteUInt32(&count));
760 
761  chunk_info.resize(count);
762  for (uint32_t i = 0; i < count; ++i) {
763  RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
764  buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
765  buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
766  // first_chunk values are always increasing.
767  RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
768  : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
769  }
770  return true;
771 }
772 
773 size_t SampleToChunk::ComputeSizeInternal() {
774  return HeaderSize() + sizeof(uint32_t) +
775  sizeof(ChunkInfo) * chunk_info.size();
776 }
777 
778 SampleSize::SampleSize() : sample_size(0), sample_count(0) {}
779 SampleSize::~SampleSize() {}
780 FourCC SampleSize::BoxType() const { return FOURCC_stsz; }
781 
782 bool SampleSize::ReadWriteInternal(BoxBuffer* buffer) {
783  RCHECK(ReadWriteHeaderInternal(buffer) &&
784  buffer->ReadWriteUInt32(&sample_size) &&
785  buffer->ReadWriteUInt32(&sample_count));
786 
787  if (sample_size == 0) {
788  if (buffer->Reading())
789  sizes.resize(sample_count);
790  else
791  DCHECK(sample_count == sizes.size());
792  for (uint32_t i = 0; i < sample_count; ++i)
793  RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
794  }
795  return true;
796 }
797 
798 size_t SampleSize::ComputeSizeInternal() {
799  return HeaderSize() + sizeof(sample_size) + sizeof(sample_count) +
800  (sample_size == 0 ? sizeof(uint32_t) * sizes.size() : 0);
801 }
802 
803 CompactSampleSize::CompactSampleSize() : field_size(0) {}
804 CompactSampleSize::~CompactSampleSize() {}
805 FourCC CompactSampleSize::BoxType() const { return FOURCC_stz2; }
806 
807 bool CompactSampleSize::ReadWriteInternal(BoxBuffer* buffer) {
808  uint32_t sample_count = static_cast<uint32_t>(sizes.size());
809  RCHECK(ReadWriteHeaderInternal(buffer) &&
810  buffer->IgnoreBytes(3) &&
811  buffer->ReadWriteUInt8(&field_size) &&
812  buffer->ReadWriteUInt32(&sample_count));
813 
814  // Reserve one more entry if field size is 4 bits.
815  sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
816  switch (field_size) {
817  case 4:
818  for (uint32_t i = 0; i < sample_count; i += 2) {
819  if (buffer->Reading()) {
820  uint8_t size = 0;
821  RCHECK(buffer->ReadWriteUInt8(&size));
822  sizes[i] = size >> 4;
823  sizes[i + 1] = size & 0x0F;
824  } else {
825  DCHECK_LT(sizes[i], 16u);
826  DCHECK_LT(sizes[i + 1], 16u);
827  uint8_t size = (sizes[i] << 4) | sizes[i + 1];
828  RCHECK(buffer->ReadWriteUInt8(&size));
829  }
830  }
831  break;
832  case 8:
833  for (uint32_t i = 0; i < sample_count; ++i) {
834  uint8_t size = sizes[i];
835  RCHECK(buffer->ReadWriteUInt8(&size));
836  sizes[i] = size;
837  }
838  break;
839  case 16:
840  for (uint32_t i = 0; i < sample_count; ++i) {
841  uint16_t size = sizes[i];
842  RCHECK(buffer->ReadWriteUInt16(&size));
843  sizes[i] = size;
844  }
845  break;
846  default:
847  RCHECK(false);
848  }
849  sizes.resize(sample_count);
850  return true;
851 }
852 
853 size_t CompactSampleSize::ComputeSizeInternal() {
854  return HeaderSize() + sizeof(uint32_t) + sizeof(uint32_t) +
855  (field_size * sizes.size() + 7) / 8;
856 }
857 
858 ChunkOffset::ChunkOffset() {}
859 ChunkOffset::~ChunkOffset() {}
860 FourCC ChunkOffset::BoxType() const { return FOURCC_stco; }
861 
862 bool ChunkOffset::ReadWriteInternal(BoxBuffer* buffer) {
863  uint32_t count = static_cast<uint32_t>(offsets.size());
864  RCHECK(ReadWriteHeaderInternal(buffer) &&
865  buffer->ReadWriteUInt32(&count));
866 
867  offsets.resize(count);
868  for (uint32_t i = 0; i < count; ++i)
869  RCHECK(buffer->ReadWriteUInt64NBytes(&offsets[i], sizeof(uint32_t)));
870  return true;
871 }
872 
873 size_t ChunkOffset::ComputeSizeInternal() {
874  return HeaderSize() + sizeof(uint32_t) + sizeof(uint32_t) * offsets.size();
875 }
876 
877 ChunkLargeOffset::ChunkLargeOffset() {}
878 ChunkLargeOffset::~ChunkLargeOffset() {}
879 FourCC ChunkLargeOffset::BoxType() const { return FOURCC_co64; }
880 
881 bool ChunkLargeOffset::ReadWriteInternal(BoxBuffer* buffer) {
882  uint32_t count = static_cast<uint32_t>(offsets.size());
883 
884  if (!buffer->Reading()) {
885  // Switch to ChunkOffset box if it is able to fit in 32 bits offset.
886  if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
887  ChunkOffset stco;
888  stco.offsets.swap(offsets);
889  DCHECK(buffer->writer());
890  stco.Write(buffer->writer());
891  stco.offsets.swap(offsets);
892  return true;
893  }
894  }
895 
896  RCHECK(ReadWriteHeaderInternal(buffer) &&
897  buffer->ReadWriteUInt32(&count));
898 
899  offsets.resize(count);
900  for (uint32_t i = 0; i < count; ++i)
901  RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
902  return true;
903 }
904 
905 size_t ChunkLargeOffset::ComputeSizeInternal() {
906  uint32_t count = static_cast<uint32_t>(offsets.size());
907  int use_large_offset =
908  (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
909  return HeaderSize() + sizeof(count) +
910  sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
911 }
912 
913 SyncSample::SyncSample() {}
914 SyncSample::~SyncSample() {}
915 FourCC SyncSample::BoxType() const { return FOURCC_stss; }
916 
917 bool SyncSample::ReadWriteInternal(BoxBuffer* buffer) {
918  uint32_t count = static_cast<uint32_t>(sample_number.size());
919  RCHECK(ReadWriteHeaderInternal(buffer) &&
920  buffer->ReadWriteUInt32(&count));
921 
922  sample_number.resize(count);
923  for (uint32_t i = 0; i < count; ++i)
924  RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
925  return true;
926 }
927 
928 size_t SyncSample::ComputeSizeInternal() {
929  // Sync sample box is optional. Skip it if it is empty.
930  if (sample_number.empty())
931  return 0;
932  return HeaderSize() + sizeof(uint32_t) +
933  sizeof(uint32_t) * sample_number.size();
934 }
935 
936 CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
937  : is_protected(0),
938  per_sample_iv_size(0),
939  crypt_byte_block(0),
940  skip_byte_block(0) {}
941 CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {};
942 
943 bool CencSampleEncryptionInfoEntry::ReadWrite(BoxBuffer* buffer) {
944  if (!buffer->Reading()) {
945  if (key_id.size() != kCencKeyIdSize) {
946  LOG(WARNING) << "CENC defines key id length of " << kCencKeyIdSize
947  << " bytes; got " << key_id.size()
948  << ". Resized accordingly.";
949  key_id.resize(kCencKeyIdSize);
950  }
951  RCHECK(crypt_byte_block < 16 && skip_byte_block < 16);
952  }
953 
954  RCHECK(buffer->IgnoreBytes(1)); // reserved.
955 
956  uint8_t pattern = crypt_byte_block << 4 | skip_byte_block;
957  RCHECK(buffer->ReadWriteUInt8(&pattern));
958  crypt_byte_block = pattern >> 4;
959  skip_byte_block = pattern & 0x0F;
960 
961  RCHECK(buffer->ReadWriteUInt8(&is_protected) &&
962  buffer->ReadWriteUInt8(&per_sample_iv_size) &&
963  buffer->ReadWriteVector(&key_id, kCencKeyIdSize));
964 
965  if (is_protected == 1) {
966  if (per_sample_iv_size == 0) { // For constant iv.
967  uint8_t constant_iv_size = static_cast<uint8_t>(constant_iv.size());
968  RCHECK(buffer->ReadWriteUInt8(&constant_iv_size));
969  RCHECK(constant_iv_size == 8 || constant_iv_size == 16);
970  RCHECK(buffer->ReadWriteVector(&constant_iv, constant_iv_size));
971  } else {
972  RCHECK(per_sample_iv_size == 8 || per_sample_iv_size == 16);
973  DCHECK(constant_iv.empty());
974  }
975  } else {
976  // Expect |is_protected| to be 0, i.e. not protected. Other values of
977  // |is_protected| is not supported.
978  RCHECK(is_protected == 0);
979  RCHECK(per_sample_iv_size == 0);
980  }
981  return true;
982 }
983 
984 uint32_t CencSampleEncryptionInfoEntry::ComputeSize() const {
985  return static_cast<uint32_t>(
986  sizeof(uint32_t) + kCencKeyIdSize +
987  (constant_iv.empty() ? 0 : (sizeof(uint8_t) + constant_iv.size())));
988 }
989 
990 AudioRollRecoveryEntry::AudioRollRecoveryEntry(): roll_distance(0) {}
991 AudioRollRecoveryEntry::~AudioRollRecoveryEntry() {}
992 
993 bool AudioRollRecoveryEntry::ReadWrite(BoxBuffer* buffer) {
994  RCHECK(buffer->ReadWriteInt16(&roll_distance));
995  return true;
996 }
997 
998 uint32_t AudioRollRecoveryEntry::ComputeSize() const {
999  return sizeof(roll_distance);
1000 }
1001 
1002 SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
1003 SampleGroupDescription::~SampleGroupDescription() {}
1004 FourCC SampleGroupDescription::BoxType() const { return FOURCC_sgpd; }
1005 
1006 bool SampleGroupDescription::ReadWriteInternal(BoxBuffer* buffer) {
1007  RCHECK(ReadWriteHeaderInternal(buffer) &&
1008  buffer->ReadWriteUInt32(&grouping_type));
1009 
1010  switch (grouping_type) {
1011  case FOURCC_seig:
1012  return ReadWriteEntries(buffer, &cenc_sample_encryption_info_entries);
1013  case FOURCC_roll:
1014  return ReadWriteEntries(buffer, &audio_roll_recovery_entries);
1015  default:
1016  DCHECK(buffer->Reading());
1017  DLOG(WARNING) << "Ignore unsupported sample group: "
1018  << FourCCToString(static_cast<FourCC>(grouping_type));
1019  return true;
1020  }
1021 }
1022 
1023 template <typename T>
1024 bool SampleGroupDescription::ReadWriteEntries(BoxBuffer* buffer,
1025  std::vector<T>* entries) {
1026  uint32_t default_length = 0;
1027  if (!buffer->Reading()) {
1028  DCHECK(!entries->empty());
1029  default_length = (*entries)[0].ComputeSize();
1030  DCHECK_NE(default_length, 0u);
1031  }
1032  if (version == 1)
1033  RCHECK(buffer->ReadWriteUInt32(&default_length));
1034  if (version >= 2) {
1035  NOTIMPLEMENTED() << "Unsupported SampleGroupDescriptionBox 'sgpd' version "
1036  << static_cast<int>(version);
1037  return false;
1038  }
1039 
1040  uint32_t count = static_cast<uint32_t>(entries->size());
1041  RCHECK(buffer->ReadWriteUInt32(&count));
1042  RCHECK(count != 0);
1043  entries->resize(count);
1044 
1045  for (T& entry : *entries) {
1046  if (version == 1) {
1047  uint32_t description_length = default_length;
1048  if (buffer->Reading() && default_length == 0)
1049  RCHECK(buffer->ReadWriteUInt32(&description_length));
1050  RCHECK(entry.ReadWrite(buffer));
1051  RCHECK(entry.ComputeSize() == description_length);
1052  } else {
1053  RCHECK(entry.ReadWrite(buffer));
1054  }
1055  }
1056  return true;
1057 }
1058 
1059 size_t SampleGroupDescription::ComputeSizeInternal() {
1060  // Version 0 is obsoleted, so always generate version 1 box.
1061  version = 1;
1062  size_t entries_size = 0;
1063  switch (grouping_type) {
1064  case FOURCC_seig:
1065  for (const auto& entry : cenc_sample_encryption_info_entries)
1066  entries_size += entry.ComputeSize();
1067  break;
1068  case FOURCC_roll:
1069  for (const auto& entry : audio_roll_recovery_entries)
1070  entries_size += entry.ComputeSize();
1071  break;
1072  }
1073  // This box is optional. Skip it if it is not used.
1074  if (entries_size == 0)
1075  return 0;
1076  return HeaderSize() + sizeof(grouping_type) +
1077  (version == 1 ? sizeof(uint32_t) : 0) + sizeof(uint32_t) +
1078  entries_size;
1079 }
1080 
1081 SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
1082 SampleToGroup::~SampleToGroup() {}
1083 FourCC SampleToGroup::BoxType() const { return FOURCC_sbgp; }
1084 
1085 bool SampleToGroup::ReadWriteInternal(BoxBuffer* buffer) {
1086  RCHECK(ReadWriteHeaderInternal(buffer) &&
1087  buffer->ReadWriteUInt32(&grouping_type));
1088  if (version == 1)
1089  RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
1090 
1091  if (grouping_type != FOURCC_seig && grouping_type != FOURCC_roll) {
1092  DCHECK(buffer->Reading());
1093  DLOG(WARNING) << "Ignore unsupported sample group: "
1094  << FourCCToString(static_cast<FourCC>(grouping_type));
1095  return true;
1096  }
1097 
1098  uint32_t count = static_cast<uint32_t>(entries.size());
1099  RCHECK(buffer->ReadWriteUInt32(&count));
1100  entries.resize(count);
1101  for (uint32_t i = 0; i < count; ++i) {
1102  RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
1103  buffer->ReadWriteUInt32(&entries[i].group_description_index));
1104  }
1105  return true;
1106 }
1107 
1108 size_t SampleToGroup::ComputeSizeInternal() {
1109  // This box is optional. Skip it if it is not used.
1110  if (entries.empty())
1111  return 0;
1112  return HeaderSize() + sizeof(grouping_type) +
1113  (version == 1 ? sizeof(grouping_type_parameter) : 0) +
1114  sizeof(uint32_t) + entries.size() * sizeof(entries[0]);
1115 }
1116 
1117 SampleTable::SampleTable() {}
1118 SampleTable::~SampleTable() {}
1119 FourCC SampleTable::BoxType() const { return FOURCC_stbl; }
1120 
1121 bool SampleTable::ReadWriteInternal(BoxBuffer* buffer) {
1122  RCHECK(ReadWriteHeaderInternal(buffer) &&
1123  buffer->PrepareChildren() &&
1124  buffer->ReadWriteChild(&description) &&
1125  buffer->ReadWriteChild(&decoding_time_to_sample) &&
1126  buffer->TryReadWriteChild(&composition_time_to_sample) &&
1127  buffer->ReadWriteChild(&sample_to_chunk));
1128 
1129  if (buffer->Reading()) {
1130  BoxReader* reader = buffer->reader();
1131  DCHECK(reader);
1132 
1133  // Either SampleSize or CompactSampleSize must present.
1134  if (reader->ChildExist(&sample_size)) {
1135  RCHECK(reader->ReadChild(&sample_size));
1136  } else {
1137  CompactSampleSize compact_sample_size;
1138  RCHECK(reader->ReadChild(&compact_sample_size));
1139  sample_size.sample_size = 0;
1140  sample_size.sample_count =
1141  static_cast<uint32_t>(compact_sample_size.sizes.size());
1142  sample_size.sizes.swap(compact_sample_size.sizes);
1143  }
1144 
1145  // Either ChunkOffset or ChunkLargeOffset must present.
1146  if (reader->ChildExist(&chunk_large_offset)) {
1147  RCHECK(reader->ReadChild(&chunk_large_offset));
1148  } else {
1149  ChunkOffset chunk_offset;
1150  RCHECK(reader->ReadChild(&chunk_offset));
1151  chunk_large_offset.offsets.swap(chunk_offset.offsets);
1152  }
1153  } else {
1154  RCHECK(buffer->ReadWriteChild(&sample_size) &&
1155  buffer->ReadWriteChild(&chunk_large_offset));
1156  }
1157  RCHECK(buffer->TryReadWriteChild(&sync_sample));
1158  if (buffer->Reading()) {
1159  RCHECK(buffer->reader()->TryReadChildren(&sample_group_descriptions) &&
1160  buffer->reader()->TryReadChildren(&sample_to_groups));
1161  } else {
1162  for (auto& sample_group_description : sample_group_descriptions)
1163  RCHECK(buffer->ReadWriteChild(&sample_group_description));
1164  for (auto& sample_to_group : sample_to_groups)
1165  RCHECK(buffer->ReadWriteChild(&sample_to_group));
1166  }
1167  return true;
1168 }
1169 
1170 size_t SampleTable::ComputeSizeInternal() {
1171  size_t box_size = HeaderSize() + description.ComputeSize() +
1172  decoding_time_to_sample.ComputeSize() +
1173  composition_time_to_sample.ComputeSize() +
1174  sample_to_chunk.ComputeSize() + sample_size.ComputeSize() +
1175  chunk_large_offset.ComputeSize() +
1176  sync_sample.ComputeSize();
1177  for (auto& sample_group_description : sample_group_descriptions)
1178  box_size += sample_group_description.ComputeSize();
1179  for (auto& sample_to_group : sample_to_groups)
1180  box_size += sample_to_group.ComputeSize();
1181  return box_size;
1182 }
1183 
1184 EditList::EditList() {}
1185 EditList::~EditList() {}
1186 FourCC EditList::BoxType() const { return FOURCC_elst; }
1187 
1188 bool EditList::ReadWriteInternal(BoxBuffer* buffer) {
1189  uint32_t count = static_cast<uint32_t>(edits.size());
1190  RCHECK(ReadWriteHeaderInternal(buffer) && buffer->ReadWriteUInt32(&count));
1191  edits.resize(count);
1192 
1193  size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
1194  for (uint32_t i = 0; i < count; ++i) {
1195  RCHECK(
1196  buffer->ReadWriteUInt64NBytes(&edits[i].segment_duration, num_bytes) &&
1197  buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
1198  buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
1199  buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
1200  }
1201  return true;
1202 }
1203 
1204 size_t EditList::ComputeSizeInternal() {
1205  // EditList box is optional. Skip it if it is empty.
1206  if (edits.empty())
1207  return 0;
1208 
1209  version = 0;
1210  for (uint32_t i = 0; i < edits.size(); ++i) {
1211  if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
1212  version = 1;
1213  break;
1214  }
1215  }
1216  return HeaderSize() + sizeof(uint32_t) +
1217  (sizeof(uint32_t) * (1 + version) * 2 + sizeof(int16_t) * 2) *
1218  edits.size();
1219 }
1220 
1221 Edit::Edit() {}
1222 Edit::~Edit() {}
1223 FourCC Edit::BoxType() const { return FOURCC_edts; }
1224 
1225 bool Edit::ReadWriteInternal(BoxBuffer* buffer) {
1226  return ReadWriteHeaderInternal(buffer) &&
1227  buffer->PrepareChildren() &&
1228  buffer->ReadWriteChild(&list);
1229 }
1230 
1231 size_t Edit::ComputeSizeInternal() {
1232  // Edit box is optional. Skip it if it is empty.
1233  if (list.edits.empty())
1234  return 0;
1235  return HeaderSize() + list.ComputeSize();
1236 }
1237 
1238 HandlerReference::HandlerReference() : handler_type(FOURCC_NULL) {}
1239 HandlerReference::~HandlerReference() {}
1240 FourCC HandlerReference::BoxType() const { return FOURCC_hdlr; }
1241 
1242 bool HandlerReference::ReadWriteInternal(BoxBuffer* buffer) {
1243  std::vector<uint8_t> handler_name;
1244  if (!buffer->Reading()) {
1245  switch (handler_type) {
1246  case FOURCC_vide:
1247  handler_name.assign(kVideoHandlerName,
1248  kVideoHandlerName + arraysize(kVideoHandlerName));
1249  break;
1250  case FOURCC_soun:
1251  handler_name.assign(kAudioHandlerName,
1252  kAudioHandlerName + arraysize(kAudioHandlerName));
1253  break;
1254  case FOURCC_text:
1255  handler_name.assign(kTextHandlerName,
1256  kTextHandlerName + arraysize(kTextHandlerName));
1257  break;
1258  case FOURCC_ID32:
1259  break;
1260  default:
1261  NOTIMPLEMENTED();
1262  return false;
1263  }
1264  }
1265  RCHECK(ReadWriteHeaderInternal(buffer) &&
1266  buffer->IgnoreBytes(4) && // predefined.
1267  buffer->ReadWriteFourCC(&handler_type));
1268  if (!buffer->Reading()) {
1269  RCHECK(buffer->IgnoreBytes(12) && // reserved.
1270  buffer->ReadWriteVector(&handler_name, handler_name.size()));
1271  }
1272  return true;
1273 }
1274 
1275 size_t HandlerReference::ComputeSizeInternal() {
1276  size_t box_size = HeaderSize() + kFourCCSize + 16; // 16 bytes Reserved
1277  switch (handler_type) {
1278  case FOURCC_vide:
1279  box_size += sizeof(kVideoHandlerName);
1280  break;
1281  case FOURCC_soun:
1282  box_size += sizeof(kAudioHandlerName);
1283  break;
1284  case FOURCC_text:
1285  box_size += sizeof(kTextHandlerName);
1286  break;
1287  case FOURCC_ID32:
1288  break;
1289  default:
1290  NOTIMPLEMENTED();
1291  }
1292  return box_size;
1293 }
1294 
1295 bool Language::ReadWrite(BoxBuffer* buffer) {
1296  if (buffer->Reading()) {
1297  // Read language codes into temp first then use BitReader to read the
1298  // values. ISO-639-2/T language code: unsigned int(5)[3] language (2 bytes).
1299  std::vector<uint8_t> temp;
1300  RCHECK(buffer->ReadWriteVector(&temp, 2));
1301 
1302  BitReader bit_reader(&temp[0], 2);
1303  bit_reader.SkipBits(1);
1304  char language[3];
1305  for (int i = 0; i < 3; ++i) {
1306  CHECK(bit_reader.ReadBits(5, &language[i]));
1307  language[i] += 0x60;
1308  }
1309  code.assign(language, 3);
1310  } else {
1311  // Set up default language if it is not set.
1312  const char kUndefinedLanguage[] = "und";
1313  if (code.empty())
1314  code = kUndefinedLanguage;
1315  DCHECK_EQ(code.size(), 3u);
1316 
1317  // Lang format: bit(1) pad, unsigned int(5)[3] language.
1318  uint16_t lang = 0;
1319  for (int i = 0; i < 3; ++i)
1320  lang |= (code[i] - 0x60) << ((2 - i) * 5);
1321  RCHECK(buffer->ReadWriteUInt16(&lang));
1322  }
1323  return true;
1324 }
1325 
1326 uint32_t Language::ComputeSize() const {
1327  // ISO-639-2/T language code: unsigned int(5)[3] language (2 bytes).
1328  return 2;
1329 }
1330 
1331 bool PrivFrame::ReadWrite(BoxBuffer* buffer) {
1332  FourCC fourcc = FOURCC_PRIV;
1333  RCHECK(buffer->ReadWriteFourCC(&fourcc));
1334  if (fourcc != FOURCC_PRIV) {
1335  VLOG(1) << "Skip unrecognized id3 frame during read: "
1336  << FourCCToString(fourcc);
1337  return true;
1338  }
1339 
1340  uint32_t frame_size = static_cast<uint32_t>(owner.size() + 1 + value.size());
1341  // size should be encoded as synchsafe integer, which is not support here.
1342  // We don't expect frame_size to be larger than 0x7F. Synchsafe integers less
1343  // than 0x7F is encoded in the same way as normal integer.
1344  DCHECK_LT(frame_size, 0x7Fu);
1345  uint16_t flags = 0;
1346  RCHECK(buffer->ReadWriteUInt32(&frame_size) &&
1347  buffer->ReadWriteUInt16(&flags));
1348 
1349  if (buffer->Reading()) {
1350  std::string str;
1351  RCHECK(buffer->ReadWriteString(&str, frame_size));
1352  // |owner| is null terminated.
1353  size_t pos = str.find('\0');
1354  RCHECK(pos < str.size());
1355  owner = str.substr(0, pos);
1356  value = str.substr(pos + 1);
1357  } else {
1358  uint8_t byte = 0; // Null terminating byte between owner and value.
1359  RCHECK(buffer->ReadWriteString(&owner, owner.size()) &&
1360  buffer->ReadWriteUInt8(&byte) &&
1361  buffer->ReadWriteString(&value, value.size()));
1362  }
1363  return true;
1364 }
1365 
1366 uint32_t PrivFrame::ComputeSize() const {
1367  if (owner.empty() && value.empty())
1368  return 0;
1369  const uint32_t kFourCCSize = 4;
1370  return kFourCCSize +
1371  static_cast<uint32_t>(sizeof(uint32_t) + sizeof(uint16_t) +
1372  owner.size() + 1 + value.size());
1373 }
1374 
1375 ID3v2::ID3v2() {}
1376 ID3v2::~ID3v2() {}
1377 
1378 FourCC ID3v2::BoxType() const { return FOURCC_ID32; }
1379 
1380 bool ID3v2::ReadWriteInternal(BoxBuffer* buffer) {
1381  RCHECK(ReadWriteHeaderInternal(buffer) &&
1382  language.ReadWrite(buffer));
1383 
1384  // Read/Write ID3v2 header
1385  std::string id3v2_identifier = kID3v2Identifier;
1386  uint16_t version = kID3v2Version;
1387  // We only support PrivateFrame in ID3.
1388  uint32_t data_size = private_frame.ComputeSize();
1389  // size should be encoded as synchsafe integer, which is not support here.
1390  // We don't expect data_size to be larger than 0x7F. Synchsafe integers less
1391  // than 0x7F is encoded in the same way as normal integer.
1392  DCHECK_LT(data_size, 0x7Fu);
1393  uint8_t flags = 0;
1394  RCHECK(buffer->ReadWriteString(&id3v2_identifier, id3v2_identifier.size()) &&
1395  buffer->ReadWriteUInt16(&version) &&
1396  buffer->ReadWriteUInt8(&flags) &&
1397  buffer->ReadWriteUInt32(&data_size));
1398 
1399  RCHECK(private_frame.ReadWrite(buffer));
1400  return true;
1401 }
1402 
1403 size_t ID3v2::ComputeSizeInternal() {
1404  uint32_t private_frame_size = private_frame.ComputeSize();
1405  // Skip ID3v2 box generation if there is no private frame.
1406  return private_frame_size == 0 ? 0 : HeaderSize() + language.ComputeSize() +
1407  kID3v2HeaderSize +
1408  private_frame_size;
1409 }
1410 
1411 Metadata::Metadata() {}
1412 Metadata::~Metadata() {}
1413 
1414 FourCC Metadata::BoxType() const {
1415  return FOURCC_meta;
1416 }
1417 
1418 bool Metadata::ReadWriteInternal(BoxBuffer* buffer) {
1419  RCHECK(ReadWriteHeaderInternal(buffer) &&
1420  buffer->PrepareChildren() &&
1421  buffer->ReadWriteChild(&handler) &&
1422  buffer->TryReadWriteChild(&id3v2));
1423  return true;
1424 }
1425 
1426 size_t Metadata::ComputeSizeInternal() {
1427  size_t id3v2_size = id3v2.ComputeSize();
1428  // Skip metadata box generation if there is no metadata box.
1429  return id3v2_size == 0 ? 0
1430  : HeaderSize() + handler.ComputeSize() + id3v2_size;
1431 }
1432 
1433 CodecConfiguration::CodecConfiguration() : box_type(FOURCC_NULL) {}
1434 CodecConfiguration::~CodecConfiguration() {}
1435 
1437  // CodecConfiguration box should be parsed according to format recovered in
1438  // VideoSampleEntry. |box_type| is determined dynamically there.
1439  return box_type;
1440 }
1441 
1442 bool CodecConfiguration::ReadWriteInternal(BoxBuffer* buffer) {
1443  DCHECK_NE(box_type, FOURCC_NULL);
1444  RCHECK(ReadWriteHeaderInternal(buffer));
1445 
1446  // VPCodecConfiguration box inherits from FullBox instead of Box. The extra 4
1447  // bytes are handled here.
1448  if (box_type == FOURCC_vpcC) {
1449  uint32_t version_flags = 0;
1450  RCHECK(buffer->ReadWriteUInt32(&version_flags));
1451  RCHECK(version_flags == 0);
1452  }
1453 
1454  if (buffer->Reading()) {
1455  RCHECK(buffer->ReadWriteVector(&data, buffer->BytesLeft()));
1456  } else {
1457  RCHECK(buffer->ReadWriteVector(&data, data.size()));
1458  }
1459  return true;
1460 }
1461 
1462 size_t CodecConfiguration::ComputeSizeInternal() {
1463  if (data.empty())
1464  return 0;
1465  DCHECK_NE(box_type, FOURCC_NULL);
1466  return HeaderSize() + (box_type == FOURCC_vpcC ? 4 : 0) + data.size();
1467 }
1468 
1469 PixelAspectRatio::PixelAspectRatio() : h_spacing(0), v_spacing(0) {}
1470 PixelAspectRatio::~PixelAspectRatio() {}
1471 FourCC PixelAspectRatio::BoxType() const { return FOURCC_pasp; }
1472 
1473 bool PixelAspectRatio::ReadWriteInternal(BoxBuffer* buffer) {
1474  RCHECK(ReadWriteHeaderInternal(buffer) &&
1475  buffer->ReadWriteUInt32(&h_spacing) &&
1476  buffer->ReadWriteUInt32(&v_spacing));
1477  return true;
1478 }
1479 
1480 size_t PixelAspectRatio::ComputeSizeInternal() {
1481  // This box is optional. Skip it if it is not initialized.
1482  if (h_spacing == 0 && v_spacing == 0)
1483  return 0;
1484  // Both values must be positive.
1485  DCHECK(h_spacing != 0 && v_spacing != 0);
1486  return HeaderSize() + sizeof(h_spacing) + sizeof(v_spacing);
1487 }
1488 
1489 VideoSampleEntry::VideoSampleEntry()
1490  : format(FOURCC_NULL), data_reference_index(1), width(0), height(0) {}
1491 
1492 VideoSampleEntry::~VideoSampleEntry() {}
1494  if (format == FOURCC_NULL) {
1495  LOG(ERROR) << "VideoSampleEntry should be parsed according to the "
1496  << "handler type recovered in its Media ancestor.";
1497  }
1498  return format;
1499 }
1500 
1501 bool VideoSampleEntry::ReadWriteInternal(BoxBuffer* buffer) {
1502  std::vector<uint8_t> compressor_name;
1503  if (buffer->Reading()) {
1504  DCHECK(buffer->reader());
1505  format = buffer->reader()->type();
1506  } else {
1507  RCHECK(ReadWriteHeaderInternal(buffer));
1508 
1509  const FourCC actual_format = GetActualFormat();
1510  switch (actual_format) {
1511  case FOURCC_avc1:
1512  compressor_name.assign(
1513  kAvcCompressorName,
1514  kAvcCompressorName + arraysize(kAvcCompressorName));
1515  break;
1516  case FOURCC_hev1:
1517  case FOURCC_hvc1:
1518  compressor_name.assign(
1519  kHevcCompressorName,
1520  kHevcCompressorName + arraysize(kHevcCompressorName));
1521  break;
1522  case FOURCC_vp08:
1523  case FOURCC_vp09:
1524  case FOURCC_vp10:
1525  compressor_name.assign(
1526  kVpcCompressorName,
1527  kVpcCompressorName + arraysize(kVpcCompressorName));
1528  break;
1529  default:
1530  LOG(ERROR) << FourCCToString(actual_format) << " is not supported.";
1531  return false;
1532  }
1533  compressor_name.resize(kCompressorNameSize);
1534  }
1535 
1536  uint32_t video_resolution = kVideoResolution;
1537  uint16_t video_frame_count = kVideoFrameCount;
1538  uint16_t video_depth = kVideoDepth;
1539  int16_t predefined = -1;
1540  RCHECK(buffer->IgnoreBytes(6) && // reserved.
1541  buffer->ReadWriteUInt16(&data_reference_index) &&
1542  buffer->IgnoreBytes(16) && // predefined 0.
1543  buffer->ReadWriteUInt16(&width) &&
1544  buffer->ReadWriteUInt16(&height) &&
1545  buffer->ReadWriteUInt32(&video_resolution) &&
1546  buffer->ReadWriteUInt32(&video_resolution) &&
1547  buffer->IgnoreBytes(4) && // reserved.
1548  buffer->ReadWriteUInt16(&video_frame_count) &&
1549  buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1550  buffer->ReadWriteUInt16(&video_depth) &&
1551  buffer->ReadWriteInt16(&predefined));
1552 
1553  RCHECK(buffer->PrepareChildren());
1554 
1555  if (format == FOURCC_encv) {
1556  if (buffer->Reading()) {
1557  // Continue scanning until a supported protection scheme is found, or
1558  // until we run out of protection schemes.
1559  while (!IsProtectionSchemeSupported(sinf.type.type))
1560  RCHECK(buffer->ReadWriteChild(&sinf));
1561  } else {
1562  DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1563  RCHECK(buffer->ReadWriteChild(&sinf));
1564  }
1565  }
1566 
1567  const FourCC actual_format = GetActualFormat();
1568  if (buffer->Reading()) {
1569  codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1570  } else {
1571  DCHECK_EQ(codec_configuration.box_type,
1572  GetCodecConfigurationBoxType(actual_format));
1573  }
1574  DCHECK_NE(codec_configuration.box_type, FOURCC_NULL);
1575 
1576  RCHECK(buffer->ReadWriteChild(&codec_configuration));
1577  RCHECK(buffer->TryReadWriteChild(&pixel_aspect));
1578  return true;
1579 }
1580 
1581 size_t VideoSampleEntry::ComputeSizeInternal() {
1582  const FourCC actual_format = GetActualFormat();
1583  if (actual_format == FOURCC_NULL)
1584  return 0;
1585  codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1586  DCHECK_NE(codec_configuration.box_type, FOURCC_NULL);
1587  return HeaderSize() + sizeof(data_reference_index) + sizeof(width) +
1588  sizeof(height) + sizeof(kVideoResolution) * 2 +
1589  sizeof(kVideoFrameCount) + sizeof(kVideoDepth) +
1590  pixel_aspect.ComputeSize() + sinf.ComputeSize() +
1591  codec_configuration.ComputeSize() + kCompressorNameSize + 6 + 4 + 16 +
1592  2; // 6 + 4 bytes reserved, 16 + 2 bytes predefined.
1593 }
1594 
1595 FourCC VideoSampleEntry::GetCodecConfigurationBoxType(FourCC format) const {
1596  switch (format) {
1597  case FOURCC_avc1:
1598  return FOURCC_avcC;
1599  case FOURCC_hev1:
1600  case FOURCC_hvc1:
1601  return FOURCC_hvcC;
1602  case FOURCC_vp08:
1603  case FOURCC_vp09:
1604  case FOURCC_vp10:
1605  return FOURCC_vpcC;
1606  default:
1607  LOG(ERROR) << FourCCToString(format) << " is not supported.";
1608  return FOURCC_NULL;
1609  }
1610 }
1611 
1612 ElementaryStreamDescriptor::ElementaryStreamDescriptor() {}
1613 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
1614 FourCC ElementaryStreamDescriptor::BoxType() const { return FOURCC_esds; }
1615 
1616 bool ElementaryStreamDescriptor::ReadWriteInternal(BoxBuffer* buffer) {
1617  RCHECK(ReadWriteHeaderInternal(buffer));
1618  if (buffer->Reading()) {
1619  std::vector<uint8_t> data;
1620  RCHECK(buffer->ReadWriteVector(&data, buffer->BytesLeft()));
1621  RCHECK(es_descriptor.Parse(data));
1622  if (es_descriptor.IsAAC()) {
1623  RCHECK(aac_audio_specific_config.Parse(
1624  es_descriptor.decoder_specific_info()));
1625  }
1626  } else {
1627  DCHECK(buffer->writer());
1628  es_descriptor.Write(buffer->writer());
1629  }
1630  return true;
1631 }
1632 
1633 size_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1634  // This box is optional. Skip it if not initialized.
1635  if (es_descriptor.object_type() == kForbidden)
1636  return 0;
1637  return HeaderSize() + es_descriptor.ComputeSize();
1638 }
1639 
1640 DTSSpecific::DTSSpecific()
1641  : sampling_frequency(0),
1642  max_bitrate(0),
1643  avg_bitrate(0),
1644  pcm_sample_depth(0) {}
1645 DTSSpecific::~DTSSpecific() {}
1646 FourCC DTSSpecific::BoxType() const { return FOURCC_ddts; }
1647 
1648 bool DTSSpecific::ReadWriteInternal(BoxBuffer* buffer) {
1649  RCHECK(ReadWriteHeaderInternal(buffer) &&
1650  buffer->ReadWriteUInt32(&sampling_frequency) &&
1651  buffer->ReadWriteUInt32(&max_bitrate) &&
1652  buffer->ReadWriteUInt32(&avg_bitrate) &&
1653  buffer->ReadWriteUInt8(&pcm_sample_depth));
1654 
1655  if (buffer->Reading()) {
1656  RCHECK(buffer->ReadWriteVector(&extra_data, buffer->BytesLeft()));
1657  } else {
1658  if (extra_data.empty()) {
1659  extra_data.assign(kDdtsExtraData,
1660  kDdtsExtraData + sizeof(kDdtsExtraData));
1661  }
1662  RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1663  }
1664  return true;
1665 }
1666 
1667 size_t DTSSpecific::ComputeSizeInternal() {
1668  // This box is optional. Skip it if not initialized.
1669  if (sampling_frequency == 0)
1670  return 0;
1671  return HeaderSize() + sizeof(sampling_frequency) + sizeof(max_bitrate) +
1672  sizeof(avg_bitrate) + sizeof(pcm_sample_depth) +
1673  sizeof(kDdtsExtraData);
1674 }
1675 
1676 AC3Specific::AC3Specific() {}
1677 AC3Specific::~AC3Specific() {}
1678 
1679 FourCC AC3Specific::BoxType() const { return FOURCC_dac3; }
1680 
1681 bool AC3Specific::ReadWriteInternal(BoxBuffer* buffer) {
1682  RCHECK(ReadWriteHeaderInternal(buffer) &&
1683  buffer->ReadWriteVector(
1684  &data, buffer->Reading() ? buffer->BytesLeft() : data.size()));
1685  return true;
1686 }
1687 
1688 size_t AC3Specific::ComputeSizeInternal() {
1689  // This box is optional. Skip it if not initialized.
1690  if (data.empty())
1691  return 0;
1692  return HeaderSize() + data.size();
1693 }
1694 
1695 EC3Specific::EC3Specific() {}
1696 EC3Specific::~EC3Specific() {}
1697 
1698 FourCC EC3Specific::BoxType() const { return FOURCC_dec3; }
1699 
1700 bool EC3Specific::ReadWriteInternal(BoxBuffer* buffer) {
1701  RCHECK(ReadWriteHeaderInternal(buffer));
1702  size_t size = buffer->Reading() ? buffer->BytesLeft() : data.size();
1703  RCHECK(buffer->ReadWriteVector(&data, size));
1704  return true;
1705 }
1706 
1707 size_t EC3Specific::ComputeSizeInternal() {
1708  // This box is optional. Skip it if not initialized.
1709  if (data.empty())
1710  return 0;
1711  return HeaderSize() + data.size();
1712 }
1713 
1714 OpusSpecific::OpusSpecific() : preskip(0) {}
1715 OpusSpecific::~OpusSpecific() {}
1716 
1717 FourCC OpusSpecific::BoxType() const { return FOURCC_dOps; }
1718 
1719 bool OpusSpecific::ReadWriteInternal(BoxBuffer* buffer) {
1720  RCHECK(ReadWriteHeaderInternal(buffer));
1721  if (buffer->Reading()) {
1722  std::vector<uint8_t> data;
1723  const int kMinOpusSpecificBoxDataSize = 11;
1724  RCHECK(buffer->BytesLeft() >= kMinOpusSpecificBoxDataSize);
1725  RCHECK(buffer->ReadWriteVector(&data, buffer->BytesLeft()));
1726  preskip = data[2] + (data[3] << 8);
1727 
1728  // https://tools.ietf.org/html/draft-ietf-codec-oggopus-06#section-5
1729  BufferWriter writer;
1730  writer.AppendInt(FOURCC_Opus);
1731  writer.AppendInt(FOURCC_Head);
1732  // The version must always be 1.
1733  const uint8_t kOpusIdentificationHeaderVersion = 1;
1734  data[0] = kOpusIdentificationHeaderVersion;
1735  writer.AppendVector(data);
1736  writer.SwapBuffer(&opus_identification_header);
1737  } else {
1738  // https://tools.ietf.org/html/draft-ietf-codec-oggopus-06#section-5
1739  // The first 8 bytes is "magic signature".
1740  const size_t kOpusMagicSignatureSize = 8u;
1741  DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1742  // https://www.opus-codec.org/docs/opus_in_isobmff.html
1743  // The version field shall be set to 0.
1744  const uint8_t kOpusSpecificBoxVersion = 0;
1745  buffer->writer()->AppendInt(kOpusSpecificBoxVersion);
1746  buffer->writer()->AppendArray(
1747  &opus_identification_header[kOpusMagicSignatureSize + 1],
1748  opus_identification_header.size() - kOpusMagicSignatureSize - 1);
1749  }
1750  return true;
1751 }
1752 
1753 size_t OpusSpecific::ComputeSizeInternal() {
1754  // This box is optional. Skip it if not initialized.
1755  if (opus_identification_header.empty())
1756  return 0;
1757  // https://tools.ietf.org/html/draft-ietf-codec-oggopus-06#section-5
1758  // The first 8 bytes is "magic signature".
1759  const size_t kOpusMagicSignatureSize = 8u;
1760  DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1761  return HeaderSize() + opus_identification_header.size() -
1762  kOpusMagicSignatureSize;
1763 }
1764 
1765 AudioSampleEntry::AudioSampleEntry()
1766  : format(FOURCC_NULL),
1767  data_reference_index(1),
1768  channelcount(2),
1769  samplesize(16),
1770  samplerate(0) {}
1771 
1772 AudioSampleEntry::~AudioSampleEntry() {}
1773 
1775  if (format == FOURCC_NULL) {
1776  LOG(ERROR) << "AudioSampleEntry should be parsed according to the "
1777  << "handler type recovered in its Media ancestor.";
1778  }
1779  return format;
1780 }
1781 
1782 bool AudioSampleEntry::ReadWriteInternal(BoxBuffer* buffer) {
1783  if (buffer->Reading()) {
1784  DCHECK(buffer->reader());
1785  format = buffer->reader()->type();
1786  } else {
1787  RCHECK(ReadWriteHeaderInternal(buffer));
1788  }
1789 
1790  // Convert from integer to 16.16 fixed point for writing.
1791  samplerate <<= 16;
1792  RCHECK(buffer->IgnoreBytes(6) && // reserved.
1793  buffer->ReadWriteUInt16(&data_reference_index) &&
1794  buffer->IgnoreBytes(8) && // reserved.
1795  buffer->ReadWriteUInt16(&channelcount) &&
1796  buffer->ReadWriteUInt16(&samplesize) &&
1797  buffer->IgnoreBytes(4) && // predefined.
1798  buffer->ReadWriteUInt32(&samplerate));
1799  // Convert from 16.16 fixed point to integer.
1800  samplerate >>= 16;
1801 
1802  RCHECK(buffer->PrepareChildren());
1803  if (format == FOURCC_enca) {
1804  if (buffer->Reading()) {
1805  // Continue scanning until a supported protection scheme is found, or
1806  // until we run out of protection schemes.
1807  while (!IsProtectionSchemeSupported(sinf.type.type))
1808  RCHECK(buffer->ReadWriteChild(&sinf));
1809  } else {
1810  DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1811  RCHECK(buffer->ReadWriteChild(&sinf));
1812  }
1813  }
1814 
1815  RCHECK(buffer->TryReadWriteChild(&esds));
1816  RCHECK(buffer->TryReadWriteChild(&ddts));
1817  RCHECK(buffer->TryReadWriteChild(&dac3));
1818  RCHECK(buffer->TryReadWriteChild(&dec3));
1819  RCHECK(buffer->TryReadWriteChild(&dops));
1820  return true;
1821 }
1822 
1823 size_t AudioSampleEntry::ComputeSizeInternal() {
1824  if (GetActualFormat() == FOURCC_NULL)
1825  return 0;
1826  return HeaderSize() + sizeof(data_reference_index) + sizeof(channelcount) +
1827  sizeof(samplesize) + sizeof(samplerate) + sinf.ComputeSize() +
1828  esds.ComputeSize() + ddts.ComputeSize() + dac3.ComputeSize() +
1829  dec3.ComputeSize() + dops.ComputeSize() +
1830  6 + 8 + // 6 + 8 bytes reserved.
1831  4; // 4 bytes predefined.
1832 }
1833 
1834 WebVTTConfigurationBox::WebVTTConfigurationBox() {}
1835 WebVTTConfigurationBox::~WebVTTConfigurationBox() {}
1836 
1838  return FOURCC_vttC;
1839 }
1840 
1841 bool WebVTTConfigurationBox::ReadWriteInternal(BoxBuffer* buffer) {
1842  RCHECK(ReadWriteHeaderInternal(buffer));
1843  return buffer->ReadWriteString(
1844  &config,
1845  buffer->Reading() ? buffer->BytesLeft() : config.size());
1846 }
1847 
1848 size_t WebVTTConfigurationBox::ComputeSizeInternal() {
1849  return HeaderSize() + config.size();
1850 }
1851 
1852 WebVTTSourceLabelBox::WebVTTSourceLabelBox() {}
1853 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() {}
1854 
1856  return FOURCC_vlab;
1857 }
1858 
1859 bool WebVTTSourceLabelBox::ReadWriteInternal(BoxBuffer* buffer) {
1860  RCHECK(ReadWriteHeaderInternal(buffer));
1861  return buffer->ReadWriteString(&source_label, buffer->Reading()
1862  ? buffer->BytesLeft()
1863  : source_label.size());
1864 }
1865 
1866 size_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1867  if (source_label.empty())
1868  return 0;
1869  return HeaderSize() + source_label.size();
1870 }
1871 
1872 TextSampleEntry::TextSampleEntry() : format(FOURCC_NULL) {}
1873 TextSampleEntry::~TextSampleEntry() {}
1874 
1876  if (format == FOURCC_NULL) {
1877  LOG(ERROR) << "TextSampleEntry should be parsed according to the "
1878  << "handler type recovered in its Media ancestor.";
1879  }
1880  return format;
1881 }
1882 
1883 bool TextSampleEntry::ReadWriteInternal(BoxBuffer* buffer) {
1884  if (buffer->Reading()) {
1885  DCHECK(buffer->reader());
1886  format = buffer->reader()->type();
1887  } else {
1888  RCHECK(ReadWriteHeaderInternal(buffer));
1889  }
1890  RCHECK(buffer->IgnoreBytes(6) && // reserved for SampleEntry.
1891  buffer->ReadWriteUInt16(&data_reference_index));
1892 
1893  if (format == FOURCC_wvtt) {
1894  // TODO(rkuroiwa): Handle the optional MPEG4BitRateBox.
1895  RCHECK(buffer->PrepareChildren() &&
1896  buffer->ReadWriteChild(&config) &&
1897  buffer->ReadWriteChild(&label));
1898  }
1899  return true;
1900 }
1901 
1902 size_t TextSampleEntry::ComputeSizeInternal() {
1903  // 6 for the (anonymous) reserved bytes for SampleEntry class.
1904  return HeaderSize() + 6 + sizeof(data_reference_index) +
1905  config.ComputeSize() + label.ComputeSize();
1906 }
1907 
1908 MediaHeader::MediaHeader()
1909  : creation_time(0), modification_time(0), timescale(0), duration(0) {}
1910 MediaHeader::~MediaHeader() {}
1911 FourCC MediaHeader::BoxType() const { return FOURCC_mdhd; }
1912 
1913 bool MediaHeader::ReadWriteInternal(BoxBuffer* buffer) {
1914  RCHECK(ReadWriteHeaderInternal(buffer));
1915 
1916  uint8_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
1917  RCHECK(buffer->ReadWriteUInt64NBytes(&creation_time, num_bytes) &&
1918  buffer->ReadWriteUInt64NBytes(&modification_time, num_bytes) &&
1919  buffer->ReadWriteUInt32(&timescale) &&
1920  buffer->ReadWriteUInt64NBytes(&duration, num_bytes) &&
1921  language.ReadWrite(buffer) &&
1922  buffer->IgnoreBytes(2)); // predefined.
1923  return true;
1924 }
1925 
1926 size_t MediaHeader::ComputeSizeInternal() {
1927  version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1928  return HeaderSize() + sizeof(timescale) +
1929  sizeof(uint32_t) * (1 + version) * 3 + language.ComputeSize() +
1930  2; // 2 bytes predefined.
1931 }
1932 
1933 VideoMediaHeader::VideoMediaHeader()
1934  : graphicsmode(0), opcolor_red(0), opcolor_green(0), opcolor_blue(0) {
1935  const uint32_t kVideoMediaHeaderFlags = 1;
1936  flags = kVideoMediaHeaderFlags;
1937 }
1938 VideoMediaHeader::~VideoMediaHeader() {}
1939 FourCC VideoMediaHeader::BoxType() const { return FOURCC_vmhd; }
1940 bool VideoMediaHeader::ReadWriteInternal(BoxBuffer* buffer) {
1941  RCHECK(ReadWriteHeaderInternal(buffer) &&
1942  buffer->ReadWriteUInt16(&graphicsmode) &&
1943  buffer->ReadWriteUInt16(&opcolor_red) &&
1944  buffer->ReadWriteUInt16(&opcolor_green) &&
1945  buffer->ReadWriteUInt16(&opcolor_blue));
1946  return true;
1947 }
1948 
1949 size_t VideoMediaHeader::ComputeSizeInternal() {
1950  return HeaderSize() + sizeof(graphicsmode) + sizeof(opcolor_red) +
1951  sizeof(opcolor_green) + sizeof(opcolor_blue);
1952 }
1953 
1954 SoundMediaHeader::SoundMediaHeader() : balance(0) {}
1955 SoundMediaHeader::~SoundMediaHeader() {}
1956 FourCC SoundMediaHeader::BoxType() const { return FOURCC_smhd; }
1957 bool SoundMediaHeader::ReadWriteInternal(BoxBuffer* buffer) {
1958  RCHECK(ReadWriteHeaderInternal(buffer) &&
1959  buffer->ReadWriteUInt16(&balance) &&
1960  buffer->IgnoreBytes(2)); // reserved.
1961  return true;
1962 }
1963 
1964 size_t SoundMediaHeader::ComputeSizeInternal() {
1965  return HeaderSize() + sizeof(balance) + sizeof(uint16_t);
1966 }
1967 
1968 SubtitleMediaHeader::SubtitleMediaHeader() {}
1969 SubtitleMediaHeader::~SubtitleMediaHeader() {}
1970 
1971 FourCC SubtitleMediaHeader::BoxType() const { return FOURCC_sthd; }
1972 
1973 bool SubtitleMediaHeader::ReadWriteInternal(BoxBuffer* buffer) {
1974  return ReadWriteHeaderInternal(buffer);
1975 }
1976 
1977 size_t SubtitleMediaHeader::ComputeSizeInternal() {
1978  return HeaderSize();
1979 }
1980 
1981 DataEntryUrl::DataEntryUrl() {
1982  const uint32_t kDataEntryUrlFlags = 1;
1983  flags = kDataEntryUrlFlags;
1984 }
1985 DataEntryUrl::~DataEntryUrl() {}
1986 FourCC DataEntryUrl::BoxType() const { return FOURCC_url; }
1987 bool DataEntryUrl::ReadWriteInternal(BoxBuffer* buffer) {
1988  RCHECK(ReadWriteHeaderInternal(buffer));
1989  if (buffer->Reading()) {
1990  RCHECK(buffer->ReadWriteVector(&location, buffer->BytesLeft()));
1991  } else {
1992  RCHECK(buffer->ReadWriteVector(&location, location.size()));
1993  }
1994  return true;
1995 }
1996 
1997 size_t DataEntryUrl::ComputeSizeInternal() {
1998  return HeaderSize() + location.size();
1999 }
2000 
2001 DataReference::DataReference() {
2002  // Default 1 entry.
2003  data_entry.resize(1);
2004 }
2005 DataReference::~DataReference() {}
2006 FourCC DataReference::BoxType() const { return FOURCC_dref; }
2007 bool DataReference::ReadWriteInternal(BoxBuffer* buffer) {
2008  uint32_t entry_count = static_cast<uint32_t>(data_entry.size());
2009  RCHECK(ReadWriteHeaderInternal(buffer) &&
2010  buffer->ReadWriteUInt32(&entry_count));
2011  data_entry.resize(entry_count);
2012  RCHECK(buffer->PrepareChildren());
2013  for (uint32_t i = 0; i < entry_count; ++i)
2014  RCHECK(buffer->ReadWriteChild(&data_entry[i]));
2015  return true;
2016 }
2017 
2018 size_t DataReference::ComputeSizeInternal() {
2019  uint32_t count = static_cast<uint32_t>(data_entry.size());
2020  size_t box_size = HeaderSize() + sizeof(count);
2021  for (uint32_t i = 0; i < count; ++i)
2022  box_size += data_entry[i].ComputeSize();
2023  return box_size;
2024 }
2025 
2026 DataInformation::DataInformation() {}
2027 DataInformation::~DataInformation() {}
2028 FourCC DataInformation::BoxType() const { return FOURCC_dinf; }
2029 
2030 bool DataInformation::ReadWriteInternal(BoxBuffer* buffer) {
2031  return ReadWriteHeaderInternal(buffer) &&
2032  buffer->PrepareChildren() &&
2033  buffer->ReadWriteChild(&dref);
2034 }
2035 
2036 size_t DataInformation::ComputeSizeInternal() {
2037  return HeaderSize() + dref.ComputeSize();
2038 }
2039 
2040 MediaInformation::MediaInformation() {}
2041 MediaInformation::~MediaInformation() {}
2042 FourCC MediaInformation::BoxType() const { return FOURCC_minf; }
2043 
2044 bool MediaInformation::ReadWriteInternal(BoxBuffer* buffer) {
2045  RCHECK(ReadWriteHeaderInternal(buffer) &&
2046  buffer->PrepareChildren() &&
2047  buffer->ReadWriteChild(&dinf) &&
2048  buffer->ReadWriteChild(&sample_table));
2049  switch (sample_table.description.type) {
2050  case kVideo:
2051  RCHECK(buffer->ReadWriteChild(&vmhd));
2052  break;
2053  case kAudio:
2054  RCHECK(buffer->ReadWriteChild(&smhd));
2055  break;
2056  case kText:
2057  RCHECK(buffer->TryReadWriteChild(&sthd));
2058  break;
2059  default:
2060  NOTIMPLEMENTED();
2061  }
2062  // Hint is not supported for now.
2063  return true;
2064 }
2065 
2066 size_t MediaInformation::ComputeSizeInternal() {
2067  size_t box_size =
2068  HeaderSize() + dinf.ComputeSize() + sample_table.ComputeSize();
2069  switch (sample_table.description.type) {
2070  case kVideo:
2071  box_size += vmhd.ComputeSize();
2072  break;
2073  case kAudio:
2074  box_size += smhd.ComputeSize();
2075  break;
2076  case kText:
2077  box_size += sthd.ComputeSize();
2078  break;
2079  default:
2080  NOTIMPLEMENTED();
2081  }
2082  return box_size;
2083 }
2084 
2085 Media::Media() {}
2086 Media::~Media() {}
2087 FourCC Media::BoxType() const { return FOURCC_mdia; }
2088 
2089 bool Media::ReadWriteInternal(BoxBuffer* buffer) {
2090  RCHECK(ReadWriteHeaderInternal(buffer) &&
2091  buffer->PrepareChildren() &&
2092  buffer->ReadWriteChild(&header));
2093  if (buffer->Reading()) {
2094  RCHECK(buffer->ReadWriteChild(&handler));
2095  // Maddeningly, the HandlerReference box specifies how to parse the
2096  // SampleDescription box, making the latter the only box (of those that we
2097  // support) which cannot be parsed correctly on its own (or even with
2098  // information from its strict ancestor tree). We thus copy the handler type
2099  // to the sample description box *before* parsing it to provide this
2100  // information while parsing.
2101  information.sample_table.description.type =
2102  FourCCToTrackType(handler.handler_type);
2103  } else {
2104  handler.handler_type =
2105  TrackTypeToFourCC(information.sample_table.description.type);
2106  RCHECK(handler.handler_type != FOURCC_NULL);
2107  RCHECK(buffer->ReadWriteChild(&handler));
2108  }
2109  RCHECK(buffer->ReadWriteChild(&information));
2110  return true;
2111 }
2112 
2113 size_t Media::ComputeSizeInternal() {
2114  handler.handler_type =
2115  TrackTypeToFourCC(information.sample_table.description.type);
2116  return HeaderSize() + header.ComputeSize() + handler.ComputeSize() +
2117  information.ComputeSize();
2118 }
2119 
2120 Track::Track() {}
2121 Track::~Track() {}
2122 FourCC Track::BoxType() const { return FOURCC_trak; }
2123 
2124 bool Track::ReadWriteInternal(BoxBuffer* buffer) {
2125  RCHECK(ReadWriteHeaderInternal(buffer) &&
2126  buffer->PrepareChildren() &&
2127  buffer->ReadWriteChild(&header) &&
2128  buffer->ReadWriteChild(&media) &&
2129  buffer->TryReadWriteChild(&edit) &&
2130  buffer->TryReadWriteChild(&sample_encryption));
2131  return true;
2132 }
2133 
2134 size_t Track::ComputeSizeInternal() {
2135  return HeaderSize() + header.ComputeSize() + media.ComputeSize() +
2136  edit.ComputeSize();
2137 }
2138 
2139 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
2140 MovieExtendsHeader::~MovieExtendsHeader() {}
2141 FourCC MovieExtendsHeader::BoxType() const { return FOURCC_mehd; }
2142 
2143 bool MovieExtendsHeader::ReadWriteInternal(BoxBuffer* buffer) {
2144  RCHECK(ReadWriteHeaderInternal(buffer));
2145  size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
2146  RCHECK(buffer->ReadWriteUInt64NBytes(&fragment_duration, num_bytes));
2147  return true;
2148 }
2149 
2150 size_t MovieExtendsHeader::ComputeSizeInternal() {
2151  // This box is optional. Skip it if it is not used.
2152  if (fragment_duration == 0)
2153  return 0;
2154  version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
2155  return HeaderSize() + sizeof(uint32_t) * (1 + version);
2156 }
2157 
2158 TrackExtends::TrackExtends()
2159  : track_id(0),
2160  default_sample_description_index(0),
2161  default_sample_duration(0),
2162  default_sample_size(0),
2163  default_sample_flags(0) {}
2164 TrackExtends::~TrackExtends() {}
2165 FourCC TrackExtends::BoxType() const { return FOURCC_trex; }
2166 
2167 bool TrackExtends::ReadWriteInternal(BoxBuffer* buffer) {
2168  RCHECK(ReadWriteHeaderInternal(buffer) &&
2169  buffer->ReadWriteUInt32(&track_id) &&
2170  buffer->ReadWriteUInt32(&default_sample_description_index) &&
2171  buffer->ReadWriteUInt32(&default_sample_duration) &&
2172  buffer->ReadWriteUInt32(&default_sample_size) &&
2173  buffer->ReadWriteUInt32(&default_sample_flags));
2174  return true;
2175 }
2176 
2177 size_t TrackExtends::ComputeSizeInternal() {
2178  return HeaderSize() + sizeof(track_id) +
2179  sizeof(default_sample_description_index) +
2180  sizeof(default_sample_duration) + sizeof(default_sample_size) +
2181  sizeof(default_sample_flags);
2182 }
2183 
2184 MovieExtends::MovieExtends() {}
2185 MovieExtends::~MovieExtends() {}
2186 FourCC MovieExtends::BoxType() const { return FOURCC_mvex; }
2187 
2188 bool MovieExtends::ReadWriteInternal(BoxBuffer* buffer) {
2189  RCHECK(ReadWriteHeaderInternal(buffer) &&
2190  buffer->PrepareChildren() &&
2191  buffer->TryReadWriteChild(&header));
2192  if (buffer->Reading()) {
2193  DCHECK(buffer->reader());
2194  RCHECK(buffer->reader()->ReadChildren(&tracks));
2195  } else {
2196  for (uint32_t i = 0; i < tracks.size(); ++i)
2197  RCHECK(buffer->ReadWriteChild(&tracks[i]));
2198  }
2199  return true;
2200 }
2201 
2202 size_t MovieExtends::ComputeSizeInternal() {
2203  // This box is optional. Skip it if it does not contain any track.
2204  if (tracks.size() == 0)
2205  return 0;
2206  size_t box_size = HeaderSize() + header.ComputeSize();
2207  for (uint32_t i = 0; i < tracks.size(); ++i)
2208  box_size += tracks[i].ComputeSize();
2209  return box_size;
2210 }
2211 
2212 Movie::Movie() {}
2213 Movie::~Movie() {}
2214 FourCC Movie::BoxType() const { return FOURCC_moov; }
2215 
2216 bool Movie::ReadWriteInternal(BoxBuffer* buffer) {
2217  RCHECK(ReadWriteHeaderInternal(buffer) &&
2218  buffer->PrepareChildren() &&
2219  buffer->ReadWriteChild(&header) &&
2220  buffer->TryReadWriteChild(&metadata) &&
2221  buffer->TryReadWriteChild(&extends));
2222  if (buffer->Reading()) {
2223  BoxReader* reader = buffer->reader();
2224  DCHECK(reader);
2225  RCHECK(reader->ReadChildren(&tracks) &&
2226  reader->TryReadChildren(&pssh));
2227  } else {
2228  for (uint32_t i = 0; i < tracks.size(); ++i)
2229  RCHECK(buffer->ReadWriteChild(&tracks[i]));
2230  for (uint32_t i = 0; i < pssh.size(); ++i)
2231  RCHECK(buffer->ReadWriteChild(&pssh[i]));
2232  }
2233  return true;
2234 }
2235 
2236 size_t Movie::ComputeSizeInternal() {
2237  size_t box_size = HeaderSize() + header.ComputeSize() +
2238  metadata.ComputeSize() + extends.ComputeSize();
2239  for (uint32_t i = 0; i < tracks.size(); ++i)
2240  box_size += tracks[i].ComputeSize();
2241  for (uint32_t i = 0; i < pssh.size(); ++i)
2242  box_size += pssh[i].ComputeSize();
2243  return box_size;
2244 }
2245 
2246 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
2247 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
2248 FourCC TrackFragmentDecodeTime::BoxType() const { return FOURCC_tfdt; }
2249 
2250 bool TrackFragmentDecodeTime::ReadWriteInternal(BoxBuffer* buffer) {
2251  RCHECK(ReadWriteHeaderInternal(buffer));
2252  size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
2253  RCHECK(buffer->ReadWriteUInt64NBytes(&decode_time, num_bytes));
2254  return true;
2255 }
2256 
2257 size_t TrackFragmentDecodeTime::ComputeSizeInternal() {
2258  version = IsFitIn32Bits(decode_time) ? 0 : 1;
2259  return HeaderSize() + sizeof(uint32_t) * (1 + version);
2260 }
2261 
2262 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
2263 MovieFragmentHeader::~MovieFragmentHeader() {}
2264 FourCC MovieFragmentHeader::BoxType() const { return FOURCC_mfhd; }
2265 
2266 bool MovieFragmentHeader::ReadWriteInternal(BoxBuffer* buffer) {
2267  return ReadWriteHeaderInternal(buffer) &&
2268  buffer->ReadWriteUInt32(&sequence_number);
2269 }
2270 
2271 size_t MovieFragmentHeader::ComputeSizeInternal() {
2272  return HeaderSize() + sizeof(sequence_number);
2273 }
2274 
2275 TrackFragmentHeader::TrackFragmentHeader()
2276  : track_id(0),
2277  sample_description_index(0),
2278  default_sample_duration(0),
2279  default_sample_size(0),
2280  default_sample_flags(0) {}
2281 
2282 TrackFragmentHeader::~TrackFragmentHeader() {}
2283 FourCC TrackFragmentHeader::BoxType() const { return FOURCC_tfhd; }
2284 
2285 bool TrackFragmentHeader::ReadWriteInternal(BoxBuffer* buffer) {
2286  RCHECK(ReadWriteHeaderInternal(buffer) &&
2287  buffer->ReadWriteUInt32(&track_id));
2288 
2289  if (flags & kBaseDataOffsetPresentMask) {
2290  // MSE requires 'default-base-is-moof' to be set and
2291  // 'base-data-offset-present' not to be set. We omit these checks as some
2292  // valid files in the wild don't follow these rules, though they use moof as
2293  // base.
2294  uint64_t base_data_offset;
2295  RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
2296  DLOG(WARNING) << "base-data-offset-present is not expected. Assumes "
2297  "default-base-is-moof.";
2298  }
2299 
2300  if (flags & kSampleDescriptionIndexPresentMask) {
2301  RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
2302  } else if (buffer->Reading()) {
2303  sample_description_index = 0;
2304  }
2305 
2306  if (flags & kDefaultSampleDurationPresentMask) {
2307  RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
2308  } else if (buffer->Reading()) {
2309  default_sample_duration = 0;
2310  }
2311 
2312  if (flags & kDefaultSampleSizePresentMask) {
2313  RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
2314  } else if (buffer->Reading()) {
2315  default_sample_size = 0;
2316  }
2317 
2318  if (flags & kDefaultSampleFlagsPresentMask)
2319  RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
2320  return true;
2321 }
2322 
2323 size_t TrackFragmentHeader::ComputeSizeInternal() {
2324  size_t box_size = HeaderSize() + sizeof(track_id);
2325  if (flags & kSampleDescriptionIndexPresentMask)
2326  box_size += sizeof(sample_description_index);
2327  if (flags & kDefaultSampleDurationPresentMask)
2328  box_size += sizeof(default_sample_duration);
2329  if (flags & kDefaultSampleSizePresentMask)
2330  box_size += sizeof(default_sample_size);
2331  if (flags & kDefaultSampleFlagsPresentMask)
2332  box_size += sizeof(default_sample_flags);
2333  return box_size;
2334 }
2335 
2336 TrackFragmentRun::TrackFragmentRun() : sample_count(0), data_offset(0) {}
2337 TrackFragmentRun::~TrackFragmentRun() {}
2338 FourCC TrackFragmentRun::BoxType() const { return FOURCC_trun; }
2339 
2340 bool TrackFragmentRun::ReadWriteInternal(BoxBuffer* buffer) {
2341  if (!buffer->Reading()) {
2342  // Determine whether version 0 or version 1 should be used.
2343  // Use version 0 if possible, use version 1 if there is a negative
2344  // sample_offset value.
2345  version = 0;
2346  if (flags & kSampleCompTimeOffsetsPresentMask) {
2347  for (uint32_t i = 0; i < sample_count; ++i) {
2348  if (sample_composition_time_offsets[i] < 0) {
2349  version = 1;
2350  break;
2351  }
2352  }
2353  }
2354  }
2355 
2356  RCHECK(ReadWriteHeaderInternal(buffer) &&
2357  buffer->ReadWriteUInt32(&sample_count));
2358 
2359  bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
2360  bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
2361  bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
2362  bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
2363  bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
2364  bool sample_composition_time_offsets_present =
2365  (flags & kSampleCompTimeOffsetsPresentMask) != 0;
2366 
2367  if (data_offset_present) {
2368  RCHECK(buffer->ReadWriteUInt32(&data_offset));
2369  } else {
2370  // NOTE: If the data-offset is not present, then the data for this run
2371  // starts immediately after the data of the previous run, or at the
2372  // base-data-offset defined by the track fragment header if this is the
2373  // first run in a track fragment. If the data-offset is present, it is
2374  // relative to the base-data-offset established in the track fragment
2375  // header.
2376  NOTIMPLEMENTED();
2377  }
2378 
2379  uint32_t first_sample_flags(0);
2380 
2381  if (buffer->Reading()) {
2382  if (first_sample_flags_present)
2383  RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2384 
2385  if (sample_duration_present)
2386  sample_durations.resize(sample_count);
2387  if (sample_size_present)
2388  sample_sizes.resize(sample_count);
2389  if (sample_flags_present)
2390  sample_flags.resize(sample_count);
2391  if (sample_composition_time_offsets_present)
2392  sample_composition_time_offsets.resize(sample_count);
2393  } else {
2394  if (first_sample_flags_present) {
2395  first_sample_flags = sample_flags[0];
2396  DCHECK(sample_flags.size() == 1);
2397  RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2398  }
2399 
2400  if (sample_duration_present)
2401  DCHECK(sample_durations.size() == sample_count);
2402  if (sample_size_present)
2403  DCHECK(sample_sizes.size() == sample_count);
2404  if (sample_flags_present)
2405  DCHECK(sample_flags.size() == sample_count);
2406  if (sample_composition_time_offsets_present)
2407  DCHECK(sample_composition_time_offsets.size() == sample_count);
2408  }
2409 
2410  for (uint32_t i = 0; i < sample_count; ++i) {
2411  if (sample_duration_present)
2412  RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
2413  if (sample_size_present)
2414  RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
2415  if (sample_flags_present)
2416  RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
2417 
2418  if (sample_composition_time_offsets_present) {
2419  if (version == 0) {
2420  uint32_t sample_offset = sample_composition_time_offsets[i];
2421  RCHECK(buffer->ReadWriteUInt32(&sample_offset));
2422  sample_composition_time_offsets[i] = sample_offset;
2423  } else {
2424  int32_t sample_offset = sample_composition_time_offsets[i];
2425  RCHECK(buffer->ReadWriteInt32(&sample_offset));
2426  sample_composition_time_offsets[i] = sample_offset;
2427  }
2428  }
2429  }
2430 
2431  if (buffer->Reading()) {
2432  if (first_sample_flags_present) {
2433  if (sample_flags.size() == 0) {
2434  sample_flags.push_back(first_sample_flags);
2435  } else {
2436  sample_flags[0] = first_sample_flags;
2437  }
2438  }
2439  }
2440  return true;
2441 }
2442 
2443 size_t TrackFragmentRun::ComputeSizeInternal() {
2444  size_t box_size = HeaderSize() + sizeof(sample_count);
2445  if (flags & kDataOffsetPresentMask)
2446  box_size += sizeof(data_offset);
2447  if (flags & kFirstSampleFlagsPresentMask)
2448  box_size += sizeof(uint32_t);
2449  uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
2450  (flags & kSampleSizePresentMask ? 1 : 0) +
2451  (flags & kSampleFlagsPresentMask ? 1 : 0) +
2452  (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
2453  box_size += fields * sizeof(uint32_t) * sample_count;
2454  return box_size;
2455 }
2456 
2457 TrackFragment::TrackFragment() : decode_time_absent(false) {}
2458 TrackFragment::~TrackFragment() {}
2459 FourCC TrackFragment::BoxType() const { return FOURCC_traf; }
2460 
2461 bool TrackFragment::ReadWriteInternal(BoxBuffer* buffer) {
2462  RCHECK(ReadWriteHeaderInternal(buffer) &&
2463  buffer->PrepareChildren() &&
2464  buffer->ReadWriteChild(&header));
2465  if (buffer->Reading()) {
2466  DCHECK(buffer->reader());
2467  decode_time_absent = !buffer->reader()->ChildExist(&decode_time);
2468  if (!decode_time_absent)
2469  RCHECK(buffer->ReadWriteChild(&decode_time));
2470  RCHECK(buffer->reader()->TryReadChildren(&runs) &&
2471  buffer->reader()->TryReadChildren(&sample_group_descriptions) &&
2472  buffer->reader()->TryReadChildren(&sample_to_groups));
2473  } else {
2474  if (!decode_time_absent)
2475  RCHECK(buffer->ReadWriteChild(&decode_time));
2476  for (uint32_t i = 0; i < runs.size(); ++i)
2477  RCHECK(buffer->ReadWriteChild(&runs[i]));
2478  for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2479  RCHECK(buffer->ReadWriteChild(&sample_to_groups[i]));
2480  for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2481  RCHECK(buffer->ReadWriteChild(&sample_group_descriptions[i]));
2482  }
2483  return buffer->TryReadWriteChild(&auxiliary_size) &&
2484  buffer->TryReadWriteChild(&auxiliary_offset) &&
2485  buffer->TryReadWriteChild(&sample_encryption);
2486 }
2487 
2488 size_t TrackFragment::ComputeSizeInternal() {
2489  size_t box_size = HeaderSize() + header.ComputeSize() +
2490  decode_time.ComputeSize() + auxiliary_size.ComputeSize() +
2491  auxiliary_offset.ComputeSize() +
2492  sample_encryption.ComputeSize();
2493  for (uint32_t i = 0; i < runs.size(); ++i)
2494  box_size += runs[i].ComputeSize();
2495  for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2496  box_size += sample_group_descriptions[i].ComputeSize();
2497  for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2498  box_size += sample_to_groups[i].ComputeSize();
2499  return box_size;
2500 }
2501 
2502 MovieFragment::MovieFragment() {}
2503 MovieFragment::~MovieFragment() {}
2504 FourCC MovieFragment::BoxType() const { return FOURCC_moof; }
2505 
2506 bool MovieFragment::ReadWriteInternal(BoxBuffer* buffer) {
2507  RCHECK(ReadWriteHeaderInternal(buffer) &&
2508  buffer->PrepareChildren() &&
2509  buffer->ReadWriteChild(&header));
2510  if (buffer->Reading()) {
2511  BoxReader* reader = buffer->reader();
2512  DCHECK(reader);
2513  RCHECK(reader->ReadChildren(&tracks) &&
2514  reader->TryReadChildren(&pssh));
2515  } else {
2516  for (uint32_t i = 0; i < tracks.size(); ++i)
2517  RCHECK(buffer->ReadWriteChild(&tracks[i]));
2518  for (uint32_t i = 0; i < pssh.size(); ++i)
2519  RCHECK(buffer->ReadWriteChild(&pssh[i]));
2520  }
2521  return true;
2522 }
2523 
2524 size_t MovieFragment::ComputeSizeInternal() {
2525  size_t box_size = HeaderSize() + header.ComputeSize();
2526  for (uint32_t i = 0; i < tracks.size(); ++i)
2527  box_size += tracks[i].ComputeSize();
2528  for (uint32_t i = 0; i < pssh.size(); ++i)
2529  box_size += pssh[i].ComputeSize();
2530  return box_size;
2531 }
2532 
2533 SegmentIndex::SegmentIndex()
2534  : reference_id(0),
2535  timescale(0),
2536  earliest_presentation_time(0),
2537  first_offset(0) {}
2538 SegmentIndex::~SegmentIndex() {}
2539 FourCC SegmentIndex::BoxType() const { return FOURCC_sidx; }
2540 
2541 bool SegmentIndex::ReadWriteInternal(BoxBuffer* buffer) {
2542  RCHECK(ReadWriteHeaderInternal(buffer) &&
2543  buffer->ReadWriteUInt32(&reference_id) &&
2544  buffer->ReadWriteUInt32(&timescale));
2545 
2546  size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
2547  RCHECK(
2548  buffer->ReadWriteUInt64NBytes(&earliest_presentation_time, num_bytes) &&
2549  buffer->ReadWriteUInt64NBytes(&first_offset, num_bytes));
2550 
2551  uint16_t reference_count = static_cast<uint16_t>(references.size());
2552  RCHECK(buffer->IgnoreBytes(2) && // reserved.
2553  buffer->ReadWriteUInt16(&reference_count));
2554  references.resize(reference_count);
2555 
2556  uint32_t reference_type_size;
2557  uint32_t sap;
2558  for (uint32_t i = 0; i < reference_count; ++i) {
2559  if (!buffer->Reading()) {
2560  reference_type_size = references[i].referenced_size;
2561  if (references[i].reference_type)
2562  reference_type_size |= (1 << 31);
2563  sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2564  if (references[i].starts_with_sap)
2565  sap |= (1 << 31);
2566  }
2567  RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2568  buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2569  buffer->ReadWriteUInt32(&sap));
2570  if (buffer->Reading()) {
2571  references[i].reference_type = (reference_type_size >> 31) ? true : false;
2572  references[i].referenced_size = reference_type_size & ~(1 << 31);
2573  references[i].starts_with_sap = (sap >> 31) ? true : false;
2574  references[i].sap_type =
2575  static_cast<SegmentReference::SAPType>((sap >> 28) & 0x07);
2576  references[i].sap_delta_time = sap & ~(0xF << 28);
2577  }
2578  }
2579  return true;
2580 }
2581 
2582 size_t SegmentIndex::ComputeSizeInternal() {
2583  version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2584  return HeaderSize() + sizeof(reference_id) + sizeof(timescale) +
2585  sizeof(uint32_t) * (1 + version) * 2 + 2 * sizeof(uint16_t) +
2586  3 * sizeof(uint32_t) * references.size();
2587 }
2588 
2589 MediaData::MediaData() : data_size(0) {}
2590 MediaData::~MediaData() {}
2591 FourCC MediaData::BoxType() const { return FOURCC_mdat; }
2592 
2593 bool MediaData::ReadWriteInternal(BoxBuffer* buffer) {
2594  NOTIMPLEMENTED() << "Actual data is parsed and written separately.";
2595  return false;
2596 }
2597 
2598 size_t MediaData::ComputeSizeInternal() {
2599  return HeaderSize() + data_size;
2600 }
2601 
2602 CueSourceIDBox::CueSourceIDBox() : source_id(kCueSourceIdNotSet) {}
2603 CueSourceIDBox::~CueSourceIDBox() {}
2604 
2605 FourCC CueSourceIDBox::BoxType() const { return FOURCC_vsid; }
2606 
2607 bool CueSourceIDBox::ReadWriteInternal(BoxBuffer* buffer) {
2608  RCHECK(ReadWriteHeaderInternal(buffer) && buffer->ReadWriteInt32(&source_id));
2609  return true;
2610 }
2611 
2612 size_t CueSourceIDBox::ComputeSizeInternal() {
2613  if (source_id == kCueSourceIdNotSet)
2614  return 0;
2615  return HeaderSize() + sizeof(source_id);
2616 }
2617 
2618 CueTimeBox::CueTimeBox() {}
2619 CueTimeBox::~CueTimeBox() {}
2620 
2621 FourCC CueTimeBox::BoxType() const {
2622  return FOURCC_ctim;
2623 }
2624 
2625 bool CueTimeBox::ReadWriteInternal(BoxBuffer* buffer) {
2626  RCHECK(ReadWriteHeaderInternal(buffer));
2627  return buffer->ReadWriteString(
2628  &cue_current_time,
2629  buffer->Reading() ? buffer->BytesLeft() : cue_current_time.size());
2630 }
2631 
2632 size_t CueTimeBox::ComputeSizeInternal() {
2633  if (cue_current_time.empty())
2634  return 0;
2635  return HeaderSize() + cue_current_time.size();
2636 }
2637 
2638 CueIDBox::CueIDBox() {}
2639 CueIDBox::~CueIDBox() {}
2640 
2641 FourCC CueIDBox::BoxType() const {
2642  return FOURCC_iden;
2643 }
2644 
2645 bool CueIDBox::ReadWriteInternal(BoxBuffer* buffer) {
2646  RCHECK(ReadWriteHeaderInternal(buffer));
2647  return buffer->ReadWriteString(
2648  &cue_id, buffer->Reading() ? buffer->BytesLeft() : cue_id.size());
2649 }
2650 
2651 size_t CueIDBox::ComputeSizeInternal() {
2652  if (cue_id.empty())
2653  return 0;
2654  return HeaderSize() + cue_id.size();
2655 }
2656 
2657 CueSettingsBox::CueSettingsBox() {}
2658 CueSettingsBox::~CueSettingsBox() {}
2659 
2660 FourCC CueSettingsBox::BoxType() const {
2661  return FOURCC_sttg;
2662 }
2663 
2664 bool CueSettingsBox::ReadWriteInternal(BoxBuffer* buffer) {
2665  RCHECK(ReadWriteHeaderInternal(buffer));
2666  return buffer->ReadWriteString(
2667  &settings, buffer->Reading() ? buffer->BytesLeft() : settings.size());
2668 }
2669 
2670 size_t CueSettingsBox::ComputeSizeInternal() {
2671  if (settings.empty())
2672  return 0;
2673  return HeaderSize() + settings.size();
2674 }
2675 
2676 CuePayloadBox::CuePayloadBox() {}
2677 CuePayloadBox::~CuePayloadBox() {}
2678 
2679 FourCC CuePayloadBox::BoxType() const {
2680  return FOURCC_payl;
2681 }
2682 
2683 bool CuePayloadBox::ReadWriteInternal(BoxBuffer* buffer) {
2684  RCHECK(ReadWriteHeaderInternal(buffer));
2685  return buffer->ReadWriteString(
2686  &cue_text, buffer->Reading() ? buffer->BytesLeft() : cue_text.size());
2687 }
2688 
2689 size_t CuePayloadBox::ComputeSizeInternal() {
2690  return HeaderSize() + cue_text.size();
2691 }
2692 
2693 VTTEmptyCueBox::VTTEmptyCueBox() {}
2694 VTTEmptyCueBox::~VTTEmptyCueBox() {}
2695 
2696 FourCC VTTEmptyCueBox::BoxType() const {
2697  return FOURCC_vtte;
2698 }
2699 
2700 bool VTTEmptyCueBox::ReadWriteInternal(BoxBuffer* buffer) {
2701  return ReadWriteHeaderInternal(buffer);
2702 }
2703 
2704 size_t VTTEmptyCueBox::ComputeSizeInternal() {
2705  return HeaderSize();
2706 }
2707 
2708 VTTAdditionalTextBox::VTTAdditionalTextBox() {}
2709 VTTAdditionalTextBox::~VTTAdditionalTextBox() {}
2710 
2712  return FOURCC_vtta;
2713 }
2714 
2715 bool VTTAdditionalTextBox::ReadWriteInternal(BoxBuffer* buffer) {
2716  RCHECK(ReadWriteHeaderInternal(buffer));
2717  return buffer->ReadWriteString(
2718  &cue_additional_text,
2719  buffer->Reading() ? buffer->BytesLeft() : cue_additional_text.size());
2720 }
2721 
2722 size_t VTTAdditionalTextBox::ComputeSizeInternal() {
2723  return HeaderSize() + cue_additional_text.size();
2724 }
2725 
2726 VTTCueBox::VTTCueBox() {}
2727 VTTCueBox::~VTTCueBox() {}
2728 
2729 FourCC VTTCueBox::BoxType() const {
2730  return FOURCC_vttc;
2731 }
2732 
2733 bool VTTCueBox::ReadWriteInternal(BoxBuffer* buffer) {
2734  RCHECK(ReadWriteHeaderInternal(buffer) &&
2735  buffer->PrepareChildren() &&
2736  buffer->TryReadWriteChild(&cue_source_id) &&
2737  buffer->TryReadWriteChild(&cue_id) &&
2738  buffer->TryReadWriteChild(&cue_time) &&
2739  buffer->TryReadWriteChild(&cue_settings) &&
2740  buffer->ReadWriteChild(&cue_payload));
2741  return true;
2742 }
2743 
2744 size_t VTTCueBox::ComputeSizeInternal() {
2745  return HeaderSize() + cue_source_id.ComputeSize() + cue_id.ComputeSize() +
2746  cue_time.ComputeSize() + cue_settings.ComputeSize() +
2747  cue_payload.ComputeSize();
2748 }
2749 
2750 } // namespace mp4
2751 } // namespace media
2752 } // namespace shaka
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
bool ParseFromBuffer(uint8_t iv_size, bool has_subsamples, BufferReader *reader)
FourCC BoxType() const override
FourCC BoxType() const override
uint32_t HeaderSize() const final
Definition: box.cc:75
FourCC BoxType() const override
FourCC BoxType() const override
bool ReadWriteHeaderInternal(BoxBuffer *buffer) final
Definition: box.cc:80
FourCC BoxType() const override
bool TryReadChildren(std::vector< T > *children) WARN_UNUSED_RESULT
Definition: box_reader.h:134
bool TryReadWriteChild(Box *box)
Definition: box_buffer.h:177
FourCC BoxType() const override
FourCC BoxType() const override
size_t BytesLeft() const
Definition: box_buffer.h:62
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
virtual bool Parse(const std::vector< uint8_t > &data)
FourCC BoxType() const override
virtual bool ReadWriteHeaderInternal(BoxBuffer *buffer)
Definition: box.cc:61
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
uint32_t ComputeSize()
Definition: box.cc:50
bool ReadWrite(uint8_t iv_size, bool has_subsamples, BoxBuffer *buffer)
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
bool ReadChildren(std::vector< T > *children) WARN_UNUSED_RESULT
Definition: box_reader.h:128
std::vector< uint8_t > sample_encryption_data
FourCC BoxType() const override
bool ParseFromSampleEncryptionData(uint8_t iv_size, std::vector< SampleEncryptionEntry > *sample_encryption_entries) const
FourCC BoxType() const override
uint32_t box_size()
Definition: box.h:55
FourCC BoxType() const override
BufferWriter * writer()
Definition: box_buffer.h:200
FourCC BoxType() const override
FourCC BoxType() const override
virtual uint32_t HeaderSize() const
Definition: box.cc:55
bool IgnoreBytes(size_t num_bytes)
Definition: box_buffer.h:189
Class for reading MP4 boxes.
Definition: box_reader.h:25
bool ReadWriteString(std::string *str, size_t size)
Definition: box_buffer.h:139
FourCC BoxType() const override
bool ReadChild(Box *child) WARN_UNUSED_RESULT
Definition: box_reader.cc:88
FourCC BoxType() const override
bool ReadWriteUInt64NBytes(uint64_t *v, size_t num_bytes)
Definition: box_buffer.h:117
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
PrivFrame private_frame
We only support PrivateFrame in ID3. Other frames are ignored.
FourCC BoxType() const override
void Write(BufferWriter *writer)
Definition: box.cc:25
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
bool ChildExist(Box *child) WARN_UNUSED_RESULT
Definition: box_reader.cc:100
bool ReadWriteChild(Box *box)
Definition: box_buffer.h:166