Home | History | Annotate | Download | only in dec
      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 
     35 namespace android {
     36 
     37 static int32_t Malloc(void *userData, int32_t size, int32_t attrs) {
     38     return reinterpret_cast<int32_t>(malloc(size));
     39 }
     40 
     41 static void Free(void *userData, int32_t ptr) {
     42     free(reinterpret_cast<void *>(ptr));
     43 }
     44 
     45 AVCDecoder::AVCDecoder(const sp<MediaSource> &source)
     46     : mSource(source),
     47       mStarted(false),
     48       mHandle(new tagAVCHandle),
     49       mInputBuffer(NULL),
     50       mAnchorTimeUs(0),
     51       mNumSamplesOutput(0),
     52       mPendingSeekTimeUs(-1) {
     53     memset(mHandle, 0, sizeof(tagAVCHandle));
     54     mHandle->AVCObject = NULL;
     55     mHandle->userData = this;
     56     mHandle->CBAVC_DPBAlloc = ActivateSPSWrapper;
     57     mHandle->CBAVC_FrameBind = BindFrameWrapper;
     58     mHandle->CBAVC_FrameUnbind = UnbindFrame;
     59     mHandle->CBAVC_Malloc = Malloc;
     60     mHandle->CBAVC_Free = Free;
     61 
     62     mFormat = new MetaData;
     63     mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
     64     int32_t width, height;
     65     CHECK(mSource->getFormat()->findInt32(kKeyWidth, &width));
     66     CHECK(mSource->getFormat()->findInt32(kKeyHeight, &height));
     67     mFormat->setInt32(kKeyWidth, width);
     68     mFormat->setInt32(kKeyHeight, height);
     69     mFormat->setInt32(kKeyColorFormat, OMX_COLOR_FormatYUV420Planar);
     70     mFormat->setCString(kKeyDecoderComponent, "AVCDecoder");
     71 
     72     int64_t durationUs;
     73     if (mSource->getFormat()->findInt64(kKeyDuration, &durationUs)) {
     74         mFormat->setInt64(kKeyDuration, durationUs);
     75     }
     76 }
     77 
     78 AVCDecoder::~AVCDecoder() {
     79     if (mStarted) {
     80         stop();
     81     }
     82 
     83     PVAVCCleanUpDecoder(mHandle);
     84 
     85     delete mHandle;
     86     mHandle = NULL;
     87 }
     88 
     89 status_t AVCDecoder::start(MetaData *) {
     90     CHECK(!mStarted);
     91 
     92     uint32_t type;
     93     const void *data;
     94     size_t size;
     95     sp<MetaData> meta = mSource->getFormat();
     96     if (meta->findData(kKeyAVCC, &type, &data, &size)) {
     97         // Parse the AVCDecoderConfigurationRecord
     98 
     99         const uint8_t *ptr = (const uint8_t *)data;
    100 
    101         CHECK(size >= 7);
    102         CHECK_EQ(ptr[0], 1);  // configurationVersion == 1
    103         uint8_t profile = ptr[1];
    104         uint8_t level = ptr[3];
    105 
    106         // There is decodable content out there that fails the following
    107         // assertion, let's be lenient for now...
    108         // CHECK((ptr[4] >> 2) == 0x3f);  // reserved
    109 
    110         size_t lengthSize = 1 + (ptr[4] & 3);
    111 
    112         // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
    113         // violates it...
    114         // CHECK((ptr[5] >> 5) == 7);  // reserved
    115 
    116         size_t numSeqParameterSets = ptr[5] & 31;
    117 
    118         ptr += 6;
    119         size -= 6;
    120 
    121         for (size_t i = 0; i < numSeqParameterSets; ++i) {
    122             CHECK(size >= 2);
    123             size_t length = U16_AT(ptr);
    124 
    125             ptr += 2;
    126             size -= 2;
    127 
    128             CHECK(size >= length);
    129 
    130             addCodecSpecificData(ptr, length);
    131 
    132             ptr += length;
    133             size -= length;
    134         }
    135 
    136         CHECK(size >= 1);
    137         size_t numPictureParameterSets = *ptr;
    138         ++ptr;
    139         --size;
    140 
    141         for (size_t i = 0; i < numPictureParameterSets; ++i) {
    142             CHECK(size >= 2);
    143             size_t length = U16_AT(ptr);
    144 
    145             ptr += 2;
    146             size -= 2;
    147 
    148             CHECK(size >= length);
    149 
    150             addCodecSpecificData(ptr, length);
    151 
    152             ptr += length;
    153             size -= length;
    154         }
    155     }
    156 
    157     sp<MetaData> params = new MetaData;
    158     params->setInt32(kKeyWantsNALFragments, true);
    159     mSource->start(params.get());
    160 
    161     mAnchorTimeUs = 0;
    162     mNumSamplesOutput = 0;
    163     mPendingSeekTimeUs = -1;
    164     mStarted = true;
    165 
    166     return OK;
    167 }
    168 
    169 void AVCDecoder::addCodecSpecificData(const uint8_t *data, size_t size) {
    170     MediaBuffer *buffer = new MediaBuffer(size);
    171     memcpy(buffer->data(), data, size);
    172     buffer->set_range(0, size);
    173 
    174     mCodecSpecificData.push(buffer);
    175 }
    176 
    177 status_t AVCDecoder::stop() {
    178     CHECK(mStarted);
    179 
    180     for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
    181         (*mCodecSpecificData.editItemAt(i)).release();
    182     }
    183     mCodecSpecificData.clear();
    184 
    185     if (mInputBuffer) {
    186         mInputBuffer->release();
    187         mInputBuffer = NULL;
    188     }
    189 
    190     mSource->stop();
    191 
    192     releaseFrames();
    193 
    194     mStarted = false;
    195 
    196     return OK;
    197 }
    198 
    199 sp<MetaData> AVCDecoder::getFormat() {
    200     return mFormat;
    201 }
    202 
    203 status_t AVCDecoder::read(
    204         MediaBuffer **out, const ReadOptions *options) {
    205     *out = NULL;
    206 
    207     int64_t seekTimeUs;
    208     if (options && options->getSeekTo(&seekTimeUs)) {
    209         LOGV("seek requested to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
    210 
    211         CHECK(seekTimeUs >= 0);
    212         mPendingSeekTimeUs = seekTimeUs;
    213 
    214         if (mInputBuffer) {
    215             mInputBuffer->release();
    216             mInputBuffer = NULL;
    217         }
    218 
    219         PVAVCDecReset(mHandle);
    220     }
    221 
    222     if (mInputBuffer == NULL) {
    223         LOGV("fetching new input buffer.");
    224 
    225         if (!mCodecSpecificData.isEmpty()) {
    226             mInputBuffer = mCodecSpecificData.editItemAt(0);
    227             mCodecSpecificData.removeAt(0);
    228         } else {
    229             for (;;) {
    230                 if (mPendingSeekTimeUs >= 0) {
    231                     LOGV("reading data from timestamp %lld (%.2f secs)",
    232                          mPendingSeekTimeUs, mPendingSeekTimeUs / 1E6);
    233                 }
    234 
    235                 ReadOptions seekOptions;
    236                 if (mPendingSeekTimeUs >= 0) {
    237                     seekOptions.setSeekTo(mPendingSeekTimeUs);
    238                     mPendingSeekTimeUs = -1;
    239                 }
    240                 status_t err = mSource->read(&mInputBuffer, &seekOptions);
    241                 seekOptions.clearSeekTo();
    242 
    243                 if (err != OK) {
    244                     return err;
    245                 }
    246 
    247                 if (mInputBuffer->range_length() > 0) {
    248                     break;
    249                 }
    250 
    251                 mInputBuffer->release();
    252                 mInputBuffer = NULL;
    253             }
    254         }
    255     }
    256 
    257     const uint8_t *inPtr =
    258         (const uint8_t *)mInputBuffer->data() + mInputBuffer->range_offset();
    259 
    260     int nalType;
    261     int nalRefIdc;
    262     AVCDec_Status res =
    263         PVAVCDecGetNALType(
    264                 const_cast<uint8_t *>(inPtr), mInputBuffer->range_length(),
    265                 &nalType, &nalRefIdc);
    266 
    267     if (res != AVCDEC_SUCCESS) {
    268         LOGE("cannot determine nal type");
    269 
    270         mInputBuffer->release();
    271         mInputBuffer = NULL;
    272 
    273         return UNKNOWN_ERROR;
    274     }
    275 
    276     switch (nalType) {
    277         case AVC_NALTYPE_SPS:
    278         {
    279             res = PVAVCDecSeqParamSet(
    280                     mHandle, const_cast<uint8_t *>(inPtr),
    281                     mInputBuffer->range_length());
    282 
    283             if (res != AVCDEC_SUCCESS) {
    284                 mInputBuffer->release();
    285                 mInputBuffer = NULL;
    286 
    287                 return UNKNOWN_ERROR;
    288             }
    289 
    290             AVCDecObject *pDecVid = (AVCDecObject *)mHandle->AVCObject;
    291 
    292             int32_t width =
    293                 (pDecVid->seqParams[0]->pic_width_in_mbs_minus1 + 1) * 16;
    294 
    295             int32_t height =
    296                 (pDecVid->seqParams[0]->pic_height_in_map_units_minus1 + 1) * 16;
    297 
    298             int32_t crop_left, crop_right, crop_top, crop_bottom;
    299             if (pDecVid->seqParams[0]->frame_cropping_flag)
    300             {
    301                 crop_left = 2 * pDecVid->seqParams[0]->frame_crop_left_offset;
    302                 crop_right =
    303                     width - (2 * pDecVid->seqParams[0]->frame_crop_right_offset + 1);
    304 
    305                 if (pDecVid->seqParams[0]->frame_mbs_only_flag)
    306                 {
    307                     crop_top = 2 * pDecVid->seqParams[0]->frame_crop_top_offset;
    308                     crop_bottom =
    309                         height -
    310                         (2 * pDecVid->seqParams[0]->frame_crop_bottom_offset + 1);
    311                 }
    312                 else
    313                 {
    314                     crop_top = 4 * pDecVid->seqParams[0]->frame_crop_top_offset;
    315                     crop_bottom =
    316                         height -
    317                         (4 * pDecVid->seqParams[0]->frame_crop_bottom_offset + 1);
    318                 }
    319             } else {
    320                 crop_bottom = height - 1;
    321                 crop_right = width - 1;
    322                 crop_top = crop_left = 0;
    323             }
    324 
    325             int32_t aligned_width = (crop_right - crop_left + 1 + 15) & ~15;
    326             int32_t aligned_height = (crop_bottom - crop_top + 1 + 15) & ~15;
    327             mFormat->setInt32(kKeyWidth, aligned_width);
    328             mFormat->setInt32(kKeyHeight, aligned_height);
    329 
    330             mInputBuffer->release();
    331             mInputBuffer = NULL;
    332 
    333             return INFO_FORMAT_CHANGED;
    334         }
    335 
    336         case AVC_NALTYPE_PPS:
    337         {
    338             res = PVAVCDecPicParamSet(
    339                     mHandle, const_cast<uint8_t *>(inPtr),
    340                     mInputBuffer->range_length());
    341 
    342             mInputBuffer->release();
    343             mInputBuffer = NULL;
    344 
    345             if (res != AVCDEC_SUCCESS) {
    346                 return UNKNOWN_ERROR;
    347             }
    348 
    349             *out = new MediaBuffer(0);
    350 
    351             return OK;
    352         }
    353 
    354         case AVC_NALTYPE_SLICE:
    355         case AVC_NALTYPE_IDR:
    356         {
    357             res = PVAVCDecodeSlice(
    358                     mHandle, const_cast<uint8_t *>(inPtr),
    359                     mInputBuffer->range_length());
    360 
    361             if (res == AVCDEC_PICTURE_OUTPUT_READY) {
    362                 int32_t index;
    363                 int32_t Release;
    364                 AVCFrameIO Output;
    365                 Output.YCbCr[0] = Output.YCbCr[1] = Output.YCbCr[2] = NULL;
    366                 CHECK_EQ(PVAVCDecGetOutput(
    367                             mHandle, &index, &Release, &Output),
    368                          AVCDEC_SUCCESS);
    369 
    370                 CHECK(index >= 0);
    371                 CHECK(index < (int32_t)mFrames.size());
    372 
    373                 *out = mFrames.editItemAt(index);
    374                 (*out)->set_range(0, (*out)->size());
    375                 (*out)->add_ref();
    376 
    377                 // Do _not_ release input buffer yet.
    378 
    379                 return OK;
    380             }
    381 
    382             mInputBuffer->release();
    383             mInputBuffer = NULL;
    384 
    385             if (res == AVCDEC_PICTURE_READY || res == AVCDEC_SUCCESS) {
    386                 *out = new MediaBuffer(0);
    387 
    388                 return OK;
    389             } else {
    390                 LOGV("failed to decode frame (res = %d)", res);
    391                 return UNKNOWN_ERROR;
    392             }
    393         }
    394 
    395         case AVC_NALTYPE_SEI:
    396         {
    397             res = PVAVCDecSEI(
    398                     mHandle, const_cast<uint8_t *>(inPtr),
    399                     mInputBuffer->range_length());
    400 
    401             mInputBuffer->release();
    402             mInputBuffer = NULL;
    403 
    404             if (res != AVCDEC_SUCCESS) {
    405                 return UNKNOWN_ERROR;
    406             }
    407 
    408             *out = new MediaBuffer(0);
    409 
    410             return OK;
    411         }
    412 
    413         case AVC_NALTYPE_AUD:
    414         {
    415             mInputBuffer->release();
    416             mInputBuffer = NULL;
    417 
    418             *out = new MediaBuffer(0);
    419 
    420             return OK;
    421         }
    422 
    423         default:
    424         {
    425             LOGE("Should not be here, unknown nalType %d", nalType);
    426             CHECK(!"Should not be here");
    427             break;
    428         }
    429     }
    430 
    431     mInputBuffer->release();
    432     mInputBuffer = NULL;
    433 
    434     return UNKNOWN_ERROR;
    435 }
    436 
    437 // static
    438 int32_t AVCDecoder::ActivateSPSWrapper(
    439         void *userData, unsigned int sizeInMbs, unsigned int numBuffers) {
    440     return static_cast<AVCDecoder *>(userData)->activateSPS(sizeInMbs, numBuffers);
    441 }
    442 
    443 // static
    444 int32_t AVCDecoder::BindFrameWrapper(
    445         void *userData, int32_t index, uint8_t **yuv) {
    446     return static_cast<AVCDecoder *>(userData)->bindFrame(index, yuv);
    447 }
    448 
    449 // static
    450 void AVCDecoder::UnbindFrame(void *userData, int32_t index) {
    451 }
    452 
    453 int32_t AVCDecoder::activateSPS(
    454         unsigned int sizeInMbs, unsigned int numBuffers) {
    455     CHECK(mFrames.isEmpty());
    456 
    457     size_t frameSize = (sizeInMbs << 7) * 3;
    458     for (unsigned int i = 0; i < numBuffers; ++i) {
    459         MediaBuffer *buffer = new MediaBuffer(frameSize);
    460         buffer->setObserver(this);
    461 
    462         mFrames.push(buffer);
    463     }
    464 
    465     return 1;
    466 }
    467 
    468 int32_t AVCDecoder::bindFrame(int32_t index, uint8_t **yuv) {
    469     CHECK(index >= 0);
    470     CHECK(index < (int32_t)mFrames.size());
    471 
    472     CHECK(mInputBuffer != NULL);
    473     int64_t timeUs;
    474     CHECK(mInputBuffer->meta_data()->findInt64(kKeyTime, &timeUs));
    475     mFrames[index]->meta_data()->setInt64(kKeyTime, timeUs);
    476 
    477     *yuv = (uint8_t *)mFrames[index]->data();
    478 
    479     return 1;
    480 }
    481 
    482 void AVCDecoder::releaseFrames() {
    483     for (size_t i = 0; i < mFrames.size(); ++i) {
    484         MediaBuffer *buffer = mFrames.editItemAt(i);
    485 
    486         buffer->setObserver(NULL);
    487         buffer->release();
    488     }
    489     mFrames.clear();
    490 }
    491 
    492 void AVCDecoder::signalBufferReturned(MediaBuffer *buffer) {
    493 }
    494 
    495 }  // namespace android
    496