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  case FOURCC_avc3:
1513  compressor_name.assign(
1514  kAvcCompressorName,
1515  kAvcCompressorName + arraysize(kAvcCompressorName));
1516  break;
1517  case FOURCC_hev1:
1518  case FOURCC_hvc1:
1519  compressor_name.assign(
1520  kHevcCompressorName,
1521  kHevcCompressorName + arraysize(kHevcCompressorName));
1522  break;
1523  case FOURCC_vp08:
1524  case FOURCC_vp09:
1525  case FOURCC_vp10:
1526  compressor_name.assign(
1527  kVpcCompressorName,
1528  kVpcCompressorName + arraysize(kVpcCompressorName));
1529  break;
1530  default:
1531  LOG(ERROR) << FourCCToString(actual_format) << " is not supported.";
1532  return false;
1533  }
1534  compressor_name.resize(kCompressorNameSize);
1535  }
1536 
1537  uint32_t video_resolution = kVideoResolution;
1538  uint16_t video_frame_count = kVideoFrameCount;
1539  uint16_t video_depth = kVideoDepth;
1540  int16_t predefined = -1;
1541  RCHECK(buffer->IgnoreBytes(6) && // reserved.
1542  buffer->ReadWriteUInt16(&data_reference_index) &&
1543  buffer->IgnoreBytes(16) && // predefined 0.
1544  buffer->ReadWriteUInt16(&width) &&
1545  buffer->ReadWriteUInt16(&height) &&
1546  buffer->ReadWriteUInt32(&video_resolution) &&
1547  buffer->ReadWriteUInt32(&video_resolution) &&
1548  buffer->IgnoreBytes(4) && // reserved.
1549  buffer->ReadWriteUInt16(&video_frame_count) &&
1550  buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1551  buffer->ReadWriteUInt16(&video_depth) &&
1552  buffer->ReadWriteInt16(&predefined));
1553 
1554  RCHECK(buffer->PrepareChildren());
1555 
1556  if (format == FOURCC_encv) {
1557  if (buffer->Reading()) {
1558  // Continue scanning until a supported protection scheme is found, or
1559  // until we run out of protection schemes.
1560  while (!IsProtectionSchemeSupported(sinf.type.type))
1561  RCHECK(buffer->ReadWriteChild(&sinf));
1562  } else {
1563  DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1564  RCHECK(buffer->ReadWriteChild(&sinf));
1565  }
1566  }
1567 
1568  const FourCC actual_format = GetActualFormat();
1569  if (buffer->Reading()) {
1570  codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1571  } else {
1572  DCHECK_EQ(codec_configuration.box_type,
1573  GetCodecConfigurationBoxType(actual_format));
1574  }
1575  DCHECK_NE(codec_configuration.box_type, FOURCC_NULL);
1576 
1577  RCHECK(buffer->ReadWriteChild(&codec_configuration));
1578  RCHECK(buffer->TryReadWriteChild(&pixel_aspect));
1579  return true;
1580 }
1581 
1582 size_t VideoSampleEntry::ComputeSizeInternal() {
1583  const FourCC actual_format = GetActualFormat();
1584  if (actual_format == FOURCC_NULL)
1585  return 0;
1586  codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1587  DCHECK_NE(codec_configuration.box_type, FOURCC_NULL);
1588  return HeaderSize() + sizeof(data_reference_index) + sizeof(width) +
1589  sizeof(height) + sizeof(kVideoResolution) * 2 +
1590  sizeof(kVideoFrameCount) + sizeof(kVideoDepth) +
1591  pixel_aspect.ComputeSize() + sinf.ComputeSize() +
1592  codec_configuration.ComputeSize() + kCompressorNameSize + 6 + 4 + 16 +
1593  2; // 6 + 4 bytes reserved, 16 + 2 bytes predefined.
1594 }
1595 
1596 FourCC VideoSampleEntry::GetCodecConfigurationBoxType(FourCC format) const {
1597  switch (format) {
1598  case FOURCC_avc1:
1599  case FOURCC_avc3:
1600  return FOURCC_avcC;
1601  case FOURCC_hev1:
1602  case FOURCC_hvc1:
1603  return FOURCC_hvcC;
1604  case FOURCC_vp08:
1605  case FOURCC_vp09:
1606  case FOURCC_vp10:
1607  return FOURCC_vpcC;
1608  default:
1609  LOG(ERROR) << FourCCToString(format) << " is not supported.";
1610  return FOURCC_NULL;
1611  }
1612 }
1613 
1614 ElementaryStreamDescriptor::ElementaryStreamDescriptor() {}
1615 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
1616 FourCC ElementaryStreamDescriptor::BoxType() const { return FOURCC_esds; }
1617 
1618 bool ElementaryStreamDescriptor::ReadWriteInternal(BoxBuffer* buffer) {
1619  RCHECK(ReadWriteHeaderInternal(buffer));
1620  if (buffer->Reading()) {
1621  std::vector<uint8_t> data;
1622  RCHECK(buffer->ReadWriteVector(&data, buffer->BytesLeft()));
1623  RCHECK(es_descriptor.Parse(data));
1624  if (es_descriptor.IsAAC()) {
1625  RCHECK(aac_audio_specific_config.Parse(
1626  es_descriptor.decoder_specific_info()));
1627  }
1628  } else {
1629  DCHECK(buffer->writer());
1630  es_descriptor.Write(buffer->writer());
1631  }
1632  return true;
1633 }
1634 
1635 size_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1636  // This box is optional. Skip it if not initialized.
1637  if (es_descriptor.object_type() == kForbidden)
1638  return 0;
1639  return HeaderSize() + es_descriptor.ComputeSize();
1640 }
1641 
1642 DTSSpecific::DTSSpecific()
1643  : sampling_frequency(0),
1644  max_bitrate(0),
1645  avg_bitrate(0),
1646  pcm_sample_depth(0) {}
1647 DTSSpecific::~DTSSpecific() {}
1648 FourCC DTSSpecific::BoxType() const { return FOURCC_ddts; }
1649 
1650 bool DTSSpecific::ReadWriteInternal(BoxBuffer* buffer) {
1651  RCHECK(ReadWriteHeaderInternal(buffer) &&
1652  buffer->ReadWriteUInt32(&sampling_frequency) &&
1653  buffer->ReadWriteUInt32(&max_bitrate) &&
1654  buffer->ReadWriteUInt32(&avg_bitrate) &&
1655  buffer->ReadWriteUInt8(&pcm_sample_depth));
1656 
1657  if (buffer->Reading()) {
1658  RCHECK(buffer->ReadWriteVector(&extra_data, buffer->BytesLeft()));
1659  } else {
1660  if (extra_data.empty()) {
1661  extra_data.assign(kDdtsExtraData,
1662  kDdtsExtraData + sizeof(kDdtsExtraData));
1663  }
1664  RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1665  }
1666  return true;
1667 }
1668 
1669 size_t DTSSpecific::ComputeSizeInternal() {
1670  // This box is optional. Skip it if not initialized.
1671  if (sampling_frequency == 0)
1672  return 0;
1673  return HeaderSize() + sizeof(sampling_frequency) + sizeof(max_bitrate) +
1674  sizeof(avg_bitrate) + sizeof(pcm_sample_depth) +
1675  sizeof(kDdtsExtraData);
1676 }
1677 
1678 AC3Specific::AC3Specific() {}
1679 AC3Specific::~AC3Specific() {}
1680 
1681 FourCC AC3Specific::BoxType() const { return FOURCC_dac3; }
1682 
1683 bool AC3Specific::ReadWriteInternal(BoxBuffer* buffer) {
1684  RCHECK(ReadWriteHeaderInternal(buffer) &&
1685  buffer->ReadWriteVector(
1686  &data, buffer->Reading() ? buffer->BytesLeft() : data.size()));
1687  return true;
1688 }
1689 
1690 size_t AC3Specific::ComputeSizeInternal() {
1691  // This box is optional. Skip it if not initialized.
1692  if (data.empty())
1693  return 0;
1694  return HeaderSize() + data.size();
1695 }
1696 
1697 EC3Specific::EC3Specific() {}
1698 EC3Specific::~EC3Specific() {}
1699 
1700 FourCC EC3Specific::BoxType() const { return FOURCC_dec3; }
1701 
1702 bool EC3Specific::ReadWriteInternal(BoxBuffer* buffer) {
1703  RCHECK(ReadWriteHeaderInternal(buffer));
1704  size_t size = buffer->Reading() ? buffer->BytesLeft() : data.size();
1705  RCHECK(buffer->ReadWriteVector(&data, size));
1706  return true;
1707 }
1708 
1709 size_t EC3Specific::ComputeSizeInternal() {
1710  // This box is optional. Skip it if not initialized.
1711  if (data.empty())
1712  return 0;
1713  return HeaderSize() + data.size();
1714 }
1715 
1716 OpusSpecific::OpusSpecific() : preskip(0) {}
1717 OpusSpecific::~OpusSpecific() {}
1718 
1719 FourCC OpusSpecific::BoxType() const { return FOURCC_dOps; }
1720 
1721 bool OpusSpecific::ReadWriteInternal(BoxBuffer* buffer) {
1722  RCHECK(ReadWriteHeaderInternal(buffer));
1723  if (buffer->Reading()) {
1724  std::vector<uint8_t> data;
1725  const int kMinOpusSpecificBoxDataSize = 11;
1726  RCHECK(buffer->BytesLeft() >= kMinOpusSpecificBoxDataSize);
1727  RCHECK(buffer->ReadWriteVector(&data, buffer->BytesLeft()));
1728  preskip = data[2] + (data[3] << 8);
1729 
1730  // https://tools.ietf.org/html/draft-ietf-codec-oggopus-06#section-5
1731  BufferWriter writer;
1732  writer.AppendInt(FOURCC_Opus);
1733  writer.AppendInt(FOURCC_Head);
1734  // The version must always be 1.
1735  const uint8_t kOpusIdentificationHeaderVersion = 1;
1736  data[0] = kOpusIdentificationHeaderVersion;
1737  writer.AppendVector(data);
1738  writer.SwapBuffer(&opus_identification_header);
1739  } else {
1740  // https://tools.ietf.org/html/draft-ietf-codec-oggopus-06#section-5
1741  // The first 8 bytes is "magic signature".
1742  const size_t kOpusMagicSignatureSize = 8u;
1743  DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1744  // https://www.opus-codec.org/docs/opus_in_isobmff.html
1745  // The version field shall be set to 0.
1746  const uint8_t kOpusSpecificBoxVersion = 0;
1747  buffer->writer()->AppendInt(kOpusSpecificBoxVersion);
1748  buffer->writer()->AppendArray(
1749  &opus_identification_header[kOpusMagicSignatureSize + 1],
1750  opus_identification_header.size() - kOpusMagicSignatureSize - 1);
1751  }
1752  return true;
1753 }
1754 
1755 size_t OpusSpecific::ComputeSizeInternal() {
1756  // This box is optional. Skip it if not initialized.
1757  if (opus_identification_header.empty())
1758  return 0;
1759  // https://tools.ietf.org/html/draft-ietf-codec-oggopus-06#section-5
1760  // The first 8 bytes is "magic signature".
1761  const size_t kOpusMagicSignatureSize = 8u;
1762  DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1763  return HeaderSize() + opus_identification_header.size() -
1764  kOpusMagicSignatureSize;
1765 }
1766 
1767 AudioSampleEntry::AudioSampleEntry()
1768  : format(FOURCC_NULL),
1769  data_reference_index(1),
1770  channelcount(2),
1771  samplesize(16),
1772  samplerate(0) {}
1773 
1774 AudioSampleEntry::~AudioSampleEntry() {}
1775 
1777  if (format == FOURCC_NULL) {
1778  LOG(ERROR) << "AudioSampleEntry should be parsed according to the "
1779  << "handler type recovered in its Media ancestor.";
1780  }
1781  return format;
1782 }
1783 
1784 bool AudioSampleEntry::ReadWriteInternal(BoxBuffer* buffer) {
1785  if (buffer->Reading()) {
1786  DCHECK(buffer->reader());
1787  format = buffer->reader()->type();
1788  } else {
1789  RCHECK(ReadWriteHeaderInternal(buffer));
1790  }
1791 
1792  // Convert from integer to 16.16 fixed point for writing.
1793  samplerate <<= 16;
1794  RCHECK(buffer->IgnoreBytes(6) && // reserved.
1795  buffer->ReadWriteUInt16(&data_reference_index) &&
1796  buffer->IgnoreBytes(8) && // reserved.
1797  buffer->ReadWriteUInt16(&channelcount) &&
1798  buffer->ReadWriteUInt16(&samplesize) &&
1799  buffer->IgnoreBytes(4) && // predefined.
1800  buffer->ReadWriteUInt32(&samplerate));
1801  // Convert from 16.16 fixed point to integer.
1802  samplerate >>= 16;
1803 
1804  RCHECK(buffer->PrepareChildren());
1805  if (format == FOURCC_enca) {
1806  if (buffer->Reading()) {
1807  // Continue scanning until a supported protection scheme is found, or
1808  // until we run out of protection schemes.
1809  while (!IsProtectionSchemeSupported(sinf.type.type))
1810  RCHECK(buffer->ReadWriteChild(&sinf));
1811  } else {
1812  DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1813  RCHECK(buffer->ReadWriteChild(&sinf));
1814  }
1815  }
1816 
1817  RCHECK(buffer->TryReadWriteChild(&esds));
1818  RCHECK(buffer->TryReadWriteChild(&ddts));
1819  RCHECK(buffer->TryReadWriteChild(&dac3));
1820  RCHECK(buffer->TryReadWriteChild(&dec3));
1821  RCHECK(buffer->TryReadWriteChild(&dops));
1822  return true;
1823 }
1824 
1825 size_t AudioSampleEntry::ComputeSizeInternal() {
1826  if (GetActualFormat() == FOURCC_NULL)
1827  return 0;
1828  return HeaderSize() + sizeof(data_reference_index) + sizeof(channelcount) +
1829  sizeof(samplesize) + sizeof(samplerate) + sinf.ComputeSize() +
1830  esds.ComputeSize() + ddts.ComputeSize() + dac3.ComputeSize() +
1831  dec3.ComputeSize() + dops.ComputeSize() +
1832  6 + 8 + // 6 + 8 bytes reserved.
1833  4; // 4 bytes predefined.
1834 }
1835 
1836 WebVTTConfigurationBox::WebVTTConfigurationBox() {}
1837 WebVTTConfigurationBox::~WebVTTConfigurationBox() {}
1838 
1840  return FOURCC_vttC;
1841 }
1842 
1843 bool WebVTTConfigurationBox::ReadWriteInternal(BoxBuffer* buffer) {
1844  RCHECK(ReadWriteHeaderInternal(buffer));
1845  return buffer->ReadWriteString(
1846  &config,
1847  buffer->Reading() ? buffer->BytesLeft() : config.size());
1848 }
1849 
1850 size_t WebVTTConfigurationBox::ComputeSizeInternal() {
1851  return HeaderSize() + config.size();
1852 }
1853 
1854 WebVTTSourceLabelBox::WebVTTSourceLabelBox() {}
1855 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() {}
1856 
1858  return FOURCC_vlab;
1859 }
1860 
1861 bool WebVTTSourceLabelBox::ReadWriteInternal(BoxBuffer* buffer) {
1862  RCHECK(ReadWriteHeaderInternal(buffer));
1863  return buffer->ReadWriteString(&source_label, buffer->Reading()
1864  ? buffer->BytesLeft()
1865  : source_label.size());
1866 }
1867 
1868 size_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1869  if (source_label.empty())
1870  return 0;
1871  return HeaderSize() + source_label.size();
1872 }
1873 
1874 TextSampleEntry::TextSampleEntry() : format(FOURCC_NULL) {}
1875 TextSampleEntry::~TextSampleEntry() {}
1876 
1878  if (format == FOURCC_NULL) {
1879  LOG(ERROR) << "TextSampleEntry should be parsed according to the "
1880  << "handler type recovered in its Media ancestor.";
1881  }
1882  return format;
1883 }
1884 
1885 bool TextSampleEntry::ReadWriteInternal(BoxBuffer* buffer) {
1886  if (buffer->Reading()) {
1887  DCHECK(buffer->reader());
1888  format = buffer->reader()->type();
1889  } else {
1890  RCHECK(ReadWriteHeaderInternal(buffer));
1891  }
1892  RCHECK(buffer->IgnoreBytes(6) && // reserved for SampleEntry.
1893  buffer->ReadWriteUInt16(&data_reference_index));
1894 
1895  if (format == FOURCC_wvtt) {
1896  // TODO(rkuroiwa): Handle the optional MPEG4BitRateBox.
1897  RCHECK(buffer->PrepareChildren() &&
1898  buffer->ReadWriteChild(&config) &&
1899  buffer->ReadWriteChild(&label));
1900  }
1901  return true;
1902 }
1903 
1904 size_t TextSampleEntry::ComputeSizeInternal() {
1905  // 6 for the (anonymous) reserved bytes for SampleEntry class.
1906  return HeaderSize() + 6 + sizeof(data_reference_index) +
1907  config.ComputeSize() + label.ComputeSize();
1908 }
1909 
1910 MediaHeader::MediaHeader()
1911  : creation_time(0), modification_time(0), timescale(0), duration(0) {}
1912 MediaHeader::~MediaHeader() {}
1913 FourCC MediaHeader::BoxType() const { return FOURCC_mdhd; }
1914 
1915 bool MediaHeader::ReadWriteInternal(BoxBuffer* buffer) {
1916  RCHECK(ReadWriteHeaderInternal(buffer));
1917 
1918  uint8_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
1919  RCHECK(buffer->ReadWriteUInt64NBytes(&creation_time, num_bytes) &&
1920  buffer->ReadWriteUInt64NBytes(&modification_time, num_bytes) &&
1921  buffer->ReadWriteUInt32(&timescale) &&
1922  buffer->ReadWriteUInt64NBytes(&duration, num_bytes) &&
1923  language.ReadWrite(buffer) &&
1924  buffer->IgnoreBytes(2)); // predefined.
1925  return true;
1926 }
1927 
1928 size_t MediaHeader::ComputeSizeInternal() {
1929  version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1930  return HeaderSize() + sizeof(timescale) +
1931  sizeof(uint32_t) * (1 + version) * 3 + language.ComputeSize() +
1932  2; // 2 bytes predefined.
1933 }
1934 
1935 VideoMediaHeader::VideoMediaHeader()
1936  : graphicsmode(0), opcolor_red(0), opcolor_green(0), opcolor_blue(0) {
1937  const uint32_t kVideoMediaHeaderFlags = 1;
1938  flags = kVideoMediaHeaderFlags;
1939 }
1940 VideoMediaHeader::~VideoMediaHeader() {}
1941 FourCC VideoMediaHeader::BoxType() const { return FOURCC_vmhd; }
1942 bool VideoMediaHeader::ReadWriteInternal(BoxBuffer* buffer) {
1943  RCHECK(ReadWriteHeaderInternal(buffer) &&
1944  buffer->ReadWriteUInt16(&graphicsmode) &&
1945  buffer->ReadWriteUInt16(&opcolor_red) &&
1946  buffer->ReadWriteUInt16(&opcolor_green) &&
1947  buffer->ReadWriteUInt16(&opcolor_blue));
1948  return true;
1949 }
1950 
1951 size_t VideoMediaHeader::ComputeSizeInternal() {
1952  return HeaderSize() + sizeof(graphicsmode) + sizeof(opcolor_red) +
1953  sizeof(opcolor_green) + sizeof(opcolor_blue);
1954 }
1955 
1956 SoundMediaHeader::SoundMediaHeader() : balance(0) {}
1957 SoundMediaHeader::~SoundMediaHeader() {}
1958 FourCC SoundMediaHeader::BoxType() const { return FOURCC_smhd; }
1959 bool SoundMediaHeader::ReadWriteInternal(BoxBuffer* buffer) {
1960  RCHECK(ReadWriteHeaderInternal(buffer) &&
1961  buffer->ReadWriteUInt16(&balance) &&
1962  buffer->IgnoreBytes(2)); // reserved.
1963  return true;
1964 }
1965 
1966 size_t SoundMediaHeader::ComputeSizeInternal() {
1967  return HeaderSize() + sizeof(balance) + sizeof(uint16_t);
1968 }
1969 
1970 SubtitleMediaHeader::SubtitleMediaHeader() {}
1971 SubtitleMediaHeader::~SubtitleMediaHeader() {}
1972 
1973 FourCC SubtitleMediaHeader::BoxType() const { return FOURCC_sthd; }
1974 
1975 bool SubtitleMediaHeader::ReadWriteInternal(BoxBuffer* buffer) {
1976  return ReadWriteHeaderInternal(buffer);
1977 }
1978 
1979 size_t SubtitleMediaHeader::ComputeSizeInternal() {
1980  return HeaderSize();
1981 }
1982 
1983 DataEntryUrl::DataEntryUrl() {
1984  const uint32_t kDataEntryUrlFlags = 1;
1985  flags = kDataEntryUrlFlags;
1986 }
1987 DataEntryUrl::~DataEntryUrl() {}
1988 FourCC DataEntryUrl::BoxType() const { return FOURCC_url; }
1989 bool DataEntryUrl::ReadWriteInternal(BoxBuffer* buffer) {
1990  RCHECK(ReadWriteHeaderInternal(buffer));
1991  if (buffer->Reading()) {
1992  RCHECK(buffer->ReadWriteVector(&location, buffer->BytesLeft()));
1993  } else {
1994  RCHECK(buffer->ReadWriteVector(&location, location.size()));
1995  }
1996  return true;
1997 }
1998 
1999 size_t DataEntryUrl::ComputeSizeInternal() {
2000  return HeaderSize() + location.size();
2001 }
2002 
2003 DataReference::DataReference() {
2004  // Default 1 entry.
2005  data_entry.resize(1);
2006 }
2007 DataReference::~DataReference() {}
2008 FourCC DataReference::BoxType() const { return FOURCC_dref; }
2009 bool DataReference::ReadWriteInternal(BoxBuffer* buffer) {
2010  uint32_t entry_count = static_cast<uint32_t>(data_entry.size());
2011  RCHECK(ReadWriteHeaderInternal(buffer) &&
2012  buffer->ReadWriteUInt32(&entry_count));
2013  data_entry.resize(entry_count);
2014  RCHECK(buffer->PrepareChildren());
2015  for (uint32_t i = 0; i < entry_count; ++i)
2016  RCHECK(buffer->ReadWriteChild(&data_entry[i]));
2017  return true;
2018 }
2019 
2020 size_t DataReference::ComputeSizeInternal() {
2021  uint32_t count = static_cast<uint32_t>(data_entry.size());
2022  size_t box_size = HeaderSize() + sizeof(count);
2023  for (uint32_t i = 0; i < count; ++i)
2024  box_size += data_entry[i].ComputeSize();
2025  return box_size;
2026 }
2027 
2028 DataInformation::DataInformation() {}
2029 DataInformation::~DataInformation() {}
2030 FourCC DataInformation::BoxType() const { return FOURCC_dinf; }
2031 
2032 bool DataInformation::ReadWriteInternal(BoxBuffer* buffer) {
2033  return ReadWriteHeaderInternal(buffer) &&
2034  buffer->PrepareChildren() &&
2035  buffer->ReadWriteChild(&dref);
2036 }
2037 
2038 size_t DataInformation::ComputeSizeInternal() {
2039  return HeaderSize() + dref.ComputeSize();
2040 }
2041 
2042 MediaInformation::MediaInformation() {}
2043 MediaInformation::~MediaInformation() {}
2044 FourCC MediaInformation::BoxType() const { return FOURCC_minf; }
2045 
2046 bool MediaInformation::ReadWriteInternal(BoxBuffer* buffer) {
2047  RCHECK(ReadWriteHeaderInternal(buffer) &&
2048  buffer->PrepareChildren() &&
2049  buffer->ReadWriteChild(&dinf) &&
2050  buffer->ReadWriteChild(&sample_table));
2051  switch (sample_table.description.type) {
2052  case kVideo:
2053  RCHECK(buffer->ReadWriteChild(&vmhd));
2054  break;
2055  case kAudio:
2056  RCHECK(buffer->ReadWriteChild(&smhd));
2057  break;
2058  case kText:
2059  RCHECK(buffer->TryReadWriteChild(&sthd));
2060  break;
2061  default:
2062  NOTIMPLEMENTED();
2063  }
2064  // Hint is not supported for now.
2065  return true;
2066 }
2067 
2068 size_t MediaInformation::ComputeSizeInternal() {
2069  size_t box_size =
2070  HeaderSize() + dinf.ComputeSize() + sample_table.ComputeSize();
2071  switch (sample_table.description.type) {
2072  case kVideo:
2073  box_size += vmhd.ComputeSize();
2074  break;
2075  case kAudio:
2076  box_size += smhd.ComputeSize();
2077  break;
2078  case kText:
2079  box_size += sthd.ComputeSize();
2080  break;
2081  default:
2082  NOTIMPLEMENTED();
2083  }
2084  return box_size;
2085 }
2086 
2087 Media::Media() {}
2088 Media::~Media() {}
2089 FourCC Media::BoxType() const { return FOURCC_mdia; }
2090 
2091 bool Media::ReadWriteInternal(BoxBuffer* buffer) {
2092  RCHECK(ReadWriteHeaderInternal(buffer) &&
2093  buffer->PrepareChildren() &&
2094  buffer->ReadWriteChild(&header));
2095  if (buffer->Reading()) {
2096  RCHECK(buffer->ReadWriteChild(&handler));
2097  // Maddeningly, the HandlerReference box specifies how to parse the
2098  // SampleDescription box, making the latter the only box (of those that we
2099  // support) which cannot be parsed correctly on its own (or even with
2100  // information from its strict ancestor tree). We thus copy the handler type
2101  // to the sample description box *before* parsing it to provide this
2102  // information while parsing.
2103  information.sample_table.description.type =
2104  FourCCToTrackType(handler.handler_type);
2105  } else {
2106  handler.handler_type =
2107  TrackTypeToFourCC(information.sample_table.description.type);
2108  RCHECK(handler.handler_type != FOURCC_NULL);
2109  RCHECK(buffer->ReadWriteChild(&handler));
2110  }
2111  RCHECK(buffer->ReadWriteChild(&information));
2112  return true;
2113 }
2114 
2115 size_t Media::ComputeSizeInternal() {
2116  handler.handler_type =
2117  TrackTypeToFourCC(information.sample_table.description.type);
2118  return HeaderSize() + header.ComputeSize() + handler.ComputeSize() +
2119  information.ComputeSize();
2120 }
2121 
2122 Track::Track() {}
2123 Track::~Track() {}
2124 FourCC Track::BoxType() const { return FOURCC_trak; }
2125 
2126 bool Track::ReadWriteInternal(BoxBuffer* buffer) {
2127  RCHECK(ReadWriteHeaderInternal(buffer) &&
2128  buffer->PrepareChildren() &&
2129  buffer->ReadWriteChild(&header) &&
2130  buffer->ReadWriteChild(&media) &&
2131  buffer->TryReadWriteChild(&edit) &&
2132  buffer->TryReadWriteChild(&sample_encryption));
2133  return true;
2134 }
2135 
2136 size_t Track::ComputeSizeInternal() {
2137  return HeaderSize() + header.ComputeSize() + media.ComputeSize() +
2138  edit.ComputeSize();
2139 }
2140 
2141 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
2142 MovieExtendsHeader::~MovieExtendsHeader() {}
2143 FourCC MovieExtendsHeader::BoxType() const { return FOURCC_mehd; }
2144 
2145 bool MovieExtendsHeader::ReadWriteInternal(BoxBuffer* buffer) {
2146  RCHECK(ReadWriteHeaderInternal(buffer));
2147  size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
2148  RCHECK(buffer->ReadWriteUInt64NBytes(&fragment_duration, num_bytes));
2149  return true;
2150 }
2151 
2152 size_t MovieExtendsHeader::ComputeSizeInternal() {
2153  // This box is optional. Skip it if it is not used.
2154  if (fragment_duration == 0)
2155  return 0;
2156  version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
2157  return HeaderSize() + sizeof(uint32_t) * (1 + version);
2158 }
2159 
2160 TrackExtends::TrackExtends()
2161  : track_id(0),
2162  default_sample_description_index(0),
2163  default_sample_duration(0),
2164  default_sample_size(0),
2165  default_sample_flags(0) {}
2166 TrackExtends::~TrackExtends() {}
2167 FourCC TrackExtends::BoxType() const { return FOURCC_trex; }
2168 
2169 bool TrackExtends::ReadWriteInternal(BoxBuffer* buffer) {
2170  RCHECK(ReadWriteHeaderInternal(buffer) &&
2171  buffer->ReadWriteUInt32(&track_id) &&
2172  buffer->ReadWriteUInt32(&default_sample_description_index) &&
2173  buffer->ReadWriteUInt32(&default_sample_duration) &&
2174  buffer->ReadWriteUInt32(&default_sample_size) &&
2175  buffer->ReadWriteUInt32(&default_sample_flags));
2176  return true;
2177 }
2178 
2179 size_t TrackExtends::ComputeSizeInternal() {
2180  return HeaderSize() + sizeof(track_id) +
2181  sizeof(default_sample_description_index) +
2182  sizeof(default_sample_duration) + sizeof(default_sample_size) +
2183  sizeof(default_sample_flags);
2184 }
2185 
2186 MovieExtends::MovieExtends() {}
2187 MovieExtends::~MovieExtends() {}
2188 FourCC MovieExtends::BoxType() const { return FOURCC_mvex; }
2189 
2190 bool MovieExtends::ReadWriteInternal(BoxBuffer* buffer) {
2191  RCHECK(ReadWriteHeaderInternal(buffer) &&
2192  buffer->PrepareChildren() &&
2193  buffer->TryReadWriteChild(&header));
2194  if (buffer->Reading()) {
2195  DCHECK(buffer->reader());
2196  RCHECK(buffer->reader()->ReadChildren(&tracks));
2197  } else {
2198  for (uint32_t i = 0; i < tracks.size(); ++i)
2199  RCHECK(buffer->ReadWriteChild(&tracks[i]));
2200  }
2201  return true;
2202 }
2203 
2204 size_t MovieExtends::ComputeSizeInternal() {
2205  // This box is optional. Skip it if it does not contain any track.
2206  if (tracks.size() == 0)
2207  return 0;
2208  size_t box_size = HeaderSize() + header.ComputeSize();
2209  for (uint32_t i = 0; i < tracks.size(); ++i)
2210  box_size += tracks[i].ComputeSize();
2211  return box_size;
2212 }
2213 
2214 Movie::Movie() {}
2215 Movie::~Movie() {}
2216 FourCC Movie::BoxType() const { return FOURCC_moov; }
2217 
2218 bool Movie::ReadWriteInternal(BoxBuffer* buffer) {
2219  RCHECK(ReadWriteHeaderInternal(buffer) &&
2220  buffer->PrepareChildren() &&
2221  buffer->ReadWriteChild(&header) &&
2222  buffer->TryReadWriteChild(&metadata) &&
2223  buffer->TryReadWriteChild(&extends));
2224  if (buffer->Reading()) {
2225  BoxReader* reader = buffer->reader();
2226  DCHECK(reader);
2227  RCHECK(reader->ReadChildren(&tracks) &&
2228  reader->TryReadChildren(&pssh));
2229  } else {
2230  for (uint32_t i = 0; i < tracks.size(); ++i)
2231  RCHECK(buffer->ReadWriteChild(&tracks[i]));
2232  for (uint32_t i = 0; i < pssh.size(); ++i)
2233  RCHECK(buffer->ReadWriteChild(&pssh[i]));
2234  }
2235  return true;
2236 }
2237 
2238 size_t Movie::ComputeSizeInternal() {
2239  size_t box_size = HeaderSize() + header.ComputeSize() +
2240  metadata.ComputeSize() + extends.ComputeSize();
2241  for (uint32_t i = 0; i < tracks.size(); ++i)
2242  box_size += tracks[i].ComputeSize();
2243  for (uint32_t i = 0; i < pssh.size(); ++i)
2244  box_size += pssh[i].ComputeSize();
2245  return box_size;
2246 }
2247 
2248 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
2249 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
2250 FourCC TrackFragmentDecodeTime::BoxType() const { return FOURCC_tfdt; }
2251 
2252 bool TrackFragmentDecodeTime::ReadWriteInternal(BoxBuffer* buffer) {
2253  RCHECK(ReadWriteHeaderInternal(buffer));
2254  size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
2255  RCHECK(buffer->ReadWriteUInt64NBytes(&decode_time, num_bytes));
2256  return true;
2257 }
2258 
2259 size_t TrackFragmentDecodeTime::ComputeSizeInternal() {
2260  version = IsFitIn32Bits(decode_time) ? 0 : 1;
2261  return HeaderSize() + sizeof(uint32_t) * (1 + version);
2262 }
2263 
2264 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
2265 MovieFragmentHeader::~MovieFragmentHeader() {}
2266 FourCC MovieFragmentHeader::BoxType() const { return FOURCC_mfhd; }
2267 
2268 bool MovieFragmentHeader::ReadWriteInternal(BoxBuffer* buffer) {
2269  return ReadWriteHeaderInternal(buffer) &&
2270  buffer->ReadWriteUInt32(&sequence_number);
2271 }
2272 
2273 size_t MovieFragmentHeader::ComputeSizeInternal() {
2274  return HeaderSize() + sizeof(sequence_number);
2275 }
2276 
2277 TrackFragmentHeader::TrackFragmentHeader()
2278  : track_id(0),
2279  sample_description_index(0),
2280  default_sample_duration(0),
2281  default_sample_size(0),
2282  default_sample_flags(0) {}
2283 
2284 TrackFragmentHeader::~TrackFragmentHeader() {}
2285 FourCC TrackFragmentHeader::BoxType() const { return FOURCC_tfhd; }
2286 
2287 bool TrackFragmentHeader::ReadWriteInternal(BoxBuffer* buffer) {
2288  RCHECK(ReadWriteHeaderInternal(buffer) &&
2289  buffer->ReadWriteUInt32(&track_id));
2290 
2291  if (flags & kBaseDataOffsetPresentMask) {
2292  // MSE requires 'default-base-is-moof' to be set and
2293  // 'base-data-offset-present' not to be set. We omit these checks as some
2294  // valid files in the wild don't follow these rules, though they use moof as
2295  // base.
2296  uint64_t base_data_offset;
2297  RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
2298  DLOG(WARNING) << "base-data-offset-present is not expected. Assumes "
2299  "default-base-is-moof.";
2300  }
2301 
2302  if (flags & kSampleDescriptionIndexPresentMask) {
2303  RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
2304  } else if (buffer->Reading()) {
2305  sample_description_index = 0;
2306  }
2307 
2308  if (flags & kDefaultSampleDurationPresentMask) {
2309  RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
2310  } else if (buffer->Reading()) {
2311  default_sample_duration = 0;
2312  }
2313 
2314  if (flags & kDefaultSampleSizePresentMask) {
2315  RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
2316  } else if (buffer->Reading()) {
2317  default_sample_size = 0;
2318  }
2319 
2320  if (flags & kDefaultSampleFlagsPresentMask)
2321  RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
2322  return true;
2323 }
2324 
2325 size_t TrackFragmentHeader::ComputeSizeInternal() {
2326  size_t box_size = HeaderSize() + sizeof(track_id);
2327  if (flags & kSampleDescriptionIndexPresentMask)
2328  box_size += sizeof(sample_description_index);
2329  if (flags & kDefaultSampleDurationPresentMask)
2330  box_size += sizeof(default_sample_duration);
2331  if (flags & kDefaultSampleSizePresentMask)
2332  box_size += sizeof(default_sample_size);
2333  if (flags & kDefaultSampleFlagsPresentMask)
2334  box_size += sizeof(default_sample_flags);
2335  return box_size;
2336 }
2337 
2338 TrackFragmentRun::TrackFragmentRun() : sample_count(0), data_offset(0) {}
2339 TrackFragmentRun::~TrackFragmentRun() {}
2340 FourCC TrackFragmentRun::BoxType() const { return FOURCC_trun; }
2341 
2342 bool TrackFragmentRun::ReadWriteInternal(BoxBuffer* buffer) {
2343  if (!buffer->Reading()) {
2344  // Determine whether version 0 or version 1 should be used.
2345  // Use version 0 if possible, use version 1 if there is a negative
2346  // sample_offset value.
2347  version = 0;
2348  if (flags & kSampleCompTimeOffsetsPresentMask) {
2349  for (uint32_t i = 0; i < sample_count; ++i) {
2350  if (sample_composition_time_offsets[i] < 0) {
2351  version = 1;
2352  break;
2353  }
2354  }
2355  }
2356  }
2357 
2358  RCHECK(ReadWriteHeaderInternal(buffer) &&
2359  buffer->ReadWriteUInt32(&sample_count));
2360 
2361  bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
2362  bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
2363  bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
2364  bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
2365  bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
2366  bool sample_composition_time_offsets_present =
2367  (flags & kSampleCompTimeOffsetsPresentMask) != 0;
2368 
2369  if (data_offset_present) {
2370  RCHECK(buffer->ReadWriteUInt32(&data_offset));
2371  } else {
2372  // NOTE: If the data-offset is not present, then the data for this run
2373  // starts immediately after the data of the previous run, or at the
2374  // base-data-offset defined by the track fragment header if this is the
2375  // first run in a track fragment. If the data-offset is present, it is
2376  // relative to the base-data-offset established in the track fragment
2377  // header.
2378  NOTIMPLEMENTED();
2379  }
2380 
2381  uint32_t first_sample_flags(0);
2382 
2383  if (buffer->Reading()) {
2384  if (first_sample_flags_present)
2385  RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2386 
2387  if (sample_duration_present)
2388  sample_durations.resize(sample_count);
2389  if (sample_size_present)
2390  sample_sizes.resize(sample_count);
2391  if (sample_flags_present)
2392  sample_flags.resize(sample_count);
2393  if (sample_composition_time_offsets_present)
2394  sample_composition_time_offsets.resize(sample_count);
2395  } else {
2396  if (first_sample_flags_present) {
2397  first_sample_flags = sample_flags[0];
2398  DCHECK(sample_flags.size() == 1);
2399  RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2400  }
2401 
2402  if (sample_duration_present)
2403  DCHECK(sample_durations.size() == sample_count);
2404  if (sample_size_present)
2405  DCHECK(sample_sizes.size() == sample_count);
2406  if (sample_flags_present)
2407  DCHECK(sample_flags.size() == sample_count);
2408  if (sample_composition_time_offsets_present)
2409  DCHECK(sample_composition_time_offsets.size() == sample_count);
2410  }
2411 
2412  for (uint32_t i = 0; i < sample_count; ++i) {
2413  if (sample_duration_present)
2414  RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
2415  if (sample_size_present)
2416  RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
2417  if (sample_flags_present)
2418  RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
2419 
2420  if (sample_composition_time_offsets_present) {
2421  if (version == 0) {
2422  uint32_t sample_offset = sample_composition_time_offsets[i];
2423  RCHECK(buffer->ReadWriteUInt32(&sample_offset));
2424  sample_composition_time_offsets[i] = sample_offset;
2425  } else {
2426  int32_t sample_offset = sample_composition_time_offsets[i];
2427  RCHECK(buffer->ReadWriteInt32(&sample_offset));
2428  sample_composition_time_offsets[i] = sample_offset;
2429  }
2430  }
2431  }
2432 
2433  if (buffer->Reading()) {
2434  if (first_sample_flags_present) {
2435  if (sample_flags.size() == 0) {
2436  sample_flags.push_back(first_sample_flags);
2437  } else {
2438  sample_flags[0] = first_sample_flags;
2439  }
2440  }
2441  }
2442  return true;
2443 }
2444 
2445 size_t TrackFragmentRun::ComputeSizeInternal() {
2446  size_t box_size = HeaderSize() + sizeof(sample_count);
2447  if (flags & kDataOffsetPresentMask)
2448  box_size += sizeof(data_offset);
2449  if (flags & kFirstSampleFlagsPresentMask)
2450  box_size += sizeof(uint32_t);
2451  uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
2452  (flags & kSampleSizePresentMask ? 1 : 0) +
2453  (flags & kSampleFlagsPresentMask ? 1 : 0) +
2454  (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
2455  box_size += fields * sizeof(uint32_t) * sample_count;
2456  return box_size;
2457 }
2458 
2459 TrackFragment::TrackFragment() : decode_time_absent(false) {}
2460 TrackFragment::~TrackFragment() {}
2461 FourCC TrackFragment::BoxType() const { return FOURCC_traf; }
2462 
2463 bool TrackFragment::ReadWriteInternal(BoxBuffer* buffer) {
2464  RCHECK(ReadWriteHeaderInternal(buffer) &&
2465  buffer->PrepareChildren() &&
2466  buffer->ReadWriteChild(&header));
2467  if (buffer->Reading()) {
2468  DCHECK(buffer->reader());
2469  decode_time_absent = !buffer->reader()->ChildExist(&decode_time);
2470  if (!decode_time_absent)
2471  RCHECK(buffer->ReadWriteChild(&decode_time));
2472  RCHECK(buffer->reader()->TryReadChildren(&runs) &&
2473  buffer->reader()->TryReadChildren(&sample_group_descriptions) &&
2474  buffer->reader()->TryReadChildren(&sample_to_groups));
2475  } else {
2476  if (!decode_time_absent)
2477  RCHECK(buffer->ReadWriteChild(&decode_time));
2478  for (uint32_t i = 0; i < runs.size(); ++i)
2479  RCHECK(buffer->ReadWriteChild(&runs[i]));
2480  for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2481  RCHECK(buffer->ReadWriteChild(&sample_to_groups[i]));
2482  for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2483  RCHECK(buffer->ReadWriteChild(&sample_group_descriptions[i]));
2484  }
2485  return buffer->TryReadWriteChild(&auxiliary_size) &&
2486  buffer->TryReadWriteChild(&auxiliary_offset) &&
2487  buffer->TryReadWriteChild(&sample_encryption);
2488 }
2489 
2490 size_t TrackFragment::ComputeSizeInternal() {
2491  size_t box_size = HeaderSize() + header.ComputeSize() +
2492  decode_time.ComputeSize() + auxiliary_size.ComputeSize() +
2493  auxiliary_offset.ComputeSize() +
2494  sample_encryption.ComputeSize();
2495  for (uint32_t i = 0; i < runs.size(); ++i)
2496  box_size += runs[i].ComputeSize();
2497  for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2498  box_size += sample_group_descriptions[i].ComputeSize();
2499  for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2500  box_size += sample_to_groups[i].ComputeSize();
2501  return box_size;
2502 }
2503 
2504 MovieFragment::MovieFragment() {}
2505 MovieFragment::~MovieFragment() {}
2506 FourCC MovieFragment::BoxType() const { return FOURCC_moof; }
2507 
2508 bool MovieFragment::ReadWriteInternal(BoxBuffer* buffer) {
2509  RCHECK(ReadWriteHeaderInternal(buffer) &&
2510  buffer->PrepareChildren() &&
2511  buffer->ReadWriteChild(&header));
2512  if (buffer->Reading()) {
2513  BoxReader* reader = buffer->reader();
2514  DCHECK(reader);
2515  RCHECK(reader->ReadChildren(&tracks) &&
2516  reader->TryReadChildren(&pssh));
2517  } else {
2518  for (uint32_t i = 0; i < tracks.size(); ++i)
2519  RCHECK(buffer->ReadWriteChild(&tracks[i]));
2520  for (uint32_t i = 0; i < pssh.size(); ++i)
2521  RCHECK(buffer->ReadWriteChild(&pssh[i]));
2522  }
2523  return true;
2524 }
2525 
2526 size_t MovieFragment::ComputeSizeInternal() {
2527  size_t box_size = HeaderSize() + header.ComputeSize();
2528  for (uint32_t i = 0; i < tracks.size(); ++i)
2529  box_size += tracks[i].ComputeSize();
2530  for (uint32_t i = 0; i < pssh.size(); ++i)
2531  box_size += pssh[i].ComputeSize();
2532  return box_size;
2533 }
2534 
2535 SegmentIndex::SegmentIndex()
2536  : reference_id(0),
2537  timescale(0),
2538  earliest_presentation_time(0),
2539  first_offset(0) {}
2540 SegmentIndex::~SegmentIndex() {}
2541 FourCC SegmentIndex::BoxType() const { return FOURCC_sidx; }
2542 
2543 bool SegmentIndex::ReadWriteInternal(BoxBuffer* buffer) {
2544  RCHECK(ReadWriteHeaderInternal(buffer) &&
2545  buffer->ReadWriteUInt32(&reference_id) &&
2546  buffer->ReadWriteUInt32(&timescale));
2547 
2548  size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
2549  RCHECK(
2550  buffer->ReadWriteUInt64NBytes(&earliest_presentation_time, num_bytes) &&
2551  buffer->ReadWriteUInt64NBytes(&first_offset, num_bytes));
2552 
2553  uint16_t reference_count = static_cast<uint16_t>(references.size());
2554  RCHECK(buffer->IgnoreBytes(2) && // reserved.
2555  buffer->ReadWriteUInt16(&reference_count));
2556  references.resize(reference_count);
2557 
2558  uint32_t reference_type_size;
2559  uint32_t sap;
2560  for (uint32_t i = 0; i < reference_count; ++i) {
2561  if (!buffer->Reading()) {
2562  reference_type_size = references[i].referenced_size;
2563  if (references[i].reference_type)
2564  reference_type_size |= (1 << 31);
2565  sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2566  if (references[i].starts_with_sap)
2567  sap |= (1 << 31);
2568  }
2569  RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2570  buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2571  buffer->ReadWriteUInt32(&sap));
2572  if (buffer->Reading()) {
2573  references[i].reference_type = (reference_type_size >> 31) ? true : false;
2574  references[i].referenced_size = reference_type_size & ~(1 << 31);
2575  references[i].starts_with_sap = (sap >> 31) ? true : false;
2576  references[i].sap_type =
2577  static_cast<SegmentReference::SAPType>((sap >> 28) & 0x07);
2578  references[i].sap_delta_time = sap & ~(0xF << 28);
2579  }
2580  }
2581  return true;
2582 }
2583 
2584 size_t SegmentIndex::ComputeSizeInternal() {
2585  version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2586  return HeaderSize() + sizeof(reference_id) + sizeof(timescale) +
2587  sizeof(uint32_t) * (1 + version) * 2 + 2 * sizeof(uint16_t) +
2588  3 * sizeof(uint32_t) * references.size();
2589 }
2590 
2591 MediaData::MediaData() : data_size(0) {}
2592 MediaData::~MediaData() {}
2593 FourCC MediaData::BoxType() const { return FOURCC_mdat; }
2594 
2595 bool MediaData::ReadWriteInternal(BoxBuffer* buffer) {
2596  NOTIMPLEMENTED() << "Actual data is parsed and written separately.";
2597  return false;
2598 }
2599 
2600 size_t MediaData::ComputeSizeInternal() {
2601  return HeaderSize() + data_size;
2602 }
2603 
2604 CueSourceIDBox::CueSourceIDBox() : source_id(kCueSourceIdNotSet) {}
2605 CueSourceIDBox::~CueSourceIDBox() {}
2606 
2607 FourCC CueSourceIDBox::BoxType() const { return FOURCC_vsid; }
2608 
2609 bool CueSourceIDBox::ReadWriteInternal(BoxBuffer* buffer) {
2610  RCHECK(ReadWriteHeaderInternal(buffer) && buffer->ReadWriteInt32(&source_id));
2611  return true;
2612 }
2613 
2614 size_t CueSourceIDBox::ComputeSizeInternal() {
2615  if (source_id == kCueSourceIdNotSet)
2616  return 0;
2617  return HeaderSize() + sizeof(source_id);
2618 }
2619 
2620 CueTimeBox::CueTimeBox() {}
2621 CueTimeBox::~CueTimeBox() {}
2622 
2623 FourCC CueTimeBox::BoxType() const {
2624  return FOURCC_ctim;
2625 }
2626 
2627 bool CueTimeBox::ReadWriteInternal(BoxBuffer* buffer) {
2628  RCHECK(ReadWriteHeaderInternal(buffer));
2629  return buffer->ReadWriteString(
2630  &cue_current_time,
2631  buffer->Reading() ? buffer->BytesLeft() : cue_current_time.size());
2632 }
2633 
2634 size_t CueTimeBox::ComputeSizeInternal() {
2635  if (cue_current_time.empty())
2636  return 0;
2637  return HeaderSize() + cue_current_time.size();
2638 }
2639 
2640 CueIDBox::CueIDBox() {}
2641 CueIDBox::~CueIDBox() {}
2642 
2643 FourCC CueIDBox::BoxType() const {
2644  return FOURCC_iden;
2645 }
2646 
2647 bool CueIDBox::ReadWriteInternal(BoxBuffer* buffer) {
2648  RCHECK(ReadWriteHeaderInternal(buffer));
2649  return buffer->ReadWriteString(
2650  &cue_id, buffer->Reading() ? buffer->BytesLeft() : cue_id.size());
2651 }
2652 
2653 size_t CueIDBox::ComputeSizeInternal() {
2654  if (cue_id.empty())
2655  return 0;
2656  return HeaderSize() + cue_id.size();
2657 }
2658 
2659 CueSettingsBox::CueSettingsBox() {}
2660 CueSettingsBox::~CueSettingsBox() {}
2661 
2662 FourCC CueSettingsBox::BoxType() const {
2663  return FOURCC_sttg;
2664 }
2665 
2666 bool CueSettingsBox::ReadWriteInternal(BoxBuffer* buffer) {
2667  RCHECK(ReadWriteHeaderInternal(buffer));
2668  return buffer->ReadWriteString(
2669  &settings, buffer->Reading() ? buffer->BytesLeft() : settings.size());
2670 }
2671 
2672 size_t CueSettingsBox::ComputeSizeInternal() {
2673  if (settings.empty())
2674  return 0;
2675  return HeaderSize() + settings.size();
2676 }
2677 
2678 CuePayloadBox::CuePayloadBox() {}
2679 CuePayloadBox::~CuePayloadBox() {}
2680 
2681 FourCC CuePayloadBox::BoxType() const {
2682  return FOURCC_payl;
2683 }
2684 
2685 bool CuePayloadBox::ReadWriteInternal(BoxBuffer* buffer) {
2686  RCHECK(ReadWriteHeaderInternal(buffer));
2687  return buffer->ReadWriteString(
2688  &cue_text, buffer->Reading() ? buffer->BytesLeft() : cue_text.size());
2689 }
2690 
2691 size_t CuePayloadBox::ComputeSizeInternal() {
2692  return HeaderSize() + cue_text.size();
2693 }
2694 
2695 VTTEmptyCueBox::VTTEmptyCueBox() {}
2696 VTTEmptyCueBox::~VTTEmptyCueBox() {}
2697 
2698 FourCC VTTEmptyCueBox::BoxType() const {
2699  return FOURCC_vtte;
2700 }
2701 
2702 bool VTTEmptyCueBox::ReadWriteInternal(BoxBuffer* buffer) {
2703  return ReadWriteHeaderInternal(buffer);
2704 }
2705 
2706 size_t VTTEmptyCueBox::ComputeSizeInternal() {
2707  return HeaderSize();
2708 }
2709 
2710 VTTAdditionalTextBox::VTTAdditionalTextBox() {}
2711 VTTAdditionalTextBox::~VTTAdditionalTextBox() {}
2712 
2714  return FOURCC_vtta;
2715 }
2716 
2717 bool VTTAdditionalTextBox::ReadWriteInternal(BoxBuffer* buffer) {
2718  RCHECK(ReadWriteHeaderInternal(buffer));
2719  return buffer->ReadWriteString(
2720  &cue_additional_text,
2721  buffer->Reading() ? buffer->BytesLeft() : cue_additional_text.size());
2722 }
2723 
2724 size_t VTTAdditionalTextBox::ComputeSizeInternal() {
2725  return HeaderSize() + cue_additional_text.size();
2726 }
2727 
2728 VTTCueBox::VTTCueBox() {}
2729 VTTCueBox::~VTTCueBox() {}
2730 
2731 FourCC VTTCueBox::BoxType() const {
2732  return FOURCC_vttc;
2733 }
2734 
2735 bool VTTCueBox::ReadWriteInternal(BoxBuffer* buffer) {
2736  RCHECK(ReadWriteHeaderInternal(buffer) &&
2737  buffer->PrepareChildren() &&
2738  buffer->TryReadWriteChild(&cue_source_id) &&
2739  buffer->TryReadWriteChild(&cue_id) &&
2740  buffer->TryReadWriteChild(&cue_time) &&
2741  buffer->TryReadWriteChild(&cue_settings) &&
2742  buffer->ReadWriteChild(&cue_payload));
2743  return true;
2744 }
2745 
2746 size_t VTTCueBox::ComputeSizeInternal() {
2747  return HeaderSize() + cue_source_id.ComputeSize() + cue_id.ComputeSize() +
2748  cue_time.ComputeSize() + cue_settings.ComputeSize() +
2749  cue_payload.ComputeSize();
2750 }
2751 
2752 } // namespace mp4
2753 } // namespace media
2754 } // 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