@@ -61,13 +61,9 @@ public class VideoTrackTranscoder implements TrackTranscoder {
61
61
private VideoDecoderOutput mDecoderOutputSurface ;
62
62
private VideoEncoderInput mEncoderInputSurface ;
63
63
64
- // A step is defined as the microseconds between two frame.
65
- // The average step is basically 1 / frame rate.
66
- private float mAvgStep = 0 ;
67
- private float mTargetAvgStep ;
68
- private int mRenderedSteps = -1 ; // frames - 1
69
- private long mLastRenderedUs ;
70
- private long mLastStep ;
64
+ private double mInFrameRateReciprocal ;
65
+ private double mOutFrameRateReciprocal ;
66
+ private double mFrameRateReciprocalSum ;
71
67
72
68
public VideoTrackTranscoder (
73
69
@ NonNull MediaExtractor extractor ,
@@ -82,8 +78,12 @@ public VideoTrackTranscoder(
82
78
public void setUp (@ NonNull MediaFormat desiredOutputFormat ) {
83
79
mExtractor .selectTrack (mTrackIndex );
84
80
85
- int frameRate = desiredOutputFormat .getInteger (MediaFormat .KEY_FRAME_RATE );
86
- mTargetAvgStep = (1F / frameRate ) * 1000 * 1000 ;
81
+ MediaFormat trackFormat = mExtractor .getTrackFormat (mTrackIndex );
82
+ int inFrameRate = trackFormat .getInteger (MediaFormat .KEY_FRAME_RATE );
83
+ int outFrameRate = desiredOutputFormat .getInteger (MediaFormat .KEY_FRAME_RATE );
84
+ mInFrameRateReciprocal = 1.0d / inFrameRate ;
85
+ mOutFrameRateReciprocal = 1.0d / outFrameRate ;
86
+ LOG .v ("mInFrameRateReciprocal: " + mInFrameRateReciprocal + " mOutFrameRateReciprocal: " + mOutFrameRateReciprocal );
87
87
88
88
// Configure encoder.
89
89
try {
@@ -228,37 +228,25 @@ private int drainDecoder(long timeoutUs) {
228
228
return DRAIN_STATE_CONSUMED ;
229
229
}
230
230
231
- // TODO improve this. as it is now, rendering a frame after dropping many,
232
- // will not decrease avgStep but rather increase it (for this single frame; then it starts decreasing).
233
- // This has the effect that, when a frame is rendered, the following frame is always rendered,
234
- // because the conditions are worse then before. After this second frame things go back to normal,
235
- // but this is terrible logic.
231
+ //Refer:https://stackoverflow.com/questions/4223766/dropping-video-frames
236
232
private boolean shouldRenderFrame () {
237
233
if (mBufferInfo .size <= 0 ) return false ;
238
- if (mRenderedSteps > 0 && mAvgStep < mTargetAvgStep ) {
239
- // We are rendering too much. Drop this frame.
240
- // Always render first 2 frames, we need them to compute the avg.
241
- LOG .v ("FRAME: Dropping. avg: " + mAvgStep + " target: " + mTargetAvgStep );
242
- long newLastStep = mBufferInfo .presentationTimeUs - mLastRenderedUs ;
243
- float allSteps = (mAvgStep * mRenderedSteps ) - mLastStep + newLastStep ;
244
- mAvgStep = allSteps / mRenderedSteps ; // we didn't add a step, just increased the last
245
- mLastStep = newLastStep ;
246
- return false ;
247
- } else {
248
- // Render this frame, since our average step is too long or exact.
249
- LOG .v ("FRAME: RENDERING. avg: " + mAvgStep + " target: " + mTargetAvgStep + "New stepCount: " + (mRenderedSteps + 1 ));
250
- if (mRenderedSteps >= 0 ) {
251
- // Update the average value, since now we have mLastRenderedUs.
252
- long step = mBufferInfo .presentationTimeUs - mLastRenderedUs ;
253
- float allSteps = (mAvgStep * mRenderedSteps ) + step ;
254
- mAvgStep = allSteps / (mRenderedSteps + 1 ); // we added a step, so +1
255
- mLastStep = step ;
256
- }
257
- // Increment both
258
- mRenderedSteps ++;
259
- mLastRenderedUs = mBufferInfo .presentationTimeUs ;
234
+ boolean firstFrame = Double .valueOf (0d ).equals (mFrameRateReciprocalSum );
235
+ mFrameRateReciprocalSum += mInFrameRateReciprocal ;
236
+ if (firstFrame ) {
237
+ // render frame
238
+ LOG .v ("render this frame -> mFrameRateReciprocalSum: " + mFrameRateReciprocalSum );
260
239
return true ;
261
240
}
241
+ if (mFrameRateReciprocalSum > mOutFrameRateReciprocal ) {
242
+ mFrameRateReciprocalSum -= mOutFrameRateReciprocal ;
243
+ // render frame
244
+ LOG .v ("render this frame -> mFrameRateReciprocalSum: " + mFrameRateReciprocalSum );
245
+ return true ;
246
+ }
247
+ // drop frame
248
+ LOG .v ("drop this frame -> mFrameRateReciprocalSum: " + mFrameRateReciprocalSum );
249
+ return false ;
262
250
}
263
251
264
252
@ SuppressWarnings ("SameParameterValue" )
0 commit comments