Merge "stagefright: add tracing to AwesomePlayer." into jb-dev
diff --git a/include/media/stagefright/ACodec.h b/include/media/stagefright/ACodec.h
index dd18e95..23e3110 100644
--- a/include/media/stagefright/ACodec.h
+++ b/include/media/stagefright/ACodec.h
@@ -56,6 +56,23 @@
     void initiateConfigureComponent(const sp<AMessage> &msg);
     void initiateStart();
 
+    struct PortDescription : public RefBase {
+        size_t countBuffers();
+        IOMX::buffer_id bufferIDAt(size_t index) const;
+        sp<ABuffer> bufferAt(size_t index) const;
+
+    private:
+        friend struct ACodec;
+
+        Vector<IOMX::buffer_id> mBufferIDs;
+        Vector<sp<ABuffer> > mBuffers;
+
+        PortDescription();
+        void addBuffer(IOMX::buffer_id id, const sp<ABuffer> &buffer);
+
+        DISALLOW_EVIL_CONSTRUCTORS(PortDescription);
+    };
+
 protected:
     virtual ~ACodec();
 
diff --git a/include/media/stagefright/VideoSourceDownSampler.h b/include/media/stagefright/VideoSourceDownSampler.h
deleted file mode 100644
index 439918c..0000000
--- a/include/media/stagefright/VideoSourceDownSampler.h
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// VideoSourceDownSampler implements the MediaSource interface,
-// downsampling frames provided from a real video source.
-
-#ifndef VIDEO_SOURCE_DOWN_SAMPLER_H_
-
-#define VIDEO_SOURCE_DOWN_SAMPLER_H_
-
-#include <media/stagefright/MediaSource.h>
-#include <utils/RefBase.h>
-
-namespace android {
-
-class IMemory;
-class MediaBuffer;
-class MetaData;
-
-class VideoSourceDownSampler : public MediaSource {
-public:
-    virtual ~VideoSourceDownSampler();
-
-    // Constructor:
-    // videoSource: The real video source which provides the original frames.
-    // width, height: The desired width, height. These should be less than or equal
-    // to those of the real video source. We then downsample the original frames to
-    // this size.
-    VideoSourceDownSampler(const sp<MediaSource> &videoSource,
-        int32_t width, int32_t height);
-
-    // MediaSource interface
-    virtual status_t start(MetaData *params = NULL);
-
-    virtual status_t stop();
-
-    virtual sp<MetaData> getFormat();
-
-    virtual status_t read(
-            MediaBuffer **buffer, const ReadOptions *options = NULL);
-
-    virtual status_t pause();
-
-private:
-    // Reference to the real video source.
-    sp<MediaSource> mRealVideoSource;
-
-    // Size of frames to be provided by this source.
-    int32_t mWidth;
-    int32_t mHeight;
-
-    // Size of frames provided by the real source.
-    int32_t mRealSourceWidth;
-    int32_t mRealSourceHeight;
-
-    // Down sampling paramters.
-    int32_t mDownSampleOffsetX;
-    int32_t mDownSampleOffsetY;
-    int32_t mDownSampleSkipX;
-    int32_t mDownSampleSkipY;
-
-    // True if we need to crop the still video image to get the video frame.
-    bool mNeedDownSampling;
-
-    // Meta data. This is a copy of the real source except for the width and
-    // height parameters.
-    sp<MetaData> mMeta;
-
-    // Computes the offset, skip parameters for downsampling the original frame
-    // to the desired size.
-    void computeDownSamplingParameters();
-
-    // Downsamples the frame in sourceBuffer to size (mWidth x mHeight). A new
-    // buffer is created which stores the downsampled image.
-    void downSampleYUVImage(const MediaBuffer &sourceBuffer, MediaBuffer **buffer) const;
-
-    // Disallow these.
-    VideoSourceDownSampler(const VideoSourceDownSampler &);
-    VideoSourceDownSampler &operator=(const VideoSourceDownSampler &);
-};
-
-}  // namespace android
-
-#endif  // VIDEO_SOURCE_DOWN_SAMPLER_H_
diff --git a/media/libstagefright/ACodec.cpp b/media/libstagefright/ACodec.cpp
index 3fd6cef..7ac0f23 100644
--- a/media/libstagefright/ACodec.cpp
+++ b/media/libstagefright/ACodec.cpp
@@ -472,14 +472,16 @@
     notify->setInt32("what", ACodec::kWhatBuffersAllocated);
 
     notify->setInt32("portIndex", portIndex);
-    for (size_t i = 0; i < mBuffers[portIndex].size(); ++i) {
-        AString name = StringPrintf("buffer-id_%d", i);
-        notify->setPointer(name.c_str(), mBuffers[portIndex][i].mBufferID);
 
-        name = StringPrintf("data_%d", i);
-        notify->setBuffer(name.c_str(), mBuffers[portIndex][i].mData);
+    sp<PortDescription> desc = new PortDescription;
+
+    for (size_t i = 0; i < mBuffers[portIndex].size(); ++i) {
+        const BufferInfo &info = mBuffers[portIndex][i];
+
+        desc->addBuffer(info.mBufferID, info.mData);
     }
 
+    notify->setObject("portDesc", desc);
     notify->post();
 
     return OK;
@@ -2110,6 +2112,29 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
+ACodec::PortDescription::PortDescription() {
+}
+
+void ACodec::PortDescription::addBuffer(
+        IOMX::buffer_id id, const sp<ABuffer> &buffer) {
+    mBufferIDs.push_back(id);
+    mBuffers.push_back(buffer);
+}
+
+size_t ACodec::PortDescription::countBuffers() {
+    return mBufferIDs.size();
+}
+
+IOMX::buffer_id ACodec::PortDescription::bufferIDAt(size_t index) const {
+    return mBufferIDs.itemAt(index);
+}
+
+sp<ABuffer> ACodec::PortDescription::bufferAt(size_t index) const {
+    return mBuffers.itemAt(index);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
 ACodec::BaseState::BaseState(ACodec *codec, const sp<AState> &parentState)
     : AState(parentState),
       mCodec(codec) {
diff --git a/media/libstagefright/Android.mk b/media/libstagefright/Android.mk
index 78a68fd..8ad1cb9 100644
--- a/media/libstagefright/Android.mk
+++ b/media/libstagefright/Android.mk
@@ -14,7 +14,6 @@
         AwesomePlayer.cpp                 \
         CameraSource.cpp                  \
         CameraSourceTimeLapse.cpp         \
-        VideoSourceDownSampler.cpp        \
         DataSource.cpp                    \
         DRMExtractor.cpp                  \
         ESDS.cpp                          \
diff --git a/media/libstagefright/MediaCodec.cpp b/media/libstagefright/MediaCodec.cpp
index 5b513a8..ff71170 100644
--- a/media/libstagefright/MediaCodec.cpp
+++ b/media/libstagefright/MediaCodec.cpp
@@ -562,20 +562,20 @@
                     mPortBuffers[portIndex].clear();
 
                     Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
-                    for (size_t i = 0;; ++i) {
-                        AString name = StringPrintf("buffer-id_%d", i);
 
-                        void *bufferID;
-                        if (!msg->findPointer(name.c_str(), &bufferID)) {
-                            break;
-                        }
+                    sp<RefBase> obj;
+                    CHECK(msg->findObject("portDesc", &obj));
 
-                        name = StringPrintf("data_%d", i);
+                    sp<ACodec::PortDescription> portDesc =
+                        static_cast<ACodec::PortDescription *>(obj.get());
 
+                    size_t numBuffers = portDesc->countBuffers();
+
+                    for (size_t i = 0; i < numBuffers; ++i) {
                         BufferInfo info;
-                        info.mBufferID = bufferID;
+                        info.mBufferID = portDesc->bufferIDAt(i);
                         info.mOwnedByClient = false;
-                        CHECK(msg->findBuffer(name.c_str(), &info.mData));
+                        info.mData = portDesc->bufferAt(i);
 
                         if (portIndex == kPortIndexInput && mCrypto != NULL) {
                             info.mEncryptedData =
diff --git a/media/libstagefright/VideoSourceDownSampler.cpp b/media/libstagefright/VideoSourceDownSampler.cpp
deleted file mode 100644
index 90a42c9..0000000
--- a/media/libstagefright/VideoSourceDownSampler.cpp
+++ /dev/null
@@ -1,142 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-//#define LOG_NDEBUG 0
-#define LOG_TAG "VideoSourceDownSampler"
-
-#include <media/stagefright/foundation/ADebug.h>
-#include <media/stagefright/VideoSourceDownSampler.h>
-#include <media/stagefright/MediaBuffer.h>
-#include <media/stagefright/MetaData.h>
-#include <media/stagefright/YUVImage.h>
-#include <media/stagefright/YUVCanvas.h>
-#include "OMX_Video.h"
-
-namespace android {
-
-VideoSourceDownSampler::VideoSourceDownSampler(const sp<MediaSource> &videoSource,
-        int32_t width, int32_t height) {
-    ALOGV("Construct VideoSourceDownSampler");
-    CHECK(width > 0);
-    CHECK(height > 0);
-
-    mRealVideoSource = videoSource;
-    mWidth = width;
-    mHeight = height;
-
-    mMeta = new MetaData(*(mRealVideoSource->getFormat()));
-    CHECK(mMeta->findInt32(kKeyWidth, &mRealSourceWidth));
-    CHECK(mMeta->findInt32(kKeyHeight, &mRealSourceHeight));
-
-    if ((mWidth != mRealSourceWidth) || (mHeight != mRealSourceHeight)) {
-        // Change meta data for width and height.
-        CHECK(mWidth <= mRealSourceWidth);
-        CHECK(mHeight <= mRealSourceHeight);
-
-        mNeedDownSampling = true;
-        computeDownSamplingParameters();
-        mMeta->setInt32(kKeyWidth, mWidth);
-        mMeta->setInt32(kKeyHeight, mHeight);
-    } else {
-        mNeedDownSampling = false;
-    }
-}
-
-VideoSourceDownSampler::~VideoSourceDownSampler() {
-}
-
-void VideoSourceDownSampler::computeDownSamplingParameters() {
-    mDownSampleSkipX = mRealSourceWidth / mWidth;
-    mDownSampleSkipY = mRealSourceHeight / mHeight;
-
-    mDownSampleOffsetX = mRealSourceWidth - mDownSampleSkipX * mWidth;
-    mDownSampleOffsetY = mRealSourceHeight - mDownSampleSkipY * mHeight;
-}
-
-void VideoSourceDownSampler::downSampleYUVImage(
-        const MediaBuffer &sourceBuffer, MediaBuffer **buffer) const {
-    // find the YUV format
-    int32_t srcFormat;
-    CHECK(mMeta->findInt32(kKeyColorFormat, &srcFormat));
-    YUVImage::YUVFormat yuvFormat;
-    if (srcFormat == OMX_COLOR_FormatYUV420SemiPlanar) {
-        yuvFormat = YUVImage::YUV420SemiPlanar;
-    } else if (srcFormat == OMX_COLOR_FormatYUV420Planar) {
-        yuvFormat = YUVImage::YUV420Planar;
-    }
-
-    // allocate mediaBuffer for down sampled image and setup a canvas.
-    *buffer = new MediaBuffer(YUVImage::bufferSize(yuvFormat, mWidth, mHeight));
-    YUVImage yuvDownSampledImage(yuvFormat,
-            mWidth, mHeight,
-            (uint8_t *)(*buffer)->data());
-    YUVCanvas yuvCanvasDownSample(yuvDownSampledImage);
-
-    YUVImage yuvImageSource(yuvFormat,
-            mRealSourceWidth, mRealSourceHeight,
-            (uint8_t *)sourceBuffer.data());
-    yuvCanvasDownSample.downsample(mDownSampleOffsetX, mDownSampleOffsetY,
-            mDownSampleSkipX, mDownSampleSkipY,
-            yuvImageSource);
-}
-
-status_t VideoSourceDownSampler::start(MetaData *params) {
-    ALOGV("start");
-    return mRealVideoSource->start();
-}
-
-status_t VideoSourceDownSampler::stop() {
-    ALOGV("stop");
-    return mRealVideoSource->stop();
-}
-
-sp<MetaData> VideoSourceDownSampler::getFormat() {
-    ALOGV("getFormat");
-    return mMeta;
-}
-
-status_t VideoSourceDownSampler::read(
-        MediaBuffer **buffer, const ReadOptions *options) {
-    ALOGV("read");
-    MediaBuffer *realBuffer;
-    status_t err = mRealVideoSource->read(&realBuffer, options);
-
-    if (mNeedDownSampling) {
-        downSampleYUVImage(*realBuffer, buffer);
-
-        int64_t frameTime;
-        realBuffer->meta_data()->findInt64(kKeyTime, &frameTime);
-        (*buffer)->meta_data()->setInt64(kKeyTime, frameTime);
-
-        // We just want this buffer to be deleted when the encoder releases it.
-        // So don't add a reference to it and set the observer to NULL.
-        (*buffer)->setObserver(NULL);
-
-        // The original buffer is no longer required. Release it.
-        realBuffer->release();
-    } else {
-        *buffer = realBuffer;
-    }
-
-    return err;
-}
-
-status_t VideoSourceDownSampler::pause() {
-    ALOGV("pause");
-    return mRealVideoSource->pause();
-}
-
-}  // namespace android
diff --git a/media/libstagefright/codecs/aacdec/SoftAAC.cpp b/media/libstagefright/codecs/aacdec/SoftAAC.cpp
index d509383..65aa2ad 100644
--- a/media/libstagefright/codecs/aacdec/SoftAAC.cpp
+++ b/media/libstagefright/codecs/aacdec/SoftAAC.cpp
@@ -69,7 +69,7 @@
 
     def.nPortIndex = 0;
     def.eDir = OMX_DirInput;
-    def.nBufferCountMin = kNumBuffers;
+    def.nBufferCountMin = kNumInputBuffers;
     def.nBufferCountActual = def.nBufferCountMin;
     def.nBufferSize = 8192;
     def.bEnabled = OMX_TRUE;
@@ -87,7 +87,7 @@
 
     def.nPortIndex = 1;
     def.eDir = OMX_DirOutput;
-    def.nBufferCountMin = kNumBuffers;
+    def.nBufferCountMin = kNumOutputBuffers;
     def.nBufferCountActual = def.nBufferCountMin;
     def.nBufferSize = 8192;
     def.bEnabled = OMX_TRUE;
diff --git a/media/libstagefright/codecs/aacdec/SoftAAC.h b/media/libstagefright/codecs/aacdec/SoftAAC.h
index da0b8ed..c0789ab 100644
--- a/media/libstagefright/codecs/aacdec/SoftAAC.h
+++ b/media/libstagefright/codecs/aacdec/SoftAAC.h
@@ -45,7 +45,8 @@
 
 private:
     enum {
-        kNumBuffers = 4
+        kNumInputBuffers        = 32,
+        kNumOutputBuffers       = 4,
     };
 
     tPVMP4AudioDecoderExternal *mConfig;
diff --git a/media/libstagefright/codecs/aacdec/SoftAAC2.cpp b/media/libstagefright/codecs/aacdec/SoftAAC2.cpp
index e499a0b..303b8ef 100644
--- a/media/libstagefright/codecs/aacdec/SoftAAC2.cpp
+++ b/media/libstagefright/codecs/aacdec/SoftAAC2.cpp
@@ -64,7 +64,7 @@
 
     def.nPortIndex = 0;
     def.eDir = OMX_DirInput;
-    def.nBufferCountMin = kNumBuffers;
+    def.nBufferCountMin = kNumInputBuffers;
     def.nBufferCountActual = def.nBufferCountMin;
     def.nBufferSize = 8192;
     def.bEnabled = OMX_TRUE;
@@ -82,7 +82,7 @@
 
     def.nPortIndex = 1;
     def.eDir = OMX_DirOutput;
-    def.nBufferCountMin = kNumBuffers;
+    def.nBufferCountMin = kNumOutputBuffers;
     def.nBufferCountActual = def.nBufferCountMin;
     def.nBufferSize = 8192 * 2;
     def.bEnabled = OMX_TRUE;
diff --git a/media/libstagefright/codecs/aacdec/SoftAAC2.h b/media/libstagefright/codecs/aacdec/SoftAAC2.h
index e5a1e3e..57565ab 100644
--- a/media/libstagefright/codecs/aacdec/SoftAAC2.h
+++ b/media/libstagefright/codecs/aacdec/SoftAAC2.h
@@ -44,7 +44,8 @@
 
 private:
     enum {
-        kNumBuffers = 4
+        kNumInputBuffers        = 32,
+        kNumOutputBuffers       = 4,
     };
 
     HANDLE_AACDECODER mAACDecoder;
diff --git a/media/libstagefright/timedtext/TimedTextPlayer.cpp b/media/libstagefright/timedtext/TimedTextPlayer.cpp
index f855d90..9b001cc 100644
--- a/media/libstagefright/timedtext/TimedTextPlayer.cpp
+++ b/media/libstagefright/timedtext/TimedTextPlayer.cpp
@@ -160,7 +160,7 @@
     status_t err = mSource->read(&startTimeUs, &endTimeUs,
                                  &(parcelEvent->parcel), options);
     if (err == WOULD_BLOCK) {
-        sp<AMessage> msg = new AMessage(kWhatRetryRead);
+        sp<AMessage> msg = new AMessage(kWhatRetryRead, id());
         if (options != NULL) {
             int64_t seekTimeUs;
             MediaSource::ReadOptions::SeekMode seekMode;
diff --git a/services/audioflinger/FastMixer.cpp b/services/audioflinger/FastMixer.cpp
index 1492a36..04d0f65 100644
--- a/services/audioflinger/FastMixer.cpp
+++ b/services/audioflinger/FastMixer.cpp
@@ -467,7 +467,7 @@
 #ifdef FAST_MIXER_STATISTICS
                 // advance the FIFO queue bounds
                 size_t i = bounds & (FastMixerDumpState::kSamplingN - 1);
-                bounds = (bounds + 1) & 0xFFFF;
+                bounds = (bounds & 0xFFFF0000) | ((bounds + 1) & 0xFFFF);
                 if (full) {
                     bounds += 0x10000;
                 } else if (!(bounds & (FastMixerDumpState::kSamplingN - 1))) {
@@ -621,7 +621,7 @@
         loadNs.sample(sampleLoadNs);
         kHz.sample(sampleCpukHz & ~0xF);
         if (sampleCpukHz == previousCpukHz) {
-            double megacycles = (double) sampleLoadNs * (double) sampleCpukHz;
+            double megacycles = (double) sampleLoadNs * (double) sampleCpukHz * 1e-12;
             double adjMHz = megacycles / mixPeriodSec;  // _not_ wallNs * 1e9
             loadMHz.sample(adjMHz);
         }