Merge "Add new scaling mode parameter in Visualizer effect"
diff --git a/camera/include/system/camera_metadata.h b/camera/include/system/camera_metadata.h
index 424fc0f..6a11cfb 100644
--- a/camera/include/system/camera_metadata.h
+++ b/camera/include/system/camera_metadata.h
@@ -208,7 +208,8 @@
 /**
  * Append camera metadata in src to an existing metadata structure in dst.  This
  * does not resize the destination structure, so if it is too small, a non-zero
- * value is returned. On success, 0 is returned.
+ * value is returned. On success, 0 is returned. Appending onto a sorted
+ * structure results in a non-sorted combined structure.
  */
 ANDROID_API
 int append_camera_metadata(camera_metadata_t *dst, const camera_metadata_t *src);
@@ -229,7 +230,9 @@
  * left to add the entry, or if the tag is unknown.  data_count is the number of
  * entries in the data array of the tag's type, not a count of
  * bytes. Vendor-defined tags can not be added using this method, unless
- * set_vendor_tag_query_ops() has been called first.
+ * set_vendor_tag_query_ops() has been called first. Entries are always added to
+ * the end of the structure (highest index), so after addition, a
+ * previously-sorted array will be marked as unsorted.
  */
 ANDROID_API
 int add_camera_metadata_entry(camera_metadata_t *dst,
@@ -238,13 +241,22 @@
         size_t data_count);
 
 /**
+ * Sort the metadata buffer for fast searching. If already sorted, does
+ * nothing. Adding or appending entries to the buffer will place the buffer back
+ * into an unsorted state.
+ */
+ANDROID_API
+int sort_camera_metadata(camera_metadata_t *dst);
+
+/**
  * Get pointers to the fields for a metadata entry at position index in the
  * entry array.  The data pointer points either to the entry's data.value field
  * or to the right offset in camera_metadata_t.data. Returns 0 on
  * success. Data_count is the number of entries in the data array when cast to
  * the tag's type, not a count of bytes.
  *
- * src and index are inputs; tag, type, data, and data_count are outputs.
+ * src and index are inputs; tag, type, data, and data_count are outputs. Any of
+ * the outputs can be set to NULL to skip reading that value.
  */
 ANDROID_API
 int get_camera_metadata_entry(camera_metadata_t *src,
@@ -255,6 +267,22 @@
         size_t *data_count);
 
 /**
+ * Find an entry with given tag value. If not found, returns -ENOENT. Otherwise,
+ * returns entry contents like get_camera_metadata_entry. Any of
+ * the outputs can be set to NULL to skip reading that value.
+ *
+ * Note: Returns only the first entry with a given tag. To speed up searching
+ * for tags, sort the metadata structure first by calling
+ * sort_camera_metadata().
+ */
+ANDROID_API
+int find_camera_metadata_entry(camera_metadata_t *src,
+        uint32_t tag,
+        uint8_t *type,
+        void **data,
+        size_t *data_count);
+
+/**
  * Retrieve human-readable name of section the tag is in. Returns NULL if
  * no such tag is defined. Returns NULL for tags in the vendor section, unless
  * set_vendor_tag_query_ops() has been used.
@@ -284,7 +312,8 @@
  * get_camera_metadata_section_name, _tag_name, and _tag_type methods with
  * vendor tags. Returns 0 on success.
  */
-typedef struct vendor_tag_query_ops {
+typedef struct vendor_tag_query_ops vendor_tag_query_ops_t;
+struct vendor_tag_query_ops {
     /**
      * Get vendor section name for a vendor-specified entry tag. Only called for
      * tags >= 0x80000000. The section name must start with the name of the
@@ -292,20 +321,26 @@
      * their sections with "com.camerazoom." Must return NULL if the tag is
      * outside the bounds of vendor-defined sections.
      */
-    const char *(*get_camera_vendor_section_name)(uint32_t tag);
+    const char *(*get_camera_vendor_section_name)(
+        const vendor_tag_query_ops_t *v,
+        uint32_t tag);
     /**
      * Get tag name for a vendor-specified entry tag. Only called for tags >=
      * 0x80000000. Must return NULL if the tag is outside the bounds of
      * vendor-defined sections.
      */
-    const char *(*get_camera_vendor_tag_name)(uint32_t tag);
+    const char *(*get_camera_vendor_tag_name)(
+        const vendor_tag_query_ops_t *v,
+        uint32_t tag);
     /**
      * Get tag type for a vendor-specified entry tag. Only called for tags >=
      * 0x80000000. Must return -1 if the tag is outside the bounds of
      * vendor-defined sections.
      */
-    int         (*get_camera_vendor_tag_type)(uint32_t tag);
-} vendor_tag_query_ops_t;
+    int (*get_camera_vendor_tag_type)(
+        const vendor_tag_query_ops_t *v,
+        uint32_t tag);
+};
 
 ANDROID_API
 int set_camera_metadata_vendor_tag_ops(const vendor_tag_query_ops_t *query_ops);
@@ -318,6 +353,7 @@
  */
 ANDROID_API
 void dump_camera_metadata(const camera_metadata_t *metadata,
+        int fd,
         int verbosity);
 
 #ifdef __cplusplus
diff --git a/camera/include/system/camera_metadata_tags.h b/camera/include/system/camera_metadata_tags.h
index 1fcfacb..bfb4fe3 100644
--- a/camera/include/system/camera_metadata_tags.h
+++ b/camera/include/system/camera_metadata_tags.h
@@ -142,7 +142,7 @@
 
     ANDROID_SENSOR_EXPOSURE_TIME_RANGE = ANDROID_SENSOR_INFO_START,
     ANDROID_SENSOR_MAX_FRAME_DURATION,
-    ANDROID_SENSOR_SENSITIVITY_RANGE,
+    ANDROID_SENSOR_AVAILABLE_SENSITIVITIES,
     ANDROID_SENSOR_COLOR_FILTER_ARRANGEMENT,
     ANDROID_SENSOR_PIXEL_ARRAY_SIZE,
     ANDROID_SENSOR_ACTIVE_ARRAY_SIZE,
@@ -245,13 +245,13 @@
     ANDROID_JPEG_INFO_END,
 
     ANDROID_STATS_FACE_DETECT_MODE = ANDROID_STATS_START,
-    ANDROID_STATS_HISTOGRAM_MODE,
-    ANDROID_STATS_SHARPNESS_MAP_MODE,
     ANDROID_STATS_FACE_RECTANGLES,
     ANDROID_STATS_FACE_SCORES,
     ANDROID_STATS_FACE_LANDMARKS,
     ANDROID_STATS_FACE_IDS,
+    ANDROID_STATS_HISTOGRAM_MODE,
     ANDROID_STATS_HISTOGRAM,
+    ANDROID_STATS_SHARPNESS_MAP_MODE,
     ANDROID_STATS_SHARPNESS_MAP,
     ANDROID_STATS_END,
 
@@ -289,3 +289,25 @@
     ANDROID_CONTROL_AF_AVAILABLE_MODES,
     ANDROID_CONTROL_INFO_END
 };
+
+/**
+ * Enumeration definitions for the various entries that need them
+ */
+
+// ANDROID_REQUEST_METADATA_MODE
+enum {
+    ANDROID_REQUEST_METADATA_NONE = 0,
+    ANDROID_REQUEST_METADATA_FULL
+};
+
+// ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT
+enum {
+    ANDROID_SENSOR_RGGB = 0,
+    ANDROID_SENSOR_GRBG,
+    ANDROID_SENSOR_GBRG,
+    ANDROID_SENSOR_BGGR,
+    ANDROID_SENSOR_RGB
+};
+
+// ANDROID_SCALER_AVAILABLE_FORMATS uses HAL_PIXEL_FORMAT_* from
+// system/graphics.h
diff --git a/camera/src/camera_metadata.c b/camera/src/camera_metadata.c
index a481bec..1991835 100644
--- a/camera/src/camera_metadata.c
+++ b/camera/src/camera_metadata.c
@@ -13,13 +13,16 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
+#define _GNU_SOURCE // for fdprintf
 #include <system/camera_metadata.h>
 #include <cutils/log.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
 
-#define OK    0
-#define ERROR 1
-
+#define OK         0
+#define ERROR      1
+#define NOT_FOUND -ENOENT
 /**
  * A single metadata entry, storing an array of values of a given type. If the
  * array is no larger than 4 bytes in size, it is stored in the data.value[]
@@ -73,6 +76,8 @@
  */
 struct camera_metadata {
     size_t                   size;
+    uint32_t                 version;
+    uint32_t                 flags;
     size_t                   entry_count;
     size_t                   entry_capacity;
     camera_metadata_entry_t *entries;
@@ -82,6 +87,14 @@
     uint8_t                  reserved[0];
 };
 
+/** Versioning information */
+#define CURRENT_METADATA_VERSION 1
+
+/** Flag definitions */
+#define FLAG_SORTED 0x00000001
+
+/** Tag information */
+
 typedef struct tag_info {
     const char *tag_name;
     uint8_t     tag_type;
@@ -128,6 +141,8 @@
     if (memory_needed > dst_size) return NULL;
 
     camera_metadata_t *metadata = (camera_metadata_t*)dst;
+    metadata->version = CURRENT_METADATA_VERSION;
+    metadata->flags = 0;
     metadata->entry_count = 0;
     metadata->entry_capacity = entry_capacity;
     metadata->entries = (camera_metadata_entry_t*)(metadata + 1);
@@ -202,6 +217,8 @@
                                            src->data_capacity);
 
     camera_metadata_t *metadata = (camera_metadata_t*)dst;
+    metadata->version = CURRENT_METADATA_VERSION;
+    metadata->flags = src->flags;
     metadata->entry_count = src->entry_count;
     metadata->entry_capacity = src->entry_count;
     metadata->entries = (camera_metadata_entry_t*)
@@ -246,6 +263,7 @@
     }
     dst->entry_count += src->entry_count;
     dst->data_count += src->data_count;
+    dst->flags &= ~FLAG_SORTED;
 
     return OK;
 }
@@ -258,7 +276,7 @@
     return data_bytes <= 4 ? 0 : data_bytes;
 }
 
-int add_camera_metadata_entry_raw(camera_metadata_t *dst,
+static int add_camera_metadata_entry_raw(camera_metadata_t *dst,
         uint32_t tag,
         uint8_t  type,
         const void *data,
@@ -285,6 +303,7 @@
         dst->data_count += data_bytes;
     }
     dst->entry_count++;
+    dst->flags &= ~FLAG_SORTED;
     return OK;
 }
 
@@ -295,7 +314,7 @@
 
     int type = get_camera_metadata_tag_type(tag);
     if (type == -1) {
-        ALOGE("Unknown tag %04x (can't find type)", tag);
+        ALOGE("%s: Unknown tag %04x.", __FUNCTION__, tag);
         return ERROR;
     }
 
@@ -306,6 +325,26 @@
             data_count);
 }
 
+static int compare_entry_tags(const void *p1, const void *p2) {
+    uint32_t tag1 = ((camera_metadata_entry_t*)p1)->tag;
+    uint32_t tag2 = ((camera_metadata_entry_t*)p2)->tag;
+    return  tag1 < tag2 ? -1 :
+            tag1 == tag2 ? 0 :
+            1;
+}
+
+int sort_camera_metadata(camera_metadata_t *dst) {
+    if (dst == NULL) return ERROR;
+    if (dst->flags & FLAG_SORTED) return OK;
+
+    qsort(dst->entries, dst->entry_count,
+            sizeof(camera_metadata_entry_t),
+            compare_entry_tags);
+    dst->flags |= FLAG_SORTED;
+
+    return OK;
+}
+
 int get_camera_metadata_entry(camera_metadata_t *src,
         uint32_t index,
         uint32_t *tag,
@@ -313,22 +352,60 @@
         void **data,
         size_t *data_count) {
     if (src == NULL ) return ERROR;
-    if (tag == NULL) return ERROR;
-    if (type == NULL ) return ERROR;
-    if (data == NULL) return ERROR;
-    if (data_count == NULL) return ERROR;
-
     if (index >= src->entry_count) return ERROR;
 
     camera_metadata_entry_t *entry = src->entries + index;
 
-    *tag = entry->tag;
-    *type = entry->type;
-    *data_count = entry->count;
-    if (entry->count * camera_metadata_type_size[entry->type] > 4) {
-        *data = src->data + entry->data.offset;
+    if (tag != NULL) *tag = entry->tag;
+    if (type != NULL) *type = entry->type;
+    if (data_count != NULL) *data_count = entry->count;
+    if (data != NULL) {
+        if (entry->count * camera_metadata_type_size[entry->type] > 4) {
+            *data = src->data + entry->data.offset;
+        } else {
+            *data = entry->data.value;
+        }
+    }
+    return OK;
+}
+
+int find_camera_metadata_entry(camera_metadata_t *src,
+        uint32_t tag,
+        uint8_t *type,
+        void **data,
+        size_t *data_count) {
+    if (src == NULL) return ERROR;
+
+    camera_metadata_entry_t *entry = NULL;
+    if (src->flags & FLAG_SORTED) {
+        // Sorted entries, do a binary search
+        camera_metadata_entry_t key;
+        key.tag = tag;
+        entry = bsearch(&key,
+                src->entries,
+                src->entry_count,
+                sizeof(camera_metadata_entry_t),
+                compare_entry_tags);
     } else {
-        *data = entry->data.value;
+        // Not sorted, linear search
+        unsigned int i;
+        for (i = 0; i < src->entry_count; i++) {
+            if (src->entries[i].tag == tag) {
+                entry = src->entries + i;
+                break;
+            }
+        }
+    }
+    if (entry == NULL) return NOT_FOUND;
+
+    if (type != NULL) *type = entry->type;
+    if (data_count != NULL) *data_count = entry->count;
+    if (data != NULL) {
+        if (entry->count * camera_metadata_type_size[entry->type] > 4) {
+            *data = src->data + entry->data.offset;
+        } else {
+            *data = entry->data.value;
+        }
     }
     return OK;
 }
@@ -338,7 +415,9 @@
 const char *get_camera_metadata_section_name(uint32_t tag) {
     uint32_t tag_section = tag >> 16;
     if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
-        return vendor_tag_ops->get_camera_vendor_section_name(tag);
+        return vendor_tag_ops->get_camera_vendor_section_name(
+            vendor_tag_ops,
+            tag);
     }
     if (tag_section >= ANDROID_SECTION_COUNT) {
         return NULL;
@@ -349,7 +428,9 @@
 const char *get_camera_metadata_tag_name(uint32_t tag) {
     uint32_t tag_section = tag >> 16;
     if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
-        return vendor_tag_ops->get_camera_vendor_tag_name(tag);
+        return vendor_tag_ops->get_camera_vendor_tag_name(
+            vendor_tag_ops,
+            tag);
     }
     if (tag_section >= ANDROID_SECTION_COUNT ||
         tag >= camera_metadata_section_bounds[tag_section][1] ) {
@@ -362,7 +443,9 @@
 int get_camera_metadata_tag_type(uint32_t tag) {
     uint32_t tag_section = tag >> 16;
     if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
-        return vendor_tag_ops->get_camera_vendor_tag_type(tag);
+        return vendor_tag_ops->get_camera_vendor_tag_type(
+            vendor_tag_ops,
+            tag);
     }
     if (tag_section >= ANDROID_SECTION_COUNT ||
             tag >= camera_metadata_section_bounds[tag_section][1] ) {
@@ -377,18 +460,24 @@
     return OK;
 }
 
-void print_data(const uint8_t *data_ptr, int type, int count);
+static void print_data(int fd, const uint8_t *data_ptr, int type, int count);
 
-void dump_camera_metadata(const camera_metadata_t *metadata, int verbosity) {
+void dump_camera_metadata(const camera_metadata_t *metadata,
+        int fd,
+        int verbosity) {
     if (metadata == NULL) {
-        ALOGE("Metadata is null.");
+        ALOGE("%s: Metadata is null.", __FUNCTION__);
         return;
     }
     unsigned int i;
-    ALOGD("Dumping camera metadata array. %d entries, %d bytes of extra data.",
+    fdprintf(fd,
+            "Dumping camera metadata array. %d entries, "
+            "%d bytes of extra data.\n",
             metadata->entry_count, metadata->data_count);
-    ALOGD("  (%d entries and %d bytes data reserved)",
+    fdprintf(fd, "  (%d entries and %d bytes data reserved)\n",
             metadata->entry_capacity, metadata->data_capacity);
+    fdprintf(fd, "  Version: %d, Flags: %08x\n",
+            metadata->version, metadata->flags);
     for (i=0; i < metadata->entry_count; i++) {
         camera_metadata_entry_t *entry = metadata->entries + i;
 
@@ -407,7 +496,7 @@
         } else {
             type_name = camera_metadata_type_names[entry->type];
         }
-        ALOGD("Tag: %s.%s (%05x): %s[%d]",
+        fdprintf(fd, "Tag: %s.%s (%05x): %s[%d]\n",
              tag_section,
              tag_name,
              entry->tag,
@@ -422,9 +511,10 @@
         uint8_t *data_ptr;
         if ( type_size * entry->count > 4 ) {
             if (entry->data.offset >= metadata->data_count) {
-                ALOGE("Malformed entry data offset: %d (max %d)",
-                     entry->data.offset,
-                     metadata->data_count);
+                ALOGE("%s: Malformed entry data offset: %d (max %d)",
+                        __FUNCTION__,
+                        entry->data.offset,
+                        metadata->data_count);
                 continue;
             }
             data_ptr = metadata->data + entry->data.offset;
@@ -434,11 +524,11 @@
         int count = entry->count;
         if (verbosity < 2 && count > 16) count = 16;
 
-        print_data(data_ptr, entry->type, count);
+        print_data(fd, data_ptr, entry->type, count);
     }
 }
 
-void print_data(const uint8_t *data_ptr, int type, int count) {
+static void print_data(int fd, const uint8_t *data_ptr, int type, int count) {
     static int values_per_line[NUM_TYPES] = {
         [TYPE_BYTE]     = 16,
         [TYPE_INT32]    = 4,
@@ -452,49 +542,46 @@
     int lines = count / values_per_line[type];
     if (count % values_per_line[type] != 0) lines++;
 
-    char tmp1[80], tmp2[80];
-
     int index = 0;
     int j, k;
     for (j = 0; j < lines; j++) {
-        tmp1[0] = 0;
+        fdprintf(fd, " [");
         for (k = 0;
              k < values_per_line[type] && count > 0;
              k++, count--, index += type_size) {
 
             switch (type) {
                 case TYPE_BYTE:
-                    snprintf(tmp2, sizeof(tmp2), "%hhu ",
+                    fdprintf(fd, "%hhu ",
                             *(data_ptr + index));
                     break;
                 case TYPE_INT32:
-                    snprintf(tmp2, sizeof(tmp2), "%d ",
+                    fdprintf(fd, "%d ",
                             *(int32_t*)(data_ptr + index));
                     break;
                 case TYPE_FLOAT:
-                    snprintf(tmp2, sizeof(tmp2), "%0.2f ",
+                    fdprintf(fd, "%0.2f ",
                             *(float*)(data_ptr + index));
                     break;
                 case TYPE_INT64:
-                    snprintf(tmp2, sizeof(tmp2), "%lld ",
+                    fdprintf(fd, "%lld ",
                             *(int64_t*)(data_ptr + index));
                     break;
                 case TYPE_DOUBLE:
-                    snprintf(tmp2, sizeof(tmp2), "%0.2f ",
+                    fdprintf(fd, "%0.2f ",
                             *(float*)(data_ptr + index));
                     break;
                 case TYPE_RATIONAL: {
                     int32_t numerator = *(int32_t*)(data_ptr + index);
                     int32_t denominator = *(int32_t*)(data_ptr + index + 4);
-                    snprintf(tmp2, sizeof(tmp2), "(%d / %d) ",
+                    fdprintf(fd, "(%d / %d) ",
                             numerator, denominator);
                     break;
                 }
                 default:
-                    snprintf(tmp2, sizeof(tmp2), "??? ");
+                    fdprintf(fd, "??? ");
             }
-            strncat(tmp1, tmp2, sizeof(tmp1));
         }
-        ALOGD(" [ %s]", tmp1);
+        fdprintf(fd, "]\n");
     }
 }
diff --git a/camera/src/camera_metadata_tag_info.c b/camera/src/camera_metadata_tag_info.c
index 5d394e2..0481eb5 100644
--- a/camera/src/camera_metadata_tag_info.c
+++ b/camera/src/camera_metadata_tag_info.c
@@ -19,185 +19,245 @@
  *
  * It is logically a part of camera_metadata.c.  It is broken out for ease of
  * maintaining the tag info.
+ *
+ * Array assignments are done using specified-index syntax to keep things in
+ * sync with camera_metadata_tags.h
  */
 
 const char *camera_metadata_section_names[ANDROID_SECTION_COUNT] = {
-    "android.request",
-    "android.control",
-    "android.control.info",
-    "android.sensor",
-    "android.sensor.info",
-    "android.lens",
-    "android.lens.info",
-    "android.flash",
-    "android.flash.info",
-    "android.hotPixel",
-    "android.hotPixel.info",
-    "android.demosaic",
-    "android.demosaic.info",
-    "android.noiseReduction",
-    "android.noiseReduction.info",
-    "android.shadingCorrection",
-    "android.shadingCorrection.info",
-    "android.geometricCorrection",
-    "android.geometricCorrection.info",
-    "android.colorCorrection",
-    "android.colorCorrection.info",
-    "android.tonemap",
-    "android.tonemap.info",
-    "android.edge",
-    "android.edge.info",
-    "android.scaler",
-    "android.scaler.info",
-    "android.jpeg",
-    "android.jpeg.info",
-    "android.statistics",
-    "android.statistics.info"
+    [ANDROID_REQUEST]        = "android.request",
+    [ANDROID_LENS]           = "android.lens",
+    [ANDROID_LENS_INFO]      = "android.lens.info",
+    [ANDROID_SENSOR]         = "android.sensor",
+    [ANDROID_SENSOR_INFO]    = "android.sensor.info",
+    [ANDROID_FLASH]          = "android.flash",
+    [ANDROID_FLASH_INFO]     = "android.flash.info",
+    [ANDROID_HOT_PIXEL]      = "android.hotPixel",
+    [ANDROID_HOT_PIXEL_INFO] = "android.hotPixel.info",
+    [ANDROID_DEMOSAIC]       = "android.demosaic",
+    [ANDROID_DEMOSAIC_INFO]  = "android.demosaic.info",
+    [ANDROID_NOISE]          = "android.noiseReduction",
+    [ANDROID_NOISE_INFO]     = "android.noiseReduction.info",
+    [ANDROID_SHADING]        = "android.shadingCorrection",
+    [ANDROID_SHADING_INFO]   = "android.shadingCorrection.info",
+    [ANDROID_GEOMETRIC]      = "android.geometricCorrection",
+    [ANDROID_GEOMETRIC_INFO] = "android.geometricCorrection.info",
+    [ANDROID_COLOR]          = "android.colorCorrection",
+    [ANDROID_COLOR_INFO]     = "android.colorCorrection.info",
+    [ANDROID_TONEMAP]        = "android.tonemap",
+    [ANDROID_TONEMAP_INFO]   = "android.tonemap.info",
+    [ANDROID_EDGE]           = "android.edge",
+    [ANDROID_EDGE_INFO]      = "android.edge.info",
+    [ANDROID_SCALER]         = "android.scaler",
+    [ANDROID_SCALER_INFO]    = "android.scaler.info",
+    [ANDROID_JPEG]           = "android.jpeg",
+    [ANDROID_JPEG_INFO]      = "android.jpeg.info",
+    [ANDROID_STATS]          = "android.statistics",
+    [ANDROID_STATS_INFO]     = "android.statistics.info",
+    [ANDROID_CONTROL]        = "android.control",
+    [ANDROID_CONTROL_INFO]   = "android.control.info"
 };
 
 unsigned int camera_metadata_section_bounds[ANDROID_SECTION_COUNT][2] = {
-    { ANDROID_REQUEST_START,        ANDROID_REQUEST_END },
-    { ANDROID_LENS_START,           ANDROID_LENS_END },
-    { ANDROID_LENS_INFO_START,      ANDROID_LENS_INFO_END },
-    { ANDROID_SENSOR_START,         ANDROID_SENSOR_END },
-    { ANDROID_SENSOR_INFO_START,    ANDROID_SENSOR_INFO_END },
-    { ANDROID_FLASH_START,          ANDROID_FLASH_END },
-    { ANDROID_FLASH_INFO_START,     ANDROID_FLASH_INFO_END },
-    { ANDROID_HOT_PIXEL_START,      ANDROID_HOT_PIXEL_END },
-    { ANDROID_HOT_PIXEL_INFO_START, ANDROID_HOT_PIXEL_INFO_END },
-    { ANDROID_DEMOSAIC_START,       ANDROID_DEMOSAIC_END },
-    { ANDROID_DEMOSAIC_INFO_START,  ANDROID_DEMOSAIC_INFO_END },
-    { ANDROID_NOISE_START,          ANDROID_NOISE_END },
-    { ANDROID_NOISE_INFO_START,     ANDROID_NOISE_INFO_END },
-    { ANDROID_SHADING_START,        ANDROID_SHADING_END },
-    { ANDROID_SHADING_INFO_START,   ANDROID_SHADING_INFO_END },
-    { ANDROID_GEOMETRIC_START,      ANDROID_GEOMETRIC_END },
-    { ANDROID_GEOMETRIC_INFO_START, ANDROID_GEOMETRIC_INFO_END },
-    { ANDROID_COLOR_START,          ANDROID_COLOR_END },
-    { ANDROID_COLOR_INFO_START,     ANDROID_COLOR_INFO_END },
-    { ANDROID_TONEMAP_START,        ANDROID_TONEMAP_END },
-    { ANDROID_TONEMAP_INFO_START,   ANDROID_TONEMAP_INFO_END },
-    { ANDROID_EDGE_START,           ANDROID_EDGE_END },
-    { ANDROID_EDGE_INFO_START,      ANDROID_EDGE_INFO_END },
-    { ANDROID_SCALER_START,         ANDROID_SCALER_END },
-    { ANDROID_SCALER_INFO_START,    ANDROID_SCALER_INFO_END },
-    { ANDROID_JPEG_START,           ANDROID_JPEG_END },
-    { ANDROID_JPEG_INFO_START,      ANDROID_JPEG_INFO_END },
-    { ANDROID_STATS_START,          ANDROID_STATS_END },
-    { ANDROID_STATS_INFO_START,     ANDROID_STATS_INFO_END },
-    { ANDROID_CONTROL_START,        ANDROID_CONTROL_END },
-    { ANDROID_CONTROL_INFO_START,   ANDROID_CONTROL_INFO_END }
+    [ANDROID_REQUEST]        = { ANDROID_REQUEST_START,
+                                 ANDROID_REQUEST_END },
+    [ANDROID_LENS]           = { ANDROID_LENS_START,
+                                 ANDROID_LENS_END },
+    [ANDROID_LENS_INFO]      = { ANDROID_LENS_INFO_START,
+                                 ANDROID_LENS_INFO_END },
+    [ANDROID_SENSOR]         = { ANDROID_SENSOR_START,
+                                 ANDROID_SENSOR_END },
+    [ANDROID_SENSOR_INFO]    = { ANDROID_SENSOR_INFO_START,
+                                 ANDROID_SENSOR_INFO_END },
+    [ANDROID_FLASH]          = { ANDROID_FLASH_START,
+                                 ANDROID_FLASH_END },
+    [ANDROID_FLASH_INFO]     = { ANDROID_FLASH_INFO_START,
+                                 ANDROID_FLASH_INFO_END },
+    [ANDROID_HOT_PIXEL]      = { ANDROID_HOT_PIXEL_START,
+                                 ANDROID_HOT_PIXEL_END },
+    [ANDROID_HOT_PIXEL_INFO] = { ANDROID_HOT_PIXEL_INFO_START,
+                                 ANDROID_HOT_PIXEL_INFO_END },
+    [ANDROID_DEMOSAIC]       = { ANDROID_DEMOSAIC_START,
+                                 ANDROID_DEMOSAIC_END },
+    [ANDROID_DEMOSAIC_INFO]  = { ANDROID_DEMOSAIC_INFO_START,
+                                 ANDROID_DEMOSAIC_INFO_END },
+    [ANDROID_NOISE]          = { ANDROID_NOISE_START,
+                                 ANDROID_NOISE_END },
+    [ANDROID_NOISE_INFO]     = { ANDROID_NOISE_INFO_START,
+                                 ANDROID_NOISE_INFO_END },
+    [ANDROID_SHADING]        = { ANDROID_SHADING_START,
+                                 ANDROID_SHADING_END },
+    [ANDROID_SHADING_INFO]   = { ANDROID_SHADING_INFO_START,
+                                 ANDROID_SHADING_INFO_END },
+    [ANDROID_GEOMETRIC]      = { ANDROID_GEOMETRIC_START,
+                                 ANDROID_GEOMETRIC_END },
+    [ANDROID_GEOMETRIC_INFO] = { ANDROID_GEOMETRIC_INFO_START,
+                                 ANDROID_GEOMETRIC_INFO_END },
+    [ANDROID_COLOR]          = { ANDROID_COLOR_START,
+                                 ANDROID_COLOR_END },
+    [ANDROID_COLOR_INFO]     = { ANDROID_COLOR_INFO_START,
+                                 ANDROID_COLOR_INFO_END },
+    [ANDROID_TONEMAP]        = { ANDROID_TONEMAP_START,
+                                 ANDROID_TONEMAP_END },
+    [ANDROID_TONEMAP_INFO]   = { ANDROID_TONEMAP_INFO_START,
+                                 ANDROID_TONEMAP_INFO_END },
+    [ANDROID_EDGE]           = { ANDROID_EDGE_START,
+                                 ANDROID_EDGE_END },
+    [ANDROID_EDGE_INFO]      = { ANDROID_EDGE_INFO_START,
+                                 ANDROID_EDGE_INFO_END },
+    [ANDROID_SCALER]         = { ANDROID_SCALER_START,
+                                 ANDROID_SCALER_END },
+    [ANDROID_SCALER_INFO]    = { ANDROID_SCALER_INFO_START,
+                                 ANDROID_SCALER_INFO_END },
+    [ANDROID_JPEG]           = { ANDROID_JPEG_START,
+                                 ANDROID_JPEG_END },
+    [ANDROID_JPEG_INFO]      = { ANDROID_JPEG_INFO_START,
+                                 ANDROID_JPEG_INFO_END },
+    [ANDROID_STATS]          = { ANDROID_STATS_START,
+                                 ANDROID_STATS_END },
+    [ANDROID_STATS_INFO]     = { ANDROID_STATS_INFO_START,
+                                 ANDROID_STATS_INFO_END },
+    [ANDROID_CONTROL]        = { ANDROID_CONTROL_START,
+                                 ANDROID_CONTROL_END },
+    [ANDROID_CONTROL_INFO]   = { ANDROID_CONTROL_INFO_START,
+                                 ANDROID_CONTROL_INFO_END }
 };
 
+// Shortcut defines to make succint names for field definitions
+#define TIDX(section, tag) \
+    [ ANDROID_ ## section ## _ ## tag - ANDROID_ ## section ## _START ]
+
+#define TIIDX(section, tag) \
+    [ ANDROID_ ## section ## _ ## tag - ANDROID_ ## section ## _INFO_START ]
+
 tag_info_t android_request[ANDROID_REQUEST_END -
         ANDROID_REQUEST_START] = {
-    { "id",                          TYPE_INT32 },
-    { "metadataMode",                TYPE_BYTE },
-    { "outputStreams",               TYPE_BYTE },
-    { "frameCount",                  TYPE_INT32 }
-};
-
-tag_info_t android_control[ANDROID_CONTROL_END -
-        ANDROID_CONTROL_START] = {
-    { "mode",                        TYPE_BYTE },
-    { "aeMode",                      TYPE_BYTE },
-    { "aeRegions",                   TYPE_INT32 },
-    { "aeExposureCompensation",      TYPE_INT32 },
-    { "aeTargetFpsRange",            TYPE_INT32 },
-    { "aeAntibandingMode",           TYPE_BYTE },
-    { "awbMode",                     TYPE_BYTE },
-    { "awbRegions",                  TYPE_INT32 },
-    { "afMode",                      TYPE_BYTE },
-    { "afRegions",                   TYPE_INT32 },
-    { "afTrigger",                   TYPE_BYTE },
-    { "afState",                     TYPE_BYTE },
-    { "videoStabilizationMode",      TYPE_BYTE }
-};
-
-tag_info_t android_control_info[ANDROID_CONTROL_INFO_END -
-        ANDROID_CONTROL_INFO_START] = {
-    { "availableModes",              TYPE_BYTE },
-    { "maxRegions",                  TYPE_INT32 },
-    { "aeAvailableModes",            TYPE_BYTE },
-    { "aeCompensationStep",          TYPE_RATIONAL },
-    { "aeCompensationRange",         TYPE_INT32 },
-    { "aeAvailableTargetFpsRanges",  TYPE_INT32 },
-    { "aeAvailableAntibandingModes", TYPE_BYTE },
-    { "awbAvailableModes",           TYPE_BYTE },
-    { "afAvailableModes",            TYPE_BYTE }
-};
-
-tag_info_t android_sensor[ANDROID_SENSOR_END -
-        ANDROID_SENSOR_START] = {
-    { "exposureTime",  TYPE_INT64 },
-    { "frameDuration", TYPE_INT64 },
-    { "sensitivity",   TYPE_INT32 },
-    { "timestamp",     TYPE_INT64 }
-};
-
-tag_info_t android_sensor_info[ANDROID_SENSOR_INFO_END -
-        ANDROID_SENSOR_INFO_START] = {
-    { "exposureTimeRange",      TYPE_INT64 },
-    { "maxFrameDuration",       TYPE_INT64 },
-    { "sensitivityRange",       TYPE_INT32 },
-    { "colorFilterArrangement", TYPE_BYTE },
-    { "pixelArraySize",         TYPE_INT32 },
-    { "activeArraySize",        TYPE_INT32 },
-    { "whiteLevel",             TYPE_INT32 },
-    { "blackLevelPattern",      TYPE_INT32 },
-    { "colorTransform1",        TYPE_RATIONAL },
-    { "colorTransform2",        TYPE_RATIONAL },
-    { "referenceIlluminant1",   TYPE_BYTE },
-    { "referenceIlluminant2",   TYPE_BYTE },
-    { "forwardMatrix1",         TYPE_RATIONAL },
-    { "forwardMatrix2",         TYPE_RATIONAL },
-    { "calibrationTransform1",  TYPE_RATIONAL },
-    { "calibrationTransform2",  TYPE_RATIONAL },
-    { "baseGainFactor",         TYPE_RATIONAL },
-    { "maxAnalogSensitivity",   TYPE_INT32 },
-    { "noiseModelCoefficients", TYPE_FLOAT },
-    { "orientation",            TYPE_INT32 }
+    TIDX(REQUEST, ID)             =
+    { "id",            TYPE_INT32 },
+    TIDX(REQUEST, METADATA_MODE)  =
+    { "metadataMode",  TYPE_BYTE },
+    TIDX(REQUEST, OUTPUT_STREAMS) =
+    { "outputStreams", TYPE_BYTE },
+    TIDX(REQUEST, FRAME_COUNT)    =
+    { "frameCount",    TYPE_INT32 }
 };
 
 tag_info_t android_lens[ANDROID_LENS_END -
         ANDROID_LENS_START] = {
+    TIDX(LENS, FOCUS_DISTANCE) =
     { "focusDistance",            TYPE_FLOAT },
+    TIDX(LENS, APERTURE)       =
     { "aperture",                 TYPE_FLOAT },
+    TIDX(LENS, FOCAL_LENGTH)   =
     { "focalLength",              TYPE_FLOAT },
+    TIDX(LENS, FILTER_DENSITY) =
     { "filterDensity",            TYPE_FLOAT },
+    TIDX(LENS, OPTICAL_STABILIZATION_MODE) =
     { "opticalStabilizationMode", TYPE_BYTE },
+    TIDX(LENS, FOCUS_RANGE)    =
     { "focusRange",               TYPE_FLOAT }
 };
 
 tag_info_t android_lens_info[ANDROID_LENS_INFO_END -
         ANDROID_LENS_INFO_START] = {
+    TIIDX(LENS, MINIMUM_FOCUS_DISTANCE)  =
     { "minimumFocusDistance",               TYPE_FLOAT },
+    TIIDX(LENS, AVAILABLE_FOCAL_LENGTHS) =
     { "availableFocalLengths",              TYPE_FLOAT },
+    TIIDX(LENS, AVAILABLE_APERTURES) =
     { "availableApertures",                 TYPE_FLOAT },
+    TIIDX(LENS, AVAILABLE_FILTER_DENSITY) =
     { "availableFilterDensities",           TYPE_FLOAT },
+    TIIDX(LENS, AVAILABLE_OPTICAL_STABILIZATION) =
     { "availableOpticalStabilizationModes", TYPE_BYTE },
+    TIIDX(LENS, SHADING_MAP) =
     { "shadingMap",                         TYPE_FLOAT },
+    TIIDX(LENS, GEOMETRIC_CORRECTION_MAP) =
     { "geometricCorrectionMap",             TYPE_FLOAT },
+    TIIDX(LENS, FACING) =
     { "facing",                             TYPE_BYTE },
+    TIIDX(LENS, POSITION) =
     { "position",                           TYPE_FLOAT }
 };
 
+tag_info_t android_sensor[ANDROID_SENSOR_END -
+        ANDROID_SENSOR_START] = {
+    TIDX(SENSOR, EXPOSURE_TIME) =
+    { "exposureTime",  TYPE_INT64 },
+    TIDX(SENSOR, FRAME_DURATION) =
+    { "frameDuration", TYPE_INT64 },
+    TIDX(SENSOR, SENSITIVITY) =
+    { "sensitivity",   TYPE_INT32 },
+    TIDX(SENSOR, TIMESTAMP) =
+    { "timestamp",     TYPE_INT64 }
+};
+
+tag_info_t android_sensor_info[ANDROID_SENSOR_INFO_END -
+        ANDROID_SENSOR_INFO_START] = {
+    TIIDX(SENSOR, EXPOSURE_TIME_RANGE) =
+    { "exposureTimeRange",      TYPE_INT64 },
+    TIIDX(SENSOR, MAX_FRAME_DURATION) =
+    { "maxFrameDuration",       TYPE_INT64 },
+    TIIDX(SENSOR, AVAILABLE_SENSITIVITIES) =
+    { "availableSensitivities", TYPE_INT32 },
+    TIIDX(SENSOR, COLOR_FILTER_ARRANGEMENT) =
+    { "colorFilterArrangement", TYPE_BYTE },
+    TIIDX(SENSOR, PIXEL_ARRAY_SIZE) =
+    { "pixelArraySize",         TYPE_INT32 },
+    TIIDX(SENSOR, ACTIVE_ARRAY_SIZE) =
+    { "activeArraySize",        TYPE_INT32 },
+    TIIDX(SENSOR, WHITE_LEVEL) =
+    { "whiteLevel",             TYPE_INT32 },
+    TIIDX(SENSOR, BLACK_LEVEL_PATTERN) =
+    { "blackLevelPattern",      TYPE_INT32 },
+    TIIDX(SENSOR, COLOR_TRANSFORM_1) =
+    { "colorTransform1",        TYPE_RATIONAL },
+    TIIDX(SENSOR, COLOR_TRANSFORM_2) =
+    { "colorTransform2",        TYPE_RATIONAL },
+    TIIDX(SENSOR, REFERENCE_ILLUMINANT_1) =
+    { "referenceIlluminant1",   TYPE_BYTE },
+    TIIDX(SENSOR, REFERENCE_ILLUMINANT_2) =
+    { "referenceIlluminant2",   TYPE_BYTE },
+    TIIDX(SENSOR, FORWARD_MATRIX_1) =
+    { "forwardMatrix1",         TYPE_RATIONAL },
+    TIIDX(SENSOR, FORWARD_MATRIX_2) =
+    { "forwardMatrix2",         TYPE_RATIONAL },
+    TIIDX(SENSOR, CALIBRATION_TRANSFORM_1) =
+    { "calibrationTransform1",  TYPE_RATIONAL },
+    TIIDX(SENSOR, CALIBRATION_TRANSFORM_2) =
+    { "calibrationTransform2",  TYPE_RATIONAL },
+    TIIDX(SENSOR, BASE_GAIN_FACTOR) =
+    { "baseGainFactor",         TYPE_RATIONAL },
+    TIIDX(SENSOR, MAX_ANALOG_SENSITIVITY) =
+    { "maxAnalogSensitivity",   TYPE_INT32 },
+    TIIDX(SENSOR, NOISE_MODEL_COEFFICIENTS) =
+    { "noiseModelCoefficients", TYPE_FLOAT },
+    TIIDX(SENSOR, ORIENTATION) =
+    { "orientation",            TYPE_INT32 }
+};
+
 tag_info_t android_flash[ANDROID_FLASH_END -
         ANDROID_FLASH_START] = {
+    TIDX(FLASH, MODE) =
     { "mode",          TYPE_BYTE },
+    TIDX(FLASH, FIRING_POWER) =
     { "firingPower",   TYPE_BYTE },
+    TIDX(FLASH, FIRING_TIME) =
     { "firingTime",    TYPE_INT64 }
 };
 
 tag_info_t android_flash_info[ANDROID_FLASH_INFO_END -
         ANDROID_FLASH_INFO_START] = {
+    TIIDX(FLASH, AVAILABLE_MODES) =
     { "available",      TYPE_BYTE },
+    TIIDX(FLASH, CHARGE_DURATION) =
     { "chargeDuration", TYPE_INT64 },
 };
 
 tag_info_t android_hot_pixel[ANDROID_HOT_PIXEL_END -
         ANDROID_HOT_PIXEL_START] = {
-    { "mode",           TYPE_BYTE }
+    TIDX(HOT_PIXEL, MODE) =
+    { "mode", TYPE_BYTE }
 };
 
 tag_info_t android_hot_pixel_info[ANDROID_HOT_PIXEL_INFO_END -
@@ -205,7 +265,8 @@
 
 tag_info_t android_demosaic[ANDROID_DEMOSAIC_END -
         ANDROID_DEMOSAIC_START] = {
-    { "mode",          TYPE_BYTE }
+    TIDX(DEMOSAIC, MODE) =
+    { "mode", TYPE_BYTE }
 };
 
 tag_info_t android_demosaic_info[ANDROID_DEMOSAIC_INFO_END -
@@ -213,8 +274,10 @@
 
 tag_info_t android_noise[ANDROID_NOISE_END -
         ANDROID_NOISE_START] = {
-    { "mode",          TYPE_BYTE },
-    { "strength",      TYPE_BYTE }
+    TIDX(NOISE, MODE) =
+    { "mode",     TYPE_BYTE },
+    TIDX(NOISE, STRENGTH) =
+    { "strength", TYPE_BYTE }
 };
 
 tag_info_t android_noise_info[ANDROID_NOISE_INFO_END -
@@ -222,7 +285,8 @@
 
 tag_info_t android_shading[ANDROID_SHADING_END -
         ANDROID_SHADING_START] = {
-    { "mode",          TYPE_BYTE }
+    TIDX(SHADING, MODE) =
+    { "mode", TYPE_BYTE }
 };
 
 tag_info_t android_shading_info[ANDROID_SHADING_INFO_END -
@@ -230,7 +294,8 @@
 
 tag_info_t android_geometric[ANDROID_GEOMETRIC_END -
         ANDROID_GEOMETRIC_START] = {
-    { "mode",          TYPE_BYTE }
+    TIDX(GEOMETRIC, MODE) =
+    { "mode", TYPE_BYTE }
 };
 
 tag_info_t android_geometric_info[ANDROID_GEOMETRIC_INFO_END -
@@ -238,31 +303,41 @@
 
 tag_info_t android_color[ANDROID_COLOR_END -
         ANDROID_COLOR_START] = {
-    { "mode",          TYPE_BYTE },
-    { "transform",     TYPE_FLOAT }
+    TIDX(COLOR, MODE) =
+    { "mode",      TYPE_BYTE },
+    TIDX(COLOR, TRANSFORM) =
+    { "transform", TYPE_FLOAT }
 };
 
 tag_info_t android_color_info[ANDROID_COLOR_INFO_END -
         ANDROID_COLOR_INFO_START] = {
+    TIIDX(COLOR, AVAILABLE_MODES) =
     { "availableModes", TYPE_INT32 }
 };
 
 tag_info_t android_tonemap[ANDROID_TONEMAP_END -
         ANDROID_TONEMAP_START] = {
-    { "mode",          TYPE_BYTE },
-    { "curveRed",      TYPE_FLOAT },
-    { "curveGreen",    TYPE_FLOAT },
-    { "curveBlue",     TYPE_FLOAT }
+    TIDX(TONEMAP, MODE) =
+    { "mode",       TYPE_BYTE },
+    TIDX(TONEMAP, CURVE_RED) =
+    { "curveRed",   TYPE_FLOAT },
+    TIDX(TONEMAP, CURVE_GREEN) =
+    { "curveGreen", TYPE_FLOAT },
+    TIDX(TONEMAP, CURVE_BLUE) =
+    { "curveBlue",  TYPE_FLOAT }
 };
 
 tag_info_t android_tonemap_info[ANDROID_TONEMAP_INFO_END -
         ANDROID_TONEMAP_INFO_START] = {
+    TIIDX(TONEMAP, MAX_CURVE_POINTS) =
     { "maxCurvePoints", TYPE_INT32 }
 };
 
 tag_info_t android_edge[ANDROID_EDGE_END -
         ANDROID_EDGE_START] = {
+    TIDX(EDGE, MODE) =
     { "mode",          TYPE_BYTE },
+    TIDX(EDGE, STRENGTH) =
     { "strength",      TYPE_BYTE }
 };
 
@@ -271,60 +346,148 @@
 
 tag_info_t android_scaler[ANDROID_SCALER_END -
         ANDROID_SCALER_START] = {
-    { "size",          TYPE_INT32 },
-    { "format",        TYPE_BYTE },
-    { "cropRegion",    TYPE_INT32 },
-    { "rotation",      TYPE_INT32 },
+    TIDX(SCALER, SIZE) =
+    { "size",       TYPE_INT32 },
+    TIDX(SCALER, FORMAT) =
+    { "format",     TYPE_BYTE },
+    TIDX(SCALER, CROP_REGION) =
+    { "cropRegion", TYPE_INT32 },
+    TIDX(SCALER, ROTATION) =
+    { "rotation",   TYPE_INT32 },
 };
 
 tag_info_t android_scaler_info[ANDROID_SCALER_INFO_END -
         ANDROID_SCALER_INFO_START] = {
-    { "availableFormats",            TYPE_INT32 },
-    { "availableSizesPerFormat",     TYPE_INT32 },
-    { "availableSizes",              TYPE_INT32 },
-    { "availableMinFrameDurations",  TYPE_INT32 },
-    { "availableMaxDigitalZoom",     TYPE_INT32 }
+    TIIDX(SCALER, AVAILABLE_FORMATS) =
+    { "availableFormats",           TYPE_INT32 },
+    TIIDX(SCALER, AVAILABLE_SIZES_PER_FORMAT) =
+    { "availableSizesPerFormat",    TYPE_INT32 },
+    TIIDX(SCALER, AVAILABLE_SIZES) =
+    { "availableSizes",             TYPE_INT32 },
+    TIIDX(SCALER, AVAILABLE_MIN_FRAME_DURATIONS) =
+    { "availableMinFrameDurations", TYPE_INT64 },
+    TIIDX(SCALER, AVAILABLE_MAX_ZOOM) =
+    { "availableMaxDigitalZoom",    TYPE_INT32 }
 };
 
 tag_info_t android_jpeg[ANDROID_JPEG_END -
         ANDROID_JPEG_START] = {
+    TIDX(JPEG, QUALITY) =
     { "quality",             TYPE_INT32 },
+    TIDX(JPEG, THUMBNAIL_SIZE) =
     { "thumbnailSize",       TYPE_INT32 },
+    TIDX(JPEG, THUMBNAIL_QUALITY) =
     { "thumbnailQuality",    TYPE_INT32 },
+    TIDX(JPEG, GPS_COORDINATES) =
     { "gpsCoordinates",      TYPE_DOUBLE },
+    TIDX(JPEG, GPS_PROCESSING_METHOD) =
     { "gpsProcessingMethod", TYPE_BYTE },
+    TIDX(JPEG, GPS_TIMESTAMP) =
     { "gpsTimestamp",        TYPE_INT64 },
+    TIDX(JPEG, ORIENTATION) =
     { "orientation",         TYPE_INT32 }
 };
 
 tag_info_t android_jpeg_info[ANDROID_JPEG_INFO_END -
         ANDROID_JPEG_INFO_START] = {
+    TIIDX(JPEG, AVAILABLE_THUMBNAIL_SIZES) =
     { "availableThumbnailSizes", TYPE_INT32 }
 };
 
 tag_info_t android_stats[ANDROID_STATS_END -
         ANDROID_STATS_START] = {
-    { "faceDetectMode",      TYPE_BYTE },
-    { "faceRectangles",      TYPE_INT32 },
-    { "faceScores",          TYPE_BYTE },
-    { "faceLandmarks",       TYPE_INT32 },
-    { "faceIds",             TYPE_INT32 },
-    { "histogramMode",       TYPE_BYTE },
-    { "histogram",           TYPE_INT32 },
-    { "sharpnessMapMode",    TYPE_BYTE },
-    { "sharpnessMap",        TYPE_INT32 }
+    TIDX(STATS, FACE_DETECT_MODE) =
+    { "faceDetectMode",   TYPE_BYTE },
+    TIDX(STATS, FACE_RECTANGLES) =
+    { "faceRectangles",   TYPE_INT32 },
+    TIDX(STATS, FACE_SCORES) =
+    { "faceScores",       TYPE_BYTE },
+    TIDX(STATS, FACE_LANDMARKS) =
+    { "faceLandmarks",    TYPE_INT32 },
+    TIDX(STATS, FACE_IDS) =
+    { "faceIds",          TYPE_INT32 },
+    TIDX(STATS, HISTOGRAM_MODE) =
+    { "histogramMode",    TYPE_BYTE },
+    TIDX(STATS, HISTOGRAM) =
+    { "histogram",        TYPE_INT32 },
+    TIDX(STATS, SHARPNESS_MAP_MODE) =
+    { "sharpnessMapMode", TYPE_BYTE },
+    TIDX(STATS, SHARPNESS_MAP) =
+    { "sharpnessMap",     TYPE_INT32 }
 };
 
 tag_info_t android_stats_info[ANDROID_STATS_INFO_END -
         ANDROID_STATS_INFO_START] = {
-    { "availableFaceDetectModes",    TYPE_BYTE },
-    { "maxFaceCount",                TYPE_INT32 },
-    { "histogramBucketCount",        TYPE_INT32 },
-    { "maxHistogramCount",           TYPE_INT32 },
-    { "sharpnessMapSize",            TYPE_INT32 },
-    { "maxSharpnessMapValue",        TYPE_INT32 }
+    TIIDX(STATS, AVAILABLE_FACE_DETECT_MODES) =
+    { "availableFaceDetectModes", TYPE_BYTE },
+    TIIDX(STATS, MAX_FACE_COUNT) =
+    { "maxFaceCount",             TYPE_INT32 },
+    TIIDX(STATS, HISTOGRAM_BUCKET_COUNT) =
+    { "histogramBucketCount",     TYPE_INT32 },
+    TIIDX(STATS, MAX_HISTOGRAM_COUNT) =
+    { "maxHistogramCount",        TYPE_INT32 },
+    TIIDX(STATS, SHARPNESS_MAP_SIZE) =
+    { "sharpnessMapSize",         TYPE_INT32 },
+    TIIDX(STATS, MAX_SHARPNESS_MAP_VALUE) =
+    { "maxSharpnessMapValue",     TYPE_INT32 }
 };
 
+
+tag_info_t android_control[ANDROID_CONTROL_END -
+        ANDROID_CONTROL_START] = {
+    TIDX(CONTROL, MODE) =
+    { "mode",                        TYPE_BYTE },
+    TIDX(CONTROL, AE_MODE) =
+    { "aeMode",                      TYPE_BYTE },
+    TIDX(CONTROL, AE_REGIONS) =
+    { "aeRegions",                   TYPE_INT32 },
+    TIDX(CONTROL, AE_EXP_COMPENSATION) =
+    { "aeExposureCompensation",      TYPE_INT32 },
+    TIDX(CONTROL, AE_TARGET_FPS_RANGE) =
+    { "aeTargetFpsRange",            TYPE_INT32 },
+    TIDX(CONTROL, AE_ANTIBANDING_MODE) =
+    { "aeAntibandingMode",           TYPE_BYTE },
+    TIDX(CONTROL, AWB_MODE) =
+    { "awbMode",                     TYPE_BYTE },
+    TIDX(CONTROL, AWB_REGIONS) =
+    { "awbRegions",                  TYPE_INT32 },
+    TIDX(CONTROL, AF_MODE) =
+    { "afMode",                      TYPE_BYTE },
+    TIDX(CONTROL, AF_REGIONS) =
+    { "afRegions",                   TYPE_INT32 },
+    TIDX(CONTROL, AF_TRIGGER) =
+    { "afTrigger",                   TYPE_BYTE },
+    TIDX(CONTROL, AF_STATE) =
+    { "afState",                     TYPE_BYTE },
+    TIDX(CONTROL, VIDEO_STABILIZATION_MODE) =
+    { "videoStabilizationMode",      TYPE_BYTE }
+};
+
+tag_info_t android_control_info[ANDROID_CONTROL_INFO_END -
+        ANDROID_CONTROL_INFO_START] = {
+    TIIDX(CONTROL, AVAILABLE_MODES) =
+    { "availableModes",              TYPE_BYTE },
+    TIIDX(CONTROL, MAX_REGIONS) =
+    { "maxRegions",                  TYPE_INT32 },
+    TIIDX(CONTROL, AE_AVAILABLE_MODES) =
+    { "aeAvailableModes",            TYPE_BYTE },
+    TIIDX(CONTROL, AE_EXP_COMPENSATION_STEP) =
+    { "aeCompensationStep",          TYPE_RATIONAL },
+    TIIDX(CONTROL, AE_EXP_COMPENSATION_RANGE) =
+    { "aeCompensationRange",         TYPE_INT32 },
+    TIIDX(CONTROL, AE_AVAILABLE_TARGET_FPS_RANGES) =
+    { "aeAvailableTargetFpsRanges",  TYPE_INT32 },
+    TIIDX(CONTROL, AE_AVAILABLE_ANTIBANDING_MODES) =
+    { "aeAvailableAntibandingModes", TYPE_BYTE },
+    TIIDX(CONTROL, AWB_AVAILABLE_MODES) =
+    { "awbAvailableModes",           TYPE_BYTE },
+    TIIDX(CONTROL, AF_AVAILABLE_MODES) =
+    { "afAvailableModes",            TYPE_BYTE }
+};
+
+#undef TIDX
+#undef TIIDX
+
 tag_info_t *tag_info[ANDROID_SECTION_COUNT] = {
     android_request,
     android_lens,
diff --git a/camera/tests/camera_metadata_tests.cpp b/camera/tests/camera_metadata_tests.cpp
index 3f1800e..53a4c45 100644
--- a/camera/tests/camera_metadata_tests.cpp
+++ b/camera/tests/camera_metadata_tests.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include <errno.h>
+
 #include "gtest/gtest.h"
 #include "system/camera_metadata.h"
 
@@ -24,6 +26,7 @@
 
 #define OK    0
 #define ERROR 1
+#define NOT_FOUND (-ENOENT)
 
 TEST(camera_metadata, allocate_normal) {
     camera_metadata_t *m = NULL;
@@ -270,7 +273,7 @@
     EXPECT_EQ(calculate_camera_metadata_size(entries_used, data_used),
             get_camera_metadata_compact_size(m) );
 
-    dump_camera_metadata(m, 2);
+    dump_camera_metadata(m, 0, 2);
 
     free_camera_metadata(m);
 }
@@ -342,7 +345,7 @@
     EXPECT_EQ((size_t)0, data_count);
     EXPECT_EQ(NULL, data_int32);
 
-    dump_camera_metadata(m, 2);
+    dump_camera_metadata(m, 0, 2);
 
     free_camera_metadata(m);
 }
@@ -823,7 +826,100 @@
         }
     }
 
-    dump_camera_metadata(m, 2);
+    dump_camera_metadata(m, 0, 2);
+
+    free_camera_metadata(m);
+}
+
+TEST(camera_metadata, sort_metadata) {
+    camera_metadata_t *m = NULL;
+    const size_t entry_capacity = 5;
+    const size_t data_capacity = 100;
+
+    int result;
+
+    m = allocate_camera_metadata(entry_capacity, data_capacity);
+
+    // Add several unique entries in non-sorted order
+
+    float colorTransform[9] = {
+        0.9f, 0.0f, 0.0f,
+        0.2f, 0.5f, 0.0f,
+        0.0f, 0.1f, 0.7f
+    };
+    result = add_camera_metadata_entry(m,
+            ANDROID_COLOR_TRANSFORM,
+            colorTransform, 9);
+    EXPECT_EQ(OK, result);
+
+    float focus_distance = 0.5f;
+    result = add_camera_metadata_entry(m,
+            ANDROID_LENS_FOCUS_DISTANCE,
+            &focus_distance, 1);
+    EXPECT_EQ(OK, result);
+
+    int64_t exposure_time = 1000000000;
+    result = add_camera_metadata_entry(m,
+            ANDROID_SENSOR_EXPOSURE_TIME,
+            &exposure_time, 1);
+    EXPECT_EQ(OK, result);
+
+    int32_t sensitivity = 800;
+    result = add_camera_metadata_entry(m,
+            ANDROID_SENSOR_SENSITIVITY,
+            &sensitivity, 1);
+    EXPECT_EQ(OK, result);
+
+    // Test unsorted find
+    uint8_t type;
+    float  *f;
+    size_t  data_count;
+    result = find_camera_metadata_entry(m,
+            ANDROID_LENS_FOCUS_DISTANCE,
+            &type,
+            (void**)&f,
+            &data_count);
+    EXPECT_EQ(OK, result);
+    EXPECT_EQ(TYPE_FLOAT, type);
+    EXPECT_EQ(1, (int)data_count);
+    EXPECT_EQ(focus_distance, *f);
+
+    result = find_camera_metadata_entry(m,
+            ANDROID_NOISE_STRENGTH,
+            &type,
+            (void**)&f,
+            &data_count);
+    EXPECT_EQ(NOT_FOUND, result);
+
+    // Sort
+    std::cout << "Pre-sorted metadata" << std::endl;
+    dump_camera_metadata(m, 0, 2);
+
+    result = sort_camera_metadata(m);
+    EXPECT_EQ(OK, result);
+
+    std::cout << "Sorted metadata" << std::endl;
+    dump_camera_metadata(m, 0, 2);
+
+    // Test sorted find
+
+    result = find_camera_metadata_entry(m,
+            ANDROID_LENS_FOCUS_DISTANCE,
+            &type,
+            (void**)&f,
+            &data_count);
+    EXPECT_EQ(OK, result);
+    EXPECT_EQ(TYPE_FLOAT, type);
+    EXPECT_EQ(1, (int)data_count);
+    EXPECT_EQ(focus_distance, *f);
+
+    result = find_camera_metadata_entry(m,
+            ANDROID_NOISE_STRENGTH,
+            &type,
+            (void**)&f,
+            &data_count);
+    EXPECT_EQ(NOT_FOUND, result);
+
 
     free_camera_metadata(m);
 }
diff --git a/camera/tests/camera_metadata_tests_fake_vendor.h b/camera/tests/camera_metadata_tests_fake_vendor.h
index 8522fa6..8e08776 100644
--- a/camera/tests/camera_metadata_tests_fake_vendor.h
+++ b/camera/tests/camera_metadata_tests_fake_vendor.h
@@ -104,7 +104,22 @@
     fakevendor_scaler
 };
 
-const char *get_fakevendor_section_name(uint32_t tag) {
+const char *get_fakevendor_section_name(const vendor_tag_query_ops_t *v,
+        uint32_t tag);
+const char *get_fakevendor_tag_name(const vendor_tag_query_ops_t *v,
+        uint32_t tag);
+int get_fakevendor_tag_type(const vendor_tag_query_ops_t *v,
+        uint32_t tag);
+
+static const vendor_tag_query_ops_t fakevendor_query_ops = {
+    get_fakevendor_section_name,
+    get_fakevendor_tag_name,
+    get_fakevendor_tag_type
+};
+
+const char *get_fakevendor_section_name(const vendor_tag_query_ops_t *v,
+        uint32_t tag) {
+    if (v != &fakevendor_query_ops) return NULL;
     int tag_section = (tag >> 16) - VENDOR_SECTION;
     if (tag_section < 0 ||
             tag_section >= FAKEVENDOR_SECTION_COUNT) return NULL;
@@ -112,7 +127,9 @@
     return fakevendor_section_names[tag_section];
 }
 
-const char *get_fakevendor_tag_name(uint32_t tag) {
+const char *get_fakevendor_tag_name(const vendor_tag_query_ops_t *v,
+        uint32_t tag) {
+    if (v != &fakevendor_query_ops) return NULL;
     int tag_section = (tag >> 16) - VENDOR_SECTION;
     if (tag_section < 0
             || tag_section >= FAKEVENDOR_SECTION_COUNT
@@ -121,7 +138,9 @@
     return fakevendor_tag_info[tag_section][tag_index].tag_name;
 }
 
-int get_fakevendor_tag_type(uint32_t tag) {
+int get_fakevendor_tag_type(const vendor_tag_query_ops_t *v,
+        uint32_t tag) {
+    if (v != &fakevendor_query_ops) return -1;
     int tag_section = (tag >> 16) - VENDOR_SECTION;
     if (tag_section < 0
             || tag_section >= FAKEVENDOR_SECTION_COUNT
@@ -130,10 +149,5 @@
     return fakevendor_tag_info[tag_section][tag_index].tag_type;
 }
 
-static const vendor_tag_query_ops_t fakevendor_query_ops = {
-    get_fakevendor_section_name,
-    get_fakevendor_tag_name,
-    get_fakevendor_tag_type
-};
 
 #endif