Home | History | Annotate | Download | only in stagefright
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef META_DATA_H_
     18 
     19 #define META_DATA_H_
     20 
     21 #include <sys/types.h>
     22 
     23 #include <stdint.h>
     24 
     25 #include <binder/Parcel.h>
     26 #include <utils/RefBase.h>
     27 #include <utils/KeyedVector.h>
     28 #include <utils/String8.h>
     29 
     30 namespace android {
     31 
     32 // The following keys map to int32_t data unless indicated otherwise.
     33 enum {
     34     kKeyMIMEType          = 'mime',  // cstring
     35     kKeyWidth             = 'widt',  // int32_t, image pixel
     36     kKeyHeight            = 'heig',  // int32_t, image pixel
     37     kKeyDisplayWidth      = 'dWid',  // int32_t, display/presentation
     38     kKeyDisplayHeight     = 'dHgt',  // int32_t, display/presentation
     39     kKeySARWidth          = 'sarW',  // int32_t, sampleAspectRatio width
     40     kKeySARHeight         = 'sarH',  // int32_t, sampleAspectRatio height
     41 
     42     // a rectangle, if absent assumed to be (0, 0, width - 1, height - 1)
     43     kKeyCropRect          = 'crop',
     44 
     45     kKeyRotation          = 'rotA',  // int32_t (angle in degrees)
     46     kKeyIFramesInterval   = 'ifiv',  // int32_t
     47     kKeyStride            = 'strd',  // int32_t
     48     kKeySliceHeight       = 'slht',  // int32_t
     49     kKeyChannelCount      = '#chn',  // int32_t
     50     kKeyChannelMask       = 'chnm',  // int32_t
     51     kKeySampleRate        = 'srte',  // int32_t (audio sampling rate Hz)
     52     kKeyPcmEncoding       = 'PCMe',  // int32_t (audio encoding enum)
     53     kKeyFrameRate         = 'frmR',  // int32_t (video frame rate fps)
     54     kKeyBitRate           = 'brte',  // int32_t (bps)
     55     kKeyMaxBitRate        = 'mxBr',  // int32_t (bps)
     56     kKeyStreamHeader      = 'stHd',  // raw data
     57     kKeyESDS              = 'esds',  // raw data
     58     kKeyAACProfile        = 'aacp',  // int32_t
     59     kKeyAVCC              = 'avcc',  // raw data
     60     kKeyHVCC              = 'hvcc',  // raw data
     61     kKeyD263              = 'd263',  // raw data
     62     kKeyVorbisInfo        = 'vinf',  // raw data
     63     kKeyVorbisBooks       = 'vboo',  // raw data
     64     kKeyOpusHeader        = 'ohdr',  // raw data
     65     kKeyOpusCodecDelay    = 'ocod',  // uint64_t (codec delay in ns)
     66     kKeyOpusSeekPreRoll   = 'ospr',  // uint64_t (seek preroll in ns)
     67     kKeyVp9CodecPrivate   = 'vp9p',  // raw data (vp9 csd information)
     68     kKeyWantsNALFragments = 'NALf',
     69     kKeyIsSyncFrame       = 'sync',  // int32_t (bool)
     70     kKeyIsCodecConfig     = 'conf',  // int32_t (bool)
     71     kKeyTime              = 'time',  // int64_t (usecs)
     72     kKeyDecodingTime      = 'decT',  // int64_t (decoding timestamp in usecs)
     73     kKeyNTPTime           = 'ntpT',  // uint64_t (ntp-timestamp)
     74     kKeyTargetTime        = 'tarT',  // int64_t (usecs)
     75     kKeyDriftTime         = 'dftT',  // int64_t (usecs)
     76     kKeyAnchorTime        = 'ancT',  // int64_t (usecs)
     77     kKeyDuration          = 'dura',  // int64_t (usecs)
     78     kKeyPixelFormat       = 'pixf',  // int32_t
     79     kKeyColorFormat       = 'colf',  // int32_t
     80     kKeyColorSpace        = 'cols',  // int32_t
     81     kKeyPlatformPrivate   = 'priv',  // pointer
     82     kKeyDecoderComponent  = 'decC',  // cstring
     83     kKeyBufferID          = 'bfID',
     84     kKeyMaxInputSize      = 'inpS',
     85     kKeyMaxWidth          = 'maxW',
     86     kKeyMaxHeight         = 'maxH',
     87     kKeyThumbnailTime     = 'thbT',  // int64_t (usecs)
     88     kKeyTrackID           = 'trID',
     89     kKeyIsDRM             = 'idrm',  // int32_t (bool)
     90     kKeyEncoderDelay      = 'encd',  // int32_t (frames)
     91     kKeyEncoderPadding    = 'encp',  // int32_t (frames)
     92 
     93     kKeyAlbum             = 'albu',  // cstring
     94     kKeyArtist            = 'arti',  // cstring
     95     kKeyAlbumArtist       = 'aart',  // cstring
     96     kKeyComposer          = 'comp',  // cstring
     97     kKeyGenre             = 'genr',  // cstring
     98     kKeyTitle             = 'titl',  // cstring
     99     kKeyYear              = 'year',  // cstring
    100     kKeyAlbumArt          = 'albA',  // compressed image data
    101     kKeyAlbumArtMIME      = 'alAM',  // cstring
    102     kKeyAuthor            = 'auth',  // cstring
    103     kKeyCDTrackNumber     = 'cdtr',  // cstring
    104     kKeyDiscNumber        = 'dnum',  // cstring
    105     kKeyDate              = 'date',  // cstring
    106     kKeyWriter            = 'writ',  // cstring
    107     kKeyCompilation       = 'cpil',  // cstring
    108     kKeyLocation          = 'loc ',  // cstring
    109     kKeyTimeScale         = 'tmsl',  // int32_t
    110     kKeyCaptureFramerate  = 'capF',  // float (capture fps)
    111 
    112     // video profile and level
    113     kKeyVideoProfile      = 'vprf',  // int32_t
    114     kKeyVideoLevel        = 'vlev',  // int32_t
    115 
    116     // Set this key to enable authoring files in 64-bit offset
    117     kKey64BitFileOffset   = 'fobt',  // int32_t (bool)
    118     kKey2ByteNalLength    = '2NAL',  // int32_t (bool)
    119 
    120     // Identify the file output format for authoring
    121     // Please see <media/mediarecorder.h> for the supported
    122     // file output formats.
    123     kKeyFileType          = 'ftyp',  // int32_t
    124 
    125     // Track authoring progress status
    126     // kKeyTrackTimeStatus is used to track progress in elapsed time
    127     kKeyTrackTimeStatus   = 'tktm',  // int64_t
    128 
    129     kKeyRealTimeRecording = 'rtrc',  // bool (int32_t)
    130     kKeyNumBuffers        = 'nbbf',  // int32_t
    131 
    132     // Ogg files can be tagged to be automatically looping...
    133     kKeyAutoLoop          = 'autL',  // bool (int32_t)
    134 
    135     kKeyValidSamples      = 'valD',  // int32_t
    136 
    137     kKeyIsUnreadable      = 'unre',  // bool (int32_t)
    138 
    139     // An indication that a video buffer has been rendered.
    140     kKeyRendered          = 'rend',  // bool (int32_t)
    141 
    142     // The language code for this media
    143     kKeyMediaLanguage     = 'lang',  // cstring
    144 
    145     // To store the timed text format data
    146     kKeyTextFormatData    = 'text',  // raw data
    147 
    148     kKeyRequiresSecureBuffers = 'secu',  // bool (int32_t)
    149 
    150     kKeyIsADTS            = 'adts',  // bool (int32_t)
    151     kKeyAACAOT            = 'aaot',  // int32_t
    152 
    153     // If a MediaBuffer's data represents (at least partially) encrypted
    154     // data, the following fields aid in decryption.
    155     // The data can be thought of as pairs of plain and encrypted data
    156     // fragments, i.e. plain and encrypted data alternate.
    157     // The first fragment is by convention plain data (if that's not the
    158     // case, simply specify plain fragment size of 0).
    159     // kKeyEncryptedSizes and kKeyPlainSizes each map to an array of
    160     // size_t values. The sum total of all size_t values of both arrays
    161     // must equal the amount of data (i.e. MediaBuffer's range_length()).
    162     // If both arrays are present, they must be of the same size.
    163     // If only encrypted sizes are present it is assumed that all
    164     // plain sizes are 0, i.e. all fragments are encrypted.
    165     // To programmatically set these array, use the MetaData::setData API, i.e.
    166     // const size_t encSizes[];
    167     // meta->setData(
    168     //  kKeyEncryptedSizes, 0 /* type */, encSizes, sizeof(encSizes));
    169     // A plain sizes array by itself makes no sense.
    170     kKeyEncryptedSizes    = 'encr',  // size_t[]
    171     kKeyPlainSizes        = 'plai',  // size_t[]
    172     kKeyCryptoKey         = 'cryK',  // uint8_t[16]
    173     kKeyCryptoIV          = 'cryI',  // uint8_t[16]
    174     kKeyCryptoMode        = 'cryM',  // int32_t
    175 
    176     kKeyCryptoDefaultIVSize = 'cryS',  // int32_t
    177 
    178     kKeyPssh              = 'pssh',  // raw data
    179 
    180     // Please see MediaFormat.KEY_IS_AUTOSELECT.
    181     kKeyTrackIsAutoselect = 'auto', // bool (int32_t)
    182     // Please see MediaFormat.KEY_IS_DEFAULT.
    183     kKeyTrackIsDefault    = 'dflt', // bool (int32_t)
    184     // Similar to MediaFormat.KEY_IS_FORCED_SUBTITLE but pertains to av tracks as well.
    185     kKeyTrackIsForced     = 'frcd', // bool (int32_t)
    186 
    187     // H264 supplemental enhancement information offsets/sizes
    188     kKeySEI               = 'sei ', // raw data
    189 
    190     // MPEG user data offsets
    191     kKeyMpegUserData      = 'mpud', // size_t[]
    192 
    193     // Size of NALU length in mkv/mp4
    194     kKeyNalLengthSize     = 'nals', // int32_t
    195 
    196     // HDR related
    197     kKeyHdrStaticInfo    = 'hdrS', // HDRStaticInfo
    198 
    199     // color aspects
    200     kKeyColorRange       = 'cRng', // int32_t, color range, value defined by ColorAspects.Range
    201     kKeyColorPrimaries   = 'cPrm', // int32_t,
    202                                    // color Primaries, value defined by ColorAspects.Primaries
    203     kKeyTransferFunction = 'tFun', // int32_t,
    204                                    // transfer Function, value defined by ColorAspects.Transfer.
    205     kKeyColorMatrix      = 'cMtx', // int32_t,
    206                                    // color Matrix, value defined by ColorAspects.MatrixCoeffs.
    207     kKeyTemporalLayerId  = 'iLyr', // int32_t, temporal layer-id. 0-based (0 => base layer)
    208     kKeyTemporalLayerCount = 'cLyr', // int32_t, number of temporal layers encoded
    209 };
    210 
    211 enum {
    212     kTypeESDS        = 'esds',
    213     kTypeAVCC        = 'avcc',
    214     kTypeHVCC        = 'hvcc',
    215     kTypeD263        = 'd263',
    216 };
    217 
    218 class MetaData : public RefBase {
    219 public:
    220     MetaData();
    221     MetaData(const MetaData &from);
    222 
    223     enum Type {
    224         TYPE_NONE     = 'none',
    225         TYPE_C_STRING = 'cstr',
    226         TYPE_INT32    = 'in32',
    227         TYPE_INT64    = 'in64',
    228         TYPE_FLOAT    = 'floa',
    229         TYPE_POINTER  = 'ptr ',
    230         TYPE_RECT     = 'rect',
    231     };
    232 
    233     void clear();
    234     bool remove(uint32_t key);
    235 
    236     bool setCString(uint32_t key, const char *value);
    237     bool setInt32(uint32_t key, int32_t value);
    238     bool setInt64(uint32_t key, int64_t value);
    239     bool setFloat(uint32_t key, float value);
    240     bool setPointer(uint32_t key, void *value);
    241 
    242     bool setRect(
    243             uint32_t key,
    244             int32_t left, int32_t top,
    245             int32_t right, int32_t bottom);
    246 
    247     bool findCString(uint32_t key, const char **value);
    248     bool findInt32(uint32_t key, int32_t *value);
    249     bool findInt64(uint32_t key, int64_t *value);
    250     bool findFloat(uint32_t key, float *value);
    251     bool findPointer(uint32_t key, void **value);
    252 
    253     bool findRect(
    254             uint32_t key,
    255             int32_t *left, int32_t *top,
    256             int32_t *right, int32_t *bottom);
    257 
    258     bool setData(uint32_t key, uint32_t type, const void *data, size_t size);
    259 
    260     bool findData(uint32_t key, uint32_t *type,
    261                   const void **data, size_t *size) const;
    262 
    263     bool hasData(uint32_t key) const;
    264 
    265     String8 toString() const;
    266     void dumpToLog() const;
    267 
    268     status_t writeToParcel(Parcel &parcel);
    269     status_t updateFromParcel(const Parcel &parcel);
    270     static sp<MetaData> createFromParcel(const Parcel &parcel);
    271 
    272 protected:
    273     virtual ~MetaData();
    274 
    275 private:
    276     struct typed_data {
    277         typed_data();
    278         ~typed_data();
    279 
    280         typed_data(const MetaData::typed_data &);
    281         typed_data &operator=(const MetaData::typed_data &);
    282 
    283         void clear();
    284         void setData(uint32_t type, const void *data, size_t size);
    285         void getData(uint32_t *type, const void **data, size_t *size) const;
    286         // may include hexdump of binary data if verbose=true
    287         String8 asString(bool verbose) const;
    288 
    289     private:
    290         uint32_t mType;
    291         size_t mSize;
    292 
    293         union {
    294             void *ext_data;
    295             float reservoir;
    296         } u;
    297 
    298         bool usesReservoir() const {
    299             return mSize <= sizeof(u.reservoir);
    300         }
    301 
    302         void *allocateStorage(size_t size);
    303         void freeStorage();
    304 
    305         void *storage() {
    306             return usesReservoir() ? &u.reservoir : u.ext_data;
    307         }
    308 
    309         const void *storage() const {
    310             return usesReservoir() ? &u.reservoir : u.ext_data;
    311         }
    312     };
    313 
    314     struct Rect {
    315         int32_t mLeft, mTop, mRight, mBottom;
    316     };
    317 
    318     KeyedVector<uint32_t, typed_data> mItems;
    319 
    320     // MetaData &operator=(const MetaData &);
    321 };
    322 
    323 }  // namespace android
    324 
    325 #endif  // META_DATA_H_
    326