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