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