• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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;
18 
19 import java.nio.ByteBuffer;
20 import java.lang.AutoCloseable;
21 
22 import android.graphics.Rect;
23 
24 /**
25  * <p>A single complete image buffer to use with a media source such as a
26  * {@link MediaCodec} or a
27  * {@link android.hardware.camera2.CameraDevice CameraDevice}.</p>
28  *
29  * <p>This class allows for efficient direct application access to the pixel
30  * data of the Image through one or more
31  * {@link java.nio.ByteBuffer ByteBuffers}. Each buffer is encapsulated in a
32  * {@link Plane} that describes the layout of the pixel data in that plane. Due
33  * to this direct access, and unlike the {@link android.graphics.Bitmap Bitmap} class,
34  * Images are not directly usable as as UI resources.</p>
35  *
36  * <p>Since Images are often directly produced or consumed by hardware
37  * components, they are a limited resource shared across the system, and should
38  * be closed as soon as they are no longer needed.</p>
39  *
40  * <p>For example, when using the {@link ImageReader} class to read out Images
41  * from various media sources, not closing old Image objects will prevent the
42  * availability of new Images once
43  * {@link ImageReader#getMaxImages the maximum outstanding image count} is
44  * reached. When this happens, the function acquiring new Images will typically
45  * throw an {@link IllegalStateException}.</p>
46  *
47  * @see ImageReader
48  */
49 public abstract class Image implements AutoCloseable {
50     /**
51      * @hide
52      */
53     protected boolean mIsImageValid = false;
54 
55     /**
56      * @hide
57      */
Image()58     protected Image() {
59     }
60 
61     /**
62      * Throw IllegalStateException if the image is invalid (already closed).
63      *
64      * @hide
65      */
throwISEIfImageIsInvalid()66     protected void throwISEIfImageIsInvalid() {
67         if (!mIsImageValid) {
68             throw new IllegalStateException("Image is already closed");
69         }
70     }
71     /**
72      * Get the format for this image. This format determines the number of
73      * ByteBuffers needed to represent the image, and the general layout of the
74      * pixel data in each in ByteBuffer.
75      *
76      * <p>
77      * The format is one of the values from
78      * {@link android.graphics.ImageFormat ImageFormat}. The mapping between the
79      * formats and the planes is as follows:
80      * </p>
81      *
82      * <table>
83      * <tr>
84      *   <th>Format</th>
85      *   <th>Plane count</th>
86      *   <th>Layout details</th>
87      * </tr>
88      * <tr>
89      *   <td>{@link android.graphics.ImageFormat#JPEG JPEG}</td>
90      *   <td>1</td>
91      *   <td>Compressed data, so row and pixel strides are 0. To uncompress, use
92      *      {@link android.graphics.BitmapFactory#decodeByteArray BitmapFactory#decodeByteArray}.
93      *   </td>
94      * </tr>
95      * <tr>
96      *   <td>{@link android.graphics.ImageFormat#YUV_420_888 YUV_420_888}</td>
97      *   <td>3</td>
98      *   <td>A luminance plane followed by the Cb and Cr chroma planes.
99      *     The chroma planes have half the width and height of the luminance
100      *     plane (4:2:0 subsampling). Each pixel sample in each plane has 8 bits.
101      *     Each plane has its own row stride and pixel stride.</td>
102      * </tr>
103      * <tr>
104      *   <td>{@link android.graphics.ImageFormat#YUV_422_888 YUV_422_888}</td>
105      *   <td>3</td>
106      *   <td>A luminance plane followed by the Cb and Cr chroma planes.
107      *     The chroma planes have half the width and the full height of the luminance
108      *     plane (4:2:2 subsampling). Each pixel sample in each plane has 8 bits.
109      *     Each plane has its own row stride and pixel stride.</td>
110      * </tr>
111      * <tr>
112      *   <td>{@link android.graphics.ImageFormat#YUV_444_888 YUV_444_888}</td>
113      *   <td>3</td>
114      *   <td>A luminance plane followed by the Cb and Cr chroma planes.
115      *     The chroma planes have the same width and height as that of the luminance
116      *     plane (4:4:4 subsampling). Each pixel sample in each plane has 8 bits.
117      *     Each plane has its own row stride and pixel stride.</td>
118      * </tr>
119      * <tr>
120      *   <td>{@link android.graphics.ImageFormat#FLEX_RGB_888 FLEX_RGB_888}</td>
121      *   <td>3</td>
122      *   <td>A R (red) plane followed by the G (green) and B (blue) planes.
123      *     All planes have the same widths and heights.
124      *     Each pixel sample in each plane has 8 bits.
125      *     Each plane has its own row stride and pixel stride.</td>
126      * </tr>
127      * <tr>
128      *   <td>{@link android.graphics.ImageFormat#FLEX_RGBA_8888 FLEX_RGBA_8888}</td>
129      *   <td>4</td>
130      *   <td>A R (red) plane followed by the G (green), B (blue), and
131      *     A (alpha) planes. All planes have the same widths and heights.
132      *     Each pixel sample in each plane has 8 bits.
133      *     Each plane has its own row stride and pixel stride.</td>
134      * </tr>
135      * <tr>
136      *   <td>{@link android.graphics.ImageFormat#RAW_SENSOR RAW_SENSOR}</td>
137      *   <td>1</td>
138      *   <td>A single plane of raw sensor image data, with 16 bits per color
139      *     sample. The details of the layout need to be queried from the source of
140      *     the raw sensor data, such as
141      *     {@link android.hardware.camera2.CameraDevice CameraDevice}.
142      *   </td>
143      * </tr>
144      * </table>
145      *
146      * @see android.graphics.ImageFormat
147      */
getFormat()148     public abstract int getFormat();
149 
150     /**
151      * The width of the image in pixels. For formats where some color channels
152      * are subsampled, this is the width of the largest-resolution plane.
153      */
getWidth()154     public abstract int getWidth();
155 
156     /**
157      * The height of the image in pixels. For formats where some color channels
158      * are subsampled, this is the height of the largest-resolution plane.
159      */
getHeight()160     public abstract int getHeight();
161 
162     /**
163      * Get the timestamp associated with this frame.
164      * <p>
165      * The timestamp is measured in nanoseconds, and is normally monotonically
166      * increasing. The timestamps for the images from different sources may have
167      * different timebases therefore may not be comparable. The specific meaning and
168      * timebase of the timestamp depend on the source providing images. See
169      * {@link android.hardware.Camera Camera},
170      * {@link android.hardware.camera2.CameraDevice CameraDevice},
171      * {@link MediaPlayer} and {@link MediaCodec} for more details.
172      * </p>
173      */
getTimestamp()174     public abstract long getTimestamp();
175 
176     /**
177      * Set the timestamp associated with this frame.
178      * <p>
179      * The timestamp is measured in nanoseconds, and is normally monotonically
180      * increasing. The timestamps for the images from different sources may have
181      * different timebases therefore may not be comparable. The specific meaning and
182      * timebase of the timestamp depend on the source providing images. See
183      * {@link android.hardware.Camera Camera},
184      * {@link android.hardware.camera2.CameraDevice CameraDevice},
185      * {@link MediaPlayer} and {@link MediaCodec} for more details.
186      * </p>
187      * <p>
188      * For images dequeued from {@link ImageWriter} via
189      * {@link ImageWriter#dequeueInputImage()}, it's up to the application to
190      * set the timestamps correctly before sending them back to the
191      * {@link ImageWriter}, or the timestamp will be generated automatically when
192      * {@link ImageWriter#queueInputImage queueInputImage()} is called.
193      * </p>
194      *
195      * @param timestamp The timestamp to be set for this image.
196      */
setTimestamp(long timestamp)197     public void setTimestamp(long timestamp) {
198         throwISEIfImageIsInvalid();
199         return;
200     }
201 
202     private Rect mCropRect;
203 
204     /**
205      * Get the crop rectangle associated with this frame.
206      * <p>
207      * The crop rectangle specifies the region of valid pixels in the image,
208      * using coordinates in the largest-resolution plane.
209      */
getCropRect()210     public Rect getCropRect() {
211         throwISEIfImageIsInvalid();
212 
213         if (mCropRect == null) {
214             return new Rect(0, 0, getWidth(), getHeight());
215         } else {
216             return new Rect(mCropRect); // return a copy
217         }
218     }
219 
220     /**
221      * Set the crop rectangle associated with this frame.
222      * <p>
223      * The crop rectangle specifies the region of valid pixels in the image,
224      * using coordinates in the largest-resolution plane.
225      */
setCropRect(Rect cropRect)226     public void setCropRect(Rect cropRect) {
227         throwISEIfImageIsInvalid();
228 
229         if (cropRect != null) {
230             cropRect = new Rect(cropRect);  // make a copy
231             if (!cropRect.intersect(0, 0, getWidth(), getHeight())) {
232                 cropRect.setEmpty();
233             }
234         }
235         mCropRect = cropRect;
236     }
237 
238     /**
239      * Get the array of pixel planes for this Image. The number of planes is
240      * determined by the format of the Image. The application will get an empty
241      * array if the image format is {@link android.graphics.ImageFormat#PRIVATE
242      * PRIVATE}, because the image pixel data is not directly accessible. The
243      * application can check the image format by calling
244      * {@link Image#getFormat()}.
245      */
getPlanes()246     public abstract Plane[] getPlanes();
247 
248     /**
249      * Free up this frame for reuse.
250      * <p>
251      * After calling this method, calling any methods on this {@code Image} will
252      * result in an {@link IllegalStateException}, and attempting to read from
253      * or write to {@link ByteBuffer ByteBuffers} returned by an earlier
254      * {@link Plane#getBuffer} call will have undefined behavior. If the image
255      * was obtained from {@link ImageWriter} via
256      * {@link ImageWriter#dequeueInputImage()}, after calling this method, any
257      * image data filled by the application will be lost and the image will be
258      * returned to {@link ImageWriter} for reuse. Images given to
259      * {@link ImageWriter#queueInputImage queueInputImage()} are automatically
260      * closed.
261      * </p>
262      */
263     @Override
close()264     public abstract void close();
265 
266     /**
267      * <p>
268      * Check if the image can be attached to a new owner (e.g. {@link ImageWriter}).
269      * </p>
270      * <p>
271      * This is a package private method that is only used internally.
272      * </p>
273      *
274      * @return true if the image is attachable to a new owner, false if the image is still attached
275      *         to its current owner, or the image is a stand-alone image and is not attachable to
276      *         a new owner.
277      */
isAttachable()278     boolean isAttachable() {
279         throwISEIfImageIsInvalid();
280 
281         return false;
282     }
283 
284     /**
285      * <p>
286      * Get the owner of the {@link Image}.
287      * </p>
288      * <p>
289      * The owner of an {@link Image} could be {@link ImageReader}, {@link ImageWriter},
290      * {@link MediaCodec} etc. This method returns the owner that produces this image, or null
291      * if the image is stand-alone image or the owner is unknown.
292      * </p>
293      * <p>
294      * This is a package private method that is only used internally.
295      * </p>
296      *
297      * @return The owner of the Image.
298      */
getOwner()299     Object getOwner() {
300         throwISEIfImageIsInvalid();
301 
302         return null;
303     }
304 
305     /**
306      * Get native context (buffer pointer) associated with this image.
307      * <p>
308      * This is a package private method that is only used internally. It can be
309      * used to get the native buffer pointer and passed to native, which may be
310      * passed to {@link ImageWriter#attachAndQueueInputImage} to avoid a reverse
311      * JNI call.
312      * </p>
313      *
314      * @return native context associated with this Image.
315      */
getNativeContext()316     long getNativeContext() {
317         throwISEIfImageIsInvalid();
318 
319         return 0;
320     }
321 
322     /**
323      * <p>A single color plane of image data.</p>
324      *
325      * <p>The number and meaning of the planes in an Image are determined by the
326      * format of the Image.</p>
327      *
328      * <p>Once the Image has been closed, any access to the the plane's
329      * ByteBuffer will fail.</p>
330      *
331      * @see #getFormat
332      */
333     public static abstract class Plane {
334         /**
335          * @hide
336          */
Plane()337         protected Plane() {
338         }
339 
340         /**
341          * <p>The row stride for this color plane, in bytes.</p>
342          *
343          * <p>This is the distance between the start of two consecutive rows of
344          * pixels in the image. The row stride is always greater than 0.</p>
345          */
getRowStride()346         public abstract int getRowStride();
347         /**
348          * <p>The distance between adjacent pixel samples, in bytes.</p>
349          *
350          * <p>This is the distance between two consecutive pixel values in a row
351          * of pixels. It may be larger than the size of a single pixel to
352          * account for interleaved image data or padded formats.
353          * The pixel stride is always greater than 0.</p>
354          */
getPixelStride()355         public abstract int getPixelStride();
356         /**
357          * <p>Get a direct {@link java.nio.ByteBuffer ByteBuffer}
358          * containing the frame data.</p>
359          *
360          * <p>In particular, the buffer returned will always have
361          * {@link java.nio.ByteBuffer#isDirect isDirect} return {@code true}, so
362          * the underlying data could be mapped as a pointer in JNI without doing
363          * any copies with {@code GetDirectBufferAddress}.</p>
364          *
365          * <p>For raw formats, each plane is only guaranteed to contain data
366          * up to the last pixel in the last row. In other words, the stride
367          * after the last row may not be mapped into the buffer. This is a
368          * necessary requirement for any interleaved format.</p>
369          *
370          * @return the byte buffer containing the image data for this plane.
371          */
getBuffer()372         public abstract ByteBuffer getBuffer();
373     }
374 
375 }
376