APacketSource.cpp revision cce326fe43411855aca2f719e505b051bc4b61b3
1/* 2 * Copyright (C) 2010 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#include "APacketSource.h" 18 19#include "ASessionDescription.h" 20 21#include "avc_utils.h" 22 23#include <ctype.h> 24 25#include <media/stagefright/foundation/ABitReader.h> 26#include <media/stagefright/foundation/ABuffer.h> 27#include <media/stagefright/foundation/ADebug.h> 28#include <media/stagefright/foundation/AMessage.h> 29#include <media/stagefright/foundation/AString.h> 30#include <media/stagefright/foundation/base64.h> 31#include <media/stagefright/foundation/hexdump.h> 32#include <media/stagefright/MediaBuffer.h> 33#include <media/stagefright/MediaDefs.h> 34#include <media/stagefright/MetaData.h> 35#include <utils/Vector.h> 36 37namespace android { 38 39static bool GetAttribute(const char *s, const char *key, AString *value) { 40 value->clear(); 41 42 size_t keyLen = strlen(key); 43 44 for (;;) { 45 while (isspace(*s)) { 46 ++s; 47 } 48 49 const char *colonPos = strchr(s, ';'); 50 51 size_t len = 52 (colonPos == NULL) ? strlen(s) : colonPos - s; 53 54 if (len >= keyLen + 1 && s[keyLen] == '=' && !strncmp(s, key, keyLen)) { 55 value->setTo(&s[keyLen + 1], len - keyLen - 1); 56 return true; 57 } 58 59 if (colonPos == NULL) { 60 return false; 61 } 62 63 s = colonPos + 1; 64 } 65} 66 67static sp<ABuffer> decodeHex(const AString &s) { 68 if ((s.size() % 2) != 0) { 69 return NULL; 70 } 71 72 size_t outLen = s.size() / 2; 73 sp<ABuffer> buffer = new ABuffer(outLen); 74 uint8_t *out = buffer->data(); 75 76 uint8_t accum = 0; 77 for (size_t i = 0; i < s.size(); ++i) { 78 char c = s.c_str()[i]; 79 unsigned value; 80 if (c >= '0' && c <= '9') { 81 value = c - '0'; 82 } else if (c >= 'a' && c <= 'f') { 83 value = c - 'a' + 10; 84 } else if (c >= 'A' && c <= 'F') { 85 value = c - 'A' + 10; 86 } else { 87 return NULL; 88 } 89 90 accum = (accum << 4) | value; 91 92 if (i & 1) { 93 *out++ = accum; 94 95 accum = 0; 96 } 97 } 98 99 return buffer; 100} 101 102static sp<ABuffer> MakeAVCCodecSpecificData( 103 const char *params, int32_t *width, int32_t *height) { 104 *width = 0; 105 *height = 0; 106 107 AString val; 108 if (!GetAttribute(params, "profile-level-id", &val)) { 109 return NULL; 110 } 111 112 sp<ABuffer> profileLevelID = decodeHex(val); 113 CHECK(profileLevelID != NULL); 114 CHECK_EQ(profileLevelID->size(), 3u); 115 116 Vector<sp<ABuffer> > paramSets; 117 118 size_t numSeqParameterSets = 0; 119 size_t totalSeqParameterSetSize = 0; 120 size_t numPicParameterSets = 0; 121 size_t totalPicParameterSetSize = 0; 122 123 if (!GetAttribute(params, "sprop-parameter-sets", &val)) { 124 return NULL; 125 } 126 127 size_t start = 0; 128 for (;;) { 129 ssize_t commaPos = val.find(",", start); 130 size_t end = (commaPos < 0) ? val.size() : commaPos; 131 132 AString nalString(val, start, end - start); 133 sp<ABuffer> nal = decodeBase64(nalString); 134 CHECK(nal != NULL); 135 CHECK_GT(nal->size(), 0u); 136 CHECK_LE(nal->size(), 65535u); 137 138 uint8_t nalType = nal->data()[0] & 0x1f; 139 if (numSeqParameterSets == 0) { 140 CHECK_EQ((unsigned)nalType, 7u); 141 } else if (numPicParameterSets > 0) { 142 CHECK_EQ((unsigned)nalType, 8u); 143 } 144 if (nalType == 7) { 145 ++numSeqParameterSets; 146 totalSeqParameterSetSize += nal->size(); 147 } else { 148 CHECK_EQ((unsigned)nalType, 8u); 149 ++numPicParameterSets; 150 totalPicParameterSetSize += nal->size(); 151 } 152 153 paramSets.push(nal); 154 155 if (commaPos < 0) { 156 break; 157 } 158 159 start = commaPos + 1; 160 } 161 162 CHECK_LT(numSeqParameterSets, 32u); 163 CHECK_LE(numPicParameterSets, 255u); 164 165 size_t csdSize = 166 1 + 3 + 1 + 1 167 + 2 * numSeqParameterSets + totalSeqParameterSetSize 168 + 1 + 2 * numPicParameterSets + totalPicParameterSetSize; 169 170 sp<ABuffer> csd = new ABuffer(csdSize); 171 uint8_t *out = csd->data(); 172 173 *out++ = 0x01; // configurationVersion 174 memcpy(out, profileLevelID->data(), 3); 175 out += 3; 176 *out++ = (0x3f << 2) | 1; // lengthSize == 2 bytes 177 *out++ = 0xe0 | numSeqParameterSets; 178 179 for (size_t i = 0; i < numSeqParameterSets; ++i) { 180 sp<ABuffer> nal = paramSets.editItemAt(i); 181 182 *out++ = nal->size() >> 8; 183 *out++ = nal->size() & 0xff; 184 185 memcpy(out, nal->data(), nal->size()); 186 187 out += nal->size(); 188 189 if (i == 0) { 190 FindAVCDimensions(nal, width, height); 191 LOG(INFO) << "dimensions " << *width << "x" << *height; 192 } 193 } 194 195 *out++ = numPicParameterSets; 196 197 for (size_t i = 0; i < numPicParameterSets; ++i) { 198 sp<ABuffer> nal = paramSets.editItemAt(i + numSeqParameterSets); 199 200 *out++ = nal->size() >> 8; 201 *out++ = nal->size() & 0xff; 202 203 memcpy(out, nal->data(), nal->size()); 204 205 out += nal->size(); 206 } 207 208 // hexdump(csd->data(), csd->size()); 209 210 return csd; 211} 212 213sp<ABuffer> MakeAACCodecSpecificData(const char *params) { 214 AString val; 215 CHECK(GetAttribute(params, "config", &val)); 216 217 sp<ABuffer> config = decodeHex(val); 218 CHECK(config != NULL); 219 CHECK_GE(config->size(), 4u); 220 221 const uint8_t *data = config->data(); 222 uint32_t x = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; 223 x = (x >> 1) & 0xffff; 224 225 static const uint8_t kStaticESDS[] = { 226 0x03, 22, 227 0x00, 0x00, // ES_ID 228 0x00, // streamDependenceFlag, URL_Flag, OCRstreamFlag 229 230 0x04, 17, 231 0x40, // Audio ISO/IEC 14496-3 232 0x00, 0x00, 0x00, 0x00, 233 0x00, 0x00, 0x00, 0x00, 234 0x00, 0x00, 0x00, 0x00, 235 236 0x05, 2, 237 // AudioSpecificInfo follows 238 }; 239 240 sp<ABuffer> csd = new ABuffer(sizeof(kStaticESDS) + 2); 241 memcpy(csd->data(), kStaticESDS, sizeof(kStaticESDS)); 242 csd->data()[sizeof(kStaticESDS)] = (x >> 8) & 0xff; 243 csd->data()[sizeof(kStaticESDS) + 1] = x & 0xff; 244 245 // hexdump(csd->data(), csd->size()); 246 247 return csd; 248} 249 250static size_t GetSizeWidth(size_t x) { 251 size_t n = 1; 252 while (x > 127) { 253 ++n; 254 x >>= 7; 255 } 256 return n; 257} 258 259static uint8_t *EncodeSize(uint8_t *dst, size_t x) { 260 while (x > 127) { 261 *dst++ = (x & 0x7f) | 0x80; 262 x >>= 7; 263 } 264 *dst++ = x; 265 return dst; 266} 267 268static bool ExtractDimensionsFromVOLHeader( 269 const sp<ABuffer> &config, int32_t *width, int32_t *height) { 270 *width = 0; 271 *height = 0; 272 273 const uint8_t *ptr = config->data(); 274 size_t offset = 0; 275 bool foundVOL = false; 276 while (offset + 3 < config->size()) { 277 if (memcmp("\x00\x00\x01", &ptr[offset], 3) 278 || (ptr[offset + 3] & 0xf0) != 0x20) { 279 ++offset; 280 continue; 281 } 282 283 foundVOL = true; 284 break; 285 } 286 287 if (!foundVOL) { 288 return false; 289 } 290 291 ABitReader br(&ptr[offset + 4], config->size() - offset - 4); 292 br.skipBits(1); // random_accessible_vol 293 unsigned video_object_type_indication = br.getBits(8); 294 295 CHECK_NE(video_object_type_indication, 296 0x21u /* Fine Granularity Scalable */); 297 298 unsigned video_object_layer_verid; 299 unsigned video_object_layer_priority; 300 if (br.getBits(1)) { 301 video_object_layer_verid = br.getBits(4); 302 video_object_layer_priority = br.getBits(3); 303 } 304 unsigned aspect_ratio_info = br.getBits(4); 305 if (aspect_ratio_info == 0x0f /* extended PAR */) { 306 br.skipBits(8); // par_width 307 br.skipBits(8); // par_height 308 } 309 if (br.getBits(1)) { // vol_control_parameters 310 br.skipBits(2); // chroma_format 311 br.skipBits(1); // low_delay 312 if (br.getBits(1)) { // vbv_parameters 313 TRESPASS(); 314 } 315 } 316 unsigned video_object_layer_shape = br.getBits(2); 317 CHECK_EQ(video_object_layer_shape, 0x00u /* rectangular */); 318 319 CHECK(br.getBits(1)); // marker_bit 320 unsigned vop_time_increment_resolution = br.getBits(16); 321 CHECK(br.getBits(1)); // marker_bit 322 323 if (br.getBits(1)) { // fixed_vop_rate 324 // range [0..vop_time_increment_resolution) 325 326 // vop_time_increment_resolution 327 // 2 => 0..1, 1 bit 328 // 3 => 0..2, 2 bits 329 // 4 => 0..3, 2 bits 330 // 5 => 0..4, 3 bits 331 // ... 332 333 CHECK_GT(vop_time_increment_resolution, 0u); 334 --vop_time_increment_resolution; 335 336 unsigned numBits = 0; 337 while (vop_time_increment_resolution > 0) { 338 ++numBits; 339 vop_time_increment_resolution >>= 1; 340 } 341 342 br.skipBits(numBits); // fixed_vop_time_increment 343 } 344 345 CHECK(br.getBits(1)); // marker_bit 346 unsigned video_object_layer_width = br.getBits(13); 347 CHECK(br.getBits(1)); // marker_bit 348 unsigned video_object_layer_height = br.getBits(13); 349 CHECK(br.getBits(1)); // marker_bit 350 351 unsigned interlaced = br.getBits(1); 352 353 *width = video_object_layer_width; 354 *height = video_object_layer_height; 355 356 LOG(INFO) << "VOL dimensions = " << *width << "x" << *height; 357 358 return true; 359} 360 361sp<ABuffer> MakeMPEG4VideoCodecSpecificData( 362 const char *params, int32_t *width, int32_t *height) { 363 *width = 0; 364 *height = 0; 365 366 AString val; 367 CHECK(GetAttribute(params, "config", &val)); 368 369 sp<ABuffer> config = decodeHex(val); 370 CHECK(config != NULL); 371 372 if (!ExtractDimensionsFromVOLHeader(config, width, height)) { 373 return NULL; 374 } 375 376 size_t len1 = config->size() + GetSizeWidth(config->size()) + 1; 377 size_t len2 = len1 + GetSizeWidth(len1) + 1 + 13; 378 size_t len3 = len2 + GetSizeWidth(len2) + 1 + 3; 379 380 sp<ABuffer> csd = new ABuffer(len3); 381 uint8_t *dst = csd->data(); 382 *dst++ = 0x03; 383 dst = EncodeSize(dst, len2 + 3); 384 *dst++ = 0x00; // ES_ID 385 *dst++ = 0x00; 386 *dst++ = 0x00; // streamDependenceFlag, URL_Flag, OCRstreamFlag 387 388 *dst++ = 0x04; 389 dst = EncodeSize(dst, len1 + 13); 390 *dst++ = 0x01; // Video ISO/IEC 14496-2 Simple Profile 391 for (size_t i = 0; i < 12; ++i) { 392 *dst++ = 0x00; 393 } 394 395 *dst++ = 0x05; 396 dst = EncodeSize(dst, config->size()); 397 memcpy(dst, config->data(), config->size()); 398 dst += config->size(); 399 400 // hexdump(csd->data(), csd->size()); 401 402 return csd; 403} 404 405APacketSource::APacketSource( 406 const sp<ASessionDescription> &sessionDesc, size_t index) 407 : mInitCheck(NO_INIT), 408 mFormat(new MetaData), 409 mEOSResult(OK) { 410 unsigned long PT; 411 AString desc; 412 AString params; 413 sessionDesc->getFormatType(index, &PT, &desc, ¶ms); 414 415 int64_t durationUs; 416 if (sessionDesc->getDurationUs(&durationUs)) { 417 mFormat->setInt64(kKeyDuration, durationUs); 418 } else { 419 mFormat->setInt64(kKeyDuration, 60 * 60 * 1000000ll); 420 } 421 422 mInitCheck = OK; 423 if (!strncmp(desc.c_str(), "H264/", 5)) { 424 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); 425 426 int32_t width, height; 427 if (!sessionDesc->getDimensions(index, PT, &width, &height)) { 428 width = -1; 429 height = -1; 430 } 431 432 int32_t encWidth, encHeight; 433 sp<ABuffer> codecSpecificData = 434 MakeAVCCodecSpecificData(params.c_str(), &encWidth, &encHeight); 435 436 if (codecSpecificData != NULL) { 437 if (width < 0) { 438 // If no explicit width/height given in the sdp, use the dimensions 439 // extracted from the first sequence parameter set. 440 width = encWidth; 441 height = encHeight; 442 } 443 444 mFormat->setData( 445 kKeyAVCC, 0, 446 codecSpecificData->data(), codecSpecificData->size()); 447 } else if (width < 0) { 448 mInitCheck = ERROR_UNSUPPORTED; 449 return; 450 } 451 452 mFormat->setInt32(kKeyWidth, width); 453 mFormat->setInt32(kKeyHeight, height); 454 } else if (!strncmp(desc.c_str(), "H263-2000/", 10) 455 || !strncmp(desc.c_str(), "H263-1998/", 10)) { 456 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263); 457 458 int32_t width, height; 459 if (!sessionDesc->getDimensions(index, PT, &width, &height)) { 460 mInitCheck = ERROR_UNSUPPORTED; 461 return; 462 } 463 464 mFormat->setInt32(kKeyWidth, width); 465 mFormat->setInt32(kKeyHeight, height); 466 } else if (!strncmp(desc.c_str(), "MP4A-LATM/", 10)) { 467 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC); 468 469 int32_t sampleRate, numChannels; 470 ASessionDescription::ParseFormatDesc( 471 desc.c_str(), &sampleRate, &numChannels); 472 473 mFormat->setInt32(kKeySampleRate, sampleRate); 474 mFormat->setInt32(kKeyChannelCount, numChannels); 475 476 sp<ABuffer> codecSpecificData = 477 MakeAACCodecSpecificData(params.c_str()); 478 479 mFormat->setData( 480 kKeyESDS, 0, 481 codecSpecificData->data(), codecSpecificData->size()); 482 } else if (!strncmp(desc.c_str(), "AMR/", 4)) { 483 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB); 484 485 int32_t sampleRate, numChannels; 486 ASessionDescription::ParseFormatDesc( 487 desc.c_str(), &sampleRate, &numChannels); 488 489 mFormat->setInt32(kKeySampleRate, sampleRate); 490 mFormat->setInt32(kKeyChannelCount, numChannels); 491 492 if (sampleRate != 8000 || numChannels != 1) { 493 mInitCheck = ERROR_UNSUPPORTED; 494 } 495 } else if (!strncmp(desc.c_str(), "AMR-WB/", 7)) { 496 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB); 497 498 int32_t sampleRate, numChannels; 499 ASessionDescription::ParseFormatDesc( 500 desc.c_str(), &sampleRate, &numChannels); 501 502 mFormat->setInt32(kKeySampleRate, sampleRate); 503 mFormat->setInt32(kKeyChannelCount, numChannels); 504 505 if (sampleRate != 16000 || numChannels != 1) { 506 mInitCheck = ERROR_UNSUPPORTED; 507 } 508 } else if (!strncmp(desc.c_str(), "MP4V-ES/", 8)) { 509 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4); 510 511 int32_t width, height; 512 if (!sessionDesc->getDimensions(index, PT, &width, &height)) { 513 width = -1; 514 height = -1; 515 } 516 517 int32_t encWidth, encHeight; 518 sp<ABuffer> codecSpecificData = 519 MakeMPEG4VideoCodecSpecificData( 520 params.c_str(), &encWidth, &encHeight); 521 522 if (codecSpecificData != NULL) { 523 mFormat->setData( 524 kKeyESDS, 0, 525 codecSpecificData->data(), codecSpecificData->size()); 526 527 if (width < 0) { 528 width = encWidth; 529 height = encHeight; 530 } 531 } else if (width < 0) { 532 mInitCheck = ERROR_UNSUPPORTED; 533 return; 534 } 535 536 mFormat->setInt32(kKeyWidth, width); 537 mFormat->setInt32(kKeyHeight, height); 538 } else { 539 mInitCheck = ERROR_UNSUPPORTED; 540 } 541} 542 543APacketSource::~APacketSource() { 544} 545 546status_t APacketSource::initCheck() const { 547 return mInitCheck; 548} 549 550status_t APacketSource::start(MetaData *params) { 551 return OK; 552} 553 554status_t APacketSource::stop() { 555 return OK; 556} 557 558sp<MetaData> APacketSource::getFormat() { 559 return mFormat; 560} 561 562status_t APacketSource::read( 563 MediaBuffer **out, const ReadOptions *) { 564 *out = NULL; 565 566 Mutex::Autolock autoLock(mLock); 567 while (mEOSResult == OK && mBuffers.empty()) { 568 mCondition.wait(mLock); 569 } 570 571 if (!mBuffers.empty()) { 572 const sp<ABuffer> buffer = *mBuffers.begin(); 573 574 MediaBuffer *mediaBuffer = new MediaBuffer(buffer->size()); 575 576 int64_t timeUs; 577 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 578 579 mediaBuffer->meta_data()->setInt64(kKeyTime, timeUs); 580 581 memcpy(mediaBuffer->data(), buffer->data(), buffer->size()); 582 *out = mediaBuffer; 583 584 mBuffers.erase(mBuffers.begin()); 585 return OK; 586 } 587 588 return mEOSResult; 589} 590 591void APacketSource::queueAccessUnit(const sp<ABuffer> &buffer) { 592 int32_t damaged; 593 if (buffer->meta()->findInt32("damaged", &damaged) && damaged) { 594 LOG(VERBOSE) << "discarding damaged AU"; 595 return; 596 } 597 598 Mutex::Autolock autoLock(mLock); 599 mBuffers.push_back(buffer); 600 mCondition.signal(); 601} 602 603void APacketSource::signalEOS(status_t result) { 604 CHECK(result != OK); 605 606 Mutex::Autolock autoLock(mLock); 607 mEOSResult = result; 608 mCondition.signal(); 609} 610 611void APacketSource::flushQueue() { 612 Mutex::Autolock autoLock(mLock); 613 mBuffers.clear(); 614} 615 616} // namespace android 617