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

Commit a1cc7d57 authored by Andreas Huber's avatar Andreas Huber
Browse files

In certain cases where AAC audio frames extended into the next PES payload

(inside transport streams) timestamps would be miscalculated. This fixes it.

Change-Id: I9d74eeea474d2b89e8a9cdc478ed6085282fb3be
parent 2e9c6f37
Loading
Loading
Loading
Loading
+37 −24
Original line number Diff line number Diff line
@@ -147,8 +147,8 @@ status_t ElementaryStreamQueue::appendData(
                }

                if (startOffset > 0) {
                    ALOGI("found something resembling an H.264/MPEG syncword at "
                         "offset %ld",
                    ALOGI("found something resembling an H.264/MPEG syncword "
                          "at offset %d",
                          startOffset);
                }

@@ -180,8 +180,8 @@ status_t ElementaryStreamQueue::appendData(
                }

                if (startOffset > 0) {
                    ALOGI("found something resembling an H.264/MPEG syncword at "
                         "offset %ld",
                    ALOGI("found something resembling an H.264/MPEG syncword "
                          "at offset %d",
                          startOffset);
                }

@@ -213,7 +213,8 @@ status_t ElementaryStreamQueue::appendData(
                }

                if (startOffset > 0) {
                    ALOGI("found something resembling an AAC syncword at offset %ld",
                    ALOGI("found something resembling an AAC syncword at "
                          "offset %d",
                          startOffset);
                }

@@ -241,7 +242,7 @@ status_t ElementaryStreamQueue::appendData(

                if (startOffset > 0) {
                    ALOGI("found something resembling an MPEG audio "
                         "syncword at offset %ld",
                          "syncword at offset %d",
                          startOffset);
                }

@@ -394,10 +395,30 @@ sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitPCMAudio() {
}

sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitAAC() {
    int64_t timeUs;
    if (mBuffer->size() == 0) {
        return NULL;
    }

    CHECK(!mRangeInfos.empty());

    const RangeInfo &info = *mRangeInfos.begin();
    if (mBuffer->size() < info.mLength) {
        return NULL;
    }

    CHECK_GE(info.mTimestampUs, 0ll);

    // The idea here is consume all AAC frames starting at offsets before
    // info.mLength so we can assign a meaningful timestamp without
    // having to interpolate.
    // The final AAC frame may well extend into the next RangeInfo but
    // that's ok.
    size_t offset = 0;
    while (offset + 7 <= mBuffer->size()) {
    while (offset < info.mLength) {
        if (offset + 7 > mBuffer->size()) {
            return NULL;
        }

        ABitReader bits(mBuffer->data() + offset, mBuffer->size() - offset);

        // adts_fixed_header
@@ -450,24 +471,15 @@ sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitAAC() {
        }

        if (offset + aac_frame_length > mBuffer->size()) {
            break;
            return NULL;
        }

        size_t headerSize = protection_absent ? 7 : 9;

        int64_t tmpUs = fetchTimestamp(aac_frame_length);
        CHECK_GE(tmpUs, 0ll);

        if (offset == 0) {
            timeUs = tmpUs;
        }

        offset += aac_frame_length;
    }

    if (offset == 0) {
        return NULL;
    }
    int64_t timeUs = fetchTimestamp(offset);

    sp<ABuffer> accessUnit = new ABuffer(offset);
    memcpy(accessUnit->data(), mBuffer->data(), offset);
@@ -492,7 +504,6 @@ int64_t ElementaryStreamQueue::fetchTimestamp(size_t size) {

        if (first) {
            timeUs = info->mTimestampUs;
            first = false;
        }

        if (info->mLength > size) {
@@ -509,6 +520,8 @@ int64_t ElementaryStreamQueue::fetchTimestamp(size_t size) {
            mRangeInfos.erase(mRangeInfos.begin());
            info = NULL;
        }

        first = false;
    }

    if (timeUs == 0ll) {