package com.lmy.lymedia.utils;

import android.util.Log;

import org.bytedeco.javacv.Frame;

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

/**
 * Created by Administrator on 2016/3/21.
 */
public class FrameUtil {
    public static int[] RGB242RGB565(int[] rgb) {
        rgb[0] = (rgb[0] << 8) & 0xF800;
        rgb[1] = (rgb[1] << 3) & 0x7E0;
        rgb[2] = rgb[2] >> 3;
        return rgb;
    }

    public static ByteBuffer RGB242RGB565(ByteBuffer buffer, int lenght) {
        for (int i = 0; i < lenght; i++) {
            if (i % 3 == 0)
                buffer.put(i, (byte) ((buffer.get(i) << 8) & 0xF800));
            if (i % 3 == 1)
                buffer.put(i, (byte) ((buffer.get(i) << 3) & 0x7E0));
            if (i % 3 == 2)
                buffer.put(i, (byte) (buffer.get(i) >> 3));
        }
        return buffer;
    }


    public static int frameType(Frame frame) {
        if (frame == null) return -1;
        if (frame.image != null) return 0;
        else if (frame.samples != null) return 1;
        else return -1;
    }

    public static Frame copy(Frame frame) {
        Frame tmp = new Frame();
        int type = frameType(frame);
        if (type == 0) {
            tmp = new Frame(frame.imageWidth, frame.imageHeight, frame.imageDepth, frame.imageChannels);
            tmp.image = new Buffer[1];
            ByteBuffer in = (ByteBuffer) frame.image[0].position(0);
            tmp.image[0] = copy(in);
        } else {
            tmp.samples = new Buffer[frame.samples.length];
            for (int i = 0; i < tmp.samples.length; i++)
                tmp.samples[i] = copy((FloatBuffer) frame.samples[i].position(0));
        }
        return tmp;
    }

    public static ByteBuffer copy(ByteBuffer src) {
        byte[] data = new byte[src.capacity()];
        src.get(data);
        return ByteBuffer.wrap(data);
    }

    public static FloatBuffer copy(FloatBuffer src) {
        float[] data = new float[src.capacity()];
        src.get(data);
        return FloatBuffer.wrap(data);
    }
}

@@ -1353,149 +1353,168 @@
 mage
- = new Buffer%5B1%5D;%0A            ByteBuffer in = (ByteBuffer) frame.image%5B0%5D.position(0);%0A            tmp.image%5B0%5D = copy(in);%0A        %7D else %7B%0A
+%5B0%5D = copy((ByteBuffer) frame.image%5B0%5D);%0A        %7D else %7B%0A            tmp.sampleRate = frame.sampleRate;%0A            tmp.audioChannels = frame.audioChannels;%0A//
     
@@ -1539,19 +1539,8 @@
 s = 
-new Buffer%5B
 fram
@@ -1553,15 +1553,15 @@
 les.
-length%5D
+clone()
 ;%0A  
@@ -1799,79 +1799,603 @@
     
-byte%5B%5D data = new byte%5Bsrc.capacity()%5D;%0A        src.get(data
+// Create the clone buffer with same capacity as the original%0A        ByteBuffer cloneBuffer = ByteBuffer.allocateDirect(src.capacity());%0A        //ByteBuffer cloneBuffer = deepCopy(originalByteBuffer);%0A%0A        // Save parameters from the original byte buffer%0A        int position = src.position();%0A        int limit = src.limit();%0A%0A        // Set range to the entire buffer%0A        src.position(0).limit(src.capacity());%0A%0A        // Read from original and put into clone%0A        cloneBuffer.put(src);%0A%0A        // Set the order same as original%0A        cloneBuffer.order(src.order()
 );%0A
+%0A
         
 retu
@@ -2394,132 +2394,370 @@
     
-return ByteBuffer.wrap(data);%0A    %7D%0A%0A    public static FloatBuffer copy(FloatBuffer src) %7B%0A        float%5B%5D data = new f
+// Set clone position to 0 and set the range as the original%0A        cloneBuffer.position(0);%0A        cloneBuffer.position(position).limit(limit);%0A%0A        return cloneBuffer;%0A    %7D%0A%0A    public static FloatBuffer copy(FloatBuffer src) %7B%0A        // Create the clone buffer with same capacity as the original%0A        FloatBuffer cloneBuffer = FloatBuffer.al
 lo
+c
 at
-%5B
+e(
 src.
@@ -2770,42 +2770,385 @@
 ty()
-%5D;%0A        src.get(data
+);%0A        //ByteBuffer cloneBuffer = deepCopy(originalByteBuffer);%0A%0A        // Save parameters from the original byte buffer%0A        int position = src.position();%0A        int limit = src.limit();%0A%0A        // Set range to the entire buffer%0A        src.position(0).limit(src.capacity());%0A%0A        // Read from original and put into clone%0A        cloneBuffer.put(src
 );%0A
+%0A
         
 retu
@@ -3147,37 +3147,267 @@
     
-return FloatBuffer.wrap(data)
+// Set the order same as original%0A//        cloneBuffer.order(src.order());%0A%0A        // Set clone position to 0 and set the range as the original%0A        cloneBuffer.position(0);%0A        cloneBuffer.position(position).limit(limit);%0A%0A        return cloneBuffer
 ;%0A  

优化copy，未测试