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