-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[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
Merged
+718
−13
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9f2c95b
working state
camsim99 dd1b1b1
Add lots of files and utils tests
camsim99 6718543
add proxy api test
camsim99 187a22a
start writing dart test
camsim99 7df4d12
Add dart test
camsim99 faac519
nits, bump version
camsim99 62aee3e
analyze
camsim99 653aae6
Merge remote-tracking branch 'upstream/main' into camx_fix_nv21
camsim99 d7cabec
self review
camsim99 d743b8d
Remove reference to third party code
camsim99 5e65758
Merge remote-tracking branch 'upstream/main' into camx_fix_nv21
camsim99 1b43a60
Merge branch 'main' into camx_fix_nv21
stuartmorgan-g c783c40
Update license header boilerplate
camsim99 ed99abd
Merge remote-tracking branch 'refs/remotes/origin/camx_fix_nv21' into…
camsim99 096386e
address review
camsim99 2f8b7ad
bump version
camsim99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...era_android_camerax/android/src/main/java/io/flutter/plugins/camerax/ImageProxyUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...oid_camerax/android/src/main/java/io/flutter/plugins/camerax/ImageProxyUtilsProxyApi.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...roid_camerax/android/src/test/java/io/flutter/plugins/camerax/ImageProxyUtilsApiTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Third-party code cannot be handled that way; the
camera_android
code shouldn't have landed like that (unless it had OSPO approval), so it should definitely not be replicated, and the old code should be fixed.The standard way to handle this would be:
third_party
directory, with the correct license.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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
BitmapUtils.java
code comes from mlkit/android/vision-quickstart that is licensed under the aforementioned agreement, I think I am okay to use it without reference to the original code. I will go ahead and make edits to accommodate this fact and also make a follow-up PR to remove the reference fromcamera_android
.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.