1b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala/* 2b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * Copyright (C) 2013 The Android Open Source Project 3b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 4b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * Licensed under the Apache License, Version 2.0 (the "License"); 5b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * you may not use this file except in compliance with the License. 6b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * You may obtain a copy of the License at 7b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 8b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * http://www.apache.org/licenses/LICENSE-2.0 9b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 10b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * Unless required by applicable law or agreed to in writing, software 11b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * distributed under the License is distributed on an "AS IS" BASIS, 12b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * See the License for the specific language governing permissions and 14b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * limitations under the License. 15b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala */ 16b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala 17b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvalapackage android.media; 18b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala 19b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvalaimport java.nio.ByteBuffer; 20b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvalaimport java.lang.AutoCloseable; 21b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala 22b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala/** 23b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <p>A single complete image buffer to use with a media source such as a 24cd925885283396c0379ec2dcaa94e318993f40f8Eino-Ville Talvala * {@link MediaCodec}.</p> 25b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 26b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <p>This class allows for efficient direct application access to the pixel 27b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * data of the Image through one or more 28b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * {@link java.nio.ByteBuffer ByteBuffers}. Each buffer is encapsulated in a 29b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * {@link Plane} that describes the layout of the pixel data in that plane. Due 305e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * to this direct access, and unlike the {@link android.graphics.Bitmap Bitmap} class, 31b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * Images are not directly usable as as UI resources.</p> 32b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 33b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <p>Since Images are often directly produced or consumed by hardware 34b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * components, they are a limited resource shared across the system, and should 35b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * be closed as soon as they are no longer needed.</p> 36b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 37b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <p>For example, when using the {@link ImageReader} class to read out Images 38b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * from various media sources, not closing old Image objects will prevent the 39b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * availability of new Images once 40b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * {@link ImageReader#getMaxImages the maximum outstanding image count} is 415e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * reached. When this happens, the function acquiring new Images will typically 42e3351f1942bfe86682389b278e7ff128a72ea671Igor Murashkin * throw an {@link IllegalStateException}.</p> 43b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 44b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * @see ImageReader 45b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala */ 465e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkinpublic abstract class Image implements AutoCloseable { 475e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin /** 485e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * @hide 495e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin */ 505e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin protected Image() { 515e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin } 525e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin 53b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala /** 54b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * Get the format for this image. This format determines the number of 55b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * ByteBuffers needed to represent the image, and the general layout of the 56b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * pixel data in each in ByteBuffer. 57b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 585e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * <p> 59b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * The format is one of the values from 605e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * {@link android.graphics.ImageFormat ImageFormat}. The mapping between the 615e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * formats and the planes is as follows: 625e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * </p> 63b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 64b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <table> 65af753a2dfc66c92bfcac64b77c7a4d89d9434ad8Zhijun He * <tr> 66af753a2dfc66c92bfcac64b77c7a4d89d9434ad8Zhijun He * <th>Format</th> 67af753a2dfc66c92bfcac64b77c7a4d89d9434ad8Zhijun He * <th>Plane count</th> 68af753a2dfc66c92bfcac64b77c7a4d89d9434ad8Zhijun He * <th>Layout details</th> 69af753a2dfc66c92bfcac64b77c7a4d89d9434ad8Zhijun He * </tr> 70b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <tr> 715e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * <td>{@link android.graphics.ImageFormat#JPEG JPEG}</td> 72b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <td>1</td> 73b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <td>Compressed data, so row and pixel strides are 0. To uncompress, use 745e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * {@link android.graphics.BitmapFactory#decodeByteArray BitmapFactory#decodeByteArray}. 755e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * </td> 76b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * </tr> 77b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <tr> 785e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * <td>{@link android.graphics.ImageFormat#YUV_420_888 YUV_420_888}</td> 79b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <td>3</td> 80b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <td>A luminance plane followed by the Cb and Cr chroma planes. 81b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * The chroma planes have half the width and height of the luminance 82b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * plane (4:2:0 subsampling). Each pixel sample in each plane has 8 bits. 83b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * Each plane has its own row stride and pixel stride.</td> 84b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * </tr> 85b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * </table> 86b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 87b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * @see android.graphics.ImageFormat 88b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala */ 895e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin public abstract int getFormat(); 90b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala 91b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala /** 92b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * The width of the image in pixels. For formats where some color channels 93b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * are subsampled, this is the width of the largest-resolution plane. 94b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala */ 955e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin public abstract int getWidth(); 96b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala 97b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala /** 98b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * The height of the image in pixels. For formats where some color channels 99b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * are subsampled, this is the height of the largest-resolution plane. 100b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala */ 1015e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin public abstract int getHeight(); 102b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala 103b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala /** 1045e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * Get the timestamp associated with this frame. 1055e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * <p> 1065e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * The timestamp is measured in nanoseconds, and is monotonically 1075e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * increasing. However, the zero point and whether the timestamp can be 1085e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * compared against other sources of time or images depend on the source of 1095e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * this image. 1105e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * </p> 111b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala */ 1125e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin public abstract long getTimestamp(); 113b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala 114b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala /** 115b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * Get the array of pixel planes for this Image. The number of planes is 116b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * determined by the format of the Image. 117b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala */ 1185e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin public abstract Plane[] getPlanes(); 119b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala 120b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala /** 1215e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * Free up this frame for reuse. 1225e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * <p> 1235e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * After calling this method, calling any methods on this {@code Image} will 1245e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * result in an {@link IllegalStateException}, and attempting to read from 1255e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * {@link ByteBuffer ByteBuffers} returned by an earlier 1265e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * {@link Plane#getBuffer} call will have undefined behavior. 1275e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * </p> 128b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala */ 1295e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin @Override 1305e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin public abstract void close(); 131b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala 132b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala /** 133b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <p>A single color plane of image data.</p> 134b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 135b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <p>The number and meaning of the planes in an Image are determined by the 136b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * format of the Image.</p> 137b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 138b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <p>Once the Image has been closed, any access to the the plane's 139b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * ByteBuffer will fail.</p> 140b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 141b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * @see #getFormat 142b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala */ 1435e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin public static abstract class Plane { 144b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala /** 1455e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * @hide 1465e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin */ 1475e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin protected Plane() { 1485e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin } 1495e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin 1505e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin /** 1515e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * <p>The row stride for this color plane, in bytes.</p> 152b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 153b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <p>This is the distance between the start of two consecutive rows of 1545e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * pixels in the image. The row stride is always greater than 0.</p> 155b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala */ 1565e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin public abstract int getRowStride(); 157b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala /** 158b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <p>The distance between adjacent pixel samples, in bytes.</p> 159b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 160b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * <p>This is the distance between two consecutive pixel values in a row 161b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * of pixels. It may be larger than the size of a single pixel to 1625e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * account for interleaved image data or padded formats. 1635e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * The pixel stride is always greater than 0.</p> 164b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala */ 1655e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin public abstract int getPixelStride(); 166b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala /** 1675e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * <p>Get a direct {@link java.nio.ByteBuffer ByteBuffer} 168b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * containing the frame data.</p> 169b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * 1705e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * <p>In particular, the buffer returned will always have 1715e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * {@link java.nio.ByteBuffer#isDirect isDirect} return {@code true}, so 1725e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * the underlying data could be mapped as a pointer in JNI without doing 1735e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * any copies with {@code GetDirectBufferAddress}.</p> 1745e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin * 175b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala * @return the byte buffer containing the image data for this plane. 176b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala */ 1775e712064dfe48992f8f732208fa4fc13f3455b30Igor Murashkin public abstract ByteBuffer getBuffer(); 178b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala } 179b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala 180b2675542c2f414154125b534767ae0903fba581eEino-Ville Talvala} 181