-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[camera_android_camerax] Fix NV21 Format #10022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 14 commits
9f2c95b
dd1b1b1
6718543
187a22a
7df4d12
faac519
62aee3e
653aae6
d7cabec
d743b8d
5e65758
1b43a60
c783c40
ed99abd
096386e
2f8b7ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is directly inspired by this camera_android code that came from Google MLKit. I made some changes the methods to (1) take in the objects I want and (2) for planesToNV21, I worked off of yuv420ThreePlanesToNV21 but deleted lots of logic and added an exception. So, I'm not sure what to do here.... Should I also link to https://github.com/googlesamples/mlkit/blob/master/android/vision-quickstart/app/src/main/java/com/google/mlkit/vision/demo/BitmapUtils.java and call it a day or are there more license implications?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stuartmorgan-g Do you know what we should do in cases like this by chance?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Third-party code cannot be handled that way; the The standard way to handle this would be:
However, because it is specifically Google that has the copyright on that code, it may be that there are simpler options. I would reach out to OSPO and ask how this should be handled.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the guidance! Reached out to OSPO and they let me know that if our code is derived from code authored by Googlers or contributed to a Google-managed open source project under the terms of Google's contributor license agreement, then I can treat the code as though we wrote it ourselves. Because the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just FYI: I did send an email back with my intentions just to sanity check my decision here! Edit: They confirmed this is the right move 👍 They also explicitly stated not to cite where the code comes from in the file. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| package io.flutter.plugins.camerax; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.camera.core.ImageProxy.PlaneProxy; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.List; | ||
|
|
||
| /* Utilities for working with {@code ImageProxy}s. */ | ||
| public class ImageProxyUtils { | ||
|
|
||
| /** | ||
| * Converts list of {@link PlaneProxy}s in YUV_420_888 format (with VU planes in NV21 layout) to a | ||
| * single NV21 {@code ByteBuffer}. | ||
| */ | ||
| @NonNull | ||
| public static ByteBuffer planesToNV21(@NonNull List<PlaneProxy> planes, int width, int height) { | ||
| if (!areUVPlanesNV21(planes, width, height)) { | ||
| throw new IllegalArgumentException( | ||
| "Provided UV planes are not in NV21 layout and thus cannot be converted."); | ||
| } | ||
|
|
||
| int imageSize = width * height; | ||
| int nv21Size = imageSize + 2 * (imageSize / 4); | ||
| byte[] nv21Bytes = new byte[nv21Size]; | ||
|
|
||
| // Copy Y plane. | ||
| ByteBuffer yBuffer = planes.get(0).getBuffer(); | ||
| yBuffer.rewind(); | ||
| yBuffer.get(nv21Bytes, 0, imageSize); | ||
|
|
||
| // Copy interleaved VU plane (NV21 layout). | ||
| ByteBuffer vBuffer = planes.get(2).getBuffer(); | ||
| ByteBuffer uBuffer = planes.get(1).getBuffer(); | ||
|
|
||
| vBuffer.rewind(); | ||
| uBuffer.rewind(); | ||
| vBuffer.get(nv21Bytes, imageSize, 1); | ||
| uBuffer.get(nv21Bytes, imageSize + 1, 2 * imageSize / 4 - 1); | ||
|
|
||
| return ByteBuffer.wrap(nv21Bytes); | ||
| } | ||
|
|
||
| public static boolean areUVPlanesNV21(@NonNull List<PlaneProxy> planes, int width, int height) { | ||
| int imageSize = width * height; | ||
|
|
||
| ByteBuffer uBuffer = planes.get(1).getBuffer(); | ||
| ByteBuffer vBuffer = planes.get(2).getBuffer(); | ||
|
|
||
| // Backup buffer properties. | ||
| int vBufferPosition = vBuffer.position(); | ||
| int uBufferLimit = uBuffer.limit(); | ||
|
|
||
| // Advance the V buffer by 1 byte, since the U buffer will not contain the first V value. | ||
| vBuffer.position(vBufferPosition + 1); | ||
| // Chop off the last byte of the U buffer, since the V buffer will not contain the last U value. | ||
| uBuffer.limit(uBufferLimit - 1); | ||
|
|
||
| // Check that the buffers are equal and have the expected number of elements. | ||
| boolean areNV21 = | ||
| (vBuffer.remaining() == (2 * imageSize / 4 - 2)) && (vBuffer.compareTo(uBuffer) == 0); | ||
|
|
||
| // Restore buffers to their initial state. | ||
| vBuffer.position(vBufferPosition); | ||
| uBuffer.limit(uBufferLimit); | ||
|
|
||
| return areNV21; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| package io.flutter.plugins.camerax; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.camera.core.ImageProxy.PlaneProxy; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * ProxyApi implementation for {@link ImageProxyUtils}. This class may handle instantiating native | ||
| * object instances that are attached to a Dart instance or handle method calls on the associated | ||
| * native class or an instance of that class. | ||
| */ | ||
| public class ImageProxyUtilsProxyApi extends PigeonApiImageProxyUtils { | ||
| ImageProxyUtilsProxyApi(@NonNull ProxyApiRegistrar pigeonRegistrar) { | ||
| super(pigeonRegistrar); | ||
| } | ||
|
|
||
| // List<? extends PlaneProxy> can be considered the same as List<PlaneProxy>. | ||
| @SuppressWarnings("unchecked") | ||
| @NonNull | ||
| @Override | ||
| public byte[] getNv21Buffer( | ||
| long imageWidth, long imageHeight, @NonNull List<? extends PlaneProxy> planes) { | ||
| final ByteBuffer nv21Buffer = | ||
| ImageProxyUtils.planesToNV21( | ||
| (List<PlaneProxy>) planes, (int) imageWidth, (int) imageHeight); | ||
|
|
||
| byte[] bytes = new byte[nv21Buffer.remaining()]; | ||
| nv21Buffer.get(bytes, 0, bytes.length); | ||
|
|
||
| return bytes; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| package io.flutter.plugins.camerax; | ||
|
|
||
| import static org.junit.Assert.assertArrayEquals; | ||
| import static org.mockito.Mockito.mockStatic; | ||
|
|
||
| import androidx.camera.core.ImageProxy.PlaneProxy; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import org.junit.Test; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.Mockito; | ||
|
|
||
| public class ImageProxyUtilsApiTest { | ||
|
|
||
| @Test | ||
| public void getNv21Buffer_returnsExpectedBytes() { | ||
| final PigeonApiImageProxyUtils api = new TestProxyApiRegistrar().getPigeonApiImageProxyUtils(); | ||
|
|
||
| List<PlaneProxy> planes = | ||
| Arrays.asList( | ||
| Mockito.mock(PlaneProxy.class), | ||
| Mockito.mock(PlaneProxy.class), | ||
| Mockito.mock(PlaneProxy.class)); | ||
| long width = 4; | ||
| long height = 2; | ||
| byte[] expectedBytes = new byte[] {1, 2, 3, 4, 5}; | ||
| ByteBuffer mockBuffer = ByteBuffer.wrap(expectedBytes); | ||
|
|
||
| try (MockedStatic<ImageProxyUtils> mockedStatic = mockStatic(ImageProxyUtils.class)) { | ||
| mockedStatic | ||
| .when( | ||
| () -> | ||
| ImageProxyUtils.planesToNV21( | ||
| Mockito.anyList(), Mockito.anyInt(), Mockito.anyInt())) | ||
| .thenReturn(mockBuffer); | ||
|
|
||
| byte[] result = api.getNv21Buffer(width, height, planes); | ||
|
|
||
| assertArrayEquals(expectedBytes, result); | ||
| mockedStatic.verify(() -> ImageProxyUtils.planesToNV21(planes, (int) width, (int) height)); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.