/*
 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 3-Clause Clear License
 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
 * License was not distributed with this source code in the LICENSE file, you
 * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/.  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
 * aomedia.org/license/patent-license/.
 */

#include <assert.h>

#include "config/avm_config.h"
#include "config/avm_scale_rtcd.h"

#include "avm/avm_integer.h"
#include "avm_mem/avm_mem.h"
#include "avm_ports/mem.h"
#include "avm_scale/yv12config.h"

static void extend_plane_high(uint16_t *const src, int src_stride, int width,
                              int height, int extend_top, int extend_left,
                              int extend_bottom, int extend_right) {
  int i;
  const int linesize = extend_left + extend_right + width;

  /* copy the left and right most columns out */
  uint16_t *src_ptr1 = src;
  uint16_t *src_ptr2 = src + width - 1;
  uint16_t *dst_ptr1 = src - extend_left;
  uint16_t *dst_ptr2 = src + width;

  for (i = 0; i < height; ++i) {
    avm_memset16(dst_ptr1, src_ptr1[0], extend_left);
    avm_memset16(dst_ptr2, src_ptr2[0], extend_right);
    src_ptr1 += src_stride;
    src_ptr2 += src_stride;
    dst_ptr1 += src_stride;
    dst_ptr2 += src_stride;
  }

  /* Now copy the top and bottom lines into each line of the respective
   * borders
   */
  src_ptr1 = src - extend_left;
  src_ptr2 = src + src_stride * (height - 1) - extend_left;
  dst_ptr1 = src + src_stride * -extend_top - extend_left;
  dst_ptr2 = src + src_stride * height - extend_left;

  for (i = 0; i < extend_top; ++i) {
    memcpy(dst_ptr1, src_ptr1, linesize * sizeof(uint16_t));
    dst_ptr1 += src_stride;
  }

  for (i = 0; i < extend_bottom; ++i) {
    memcpy(dst_ptr2, src_ptr2, linesize * sizeof(uint16_t));
    dst_ptr2 += src_stride;
  }
}

static void extend_frame(YV12_BUFFER_CONFIG *const ybf, int ext_size,
                         const int num_planes) {
  const int ss_x = ybf->uv_width < ybf->y_width;
  const int ss_y = ybf->uv_height < ybf->y_height;

  assert(ybf->y_height - ybf->y_crop_height < 16);
  assert(ybf->y_width - ybf->y_crop_width < 16);
  assert(ybf->y_height - ybf->y_crop_height >= 0);
  assert(ybf->y_width - ybf->y_crop_width >= 0);

  for (int plane = 0; plane < num_planes; ++plane) {
    const int is_uv = plane > 0;
    const int top = ext_size >> (is_uv ? ss_y : 0);
    const int left = ext_size >> (is_uv ? ss_x : 0);
    const int bottom = top;
    const int right = left;
    extend_plane_high(ybf->buffers[plane], ybf->strides[is_uv],
                      ybf->widths[is_uv], ybf->heights[is_uv], top, left,
                      bottom, right);
  }
}

void avm_extend_frame_borders_c(YV12_BUFFER_CONFIG *ybf, const int num_planes,
                                bool decoding) {
  (void)decoding;
  // if (decoding) return;
  extend_frame(ybf, ybf->border, num_planes);
}

void avm_extend_frame_inner_borders_c(YV12_BUFFER_CONFIG *ybf,
                                      const int num_planes) {
  const int inner_bw = (ybf->border > AVMINNERBORDERINPIXELS)
                           ? AVMINNERBORDERINPIXELS
                           : ybf->border;
  extend_frame(ybf, inner_bw, num_planes);
}

void avm_extend_frame_borders_y_c(YV12_BUFFER_CONFIG *ybf) {
  int ext_size = ybf->border;
  assert(ybf->y_height - ybf->y_crop_height < 16);
  assert(ybf->y_width - ybf->y_crop_width < 16);
  assert(ybf->y_height - ybf->y_crop_height >= 0);
  assert(ybf->y_width - ybf->y_crop_width >= 0);

  extend_plane_high(ybf->y_buffer, ybf->y_stride, ybf->y_width, ybf->y_height,
                    ext_size, ext_size, ext_size, ext_size);
}

// Copies the source image into the destination image and updates the
// destination's UMV borders.
// Note: The frames are assumed to be identical in size.
void avm_yv12_copy_frame_c(const YV12_BUFFER_CONFIG *src_bc,
                           YV12_BUFFER_CONFIG *dst_bc, const int num_planes) {
  assert(src_bc->y_width == dst_bc->y_width);
  assert(src_bc->y_height == dst_bc->y_height);

  for (int plane = 0; plane < num_planes; ++plane) {
    const uint16_t *plane_src = src_bc->buffers[plane];
    uint16_t *plane_dst = dst_bc->buffers[plane];
    const int is_uv = plane > 0;

    for (int row = 0; row < src_bc->heights[is_uv]; ++row) {
      memcpy(plane_dst, plane_src, src_bc->widths[is_uv] * sizeof(*plane_dst));
      plane_src += src_bc->strides[is_uv];
      plane_dst += dst_bc->strides[is_uv];
    }
  }
  avm_extend_frame_borders(dst_bc, num_planes, 0);
}

void avm_yv12_copy_y_c(const YV12_BUFFER_CONFIG *src_ybc,
                       YV12_BUFFER_CONFIG *dst_ybc) {
  int row;
  const uint16_t *src = src_ybc->y_buffer;
  uint16_t *dst = dst_ybc->y_buffer;

  for (row = 0; row < src_ybc->y_height; ++row) {
    memcpy(dst, src, src_ybc->y_width * sizeof(*dst));
    src += src_ybc->y_stride;
    dst += dst_ybc->y_stride;
  }
}

void avm_yv12_copy_u_c(const YV12_BUFFER_CONFIG *src_bc,
                       YV12_BUFFER_CONFIG *dst_bc) {
  int row;
  const uint16_t *src = src_bc->u_buffer;
  uint16_t *dst = dst_bc->u_buffer;

  for (row = 0; row < src_bc->uv_height; ++row) {
    memcpy(dst, src, src_bc->uv_width * sizeof(*dst));
    src += src_bc->uv_stride;
    dst += dst_bc->uv_stride;
  }
}

void avm_yv12_copy_v_c(const YV12_BUFFER_CONFIG *src_bc,
                       YV12_BUFFER_CONFIG *dst_bc) {
  int row;
  const uint16_t *src = src_bc->v_buffer;
  uint16_t *dst = dst_bc->v_buffer;

  for (row = 0; row < src_bc->uv_height; ++row) {
    memcpy(dst, src, src_bc->uv_width * sizeof(*dst));
    src += src_bc->uv_stride;
    dst += dst_bc->uv_stride;
  }
}

void avm_yv12_partial_copy_y_c(const YV12_BUFFER_CONFIG *src_ybc, int hstart1,
                               int hend1, int vstart1, int vend1,
                               YV12_BUFFER_CONFIG *dst_ybc, int hstart2,
                               int vstart2) {
  int row;
  const uint16_t *src =
      src_ybc->y_buffer + vstart1 * src_ybc->y_stride + hstart1;
  uint16_t *dst = dst_ybc->y_buffer + vstart2 * dst_ybc->y_stride + hstart2;

  for (row = vstart1; row < vend1; ++row) {
    memcpy(dst, src, (hend1 - hstart1) * sizeof(*dst));
    src += src_ybc->y_stride;
    dst += dst_ybc->y_stride;
  }
}

void avm_yv12_partial_coloc_copy_y_c(const YV12_BUFFER_CONFIG *src_ybc,
                                     YV12_BUFFER_CONFIG *dst_ybc, int hstart,
                                     int hend, int vstart, int vend) {
  avm_yv12_partial_copy_y_c(src_ybc, hstart, hend, vstart, vend, dst_ybc,
                            hstart, vstart);
}

void avm_yv12_partial_copy_u_c(const YV12_BUFFER_CONFIG *src_bc, int hstart1,
                               int hend1, int vstart1, int vend1,
                               YV12_BUFFER_CONFIG *dst_bc, int hstart2,
                               int vstart2) {
  int row;
  const uint16_t *src =
      src_bc->u_buffer + vstart1 * src_bc->uv_stride + hstart1;
  uint16_t *dst = dst_bc->u_buffer + vstart2 * dst_bc->uv_stride + hstart2;

  for (row = vstart1; row < vend1; ++row) {
    memcpy(dst, src, (hend1 - hstart1) * sizeof(*dst));
    src += src_bc->uv_stride;
    dst += dst_bc->uv_stride;
  }
}

void avm_yv12_partial_coloc_copy_u_c(const YV12_BUFFER_CONFIG *src_bc,
                                     YV12_BUFFER_CONFIG *dst_bc, int hstart,
                                     int hend, int vstart, int vend) {
  avm_yv12_partial_copy_u_c(src_bc, hstart, hend, vstart, vend, dst_bc, hstart,
                            vstart);
}

void avm_yv12_partial_copy_v_c(const YV12_BUFFER_CONFIG *src_bc, int hstart1,
                               int hend1, int vstart1, int vend1,
                               YV12_BUFFER_CONFIG *dst_bc, int hstart2,
                               int vstart2) {
  int row;
  const uint16_t *src =
      src_bc->v_buffer + vstart1 * src_bc->uv_stride + hstart1;
  uint16_t *dst = dst_bc->v_buffer + vstart2 * dst_bc->uv_stride + hstart2;

  for (row = vstart1; row < vend1; ++row) {
    memcpy(dst, src, (hend1 - hstart1) * sizeof(*dst));
    src += src_bc->uv_stride;
    dst += dst_bc->uv_stride;
  }
}

void avm_yv12_partial_coloc_copy_v_c(const YV12_BUFFER_CONFIG *src_bc,
                                     YV12_BUFFER_CONFIG *dst_bc, int hstart,
                                     int hend, int vstart, int vend) {
  avm_yv12_partial_copy_v_c(src_bc, hstart, hend, vstart, vend, dst_bc, hstart,
                            vstart);
}

int avm_yv12_realloc_with_new_border_c(YV12_BUFFER_CONFIG *ybf, int new_border,
                                       int byte_alignment, int num_planes,
                                       bool alloc_pyramid) {
  if (ybf) {
    if (new_border == ybf->border) return 0;
    YV12_BUFFER_CONFIG new_buf;
    memset(&new_buf, 0, sizeof(new_buf));
    const int error = avm_alloc_frame_buffer(
        &new_buf, ybf->y_width, ybf->y_height, ybf->subsampling_x,
        ybf->subsampling_y, new_border, byte_alignment, alloc_pyramid);
    if (error) return error;
    // Copy image buffer
    avm_yv12_copy_frame(ybf, &new_buf, num_planes);

    // Now free the old buffer and replace with the new
    avm_free_frame_buffer(ybf);
    memcpy(ybf, &new_buf, sizeof(new_buf));
    return 0;
  }
  return -2;
}
