Skip to content

Commit 9515163

Browse files
author
Eino-Ville Talvala
committed
Update docs for YV12 format and camera preview callbacks.
- Define stride for YV12 when using it for preview callbacks - Include equations for calculating stride and start indexes of Y, U, and V planes for YV12. - Add more cross-references so that equations are easier to find. Bug: 6330501 Change-Id: I85a78757ec767d08173b9fe714adb715835244b4
1 parent 427db9b commit 9515163

File tree

2 files changed

+71
-15
lines changed

2 files changed

+71
-15
lines changed

core/java/android/hardware/Camera.java

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@ public interface PreviewCallback
469469
* Called as preview frames are displayed. This callback is invoked
470470
* on the event thread {@link #open(int)} was called from.
471471
*
472+
* <p>If using the {@link android.graphics.ImageFormat#YV12} format,
473+
* refer to the equations in {@link Camera.Parameters#setPreviewFormat}
474+
* for the arrangement of the pixel data in the preview callback
475+
* buffers.
476+
*
472477
* @param data the contents of the preview frame in the format defined
473478
* by {@link android.graphics.ImageFormat}, which can be queried
474479
* with {@link android.hardware.Camera.Parameters#getPreviewFormat()}.
@@ -609,12 +614,17 @@ public final void setPreviewCallbackWithBuffer(PreviewCallback cb) {
609614
* the frame is discarded. Applications should add buffers back when they
610615
* finish processing the data in them.
611616
*
612-
* <p>The size of the buffer is determined by multiplying the preview
613-
* image width, height, and bytes per pixel. The width and height can be
614-
* read from {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel
615-
* can be computed from
616-
* {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8,
617-
* using the image format from {@link Camera.Parameters#getPreviewFormat()}.
617+
* <p>For formats besides YV12, the size of the buffer is determined by
618+
* multiplying the preview image width, height, and bytes per pixel. The
619+
* width and height can be read from
620+
* {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel can be
621+
* computed from {@link android.graphics.ImageFormat#getBitsPerPixel(int)} /
622+
* 8, using the image format from
623+
* {@link Camera.Parameters#getPreviewFormat()}.
624+
*
625+
* <p>If using the {@link android.graphics.ImageFormat#YV12} format, the
626+
* size can be calculated using the equations listed in
627+
* {@link Camera.Parameters#setPreviewFormat}.
618628
*
619629
* <p>This method is only necessary when
620630
* {@link #setPreviewCallbackWithBuffer(PreviewCallback)} is used. When
@@ -624,8 +634,8 @@ public final void setPreviewCallbackWithBuffer(PreviewCallback cb) {
624634
* hold the preview frame data, preview callback will return null and
625635
* the buffer will be removed from the buffer queue.
626636
*
627-
* @param callbackBuffer the buffer to add to the queue.
628-
* The size should be width * height * bits_per_pixel / 8.
637+
* @param callbackBuffer the buffer to add to the queue. The size of the
638+
* buffer must match the values described above.
629639
* @see #setPreviewCallbackWithBuffer(PreviewCallback)
630640
*/
631641
public final void addCallbackBuffer(byte[] callbackBuffer)
@@ -2270,12 +2280,44 @@ public List<int[]> getSupportedPreviewFpsRange() {
22702280
* {@link android.graphics.ImageFormat#NV21}, which
22712281
* uses the NV21 encoding format.</p>
22722282
*
2273-
* @param pixel_format the desired preview picture format, defined
2274-
* by one of the {@link android.graphics.ImageFormat} constants.
2275-
* (E.g., <var>ImageFormat.NV21</var> (default),
2276-
* <var>ImageFormat.RGB_565</var>, or
2277-
* <var>ImageFormat.JPEG</var>)
2283+
* <p>Use {@link Parameters#getSupportedPreviewFormats} to get a list of
2284+
* the available preview formats.
2285+
*
2286+
* <p>It is strongly recommended that either
2287+
* {@link android.graphics.ImageFormat#NV21} or
2288+
* {@link android.graphics.ImageFormat#YV12} is used, since
2289+
* they are supported by all camera devices.</p>
2290+
*
2291+
* <p>For YV12, the image buffer that is received is not necessarily
2292+
* tightly packed, as there may be padding at the end of each row of
2293+
* pixel data, as described in
2294+
* {@link android.graphics.ImageFormat#YV12}. For camera callback data,
2295+
* it can be assumed that the stride of the Y and UV data is the
2296+
* smallest possible that meets the alignment requirements. That is, if
2297+
* the preview size is <var>width x height</var>, then the following
2298+
* equations describe the buffer index for the beginning of row
2299+
* <var>y</var> for the Y plane and row <var>c</var> for the U and V
2300+
* planes:
2301+
*
2302+
* {@code
2303+
* <pre>
2304+
* yStride = (int) ceil(width / 16.0) * 16;
2305+
* uvStride = (int) ceil( (yStride / 2) / 16.0) * 16;
2306+
* ySize = yStride * height;
2307+
* uvSize = uvStride * height / 2;
2308+
* yRowIndex = yStride * y;
2309+
* uRowIndex = ySize + uvSize + uvStride * c;
2310+
* vRowIndex = ySize + uvStride * c;
2311+
* size = ySize + uvSize * 2;</pre>
2312+
* }
2313+
*
2314+
* @param pixel_format the desired preview picture format, defined by
2315+
* one of the {@link android.graphics.ImageFormat} constants. (E.g.,
2316+
* <var>ImageFormat.NV21</var> (default), or
2317+
* <var>ImageFormat.YV12</var>)
2318+
*
22782319
* @see android.graphics.ImageFormat
2320+
* @see android.hardware.Camera.Parameters#getSupportedPreviewFormats
22792321
*/
22802322
public void setPreviewFormat(int pixel_format) {
22812323
String s = cameraFormatForPixelFormat(pixel_format);
@@ -2293,6 +2335,7 @@ public void setPreviewFormat(int pixel_format) {
22932335
*
22942336
* @return the preview format.
22952337
* @see android.graphics.ImageFormat
2338+
* @see #setPreviewFormat
22962339
*/
22972340
public int getPreviewFormat() {
22982341
return pixelFormatForCameraFormat(get(KEY_PREVIEW_FORMAT));
@@ -2306,6 +2349,7 @@ public int getPreviewFormat() {
23062349
* @return a list of supported preview formats. This method will always
23072350
* return a list with at least one element.
23082351
* @see android.graphics.ImageFormat
2352+
* @see #setPreviewFormat
23092353
*/
23102354
public List<Integer> getSupportedPreviewFormats() {
23112355
String str = get(KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX);

graphics/java/android/graphics/ImageFormat.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,26 @@ public class ImageFormat {
4848
* </p>
4949
*
5050
* <pre> y_size = stride * height
51-
* c_size = ALIGN(stride/2, 16) * height/2
51+
* c_stride = ALIGN(stride/2, 16)
52+
* c_size = c_stride * height/2
5253
* size = y_size + c_size * 2
5354
* cr_offset = y_size
5455
* cb_offset = y_size + c_size</pre>
5556
*
56-
* This format is guaranteed to be supported for camera preview images since
57+
* <p>This format is guaranteed to be supported for camera preview images since
5758
* API level 12; for earlier API versions, check
5859
* {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.
60+
*
61+
* <p>Note that for camera preview callback use (see
62+
* {@link android.hardware.Camera#setPreviewCallback}), the
63+
* <var>stride</var> value is the smallest possible; that is, it is equal
64+
* to:
65+
*
66+
* <pre>stride = ALIGN(width, 16)</pre>
67+
*
68+
* @see android.hardware.Camera.Parameters#setPreviewCallback
69+
* @see android.hardware.Camera.Parameters#setPreviewFormat
70+
* @see android.hardware.Camera.Parameters#getSupportedPreviewFormats
5971
* </p>
6072
*/
6173
public static final int YV12 = 0x32315659;

0 commit comments

Comments
 (0)