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/formats/mp4/aac_audio_specific_config.h"
11 #include "packager/media/formats/mp4/box.h"
12 #include "packager/media/formats/mp4/es_descriptor.h"
13 #include "packager/media/formats/mp4/fourccs.h"
14 
15 namespace edash_packager {
16 namespace media {
17 
18 class BufferReader;
19 
20 namespace mp4 {
21 
22 enum TrackType {
23  kInvalid = 0,
24  kVideo,
25  kAudio,
26  kHint
27 };
28 
29 class BoxBuffer;
30 
31 #define DECLARE_BOX_METHODS(T) \
32  T(); \
33  ~T() override; \
34  bool ReadWrite(BoxBuffer* buffer) override; \
35  FourCC BoxType() const override; \
36  uint32_t ComputeSize() override;
37 
38 struct FileType : Box {
39  DECLARE_BOX_METHODS(FileType);
40 
41  FourCC major_brand;
42  uint32_t minor_version;
43  std::vector<FourCC> compatible_brands;
44 };
45 
47  DECLARE_BOX_METHODS(SegmentType);
48 };
49 
51  DECLARE_BOX_METHODS(ProtectionSystemSpecificHeader);
52 
53  std::vector<uint8_t> system_id;
54  std::vector<uint8_t> data;
55  std::vector<uint8_t> raw_box;
56 };
57 
59  DECLARE_BOX_METHODS(SampleAuxiliaryInformationOffset);
60 
61  std::vector<uint64_t> offsets;
62 };
63 
65  DECLARE_BOX_METHODS(SampleAuxiliaryInformationSize);
66 
67  uint8_t default_sample_info_size;
68  uint32_t sample_count;
69  std::vector<uint8_t> sample_info_sizes;
70 };
71 
72 struct OriginalFormat : Box {
73  DECLARE_BOX_METHODS(OriginalFormat);
74 
75  FourCC format;
76 };
77 
78 struct SchemeType : FullBox {
79  DECLARE_BOX_METHODS(SchemeType);
80 
81  FourCC type;
82  uint32_t version;
83 };
84 
86  DECLARE_BOX_METHODS(TrackEncryption);
87 
88  // Note: this definition is specific to the CENC protection type.
89  bool is_encrypted;
90  uint8_t default_iv_size;
91  std::vector<uint8_t> default_kid;
92 };
93 
94 struct SchemeInfo : Box {
95  DECLARE_BOX_METHODS(SchemeInfo);
96 
97  TrackEncryption track_encryption;
98 };
99 
101  DECLARE_BOX_METHODS(ProtectionSchemeInfo);
102 
103  OriginalFormat format;
104  SchemeType type;
105  SchemeInfo info;
106 };
107 
109  DECLARE_BOX_METHODS(MovieHeader);
110 
111  uint64_t creation_time;
112  uint64_t modification_time;
113  uint32_t timescale;
114  uint64_t duration;
115  int32_t rate;
116  int16_t volume;
117  uint32_t next_track_id;
118 };
119 
121  enum TrackHeaderFlags {
122  kTrackEnabled = 0x000001,
123  kTrackInMovie = 0x000002,
124  kTrackInPreview = 0x000004,
125  };
126 
127  DECLARE_BOX_METHODS(TrackHeader);
128 
129  uint64_t creation_time;
130  uint64_t modification_time;
131  uint32_t track_id;
132  uint64_t duration;
133  int16_t layer;
134  int16_t alternate_group;
135  int16_t volume;
136  // width and height specify the track's visual presentation size as
137  // fixed-point 16.16 values.
138  uint32_t width;
139  uint32_t height;
140 };
141 
143  uint64_t segment_duration;
144  int64_t media_time;
145  int16_t media_rate_integer;
146  int16_t media_rate_fraction;
147 };
148 
149 struct EditList : FullBox {
150  DECLARE_BOX_METHODS(EditList);
151 
152  std::vector<EditListEntry> edits;
153 };
154 
155 struct Edit : Box {
156  DECLARE_BOX_METHODS(Edit);
157 
158  EditList list;
159 };
160 
162  DECLARE_BOX_METHODS(HandlerReference);
163 
164  TrackType type;
165 };
166 
168  DECLARE_BOX_METHODS(CodecConfigurationRecord);
169 
170  FourCC box_type;
171  // Contains full codec configuration record, including possible extension boxes.
172  std::vector<uint8_t> data;
173 };
174 
176  DECLARE_BOX_METHODS(PixelAspectRatioBox);
177 
178  uint32_t h_spacing;
179  uint32_t v_spacing;
180 };
181 
183  DECLARE_BOX_METHODS(VideoSampleEntry);
184  // Returns actual format of this sample entry.
185  FourCC GetActualFormat() const {
186  return format == FOURCC_ENCV ? sinf.format.format : format;
187  }
188 
189  FourCC format;
190  uint16_t data_reference_index;
191  uint16_t width;
192  uint16_t height;
193 
194  PixelAspectRatioBox pixel_aspect;
196  CodecConfigurationRecord codec_config_record;
197 };
198 
200  DECLARE_BOX_METHODS(ElementaryStreamDescriptor);
201 
202  AACAudioSpecificConfig aac_audio_specific_config;
203  ESDescriptor es_descriptor;
204 };
205 
207  DECLARE_BOX_METHODS(AudioSampleEntry);
208  // Returns actual format of this sample entry.
209  FourCC GetActualFormat() const {
210  return format == FOURCC_ENCA ? sinf.format.format : format;
211  }
212 
213  FourCC format;
214  uint16_t data_reference_index;
215  uint16_t channelcount;
216  uint16_t samplesize;
217  uint32_t samplerate;
218 
221  std::vector<uint8_t> extra_data;
222 };
223 
225  DECLARE_BOX_METHODS(SampleDescription);
226 
227  TrackType type;
228  std::vector<VideoSampleEntry> video_entries;
229  std::vector<AudioSampleEntry> audio_entries;
230 };
231 
232 struct DecodingTime {
233  uint32_t sample_count;
234  uint32_t sample_delta;
235 };
236 
237 // stts.
239  DECLARE_BOX_METHODS(DecodingTimeToSample);
240 
241  std::vector<DecodingTime> decoding_time;
242 };
243 
245  uint32_t sample_count;
246  // If version == 0, sample_offset is uint32_t;
247  // If version == 1, sample_offset is int32_t.
248  // Use int64_t so both can be supported properly.
249  int64_t sample_offset;
250 };
251 
252 // ctts. Optional.
254  DECLARE_BOX_METHODS(CompositionTimeToSample);
255 
256  std::vector<CompositionOffset> composition_offset;
257 };
258 
259 struct ChunkInfo {
260  uint32_t first_chunk;
261  uint32_t samples_per_chunk;
262  uint32_t sample_description_index;
263 };
264 
265 // stsc.
267  DECLARE_BOX_METHODS(SampleToChunk);
268 
269  std::vector<ChunkInfo> chunk_info;
270 };
271 
272 // stsz.
273 struct SampleSize : FullBox {
274  DECLARE_BOX_METHODS(SampleSize);
275 
276  uint32_t sample_size;
277  uint32_t sample_count;
278  std::vector<uint32_t> sizes;
279 };
280 
281 // stz2.
283  DECLARE_BOX_METHODS(CompactSampleSize);
284 
285  uint8_t field_size;
286  std::vector<uint32_t> sizes;
287 };
288 
289 // co64.
291  DECLARE_BOX_METHODS(ChunkLargeOffset);
292 
293  std::vector<uint64_t> offsets;
294 };
295 
296 // stco.
298  DECLARE_BOX_METHODS(ChunkOffset);
299 };
300 
301 // stss. Optional.
302 struct SyncSample : FullBox {
303  DECLARE_BOX_METHODS(SyncSample);
304 
305  std::vector<uint32_t> sample_number;
306 };
307 
308 struct SampleTable : Box {
309  DECLARE_BOX_METHODS(SampleTable);
310 
311  SampleDescription description;
312  DecodingTimeToSample decoding_time_to_sample;
313  CompositionTimeToSample composition_time_to_sample;
314  SampleToChunk sample_to_chunk;
315  // Either SampleSize or CompactSampleSize must present. Store in SampleSize.
316  SampleSize sample_size;
317  // Either ChunkOffset or ChunkLargeOffset must present. Store in
318  // ChunkLargeOffset.
319  ChunkLargeOffset chunk_large_offset;
320  SyncSample sync_sample;
321 };
322 
324  DECLARE_BOX_METHODS(MediaHeader);
325 
326  uint64_t creation_time;
327  uint64_t modification_time;
328  uint32_t timescale;
329  uint64_t duration;
330  // 3-char language code + 1 null terminating char.
331  char language[4];
332 };
333 
335  DECLARE_BOX_METHODS(VideoMediaHeader);
336 
337  uint16_t graphicsmode;
338  uint16_t opcolor_red;
339  uint16_t opcolor_green;
340  uint16_t opcolor_blue;
341 };
342 
344  DECLARE_BOX_METHODS(SoundMediaHeader);
345 
346  uint16_t balance;
347 };
348 
350  DECLARE_BOX_METHODS(DataEntryUrl);
351 
352  std::vector<uint8_t> location;
353 };
354 
356  DECLARE_BOX_METHODS(DataReference);
357 
358  // data entry can be either url or urn box. Fix to url box for now.
359  std::vector<DataEntryUrl> data_entry;
360 };
361 
363  DECLARE_BOX_METHODS(DataInformation);
364 
365  DataReference dref;
366 };
367 
369  DECLARE_BOX_METHODS(MediaInformation);
370 
371  DataInformation dinf;
372  SampleTable sample_table;
373  // Exactly one specific meida header shall be present, vmhd, smhd, hmhd, nmhd.
374  VideoMediaHeader vmhd;
375  SoundMediaHeader smhd;
376 };
377 
378 struct Media : Box {
379  DECLARE_BOX_METHODS(Media);
380 
381  MediaHeader header;
382  HandlerReference handler;
383  MediaInformation information;
384 };
385 
386 struct Track : Box {
387  DECLARE_BOX_METHODS(Track);
388 
389  TrackHeader header;
390  Media media;
391  Edit edit;
392 };
393 
395  DECLARE_BOX_METHODS(MovieExtendsHeader);
396 
397  uint64_t fragment_duration;
398 };
399 
401  DECLARE_BOX_METHODS(TrackExtends);
402 
403  uint32_t track_id;
404  uint32_t default_sample_description_index;
405  uint32_t default_sample_duration;
406  uint32_t default_sample_size;
407  uint32_t default_sample_flags;
408 };
409 
410 struct MovieExtends : Box {
411  DECLARE_BOX_METHODS(MovieExtends);
412 
413  MovieExtendsHeader header;
414  std::vector<TrackExtends> tracks;
415 };
416 
417 struct Movie : Box {
418  DECLARE_BOX_METHODS(Movie);
419 
420  MovieHeader header;
421  MovieExtends extends;
422  std::vector<Track> tracks;
423  std::vector<ProtectionSystemSpecificHeader> pssh;
424 };
425 
427  DECLARE_BOX_METHODS(TrackFragmentDecodeTime);
428 
429  uint64_t decode_time;
430 };
431 
433  DECLARE_BOX_METHODS(MovieFragmentHeader);
434 
435  uint32_t sequence_number;
436 };
437 
439  enum TrackFragmentFlagsMasks {
440  kBaseDataOffsetPresentMask = 0x000001,
441  kSampleDescriptionIndexPresentMask = 0x000002,
442  kDefaultSampleDurationPresentMask = 0x000008,
443  kDefaultSampleSizePresentMask = 0x000010,
444  kDefaultSampleFlagsPresentMask = 0x000020,
445  kDurationIsEmptyMask = 0x010000,
446  kDefaultBaseIsMoofMask = 0x020000,
447  };
448 
449  enum SampleFlagsMasks {
450  kReservedMask = 0xFC000000,
451  kSampleDependsOnMask = 0x03000000,
452  kSampleIsDependedOnMask = 0x00C00000,
453  kSampleHasRedundancyMask = 0x00300000,
454  kSamplePaddingValueMask = 0x000E0000,
455  kNonKeySampleMask = 0x00010000,
456  kSampleDegradationPriorityMask = 0x0000FFFF,
457  };
458 
459  DECLARE_BOX_METHODS(TrackFragmentHeader);
460 
461  uint32_t track_id;
462  uint32_t sample_description_index;
463  uint32_t default_sample_duration;
464  uint32_t default_sample_size;
465  uint32_t default_sample_flags;
466 };
467 
469  enum TrackFragmentFlagsMasks {
470  kDataOffsetPresentMask = 0x000001,
471  kFirstSampleFlagsPresentMask = 0x000004,
472  kSampleDurationPresentMask = 0x000100,
473  kSampleSizePresentMask = 0x000200,
474  kSampleFlagsPresentMask = 0x000400,
475  kSampleCompTimeOffsetsPresentMask = 0x000800,
476  };
477 
478  DECLARE_BOX_METHODS(TrackFragmentRun);
479 
480  uint32_t sample_count;
481  uint32_t data_offset;
482  std::vector<uint32_t> sample_flags;
483  std::vector<uint32_t> sample_sizes;
484  std::vector<uint32_t> sample_durations;
485  std::vector<int64_t> sample_composition_time_offsets;
486 };
487 
489  enum GroupDescriptionIndexBase {
490  kTrackGroupDescriptionIndexBase = 0,
491  kTrackFragmentGroupDescriptionIndexBase = 0x10000,
492  };
493 
494  uint32_t sample_count;
495  uint32_t group_description_index;
496 };
497 
499  DECLARE_BOX_METHODS(SampleToGroup);
500 
501  uint32_t grouping_type;
502  uint32_t grouping_type_parameter; // Version 1 only.
503  std::vector<SampleToGroupEntry> entries;
504 };
505 
509 
510  bool is_encrypted;
511  uint8_t iv_size;
512  std::vector<uint8_t> key_id;
513 };
514 
516  DECLARE_BOX_METHODS(SampleGroupDescription);
517 
518  uint32_t grouping_type;
519  std::vector<CencSampleEncryptionInfoEntry> entries;
520 };
521 
522 struct TrackFragment : Box {
523  DECLARE_BOX_METHODS(TrackFragment);
524 
525  TrackFragmentHeader header;
526  std::vector<TrackFragmentRun> runs;
527  bool decode_time_absent;
528  TrackFragmentDecodeTime decode_time;
529  SampleToGroup sample_to_group;
530  SampleGroupDescription sample_group_description;
531  SampleAuxiliaryInformationSize auxiliary_size;
532  SampleAuxiliaryInformationOffset auxiliary_offset;
533 };
534 
535 struct MovieFragment : Box {
536  DECLARE_BOX_METHODS(MovieFragment);
537 
538  MovieFragmentHeader header;
539  std::vector<TrackFragment> tracks;
540  std::vector<ProtectionSystemSpecificHeader> pssh;
541 };
542 
544  enum SAPType {
545  TypeUnknown = 0,
546  Type1 = 1, // T(ept) = T(dec) = T(sap) = T(ptf)
547  Type2 = 2, // T(ept) = T(dec) = T(sap) < T(ptf)
548  Type3 = 3, // T(ept) < T(dec) = T(sap) <= T(ptf)
549  Type4 = 4, // T(ept) <= T(ptf) < T(dec) = T(sap)
550  Type5 = 5, // T(ept) = T(dec) < T(sap)
551  Type6 = 6, // T(ept) < T(dec) < T(sap)
552  };
553 
554  bool reference_type;
555  uint32_t referenced_size;
556  uint32_t subsegment_duration;
557  bool starts_with_sap;
558  SAPType sap_type;
559  uint32_t sap_delta_time;
560  // We add this field to keep track of earliest_presentation_time in this
561  // subsegment. It is not part of SegmentReference.
562  uint64_t earliest_presentation_time;
563 };
564 
566  DECLARE_BOX_METHODS(SegmentIndex);
567 
568  uint32_t reference_id;
569  uint32_t timescale;
570  uint64_t earliest_presentation_time;
571  uint64_t first_offset;
572  std::vector<SegmentReference> references;
573 };
574 
575 // The actual data is parsed and written separately, so we do not inherit it
576 // from Box.
577 struct MediaData {
578  MediaData();
579  ~MediaData();
580  void Write(BufferWriter* buffer_writer);
581  uint32_t ComputeSize();
582  FourCC BoxType() const;
583 
584  uint32_t data_size;
585 };
586 
587 #undef DECLARE_BOX
588 
589 } // namespace mp4
590 } // namespace media
591 } // namespace edash_packager
592 
593 #endif // MEDIA_FORMATS_MP4_BOX_DEFINITIONS_H_