mirror of
https://github.com/androidx/media.git
synced 2025-05-17 04:29:55 +08:00
Use MediaFormatUtil for creating MediaFormat from Format.
PiperOrigin-RevId: 509785247
This commit is contained in:
parent
79a3464014
commit
3608c2e831
@ -17,6 +17,7 @@
|
|||||||
package androidx.media3.transformer;
|
package androidx.media3.transformer;
|
||||||
|
|
||||||
import static androidx.media3.common.util.Assertions.checkNotNull;
|
import static androidx.media3.common.util.Assertions.checkNotNull;
|
||||||
|
import static androidx.media3.common.util.MediaFormatUtil.createMediaFormatFromFormat;
|
||||||
import static androidx.media3.common.util.Util.SDK_INT;
|
import static androidx.media3.common.util.Util.SDK_INT;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
@ -51,12 +52,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Codec createForAudioDecoding(Format format) throws TransformationException {
|
public Codec createForAudioDecoding(Format format) throws TransformationException {
|
||||||
MediaFormat mediaFormat =
|
checkNotNull(format.sampleMimeType);
|
||||||
MediaFormat.createAudioFormat(
|
MediaFormat mediaFormat = createMediaFormatFromFormat(format);
|
||||||
checkNotNull(format.sampleMimeType), format.sampleRate, format.channelCount);
|
|
||||||
MediaFormatUtil.maybeSetInteger(
|
|
||||||
mediaFormat, MediaFormat.KEY_MAX_INPUT_SIZE, format.maxInputSize);
|
|
||||||
MediaFormatUtil.setCsdBuffers(mediaFormat, format.initializationData);
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
String mediaCodecName = EncoderUtil.findCodecForFormat(mediaFormat, /* isDecoder= */ true);
|
String mediaCodecName = EncoderUtil.findCodecForFormat(mediaFormat, /* isDecoder= */ true);
|
||||||
@ -79,6 +76,7 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
|
|||||||
Format format, Surface outputSurface, boolean requestSdrToneMapping)
|
Format format, Surface outputSurface, boolean requestSdrToneMapping)
|
||||||
throws TransformationException {
|
throws TransformationException {
|
||||||
checkNotNull(format.sampleMimeType);
|
checkNotNull(format.sampleMimeType);
|
||||||
|
|
||||||
if (ColorInfo.isTransferHdr(format.colorInfo)) {
|
if (ColorInfo.isTransferHdr(format.colorInfo)) {
|
||||||
if (requestSdrToneMapping && (SDK_INT < 31 || deviceNeedsNoToneMappingWorkaround())) {
|
if (requestSdrToneMapping && (SDK_INT < 31 || deviceNeedsNoToneMappingWorkaround())) {
|
||||||
throw createTransformationException(
|
throw createTransformationException(
|
||||||
@ -91,13 +89,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MediaFormat mediaFormat =
|
MediaFormat mediaFormat = createMediaFormatFromFormat(format);
|
||||||
MediaFormat.createVideoFormat(format.sampleMimeType, format.width, format.height);
|
|
||||||
MediaFormatUtil.maybeSetInteger(mediaFormat, MediaFormat.KEY_ROTATION, format.rotationDegrees);
|
|
||||||
MediaFormatUtil.maybeSetInteger(
|
|
||||||
mediaFormat, MediaFormat.KEY_MAX_INPUT_SIZE, format.maxInputSize);
|
|
||||||
MediaFormatUtil.setCsdBuffers(mediaFormat, format.initializationData);
|
|
||||||
MediaFormatUtil.maybeSetColorInfo(mediaFormat, format.colorInfo);
|
|
||||||
if (decoderSupportsKeyAllowFrameDrop) {
|
if (decoderSupportsKeyAllowFrameDrop) {
|
||||||
// This key ensures no frame dropping when the decoder's output surface is full. This allows
|
// This key ensures no frame dropping when the decoder's output surface is full. This allows
|
||||||
// transformer to decode as many frames as possible in one render cycle.
|
// transformer to decode as many frames as possible in one render cycle.
|
||||||
|
@ -20,6 +20,7 @@ import static androidx.media3.common.util.Assertions.checkArgument;
|
|||||||
import static androidx.media3.common.util.Assertions.checkNotNull;
|
import static androidx.media3.common.util.Assertions.checkNotNull;
|
||||||
import static androidx.media3.common.util.Assertions.checkState;
|
import static androidx.media3.common.util.Assertions.checkState;
|
||||||
import static androidx.media3.common.util.Assertions.checkStateNotNull;
|
import static androidx.media3.common.util.Assertions.checkStateNotNull;
|
||||||
|
import static androidx.media3.common.util.MediaFormatUtil.createMediaFormatFromFormat;
|
||||||
import static java.lang.Math.abs;
|
import static java.lang.Math.abs;
|
||||||
import static java.lang.Math.floor;
|
import static java.lang.Math.floor;
|
||||||
import static java.lang.Math.round;
|
import static java.lang.Math.round;
|
||||||
@ -34,7 +35,6 @@ import androidx.media3.common.ColorInfo;
|
|||||||
import androidx.media3.common.Format;
|
import androidx.media3.common.Format;
|
||||||
import androidx.media3.common.MimeTypes;
|
import androidx.media3.common.MimeTypes;
|
||||||
import androidx.media3.common.util.Log;
|
import androidx.media3.common.util.Log;
|
||||||
import androidx.media3.common.util.MediaFormatUtil;
|
|
||||||
import androidx.media3.common.util.UnstableApi;
|
import androidx.media3.common.util.UnstableApi;
|
||||||
import androidx.media3.common.util.Util;
|
import androidx.media3.common.util.Util;
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
@ -175,10 +175,7 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
|
|||||||
@Override
|
@Override
|
||||||
public DefaultCodec createForAudioEncoding(Format format) throws TransformationException {
|
public DefaultCodec createForAudioEncoding(Format format) throws TransformationException {
|
||||||
checkNotNull(format.sampleMimeType);
|
checkNotNull(format.sampleMimeType);
|
||||||
MediaFormat mediaFormat =
|
MediaFormat mediaFormat = createMediaFormatFromFormat(format);
|
||||||
MediaFormat.createAudioFormat(
|
|
||||||
format.sampleMimeType, format.sampleRate, format.channelCount);
|
|
||||||
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, format.bitrate);
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
String mediaCodecName = EncoderUtil.findCodecForFormat(mediaFormat, /* isDecoder= */ false);
|
String mediaCodecName = EncoderUtil.findCodecForFormat(mediaFormat, /* isDecoder= */ false);
|
||||||
@ -233,10 +230,6 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
|
|||||||
encoderAndClosestFormatSupport.supportedEncoderSettings;
|
encoderAndClosestFormatSupport.supportedEncoderSettings;
|
||||||
|
|
||||||
String mimeType = checkNotNull(encoderSupportedFormat.sampleMimeType);
|
String mimeType = checkNotNull(encoderSupportedFormat.sampleMimeType);
|
||||||
MediaFormat mediaFormat =
|
|
||||||
MediaFormat.createVideoFormat(
|
|
||||||
mimeType, encoderSupportedFormat.width, encoderSupportedFormat.height);
|
|
||||||
mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, round(encoderSupportedFormat.frameRate));
|
|
||||||
|
|
||||||
int finalBitrate;
|
int finalBitrate;
|
||||||
if (enableFallback) {
|
if (enableFallback) {
|
||||||
@ -267,8 +260,10 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
|
|||||||
encoderSupportedFormat =
|
encoderSupportedFormat =
|
||||||
encoderSupportedFormat.buildUpon().setAverageBitrate(finalBitrate).build();
|
encoderSupportedFormat.buildUpon().setAverageBitrate(finalBitrate).build();
|
||||||
|
|
||||||
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, encoderSupportedFormat.averageBitrate);
|
MediaFormat mediaFormat = createMediaFormatFromFormat(encoderSupportedFormat);
|
||||||
mediaFormat.setInteger(MediaFormat.KEY_BITRATE_MODE, supportedVideoEncoderSettings.bitrateMode);
|
mediaFormat.setInteger(MediaFormat.KEY_BITRATE_MODE, supportedVideoEncoderSettings.bitrateMode);
|
||||||
|
// Some older devices (API 21) fail to initialize the encoder if frame rate is not an integer.
|
||||||
|
mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, round(encoderSupportedFormat.frameRate));
|
||||||
|
|
||||||
if (supportedVideoEncoderSettings.profile != VideoEncoderSettings.NO_VALUE
|
if (supportedVideoEncoderSettings.profile != VideoEncoderSettings.NO_VALUE
|
||||||
&& supportedVideoEncoderSettings.level != VideoEncoderSettings.NO_VALUE
|
&& supportedVideoEncoderSettings.level != VideoEncoderSettings.NO_VALUE
|
||||||
@ -283,7 +278,6 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
|
|||||||
adjustMediaFormatForH264EncoderSettings(format.colorInfo, encoderInfo, mediaFormat);
|
adjustMediaFormatForH264EncoderSettings(format.colorInfo, encoderInfo, mediaFormat);
|
||||||
}
|
}
|
||||||
|
|
||||||
MediaFormatUtil.maybeSetColorInfo(mediaFormat, encoderSupportedFormat.colorInfo);
|
|
||||||
if (Util.SDK_INT >= 31 && ColorInfo.isTransferHdr(format.colorInfo)) {
|
if (Util.SDK_INT >= 31 && ColorInfo.isTransferHdr(format.colorInfo)) {
|
||||||
// TODO(b/260389841): Validate the picked encoder supports HDR editing.
|
// TODO(b/260389841): Validate the picked encoder supports HDR editing.
|
||||||
if (EncoderUtil.getSupportedColorFormats(encoderInfo, mimeType)
|
if (EncoderUtil.getSupportedColorFormats(encoderInfo, mimeType)
|
||||||
@ -300,12 +294,12 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
|
|||||||
MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
|
MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Float I-frame intervals are only supported from API 25.
|
||||||
if (Util.SDK_INT >= 25) {
|
if (Util.SDK_INT >= 25) {
|
||||||
mediaFormat.setFloat(
|
mediaFormat.setFloat(
|
||||||
MediaFormat.KEY_I_FRAME_INTERVAL, supportedVideoEncoderSettings.iFrameIntervalSeconds);
|
MediaFormat.KEY_I_FRAME_INTERVAL, supportedVideoEncoderSettings.iFrameIntervalSeconds);
|
||||||
} else {
|
} else {
|
||||||
float iFrameIntervalSeconds = supportedVideoEncoderSettings.iFrameIntervalSeconds;
|
float iFrameIntervalSeconds = supportedVideoEncoderSettings.iFrameIntervalSeconds;
|
||||||
// Only integer I-frame intervals are supported before API 25.
|
|
||||||
// Round up values in (0, 1] to avoid the special 'all keyframes' behavior when passing 0.
|
// Round up values in (0, 1] to avoid the special 'all keyframes' behavior when passing 0.
|
||||||
mediaFormat.setInteger(
|
mediaFormat.setInteger(
|
||||||
MediaFormat.KEY_I_FRAME_INTERVAL,
|
MediaFormat.KEY_I_FRAME_INTERVAL,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user