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 <media/esds/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         { "dvb-component-tag", kKeyDvbComponentTag},
798         { "dvb-audio-description", kKeyDvbAudioDescription},
799         { "dvb-teletext-magazine-number", kKeyDvbTeletextMagazineNumber},
800         { "dvb-teletext-page-number", kKeyDvbTeletextPageNumber},
801         { "profile", kKeyAudioProfile },
802         { "level", kKeyAudioLevel },
803     }
804 };
805 
806 static std::vector<std::pair<const char *, uint32_t>> bufferMappings {
807     {
808         { "albumart", kKeyAlbumArt },
809         { "audio-presentation-info", kKeyAudioPresentationInfo },
810         { "pssh", kKeyPssh },
811         { "crypto-iv", kKeyCryptoIV },
812         { "crypto-key", kKeyCryptoKey },
813         { "crypto-encrypted-sizes", kKeyEncryptedSizes },
814         { "crypto-plain-sizes", kKeyPlainSizes },
815         { "icc-profile", kKeyIccProfile },
816         { "sei", kKeySEI },
817         { "text-format-data", kKeyTextFormatData },
818         { "thumbnail-csd-hevc", kKeyThumbnailHVCC },
819         { "slow-motion-markers", kKeySlowMotionMarkers },
820         { "thumbnail-csd-av1c", kKeyThumbnailAV1C },
821     }
822 };
823 
824 static std::vector<std::pair<const char *, uint32_t>> CSDMappings {
825     {
826         { "csd-0", kKeyOpaqueCSD0 },
827         { "csd-1", kKeyOpaqueCSD1 },
828         { "csd-2", kKeyOpaqueCSD2 },
829     }
830 };
831 
convertMessageToMetaDataFromMappings(const sp<AMessage> & msg,sp<MetaData> & meta)832 void convertMessageToMetaDataFromMappings(const sp<AMessage> &msg, sp<MetaData> &meta) {
833     for (auto elem : stringMappings) {
834         AString value;
835         if (msg->findString(elem.first, &value)) {
836             meta->setCString(elem.second, value.c_str());
837         }
838     }
839 
840     for (auto elem : floatMappings) {
841         float value;
842         if (msg->findFloat(elem.first, &value)) {
843             meta->setFloat(elem.second, value);
844         }
845     }
846 
847     for (auto elem : int64Mappings) {
848         int64_t value;
849         if (msg->findInt64(elem.first, &value)) {
850             meta->setInt64(elem.second, value);
851         }
852     }
853 
854     for (auto elem : int32Mappings) {
855         int32_t value;
856         if (msg->findInt32(elem.first, &value)) {
857             meta->setInt32(elem.second, value);
858         }
859     }
860 
861     for (auto elem : bufferMappings) {
862         sp<ABuffer> value;
863         if (msg->findBuffer(elem.first, &value)) {
864             meta->setData(elem.second,
865                     MetaDataBase::Type::TYPE_NONE, value->data(), value->size());
866         }
867     }
868 
869     for (auto elem : CSDMappings) {
870         sp<ABuffer> value;
871         if (msg->findBuffer(elem.first, &value)) {
872             meta->setData(elem.second,
873                     MetaDataBase::Type::TYPE_NONE, value->data(), value->size());
874         }
875     }
876 }
877 
convertMetaDataToMessageFromMappings(const MetaDataBase * meta,sp<AMessage> format)878 void convertMetaDataToMessageFromMappings(const MetaDataBase *meta, sp<AMessage> format) {
879     for (auto elem : stringMappings) {
880         const char *value;
881         if (meta->findCString(elem.second, &value)) {
882             format->setString(elem.first, value, strlen(value));
883         }
884     }
885 
886     for (auto elem : floatMappings) {
887         float value;
888         if (meta->findFloat(elem.second, &value)) {
889             format->setFloat(elem.first, value);
890         }
891     }
892 
893     for (auto elem : int64Mappings) {
894         int64_t value;
895         if (meta->findInt64(elem.second, &value)) {
896             format->setInt64(elem.first, value);
897         }
898     }
899 
900     for (auto elem : int32Mappings) {
901         int32_t value;
902         if (meta->findInt32(elem.second, &value)) {
903             format->setInt32(elem.first, value);
904         }
905     }
906 
907     for (auto elem : bufferMappings) {
908         uint32_t type;
909         const void* data;
910         size_t size;
911         if (meta->findData(elem.second, &type, &data, &size)) {
912             sp<ABuffer> buf = ABuffer::CreateAsCopy(data, size);
913             format->setBuffer(elem.first, buf);
914         }
915     }
916 
917     for (auto elem : CSDMappings) {
918         uint32_t type;
919         const void* data;
920         size_t size;
921         if (meta->findData(elem.second, &type, &data, &size)) {
922             sp<ABuffer> buf = ABuffer::CreateAsCopy(data, size);
923             buf->meta()->setInt32("csd", true);
924             buf->meta()->setInt64("timeUs", 0);
925             format->setBuffer(elem.first, buf);
926         }
927     }
928 }
929 
convertMetaDataToMessage(const sp<MetaData> & meta,sp<AMessage> * format)930 status_t convertMetaDataToMessage(
931         const sp<MetaData> &meta, sp<AMessage> *format) {
932     return convertMetaDataToMessage(meta.get(), format);
933 }
934 
convertMetaDataToMessage(const MetaDataBase * meta,sp<AMessage> * format)935 status_t convertMetaDataToMessage(
936         const MetaDataBase *meta, sp<AMessage> *format) {
937 
938     format->clear();
939 
940     if (meta == NULL) {
941         ALOGE("convertMetaDataToMessage: NULL input");
942         return BAD_VALUE;
943     }
944 
945     const char *mime;
946     if (!meta->findCString(kKeyMIMEType, &mime)) {
947         return BAD_VALUE;
948     }
949 
950     sp<AMessage> msg = new AMessage;
951     msg->setString("mime", mime);
952 
953     convertMetaDataToMessageFromMappings(meta, msg);
954 
955     uint32_t type;
956     const void *data;
957     size_t size;
958     if (meta->findData(kKeyCASessionID, &type, &data, &size)) {
959         sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
960         if (buffer.get() == NULL || buffer->base() == NULL) {
961             return NO_MEMORY;
962         }
963 
964         msg->setBuffer("ca-session-id", buffer);
965         memcpy(buffer->data(), data, size);
966     }
967 
968     if (meta->findData(kKeyCAPrivateData, &type, &data, &size)) {
969         sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
970         if (buffer.get() == NULL || buffer->base() == NULL) {
971             return NO_MEMORY;
972         }
973 
974         msg->setBuffer("ca-private-data", buffer);
975         memcpy(buffer->data(), data, size);
976     }
977 
978     int32_t systemId;
979     if (meta->findInt32(kKeyCASystemID, &systemId)) {
980         msg->setInt32("ca-system-id", systemId);
981     }
982 
983     if (!strncasecmp("video/scrambled", mime, 15)
984             || !strncasecmp("audio/scrambled", mime, 15)) {
985 
986         *format = msg;
987         return OK;
988     }
989 
990     int64_t durationUs;
991     if (meta->findInt64(kKeyDuration, &durationUs)) {
992         msg->setInt64("durationUs", durationUs);
993     }
994 
995     int32_t avgBitRate = 0;
996     if (meta->findInt32(kKeyBitRate, &avgBitRate) && avgBitRate > 0) {
997         msg->setInt32("bitrate", avgBitRate);
998     }
999 
1000     int32_t maxBitRate;
1001     if (meta->findInt32(kKeyMaxBitRate, &maxBitRate)
1002             && maxBitRate > 0 && maxBitRate >= avgBitRate) {
1003         msg->setInt32("max-bitrate", maxBitRate);
1004     }
1005 
1006     int32_t isSync;
1007     if (meta->findInt32(kKeyIsSyncFrame, &isSync) && isSync != 0) {
1008         msg->setInt32("is-sync-frame", 1);
1009     }
1010 
1011     int32_t dvbComponentTag = 0;
1012     if (meta->findInt32(kKeyDvbComponentTag, &dvbComponentTag)) {
1013         msg->setInt32("dvb-component-tag", dvbComponentTag);
1014     }
1015 
1016     int32_t dvbAudioDescription = 0;
1017     if (meta->findInt32(kKeyDvbAudioDescription, &dvbAudioDescription)) {
1018         msg->setInt32("dvb-audio-description", dvbAudioDescription);
1019     }
1020 
1021     int32_t dvbTeletextMagazineNumber = 0;
1022     if (meta->findInt32(kKeyDvbTeletextMagazineNumber, &dvbTeletextMagazineNumber)) {
1023         msg->setInt32("dvb-teletext-magazine-number", dvbTeletextMagazineNumber);
1024     }
1025 
1026     int32_t dvbTeletextPageNumber = 0;
1027     if (meta->findInt32(kKeyDvbTeletextPageNumber, &dvbTeletextPageNumber)) {
1028         msg->setInt32("dvb-teletext-page-number", dvbTeletextPageNumber);
1029     }
1030 
1031     const char *lang;
1032     if (meta->findCString(kKeyMediaLanguage, &lang)) {
1033         msg->setString("language", lang);
1034     }
1035 
1036     if (!strncasecmp("video/", mime, 6) ||
1037             !strncasecmp("image/", mime, 6)) {
1038         int32_t width, height;
1039         if (!meta->findInt32(kKeyWidth, &width)
1040                 || !meta->findInt32(kKeyHeight, &height)) {
1041             return BAD_VALUE;
1042         }
1043 
1044         msg->setInt32("width", width);
1045         msg->setInt32("height", height);
1046 
1047         int32_t displayWidth, displayHeight;
1048         if (meta->findInt32(kKeyDisplayWidth, &displayWidth)
1049                 && meta->findInt32(kKeyDisplayHeight, &displayHeight)) {
1050             msg->setInt32("display-width", displayWidth);
1051             msg->setInt32("display-height", displayHeight);
1052         }
1053 
1054         int32_t sarWidth, sarHeight;
1055         if (meta->findInt32(kKeySARWidth, &sarWidth)
1056                 && meta->findInt32(kKeySARHeight, &sarHeight)) {
1057             msg->setInt32("sar-width", sarWidth);
1058             msg->setInt32("sar-height", sarHeight);
1059         }
1060 
1061         if (!strncasecmp("image/", mime, 6)) {
1062             int32_t tileWidth, tileHeight, gridRows, gridCols;
1063             if (meta->findInt32(kKeyTileWidth, &tileWidth)
1064                     && meta->findInt32(kKeyTileHeight, &tileHeight)
1065                     && meta->findInt32(kKeyGridRows, &gridRows)
1066                     && meta->findInt32(kKeyGridCols, &gridCols)) {
1067                 msg->setInt32("tile-width", tileWidth);
1068                 msg->setInt32("tile-height", tileHeight);
1069                 msg->setInt32("grid-rows", gridRows);
1070                 msg->setInt32("grid-cols", gridCols);
1071             }
1072             int32_t isPrimary;
1073             if (meta->findInt32(kKeyTrackIsDefault, &isPrimary) && isPrimary) {
1074                 msg->setInt32("is-default", 1);
1075             }
1076         }
1077 
1078         int32_t colorFormat;
1079         if (meta->findInt32(kKeyColorFormat, &colorFormat)) {
1080             msg->setInt32("color-format", colorFormat);
1081         }
1082 
1083         int32_t cropLeft, cropTop, cropRight, cropBottom;
1084         if (meta->findRect(kKeyCropRect,
1085                            &cropLeft,
1086                            &cropTop,
1087                            &cropRight,
1088                            &cropBottom)) {
1089             msg->setRect("crop", cropLeft, cropTop, cropRight, cropBottom);
1090         }
1091 
1092         int32_t rotationDegrees;
1093         if (meta->findInt32(kKeyRotation, &rotationDegrees)) {
1094             msg->setInt32("rotation-degrees", rotationDegrees);
1095         }
1096 
1097         uint32_t type;
1098         const void *data;
1099         size_t size;
1100         if (meta->findData(kKeyHdrStaticInfo, &type, &data, &size)
1101                 && type == 'hdrS' && size == sizeof(HDRStaticInfo)) {
1102             ColorUtils::setHDRStaticInfoIntoFormat(*(HDRStaticInfo*)data, msg);
1103         }
1104 
1105         if (meta->findData(kKeyHdr10PlusInfo, &type, &data, &size)
1106                 && size > 0) {
1107             sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1108             if (buffer.get() == NULL || buffer->base() == NULL) {
1109                 return NO_MEMORY;
1110             }
1111             memcpy(buffer->data(), data, size);
1112             msg->setBuffer("hdr10-plus-info", buffer);
1113         }
1114 
1115         convertMetaDataToMessageColorAspects(meta, msg);
1116     } else if (!strncasecmp("audio/", mime, 6)) {
1117         int32_t numChannels, sampleRate;
1118         if (!meta->findInt32(kKeyChannelCount, &numChannels)
1119                 || !meta->findInt32(kKeySampleRate, &sampleRate)) {
1120             return BAD_VALUE;
1121         }
1122 
1123         msg->setInt32("channel-count", numChannels);
1124         msg->setInt32("sample-rate", sampleRate);
1125 
1126         int32_t bitsPerSample;
1127         if (meta->findInt32(kKeyBitsPerSample, &bitsPerSample)) {
1128             msg->setInt32("bits-per-sample", bitsPerSample);
1129         }
1130 
1131         int32_t channelMask;
1132         if (meta->findInt32(kKeyChannelMask, &channelMask)) {
1133             msg->setInt32("channel-mask", channelMask);
1134         }
1135 
1136         int32_t delay = 0;
1137         if (meta->findInt32(kKeyEncoderDelay, &delay)) {
1138             msg->setInt32("encoder-delay", delay);
1139         }
1140         int32_t padding = 0;
1141         if (meta->findInt32(kKeyEncoderPadding, &padding)) {
1142             msg->setInt32("encoder-padding", padding);
1143         }
1144 
1145         int32_t isADTS;
1146         if (meta->findInt32(kKeyIsADTS, &isADTS)) {
1147             msg->setInt32("is-adts", isADTS);
1148         }
1149 
1150         int32_t mpeghProfileLevelIndication;
1151         if (meta->findInt32(kKeyMpeghProfileLevelIndication, &mpeghProfileLevelIndication)) {
1152             msg->setInt32(AMEDIAFORMAT_KEY_MPEGH_PROFILE_LEVEL_INDICATION,
1153                     mpeghProfileLevelIndication);
1154         }
1155         int32_t mpeghReferenceChannelLayout;
1156         if (meta->findInt32(kKeyMpeghReferenceChannelLayout, &mpeghReferenceChannelLayout)) {
1157             msg->setInt32(AMEDIAFORMAT_KEY_MPEGH_REFERENCE_CHANNEL_LAYOUT,
1158                     mpeghReferenceChannelLayout);
1159         }
1160         if (meta->findData(kKeyMpeghCompatibleSets, &type, &data, &size)) {
1161             sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1162             if (buffer.get() == NULL || buffer->base() == NULL) {
1163                 return NO_MEMORY;
1164             }
1165             msg->setBuffer(AMEDIAFORMAT_KEY_MPEGH_COMPATIBLE_SETS, buffer);
1166             memcpy(buffer->data(), data, size);
1167         }
1168 
1169         int32_t aacProfile = -1;
1170         if (meta->findInt32(kKeyAACAOT, &aacProfile)) {
1171             msg->setInt32("aac-profile", aacProfile);
1172         }
1173 
1174         int32_t pcmEncoding;
1175         if (meta->findInt32(kKeyPcmEncoding, &pcmEncoding)) {
1176             msg->setInt32("pcm-encoding", pcmEncoding);
1177         }
1178 
1179         int32_t hapticChannelCount;
1180         if (meta->findInt32(kKeyHapticChannelCount, &hapticChannelCount)) {
1181             msg->setInt32("haptic-channel-count", hapticChannelCount);
1182         }
1183     }
1184 
1185     int32_t maxInputSize;
1186     if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
1187         msg->setInt32("max-input-size", maxInputSize);
1188     }
1189 
1190     int32_t maxWidth;
1191     if (meta->findInt32(kKeyMaxWidth, &maxWidth)) {
1192         msg->setInt32("max-width", maxWidth);
1193     }
1194 
1195     int32_t maxHeight;
1196     if (meta->findInt32(kKeyMaxHeight, &maxHeight)) {
1197         msg->setInt32("max-height", maxHeight);
1198     }
1199 
1200     int32_t rotationDegrees;
1201     if (meta->findInt32(kKeyRotation, &rotationDegrees)) {
1202         msg->setInt32("rotation-degrees", rotationDegrees);
1203     }
1204 
1205     int32_t fps;
1206     if (meta->findInt32(kKeyFrameRate, &fps) && fps > 0) {
1207         msg->setInt32("frame-rate", fps);
1208     }
1209 
1210     if (meta->findData(kKeyAVCC, &type, &data, &size)) {
1211         // Parse the AVCDecoderConfigurationRecord
1212 
1213         const uint8_t *ptr = (const uint8_t *)data;
1214 
1215         if (size < 7 || ptr[0] != 1) {  // configurationVersion == 1
1216             ALOGE("b/23680780");
1217             return BAD_VALUE;
1218         }
1219 
1220         parseAvcProfileLevelFromAvcc(ptr, size, msg);
1221 
1222         // There is decodable content out there that fails the following
1223         // assertion, let's be lenient for now...
1224         // CHECK((ptr[4] >> 2) == 0x3f);  // reserved
1225 
1226         // we can get lengthSize value from 1 + (ptr[4] & 3)
1227 
1228         // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
1229         // violates it...
1230         // CHECK((ptr[5] >> 5) == 7);  // reserved
1231 
1232         size_t numSeqParameterSets = ptr[5] & 31;
1233 
1234         ptr += 6;
1235         size -= 6;
1236 
1237         sp<ABuffer> buffer = new (std::nothrow) ABuffer(1024);
1238         if (buffer.get() == NULL || buffer->base() == NULL) {
1239             return NO_MEMORY;
1240         }
1241         buffer->setRange(0, 0);
1242 
1243         for (size_t i = 0; i < numSeqParameterSets; ++i) {
1244             if (size < 2) {
1245                 ALOGE("b/23680780");
1246                 return BAD_VALUE;
1247             }
1248             size_t length = U16_AT(ptr);
1249 
1250             ptr += 2;
1251             size -= 2;
1252 
1253             if (size < length) {
1254                 return BAD_VALUE;
1255             }
1256             status_t err = copyNALUToABuffer(&buffer, ptr, length);
1257             if (err != OK) {
1258                 return err;
1259             }
1260 
1261             ptr += length;
1262             size -= length;
1263         }
1264 
1265         buffer->meta()->setInt32("csd", true);
1266         buffer->meta()->setInt64("timeUs", 0);
1267 
1268         msg->setBuffer("csd-0", buffer);
1269 
1270         buffer = new (std::nothrow) ABuffer(1024);
1271         if (buffer.get() == NULL || buffer->base() == NULL) {
1272             return NO_MEMORY;
1273         }
1274         buffer->setRange(0, 0);
1275 
1276         if (size < 1) {
1277             ALOGE("b/23680780");
1278             return BAD_VALUE;
1279         }
1280         size_t numPictureParameterSets = *ptr;
1281         ++ptr;
1282         --size;
1283 
1284         for (size_t i = 0; i < numPictureParameterSets; ++i) {
1285             if (size < 2) {
1286                 ALOGE("b/23680780");
1287                 return BAD_VALUE;
1288             }
1289             size_t length = U16_AT(ptr);
1290 
1291             ptr += 2;
1292             size -= 2;
1293 
1294             if (size < length) {
1295                 return BAD_VALUE;
1296             }
1297             status_t err = copyNALUToABuffer(&buffer, ptr, length);
1298             if (err != OK) {
1299                 return err;
1300             }
1301 
1302             ptr += length;
1303             size -= length;
1304         }
1305 
1306         buffer->meta()->setInt32("csd", true);
1307         buffer->meta()->setInt64("timeUs", 0);
1308         msg->setBuffer("csd-1", buffer);
1309     } else if (meta->findData(kKeyHVCC, &type, &data, &size)) {
1310         const uint8_t *ptr = (const uint8_t *)data;
1311 
1312         if (size < 23 || (ptr[0] != 1 && ptr[0] != 0)) {
1313             // configurationVersion == 1 or 0
1314             // 1 is what the standard dictates, but some old muxers may have used 0.
1315             ALOGE("b/23680780");
1316             return BAD_VALUE;
1317         }
1318 
1319         const size_t dataSize = size; // save for later
1320         ptr += 22;
1321         size -= 22;
1322 
1323         size_t numofArrays = (char)ptr[0];
1324         ptr += 1;
1325         size -= 1;
1326         size_t j = 0, i = 0;
1327 
1328         sp<ABuffer> buffer = new (std::nothrow) ABuffer(1024);
1329         if (buffer.get() == NULL || buffer->base() == NULL) {
1330             return NO_MEMORY;
1331         }
1332         buffer->setRange(0, 0);
1333 
1334         HevcParameterSets hvcc;
1335 
1336         for (i = 0; i < numofArrays; i++) {
1337             if (size < 3) {
1338                 ALOGE("b/23680780");
1339                 return BAD_VALUE;
1340             }
1341             ptr += 1;
1342             size -= 1;
1343 
1344             //Num of nals
1345             size_t numofNals = U16_AT(ptr);
1346 
1347             ptr += 2;
1348             size -= 2;
1349 
1350             for (j = 0; j < numofNals; j++) {
1351                 if (size < 2) {
1352                     ALOGE("b/23680780");
1353                     return BAD_VALUE;
1354                 }
1355                 size_t length = U16_AT(ptr);
1356 
1357                 ptr += 2;
1358                 size -= 2;
1359 
1360                 if (size < length) {
1361                     return BAD_VALUE;
1362                 }
1363                 status_t err = copyNALUToABuffer(&buffer, ptr, length);
1364                 if (err != OK) {
1365                     return err;
1366                 }
1367                 (void)hvcc.addNalUnit(ptr, length);
1368 
1369                 ptr += length;
1370                 size -= length;
1371             }
1372         }
1373         buffer->meta()->setInt32("csd", true);
1374         buffer->meta()->setInt64("timeUs", 0);
1375         msg->setBuffer("csd-0", buffer);
1376 
1377         // if we saw VUI color information we know whether this is HDR because VUI trumps other
1378         // format parameters for HEVC.
1379         HevcParameterSets::Info info = hvcc.getInfo();
1380         if (info & hvcc.kInfoHasColorDescription) {
1381             msg->setInt32("android._is-hdr", (info & hvcc.kInfoIsHdr) != 0);
1382         }
1383 
1384         uint32_t isoPrimaries, isoTransfer, isoMatrix, isoRange;
1385         if (hvcc.findParam32(kColourPrimaries, &isoPrimaries)
1386                 && hvcc.findParam32(kTransferCharacteristics, &isoTransfer)
1387                 && hvcc.findParam32(kMatrixCoeffs, &isoMatrix)
1388                 && hvcc.findParam32(kVideoFullRangeFlag, &isoRange)) {
1389             ALOGV("found iso color aspects : primaris=%d, transfer=%d, matrix=%d, range=%d",
1390                     isoPrimaries, isoTransfer, isoMatrix, isoRange);
1391 
1392             ColorAspects aspects;
1393             ColorUtils::convertIsoColorAspectsToCodecAspects(
1394                     isoPrimaries, isoTransfer, isoMatrix, isoRange, aspects);
1395 
1396             if (aspects.mPrimaries == ColorAspects::PrimariesUnspecified) {
1397                 int32_t primaries;
1398                 if (meta->findInt32(kKeyColorPrimaries, &primaries)) {
1399                     ALOGV("unspecified primaries found, replaced to %d", primaries);
1400                     aspects.mPrimaries = static_cast<ColorAspects::Primaries>(primaries);
1401                 }
1402             }
1403             if (aspects.mTransfer == ColorAspects::TransferUnspecified) {
1404                 int32_t transferFunction;
1405                 if (meta->findInt32(kKeyTransferFunction, &transferFunction)) {
1406                     ALOGV("unspecified transfer found, replaced to %d", transferFunction);
1407                     aspects.mTransfer = static_cast<ColorAspects::Transfer>(transferFunction);
1408                 }
1409             }
1410             if (aspects.mMatrixCoeffs == ColorAspects::MatrixUnspecified) {
1411                 int32_t colorMatrix;
1412                 if (meta->findInt32(kKeyColorMatrix, &colorMatrix)) {
1413                     ALOGV("unspecified matrix found, replaced to %d", colorMatrix);
1414                     aspects.mMatrixCoeffs = static_cast<ColorAspects::MatrixCoeffs>(colorMatrix);
1415                 }
1416             }
1417             if (aspects.mRange == ColorAspects::RangeUnspecified) {
1418                 int32_t range;
1419                 if (meta->findInt32(kKeyColorRange, &range)) {
1420                     ALOGV("unspecified range found, replaced to %d", range);
1421                     aspects.mRange = static_cast<ColorAspects::Range>(range);
1422                 }
1423             }
1424 
1425             int32_t standard, transfer, range;
1426             if (ColorUtils::convertCodecColorAspectsToPlatformAspects(
1427                     aspects, &range, &standard, &transfer) == OK) {
1428                 msg->setInt32("color-standard", standard);
1429                 msg->setInt32("color-transfer", transfer);
1430                 msg->setInt32("color-range", range);
1431             }
1432         }
1433 
1434         parseHevcProfileLevelFromHvcc((const uint8_t *)data, dataSize, msg);
1435     } else if (meta->findData(kKeyAV1C, &type, &data, &size)) {
1436         sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1437         if (buffer.get() == NULL || buffer->base() == NULL) {
1438             return NO_MEMORY;
1439         }
1440         memcpy(buffer->data(), data, size);
1441 
1442         buffer->meta()->setInt32("csd", true);
1443         buffer->meta()->setInt64("timeUs", 0);
1444         msg->setBuffer("csd-0", buffer);
1445         parseAV1ProfileLevelFromCsd(buffer, msg);
1446     } else if (meta->findData(kKeyESDS, &type, &data, &size)) {
1447         ESDS esds((const char *)data, size);
1448         if (esds.InitCheck() != (status_t)OK) {
1449             return BAD_VALUE;
1450         }
1451 
1452         const void *codec_specific_data;
1453         size_t codec_specific_data_size;
1454         esds.getCodecSpecificInfo(
1455                 &codec_specific_data, &codec_specific_data_size);
1456 
1457         sp<ABuffer> buffer = new (std::nothrow) ABuffer(codec_specific_data_size);
1458         if (buffer.get() == NULL || buffer->base() == NULL) {
1459             return NO_MEMORY;
1460         }
1461 
1462         memcpy(buffer->data(), codec_specific_data,
1463                codec_specific_data_size);
1464 
1465         buffer->meta()->setInt32("csd", true);
1466         buffer->meta()->setInt64("timeUs", 0);
1467         msg->setBuffer("csd-0", buffer);
1468 
1469         if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4)) {
1470             parseMpeg4ProfileLevelFromCsd(buffer, msg);
1471         } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG2)) {
1472             parseMpeg2ProfileLevelFromEsds(esds, msg);
1473             if (meta->findData(kKeyStreamHeader, &type, &data, &size)) {
1474                 parseMpeg2ProfileLevelFromHeader((uint8_t*)data, size, msg);
1475             }
1476         } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) {
1477             parseAacProfileFromCsd(buffer, msg);
1478         }
1479 
1480         uint32_t maxBitrate, avgBitrate;
1481         if (esds.getBitRate(&maxBitrate, &avgBitrate) == OK) {
1482             if (!meta->hasData(kKeyBitRate)
1483                     && avgBitrate > 0 && avgBitrate <= INT32_MAX) {
1484                 msg->setInt32("bitrate", (int32_t)avgBitrate);
1485             } else {
1486                 (void)msg->findInt32("bitrate", (int32_t*)&avgBitrate);
1487             }
1488             if (!meta->hasData(kKeyMaxBitRate)
1489                     && maxBitrate > 0 && maxBitrate <= INT32_MAX && maxBitrate >= avgBitrate) {
1490                 msg->setInt32("max-bitrate", (int32_t)maxBitrate);
1491             }
1492         }
1493     } else if (meta->findData(kKeyD263, &type, &data, &size)) {
1494         const uint8_t *ptr = (const uint8_t *)data;
1495         parseH263ProfileLevelFromD263(ptr, size, msg);
1496     } else if (meta->findData(kKeyOpusHeader, &type, &data, &size)) {
1497         sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1498         if (buffer.get() == NULL || buffer->base() == NULL) {
1499             return NO_MEMORY;
1500         }
1501         memcpy(buffer->data(), data, size);
1502 
1503         buffer->meta()->setInt32("csd", true);
1504         buffer->meta()->setInt64("timeUs", 0);
1505         msg->setBuffer("csd-0", buffer);
1506 
1507         if (!meta->findData(kKeyOpusCodecDelay, &type, &data, &size)) {
1508             return -EINVAL;
1509         }
1510 
1511         buffer = new (std::nothrow) ABuffer(size);
1512         if (buffer.get() == NULL || buffer->base() == NULL) {
1513             return NO_MEMORY;
1514         }
1515         memcpy(buffer->data(), data, size);
1516 
1517         buffer->meta()->setInt32("csd", true);
1518         buffer->meta()->setInt64("timeUs", 0);
1519         msg->setBuffer("csd-1", buffer);
1520 
1521         if (!meta->findData(kKeyOpusSeekPreRoll, &type, &data, &size)) {
1522             return -EINVAL;
1523         }
1524 
1525         buffer = new (std::nothrow) ABuffer(size);
1526         if (buffer.get() == NULL || buffer->base() == NULL) {
1527             return NO_MEMORY;
1528         }
1529         memcpy(buffer->data(), data, size);
1530 
1531         buffer->meta()->setInt32("csd", true);
1532         buffer->meta()->setInt64("timeUs", 0);
1533         msg->setBuffer("csd-2", buffer);
1534     } else if (meta->findData(kKeyVp9CodecPrivate, &type, &data, &size)) {
1535         sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1536         if (buffer.get() == NULL || buffer->base() == NULL) {
1537             return NO_MEMORY;
1538         }
1539         memcpy(buffer->data(), data, size);
1540 
1541         buffer->meta()->setInt32("csd", true);
1542         buffer->meta()->setInt64("timeUs", 0);
1543         msg->setBuffer("csd-0", buffer);
1544 
1545         parseVp9ProfileLevelFromCsd(buffer, msg);
1546     } else if (meta->findData(kKeyAlacMagicCookie, &type, &data, &size)) {
1547         ALOGV("convertMetaDataToMessage found kKeyAlacMagicCookie of size %zu\n", size);
1548         sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1549         if (buffer.get() == NULL || buffer->base() == NULL) {
1550             return NO_MEMORY;
1551         }
1552         memcpy(buffer->data(), data, size);
1553 
1554         buffer->meta()->setInt32("csd", true);
1555         buffer->meta()->setInt64("timeUs", 0);
1556         msg->setBuffer("csd-0", buffer);
1557     }
1558 
1559     if (meta->findData(kKeyDVCC, &type, &data, &size)
1560             || meta->findData(kKeyDVVC, &type, &data, &size)
1561             || meta->findData(kKeyDVWC, &type, &data, &size)) {
1562         const uint8_t *ptr = (const uint8_t *)data;
1563         ALOGV("DV: calling parseDolbyVisionProfileLevelFromDvcc with data size %zu", size);
1564         parseDolbyVisionProfileLevelFromDvcc(ptr, size, msg);
1565         sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1566         if (buffer.get() == nullptr || buffer->base() == nullptr) {
1567             return NO_MEMORY;
1568         }
1569         memcpy(buffer->data(), data, size);
1570 
1571         buffer->meta()->setInt32("csd", true);
1572         buffer->meta()->setInt64("timeUs", 0);
1573         msg->setBuffer("csd-2", buffer);
1574     }
1575 
1576     *format = msg;
1577 
1578     return OK;
1579 }
1580 
findNextNalStartCode(const uint8_t * data,size_t length)1581 const uint8_t *findNextNalStartCode(const uint8_t *data, size_t length) {
1582     uint8_t *res = NULL;
1583     if (length > 4) {
1584         // minus 1 as to not match NAL start code at end
1585         res = (uint8_t *)memmem(data, length - 1, "\x00\x00\x00\x01", 4);
1586     }
1587     return res != NULL && res < data + length - 4 ? res : &data[length];
1588 }
1589 
reassembleAVCC(const sp<ABuffer> & csd0,const sp<ABuffer> & csd1,char * avcc)1590 static size_t reassembleAVCC(const sp<ABuffer> &csd0, const sp<ABuffer> &csd1, char *avcc) {
1591     avcc[0] = 1;        // version
1592     avcc[1] = 0x64;     // profile (default to high)
1593     avcc[2] = 0;        // constraints (default to none)
1594     avcc[3] = 0xd;      // level (default to 1.3)
1595     avcc[4] = 0xff;     // reserved+size
1596 
1597     size_t i = 0;
1598     int numparams = 0;
1599     int lastparamoffset = 0;
1600     int avccidx = 6;
1601     do {
1602         i = findNextNalStartCode(csd0->data() + i, csd0->size() - i) - csd0->data();
1603         ALOGV("block at %zu, last was %d", i, lastparamoffset);
1604         if (lastparamoffset > 0) {
1605             const uint8_t *lastparam = csd0->data() + lastparamoffset;
1606             int size = i - lastparamoffset;
1607             if (size > 3) {
1608                 if (numparams && memcmp(avcc + 1, lastparam + 1, 3)) {
1609                     ALOGW("Inconsisted profile/level found in SPS: %x,%x,%x vs %x,%x,%x",
1610                             avcc[1], avcc[2], avcc[3], lastparam[1], lastparam[2], lastparam[3]);
1611                 } else if (!numparams) {
1612                     // fill in profile, constraints and level
1613                     memcpy(avcc + 1, lastparam + 1, 3);
1614                 }
1615             }
1616             avcc[avccidx++] = size >> 8;
1617             avcc[avccidx++] = size & 0xff;
1618             memcpy(avcc+avccidx, lastparam, size);
1619             avccidx += size;
1620             numparams++;
1621         }
1622         i += 4;
1623         lastparamoffset = i;
1624     } while(i < csd0->size());
1625     ALOGV("csd0 contains %d params", numparams);
1626 
1627     avcc[5] = 0xe0 | numparams;
1628     //and now csd-1
1629     i = 0;
1630     numparams = 0;
1631     lastparamoffset = 0;
1632     int numpicparamsoffset = avccidx;
1633     avccidx++;
1634     do {
1635         i = findNextNalStartCode(csd1->data() + i, csd1->size() - i) - csd1->data();
1636         ALOGV("block at %zu, last was %d", i, lastparamoffset);
1637         if (lastparamoffset > 0) {
1638             int size = i - lastparamoffset;
1639             avcc[avccidx++] = size >> 8;
1640             avcc[avccidx++] = size & 0xff;
1641             memcpy(avcc+avccidx, csd1->data() + lastparamoffset, size);
1642             avccidx += size;
1643             numparams++;
1644         }
1645         i += 4;
1646         lastparamoffset = i;
1647     } while(i < csd1->size());
1648     avcc[numpicparamsoffset] = numparams;
1649     return avccidx;
1650 }
1651 
reassembleESDS(const sp<ABuffer> & csd0,char * esds)1652 static void reassembleESDS(const sp<ABuffer> &csd0, char *esds) {
1653     int csd0size = csd0->size();
1654     esds[0] = 3; // kTag_ESDescriptor;
1655     int esdescriptorsize = 26 + csd0size;
1656     CHECK(esdescriptorsize < 268435456); // 7 bits per byte, so max is 2^28-1
1657     esds[1] = 0x80 | (esdescriptorsize >> 21);
1658     esds[2] = 0x80 | ((esdescriptorsize >> 14) & 0x7f);
1659     esds[3] = 0x80 | ((esdescriptorsize >> 7) & 0x7f);
1660     esds[4] = (esdescriptorsize & 0x7f);
1661     esds[5] = esds[6] = 0; // es id
1662     esds[7] = 0; // flags
1663     esds[8] = 4; // kTag_DecoderConfigDescriptor
1664     int configdescriptorsize = 18 + csd0size;
1665     esds[9] = 0x80 | (configdescriptorsize >> 21);
1666     esds[10] = 0x80 | ((configdescriptorsize >> 14) & 0x7f);
1667     esds[11] = 0x80 | ((configdescriptorsize >> 7) & 0x7f);
1668     esds[12] = (configdescriptorsize & 0x7f);
1669     esds[13] = 0x40; // objectTypeIndication
1670     // bytes 14-25 are examples from a real file. they are unused/overwritten by muxers.
1671     esds[14] = 0x15; // streamType(5), upStream(0),
1672     esds[15] = 0x00; // 15-17: bufferSizeDB (6KB)
1673     esds[16] = 0x18;
1674     esds[17] = 0x00;
1675     esds[18] = 0x00; // 18-21: maxBitrate (64kbps)
1676     esds[19] = 0x00;
1677     esds[20] = 0xfa;
1678     esds[21] = 0x00;
1679     esds[22] = 0x00; // 22-25: avgBitrate (64kbps)
1680     esds[23] = 0x00;
1681     esds[24] = 0xfa;
1682     esds[25] = 0x00;
1683     esds[26] = 5; // kTag_DecoderSpecificInfo;
1684     esds[27] = 0x80 | (csd0size >> 21);
1685     esds[28] = 0x80 | ((csd0size >> 14) & 0x7f);
1686     esds[29] = 0x80 | ((csd0size >> 7) & 0x7f);
1687     esds[30] = (csd0size & 0x7f);
1688     memcpy((void*)&esds[31], csd0->data(), csd0size);
1689     // data following this is ignored, so don't bother appending it
1690 }
1691 
reassembleHVCC(const sp<ABuffer> & csd0,uint8_t * hvcc,size_t hvccSize,size_t nalSizeLength)1692 static size_t reassembleHVCC(const sp<ABuffer> &csd0, uint8_t *hvcc, size_t hvccSize, size_t nalSizeLength) {
1693     HevcParameterSets paramSets;
1694     uint8_t* data = csd0->data();
1695     if (csd0->size() < 4) {
1696         ALOGE("csd0 too small");
1697         return 0;
1698     }
1699     if (memcmp(data, "\x00\x00\x00\x01", 4) != 0) {
1700         ALOGE("csd0 doesn't start with a start code");
1701         return 0;
1702     }
1703     size_t prevNalOffset = 4;
1704     status_t err = OK;
1705     for (size_t i = 1; i < csd0->size() - 4; ++i) {
1706         if (memcmp(&data[i], "\x00\x00\x00\x01", 4) != 0) {
1707             continue;
1708         }
1709         err = paramSets.addNalUnit(&data[prevNalOffset], i - prevNalOffset);
1710         if (err != OK) {
1711             return 0;
1712         }
1713         prevNalOffset = i + 4;
1714     }
1715     err = paramSets.addNalUnit(&data[prevNalOffset], csd0->size() - prevNalOffset);
1716     if (err != OK) {
1717         return 0;
1718     }
1719     size_t size = hvccSize;
1720     err = paramSets.makeHvcc(hvcc, &size, nalSizeLength);
1721     if (err != OK) {
1722         return 0;
1723     }
1724     return size;
1725 }
1726 
1727 #if 0
1728 static void convertMessageToMetaDataInt32(
1729         const sp<AMessage> &msg, sp<MetaData> &meta, uint32_t key, const char *name) {
1730     int32_t value;
1731     if (msg->findInt32(name, &value)) {
1732         meta->setInt32(key, value);
1733     }
1734 }
1735 #endif
1736 
convertMessageToMetaDataColorAspects(const sp<AMessage> & msg,sp<MetaData> & meta)1737 static void convertMessageToMetaDataColorAspects(const sp<AMessage> &msg, sp<MetaData> &meta) {
1738     // 0 values are unspecified
1739     int32_t range = 0, standard = 0, transfer = 0;
1740     (void)msg->findInt32("color-range", &range);
1741     (void)msg->findInt32("color-standard", &standard);
1742     (void)msg->findInt32("color-transfer", &transfer);
1743 
1744     ColorAspects colorAspects;
1745     memset(&colorAspects, 0, sizeof(colorAspects));
1746     if (CodecBase::convertPlatformColorAspectsToCodecAspects(
1747             range, standard, transfer, colorAspects) != OK) {
1748         return;
1749     }
1750 
1751     // save specified values to meta
1752     if (colorAspects.mRange != 0) {
1753         meta->setInt32(kKeyColorRange, colorAspects.mRange);
1754     }
1755     if (colorAspects.mPrimaries != 0) {
1756         meta->setInt32(kKeyColorPrimaries, colorAspects.mPrimaries);
1757     }
1758     if (colorAspects.mTransfer != 0) {
1759         meta->setInt32(kKeyTransferFunction, colorAspects.mTransfer);
1760     }
1761     if (colorAspects.mMatrixCoeffs != 0) {
1762         meta->setInt32(kKeyColorMatrix, colorAspects.mMatrixCoeffs);
1763     }
1764 }
1765 /* Converts key and value pairs in AMessage format to MetaData format.
1766  * Also checks for the presence of required keys.
1767  */
convertMessageToMetaData(const sp<AMessage> & msg,sp<MetaData> & meta)1768 status_t convertMessageToMetaData(const sp<AMessage> &msg, sp<MetaData> &meta) {
1769     AString mime;
1770     if (msg->findString("mime", &mime)) {
1771         meta->setCString(kKeyMIMEType, mime.c_str());
1772     } else {
1773         ALOGV("did not find mime type");
1774         return BAD_VALUE;
1775     }
1776 
1777     convertMessageToMetaDataFromMappings(msg, meta);
1778 
1779     int32_t systemId;
1780     if (msg->findInt32("ca-system-id", &systemId)) {
1781         meta->setInt32(kKeyCASystemID, systemId);
1782 
1783         sp<ABuffer> caSessionId, caPvtData;
1784         if (msg->findBuffer("ca-session-id", &caSessionId)) {
1785             meta->setData(kKeyCASessionID, 0, caSessionId->data(), caSessionId->size());
1786         }
1787         if (msg->findBuffer("ca-private-data", &caPvtData)) {
1788             meta->setData(kKeyCAPrivateData, 0, caPvtData->data(), caPvtData->size());
1789         }
1790     }
1791 
1792     int64_t durationUs;
1793     if (msg->findInt64("durationUs", &durationUs)) {
1794         meta->setInt64(kKeyDuration, durationUs);
1795     }
1796 
1797     int32_t isSync;
1798     if (msg->findInt32("is-sync-frame", &isSync) && isSync != 0) {
1799         meta->setInt32(kKeyIsSyncFrame, 1);
1800     }
1801 
1802     // Mode for media transcoding.
1803     int32_t isBackgroundMode;
1804     if (msg->findInt32("android._background-mode", &isBackgroundMode) && isBackgroundMode != 0) {
1805         meta->setInt32(isBackgroundMode, 1);
1806     }
1807 
1808     int32_t avgBitrate = 0;
1809     int32_t maxBitrate;
1810     if (msg->findInt32("bitrate", &avgBitrate) && avgBitrate > 0) {
1811         meta->setInt32(kKeyBitRate, avgBitrate);
1812     }
1813     if (msg->findInt32("max-bitrate", &maxBitrate) && maxBitrate > 0 && maxBitrate >= avgBitrate) {
1814         meta->setInt32(kKeyMaxBitRate, maxBitrate);
1815     }
1816 
1817     int32_t dvbComponentTag = 0;
1818     if (msg->findInt32("dvb-component-tag", &dvbComponentTag) && dvbComponentTag > 0) {
1819         meta->setInt32(kKeyDvbComponentTag, dvbComponentTag);
1820     }
1821 
1822     int32_t dvbAudioDescription = 0;
1823     if (msg->findInt32("dvb-audio-description", &dvbAudioDescription)) {
1824         meta->setInt32(kKeyDvbAudioDescription, dvbAudioDescription);
1825     }
1826 
1827     int32_t dvbTeletextMagazineNumber = 0;
1828     if (msg->findInt32("dvb-teletext-magazine-number", &dvbTeletextMagazineNumber)) {
1829         meta->setInt32(kKeyDvbTeletextMagazineNumber, dvbTeletextMagazineNumber);
1830     }
1831 
1832     int32_t dvbTeletextPageNumber = 0;
1833     if (msg->findInt32("dvb-teletext-page-number", &dvbTeletextPageNumber)) {
1834         meta->setInt32(kKeyDvbTeletextPageNumber, dvbTeletextPageNumber);
1835     }
1836 
1837     AString lang;
1838     if (msg->findString("language", &lang)) {
1839         meta->setCString(kKeyMediaLanguage, lang.c_str());
1840     }
1841 
1842     if (mime.startsWith("video/") || mime.startsWith("image/")) {
1843         int32_t width;
1844         int32_t height;
1845         if (!msg->findInt32("width", &width) || !msg->findInt32("height", &height)) {
1846             ALOGV("did not find width and/or height");
1847             return BAD_VALUE;
1848         }
1849         if (width <= 0 || height <= 0) {
1850             ALOGE("Invalid value of width: %d and/or height: %d", width, height);
1851             return BAD_VALUE;
1852         }
1853         meta->setInt32(kKeyWidth, width);
1854         meta->setInt32(kKeyHeight, height);
1855 
1856         int32_t sarWidth = -1, sarHeight = -1;
1857         bool foundWidth, foundHeight;
1858         foundWidth = msg->findInt32("sar-width", &sarWidth);
1859         foundHeight = msg->findInt32("sar-height", &sarHeight);
1860         if (foundWidth || foundHeight) {
1861             if (sarWidth <= 0 || sarHeight <= 0) {
1862                 ALOGE("Invalid value of sarWidth: %d and/or sarHeight: %d", sarWidth, sarHeight);
1863                 return BAD_VALUE;
1864             }
1865             meta->setInt32(kKeySARWidth, sarWidth);
1866             meta->setInt32(kKeySARHeight, sarHeight);
1867         }
1868 
1869         int32_t displayWidth = -1, displayHeight = -1;
1870         foundWidth = msg->findInt32("display-width", &displayWidth);
1871         foundHeight = msg->findInt32("display-height", &displayHeight);
1872         if (foundWidth || foundHeight) {
1873             if (displayWidth <= 0 || displayHeight <= 0) {
1874                 ALOGE("Invalid value of displayWidth: %d and/or displayHeight: %d",
1875                         displayWidth, displayHeight);
1876                 return BAD_VALUE;
1877             }
1878             meta->setInt32(kKeyDisplayWidth, displayWidth);
1879             meta->setInt32(kKeyDisplayHeight, displayHeight);
1880         }
1881 
1882         if (mime.startsWith("image/")){
1883             int32_t isPrimary;
1884             if (msg->findInt32("is-default", &isPrimary) && isPrimary) {
1885                 meta->setInt32(kKeyTrackIsDefault, 1);
1886             }
1887             int32_t tileWidth = -1, tileHeight = -1;
1888             foundWidth = msg->findInt32("tile-width", &tileWidth);
1889             foundHeight = msg->findInt32("tile-height", &tileHeight);
1890             if (foundWidth || foundHeight) {
1891                 if (tileWidth <= 0 || tileHeight <= 0) {
1892                     ALOGE("Invalid value of tileWidth: %d and/or tileHeight: %d",
1893                             tileWidth, tileHeight);
1894                     return BAD_VALUE;
1895                 }
1896                 meta->setInt32(kKeyTileWidth, tileWidth);
1897                 meta->setInt32(kKeyTileHeight, tileHeight);
1898             }
1899             int32_t gridRows = -1, gridCols = -1;
1900             bool foundRows, foundCols;
1901             foundRows = msg->findInt32("grid-rows", &gridRows);
1902             foundCols = msg->findInt32("grid-cols", &gridCols);
1903             if (foundRows || foundCols) {
1904                 if (gridRows <= 0 || gridCols <= 0) {
1905                     ALOGE("Invalid value of gridRows: %d and/or gridCols: %d",
1906                             gridRows, gridCols);
1907                     return BAD_VALUE;
1908                 }
1909                 meta->setInt32(kKeyGridRows, gridRows);
1910                 meta->setInt32(kKeyGridCols, gridCols);
1911             }
1912         }
1913 
1914         int32_t colorFormat;
1915         if (msg->findInt32("color-format", &colorFormat)) {
1916             meta->setInt32(kKeyColorFormat, colorFormat);
1917         }
1918 
1919         int32_t cropLeft, cropTop, cropRight, cropBottom;
1920         if (msg->findRect("crop",
1921                           &cropLeft,
1922                           &cropTop,
1923                           &cropRight,
1924                           &cropBottom)) {
1925             if (cropLeft < 0 || cropLeft > cropRight || cropRight >= width) {
1926                 ALOGE("Invalid value of cropLeft: %d and/or cropRight: %d", cropLeft, cropRight);
1927                 return BAD_VALUE;
1928             }
1929             if (cropTop < 0 || cropTop > cropBottom || cropBottom >= height) {
1930                 ALOGE("Invalid value of cropTop: %d and/or cropBottom: %d", cropTop, cropBottom);
1931                 return BAD_VALUE;
1932             }
1933             meta->setRect(kKeyCropRect, cropLeft, cropTop, cropRight, cropBottom);
1934         }
1935 
1936         int32_t rotationDegrees;
1937         if (msg->findInt32("rotation-degrees", &rotationDegrees)) {
1938             meta->setInt32(kKeyRotation, rotationDegrees);
1939         }
1940 
1941         if (msg->contains("hdr-static-info")) {
1942             HDRStaticInfo info;
1943             if (ColorUtils::getHDRStaticInfoFromFormat(msg, &info)) {
1944                 meta->setData(kKeyHdrStaticInfo, 'hdrS', &info, sizeof(info));
1945             }
1946         }
1947 
1948         sp<ABuffer> hdr10PlusInfo;
1949         if (msg->findBuffer("hdr10-plus-info", &hdr10PlusInfo)) {
1950             meta->setData(kKeyHdr10PlusInfo, 0,
1951                     hdr10PlusInfo->data(), hdr10PlusInfo->size());
1952         }
1953 
1954         convertMessageToMetaDataColorAspects(msg, meta);
1955 
1956         AString tsSchema;
1957         if (msg->findString("ts-schema", &tsSchema)) {
1958             unsigned int numLayers = 0;
1959             unsigned int numBLayers = 0;
1960             char placeholder;
1961             int tags = sscanf(tsSchema.c_str(), "android.generic.%u%c%u%c",
1962                     &numLayers, &placeholder, &numBLayers, &placeholder);
1963             if ((tags == 1 || (tags == 3 && placeholder == '+'))
1964                     && numLayers > 0 && numLayers < UINT32_MAX - numBLayers
1965                     && numLayers + numBLayers <= INT32_MAX) {
1966                 meta->setInt32(kKeyTemporalLayerCount, numLayers + numBLayers);
1967             }
1968         }
1969     } else if (mime.startsWith("audio/")) {
1970         int32_t numChannels, sampleRate;
1971         if (!msg->findInt32("channel-count", &numChannels) ||
1972                 !msg->findInt32("sample-rate", &sampleRate)) {
1973             ALOGV("did not find channel-count and/or sample-rate");
1974             return BAD_VALUE;
1975         }
1976         // channel count can be zero in some cases like mpeg h
1977         if (sampleRate <= 0 || numChannels < 0) {
1978             ALOGE("Invalid value of channel-count: %d and/or sample-rate: %d",
1979                    numChannels, sampleRate);
1980             return BAD_VALUE;
1981         }
1982         meta->setInt32(kKeyChannelCount, numChannels);
1983         meta->setInt32(kKeySampleRate, sampleRate);
1984         int32_t bitsPerSample;
1985         // TODO:(b/204430952) add appropriate bound check for bitsPerSample
1986         if (msg->findInt32("bits-per-sample", &bitsPerSample)) {
1987             meta->setInt32(kKeyBitsPerSample, bitsPerSample);
1988         }
1989         int32_t channelMask;
1990         if (msg->findInt32("channel-mask", &channelMask)) {
1991             meta->setInt32(kKeyChannelMask, channelMask);
1992         }
1993         int32_t delay = 0;
1994         if (msg->findInt32("encoder-delay", &delay)) {
1995             meta->setInt32(kKeyEncoderDelay, delay);
1996         }
1997         int32_t padding = 0;
1998         if (msg->findInt32("encoder-padding", &padding)) {
1999             meta->setInt32(kKeyEncoderPadding, padding);
2000         }
2001 
2002         int32_t isADTS;
2003         if (msg->findInt32("is-adts", &isADTS)) {
2004             meta->setInt32(kKeyIsADTS, isADTS);
2005         }
2006 
2007         int32_t mpeghProfileLevelIndication = -1;
2008         if (msg->findInt32(AMEDIAFORMAT_KEY_MPEGH_PROFILE_LEVEL_INDICATION,
2009                 &mpeghProfileLevelIndication)) {
2010             meta->setInt32(kKeyMpeghProfileLevelIndication, mpeghProfileLevelIndication);
2011         }
2012         int32_t mpeghReferenceChannelLayout = -1;
2013         if (msg->findInt32(AMEDIAFORMAT_KEY_MPEGH_REFERENCE_CHANNEL_LAYOUT,
2014                 &mpeghReferenceChannelLayout)) {
2015             meta->setInt32(kKeyMpeghReferenceChannelLayout, mpeghReferenceChannelLayout);
2016         }
2017         sp<ABuffer> mpeghCompatibleSets;
2018         if (msg->findBuffer(AMEDIAFORMAT_KEY_MPEGH_COMPATIBLE_SETS,
2019                 &mpeghCompatibleSets)) {
2020             meta->setData(kKeyMpeghCompatibleSets, kTypeHCOS,
2021                     mpeghCompatibleSets->data(), mpeghCompatibleSets->size());
2022         }
2023 
2024         int32_t aacProfile = -1;
2025         if (msg->findInt32("aac-profile", &aacProfile)) {
2026             meta->setInt32(kKeyAACAOT, aacProfile);
2027         }
2028 
2029         int32_t pcmEncoding;
2030         if (msg->findInt32("pcm-encoding", &pcmEncoding)) {
2031             meta->setInt32(kKeyPcmEncoding, pcmEncoding);
2032         }
2033 
2034         int32_t hapticChannelCount;
2035         if (msg->findInt32("haptic-channel-count", &hapticChannelCount)) {
2036             meta->setInt32(kKeyHapticChannelCount, hapticChannelCount);
2037         }
2038     }
2039 
2040     int32_t maxInputSize;
2041     if (msg->findInt32("max-input-size", &maxInputSize)) {
2042         meta->setInt32(kKeyMaxInputSize, maxInputSize);
2043     }
2044 
2045     int32_t maxWidth;
2046     if (msg->findInt32("max-width", &maxWidth)) {
2047         meta->setInt32(kKeyMaxWidth, maxWidth);
2048     }
2049 
2050     int32_t maxHeight;
2051     if (msg->findInt32("max-height", &maxHeight)) {
2052         meta->setInt32(kKeyMaxHeight, maxHeight);
2053     }
2054 
2055     int32_t fps;
2056     float fpsFloat;
2057     if (msg->findInt32("frame-rate", &fps) && fps > 0) {
2058         meta->setInt32(kKeyFrameRate, fps);
2059     } else if (msg->findFloat("frame-rate", &fpsFloat)
2060             && fpsFloat >= 1 && fpsFloat <= (float)INT32_MAX) {
2061         // truncate values to distinguish between e.g. 24 vs 23.976 fps
2062         meta->setInt32(kKeyFrameRate, (int32_t)fpsFloat);
2063     }
2064 
2065     // reassemble the csd data into its original form
2066     sp<ABuffer> csd0, csd1, csd2;
2067     if (msg->findBuffer("csd-0", &csd0)) {
2068         int csd0size = csd0->size();
2069         if (mime == MEDIA_MIMETYPE_VIDEO_AVC) {
2070             sp<ABuffer> csd1;
2071             if (msg->findBuffer("csd-1", &csd1)) {
2072                 std::vector<char> avcc(csd0size + csd1->size() + 1024);
2073                 size_t outsize = reassembleAVCC(csd0, csd1, avcc.data());
2074                 meta->setData(kKeyAVCC, kTypeAVCC, avcc.data(), outsize);
2075             }
2076         } else if (mime == MEDIA_MIMETYPE_AUDIO_AAC ||
2077                 mime == MEDIA_MIMETYPE_VIDEO_MPEG4 ||
2078                 mime == MEDIA_MIMETYPE_AUDIO_WMA ||
2079                 mime == MEDIA_MIMETYPE_AUDIO_MS_ADPCM ||
2080                 mime == MEDIA_MIMETYPE_AUDIO_DVI_IMA_ADPCM) {
2081             std::vector<char> esds(csd0size + 31);
2082             // The written ESDS is actually for an audio stream, but it's enough
2083             // for transporting the CSD to muxers.
2084             reassembleESDS(csd0, esds.data());
2085             meta->setData(kKeyESDS, kTypeESDS, esds.data(), esds.size());
2086         } else if (mime == MEDIA_MIMETYPE_VIDEO_HEVC ||
2087                    mime == MEDIA_MIMETYPE_IMAGE_ANDROID_HEIC) {
2088             std::vector<uint8_t> hvcc(csd0size + 1024);
2089             size_t outsize = reassembleHVCC(csd0, hvcc.data(), hvcc.size(), 4);
2090             meta->setData(kKeyHVCC, kTypeHVCC, hvcc.data(), outsize);
2091         } else if (mime == MEDIA_MIMETYPE_VIDEO_AV1 ||
2092                    mime == MEDIA_MIMETYPE_IMAGE_AVIF) {
2093             meta->setData(kKeyAV1C, 0, csd0->data(), csd0->size());
2094         } else if (mime == MEDIA_MIMETYPE_VIDEO_DOLBY_VISION) {
2095             int32_t profile = -1;
2096             uint8_t blCompatibilityId = -1;
2097             int32_t level = 0;
2098             uint8_t profileVal = -1;
2099             uint8_t profileVal1 = -1;
2100             uint8_t profileVal2 = -1;
2101             constexpr size_t dvccSize = 24;
2102 
2103             const ALookup<uint8_t, int32_t> &profiles =
2104                 getDolbyVisionProfileTable();
2105             const ALookup<uint8_t, int32_t> &levels =
2106                 getDolbyVisionLevelsTable();
2107 
2108             if (!msg->findBuffer("csd-2", &csd2)) {
2109                 // MP4 extractors are expected to generate csd buffer
2110                 // some encoders might not be generating it, in which
2111                 // case we populate the track metadata dv (cc|vc|wc)
2112                 // from the 'profile' and 'level' info.
2113                 // This is done according to Dolby Vision ISOBMFF spec
2114 
2115                 if (!msg->findInt32("profile", &profile)) {
2116                     ALOGE("Dolby Vision profile not found");
2117                     return BAD_VALUE;
2118                 }
2119                 msg->findInt32("level", &level);
2120 
2121                 if (profile == DolbyVisionProfileDvheSt) {
2122                     if (!profiles.rlookup(DolbyVisionProfileDvheSt, &profileVal)) { // dvhe.08
2123                         ALOGE("Dolby Vision profile lookup error");
2124                         return BAD_VALUE;
2125                     }
2126                     blCompatibilityId = 4;
2127                 } else if (profile == DolbyVisionProfileDvavSe) {
2128                     if (!profiles.rlookup(DolbyVisionProfileDvavSe, &profileVal)) { // dvav.09
2129                         ALOGE("Dolby Vision profile lookup error");
2130                         return BAD_VALUE;
2131                     }
2132                     blCompatibilityId = 2;
2133                 } else {
2134                     ALOGE("Dolby Vision profile look up error");
2135                     return BAD_VALUE;
2136                 }
2137 
2138                 profile = (int32_t) profileVal;
2139 
2140                 uint8_t level_val = 0;
2141                 if (!levels.map(level, &level_val)) {
2142                     ALOGE("Dolby Vision level lookup error");
2143                     return BAD_VALUE;
2144                 }
2145 
2146                 std::vector<uint8_t> dvcc(dvccSize);
2147 
2148                 dvcc[0] = 1; // major version
2149                 dvcc[1] = 0; // minor version
2150                 dvcc[2] = (uint8_t)((profile & 0x7f) << 1); // dolby vision profile
2151                 dvcc[2] = (uint8_t)((dvcc[2] | (uint8_t)((level_val >> 5) & 0x1)) & 0xff);
2152                 dvcc[3] = (uint8_t)((level_val & 0x1f) << 3); // dolby vision level
2153                 dvcc[3] = (uint8_t)(dvcc[3] | (1 << 2)); // rpu_present_flag
2154                 dvcc[3] = (uint8_t)(dvcc[3] | (1)); // bl_present_flag
2155                 dvcc[4] = (uint8_t)(blCompatibilityId << 4); // bl_compatibility id
2156 
2157                 profiles.rlookup(DolbyVisionProfileDvav110, &profileVal);
2158                 profiles.rlookup(DolbyVisionProfileDvheDtb, &profileVal1);
2159                 if (profile > (int32_t) profileVal) {
2160                     meta->setData(kKeyDVWC, kTypeDVWC, dvcc.data(), dvccSize);
2161                 } else if (profile > (int32_t) profileVal1) {
2162                     meta->setData(kKeyDVVC, kTypeDVVC, dvcc.data(), dvccSize);
2163                 } else {
2164                     meta->setData(kKeyDVCC, kTypeDVCC, dvcc.data(), dvccSize);
2165                 }
2166 
2167             } else {
2168                 // we have csd-2, just use that to populate dvcc
2169                 if (csd2->size() == dvccSize) {
2170                     uint8_t *dvcc = csd2->data();
2171                     profile = dvcc[2] >> 1;
2172 
2173                     profiles.rlookup(DolbyVisionProfileDvav110, &profileVal);
2174                     profiles.rlookup(DolbyVisionProfileDvheDtb, &profileVal1);
2175                     if (profile > (int32_t) profileVal) {
2176                         meta->setData(kKeyDVWC, kTypeDVWC, csd2->data(), csd2->size());
2177                     } else if (profile > (int32_t) profileVal1) {
2178                         meta->setData(kKeyDVVC, kTypeDVVC, csd2->data(), csd2->size());
2179                     } else {
2180                          meta->setData(kKeyDVCC, kTypeDVCC, csd2->data(), csd2->size());
2181                     }
2182 
2183                 } else {
2184                     ALOGE("Convert MessageToMetadata csd-2 is present but not valid");
2185                     return BAD_VALUE;
2186                 }
2187             }
2188             profiles.rlookup(DolbyVisionProfileDvavPen, &profileVal);
2189             profiles.rlookup(DolbyVisionProfileDvavSe, &profileVal1);
2190             profiles.rlookup(DolbyVisionProfileDvav110, &profileVal2);
2191             if ((profile > (int32_t) profileVal) && (profile < (int32_t) profileVal1)) {
2192                 std::vector<uint8_t> hvcc(csd0size + 1024);
2193                 size_t outsize = reassembleHVCC(csd0, hvcc.data(), hvcc.size(), 4);
2194                 meta->setData(kKeyHVCC, kTypeHVCC, hvcc.data(), outsize);
2195             } else if (profile == (int32_t) profileVal2) {
2196                 meta->setData(kKeyAV1C, 0, csd0->data(), csd0->size());
2197             } else {
2198                 sp<ABuffer> csd1;
2199                 if (msg->findBuffer("csd-1", &csd1)) {
2200                     std::vector<char> avcc(csd0size + csd1->size() + 1024);
2201                     size_t outsize = reassembleAVCC(csd0, csd1, avcc.data());
2202                     meta->setData(kKeyAVCC, kTypeAVCC, avcc.data(), outsize);
2203                 }
2204                 else {
2205                     // for dolby vision avc, csd0 also holds csd1
2206                     size_t i = 0;
2207                     int csd0realsize = 0;
2208                     do {
2209                         i = findNextNalStartCode(csd0->data() + i,
2210                                         csd0->size() - i) - csd0->data();
2211                         if (i > 0) {
2212                             csd0realsize = i;
2213                             break;
2214                         }
2215                         i += 4;
2216                     } while(i < csd0->size());
2217                     // buffer0 -> csd0
2218                     sp<ABuffer> buffer0 = new (std::nothrow) ABuffer(csd0realsize);
2219                     if (buffer0.get() == NULL || buffer0->base() == NULL) {
2220                         return NO_MEMORY;
2221                     }
2222                     memcpy(buffer0->data(), csd0->data(), csd0realsize);
2223                     // buffer1 -> csd1
2224                     sp<ABuffer> buffer1 = new (std::nothrow)
2225                             ABuffer(csd0->size() - csd0realsize);
2226                     if (buffer1.get() == NULL || buffer1->base() == NULL) {
2227                         return NO_MEMORY;
2228                     }
2229                     memcpy(buffer1->data(), csd0->data()+csd0realsize,
2230                                 csd0->size() - csd0realsize);
2231 
2232                     std::vector<char> avcc(csd0->size() + 1024);
2233                     size_t outsize = reassembleAVCC(buffer0, buffer1, avcc.data());
2234                     meta->setData(kKeyAVCC, kTypeAVCC, avcc.data(), outsize);
2235                 }
2236             }
2237         } else if (mime == MEDIA_MIMETYPE_VIDEO_VP9) {
2238             meta->setData(kKeyVp9CodecPrivate, 0, csd0->data(), csd0->size());
2239         } else if (mime == MEDIA_MIMETYPE_AUDIO_OPUS) {
2240             size_t opusHeadSize = csd0->size();
2241             size_t codecDelayBufSize = 0;
2242             size_t seekPreRollBufSize = 0;
2243             void *opusHeadBuf = csd0->data();
2244             void *codecDelayBuf = NULL;
2245             void *seekPreRollBuf = NULL;
2246             if (msg->findBuffer("csd-1", &csd1)) {
2247                 codecDelayBufSize = csd1->size();
2248                 codecDelayBuf = csd1->data();
2249             }
2250             if (msg->findBuffer("csd-2", &csd2)) {
2251                 seekPreRollBufSize = csd2->size();
2252                 seekPreRollBuf = csd2->data();
2253             }
2254             /* Extract codec delay and seek pre roll from csd-0,
2255              * if csd-1 and csd-2 are not present */
2256             if (!codecDelayBuf && !seekPreRollBuf) {
2257                 GetOpusHeaderBuffers(csd0->data(), csd0->size(), &opusHeadBuf,
2258                                     &opusHeadSize, &codecDelayBuf,
2259                                     &codecDelayBufSize, &seekPreRollBuf,
2260                                     &seekPreRollBufSize);
2261             }
2262             meta->setData(kKeyOpusHeader, 0, opusHeadBuf, opusHeadSize);
2263             if (codecDelayBuf) {
2264                 meta->setData(kKeyOpusCodecDelay, 0, codecDelayBuf, codecDelayBufSize);
2265             }
2266             if (seekPreRollBuf) {
2267                 meta->setData(kKeyOpusSeekPreRoll, 0, seekPreRollBuf, seekPreRollBufSize);
2268             }
2269         } else if (mime == MEDIA_MIMETYPE_AUDIO_ALAC) {
2270             meta->setData(kKeyAlacMagicCookie, 0, csd0->data(), csd0->size());
2271         }
2272     } else if (mime == MEDIA_MIMETYPE_VIDEO_AVC && msg->findBuffer("csd-avc", &csd0)) {
2273         meta->setData(kKeyAVCC, kTypeAVCC, csd0->data(), csd0->size());
2274     } else if ((mime == MEDIA_MIMETYPE_VIDEO_HEVC || mime == MEDIA_MIMETYPE_IMAGE_ANDROID_HEIC)
2275             && msg->findBuffer("csd-hevc", &csd0)) {
2276         meta->setData(kKeyHVCC, kTypeHVCC, csd0->data(), csd0->size());
2277     } else if (msg->findBuffer("esds", &csd0)) {
2278         meta->setData(kKeyESDS, kTypeESDS, csd0->data(), csd0->size());
2279     } else if (msg->findBuffer("mpeg2-stream-header", &csd0)) {
2280         meta->setData(kKeyStreamHeader, 'mdat', csd0->data(), csd0->size());
2281     } else if (msg->findBuffer("d263", &csd0)) {
2282         meta->setData(kKeyD263, kTypeD263, csd0->data(), csd0->size());
2283     } else if (mime == MEDIA_MIMETYPE_VIDEO_DOLBY_VISION && msg->findBuffer("csd-2", &csd2)) {
2284         meta->setData(kKeyDVCC, kTypeDVCC, csd2->data(), csd2->size());
2285 
2286         // Remove CSD-2 from the data here to avoid duplicate data in meta
2287         meta->remove(kKeyOpaqueCSD2);
2288 
2289         if (msg->findBuffer("csd-avc", &csd0)) {
2290             meta->setData(kKeyAVCC, kTypeAVCC, csd0->data(), csd0->size());
2291         } else if (msg->findBuffer("csd-hevc", &csd0)) {
2292             meta->setData(kKeyHVCC, kTypeHVCC, csd0->data(), csd0->size());
2293         }
2294     }
2295     // XXX TODO add whatever other keys there are
2296 
2297 #if 0
2298     ALOGI("converted %s to:", msg->debugString(0).c_str());
2299     meta->dumpToLog();
2300 #endif
2301     return OK;
2302 }
2303 
sendMetaDataToHal(sp<MediaPlayerBase::AudioSink> & sink,const sp<MetaData> & meta)2304 status_t sendMetaDataToHal(sp<MediaPlayerBase::AudioSink>& sink,
2305                            const sp<MetaData>& meta)
2306 {
2307     int32_t sampleRate = 0;
2308     int32_t bitRate = 0;
2309     int32_t channelMask = 0;
2310     int32_t delaySamples = 0;
2311     int32_t paddingSamples = 0;
2312 
2313     AudioParameter param = AudioParameter();
2314 
2315     if (meta->findInt32(kKeySampleRate, &sampleRate)) {
2316         param.addInt(String8(AUDIO_OFFLOAD_CODEC_SAMPLE_RATE), sampleRate);
2317     }
2318     if (meta->findInt32(kKeyChannelMask, &channelMask)) {
2319         param.addInt(String8(AUDIO_OFFLOAD_CODEC_NUM_CHANNEL), channelMask);
2320     }
2321     if (meta->findInt32(kKeyBitRate, &bitRate)) {
2322         param.addInt(String8(AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE), bitRate);
2323     }
2324     if (meta->findInt32(kKeyEncoderDelay, &delaySamples)) {
2325         param.addInt(String8(AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES), delaySamples);
2326     }
2327     if (meta->findInt32(kKeyEncoderPadding, &paddingSamples)) {
2328         param.addInt(String8(AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES), paddingSamples);
2329     }
2330 
2331     ALOGV("sendMetaDataToHal: bitRate %d, sampleRate %d, chanMask %d,"
2332           "delaySample %d, paddingSample %d", bitRate, sampleRate,
2333           channelMask, delaySamples, paddingSamples);
2334 
2335     sink->setParameters(param.toString());
2336     return OK;
2337 }
2338 
2339 struct mime_conv_t {
2340     const char* mime;
2341     audio_format_t format;
2342 };
2343 
2344 static const struct mime_conv_t mimeLookup[] = {
2345     { MEDIA_MIMETYPE_AUDIO_MPEG,        AUDIO_FORMAT_MP3 },
2346     { MEDIA_MIMETYPE_AUDIO_RAW,         AUDIO_FORMAT_PCM_16_BIT },
2347     { MEDIA_MIMETYPE_AUDIO_AMR_NB,      AUDIO_FORMAT_AMR_NB },
2348     { MEDIA_MIMETYPE_AUDIO_AMR_WB,      AUDIO_FORMAT_AMR_WB },
2349     { MEDIA_MIMETYPE_AUDIO_AAC,         AUDIO_FORMAT_AAC },
2350     { MEDIA_MIMETYPE_AUDIO_VORBIS,      AUDIO_FORMAT_VORBIS },
2351     { MEDIA_MIMETYPE_AUDIO_OPUS,        AUDIO_FORMAT_OPUS},
2352     { MEDIA_MIMETYPE_AUDIO_AC3,         AUDIO_FORMAT_AC3},
2353     { MEDIA_MIMETYPE_AUDIO_EAC3,        AUDIO_FORMAT_E_AC3},
2354     { MEDIA_MIMETYPE_AUDIO_EAC3_JOC,    AUDIO_FORMAT_E_AC3_JOC},
2355     { MEDIA_MIMETYPE_AUDIO_AC4,         AUDIO_FORMAT_AC4},
2356     { MEDIA_MIMETYPE_AUDIO_FLAC,        AUDIO_FORMAT_FLAC},
2357     { MEDIA_MIMETYPE_AUDIO_ALAC,        AUDIO_FORMAT_ALAC },
2358     { 0, AUDIO_FORMAT_INVALID }
2359 };
2360 
mapMimeToAudioFormat(audio_format_t & format,const char * mime)2361 status_t mapMimeToAudioFormat( audio_format_t& format, const char* mime )
2362 {
2363 const struct mime_conv_t* p = &mimeLookup[0];
2364     while (p->mime != NULL) {
2365         if (0 == strcasecmp(mime, p->mime)) {
2366             format = p->format;
2367             return OK;
2368         }
2369         ++p;
2370     }
2371 
2372     return BAD_VALUE;
2373 }
2374 
2375 struct aac_format_conv_t {
2376     int32_t eAacProfileType;
2377     audio_format_t format;
2378 };
2379 
2380 static const struct aac_format_conv_t profileLookup[] = {
2381     { AACObjectMain,        AUDIO_FORMAT_AAC_MAIN},
2382     { AACObjectLC,          AUDIO_FORMAT_AAC_LC},
2383     { AACObjectSSR,         AUDIO_FORMAT_AAC_SSR},
2384     { AACObjectLTP,         AUDIO_FORMAT_AAC_LTP},
2385     { AACObjectHE,          AUDIO_FORMAT_AAC_HE_V1},
2386     { AACObjectScalable,    AUDIO_FORMAT_AAC_SCALABLE},
2387     { AACObjectERLC,        AUDIO_FORMAT_AAC_ERLC},
2388     { AACObjectLD,          AUDIO_FORMAT_AAC_LD},
2389     { AACObjectHE_PS,       AUDIO_FORMAT_AAC_HE_V2},
2390     { AACObjectELD,         AUDIO_FORMAT_AAC_ELD},
2391     { AACObjectXHE,         AUDIO_FORMAT_AAC_XHE},
2392     { AACObjectNull,        AUDIO_FORMAT_AAC},
2393 };
2394 
mapAACProfileToAudioFormat(audio_format_t & format,uint64_t eAacProfile)2395 void mapAACProfileToAudioFormat( audio_format_t& format, uint64_t eAacProfile)
2396 {
2397     const struct aac_format_conv_t* p = &profileLookup[0];
2398     while (p->eAacProfileType != AACObjectNull) {
2399         if (eAacProfile == p->eAacProfileType) {
2400             format = p->format;
2401             return;
2402         }
2403         ++p;
2404     }
2405     format = AUDIO_FORMAT_AAC;
2406     return;
2407 }
2408 
getAudioOffloadInfo(const sp<MetaData> & meta,bool hasVideo,bool isStreaming,audio_stream_type_t streamType,audio_offload_info_t * info)2409 status_t getAudioOffloadInfo(const sp<MetaData>& meta, bool hasVideo,
2410         bool isStreaming, audio_stream_type_t streamType, audio_offload_info_t *info)
2411 {
2412     const char *mime;
2413     if (meta == NULL) {
2414         return BAD_VALUE;
2415     }
2416     CHECK(meta->findCString(kKeyMIMEType, &mime));
2417 
2418     (*info) = AUDIO_INFO_INITIALIZER;
2419 
2420     info->format = AUDIO_FORMAT_INVALID;
2421     if (mapMimeToAudioFormat(info->format, mime) != OK) {
2422         ALOGE(" Couldn't map mime type \"%s\" to a valid AudioSystem::audio_format !", mime);
2423         return BAD_VALUE;
2424     } else {
2425         ALOGV("Mime type \"%s\" mapped to audio_format %d", mime, info->format);
2426     }
2427 
2428     if (AUDIO_FORMAT_INVALID == info->format) {
2429         // can't offload if we don't know what the source format is
2430         ALOGE("mime type \"%s\" not a known audio format", mime);
2431         return BAD_VALUE;
2432     }
2433 
2434     // Redefine aac format according to its profile
2435     // Offloading depends on audio DSP capabilities.
2436     int32_t aacaot = -1;
2437     if (meta->findInt32(kKeyAACAOT, &aacaot)) {
2438         mapAACProfileToAudioFormat(info->format, aacaot);
2439     }
2440 
2441     int32_t srate = -1;
2442     if (!meta->findInt32(kKeySampleRate, &srate)) {
2443         ALOGV("track of type '%s' does not publish sample rate", mime);
2444     }
2445     info->sample_rate = srate;
2446 
2447     int32_t rawChannelMask;
2448     audio_channel_mask_t cmask = meta->findInt32(kKeyChannelMask, &rawChannelMask) ?
2449             static_cast<audio_channel_mask_t>(rawChannelMask) : CHANNEL_MASK_USE_CHANNEL_ORDER;
2450     if (cmask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
2451         ALOGV("track of type '%s' does not publish channel mask", mime);
2452 
2453         // Try a channel count instead
2454         int32_t channelCount;
2455         if (!meta->findInt32(kKeyChannelCount, &channelCount)) {
2456             ALOGV("track of type '%s' does not publish channel count", mime);
2457         } else {
2458             cmask = audio_channel_out_mask_from_count(channelCount);
2459         }
2460     }
2461     info->channel_mask = cmask;
2462 
2463     int64_t duration = 0;
2464     if (!meta->findInt64(kKeyDuration, &duration)) {
2465         ALOGV("track of type '%s' does not publish duration", mime);
2466     }
2467     info->duration_us = duration;
2468 
2469     int32_t brate = 0;
2470     if (!meta->findInt32(kKeyBitRate, &brate)) {
2471         ALOGV("track of type '%s' does not publish bitrate", mime);
2472     }
2473     info->bit_rate = brate;
2474 
2475 
2476     info->stream_type = streamType;
2477     info->has_video = hasVideo;
2478     info->is_streaming = isStreaming;
2479     return OK;
2480 }
2481 
canOffloadStream(const sp<MetaData> & meta,bool hasVideo,bool isStreaming,audio_stream_type_t streamType)2482 bool canOffloadStream(const sp<MetaData>& meta, bool hasVideo,
2483                       bool isStreaming, audio_stream_type_t streamType)
2484 {
2485     audio_offload_info_t info = AUDIO_INFO_INITIALIZER;
2486     const char *mime;
2487     if (meta != nullptr && meta->findCString(kKeyMIMEType, &mime)
2488         && strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_OPUS) == 0) {
2489         return false;
2490     }
2491     if (OK != getAudioOffloadInfo(meta, hasVideo, isStreaming, streamType, &info)) {
2492         return false;
2493     }
2494     // Check if offload is possible for given format, stream type, sample rate,
2495     // bit rate, duration, video and streaming
2496 #ifdef DISABLE_AUDIO_SYSTEM_OFFLOAD
2497     return false;
2498 #else
2499     return AudioSystem::getOffloadSupport(info) != AUDIO_OFFLOAD_NOT_SUPPORTED;
2500 #endif
2501 }
2502 
HLSTime(const sp<AMessage> & meta)2503 HLSTime::HLSTime(const sp<AMessage>& meta) :
2504     mSeq(-1),
2505     mTimeUs(-1LL),
2506     mMeta(meta) {
2507     if (meta != NULL) {
2508         CHECK(meta->findInt32("discontinuitySeq", &mSeq));
2509         CHECK(meta->findInt64("timeUs", &mTimeUs));
2510     }
2511 }
2512 
getSegmentTimeUs() const2513 int64_t HLSTime::getSegmentTimeUs() const {
2514     int64_t segmentStartTimeUs = -1LL;
2515     if (mMeta != NULL) {
2516         CHECK(mMeta->findInt64("segmentStartTimeUs", &segmentStartTimeUs));
2517 
2518         int64_t segmentFirstTimeUs;
2519         if (mMeta->findInt64("segmentFirstTimeUs", &segmentFirstTimeUs)) {
2520             segmentStartTimeUs += mTimeUs - segmentFirstTimeUs;
2521         }
2522 
2523         // adjust segment time by playlist age (for live streaming)
2524         int64_t playlistTimeUs;
2525         if (mMeta->findInt64("playlistTimeUs", &playlistTimeUs)) {
2526             int64_t playlistAgeUs = ALooper::GetNowUs() - playlistTimeUs;
2527 
2528             int64_t durationUs;
2529             CHECK(mMeta->findInt64("segmentDurationUs", &durationUs));
2530 
2531             // round to nearest whole segment
2532             playlistAgeUs = (playlistAgeUs + durationUs / 2)
2533                     / durationUs * durationUs;
2534 
2535             segmentStartTimeUs -= playlistAgeUs;
2536             if (segmentStartTimeUs < 0) {
2537                 segmentStartTimeUs = 0;
2538             }
2539         }
2540     }
2541     return segmentStartTimeUs;
2542 }
2543 
operator <(const HLSTime & t0,const HLSTime & t1)2544 bool operator <(const HLSTime &t0, const HLSTime &t1) {
2545     // we can only compare discontinuity sequence and timestamp.
2546     // (mSegmentTimeUs is not reliable in live streaming case, it's the
2547     // time starting from beginning of playlist but playlist could change.)
2548     return t0.mSeq < t1.mSeq
2549             || (t0.mSeq == t1.mSeq && t0.mTimeUs < t1.mTimeUs);
2550 }
2551 
writeToAMessage(const sp<AMessage> & msg,const AudioPlaybackRate & rate)2552 void writeToAMessage(const sp<AMessage> &msg, const AudioPlaybackRate &rate) {
2553     msg->setFloat("speed", rate.mSpeed);
2554     msg->setFloat("pitch", rate.mPitch);
2555     msg->setInt32("audio-fallback-mode", rate.mFallbackMode);
2556     msg->setInt32("audio-stretch-mode", rate.mStretchMode);
2557 }
2558 
readFromAMessage(const sp<AMessage> & msg,AudioPlaybackRate * rate)2559 void readFromAMessage(const sp<AMessage> &msg, AudioPlaybackRate *rate /* nonnull */) {
2560     *rate = AUDIO_PLAYBACK_RATE_DEFAULT;
2561     CHECK(msg->findFloat("speed", &rate->mSpeed));
2562     CHECK(msg->findFloat("pitch", &rate->mPitch));
2563     CHECK(msg->findInt32("audio-fallback-mode", (int32_t *)&rate->mFallbackMode));
2564     CHECK(msg->findInt32("audio-stretch-mode", (int32_t *)&rate->mStretchMode));
2565 }
2566 
writeToAMessage(const sp<AMessage> & msg,const AVSyncSettings & sync,float videoFpsHint)2567 void writeToAMessage(const sp<AMessage> &msg, const AVSyncSettings &sync, float videoFpsHint) {
2568     msg->setInt32("sync-source", sync.mSource);
2569     msg->setInt32("audio-adjust-mode", sync.mAudioAdjustMode);
2570     msg->setFloat("tolerance", sync.mTolerance);
2571     msg->setFloat("video-fps", videoFpsHint);
2572 }
2573 
readFromAMessage(const sp<AMessage> & msg,AVSyncSettings * sync,float * videoFps)2574 void readFromAMessage(
2575         const sp<AMessage> &msg,
2576         AVSyncSettings *sync /* nonnull */,
2577         float *videoFps /* nonnull */) {
2578     AVSyncSettings settings;
2579     CHECK(msg->findInt32("sync-source", (int32_t *)&settings.mSource));
2580     CHECK(msg->findInt32("audio-adjust-mode", (int32_t *)&settings.mAudioAdjustMode));
2581     CHECK(msg->findFloat("tolerance", &settings.mTolerance));
2582     CHECK(msg->findFloat("video-fps", videoFps));
2583     *sync = settings;
2584 }
2585 
writeToAMessage(const sp<AMessage> & msg,const BufferingSettings & buffering)2586 void writeToAMessage(const sp<AMessage> &msg, const BufferingSettings &buffering) {
2587     msg->setInt32("init-ms", buffering.mInitialMarkMs);
2588     msg->setInt32("resume-playback-ms", buffering.mResumePlaybackMarkMs);
2589 }
2590 
readFromAMessage(const sp<AMessage> & msg,BufferingSettings * buffering)2591 void readFromAMessage(const sp<AMessage> &msg, BufferingSettings *buffering /* nonnull */) {
2592     int32_t value;
2593     if (msg->findInt32("init-ms", &value)) {
2594         buffering->mInitialMarkMs = value;
2595     }
2596     if (msg->findInt32("resume-playback-ms", &value)) {
2597         buffering->mResumePlaybackMarkMs = value;
2598     }
2599 }
2600 
2601 }  // namespace android
2602