1// Copyright 2014 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 <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/compiler_specific.h"
13#include "media/base/media_export.h"
14#include "media/formats/mp4/aac.h"
15#include "media/formats/mp4/avc.h"
16#include "media/formats/mp4/box_reader.h"
17#include "media/formats/mp4/fourccs.h"
18
19namespace media {
20namespace mp4 {
21
22enum TrackType {
23  kInvalid = 0,
24  kVideo,
25  kAudio,
26  kHint
27};
28
29enum SampleFlags {
30  kSampleIsNonSyncSample = 0x10000
31};
32
33#define DECLARE_BOX_METHODS(T) \
34  T(); \
35  virtual ~T(); \
36  virtual bool Parse(BoxReader* reader) OVERRIDE; \
37  virtual FourCC BoxType() const OVERRIDE; \
38
39struct MEDIA_EXPORT FileType : Box {
40  DECLARE_BOX_METHODS(FileType);
41
42  FourCC major_brand;
43  uint32 minor_version;
44};
45
46struct MEDIA_EXPORT ProtectionSystemSpecificHeader : Box {
47  DECLARE_BOX_METHODS(ProtectionSystemSpecificHeader);
48
49  std::vector<uint8> system_id;
50  std::vector<uint8> raw_box;
51};
52
53struct MEDIA_EXPORT SampleAuxiliaryInformationOffset : Box {
54  DECLARE_BOX_METHODS(SampleAuxiliaryInformationOffset);
55
56  std::vector<uint64> offsets;
57};
58
59struct MEDIA_EXPORT SampleAuxiliaryInformationSize : Box {
60  DECLARE_BOX_METHODS(SampleAuxiliaryInformationSize);
61
62  uint8 default_sample_info_size;
63  uint32 sample_count;
64  std::vector<uint8> sample_info_sizes;
65};
66
67struct MEDIA_EXPORT OriginalFormat : Box {
68  DECLARE_BOX_METHODS(OriginalFormat);
69
70  FourCC format;
71};
72
73struct MEDIA_EXPORT SchemeType : Box {
74  DECLARE_BOX_METHODS(SchemeType);
75
76  FourCC type;
77  uint32 version;
78};
79
80struct MEDIA_EXPORT TrackEncryption : Box {
81  DECLARE_BOX_METHODS(TrackEncryption);
82
83  // Note: this definition is specific to the CENC protection type.
84  bool is_encrypted;
85  uint8 default_iv_size;
86  std::vector<uint8> default_kid;
87};
88
89struct MEDIA_EXPORT SchemeInfo : Box {
90  DECLARE_BOX_METHODS(SchemeInfo);
91
92  TrackEncryption track_encryption;
93};
94
95struct MEDIA_EXPORT ProtectionSchemeInfo : Box {
96  DECLARE_BOX_METHODS(ProtectionSchemeInfo);
97
98  OriginalFormat format;
99  SchemeType type;
100  SchemeInfo info;
101};
102
103struct MEDIA_EXPORT MovieHeader : Box {
104  DECLARE_BOX_METHODS(MovieHeader);
105
106  uint64 creation_time;
107  uint64 modification_time;
108  uint32 timescale;
109  uint64 duration;
110  int32 rate;
111  int16 volume;
112  uint32 next_track_id;
113};
114
115struct MEDIA_EXPORT TrackHeader : Box {
116  DECLARE_BOX_METHODS(TrackHeader);
117
118  uint64 creation_time;
119  uint64 modification_time;
120  uint32 track_id;
121  uint64 duration;
122  int16 layer;
123  int16 alternate_group;
124  int16 volume;
125  uint32 width;
126  uint32 height;
127};
128
129struct MEDIA_EXPORT EditListEntry {
130  uint64 segment_duration;
131  int64 media_time;
132  int16 media_rate_integer;
133  int16 media_rate_fraction;
134};
135
136struct MEDIA_EXPORT EditList : Box {
137  DECLARE_BOX_METHODS(EditList);
138
139  std::vector<EditListEntry> edits;
140};
141
142struct MEDIA_EXPORT Edit : Box {
143  DECLARE_BOX_METHODS(Edit);
144
145  EditList list;
146};
147
148struct MEDIA_EXPORT HandlerReference : Box {
149  DECLARE_BOX_METHODS(HandlerReference);
150
151  TrackType type;
152};
153
154struct MEDIA_EXPORT AVCDecoderConfigurationRecord : Box {
155  DECLARE_BOX_METHODS(AVCDecoderConfigurationRecord);
156
157  // Parses AVCDecoderConfigurationRecord data encoded in |data|.
158  // Note: This method is intended to parse data outside the MP4StreamParser
159  //       context and therefore the box header is not expected to be present
160  //       in |data|.
161  // Returns true if |data| was successfully parsed.
162  bool Parse(const uint8* data, int data_size);
163
164  uint8 version;
165  uint8 profile_indication;
166  uint8 profile_compatibility;
167  uint8 avc_level;
168  uint8 length_size;
169
170  typedef std::vector<uint8> SPS;
171  typedef std::vector<uint8> PPS;
172
173  std::vector<SPS> sps_list;
174  std::vector<PPS> pps_list;
175
176 private:
177  bool ParseInternal(BufferReader* reader, const LogCB& log_cb);
178};
179
180struct MEDIA_EXPORT PixelAspectRatioBox : Box {
181  DECLARE_BOX_METHODS(PixelAspectRatioBox);
182
183  uint32 h_spacing;
184  uint32 v_spacing;
185};
186
187struct MEDIA_EXPORT VideoSampleEntry : Box {
188  DECLARE_BOX_METHODS(VideoSampleEntry);
189
190  FourCC format;
191  uint16 data_reference_index;
192  uint16 width;
193  uint16 height;
194
195  PixelAspectRatioBox pixel_aspect;
196  ProtectionSchemeInfo sinf;
197
198  // Currently expected to be present regardless of format.
199  AVCDecoderConfigurationRecord avcc;
200
201  bool IsFormatValid() const;
202};
203
204struct MEDIA_EXPORT ElementaryStreamDescriptor : Box {
205  DECLARE_BOX_METHODS(ElementaryStreamDescriptor);
206
207  uint8 object_type;
208  AAC aac;
209};
210
211struct MEDIA_EXPORT AudioSampleEntry : Box {
212  DECLARE_BOX_METHODS(AudioSampleEntry);
213
214  FourCC format;
215  uint16 data_reference_index;
216  uint16 channelcount;
217  uint16 samplesize;
218  uint32 samplerate;
219
220  ProtectionSchemeInfo sinf;
221  ElementaryStreamDescriptor esds;
222};
223
224struct MEDIA_EXPORT SampleDescription : Box {
225  DECLARE_BOX_METHODS(SampleDescription);
226
227  TrackType type;
228  std::vector<VideoSampleEntry> video_entries;
229  std::vector<AudioSampleEntry> audio_entries;
230};
231
232struct MEDIA_EXPORT SyncSample : Box {
233  DECLARE_BOX_METHODS(SyncSample);
234
235  // Returns true if the |k|th sample is a sync sample (aka a random
236  // access point). Returns false if sample |k| is not a sync sample.
237  bool IsSyncSample(size_t k) const;
238
239  bool is_present;
240  std::vector<uint32> entries;
241};
242
243struct MEDIA_EXPORT SampleTable : Box {
244  DECLARE_BOX_METHODS(SampleTable);
245
246  // Media Source specific: we ignore many of the sub-boxes in this box,
247  // including some that are required to be present in the BMFF spec. This
248  // includes the 'stts', 'stsc', and 'stco' boxes, which must contain no
249  // samples in order to be compliant files.
250  SampleDescription description;
251  SyncSample sync_sample;
252};
253
254struct MEDIA_EXPORT MediaHeader : Box {
255  DECLARE_BOX_METHODS(MediaHeader);
256
257  uint64 creation_time;
258  uint64 modification_time;
259  uint32 timescale;
260  uint64 duration;
261};
262
263struct MEDIA_EXPORT MediaInformation : Box {
264  DECLARE_BOX_METHODS(MediaInformation);
265
266  SampleTable sample_table;
267};
268
269struct MEDIA_EXPORT Media : Box {
270  DECLARE_BOX_METHODS(Media);
271
272  MediaHeader header;
273  HandlerReference handler;
274  MediaInformation information;
275};
276
277struct MEDIA_EXPORT Track : Box {
278  DECLARE_BOX_METHODS(Track);
279
280  TrackHeader header;
281  Media media;
282  Edit edit;
283};
284
285struct MEDIA_EXPORT MovieExtendsHeader : Box {
286  DECLARE_BOX_METHODS(MovieExtendsHeader);
287
288  uint64 fragment_duration;
289};
290
291struct MEDIA_EXPORT TrackExtends : Box {
292  DECLARE_BOX_METHODS(TrackExtends);
293
294  uint32 track_id;
295  uint32 default_sample_description_index;
296  uint32 default_sample_duration;
297  uint32 default_sample_size;
298  uint32 default_sample_flags;
299};
300
301struct MEDIA_EXPORT MovieExtends : Box {
302  DECLARE_BOX_METHODS(MovieExtends);
303
304  MovieExtendsHeader header;
305  std::vector<TrackExtends> tracks;
306};
307
308struct MEDIA_EXPORT Movie : Box {
309  DECLARE_BOX_METHODS(Movie);
310
311  bool fragmented;
312  MovieHeader header;
313  MovieExtends extends;
314  std::vector<Track> tracks;
315  std::vector<ProtectionSystemSpecificHeader> pssh;
316};
317
318struct MEDIA_EXPORT TrackFragmentDecodeTime : Box {
319  DECLARE_BOX_METHODS(TrackFragmentDecodeTime);
320
321  uint64 decode_time;
322};
323
324struct MEDIA_EXPORT MovieFragmentHeader : Box {
325  DECLARE_BOX_METHODS(MovieFragmentHeader);
326
327  uint32 sequence_number;
328};
329
330struct MEDIA_EXPORT TrackFragmentHeader : Box {
331  DECLARE_BOX_METHODS(TrackFragmentHeader);
332
333  uint32 track_id;
334
335  uint32 sample_description_index;
336  uint32 default_sample_duration;
337  uint32 default_sample_size;
338  uint32 default_sample_flags;
339
340  // As 'flags' might be all zero, we cannot use zeroness alone to identify
341  // when default_sample_flags wasn't specified, unlike the other values.
342  bool has_default_sample_flags;
343};
344
345struct MEDIA_EXPORT TrackFragmentRun : Box {
346  DECLARE_BOX_METHODS(TrackFragmentRun);
347
348  uint32 sample_count;
349  uint32 data_offset;
350  std::vector<uint32> sample_flags;
351  std::vector<uint32> sample_sizes;
352  std::vector<uint32> sample_durations;
353  std::vector<int32> sample_composition_time_offsets;
354};
355
356// sample_depends_on values in ISO/IEC 14496-12 Section 8.40.2.3.
357enum SampleDependsOn {
358  kSampleDependsOnUnknown = 0,
359  kSampleDependsOnOthers = 1,
360  kSampleDependsOnNoOther = 2,
361  kSampleDependsOnReserved = 3,
362};
363
364class MEDIA_EXPORT IndependentAndDisposableSamples : public Box {
365 public:
366  DECLARE_BOX_METHODS(IndependentAndDisposableSamples);
367
368  // Returns the SampleDependsOn value for the |i|'th value
369  // in the track. If no data was parsed for the |i|'th sample,
370  // then |kSampleDependsOnUnknown| is returned.
371  SampleDependsOn sample_depends_on(size_t i) const;
372
373 private:
374  std::vector<SampleDependsOn> sample_depends_on_;
375};
376
377struct MEDIA_EXPORT CencSampleEncryptionInfoEntry {
378  CencSampleEncryptionInfoEntry();
379  ~CencSampleEncryptionInfoEntry();
380
381  bool is_encrypted;
382  uint8 iv_size;
383  std::vector<uint8> key_id;
384};
385
386struct MEDIA_EXPORT SampleGroupDescription : Box {  // 'sgpd'.
387  DECLARE_BOX_METHODS(SampleGroupDescription);
388
389  uint32 grouping_type;
390  std::vector<CencSampleEncryptionInfoEntry> entries;
391};
392
393struct MEDIA_EXPORT SampleToGroupEntry {
394  enum GroupDescriptionIndexBase {
395    kTrackGroupDescriptionIndexBase = 0,
396    kFragmentGroupDescriptionIndexBase = 0x10000,
397  };
398
399  uint32 sample_count;
400  uint32 group_description_index;
401};
402
403struct MEDIA_EXPORT SampleToGroup : Box {  // 'sbgp'.
404  DECLARE_BOX_METHODS(SampleToGroup);
405
406  uint32 grouping_type;
407  uint32 grouping_type_parameter;  // Version 1 only.
408  std::vector<SampleToGroupEntry> entries;
409};
410
411struct MEDIA_EXPORT TrackFragment : Box {
412  DECLARE_BOX_METHODS(TrackFragment);
413
414  TrackFragmentHeader header;
415  std::vector<TrackFragmentRun> runs;
416  TrackFragmentDecodeTime decode_time;
417  SampleAuxiliaryInformationOffset auxiliary_offset;
418  SampleAuxiliaryInformationSize auxiliary_size;
419  IndependentAndDisposableSamples sdtp;
420  SampleGroupDescription sample_group_description;
421  SampleToGroup sample_to_group;
422};
423
424struct MEDIA_EXPORT MovieFragment : Box {
425  DECLARE_BOX_METHODS(MovieFragment);
426
427  MovieFragmentHeader header;
428  std::vector<TrackFragment> tracks;
429  std::vector<ProtectionSystemSpecificHeader> pssh;
430};
431
432#undef DECLARE_BOX
433
434}  // namespace mp4
435}  // namespace media
436
437#endif  // MEDIA_FORMATS_MP4_BOX_DEFINITIONS_H_
438