Get yuv size size stored by Mediabuffer

195 Views Asked by At

I am use OpenMax to decode the video frame,my sample is like this:

FILE* fp = fopen("/data/local/tmp/test.yuv", "wb");
while(!isExit)
{
    MediaBuffer *mVideoBuffer;
    MediaSource::ReadOptions options;
    status_t err = mVideoDecoder->read(&mVideoBuffer, &options);
    if (err == OK) 
    {
        if (mVideoBuffer->range_length() > 0) 
        {
            // If video frame availabe, render it to mNativeWindow
            int w = 0;
            int h = 0;
            int dw = 0;
            int dh = 0;
            int stride = 0;
            sp<MetaData> metaData = mVideoBuffer->meta_data();
            sp<MetaData> outFormat = mVideoDecoder->getFormat();

            outFormat->findInt32(kKeyWidth , &w);
            outFormat->findInt32(kKeyHeight, &h);

            int64_t timeUs = 0;
            metaData->findInt64(kKeyTime, &timeUs);
            metaData->findInt32(kKeyDisplayHeight, &dh);
            metaData->findInt32(kKeyDisplayWidth, &dw);
            metaData->findInt32(kKeyStride, &stride);
            printf("out format w:%d h:%d dw:%d dh:%d stride:%d timestamp:%lld\n",
                        w, h, dw, dh, stride, timeUs);

            if(fp)
            {
                printf("decode a frame, range_length = %d range_offset = %d size = %d width = %d height = %d\n",
                            mVideoBuffer->range_length(), mVideoBuffer->range_offset(), mVideoBuffer->size(), w, h);
                fwrite(mVideoBuffer->data( ) + mVideoBuffer->range_offset( ), 1, mVideoBuffer->range_length(), fp);
            }

            metaData->setInt32(kKeyRendered, 1);
        }
        mVideoBuffer->release();
    }
    else
    {
        printf("end of file\n");
        isExit = true;
    }
}
fclose(fp);

the output is like this:

out format w:1280 h:720 dw:0 dh:0 stride:0 timestamp:44044
decode a frame, range_length = 1417216 range_offset = 0 size = 1417216 width = 1280 height = 720

My question is how to know the yuv realy size stored by the MediaBuffer, because 1280 x 736 (stride is 32, i guess) x 1.5 = 1413120,but mVideoBuffer's range_length() is 1417216, there is no regular i can get the yuv size, please help me, thanks !

1

There are 1 best solutions below

0
On

The MediaBuffer which is being read as part read method is initialized here. Please note that the info.mSize is set to def.nBufferSize which is same as allocated size. This information is not updated to reflect the actual size filled by the decoder.

The allocation is 1280 x 736 x 1.5 with the height aligned to 32. The stride is really not required as 1280 is a very well aligned number.