1 /*
2  * Copyright 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.media.cts;
18 
19 import android.graphics.Rect;
20 import android.media.cts.CodecImage;
21 import android.media.Image;
22 import android.util.Log;
23 
24 import java.nio.ByteBuffer;
25 
26 public class CodecUtils  {
27     private static final String TAG = "CodecUtils";
28 
29     /** Load jni on initialization */
30     static {
Log.i(TAG, "before loadlibrary")31         Log.i(TAG, "before loadlibrary");
32         System.loadLibrary("ctsmediacodec_jni");
Log.i(TAG, "after loadlibrary")33         Log.i(TAG, "after loadlibrary");
34     }
35 
36     private static class ImageWrapper extends CodecImage {
37         private final Image mImage;
38         private final Plane[] mPlanes;
39 
ImageWrapper(Image image)40         private ImageWrapper(Image image) {
41             mImage = image;
42             Image.Plane[] planes = mImage.getPlanes();
43 
44             mPlanes = new Plane[planes.length];
45             for (int i = 0; i < planes.length; i++) {
46                 mPlanes[i] = new PlaneWrapper(planes[i]);
47             }
48         }
49 
createFromImage(Image image)50         public static ImageWrapper createFromImage(Image image) {
51             return new ImageWrapper(image);
52         }
53 
54         @Override
getFormat()55         public int getFormat() {
56             return mImage.getFormat();
57         }
58 
59         @Override
getWidth()60         public int getWidth() {
61             return mImage.getWidth();
62         }
63 
64         @Override
getHeight()65         public int getHeight() {
66             return mImage.getHeight();
67         }
68 
69         @Override
getTimestamp()70         public long getTimestamp() {
71             return mImage.getTimestamp();
72         }
73 
74         @Override
getPlanes()75         public Plane[] getPlanes() {
76             return mPlanes;
77         }
78 
79         @Override
close()80         public void close() {
81             mImage.close();
82         }
83 
84         private static class PlaneWrapper extends CodecImage.Plane {
85             private final Image.Plane mPlane;
86 
PlaneWrapper(Image.Plane plane)87             PlaneWrapper(Image.Plane plane) {
88                 mPlane = plane;
89             }
90 
91             @Override
getRowStride()92             public int getRowStride() {
93                 return mPlane.getRowStride();
94             }
95 
96            @Override
getPixelStride()97             public int getPixelStride() {
98                return mPlane.getPixelStride();
99             }
100 
101             @Override
getBuffer()102             public ByteBuffer getBuffer() {
103                 return mPlane.getBuffer();
104             }
105         }
106     }
107 
108 
getImageChecksum(CodecImage image)109     public native static int getImageChecksum(CodecImage image);
copyFlexYUVImage(CodecImage target, CodecImage source)110     public native static void copyFlexYUVImage(CodecImage target, CodecImage source);
111 
copyFlexYUVImage(Image target, CodecImage source)112     public static void copyFlexYUVImage(Image target, CodecImage source) {
113         copyFlexYUVImage(ImageWrapper.createFromImage(target), source);
114     }
copyFlexYUVImage(Image target, Image source)115     public static void copyFlexYUVImage(Image target, Image source) {
116         copyFlexYUVImage(
117                 ImageWrapper.createFromImage(target),
118                 ImageWrapper.createFromImage(source));
119     }
120 
fillImageRectWithYUV( CodecImage image, Rect area, int y, int u, int v)121     public native static void fillImageRectWithYUV(
122             CodecImage image, Rect area, int y, int u, int v);
123 
fillImageRectWithYUV(Image image, Rect area, int y, int u, int v)124     public static void fillImageRectWithYUV(Image image, Rect area, int y, int u, int v) {
125         fillImageRectWithYUV(ImageWrapper.createFromImage(image), area, y, u, v);
126     }
127 
getRawStats(CodecImage image, Rect area)128     public native static long[] getRawStats(CodecImage image, Rect area);
129 
getRawStats(Image image, Rect area)130     public static long[] getRawStats(Image image, Rect area) {
131         return getRawStats(ImageWrapper.createFromImage(image), area);
132     }
133 
getYUVStats(CodecImage image, Rect area)134     public native static float[] getYUVStats(CodecImage image, Rect area);
135 
getYUVStats(Image image, Rect area)136     public static float[] getYUVStats(Image image, Rect area) {
137         return getYUVStats(ImageWrapper.createFromImage(image), area);
138     }
139 
Raw2YUVStats(long[] rawStats)140     public native static float[] Raw2YUVStats(long[] rawStats);
141 }
142 
143