AVIF-info library and test

libavifinfo is a standalone library that can be used to extract the
width, height, bit depth and number of channels from an AVIF payload.
It is small on purpose and made of avifinfo.h and avifinfo.c.

Licensing is inspired from libaom (boilerplates, LICENSE and PATENTS).

Add CMakeLists.txt as the build system configuration file.

Add tests/avifinfo_test.cc and a 1x1 white pixel AVIF image compressed
with libavif for testing purposes.

Add README.md.

Change-Id: I26244b666854523fce3e001d2be9f19574a4715f
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..ce831e6
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,36 @@
+# Copyright (c) 2021, Alliance for Open Media. All rights reserved
+#
+# This source code is subject to the terms of the BSD 2 Clause License and the
+# Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License was
+# not distributed with this source code in the LICENSE file, you can obtain it
+# at www.aomedia.org/license/software. If the Alliance for Open Media Patent
+# License 1.0 was not distributed with this source code in the PATENTS file, you
+# can obtain it at www.aomedia.org/license/patent.
+
+cmake_minimum_required(VERSION 3.6)
+project(avifinfo C CXX)
+set(CMAKE_C_STANDARD 11)
+
+option(AVIFINFO_BUILD_TESTS
+       "Build and enable tests (GoogleTest must be installed)" OFF)
+
+# C library
+
+add_library(avifinfo avifinfo.c)
+
+# C++ tests
+
+if(AVIFINFO_BUILD_TESTS)
+  find_package(Threads REQUIRED) # Needed by GoogleTest
+  find_package(GTest REQUIRED)
+
+  enable_testing()
+  add_executable(avifinfo_test tests/avifinfo_test.cc)
+  target_include_directories(avifinfo_test PRIVATE ${GTEST_INCLUDE_DIRS}
+                                                   ${CMAKE_CURRENT_SOURCE_DIR})
+  target_link_libraries(avifinfo_test PRIVATE ${GTEST_BOTH_LIBRARIES} avifinfo)
+  add_test(
+    NAME avifinfo_test
+    COMMAND ${CMAKE_CURRENT_BINARY_DIR}/avifinfo_test
+    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests)
+endif()
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..330b6b8
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,26 @@
+Copyright (c) 2021, Alliance for Open Media. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in
+   the documentation and/or other materials provided with the
+   distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
diff --git a/PATENTS b/PATENTS
new file mode 100644
index 0000000..826644b
--- /dev/null
+++ b/PATENTS
@@ -0,0 +1,107 @@
+Alliance for Open Media Patent License 1.0
+
+1. License Terms.
+
+1.1. Patent License. Subject to the terms and conditions of this License, each
+     Licensor, on behalf of itself and successors in interest and assigns,
+     grants Licensee a non-sublicensable, perpetual, worldwide, non-exclusive,
+     no-charge, royalty-free, irrevocable (except as expressly stated in this
+     License) patent license to its Necessary Claims to make, use, sell, offer
+     for sale, import or distribute any Implementation.
+
+1.2. Conditions.
+
+1.2.1. Availability. As a condition to the grant of rights to Licensee to make,
+       sell, offer for sale, import or distribute an Implementation under
+       Section 1.1, Licensee must make its Necessary Claims available under
+       this License, and must reproduce this License with any Implementation
+       as follows:
+
+       a. For distribution in source code, by including this License in the
+          root directory of the source code with its Implementation.
+
+       b. For distribution in any other form (including binary, object form,
+          and/or hardware description code (e.g., HDL, RTL, Gate Level Netlist,
+          GDSII, etc.)), by including this License in the documentation, legal
+          notices, and/or other written materials provided with the
+          Implementation.
+
+1.2.2. Additional Conditions. This license is directly from Licensor to
+       Licensee.  Licensee acknowledges as a condition of benefiting from it
+       that no rights from Licensor are received from suppliers, distributors,
+       or otherwise in connection with this License.
+
+1.3. Defensive Termination. If any Licensee, its Affiliates, or its agents
+     initiates patent litigation or files, maintains, or voluntarily
+     participates in a lawsuit against another entity or any person asserting
+     that any Implementation infringes Necessary Claims, any patent licenses
+     granted under this License directly to the Licensee are immediately
+     terminated as of the date of the initiation of action unless 1) that suit
+     was in response to a corresponding suit regarding an Implementation first
+     brought against an initiating entity, or 2) that suit was brought to
+     enforce the terms of this License (including intervention in a third-party
+     action by a Licensee).
+
+1.4. Disclaimers. The Reference Implementation and Specification are provided
+     "AS IS" and without warranty. The entire risk as to implementing or
+     otherwise using the Reference Implementation or Specification is assumed
+     by the implementer and user. Licensor expressly disclaims any warranties
+     (express, implied, or otherwise), including implied warranties of
+     merchantability, non-infringement, fitness for a particular purpose, or
+     title, related to the material. IN NO EVENT WILL LICENSOR BE LIABLE TO
+     ANY OTHER PARTY FOR LOST PROFITS OR ANY FORM OF INDIRECT, SPECIAL,
+     INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY CHARACTER FROM ANY CAUSES OF
+     ACTION OF ANY KIND WITH RESPECT TO THIS LICENSE, WHETHER BASED ON BREACH
+     OF CONTRACT, TORT (INCLUDING NEGLIGENCE), OR OTHERWISE, AND WHETHER OR
+     NOT THE OTHER PARTRY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+2. Definitions.
+
+2.1. Affiliate.  "Affiliate" means an entity that directly or indirectly
+     Controls, is Controlled by, or is under common Control of that party.
+
+2.2. Control. "Control" means direct or indirect control of more than 50% of
+     the voting power to elect directors of that corporation, or for any other
+     entity, the power to direct management of such entity.
+
+2.3. Decoder.  "Decoder" means any decoder that conforms fully with all
+     non-optional portions of the Specification.
+
+2.4. Encoder.  "Encoder" means any encoder that produces a bitstream that can
+     be decoded by a Decoder only to the extent it produces such a bitstream.
+
+2.5. Final Deliverable.  "Final Deliverable" means the final version of a
+     deliverable approved by the Alliance for Open Media as a Final
+     Deliverable.
+
+2.6. Implementation.  "Implementation" means any implementation, including the
+     Reference Implementation, that is an Encoder and/or a Decoder. An
+     Implementation also includes components of an Implementation only to the
+     extent they are used as part of an Implementation.
+
+2.7. License. "License" means this license.
+
+2.8. Licensee. "Licensee" means any person or entity who exercises patent
+     rights granted under this License.
+
+2.9. Licensor.  "Licensor" means (i) any Licensee that makes, sells, offers
+     for sale, imports or distributes any Implementation, or (ii) a person
+     or entity that has a licensing obligation to the Implementation as a
+     result of its membership and/or participation in the Alliance for Open
+     Media working group that developed the Specification.
+
+2.10. Necessary Claims.  "Necessary Claims" means all claims of patents or
+      patent applications, (a) that currently or at any time in the future,
+      are owned or controlled by the Licensor, and (b) (i) would be an
+      Essential Claim as defined by the W3C Policy as of February 5, 2004
+      (https://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential)
+      as if the Specification was a W3C Recommendation; or (ii) are infringed
+      by the Reference Implementation.
+
+2.11. Reference Implementation. "Reference Implementation" means an Encoder
+      and/or Decoder released by the Alliance for Open Media as a Final
+      Deliverable.
+
+2.12. Specification. "Specification" means the specification designated by
+      the Alliance for Open Media as a Final Deliverable for which this
+      License was issued.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..01afccb
--- /dev/null
+++ b/README.md
@@ -0,0 +1,80 @@
+# AVIF-info
+
+**libavifinfo** is a standalone library that can be used to extract the width,
+height, bit depth and number of channels from an AVIF payload.
+
+See `avifinfo.h` for details on the API and `avifinfo.c` for the implementation.
+See `avifinfo_test.cc` for usage examples.
+
+## Contents
+
+1.  [How to use](#how-to-use)
+
+    1.  [Build](#build)
+    2.  [Test](#test)
+
+2.  [Development](#development)
+
+    1.  [Coding style](#coding-style)
+    2.  [Submitting patches](#submitting-patches)
+
+3.  [Bug reports](#bug-reports)
+
+## How to use {#how-to-use}
+
+**libavifinfo** can be used when only a few AVIF features are needed and when
+linking to or including [libavif](https://github.com/AOMediaCodec/libavif) is
+not an option. For decoding an image or extracting more features, please rely on
+[libavif](https://github.com/AOMediaCodec/libavif).
+
+```
+AvifInfoFeatures features;
+if (AvifInfoGet(bytes, number_of_available_bytes, &features) == kAvifInfoOk) {
+  // Use 'features.width' etc.
+}
+```
+
+Note: `AvifInfoGet()` is designed to return the same `avifImage` field values as
+[`avifDecoderRead()`](https://github.com/AOMediaCodec/libavif/blob/9d8f9f9eb24fcea36113c946fa72f9f92aa7b317/include/avif/avif.h#L894).
+However **libavifinfo** is more permissive and may return features of images
+considered invalid by **libavif**.
+
+### Build {#build}
+
+`avifinfo.c` is written in C. To build from this directory:
+
+```
+mkdir build && \
+cd build && \
+cmake .. && \
+cmake --build . --config Release
+```
+
+### Test {#test}
+
+Tests are written in C++. GoogleTest is required.
+
+```
+mkdir build && \
+cd build && \
+cmake .. -DAVIFINFO_BUILD_TESTS=ON && \
+cmake --build . --config Debug && \
+ctest .
+```
+
+## Development {#development}
+
+### Coding style {#coding-style}
+
+[Google C/C++ Style Guide](https://google.github.io/styleguide/cppguide.html) is
+used in this project.
+
+### Submitting patches {#submitting-patches}
+
+If you would like to contribute to **libavifinfo**, please follow the steps for
+**libaom** at https://aomedia.googlesource.com/aom/#submitting-patches.
+
+## Bug reports {#bug-reports}
+
+Bug reports can be filed in the Alliance for Open Media
+[issue tracker](https://bugs.chromium.org/p/aomedia/issues/list).
diff --git a/avifinfo.c b/avifinfo.c
new file mode 100644
index 0000000..f284924
--- /dev/null
+++ b/avifinfo.c
@@ -0,0 +1,710 @@
+// Copyright (c) 2021, Alliance for Open Media. All rights reserved
+//
+// This source code is subject to the terms of the BSD 2 Clause License and
+// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+// was not distributed with this source code in the LICENSE file, you can
+// obtain it at www.aomedia.org/license/software. If the Alliance for Open
+// Media Patent License 1.0 was not distributed with this source code in the
+// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+
+#include "avifinfo.h"
+
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+//------------------------------------------------------------------------------
+
+// Status returned when reading the content of a box (or file).
+typedef enum {
+  kFound,     // Input correctly parsed and information retrieved.
+  kNotFound,  // Input correctly parsed but information is missing or elsewhere.
+  kTruncated,  // Input correctly parsed until missing bytes to continue.
+  kAborted,  // Input correctly parsed until stopped to avoid timeout or crash.
+  kInvalid,  // Input incorrectly parsed.
+} AvifInfoInternalStatus;
+
+// uint32_t is used everywhere in this file. It is unlikely to be insufficient
+// to parse AVIF headers. Clamp any input to 2^32-1 for simplicity.
+static const uint32_t kAvifInfoInternalMaxSize = UINT32_MAX;
+
+// Reads an unsigned integer from 'input' with most significant bits first.
+// 'input' must be at least 'num_bytes'-long.
+static uint32_t AvifInfoInternalReadBigEndian(const uint8_t* input,
+                                              uint32_t num_bytes) {
+  uint32_t value = 0;
+  for (uint32_t i = 0; i < num_bytes; ++i) {
+    value = (value << 8) | input[i];
+  }
+  return value;
+}
+
+//------------------------------------------------------------------------------
+// Convenience macros.
+
+#if defined(AVIFINFO_LOG_ERROR)  // Toggle to log encountered issues.
+static void AvifInfoInternalLogError(const char* file, int line,
+                                     AvifInfoInternalStatus status) {
+  const char* kStr[] = {"Found", "NotFound", "Truncated", "Invalid", "Aborted"};
+  fprintf(stderr, "  %s:%d: %s\n", file, line, kStr[status]);
+  // Set a breakpoint here to catch the first detected issue.
+}
+#define AVIFINFO_RETURN(check_status)                               \
+  do {                                                              \
+    const AvifInfoInternalStatus status_checked = (check_status);   \
+    if (status_checked != kFound && status_checked != kNotFound) {  \
+      AvifInfoInternalLogError(__FILE__, __LINE__, status_checked); \
+    }                                                               \
+    return status_checked;                                          \
+  } while (0)
+#else
+#define AVIFINFO_RETURN(check_status) \
+  do {                                \
+    return (check_status);            \
+  } while (0)
+#endif
+
+#define AVIFINFO_CHECK(check_condition, check_status)      \
+  do {                                                     \
+    if (!(check_condition)) AVIFINFO_RETURN(check_status); \
+  } while (0)
+#define AVIFINFO_CHECK_STATUS_IS(check_status, expected_status)            \
+  do {                                                                     \
+    const AvifInfoInternalStatus status_returned = (check_status);         \
+    AVIFINFO_CHECK(status_returned == (expected_status), status_returned); \
+  } while (0)
+#define AVIFINFO_CHECK_FOUND(check_status) \
+  AVIFINFO_CHECK_STATUS_IS((check_status), kFound)
+#define AVIFINFO_CHECK_NOT_FOUND(check_status) \
+  AVIFINFO_CHECK_STATUS_IS((check_status), kNotFound)
+
+//------------------------------------------------------------------------------
+// Box header parsing and various size checks.
+
+typedef struct {
+  uint32_t size;              // In bytes.
+  const uint8_t* type;        // Points to four characters.
+  uint32_t version;           // 0 or actual version if this is a full box.
+  uint32_t flags;             // 0 or actual value if this is a full box.
+  uint32_t content_size;      // 'size' minus the header size.
+  uint32_t content_position;  // Position in bytes of the 'content' of this box
+                              // relative to its container.
+  const uint8_t* content;     // Content bytes of this box (after its header).
+} AvifInfoInternalBox;
+
+// Reads the header of a 'box' starting at 'bytes + position'.
+// 'num_bytes' is the number of available 'bytes'.
+// 'max_num_bytes' is the size of the container of the 'box' (either the file
+// itself or the content of the parent of the 'box').
+static AvifInfoInternalStatus AvifInfoInternalParseBox(
+    const uint8_t* bytes, uint32_t num_bytes, uint32_t max_num_bytes,
+    uint32_t position, uint32_t* num_parsed_boxes, AvifInfoInternalBox* box) {
+  // See ISO/IEC 14496-12:2012(E) 4.2
+  AVIFINFO_CHECK(position <= kAvifInfoInternalMaxSize - 8, kAborted);
+  AVIFINFO_CHECK(position + 8 <= max_num_bytes, kInvalid);  // box size+type
+  AVIFINFO_CHECK(position + 4 <= num_bytes, kTruncated);    // 32b size
+  box->size = AvifInfoInternalReadBigEndian(bytes + position, sizeof(uint32_t));
+  // Note: 'box->size==1' means 64b size should be read.
+  //       'box->size==0' means this box extends to all remaining bytes.
+  //       These two use cases are not handled here for simplicity.
+  AVIFINFO_CHECK(box->size >= 2, kAborted);
+  AVIFINFO_CHECK(box->size >= 8, kInvalid);  // box 32b size + 32b type
+  AVIFINFO_CHECK(box->size <= kAvifInfoInternalMaxSize - position, kAborted);
+  AVIFINFO_CHECK(position + box->size <= max_num_bytes, kInvalid);
+  AVIFINFO_CHECK(position + 8 <= num_bytes, kTruncated);
+  box->type = bytes + position + 4;
+
+  const int has_fullbox_header =
+      !memcmp(box->type, "meta", 4) || !memcmp(box->type, "pitm", 4) ||
+      !memcmp(box->type, "ipma", 4) || !memcmp(box->type, "ispe", 4) ||
+      !memcmp(box->type, "pixi", 4) || !memcmp(box->type, "iref", 4) ||
+      !memcmp(box->type, "auxC", 4);
+  const uint32_t box_header_size = (has_fullbox_header ? 12 : 8);
+  AVIFINFO_CHECK(box->size >= box_header_size, kInvalid);
+  box->content_position = position + box_header_size;
+  AVIFINFO_CHECK(box->content_position <= num_bytes, kTruncated);
+  box->content_size = box->size - box_header_size;
+  box->content = bytes + box->content_position;
+  // Avoid timeouts. The maximum number of parsed boxes is arbitrary.
+  ++*num_parsed_boxes;
+  AVIFINFO_CHECK(*num_parsed_boxes < 4096, kAborted);
+
+  box->version = 0;
+  box->flags = 0;
+  if (has_fullbox_header) {
+    box->version = AvifInfoInternalReadBigEndian(bytes + position + 8, 1);
+    box->flags = AvifInfoInternalReadBigEndian(bytes + position + 9, 3);
+    // See AV1 Image File Format (AVIF) 8.1
+    // at https://aomediacodec.github.io/av1-avif/#avif-boxes (available when
+    // https://github.com/AOMediaCodec/av1-avif/pull/170 is merged).
+    uint32_t is_parsable = 1;
+    if (!memcmp(box->type, "meta", 4)) is_parsable = (box->version <= 0);
+    if (!memcmp(box->type, "pitm", 4)) is_parsable = (box->version <= 1);
+    if (!memcmp(box->type, "ipma", 4)) is_parsable = (box->version <= 1);
+    if (!memcmp(box->type, "ispe", 4)) is_parsable = (box->version <= 0);
+    if (!memcmp(box->type, "pixi", 4)) is_parsable = (box->version <= 0);
+    if (!memcmp(box->type, "iref", 4)) is_parsable = (box->version <= 1);
+    if (!memcmp(box->type, "auxC", 4)) is_parsable = (box->version <= 0);
+    // Instead of considering this file as invalid, skip unparsable boxes.
+    if (!is_parsable) box->type = (const uint8_t*)"\0skip";
+  }
+  return kFound;
+}
+
+// Returns kFound if 'min_size' bytes can be read from the 'box.content' now.
+// 'num_bytes' is the number of available bytes of the parent of the 'box'.
+static AvifInfoInternalStatus AccessContent(AvifInfoInternalBox* box,
+                                            uint32_t num_bytes,
+                                            uint32_t min_size) {
+  AVIFINFO_CHECK(box->content_size >= min_size, kInvalid);
+  AVIFINFO_CHECK(box->content_position + min_size <= num_bytes, kTruncated);
+  return kFound;
+}
+
+//------------------------------------------------------------------------------
+// Search if the file identifies itself as AVIF through an "ftyp" box.
+
+static AvifInfoInternalStatus ParseFileForBrand(const uint8_t* bytes,
+                                                uint32_t num_bytes,
+                                                uint32_t file_size,
+                                                uint32_t* num_parsed_boxes) {
+  uint32_t position = 0;  // Within 'bytes' (that points to first byte of file).
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, file_size, position, num_parsed_boxes, &box));
+
+    if (!memcmp(box.type, "ftyp", 4)) {
+      // Iterate over brands. See ISO/IEC 14496-12:2012(E) 4.3.1
+      AVIFINFO_CHECK(box.content_size >= 8, kInvalid);  // major_brand + version
+      for (uint32_t i = 0; i < box.content_size; i += 4) {
+        AVIFINFO_CHECK_FOUND(AccessContent(&box, num_bytes, i + 4));
+        if (i == 4) continue;  // Skip minor_version.
+        if (!memcmp(box.content + i, "avif", 4) ||
+            !memcmp(box.content + i, "avis", 4)) {
+          return kFound;
+        }
+      }
+      AVIFINFO_RETURN(kInvalid);  // Only one "ftyp" allowed per file.
+    }
+    position += box.size;
+    // File is valid only if the end of at least one box is at the same position
+    // as the end of the container. Oddities are caught when parsing further.
+  } while (position != file_size);
+  AVIFINFO_RETURN(kInvalid);  // There should be one "ftyp" box.
+}
+
+//------------------------------------------------------------------------------
+// Search the primary item ID through "meta > pitm" boxes.
+
+static AvifInfoInternalStatus ParseMetaForPrimaryItemId(
+    const uint8_t* bytes, uint32_t num_bytes, uint32_t max_num_bytes,
+    uint32_t* num_parsed_boxes, uint32_t* primary_item_id) {
+  uint32_t position = 0;  // Within 'bytes' (first byte of "meta" box content).
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, max_num_bytes, position, num_parsed_boxes, &box));
+
+    if (!memcmp(box.type, "pitm", 4)) {
+      // See ISO/IEC 14496-12:2015(E) 8.11.4.2
+      const uint32_t num_bytes_per_id = (box.version == 0) ? 2 : 4;
+      AVIFINFO_CHECK_FOUND(AccessContent(&box, num_bytes, num_bytes_per_id));
+      *primary_item_id =
+          AvifInfoInternalReadBigEndian(box.content + 0, num_bytes_per_id);
+      return kFound;
+    }
+    position += box.size;
+  } while (position != max_num_bytes);
+
+  // According to ISO/IEC 14496-12:2012(E) 8.11.1.1, there is at most one "meta"
+  // per file. No "pitm" until now means never.
+  AVIFINFO_RETURN(kInvalid);
+}
+
+static AvifInfoInternalStatus ParseFileForPrimaryItemId(
+    const uint8_t* bytes, uint32_t num_bytes, uint32_t file_size,
+    uint32_t* num_parsed_boxes, uint32_t* primary_item_id) {
+  uint32_t position = 0;  // Within 'bytes' (that points to first byte of file).
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, file_size, position, num_parsed_boxes, &box));
+
+    if (!memcmp(box.type, "meta", 4)) {
+      return ParseMetaForPrimaryItemId(
+          box.content, num_bytes - box.content_position, box.content_size,
+          num_parsed_boxes, primary_item_id);
+    }
+    position += box.size;
+  } while (position != file_size);
+  AVIFINFO_RETURN(kInvalid);  // No "meta" is an issue.
+}
+
+//------------------------------------------------------------------------------
+// Search the features of an item given its ID through "meta > iprp" boxes.
+
+static AvifInfoInternalStatus ParseIpcoForFeaturesInProperty(
+    const uint8_t* bytes, uint32_t num_bytes, uint32_t max_num_bytes,
+    uint32_t target_property_index, uint32_t* num_parsed_boxes,
+    AvifInfoFeatures* features) {
+  uint32_t position = 0;
+  uint32_t box_index = 1;  // 1-based index. Used for iterating over properties.
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, max_num_bytes, position, num_parsed_boxes, &box));
+
+    if (box_index != target_property_index) {
+      // Skip.
+    } else if (features->width == 0 && !memcmp(box.type, "ispe", 4)) {
+      // See ISO/IEC 23008-12:2017(E) 6.5.3.2
+      AVIFINFO_CHECK_FOUND(AccessContent(&box, num_bytes, 4 + 4));
+      features->width = AvifInfoInternalReadBigEndian(box.content + 0, 4);
+      features->height = AvifInfoInternalReadBigEndian(box.content + 4, 4);
+      AVIFINFO_CHECK(features->width != 0 && features->height != 0, kInvalid);
+      return kFound;
+    } else if (features->num_channels == 0 && !memcmp(box.type, "pixi", 4)) {
+      // See ISO/IEC 23008-12:2017(E) 6.5.6.2
+      AVIFINFO_CHECK_FOUND(AccessContent(&box, num_bytes, 1));
+      features->num_channels =
+          AvifInfoInternalReadBigEndian(box.content + 0, 1);
+      AVIFINFO_CHECK(features->num_channels >= 1, kInvalid);
+      AVIFINFO_CHECK_FOUND(
+          AccessContent(&box, num_bytes, 1 + features->num_channels));
+      features->bit_depth = AvifInfoInternalReadBigEndian(box.content + 1, 1);
+      AVIFINFO_CHECK(features->bit_depth >= 1, kInvalid);
+      for (uint32_t i = 1; i < features->num_channels; ++i) {
+        const uint32_t bit_depth =
+            AvifInfoInternalReadBigEndian(box.content + 1 + i, 1);
+        // Bit depth should be the same for all channels.
+        AVIFINFO_CHECK(bit_depth == features->bit_depth, kInvalid);
+      }
+      return kFound;
+    } else if (features->num_channels == 0 && !memcmp(box.type, "av1C", 4)) {
+      // See AV1 Codec ISO Media File Format Binding 2.3.1
+      // at https://aomediacodec.github.io/av1-isobmff/#av1c
+      // Only parse the necessary third byte. Assume that the others are valid.
+      AVIFINFO_CHECK_FOUND(AccessContent(&box, num_bytes, 3));
+      const uint32_t fields = AvifInfoInternalReadBigEndian(box.content + 2, 1);
+      const int high_bitdepth = (fields & 0x40) != 0;
+      const int twelve_bit = (fields & 0x20) != 0;
+      const int monochrome = (fields & 0x10) != 0;
+      AVIFINFO_CHECK(twelve_bit || !high_bitdepth, kInvalid);
+      features->num_channels = monochrome ? 1 : 3;
+      features->bit_depth = high_bitdepth ? twelve_bit ? 12 : 10 : 8;
+      return kFound;
+    }
+    ++box_index;
+    position += box.size;
+  } while (position != max_num_bytes && box_index <= target_property_index);
+  AVIFINFO_RETURN(kNotFound);
+}
+
+static AvifInfoInternalStatus ParseIprpForFeaturesInProperty(
+    const uint8_t* bytes, uint32_t num_bytes, uint32_t max_num_bytes,
+    uint32_t target_property_index, uint32_t* num_parsed_boxes,
+    AvifInfoFeatures* features) {
+  uint32_t position = 0;
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, max_num_bytes, position, num_parsed_boxes, &box));
+
+    if (!memcmp(box.type, "ipco", 4)) {
+      return ParseIpcoForFeaturesInProperty(
+          box.content, num_bytes - box.content_position, box.content_size,
+          target_property_index, num_parsed_boxes, features);
+    }
+    position += box.size;
+  } while (position != max_num_bytes);
+  AVIFINFO_RETURN(kInvalid);  // No "ipco" in "iprp" is an issue.
+}
+
+static AvifInfoInternalStatus ParseIprpForFeatures(const uint8_t* bytes,
+                                                   uint32_t num_bytes,
+                                                   uint32_t max_num_bytes,
+                                                   uint32_t target_item_id,
+                                                   uint32_t* num_parsed_boxes,
+                                                   AvifInfoFeatures* features) {
+  uint32_t position = 0;
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, max_num_bytes, position, num_parsed_boxes, &box));
+
+    if (!memcmp(box.type, "ipma", 4)) {
+      // See ISO/IEC 23008-12:2017(E) 9.3.2
+      AVIFINFO_CHECK_FOUND(AccessContent(&box, num_bytes, 4));
+      const uint32_t entry_count =
+          AvifInfoInternalReadBigEndian(box.content + 0, 4);
+      uint32_t offset = 4;
+      const uint32_t id_num_bytes = (box.version < 1) ? 2 : 4;
+      const uint32_t index_num_bytes = (box.flags & 1) ? 2 : 1;
+      const uint32_t essential_bit_mask = (box.flags & 1) ? 0x8000 : 0x80;
+
+      for (uint32_t entry = 0; entry < entry_count; ++entry) {
+        AVIFINFO_CHECK_FOUND(
+            AccessContent(&box, num_bytes, offset + id_num_bytes + 1));
+        const uint32_t item_id =
+            AvifInfoInternalReadBigEndian(box.content + offset, id_num_bytes);
+
+        offset += id_num_bytes;
+        const uint32_t association_count =
+            AvifInfoInternalReadBigEndian(box.content + offset, 1);
+        offset += 1;
+
+        for (uint32_t property = 0; property < association_count; ++property) {
+          AVIFINFO_CHECK_FOUND(
+              AccessContent(&box, num_bytes, offset + index_num_bytes));
+          const uint32_t value = AvifInfoInternalReadBigEndian(
+              box.content + offset, index_num_bytes);
+          offset += index_num_bytes;
+
+          if (item_id == target_item_id) {
+            // const int essential = (value & essential_bit_mask);  // Unused.
+            const uint32_t property_index = (value & ~essential_bit_mask);
+
+            // Parse again at the same "iprp" level to find the associated
+            // "ipco" and the "ispe", "pixi" or "av1C" within.
+            const AvifInfoInternalStatus status =
+                ParseIprpForFeaturesInProperty(bytes, num_bytes, max_num_bytes,
+                                               property_index, num_parsed_boxes,
+                                               features);
+            if (status != kFound) {
+              // Stop in case of error, carry on if not found.
+              AVIFINFO_CHECK_NOT_FOUND(status);
+            } else if (features->width != 0 && features->height != 0 &&
+                       features->num_channels != 0 &&
+                       features->bit_depth != 0) {
+              return kFound;  // Found everything. Otherwise carry on.
+            }
+          }
+        }
+      }
+
+      // According to ISO/IEC 14496-12:2012(E) 8.11.1.1, there is at most one
+      // "meta" per file. According to ISO/IEC 23008-12:2017(E) 9.3.1, there is
+      // exactly one "ipma" per "iprp" and at most one "iprp" per "meta".
+      // The primary properties shall have been found now.
+      if (features->width != 0 && features->height != 0) {
+        // Exception: The bit depth and number of channels may be referenced
+        //            in a tile and not in the primary item of item type "grid".
+        return kNotFound;  // Continue the search at a higher level.
+      }
+      AVIFINFO_RETURN(kInvalid);
+    }
+    position += box.size;
+  } while (position != max_num_bytes);
+  AVIFINFO_RETURN(kInvalid);  // No "ipma" in "iprp" is an issue.
+}
+
+static AvifInfoInternalStatus ParseMetaForFeatures(const uint8_t* bytes,
+                                                   uint32_t num_bytes,
+                                                   uint32_t max_num_bytes,
+                                                   uint32_t target_item_id,
+                                                   uint32_t* num_parsed_boxes,
+                                                   AvifInfoFeatures* features) {
+  uint32_t position = 0;
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, max_num_bytes, position, num_parsed_boxes, &box));
+
+    if (!memcmp(box.type, "iprp", 4)) {
+      return ParseIprpForFeatures(box.content, num_bytes - box.content_position,
+                                  box.content_size, target_item_id,
+                                  num_parsed_boxes, features);
+    }
+    position += box.size;
+  } while (position != max_num_bytes);
+  AVIFINFO_RETURN(kInvalid);  // No "iprp" in "meta" is an issue.
+}
+
+static AvifInfoInternalStatus ParseFileForFeatures(const uint8_t* bytes,
+                                                   uint32_t num_bytes,
+                                                   uint32_t file_size,
+                                                   uint32_t target_item_id,
+                                                   uint32_t* num_parsed_boxes,
+                                                   AvifInfoFeatures* features) {
+  uint32_t position = 0;
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, file_size, position, num_parsed_boxes, &box));
+
+    if (!memcmp(box.type, "meta", 4)) {
+      return ParseMetaForFeatures(box.content, num_bytes - box.content_position,
+                                  box.content_size, target_item_id,
+                                  num_parsed_boxes, features);
+    }
+    position += box.size;
+  } while (position != file_size);
+  AVIFINFO_RETURN(kInvalid);  // No "meta" is an issue.
+}
+
+//------------------------------------------------------------------------------
+// Search if a tile contains features through "meta > iref > dimg" boxes.
+
+static AvifInfoInternalStatus ParseIrefForFeaturesInTiles(
+    const uint8_t* bytes, uint32_t num_bytes, uint32_t max_num_bytes,
+    const uint8_t* meta_bytes, uint32_t meta_num_bytes,
+    uint32_t meta_max_num_bytes, uint32_t primary_item_id,
+    uint32_t* num_parsed_boxes, AvifInfoFeatures* features) {
+  uint32_t position = 0;
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, max_num_bytes, position, num_parsed_boxes, &box));
+
+    if (!memcmp(box.type, "dimg", 4)) {
+      // See ISO/IEC 14496-12:2015(E) 8.11.12.2
+      const uint32_t num_bytes_per_id = (box.version == 0) ? 2 : 4;
+      uint32_t offset = 0;
+      AVIFINFO_CHECK_FOUND(
+          AccessContent(&box, num_bytes, num_bytes_per_id + 2));
+      const uint32_t from_item_id =
+          AvifInfoInternalReadBigEndian(box.content + offset, num_bytes_per_id);
+      offset += num_bytes_per_id;
+      if (from_item_id == primary_item_id) {
+        const uint32_t reference_count =
+            AvifInfoInternalReadBigEndian(box.content + offset, 2);
+        offset += 2;
+        for (uint32_t i = 0; i < reference_count; ++i) {
+          AVIFINFO_CHECK_FOUND(
+              AccessContent(&box, num_bytes, offset + num_bytes_per_id));
+          const uint32_t to_item_id = AvifInfoInternalReadBigEndian(
+              box.content + offset, num_bytes_per_id);
+          offset += num_bytes_per_id;
+          AVIFINFO_CHECK(meta_bytes != NULL && meta_bytes < bytes, kInvalid);
+          AVIFINFO_CHECK(meta_max_num_bytes > 0, kInvalid);
+          // Go up one level: from "dimg" among "iref" to boxes among "meta".
+          AVIFINFO_CHECK_NOT_FOUND(ParseMetaForFeatures(
+              meta_bytes, meta_num_bytes, meta_max_num_bytes, to_item_id,
+              num_parsed_boxes, features));
+          // Trying the first tile should be enough. Check others just in case.
+        }
+      }
+    }
+    position += box.size;
+  } while (position != max_num_bytes);
+  AVIFINFO_RETURN(kNotFound);  // No "dimg" in "iref" is not an issue.
+}
+
+static AvifInfoInternalStatus ParseMetaForFeaturesInTiles(
+    const uint8_t* bytes, uint32_t num_bytes, uint32_t max_num_bytes,
+    uint32_t primary_item_id, uint32_t* num_parsed_boxes,
+    AvifInfoFeatures* features) {
+  uint32_t position = 0;
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, max_num_bytes, position, num_parsed_boxes, &box));
+
+    if (!memcmp(box.type, "iref", 4)) {
+      return ParseIrefForFeaturesInTiles(
+          box.content, num_bytes - box.content_position, box.content_size,
+          bytes, num_bytes, max_num_bytes, primary_item_id, num_parsed_boxes,
+          features);
+    }
+    position += box.size;
+  } while (position != max_num_bytes);
+  AVIFINFO_RETURN(kNotFound);  // No "iref" in "meta" is not an issue.
+}
+
+static AvifInfoInternalStatus ParseFileForFeaturesInTiles(
+    const uint8_t* bytes, uint32_t num_bytes, uint32_t file_size,
+    uint32_t primary_item_id, uint32_t* num_parsed_boxes,
+    AvifInfoFeatures* features) {
+  uint32_t position = 0;
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, file_size, position, num_parsed_boxes, &box));
+
+    if (!memcmp(box.type, "meta", 4)) {
+      return ParseMetaForFeaturesInTiles(
+          box.content, num_bytes - box.content_position, box.content_size,
+          primary_item_id, num_parsed_boxes, features);
+    }
+    position += box.size;
+  } while (position != file_size);
+  AVIFINFO_RETURN(kInvalid);  // No "meta" is an issue.
+}
+
+//------------------------------------------------------------------------------
+// Search if there is an alpha layer through "meta > iprp > ipco > auxC" boxes.
+
+static AvifInfoInternalStatus ParseIpcoForAlpha(const uint8_t* bytes,
+                                                uint32_t num_bytes,
+                                                uint32_t max_num_bytes,
+                                                uint32_t* num_parsed_boxes) {
+  uint32_t position = 0;
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, max_num_bytes, position, num_parsed_boxes, &box));
+
+    if (!memcmp(box.type, "auxC", 4)) {
+      // See AV1 Image File Format (AVIF) 4
+      // at https://aomediacodec.github.io/av1-avif/#auxiliary-images
+      const char* kAlphaStr = "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha";
+      const uint32_t kAlphaStrLength = 44;  // Includes terminating character.
+      if (box.content_size >= kAlphaStrLength) {
+        AVIFINFO_CHECK(box.content_position + kAlphaStrLength <= num_bytes,
+                       kTruncated);
+        const char* const aux_type = (const char*)box.content;
+        if (strcmp(aux_type, kAlphaStr) == 0) {
+          // Note: It is unlikely but it is possible that this alpha plane does
+          //       not belong to the primary item or a tile. Ignore this issue.
+          return kFound;
+        }
+      }
+    }
+    position += box.size;
+  } while (position != max_num_bytes);
+  AVIFINFO_RETURN(kNotFound);  // No "auxC" in "ipco" is not an issue.
+}
+
+static AvifInfoInternalStatus ParseIprpForAlpha(const uint8_t* bytes,
+                                                uint32_t num_bytes,
+                                                uint32_t max_num_bytes,
+                                                uint32_t* num_parsed_boxes) {
+  uint32_t position = 0;
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, max_num_bytes, position, num_parsed_boxes, &box));
+
+    if (!memcmp(box.type, "ipco", 4)) {
+      return ParseIpcoForAlpha(box.content, num_bytes - box.content_position,
+                               box.content_size, num_parsed_boxes);
+    }
+    position += box.size;
+  } while (position != max_num_bytes);
+  AVIFINFO_RETURN(kInvalid);  // No "ipco" in "iprp" is an issue.
+}
+
+static AvifInfoInternalStatus ParseMetaForAlpha(const uint8_t* bytes,
+                                                uint32_t num_bytes,
+                                                uint32_t max_num_bytes,
+                                                uint32_t* num_parsed_boxes) {
+  uint32_t position = 0;
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, max_num_bytes, position, num_parsed_boxes, &box));
+
+    if (!memcmp(box.type, "iprp", 4)) {
+      return ParseIprpForAlpha(box.content, num_bytes - box.content_position,
+                               box.content_size, num_parsed_boxes);
+    }
+    position += box.size;
+  } while (position != max_num_bytes);
+  AVIFINFO_RETURN(kInvalid);  // No "iprp" in "meta" is an issue.
+}
+
+static AvifInfoInternalStatus ParseFileForAlpha(const uint8_t* bytes,
+                                                uint32_t num_bytes,
+                                                uint32_t file_size,
+                                                uint32_t* num_parsed_boxes) {
+  uint32_t position = 0;
+  do {
+    AvifInfoInternalBox box;
+    AVIFINFO_CHECK_FOUND(AvifInfoInternalParseBox(
+        bytes, num_bytes, file_size, position, num_parsed_boxes, &box));
+
+    if (!memcmp(box.type, "meta", 4)) {
+      return ParseMetaForAlpha(box.content, num_bytes - box.content_position,
+                               box.content_size, num_parsed_boxes);
+    }
+    position += box.size;
+  } while (position != file_size);
+  AVIFINFO_RETURN(kInvalid);  // No "meta" is an issue.
+}
+
+//------------------------------------------------------------------------------
+// Parsing starting point.
+
+static AvifInfoInternalStatus AvifInfoInternalParseFile(
+    const uint8_t* bytes, uint32_t num_bytes, uint32_t file_size,
+    AvifInfoFeatures* features) {
+  uint32_t num_parsed_boxes = 0;
+  AVIFINFO_CHECK_FOUND(
+      ParseFileForBrand(bytes, num_bytes, file_size, &num_parsed_boxes));
+
+  // 'bytes' is an AVIF file. Next step is finding the ID of the primary item.
+  uint32_t primary_item_id;
+  AVIFINFO_CHECK_FOUND(ParseFileForPrimaryItemId(
+      bytes, num_bytes, file_size, &num_parsed_boxes, &primary_item_id));
+
+  // Now find the 'features' of the primary item.
+  AvifInfoInternalStatus status =
+      ParseFileForFeatures(bytes, num_bytes, file_size, primary_item_id,
+                           &num_parsed_boxes, features);
+  if (status == kNotFound) {
+    // It is possible that some of the 'features' are missing for the primary
+    // item. Try to look into tiles in case they are defined there.
+    status = ParseFileForFeaturesInTiles(bytes, num_bytes, file_size,
+                                         primary_item_id, &num_parsed_boxes,
+                                         features);
+  }
+  AVIFINFO_CHECK_FOUND(status);
+
+  // If there is an alpha plane, add 1 to the number of channels.
+  status = ParseFileForAlpha(bytes, num_bytes, file_size, &num_parsed_boxes);
+  if (status == kFound) {
+    ++features->num_channels;
+  } else {
+    AVIFINFO_CHECK_NOT_FOUND(status);
+  }
+  return kFound;
+}
+
+//------------------------------------------------------------------------------
+// Public API
+
+AvifInfoStatus AvifInfoGet(const uint8_t* data, size_t data_size,
+                           AvifInfoFeatures* features) {
+  // Consider the file to be of maximum size.
+  return AvifInfoGetWithSize(data, data_size, features,
+                             /*file_size=*/kAvifInfoInternalMaxSize);
+}
+
+AvifInfoStatus AvifInfoGetWithSize(const uint8_t* data, size_t data_size,
+                                   AvifInfoFeatures* features,
+                                   size_t file_size) {
+  if (features != NULL) memset(features, 0, sizeof(*features));
+  if (data == NULL) return kAvifInfoNotEnoughData;
+  if (data_size > file_size) data_size = file_size;
+
+  AvifInfoFeatures parsed_features;
+  memset(&parsed_features, 0, sizeof(parsed_features));
+  const AvifInfoInternalStatus status = AvifInfoInternalParseFile(
+      data,
+      (data_size >= kAvifInfoInternalMaxSize) ? kAvifInfoInternalMaxSize
+                                              : (uint32_t)data_size,
+      (file_size >= kAvifInfoInternalMaxSize) ? kAvifInfoInternalMaxSize
+                                              : (uint32_t)file_size,
+      &parsed_features);
+
+  if (status == kNotFound) {
+    return (data_size < file_size) ? kAvifInfoNotEnoughData
+                                   : kAvifInfoInvalidFile;
+  }
+  if (status == kTruncated) return kAvifInfoNotEnoughData;
+  if (status == kInvalid) return kAvifInfoInvalidFile;
+  if (status == kAborted) return kAvifInfoTooComplex;
+  if (features != NULL) {
+    memcpy(features, &parsed_features, sizeof(*features));
+  }
+  return kAvifInfoOk;
+}
+
+//------------------------------------------------------------------------------
+
+#undef AVIFINFO_RETURN
+#undef AVIFINFO_CHECK
+#undef AVIFINFO_CHECK_STATUS_IS
+#undef AVIFINFO_CHECK_FOUND
+#undef AVIFINFO_CHECK_NOT_FOUND
diff --git a/avifinfo.h b/avifinfo.h
new file mode 100644
index 0000000..ea4d9d7
--- /dev/null
+++ b/avifinfo.h
@@ -0,0 +1,70 @@
+// Copyright (c) 2021, Alliance for Open Media. All rights reserved
+//
+// This source code is subject to the terms of the BSD 2 Clause License and
+// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+// was not distributed with this source code in the LICENSE file, you can
+// obtain it at www.aomedia.org/license/software. If the Alliance for Open
+// Media Patent License 1.0 was not distributed with this source code in the
+// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+
+#ifndef AVIFINFO_H_
+#define AVIFINFO_H_
+
+#include <stddef.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//------------------------------------------------------------------------------
+
+typedef enum {
+  kAvifInfoOk,             // The file was correctly parsed and the requested
+                           // information was extracted. It is not guaranteed
+                           // that the input bitstream is a valid complete
+                           // AVIF file.
+  kAvifInfoNotEnoughData,  // The input bitstream was correctly parsed until
+                           // now but bytes are missing. The request should be
+                           // repeated with more input bytes.
+  kAvifInfoTooComplex,     // The input bitstream was correctly parsed until
+                           // now but it is too complex. The parsing was
+                           // stopped to avoid any timeout or crash.
+  kAvifInfoInvalidFile,    // The input bitstream is not a valid AVIF file,
+                           // truncated or not.
+} AvifInfoStatus;
+
+typedef struct {
+  uint32_t width, height;  // In number of pixels. Ignores mirror and rotation.
+  uint32_t bit_depth;      // Likely 8, 10 or 12 bits per channel per pixel.
+  uint32_t num_channels;   // Likely 1, 2, 3 or 4 channels:
+                           //   (1 monochrome or 3 colors) + (0 or 1 alpha)
+} AvifInfoFeatures;
+
+// Parses the AVIF 'data' and extracts its 'features'.
+// 'data' can be partial but must point to the beginning of the AVIF file.
+// The 'features' can be parsed in the first 450 bytes of most AVIF files.
+// 'features' are set to 0 unless kAvifInfoOk is returned.
+AvifInfoStatus AvifInfoGet(const uint8_t* data, size_t data_size,
+                           AvifInfoFeatures* features);
+
+// Same as above with an extra argument 'file_size'. If the latter is known,
+// please use this version for extra bitstream validation.
+AvifInfoStatus AvifInfoGetWithSize(const uint8_t* data, size_t data_size,
+                                   AvifInfoFeatures* features,
+                                   size_t file_size);
+
+//------------------------------------------------------------------------------
+
+// If needed, avifinfo.h and avifinfo.c can be merged into a single file:
+//   1. Replace this block comment by the content of avifinfo.c
+//   2. Discard #include "./avifinfo.h" and move other includes to the top
+//   3. Mark AvifInfoGet*() declarations and definitions as static
+// This procedure can be useful when only one translation unit uses avifinfo,
+// whether it includes the merged .h or the merged code is inserted into a file.
+
+#ifdef __cplusplus
+}  // extern "C"
+#endif
+
+#endif  // AVIFINFO_H_
diff --git a/tests/avifinfo_test.cc b/tests/avifinfo_test.cc
new file mode 100644
index 0000000..de28ba8
--- /dev/null
+++ b/tests/avifinfo_test.cc
@@ -0,0 +1,181 @@
+// Copyright (c) 2021, Alliance for Open Media. All rights reserved
+//
+// This source code is subject to the terms of the BSD 2 Clause License and
+// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+// was not distributed with this source code in the LICENSE file, you can
+// obtain it at www.aomedia.org/license/software. If the Alliance for Open
+// Media Patent License 1.0 was not distributed with this source code in the
+// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+
+#include "avifinfo.h"
+
+#include <algorithm>
+#include <fstream>
+#include <vector>
+
+#include "gtest/gtest.h"
+
+namespace {
+
+using Data = std::vector<uint8_t>;
+
+Data LoadFile(const char file_name[]) {
+  std::ifstream file(file_name, std::ios::binary | std::ios::ate);
+  if (!file) return Data();
+  const auto file_size = file.tellg();
+  Data bytes(file_size * sizeof(char));
+  file.seekg(0);  // Rewind.
+  return file.read(reinterpret_cast<char*>(bytes.data()), file_size) ? bytes
+                                                                     : Data();
+}
+
+//------------------------------------------------------------------------------
+// Positive tests
+
+TEST(AvifInfoGetTest, WithoutFileSize) {
+  const Data input = LoadFile("avifinfo_test_1x1.avif");
+  ASSERT_FALSE(input.empty());
+
+  AvifInfoFeatures features;
+  EXPECT_EQ(AvifInfoGet(input.data(), input.size(), &features), kAvifInfoOk);
+  EXPECT_EQ(features.width, 1u);
+  EXPECT_EQ(features.height, 1u);
+  EXPECT_EQ(features.bit_depth, 8u);
+  EXPECT_EQ(features.num_channels, 3u);
+}
+
+TEST(AvifInfoGetTest, WithFileSize) {
+  const Data input = LoadFile("avifinfo_test_1x1.avif");
+  ASSERT_FALSE(input.empty());
+
+  AvifInfoFeatures features;
+  EXPECT_EQ(AvifInfoGetWithSize(input.data(), /*data_size=*/input.size(),
+                                &features, /*file_size=*/input.size()),
+            kAvifInfoOk);
+  EXPECT_EQ(features.width, 1u);
+  EXPECT_EQ(features.height, 1u);
+  EXPECT_EQ(features.bit_depth, 8u);
+  EXPECT_EQ(features.num_channels, 3u);
+}
+
+TEST(AvifInfoGetTest, WithShorterSize) {
+  const Data input = LoadFile("avifinfo_test_1x1.avif");
+  ASSERT_FALSE(input.empty());
+
+  AvifInfoFeatures features;
+  // No more than 'file_size' bytes should be read, even if more are passed.
+  EXPECT_EQ(AvifInfoGetWithSize(input.data(), /*data_size=*/input.size() * 10,
+                                &features,
+                                /*file_size=*/input.size()),
+            kAvifInfoOk);
+  EXPECT_EQ(features.width, 1u);
+  EXPECT_EQ(features.height, 1u);
+  EXPECT_EQ(features.bit_depth, 8u);
+  EXPECT_EQ(features.num_channels, 3u);
+}
+
+TEST(AvifInfoGetTest, EnoughBytes) {
+  Data input = LoadFile("avifinfo_test_1x1.avif");
+  ASSERT_FALSE(input.empty());
+  // Truncate 'input' just after the required information (discard AV1 box).
+  const uint8_t kMdatTag[] = {'m', 'd', 'a', 't'};
+  input.resize(std::search(input.begin(), input.end(), kMdatTag, kMdatTag + 4) -
+               input.begin());
+
+  AvifInfoFeatures features;
+  EXPECT_EQ(AvifInfoGet(input.data(), input.size(), &features), kAvifInfoOk);
+  EXPECT_EQ(features.width, 1u);
+  EXPECT_EQ(features.height, 1u);
+  EXPECT_EQ(features.bit_depth, 8u);
+  EXPECT_EQ(features.num_channels, 3u);
+}
+
+TEST(AvifInfoGetTest, Null) {
+  const Data input = LoadFile("avifinfo_test_1x1.avif");
+  ASSERT_FALSE(input.empty());
+
+  EXPECT_EQ(AvifInfoGet(input.data(), input.size(), nullptr), kAvifInfoOk);
+  EXPECT_EQ(
+      AvifInfoGetWithSize(input.data(), input.size(), nullptr, input.size()),
+      kAvifInfoOk);
+}
+
+//------------------------------------------------------------------------------
+// Negative tests
+
+TEST(AvifInfoGetTest, Empty) {
+  AvifInfoFeatures features;
+  EXPECT_EQ(AvifInfoGet(nullptr, 0, &features), kAvifInfoNotEnoughData);
+  EXPECT_EQ(features.width, 0u);
+  EXPECT_EQ(features.height, 0u);
+  EXPECT_EQ(features.bit_depth, 0u);
+  EXPECT_EQ(features.num_channels, 0u);
+}
+
+TEST(AvifInfoGetTest, NotEnoughBytes) {
+  Data input = LoadFile("avifinfo_test_1x1.avif");
+  ASSERT_FALSE(input.empty());
+  // Truncate 'input' before having all the required information.
+  const uint8_t kIpmaTag[] = {'i', 'p', 'm', 'a'};
+  input.resize(std::search(input.begin(), input.end(), kIpmaTag, kIpmaTag + 4) -
+               input.begin());
+
+  AvifInfoFeatures features;
+  EXPECT_EQ(AvifInfoGet(input.data(), input.size(), &features),
+            kAvifInfoNotEnoughData);
+}
+
+TEST(AvifInfoGetTest, Broken) {
+  Data input = LoadFile("avifinfo_test_1x1.avif");
+  ASSERT_FALSE(input.empty());
+  // Change "ispe" to "aspe".
+  const uint8_t kIspeTag[] = {'i', 's', 'p', 'e'};
+  std::search(input.begin(), input.end(), kIspeTag, kIspeTag + 4)[0] = 'a';
+
+  AvifInfoFeatures features;
+  EXPECT_EQ(AvifInfoGet(input.data(), input.size(), &features),
+            kAvifInfoInvalidFile);
+  EXPECT_EQ(features.width, 0u);
+  EXPECT_EQ(features.height, 0u);
+  EXPECT_EQ(features.bit_depth, 0u);
+  EXPECT_EQ(features.num_channels, 0u);
+}
+
+TEST(AvifInfoGetTest, MetaBoxIsTooBig) {
+  Data input = LoadFile("avifinfo_test_1x1.avif");
+  ASSERT_FALSE(input.empty());
+  // Change "meta" box size to the maximum size 2^32-1.
+  const uint8_t kMetaTag[] = {'m', 'e', 't', 'a'};
+  auto meta_tag =
+      std::search(input.begin(), input.end(), kMetaTag, kMetaTag + 4);
+  meta_tag[-4] = meta_tag[-3] = meta_tag[-2] = meta_tag[-1] = 255;
+
+  AvifInfoFeatures features;
+  EXPECT_EQ(AvifInfoGet(input.data(), input.size(), &features),
+            kAvifInfoTooComplex);
+  EXPECT_EQ(features.width, 0u);
+  EXPECT_EQ(features.height, 0u);
+  EXPECT_EQ(features.bit_depth, 0u);
+  EXPECT_EQ(features.num_channels, 0u);
+}
+
+TEST(AvifInfoGetTest, TooManyBoxes) {
+  // Create a valid-ish input with too many boxes to parse.
+  Data input = {0,   0,   0,   16,  'f', 't', 'y', 'p',
+                'a', 'v', 'i', 'f', 0,   0,   0,   0};
+  const uint32_t kNumBoxes = 12345;
+  input.reserve(input.size() + kNumBoxes * 8);
+  for (uint32_t i = 0; i < kNumBoxes; ++i) {
+    const uint8_t kBox[] = {0, 0, 0, 8, 'a', 'b', 'c', 'd'};
+    input.insert(input.end(), kBox, kBox + kBox[3]);
+  }
+
+  AvifInfoFeatures features;
+  EXPECT_EQ(AvifInfoGet(reinterpret_cast<uint8_t*>(input.data()),
+                        input.size() * 4, &features),
+            kAvifInfoTooComplex);
+}
+
+//------------------------------------------------------------------------------
+
+}  // namespace
diff --git a/tests/avifinfo_test_1x1.avif b/tests/avifinfo_test_1x1.avif
new file mode 100644
index 0000000..21707b2
--- /dev/null
+++ b/tests/avifinfo_test_1x1.avif
Binary files differ