/*
 * 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 <array>
#include <climits>

#include "aom/aom_image.h"
#include "gtest/gtest.h"

TEST(AomImageTest, AomImgWrapInvalidAlign) {
  const int kWidth = 128;
  const int kHeight = 128;
  unsigned char buf[kWidth * kHeight * 3];

  aom_image_t img;
  // Set img_data and img_data_owner to junk values. aom_img_wrap() should
  // not read these values on failure.
  img.img_data = (unsigned char *)"";
  img.img_data_owner = 1;

  aom_img_fmt_t format = AOM_IMG_FMT_I444;
  // 'align' must be a power of 2 but is not. This causes the aom_img_wrap()
  // call to fail. The test verifies we do not read the junk values in 'img'.
  unsigned int align = 31;
  EXPECT_EQ(aom_img_wrap(&img, format, kWidth, kHeight, align, buf), nullptr);
}

TEST(AomImageTest, AomImgSetRectOverflow) {
  const int kWidth = 128;
  const int kHeight = 128;
  unsigned char buf[kWidth * kHeight * 3];

  aom_image_t img;
  aom_img_fmt_t format = AOM_IMG_FMT_I444;
  unsigned int align = 32;
  EXPECT_EQ(aom_img_wrap(&img, format, kWidth, kHeight, align, buf), &img);

  EXPECT_EQ(aom_img_set_rect(&img, 0, 0, kWidth, kHeight, 0), 0);
  // This would result in overflow because -1 is cast to UINT_MAX.
  EXPECT_NE(aom_img_set_rect(&img, static_cast<unsigned int>(-1),
                             static_cast<unsigned int>(-1), kWidth, kHeight, 0),
            0);
}

TEST(AomImageTest, AomImgAllocInvalidImageFormats) {
  static constexpr std::array<int, 7> kImageFormats = {
    AOM_IMG_FMT_NONE,       AOM_IMG_FMT_NONE - 1,   AOM_IMG_FMT_NV12 + 1,
    AOM_IMG_FMT_I42016 - 1, AOM_IMG_FMT_I44416 + 1, AOM_IMG_FMT_AOMYV12,
    AOM_IMG_FMT_AOMI420
  };

  for (const auto img_fmt : kImageFormats) {
    EXPECT_EQ(
        aom_img_alloc(/*img=*/nullptr, static_cast<aom_img_fmt_t>(img_fmt),
                      /*d_w=*/32, /*d_h=*/32, /*align=*/1),
        nullptr);
  }
}

TEST(AomImageTest, AomImgAllocNv12) {
  const int kWidth = 128;
  const int kHeight = 128;

  aom_image_t img;
  aom_img_fmt_t format = AOM_IMG_FMT_NV12;
  unsigned int align = 32;
  EXPECT_EQ(aom_img_alloc(&img, format, kWidth, kHeight, align), &img);
  EXPECT_EQ(img.stride[AOM_PLANE_U], img.stride[AOM_PLANE_Y]);
  EXPECT_EQ(img.stride[AOM_PLANE_V], 0);
  EXPECT_EQ(img.planes[AOM_PLANE_V], nullptr);
  aom_img_free(&img);
}

TEST(AomImageTest, AomImgAllocHugeWidth) {
  // The stride (0x80000000 * 2) would overflow unsigned int.
  aom_image_t *image =
      aom_img_alloc(nullptr, AOM_IMG_FMT_I42016, 0x80000000, 1, 1);
  ASSERT_EQ(image, nullptr);

  // The stride (0x80000000) would overflow int.
  image = aom_img_alloc(nullptr, AOM_IMG_FMT_I420, 0x80000000, 1, 1);
  ASSERT_EQ(image, nullptr);

  // The aligned width (UINT_MAX + 1) would overflow unsigned int.
  image = aom_img_alloc(nullptr, AOM_IMG_FMT_I420, UINT_MAX, 1, 1);
  ASSERT_EQ(image, nullptr);

  image = aom_img_alloc_with_border(nullptr, AOM_IMG_FMT_I422, 1, INT_MAX, 1,
                                    0x40000000, 0);
  if (image) {
    uint16_t *y_plane =
        reinterpret_cast<uint16_t *>(image->planes[AOM_PLANE_Y]);
    y_plane[0] = 0;
    y_plane[image->d_w - 1] = 0;
    aom_img_free(image);
  }

  image = aom_img_alloc(nullptr, AOM_IMG_FMT_I420, 0x7ffffffe, 1, 1);
  if (image) {
    aom_img_free(image);
  }

  image = aom_img_alloc(nullptr, AOM_IMG_FMT_I420, 285245883, 64, 1);
  if (image) {
    aom_img_free(image);
  }

  image = aom_img_alloc(nullptr, AOM_IMG_FMT_NV12, 285245883, 64, 1);
  if (image) {
    aom_img_free(image);
  }

  image = aom_img_alloc(nullptr, AOM_IMG_FMT_YV12, 285245883, 64, 1);
  if (image) {
    aom_img_free(image);
  }

  image = aom_img_alloc(nullptr, AOM_IMG_FMT_I42016, 65536, 2, 1);
  if (image) {
    uint16_t *y_plane =
        reinterpret_cast<uint16_t *>(image->planes[AOM_PLANE_Y]);
    y_plane[0] = 0;
    y_plane[image->d_w - 1] = 0;
    aom_img_free(image);
  }

  image = aom_img_alloc(nullptr, AOM_IMG_FMT_I42016, 285245883, 2, 1);
  if (image) {
    uint16_t *y_plane =
        reinterpret_cast<uint16_t *>(image->planes[AOM_PLANE_Y]);
    y_plane[0] = 0;
    y_plane[image->d_w - 1] = 0;
    aom_img_free(image);
  }
}

TEST(AomImageTest, AomImgFlipNoAlpha) {
  aom_image_t *img = aom_img_alloc(nullptr, AOM_IMG_FMT_I420, 64, 64, 16);
  ASSERT_NE(img, nullptr);
  aom_img_flip(img);
  aom_img_free(img);
}

TEST(AomImageTest, AomImgFlipOneRow) {
  aom_image_t *img = aom_img_alloc(nullptr, AOM_IMG_FMT_I420, 16, 1, 1);
  ASSERT_NE(img, nullptr);
  unsigned char *const y_plane = img->planes[AOM_PLANE_Y];
  unsigned char *const u_plane = img->planes[AOM_PLANE_U];
  unsigned char *const v_plane = img->planes[AOM_PLANE_V];
  const int y_stride = img->stride[AOM_PLANE_Y];
  const int u_stride = img->stride[AOM_PLANE_U];
  const int v_stride = img->stride[AOM_PLANE_V];

  aom_img_flip(img);

  EXPECT_EQ(img->planes[AOM_PLANE_Y], y_plane);
  EXPECT_EQ(img->planes[AOM_PLANE_U], u_plane);
  EXPECT_EQ(img->planes[AOM_PLANE_V], v_plane);
  EXPECT_EQ(img->stride[AOM_PLANE_Y], -y_stride);
  EXPECT_EQ(img->stride[AOM_PLANE_U], -u_stride);
  EXPECT_EQ(img->stride[AOM_PLANE_V], -v_stride);

  aom_img_flip(img);

  EXPECT_EQ(img->planes[AOM_PLANE_Y], y_plane);
  EXPECT_EQ(img->planes[AOM_PLANE_U], u_plane);
  EXPECT_EQ(img->planes[AOM_PLANE_V], v_plane);
  EXPECT_EQ(img->stride[AOM_PLANE_Y], y_stride);
  EXPECT_EQ(img->stride[AOM_PLANE_U], u_stride);
  EXPECT_EQ(img->stride[AOM_PLANE_V], v_stride);

  aom_img_free(img);
}

TEST(AomImageTest, AomImgFlipOddHeight) {
  static constexpr aom_img_fmt_t kFormats[] = {
    AOM_IMG_FMT_YV12,   AOM_IMG_FMT_I420,   AOM_IMG_FMT_NV12,
    AOM_IMG_FMT_I42016, AOM_IMG_FMT_YV1216,
  };

  for (const aom_img_fmt_t format : kFormats) {
    aom_image_t *img = aom_img_alloc(nullptr, format, 16, 3, 1);
    ASSERT_NE(img, nullptr);
    unsigned char *const y_plane = img->planes[AOM_PLANE_Y];
    unsigned char *const u_plane = img->planes[AOM_PLANE_U];
    unsigned char *const v_plane = img->planes[AOM_PLANE_V];
    const int y_stride = img->stride[AOM_PLANE_Y];
    const int u_stride = img->stride[AOM_PLANE_U];
    const int v_stride = img->stride[AOM_PLANE_V];

    aom_img_flip(img);

    EXPECT_EQ(img->planes[AOM_PLANE_Y], y_plane + 2 * y_stride);
    EXPECT_EQ(img->planes[AOM_PLANE_U], u_plane + u_stride);
    EXPECT_EQ(img->planes[AOM_PLANE_V], v_plane + v_stride);
    EXPECT_EQ(img->stride[AOM_PLANE_Y], -y_stride);
    EXPECT_EQ(img->stride[AOM_PLANE_U], -u_stride);
    EXPECT_EQ(img->stride[AOM_PLANE_V], -v_stride);

    aom_img_flip(img);

    EXPECT_EQ(img->planes[AOM_PLANE_Y], y_plane);
    EXPECT_EQ(img->planes[AOM_PLANE_U], u_plane);
    EXPECT_EQ(img->planes[AOM_PLANE_V], v_plane);
    EXPECT_EQ(img->stride[AOM_PLANE_Y], y_stride);
    EXPECT_EQ(img->stride[AOM_PLANE_U], u_stride);
    EXPECT_EQ(img->stride[AOM_PLANE_V], v_stride);

    aom_img_free(img);
  }
}
