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

Commit 1b0be545 authored by Gopalakrishnan Nallasamy's avatar Gopalakrishnan Nallasamy
Browse files

MPEG4Writer:Optimize number of write(2) calls

Instead of making several write(2) calls to pass length of a video buffer,
club them together in an array and call write(2) once. This reduces the
number of system calls to write a buffer length from 4 to 1(default case),
there by avoiding 75% of user/kernel context switches just for this.
At 240 fps, it would be 240 instead of 960 calls.

Bug: 157160851

Test: atest android.media.cts.MediaMuxerTest
Test: atest android.media.cts.MediaRecorderTest
Test: atest android.mediav2.cts.MuxerTest

Change-Id: I7f3c8ecd5f2bccc6c4b87ccd0dc8104f4e98198a
parent e658962c
Loading
Loading
Loading
Loading
+10 −14
Original line number Diff line number Diff line
@@ -1506,25 +1506,21 @@ void MPEG4Writer::addMultipleLengthPrefixedSamples_l(MediaBuffer *buffer) {
void MPEG4Writer::addLengthPrefixedSample_l(MediaBuffer *buffer) {
    size_t length = buffer->range_length();
    if (mUse4ByteNalLength) {
        uint8_t x = length >> 24;
        writeOrPostError(mFd, &x, 1);
        x = (length >> 16) & 0xff;
        writeOrPostError(mFd, &x, 1);
        x = (length >> 8) & 0xff;
        writeOrPostError(mFd, &x, 1);
        x = length & 0xff;
        writeOrPostError(mFd, &x, 1);

        uint8_t x[4];
        x[0] = length >> 24;
        x[1] = (length >> 16) & 0xff;
        x[2] = (length >> 8) & 0xff;
        x[3] = length & 0xff;
        writeOrPostError(mFd, &x, 4);
        writeOrPostError(mFd, (const uint8_t*)buffer->data() + buffer->range_offset(), length);

        mOffset += length + 4;
    } else {
        CHECK_LT(length, 65536u);

        uint8_t x = length >> 8;
        writeOrPostError(mFd, &x, 1);
        x = length & 0xff;
        writeOrPostError(mFd, &x, 1);
        uint8_t x[2];
        x[0] = length >> 8;
        x[1] = length & 0xff;
        writeOrPostError(mFd, &x, 2);
        writeOrPostError(mFd, (const uint8_t*)buffer->data() + buffer->range_offset(), length);
        mOffset += length + 2;
    }