diff --git a/libavcodec/libstagefright.cpp b/libavcodec/libstagefright.cpp
index caaf6f4590e0f1624ef593a8c6ada94fb18b6b12..e29a377311eb0deba33be63a8d8ae789821a1912 100644
--- a/libavcodec/libstagefright.cpp
+++ b/libavcodec/libstagefright.cpp
@@ -445,49 +445,49 @@ static av_cold int Stagefright_close(AVCodecContext *avctx)
     Frame *frame;
 
     if (s->thread_started) {
-    if (!s->thread_exited) {
-        s->stop_decode = 1;
+        if (!s->thread_exited) {
+            s->stop_decode = 1;
 
-        // Make sure decode_thread() doesn't get stuck
-        pthread_mutex_lock(&s->out_mutex);
-        while (!s->out_queue->empty()) {
-            frame = *s->out_queue->begin();
-            s->out_queue->erase(s->out_queue->begin());
-            if (frame->size)
-                frame->mbuffer->release();
-            av_freep(&frame);
-        }
-        pthread_mutex_unlock(&s->out_mutex);
+            // Make sure decode_thread() doesn't get stuck
+            pthread_mutex_lock(&s->out_mutex);
+            while (!s->out_queue->empty()) {
+                frame = *s->out_queue->begin();
+                s->out_queue->erase(s->out_queue->begin());
+                if (frame->size)
+                    frame->mbuffer->release();
+                av_freep(&frame);
+            }
+            pthread_mutex_unlock(&s->out_mutex);
+
+            // Feed a dummy frame prior to signalling EOF.
+            // This is required to terminate the decoder(OMX.SEC)
+            // when only one frame is read during stream info detection.
+            if (s->dummy_buf && (frame = (Frame*)av_mallocz(sizeof(Frame)))) {
+                frame->status = OK;
+                frame->size   = s->dummy_bufsize;
+                frame->key    = 1;
+                frame->buffer = s->dummy_buf;
+                pthread_mutex_lock(&s->in_mutex);
+                s->in_queue->push_back(frame);
+                pthread_cond_signal(&s->condition);
+                pthread_mutex_unlock(&s->in_mutex);
+                s->dummy_buf = NULL;
+            }
 
-        // Feed a dummy frame prior to signalling EOF.
-        // This is required to terminate the decoder(OMX.SEC)
-        // when only one frame is read during stream info detection.
-        if (s->dummy_buf && (frame = (Frame*)av_mallocz(sizeof(Frame)))) {
-            frame->status = OK;
-            frame->size   = s->dummy_bufsize;
-            frame->key    = 1;
-            frame->buffer = s->dummy_buf;
             pthread_mutex_lock(&s->in_mutex);
-            s->in_queue->push_back(frame);
+            s->end_frame->status = ERROR_END_OF_STREAM;
+            s->in_queue->push_back(s->end_frame);
             pthread_cond_signal(&s->condition);
             pthread_mutex_unlock(&s->in_mutex);
-            s->dummy_buf = NULL;
+            s->end_frame = NULL;
         }
 
-        pthread_mutex_lock(&s->in_mutex);
-        s->end_frame->status = ERROR_END_OF_STREAM;
-        s->in_queue->push_back(s->end_frame);
-        pthread_cond_signal(&s->condition);
-        pthread_mutex_unlock(&s->in_mutex);
-        s->end_frame = NULL;
-    }
-
-    pthread_join(s->decode_thread_id, NULL);
+        pthread_join(s->decode_thread_id, NULL);
 
-    if (s->ret_frame.data[0])
-        avctx->release_buffer(avctx, &s->ret_frame);
+        if (s->ret_frame.data[0])
+            avctx->release_buffer(avctx, &s->ret_frame);
 
-    s->thread_started = false;
+        s->thread_started = false;
     }
 
     while (!s->in_queue->empty()) {
diff --git a/tools/build_libstagefright b/tools/build_libstagefright
index a228487fb59c282899b7ce0ab659943d3fa47465..ada8d4e23933ded74ce06bf39ece6e23b150aace 100644
--- a/tools/build_libstagefright
+++ b/tools/build_libstagefright
@@ -5,12 +5,27 @@ if [ "$NDK" = "" ]; then
     export NDK=${HOME}/android-ndk
 fi
 
+echo "Fetching Android system headers"
+git clone --depth=1 git://github.com/CyanogenMod/android_frameworks_base.git ../android-source/frameworks/base
+git clone --depth=1 git://github.com/CyanogenMod/android_system_core.git ../android-source/system/core
+
+echo "Fetching Android libraries for linking"
+# Libraries from any froyo/gingerbread device/emulator should work
+# fine, since the symbols used should be available on most of them.
+if [ ! -f "../update-cm-7.0.3-N1-signed.zip" ]; then
+    wget http://download.cyanogenmod.com/get/update-cm-7.0.3-N1-signed.zip -P../
+    unzip update-cm-7.0.3-N1-signed.zip system/lib/* -d../
+    mv ../system/lib ../android-libs
+    rmdir ../system
+fi
+
+
 SYSROOT=$NDK/platforms/android-9/arch-arm
 # Expand the prebuilt/* path into the correct one
 TOOLCHAIN=`echo $NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/*-x86`
 export PATH=$TOOLCHAIN/bin:$PATH
-ANDROID_SOURCE=$HOME/android
-ANDROID_LIBS=$HOME/glib
+ANDROID_SOURCE=../android-source
+ANDROID_LIBS=../android-libs
 
 rm -rf ../build/stagefright
 mkdir -p ../build/stagefright