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 "AVCDecoder" 19 #include <utils/Log.h> 20 21 #include "AVCDecoder.h" 22 23 #include "avcdec_api.h" 24 #include "avcdec_int.h" 25 26 #include <OMX_Component.h> 27 28 #include <media/stagefright/MediaBufferGroup.h> 29 #include <media/stagefright/MediaDebug.h> 30 #include <media/stagefright/MediaDefs.h> 31 #include <media/stagefright/MediaErrors.h> 32 #include <media/stagefright/MetaData.h> 33 #include <media/stagefright/Utils.h> 34 #include <media/stagefright/foundation/hexdump.h> 35 36 namespace android { 37 38 static const char kStartCode[4] = { 0x00, 0x00, 0x00, 0x01 }; 39 40 static int32_t Malloc(void *userData, int32_t size, int32_t attrs) { 41 return reinterpret_cast<int32_t>(malloc(size)); 42 } 43 44 static void Free(void *userData, int32_t ptr) { 45 free(reinterpret_cast<void *>(ptr)); 46 } 47 48 AVCDecoder::AVCDecoder(const sp<MediaSource> &source) 49 : mSource(source), 50 mStarted(false), 51 mHandle(new tagAVCHandle), 52 mInputBuffer(NULL), 53 mAnchorTimeUs(0), 54 mNumSamplesOutput(0), 55 mPendingSeekTimeUs(-1), 56 mPendingSeekMode(MediaSource::ReadOptions::SEEK_CLOSEST_SYNC), 57 mTargetTimeUs(-1), 58 mSPSSeen(false), 59 mPPSSeen(false) { 60 memset(mHandle, 0, sizeof(tagAVCHandle)); 61 mHandle->AVCObject = NULL; 62 mHandle->userData = this; 63 mHandle->CBAVC_DPBAlloc = ActivateSPSWrapper; 64 mHandle->CBAVC_FrameBind = BindFrameWrapper; 65 mHandle->CBAVC_FrameUnbind = UnbindFrame; 66 mHandle->CBAVC_Malloc = Malloc; 67 mHandle->CBAVC_Free = Free; 68 69 mFormat = new MetaData; 70 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); 71 int32_t width, height; 72 CHECK(mSource->getFormat()->findInt32(kKeyWidth, &width)); 73 CHECK(mSource->getFormat()->findInt32(kKeyHeight, &height)); 74 mFormat->setInt32(kKeyWidth, width); 75 mFormat->setInt32(kKeyHeight, height); 76 mFormat->setInt32(kKeyColorFormat, OMX_COLOR_FormatYUV420Planar); 77 mFormat->setCString(kKeyDecoderComponent, "AVCDecoder"); 78 79 int64_t durationUs; 80 if (mSource->getFormat()->findInt64(kKeyDuration, &durationUs)) { 81 mFormat->setInt64(kKeyDuration, durationUs); 82 } 83 } 84 85 AVCDecoder::~AVCDecoder() { 86 if (mStarted) { 87 stop(); 88 } 89 90 PVAVCCleanUpDecoder(mHandle); 91 92 delete mHandle; 93 mHandle = NULL; 94 } 95 96 status_t AVCDecoder::start(MetaData *) { 97 CHECK(!mStarted); 98 99 uint32_t type; 100 const void *data; 101 size_t size; 102 sp<MetaData> meta = mSource->getFormat(); 103 if (meta->findData(kKeyAVCC, &type, &data, &size)) { 104 // Parse the AVCDecoderConfigurationRecord 105 106 const uint8_t *ptr = (const uint8_t *)data; 107 108 CHECK(size >= 7); 109 CHECK_EQ(ptr[0], 1); // configurationVersion == 1 110 uint8_t profile = ptr[1]; 111 uint8_t level = ptr[3]; 112 113 // There is decodable content out there that fails the following 114 // assertion, let's be lenient for now... 115 // CHECK((ptr[4] >> 2) == 0x3f); // reserved 116 117 size_t lengthSize = 1 + (ptr[4] & 3); 118 119 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp 120 // violates it... 121 // CHECK((ptr[5] >> 5) == 7); // reserved 122 123 size_t numSeqParameterSets = ptr[5] & 31; 124 125 ptr += 6; 126 size -= 6; 127 128 for (size_t i = 0; i < numSeqParameterSets; ++i) { 129 CHECK(size >= 2); 130 size_t length = U16_AT(ptr); 131 132 ptr += 2; 133 size -= 2; 134 135 CHECK(size >= length); 136 137 addCodecSpecificData(ptr, length); 138 139 ptr += length; 140 size -= length; 141 } 142 143 CHECK(size >= 1); 144 size_t numPictureParameterSets = *ptr; 145 ++ptr; 146 --size; 147 148 for (size_t i = 0; i < numPictureParameterSets; ++i) { 149 CHECK(size >= 2); 150 size_t length = U16_AT(ptr); 151 152 ptr += 2; 153 size -= 2; 154 155 CHECK(size >= length); 156 157 addCodecSpecificData(ptr, length); 158 159 ptr += length; 160 size -= length; 161 } 162 } 163 164 mSource->start(); 165 166 mAnchorTimeUs = 0; 167 mNumSamplesOutput = 0; 168 mPendingSeekTimeUs = -1; 169 mPendingSeekMode = ReadOptions::SEEK_CLOSEST_SYNC; 170 mTargetTimeUs = -1; 171 mSPSSeen = false; 172 mPPSSeen = false; 173 mStarted = true; 174 175 return OK; 176 } 177 178 void AVCDecoder::addCodecSpecificData(const uint8_t *data, size_t size) { 179 MediaBuffer *buffer = new MediaBuffer(size + 4); 180 memcpy(buffer->data(), kStartCode, 4); 181 memcpy((uint8_t *)buffer->data() + 4, data, size); 182 buffer->set_range(0, size + 4); 183 184 mCodecSpecificData.push(buffer); 185 } 186 187 status_t AVCDecoder::stop() { 188 CHECK(mStarted); 189 190 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) { 191 (*mCodecSpecificData.editItemAt(i)).release(); 192 } 193 mCodecSpecificData.clear(); 194 195 if (mInputBuffer) { 196 mInputBuffer->release(); 197 mInputBuffer = NULL; 198 } 199 200 mSource->stop(); 201 202 releaseFrames(); 203 204 mStarted = false; 205 206 return OK; 207 } 208 209 sp<MetaData> AVCDecoder::getFormat() { 210 return mFormat; 211 } 212 213 static void findNALFragment( 214 const MediaBuffer *buffer, const uint8_t **fragPtr, size_t *fragSize) { 215 const uint8_t *data = 216 (const uint8_t *)buffer->data() + buffer->range_offset(); 217 218 size_t size = buffer->range_length(); 219 220 CHECK(size >= 4); 221 CHECK(!memcmp(kStartCode, data, 4)); 222 223 size_t offset = 4; 224 while (offset + 3 < size && memcmp(kStartCode, &data[offset], 4)) { 225 ++offset; 226 } 227 228 *fragPtr = &data[4]; 229 if (offset + 3 >= size) { 230 *fragSize = size - 4; 231 } else { 232 *fragSize = offset - 4; 233 } 234 } 235 236 MediaBuffer *AVCDecoder::drainOutputBuffer() { 237 int32_t index; 238 int32_t Release; 239 AVCFrameIO Output; 240 Output.YCbCr[0] = Output.YCbCr[1] = Output.YCbCr[2] = NULL; 241 AVCDec_Status status = PVAVCDecGetOutput(mHandle, &index, &Release, &Output); 242 243 if (status != AVCDEC_SUCCESS) { 244 LOGV("PVAVCDecGetOutput returned error %d", status); 245 return NULL; 246 } 247 248 CHECK(index >= 0); 249 CHECK(index < (int32_t)mFrames.size()); 250 251 MediaBuffer *mbuf = mFrames.editItemAt(index); 252 253 bool skipFrame = false; 254 255 if (mTargetTimeUs >= 0) { 256 int64_t timeUs; 257 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs)); 258 CHECK(timeUs <= mTargetTimeUs); 259 260 if (timeUs < mTargetTimeUs) { 261 // We're still waiting for the frame with the matching 262 // timestamp and we won't return the current one. 263 skipFrame = true; 264 265 LOGV("skipping frame at %lld us", timeUs); 266 } else { 267 LOGV("found target frame at %lld us", timeUs); 268 269 mTargetTimeUs = -1; 270 } 271 } 272 273 if (!skipFrame) { 274 mbuf->set_range(0, mbuf->size()); 275 mbuf->add_ref(); 276 277 return mbuf; 278 } 279 280 return new MediaBuffer(0); 281 } 282 283 status_t AVCDecoder::read( 284 MediaBuffer **out, const ReadOptions *options) { 285 *out = NULL; 286 287 int64_t seekTimeUs; 288 ReadOptions::SeekMode mode; 289 if (options && options->getSeekTo(&seekTimeUs, &mode)) { 290 LOGV("seek requested to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6); 291 292 CHECK(seekTimeUs >= 0); 293 mPendingSeekTimeUs = seekTimeUs; 294 mPendingSeekMode = mode; 295 296 if (mInputBuffer) { 297 mInputBuffer->release(); 298 mInputBuffer = NULL; 299 } 300 301 PVAVCDecReset(mHandle); 302 } 303 304 if (mInputBuffer == NULL) { 305 LOGV("fetching new input buffer."); 306 307 bool seeking = false; 308 309 if (!mCodecSpecificData.isEmpty()) { 310 mInputBuffer = mCodecSpecificData.editItemAt(0); 311 mCodecSpecificData.removeAt(0); 312 } else { 313 for (;;) { 314 if (mPendingSeekTimeUs >= 0) { 315 LOGV("reading data from timestamp %lld (%.2f secs)", 316 mPendingSeekTimeUs, mPendingSeekTimeUs / 1E6); 317 } 318 319 ReadOptions seekOptions; 320 if (mPendingSeekTimeUs >= 0) { 321 seeking = true; 322 323 seekOptions.setSeekTo(mPendingSeekTimeUs, mPendingSeekMode); 324 mPendingSeekTimeUs = -1; 325 } 326 status_t err = mSource->read(&mInputBuffer, &seekOptions); 327 seekOptions.clearSeekTo(); 328 329 if (err != OK) { 330 *out = drainOutputBuffer(); 331 return (*out == NULL) ? err : (status_t)OK; 332 } 333 334 if (mInputBuffer->range_length() > 0) { 335 break; 336 } 337 338 mInputBuffer->release(); 339 mInputBuffer = NULL; 340 } 341 } 342 343 if (seeking) { 344 int64_t targetTimeUs; 345 if (mInputBuffer->meta_data()->findInt64(kKeyTargetTime, &targetTimeUs) 346 && targetTimeUs >= 0) { 347 mTargetTimeUs = targetTimeUs; 348 } else { 349 mTargetTimeUs = -1; 350 } 351 } 352 } 353 354 const uint8_t *fragPtr; 355 size_t fragSize; 356 findNALFragment(mInputBuffer, &fragPtr, &fragSize); 357 358 bool releaseFragment = true; 359 status_t err = UNKNOWN_ERROR; 360 361 int nalType; 362 int nalRefIdc; 363 AVCDec_Status res = 364 PVAVCDecGetNALType( 365 const_cast<uint8_t *>(fragPtr), fragSize, 366 &nalType, &nalRefIdc); 367 368 if (res != AVCDEC_SUCCESS) { 369 LOGV("cannot determine nal type"); 370 } else if (nalType == AVC_NALTYPE_SPS || nalType == AVC_NALTYPE_PPS 371 || (mSPSSeen && mPPSSeen)) { 372 switch (nalType) { 373 case AVC_NALTYPE_SPS: 374 { 375 mSPSSeen = true; 376 377 res = PVAVCDecSeqParamSet( 378 mHandle, const_cast<uint8_t *>(fragPtr), 379 fragSize); 380 381 if (res != AVCDEC_SUCCESS) { 382 LOGV("PVAVCDecSeqParamSet returned error %d", res); 383 break; 384 } 385 386 AVCDecObject *pDecVid = (AVCDecObject *)mHandle->AVCObject; 387 388 int32_t width = 389 (pDecVid->seqParams[0]->pic_width_in_mbs_minus1 + 1) * 16; 390 391 int32_t height = 392 (pDecVid->seqParams[0]->pic_height_in_map_units_minus1 + 1) * 16; 393 394 int32_t crop_left, crop_right, crop_top, crop_bottom; 395 if (pDecVid->seqParams[0]->frame_cropping_flag) 396 { 397 crop_left = 2 * pDecVid->seqParams[0]->frame_crop_left_offset; 398 crop_right = 399 width - (2 * pDecVid->seqParams[0]->frame_crop_right_offset + 1); 400 401 if (pDecVid->seqParams[0]->frame_mbs_only_flag) 402 { 403 crop_top = 2 * pDecVid->seqParams[0]->frame_crop_top_offset; 404 crop_bottom = 405 height - 406 (2 * pDecVid->seqParams[0]->frame_crop_bottom_offset + 1); 407 } 408 else 409 { 410 crop_top = 4 * pDecVid->seqParams[0]->frame_crop_top_offset; 411 crop_bottom = 412 height - 413 (4 * pDecVid->seqParams[0]->frame_crop_bottom_offset + 1); 414 } 415 } else { 416 crop_bottom = height - 1; 417 crop_right = width - 1; 418 crop_top = crop_left = 0; 419 } 420 421 int32_t aligned_width = (crop_right - crop_left + 1 + 15) & ~15; 422 int32_t aligned_height = (crop_bottom - crop_top + 1 + 15) & ~15; 423 424 int32_t oldWidth, oldHeight; 425 CHECK(mFormat->findInt32(kKeyWidth, &oldWidth)); 426 CHECK(mFormat->findInt32(kKeyHeight, &oldHeight)); 427 428 if (oldWidth != aligned_width || oldHeight != aligned_height) { 429 mFormat->setInt32(kKeyWidth, aligned_width); 430 mFormat->setInt32(kKeyHeight, aligned_height); 431 432 err = INFO_FORMAT_CHANGED; 433 } else { 434 *out = new MediaBuffer(0); 435 err = OK; 436 } 437 break; 438 } 439 440 case AVC_NALTYPE_PPS: 441 { 442 mPPSSeen = true; 443 444 res = PVAVCDecPicParamSet( 445 mHandle, const_cast<uint8_t *>(fragPtr), 446 fragSize); 447 448 if (res != AVCDEC_SUCCESS) { 449 LOGV("PVAVCDecPicParamSet returned error %d", res); 450 break; 451 } 452 453 *out = new MediaBuffer(0); 454 455 err = OK; 456 break; 457 } 458 459 case AVC_NALTYPE_SLICE: 460 case AVC_NALTYPE_IDR: 461 { 462 res = PVAVCDecodeSlice( 463 mHandle, const_cast<uint8_t *>(fragPtr), 464 fragSize); 465 466 if (res == AVCDEC_PICTURE_OUTPUT_READY) { 467 MediaBuffer *mbuf = drainOutputBuffer(); 468 if (mbuf == NULL) { 469 break; 470 } 471 472 *out = mbuf; 473 474 // Do _not_ release input buffer yet. 475 476 releaseFragment = false; 477 err = OK; 478 break; 479 } 480 481 if (res == AVCDEC_PICTURE_READY || res == AVCDEC_SUCCESS) { 482 *out = new MediaBuffer(0); 483 484 err = OK; 485 } else { 486 LOGV("PVAVCDecodeSlice returned error %d", res); 487 } 488 break; 489 } 490 491 case AVC_NALTYPE_SEI: 492 { 493 res = PVAVCDecSEI( 494 mHandle, const_cast<uint8_t *>(fragPtr), 495 fragSize); 496 497 if (res != AVCDEC_SUCCESS) { 498 break; 499 } 500 501 *out = new MediaBuffer(0); 502 503 err = OK; 504 break; 505 } 506 507 case AVC_NALTYPE_AUD: 508 case AVC_NALTYPE_FILL: 509 case AVC_NALTYPE_EOSEQ: 510 { 511 *out = new MediaBuffer(0); 512 513 err = OK; 514 break; 515 } 516 517 default: 518 { 519 LOGE("Should not be here, unknown nalType %d", nalType); 520 CHECK(!"Should not be here"); 521 break; 522 } 523 } 524 } else { 525 // We haven't seen SPS or PPS yet. 526 527 *out = new MediaBuffer(0); 528 err = OK; 529 } 530 531 if (releaseFragment) { 532 size_t offset = mInputBuffer->range_offset(); 533 if (fragSize + 4 == mInputBuffer->range_length()) { 534 mInputBuffer->release(); 535 mInputBuffer = NULL; 536 } else { 537 mInputBuffer->set_range( 538 offset + fragSize + 4, 539 mInputBuffer->range_length() - fragSize - 4); 540 } 541 } 542 543 return err; 544 } 545 546 // static 547 int32_t AVCDecoder::ActivateSPSWrapper( 548 void *userData, unsigned int sizeInMbs, unsigned int numBuffers) { 549 return static_cast<AVCDecoder *>(userData)->activateSPS(sizeInMbs, numBuffers); 550 } 551 552 // static 553 int32_t AVCDecoder::BindFrameWrapper( 554 void *userData, int32_t index, uint8_t **yuv) { 555 return static_cast<AVCDecoder *>(userData)->bindFrame(index, yuv); 556 } 557 558 // static 559 void AVCDecoder::UnbindFrame(void *userData, int32_t index) { 560 } 561 562 int32_t AVCDecoder::activateSPS( 563 unsigned int sizeInMbs, unsigned int numBuffers) { 564 CHECK(mFrames.isEmpty()); 565 566 size_t frameSize = (sizeInMbs << 7) * 3; 567 for (unsigned int i = 0; i < numBuffers; ++i) { 568 MediaBuffer *buffer = new MediaBuffer(frameSize); 569 buffer->setObserver(this); 570 571 mFrames.push(buffer); 572 } 573 574 return 1; 575 } 576 577 int32_t AVCDecoder::bindFrame(int32_t index, uint8_t **yuv) { 578 CHECK(index >= 0); 579 CHECK(index < (int32_t)mFrames.size()); 580 581 CHECK(mInputBuffer != NULL); 582 int64_t timeUs; 583 CHECK(mInputBuffer->meta_data()->findInt64(kKeyTime, &timeUs)); 584 mFrames[index]->meta_data()->setInt64(kKeyTime, timeUs); 585 586 *yuv = (uint8_t *)mFrames[index]->data(); 587 588 return 1; 589 } 590 591 void AVCDecoder::releaseFrames() { 592 for (size_t i = 0; i < mFrames.size(); ++i) { 593 MediaBuffer *buffer = mFrames.editItemAt(i); 594 595 buffer->setObserver(NULL); 596 buffer->release(); 597 } 598 mFrames.clear(); 599 } 600 601 void AVCDecoder::signalBufferReturned(MediaBuffer *buffer) { 602 } 603 604 } // namespace android 605