Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 05b6a7c5 authored by Wonsik Kim's avatar Wonsik Kim
Browse files

stagefright: propagate dataspace from BufferQueue to MediaCodec.

In addition to the plumbing, MPEG4Writer reads format() again when
writing color information, since it could have been updated from
BufferQueue at that time.

Bug: 160727754
Test: manual
Change-Id: I47894ce36f159ea6ddd6a5c2e1794b9690a86537
parent 40aaf954
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -797,7 +797,7 @@ status_t CameraSource::start(MetaData *meta) {
    mStartTimeUs = 0;
    mNumInputBuffers = 0;
    mEncoderFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
    mEncoderDataSpace = HAL_DATASPACE_V0_BT709;
    mEncoderDataSpace = mBufferDataSpace = HAL_DATASPACE_V0_BT709;

    if (meta) {
        int64_t startTimeUs;
@@ -817,6 +817,7 @@ status_t CameraSource::start(MetaData *meta) {
        }
        if (meta->findInt32(kKeyColorSpace, &mEncoderDataSpace)) {
            ALOGI("Using encoder data space: %#x", mEncoderDataSpace);
            mBufferDataSpace = mEncoderDataSpace;
        }
    }

@@ -1114,6 +1115,11 @@ status_t CameraSource::read(
        (*buffer)->setObserver(this);
        (*buffer)->add_ref();
        (*buffer)->meta_data().setInt64(kKeyTime, frameTime);
        if (mBufferDataSpace != mEncoderDataSpace) {
            ALOGD("Data space updated to %x", mBufferDataSpace);
            (*buffer)->meta_data().setInt32(kKeyColorSpace, mBufferDataSpace);
            mEncoderDataSpace = mBufferDataSpace;
        }
    }
    return OK;
}
@@ -1391,6 +1397,7 @@ void CameraSource::processBufferQueueFrame(BufferItem& buffer) {
    // Find a available memory slot to store the buffer as VideoNativeMetadata.
    sp<IMemory> data = *mMemoryBases.begin();
    mMemoryBases.erase(mMemoryBases.begin());
    mBufferDataSpace = buffer.mDataSpace;

    ssize_t offset;
    size_t size;
+13 −4
Original line number Diff line number Diff line
@@ -4217,13 +4217,20 @@ void MPEG4Writer::Track::writeVideoFourCCBox() {
void MPEG4Writer::Track::writeColrBox() {
    ColorAspects aspects;
    memset(&aspects, 0, sizeof(aspects));
    // Color metadata may have changed.
    sp<MetaData> meta = mSource->getFormat();
    // TRICKY: using | instead of || because we want to execute all findInt32-s
    if (mMeta->findInt32(kKeyColorPrimaries, (int32_t*)&aspects.mPrimaries)
            | mMeta->findInt32(kKeyTransferFunction, (int32_t*)&aspects.mTransfer)
            | mMeta->findInt32(kKeyColorMatrix, (int32_t*)&aspects.mMatrixCoeffs)
            | mMeta->findInt32(kKeyColorRange, (int32_t*)&aspects.mRange)) {
    if (meta->findInt32(kKeyColorPrimaries, (int32_t*)&aspects.mPrimaries)
            | meta->findInt32(kKeyTransferFunction, (int32_t*)&aspects.mTransfer)
            | meta->findInt32(kKeyColorMatrix, (int32_t*)&aspects.mMatrixCoeffs)
            | meta->findInt32(kKeyColorRange, (int32_t*)&aspects.mRange)) {
        int32_t primaries, transfer, coeffs;
        bool fullRange;
        ALOGV("primaries=%s transfer=%s matrix=%s range=%s",
                asString(aspects.mPrimaries),
                asString(aspects.mTransfer),
                asString(aspects.mMatrixCoeffs),
                asString(aspects.mRange));
        ColorUtils::convertCodecColorAspectsToIsoAspects(
                aspects, &primaries, &transfer, &coeffs, &fullRange);
        mOwner->beginBox("colr");
@@ -4233,6 +4240,8 @@ void MPEG4Writer::Track::writeColrBox() {
        mOwner->writeInt16(coeffs);
        mOwner->writeInt8(int8_t(fullRange ? 0x80 : 0x0));
        mOwner->endBox(); // colr
    } else {
        ALOGV("no color information");
    }
}

+21 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/ALooper.h>
#include <media/stagefright/foundation/AMessage.h>
#include <media/stagefright/foundation/ColorUtils.h>
#include <media/stagefright/MediaBuffer.h>
#include <media/stagefright/MediaCodec.h>
#include <media/stagefright/MediaCodecConstants.h>
@@ -768,6 +769,26 @@ status_t MediaCodecSource::feedEncoderInputBuffers() {
            memcpy(inbuf->data(), mbuf->data(), size);

            if (mIsVideo) {
                int32_t ds = 0;
                if (mbuf->meta_data().findInt32(kKeyColorSpace, &ds)
                        && ds != HAL_DATASPACE_UNKNOWN) {
                    android_dataspace dataspace = static_cast<android_dataspace>(ds);
                    ColorUtils::convertDataSpaceToV0(dataspace);
                    ALOGD("Updating dataspace to %x", dataspace);
                    int32_t standard = (int32_t(dataspace) & HAL_DATASPACE_STANDARD_MASK)
                        >> HAL_DATASPACE_STANDARD_SHIFT;
                    int32_t transfer = (int32_t(dataspace) & HAL_DATASPACE_TRANSFER_MASK)
                        >> HAL_DATASPACE_TRANSFER_SHIFT;
                    int32_t range = (int32_t(dataspace) & HAL_DATASPACE_RANGE_MASK)
                        >> HAL_DATASPACE_RANGE_SHIFT;
                    sp<AMessage> msg = new AMessage;
                    msg->setInt32(KEY_COLOR_STANDARD, standard);
                    msg->setInt32(KEY_COLOR_TRANSFER, transfer);
                    msg->setInt32(KEY_COLOR_RANGE, range);
                    msg->setInt32("android._dataspace", dataspace);
                    mEncoder->setParameters(msg);
                }

                // video encoder will release MediaBuffer when done
                // with underlying data.
                inbuf->meta()->setObject("mediaBufferHolder", new MediaBufferHolder(mbuf));
+1 −0
Original line number Diff line number Diff line
@@ -192,6 +192,7 @@ protected:
    int32_t  mColorFormat;
    int32_t  mEncoderFormat;
    int32_t  mEncoderDataSpace;
    int32_t  mBufferDataSpace;
    status_t mInitCheck;

    sp<Camera>   mCamera;