Squashed commit of the following:

commit 333057b355f8c260c549553b9a0634755c838b6a
Author: Andreas Huber <andih@google.com>
Date:   Fri Nov 13 15:35:48 2009 -0800

    Some more tweaks to AVC encoding on sholes.

commit 9981d0ee52ec5b8b0182aae733d1571e3ebb8390
Author: Andreas Huber <andih@google.com>
Date:   Thu Nov 12 16:36:57 2009 -0800

    Support for avc encoding, including sholes specific tweaks to pick the right colorspace for the camera to not require transcoding.

commit 5ba0ebbbd4efca51f3ae1f60e2ca31e7d2cf136d
Author: Andreas Huber <andih@google.com>
Date:   Wed Nov 11 09:50:03 2009 -0800

    Enable actual (camera) video-only recording using h.263 or mpeg4 encoding.

commit 3fd59c3526a37fe7c696f4a978925d1831c09313
Author: Andreas Huber <andih@google.com>
Date:   Tue Nov 10 14:57:48 2009 -0800

    Allow switching between the PV recorder implementation and one supported by stagefright.

    This is controlled through the property "media.stagefright.enable-record".
diff --git a/media/libstagefright/CameraSource.cpp b/media/libstagefright/CameraSource.cpp
index e9d8557..bd862e0 100644
--- a/media/libstagefright/CameraSource.cpp
+++ b/media/libstagefright/CameraSource.cpp
@@ -19,6 +19,7 @@
 #include <OMX_Component.h>
 
 #include <binder/IServiceManager.h>
+#include <cutils/properties.h> // for property_get
 #include <media/stagefright/CameraSource.h>
 #include <media/stagefright/MediaDebug.h>
 #include <media/stagefright/MediaDefs.h>
@@ -123,6 +124,17 @@
     return new CameraSource(camera);
 }
 
+// static
+CameraSource *CameraSource::CreateFromICamera(const sp<ICamera> &icamera) {
+    sp<Camera> camera = Camera::create(icamera);
+
+    if (camera.get() == NULL) {
+        return NULL;
+    }
+
+    return new CameraSource(camera);
+}
+
 CameraSource::CameraSource(const sp<Camera> &camera)
     : mCamera(camera),
       mWidth(0),
@@ -130,6 +142,13 @@
       mFirstFrameTimeUs(0),
       mNumFrames(0),
       mStarted(false) {
+    char value[PROPERTY_VALUE_MAX];
+    if (property_get("ro.hardware", value, NULL) && !strcmp(value, "sholes")) {
+        // The hardware encoder(s) do not support yuv420, but only YCbYCr,
+        // fortunately the camera also supports this, so we needn't transcode.
+        mCamera->setParameters(String8("preview-format=yuv422i-yuyv"));
+    }
+
     String8 s = mCamera->getParameters();
     printf("params: \"%s\"\n", s.string());
 
@@ -143,13 +162,18 @@
     }
 }
 
+void CameraSource::setPreviewSurface(const sp<ISurface> &surface) {
+    mPreviewSurface = surface;
+}
+
 status_t CameraSource::start(MetaData *) {
     CHECK(!mStarted);
 
     mCamera->setListener(new CameraSourceListener(this));
 
-    sp<ISurface> dummy = new DummySurface;
-    status_t err = mCamera->setPreviewDisplay(dummy);
+    status_t err =
+        mCamera->setPreviewDisplay(
+                mPreviewSurface != NULL ? mPreviewSurface : new DummySurface);
     CHECK_EQ(err, OK);
 
     mCamera->setPreviewCallbackFlags(
diff --git a/media/libstagefright/MPEG4Writer.cpp b/media/libstagefright/MPEG4Writer.cpp
index 9a7a873..367459f 100644
--- a/media/libstagefright/MPEG4Writer.cpp
+++ b/media/libstagefright/MPEG4Writer.cpp
@@ -75,6 +75,13 @@
     CHECK(mFile != NULL);
 }
 
+MPEG4Writer::MPEG4Writer(int fd)
+    : mFile(fdopen(fd, "wb")),
+      mOffset(0),
+      mMdatOffset(0) {
+    CHECK(mFile != NULL);
+}
+
 MPEG4Writer::~MPEG4Writer() {
     stop();
 
@@ -203,6 +210,27 @@
     return old_offset;
 }
 
+off_t MPEG4Writer::addLengthPrefixedSample(MediaBuffer *buffer) {
+    Mutex::Autolock autoLock(mLock);
+
+    off_t old_offset = mOffset;
+
+    size_t length = buffer->range_length();
+    CHECK(length < 65536);
+
+    uint8_t x = length >> 8;
+    fwrite(&x, 1, 1, mFile);
+    x = length & 0xff;
+    fwrite(&x, 1, 1, mFile);
+
+    fwrite((const uint8_t *)buffer->data() + buffer->range_offset(),
+           1, length, mFile);
+
+    mOffset += length + 2;
+
+    return old_offset;
+}
+
 void MPEG4Writer::beginBox(const char *fourcc) {
     CHECK_EQ(strlen(fourcc), 4);
 
@@ -348,11 +376,12 @@
 }
 
 void MPEG4Writer::Track::threadEntry() {
-    bool is_mpeg4 = false;
     sp<MetaData> meta = mSource->getFormat();
     const char *mime;
     meta->findCString(kKeyMIMEType, &mime);
-    is_mpeg4 = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4);
+    bool is_mpeg4 = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4);
+    bool is_avc = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC);
+    int32_t count = 0;
 
     MediaBuffer *buffer;
     while (!mDone && mSource->read(&buffer) == OK) {
@@ -363,6 +392,55 @@
             continue;
         }
 
+        ++count;
+
+        if (is_avc && count < 3) {
+            size_t size = buffer->range_length();
+
+            switch (count) {
+                case 1:
+                {
+                    CHECK_EQ(mCodecSpecificData, NULL);
+                    mCodecSpecificData = malloc(size + 8);
+                    uint8_t *header = (uint8_t *)mCodecSpecificData;
+                    header[0] = 1;
+                    header[1] = 0x42;  // profile
+                    header[2] = 0x80;
+                    header[3] = 0x1e;  // level
+                    header[4] = 0xfc | 3;
+                    header[5] = 0xe0 | 1;
+                    header[6] = size >> 8;
+                    header[7] = size & 0xff;
+                    memcpy(&header[8],
+                            (const uint8_t *)buffer->data() + buffer->range_offset(),
+                            size);
+
+                    mCodecSpecificDataSize = size + 8;
+                    break;
+                }
+
+                case 2:
+                {
+                    size_t offset = mCodecSpecificDataSize;
+                    mCodecSpecificDataSize += size + 3;
+                    mCodecSpecificData = realloc(mCodecSpecificData, mCodecSpecificDataSize);
+                    uint8_t *header = (uint8_t *)mCodecSpecificData;
+                    header[offset] = 1;
+                    header[offset + 1] = size >> 8;
+                    header[offset + 2] = size & 0xff;
+                    memcpy(&header[offset + 3],
+                            (const uint8_t *)buffer->data() + buffer->range_offset(),
+                            size);
+                    break;
+                }
+            }
+
+            buffer->release();
+            buffer = NULL;
+
+            continue;
+        }
+
         if (mCodecSpecificData == NULL && is_mpeg4) {
             const uint8_t *data =
                 (const uint8_t *)buffer->data() + buffer->range_offset();
@@ -393,10 +471,11 @@
             buffer->set_range(buffer->range_offset() + offset, size - offset);
         }
 
-        off_t offset = mOwner->addSample(buffer);
+        off_t offset = is_avc ? mOwner->addLengthPrefixedSample(buffer)
+                              : mOwner->addSample(buffer);
 
         SampleInfo info;
-        info.size = buffer->range_length();
+        info.size = is_avc ? buffer->range_length() + 2 : buffer->range_length();
         info.offset = offset;
 
         int64_t timestampUs;
@@ -556,6 +635,8 @@
                     mOwner->beginBox("mp4v");
                 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
                     mOwner->beginBox("s263");
+                } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
+                    mOwner->beginBox("avc1");
                 } else {
                     LOGE("Unknown mime type '%s'.", mime);
                     CHECK(!"should not be here, unknown mime type.");
@@ -631,8 +712,13 @@
                           mOwner->writeInt8(0);   // profile: 0
 
                       mOwner->endBox();  // d263
+                  } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
+                      mOwner->beginBox("avcC");
+                        mOwner->write(mCodecSpecificData, mCodecSpecificDataSize);
+                      mOwner->endBox();  // avcC
                   }
-                mOwner->endBox();  // mp4v or s263
+
+                mOwner->endBox();  // mp4v, s263 or avc1
             }
           mOwner->endBox();  // stsd
 
diff --git a/media/libstagefright/OMXCodec.cpp b/media/libstagefright/OMXCodec.cpp
index 6c0367a..d36653e 100644
--- a/media/libstagefright/OMXCodec.cpp
+++ b/media/libstagefright/OMXCodec.cpp
@@ -594,11 +594,9 @@
         CHECK(!"Should not be here. Not a supported video mime type.");
     }
 
-    OMX_COLOR_FORMATTYPE colorFormat =
-        0 ? OMX_COLOR_FormatYCbYCr : OMX_COLOR_FormatCbYCrY;
-
-    if (!strncmp("OMX.qcom.video.encoder.", mComponentName, 23)) {
-        colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
+    OMX_COLOR_FORMATTYPE colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
+    if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
+        colorFormat = OMX_COLOR_FormatYCbYCr;
     }
 
     CHECK_EQ(setVideoPortFormatType(
@@ -666,6 +664,12 @@
         case OMX_VIDEO_CodingH263:
             break;
 
+        case OMX_VIDEO_CodingAVC:
+        {
+            CHECK_EQ(setupAVCEncoderParameters(), OK);
+            break;
+        }
+
         default:
             CHECK(!"Support for this compressionFormat to be implemented.");
             break;
@@ -749,6 +753,64 @@
     return OK;
 }
 
+status_t OMXCodec::setupAVCEncoderParameters() {
+    OMX_VIDEO_PARAM_AVCTYPE h264type;
+    InitOMXParams(&h264type);
+    h264type.nPortIndex = kPortIndexOutput;
+
+    status_t err = mOMX->getParameter(
+            mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
+    CHECK_EQ(err, OK);
+
+    h264type.nAllowedPictureTypes =
+        OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
+
+    h264type.nSliceHeaderSpacing = 0;
+    h264type.nBFrames = 0;
+    h264type.bUseHadamard = OMX_TRUE;
+    h264type.nRefFrames = 1;
+    h264type.nRefIdx10ActiveMinus1 = 0;
+    h264type.nRefIdx11ActiveMinus1 = 0;
+    h264type.bEnableUEP = OMX_FALSE;
+    h264type.bEnableFMO = OMX_FALSE;
+    h264type.bEnableASO = OMX_FALSE;
+    h264type.bEnableRS = OMX_FALSE;
+    h264type.eProfile = OMX_VIDEO_AVCProfileBaseline;
+    h264type.eLevel = OMX_VIDEO_AVCLevel1b;
+    h264type.bFrameMBsOnly = OMX_TRUE;
+    h264type.bMBAFF = OMX_FALSE;
+    h264type.bEntropyCodingCABAC = OMX_FALSE;
+    h264type.bWeightedPPrediction = OMX_FALSE;
+    h264type.bconstIpred = OMX_FALSE;
+    h264type.bDirect8x8Inference = OMX_FALSE;
+    h264type.bDirectSpatialTemporal = OMX_FALSE;
+    h264type.nCabacInitIdc = 0;
+    h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
+
+    err = mOMX->setParameter(
+            mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
+    CHECK_EQ(err, OK);
+
+    OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
+    InitOMXParams(&bitrateType);
+    bitrateType.nPortIndex = kPortIndexOutput;
+
+    err = mOMX->getParameter(
+            mNode, OMX_IndexParamVideoBitrate,
+            &bitrateType, sizeof(bitrateType));
+    CHECK_EQ(err, OK);
+
+    bitrateType.eControlRate = OMX_Video_ControlRateVariable;
+    bitrateType.nTargetBitrate = 1000000;
+
+    err = mOMX->setParameter(
+            mNode, OMX_IndexParamVideoBitrate,
+            &bitrateType, sizeof(bitrateType));
+    CHECK_EQ(err, OK);
+
+    return OK;
+}
+
 void OMXCodec::setVideoOutputFormat(
         const char *mime, OMX_U32 width, OMX_U32 height) {
     CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
@@ -849,7 +911,6 @@
     CHECK_EQ(err, OK);
 }
 
-
 OMXCodec::OMXCodec(
         const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
         bool isEncoder,
@@ -1178,6 +1239,9 @@
                 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
                     buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
                 }
+                if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
+                    buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
+                }
 
                 buffer->meta_data()->setPointer(
                         kKeyPlatformPrivate,
@@ -1738,6 +1802,13 @@
     }
 
     info->mOwnedByComponent = true;
+
+    // This component does not ever signal the EOS flag on output buffers,
+    // Thanks for nothing.
+    if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
+        mNoMoreOutputData = true;
+        mBufferFilled.signal();
+    }
 }
 
 void OMXCodec::fillOutputBuffer(BufferInfo *info) {