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