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_BASE_H_
18 
19 #define META_DATA_BASE_H_
20 
21 #include <sys/types.h>
22 
23 #include <stdint.h>
24 
25 #include <utils/RefBase.h>
26 #include <utils/String8.h>
27 
28 namespace android {
29 
30 // The following keys map to int32_t data unless indicated otherwise.
31 enum {
32     kKeyMIMEType          = 'mime',  // cstring
33     kKeyWidth             = 'widt',  // int32_t, image pixel
34     kKeyHeight            = 'heig',  // int32_t, image pixel
35     kKeyDisplayWidth      = 'dWid',  // int32_t, display/presentation
36     kKeyDisplayHeight     = 'dHgt',  // int32_t, display/presentation
37     kKeySARWidth          = 'sarW',  // int32_t, sampleAspectRatio width
38     kKeySARHeight         = 'sarH',  // int32_t, sampleAspectRatio height
39     kKeyThumbnailWidth    = 'thbW',  // int32_t, thumbnail width
40     kKeyThumbnailHeight   = 'thbH',  // int32_t, thumbnail 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     kKeyMaxBitRate        = 'mxBr',  // int32_t (bps)
56     kKeyBitsPerSample     = 'bits',  // int32_t (bits per sample)
57     kKeyStreamHeader      = 'stHd',  // raw data
58     kKeyESDS              = 'esds',  // raw data
59     kKeyAACProfile        = 'aacp',  // int32_t
60     kKeyAVCC              = 'avcc',  // raw data
61     kKeyHVCC              = 'hvcc',  // raw data
62     kKeyDVCC              = 'dvcc',  // raw data
63     kKeyAV1C              = 'av1c',  // raw data
64     kKeyThumbnailHVCC     = 'thvc',  // raw data
65     kKeyD263              = 'd263',  // raw data
66     kKeyOpusHeader        = 'ohdr',  // raw data
67     kKeyOpusCodecDelay    = 'ocod',  // uint64_t (codec delay in ns)
68     kKeyOpusSeekPreRoll   = 'ospr',  // uint64_t (seek preroll in ns)
69     kKeyVp9CodecPrivate   = 'vp9p',  // raw data (vp9 csd information)
70     kKeyIsSyncFrame       = 'sync',  // int32_t (bool)
71     kKeyIsCodecConfig     = 'conf',  // int32_t (bool)
72     kKeyIsMuxerData       = 'muxd',  // int32_t (bool)
73     kKeyIsEndOfStream     = 'feos',  // int32_t (bool)
74     kKeyTime              = 'time',  // int64_t (usecs)
75     kKeyDecodingTime      = 'decT',  // int64_t (decoding timestamp in usecs)
76     kKeyNTPTime           = 'ntpT',  // uint64_t (ntp-timestamp)
77     kKeyTargetTime        = 'tarT',  // int64_t (usecs)
78     kKeyDriftTime         = 'dftT',  // int64_t (usecs)
79     kKeyAnchorTime        = 'ancT',  // int64_t (usecs)
80     kKeyDuration          = 'dura',  // int64_t (usecs)
81     kKeyPixelFormat       = 'pixf',  // int32_t
82     kKeyColorFormat       = 'colf',  // int32_t
83     kKeyColorSpace        = 'cols',  // int32_t
84     kKeyPlatformPrivate   = 'priv',  // pointer
85     kKeyDecoderComponent  = 'decC',  // cstring
86     kKeyBufferID          = 'bfID',
87     kKeyMaxInputSize      = 'inpS',
88     kKeyMaxWidth          = 'maxW',
89     kKeyMaxHeight         = 'maxH',
90     kKeyThumbnailTime     = 'thbT',  // int64_t (usecs)
91     kKeyTrackID           = 'trID',
92     kKeyEncoderDelay      = 'encd',  // int32_t (frames)
93     kKeyEncoderPadding    = 'encp',  // int32_t (frames)
94 
95     kKeyAlbum             = 'albu',  // cstring
96     kKeyArtist            = 'arti',  // cstring
97     kKeyAlbumArtist       = 'aart',  // cstring
98     kKeyComposer          = 'comp',  // cstring
99     kKeyGenre             = 'genr',  // cstring
100     kKeyTitle             = 'titl',  // cstring
101     kKeyYear              = 'year',  // cstring
102     kKeyAlbumArt          = 'albA',  // compressed image data
103     kKeyAuthor            = 'auth',  // cstring
104     kKeyCDTrackNumber     = 'cdtr',  // cstring
105     kKeyDiscNumber        = 'dnum',  // cstring
106     kKeyDate              = 'date',  // cstring
107     kKeyWriter            = 'writ',  // cstring
108     kKeyCompilation       = 'cpil',  // cstring
109     kKeyLocation          = 'loc ',  // cstring
110     kKeyTimeScale         = 'tmsl',  // int32_t
111     kKeyCaptureFramerate  = 'capF',  // float (capture fps)
112 
113     // video profile and level
114     kKeyVideoProfile      = 'vprf',  // int32_t
115     kKeyVideoLevel        = 'vlev',  // int32_t
116 
117     kKey2ByteNalLength    = '2NAL',  // int32_t (bool)
118 
119     // Identify the file output format for authoring
120     // Please see <media/mediarecorder.h> for the supported
121     // file output formats.
122     kKeyFileType          = 'ftyp',  // int32_t
123 
124     // Track authoring progress status
125     // kKeyTrackTimeStatus is used to track progress in elapsed time
126     kKeyTrackTimeStatus   = 'tktm',  // int64_t
127 
128     kKeyRealTimeRecording = 'rtrc',  // bool (int32_t)
129     kKeyNumBuffers        = 'nbbf',  // int32_t
130 
131     // Ogg files can be tagged to be automatically looping...
132     kKeyAutoLoop          = 'autL',  // bool (int32_t)
133 
134     kKeyValidSamples      = 'valD',  // int32_t
135 
136     kKeyIsUnreadable      = 'unre',  // bool (int32_t)
137 
138     // An indication that a video buffer has been rendered.
139     kKeyRendered          = 'rend',  // bool (int32_t)
140 
141     // The language code for this media
142     kKeyMediaLanguage     = 'lang',  // cstring
143 
144     // The manufacturer code for this media
145     kKeyManufacturer  = 'manu',  // cstring
146 
147     // To store the timed text format data
148     kKeyTextFormatData    = 'text',  // raw data
149 
150     kKeyRequiresSecureBuffers = 'secu',  // bool (int32_t)
151 
152     kKeyIsADTS            = 'adts',  // bool (int32_t)
153     kKeyAACAOT            = 'aaot',  // int32_t
154 
155     // If a MediaBuffer's data represents (at least partially) encrypted
156     // data, the following fields aid in decryption.
157     // The data can be thought of as pairs of plain and encrypted data
158     // fragments, i.e. plain and encrypted data alternate.
159     // The first fragment is by convention plain data (if that's not the
160     // case, simply specify plain fragment size of 0).
161     // kKeyEncryptedSizes and kKeyPlainSizes each map to an array of
162     // size_t values. The sum total of all size_t values of both arrays
163     // must equal the amount of data (i.e. MediaBuffer's range_length()).
164     // If both arrays are present, they must be of the same size.
165     // If only encrypted sizes are present it is assumed that all
166     // plain sizes are 0, i.e. all fragments are encrypted.
167     // To programmatically set these array, use the MetaDataBase::setData API, i.e.
168     // const size_t encSizes[];
169     // meta->setData(
170     //  kKeyEncryptedSizes, 0 /* type */, encSizes, sizeof(encSizes));
171     // A plain sizes array by itself makes no sense.
172     kKeyEncryptedSizes    = 'encr',  // size_t[]
173     kKeyPlainSizes        = 'plai',  // size_t[]
174     kKeyCryptoKey         = 'cryK',  // uint8_t[16]
175     kKeyCryptoIV          = 'cryI',  // uint8_t[16]
176     kKeyCryptoMode        = 'cryM',  // int32_t
177 
178     kKeyCryptoDefaultIVSize = 'cryS',  // int32_t
179 
180     kKeyPssh              = 'pssh',  // raw data
181     kKeyCASystemID        = 'caid',  // int32_t
182     kKeyCASessionID       = 'seid',  // raw data
183     kKeyCAPrivateData     = 'cadc',  // raw data
184 
185     kKeyEncryptedByteBlock = 'cblk',  // uint8_t
186     kKeySkipByteBlock     = 'sblk',  // uint8_t
187 
188     // Please see MediaFormat.KEY_IS_AUTOSELECT.
189     kKeyTrackIsAutoselect = 'auto', // bool (int32_t)
190     // Please see MediaFormat.KEY_IS_DEFAULT.
191     kKeyTrackIsDefault    = 'dflt', // bool (int32_t)
192     // Similar to MediaFormat.KEY_IS_FORCED_SUBTITLE but pertains to av tracks as well.
193     kKeyTrackIsForced     = 'frcd', // bool (int32_t)
194 
195     // H264 supplemental enhancement information offsets/sizes
196     kKeySEI               = 'sei ', // raw data
197 
198     // MPEG user data offsets
199     kKeyMpegUserData      = 'mpud', // size_t[]
200 
201     // HDR related
202     kKeyHdrStaticInfo    = 'hdrS', // HDRStaticInfo
203     kKeyHdr10PlusInfo    = 'hdrD', // raw data
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     kKeyTileWidth        = 'tilW', // int32_t, HEIF tile width
217     kKeyTileHeight       = 'tilH', // int32_t, HEIF tile height
218     kKeyGridRows         = 'grdR', // int32_t, HEIF grid rows
219     kKeyGridCols         = 'grdC', // int32_t, HEIF grid columns
220     kKeyIccProfile       = 'prof', // raw data, ICC profile data
221     kKeyIsPrimaryImage   = 'prim', // bool (int32_t), image track is the primary image
222     kKeyFrameCount       = 'nfrm', // int32_t, total number of frame in video track
223     kKeyExifOffset       = 'exof', // int64_t, Exif data offset
224     kKeyExifSize         = 'exsz', // int64_t, Exif data size
225     kKeyExifTiffOffset   = 'thdr', // int32_t, if > 0, buffer contains exif data block with
226                                    // tiff hdr at specified offset
227     kKeyPcmBigEndian     = 'pcmb', // bool (int32_t)
228 
229     // Key for ALAC Magic Cookie
230     kKeyAlacMagicCookie  = 'almc', // raw data
231 
232     // AC-4 AudioPresentationInfo
233     kKeyAudioPresentationInfo = 'audP',  // raw data
234 
235     // opaque codec specific data being passed from extractor to codec
236     kKeyOpaqueCSD0       = 'csd0',
237     kKeyOpaqueCSD1       = 'csd1',
238     kKeyOpaqueCSD2       = 'csd2',
239 
240     kKeyHapticChannelCount = 'hapC',
241 
242     /* MediaRecorder.h, error notifications can represent track ids with 4 bits only.
243      * | track id | reserved |     error or info type     |
244      * 31         28         16                           0
245      */
246     kKey4BitTrackIds = '4bid',
247 
248     // Treat empty track as malformed for MediaRecorder.
249     kKeyEmptyTrackMalFormed = 'nemt', // bool (int32_t)
250 };
251 
252 enum {
253     kTypeESDS        = 'esds',
254     kTypeAVCC        = 'avcc',
255     kTypeHVCC        = 'hvcc',
256     kTypeAV1C        = 'av1c',
257     kTypeDVCC        = 'dvcc',
258     kTypeD263        = 'd263',
259 };
260 
261 enum {
262     kCryptoModeUnencrypted = 0,
263     kCryptoModeAesCtr      = 1,
264     kCryptoModeAesCbc      = 2,
265 };
266 
267 class Parcel;
268 
269 class MetaDataBase {
270 public:
271     MetaDataBase();
272     MetaDataBase(const MetaDataBase &from);
273     MetaDataBase& operator = (const MetaDataBase &);
274 
275     virtual ~MetaDataBase();
276 
277     enum Type {
278         TYPE_NONE     = 'none',
279         TYPE_C_STRING = 'cstr',
280         TYPE_INT32    = 'in32',
281         TYPE_INT64    = 'in64',
282         TYPE_FLOAT    = 'floa',
283         TYPE_POINTER  = 'ptr ',
284         TYPE_RECT     = 'rect',
285     };
286 
287     void clear();
288     bool remove(uint32_t key);
289 
290     bool setCString(uint32_t key, const char *value);
291     bool setInt32(uint32_t key, int32_t value);
292     bool setInt64(uint32_t key, int64_t value);
293     bool setFloat(uint32_t key, float value);
294     bool setPointer(uint32_t key, void *value);
295 
296     bool setRect(
297             uint32_t key,
298             int32_t left, int32_t top,
299             int32_t right, int32_t bottom);
300 
301     bool findCString(uint32_t key, const char **value) const;
302     bool findInt32(uint32_t key, int32_t *value) const;
303     bool findInt64(uint32_t key, int64_t *value) const;
304     bool findFloat(uint32_t key, float *value) const;
305     bool findPointer(uint32_t key, void **value) const;
306 
307     bool findRect(
308             uint32_t key,
309             int32_t *left, int32_t *top,
310             int32_t *right, int32_t *bottom) const;
311 
312     bool setData(uint32_t key, uint32_t type, const void *data, size_t size);
313 
314     bool findData(uint32_t key, uint32_t *type,
315                   const void **data, size_t *size) const;
316 
317     bool hasData(uint32_t key) const;
318 
319     String8 toString() const;
320     void dumpToLog() const;
321 
322 private:
323     friend class BpMediaSource;
324     friend class BnMediaSource;
325     friend class BnMediaExtractor;
326     friend class MetaData;
327 
328     struct typed_data;
329     struct Rect;
330     struct MetaDataInternal;
331     MetaDataInternal *mInternalData;
332 #ifndef __ANDROID_VNDK__
333     status_t writeToParcel(Parcel &parcel);
334     status_t updateFromParcel(const Parcel &parcel);
335 #endif
336 };
337 
338 }  // namespace android
339 
340 #endif  // META_DATA_H_
341