@UnstableApi
public final class Presentation implements MatrixTransformation


Controls how a frame is presented with options to set the output resolution and choose how to map the input pixels onto the output frame geometry (for example, by stretching the input frame to match the specified output frame, or fitting the input frame using letterboxing).

The background color of the output frame will be black, with alpha = 0 if applicable.

Summary

Nested types

@Documented
@Retention(value = SOURCE)
@Target(value = TYPE_USE)
@IntDef(value = )
public annotation Presentation.Layout

Strategies controlling the layout of input pixels in the output frame.

Constants

static final int

Empty pixels added above and below the input frame (for letterboxing), or to the left and right of the input frame (for pillarboxing), until the desired aspect ratio is achieved.

static final int

Pixels cropped from the input frame, until the desired aspect ratio is achieved.

static final int

Frame stretched larger on the x or y axes to fit the desired aspect ratio.

Public methods

Size
configure(int inputWidth, int inputHeight)

Configures the input and output dimensions.

static Presentation
createForAspectRatio(
    @FloatRange(from = 0, fromInclusive = false) float aspectRatio,
    @Presentation.Layout int layout
)

Creates a new Presentation instance.

static Presentation
createForHeight(int height)

Creates a new Presentation instance.

static Presentation
createForWidthAndHeight(
    int width,
    int height,
    @Presentation.Layout int layout
)

Creates a new Presentation instance.

Matrix
getMatrix(long presentationTimeUs)

Returns the 3x3 transformation Matrix to apply to the frame with the given timestamp.

boolean
isNoOp(int inputWidth, int inputHeight)

Returns whether a GlEffect applies no change at every timestamp.

Inherited methods

From androidx.media3.common.Effect
long

Returns the expected duration of the output stream when the effect is applied given a input durationUs.

From androidx.media3.effect.GlEffect
abstract GlShaderProgram
toGlShaderProgram(Context context, boolean useHdr)

Returns a GlShaderProgram that applies the effect.

From androidx.media3.effect.GlMatrixTransformation
BaseGlShaderProgram
toGlShaderProgram(Context context, boolean useHdr)

Returns a GlShaderProgram that applies the effect.

From androidx.media3.effect.MatrixTransformation
float[]
getGlMatrixArray(long presentationTimeUs)

Returns the 4x4 transformation Matrix to apply to the frame with the given timestamp.

Constants

LAYOUT_SCALE_TO_FIT

public static final int LAYOUT_SCALE_TO_FIT = 0

Empty pixels added above and below the input frame (for letterboxing), or to the left and right of the input frame (for pillarboxing), until the desired aspect ratio is achieved. All input frame pixels will be within the output frame.

When applying:

  • letterboxing, the output width will default to the input width, and the output height will be scaled appropriately.
  • pillarboxing, the output height will default to the input height, and the output width will be scaled appropriately.

LAYOUT_SCALE_TO_FIT_WITH_CROP

public static final int LAYOUT_SCALE_TO_FIT_WITH_CROP = 1

Pixels cropped from the input frame, until the desired aspect ratio is achieved. Pixels may be cropped either from the bottom and top, or from the left and right sides, of the input frame.

When cropping from the:

  • bottom and top, the output width will default to the input width, and the output height will be scaled appropriately.
  • left and right, the output height will default to the input height, and the output width will be scaled appropriately.

LAYOUT_STRETCH_TO_FIT

public static final int LAYOUT_STRETCH_TO_FIT = 2

Frame stretched larger on the x or y axes to fit the desired aspect ratio.

When stretching to a:

  • taller aspect ratio, the output width will default to the input width, and the output height will be scaled appropriately.
  • narrower aspect ratio, the output height will default to the input height, and the output width will be scaled appropriately.

Public methods

configure

public Size configure(int inputWidth, int inputHeight)

Configures the input and output dimensions.

Must be called before getGlMatrixArray.

Parameters
int inputWidth

The input frame width, in pixels.

int inputHeight

The input frame height, in pixels.

Returns
Size

The output frame width and height, in pixels.

createForAspectRatio

public static Presentation createForAspectRatio(
    @FloatRange(from = 0, fromInclusive = false) float aspectRatio,
    @Presentation.Layout int layout
)

Creates a new Presentation instance.

The output frame will have the given aspect ratio (width/height ratio). Width or height will be resized to conform to this aspectRatio, given a Layout.

Parameters
@FloatRange(from = 0, fromInclusive = false) float aspectRatio

The aspect ratio (width/height ratio) of the output frame. Must be positive.

@Presentation.Layout int layout

The layout of the output frame.

createForHeight

public static Presentation createForHeight(int height)

Creates a new Presentation instance.

The output frame will have the given height. Width will scale to preserve the input aspect ratio. For example, a 1920x1440 video can be scaled to 640x480 by passing a height of 480.

Parameters
int height

The height of the output frame, in pixels.

createForWidthAndHeight

public static Presentation createForWidthAndHeight(
    int width,
    int height,
    @Presentation.Layout int layout
)

Creates a new Presentation instance.

The output frame will have the given width and height, given a Layout.

Width and height must be positive integers representing the output frame's width and height.

Parameters
int width

The width of the output frame, in pixels.

int height

The height of the output frame, in pixels.

@Presentation.Layout int layout

The layout of the output frame.

getMatrix

public Matrix getMatrix(long presentationTimeUs)

Returns the 3x3 transformation Matrix to apply to the frame with the given timestamp.

isNoOp

public boolean isNoOp(int inputWidth, int inputHeight)

Returns whether a GlEffect applies no change at every timestamp.

This can be used as a hint to skip this instance.

Parameters
int inputWidth

The input frame width, in pixels.

int inputHeight

The input frame height, in pixels.