From d831e2ea5e74c06bd6fb9e8b61d710f466a507cc Mon Sep 17 00:00:00 2001 From: Marco Nelissen Date: Thu, 29 Oct 2015 14:10:48 -0700 Subject: MatroskaExtractor: don't use new buffer unless necessary When the NAL size length is 4 bytes, we can store the start code in the space occupied by the NAL size length and avoid allocating a new buffer and copying the data into it. Also fix potential memory leak. Change-Id: I29dd72a0616dfae66ddd7b7f9478678e356df440 --- .../libstagefright/matroska/MatroskaExtractor.cpp | 23 ++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/media/libstagefright/matroska/MatroskaExtractor.cpp b/media/libstagefright/matroska/MatroskaExtractor.cpp index ff81379..06057d0 100644 --- a/media/libstagefright/matroska/MatroskaExtractor.cpp +++ b/media/libstagefright/matroska/MatroskaExtractor.cpp @@ -548,6 +548,7 @@ status_t MatroskaSource::readBlock() { mPendingFrames.clear(); mBlockIter.advance(); + mbuf->release(); return ERROR_IO; } @@ -668,9 +669,11 @@ status_t MatroskaSource::read( if (pass == 1) { memcpy(&dstPtr[dstOffset], "\x00\x00\x00\x01", 4); - memcpy(&dstPtr[dstOffset + 4], - &srcPtr[srcOffset + mNALSizeLen], - NALsize); + if (frame != buffer) { + memcpy(&dstPtr[dstOffset + 4], + &srcPtr[srcOffset + mNALSizeLen], + NALsize); + } } dstOffset += 4; // 0x00 00 00 01 @@ -692,7 +695,13 @@ status_t MatroskaSource::read( if (pass == 0) { dstSize = dstOffset; - buffer = new MediaBuffer(dstSize); + if (dstSize == srcSize && mNALSizeLen == 4) { + // In this special case we can re-use the input buffer by substituting + // each 4-byte nal size with a 4-byte start code + buffer = frame; + } else { + buffer = new MediaBuffer(dstSize); + } int64_t timeUs; CHECK(frame->meta_data()->findInt64(kKeyTime, &timeUs)); @@ -706,8 +715,10 @@ status_t MatroskaSource::read( } } - frame->release(); - frame = NULL; + if (frame != buffer) { + frame->release(); + frame = NULL; + } if (targetSampleTimeUs >= 0ll) { buffer->meta_data()->setInt64( -- cgit v1.1