-
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
Open
camsim99
wants to merge
9
commits into
flutter:main
Choose a base branch
from
camsim99:camx_fix_nv21
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
9 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 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
4,786 changes: 1,916 additions & 2,870 deletions
4,786
...mera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/CameraXLibrary.g.kt
Large diffs are not rendered by default.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
...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,75 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
// | ||
// Note: code in this file is directly inspired by the official Google MLKit example: | ||
// https://github.com/googlesamples/mlkit | ||
|
||
package io.flutter.plugins.camerax; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.camera.core.ImageProxy.PlaneProxy; | ||
import java.nio.ByteBuffer; | ||
import java.util.List; | ||
|
||
/* Utitlities for working with {@code ImageProxy}s. */ | ||
public class ImageProxyUtils { | ||
|
||
/** | ||
* Converts PlaneProxy[] in YUV_420_888 format (with VU planes in NV21 layout) to a single NV21 | ||
* 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. All rights reserved. | ||
// 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 DeviceOrientationManager}. 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
44 changes: 44 additions & 0 deletions
44
...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,44 @@ | ||
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)); | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...android_camerax/android/src/test/java/io/flutter/plugins/camerax/ImageProxyUtilsTest.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,99 @@ | ||
package io.flutter.plugins.camerax; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
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.Mockito; | ||
|
||
public class ImageProxyUtilsTest { | ||
|
||
@Test | ||
public void planesToNV21_throwsExceptionForNonNV21Layout() { | ||
int width = 4; | ||
int height = 2; | ||
byte[] y = new byte[] {0, 1, 2, 3, 4, 5, 6, 7}; | ||
|
||
// U and V planes: not in NV21 layout (all values are the same) | ||
byte[] u = new byte[] {20, 20, 20, 20}; | ||
byte[] v = new byte[] {30, 30, 30, 30}; | ||
|
||
PlaneProxy yPlane = mockPlaneProxyWithData(y); | ||
PlaneProxy uPlane = mockPlaneProxyWithData(u); | ||
PlaneProxy vPlane = mockPlaneProxyWithData(v); | ||
|
||
List<PlaneProxy> planes = Arrays.asList(yPlane, uPlane, vPlane); | ||
|
||
assertThrows( | ||
IllegalArgumentException.class, () -> ImageProxyUtils.planesToNV21(planes, width, height)); | ||
} | ||
|
||
@Test | ||
public void planesToNV21_returnsExpectedBufferWhenPlanesAreNV21Compatible() { | ||
int width = 4; | ||
int height = 2; | ||
int imageSize = width * height; // 8 | ||
|
||
// Y plane. | ||
byte[] y = new byte[] {0, 1, 2, 3, 4, 5, 6, 7}; | ||
PlaneProxy yPlane = mockPlaneProxyWithData(y); | ||
|
||
// U and V planes in NV21 format. Both have 2 bytes that are identical (5, 7). | ||
ByteBuffer vBuffer = ByteBuffer.wrap(new byte[] {9, 5, 7}); | ||
ByteBuffer uBuffer = ByteBuffer.wrap(new byte[] {5, 7, 33}); | ||
|
||
PlaneProxy uPlane = Mockito.mock(PlaneProxy.class); | ||
PlaneProxy vPlane = Mockito.mock(PlaneProxy.class); | ||
|
||
Mockito.when(uPlane.getBuffer()).thenReturn(uBuffer); | ||
Mockito.when(vPlane.getBuffer()).thenReturn(vBuffer); | ||
|
||
List<PlaneProxy> planes = Arrays.asList(yPlane, uPlane, vPlane); | ||
|
||
ByteBuffer nv21Buffer = ImageProxyUtils.planesToNV21(planes, width, height); | ||
byte[] nv21 = new byte[nv21Buffer.remaining()]; | ||
nv21Buffer.get(nv21); | ||
|
||
// The planesToNV21 method copies: | ||
// 1. All of the Y plane bytes. | ||
// 2. The first byte of the V plane. | ||
// 3. The first three (2 * 8 / 4 - 1) bytes of the U plane. | ||
byte[] expected = | ||
new byte[] { | ||
0, | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
6, | ||
7, // Y | ||
9, | ||
5, | ||
7, | ||
33 // V0, U0, U1, U2 | ||
}; | ||
|
||
assertArrayEquals(expected, nv21); | ||
} | ||
|
||
// Creates a mock PlaneProxy with a buffer (of zeroes) of the given size. | ||
private PlaneProxy mockPlaneProxy(int bufferSize) { | ||
PlaneProxy plane = Mockito.mock(PlaneProxy.class); | ||
ByteBuffer buffer = ByteBuffer.allocate(bufferSize); | ||
Mockito.when(plane.getBuffer()).thenReturn(buffer); | ||
return plane; | ||
} | ||
|
||
// Creates a mock PlaneProxy with specific data. | ||
private PlaneProxy mockPlaneProxyWithData(byte[] data) { | ||
PlaneProxy plane = Mockito.mock(PlaneProxy.class); | ||
ByteBuffer buffer = ByteBuffer.wrap(Arrays.copyOf(data, data.length)); | ||
Mockito.when(plane.getBuffer()).thenReturn(buffer); | ||
return plane; | ||
} | ||
} |
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
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.