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

Commit 999544b8 authored by Automerger Merge Worker's avatar Automerger Merge Worker
Browse files

Merge "Benchmark: Fix for audio encoders on Android 9" am: fb4c7836

Change-Id: I84dfdbdf9bdcdbb2aec7698f39aaf9cb479813ef
parents 17a72c3f fb4c7836
Loading
Loading
Loading
Loading
+22 −12
Original line number Diff line number Diff line
@@ -29,7 +29,9 @@ import java.io.IOException;
import java.nio.ByteBuffer;

public class Encoder {
    private static final int ENCODE_DEFAULT_MAX_INPUT_SIZE = 3840;
    // Change in AUDIO_ENCODE_DEFAULT_MAX_INPUT_SIZE should also be taken to
    // kDefaultAudioEncodeFrameSize present in BenchmarkCommon.h
    private static final int AUDIO_ENCODE_DEFAULT_MAX_INPUT_SIZE = 4096;
    private static final String TAG = "Encoder";
    private static final boolean DEBUG = false;
    private static final int kQueueDequeueTimeoutUs = 1000;
@@ -134,7 +136,7 @@ public class Encoder {
        if (mMime.startsWith("video/")) {
            mFrameSize = frameSize;
        } else {
            int maxInputSize = ENCODE_DEFAULT_MAX_INPUT_SIZE;
            int maxInputSize = AUDIO_ENCODE_DEFAULT_MAX_INPUT_SIZE;
            MediaFormat format = mCodec.getInputFormat();
            if (format.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) {
                maxInputSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
@@ -281,19 +283,27 @@ public class Encoder {
            return;
        }
        int bufSize = inputBuffer.capacity();
        int bytesRead = mFrameSize;
        int bytesToRead = mFrameSize;
        if (mInputBufferSize - mOffset < mFrameSize) {
            bytesRead = (int) (mInputBufferSize - mOffset);
            bytesToRead = (int) (mInputBufferSize - mOffset);
        }
        if (bufSize < bytesRead) {
        //b/148655275 - Update Frame size, as Format value may not be valid
        if (bufSize < bytesToRead) {
            if(mNumInputFrame == 0) {
                mFrameSize = bufSize;
                bytesToRead = bufSize;
                mNumFrames = (int) ((mInputBufferSize + mFrameSize - 1) / mFrameSize);
            } else {
                mSignalledError = true;
                return;
            }
        byte[] inputArray = new byte[bytesRead];
        mInputStream.read(inputArray, 0, bytesRead);
        }

        byte[] inputArray = new byte[bytesToRead];
        mInputStream.read(inputArray, 0, bytesToRead);
        inputBuffer.put(inputArray);
        int flag = 0;
        if (mNumInputFrame >= mNumFrames - 1 || bytesRead == 0) {
        if (mNumInputFrame >= mNumFrames - 1 || bytesToRead == 0) {
            Log.i(TAG, "Sending EOS on input last frame");
            mSawInputEOS = true;
            flag = MediaCodec.BUFFER_FLAG_END_OF_STREAM;
@@ -304,9 +314,9 @@ public class Encoder {
        } else {
            presentationTimeUs = mNumInputFrame * mFrameSize * 1000000 / mSampleRate;
        }
        mediaCodec.queueInputBuffer(inputBufferId, 0, bytesRead, presentationTimeUs, flag);
        mediaCodec.queueInputBuffer(inputBufferId, 0, bytesToRead, presentationTimeUs, flag);
        mNumInputFrame++;
        mOffset += bytesRead;
        mOffset += bytesToRead;
    }

    /**
+3 −0
Original line number Diff line number Diff line
@@ -35,6 +35,9 @@ using namespace std;
constexpr uint32_t kQueueDequeueTimeoutUs = 1000;
constexpr uint32_t kMaxCSDStrlen = 16;
constexpr uint32_t kMaxBufferSize = 1024 * 1024 * 16;
// Change in kDefaultAudioEncodeFrameSize should also be taken to
// AUDIO_ENCODE_DEFAULT_MAX_INPUT_SIZE present in Encoder.java
constexpr uint32_t kDefaultAudioEncodeFrameSize = 4096;

template <typename T>
class CallBackQueue {
+27 −21
Original line number Diff line number Diff line
@@ -47,29 +47,36 @@ void Encoder::onInputAvailable(AMediaCodec *mediaCodec, int32_t bufIdx) {
            mEncoderDoneCondition.notify_one();
            return;
        }
        size_t bytesRead = mParams.frameSize;
        size_t bytesToRead = mParams.frameSize;
        if (mInputBufferSize - mOffset < mParams.frameSize) {
            bytesRead = mInputBufferSize - mOffset;
            bytesToRead = mInputBufferSize - mOffset;
        }
        if (bufSize < bytesRead) {
            ALOGE("bytes to read %zu bufSize %zu \n", bytesRead, bufSize);
        //b/148655275 - Update Frame size, as Format value may not be valid
        if (bufSize < bytesToRead) {
            if(mNumInputFrame == 0) {
                mParams.frameSize = bufSize;
                bytesToRead = bufSize;
                mParams.numFrames = (mInputBufferSize + mParams.frameSize - 1) / mParams.frameSize;
            } else {
                ALOGE("bytes to read %zu bufSize %zu \n", bytesToRead, bufSize);
                mErrorCode = AMEDIA_ERROR_MALFORMED;
                mSignalledError = true;
                mEncoderDoneCondition.notify_one();
                return;
            }
        if (bytesRead < mParams.frameSize && mNumInputFrame < mParams.numFrames - 1) {
            ALOGE("Partial frame at frameID %d bytesRead %zu frameSize %d total numFrames %d\n",
                  mNumInputFrame, bytesRead, mParams.frameSize, mParams.numFrames);
        }
        if (bytesToRead < mParams.frameSize && mNumInputFrame < mParams.numFrames - 1) {
            ALOGE("Partial frame at frameID %d bytesToRead %zu frameSize %d total numFrames %d\n",
                  mNumInputFrame, bytesToRead, mParams.frameSize, mParams.numFrames);
            mErrorCode = AMEDIA_ERROR_MALFORMED;
            mSignalledError = true;
            mEncoderDoneCondition.notify_one();
            return;
        }
        mEleStream->read(buf, bytesRead);
        mEleStream->read(buf, bytesToRead);
        size_t bytesgcount = mEleStream->gcount();
        if (bytesgcount != bytesRead) {
            ALOGE("bytes to read %zu actual bytes read %zu \n", bytesRead, bytesgcount);
        if (bytesgcount != bytesToRead) {
            ALOGE("bytes to read %zu actual bytes read %zu \n", bytesToRead, bytesgcount);
            mErrorCode = AMEDIA_ERROR_MALFORMED;
            mSignalledError = true;
            mEncoderDoneCondition.notify_one();
@@ -77,7 +84,7 @@ void Encoder::onInputAvailable(AMediaCodec *mediaCodec, int32_t bufIdx) {
        }

        uint32_t flag = 0;
        if (mNumInputFrame == mParams.numFrames - 1 || bytesRead == 0) {
        if (mNumInputFrame == mParams.numFrames - 1 || bytesToRead == 0) {
            ALOGD("Sending EOS on input Last frame\n");
            flag |= AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM;
        }
@@ -92,10 +99,10 @@ void Encoder::onInputAvailable(AMediaCodec *mediaCodec, int32_t bufIdx) {

        if (flag == AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM) mSawInputEOS = true;
        ALOGV("%s bytesRead : %zd presentationTimeUs : %" PRIu64 " mSawInputEOS : %s", __FUNCTION__,
              bytesRead, presentationTimeUs, mSawInputEOS ? "TRUE" : "FALSE");
              bytesToRead, presentationTimeUs, mSawInputEOS ? "TRUE" : "FALSE");

        media_status_t status = AMediaCodec_queueInputBuffer(mCodec, bufIdx, 0 /* offset */,
                                                             bytesRead, presentationTimeUs, flag);
                                                             bytesToRead, presentationTimeUs, flag);
        if (AMEDIA_OK != status) {
            mErrorCode = status;
            mSignalledError = true;
@@ -103,7 +110,7 @@ void Encoder::onInputAvailable(AMediaCodec *mediaCodec, int32_t bufIdx) {
            return;
        }
        mNumInputFrame++;
        mOffset += bytesRead;
        mOffset += bytesToRead;
    }
}

@@ -220,13 +227,12 @@ int32_t Encoder::encode(string &codecName, ifstream &eleStream, size_t eleSize,
    if (!strncmp(mMime, "video/", 6)) {
        mParams.frameSize = mParams.width * mParams.height * 3 / 2;
    } else {
        mParams.frameSize = 4096;
        mParams.frameSize = kDefaultAudioEncodeFrameSize;
        // Get mInputMaxBufSize
        AMediaFormat *inputFormat = AMediaCodec_getInputFormat(mCodec);
        AMediaFormat_getInt32(inputFormat, AMEDIAFORMAT_KEY_MAX_INPUT_SIZE, &mParams.maxFrameSize);
        if (mParams.maxFrameSize < 0) {
            ALOGE("Invalid mParams.maxFrameSize %d\n", mParams.maxFrameSize);
            return AMEDIA_ERROR_INVALID_PARAMETER;
            mParams.maxFrameSize = kDefaultAudioEncodeFrameSize;
        }
        if (mParams.frameSize > mParams.maxFrameSize) {
            mParams.frameSize = mParams.maxFrameSize;