Skip to content

Commit 9bae96f

Browse files
stefankieszsirknightjmattcreaser
authored
KVS - Add Support for More Resolution Widths (#3609)
* Add kvs producer audio, start fixing rotation issue (#1) * MIcrophoneFrameSource builds, runs with "failed sampleRate 8000" exception * Fix all runtime errors * Started on 2-track streamInfo * Construct TrackInfo, build errors fixed * Progress, facing STATUS_MKV_INVALID_FRAME_DATA now if include audio with video * Working, missing CPD error on playback though * Works!! Todo: Should up the sample rate * Begin modularizing code, not yet built nor test run * Cleanup spaces, add to createFrameWithTrackID log * Cleanup diffs * Fix some importing and naming * Rename AndroidAudioVideoMediaSource * Add configuration class, control audio start/stop, working * Saving attempted solutions to the issue * Add TimeStampProvider to enforce unique timestamps * Not cleaning junk, saving working camera 90deg rotation! * Saving progress of fixing semi-planar case * Fix convertByteArrayToPlanes, conversions now working perfect for semiplanar, rotation still not * Rotation and colors work! * Optimized rotation working, cleaned up comments. * Turn off rotation, cleanup * Revert EncoderFactory color format changes * Revert "Revert EncoderFactory color format changes" This reverts commit bfd9940. * Revert "Add kvs producer audio, start fixing rotation issue (#1)" This reverts commit 2355788. * Initial fix-resolution commit * Cleanup * Add comments * Apply suggestions from code review Co-authored-by: sirknightj <sirknightj@gmail.com> Co-authored-by: Matt Creaser <mattcreaser@gmail.com> * Update aws-android-sdk-kinesisvideo/src/main/java/com/amazonaws/mobileconnectors/kinesisvideo/encoding/EncoderFrameSubmitter.java Co-authored-by: sirknightj <sirknightj@gmail.com> --------- Co-authored-by: sirknightj <sirknightj@gmail.com> Co-authored-by: Matt Creaser <mattcreaser@gmail.com> Co-authored-by: Matt Creaser <mattwcc@amazon.com>
1 parent d46434f commit 9bae96f

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

aws-android-sdk-kinesisvideo/src/main/java/com/amazonaws/mobileconnectors/kinesisvideo/encoding/EncoderFrameSubmitter.java

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,27 @@ private void copyCameraFrameIntoInputImage(final int inputBufferIndex,
137137
final Image cameraFrame) {
138138
final Image codecInputImage = mEncoder.getInputImage(inputBufferIndex);
139139

140+
final int imageWidth = cameraFrame.getWidth();
141+
final int imageRowStride = cameraFrame.getPlanes()[0].getRowStride();
142+
final int imageChromaPixelStride = cameraFrame.getPlanes()[1].getPixelStride();
143+
144+
final boolean isRowPaddingPresent = imageWidth != imageRowStride;
145+
final boolean isSemiPlanar = imageChromaPixelStride != 1;
146+
140147
for (int i = 0; i < cameraFrame.getPlanes().length; i++) {
141148
final ByteBuffer sourceImagePlane = cameraFrame.getPlanes()[i].getBuffer();
142149
final ByteBuffer destinationImagePlane = codecInputImage.getPlanes()[i].getBuffer();
143-
copyBuffer(sourceImagePlane, destinationImagePlane);
150+
151+
final int pixelStride = codecInputImage.getPlanes()[i].getPixelStride();
152+
153+
// Accounting for horizontal padding that may have been added for camera optimization. For example, non-multiple-of-64-width
154+
// resolutions may have had padding added to the rows to reach the nearest greater multiple of 64.
155+
// This accounting for is only supported for SemiPlanar format at the moment.
156+
if (isRowPaddingPresent && isSemiPlanar) {
157+
copyPaddedBuffer(sourceImagePlane, destinationImagePlane, imageWidth, imageRowStride, pixelStride);
158+
} else {
159+
copyBuffer(sourceImagePlane, destinationImagePlane);
160+
}
144161
}
145162
}
146163

@@ -156,6 +173,58 @@ private int copyBuffer(final ByteBuffer sourceBuffer,
156173
return bytesToCopy;
157174
}
158175

176+
// This copy function will ignore the extra padding data when copying into the encoder's input buffer.
177+
// This will only work for SemiPlanar format.
178+
private int copyPaddedBuffer(final ByteBuffer sourceBuffer,
179+
final ByteBuffer destinationBuffer,
180+
final int imageWidth,
181+
final int sourceBufferRowStride,
182+
final int pixelStride) {
183+
184+
// For an unknown reason, the sourceBuffer's remaining bytes are slightly less than expected, rounding up fixes this.
185+
final int numRows = (int) Math.ceil((double) sourceBuffer.remaining() / sourceBufferRowStride);
186+
187+
// Desired bytes might exceed destination buffer capacity.
188+
final int desiredBytesToCopy = numRows * imageWidth;
189+
final int bytesToCopy = Math.min(desiredBytesToCopy, destinationBuffer.capacity());
190+
destinationBuffer.limit(bytesToCopy);
191+
192+
// If this is a chroma plane, then don't include the final row in the for loop - it is an edge case.
193+
final int numRowsToIterate;
194+
if (pixelStride == 2) {
195+
numRowsToIterate = numRows - 1;
196+
} else {
197+
numRowsToIterate = numRows;
198+
}
199+
200+
for (int row = 0; row < numRowsToIterate; row++) {
201+
final int startPos = row * sourceBufferRowStride;
202+
final int endPos = startPos + imageWidth;
203+
204+
sourceBuffer.limit(endPos);
205+
sourceBuffer.position(startPos);
206+
207+
// Put the valid row data into the destination buffer.
208+
destinationBuffer.put(sourceBuffer);
209+
}
210+
211+
// If this is a chroma frame, handle the final row which is missing one value, hence the "-1" to the endPos.
212+
if (pixelStride == 2) {
213+
final int startPos = numRowsToIterate * sourceBufferRowStride;
214+
final int endPos = startPos + imageWidth - 1;
215+
216+
sourceBuffer.limit(endPos);
217+
sourceBuffer.position(startPos);
218+
219+
// Put the valid row data into the destination buffer.
220+
destinationBuffer.put(sourceBuffer);
221+
}
222+
223+
destinationBuffer.rewind();
224+
225+
return bytesToCopy;
226+
}
227+
159228
private long nanosSinceFirstFrame() {
160229
final long currentTime = System.currentTimeMillis();
161230
if (mFirstFrameTimestamp < 0) {

0 commit comments

Comments
 (0)