Code drop from //branches/cupcake/...@124589
diff --git a/Android.mk b/Android.mk
index 9cfe4f6..290a027 100644
--- a/Android.mk
+++ b/Android.mk
@@ -12,7 +12,7 @@
 	jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
 	jfdctint.c jidctflt.c jidctred.c jquant1.c \
 	jquant2.c jutils.c jmemmgr.c \
-	jmemnobs.c
+	jmem-android.c
 
 # the assembler is only for the ARM version, don't break the Linux sim
 ifneq ($(TARGET_ARCH),arm)
diff --git a/jidctfst.S b/jidctfst.S
index 88fb661..34e1c24 100644
--- a/jidctfst.S
+++ b/jidctfst.S
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <machine/cpu-features.h>
 
     .text
     .align
@@ -29,10 +30,6 @@
 //                 unsigned char*          output_buf,
 //                 int                     output_col)
 
-#if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__)
-#define ARMv6 1
-#endif
-
 #define  local_TMP0123       sp
 #define  local_TMP0          [sp, #0]
 #define  local_TMP1          [sp, #4]
@@ -66,7 +63,7 @@
 
 
 jpeg_idct_ifast:
-    pld     [r2, #0]
+    PLD     (r2, #0)
     stmdb   sp!, {r4,r5, r6,r7, r8,r9, r10,r11, r12,lr}
     ldr     r4, [sp, #4*10]
     sub     sp, #local_SIZE
@@ -106,13 +103,25 @@
 
     ldr      r9, [r10, #QY(4)]
     ldr      r8, [r10, #QY(0)]
+#if __ARM_HAVE_HALFWORD_MULTIPLY
     smulbb   r4, r9, r4
     smlabb   r0, r8, r0, r4
+#else
+    mul      r4, r9, r4
+    mul      r0, r8, r0
+    add      r0, r4
+#endif
     ldr      r9, [r10, #QY(6)]
     ldr      r8, [r10, #QY(2)]
     sub      r4, r0, r4, lsl #1
+#if __ARM_HAVE_HALFWORD_MULTIPLY
     smulbb   r6, r9, r6
     smlabb   r2, r8, r2, r6
+#else
+    mul      r6, r9, r6
+    mul      r2, r8, r2
+    add      r2, r6
+#endif
 
     // tmp13 = tmp1 + tmp3                                       (r2)
     // tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13    (r6)
@@ -148,12 +157,27 @@
 
     ldr     r2, [r10, #QY(1)]
     ldr     r9, [r10, #QY(5)]
+#if __ARM_HAVE_HALFWORD_MULTIPLY
     smulbb  r1, r2, r1
+#else
+    mul     r1, r2, r1
+#endif
     ldr     r2, [r10, #QY(3)]
+#if __ARM_HAVE_HALFWORD_MULTIPLY
     smulbb  r5, r9, r5
+#else
+    mul     r5, r9, r5
+#endif
     ldr     r9, [r10, #QY(7)]
+#if __ARM_HAVE_HALFWORD_MULTIPLY
     smlabb  r0, r2, r3, r5
     smlabb  r4, r9, r7, r1
+#else
+    mul     r0, r2, r3
+    add     r0, r5
+    mul     r4, r9, r7
+    add     r4, r1
+#endif
     rsb  r2, r0, r5, lsl #1
     rsb  r6, r4, r1, lsl #1
 
@@ -232,7 +256,7 @@
 
 HLoopStart:
     // reset pointers
-    pld     [sp, #off_WORKSPACE]
+    PLD     (sp, #off_WORKSPACE)
     add     ip, sp, #off_WORKSPACE
     ldr     r10, local_RANGE_TABLE
 
@@ -244,7 +268,7 @@
     str      r0, local_OUTPUT_BUF
     add      fp, r2, r1
 
-    pld      [ip, #32]
+    PLD      (ip, #32)
     ldmia    ip!, {r0-r7}
 
     cmp      r1, #0
@@ -352,7 +376,7 @@
     add    r3, r8, r3, asr #5
     add    r4, r8, r4, asr #5
 
-#ifdef ARMv6
+#if __ARM_ARCH__ >= 6
     usat   r0, #8, r0
     usat   r1, #8, r1
     usat   r2, #8, r2
@@ -431,7 +455,7 @@
     mov      r0, r0, asr #5
     add      r0, #128
 
-#ifdef ARMv6
+#if __ARM_ARCH__ >= 6
     usat     r0, #8, r0
 #else
     cmp      r0, #255
diff --git a/jmem-android.c b/jmem-android.c
new file mode 100644
index 0000000..5da4d0a
--- /dev/null
+++ b/jmem-android.c
@@ -0,0 +1,186 @@
+/*
+ * Copyright (C) 2007-2008 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 JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jmemsys.h"		/* import the system-dependent declarations */
+
+#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare malloc(),free() */
+extern void * malloc JPP((size_t size));
+extern void free JPP((void *ptr));
+#endif
+
+#ifndef SEEK_SET		/* pre-ANSI systems may not define this; */
+#define SEEK_SET  0		/* if not, assume 0 is correct */
+#endif
+
+
+/*
+ * Memory allocation and freeing are controlled by the regular library
+ * routines malloc() and free().
+ */
+
+GLOBAL(void *)
+jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
+{
+  return (void *) malloc(sizeofobject);
+}
+
+GLOBAL(void)
+jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
+{
+  free(object);
+}
+
+
+/*
+ * "Large" objects are treated the same as "small" ones.
+ * NB: although we include FAR keywords in the routine declarations,
+ * this file won't actually work in 80x86 small/medium model; at least,
+ * you probably won't be able to process useful-size images in only 64KB.
+ */
+
+GLOBAL(void FAR *)
+jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
+{
+  return (void FAR *) malloc(sizeofobject);
+}
+
+GLOBAL(void)
+jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
+{
+  free(object);
+}
+
+
+/*
+ * This routine computes the total memory space available for allocation.
+ * It's impossible to do this in a portable way; our current solution is
+ * to make the user tell us (with a default value set at compile time).
+ * If you can actually get the available space, it's a good idea to subtract
+ * a slop factor of 5% or so.
+ */
+
+#ifndef DEFAULT_MAX_MEM		/* so can override from makefile */
+#define DEFAULT_MAX_MEM		10000000L /* default: ten megabyte */
+#endif
+
+GLOBAL(long)
+jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
+		    long max_bytes_needed, long already_allocated)
+{
+  return cinfo->mem->max_memory_to_use - already_allocated;
+}
+
+
+/*
+ * Backing store (temporary file) management.
+ * Backing store objects are only used when the value returned by
+ * jpeg_mem_available is less than the total space needed.  You can dispense
+ * with these routines if you have plenty of virtual memory; see jmemnobs.c.
+ */
+
+
+METHODDEF(void)
+read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+		    void FAR * buffer_address,
+		    long file_offset, long byte_count)
+{
+  if (fseek(info->temp_file, file_offset, SEEK_SET))
+    ERREXIT(cinfo, JERR_TFILE_SEEK);
+  if (JFREAD(info->temp_file, buffer_address, byte_count)
+      != (size_t) byte_count)
+    ERREXIT(cinfo, JERR_TFILE_READ);
+}
+
+
+METHODDEF(void)
+write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+		     void FAR * buffer_address,
+		     long file_offset, long byte_count)
+{
+  if (fseek(info->temp_file, file_offset, SEEK_SET))
+    ERREXIT(cinfo, JERR_TFILE_SEEK);
+  if (JFWRITE(info->temp_file, buffer_address, byte_count)
+      != (size_t) byte_count)
+    ERREXIT(cinfo, JERR_TFILE_WRITE);
+}
+
+
+METHODDEF(void)
+close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
+{
+  fclose(info->temp_file);
+  /* Since this implementation uses tmpfile() to create the file,
+   * no explicit file deletion is needed.
+   */
+}
+
+static FILE* getTempFileFromPath(const char * path) {
+    FILE * fd = fopen(path, "w+");
+    unlink(path);
+    return fd;
+}
+
+static FILE* getTempFile() {
+    char path[1024];
+    snprintf(path, 1023, "/sdcard/.%d.tmp", getpid());
+    FILE * fd = getTempFileFromPath(path);
+    if (fd == NULL) {
+        // anywhere else we can create a temp file?
+		//	    snprintf(path, 1023, "/data/data/.%d.tmp", getpid());
+		//      fd = getTempFileFromPath(path);
+    }
+    return fd;
+}
+
+/*
+ * Initial opening of a backing-store object.
+ *
+ * This version uses tmpfile(), which constructs a suitable file name
+ * behind the scenes.  We don't have to use info->temp_name[] at all;
+ * indeed, we can't even find out the actual name of the temp file.
+ */
+
+GLOBAL(void)
+jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+			 long total_bytes_needed)
+{
+  if ((info->temp_file = getTempFile()) == NULL)
+    ERREXITS(cinfo, JERR_TFILE_CREATE, "");
+  info->read_backing_store = read_backing_store;
+  info->write_backing_store = write_backing_store;
+  info->close_backing_store = close_backing_store;
+}
+
+
+/*
+ * These routines take care of any system-dependent initialization and
+ * cleanup required.
+ */
+
+GLOBAL(long)
+jpeg_mem_init (j_common_ptr cinfo)
+{
+  return DEFAULT_MAX_MEM;	/* default for max_memory_to_use */
+}
+
+GLOBAL(void)
+jpeg_mem_term (j_common_ptr cinfo)
+{
+  /* no work */
+}