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 };
222 
224  DECLARE_BOX_METHODS(SampleDescription);
225 
226  TrackType type;
227  std::vector<VideoSampleEntry> video_entries;
228  std::vector<AudioSampleEntry> audio_entries;
229 };
230 
231 struct DecodingTime {
232  uint32_t sample_count;
233  uint32_t sample_delta;
234 };
235 
236 // stts.
238  DECLARE_BOX_METHODS(DecodingTimeToSample);
239 
240  std::vector<DecodingTime> decoding_time;
241 };
242 
244  uint32_t sample_count;
245  // If version == 0, sample_offset is uint32_t;
246  // If version == 1, sample_offset is int32_t.
247  // Use int64_t so both can be supported properly.
248  int64_t sample_offset;
249 };
250 
251 // ctts. Optional.
253  DECLARE_BOX_METHODS(CompositionTimeToSample);
254 
255  std::vector<CompositionOffset> composition_offset;
256 };
257 
258 struct ChunkInfo {
259  uint32_t first_chunk;
260  uint32_t samples_per_chunk;
261  uint32_t sample_description_index;
262 };
263 
264 // stsc.
266  DECLARE_BOX_METHODS(SampleToChunk);
267 
268  std::vector<ChunkInfo> chunk_info;
269 };
270 
271 // stsz.
272 struct SampleSize : FullBox {
273  DECLARE_BOX_METHODS(SampleSize);
274 
275  uint32_t sample_size;
276  uint32_t sample_count;
277  std::vector<uint32_t> sizes;
278 };
279 
280 // stz2.
282  DECLARE_BOX_METHODS(CompactSampleSize);
283 
284  uint8_t field_size;
285  std::vector<uint32_t> sizes;
286 };
287 
288 // co64.
290  DECLARE_BOX_METHODS(ChunkLargeOffset);
291 
292  std::vector<uint64_t> offsets;
293 };
294 
295 // stco.
297  DECLARE_BOX_METHODS(ChunkOffset);
298 };
299 
300 // stss. Optional.
301 struct SyncSample : FullBox {
302  DECLARE_BOX_METHODS(SyncSample);
303 
304  std::vector<uint32_t> sample_number;
305 };
306 
307 struct SampleTable : Box {
308  DECLARE_BOX_METHODS(SampleTable);
309 
310  SampleDescription description;
311  DecodingTimeToSample decoding_time_to_sample;
312  CompositionTimeToSample composition_time_to_sample;
313  SampleToChunk sample_to_chunk;
314  // Either SampleSize or CompactSampleSize must present. Store in SampleSize.
315  SampleSize sample_size;
316  // Either ChunkOffset or ChunkLargeOffset must present. Store in
317  // ChunkLargeOffset.
318  ChunkLargeOffset chunk_large_offset;
319  SyncSample sync_sample;
320 };
321 
323  DECLARE_BOX_METHODS(MediaHeader);
324 
325  uint64_t creation_time;
326  uint64_t modification_time;
327  uint32_t timescale;
328  uint64_t duration;
329  // 3-char language code + 1 null terminating char.
330  char language[4];
331 };
332 
334  DECLARE_BOX_METHODS(VideoMediaHeader);
335 
336  uint16_t graphicsmode;
337  uint16_t opcolor_red;
338  uint16_t opcolor_green;
339  uint16_t opcolor_blue;
340 };
341 
343  DECLARE_BOX_METHODS(SoundMediaHeader);
344 
345  uint16_t balance;
346 };
347 
349  DECLARE_BOX_METHODS(DataEntryUrl);
350 
351  std::vector<uint8_t> location;
352 };
353 
355  DECLARE_BOX_METHODS(DataReference);
356 
357  // data entry can be either url or urn box. Fix to url box for now.
358  std::vector<DataEntryUrl> data_entry;
359 };
360 
362  DECLARE_BOX_METHODS(DataInformation);
363 
364  DataReference dref;
365 };
366 
368  DECLARE_BOX_METHODS(MediaInformation);
369 
370  DataInformation dinf;
371  SampleTable sample_table;
372  // Exactly one specific meida header shall be present, vmhd, smhd, hmhd, nmhd.
373  VideoMediaHeader vmhd;
374  SoundMediaHeader smhd;
375 };
376 
377 struct Media : Box {
378  DECLARE_BOX_METHODS(Media);
379 
380  MediaHeader header;
381  HandlerReference handler;
382  MediaInformation information;
383 };
384 
385 struct Track : Box {
386  DECLARE_BOX_METHODS(Track);
387 
388  TrackHeader header;
389  Media media;
390  Edit edit;
391 };
392 
394  DECLARE_BOX_METHODS(MovieExtendsHeader);
395 
396  uint64_t fragment_duration;
397 };
398 
400  DECLARE_BOX_METHODS(TrackExtends);
401 
402  uint32_t track_id;
403  uint32_t default_sample_description_index;
404  uint32_t default_sample_duration;
405  uint32_t default_sample_size;
406  uint32_t default_sample_flags;
407 };
408 
409 struct MovieExtends : Box {
410  DECLARE_BOX_METHODS(MovieExtends);
411 
412  MovieExtendsHeader header;
413  std::vector<TrackExtends> tracks;
414 };
415 
416 struct Movie : Box {
417  DECLARE_BOX_METHODS(Movie);
418 
419  MovieHeader header;
420  MovieExtends extends;
421  std::vector<Track> tracks;
422  std::vector<ProtectionSystemSpecificHeader> pssh;
423 };
424 
426  DECLARE_BOX_METHODS(TrackFragmentDecodeTime);
427 
428  uint64_t decode_time;
429 };
430 
432  DECLARE_BOX_METHODS(MovieFragmentHeader);
433 
434  uint32_t sequence_number;
435 };
436 
438  enum TrackFragmentFlagsMasks {
439  kBaseDataOffsetPresentMask = 0x000001,
440  kSampleDescriptionIndexPresentMask = 0x000002,
441  kDefaultSampleDurationPresentMask = 0x000008,
442  kDefaultSampleSizePresentMask = 0x000010,
443  kDefaultSampleFlagsPresentMask = 0x000020,
444  kDurationIsEmptyMask = 0x010000,
445  kDefaultBaseIsMoofMask = 0x020000,
446  };
447 
448  enum SampleFlagsMasks {
449  kReservedMask = 0xFC000000,
450  kSampleDependsOnMask = 0x03000000,
451  kSampleIsDependedOnMask = 0x00C00000,
452  kSampleHasRedundancyMask = 0x00300000,
453  kSamplePaddingValueMask = 0x000E0000,
454  kNonKeySampleMask = 0x00010000,
455  kSampleDegradationPriorityMask = 0x0000FFFF,
456  };
457 
458  DECLARE_BOX_METHODS(TrackFragmentHeader);
459 
460  uint32_t track_id;
461  uint32_t sample_description_index;
462  uint32_t default_sample_duration;
463  uint32_t default_sample_size;
464  uint32_t default_sample_flags;
465 };
466 
468  enum TrackFragmentFlagsMasks {
469  kDataOffsetPresentMask = 0x000001,
470  kFirstSampleFlagsPresentMask = 0x000004,
471  kSampleDurationPresentMask = 0x000100,
472  kSampleSizePresentMask = 0x000200,
473  kSampleFlagsPresentMask = 0x000400,
474  kSampleCompTimeOffsetsPresentMask = 0x000800,
475  };
476 
477  DECLARE_BOX_METHODS(TrackFragmentRun);
478 
479  uint32_t sample_count;
480  uint32_t data_offset;
481  std::vector<uint32_t> sample_flags;
482  std::vector<uint32_t> sample_sizes;
483  std::vector<uint32_t> sample_durations;
484  std::vector<int64_t> sample_composition_time_offsets;
485 };
486 
488  enum GroupDescriptionIndexBase {
489  kTrackGroupDescriptionIndexBase = 0,
490  kTrackFragmentGroupDescriptionIndexBase = 0x10000,
491  };
492 
493  uint32_t sample_count;
494  uint32_t group_description_index;
495 };
496 
498  DECLARE_BOX_METHODS(SampleToGroup);
499 
500  uint32_t grouping_type;
501  uint32_t grouping_type_parameter; // Version 1 only.
502  std::vector<SampleToGroupEntry> entries;
503 };
504 
508 
509  bool is_encrypted;
510  uint8_t iv_size;
511  std::vector<uint8_t> key_id;
512 };
513 
515  DECLARE_BOX_METHODS(SampleGroupDescription);
516 
517  uint32_t grouping_type;
518  std::vector<CencSampleEncryptionInfoEntry> entries;
519 };
520 
521 struct TrackFragment : Box {
522  DECLARE_BOX_METHODS(TrackFragment);
523 
524  TrackFragmentHeader header;
525  std::vector<TrackFragmentRun> runs;
526  bool decode_time_absent;
527  TrackFragmentDecodeTime decode_time;
528  SampleToGroup sample_to_group;
529  SampleGroupDescription sample_group_description;
530  SampleAuxiliaryInformationSize auxiliary_size;
531  SampleAuxiliaryInformationOffset auxiliary_offset;
532 };
533 
534 struct MovieFragment : Box {
535  DECLARE_BOX_METHODS(MovieFragment);
536 
537  MovieFragmentHeader header;
538  std::vector<TrackFragment> tracks;
539  std::vector<ProtectionSystemSpecificHeader> pssh;
540 };
541 
543  enum SAPType {
544  TypeUnknown = 0,
545  Type1 = 1, // T(ept) = T(dec) = T(sap) = T(ptf)
546  Type2 = 2, // T(ept) = T(dec) = T(sap) < T(ptf)
547  Type3 = 3, // T(ept) < T(dec) = T(sap) <= T(ptf)
548  Type4 = 4, // T(ept) <= T(ptf) < T(dec) = T(sap)
549  Type5 = 5, // T(ept) = T(dec) < T(sap)
550  Type6 = 6, // T(ept) < T(dec) < T(sap)
551  };
552 
553  bool reference_type;
554  uint32_t referenced_size;
555  uint32_t subsegment_duration;
556  bool starts_with_sap;
557  SAPType sap_type;
558  uint32_t sap_delta_time;
559  // We add this field to keep track of earliest_presentation_time in this
560  // subsegment. It is not part of SegmentReference.
561  uint64_t earliest_presentation_time;
562 };
563 
565  DECLARE_BOX_METHODS(SegmentIndex);
566 
567  uint32_t reference_id;
568  uint32_t timescale;
569  uint64_t earliest_presentation_time;
570  uint64_t first_offset;
571  std::vector<SegmentReference> references;
572 };
573 
574 // The actual data is parsed and written separately, so we do not inherit it
575 // from Box.
576 struct MediaData {
577  MediaData();
578  ~MediaData();
579  void Write(BufferWriter* buffer_writer);
580  uint32_t ComputeSize();
581  FourCC BoxType() const;
582 
583  uint32_t data_size;
584 };
585 
586 #undef DECLARE_BOX
587 
588 } // namespace mp4
589 } // namespace media
590 } // namespace edash_packager
591 
592 #endif // MEDIA_FORMATS_MP4_BOX_DEFINITIONS_H_