MetaData.h revision f8bd851cc652501239a8e3759b21890dfa2afdea
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    kKeyColorFormat       = 'colf',
74    kKeyPlatformPrivate   = 'priv',  // pointer
75    kKeyDecoderComponent  = 'decC',  // cstring
76    kKeyBufferID          = 'bfID',
77    kKeyMaxInputSize      = 'inpS',
78    kKeyMaxWidth          = 'maxW',
79    kKeyMaxHeight         = 'maxH',
80    kKeyThumbnailTime     = 'thbT',  // int64_t (usecs)
81    kKeyTrackID           = 'trID',
82    kKeyIsDRM             = 'idrm',  // int32_t (bool)
83    kKeyEncoderDelay      = 'encd',  // int32_t (frames)
84    kKeyEncoderPadding    = 'encp',  // int32_t (frames)
85
86    kKeyAlbum             = 'albu',  // cstring
87    kKeyArtist            = 'arti',  // cstring
88    kKeyAlbumArtist       = 'aart',  // cstring
89    kKeyComposer          = 'comp',  // cstring
90    kKeyGenre             = 'genr',  // cstring
91    kKeyTitle             = 'titl',  // cstring
92    kKeyYear              = 'year',  // cstring
93    kKeyAlbumArt          = 'albA',  // compressed image data
94    kKeyAlbumArtMIME      = 'alAM',  // cstring
95    kKeyAuthor            = 'auth',  // cstring
96    kKeyCDTrackNumber     = 'cdtr',  // cstring
97    kKeyDiscNumber        = 'dnum',  // cstring
98    kKeyDate              = 'date',  // cstring
99    kKeyWriter            = 'writ',  // cstring
100    kKeyCompilation       = 'cpil',  // cstring
101    kKeyLocation          = 'loc ',  // cstring
102    kKeyTimeScale         = 'tmsl',  // int32_t
103    kKeyCaptureFramerate  = 'capF',  // float (capture fps)
104
105    // video profile and level
106    kKeyVideoProfile      = 'vprf',  // int32_t
107    kKeyVideoLevel        = 'vlev',  // int32_t
108
109    // Set this key to enable authoring files in 64-bit offset
110    kKey64BitFileOffset   = 'fobt',  // int32_t (bool)
111    kKey2ByteNalLength    = '2NAL',  // int32_t (bool)
112
113    // Identify the file output format for authoring
114    // Please see <media/mediarecorder.h> for the supported
115    // file output formats.
116    kKeyFileType          = 'ftyp',  // int32_t
117
118    // Track authoring progress status
119    // kKeyTrackTimeStatus is used to track progress in elapsed time
120    kKeyTrackTimeStatus   = 'tktm',  // int64_t
121
122    kKeyRealTimeRecording = 'rtrc',  // bool (int32_t)
123    kKeyNumBuffers        = 'nbbf',  // int32_t
124
125    // Ogg files can be tagged to be automatically looping...
126    kKeyAutoLoop          = 'autL',  // bool (int32_t)
127
128    kKeyValidSamples      = 'valD',  // int32_t
129
130    kKeyIsUnreadable      = 'unre',  // bool (int32_t)
131
132    // An indication that a video buffer has been rendered.
133    kKeyRendered          = 'rend',  // bool (int32_t)
134
135    // The language code for this media
136    kKeyMediaLanguage     = 'lang',  // cstring
137
138    // To store the timed text format data
139    kKeyTextFormatData    = 'text',  // raw data
140
141    kKeyRequiresSecureBuffers = 'secu',  // bool (int32_t)
142
143    kKeyIsADTS            = 'adts',  // bool (int32_t)
144    kKeyAACAOT            = 'aaot',  // int32_t
145
146    // If a MediaBuffer's data represents (at least partially) encrypted
147    // data, the following fields aid in decryption.
148    // The data can be thought of as pairs of plain and encrypted data
149    // fragments, i.e. plain and encrypted data alternate.
150    // The first fragment is by convention plain data (if that's not the
151    // case, simply specify plain fragment size of 0).
152    // kKeyEncryptedSizes and kKeyPlainSizes each map to an array of
153    // size_t values. The sum total of all size_t values of both arrays
154    // must equal the amount of data (i.e. MediaBuffer's range_length()).
155    // If both arrays are present, they must be of the same size.
156    // If only encrypted sizes are present it is assumed that all
157    // plain sizes are 0, i.e. all fragments are encrypted.
158    // To programmatically set these array, use the MetaData::setData API, i.e.
159    // const size_t encSizes[];
160    // meta->setData(
161    //  kKeyEncryptedSizes, 0 /* type */, encSizes, sizeof(encSizes));
162    // A plain sizes array by itself makes no sense.
163    kKeyEncryptedSizes    = 'encr',  // size_t[]
164    kKeyPlainSizes        = 'plai',  // size_t[]
165    kKeyCryptoKey         = 'cryK',  // uint8_t[16]
166    kKeyCryptoIV          = 'cryI',  // uint8_t[16]
167    kKeyCryptoMode        = 'cryM',  // int32_t
168
169    kKeyCryptoDefaultIVSize = 'cryS',  // int32_t
170
171    kKeyPssh              = 'pssh',  // raw data
172
173    // Please see MediaFormat.KEY_IS_AUTOSELECT.
174    kKeyTrackIsAutoselect = 'auto', // bool (int32_t)
175    // Please see MediaFormat.KEY_IS_DEFAULT.
176    kKeyTrackIsDefault    = 'dflt', // bool (int32_t)
177    // Similar to MediaFormat.KEY_IS_FORCED_SUBTITLE but pertains to av tracks as well.
178    kKeyTrackIsForced     = 'frcd', // bool (int32_t)
179
180    // H264 supplemental enhancement information offsets/sizes
181    kKeySEI               = 'sei ', // raw data
182};
183
184enum {
185    kTypeESDS        = 'esds',
186    kTypeAVCC        = 'avcc',
187    kTypeHVCC        = 'hvcc',
188    kTypeD263        = 'd263',
189};
190
191class MetaData : public RefBase {
192public:
193    MetaData();
194    MetaData(const MetaData &from);
195
196    enum Type {
197        TYPE_NONE     = 'none',
198        TYPE_C_STRING = 'cstr',
199        TYPE_INT32    = 'in32',
200        TYPE_INT64    = 'in64',
201        TYPE_FLOAT    = 'floa',
202        TYPE_POINTER  = 'ptr ',
203        TYPE_RECT     = 'rect',
204    };
205
206    void clear();
207    bool remove(uint32_t key);
208
209    bool setCString(uint32_t key, const char *value);
210    bool setInt32(uint32_t key, int32_t value);
211    bool setInt64(uint32_t key, int64_t value);
212    bool setFloat(uint32_t key, float value);
213    bool setPointer(uint32_t key, void *value);
214
215    bool setRect(
216            uint32_t key,
217            int32_t left, int32_t top,
218            int32_t right, int32_t bottom);
219
220    bool findCString(uint32_t key, const char **value);
221    bool findInt32(uint32_t key, int32_t *value);
222    bool findInt64(uint32_t key, int64_t *value);
223    bool findFloat(uint32_t key, float *value);
224    bool findPointer(uint32_t key, void **value);
225
226    bool findRect(
227            uint32_t key,
228            int32_t *left, int32_t *top,
229            int32_t *right, int32_t *bottom);
230
231    bool setData(uint32_t key, uint32_t type, const void *data, size_t size);
232
233    bool findData(uint32_t key, uint32_t *type,
234                  const void **data, size_t *size) const;
235
236    bool hasData(uint32_t key) const;
237
238    void dumpToLog() const;
239
240protected:
241    virtual ~MetaData();
242
243private:
244    struct typed_data {
245        typed_data();
246        ~typed_data();
247
248        typed_data(const MetaData::typed_data &);
249        typed_data &operator=(const MetaData::typed_data &);
250
251        void clear();
252        void setData(uint32_t type, const void *data, size_t size);
253        void getData(uint32_t *type, const void **data, size_t *size) const;
254        String8 asString() const;
255
256    private:
257        uint32_t mType;
258        size_t mSize;
259
260        union {
261            void *ext_data;
262            float reservoir;
263        } u;
264
265        bool usesReservoir() const {
266            return mSize <= sizeof(u.reservoir);
267        }
268
269        void allocateStorage(size_t size);
270        void freeStorage();
271
272        void *storage() {
273            return usesReservoir() ? &u.reservoir : u.ext_data;
274        }
275
276        const void *storage() const {
277            return usesReservoir() ? &u.reservoir : u.ext_data;
278        }
279    };
280
281    struct Rect {
282        int32_t mLeft, mTop, mRight, mBottom;
283    };
284
285    KeyedVector<uint32_t, typed_data> mItems;
286
287    // MetaData &operator=(const MetaData &);
288};
289
290}  // namespace android
291
292#endif  // META_DATA_H_
293