/*
 *  Copyright 2017 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

package tee3.webrtc;

import java.nio.ByteBuffer;

/** Wraps libyuv methods to Java. All passed byte buffers must be direct byte buffers. */
public class YuvHelper {
  /** Helper method for copying I420 to tightly packed destination buffer. */
  public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
      ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int width, int height) {
    final int chromaHeight = (height + 1) / 2;
    final int chromaWidth = (width + 1) / 2;

    final int minSize = width * height + chromaWidth * chromaHeight * 2;
    if (dst.capacity() < minSize) {
      throw new IllegalArgumentException("Expected destination buffer capacity to be at least "
          + minSize + " was " + dst.capacity());
    }

    final int startY = 0;
    final int startU = height * width;
    final int startV = startU + chromaHeight * chromaWidth;

    dst.position(startY);
    final ByteBuffer dstY = dst.slice();
    dst.position(startU);
    final ByteBuffer dstU = dst.slice();
    dst.position(startV);
    final ByteBuffer dstV = dst.slice();

    nativeI420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, width, dstU,
        chromaWidth, dstV, chromaWidth, width, height);
  }

  /** Helper method for copying I420 to tightly packed NV12 destination buffer. */
  public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
      ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int width, int height) {
    final int chromaWidth = (width + 1) / 2;
    final int chromaHeight = (height + 1) / 2;

    final int minSize = width * height + chromaWidth * chromaHeight * 2;
    if (dst.capacity() < minSize) {
      throw new IllegalArgumentException("Expected destination buffer capacity to be at least "
          + minSize + " was " + dst.capacity());
    }

    final int startY = 0;
    final int startUV = height * width;

    dst.position(startY);
    final ByteBuffer dstY = dst.slice();
    dst.position(startUV);
    final ByteBuffer dstUV = dst.slice();

    nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, width, dstUV,
        chromaWidth * 2, width, height);
  }

  /** Helper method for rotating I420 to tightly packed destination buffer. */
  public static void I420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
      ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int srcWidth, int srcHeight,
      int rotationMode) {
    final int dstWidth = rotationMode % 180 == 0 ? srcWidth : srcHeight;
    final int dstHeight = rotationMode % 180 == 0 ? srcHeight : srcWidth;

    final int dstChromaHeight = (dstHeight + 1) / 2;
    final int dstChromaWidth = (dstWidth + 1) / 2;

    final int minSize = dstWidth * dstHeight + dstChromaWidth * dstChromaHeight * 2;
    if (dst.capacity() < minSize) {
      throw new IllegalArgumentException("Expected destination buffer capacity to be at least "
          + minSize + " was " + dst.capacity());
    }

    final int startY = 0;
    final int startU = dstHeight * dstWidth;
    final int startV = startU + dstChromaHeight * dstChromaWidth;

    dst.position(startY);
    final ByteBuffer dstY = dst.slice();
    dst.position(startU);
    final ByteBuffer dstU = dst.slice();
    dst.position(startV);
    final ByteBuffer dstV = dst.slice();

    nativeI420Rotate(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstWidth, dstU,
        dstChromaWidth, dstV, dstChromaWidth, srcWidth, srcHeight, rotationMode);
  }

  /** Helper method for copying a single colour plane. */
  public static void copyPlane(
      ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height) {
    nativeCopyPlane(src, srcStride, dst, dstStride, width, height);
  }

  public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
      ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU,
      int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height) {
    nativeI420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU,
        dstStrideU, dstV, dstStrideV, width, height);
  }

  public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
      ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstUV,
      int dstStrideUV, int width, int height) {
    nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstUV,
        dstStrideUV, width, height);
  }

  public static void I420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
      ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU,
      int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight,
      int rotationMode) {
    nativeI420Rotate(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU,
        dstStrideU, dstV, dstStrideV, srcWidth, srcHeight, rotationMode);
  }


  public static void I420Scale(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
      ByteBuffer srcV, int srcStrideV, int src_width, int src_height, int src_x, int src_y, int src_w, int src_h, ByteBuffer dst, int width, int height) {
    final int chromaHeight = (height + 1) / 2;
    final int chromaWidth = (width + 1) / 2;

    final int minSize = width * height + chromaWidth * chromaHeight * 2;
    if (dst.capacity() < minSize) {
      throw new IllegalArgumentException("Expected destination buffer capacity to be at least "
          + minSize + " was " + dst.capacity());
    }

    final int startY = 0;
    final int startU = height * width;
    final int startV = startU + chromaHeight * chromaWidth;

    dst.position(startY);
    final ByteBuffer dstY = dst.slice();
    dst.position(startU);
    final ByteBuffer dstU = dst.slice();
    dst.position(startV);
    final ByteBuffer dstV = dst.slice();

    nativeI420Scale(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, src_width,src_height, src_x, src_y, src_w, src_h, 
		dstY, width, dstU, chromaWidth, dstV, chromaWidth, width, height, false);
  }

  public static void I420Scale(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
      ByteBuffer srcV, int srcStrideV, int src_width, int src_height, int src_x, int src_y, int src_w, int src_h, ByteBuffer dstY, ByteBuffer dstU, ByteBuffer dstV, int width, int height) {
    final int chromaHeight = (height + 1) / 2;
    final int chromaWidth = (width + 1) / 2;

    final int minSizeY = width * height;
    final int minSizeUV = chromaWidth * chromaHeight;

    if (dstY.capacity() < minSizeY || dstU.capacity() < minSizeUV || dstV.capacity() < minSizeUV) {
      throw new IllegalArgumentException("Expected destination buffer capacity to be at least "
              + minSizeY + " was " + dstY.capacity());
    }

    nativeI420Scale(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, src_width,src_height, src_x, src_y, src_w, src_h,
		dstY, width, dstU, chromaWidth, dstV, chromaWidth, width, height, false);
  }

  public static void bmp2Yuv(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV,
                             int src_width, int src_height, ByteBuffer strData, int datalen, int format) {
    nativeRgbToYuv(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, src_width, src_height, strData, datalen, format);
  }

//  public static void bmp2Yuv(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
//                             ByteBuffer srcV, int srcStrideV, int src_width, int src_height, int src_x, int src_y, int src_w, int src_h, ByteBuffer dstY, ByteBuffer dstU, ByteBuffer dstV, int width, int height) {
//      nativeI420Scale(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, src_width,src_height, src_x, src_y, src_w, src_h,
//              dstY, width, dstU, chromaWidth, dstV, chromaWidth, width, height, false);
//  }

  public static ByteBuffer videoFrame2ARGB(VideoFrame frame, int cropX, int cropY, int cropWidth, int cropHeight, int rotation) {
    VideoFrame.Buffer buffer = frame.getBuffer();
    VideoFrame.I420Buffer i420Buffer = buffer.toI420();

    ByteBuffer dataY = i420Buffer.getDataY();
    ByteBuffer dataU = i420Buffer.getDataU();
    ByteBuffer dataV = i420Buffer.getDataV();
    ByteBuffer orgBuffer = ByteBuffer.allocate(dataY.capacity() + dataU.capacity() + dataV.capacity());

    orgBuffer.put(dataY)
            .put(dataU)
            .put(dataV);

    int width = buffer.getWidth();
    int height = buffer.getHeight();
    ByteBuffer dstBuffer = ByteBuffer.allocate(width * height * 4);

    nativeConvertToARGB(orgBuffer, orgBuffer.capacity(), dstBuffer, dstBuffer.capacity(), cropX, cropY, width, height, cropWidth, cropHeight, rotation, 0);
    return dstBuffer;
  }

  private static native void nativeCopyPlane(
      ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height);
  private static native void nativeI420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU,
      int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY,
      ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height);
  private static native void nativeI420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU,
      int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY,
      ByteBuffer dstUV, int dstStrideUV, int width, int height);
  private static native void nativeI420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU,
      int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY,
      ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight,
      int rotationMode);
  private static native void nativeI420Scale(
	  ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, int src_width, int src_height, int src_x, int src_y, int src_w, int src_h,
	  ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int dst_w, int dst_h, boolean vert_crop);

//  private static native void nativeRgbToYuv(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV,
//                                            int src_width, int src_height, ByteBuffer strData, int datalen, int format);
//  private static native void nativeRgbToYuv2(ByteBuffer srcY, ByteBuffer srcU, ByteBuffer srcV, ByteBuffer strData, int width, int height, int format);

    private static native void nativeRgbToYuv(ByteBuffer dstY, int srcStrideY, ByteBuffer dstU, int srcStrideU, ByteBuffer dstV, int srcStrideV,
                                              int src_width, int src_height, ByteBuffer argbData, int datalen, int format);
//  private static native void nativeRgbToYuv2(ByteBuffer srcY, ByteBuffer srcU, ByteBuffer srcV, int[] strData, int width, int height, int format);

  private static native void nativeConvertToARGB(ByteBuffer  sample/*YUV拼接buffer*/, int sample_size/*YUV长度*/, ByteBuffer dst_argb/*ARGB数组*/, int dst_stride_argb/*ARGB数组长度*/,
                                                 int crop_x/*起始坐标x*/, int crop_y/*起始坐标y*/, int src_width/*原始宽度*/, int src_height/*原始高度*/, int crop_width/*目标宽度*/, int crop_height/*目标高度*/,
                                                 int rotation_mode/*旋转角度（0）*/, int fourcc/*yuv数据输入格式（0）*/ );
}
