Skip to content

Commit

Permalink
Avoid value close to overflow for KEY_OPERATING_RATE
Browse files Browse the repository at this point in the history
Using `Integer.MAX_VALUE` risks causing arithmetic overflow in the codec
implementation.

Issue: androidx#810

#minor-release

PiperOrigin-RevId: 585104621
  • Loading branch information
andrewlewis authored and copybara-github committed Nov 24, 2023
1 parent bc36553 commit ad40db4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* Transformer:
* Add support for flattening H.265/HEVC SEF slow motion videos.
* Increase transmuxing speed, especially for 'remove video' edits.
* Work around an issue where the encoder would throw at configuration time
due to setting a high operating rate.
* Track Selection:
* Add `DefaultTrackSelector.selectImageTrack` to enable image track
selection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
/** Best effort, or as-fast-as-possible priority setting for {@link MediaFormat#KEY_PRIORITY}. */
private static final int PRIORITY_BEST_EFFORT = 1;

private static final String TAG = "DefaultEncoderFactory";

/** A builder for {@link DefaultEncoderFactory} instances. */
public static final class Builder {
private final Context context;
Expand Down Expand Up @@ -519,6 +517,11 @@ private static void adjustMediaFormatForEncoderPerformanceSettings(MediaFormat m

if (Util.SDK_INT == 26) {
mediaFormat.setInteger(MediaFormat.KEY_OPERATING_RATE, DEFAULT_FRAME_RATE);
} else if (Util.SDK_INT <= 34) {
// On some devices setting Integer.MAX_VALUE will cause the encoder to throw at configuration
// time. Setting the operating to 1000 avoids being close to an integer overflow limit while
// being higher than a maximum feasible operating rate. See [internal b/311206113].
mediaFormat.setInteger(MediaFormat.KEY_OPERATING_RATE, 1000);
} else {
mediaFormat.setInteger(MediaFormat.KEY_OPERATING_RATE, Integer.MAX_VALUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ public void createForVideoEncoding_withFallbackOnAndUnsupportedResolution_config
assertThat(configurationMediaFormat.containsKey(MediaFormat.KEY_PRIORITY)).isTrue();
assertThat(configurationMediaFormat.getInteger(MediaFormat.KEY_PRIORITY)).isEqualTo(1);
assertThat(configurationMediaFormat.containsKey(MediaFormat.KEY_OPERATING_RATE)).isTrue();
assertThat(configurationMediaFormat.getInteger(MediaFormat.KEY_OPERATING_RATE))
.isEqualTo(Integer.MAX_VALUE);
assertThat(configurationMediaFormat.getInteger(MediaFormat.KEY_OPERATING_RATE)).isEqualTo(1000);
}

@Test
Expand Down

0 comments on commit ad40db4

Please sign in to comment.