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