• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "Utils"
19 #include <utils/Log.h>
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <sys/stat.h>
23 
24 #include <utility>
25 #include <vector>
26 
27 #include "include/ESDS.h"
28 #include "include/HevcUtils.h"
29 
30 #include <cutils/properties.h>
31 #include <media/stagefright/CodecBase.h>
32 #include <media/stagefright/foundation/ABuffer.h>
33 #include <media/stagefright/foundation/ADebug.h>
34 #include <media/stagefright/foundation/ALookup.h>
35 #include <media/stagefright/foundation/AMessage.h>
36 #include <media/stagefright/foundation/ByteUtils.h>
37 #include <media/stagefright/foundation/OpusHeader.h>
38 #include <media/stagefright/MetaData.h>
39 #include <media/stagefright/MediaCodecConstants.h>
40 #include <media/stagefright/MediaDefs.h>
41 #include <media/AudioSystem.h>
42 #include <media/MediaPlayerInterface.h>
43 #include <media/stagefright/Utils.h>
44 #include <media/AudioParameter.h>
45 #include <system/audio.h>
46 
47 // TODO : Remove the defines once mainline media is built against NDK >= 31.
48 // The mp4 extractor is part of mainline and builds against NDK 29 as of
49 // writing. These keys are available only from NDK 31:
50 #define AMEDIAFORMAT_KEY_MPEGH_PROFILE_LEVEL_INDICATION \
51   "mpegh-profile-level-indication"
52 #define AMEDIAFORMAT_KEY_MPEGH_REFERENCE_CHANNEL_LAYOUT \
53   "mpegh-reference-channel-layout"
54 #define AMEDIAFORMAT_KEY_MPEGH_COMPATIBLE_SETS \
55   "mpegh-compatible-sets"
56 
57 namespace {
58     // TODO: this should possibly be handled in an else
59     constexpr static int32_t AACObjectNull = 0;
60 
61     // TODO: decide if we should just not transmit the level in this case
62     constexpr static int32_t DolbyVisionLevelUnknown = 0;
63 }
64 
65 namespace android {
66 
copyNALUToABuffer(sp<ABuffer> * buffer,const uint8_t * ptr,size_t length)67 static status_t copyNALUToABuffer(sp<ABuffer> *buffer, const uint8_t *ptr, size_t length) {
68     if (((*buffer)->size() + 4 + length) > ((*buffer)->capacity() - (*buffer)->offset())) {
69         sp<ABuffer> tmpBuffer = new (std::nothrow) ABuffer((*buffer)->size() + 4 + length + 1024);
70         if (tmpBuffer.get() == NULL || tmpBuffer->base() == NULL) {
71             return NO_MEMORY;
72         }
73         memcpy(tmpBuffer->data(), (*buffer)->data(), (*buffer)->size());
74         tmpBuffer->setRange(0, (*buffer)->size());
75         (*buffer) = tmpBuffer;
76     }
77 
78     memcpy((*buffer)->data() + (*buffer)->size(), "\x00\x00\x00\x01", 4);
79     memcpy((*buffer)->data() + (*buffer)->size() + 4, ptr, length);
80     (*buffer)->setRange((*buffer)->offset(), (*buffer)->size() + 4 + length);
81     return OK;
82 }
83 
84 #if 0
85 static void convertMetaDataToMessageInt32(
86         const sp<MetaData> &meta, sp<AMessage> &msg, uint32_t key, const char *name) {
87     int32_t value;
88     if (meta->findInt32(key, &value)) {
89         msg->setInt32(name, value);
90     }
91 }
92 #endif
93 
convertMetaDataToMessageColorAspects(const MetaDataBase * meta,sp<AMessage> & msg)94 static void convertMetaDataToMessageColorAspects(const MetaDataBase *meta, sp<AMessage> &msg) {
95     // 0 values are unspecified
96     int32_t range = 0;
97     int32_t primaries = 0;
98     int32_t transferFunction = 0;
99     int32_t colorMatrix = 0;
100     meta->findInt32(kKeyColorRange, &range);
101     meta->findInt32(kKeyColorPrimaries, &primaries);
102     meta->findInt32(kKeyTransferFunction, &transferFunction);
103     meta->findInt32(kKeyColorMatrix, &colorMatrix);
104     ColorAspects colorAspects;
105     memset(&colorAspects, 0, sizeof(colorAspects));
106     colorAspects.mRange = (ColorAspects::Range)range;
107     colorAspects.mPrimaries = (ColorAspects::Primaries)primaries;
108     colorAspects.mTransfer = (ColorAspects::Transfer)transferFunction;
109     colorAspects.mMatrixCoeffs = (ColorAspects::MatrixCoeffs)colorMatrix;
110 
111     int32_t rangeMsg, standardMsg, transferMsg;
112     if (CodecBase::convertCodecColorAspectsToPlatformAspects(
113             colorAspects, &rangeMsg, &standardMsg, &transferMsg) != OK) {
114         return;
115     }
116 
117     // save specified values to msg
118     if (rangeMsg != 0) {
119         msg->setInt32("color-range", rangeMsg);
120     }
121     if (standardMsg != 0) {
122         msg->setInt32("color-standard", standardMsg);
123     }
124     if (transferMsg != 0) {
125         msg->setInt32("color-transfer", transferMsg);
126     }
127 }
128 
129 /**
130  * Returns true if, and only if, the given format corresponds to HDR10 or HDR10+.
131  */
isHdr10or10Plus(const sp<AMessage> & format)132 static bool isHdr10or10Plus(const sp<AMessage> &format) {
133 
134     // if user/container supplied HDR static info without transfer set, assume true
135     if ((format->contains("hdr-static-info") || format->contains("hdr10-plus-info"))
136             && !format->contains("color-transfer")) {
137         return true;
138     }
139     // otherwise, verify that an HDR transfer function is set
140     int32_t transfer;
141     if (format->findInt32("color-transfer", &transfer)) {
142         return transfer == ColorUtils::kColorTransferST2084;
143     }
144     return false;
145 }
146 
parseAacProfileFromCsd(const sp<ABuffer> & csd,sp<AMessage> & format)147 static void parseAacProfileFromCsd(const sp<ABuffer> &csd, sp<AMessage> &format) {
148     if (csd->size() < 2) {
149         return;
150     }
151 
152     uint16_t audioObjectType = U16_AT((uint8_t*)csd->data());
153     if ((audioObjectType & 0xF800) == 0xF800) {
154         audioObjectType = 32 + ((audioObjectType >> 5) & 0x3F);
155     } else {
156         audioObjectType >>= 11;
157     }
158 
159 
160     const static ALookup<uint16_t, int32_t> profiles {
161         { 1,  AACObjectMain     },
162         { 2,  AACObjectLC       },
163         { 3,  AACObjectSSR      },
164         { 4,  AACObjectLTP      },
165         { 5,  AACObjectHE       },
166         { 6,  AACObjectScalable },
167         { 17, AACObjectERLC     },
168         { 23, AACObjectLD       },
169         { 29, AACObjectHE_PS    },
170         { 39, AACObjectELD      },
171         { 42, AACObjectXHE      },
172     };
173 
174     int32_t profile;
175     if (profiles.map(audioObjectType, &profile)) {
176         format->setInt32("profile", profile);
177     }
178 }
179 
parseAvcProfileLevelFromAvcc(const uint8_t * ptr,size_t size,sp<AMessage> & format)180 static void parseAvcProfileLevelFromAvcc(const uint8_t *ptr, size_t size, sp<AMessage> &format) {
181     if (size < 4 || ptr[0] != 1) {  // configurationVersion == 1
182         return;
183     }
184     const uint8_t profile = ptr[1];
185     const uint8_t constraints = ptr[2];
186     const uint8_t level = ptr[3];
187 
188     const static ALookup<uint8_t, int32_t> levels {
189         {  9, AVCLevel1b }, // technically, 9 is only used for High+ profiles
190         { 10, AVCLevel1  },
191         { 11, AVCLevel11 }, // prefer level 1.1 for the value 11
192         { 11, AVCLevel1b },
193         { 12, AVCLevel12 },
194         { 13, AVCLevel13 },
195         { 20, AVCLevel2  },
196         { 21, AVCLevel21 },
197         { 22, AVCLevel22 },
198         { 30, AVCLevel3  },
199         { 31, AVCLevel31 },
200         { 32, AVCLevel32 },
201         { 40, AVCLevel4  },
202         { 41, AVCLevel41 },
203         { 42, AVCLevel42 },
204         { 50, AVCLevel5  },
205         { 51, AVCLevel51 },
206         { 52, AVCLevel52 },
207         { 60, AVCLevel6  },
208         { 61, AVCLevel61 },
209         { 62, AVCLevel62 },
210     };
211     const static ALookup<uint8_t, int32_t> profiles {
212         { 66, AVCProfileBaseline },
213         { 77, AVCProfileMain     },
214         { 88, AVCProfileExtended },
215         { 100, AVCProfileHigh    },
216         { 110, AVCProfileHigh10  },
217         { 122, AVCProfileHigh422 },
218         { 244, AVCProfileHigh444 },
219     };
220 
221     // set profile & level if they are recognized
222     int32_t codecProfile;
223     int32_t codecLevel;
224     if (profiles.map(profile, &codecProfile)) {
225         if (profile == 66 && (constraints & 0x40)) {
226             codecProfile = AVCProfileConstrainedBaseline;
227         } else if (profile == 100 && (constraints & 0x0C) == 0x0C) {
228             codecProfile = AVCProfileConstrainedHigh;
229         }
230         format->setInt32("profile", codecProfile);
231         if (levels.map(level, &codecLevel)) {
232             // for 9 && 11 decide level based on profile and constraint_set3 flag
233             if (level == 11 && (profile == 66 || profile == 77 || profile == 88)) {
234                 codecLevel = (constraints & 0x10) ? AVCLevel1b : AVCLevel11;
235             }
236             format->setInt32("level", codecLevel);
237         }
238     }
239 }
240 
getDolbyVisionProfileTable()241 static const ALookup<uint8_t, int32_t>&  getDolbyVisionProfileTable() {
242     static const ALookup<uint8_t, int32_t> profileTable = {
243         {1, DolbyVisionProfileDvavPen},
244         {3, DolbyVisionProfileDvheDen},
245         {4, DolbyVisionProfileDvheDtr},
246         {5, DolbyVisionProfileDvheStn},
247         {6, DolbyVisionProfileDvheDth},
248         {7, DolbyVisionProfileDvheDtb},
249         {8, DolbyVisionProfileDvheSt},
250         {9, DolbyVisionProfileDvavSe},
251         {10, DolbyVisionProfileDvav110},
252     };
253     return profileTable;
254 }
255 
getDolbyVisionLevelsTable()256 static const ALookup<uint8_t, int32_t>&  getDolbyVisionLevelsTable() {
257     static const ALookup<uint8_t, int32_t> levelsTable = {
258         {0, DolbyVisionLevelUnknown},
259         {1, DolbyVisionLevelHd24},
260         {2, DolbyVisionLevelHd30},
261         {3, DolbyVisionLevelFhd24},
262         {4, DolbyVisionLevelFhd30},
263         {5, DolbyVisionLevelFhd60},
264         {6, DolbyVisionLevelUhd24},
265         {7, DolbyVisionLevelUhd30},
266         {8, DolbyVisionLevelUhd48},
267         {9, DolbyVisionLevelUhd60},
268         {10, DolbyVisionLevelUhd120},
269         {11, DolbyVisionLevel8k30},
270         {12, DolbyVisionLevel8k60},
271     };
272     return levelsTable;
273 }
parseDolbyVisionProfileLevelFromDvcc(const uint8_t * ptr,size_t size,sp<AMessage> & format)274 static void parseDolbyVisionProfileLevelFromDvcc(const uint8_t *ptr, size_t size, sp<AMessage> &format) {
275     // dv_major.dv_minor Should be 1.0 or 2.1
276     if (size != 24 || ((ptr[0] != 1 || ptr[1] != 0) && (ptr[0] != 2 || ptr[1] != 1))) {
277         ALOGV("Size %zu, dv_major %d, dv_minor %d", size, ptr[0], ptr[1]);
278         return;
279     }
280 
281     const uint8_t profile = ptr[2] >> 1;
282     const uint8_t level = ((ptr[2] & 0x1) << 5) | ((ptr[3] >> 3) & 0x1f);
283     const uint8_t rpu_present_flag = (ptr[3] >> 2) & 0x01;
284     const uint8_t el_present_flag = (ptr[3] >> 1) & 0x01;
285     const uint8_t bl_present_flag = (ptr[3] & 0x01);
286     const int32_t bl_compatibility_id = (int32_t)(ptr[4] >> 4);
287 
288     ALOGV("profile-level-compatibility value in dv(c|v)c box %d-%d-%d",
289           profile, level, bl_compatibility_id);
290 
291     // All Dolby Profiles will have profile and level info in MediaFormat
292     // Profile 8 and 9 will have bl_compatibility_id too.
293     const ALookup<uint8_t, int32_t> &profiles = getDolbyVisionProfileTable();
294     const ALookup<uint8_t, int32_t> &levels = getDolbyVisionLevelsTable();
295 
296     // set rpuAssoc
297     if (rpu_present_flag && el_present_flag && !bl_present_flag) {
298         format->setInt32("rpuAssoc", 1);
299     }
300     // set profile & level if they are recognized
301     int32_t codecProfile;
302     int32_t codecLevel;
303     if (profiles.map(profile, &codecProfile)) {
304         format->setInt32("profile", codecProfile);
305         if (codecProfile == DolbyVisionProfileDvheSt ||
306             codecProfile == DolbyVisionProfileDvavSe) {
307             format->setInt32("bl_compatibility_id", bl_compatibility_id);
308         }
309         if (levels.map(level, &codecLevel)) {
310             format->setInt32("level", codecLevel);
311         }
312     }
313 }
314 
parseH263ProfileLevelFromD263(const uint8_t * ptr,size_t size,sp<AMessage> & format)315 static void parseH263ProfileLevelFromD263(const uint8_t *ptr, size_t size, sp<AMessage> &format) {
316     if (size < 7) {
317         return;
318     }
319 
320     const uint8_t profile = ptr[6];
321     const uint8_t level = ptr[5];
322 
323     const static ALookup<uint8_t, int32_t> profiles {
324         { 0, H263ProfileBaseline },
325         { 1, H263ProfileH320Coding },
326         { 2, H263ProfileBackwardCompatible },
327         { 3, H263ProfileISWV2 },
328         { 4, H263ProfileISWV3 },
329         { 5, H263ProfileHighCompression },
330         { 6, H263ProfileInternet },
331         { 7, H263ProfileInterlace },
332         { 8, H263ProfileHighLatency },
333     };
334 
335     const static ALookup<uint8_t, int32_t> levels {
336         { 10, H263Level10 },
337         { 20, H263Level20 },
338         { 30, H263Level30 },
339         { 40, H263Level40 },
340         { 45, H263Level45 },
341         { 50, H263Level50 },
342         { 60, H263Level60 },
343         { 70, H263Level70 },
344     };
345 
346     // set profile & level if they are recognized
347     int32_t codecProfile;
348     int32_t codecLevel;
349     if (profiles.map(profile, &codecProfile)) {
350         format->setInt32("profile", codecProfile);
351         if (levels.map(level, &codecLevel)) {
352             format->setInt32("level", codecLevel);
353         }
354     }
355 }
356 
parseHevcProfileLevelFromHvcc(const uint8_t * ptr,size_t size,sp<AMessage> & format)357 static void parseHevcProfileLevelFromHvcc(const uint8_t *ptr, size_t size, sp<AMessage> &format) {
358     if (size < 13 || ptr[0] != 1) {  // configurationVersion == 1
359         return;
360     }
361 
362     const uint8_t profile = ptr[1] & 0x1F;
363     const uint8_t tier = (ptr[1] & 0x20) >> 5;
364     const uint8_t level = ptr[12];
365 
366     const static ALookup<std::pair<uint8_t, uint8_t>, int32_t> levels {
367         { { 0, 30  }, HEVCMainTierLevel1  },
368         { { 0, 60  }, HEVCMainTierLevel2  },
369         { { 0, 63  }, HEVCMainTierLevel21 },
370         { { 0, 90  }, HEVCMainTierLevel3  },
371         { { 0, 93  }, HEVCMainTierLevel31 },
372         { { 0, 120 }, HEVCMainTierLevel4  },
373         { { 0, 123 }, HEVCMainTierLevel41 },
374         { { 0, 150 }, HEVCMainTierLevel5  },
375         { { 0, 153 }, HEVCMainTierLevel51 },
376         { { 0, 156 }, HEVCMainTierLevel52 },
377         { { 0, 180 }, HEVCMainTierLevel6  },
378         { { 0, 183 }, HEVCMainTierLevel61 },
379         { { 0, 186 }, HEVCMainTierLevel62 },
380         { { 1, 30  }, HEVCHighTierLevel1  },
381         { { 1, 60  }, HEVCHighTierLevel2  },
382         { { 1, 63  }, HEVCHighTierLevel21 },
383         { { 1, 90  }, HEVCHighTierLevel3  },
384         { { 1, 93  }, HEVCHighTierLevel31 },
385         { { 1, 120 }, HEVCHighTierLevel4  },
386         { { 1, 123 }, HEVCHighTierLevel41 },
387         { { 1, 150 }, HEVCHighTierLevel5  },
388         { { 1, 153 }, HEVCHighTierLevel51 },
389         { { 1, 156 }, HEVCHighTierLevel52 },
390         { { 1, 180 }, HEVCHighTierLevel6  },
391         { { 1, 183 }, HEVCHighTierLevel61 },
392         { { 1, 186 }, HEVCHighTierLevel62 },
393     };
394 
395     const static ALookup<uint8_t, int32_t> profiles {
396         { 1, HEVCProfileMain   },
397         { 2, HEVCProfileMain10 },
398         // use Main for Main Still Picture decoding
399         { 3, HEVCProfileMain },
400     };
401 
402     // set profile & level if they are recognized
403     int32_t codecProfile;
404     int32_t codecLevel;
405     if (!profiles.map(profile, &codecProfile)) {
406         if (ptr[2] & 0x40 /* general compatibility flag 1 */) {
407             // Note that this case covers Main Still Picture too
408             codecProfile = HEVCProfileMain;
409         } else if (ptr[2] & 0x20 /* general compatibility flag 2 */) {
410             codecProfile = HEVCProfileMain10;
411         } else {
412             return;
413         }
414     }
415 
416     // bump to HDR profile
417     if (isHdr10or10Plus(format) && codecProfile == HEVCProfileMain10) {
418         if (format->contains("hdr10-plus-info")) {
419             codecProfile = HEVCProfileMain10HDR10Plus;
420         } else {
421             codecProfile = HEVCProfileMain10HDR10;
422         }
423     }
424 
425     format->setInt32("profile", codecProfile);
426     if (levels.map(std::make_pair(tier, level), &codecLevel)) {
427         format->setInt32("level", codecLevel);
428     }
429 }
430 
parseMpeg2ProfileLevelFromHeader(const uint8_t * data,size_t size,sp<AMessage> & format)431 static void parseMpeg2ProfileLevelFromHeader(
432         const uint8_t *data, size_t size, sp<AMessage> &format) {
433     // find sequence extension
434     const uint8_t *seq = (const uint8_t*)memmem(data, size, "\x00\x00\x01\xB5", 4);
435     if (seq != NULL && seq + 5 < data + size) {
436         const uint8_t start_code = seq[4] >> 4;
437         if (start_code != 1 /* sequence extension ID */) {
438             return;
439         }
440         const uint8_t indication = ((seq[4] & 0xF) << 4) | ((seq[5] & 0xF0) >> 4);
441 
442         const static ALookup<uint8_t, int32_t> profiles {
443             { 0x50, MPEG2ProfileSimple  },
444             { 0x40, MPEG2ProfileMain    },
445             { 0x30, MPEG2ProfileSNR     },
446             { 0x20, MPEG2ProfileSpatial },
447             { 0x10, MPEG2ProfileHigh    },
448         };
449 
450         const static ALookup<uint8_t, int32_t> levels {
451             { 0x0A, MPEG2LevelLL  },
452             { 0x08, MPEG2LevelML  },
453             { 0x06, MPEG2LevelH14 },
454             { 0x04, MPEG2LevelHL  },
455             { 0x02, MPEG2LevelHP  },
456         };
457 
458         const static ALookup<uint8_t,
459                 std::pair<int32_t, int32_t>> escapes {
460             /* unsupported
461             { 0x8E, { XXX_MPEG2ProfileMultiView, MPEG2LevelLL  } },
462             { 0x8D, { XXX_MPEG2ProfileMultiView, MPEG2LevelML  } },
463             { 0x8B, { XXX_MPEG2ProfileMultiView, MPEG2LevelH14 } },
464             { 0x8A, { XXX_MPEG2ProfileMultiView, MPEG2LevelHL  } }, */
465             { 0x85, { MPEG2Profile422, MPEG2LevelML  } },
466             { 0x82, { MPEG2Profile422, MPEG2LevelHL  } },
467         };
468 
469         int32_t profile;
470         int32_t level;
471         std::pair<int32_t, int32_t> profileLevel;
472         if (escapes.map(indication, &profileLevel)) {
473             format->setInt32("profile", profileLevel.first);
474             format->setInt32("level", profileLevel.second);
475         } else if (profiles.map(indication & 0x70, &profile)) {
476             format->setInt32("profile", profile);
477             if (levels.map(indication & 0xF, &level)) {
478                 format->setInt32("level", level);
479             }
480         }
481     }
482 }
483 
parseMpeg2ProfileLevelFromEsds(ESDS & esds,sp<AMessage> & format)484 static void parseMpeg2ProfileLevelFromEsds(ESDS &esds, sp<AMessage> &format) {
485     // esds seems to only contain the profile for MPEG-2
486     uint8_t objType;
487     if (esds.getObjectTypeIndication(&objType) == OK) {
488         const static ALookup<uint8_t, int32_t> profiles{
489             { 0x60, MPEG2ProfileSimple  },
490             { 0x61, MPEG2ProfileMain    },
491             { 0x62, MPEG2ProfileSNR     },
492             { 0x63, MPEG2ProfileSpatial },
493             { 0x64, MPEG2ProfileHigh    },
494             { 0x65, MPEG2Profile422     },
495         };
496 
497         int32_t profile;
498         if (profiles.map(objType, &profile)) {
499             format->setInt32("profile", profile);
500         }
501     }
502 }
503 
parseMpeg4ProfileLevelFromCsd(const sp<ABuffer> & csd,sp<AMessage> & format)504 static void parseMpeg4ProfileLevelFromCsd(const sp<ABuffer> &csd, sp<AMessage> &format) {
505     const uint8_t *data = csd->data();
506     // find visual object sequence
507     const uint8_t *seq = (const uint8_t*)memmem(data, csd->size(), "\x00\x00\x01\xB0", 4);
508     if (seq != NULL && seq + 4 < data + csd->size()) {
509         const uint8_t indication = seq[4];
510 
511         const static ALookup<uint8_t,
512                 std::pair<int32_t, int32_t>> table {
513             { 0b00000001, { MPEG4ProfileSimple,            MPEG4Level1  } },
514             { 0b00000010, { MPEG4ProfileSimple,            MPEG4Level2  } },
515             { 0b00000011, { MPEG4ProfileSimple,            MPEG4Level3  } },
516             { 0b00000100, { MPEG4ProfileSimple,            MPEG4Level4a } },
517             { 0b00000101, { MPEG4ProfileSimple,            MPEG4Level5  } },
518             { 0b00000110, { MPEG4ProfileSimple,            MPEG4Level6  } },
519             { 0b00001000, { MPEG4ProfileSimple,            MPEG4Level0  } },
520             { 0b00001001, { MPEG4ProfileSimple,            MPEG4Level0b } },
521             { 0b00010000, { MPEG4ProfileSimpleScalable,    MPEG4Level0  } },
522             { 0b00010001, { MPEG4ProfileSimpleScalable,    MPEG4Level1  } },
523             { 0b00010010, { MPEG4ProfileSimpleScalable,    MPEG4Level2  } },
524             /* unsupported
525             { 0b00011101, { XXX_MPEG4ProfileSimpleScalableER,        MPEG4Level0  } },
526             { 0b00011110, { XXX_MPEG4ProfileSimpleScalableER,        MPEG4Level1  } },
527             { 0b00011111, { XXX_MPEG4ProfileSimpleScalableER,        MPEG4Level2  } }, */
528             { 0b00100001, { MPEG4ProfileCore,              MPEG4Level1  } },
529             { 0b00100010, { MPEG4ProfileCore,              MPEG4Level2  } },
530             { 0b00110010, { MPEG4ProfileMain,              MPEG4Level2  } },
531             { 0b00110011, { MPEG4ProfileMain,              MPEG4Level3  } },
532             { 0b00110100, { MPEG4ProfileMain,              MPEG4Level4  } },
533             /* deprecated
534             { 0b01000010, { MPEG4ProfileNbit,              MPEG4Level2  } }, */
535             { 0b01010001, { MPEG4ProfileScalableTexture,   MPEG4Level1  } },
536             { 0b01100001, { MPEG4ProfileSimpleFace,        MPEG4Level1  } },
537             { 0b01100010, { MPEG4ProfileSimpleFace,        MPEG4Level2  } },
538             { 0b01100011, { MPEG4ProfileSimpleFBA,         MPEG4Level1  } },
539             { 0b01100100, { MPEG4ProfileSimpleFBA,         MPEG4Level2  } },
540             { 0b01110001, { MPEG4ProfileBasicAnimated,     MPEG4Level1  } },
541             { 0b01110010, { MPEG4ProfileBasicAnimated,     MPEG4Level2  } },
542             { 0b10000001, { MPEG4ProfileHybrid,            MPEG4Level1  } },
543             { 0b10000010, { MPEG4ProfileHybrid,            MPEG4Level2  } },
544             { 0b10010001, { MPEG4ProfileAdvancedRealTime,  MPEG4Level1  } },
545             { 0b10010010, { MPEG4ProfileAdvancedRealTime,  MPEG4Level2  } },
546             { 0b10010011, { MPEG4ProfileAdvancedRealTime,  MPEG4Level3  } },
547             { 0b10010100, { MPEG4ProfileAdvancedRealTime,  MPEG4Level4  } },
548             { 0b10100001, { MPEG4ProfileCoreScalable,      MPEG4Level1  } },
549             { 0b10100010, { MPEG4ProfileCoreScalable,      MPEG4Level2  } },
550             { 0b10100011, { MPEG4ProfileCoreScalable,      MPEG4Level3  } },
551             { 0b10110001, { MPEG4ProfileAdvancedCoding,    MPEG4Level1  } },
552             { 0b10110010, { MPEG4ProfileAdvancedCoding,    MPEG4Level2  } },
553             { 0b10110011, { MPEG4ProfileAdvancedCoding,    MPEG4Level3  } },
554             { 0b10110100, { MPEG4ProfileAdvancedCoding,    MPEG4Level4  } },
555             { 0b11000001, { MPEG4ProfileAdvancedCore,      MPEG4Level1  } },
556             { 0b11000010, { MPEG4ProfileAdvancedCore,      MPEG4Level2  } },
557             { 0b11010001, { MPEG4ProfileAdvancedScalable,  MPEG4Level1  } },
558             { 0b11010010, { MPEG4ProfileAdvancedScalable,  MPEG4Level2  } },
559             { 0b11010011, { MPEG4ProfileAdvancedScalable,  MPEG4Level3  } },
560             /* unsupported
561             { 0b11100001, { XXX_MPEG4ProfileSimpleStudio,            MPEG4Level1  } },
562             { 0b11100010, { XXX_MPEG4ProfileSimpleStudio,            MPEG4Level2  } },
563             { 0b11100011, { XXX_MPEG4ProfileSimpleStudio,            MPEG4Level3  } },
564             { 0b11100100, { XXX_MPEG4ProfileSimpleStudio,            MPEG4Level4  } },
565             { 0b11100101, { XXX_MPEG4ProfileCoreStudio,              MPEG4Level1  } },
566             { 0b11100110, { XXX_MPEG4ProfileCoreStudio,              MPEG4Level2  } },
567             { 0b11100111, { XXX_MPEG4ProfileCoreStudio,              MPEG4Level3  } },
568             { 0b11101000, { XXX_MPEG4ProfileCoreStudio,              MPEG4Level4  } },
569             { 0b11101011, { XXX_MPEG4ProfileSimpleStudio,            MPEG4Level5  } },
570             { 0b11101100, { XXX_MPEG4ProfileSimpleStudio,            MPEG4Level6  } }, */
571             { 0b11110000, { MPEG4ProfileAdvancedSimple,    MPEG4Level0  } },
572             { 0b11110001, { MPEG4ProfileAdvancedSimple,    MPEG4Level1  } },
573             { 0b11110010, { MPEG4ProfileAdvancedSimple,    MPEG4Level2  } },
574             { 0b11110011, { MPEG4ProfileAdvancedSimple,    MPEG4Level3  } },
575             { 0b11110100, { MPEG4ProfileAdvancedSimple,    MPEG4Level4  } },
576             { 0b11110101, { MPEG4ProfileAdvancedSimple,    MPEG4Level5  } },
577             { 0b11110111, { MPEG4ProfileAdvancedSimple,    MPEG4Level3b } },
578             /* deprecated
579             { 0b11111000, { XXX_MPEG4ProfileFineGranularityScalable, MPEG4Level0  } },
580             { 0b11111001, { XXX_MPEG4ProfileFineGranularityScalable, MPEG4Level1  } },
581             { 0b11111010, { XXX_MPEG4ProfileFineGranularityScalable, MPEG4Level2  } },
582             { 0b11111011, { XXX_MPEG4ProfileFineGranularityScalable, MPEG4Level3  } },
583             { 0b11111100, { XXX_MPEG4ProfileFineGranularityScalable, MPEG4Level4  } },
584             { 0b11111101, { XXX_MPEG4ProfileFineGranularityScalable, MPEG4Level5  } }, */
585         };
586 
587         std::pair<int32_t, int32_t> profileLevel;
588         if (table.map(indication, &profileLevel)) {
589             format->setInt32("profile", profileLevel.first);
590             format->setInt32("level", profileLevel.second);
591         }
592     }
593 }
594 
parseVp9ProfileLevelFromCsd(const sp<ABuffer> & csd,sp<AMessage> & format)595 static void parseVp9ProfileLevelFromCsd(const sp<ABuffer> &csd, sp<AMessage> &format) {
596     const uint8_t *data = csd->data();
597     size_t remaining = csd->size();
598 
599     while (remaining >= 2) {
600         const uint8_t id = data[0];
601         const uint8_t length = data[1];
602         remaining -= 2;
603         data += 2;
604         if (length > remaining) {
605             break;
606         }
607         switch (id) {
608             case 1 /* profileId */:
609                 if (length >= 1) {
610                     const static ALookup<uint8_t, int32_t> profiles {
611                         { 0, VP9Profile0 },
612                         { 1, VP9Profile1 },
613                         { 2, VP9Profile2 },
614                         { 3, VP9Profile3 },
615                     };
616 
617                     const static ALookup<int32_t, int32_t> toHdr10 {
618                         { VP9Profile2, VP9Profile2HDR },
619                         { VP9Profile3, VP9Profile3HDR },
620                     };
621 
622                     const static ALookup<int32_t, int32_t> toHdr10Plus {
623                         { VP9Profile2, VP9Profile2HDR10Plus },
624                         { VP9Profile3, VP9Profile3HDR10Plus },
625                     };
626 
627                     int32_t profile;
628                     if (profiles.map(data[0], &profile)) {
629                         // convert to HDR profile
630                         if (isHdr10or10Plus(format)) {
631                             if (format->contains("hdr10-plus-info")) {
632                                 toHdr10Plus.lookup(profile, &profile);
633                             } else {
634                                 toHdr10.lookup(profile, &profile);
635                             }
636                         }
637 
638                         format->setInt32("profile", profile);
639                     }
640                 }
641                 break;
642             case 2 /* levelId */:
643                 if (length >= 1) {
644                     const static ALookup<uint8_t, int32_t> levels {
645                         { 10, VP9Level1  },
646                         { 11, VP9Level11 },
647                         { 20, VP9Level2  },
648                         { 21, VP9Level21 },
649                         { 30, VP9Level3  },
650                         { 31, VP9Level31 },
651                         { 40, VP9Level4  },
652                         { 41, VP9Level41 },
653                         { 50, VP9Level5  },
654                         { 51, VP9Level51 },
655                         { 52, VP9Level52 },
656                         { 60, VP9Level6  },
657                         { 61, VP9Level61 },
658                         { 62, VP9Level62 },
659                     };
660 
661                     int32_t level;
662                     if (levels.map(data[0], &level)) {
663                         format->setInt32("level", level);
664                     }
665                 }
666                 break;
667             default:
668                 break;
669         }
670         remaining -= length;
671         data += length;
672     }
673 }
674 
parseAV1ProfileLevelFromCsd(const sp<ABuffer> & csd,sp<AMessage> & format)675 static void parseAV1ProfileLevelFromCsd(const sp<ABuffer> &csd, sp<AMessage> &format) {
676     // Parse CSD structure to extract profile level information
677     // https://aomediacodec.github.io/av1-isobmff/#av1codecconfigurationbox
678     const uint8_t *data = csd->data();
679     size_t remaining = csd->size();
680     if (remaining < 4 || data[0] != 0x81) {  // configurationVersion == 1
681         return;
682     }
683     uint8_t profileData = (data[1] & 0xE0) >> 5;
684     uint8_t levelData = data[1] & 0x1F;
685     uint8_t highBitDepth = (data[2] & 0x40) >> 6;
686 
687     const static ALookup<std::pair<uint8_t, uint8_t>, int32_t> profiles {
688         { { 0, 0 }, AV1ProfileMain8 },
689         { { 1, 0 }, AV1ProfileMain10 },
690     };
691 
692     int32_t profile;
693     if (profiles.map(std::make_pair(highBitDepth, profileData), &profile)) {
694         // bump to HDR profile
695         if (isHdr10or10Plus(format) && profile == AV1ProfileMain10) {
696             if (format->contains("hdr10-plus-info")) {
697                 profile = AV1ProfileMain10HDR10Plus;
698             } else {
699                 profile = AV1ProfileMain10HDR10;
700             }
701         }
702         format->setInt32("profile", profile);
703     }
704     const static ALookup<uint8_t, int32_t> levels {
705         { 0, AV1Level2   },
706         { 1, AV1Level21  },
707         { 2, AV1Level22  },
708         { 3, AV1Level23  },
709         { 4, AV1Level3   },
710         { 5, AV1Level31  },
711         { 6, AV1Level32  },
712         { 7, AV1Level33  },
713         { 8, AV1Level4   },
714         { 9, AV1Level41  },
715         { 10, AV1Level42  },
716         { 11, AV1Level43  },
717         { 12, AV1Level5   },
718         { 13, AV1Level51  },
719         { 14, AV1Level52  },
720         { 15, AV1Level53  },
721         { 16, AV1Level6   },
722         { 17, AV1Level61  },
723         { 18, AV1Level62  },
724         { 19, AV1Level63  },
725         { 20, AV1Level7   },
726         { 21, AV1Level71  },
727         { 22, AV1Level72  },
728         { 23, AV1Level73  },
729     };
730 
731     int32_t level;
732     if (levels.map(levelData, &level)) {
733         format->setInt32("level", level);
734     }
735 }
736 
737 
738 static std::vector<std::pair<const char *, uint32_t>> stringMappings {
739     {
740         { "album", kKeyAlbum },
741         { "albumartist", kKeyAlbumArtist },
742         { "artist", kKeyArtist },
743         { "author", kKeyAuthor },
744         { "cdtracknum", kKeyCDTrackNumber },
745         { "compilation", kKeyCompilation },
746         { "composer", kKeyComposer },
747         { "date", kKeyDate },
748         { "discnum", kKeyDiscNumber },
749         { "genre", kKeyGenre },
750         { "location", kKeyLocation },
751         { "lyricist", kKeyWriter },
752         { "manufacturer", kKeyManufacturer },
753         { "title", kKeyTitle },
754         { "year", kKeyYear },
755     }
756 };
757 
758 static std::vector<std::pair<const char *, uint32_t>> floatMappings {
759     {
760         { "capture-rate", kKeyCaptureFramerate },
761     }
762 };
763 
764 static std::vector<std::pair<const char*, uint32_t>> int64Mappings {
765     {
766         { "exif-offset", kKeyExifOffset},
767         { "exif-size", kKeyExifSize},
768         { "xmp-offset", kKeyXmpOffset},
769         { "xmp-size", kKeyXmpSize},
770         { "target-time", kKeyTargetTime},
771         { "thumbnail-time", kKeyThumbnailTime},
772         { "timeUs", kKeyTime},
773         { "durationUs", kKeyDuration},
774         { "sample-file-offset", kKeySampleFileOffset},
775         { "last-sample-index-in-chunk", kKeyLastSampleIndexInChunk},
776         { "sample-time-before-append", kKeySampleTimeBeforeAppend},
777     }
778 };
779 
780 static std::vector<std::pair<const char *, uint32_t>> int32Mappings {
781     {
782         { "loop", kKeyAutoLoop },
783         { "time-scale", kKeyTimeScale },
784         { "crypto-mode", kKeyCryptoMode },
785         { "crypto-default-iv-size", kKeyCryptoDefaultIVSize },
786         { "crypto-encrypted-byte-block", kKeyEncryptedByteBlock },
787         { "crypto-skip-byte-block", kKeySkipByteBlock },
788         { "frame-count", kKeyFrameCount },
789         { "max-bitrate", kKeyMaxBitRate },
790         { "pcm-big-endian", kKeyPcmBigEndian },
791         { "temporal-layer-count", kKeyTemporalLayerCount },
792         { "temporal-layer-id", kKeyTemporalLayerId },
793         { "thumbnail-width", kKeyThumbnailWidth },
794         { "thumbnail-height", kKeyThumbnailHeight },
795         { "track-id", kKeyTrackID },
796         { "valid-samples", kKeyValidSamples },
797     }
798 };
799 
800 static std::vector<std::pair<const char *, uint32_t>> bufferMappings {
801     {
802         { "albumart", kKeyAlbumArt },
803         { "audio-presentation-info", kKeyAudioPresentationInfo },
804         { "pssh", kKeyPssh },
805         { "crypto-iv", kKeyCryptoIV },
806         { "crypto-key", kKeyCryptoKey },
807         { "crypto-encrypted-sizes", kKeyEncryptedSizes },
808         { "crypto-plain-sizes", kKeyPlainSizes },
809         { "icc-profile", kKeyIccProfile },
810         { "sei", kKeySEI },
811         { "text-format-data", kKeyTextFormatData },
812         { "thumbnail-csd-hevc", kKeyThumbnailHVCC },
813         { "slow-motion-markers", kKeySlowMotionMarkers },
814         { "thumbnail-csd-av1c", kKeyThumbnailAV1C },
815     }
816 };
817 
818 static std::vector<std::pair<const char *, uint32_t>> CSDMappings {
819     {
820         { "csd-0", kKeyOpaqueCSD0 },
821         { "csd-1", kKeyOpaqueCSD1 },
822         { "csd-2", kKeyOpaqueCSD2 },
823     }
824 };
825 
convertMessageToMetaDataFromMappings(const sp<AMessage> & msg,sp<MetaData> & meta)826 void convertMessageToMetaDataFromMappings(const sp<AMessage> &msg, sp<MetaData> &meta) {
827     for (auto elem : stringMappings) {
828         AString value;
829         if (msg->findString(elem.first, &value)) {
830             meta->setCString(elem.second, value.c_str());
831         }
832     }
833 
834     for (auto elem : floatMappings) {
835         float value;
836         if (msg->findFloat(elem.first, &value)) {
837             meta->setFloat(elem.second, value);
838         }
839     }
840 
841     for (auto elem : int64Mappings) {
842         int64_t value;
843         if (msg->findInt64(elem.first, &value)) {
844             meta->setInt64(elem.second, value);
845         }
846     }
847 
848     for (auto elem : int32Mappings) {
849         int32_t value;
850         if (msg->findInt32(elem.first, &value)) {
851             meta->setInt32(elem.second, value);
852         }
853     }
854 
855     for (auto elem : bufferMappings) {
856         sp<ABuffer> value;
857         if (msg->findBuffer(elem.first, &value)) {
858             meta->setData(elem.second,
859                     MetaDataBase::Type::TYPE_NONE, value->data(), value->size());
860         }
861     }
862 
863     for (auto elem : CSDMappings) {
864         sp<ABuffer> value;
865         if (msg->findBuffer(elem.first, &value)) {
866             meta->setData(elem.second,
867                     MetaDataBase::Type::TYPE_NONE, value->data(), value->size());
868         }
869     }
870 }
871 
convertMetaDataToMessageFromMappings(const MetaDataBase * meta,sp<AMessage> format)872 void convertMetaDataToMessageFromMappings(const MetaDataBase *meta, sp<AMessage> format) {
873     for (auto elem : stringMappings) {
874         const char *value;
875         if (meta->findCString(elem.second, &value)) {
876             format->setString(elem.first, value, strlen(value));
877         }
878     }
879 
880     for (auto elem : floatMappings) {
881         float value;
882         if (meta->findFloat(elem.second, &value)) {
883             format->setFloat(elem.first, value);
884         }
885     }
886 
887     for (auto elem : int64Mappings) {
888         int64_t value;
889         if (meta->findInt64(elem.second, &value)) {
890             format->setInt64(elem.first, value);
891         }
892     }
893 
894     for (auto elem : int32Mappings) {
895         int32_t value;
896         if (meta->findInt32(elem.second, &value)) {
897             format->setInt32(elem.first, value);
898         }
899     }
900 
901     for (auto elem : bufferMappings) {
902         uint32_t type;
903         const void* data;
904         size_t size;
905         if (meta->findData(elem.second, &type, &data, &size)) {
906             sp<ABuffer> buf = ABuffer::CreateAsCopy(data, size);
907             format->setBuffer(elem.first, buf);
908         }
909     }
910 
911     for (auto elem : CSDMappings) {
912         uint32_t type;
913         const void* data;
914         size_t size;
915         if (meta->findData(elem.second, &type, &data, &size)) {
916             sp<ABuffer> buf = ABuffer::CreateAsCopy(data, size);
917             buf->meta()->setInt32("csd", true);
918             buf->meta()->setInt64("timeUs", 0);
919             format->setBuffer(elem.first, buf);
920         }
921     }
922 }
923 
convertMetaDataToMessage(const sp<MetaData> & meta,sp<AMessage> * format)924 status_t convertMetaDataToMessage(
925         const sp<MetaData> &meta, sp<AMessage> *format) {
926     return convertMetaDataToMessage(meta.get(), format);
927 }
928 
convertMetaDataToMessage(const MetaDataBase * meta,sp<AMessage> * format)929 status_t convertMetaDataToMessage(
930         const MetaDataBase *meta, sp<AMessage> *format) {
931 
932     format->clear();
933 
934     if (meta == NULL) {
935         ALOGE("convertMetaDataToMessage: NULL input");
936         return BAD_VALUE;
937     }
938 
939     const char *mime;
940     if (!meta->findCString(kKeyMIMEType, &mime)) {
941         return BAD_VALUE;
942     }
943 
944     sp<AMessage> msg = new AMessage;
945     msg->setString("mime", mime);
946 
947     convertMetaDataToMessageFromMappings(meta, msg);
948 
949     uint32_t type;
950     const void *data;
951     size_t size;
952     if (meta->findData(kKeyCASessionID, &type, &data, &size)) {
953         sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
954         if (buffer.get() == NULL || buffer->base() == NULL) {
955             return NO_MEMORY;
956         }
957 
958         msg->setBuffer("ca-session-id", buffer);
959         memcpy(buffer->data(), data, size);
960     }
961 
962     if (meta->findData(kKeyCAPrivateData, &type, &data, &size)) {
963         sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
964         if (buffer.get() == NULL || buffer->base() == NULL) {
965             return NO_MEMORY;
966         }
967 
968         msg->setBuffer("ca-private-data", buffer);
969         memcpy(buffer->data(), data, size);
970     }
971 
972     int32_t systemId;
973     if (meta->findInt32(kKeyCASystemID, &systemId)) {
974         msg->setInt32("ca-system-id", systemId);
975     }
976 
977     if (!strncasecmp("video/scrambled", mime, 15)
978             || !strncasecmp("audio/scrambled", mime, 15)) {
979 
980         *format = msg;
981         return OK;
982     }
983 
984     int64_t durationUs;
985     if (meta->findInt64(kKeyDuration, &durationUs)) {
986         msg->setInt64("durationUs", durationUs);
987     }
988 
989     int32_t avgBitRate = 0;
990     if (meta->findInt32(kKeyBitRate, &avgBitRate) && avgBitRate > 0) {
991         msg->setInt32("bitrate", avgBitRate);
992     }
993 
994     int32_t maxBitRate;
995     if (meta->findInt32(kKeyMaxBitRate, &maxBitRate)
996             && maxBitRate > 0 && maxBitRate >= avgBitRate) {
997         msg->setInt32("max-bitrate", maxBitRate);
998     }
999 
1000     int32_t isSync;
1001     if (meta->findInt32(kKeyIsSyncFrame, &isSync) && isSync != 0) {
1002         msg->setInt32("is-sync-frame", 1);
1003     }
1004 
1005     const char *lang;
1006     if (meta->findCString(kKeyMediaLanguage, &lang)) {
1007         msg->setString("language", lang);
1008     }
1009 
1010     if (!strncasecmp("video/", mime, 6) ||
1011             !strncasecmp("image/", mime, 6)) {
1012         int32_t width, height;
1013         if (!meta->findInt32(kKeyWidth, &width)
1014                 || !meta->findInt32(kKeyHeight, &height)) {
1015             return BAD_VALUE;
1016         }
1017 
1018         msg->setInt32("width", width);
1019         msg->setInt32("height", height);
1020 
1021         int32_t displayWidth, displayHeight;
1022         if (meta->findInt32(kKeyDisplayWidth, &displayWidth)
1023                 && meta->findInt32(kKeyDisplayHeight, &displayHeight)) {
1024             msg->setInt32("display-width", displayWidth);
1025             msg->setInt32("display-height", displayHeight);
1026         }
1027 
1028         int32_t sarWidth, sarHeight;
1029         if (meta->findInt32(kKeySARWidth, &sarWidth)
1030                 && meta->findInt32(kKeySARHeight, &sarHeight)) {
1031             msg->setInt32("sar-width", sarWidth);
1032             msg->setInt32("sar-height", sarHeight);
1033         }
1034 
1035         if (!strncasecmp("image/", mime, 6)) {
1036             int32_t tileWidth, tileHeight, gridRows, gridCols;
1037             if (meta->findInt32(kKeyTileWidth, &tileWidth)
1038                     && meta->findInt32(kKeyTileHeight, &tileHeight)
1039                     && meta->findInt32(kKeyGridRows, &gridRows)
1040                     && meta->findInt32(kKeyGridCols, &gridCols)) {
1041                 msg->setInt32("tile-width", tileWidth);
1042                 msg->setInt32("tile-height", tileHeight);
1043                 msg->setInt32("grid-rows", gridRows);
1044                 msg->setInt32("grid-cols", gridCols);
1045             }
1046             int32_t isPrimary;
1047             if (meta->findInt32(kKeyTrackIsDefault, &isPrimary) && isPrimary) {
1048                 msg->setInt32("is-default", 1);
1049             }
1050         }
1051 
1052         int32_t colorFormat;
1053         if (meta->findInt32(kKeyColorFormat, &colorFormat)) {
1054             msg->setInt32("color-format", colorFormat);
1055         }
1056 
1057         int32_t cropLeft, cropTop, cropRight, cropBottom;
1058         if (meta->findRect(kKeyCropRect,
1059                            &cropLeft,
1060                            &cropTop,
1061                            &cropRight,
1062                            &cropBottom)) {
1063             msg->setRect("crop", cropLeft, cropTop, cropRight, cropBottom);
1064         }
1065 
1066         int32_t rotationDegrees;
1067         if (meta->findInt32(kKeyRotation, &rotationDegrees)) {
1068             msg->setInt32("rotation-degrees", rotationDegrees);
1069         }
1070 
1071         uint32_t type;
1072         const void *data;
1073         size_t size;
1074         if (meta->findData(kKeyHdrStaticInfo, &type, &data, &size)
1075                 && type == 'hdrS' && size == sizeof(HDRStaticInfo)) {
1076             ColorUtils::setHDRStaticInfoIntoFormat(*(HDRStaticInfo*)data, msg);
1077         }
1078 
1079         if (meta->findData(kKeyHdr10PlusInfo, &type, &data, &size)
1080                 && size > 0) {
1081             sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1082             if (buffer.get() == NULL || buffer->base() == NULL) {
1083                 return NO_MEMORY;
1084             }
1085             memcpy(buffer->data(), data, size);
1086             msg->setBuffer("hdr10-plus-info", buffer);
1087         }
1088 
1089         convertMetaDataToMessageColorAspects(meta, msg);
1090     } else if (!strncasecmp("audio/", mime, 6)) {
1091         int32_t numChannels, sampleRate;
1092         if (!meta->findInt32(kKeyChannelCount, &numChannels)
1093                 || !meta->findInt32(kKeySampleRate, &sampleRate)) {
1094             return BAD_VALUE;
1095         }
1096 
1097         msg->setInt32("channel-count", numChannels);
1098         msg->setInt32("sample-rate", sampleRate);
1099 
1100         int32_t bitsPerSample;
1101         if (meta->findInt32(kKeyBitsPerSample, &bitsPerSample)) {
1102             msg->setInt32("bits-per-sample", bitsPerSample);
1103         }
1104 
1105         int32_t channelMask;
1106         if (meta->findInt32(kKeyChannelMask, &channelMask)) {
1107             msg->setInt32("channel-mask", channelMask);
1108         }
1109 
1110         int32_t delay = 0;
1111         if (meta->findInt32(kKeyEncoderDelay, &delay)) {
1112             msg->setInt32("encoder-delay", delay);
1113         }
1114         int32_t padding = 0;
1115         if (meta->findInt32(kKeyEncoderPadding, &padding)) {
1116             msg->setInt32("encoder-padding", padding);
1117         }
1118 
1119         int32_t isADTS;
1120         if (meta->findInt32(kKeyIsADTS, &isADTS)) {
1121             msg->setInt32("is-adts", isADTS);
1122         }
1123 
1124         int32_t mpeghProfileLevelIndication;
1125         if (meta->findInt32(kKeyMpeghProfileLevelIndication, &mpeghProfileLevelIndication)) {
1126             msg->setInt32(AMEDIAFORMAT_KEY_MPEGH_PROFILE_LEVEL_INDICATION,
1127                     mpeghProfileLevelIndication);
1128         }
1129         int32_t mpeghReferenceChannelLayout;
1130         if (meta->findInt32(kKeyMpeghReferenceChannelLayout, &mpeghReferenceChannelLayout)) {
1131             msg->setInt32(AMEDIAFORMAT_KEY_MPEGH_REFERENCE_CHANNEL_LAYOUT,
1132                     mpeghReferenceChannelLayout);
1133         }
1134         if (meta->findData(kKeyMpeghCompatibleSets, &type, &data, &size)) {
1135             sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1136             if (buffer.get() == NULL || buffer->base() == NULL) {
1137                 return NO_MEMORY;
1138             }
1139             msg->setBuffer(AMEDIAFORMAT_KEY_MPEGH_COMPATIBLE_SETS, buffer);
1140             memcpy(buffer->data(), data, size);
1141         }
1142 
1143         int32_t aacProfile = -1;
1144         if (meta->findInt32(kKeyAACAOT, &aacProfile)) {
1145             msg->setInt32("aac-profile", aacProfile);
1146         }
1147 
1148         int32_t pcmEncoding;
1149         if (meta->findInt32(kKeyPcmEncoding, &pcmEncoding)) {
1150             msg->setInt32("pcm-encoding", pcmEncoding);
1151         }
1152 
1153         int32_t hapticChannelCount;
1154         if (meta->findInt32(kKeyHapticChannelCount, &hapticChannelCount)) {
1155             msg->setInt32("haptic-channel-count", hapticChannelCount);
1156         }
1157     }
1158 
1159     int32_t maxInputSize;
1160     if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
1161         msg->setInt32("max-input-size", maxInputSize);
1162     }
1163 
1164     int32_t maxWidth;
1165     if (meta->findInt32(kKeyMaxWidth, &maxWidth)) {
1166         msg->setInt32("max-width", maxWidth);
1167     }
1168 
1169     int32_t maxHeight;
1170     if (meta->findInt32(kKeyMaxHeight, &maxHeight)) {
1171         msg->setInt32("max-height", maxHeight);
1172     }
1173 
1174     int32_t rotationDegrees;
1175     if (meta->findInt32(kKeyRotation, &rotationDegrees)) {
1176         msg->setInt32("rotation-degrees", rotationDegrees);
1177     }
1178 
1179     int32_t fps;
1180     if (meta->findInt32(kKeyFrameRate, &fps) && fps > 0) {
1181         msg->setInt32("frame-rate", fps);
1182     }
1183 
1184     if (meta->findData(kKeyAVCC, &type, &data, &size)) {
1185         // Parse the AVCDecoderConfigurationRecord
1186 
1187         const uint8_t *ptr = (const uint8_t *)data;
1188 
1189         if (size < 7 || ptr[0] != 1) {  // configurationVersion == 1
1190             ALOGE("b/23680780");
1191             return BAD_VALUE;
1192         }
1193 
1194         parseAvcProfileLevelFromAvcc(ptr, size, msg);
1195 
1196         // There is decodable content out there that fails the following
1197         // assertion, let's be lenient for now...
1198         // CHECK((ptr[4] >> 2) == 0x3f);  // reserved
1199 
1200         // we can get lengthSize value from 1 + (ptr[4] & 3)
1201 
1202         // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
1203         // violates it...
1204         // CHECK((ptr[5] >> 5) == 7);  // reserved
1205 
1206         size_t numSeqParameterSets = ptr[5] & 31;
1207 
1208         ptr += 6;
1209         size -= 6;
1210 
1211         sp<ABuffer> buffer = new (std::nothrow) ABuffer(1024);
1212         if (buffer.get() == NULL || buffer->base() == NULL) {
1213             return NO_MEMORY;
1214         }
1215         buffer->setRange(0, 0);
1216 
1217         for (size_t i = 0; i < numSeqParameterSets; ++i) {
1218             if (size < 2) {
1219                 ALOGE("b/23680780");
1220                 return BAD_VALUE;
1221             }
1222             size_t length = U16_AT(ptr);
1223 
1224             ptr += 2;
1225             size -= 2;
1226 
1227             if (size < length) {
1228                 return BAD_VALUE;
1229             }
1230             status_t err = copyNALUToABuffer(&buffer, ptr, length);
1231             if (err != OK) {
1232                 return err;
1233             }
1234 
1235             ptr += length;
1236             size -= length;
1237         }
1238 
1239         buffer->meta()->setInt32("csd", true);
1240         buffer->meta()->setInt64("timeUs", 0);
1241 
1242         msg->setBuffer("csd-0", buffer);
1243 
1244         buffer = new (std::nothrow) ABuffer(1024);
1245         if (buffer.get() == NULL || buffer->base() == NULL) {
1246             return NO_MEMORY;
1247         }
1248         buffer->setRange(0, 0);
1249 
1250         if (size < 1) {
1251             ALOGE("b/23680780");
1252             return BAD_VALUE;
1253         }
1254         size_t numPictureParameterSets = *ptr;
1255         ++ptr;
1256         --size;
1257 
1258         for (size_t i = 0; i < numPictureParameterSets; ++i) {
1259             if (size < 2) {
1260                 ALOGE("b/23680780");
1261                 return BAD_VALUE;
1262             }
1263             size_t length = U16_AT(ptr);
1264 
1265             ptr += 2;
1266             size -= 2;
1267 
1268             if (size < length) {
1269                 return BAD_VALUE;
1270             }
1271             status_t err = copyNALUToABuffer(&buffer, ptr, length);
1272             if (err != OK) {
1273                 return err;
1274             }
1275 
1276             ptr += length;
1277             size -= length;
1278         }
1279 
1280         buffer->meta()->setInt32("csd", true);
1281         buffer->meta()->setInt64("timeUs", 0);
1282         msg->setBuffer("csd-1", buffer);
1283     } else if (meta->findData(kKeyHVCC, &type, &data, &size)) {
1284         const uint8_t *ptr = (const uint8_t *)data;
1285 
1286         if (size < 23 || (ptr[0] != 1 && ptr[0] != 0)) {
1287             // configurationVersion == 1 or 0
1288             // 1 is what the standard dictates, but some old muxers may have used 0.
1289             ALOGE("b/23680780");
1290             return BAD_VALUE;
1291         }
1292 
1293         const size_t dataSize = size; // save for later
1294         ptr += 22;
1295         size -= 22;
1296 
1297         size_t numofArrays = (char)ptr[0];
1298         ptr += 1;
1299         size -= 1;
1300         size_t j = 0, i = 0;
1301 
1302         sp<ABuffer> buffer = new (std::nothrow) ABuffer(1024);
1303         if (buffer.get() == NULL || buffer->base() == NULL) {
1304             return NO_MEMORY;
1305         }
1306         buffer->setRange(0, 0);
1307 
1308         HevcParameterSets hvcc;
1309 
1310         for (i = 0; i < numofArrays; i++) {
1311             if (size < 3) {
1312                 ALOGE("b/23680780");
1313                 return BAD_VALUE;
1314             }
1315             ptr += 1;
1316             size -= 1;
1317 
1318             //Num of nals
1319             size_t numofNals = U16_AT(ptr);
1320 
1321             ptr += 2;
1322             size -= 2;
1323 
1324             for (j = 0; j < numofNals; j++) {
1325                 if (size < 2) {
1326                     ALOGE("b/23680780");
1327                     return BAD_VALUE;
1328                 }
1329                 size_t length = U16_AT(ptr);
1330 
1331                 ptr += 2;
1332                 size -= 2;
1333 
1334                 if (size < length) {
1335                     return BAD_VALUE;
1336                 }
1337                 status_t err = copyNALUToABuffer(&buffer, ptr, length);
1338                 if (err != OK) {
1339                     return err;
1340                 }
1341                 (void)hvcc.addNalUnit(ptr, length);
1342 
1343                 ptr += length;
1344                 size -= length;
1345             }
1346         }
1347         buffer->meta()->setInt32("csd", true);
1348         buffer->meta()->setInt64("timeUs", 0);
1349         msg->setBuffer("csd-0", buffer);
1350 
1351         // if we saw VUI color information we know whether this is HDR because VUI trumps other
1352         // format parameters for HEVC.
1353         HevcParameterSets::Info info = hvcc.getInfo();
1354         if (info & hvcc.kInfoHasColorDescription) {
1355             msg->setInt32("android._is-hdr", (info & hvcc.kInfoIsHdr) != 0);
1356         }
1357 
1358         uint32_t isoPrimaries, isoTransfer, isoMatrix, isoRange;
1359         if (hvcc.findParam32(kColourPrimaries, &isoPrimaries)
1360                 && hvcc.findParam32(kTransferCharacteristics, &isoTransfer)
1361                 && hvcc.findParam32(kMatrixCoeffs, &isoMatrix)
1362                 && hvcc.findParam32(kVideoFullRangeFlag, &isoRange)) {
1363             ALOGV("found iso color aspects : primaris=%d, transfer=%d, matrix=%d, range=%d",
1364                     isoPrimaries, isoTransfer, isoMatrix, isoRange);
1365 
1366             ColorAspects aspects;
1367             ColorUtils::convertIsoColorAspectsToCodecAspects(
1368                     isoPrimaries, isoTransfer, isoMatrix, isoRange, aspects);
1369 
1370             if (aspects.mPrimaries == ColorAspects::PrimariesUnspecified) {
1371                 int32_t primaries;
1372                 if (meta->findInt32(kKeyColorPrimaries, &primaries)) {
1373                     ALOGV("unspecified primaries found, replaced to %d", primaries);
1374                     aspects.mPrimaries = static_cast<ColorAspects::Primaries>(primaries);
1375                 }
1376             }
1377             if (aspects.mTransfer == ColorAspects::TransferUnspecified) {
1378                 int32_t transferFunction;
1379                 if (meta->findInt32(kKeyTransferFunction, &transferFunction)) {
1380                     ALOGV("unspecified transfer found, replaced to %d", transferFunction);
1381                     aspects.mTransfer = static_cast<ColorAspects::Transfer>(transferFunction);
1382                 }
1383             }
1384             if (aspects.mMatrixCoeffs == ColorAspects::MatrixUnspecified) {
1385                 int32_t colorMatrix;
1386                 if (meta->findInt32(kKeyColorMatrix, &colorMatrix)) {
1387                     ALOGV("unspecified matrix found, replaced to %d", colorMatrix);
1388                     aspects.mMatrixCoeffs = static_cast<ColorAspects::MatrixCoeffs>(colorMatrix);
1389                 }
1390             }
1391             if (aspects.mRange == ColorAspects::RangeUnspecified) {
1392                 int32_t range;
1393                 if (meta->findInt32(kKeyColorRange, &range)) {
1394                     ALOGV("unspecified range found, replaced to %d", range);
1395                     aspects.mRange = static_cast<ColorAspects::Range>(range);
1396                 }
1397             }
1398 
1399             int32_t standard, transfer, range;
1400             if (ColorUtils::convertCodecColorAspectsToPlatformAspects(
1401                     aspects, &range, &standard, &transfer) == OK) {
1402                 msg->setInt32("color-standard", standard);
1403                 msg->setInt32("color-transfer", transfer);
1404                 msg->setInt32("color-range", range);
1405             }
1406         }
1407 
1408         parseHevcProfileLevelFromHvcc((const uint8_t *)data, dataSize, msg);
1409     } else if (meta->findData(kKeyAV1C, &type, &data, &size)) {
1410         sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1411         if (buffer.get() == NULL || buffer->base() == NULL) {
1412             return NO_MEMORY;
1413         }
1414         memcpy(buffer->data(), data, size);
1415 
1416         buffer->meta()->setInt32("csd", true);
1417         buffer->meta()->setInt64("timeUs", 0);
1418         msg->setBuffer("csd-0", buffer);
1419         parseAV1ProfileLevelFromCsd(buffer, msg);
1420     } else if (meta->findData(kKeyESDS, &type, &data, &size)) {
1421         ESDS esds((const char *)data, size);
1422         if (esds.InitCheck() != (status_t)OK) {
1423             return BAD_VALUE;
1424         }
1425 
1426         const void *codec_specific_data;
1427         size_t codec_specific_data_size;
1428         esds.getCodecSpecificInfo(
1429                 &codec_specific_data, &codec_specific_data_size);
1430 
1431         sp<ABuffer> buffer = new (std::nothrow) ABuffer(codec_specific_data_size);
1432         if (buffer.get() == NULL || buffer->base() == NULL) {
1433             return NO_MEMORY;
1434         }
1435 
1436         memcpy(buffer->data(), codec_specific_data,
1437                codec_specific_data_size);
1438 
1439         buffer->meta()->setInt32("csd", true);
1440         buffer->meta()->setInt64("timeUs", 0);
1441         msg->setBuffer("csd-0", buffer);
1442 
1443         if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4)) {
1444             parseMpeg4ProfileLevelFromCsd(buffer, msg);
1445         } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG2)) {
1446             parseMpeg2ProfileLevelFromEsds(esds, msg);
1447             if (meta->findData(kKeyStreamHeader, &type, &data, &size)) {
1448                 parseMpeg2ProfileLevelFromHeader((uint8_t*)data, size, msg);
1449             }
1450         } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) {
1451             parseAacProfileFromCsd(buffer, msg);
1452         }
1453 
1454         uint32_t maxBitrate, avgBitrate;
1455         if (esds.getBitRate(&maxBitrate, &avgBitrate) == OK) {
1456             if (!meta->hasData(kKeyBitRate)
1457                     && avgBitrate > 0 && avgBitrate <= INT32_MAX) {
1458                 msg->setInt32("bitrate", (int32_t)avgBitrate);
1459             } else {
1460                 (void)msg->findInt32("bitrate", (int32_t*)&avgBitrate);
1461             }
1462             if (!meta->hasData(kKeyMaxBitRate)
1463                     && maxBitrate > 0 && maxBitrate <= INT32_MAX && maxBitrate >= avgBitrate) {
1464                 msg->setInt32("max-bitrate", (int32_t)maxBitrate);
1465             }
1466         }
1467     } else if (meta->findData(kKeyD263, &type, &data, &size)) {
1468         const uint8_t *ptr = (const uint8_t *)data;
1469         parseH263ProfileLevelFromD263(ptr, size, msg);
1470     } else if (meta->findData(kKeyOpusHeader, &type, &data, &size)) {
1471         sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1472         if (buffer.get() == NULL || buffer->base() == NULL) {
1473             return NO_MEMORY;
1474         }
1475         memcpy(buffer->data(), data, size);
1476 
1477         buffer->meta()->setInt32("csd", true);
1478         buffer->meta()->setInt64("timeUs", 0);
1479         msg->setBuffer("csd-0", buffer);
1480 
1481         if (!meta->findData(kKeyOpusCodecDelay, &type, &data, &size)) {
1482             return -EINVAL;
1483         }
1484 
1485         buffer = new (std::nothrow) ABuffer(size);
1486         if (buffer.get() == NULL || buffer->base() == NULL) {
1487             return NO_MEMORY;
1488         }
1489         memcpy(buffer->data(), data, size);
1490 
1491         buffer->meta()->setInt32("csd", true);
1492         buffer->meta()->setInt64("timeUs", 0);
1493         msg->setBuffer("csd-1", buffer);
1494 
1495         if (!meta->findData(kKeyOpusSeekPreRoll, &type, &data, &size)) {
1496             return -EINVAL;
1497         }
1498 
1499         buffer = new (std::nothrow) ABuffer(size);
1500         if (buffer.get() == NULL || buffer->base() == NULL) {
1501             return NO_MEMORY;
1502         }
1503         memcpy(buffer->data(), data, size);
1504 
1505         buffer->meta()->setInt32("csd", true);
1506         buffer->meta()->setInt64("timeUs", 0);
1507         msg->setBuffer("csd-2", buffer);
1508     } else if (meta->findData(kKeyVp9CodecPrivate, &type, &data, &size)) {
1509         sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1510         if (buffer.get() == NULL || buffer->base() == NULL) {
1511             return NO_MEMORY;
1512         }
1513         memcpy(buffer->data(), data, size);
1514 
1515         buffer->meta()->setInt32("csd", true);
1516         buffer->meta()->setInt64("timeUs", 0);
1517         msg->setBuffer("csd-0", buffer);
1518 
1519         parseVp9ProfileLevelFromCsd(buffer, msg);
1520     } else if (meta->findData(kKeyAlacMagicCookie, &type, &data, &size)) {
1521         ALOGV("convertMetaDataToMessage found kKeyAlacMagicCookie of size %zu\n", size);
1522         sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1523         if (buffer.get() == NULL || buffer->base() == NULL) {
1524             return NO_MEMORY;
1525         }
1526         memcpy(buffer->data(), data, size);
1527 
1528         buffer->meta()->setInt32("csd", true);
1529         buffer->meta()->setInt64("timeUs", 0);
1530         msg->setBuffer("csd-0", buffer);
1531     }
1532 
1533     if (meta->findData(kKeyDVCC, &type, &data, &size)
1534             || meta->findData(kKeyDVVC, &type, &data, &size)
1535             || meta->findData(kKeyDVWC, &type, &data, &size)) {
1536         const uint8_t *ptr = (const uint8_t *)data;
1537         ALOGV("DV: calling parseDolbyVisionProfileLevelFromDvcc with data size %zu", size);
1538         parseDolbyVisionProfileLevelFromDvcc(ptr, size, msg);
1539         sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1540         if (buffer.get() == nullptr || buffer->base() == nullptr) {
1541             return NO_MEMORY;
1542         }
1543         memcpy(buffer->data(), data, size);
1544 
1545         buffer->meta()->setInt32("csd", true);
1546         buffer->meta()->setInt64("timeUs", 0);
1547         msg->setBuffer("csd-2", buffer);
1548     }
1549 
1550     *format = msg;
1551 
1552     return OK;
1553 }
1554 
findNextNalStartCode(const uint8_t * data,size_t length)1555 const uint8_t *findNextNalStartCode(const uint8_t *data, size_t length) {
1556     uint8_t *res = NULL;
1557     if (length > 4) {
1558         // minus 1 as to not match NAL start code at end
1559         res = (uint8_t *)memmem(data, length - 1, "\x00\x00\x00\x01", 4);
1560     }
1561     return res != NULL && res < data + length - 4 ? res : &data[length];
1562 }
1563 
reassembleAVCC(const sp<ABuffer> & csd0,const sp<ABuffer> & csd1,char * avcc)1564 static size_t reassembleAVCC(const sp<ABuffer> &csd0, const sp<ABuffer> &csd1, char *avcc) {
1565     avcc[0] = 1;        // version
1566     avcc[1] = 0x64;     // profile (default to high)
1567     avcc[2] = 0;        // constraints (default to none)
1568     avcc[3] = 0xd;      // level (default to 1.3)
1569     avcc[4] = 0xff;     // reserved+size
1570 
1571     size_t i = 0;
1572     int numparams = 0;
1573     int lastparamoffset = 0;
1574     int avccidx = 6;
1575     do {
1576         i = findNextNalStartCode(csd0->data() + i, csd0->size() - i) - csd0->data();
1577         ALOGV("block at %zu, last was %d", i, lastparamoffset);
1578         if (lastparamoffset > 0) {
1579             const uint8_t *lastparam = csd0->data() + lastparamoffset;
1580             int size = i - lastparamoffset;
1581             if (size > 3) {
1582                 if (numparams && memcmp(avcc + 1, lastparam + 1, 3)) {
1583                     ALOGW("Inconsisted profile/level found in SPS: %x,%x,%x vs %x,%x,%x",
1584                             avcc[1], avcc[2], avcc[3], lastparam[1], lastparam[2], lastparam[3]);
1585                 } else if (!numparams) {
1586                     // fill in profile, constraints and level
1587                     memcpy(avcc + 1, lastparam + 1, 3);
1588                 }
1589             }
1590             avcc[avccidx++] = size >> 8;
1591             avcc[avccidx++] = size & 0xff;
1592             memcpy(avcc+avccidx, lastparam, size);
1593             avccidx += size;
1594             numparams++;
1595         }
1596         i += 4;
1597         lastparamoffset = i;
1598     } while(i < csd0->size());
1599     ALOGV("csd0 contains %d params", numparams);
1600 
1601     avcc[5] = 0xe0 | numparams;
1602     //and now csd-1
1603     i = 0;
1604     numparams = 0;
1605     lastparamoffset = 0;
1606     int numpicparamsoffset = avccidx;
1607     avccidx++;
1608     do {
1609         i = findNextNalStartCode(csd1->data() + i, csd1->size() - i) - csd1->data();
1610         ALOGV("block at %zu, last was %d", i, lastparamoffset);
1611         if (lastparamoffset > 0) {
1612             int size = i - lastparamoffset;
1613             avcc[avccidx++] = size >> 8;
1614             avcc[avccidx++] = size & 0xff;
1615             memcpy(avcc+avccidx, csd1->data() + lastparamoffset, size);
1616             avccidx += size;
1617             numparams++;
1618         }
1619         i += 4;
1620         lastparamoffset = i;
1621     } while(i < csd1->size());
1622     avcc[numpicparamsoffset] = numparams;
1623     return avccidx;
1624 }
1625 
reassembleESDS(const sp<ABuffer> & csd0,char * esds)1626 static void reassembleESDS(const sp<ABuffer> &csd0, char *esds) {
1627     int csd0size = csd0->size();
1628     esds[0] = 3; // kTag_ESDescriptor;
1629     int esdescriptorsize = 26 + csd0size;
1630     CHECK(esdescriptorsize < 268435456); // 7 bits per byte, so max is 2^28-1
1631     esds[1] = 0x80 | (esdescriptorsize >> 21);
1632     esds[2] = 0x80 | ((esdescriptorsize >> 14) & 0x7f);
1633     esds[3] = 0x80 | ((esdescriptorsize >> 7) & 0x7f);
1634     esds[4] = (esdescriptorsize & 0x7f);
1635     esds[5] = esds[6] = 0; // es id
1636     esds[7] = 0; // flags
1637     esds[8] = 4; // kTag_DecoderConfigDescriptor
1638     int configdescriptorsize = 18 + csd0size;
1639     esds[9] = 0x80 | (configdescriptorsize >> 21);
1640     esds[10] = 0x80 | ((configdescriptorsize >> 14) & 0x7f);
1641     esds[11] = 0x80 | ((configdescriptorsize >> 7) & 0x7f);
1642     esds[12] = (configdescriptorsize & 0x7f);
1643     esds[13] = 0x40; // objectTypeIndication
1644     // bytes 14-25 are examples from a real file. they are unused/overwritten by muxers.
1645     esds[14] = 0x15; // streamType(5), upStream(0),
1646     esds[15] = 0x00; // 15-17: bufferSizeDB (6KB)
1647     esds[16] = 0x18;
1648     esds[17] = 0x00;
1649     esds[18] = 0x00; // 18-21: maxBitrate (64kbps)
1650     esds[19] = 0x00;
1651     esds[20] = 0xfa;
1652     esds[21] = 0x00;
1653     esds[22] = 0x00; // 22-25: avgBitrate (64kbps)
1654     esds[23] = 0x00;
1655     esds[24] = 0xfa;
1656     esds[25] = 0x00;
1657     esds[26] = 5; // kTag_DecoderSpecificInfo;
1658     esds[27] = 0x80 | (csd0size >> 21);
1659     esds[28] = 0x80 | ((csd0size >> 14) & 0x7f);
1660     esds[29] = 0x80 | ((csd0size >> 7) & 0x7f);
1661     esds[30] = (csd0size & 0x7f);
1662     memcpy((void*)&esds[31], csd0->data(), csd0size);
1663     // data following this is ignored, so don't bother appending it
1664 }
1665 
reassembleHVCC(const sp<ABuffer> & csd0,uint8_t * hvcc,size_t hvccSize,size_t nalSizeLength)1666 static size_t reassembleHVCC(const sp<ABuffer> &csd0, uint8_t *hvcc, size_t hvccSize, size_t nalSizeLength) {
1667     HevcParameterSets paramSets;
1668     uint8_t* data = csd0->data();
1669     if (csd0->size() < 4) {
1670         ALOGE("csd0 too small");
1671         return 0;
1672     }
1673     if (memcmp(data, "\x00\x00\x00\x01", 4) != 0) {
1674         ALOGE("csd0 doesn't start with a start code");
1675         return 0;
1676     }
1677     size_t prevNalOffset = 4;
1678     status_t err = OK;
1679     for (size_t i = 1; i < csd0->size() - 4; ++i) {
1680         if (memcmp(&data[i], "\x00\x00\x00\x01", 4) != 0) {
1681             continue;
1682         }
1683         err = paramSets.addNalUnit(&data[prevNalOffset], i - prevNalOffset);
1684         if (err != OK) {
1685             return 0;
1686         }
1687         prevNalOffset = i + 4;
1688     }
1689     err = paramSets.addNalUnit(&data[prevNalOffset], csd0->size() - prevNalOffset);
1690     if (err != OK) {
1691         return 0;
1692     }
1693     size_t size = hvccSize;
1694     err = paramSets.makeHvcc(hvcc, &size, nalSizeLength);
1695     if (err != OK) {
1696         return 0;
1697     }
1698     return size;
1699 }
1700 
1701 #if 0
1702 static void convertMessageToMetaDataInt32(
1703         const sp<AMessage> &msg, sp<MetaData> &meta, uint32_t key, const char *name) {
1704     int32_t value;
1705     if (msg->findInt32(name, &value)) {
1706         meta->setInt32(key, value);
1707     }
1708 }
1709 #endif
1710 
convertMessageToMetaDataColorAspects(const sp<AMessage> & msg,sp<MetaData> & meta)1711 static void convertMessageToMetaDataColorAspects(const sp<AMessage> &msg, sp<MetaData> &meta) {
1712     // 0 values are unspecified
1713     int32_t range = 0, standard = 0, transfer = 0;
1714     (void)msg->findInt32("color-range", &range);
1715     (void)msg->findInt32("color-standard", &standard);
1716     (void)msg->findInt32("color-transfer", &transfer);
1717 
1718     ColorAspects colorAspects;
1719     memset(&colorAspects, 0, sizeof(colorAspects));
1720     if (CodecBase::convertPlatformColorAspectsToCodecAspects(
1721             range, standard, transfer, colorAspects) != OK) {
1722         return;
1723     }
1724 
1725     // save specified values to meta
1726     if (colorAspects.mRange != 0) {
1727         meta->setInt32(kKeyColorRange, colorAspects.mRange);
1728     }
1729     if (colorAspects.mPrimaries != 0) {
1730         meta->setInt32(kKeyColorPrimaries, colorAspects.mPrimaries);
1731     }
1732     if (colorAspects.mTransfer != 0) {
1733         meta->setInt32(kKeyTransferFunction, colorAspects.mTransfer);
1734     }
1735     if (colorAspects.mMatrixCoeffs != 0) {
1736         meta->setInt32(kKeyColorMatrix, colorAspects.mMatrixCoeffs);
1737     }
1738 }
1739 /* Converts key and value pairs in AMessage format to MetaData format.
1740  * Also checks for the presence of required keys.
1741  */
convertMessageToMetaData(const sp<AMessage> & msg,sp<MetaData> & meta)1742 status_t convertMessageToMetaData(const sp<AMessage> &msg, sp<MetaData> &meta) {
1743     AString mime;
1744     if (msg->findString("mime", &mime)) {
1745         meta->setCString(kKeyMIMEType, mime.c_str());
1746     } else {
1747         ALOGV("did not find mime type");
1748         return BAD_VALUE;
1749     }
1750 
1751     convertMessageToMetaDataFromMappings(msg, meta);
1752 
1753     int32_t systemId;
1754     if (msg->findInt32("ca-system-id", &systemId)) {
1755         meta->setInt32(kKeyCASystemID, systemId);
1756 
1757         sp<ABuffer> caSessionId, caPvtData;
1758         if (msg->findBuffer("ca-session-id", &caSessionId)) {
1759             meta->setData(kKeyCASessionID, 0, caSessionId->data(), caSessionId->size());
1760         }
1761         if (msg->findBuffer("ca-private-data", &caPvtData)) {
1762             meta->setData(kKeyCAPrivateData, 0, caPvtData->data(), caPvtData->size());
1763         }
1764     }
1765 
1766     int64_t durationUs;
1767     if (msg->findInt64("durationUs", &durationUs)) {
1768         meta->setInt64(kKeyDuration, durationUs);
1769     }
1770 
1771     int32_t isSync;
1772     if (msg->findInt32("is-sync-frame", &isSync) && isSync != 0) {
1773         meta->setInt32(kKeyIsSyncFrame, 1);
1774     }
1775 
1776     // Mode for media transcoding.
1777     int32_t isBackgroundMode;
1778     if (msg->findInt32("android._background-mode", &isBackgroundMode) && isBackgroundMode != 0) {
1779         meta->setInt32(isBackgroundMode, 1);
1780     }
1781 
1782     int32_t avgBitrate = 0;
1783     int32_t maxBitrate;
1784     if (msg->findInt32("bitrate", &avgBitrate) && avgBitrate > 0) {
1785         meta->setInt32(kKeyBitRate, avgBitrate);
1786     }
1787     if (msg->findInt32("max-bitrate", &maxBitrate) && maxBitrate > 0 && maxBitrate >= avgBitrate) {
1788         meta->setInt32(kKeyMaxBitRate, maxBitrate);
1789     }
1790 
1791     AString lang;
1792     if (msg->findString("language", &lang)) {
1793         meta->setCString(kKeyMediaLanguage, lang.c_str());
1794     }
1795 
1796     if (mime.startsWith("video/") || mime.startsWith("image/")) {
1797         int32_t width;
1798         int32_t height;
1799         if (!msg->findInt32("width", &width) || !msg->findInt32("height", &height)) {
1800             ALOGV("did not find width and/or height");
1801             return BAD_VALUE;
1802         }
1803         if (width <= 0 || height <= 0) {
1804             ALOGE("Invalid value of width: %d and/or height: %d", width, height);
1805             return BAD_VALUE;
1806         }
1807         meta->setInt32(kKeyWidth, width);
1808         meta->setInt32(kKeyHeight, height);
1809 
1810         int32_t sarWidth = -1, sarHeight = -1;
1811         bool foundWidth, foundHeight;
1812         foundWidth = msg->findInt32("sar-width", &sarWidth);
1813         foundHeight = msg->findInt32("sar-height", &sarHeight);
1814         if (foundWidth || foundHeight) {
1815             if (sarWidth <= 0 || sarHeight <= 0) {
1816                 ALOGE("Invalid value of sarWidth: %d and/or sarHeight: %d", sarWidth, sarHeight);
1817                 return BAD_VALUE;
1818             }
1819             meta->setInt32(kKeySARWidth, sarWidth);
1820             meta->setInt32(kKeySARHeight, sarHeight);
1821         }
1822 
1823         int32_t displayWidth = -1, displayHeight = -1;
1824         foundWidth = msg->findInt32("display-width", &displayWidth);
1825         foundHeight = msg->findInt32("display-height", &displayHeight);
1826         if (foundWidth || foundHeight) {
1827             if (displayWidth <= 0 || displayHeight <= 0) {
1828                 ALOGE("Invalid value of displayWidth: %d and/or displayHeight: %d",
1829                         displayWidth, displayHeight);
1830                 return BAD_VALUE;
1831             }
1832             meta->setInt32(kKeyDisplayWidth, displayWidth);
1833             meta->setInt32(kKeyDisplayHeight, displayHeight);
1834         }
1835 
1836         if (mime.startsWith("image/")){
1837             int32_t isPrimary;
1838             if (msg->findInt32("is-default", &isPrimary) && isPrimary) {
1839                 meta->setInt32(kKeyTrackIsDefault, 1);
1840             }
1841             int32_t tileWidth = -1, tileHeight = -1;
1842             foundWidth = msg->findInt32("tile-width", &tileWidth);
1843             foundHeight = msg->findInt32("tile-height", &tileHeight);
1844             if (foundWidth || foundHeight) {
1845                 if (tileWidth <= 0 || tileHeight <= 0) {
1846                     ALOGE("Invalid value of tileWidth: %d and/or tileHeight: %d",
1847                             tileWidth, tileHeight);
1848                     return BAD_VALUE;
1849                 }
1850                 meta->setInt32(kKeyTileWidth, tileWidth);
1851                 meta->setInt32(kKeyTileHeight, tileHeight);
1852             }
1853             int32_t gridRows = -1, gridCols = -1;
1854             bool foundRows, foundCols;
1855             foundRows = msg->findInt32("grid-rows", &gridRows);
1856             foundCols = msg->findInt32("grid-cols", &gridCols);
1857             if (foundRows || foundCols) {
1858                 if (gridRows <= 0 || gridCols <= 0) {
1859                     ALOGE("Invalid value of gridRows: %d and/or gridCols: %d",
1860                             gridRows, gridCols);
1861                     return BAD_VALUE;
1862                 }
1863                 meta->setInt32(kKeyGridRows, gridRows);
1864                 meta->setInt32(kKeyGridCols, gridCols);
1865             }
1866         }
1867 
1868         int32_t colorFormat;
1869         if (msg->findInt32("color-format", &colorFormat)) {
1870             meta->setInt32(kKeyColorFormat, colorFormat);
1871         }
1872 
1873         int32_t cropLeft, cropTop, cropRight, cropBottom;
1874         if (msg->findRect("crop",
1875                           &cropLeft,
1876                           &cropTop,
1877                           &cropRight,
1878                           &cropBottom)) {
1879             if (cropLeft < 0 || cropLeft > cropRight || cropRight >= width) {
1880                 ALOGE("Invalid value of cropLeft: %d and/or cropRight: %d", cropLeft, cropRight);
1881                 return BAD_VALUE;
1882             }
1883             if (cropTop < 0 || cropTop > cropBottom || cropBottom >= height) {
1884                 ALOGE("Invalid value of cropTop: %d and/or cropBottom: %d", cropTop, cropBottom);
1885                 return BAD_VALUE;
1886             }
1887             meta->setRect(kKeyCropRect, cropLeft, cropTop, cropRight, cropBottom);
1888         }
1889 
1890         int32_t rotationDegrees;
1891         if (msg->findInt32("rotation-degrees", &rotationDegrees)) {
1892             meta->setInt32(kKeyRotation, rotationDegrees);
1893         }
1894 
1895         if (msg->contains("hdr-static-info")) {
1896             HDRStaticInfo info;
1897             if (ColorUtils::getHDRStaticInfoFromFormat(msg, &info)) {
1898                 meta->setData(kKeyHdrStaticInfo, 'hdrS', &info, sizeof(info));
1899             }
1900         }
1901 
1902         sp<ABuffer> hdr10PlusInfo;
1903         if (msg->findBuffer("hdr10-plus-info", &hdr10PlusInfo)) {
1904             meta->setData(kKeyHdr10PlusInfo, 0,
1905                     hdr10PlusInfo->data(), hdr10PlusInfo->size());
1906         }
1907 
1908         convertMessageToMetaDataColorAspects(msg, meta);
1909 
1910         AString tsSchema;
1911         if (msg->findString("ts-schema", &tsSchema)) {
1912             unsigned int numLayers = 0;
1913             unsigned int numBLayers = 0;
1914             char dummy;
1915             int tags = sscanf(tsSchema.c_str(), "android.generic.%u%c%u%c",
1916                     &numLayers, &dummy, &numBLayers, &dummy);
1917             if ((tags == 1 || (tags == 3 && dummy == '+'))
1918                     && numLayers > 0 && numLayers < UINT32_MAX - numBLayers
1919                     && numLayers + numBLayers <= INT32_MAX) {
1920                 meta->setInt32(kKeyTemporalLayerCount, numLayers + numBLayers);
1921             }
1922         }
1923     } else if (mime.startsWith("audio/")) {
1924         int32_t numChannels, sampleRate;
1925         if (!msg->findInt32("channel-count", &numChannels) ||
1926                 !msg->findInt32("sample-rate", &sampleRate)) {
1927             ALOGV("did not find channel-count and/or sample-rate");
1928             return BAD_VALUE;
1929         }
1930         // channel count can be zero in some cases like mpeg h
1931         if (sampleRate <= 0 || numChannels < 0) {
1932             ALOGE("Invalid value of channel-count: %d and/or sample-rate: %d",
1933                    numChannels, sampleRate);
1934             return BAD_VALUE;
1935         }
1936         meta->setInt32(kKeyChannelCount, numChannels);
1937         meta->setInt32(kKeySampleRate, sampleRate);
1938         int32_t bitsPerSample;
1939         // TODO:(b/204430952) add appropriate bound check for bitsPerSample
1940         if (msg->findInt32("bits-per-sample", &bitsPerSample)) {
1941             meta->setInt32(kKeyBitsPerSample, bitsPerSample);
1942         }
1943         int32_t channelMask;
1944         if (msg->findInt32("channel-mask", &channelMask)) {
1945             meta->setInt32(kKeyChannelMask, channelMask);
1946         }
1947         int32_t delay = 0;
1948         if (msg->findInt32("encoder-delay", &delay)) {
1949             meta->setInt32(kKeyEncoderDelay, delay);
1950         }
1951         int32_t padding = 0;
1952         if (msg->findInt32("encoder-padding", &padding)) {
1953             meta->setInt32(kKeyEncoderPadding, padding);
1954         }
1955 
1956         int32_t isADTS;
1957         if (msg->findInt32("is-adts", &isADTS)) {
1958             meta->setInt32(kKeyIsADTS, isADTS);
1959         }
1960 
1961         int32_t mpeghProfileLevelIndication = -1;
1962         if (msg->findInt32(AMEDIAFORMAT_KEY_MPEGH_PROFILE_LEVEL_INDICATION,
1963                 &mpeghProfileLevelIndication)) {
1964             meta->setInt32(kKeyMpeghProfileLevelIndication, mpeghProfileLevelIndication);
1965         }
1966         int32_t mpeghReferenceChannelLayout = -1;
1967         if (msg->findInt32(AMEDIAFORMAT_KEY_MPEGH_REFERENCE_CHANNEL_LAYOUT,
1968                 &mpeghReferenceChannelLayout)) {
1969             meta->setInt32(kKeyMpeghReferenceChannelLayout, mpeghReferenceChannelLayout);
1970         }
1971         sp<ABuffer> mpeghCompatibleSets;
1972         if (msg->findBuffer(AMEDIAFORMAT_KEY_MPEGH_COMPATIBLE_SETS,
1973                 &mpeghCompatibleSets)) {
1974             meta->setData(kKeyMpeghCompatibleSets, kTypeHCOS,
1975                     mpeghCompatibleSets->data(), mpeghCompatibleSets->size());
1976         }
1977 
1978         int32_t aacProfile = -1;
1979         if (msg->findInt32("aac-profile", &aacProfile)) {
1980             meta->setInt32(kKeyAACAOT, aacProfile);
1981         }
1982 
1983         int32_t pcmEncoding;
1984         if (msg->findInt32("pcm-encoding", &pcmEncoding)) {
1985             meta->setInt32(kKeyPcmEncoding, pcmEncoding);
1986         }
1987 
1988         int32_t hapticChannelCount;
1989         if (msg->findInt32("haptic-channel-count", &hapticChannelCount)) {
1990             meta->setInt32(kKeyHapticChannelCount, hapticChannelCount);
1991         }
1992     }
1993 
1994     int32_t maxInputSize;
1995     if (msg->findInt32("max-input-size", &maxInputSize)) {
1996         meta->setInt32(kKeyMaxInputSize, maxInputSize);
1997     }
1998 
1999     int32_t maxWidth;
2000     if (msg->findInt32("max-width", &maxWidth)) {
2001         meta->setInt32(kKeyMaxWidth, maxWidth);
2002     }
2003 
2004     int32_t maxHeight;
2005     if (msg->findInt32("max-height", &maxHeight)) {
2006         meta->setInt32(kKeyMaxHeight, maxHeight);
2007     }
2008 
2009     int32_t fps;
2010     float fpsFloat;
2011     if (msg->findInt32("frame-rate", &fps) && fps > 0) {
2012         meta->setInt32(kKeyFrameRate, fps);
2013     } else if (msg->findFloat("frame-rate", &fpsFloat)
2014             && fpsFloat >= 1 && fpsFloat <= (float)INT32_MAX) {
2015         // truncate values to distinguish between e.g. 24 vs 23.976 fps
2016         meta->setInt32(kKeyFrameRate, (int32_t)fpsFloat);
2017     }
2018 
2019     // reassemble the csd data into its original form
2020     sp<ABuffer> csd0, csd1, csd2;
2021     if (msg->findBuffer("csd-0", &csd0)) {
2022         int csd0size = csd0->size();
2023         if (mime == MEDIA_MIMETYPE_VIDEO_AVC) {
2024             sp<ABuffer> csd1;
2025             if (msg->findBuffer("csd-1", &csd1)) {
2026                 std::vector<char> avcc(csd0size + csd1->size() + 1024);
2027                 size_t outsize = reassembleAVCC(csd0, csd1, avcc.data());
2028                 meta->setData(kKeyAVCC, kTypeAVCC, avcc.data(), outsize);
2029             }
2030         } else if (mime == MEDIA_MIMETYPE_AUDIO_AAC ||
2031                 mime == MEDIA_MIMETYPE_VIDEO_MPEG4 ||
2032                 mime == MEDIA_MIMETYPE_AUDIO_WMA ||
2033                 mime == MEDIA_MIMETYPE_AUDIO_MS_ADPCM ||
2034                 mime == MEDIA_MIMETYPE_AUDIO_DVI_IMA_ADPCM) {
2035             std::vector<char> esds(csd0size + 31);
2036             // The written ESDS is actually for an audio stream, but it's enough
2037             // for transporting the CSD to muxers.
2038             reassembleESDS(csd0, esds.data());
2039             meta->setData(kKeyESDS, kTypeESDS, esds.data(), esds.size());
2040         } else if (mime == MEDIA_MIMETYPE_VIDEO_HEVC ||
2041                    mime == MEDIA_MIMETYPE_IMAGE_ANDROID_HEIC) {
2042             std::vector<uint8_t> hvcc(csd0size + 1024);
2043             size_t outsize = reassembleHVCC(csd0, hvcc.data(), hvcc.size(), 4);
2044             meta->setData(kKeyHVCC, kTypeHVCC, hvcc.data(), outsize);
2045         } else if (mime == MEDIA_MIMETYPE_VIDEO_AV1 ||
2046                    mime == MEDIA_MIMETYPE_IMAGE_AVIF) {
2047             meta->setData(kKeyAV1C, 0, csd0->data(), csd0->size());
2048         } else if (mime == MEDIA_MIMETYPE_VIDEO_DOLBY_VISION) {
2049             int32_t profile = -1;
2050             uint8_t blCompatibilityId = -1;
2051             int32_t level = 0;
2052             uint8_t profileVal = -1;
2053             uint8_t profileVal1 = -1;
2054             uint8_t profileVal2 = -1;
2055             constexpr size_t dvccSize = 24;
2056 
2057             const ALookup<uint8_t, int32_t> &profiles =
2058                 getDolbyVisionProfileTable();
2059             const ALookup<uint8_t, int32_t> &levels =
2060                 getDolbyVisionLevelsTable();
2061 
2062             if (!msg->findBuffer("csd-2", &csd2)) {
2063                 // MP4 extractors are expected to generate csd buffer
2064                 // some encoders might not be generating it, in which
2065                 // case we populate the track metadata dv (cc|vc|wc)
2066                 // from the 'profile' and 'level' info.
2067                 // This is done according to Dolby Vision ISOBMFF spec
2068 
2069                 if (!msg->findInt32("profile", &profile)) {
2070                     ALOGE("Dolby Vision profile not found");
2071                     return BAD_VALUE;
2072                 }
2073                 msg->findInt32("level", &level);
2074 
2075                 if (profile == DolbyVisionProfileDvheSt) {
2076                     if (!profiles.rlookup(DolbyVisionProfileDvheSt, &profileVal)) { // dvhe.08
2077                         ALOGE("Dolby Vision profile lookup error");
2078                         return BAD_VALUE;
2079                     }
2080                     blCompatibilityId = 4;
2081                 } else if (profile == DolbyVisionProfileDvavSe) {
2082                     if (!profiles.rlookup(DolbyVisionProfileDvavSe, &profileVal)) { // dvav.09
2083                         ALOGE("Dolby Vision profile lookup error");
2084                         return BAD_VALUE;
2085                     }
2086                     blCompatibilityId = 2;
2087                 } else {
2088                     ALOGE("Dolby Vision profile look up error");
2089                     return BAD_VALUE;
2090                 }
2091 
2092                 profile = (int32_t) profileVal;
2093 
2094                 uint8_t level_val = 0;
2095                 if (!levels.map(level, &level_val)) {
2096                     ALOGE("Dolby Vision level lookup error");
2097                     return BAD_VALUE;
2098                 }
2099 
2100                 std::vector<uint8_t> dvcc(dvccSize);
2101 
2102                 dvcc[0] = 1; // major version
2103                 dvcc[1] = 0; // minor version
2104                 dvcc[2] = (uint8_t)((profile & 0x7f) << 1); // dolby vision profile
2105                 dvcc[2] = (uint8_t)((dvcc[2] | (uint8_t)((level_val >> 5) & 0x1)) & 0xff);
2106                 dvcc[3] = (uint8_t)((level_val & 0x1f) << 3); // dolby vision level
2107                 dvcc[3] = (uint8_t)(dvcc[3] | (1 << 2)); // rpu_present_flag
2108                 dvcc[3] = (uint8_t)(dvcc[3] | (1)); // bl_present_flag
2109                 dvcc[4] = (uint8_t)(blCompatibilityId << 4); // bl_compatibility id
2110 
2111                 profiles.rlookup(DolbyVisionProfileDvav110, &profileVal);
2112                 profiles.rlookup(DolbyVisionProfileDvheDtb, &profileVal1);
2113                 if (profile > (int32_t) profileVal) {
2114                     meta->setData(kKeyDVWC, kTypeDVWC, dvcc.data(), dvccSize);
2115                 } else if (profile > (int32_t) profileVal1) {
2116                     meta->setData(kKeyDVVC, kTypeDVVC, dvcc.data(), dvccSize);
2117                 } else {
2118                     meta->setData(kKeyDVCC, kTypeDVCC, dvcc.data(), dvccSize);
2119                 }
2120 
2121             } else {
2122                 // we have csd-2, just use that to populate dvcc
2123                 if (csd2->size() == dvccSize) {
2124                     uint8_t *dvcc = csd2->data();
2125                     profile = dvcc[2] >> 1;
2126 
2127                     profiles.rlookup(DolbyVisionProfileDvav110, &profileVal);
2128                     profiles.rlookup(DolbyVisionProfileDvheDtb, &profileVal1);
2129                     if (profile > (int32_t) profileVal) {
2130                         meta->setData(kKeyDVWC, kTypeDVWC, csd2->data(), csd2->size());
2131                     } else if (profile > (int32_t) profileVal1) {
2132                         meta->setData(kKeyDVVC, kTypeDVVC, csd2->data(), csd2->size());
2133                     } else {
2134                          meta->setData(kKeyDVCC, kTypeDVCC, csd2->data(), csd2->size());
2135                     }
2136 
2137                 } else {
2138                     ALOGE("Convert MessageToMetadata csd-2 is present but not valid");
2139                     return BAD_VALUE;
2140                 }
2141             }
2142             profiles.rlookup(DolbyVisionProfileDvavPen, &profileVal);
2143             profiles.rlookup(DolbyVisionProfileDvavSe, &profileVal1);
2144             profiles.rlookup(DolbyVisionProfileDvav110, &profileVal2);
2145             if ((profile > (int32_t) profileVal) && (profile < (int32_t) profileVal1)) {
2146                 std::vector<uint8_t> hvcc(csd0size + 1024);
2147                 size_t outsize = reassembleHVCC(csd0, hvcc.data(), hvcc.size(), 4);
2148                 meta->setData(kKeyHVCC, kTypeHVCC, hvcc.data(), outsize);
2149             } else if (profile == (int32_t) profileVal2) {
2150                 meta->setData(kKeyAV1C, 0, csd0->data(), csd0->size());
2151             } else {
2152                 sp<ABuffer> csd1;
2153                 if (msg->findBuffer("csd-1", &csd1)) {
2154                     std::vector<char> avcc(csd0size + csd1->size() + 1024);
2155                     size_t outsize = reassembleAVCC(csd0, csd1, avcc.data());
2156                     meta->setData(kKeyAVCC, kTypeAVCC, avcc.data(), outsize);
2157                 }
2158                 else {
2159                     // for dolby vision avc, csd0 also holds csd1
2160                     size_t i = 0;
2161                     int csd0realsize = 0;
2162                     do {
2163                         i = findNextNalStartCode(csd0->data() + i,
2164                                         csd0->size() - i) - csd0->data();
2165                         if (i > 0) {
2166                             csd0realsize = i;
2167                             break;
2168                         }
2169                         i += 4;
2170                     } while(i < csd0->size());
2171                     // buffer0 -> csd0
2172                     sp<ABuffer> buffer0 = new (std::nothrow) ABuffer(csd0realsize);
2173                     if (buffer0.get() == NULL || buffer0->base() == NULL) {
2174                         return NO_MEMORY;
2175                     }
2176                     memcpy(buffer0->data(), csd0->data(), csd0realsize);
2177                     // buffer1 -> csd1
2178                     sp<ABuffer> buffer1 = new (std::nothrow)
2179                             ABuffer(csd0->size() - csd0realsize);
2180                     if (buffer1.get() == NULL || buffer1->base() == NULL) {
2181                         return NO_MEMORY;
2182                     }
2183                     memcpy(buffer1->data(), csd0->data()+csd0realsize,
2184                                 csd0->size() - csd0realsize);
2185 
2186                     std::vector<char> avcc(csd0->size() + 1024);
2187                     size_t outsize = reassembleAVCC(buffer0, buffer1, avcc.data());
2188                     meta->setData(kKeyAVCC, kTypeAVCC, avcc.data(), outsize);
2189                 }
2190             }
2191         } else if (mime == MEDIA_MIMETYPE_VIDEO_VP9) {
2192             meta->setData(kKeyVp9CodecPrivate, 0, csd0->data(), csd0->size());
2193         } else if (mime == MEDIA_MIMETYPE_AUDIO_OPUS) {
2194             size_t opusHeadSize = csd0->size();
2195             size_t codecDelayBufSize = 0;
2196             size_t seekPreRollBufSize = 0;
2197             void *opusHeadBuf = csd0->data();
2198             void *codecDelayBuf = NULL;
2199             void *seekPreRollBuf = NULL;
2200             if (msg->findBuffer("csd-1", &csd1)) {
2201                 codecDelayBufSize = csd1->size();
2202                 codecDelayBuf = csd1->data();
2203             }
2204             if (msg->findBuffer("csd-2", &csd2)) {
2205                 seekPreRollBufSize = csd2->size();
2206                 seekPreRollBuf = csd2->data();
2207             }
2208             /* Extract codec delay and seek pre roll from csd-0,
2209              * if csd-1 and csd-2 are not present */
2210             if (!codecDelayBuf && !seekPreRollBuf) {
2211                 GetOpusHeaderBuffers(csd0->data(), csd0->size(), &opusHeadBuf,
2212                                     &opusHeadSize, &codecDelayBuf,
2213                                     &codecDelayBufSize, &seekPreRollBuf,
2214                                     &seekPreRollBufSize);
2215             }
2216             meta->setData(kKeyOpusHeader, 0, opusHeadBuf, opusHeadSize);
2217             if (codecDelayBuf) {
2218                 meta->setData(kKeyOpusCodecDelay, 0, codecDelayBuf, codecDelayBufSize);
2219             }
2220             if (seekPreRollBuf) {
2221                 meta->setData(kKeyOpusSeekPreRoll, 0, seekPreRollBuf, seekPreRollBufSize);
2222             }
2223         } else if (mime == MEDIA_MIMETYPE_AUDIO_ALAC) {
2224             meta->setData(kKeyAlacMagicCookie, 0, csd0->data(), csd0->size());
2225         }
2226     } else if (mime == MEDIA_MIMETYPE_VIDEO_AVC && msg->findBuffer("csd-avc", &csd0)) {
2227         meta->setData(kKeyAVCC, kTypeAVCC, csd0->data(), csd0->size());
2228     } else if ((mime == MEDIA_MIMETYPE_VIDEO_HEVC || mime == MEDIA_MIMETYPE_IMAGE_ANDROID_HEIC)
2229             && msg->findBuffer("csd-hevc", &csd0)) {
2230         meta->setData(kKeyHVCC, kTypeHVCC, csd0->data(), csd0->size());
2231     } else if (msg->findBuffer("esds", &csd0)) {
2232         meta->setData(kKeyESDS, kTypeESDS, csd0->data(), csd0->size());
2233     } else if (msg->findBuffer("mpeg2-stream-header", &csd0)) {
2234         meta->setData(kKeyStreamHeader, 'mdat', csd0->data(), csd0->size());
2235     } else if (msg->findBuffer("d263", &csd0)) {
2236         meta->setData(kKeyD263, kTypeD263, csd0->data(), csd0->size());
2237     } else if (mime == MEDIA_MIMETYPE_VIDEO_DOLBY_VISION && msg->findBuffer("csd-2", &csd2)) {
2238         meta->setData(kKeyDVCC, kTypeDVCC, csd2->data(), csd2->size());
2239 
2240         // Remove CSD-2 from the data here to avoid duplicate data in meta
2241         meta->remove(kKeyOpaqueCSD2);
2242 
2243         if (msg->findBuffer("csd-avc", &csd0)) {
2244             meta->setData(kKeyAVCC, kTypeAVCC, csd0->data(), csd0->size());
2245         } else if (msg->findBuffer("csd-hevc", &csd0)) {
2246             meta->setData(kKeyHVCC, kTypeHVCC, csd0->data(), csd0->size());
2247         }
2248     }
2249     // XXX TODO add whatever other keys there are
2250 
2251 #if 0
2252     ALOGI("converted %s to:", msg->debugString(0).c_str());
2253     meta->dumpToLog();
2254 #endif
2255     return OK;
2256 }
2257 
sendMetaDataToHal(sp<MediaPlayerBase::AudioSink> & sink,const sp<MetaData> & meta)2258 status_t sendMetaDataToHal(sp<MediaPlayerBase::AudioSink>& sink,
2259                            const sp<MetaData>& meta)
2260 {
2261     int32_t sampleRate = 0;
2262     int32_t bitRate = 0;
2263     int32_t channelMask = 0;
2264     int32_t delaySamples = 0;
2265     int32_t paddingSamples = 0;
2266 
2267     AudioParameter param = AudioParameter();
2268 
2269     if (meta->findInt32(kKeySampleRate, &sampleRate)) {
2270         param.addInt(String8(AUDIO_OFFLOAD_CODEC_SAMPLE_RATE), sampleRate);
2271     }
2272     if (meta->findInt32(kKeyChannelMask, &channelMask)) {
2273         param.addInt(String8(AUDIO_OFFLOAD_CODEC_NUM_CHANNEL), channelMask);
2274     }
2275     if (meta->findInt32(kKeyBitRate, &bitRate)) {
2276         param.addInt(String8(AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE), bitRate);
2277     }
2278     if (meta->findInt32(kKeyEncoderDelay, &delaySamples)) {
2279         param.addInt(String8(AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES), delaySamples);
2280     }
2281     if (meta->findInt32(kKeyEncoderPadding, &paddingSamples)) {
2282         param.addInt(String8(AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES), paddingSamples);
2283     }
2284 
2285     ALOGV("sendMetaDataToHal: bitRate %d, sampleRate %d, chanMask %d,"
2286           "delaySample %d, paddingSample %d", bitRate, sampleRate,
2287           channelMask, delaySamples, paddingSamples);
2288 
2289     sink->setParameters(param.toString());
2290     return OK;
2291 }
2292 
2293 struct mime_conv_t {
2294     const char* mime;
2295     audio_format_t format;
2296 };
2297 
2298 static const struct mime_conv_t mimeLookup[] = {
2299     { MEDIA_MIMETYPE_AUDIO_MPEG,        AUDIO_FORMAT_MP3 },
2300     { MEDIA_MIMETYPE_AUDIO_RAW,         AUDIO_FORMAT_PCM_16_BIT },
2301     { MEDIA_MIMETYPE_AUDIO_AMR_NB,      AUDIO_FORMAT_AMR_NB },
2302     { MEDIA_MIMETYPE_AUDIO_AMR_WB,      AUDIO_FORMAT_AMR_WB },
2303     { MEDIA_MIMETYPE_AUDIO_AAC,         AUDIO_FORMAT_AAC },
2304     { MEDIA_MIMETYPE_AUDIO_VORBIS,      AUDIO_FORMAT_VORBIS },
2305     { MEDIA_MIMETYPE_AUDIO_OPUS,        AUDIO_FORMAT_OPUS},
2306     { MEDIA_MIMETYPE_AUDIO_AC3,         AUDIO_FORMAT_AC3},
2307     { MEDIA_MIMETYPE_AUDIO_EAC3,        AUDIO_FORMAT_E_AC3},
2308     { MEDIA_MIMETYPE_AUDIO_EAC3_JOC,    AUDIO_FORMAT_E_AC3_JOC},
2309     { MEDIA_MIMETYPE_AUDIO_AC4,         AUDIO_FORMAT_AC4},
2310     { MEDIA_MIMETYPE_AUDIO_FLAC,        AUDIO_FORMAT_FLAC},
2311     { MEDIA_MIMETYPE_AUDIO_ALAC,        AUDIO_FORMAT_ALAC },
2312     { 0, AUDIO_FORMAT_INVALID }
2313 };
2314 
mapMimeToAudioFormat(audio_format_t & format,const char * mime)2315 status_t mapMimeToAudioFormat( audio_format_t& format, const char* mime )
2316 {
2317 const struct mime_conv_t* p = &mimeLookup[0];
2318     while (p->mime != NULL) {
2319         if (0 == strcasecmp(mime, p->mime)) {
2320             format = p->format;
2321             return OK;
2322         }
2323         ++p;
2324     }
2325 
2326     return BAD_VALUE;
2327 }
2328 
2329 struct aac_format_conv_t {
2330     int32_t eAacProfileType;
2331     audio_format_t format;
2332 };
2333 
2334 static const struct aac_format_conv_t profileLookup[] = {
2335     { AACObjectMain,        AUDIO_FORMAT_AAC_MAIN},
2336     { AACObjectLC,          AUDIO_FORMAT_AAC_LC},
2337     { AACObjectSSR,         AUDIO_FORMAT_AAC_SSR},
2338     { AACObjectLTP,         AUDIO_FORMAT_AAC_LTP},
2339     { AACObjectHE,          AUDIO_FORMAT_AAC_HE_V1},
2340     { AACObjectScalable,    AUDIO_FORMAT_AAC_SCALABLE},
2341     { AACObjectERLC,        AUDIO_FORMAT_AAC_ERLC},
2342     { AACObjectLD,          AUDIO_FORMAT_AAC_LD},
2343     { AACObjectHE_PS,       AUDIO_FORMAT_AAC_HE_V2},
2344     { AACObjectELD,         AUDIO_FORMAT_AAC_ELD},
2345     { AACObjectXHE,         AUDIO_FORMAT_AAC_XHE},
2346     { AACObjectNull,        AUDIO_FORMAT_AAC},
2347 };
2348 
mapAACProfileToAudioFormat(audio_format_t & format,uint64_t eAacProfile)2349 void mapAACProfileToAudioFormat( audio_format_t& format, uint64_t eAacProfile)
2350 {
2351     const struct aac_format_conv_t* p = &profileLookup[0];
2352     while (p->eAacProfileType != AACObjectNull) {
2353         if (eAacProfile == p->eAacProfileType) {
2354             format = p->format;
2355             return;
2356         }
2357         ++p;
2358     }
2359     format = AUDIO_FORMAT_AAC;
2360     return;
2361 }
2362 
getAudioOffloadInfo(const sp<MetaData> & meta,bool hasVideo,bool isStreaming,audio_stream_type_t streamType,audio_offload_info_t * info)2363 status_t getAudioOffloadInfo(const sp<MetaData>& meta, bool hasVideo,
2364         bool isStreaming, audio_stream_type_t streamType, audio_offload_info_t *info)
2365 {
2366     const char *mime;
2367     if (meta == NULL) {
2368         return BAD_VALUE;
2369     }
2370     CHECK(meta->findCString(kKeyMIMEType, &mime));
2371 
2372     (*info) = AUDIO_INFO_INITIALIZER;
2373 
2374     info->format = AUDIO_FORMAT_INVALID;
2375     if (mapMimeToAudioFormat(info->format, mime) != OK) {
2376         ALOGE(" Couldn't map mime type \"%s\" to a valid AudioSystem::audio_format !", mime);
2377         return BAD_VALUE;
2378     } else {
2379         ALOGV("Mime type \"%s\" mapped to audio_format %d", mime, info->format);
2380     }
2381 
2382     if (AUDIO_FORMAT_INVALID == info->format) {
2383         // can't offload if we don't know what the source format is
2384         ALOGE("mime type \"%s\" not a known audio format", mime);
2385         return BAD_VALUE;
2386     }
2387 
2388     // Redefine aac format according to its profile
2389     // Offloading depends on audio DSP capabilities.
2390     int32_t aacaot = -1;
2391     if (meta->findInt32(kKeyAACAOT, &aacaot)) {
2392         mapAACProfileToAudioFormat(info->format, aacaot);
2393     }
2394 
2395     int32_t srate = -1;
2396     if (!meta->findInt32(kKeySampleRate, &srate)) {
2397         ALOGV("track of type '%s' does not publish sample rate", mime);
2398     }
2399     info->sample_rate = srate;
2400 
2401     int32_t rawChannelMask;
2402     audio_channel_mask_t cmask = meta->findInt32(kKeyChannelMask, &rawChannelMask) ?
2403             static_cast<audio_channel_mask_t>(rawChannelMask) : CHANNEL_MASK_USE_CHANNEL_ORDER;
2404     if (cmask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
2405         ALOGV("track of type '%s' does not publish channel mask", mime);
2406 
2407         // Try a channel count instead
2408         int32_t channelCount;
2409         if (!meta->findInt32(kKeyChannelCount, &channelCount)) {
2410             ALOGV("track of type '%s' does not publish channel count", mime);
2411         } else {
2412             cmask = audio_channel_out_mask_from_count(channelCount);
2413         }
2414     }
2415     info->channel_mask = cmask;
2416 
2417     int64_t duration = 0;
2418     if (!meta->findInt64(kKeyDuration, &duration)) {
2419         ALOGV("track of type '%s' does not publish duration", mime);
2420     }
2421     info->duration_us = duration;
2422 
2423     int32_t brate = 0;
2424     if (!meta->findInt32(kKeyBitRate, &brate)) {
2425         ALOGV("track of type '%s' does not publish bitrate", mime);
2426     }
2427     info->bit_rate = brate;
2428 
2429 
2430     info->stream_type = streamType;
2431     info->has_video = hasVideo;
2432     info->is_streaming = isStreaming;
2433     return OK;
2434 }
2435 
canOffloadStream(const sp<MetaData> & meta,bool hasVideo,bool isStreaming,audio_stream_type_t streamType)2436 bool canOffloadStream(const sp<MetaData>& meta, bool hasVideo,
2437                       bool isStreaming, audio_stream_type_t streamType)
2438 {
2439     audio_offload_info_t info = AUDIO_INFO_INITIALIZER;
2440     if (OK != getAudioOffloadInfo(meta, hasVideo, isStreaming, streamType, &info)) {
2441         return false;
2442     }
2443     // Check if offload is possible for given format, stream type, sample rate,
2444     // bit rate, duration, video and streaming
2445 #ifdef DISABLE_AUDIO_SYSTEM_OFFLOAD
2446     return false;
2447 #else
2448     return AudioSystem::getOffloadSupport(info) != AUDIO_OFFLOAD_NOT_SUPPORTED;
2449 #endif
2450 }
2451 
HLSTime(const sp<AMessage> & meta)2452 HLSTime::HLSTime(const sp<AMessage>& meta) :
2453     mSeq(-1),
2454     mTimeUs(-1LL),
2455     mMeta(meta) {
2456     if (meta != NULL) {
2457         CHECK(meta->findInt32("discontinuitySeq", &mSeq));
2458         CHECK(meta->findInt64("timeUs", &mTimeUs));
2459     }
2460 }
2461 
getSegmentTimeUs() const2462 int64_t HLSTime::getSegmentTimeUs() const {
2463     int64_t segmentStartTimeUs = -1LL;
2464     if (mMeta != NULL) {
2465         CHECK(mMeta->findInt64("segmentStartTimeUs", &segmentStartTimeUs));
2466 
2467         int64_t segmentFirstTimeUs;
2468         if (mMeta->findInt64("segmentFirstTimeUs", &segmentFirstTimeUs)) {
2469             segmentStartTimeUs += mTimeUs - segmentFirstTimeUs;
2470         }
2471 
2472         // adjust segment time by playlist age (for live streaming)
2473         int64_t playlistTimeUs;
2474         if (mMeta->findInt64("playlistTimeUs", &playlistTimeUs)) {
2475             int64_t playlistAgeUs = ALooper::GetNowUs() - playlistTimeUs;
2476 
2477             int64_t durationUs;
2478             CHECK(mMeta->findInt64("segmentDurationUs", &durationUs));
2479 
2480             // round to nearest whole segment
2481             playlistAgeUs = (playlistAgeUs + durationUs / 2)
2482                     / durationUs * durationUs;
2483 
2484             segmentStartTimeUs -= playlistAgeUs;
2485             if (segmentStartTimeUs < 0) {
2486                 segmentStartTimeUs = 0;
2487             }
2488         }
2489     }
2490     return segmentStartTimeUs;
2491 }
2492 
operator <(const HLSTime & t0,const HLSTime & t1)2493 bool operator <(const HLSTime &t0, const HLSTime &t1) {
2494     // we can only compare discontinuity sequence and timestamp.
2495     // (mSegmentTimeUs is not reliable in live streaming case, it's the
2496     // time starting from beginning of playlist but playlist could change.)
2497     return t0.mSeq < t1.mSeq
2498             || (t0.mSeq == t1.mSeq && t0.mTimeUs < t1.mTimeUs);
2499 }
2500 
writeToAMessage(const sp<AMessage> & msg,const AudioPlaybackRate & rate)2501 void writeToAMessage(const sp<AMessage> &msg, const AudioPlaybackRate &rate) {
2502     msg->setFloat("speed", rate.mSpeed);
2503     msg->setFloat("pitch", rate.mPitch);
2504     msg->setInt32("audio-fallback-mode", rate.mFallbackMode);
2505     msg->setInt32("audio-stretch-mode", rate.mStretchMode);
2506 }
2507 
readFromAMessage(const sp<AMessage> & msg,AudioPlaybackRate * rate)2508 void readFromAMessage(const sp<AMessage> &msg, AudioPlaybackRate *rate /* nonnull */) {
2509     *rate = AUDIO_PLAYBACK_RATE_DEFAULT;
2510     CHECK(msg->findFloat("speed", &rate->mSpeed));
2511     CHECK(msg->findFloat("pitch", &rate->mPitch));
2512     CHECK(msg->findInt32("audio-fallback-mode", (int32_t *)&rate->mFallbackMode));
2513     CHECK(msg->findInt32("audio-stretch-mode", (int32_t *)&rate->mStretchMode));
2514 }
2515 
writeToAMessage(const sp<AMessage> & msg,const AVSyncSettings & sync,float videoFpsHint)2516 void writeToAMessage(const sp<AMessage> &msg, const AVSyncSettings &sync, float videoFpsHint) {
2517     msg->setInt32("sync-source", sync.mSource);
2518     msg->setInt32("audio-adjust-mode", sync.mAudioAdjustMode);
2519     msg->setFloat("tolerance", sync.mTolerance);
2520     msg->setFloat("video-fps", videoFpsHint);
2521 }
2522 
readFromAMessage(const sp<AMessage> & msg,AVSyncSettings * sync,float * videoFps)2523 void readFromAMessage(
2524         const sp<AMessage> &msg,
2525         AVSyncSettings *sync /* nonnull */,
2526         float *videoFps /* nonnull */) {
2527     AVSyncSettings settings;
2528     CHECK(msg->findInt32("sync-source", (int32_t *)&settings.mSource));
2529     CHECK(msg->findInt32("audio-adjust-mode", (int32_t *)&settings.mAudioAdjustMode));
2530     CHECK(msg->findFloat("tolerance", &settings.mTolerance));
2531     CHECK(msg->findFloat("video-fps", videoFps));
2532     *sync = settings;
2533 }
2534 
writeToAMessage(const sp<AMessage> & msg,const BufferingSettings & buffering)2535 void writeToAMessage(const sp<AMessage> &msg, const BufferingSettings &buffering) {
2536     msg->setInt32("init-ms", buffering.mInitialMarkMs);
2537     msg->setInt32("resume-playback-ms", buffering.mResumePlaybackMarkMs);
2538 }
2539 
readFromAMessage(const sp<AMessage> & msg,BufferingSettings * buffering)2540 void readFromAMessage(const sp<AMessage> &msg, BufferingSettings *buffering /* nonnull */) {
2541     int32_t value;
2542     if (msg->findInt32("init-ms", &value)) {
2543         buffering->mInitialMarkMs = value;
2544     }
2545     if (msg->findInt32("resume-playback-ms", &value)) {
2546         buffering->mResumePlaybackMarkMs = value;
2547     }
2548 }
2549 
2550 }  // namespace android
2551