Skip to content

Commit

Permalink
Fix Samsung MediaCodec encoder reports incorrect timestmp in EOS
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 492023573
  • Loading branch information
claincly authored and icbaker committed Dec 12, 2022
1 parent 5f73984 commit e4db710
Showing 1 changed file with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
private final EncoderWrapper encoderWrapper;
private final DecoderInputBuffer encoderOutputBuffer;

/**
* The timestamp of the last buffer processed before {@linkplain
* FrameProcessor.Listener#onFrameProcessingEnded() frame processing has ended}.
*/
private volatile long finalFramePresentationTimeUs;

public VideoTranscodingSamplePipeline(
Context context,
Format inputFormat,
Expand Down Expand Up @@ -109,6 +115,8 @@ public VideoTranscodingSamplePipeline(
/* mediaCodecName= */ null,
TransformationException.ERROR_CODE_HDR_ENCODING_UNSUPPORTED);
}

finalFramePresentationTimeUs = C.TIME_UNSET;
}

decoderInputBuffer =
Expand Down Expand Up @@ -151,6 +159,8 @@ public VideoTranscodingSamplePipeline(
frameProcessorFactory.create(
context,
new FrameProcessor.Listener() {
private long lastProcessedFramePresentationTimeUs;

@Override
public void onOutputSizeChanged(int width, int height) {
try {
Expand All @@ -163,7 +173,8 @@ public void onOutputSizeChanged(int width, int height) {

@Override
public void onOutputFrameAvailable(long presentationTimeUs) {
// Do nothing as frames are released automatically.
// Frames are released automatically.
lastProcessedFramePresentationTimeUs = presentationTimeUs;
}

@Override
Expand All @@ -175,6 +186,8 @@ public void onFrameProcessingError(FrameProcessingException exception) {

@Override
public void onFrameProcessingEnded() {
VideoTranscodingSamplePipeline.this.finalFramePresentationTimeUs =
lastProcessedFramePresentationTimeUs;
try {
encoderWrapper.signalEndOfInputStream();
} catch (TransformationException exception) {
Expand Down Expand Up @@ -264,6 +277,16 @@ protected DecoderInputBuffer getMuxerInputBuffer() throws TransformationExceptio
return null;
}
MediaCodec.BufferInfo bufferInfo = checkNotNull(encoderWrapper.getOutputBufferInfo());
if (finalFramePresentationTimeUs != C.TIME_UNSET
&& bufferInfo.size > 0
&& bufferInfo.presentationTimeUs == 0) {
// Internal ref b/235045165: Some encoder incorrectly set a zero presentation time on the
// penultimate buffer (before EOS), and sets the actual timestamp on the EOS buffer. Use the
// last processed frame presentation time instead.
// bufferInfo.presentationTimeUs should never be 0 because we apply streamOffsetUs to the
// buffer presentationTimeUs.
bufferInfo.presentationTimeUs = finalFramePresentationTimeUs;
}
encoderOutputBuffer.timeUs = bufferInfo.presentationTimeUs;
encoderOutputBuffer.setFlags(bufferInfo.flags);
return encoderOutputBuffer;
Expand Down

0 comments on commit e4db710

Please sign in to comment.