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