DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
box_definitions.h
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 #ifndef MEDIA_FORMATS_MP4_BOX_DEFINITIONS_H_
6 #define MEDIA_FORMATS_MP4_BOX_DEFINITIONS_H_
7 
8 #include <vector>
9 
10 #include "packager/media/base/decrypt_config.h"
11 #include "packager/media/formats/mp4/aac_audio_specific_config.h"
12 #include "packager/media/formats/mp4/box.h"
13 #include "packager/media/formats/mp4/es_descriptor.h"
14 #include "packager/media/formats/mp4/fourccs.h"
15 
16 namespace edash_packager {
17 namespace media {
18 
19 class BufferReader;
20 
21 namespace mp4 {
22 
23 enum TrackType {
24  kInvalid = 0,
25  kVideo,
26  kAudio,
27  kHint,
28  kText,
29 };
30 
31 class BoxBuffer;
32 
33 #define DECLARE_BOX_METHODS(T) \
34  public: \
35  T(); \
36  ~T() override; \
37  FourCC BoxType() const override; \
38  \
39  private: \
40  bool ReadWriteInternal(BoxBuffer* buffer) override; \
41  uint32_t ComputeSizeInternal() override; \
42  \
43  public:
44 
45 struct FileType : Box {
46  DECLARE_BOX_METHODS(FileType);
47 
48  FourCC major_brand;
49  uint32_t minor_version;
50  std::vector<FourCC> compatible_brands;
51 };
52 
54  FourCC BoxType() const override;
55 };
56 
58  DECLARE_BOX_METHODS(ProtectionSystemSpecificHeader);
59 
60  std::vector<uint8_t> system_id;
61  std::vector<uint8_t> data;
62  std::vector<uint8_t> raw_box;
63 };
64 
66  DECLARE_BOX_METHODS(SampleAuxiliaryInformationOffset);
67 
68  std::vector<uint64_t> offsets;
69 };
70 
72  DECLARE_BOX_METHODS(SampleAuxiliaryInformationSize);
73 
74  uint8_t default_sample_info_size;
75  uint32_t sample_count;
76  std::vector<uint8_t> sample_info_sizes;
77 };
78 
88  bool ReadWrite(uint8_t iv_size,
89  bool has_subsamples,
90  BoxBuffer* buffer);
97  bool ParseFromBuffer(uint8_t iv_size,
98  bool has_subsamples,
99  BufferReader* reader);
101  uint32_t ComputeSize() const;
104  uint32_t GetTotalSizeOfSubsamples() const;
105 
106  std::vector<uint8_t> initialization_vector;
107  std::vector<SubsampleEntry> subsamples;
108 };
109 
111  enum SampleEncryptionFlags {
112  kUseSubsampleEncryption = 2,
113  };
114 
115  DECLARE_BOX_METHODS(SampleEncryption);
122  size_t iv_size,
123  std::vector<SampleEncryptionEntry>* sample_encryption_entries) const;
124 
127  std::vector<uint8_t> sample_encryption_data;
128 
129  size_t iv_size;
130  std::vector<SampleEncryptionEntry> sample_encryption_entries;
131 };
132 
133 struct OriginalFormat : Box {
134  DECLARE_BOX_METHODS(OriginalFormat);
135 
136  FourCC format;
137 };
138 
139 struct SchemeType : FullBox {
140  DECLARE_BOX_METHODS(SchemeType);
141 
142  FourCC type;
143  uint32_t version;
144 };
145 
147  DECLARE_BOX_METHODS(TrackEncryption);
148 
149  // Note: this definition is specific to the CENC protection type.
150  bool is_encrypted;
151  uint8_t default_iv_size;
152  std::vector<uint8_t> default_kid;
153 };
154 
155 struct SchemeInfo : Box {
156  DECLARE_BOX_METHODS(SchemeInfo);
157 
158  TrackEncryption track_encryption;
159 };
160 
162  DECLARE_BOX_METHODS(ProtectionSchemeInfo);
163 
164  OriginalFormat format;
165  SchemeType type;
166  SchemeInfo info;
167 };
168 
170  DECLARE_BOX_METHODS(MovieHeader);
171 
172  uint64_t creation_time;
173  uint64_t modification_time;
174  uint32_t timescale;
175  uint64_t duration;
176  int32_t rate;
177  int16_t volume;
178  uint32_t next_track_id;
179 };
180 
182  enum TrackHeaderFlags {
183  kTrackEnabled = 0x000001,
184  kTrackInMovie = 0x000002,
185  kTrackInPreview = 0x000004,
186  };
187 
188  DECLARE_BOX_METHODS(TrackHeader);
189 
190  uint64_t creation_time;
191  uint64_t modification_time;
192  uint32_t track_id;
193  uint64_t duration;
194  int16_t layer;
195  int16_t alternate_group;
196  int16_t volume;
197  // width and height specify the track's visual presentation size as
198  // fixed-point 16.16 values.
199  uint32_t width;
200  uint32_t height;
201 };
202 
204  uint64_t segment_duration;
205  int64_t media_time;
206  int16_t media_rate_integer;
207  int16_t media_rate_fraction;
208 };
209 
210 struct EditList : FullBox {
211  DECLARE_BOX_METHODS(EditList);
212 
213  std::vector<EditListEntry> edits;
214 };
215 
216 struct Edit : Box {
217  DECLARE_BOX_METHODS(Edit);
218 
219  EditList list;
220 };
221 
223  DECLARE_BOX_METHODS(HandlerReference);
224 
225  FourCC handler_type;
226 };
227 
228 struct Language {
229  bool ReadWrite(BoxBuffer* buffer);
230  uint32_t ComputeSize() const;
231 
232  std::string code;
233 };
234 
236 struct PrivFrame {
237  bool ReadWrite(BoxBuffer* buffer);
238  uint32_t ComputeSize() const;
239 
240  std::string owner;
241  std::string value;
242 };
243 
246 struct ID3v2 : FullBox {
247  DECLARE_BOX_METHODS(ID3v2);
248 
249  Language language;
250 
253 };
254 
255 struct Metadata : FullBox {
256  DECLARE_BOX_METHODS(Metadata);
257 
258  HandlerReference handler;
259  ID3v2 id3v2;
260 };
261 
263  DECLARE_BOX_METHODS(CodecConfigurationRecord);
264 
265  FourCC box_type;
266  // Contains full codec configuration record, including possible extension
267  // boxes.
268  std::vector<uint8_t> data;
269 };
270 
272  DECLARE_BOX_METHODS(PixelAspectRatio);
273 
274  uint32_t h_spacing;
275  uint32_t v_spacing;
276 };
277 
279  DECLARE_BOX_METHODS(VideoSampleEntry);
280  // Returns actual format of this sample entry.
281  FourCC GetActualFormat() const {
282  return format == FOURCC_ENCV ? sinf.format.format : format;
283  }
284 
285  FourCC format;
286  uint16_t data_reference_index;
287  uint16_t width;
288  uint16_t height;
289 
290  PixelAspectRatio pixel_aspect;
292  CodecConfigurationRecord codec_config_record;
293 };
294 
296  DECLARE_BOX_METHODS(ElementaryStreamDescriptor);
297 
298  AACAudioSpecificConfig aac_audio_specific_config;
299  ESDescriptor es_descriptor;
300 };
301 
302 struct DTSSpecific : Box {
303  DECLARE_BOX_METHODS(DTSSpecific);
304 
305  uint32_t sampling_frequency;
306  uint32_t max_bitrate;
307  uint32_t avg_bitrate;
308  uint8_t pcm_sample_depth;
309  std::vector<uint8_t> extra_data;
310 };
311 
313  DECLARE_BOX_METHODS(AudioSampleEntry);
314  // Returns actual format of this sample entry.
315  FourCC GetActualFormat() const {
316  return format == FOURCC_ENCA ? sinf.format.format : format;
317  }
318 
319  FourCC format;
320  uint16_t data_reference_index;
321  uint16_t channelcount;
322  uint16_t samplesize;
323  uint32_t samplerate;
324 
327  DTSSpecific ddts;
328 };
329 
331  DECLARE_BOX_METHODS(WebVTTConfigurationBox);
332  std::string config;
333 };
334 
336  DECLARE_BOX_METHODS(WebVTTSourceLabelBox);
337  std::string source_label;
338 };
339 
341  DECLARE_BOX_METHODS(WVTTSampleEntry);
342 
343  uint16_t data_reference_index;
344 
345  WebVTTConfigurationBox config;
346  WebVTTSourceLabelBox label;
347  // Optional MPEG4BitRateBox.
348 };
349 
351  DECLARE_BOX_METHODS(SampleDescription);
352 
353  TrackType type;
354  // TODO(kqyang): Clean up the code to have one single member, e.g. by creating
355  // SampleEntry struct, std::vector<SampleEntry> sample_entries.
356  std::vector<VideoSampleEntry> video_entries;
357  std::vector<AudioSampleEntry> audio_entries;
358  std::vector<WVTTSampleEntry> wvtt_entries;
359 };
360 
361 struct DecodingTime {
362  uint32_t sample_count;
363  uint32_t sample_delta;
364 };
365 
366 // stts.
368  DECLARE_BOX_METHODS(DecodingTimeToSample);
369 
370  std::vector<DecodingTime> decoding_time;
371 };
372 
374  uint32_t sample_count;
375  // If version == 0, sample_offset is uint32_t;
376  // If version == 1, sample_offset is int32_t.
377  // Use int64_t so both can be supported properly.
378  int64_t sample_offset;
379 };
380 
381 // ctts. Optional.
383  DECLARE_BOX_METHODS(CompositionTimeToSample);
384 
385  std::vector<CompositionOffset> composition_offset;
386 };
387 
388 struct ChunkInfo {
389  uint32_t first_chunk;
390  uint32_t samples_per_chunk;
391  uint32_t sample_description_index;
392 };
393 
394 // stsc.
396  DECLARE_BOX_METHODS(SampleToChunk);
397 
398  std::vector<ChunkInfo> chunk_info;
399 };
400 
401 // stsz.
402 struct SampleSize : FullBox {
403  DECLARE_BOX_METHODS(SampleSize);
404 
405  uint32_t sample_size;
406  uint32_t sample_count;
407  std::vector<uint32_t> sizes;
408 };
409 
410 // stz2.
412  DECLARE_BOX_METHODS(CompactSampleSize);
413 
414  uint8_t field_size;
415  std::vector<uint32_t> sizes;
416 };
417 
418 // co64.
420  DECLARE_BOX_METHODS(ChunkLargeOffset);
421 
422  std::vector<uint64_t> offsets;
423 };
424 
425 // stco.
427  DECLARE_BOX_METHODS(ChunkOffset);
428 };
429 
430 // stss. Optional.
431 struct SyncSample : FullBox {
432  DECLARE_BOX_METHODS(SyncSample);
433 
434  std::vector<uint32_t> sample_number;
435 };
436 
437 struct SampleTable : Box {
438  DECLARE_BOX_METHODS(SampleTable);
439 
440  SampleDescription description;
441  DecodingTimeToSample decoding_time_to_sample;
442  CompositionTimeToSample composition_time_to_sample;
443  SampleToChunk sample_to_chunk;
444  // Either SampleSize or CompactSampleSize must present. Store in SampleSize.
445  SampleSize sample_size;
446  // Either ChunkOffset or ChunkLargeOffset must present. Store in
447  // ChunkLargeOffset.
448  ChunkLargeOffset chunk_large_offset;
449  SyncSample sync_sample;
450 };
451 
453  DECLARE_BOX_METHODS(MediaHeader);
454 
455  uint64_t creation_time;
456  uint64_t modification_time;
457  uint32_t timescale;
458  uint64_t duration;
459  Language language;
460 };
461 
463  DECLARE_BOX_METHODS(VideoMediaHeader);
464 
465  uint16_t graphicsmode;
466  uint16_t opcolor_red;
467  uint16_t opcolor_green;
468  uint16_t opcolor_blue;
469 };
470 
472  DECLARE_BOX_METHODS(SoundMediaHeader);
473 
474  uint16_t balance;
475 };
476 
478  DECLARE_BOX_METHODS(SubtitleMediaHeader);
479 };
480 
482  DECLARE_BOX_METHODS(DataEntryUrl);
483 
484  std::vector<uint8_t> location;
485 };
486 
488  DECLARE_BOX_METHODS(DataReference);
489 
490  // data entry can be either url or urn box. Fix to url box for now.
491  std::vector<DataEntryUrl> data_entry;
492 };
493 
495  DECLARE_BOX_METHODS(DataInformation);
496 
497  DataReference dref;
498 };
499 
501  DECLARE_BOX_METHODS(MediaInformation);
502 
503  DataInformation dinf;
504  SampleTable sample_table;
505  // Exactly one specific meida header shall be present, vmhd, smhd, hmhd, nmhd.
506  VideoMediaHeader vmhd;
507  SoundMediaHeader smhd;
508  SubtitleMediaHeader sthd;
509 };
510 
511 struct Media : Box {
512  DECLARE_BOX_METHODS(Media);
513 
514  MediaHeader header;
515  HandlerReference handler;
516  MediaInformation information;
517 };
518 
519 struct Track : Box {
520  DECLARE_BOX_METHODS(Track);
521 
522  TrackHeader header;
523  Media media;
524  Edit edit;
525  SampleEncryption sample_encryption;
526 };
527 
529  DECLARE_BOX_METHODS(MovieExtendsHeader);
530 
531  uint64_t fragment_duration;
532 };
533 
535  DECLARE_BOX_METHODS(TrackExtends);
536 
537  uint32_t track_id;
538  uint32_t default_sample_description_index;
539  uint32_t default_sample_duration;
540  uint32_t default_sample_size;
541  uint32_t default_sample_flags;
542 };
543 
544 struct MovieExtends : Box {
545  DECLARE_BOX_METHODS(MovieExtends);
546 
547  MovieExtendsHeader header;
548  std::vector<TrackExtends> tracks;
549 };
550 
551 struct Movie : Box {
552  DECLARE_BOX_METHODS(Movie);
553 
554  MovieHeader header;
555  Metadata metadata; // Used to hold version information.
556  MovieExtends extends;
557  std::vector<Track> tracks;
558  std::vector<ProtectionSystemSpecificHeader> pssh;
559 };
560 
562  DECLARE_BOX_METHODS(TrackFragmentDecodeTime);
563 
564  uint64_t decode_time;
565 };
566 
568  DECLARE_BOX_METHODS(MovieFragmentHeader);
569 
570  uint32_t sequence_number;
571 };
572 
574  enum TrackFragmentFlagsMasks {
575  kBaseDataOffsetPresentMask = 0x000001,
576  kSampleDescriptionIndexPresentMask = 0x000002,
577  kDefaultSampleDurationPresentMask = 0x000008,
578  kDefaultSampleSizePresentMask = 0x000010,
579  kDefaultSampleFlagsPresentMask = 0x000020,
580  kDurationIsEmptyMask = 0x010000,
581  kDefaultBaseIsMoofMask = 0x020000,
582  };
583 
584  enum SampleFlagsMasks {
585  kReservedMask = 0xFC000000,
586  kSampleDependsOnMask = 0x03000000,
587  kSampleIsDependedOnMask = 0x00C00000,
588  kSampleHasRedundancyMask = 0x00300000,
589  kSamplePaddingValueMask = 0x000E0000,
590  kNonKeySampleMask = 0x00010000,
591  kSampleDegradationPriorityMask = 0x0000FFFF,
592  };
593 
594  DECLARE_BOX_METHODS(TrackFragmentHeader);
595 
596  uint32_t track_id;
597  uint32_t sample_description_index;
598  uint32_t default_sample_duration;
599  uint32_t default_sample_size;
600  uint32_t default_sample_flags;
601 };
602 
604  enum TrackFragmentFlagsMasks {
605  kDataOffsetPresentMask = 0x000001,
606  kFirstSampleFlagsPresentMask = 0x000004,
607  kSampleDurationPresentMask = 0x000100,
608  kSampleSizePresentMask = 0x000200,
609  kSampleFlagsPresentMask = 0x000400,
610  kSampleCompTimeOffsetsPresentMask = 0x000800,
611  };
612 
613  DECLARE_BOX_METHODS(TrackFragmentRun);
614 
615  uint32_t sample_count;
616  uint32_t data_offset;
617  std::vector<uint32_t> sample_flags;
618  std::vector<uint32_t> sample_sizes;
619  std::vector<uint32_t> sample_durations;
620  std::vector<int64_t> sample_composition_time_offsets;
621 };
622 
624  enum GroupDescriptionIndexBase {
625  kTrackGroupDescriptionIndexBase = 0,
626  kTrackFragmentGroupDescriptionIndexBase = 0x10000,
627  };
628 
629  uint32_t sample_count;
630  uint32_t group_description_index;
631 };
632 
634  DECLARE_BOX_METHODS(SampleToGroup);
635 
636  uint32_t grouping_type;
637  uint32_t grouping_type_parameter; // Version 1 only.
638  std::vector<SampleToGroupEntry> entries;
639 };
640 
644 
645  bool is_encrypted;
646  uint8_t iv_size;
647  std::vector<uint8_t> key_id;
648 };
649 
651  DECLARE_BOX_METHODS(SampleGroupDescription);
652 
653  uint32_t grouping_type;
654  std::vector<CencSampleEncryptionInfoEntry> entries;
655 };
656 
657 struct TrackFragment : Box {
658  DECLARE_BOX_METHODS(TrackFragment);
659 
660  TrackFragmentHeader header;
661  std::vector<TrackFragmentRun> runs;
662  bool decode_time_absent;
663  TrackFragmentDecodeTime decode_time;
664  SampleToGroup sample_to_group;
665  SampleGroupDescription sample_group_description;
666  SampleAuxiliaryInformationSize auxiliary_size;
667  SampleAuxiliaryInformationOffset auxiliary_offset;
668  SampleEncryption sample_encryption;
669 };
670 
671 struct MovieFragment : Box {
672  DECLARE_BOX_METHODS(MovieFragment);
673 
674  MovieFragmentHeader header;
675  std::vector<TrackFragment> tracks;
676  std::vector<ProtectionSystemSpecificHeader> pssh;
677 };
678 
680  enum SAPType {
681  TypeUnknown = 0,
682  Type1 = 1, // T(ept) = T(dec) = T(sap) = T(ptf)
683  Type2 = 2, // T(ept) = T(dec) = T(sap) < T(ptf)
684  Type3 = 3, // T(ept) < T(dec) = T(sap) <= T(ptf)
685  Type4 = 4, // T(ept) <= T(ptf) < T(dec) = T(sap)
686  Type5 = 5, // T(ept) = T(dec) < T(sap)
687  Type6 = 6, // T(ept) < T(dec) < T(sap)
688  };
689 
690  bool reference_type;
691  uint32_t referenced_size;
692  uint32_t subsegment_duration;
693  bool starts_with_sap;
694  SAPType sap_type;
695  uint32_t sap_delta_time;
696  // We add this field to keep track of earliest_presentation_time in this
697  // subsegment. It is not part of SegmentReference.
698  uint64_t earliest_presentation_time;
699 };
700 
702  DECLARE_BOX_METHODS(SegmentIndex);
703 
704  uint32_t reference_id;
705  uint32_t timescale;
706  uint64_t earliest_presentation_time;
707  uint64_t first_offset;
708  std::vector<SegmentReference> references;
709 };
710 
711 // The actual data is parsed and written separately.
712 struct MediaData : Box {
713  DECLARE_BOX_METHODS(MediaData);
714 
715  uint32_t data_size;
716 };
717 
718 struct CueSourceIDBox : Box {
719  DECLARE_BOX_METHODS(CueSourceIDBox);
720  int32_t source_id;
721 };
722 
723 struct CueTimeBox : Box {
724  DECLARE_BOX_METHODS(CueTimeBox);
725  std::string cue_current_time;
726 };
727 
728 struct CueIDBox : Box {
729  DECLARE_BOX_METHODS(CueIDBox);
730  std::string cue_id;
731 };
732 
733 struct CueSettingsBox : Box {
734  DECLARE_BOX_METHODS(CueSettingsBox);
735  std::string settings;
736 };
737 
738 struct CuePayloadBox : Box {
739  DECLARE_BOX_METHODS(CuePayloadBox);
740  std::string cue_text;
741 };
742 
743 struct VTTEmptyCueBox : Box {
744  DECLARE_BOX_METHODS(VTTEmptyCueBox);
745 };
746 
748  DECLARE_BOX_METHODS(VTTAdditionalTextBox);
749  std::string cue_additional_text;
750 };
751 
752 struct VTTCueBox : Box {
753  DECLARE_BOX_METHODS(VTTCueBox);
754 
755  CueSourceIDBox cue_source_id;
756  CueIDBox cue_id;
757  CueTimeBox cue_time;
758  CueSettingsBox cue_settings;
759  CuePayloadBox cue_payload;
760 };
761 
762 #undef DECLARE_BOX
763 
764 } // namespace mp4
765 } // namespace media
766 } // namespace edash_packager
767 
768 #endif // MEDIA_FORMATS_MP4_BOX_DEFINITIONS_H_
Implemented per http://id3.org/id3v2.4.0-frames.
bool ParseFromSampleEncryptionData(size_t iv_size, std::vector< SampleEncryptionEntry > *sample_encryption_entries) const
PrivFrame private_frame
We only support PrivateFrame in ID3. Other frames are ignored.
bool ParseFromBuffer(uint8_t iv_size, bool has_subsamples, BufferReader *reader)
bool ReadWrite(uint8_t iv_size, bool has_subsamples, BoxBuffer *buffer)