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/MetaData.h> 41 #include <media/stagefright/MediaDefs.h> 42 #include <media/AudioSystem.h> 43 #include <media/MediaPlayerInterface.h> 44 #include <media/stagefright/Utils.h> 45 #include <media/AudioParameter.h> 46 #include <system/audio.h> 47 48 namespace android { 49 50 static status_t copyNALUToABuffer(sp<ABuffer> *buffer, const uint8_t *ptr, size_t length) { 51 if (((*buffer)->size() + 4 + length) > ((*buffer)->capacity() - (*buffer)->offset())) { 52 sp<ABuffer> tmpBuffer = new (std::nothrow) ABuffer((*buffer)->size() + 4 + length + 1024); 53 if (tmpBuffer.get() == NULL || tmpBuffer->base() == NULL) { 54 return NO_MEMORY; 55 } 56 memcpy(tmpBuffer->data(), (*buffer)->data(), (*buffer)->size()); 57 tmpBuffer->setRange(0, (*buffer)->size()); 58 (*buffer) = tmpBuffer; 59 } 60 61 memcpy((*buffer)->data() + (*buffer)->size(), "\x00\x00\x00\x01", 4); 62 memcpy((*buffer)->data() + (*buffer)->size() + 4, ptr, length); 63 (*buffer)->setRange((*buffer)->offset(), (*buffer)->size() + 4 + length); 64 return OK; 65 } 66 67 #if 0 68 static void convertMetaDataToMessageInt32( 69 const sp<MetaData> &meta, sp<AMessage> &msg, uint32_t key, const char *name) { 70 int32_t value; 71 if (meta->findInt32(key, &value)) { 72 msg->setInt32(name, value); 73 } 74 } 75 #endif 76 77 static void convertMetaDataToMessageColorAspects(const sp<MetaData> &meta, sp<AMessage> &msg) { 78 // 0 values are unspecified 79 int32_t range = 0; 80 int32_t primaries = 0; 81 int32_t transferFunction = 0; 82 int32_t colorMatrix = 0; 83 meta->findInt32(kKeyColorRange, &range); 84 meta->findInt32(kKeyColorPrimaries, &primaries); 85 meta->findInt32(kKeyTransferFunction, &transferFunction); 86 meta->findInt32(kKeyColorMatrix, &colorMatrix); 87 ColorAspects colorAspects; 88 memset(&colorAspects, 0, sizeof(colorAspects)); 89 colorAspects.mRange = (ColorAspects::Range)range; 90 colorAspects.mPrimaries = (ColorAspects::Primaries)primaries; 91 colorAspects.mTransfer = (ColorAspects::Transfer)transferFunction; 92 colorAspects.mMatrixCoeffs = (ColorAspects::MatrixCoeffs)colorMatrix; 93 94 int32_t rangeMsg, standardMsg, transferMsg; 95 if (CodecBase::convertCodecColorAspectsToPlatformAspects( 96 colorAspects, &rangeMsg, &standardMsg, &transferMsg) != OK) { 97 return; 98 } 99 100 // save specified values to msg 101 if (rangeMsg != 0) { 102 msg->setInt32("color-range", rangeMsg); 103 } 104 if (standardMsg != 0) { 105 msg->setInt32("color-standard", standardMsg); 106 } 107 if (transferMsg != 0) { 108 msg->setInt32("color-transfer", transferMsg); 109 } 110 } 111 112 static bool isHdr(const sp<AMessage> &format) { 113 // if CSD specifies HDR transfer(s), we assume HDR. Otherwise, if it specifies non-HDR 114 // transfers, we must assume non-HDR. This is because CSD trumps any color-transfer key 115 // in the format. 116 int32_t isHdr; 117 if (format->findInt32("android._is-hdr", &isHdr)) { 118 return isHdr; 119 } 120 121 // if user/container supplied HDR static info without transfer set, assume true 122 if (format->contains("hdr-static-info") && !format->contains("color-transfer")) { 123 return true; 124 } 125 // otherwise, verify that an HDR transfer function is set 126 int32_t transfer; 127 if (format->findInt32("color-transfer", &transfer)) { 128 return transfer == ColorUtils::kColorTransferST2084 129 || transfer == ColorUtils::kColorTransferHLG; 130 } 131 return false; 132 } 133 134 static void parseAacProfileFromCsd(const sp<ABuffer> &csd, sp<AMessage> &format) { 135 if (csd->size() < 2) { 136 return; 137 } 138 139 uint16_t audioObjectType = U16_AT((uint8_t*)csd->data()); 140 if ((audioObjectType & 0xF800) == 0xF800) { 141 audioObjectType = 32 + ((audioObjectType >> 5) & 0x3F); 142 } else { 143 audioObjectType >>= 11; 144 } 145 146 const static ALookup<uint16_t, OMX_AUDIO_AACPROFILETYPE> profiles { 147 { 1, OMX_AUDIO_AACObjectMain }, 148 { 2, OMX_AUDIO_AACObjectLC }, 149 { 3, OMX_AUDIO_AACObjectSSR }, 150 { 4, OMX_AUDIO_AACObjectLTP }, 151 { 5, OMX_AUDIO_AACObjectHE }, 152 { 6, OMX_AUDIO_AACObjectScalable }, 153 { 17, OMX_AUDIO_AACObjectERLC }, 154 { 23, OMX_AUDIO_AACObjectLD }, 155 { 29, OMX_AUDIO_AACObjectHE_PS }, 156 { 39, OMX_AUDIO_AACObjectELD }, 157 { 42, OMX_AUDIO_AACObjectXHE }, 158 }; 159 160 OMX_AUDIO_AACPROFILETYPE profile; 161 if (profiles.map(audioObjectType, &profile)) { 162 format->setInt32("profile", profile); 163 } 164 } 165 166 static void parseAvcProfileLevelFromAvcc(const uint8_t *ptr, size_t size, sp<AMessage> &format) { 167 if (size < 4 || ptr[0] != 1) { // configurationVersion == 1 168 return; 169 } 170 const uint8_t profile = ptr[1]; 171 const uint8_t constraints = ptr[2]; 172 const uint8_t level = ptr[3]; 173 174 const static ALookup<uint8_t, OMX_VIDEO_AVCLEVELTYPE> levels { 175 { 9, OMX_VIDEO_AVCLevel1b }, // technically, 9 is only used for High+ profiles 176 { 10, OMX_VIDEO_AVCLevel1 }, 177 { 11, OMX_VIDEO_AVCLevel11 }, // prefer level 1.1 for the value 11 178 { 11, OMX_VIDEO_AVCLevel1b }, 179 { 12, OMX_VIDEO_AVCLevel12 }, 180 { 13, OMX_VIDEO_AVCLevel13 }, 181 { 20, OMX_VIDEO_AVCLevel2 }, 182 { 21, OMX_VIDEO_AVCLevel21 }, 183 { 22, OMX_VIDEO_AVCLevel22 }, 184 { 30, OMX_VIDEO_AVCLevel3 }, 185 { 31, OMX_VIDEO_AVCLevel31 }, 186 { 32, OMX_VIDEO_AVCLevel32 }, 187 { 40, OMX_VIDEO_AVCLevel4 }, 188 { 41, OMX_VIDEO_AVCLevel41 }, 189 { 42, OMX_VIDEO_AVCLevel42 }, 190 { 50, OMX_VIDEO_AVCLevel5 }, 191 { 51, OMX_VIDEO_AVCLevel51 }, 192 { 52, OMX_VIDEO_AVCLevel52 }, 193 }; 194 const static ALookup<uint8_t, OMX_VIDEO_AVCPROFILETYPE> profiles { 195 { 66, OMX_VIDEO_AVCProfileBaseline }, 196 { 77, OMX_VIDEO_AVCProfileMain }, 197 { 88, OMX_VIDEO_AVCProfileExtended }, 198 { 100, OMX_VIDEO_AVCProfileHigh }, 199 { 110, OMX_VIDEO_AVCProfileHigh10 }, 200 { 122, OMX_VIDEO_AVCProfileHigh422 }, 201 { 244, OMX_VIDEO_AVCProfileHigh444 }, 202 }; 203 204 // set profile & level if they are recognized 205 OMX_VIDEO_AVCPROFILETYPE codecProfile; 206 OMX_VIDEO_AVCLEVELTYPE codecLevel; 207 if (profiles.map(profile, &codecProfile)) { 208 if (profile == 66 && (constraints & 0x40)) { 209 codecProfile = (OMX_VIDEO_AVCPROFILETYPE)OMX_VIDEO_AVCProfileConstrainedBaseline; 210 } else if (profile == 100 && (constraints & 0x0C) == 0x0C) { 211 codecProfile = (OMX_VIDEO_AVCPROFILETYPE)OMX_VIDEO_AVCProfileConstrainedHigh; 212 } 213 format->setInt32("profile", codecProfile); 214 if (levels.map(level, &codecLevel)) { 215 // for 9 && 11 decide level based on profile and constraint_set3 flag 216 if (level == 11 && (profile == 66 || profile == 77 || profile == 88)) { 217 codecLevel = (constraints & 0x10) ? OMX_VIDEO_AVCLevel1b : OMX_VIDEO_AVCLevel11; 218 } 219 format->setInt32("level", codecLevel); 220 } 221 } 222 } 223 224 static void parseH263ProfileLevelFromD263(const uint8_t *ptr, size_t size, sp<AMessage> &format) { 225 if (size < 7) { 226 return; 227 } 228 229 const uint8_t profile = ptr[6]; 230 const uint8_t level = ptr[5]; 231 232 const static ALookup<uint8_t, OMX_VIDEO_H263PROFILETYPE> profiles { 233 { 0, OMX_VIDEO_H263ProfileBaseline }, 234 { 1, OMX_VIDEO_H263ProfileH320Coding }, 235 { 2, OMX_VIDEO_H263ProfileBackwardCompatible }, 236 { 3, OMX_VIDEO_H263ProfileISWV2 }, 237 { 4, OMX_VIDEO_H263ProfileISWV3 }, 238 { 5, OMX_VIDEO_H263ProfileHighCompression }, 239 { 6, OMX_VIDEO_H263ProfileInternet }, 240 { 7, OMX_VIDEO_H263ProfileInterlace }, 241 { 8, OMX_VIDEO_H263ProfileHighLatency }, 242 }; 243 244 const static ALookup<uint8_t, OMX_VIDEO_H263LEVELTYPE> levels { 245 { 10, OMX_VIDEO_H263Level10 }, 246 { 20, OMX_VIDEO_H263Level20 }, 247 { 30, OMX_VIDEO_H263Level30 }, 248 { 40, OMX_VIDEO_H263Level40 }, 249 { 45, OMX_VIDEO_H263Level45 }, 250 { 50, OMX_VIDEO_H263Level50 }, 251 { 60, OMX_VIDEO_H263Level60 }, 252 { 70, OMX_VIDEO_H263Level70 }, 253 }; 254 255 // set profile & level if they are recognized 256 OMX_VIDEO_H263PROFILETYPE codecProfile; 257 OMX_VIDEO_H263LEVELTYPE codecLevel; 258 if (profiles.map(profile, &codecProfile)) { 259 format->setInt32("profile", codecProfile); 260 if (levels.map(level, &codecLevel)) { 261 format->setInt32("level", codecLevel); 262 } 263 } 264 } 265 266 static void parseHevcProfileLevelFromHvcc(const uint8_t *ptr, size_t size, sp<AMessage> &format) { 267 if (size < 13 || ptr[0] != 1) { // configurationVersion == 1 268 return; 269 } 270 271 const uint8_t profile = ptr[1] & 0x1F; 272 const uint8_t tier = (ptr[1] & 0x20) >> 5; 273 const uint8_t level = ptr[12]; 274 275 const static ALookup<std::pair<uint8_t, uint8_t>, OMX_VIDEO_HEVCLEVELTYPE> levels { 276 { { 0, 30 }, OMX_VIDEO_HEVCMainTierLevel1 }, 277 { { 0, 60 }, OMX_VIDEO_HEVCMainTierLevel2 }, 278 { { 0, 63 }, OMX_VIDEO_HEVCMainTierLevel21 }, 279 { { 0, 90 }, OMX_VIDEO_HEVCMainTierLevel3 }, 280 { { 0, 93 }, OMX_VIDEO_HEVCMainTierLevel31 }, 281 { { 0, 120 }, OMX_VIDEO_HEVCMainTierLevel4 }, 282 { { 0, 123 }, OMX_VIDEO_HEVCMainTierLevel41 }, 283 { { 0, 150 }, OMX_VIDEO_HEVCMainTierLevel5 }, 284 { { 0, 153 }, OMX_VIDEO_HEVCMainTierLevel51 }, 285 { { 0, 156 }, OMX_VIDEO_HEVCMainTierLevel52 }, 286 { { 0, 180 }, OMX_VIDEO_HEVCMainTierLevel6 }, 287 { { 0, 183 }, OMX_VIDEO_HEVCMainTierLevel61 }, 288 { { 0, 186 }, OMX_VIDEO_HEVCMainTierLevel62 }, 289 { { 1, 30 }, OMX_VIDEO_HEVCHighTierLevel1 }, 290 { { 1, 60 }, OMX_VIDEO_HEVCHighTierLevel2 }, 291 { { 1, 63 }, OMX_VIDEO_HEVCHighTierLevel21 }, 292 { { 1, 90 }, OMX_VIDEO_HEVCHighTierLevel3 }, 293 { { 1, 93 }, OMX_VIDEO_HEVCHighTierLevel31 }, 294 { { 1, 120 }, OMX_VIDEO_HEVCHighTierLevel4 }, 295 { { 1, 123 }, OMX_VIDEO_HEVCHighTierLevel41 }, 296 { { 1, 150 }, OMX_VIDEO_HEVCHighTierLevel5 }, 297 { { 1, 153 }, OMX_VIDEO_HEVCHighTierLevel51 }, 298 { { 1, 156 }, OMX_VIDEO_HEVCHighTierLevel52 }, 299 { { 1, 180 }, OMX_VIDEO_HEVCHighTierLevel6 }, 300 { { 1, 183 }, OMX_VIDEO_HEVCHighTierLevel61 }, 301 { { 1, 186 }, OMX_VIDEO_HEVCHighTierLevel62 }, 302 }; 303 304 const static ALookup<uint8_t, OMX_VIDEO_HEVCPROFILETYPE> profiles { 305 { 1, OMX_VIDEO_HEVCProfileMain }, 306 { 2, OMX_VIDEO_HEVCProfileMain10 }, 307 // use Main for Main Still Picture decoding 308 { 3, OMX_VIDEO_HEVCProfileMain }, 309 }; 310 311 // set profile & level if they are recognized 312 OMX_VIDEO_HEVCPROFILETYPE codecProfile; 313 OMX_VIDEO_HEVCLEVELTYPE codecLevel; 314 if (!profiles.map(profile, &codecProfile)) { 315 if (ptr[2] & 0x40 /* general compatibility flag 1 */) { 316 // Note that this case covers Main Still Picture too 317 codecProfile = OMX_VIDEO_HEVCProfileMain; 318 } else if (ptr[2] & 0x20 /* general compatibility flag 2 */) { 319 codecProfile = OMX_VIDEO_HEVCProfileMain10; 320 } else { 321 return; 322 } 323 } 324 325 // bump to HDR profile 326 if (isHdr(format) && codecProfile == OMX_VIDEO_HEVCProfileMain10) { 327 codecProfile = OMX_VIDEO_HEVCProfileMain10HDR10; 328 } 329 330 format->setInt32("profile", codecProfile); 331 if (levels.map(std::make_pair(tier, level), &codecLevel)) { 332 format->setInt32("level", codecLevel); 333 } 334 } 335 336 static void parseMpeg2ProfileLevelFromHeader( 337 const uint8_t *data, size_t size, sp<AMessage> &format) { 338 // find sequence extension 339 const uint8_t *seq = (const uint8_t*)memmem(data, size, "\x00\x00\x01\xB5", 4); 340 if (seq != NULL && seq + 5 < data + size) { 341 const uint8_t start_code = seq[4] >> 4; 342 if (start_code != 1 /* sequence extension ID */) { 343 return; 344 } 345 const uint8_t indication = ((seq[4] & 0xF) << 4) | ((seq[5] & 0xF0) >> 4); 346 347 const static ALookup<uint8_t, OMX_VIDEO_MPEG2PROFILETYPE> profiles { 348 { 0x50, OMX_VIDEO_MPEG2ProfileSimple }, 349 { 0x40, OMX_VIDEO_MPEG2ProfileMain }, 350 { 0x30, OMX_VIDEO_MPEG2ProfileSNR }, 351 { 0x20, OMX_VIDEO_MPEG2ProfileSpatial }, 352 { 0x10, OMX_VIDEO_MPEG2ProfileHigh }, 353 }; 354 355 const static ALookup<uint8_t, OMX_VIDEO_MPEG2LEVELTYPE> levels { 356 { 0x0A, OMX_VIDEO_MPEG2LevelLL }, 357 { 0x08, OMX_VIDEO_MPEG2LevelML }, 358 { 0x06, OMX_VIDEO_MPEG2LevelH14 }, 359 { 0x04, OMX_VIDEO_MPEG2LevelHL }, 360 { 0x02, OMX_VIDEO_MPEG2LevelHP }, 361 }; 362 363 const static ALookup<uint8_t, 364 std::pair<OMX_VIDEO_MPEG2PROFILETYPE, OMX_VIDEO_MPEG2LEVELTYPE>> escapes { 365 /* unsupported 366 { 0x8E, { XXX_MPEG2ProfileMultiView, OMX_VIDEO_MPEG2LevelLL } }, 367 { 0x8D, { XXX_MPEG2ProfileMultiView, OMX_VIDEO_MPEG2LevelML } }, 368 { 0x8B, { XXX_MPEG2ProfileMultiView, OMX_VIDEO_MPEG2LevelH14 } }, 369 { 0x8A, { XXX_MPEG2ProfileMultiView, OMX_VIDEO_MPEG2LevelHL } }, */ 370 { 0x85, { OMX_VIDEO_MPEG2Profile422, OMX_VIDEO_MPEG2LevelML } }, 371 { 0x82, { OMX_VIDEO_MPEG2Profile422, OMX_VIDEO_MPEG2LevelHL } }, 372 }; 373 374 OMX_VIDEO_MPEG2PROFILETYPE profile; 375 OMX_VIDEO_MPEG2LEVELTYPE level; 376 std::pair<OMX_VIDEO_MPEG2PROFILETYPE, OMX_VIDEO_MPEG2LEVELTYPE> profileLevel; 377 if (escapes.map(indication, &profileLevel)) { 378 format->setInt32("profile", profileLevel.first); 379 format->setInt32("level", profileLevel.second); 380 } else if (profiles.map(indication & 0x70, &profile)) { 381 format->setInt32("profile", profile); 382 if (levels.map(indication & 0xF, &level)) { 383 format->setInt32("level", level); 384 } 385 } 386 } 387 } 388 389 static void parseMpeg2ProfileLevelFromEsds(ESDS &esds, sp<AMessage> &format) { 390 // esds seems to only contain the profile for MPEG-2 391 uint8_t objType; 392 if (esds.getObjectTypeIndication(&objType) == OK) { 393 const static ALookup<uint8_t, OMX_VIDEO_MPEG2PROFILETYPE> profiles{ 394 { 0x60, OMX_VIDEO_MPEG2ProfileSimple }, 395 { 0x61, OMX_VIDEO_MPEG2ProfileMain }, 396 { 0x62, OMX_VIDEO_MPEG2ProfileSNR }, 397 { 0x63, OMX_VIDEO_MPEG2ProfileSpatial }, 398 { 0x64, OMX_VIDEO_MPEG2ProfileHigh }, 399 { 0x65, OMX_VIDEO_MPEG2Profile422 }, 400 }; 401 402 OMX_VIDEO_MPEG2PROFILETYPE profile; 403 if (profiles.map(objType, &profile)) { 404 format->setInt32("profile", profile); 405 } 406 } 407 } 408 409 static void parseMpeg4ProfileLevelFromCsd(const sp<ABuffer> &csd, sp<AMessage> &format) { 410 const uint8_t *data = csd->data(); 411 // find visual object sequence 412 const uint8_t *seq = (const uint8_t*)memmem(data, csd->size(), "\x00\x00\x01\xB0", 4); 413 if (seq != NULL && seq + 4 < data + csd->size()) { 414 const uint8_t indication = seq[4]; 415 416 const static ALookup<uint8_t, 417 std::pair<OMX_VIDEO_MPEG4PROFILETYPE, OMX_VIDEO_MPEG4LEVELTYPE>> table { 418 { 0b00000001, { OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level1 } }, 419 { 0b00000010, { OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level2 } }, 420 { 0b00000011, { OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level3 } }, 421 { 0b00000100, { OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level4a } }, 422 { 0b00000101, { OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level5 } }, 423 { 0b00000110, { OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level6 } }, 424 { 0b00001000, { OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level0 } }, 425 { 0b00001001, { OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level0b } }, 426 { 0b00010000, { OMX_VIDEO_MPEG4ProfileSimpleScalable, OMX_VIDEO_MPEG4Level0 } }, 427 { 0b00010001, { OMX_VIDEO_MPEG4ProfileSimpleScalable, OMX_VIDEO_MPEG4Level1 } }, 428 { 0b00010010, { OMX_VIDEO_MPEG4ProfileSimpleScalable, OMX_VIDEO_MPEG4Level2 } }, 429 /* unsupported 430 { 0b00011101, { XXX_MPEG4ProfileSimpleScalableER, OMX_VIDEO_MPEG4Level0 } }, 431 { 0b00011110, { XXX_MPEG4ProfileSimpleScalableER, OMX_VIDEO_MPEG4Level1 } }, 432 { 0b00011111, { XXX_MPEG4ProfileSimpleScalableER, OMX_VIDEO_MPEG4Level2 } }, */ 433 { 0b00100001, { OMX_VIDEO_MPEG4ProfileCore, OMX_VIDEO_MPEG4Level1 } }, 434 { 0b00100010, { OMX_VIDEO_MPEG4ProfileCore, OMX_VIDEO_MPEG4Level2 } }, 435 { 0b00110010, { OMX_VIDEO_MPEG4ProfileMain, OMX_VIDEO_MPEG4Level2 } }, 436 { 0b00110011, { OMX_VIDEO_MPEG4ProfileMain, OMX_VIDEO_MPEG4Level3 } }, 437 { 0b00110100, { OMX_VIDEO_MPEG4ProfileMain, OMX_VIDEO_MPEG4Level4 } }, 438 /* deprecated 439 { 0b01000010, { OMX_VIDEO_MPEG4ProfileNbit, OMX_VIDEO_MPEG4Level2 } }, */ 440 { 0b01010001, { OMX_VIDEO_MPEG4ProfileScalableTexture, OMX_VIDEO_MPEG4Level1 } }, 441 { 0b01100001, { OMX_VIDEO_MPEG4ProfileSimpleFace, OMX_VIDEO_MPEG4Level1 } }, 442 { 0b01100010, { OMX_VIDEO_MPEG4ProfileSimpleFace, OMX_VIDEO_MPEG4Level2 } }, 443 { 0b01100011, { OMX_VIDEO_MPEG4ProfileSimpleFBA, OMX_VIDEO_MPEG4Level1 } }, 444 { 0b01100100, { OMX_VIDEO_MPEG4ProfileSimpleFBA, OMX_VIDEO_MPEG4Level2 } }, 445 { 0b01110001, { OMX_VIDEO_MPEG4ProfileBasicAnimated, OMX_VIDEO_MPEG4Level1 } }, 446 { 0b01110010, { OMX_VIDEO_MPEG4ProfileBasicAnimated, OMX_VIDEO_MPEG4Level2 } }, 447 { 0b10000001, { OMX_VIDEO_MPEG4ProfileHybrid, OMX_VIDEO_MPEG4Level1 } }, 448 { 0b10000010, { OMX_VIDEO_MPEG4ProfileHybrid, OMX_VIDEO_MPEG4Level2 } }, 449 { 0b10010001, { OMX_VIDEO_MPEG4ProfileAdvancedRealTime, OMX_VIDEO_MPEG4Level1 } }, 450 { 0b10010010, { OMX_VIDEO_MPEG4ProfileAdvancedRealTime, OMX_VIDEO_MPEG4Level2 } }, 451 { 0b10010011, { OMX_VIDEO_MPEG4ProfileAdvancedRealTime, OMX_VIDEO_MPEG4Level3 } }, 452 { 0b10010100, { OMX_VIDEO_MPEG4ProfileAdvancedRealTime, OMX_VIDEO_MPEG4Level4 } }, 453 { 0b10100001, { OMX_VIDEO_MPEG4ProfileCoreScalable, OMX_VIDEO_MPEG4Level1 } }, 454 { 0b10100010, { OMX_VIDEO_MPEG4ProfileCoreScalable, OMX_VIDEO_MPEG4Level2 } }, 455 { 0b10100011, { OMX_VIDEO_MPEG4ProfileCoreScalable, OMX_VIDEO_MPEG4Level3 } }, 456 { 0b10110001, { OMX_VIDEO_MPEG4ProfileAdvancedCoding, OMX_VIDEO_MPEG4Level1 } }, 457 { 0b10110010, { OMX_VIDEO_MPEG4ProfileAdvancedCoding, OMX_VIDEO_MPEG4Level2 } }, 458 { 0b10110011, { OMX_VIDEO_MPEG4ProfileAdvancedCoding, OMX_VIDEO_MPEG4Level3 } }, 459 { 0b10110100, { OMX_VIDEO_MPEG4ProfileAdvancedCoding, OMX_VIDEO_MPEG4Level4 } }, 460 { 0b11000001, { OMX_VIDEO_MPEG4ProfileAdvancedCore, OMX_VIDEO_MPEG4Level1 } }, 461 { 0b11000010, { OMX_VIDEO_MPEG4ProfileAdvancedCore, OMX_VIDEO_MPEG4Level2 } }, 462 { 0b11010001, { OMX_VIDEO_MPEG4ProfileAdvancedScalable, OMX_VIDEO_MPEG4Level1 } }, 463 { 0b11010010, { OMX_VIDEO_MPEG4ProfileAdvancedScalable, OMX_VIDEO_MPEG4Level2 } }, 464 { 0b11010011, { OMX_VIDEO_MPEG4ProfileAdvancedScalable, OMX_VIDEO_MPEG4Level3 } }, 465 /* unsupported 466 { 0b11100001, { XXX_MPEG4ProfileSimpleStudio, OMX_VIDEO_MPEG4Level1 } }, 467 { 0b11100010, { XXX_MPEG4ProfileSimpleStudio, OMX_VIDEO_MPEG4Level2 } }, 468 { 0b11100011, { XXX_MPEG4ProfileSimpleStudio, OMX_VIDEO_MPEG4Level3 } }, 469 { 0b11100100, { XXX_MPEG4ProfileSimpleStudio, OMX_VIDEO_MPEG4Level4 } }, 470 { 0b11100101, { XXX_MPEG4ProfileCoreStudio, OMX_VIDEO_MPEG4Level1 } }, 471 { 0b11100110, { XXX_MPEG4ProfileCoreStudio, OMX_VIDEO_MPEG4Level2 } }, 472 { 0b11100111, { XXX_MPEG4ProfileCoreStudio, OMX_VIDEO_MPEG4Level3 } }, 473 { 0b11101000, { XXX_MPEG4ProfileCoreStudio, OMX_VIDEO_MPEG4Level4 } }, 474 { 0b11101011, { XXX_MPEG4ProfileSimpleStudio, OMX_VIDEO_MPEG4Level5 } }, 475 { 0b11101100, { XXX_MPEG4ProfileSimpleStudio, OMX_VIDEO_MPEG4Level6 } }, */ 476 { 0b11110000, { OMX_VIDEO_MPEG4ProfileAdvancedSimple, OMX_VIDEO_MPEG4Level0 } }, 477 { 0b11110001, { OMX_VIDEO_MPEG4ProfileAdvancedSimple, OMX_VIDEO_MPEG4Level1 } }, 478 { 0b11110010, { OMX_VIDEO_MPEG4ProfileAdvancedSimple, OMX_VIDEO_MPEG4Level2 } }, 479 { 0b11110011, { OMX_VIDEO_MPEG4ProfileAdvancedSimple, OMX_VIDEO_MPEG4Level3 } }, 480 { 0b11110100, { OMX_VIDEO_MPEG4ProfileAdvancedSimple, OMX_VIDEO_MPEG4Level4 } }, 481 { 0b11110101, { OMX_VIDEO_MPEG4ProfileAdvancedSimple, OMX_VIDEO_MPEG4Level5 } }, 482 { 0b11110111, { OMX_VIDEO_MPEG4ProfileAdvancedSimple, OMX_VIDEO_MPEG4Level3b } }, 483 /* deprecated 484 { 0b11111000, { XXX_MPEG4ProfileFineGranularityScalable, OMX_VIDEO_MPEG4Level0 } }, 485 { 0b11111001, { XXX_MPEG4ProfileFineGranularityScalable, OMX_VIDEO_MPEG4Level1 } }, 486 { 0b11111010, { XXX_MPEG4ProfileFineGranularityScalable, OMX_VIDEO_MPEG4Level2 } }, 487 { 0b11111011, { XXX_MPEG4ProfileFineGranularityScalable, OMX_VIDEO_MPEG4Level3 } }, 488 { 0b11111100, { XXX_MPEG4ProfileFineGranularityScalable, OMX_VIDEO_MPEG4Level4 } }, 489 { 0b11111101, { XXX_MPEG4ProfileFineGranularityScalable, OMX_VIDEO_MPEG4Level5 } }, */ 490 }; 491 492 std::pair<OMX_VIDEO_MPEG4PROFILETYPE, OMX_VIDEO_MPEG4LEVELTYPE> profileLevel; 493 if (table.map(indication, &profileLevel)) { 494 format->setInt32("profile", profileLevel.first); 495 format->setInt32("level", profileLevel.second); 496 } 497 } 498 } 499 500 static void parseVp9ProfileLevelFromCsd(const sp<ABuffer> &csd, sp<AMessage> &format) { 501 const uint8_t *data = csd->data(); 502 size_t remaining = csd->size(); 503 504 while (remaining >= 2) { 505 const uint8_t id = data[0]; 506 const uint8_t length = data[1]; 507 remaining -= 2; 508 data += 2; 509 if (length > remaining) { 510 break; 511 } 512 switch (id) { 513 case 1 /* profileId */: 514 if (length >= 1) { 515 const static ALookup<uint8_t, OMX_VIDEO_VP9PROFILETYPE> profiles { 516 { 0, OMX_VIDEO_VP9Profile0 }, 517 { 1, OMX_VIDEO_VP9Profile1 }, 518 { 2, OMX_VIDEO_VP9Profile2 }, 519 { 3, OMX_VIDEO_VP9Profile3 }, 520 }; 521 522 const static ALookup<OMX_VIDEO_VP9PROFILETYPE, OMX_VIDEO_VP9PROFILETYPE> toHdr { 523 { OMX_VIDEO_VP9Profile2, OMX_VIDEO_VP9Profile2HDR }, 524 { OMX_VIDEO_VP9Profile3, OMX_VIDEO_VP9Profile3HDR }, 525 }; 526 527 OMX_VIDEO_VP9PROFILETYPE profile; 528 if (profiles.map(data[0], &profile)) { 529 // convert to HDR profile 530 if (isHdr(format)) { 531 toHdr.lookup(profile, &profile); 532 } 533 534 format->setInt32("profile", profile); 535 } 536 } 537 break; 538 case 2 /* levelId */: 539 if (length >= 1) { 540 const static ALookup<uint8_t, OMX_VIDEO_VP9LEVELTYPE> levels { 541 { 10, OMX_VIDEO_VP9Level1 }, 542 { 11, OMX_VIDEO_VP9Level11 }, 543 { 20, OMX_VIDEO_VP9Level2 }, 544 { 21, OMX_VIDEO_VP9Level21 }, 545 { 30, OMX_VIDEO_VP9Level3 }, 546 { 31, OMX_VIDEO_VP9Level31 }, 547 { 40, OMX_VIDEO_VP9Level4 }, 548 { 41, OMX_VIDEO_VP9Level41 }, 549 { 50, OMX_VIDEO_VP9Level5 }, 550 { 51, OMX_VIDEO_VP9Level51 }, 551 { 52, OMX_VIDEO_VP9Level52 }, 552 { 60, OMX_VIDEO_VP9Level6 }, 553 { 61, OMX_VIDEO_VP9Level61 }, 554 { 62, OMX_VIDEO_VP9Level62 }, 555 }; 556 557 OMX_VIDEO_VP9LEVELTYPE level; 558 if (levels.map(data[0], &level)) { 559 format->setInt32("level", level); 560 } 561 } 562 break; 563 default: 564 break; 565 } 566 remaining -= length; 567 data += length; 568 } 569 } 570 571 status_t convertMetaDataToMessage( 572 const sp<MetaData> &meta, sp<AMessage> *format) { 573 574 format->clear(); 575 576 if (meta == NULL) { 577 ALOGE("convertMetaDataToMessage: NULL input"); 578 return BAD_VALUE; 579 } 580 581 const char *mime; 582 if (!meta->findCString(kKeyMIMEType, &mime)) { 583 return BAD_VALUE; 584 } 585 586 sp<AMessage> msg = new AMessage; 587 msg->setString("mime", mime); 588 589 uint32_t type; 590 const void *data; 591 size_t size; 592 if (meta->findData(kKeyCASessionID, &type, &data, &size)) { 593 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size); 594 if (buffer.get() == NULL || buffer->base() == NULL) { 595 return NO_MEMORY; 596 } 597 598 msg->setBuffer("ca-session-id", buffer); 599 memcpy(buffer->data(), data, size); 600 } 601 602 int32_t systemId; 603 if (meta->findInt32(kKeyCASystemID, &systemId)) { 604 msg->setInt32("ca-system-id", systemId); 605 } 606 607 if (!strncasecmp("video/scrambled", mime, 15) 608 || !strncasecmp("audio/scrambled", mime, 15)) { 609 610 *format = msg; 611 return OK; 612 } 613 614 int64_t durationUs; 615 if (meta->findInt64(kKeyDuration, &durationUs)) { 616 msg->setInt64("durationUs", durationUs); 617 } 618 619 int32_t avgBitRate = 0; 620 if (meta->findInt32(kKeyBitRate, &avgBitRate) && avgBitRate > 0) { 621 msg->setInt32("bitrate", avgBitRate); 622 } 623 624 int32_t maxBitRate; 625 if (meta->findInt32(kKeyMaxBitRate, &maxBitRate) 626 && maxBitRate > 0 && maxBitRate >= avgBitRate) { 627 msg->setInt32("max-bitrate", maxBitRate); 628 } 629 630 int32_t isSync; 631 if (meta->findInt32(kKeyIsSyncFrame, &isSync) && isSync != 0) { 632 msg->setInt32("is-sync-frame", 1); 633 } 634 635 // this only needs to be translated from meta to message as it is an extractor key 636 int32_t trackID; 637 if (meta->findInt32(kKeyTrackID, &trackID)) { 638 msg->setInt32("track-id", trackID); 639 } 640 641 const char *lang; 642 if (meta->findCString(kKeyMediaLanguage, &lang)) { 643 msg->setString("language", lang); 644 } 645 646 if (!strncasecmp("video/", mime, 6) || 647 !strncasecmp("image/", mime, 6)) { 648 int32_t width, height; 649 if (!meta->findInt32(kKeyWidth, &width) 650 || !meta->findInt32(kKeyHeight, &height)) { 651 return BAD_VALUE; 652 } 653 654 msg->setInt32("width", width); 655 msg->setInt32("height", height); 656 657 int32_t displayWidth, displayHeight; 658 if (meta->findInt32(kKeyDisplayWidth, &displayWidth) 659 && meta->findInt32(kKeyDisplayHeight, &displayHeight)) { 660 msg->setInt32("display-width", displayWidth); 661 msg->setInt32("display-height", displayHeight); 662 } 663 664 int32_t sarWidth, sarHeight; 665 if (meta->findInt32(kKeySARWidth, &sarWidth) 666 && meta->findInt32(kKeySARHeight, &sarHeight)) { 667 msg->setInt32("sar-width", sarWidth); 668 msg->setInt32("sar-height", sarHeight); 669 } 670 671 if (!strncasecmp("image/", mime, 6)) { 672 int32_t tileWidth, tileHeight, gridRows, gridCols; 673 if (meta->findInt32(kKeyTileWidth, &tileWidth) 674 && meta->findInt32(kKeyTileHeight, &tileHeight) 675 && meta->findInt32(kKeyGridRows, &gridRows) 676 && meta->findInt32(kKeyGridCols, &gridCols)) { 677 msg->setInt32("tile-width", tileWidth); 678 msg->setInt32("tile-height", tileHeight); 679 msg->setInt32("grid-rows", gridRows); 680 msg->setInt32("grid-cols", gridCols); 681 } 682 int32_t isPrimary; 683 if (meta->findInt32(kKeyTrackIsDefault, &isPrimary) && isPrimary) { 684 msg->setInt32("is-default", 1); 685 } 686 } 687 688 int32_t colorFormat; 689 if (meta->findInt32(kKeyColorFormat, &colorFormat)) { 690 msg->setInt32("color-format", colorFormat); 691 } 692 693 int32_t cropLeft, cropTop, cropRight, cropBottom; 694 if (meta->findRect(kKeyCropRect, 695 &cropLeft, 696 &cropTop, 697 &cropRight, 698 &cropBottom)) { 699 msg->setRect("crop", cropLeft, cropTop, cropRight, cropBottom); 700 } 701 702 int32_t rotationDegrees; 703 if (meta->findInt32(kKeyRotation, &rotationDegrees)) { 704 msg->setInt32("rotation-degrees", rotationDegrees); 705 } 706 707 uint32_t type; 708 const void *data; 709 size_t size; 710 if (meta->findData(kKeyHdrStaticInfo, &type, &data, &size) 711 && type == 'hdrS' && size == sizeof(HDRStaticInfo)) { 712 ColorUtils::setHDRStaticInfoIntoFormat(*(HDRStaticInfo*)data, msg); 713 } 714 715 convertMetaDataToMessageColorAspects(meta, msg); 716 } else if (!strncasecmp("audio/", mime, 6)) { 717 int32_t numChannels, sampleRate; 718 if (!meta->findInt32(kKeyChannelCount, &numChannels) 719 || !meta->findInt32(kKeySampleRate, &sampleRate)) { 720 return BAD_VALUE; 721 } 722 723 msg->setInt32("channel-count", numChannels); 724 msg->setInt32("sample-rate", sampleRate); 725 726 int32_t channelMask; 727 if (meta->findInt32(kKeyChannelMask, &channelMask)) { 728 msg->setInt32("channel-mask", channelMask); 729 } 730 731 int32_t delay = 0; 732 if (meta->findInt32(kKeyEncoderDelay, &delay)) { 733 msg->setInt32("encoder-delay", delay); 734 } 735 int32_t padding = 0; 736 if (meta->findInt32(kKeyEncoderPadding, &padding)) { 737 msg->setInt32("encoder-padding", padding); 738 } 739 740 int32_t isADTS; 741 if (meta->findInt32(kKeyIsADTS, &isADTS)) { 742 msg->setInt32("is-adts", isADTS); 743 } 744 745 int32_t aacProfile = -1; 746 if (meta->findInt32(kKeyAACAOT, &aacProfile)) { 747 msg->setInt32("aac-profile", aacProfile); 748 } 749 750 int32_t pcmEncoding; 751 if (meta->findInt32(kKeyPcmEncoding, &pcmEncoding)) { 752 msg->setInt32("pcm-encoding", pcmEncoding); 753 } 754 } 755 756 int32_t maxInputSize; 757 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) { 758 msg->setInt32("max-input-size", maxInputSize); 759 } 760 761 int32_t maxWidth; 762 if (meta->findInt32(kKeyMaxWidth, &maxWidth)) { 763 msg->setInt32("max-width", maxWidth); 764 } 765 766 int32_t maxHeight; 767 if (meta->findInt32(kKeyMaxHeight, &maxHeight)) { 768 msg->setInt32("max-height", maxHeight); 769 } 770 771 int32_t rotationDegrees; 772 if (meta->findInt32(kKeyRotation, &rotationDegrees)) { 773 msg->setInt32("rotation-degrees", rotationDegrees); 774 } 775 776 int32_t fps; 777 if (meta->findInt32(kKeyFrameRate, &fps) && fps > 0) { 778 msg->setInt32("frame-rate", fps); 779 } 780 781 if (meta->findData(kKeyAVCC, &type, &data, &size)) { 782 // Parse the AVCDecoderConfigurationRecord 783 784 const uint8_t *ptr = (const uint8_t *)data; 785 786 if (size < 7 || ptr[0] != 1) { // configurationVersion == 1 787 ALOGE("b/23680780"); 788 return BAD_VALUE; 789 } 790 791 parseAvcProfileLevelFromAvcc(ptr, size, msg); 792 793 // There is decodable content out there that fails the following 794 // assertion, let's be lenient for now... 795 // CHECK((ptr[4] >> 2) == 0x3f); // reserved 796 797 size_t lengthSize __unused = 1 + (ptr[4] & 3); 798 799 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp 800 // violates it... 801 // CHECK((ptr[5] >> 5) == 7); // reserved 802 803 size_t numSeqParameterSets = ptr[5] & 31; 804 805 ptr += 6; 806 size -= 6; 807 808 sp<ABuffer> buffer = new (std::nothrow) ABuffer(1024); 809 if (buffer.get() == NULL || buffer->base() == NULL) { 810 return NO_MEMORY; 811 } 812 buffer->setRange(0, 0); 813 814 for (size_t i = 0; i < numSeqParameterSets; ++i) { 815 if (size < 2) { 816 ALOGE("b/23680780"); 817 return BAD_VALUE; 818 } 819 size_t length = U16_AT(ptr); 820 821 ptr += 2; 822 size -= 2; 823 824 if (size < length) { 825 return BAD_VALUE; 826 } 827 status_t err = copyNALUToABuffer(&buffer, ptr, length); 828 if (err != OK) { 829 return err; 830 } 831 832 ptr += length; 833 size -= length; 834 } 835 836 buffer->meta()->setInt32("csd", true); 837 buffer->meta()->setInt64("timeUs", 0); 838 839 msg->setBuffer("csd-0", buffer); 840 841 buffer = new (std::nothrow) ABuffer(1024); 842 if (buffer.get() == NULL || buffer->base() == NULL) { 843 return NO_MEMORY; 844 } 845 buffer->setRange(0, 0); 846 847 if (size < 1) { 848 ALOGE("b/23680780"); 849 return BAD_VALUE; 850 } 851 size_t numPictureParameterSets = *ptr; 852 ++ptr; 853 --size; 854 855 for (size_t i = 0; i < numPictureParameterSets; ++i) { 856 if (size < 2) { 857 ALOGE("b/23680780"); 858 return BAD_VALUE; 859 } 860 size_t length = U16_AT(ptr); 861 862 ptr += 2; 863 size -= 2; 864 865 if (size < length) { 866 return BAD_VALUE; 867 } 868 status_t err = copyNALUToABuffer(&buffer, ptr, length); 869 if (err != OK) { 870 return err; 871 } 872 873 ptr += length; 874 size -= length; 875 } 876 877 buffer->meta()->setInt32("csd", true); 878 buffer->meta()->setInt64("timeUs", 0); 879 msg->setBuffer("csd-1", buffer); 880 } else if (meta->findData(kKeyHVCC, &type, &data, &size)) { 881 const uint8_t *ptr = (const uint8_t *)data; 882 883 if (size < 23 || ptr[0] != 1) { // configurationVersion == 1 884 ALOGE("b/23680780"); 885 return BAD_VALUE; 886 } 887 888 const size_t dataSize = size; // save for later 889 ptr += 22; 890 size -= 22; 891 892 size_t numofArrays = (char)ptr[0]; 893 ptr += 1; 894 size -= 1; 895 size_t j = 0, i = 0; 896 897 sp<ABuffer> buffer = new (std::nothrow) ABuffer(1024); 898 if (buffer.get() == NULL || buffer->base() == NULL) { 899 return NO_MEMORY; 900 } 901 buffer->setRange(0, 0); 902 903 HevcParameterSets hvcc; 904 905 for (i = 0; i < numofArrays; i++) { 906 if (size < 3) { 907 ALOGE("b/23680780"); 908 return BAD_VALUE; 909 } 910 ptr += 1; 911 size -= 1; 912 913 //Num of nals 914 size_t numofNals = U16_AT(ptr); 915 916 ptr += 2; 917 size -= 2; 918 919 for (j = 0; j < numofNals; j++) { 920 if (size < 2) { 921 ALOGE("b/23680780"); 922 return BAD_VALUE; 923 } 924 size_t length = U16_AT(ptr); 925 926 ptr += 2; 927 size -= 2; 928 929 if (size < length) { 930 return BAD_VALUE; 931 } 932 status_t err = copyNALUToABuffer(&buffer, ptr, length); 933 if (err != OK) { 934 return err; 935 } 936 (void)hvcc.addNalUnit(ptr, length); 937 938 ptr += length; 939 size -= length; 940 } 941 } 942 buffer->meta()->setInt32("csd", true); 943 buffer->meta()->setInt64("timeUs", 0); 944 msg->setBuffer("csd-0", buffer); 945 946 // if we saw VUI color information we know whether this is HDR because VUI trumps other 947 // format parameters for HEVC. 948 HevcParameterSets::Info info = hvcc.getInfo(); 949 if (info & hvcc.kInfoHasColorDescription) { 950 msg->setInt32("android._is-hdr", (info & hvcc.kInfoIsHdr) != 0); 951 } 952 953 parseHevcProfileLevelFromHvcc((const uint8_t *)data, dataSize, msg); 954 } else if (meta->findData(kKeyESDS, &type, &data, &size)) { 955 ESDS esds((const char *)data, size); 956 if (esds.InitCheck() != (status_t)OK) { 957 return BAD_VALUE; 958 } 959 960 const void *codec_specific_data; 961 size_t codec_specific_data_size; 962 esds.getCodecSpecificInfo( 963 &codec_specific_data, &codec_specific_data_size); 964 965 sp<ABuffer> buffer = new (std::nothrow) ABuffer(codec_specific_data_size); 966 if (buffer.get() == NULL || buffer->base() == NULL) { 967 return NO_MEMORY; 968 } 969 970 memcpy(buffer->data(), codec_specific_data, 971 codec_specific_data_size); 972 973 buffer->meta()->setInt32("csd", true); 974 buffer->meta()->setInt64("timeUs", 0); 975 msg->setBuffer("csd-0", buffer); 976 977 if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4)) { 978 parseMpeg4ProfileLevelFromCsd(buffer, msg); 979 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG2)) { 980 parseMpeg2ProfileLevelFromEsds(esds, msg); 981 if (meta->findData(kKeyStreamHeader, &type, &data, &size)) { 982 parseMpeg2ProfileLevelFromHeader((uint8_t*)data, size, msg); 983 } 984 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) { 985 parseAacProfileFromCsd(buffer, msg); 986 } 987 988 uint32_t maxBitrate, avgBitrate; 989 if (esds.getBitRate(&maxBitrate, &avgBitrate) == OK) { 990 if (!meta->hasData(kKeyBitRate) 991 && avgBitrate > 0 && avgBitrate <= INT32_MAX) { 992 msg->setInt32("bitrate", (int32_t)avgBitrate); 993 } else { 994 (void)msg->findInt32("bitrate", (int32_t*)&avgBitrate); 995 } 996 if (!meta->hasData(kKeyMaxBitRate) 997 && maxBitrate > 0 && maxBitrate <= INT32_MAX && maxBitrate >= avgBitrate) { 998 msg->setInt32("max-bitrate", (int32_t)maxBitrate); 999 } 1000 } 1001 } else if (meta->findData(kTypeD263, &type, &data, &size)) { 1002 const uint8_t *ptr = (const uint8_t *)data; 1003 parseH263ProfileLevelFromD263(ptr, size, msg); 1004 } else if (meta->findData(kKeyVorbisInfo, &type, &data, &size)) { 1005 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size); 1006 if (buffer.get() == NULL || buffer->base() == NULL) { 1007 return NO_MEMORY; 1008 } 1009 memcpy(buffer->data(), data, size); 1010 1011 buffer->meta()->setInt32("csd", true); 1012 buffer->meta()->setInt64("timeUs", 0); 1013 msg->setBuffer("csd-0", buffer); 1014 1015 if (!meta->findData(kKeyVorbisBooks, &type, &data, &size)) { 1016 return -EINVAL; 1017 } 1018 1019 buffer = new (std::nothrow) ABuffer(size); 1020 if (buffer.get() == NULL || buffer->base() == NULL) { 1021 return NO_MEMORY; 1022 } 1023 memcpy(buffer->data(), data, size); 1024 1025 buffer->meta()->setInt32("csd", true); 1026 buffer->meta()->setInt64("timeUs", 0); 1027 msg->setBuffer("csd-1", buffer); 1028 } else if (meta->findData(kKeyOpusHeader, &type, &data, &size)) { 1029 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size); 1030 if (buffer.get() == NULL || buffer->base() == NULL) { 1031 return NO_MEMORY; 1032 } 1033 memcpy(buffer->data(), data, size); 1034 1035 buffer->meta()->setInt32("csd", true); 1036 buffer->meta()->setInt64("timeUs", 0); 1037 msg->setBuffer("csd-0", buffer); 1038 1039 if (!meta->findData(kKeyOpusCodecDelay, &type, &data, &size)) { 1040 return -EINVAL; 1041 } 1042 1043 buffer = new (std::nothrow) ABuffer(size); 1044 if (buffer.get() == NULL || buffer->base() == NULL) { 1045 return NO_MEMORY; 1046 } 1047 memcpy(buffer->data(), data, size); 1048 1049 buffer->meta()->setInt32("csd", true); 1050 buffer->meta()->setInt64("timeUs", 0); 1051 msg->setBuffer("csd-1", buffer); 1052 1053 if (!meta->findData(kKeyOpusSeekPreRoll, &type, &data, &size)) { 1054 return -EINVAL; 1055 } 1056 1057 buffer = new (std::nothrow) ABuffer(size); 1058 if (buffer.get() == NULL || buffer->base() == NULL) { 1059 return NO_MEMORY; 1060 } 1061 memcpy(buffer->data(), data, size); 1062 1063 buffer->meta()->setInt32("csd", true); 1064 buffer->meta()->setInt64("timeUs", 0); 1065 msg->setBuffer("csd-2", buffer); 1066 } else if (meta->findData(kKeyFlacMetadata, &type, &data, &size)) { 1067 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size); 1068 if (buffer.get() == NULL || buffer->base() == NULL) { 1069 return NO_MEMORY; 1070 } 1071 memcpy(buffer->data(), data, size); 1072 1073 buffer->meta()->setInt32("csd", true); 1074 buffer->meta()->setInt64("timeUs", 0); 1075 msg->setBuffer("csd-0", buffer); 1076 } else if (meta->findData(kKeyVp9CodecPrivate, &type, &data, &size)) { 1077 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size); 1078 if (buffer.get() == NULL || buffer->base() == NULL) { 1079 return NO_MEMORY; 1080 } 1081 memcpy(buffer->data(), data, size); 1082 1083 buffer->meta()->setInt32("csd", true); 1084 buffer->meta()->setInt64("timeUs", 0); 1085 msg->setBuffer("csd-0", buffer); 1086 1087 parseVp9ProfileLevelFromCsd(buffer, msg); 1088 } 1089 1090 // TODO expose "crypto-key"/kKeyCryptoKey through public api 1091 if (meta->findData(kKeyCryptoKey, &type, &data, &size)) { 1092 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size); 1093 msg->setBuffer("crypto-key", buffer); 1094 memcpy(buffer->data(), data, size); 1095 } 1096 1097 *format = msg; 1098 1099 return OK; 1100 } 1101 1102 const uint8_t *findNextNalStartCode(const uint8_t *data, size_t length) { 1103 uint8_t *res = NULL; 1104 if (length > 4) { 1105 // minus 1 as to not match NAL start code at end 1106 res = (uint8_t *)memmem(data, length - 1, "\x00\x00\x00\x01", 4); 1107 } 1108 return res != NULL && res < data + length - 4 ? res : &data[length]; 1109 } 1110 1111 static size_t reassembleAVCC(const sp<ABuffer> &csd0, const sp<ABuffer> &csd1, char *avcc) { 1112 avcc[0] = 1; // version 1113 avcc[1] = 0x64; // profile (default to high) 1114 avcc[2] = 0; // constraints (default to none) 1115 avcc[3] = 0xd; // level (default to 1.3) 1116 avcc[4] = 0xff; // reserved+size 1117 1118 size_t i = 0; 1119 int numparams = 0; 1120 int lastparamoffset = 0; 1121 int avccidx = 6; 1122 do { 1123 i = findNextNalStartCode(csd0->data() + i, csd0->size() - i) - csd0->data(); 1124 ALOGV("block at %zu, last was %d", i, lastparamoffset); 1125 if (lastparamoffset > 0) { 1126 const uint8_t *lastparam = csd0->data() + lastparamoffset; 1127 int size = i - lastparamoffset; 1128 if (size > 3) { 1129 if (numparams && memcmp(avcc + 1, lastparam + 1, 3)) { 1130 ALOGW("Inconsisted profile/level found in SPS: %x,%x,%x vs %x,%x,%x", 1131 avcc[1], avcc[2], avcc[3], lastparam[1], lastparam[2], lastparam[3]); 1132 } else if (!numparams) { 1133 // fill in profile, constraints and level 1134 memcpy(avcc + 1, lastparam + 1, 3); 1135 } 1136 } 1137 avcc[avccidx++] = size >> 8; 1138 avcc[avccidx++] = size & 0xff; 1139 memcpy(avcc+avccidx, lastparam, size); 1140 avccidx += size; 1141 numparams++; 1142 } 1143 i += 4; 1144 lastparamoffset = i; 1145 } while(i < csd0->size()); 1146 ALOGV("csd0 contains %d params", numparams); 1147 1148 avcc[5] = 0xe0 | numparams; 1149 //and now csd-1 1150 i = 0; 1151 numparams = 0; 1152 lastparamoffset = 0; 1153 int numpicparamsoffset = avccidx; 1154 avccidx++; 1155 do { 1156 i = findNextNalStartCode(csd1->data() + i, csd1->size() - i) - csd1->data(); 1157 ALOGV("block at %zu, last was %d", i, lastparamoffset); 1158 if (lastparamoffset > 0) { 1159 int size = i - lastparamoffset; 1160 avcc[avccidx++] = size >> 8; 1161 avcc[avccidx++] = size & 0xff; 1162 memcpy(avcc+avccidx, csd1->data() + lastparamoffset, size); 1163 avccidx += size; 1164 numparams++; 1165 } 1166 i += 4; 1167 lastparamoffset = i; 1168 } while(i < csd1->size()); 1169 avcc[numpicparamsoffset] = numparams; 1170 return avccidx; 1171 } 1172 1173 static void reassembleESDS(const sp<ABuffer> &csd0, char *esds) { 1174 int csd0size = csd0->size(); 1175 esds[0] = 3; // kTag_ESDescriptor; 1176 int esdescriptorsize = 26 + csd0size; 1177 CHECK(esdescriptorsize < 268435456); // 7 bits per byte, so max is 2^28-1 1178 esds[1] = 0x80 | (esdescriptorsize >> 21); 1179 esds[2] = 0x80 | ((esdescriptorsize >> 14) & 0x7f); 1180 esds[3] = 0x80 | ((esdescriptorsize >> 7) & 0x7f); 1181 esds[4] = (esdescriptorsize & 0x7f); 1182 esds[5] = esds[6] = 0; // es id 1183 esds[7] = 0; // flags 1184 esds[8] = 4; // kTag_DecoderConfigDescriptor 1185 int configdescriptorsize = 18 + csd0size; 1186 esds[9] = 0x80 | (configdescriptorsize >> 21); 1187 esds[10] = 0x80 | ((configdescriptorsize >> 14) & 0x7f); 1188 esds[11] = 0x80 | ((configdescriptorsize >> 7) & 0x7f); 1189 esds[12] = (configdescriptorsize & 0x7f); 1190 esds[13] = 0x40; // objectTypeIndication 1191 // bytes 14-25 are examples from a real file. they are unused/overwritten by muxers. 1192 esds[14] = 0x15; // streamType(5), upStream(0), 1193 esds[15] = 0x00; // 15-17: bufferSizeDB (6KB) 1194 esds[16] = 0x18; 1195 esds[17] = 0x00; 1196 esds[18] = 0x00; // 18-21: maxBitrate (64kbps) 1197 esds[19] = 0x00; 1198 esds[20] = 0xfa; 1199 esds[21] = 0x00; 1200 esds[22] = 0x00; // 22-25: avgBitrate (64kbps) 1201 esds[23] = 0x00; 1202 esds[24] = 0xfa; 1203 esds[25] = 0x00; 1204 esds[26] = 5; // kTag_DecoderSpecificInfo; 1205 esds[27] = 0x80 | (csd0size >> 21); 1206 esds[28] = 0x80 | ((csd0size >> 14) & 0x7f); 1207 esds[29] = 0x80 | ((csd0size >> 7) & 0x7f); 1208 esds[30] = (csd0size & 0x7f); 1209 memcpy((void*)&esds[31], csd0->data(), csd0size); 1210 // data following this is ignored, so don't bother appending it 1211 } 1212 1213 static size_t reassembleHVCC(const sp<ABuffer> &csd0, uint8_t *hvcc, size_t hvccSize, size_t nalSizeLength) { 1214 HevcParameterSets paramSets; 1215 uint8_t* data = csd0->data(); 1216 if (csd0->size() < 4) { 1217 ALOGE("csd0 too small"); 1218 return 0; 1219 } 1220 if (memcmp(data, "\x00\x00\x00\x01", 4) != 0) { 1221 ALOGE("csd0 doesn't start with a start code"); 1222 return 0; 1223 } 1224 size_t prevNalOffset = 4; 1225 status_t err = OK; 1226 for (size_t i = 1; i < csd0->size() - 4; ++i) { 1227 if (memcmp(&data[i], "\x00\x00\x00\x01", 4) != 0) { 1228 continue; 1229 } 1230 err = paramSets.addNalUnit(&data[prevNalOffset], i - prevNalOffset); 1231 if (err != OK) { 1232 return 0; 1233 } 1234 prevNalOffset = i + 4; 1235 } 1236 err = paramSets.addNalUnit(&data[prevNalOffset], csd0->size() - prevNalOffset); 1237 if (err != OK) { 1238 return 0; 1239 } 1240 size_t size = hvccSize; 1241 err = paramSets.makeHvcc(hvcc, &size, nalSizeLength); 1242 if (err != OK) { 1243 return 0; 1244 } 1245 return size; 1246 } 1247 1248 #if 0 1249 static void convertMessageToMetaDataInt32( 1250 const sp<AMessage> &msg, sp<MetaData> &meta, uint32_t key, const char *name) { 1251 int32_t value; 1252 if (msg->findInt32(name, &value)) { 1253 meta->setInt32(key, value); 1254 } 1255 } 1256 #endif 1257 1258 static void convertMessageToMetaDataColorAspects(const sp<AMessage> &msg, sp<MetaData> &meta) { 1259 // 0 values are unspecified 1260 int32_t range = 0, standard = 0, transfer = 0; 1261 (void)msg->findInt32("color-range", &range); 1262 (void)msg->findInt32("color-standard", &standard); 1263 (void)msg->findInt32("color-transfer", &transfer); 1264 1265 ColorAspects colorAspects; 1266 memset(&colorAspects, 0, sizeof(colorAspects)); 1267 if (CodecBase::convertPlatformColorAspectsToCodecAspects( 1268 range, standard, transfer, colorAspects) != OK) { 1269 return; 1270 } 1271 1272 // save specified values to meta 1273 if (colorAspects.mRange != 0) { 1274 meta->setInt32(kKeyColorRange, colorAspects.mRange); 1275 } 1276 if (colorAspects.mPrimaries != 0) { 1277 meta->setInt32(kKeyColorPrimaries, colorAspects.mPrimaries); 1278 } 1279 if (colorAspects.mTransfer != 0) { 1280 meta->setInt32(kKeyTransferFunction, colorAspects.mTransfer); 1281 } 1282 if (colorAspects.mMatrixCoeffs != 0) { 1283 meta->setInt32(kKeyColorMatrix, colorAspects.mMatrixCoeffs); 1284 } 1285 } 1286 1287 void convertMessageToMetaData(const sp<AMessage> &msg, sp<MetaData> &meta) { 1288 AString mime; 1289 if (msg->findString("mime", &mime)) { 1290 meta->setCString(kKeyMIMEType, mime.c_str()); 1291 } else { 1292 ALOGW("did not find mime type"); 1293 } 1294 1295 int64_t durationUs; 1296 if (msg->findInt64("durationUs", &durationUs)) { 1297 meta->setInt64(kKeyDuration, durationUs); 1298 } 1299 1300 int32_t isSync; 1301 if (msg->findInt32("is-sync-frame", &isSync) && isSync != 0) { 1302 meta->setInt32(kKeyIsSyncFrame, 1); 1303 } 1304 1305 int32_t avgBitrate = 0; 1306 int32_t maxBitrate; 1307 if (msg->findInt32("bitrate", &avgBitrate) && avgBitrate > 0) { 1308 meta->setInt32(kKeyBitRate, avgBitrate); 1309 } 1310 if (msg->findInt32("max-bitrate", &maxBitrate) && maxBitrate > 0 && maxBitrate >= avgBitrate) { 1311 meta->setInt32(kKeyMaxBitRate, maxBitrate); 1312 } 1313 1314 AString lang; 1315 if (msg->findString("language", &lang)) { 1316 meta->setCString(kKeyMediaLanguage, lang.c_str()); 1317 } 1318 1319 if (mime.startsWith("video/") || mime.startsWith("image/")) { 1320 int32_t width; 1321 int32_t height; 1322 if (msg->findInt32("width", &width) && msg->findInt32("height", &height)) { 1323 meta->setInt32(kKeyWidth, width); 1324 meta->setInt32(kKeyHeight, height); 1325 } else { 1326 ALOGW("did not find width and/or height"); 1327 } 1328 1329 int32_t sarWidth, sarHeight; 1330 if (msg->findInt32("sar-width", &sarWidth) 1331 && msg->findInt32("sar-height", &sarHeight)) { 1332 meta->setInt32(kKeySARWidth, sarWidth); 1333 meta->setInt32(kKeySARHeight, sarHeight); 1334 } 1335 1336 int32_t displayWidth, displayHeight; 1337 if (msg->findInt32("display-width", &displayWidth) 1338 && msg->findInt32("display-height", &displayHeight)) { 1339 meta->setInt32(kKeyDisplayWidth, displayWidth); 1340 meta->setInt32(kKeyDisplayHeight, displayHeight); 1341 } 1342 1343 if (mime.startsWith("image/")){ 1344 int32_t isPrimary; 1345 if (msg->findInt32("is-default", &isPrimary) && isPrimary) { 1346 meta->setInt32(kKeyTrackIsDefault, 1); 1347 } 1348 int32_t tileWidth, tileHeight, gridRows, gridCols; 1349 if (msg->findInt32("tile-width", &tileWidth)) { 1350 meta->setInt32(kKeyTileWidth, tileWidth); 1351 } 1352 if (msg->findInt32("tile-height", &tileHeight)) { 1353 meta->setInt32(kKeyTileHeight, tileHeight); 1354 } 1355 if (msg->findInt32("grid-rows", &gridRows)) { 1356 meta->setInt32(kKeyGridRows, gridRows); 1357 } 1358 if (msg->findInt32("grid-cols", &gridCols)) { 1359 meta->setInt32(kKeyGridCols, gridCols); 1360 } 1361 } 1362 1363 int32_t colorFormat; 1364 if (msg->findInt32("color-format", &colorFormat)) { 1365 meta->setInt32(kKeyColorFormat, colorFormat); 1366 } 1367 1368 int32_t cropLeft, cropTop, cropRight, cropBottom; 1369 if (msg->findRect("crop", 1370 &cropLeft, 1371 &cropTop, 1372 &cropRight, 1373 &cropBottom)) { 1374 meta->setRect(kKeyCropRect, cropLeft, cropTop, cropRight, cropBottom); 1375 } 1376 1377 int32_t rotationDegrees; 1378 if (msg->findInt32("rotation-degrees", &rotationDegrees)) { 1379 meta->setInt32(kKeyRotation, rotationDegrees); 1380 } 1381 1382 if (msg->contains("hdr-static-info")) { 1383 HDRStaticInfo info; 1384 if (ColorUtils::getHDRStaticInfoFromFormat(msg, &info)) { 1385 meta->setData(kKeyHdrStaticInfo, 'hdrS', &info, sizeof(info)); 1386 } 1387 } 1388 1389 convertMessageToMetaDataColorAspects(msg, meta); 1390 1391 AString tsSchema; 1392 if (msg->findString("ts-schema", &tsSchema)) { 1393 unsigned int numLayers = 0; 1394 unsigned int numBLayers = 0; 1395 char dummy; 1396 int tags = sscanf(tsSchema.c_str(), "android.generic.%u%c%u%c", 1397 &numLayers, &dummy, &numBLayers, &dummy); 1398 if ((tags == 1 || (tags == 3 && dummy == '+')) 1399 && numLayers > 0 && numLayers < UINT32_MAX - numBLayers 1400 && numLayers + numBLayers <= INT32_MAX) { 1401 meta->setInt32(kKeyTemporalLayerCount, numLayers + numBLayers); 1402 } 1403 } 1404 } else if (mime.startsWith("audio/")) { 1405 int32_t numChannels; 1406 if (msg->findInt32("channel-count", &numChannels)) { 1407 meta->setInt32(kKeyChannelCount, numChannels); 1408 } 1409 int32_t sampleRate; 1410 if (msg->findInt32("sample-rate", &sampleRate)) { 1411 meta->setInt32(kKeySampleRate, sampleRate); 1412 } 1413 int32_t channelMask; 1414 if (msg->findInt32("channel-mask", &channelMask)) { 1415 meta->setInt32(kKeyChannelMask, channelMask); 1416 } 1417 int32_t delay = 0; 1418 if (msg->findInt32("encoder-delay", &delay)) { 1419 meta->setInt32(kKeyEncoderDelay, delay); 1420 } 1421 int32_t padding = 0; 1422 if (msg->findInt32("encoder-padding", &padding)) { 1423 meta->setInt32(kKeyEncoderPadding, padding); 1424 } 1425 1426 int32_t isADTS; 1427 if (msg->findInt32("is-adts", &isADTS)) { 1428 meta->setInt32(kKeyIsADTS, isADTS); 1429 } 1430 1431 int32_t pcmEncoding; 1432 if (msg->findInt32("pcm-encoding", &pcmEncoding)) { 1433 meta->setInt32(kKeyPcmEncoding, pcmEncoding); 1434 } 1435 } 1436 1437 int32_t maxInputSize; 1438 if (msg->findInt32("max-input-size", &maxInputSize)) { 1439 meta->setInt32(kKeyMaxInputSize, maxInputSize); 1440 } 1441 1442 int32_t maxWidth; 1443 if (msg->findInt32("max-width", &maxWidth)) { 1444 meta->setInt32(kKeyMaxWidth, maxWidth); 1445 } 1446 1447 int32_t maxHeight; 1448 if (msg->findInt32("max-height", &maxHeight)) { 1449 meta->setInt32(kKeyMaxHeight, maxHeight); 1450 } 1451 1452 int32_t fps; 1453 float fpsFloat; 1454 if (msg->findInt32("frame-rate", &fps) && fps > 0) { 1455 meta->setInt32(kKeyFrameRate, fps); 1456 } else if (msg->findFloat("frame-rate", &fpsFloat) 1457 && fpsFloat >= 1 && fpsFloat <= INT32_MAX) { 1458 // truncate values to distinguish between e.g. 24 vs 23.976 fps 1459 meta->setInt32(kKeyFrameRate, (int32_t)fpsFloat); 1460 } 1461 1462 // reassemble the csd data into its original form 1463 sp<ABuffer> csd0, csd1, csd2; 1464 if (msg->findBuffer("csd-0", &csd0)) { 1465 int csd0size = csd0->size(); 1466 if (mime == MEDIA_MIMETYPE_VIDEO_AVC) { 1467 sp<ABuffer> csd1; 1468 if (msg->findBuffer("csd-1", &csd1)) { 1469 std::vector<char> avcc(csd0size + csd1->size() + 1024); 1470 size_t outsize = reassembleAVCC(csd0, csd1, avcc.data()); 1471 meta->setData(kKeyAVCC, kKeyAVCC, avcc.data(), outsize); 1472 } 1473 } else if (mime == MEDIA_MIMETYPE_AUDIO_AAC || mime == MEDIA_MIMETYPE_VIDEO_MPEG4) { 1474 std::vector<char> esds(csd0size + 31); 1475 // The written ESDS is actually for an audio stream, but it's enough 1476 // for transporting the CSD to muxers. 1477 reassembleESDS(csd0, esds.data()); 1478 meta->setData(kKeyESDS, kKeyESDS, esds.data(), esds.size()); 1479 } else if (mime == MEDIA_MIMETYPE_VIDEO_HEVC || 1480 mime == MEDIA_MIMETYPE_IMAGE_ANDROID_HEIC) { 1481 std::vector<uint8_t> hvcc(csd0size + 1024); 1482 size_t outsize = reassembleHVCC(csd0, hvcc.data(), hvcc.size(), 4); 1483 meta->setData(kKeyHVCC, kKeyHVCC, hvcc.data(), outsize); 1484 } else if (mime == MEDIA_MIMETYPE_VIDEO_VP9) { 1485 meta->setData(kKeyVp9CodecPrivate, 0, csd0->data(), csd0->size()); 1486 } else if (mime == MEDIA_MIMETYPE_AUDIO_OPUS) { 1487 meta->setData(kKeyOpusHeader, 0, csd0->data(), csd0->size()); 1488 if (msg->findBuffer("csd-1", &csd1)) { 1489 meta->setData(kKeyOpusCodecDelay, 0, csd1->data(), csd1->size()); 1490 } 1491 if (msg->findBuffer("csd-2", &csd2)) { 1492 meta->setData(kKeyOpusSeekPreRoll, 0, csd2->data(), csd2->size()); 1493 } 1494 } else if (mime == MEDIA_MIMETYPE_AUDIO_VORBIS) { 1495 meta->setData(kKeyVorbisInfo, 0, csd0->data(), csd0->size()); 1496 if (msg->findBuffer("csd-1", &csd1)) { 1497 meta->setData(kKeyVorbisBooks, 0, csd1->data(), csd1->size()); 1498 } 1499 } 1500 } 1501 1502 int32_t timeScale; 1503 if (msg->findInt32("time-scale", &timeScale)) { 1504 meta->setInt32(kKeyTimeScale, timeScale); 1505 } 1506 1507 // XXX TODO add whatever other keys there are 1508 1509 #if 0 1510 ALOGI("converted %s to:", msg->debugString(0).c_str()); 1511 meta->dumpToLog(); 1512 #endif 1513 } 1514 1515 AString MakeUserAgent() { 1516 AString ua; 1517 ua.append("stagefright/1.2 (Linux;Android "); 1518 1519 #if (PROPERTY_VALUE_MAX < 8) 1520 #error "PROPERTY_VALUE_MAX must be at least 8" 1521 #endif 1522 1523 char value[PROPERTY_VALUE_MAX]; 1524 property_get("ro.build.version.release", value, "Unknown"); 1525 ua.append(value); 1526 ua.append(")"); 1527 1528 return ua; 1529 } 1530 1531 status_t sendMetaDataToHal(sp<MediaPlayerBase::AudioSink>& sink, 1532 const sp<MetaData>& meta) 1533 { 1534 int32_t sampleRate = 0; 1535 int32_t bitRate = 0; 1536 int32_t channelMask = 0; 1537 int32_t delaySamples = 0; 1538 int32_t paddingSamples = 0; 1539 1540 AudioParameter param = AudioParameter(); 1541 1542 if (meta->findInt32(kKeySampleRate, &sampleRate)) { 1543 param.addInt(String8(AUDIO_OFFLOAD_CODEC_SAMPLE_RATE), sampleRate); 1544 } 1545 if (meta->findInt32(kKeyChannelMask, &channelMask)) { 1546 param.addInt(String8(AUDIO_OFFLOAD_CODEC_NUM_CHANNEL), channelMask); 1547 } 1548 if (meta->findInt32(kKeyBitRate, &bitRate)) { 1549 param.addInt(String8(AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE), bitRate); 1550 } 1551 if (meta->findInt32(kKeyEncoderDelay, &delaySamples)) { 1552 param.addInt(String8(AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES), delaySamples); 1553 } 1554 if (meta->findInt32(kKeyEncoderPadding, &paddingSamples)) { 1555 param.addInt(String8(AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES), paddingSamples); 1556 } 1557 1558 ALOGV("sendMetaDataToHal: bitRate %d, sampleRate %d, chanMask %d," 1559 "delaySample %d, paddingSample %d", bitRate, sampleRate, 1560 channelMask, delaySamples, paddingSamples); 1561 1562 sink->setParameters(param.toString()); 1563 return OK; 1564 } 1565 1566 struct mime_conv_t { 1567 const char* mime; 1568 audio_format_t format; 1569 }; 1570 1571 static const struct mime_conv_t mimeLookup[] = { 1572 { MEDIA_MIMETYPE_AUDIO_MPEG, AUDIO_FORMAT_MP3 }, 1573 { MEDIA_MIMETYPE_AUDIO_RAW, AUDIO_FORMAT_PCM_16_BIT }, 1574 { MEDIA_MIMETYPE_AUDIO_AMR_NB, AUDIO_FORMAT_AMR_NB }, 1575 { MEDIA_MIMETYPE_AUDIO_AMR_WB, AUDIO_FORMAT_AMR_WB }, 1576 { MEDIA_MIMETYPE_AUDIO_AAC, AUDIO_FORMAT_AAC }, 1577 { MEDIA_MIMETYPE_AUDIO_VORBIS, AUDIO_FORMAT_VORBIS }, 1578 { MEDIA_MIMETYPE_AUDIO_OPUS, AUDIO_FORMAT_OPUS}, 1579 { MEDIA_MIMETYPE_AUDIO_AC3, AUDIO_FORMAT_AC3}, 1580 { MEDIA_MIMETYPE_AUDIO_FLAC, AUDIO_FORMAT_FLAC}, 1581 { 0, AUDIO_FORMAT_INVALID } 1582 }; 1583 1584 status_t mapMimeToAudioFormat( audio_format_t& format, const char* mime ) 1585 { 1586 const struct mime_conv_t* p = &mimeLookup[0]; 1587 while (p->mime != NULL) { 1588 if (0 == strcasecmp(mime, p->mime)) { 1589 format = p->format; 1590 return OK; 1591 } 1592 ++p; 1593 } 1594 1595 return BAD_VALUE; 1596 } 1597 1598 struct aac_format_conv_t { 1599 OMX_AUDIO_AACPROFILETYPE eAacProfileType; 1600 audio_format_t format; 1601 }; 1602 1603 static const struct aac_format_conv_t profileLookup[] = { 1604 { OMX_AUDIO_AACObjectMain, AUDIO_FORMAT_AAC_MAIN}, 1605 { OMX_AUDIO_AACObjectLC, AUDIO_FORMAT_AAC_LC}, 1606 { OMX_AUDIO_AACObjectSSR, AUDIO_FORMAT_AAC_SSR}, 1607 { OMX_AUDIO_AACObjectLTP, AUDIO_FORMAT_AAC_LTP}, 1608 { OMX_AUDIO_AACObjectHE, AUDIO_FORMAT_AAC_HE_V1}, 1609 { OMX_AUDIO_AACObjectScalable, AUDIO_FORMAT_AAC_SCALABLE}, 1610 { OMX_AUDIO_AACObjectERLC, AUDIO_FORMAT_AAC_ERLC}, 1611 { OMX_AUDIO_AACObjectLD, AUDIO_FORMAT_AAC_LD}, 1612 { OMX_AUDIO_AACObjectHE_PS, AUDIO_FORMAT_AAC_HE_V2}, 1613 { OMX_AUDIO_AACObjectELD, AUDIO_FORMAT_AAC_ELD}, 1614 { OMX_AUDIO_AACObjectXHE, AUDIO_FORMAT_AAC_XHE}, 1615 { OMX_AUDIO_AACObjectNull, AUDIO_FORMAT_AAC}, 1616 }; 1617 1618 void mapAACProfileToAudioFormat( audio_format_t& format, uint64_t eAacProfile) 1619 { 1620 const struct aac_format_conv_t* p = &profileLookup[0]; 1621 while (p->eAacProfileType != OMX_AUDIO_AACObjectNull) { 1622 if (eAacProfile == p->eAacProfileType) { 1623 format = p->format; 1624 return; 1625 } 1626 ++p; 1627 } 1628 format = AUDIO_FORMAT_AAC; 1629 return; 1630 } 1631 1632 bool canOffloadStream(const sp<MetaData>& meta, bool hasVideo, 1633 bool isStreaming, audio_stream_type_t streamType) 1634 { 1635 const char *mime; 1636 if (meta == NULL) { 1637 return false; 1638 } 1639 CHECK(meta->findCString(kKeyMIMEType, &mime)); 1640 1641 audio_offload_info_t info = AUDIO_INFO_INITIALIZER; 1642 1643 info.format = AUDIO_FORMAT_INVALID; 1644 if (mapMimeToAudioFormat(info.format, mime) != OK) { 1645 ALOGE(" Couldn't map mime type \"%s\" to a valid AudioSystem::audio_format !", mime); 1646 return false; 1647 } else { 1648 ALOGV("Mime type \"%s\" mapped to audio_format %d", mime, info.format); 1649 } 1650 1651 if (AUDIO_FORMAT_INVALID == info.format) { 1652 // can't offload if we don't know what the source format is 1653 ALOGE("mime type \"%s\" not a known audio format", mime); 1654 return false; 1655 } 1656 1657 // Redefine aac format according to its profile 1658 // Offloading depends on audio DSP capabilities. 1659 int32_t aacaot = -1; 1660 if (meta->findInt32(kKeyAACAOT, &aacaot)) { 1661 mapAACProfileToAudioFormat(info.format,(OMX_AUDIO_AACPROFILETYPE) aacaot); 1662 } 1663 1664 int32_t srate = -1; 1665 if (!meta->findInt32(kKeySampleRate, &srate)) { 1666 ALOGV("track of type '%s' does not publish sample rate", mime); 1667 } 1668 info.sample_rate = srate; 1669 1670 int32_t cmask = 0; 1671 if (!meta->findInt32(kKeyChannelMask, &cmask)) { 1672 ALOGV("track of type '%s' does not publish channel mask", mime); 1673 1674 // Try a channel count instead 1675 int32_t channelCount; 1676 if (!meta->findInt32(kKeyChannelCount, &channelCount)) { 1677 ALOGV("track of type '%s' does not publish channel count", mime); 1678 } else { 1679 cmask = audio_channel_out_mask_from_count(channelCount); 1680 } 1681 } 1682 info.channel_mask = cmask; 1683 1684 int64_t duration = 0; 1685 if (!meta->findInt64(kKeyDuration, &duration)) { 1686 ALOGV("track of type '%s' does not publish duration", mime); 1687 } 1688 info.duration_us = duration; 1689 1690 int32_t brate = -1; 1691 if (!meta->findInt32(kKeyBitRate, &brate)) { 1692 ALOGV("track of type '%s' does not publish bitrate", mime); 1693 } 1694 info.bit_rate = brate; 1695 1696 1697 info.stream_type = streamType; 1698 info.has_video = hasVideo; 1699 info.is_streaming = isStreaming; 1700 1701 // Check if offload is possible for given format, stream type, sample rate, 1702 // bit rate, duration, video and streaming 1703 return AudioSystem::isOffloadSupported(info); 1704 } 1705 1706 AString uriDebugString(const AString &uri, bool incognito) { 1707 if (incognito) { 1708 return AString("<URI suppressed>"); 1709 } 1710 1711 if (property_get_bool("media.stagefright.log-uri", false)) { 1712 return uri; 1713 } 1714 1715 // find scheme 1716 AString scheme; 1717 const char *chars = uri.c_str(); 1718 for (size_t i = 0; i < uri.size(); i++) { 1719 const char c = chars[i]; 1720 if (!isascii(c)) { 1721 break; 1722 } else if (isalpha(c)) { 1723 continue; 1724 } else if (i == 0) { 1725 // first character must be a letter 1726 break; 1727 } else if (isdigit(c) || c == '+' || c == '.' || c =='-') { 1728 continue; 1729 } else if (c != ':') { 1730 break; 1731 } 1732 scheme = AString(uri, 0, i); 1733 scheme.append("://<suppressed>"); 1734 return scheme; 1735 } 1736 return AString("<no-scheme URI suppressed>"); 1737 } 1738 1739 HLSTime::HLSTime(const sp<AMessage>& meta) : 1740 mSeq(-1), 1741 mTimeUs(-1ll), 1742 mMeta(meta) { 1743 if (meta != NULL) { 1744 CHECK(meta->findInt32("discontinuitySeq", &mSeq)); 1745 CHECK(meta->findInt64("timeUs", &mTimeUs)); 1746 } 1747 } 1748 1749 int64_t HLSTime::getSegmentTimeUs() const { 1750 int64_t segmentStartTimeUs = -1ll; 1751 if (mMeta != NULL) { 1752 CHECK(mMeta->findInt64("segmentStartTimeUs", &segmentStartTimeUs)); 1753 1754 int64_t segmentFirstTimeUs; 1755 if (mMeta->findInt64("segmentFirstTimeUs", &segmentFirstTimeUs)) { 1756 segmentStartTimeUs += mTimeUs - segmentFirstTimeUs; 1757 } 1758 1759 // adjust segment time by playlist age (for live streaming) 1760 int64_t playlistTimeUs; 1761 if (mMeta->findInt64("playlistTimeUs", &playlistTimeUs)) { 1762 int64_t playlistAgeUs = ALooper::GetNowUs() - playlistTimeUs; 1763 1764 int64_t durationUs; 1765 CHECK(mMeta->findInt64("segmentDurationUs", &durationUs)); 1766 1767 // round to nearest whole segment 1768 playlistAgeUs = (playlistAgeUs + durationUs / 2) 1769 / durationUs * durationUs; 1770 1771 segmentStartTimeUs -= playlistAgeUs; 1772 if (segmentStartTimeUs < 0) { 1773 segmentStartTimeUs = 0; 1774 } 1775 } 1776 } 1777 return segmentStartTimeUs; 1778 } 1779 1780 bool operator <(const HLSTime &t0, const HLSTime &t1) { 1781 // we can only compare discontinuity sequence and timestamp. 1782 // (mSegmentTimeUs is not reliable in live streaming case, it's the 1783 // time starting from beginning of playlist but playlist could change.) 1784 return t0.mSeq < t1.mSeq 1785 || (t0.mSeq == t1.mSeq && t0.mTimeUs < t1.mTimeUs); 1786 } 1787 1788 void writeToAMessage(const sp<AMessage> &msg, const AudioPlaybackRate &rate) { 1789 msg->setFloat("speed", rate.mSpeed); 1790 msg->setFloat("pitch", rate.mPitch); 1791 msg->setInt32("audio-fallback-mode", rate.mFallbackMode); 1792 msg->setInt32("audio-stretch-mode", rate.mStretchMode); 1793 } 1794 1795 void readFromAMessage(const sp<AMessage> &msg, AudioPlaybackRate *rate /* nonnull */) { 1796 *rate = AUDIO_PLAYBACK_RATE_DEFAULT; 1797 CHECK(msg->findFloat("speed", &rate->mSpeed)); 1798 CHECK(msg->findFloat("pitch", &rate->mPitch)); 1799 CHECK(msg->findInt32("audio-fallback-mode", (int32_t *)&rate->mFallbackMode)); 1800 CHECK(msg->findInt32("audio-stretch-mode", (int32_t *)&rate->mStretchMode)); 1801 } 1802 1803 void writeToAMessage(const sp<AMessage> &msg, const AVSyncSettings &sync, float videoFpsHint) { 1804 msg->setInt32("sync-source", sync.mSource); 1805 msg->setInt32("audio-adjust-mode", sync.mAudioAdjustMode); 1806 msg->setFloat("tolerance", sync.mTolerance); 1807 msg->setFloat("video-fps", videoFpsHint); 1808 } 1809 1810 void readFromAMessage( 1811 const sp<AMessage> &msg, 1812 AVSyncSettings *sync /* nonnull */, 1813 float *videoFps /* nonnull */) { 1814 AVSyncSettings settings; 1815 CHECK(msg->findInt32("sync-source", (int32_t *)&settings.mSource)); 1816 CHECK(msg->findInt32("audio-adjust-mode", (int32_t *)&settings.mAudioAdjustMode)); 1817 CHECK(msg->findFloat("tolerance", &settings.mTolerance)); 1818 CHECK(msg->findFloat("video-fps", videoFps)); 1819 *sync = settings; 1820 } 1821 1822 void writeToAMessage(const sp<AMessage> &msg, const BufferingSettings &buffering) { 1823 msg->setInt32("init-ms", buffering.mInitialMarkMs); 1824 msg->setInt32("resume-playback-ms", buffering.mResumePlaybackMarkMs); 1825 } 1826 1827 void readFromAMessage(const sp<AMessage> &msg, BufferingSettings *buffering /* nonnull */) { 1828 int32_t value; 1829 if (msg->findInt32("init-ms", &value)) { 1830 buffering->mInitialMarkMs = value; 1831 } 1832 if (msg->findInt32("resume-playback-ms", &value)) { 1833 buffering->mResumePlaybackMarkMs = value; 1834 } 1835 } 1836 1837 AString nameForFd(int fd) { 1838 const size_t SIZE = 256; 1839 char buffer[SIZE]; 1840 AString result; 1841 snprintf(buffer, SIZE, "/proc/%d/fd/%d", getpid(), fd); 1842 struct stat s; 1843 if (lstat(buffer, &s) == 0) { 1844 if ((s.st_mode & S_IFMT) == S_IFLNK) { 1845 char linkto[256]; 1846 int len = readlink(buffer, linkto, sizeof(linkto)); 1847 if(len > 0) { 1848 if(len > 255) { 1849 linkto[252] = '.'; 1850 linkto[253] = '.'; 1851 linkto[254] = '.'; 1852 linkto[255] = 0; 1853 } else { 1854 linkto[len] = 0; 1855 } 1856 result.append(linkto); 1857 } 1858 } else { 1859 result.append("unexpected type for "); 1860 result.append(buffer); 1861 } 1862 } else { 1863 result.append("couldn't open "); 1864 result.append(buffer); 1865 } 1866 return result; 1867 } 1868 1869 } // namespace android 1870 1871