1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.media;
18 
19 import static android.media.codec.Flags.FLAG_IN_PROCESS_SW_AUDIO_CODEC;
20 import static android.media.codec.Flags.FLAG_REGION_OF_INTEREST;
21 
22 import static com.android.media.codec.flags.Flags.FLAG_CODEC_IMPORTANCE;
23 import static com.android.media.codec.flags.Flags.FLAG_LARGE_AUDIO_FRAME;
24 
25 import android.annotation.FlaggedApi;
26 import android.annotation.IntDef;
27 import android.annotation.NonNull;
28 import android.annotation.Nullable;
29 import android.compat.annotation.UnsupportedAppUsage;
30 import android.graphics.Rect;
31 import android.text.TextUtils;
32 
33 import java.lang.annotation.Retention;
34 import java.lang.annotation.RetentionPolicy;
35 import java.nio.ByteBuffer;
36 import java.nio.ByteOrder;
37 import java.util.AbstractSet;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Set;
43 import java.util.stream.Collectors;
44 
45 /**
46  * Encapsulates the information describing the format of media data, be it audio or video, as
47  * well as optional feature metadata.
48  * <p>
49  * The format of the media data is specified as key/value pairs. Keys are strings. Values can
50  * be integer, long, float, String or ByteBuffer.
51  * <p>
52  * The feature metadata is specificed as string/boolean pairs.
53  * <p>
54  * Keys common to all audio/video formats, <b>all keys not marked optional are mandatory</b>:
55  *
56  * <table>
57  * <tr><th>Name</th><th>Value Type</th><th>Description</th></tr>
58  * <tr><td>{@link #KEY_MIME}</td><td>String</td><td>The type of the format.</td></tr>
59  * <tr><td>{@link #KEY_CODECS_STRING}</td><td>String</td><td>optional, the RFC 6381 codecs string of the MediaFormat</td></tr>
60  * <tr><td>{@link #KEY_MAX_INPUT_SIZE}</td><td>Integer</td><td>optional, maximum size of a buffer of input data</td></tr>
61  * <tr><td>{@link #KEY_PIXEL_ASPECT_RATIO_WIDTH}</td><td>Integer</td><td>optional, the pixel aspect ratio width</td></tr>
62  * <tr><td>{@link #KEY_PIXEL_ASPECT_RATIO_HEIGHT}</td><td>Integer</td><td>optional, the pixel aspect ratio height</td></tr>
63  * <tr><td>{@link #KEY_BIT_RATE}</td><td>Integer</td><td><b>encoder-only</b>, desired bitrate in bits/second</td></tr>
64  * <tr><td>{@link #KEY_DURATION}</td><td>long</td><td>the duration of the content (in microseconds)</td></tr>
65  * </table>
66  *
67  * Video formats have the following keys:
68  * <table>
69  * <tr><th>Name</th><th>Value Type</th><th>Description</th></tr>
70  * <tr><td>{@link #KEY_WIDTH}</td><td>Integer</td><td></td></tr>
71  * <tr><td>{@link #KEY_HEIGHT}</td><td>Integer</td><td></td></tr>
72  * <tr><td>{@link #KEY_COLOR_FORMAT}</td><td>Integer</td><td>set by the user
73  *         for encoders, readable in the output format of decoders</b></td></tr>
74  * <tr><td>{@link #KEY_FRAME_RATE}</td><td>Integer or Float</td><td>required for <b>encoders</b>,
75  *         optional for <b>decoders</b></td></tr>
76  * <tr><td>{@link #KEY_CAPTURE_RATE}</td><td>Integer</td><td></td></tr>
77  * <tr><td>{@link #KEY_I_FRAME_INTERVAL}</td><td>Integer (or Float)</td><td><b>encoder-only</b>,
78  *         time-interval between key frames.
79  *         Float support added in {@link android.os.Build.VERSION_CODES#N_MR1}</td></tr>
80  * <tr><td>{@link #KEY_INTRA_REFRESH_PERIOD}</td><td>Integer</td><td><b>encoder-only</b>, optional</td></tr>
81  * <tr><td>{@link #KEY_LATENCY}</td><td>Integer</td><td><b>encoder-only</b>, optional</td></tr>
82  * <tr><td>{@link #KEY_MAX_WIDTH}</td><td>Integer</td><td><b>decoder-only</b>, optional, max-resolution width</td></tr>
83  * <tr><td>{@link #KEY_MAX_HEIGHT}</td><td>Integer</td><td><b>decoder-only</b>, optional, max-resolution height</td></tr>
84  * <tr><td>{@link #KEY_REPEAT_PREVIOUS_FRAME_AFTER}</td><td>Long</td><td><b>encoder in surface-mode
85  *         only</b>, optional</td></tr>
86  * <tr><td>{@link #KEY_PUSH_BLANK_BUFFERS_ON_STOP}</td><td>Integer(1)</td><td><b>decoder rendering
87  *         to a surface only</b>, optional</td></tr>
88  * <tr><td>{@link #KEY_TEMPORAL_LAYERING}</td><td>String</td><td><b>encoder only</b>, optional,
89  *         temporal-layering schema</td></tr>
90  * </table>
91  * Specify both {@link #KEY_MAX_WIDTH} and {@link #KEY_MAX_HEIGHT} to enable
92  * adaptive playback (seamless resolution change) for a video decoder that
93  * supports it ({@link MediaCodecInfo.CodecCapabilities#FEATURE_AdaptivePlayback}).
94  * The values are used as hints for the codec: they are the maximum expected
95  * resolution to prepare for.  Depending on codec support, preparing for larger
96  * maximum resolution may require more memory even if that resolution is never
97  * reached.  These fields have no effect for codecs that do not support adaptive
98  * playback.<br /><br />
99  *
100  * Audio formats have the following keys:
101  * <table>
102  * <tr><th>Name</th><th>Value Type</th><th>Description</th></tr>
103  * <tr><td>{@link #KEY_CHANNEL_COUNT}</td><td>Integer</td><td></td></tr>
104  * <tr><td>{@link #KEY_SAMPLE_RATE}</td><td>Integer</td><td></td></tr>
105  * <tr><td>{@link #KEY_PCM_ENCODING}</td><td>Integer</td><td>optional</td></tr>
106  * <tr><td>{@link #KEY_IS_ADTS}</td><td>Integer</td><td>optional, if <em>decoding</em> AAC audio content, setting this key to 1 indicates that each audio frame is prefixed by the ADTS header.</td></tr>
107  * <tr><td>{@link #KEY_AAC_PROFILE}</td><td>Integer</td><td><b>encoder-only</b>, optional, if content is AAC audio, specifies the desired profile.</td></tr>
108  * <tr><td>{@link #KEY_AAC_SBR_MODE}</td><td>Integer</td><td><b>encoder-only</b>, optional, if content is AAC audio, specifies the desired SBR mode.</td></tr>
109  * <tr><td>{@link #KEY_AAC_DRC_TARGET_REFERENCE_LEVEL}</td><td>Integer</td><td><b>decoder-only</b>, optional, if content is AAC audio, specifies the target reference level.</td></tr>
110  * <tr><td>{@link #KEY_AAC_ENCODED_TARGET_LEVEL}</td><td>Integer</td><td><b>decoder-only</b>, optional, if content is AAC audio, specifies the target reference level used at encoder.</td></tr>
111  * <tr><td>{@link #KEY_AAC_DRC_BOOST_FACTOR}</td><td>Integer</td><td><b>decoder-only</b>, optional, if content is AAC audio, specifies the DRC boost factor.</td></tr>
112  * <tr><td>{@link #KEY_AAC_DRC_ATTENUATION_FACTOR}</td><td>Integer</td><td><b>decoder-only</b>, optional, if content is AAC audio, specifies the DRC attenuation factor.</td></tr>
113  * <tr><td>{@link #KEY_AAC_DRC_HEAVY_COMPRESSION}</td><td>Integer</td><td><b>decoder-only</b>, optional, if content is AAC audio, specifies whether to use heavy compression.</td></tr>
114  * <tr><td>{@link #KEY_AAC_MAX_OUTPUT_CHANNEL_COUNT}</td><td>Integer</td><td><b>decoder-only</b>, optional, if content is AAC audio, specifies the maximum number of channels the decoder outputs.</td></tr>
115  * <tr><td>{@link #KEY_AAC_DRC_EFFECT_TYPE}</td><td>Integer</td><td><b>decoder-only</b>, optional, if content is AAC audio, specifies the MPEG-D DRC effect type to use.</td></tr>
116  * <tr><td>{@link #KEY_AAC_DRC_OUTPUT_LOUDNESS}</td><td>Integer</td><td><b>decoder-only</b>, optional, if content is AAC audio, returns the DRC output loudness.</td></tr>
117  * <tr><td>{@link #KEY_AAC_DRC_ALBUM_MODE}</td><td>Integer</td><td><b>decoder-only</b>, optional, if content is AAC audio, specifies the whether MPEG-D DRC Album Mode is active or not.</td></tr>
118  * <tr><td>{@link #KEY_CHANNEL_MASK}</td><td>Integer</td><td>optional, a mask of audio channel assignments</td></tr>
119  * <tr><td>{@link #KEY_ENCODER_DELAY}</td><td>Integer</td><td>optional, the number of frames to trim from the start of the decoded audio stream.</td></tr>
120  * <tr><td>{@link #KEY_ENCODER_PADDING}</td><td>Integer</td><td>optional, the number of frames to trim from the end of the decoded audio stream.</td></tr>
121  * <tr><td>{@link #KEY_FLAC_COMPRESSION_LEVEL}</td><td>Integer</td><td><b>encoder-only</b>, optional, if content is FLAC audio, specifies the desired compression level.</td></tr>
122  * <tr><td>{@link #KEY_MPEGH_PROFILE_LEVEL_INDICATION}</td><td>Integer</td>
123  *     <td><b>decoder-only</b>, optional, if content is MPEG-H audio,
124  *         specifies the profile and level of the stream.</td></tr>
125  * <tr><td>{@link #KEY_MPEGH_COMPATIBLE_SETS}</td><td>ByteBuffer</td>
126  *     <td><b>decoder-only</b>, optional, if content is MPEG-H audio,
127  *         specifies the compatible sets (profile and level) of the stream.</td></tr>
128  * <tr><td>{@link #KEY_MPEGH_REFERENCE_CHANNEL_LAYOUT}</td>
129  *     <td>Integer</td><td><b>decoder-only</b>, optional, if content is MPEG-H audio,
130  *         specifies the preferred reference channel layout of the stream.</td></tr>
131  * <tr><td>{@link #KEY_MAX_BUFFER_BATCH_OUTPUT_SIZE}</td><td>Integer</td><td>optional, used with
132  *         large audio frame support, specifies max size of output buffer in bytes.</td></tr>
133  * <tr><td>{@link #KEY_BUFFER_BATCH_THRESHOLD_OUTPUT_SIZE}</td><td>Integer</td><td>optional,
134  *         used with large audio frame support, specifies threshold output size in bytes.</td></tr>
135  * </table>
136  *
137  * Subtitle formats have the following keys:
138  * <table>
139  * <tr><td>{@link #KEY_MIME}</td><td>String</td><td>The type of the format.</td></tr>
140  * <tr><td>{@link #KEY_LANGUAGE}</td><td>String</td><td>The language of the content.</td></tr>
141  * <tr><td>{@link #KEY_CAPTION_SERVICE_NUMBER}</td><td>int</td><td>optional, the closed-caption service or channel number.</td></tr>
142  * </table>
143  *
144  * Image formats have the following keys:
145  * <table>
146  * <tr><td>{@link #KEY_MIME}</td><td>String</td><td>The type of the format.</td></tr>
147  * <tr><td>{@link #KEY_WIDTH}</td><td>Integer</td><td></td></tr>
148  * <tr><td>{@link #KEY_HEIGHT}</td><td>Integer</td><td></td></tr>
149  * <tr><td>{@link #KEY_COLOR_FORMAT}</td><td>Integer</td><td>set by the user
150  *         for encoders, readable in the output format of decoders</b></td></tr>
151  * <tr><td>{@link #KEY_TILE_WIDTH}</td><td>Integer</td><td>required if the image has grid</td></tr>
152  * <tr><td>{@link #KEY_TILE_HEIGHT}</td><td>Integer</td><td>required if the image has grid</td></tr>
153  * <tr><td>{@link #KEY_GRID_ROWS}</td><td>Integer</td><td>required if the image has grid</td></tr>
154  * <tr><td>{@link #KEY_GRID_COLUMNS}</td><td>Integer</td><td>required if the image has grid</td></tr>
155  * </table>
156  */
157 public final class MediaFormat {
158     public static final String MIMETYPE_VIDEO_VP8 = "video/x-vnd.on2.vp8";
159     public static final String MIMETYPE_VIDEO_VP9 = "video/x-vnd.on2.vp9";
160     public static final String MIMETYPE_VIDEO_AV1 = "video/av01";
161     public static final String MIMETYPE_VIDEO_AVC = "video/avc";
162     public static final String MIMETYPE_VIDEO_HEVC = "video/hevc";
163     public static final String MIMETYPE_VIDEO_MPEG4 = "video/mp4v-es";
164     public static final String MIMETYPE_VIDEO_H263 = "video/3gpp";
165     public static final String MIMETYPE_VIDEO_MPEG2 = "video/mpeg2";
166     public static final String MIMETYPE_VIDEO_RAW = "video/raw";
167     public static final String MIMETYPE_VIDEO_DOLBY_VISION = "video/dolby-vision";
168     public static final String MIMETYPE_VIDEO_SCRAMBLED = "video/scrambled";
169 
170     public static final String MIMETYPE_AUDIO_AMR_NB = "audio/3gpp";
171     public static final String MIMETYPE_AUDIO_AMR_WB = "audio/amr-wb";
172     public static final String MIMETYPE_AUDIO_MPEG = "audio/mpeg";
173     public static final String MIMETYPE_AUDIO_AAC = "audio/mp4a-latm";
174     public static final String MIMETYPE_AUDIO_QCELP = "audio/qcelp";
175     public static final String MIMETYPE_AUDIO_VORBIS = "audio/vorbis";
176     public static final String MIMETYPE_AUDIO_OPUS = "audio/opus";
177     public static final String MIMETYPE_AUDIO_G711_ALAW = "audio/g711-alaw";
178     public static final String MIMETYPE_AUDIO_G711_MLAW = "audio/g711-mlaw";
179     public static final String MIMETYPE_AUDIO_RAW = "audio/raw";
180     public static final String MIMETYPE_AUDIO_FLAC = "audio/flac";
181     public static final String MIMETYPE_AUDIO_MSGSM = "audio/gsm";
182     public static final String MIMETYPE_AUDIO_AC3 = "audio/ac3";
183     public static final String MIMETYPE_AUDIO_EAC3 = "audio/eac3";
184     public static final String MIMETYPE_AUDIO_EAC3_JOC = "audio/eac3-joc";
185     public static final String MIMETYPE_AUDIO_AC4 = "audio/ac4";
186     public static final String MIMETYPE_AUDIO_SCRAMBLED = "audio/scrambled";
187     /** MIME type for MPEG-H Audio single stream */
188     public static final String MIMETYPE_AUDIO_MPEGH_MHA1 = "audio/mha1";
189     /** MIME type for MPEG-H Audio single stream, encapsulated in MHAS */
190     public static final String MIMETYPE_AUDIO_MPEGH_MHM1 = "audio/mhm1";
191     /** MIME type for DTS Digital Surround (up to 5.1 channels) audio stream, aka DTS-CA. */
192     public static final String MIMETYPE_AUDIO_DTS = "audio/vnd.dts";
193     /**
194      * MIME type for DTS HD (up to 7.1 channels) audio stream.
195      * With codec profile DTS_HDProfileHRA represents DTS HD High Resolution Audio.
196      * With codec profile DTS_HDProfileMA represents DTS HD Master Audio.
197      * With codec profile DTS_HDProfileLBR represents DTS Express.
198      */
199     public static final String MIMETYPE_AUDIO_DTS_HD = "audio/vnd.dts.hd";
200     /**
201      * MIME type for DTS UHD (object-based) audio stream, aka DTS:X.
202      * With codec profile DTS_UHDProfileP1 represents DTS-UHD P1.
203      * With codec profile DTS_UHDProfileP2 represents DTS-UHD P2.
204      */
205     public static final String MIMETYPE_AUDIO_DTS_UHD = "audio/vnd.dts.uhd";
206     /** MIME type for Dynamic Resolution Adaptation (DRA) audio stream. */
207     public static final String MIMETYPE_AUDIO_DRA = "audio/vnd.dra";
208     /** MIME type for Dolby Metadata-enhanced Audio Transmission (MAT) audio stream. */
209     public static final String MIMETYPE_AUDIO_DOLBY_MAT = "audio/vnd.dolby.mat";
210     /** MIME type for Dolby TrueHD audio format, based on Meridian Lossless Packing (MLP). */
211     public static final String MIMETYPE_AUDIO_DOLBY_TRUEHD = "audio/vnd.dolby.mlp";
212     /**
213      * MIME type for AAC Low Complexity (LC) audio stream. Uses the scheme defined by
214      * RFC 6381 with OTI of MPEG-4 (40) and AOT of AAC LC (2) from ISO/IEC 14496-3.
215      */
216     public static final String MIMETYPE_AUDIO_AAC_LC = "audio/mp4a.40.02";
217     /**
218      * MIME type for HE-AAC v1 (LC + SBR) audio stream. Uses the scheme defined by
219      * RFC 6381 with OTI of MPEG-4 (40) and AOT of AAC SBR (5) from ISO/IEC 14496-3.
220      */
221     public static final String MIMETYPE_AUDIO_AAC_HE_V1 = "audio/mp4a.40.05";
222     /**
223      * MIME type for HE-AAC v2 (LC + SBR + PS) audio stream. Uses the scheme defined by
224      * RFC 6381 with OTI of MPEG-4 (40) and AOT of PS (29) from ISO/IEC 14496-3.
225      */
226     public static final String MIMETYPE_AUDIO_AAC_HE_V2 = "audio/mp4a.40.29";
227     /**
228      * MIME type for AAC Enhanced Low Delay (ELD) audio stream. Uses the scheme defined by
229      * RFC 6381 with OTI of MPEG-4 (40) and AOT of ELD (39) from ISO/IEC 14496-3.
230      */
231     public static final String MIMETYPE_AUDIO_AAC_ELD = "audio/mp4a.40.39";
232     /**
233      * MIME type for AAC XHE audio stream. Uses the scheme defined by
234      * RFC 6381 with OTI of MPEG-4 (40) and AOT of USAC (42) from ISO/IEC 14496-3.
235      */
236     public static final String MIMETYPE_AUDIO_AAC_XHE = "audio/mp4a.40.42";
237     /**
238      * MIME type for MPEG-H Baseline (BL) Profile L3 audio stream. Uses the scheme defined by
239      * RFC 6381 with mpegh3daProfileLevelIndication for main profile/L3 (0x3) from ISO/IEC 23008-3.
240      */
241     public static final String MIMETYPE_AUDIO_MPEGH_BL_L3 = "audio/mhm1.03";
242     /**
243      * MIME type for MPEG-H Baseline (BL) Profile L4 audio stream. Uses the scheme defined by
244      * RFC 6381 with mpegh3daProfileLevelIndication for main profile/L4 (0x4) from ISO/IEC 23008-3.
245      */
246     public static final String MIMETYPE_AUDIO_MPEGH_BL_L4 = "audio/mhm1.04";
247     /**
248      * MIME type for MPEG-H Low Complexity (LC) L3 audio stream. Uses the scheme defined by
249      * RFC 6381 with mpegh3daProfileLevelIndication for LC profile/L3 (0xD) from ISO/IEC 23008-3.
250      */
251     public static final String MIMETYPE_AUDIO_MPEGH_LC_L3 = "audio/mhm1.0d";
252     /**
253      * MIME type for MPEG-H Low Complexity (LC) L4 audio stream. Uses the scheme defined by
254      * RFC 6381 with mpegh3daProfileLevelIndication for LC profile/L4 (0xE) from ISO/IEC 23008-3.
255      */
256     public static final String MIMETYPE_AUDIO_MPEGH_LC_L4 = "audio/mhm1.0e";
257     /**
258      * MIME type for the IEC61937 audio stream encapsulation. This type isn't defined by IANA.
259      */
260     public static final String MIMETYPE_AUDIO_IEC61937 = "audio/x-iec61937";
261 
262     /**
263      * MIME type for HEIF still image data encoded in HEVC.
264      *
265      * To decode such an image, {@link MediaCodec} decoder for
266      * {@link #MIMETYPE_VIDEO_HEVC} shall be used. The client needs to form
267      * the correct {@link #MediaFormat} based on additional information in
268      * the track format (shown in the next paragraph), and send it to
269      * {@link MediaCodec#configure}.
270      *
271      * The track's MediaFormat will come with {@link #KEY_WIDTH} and
272      * {@link #KEY_HEIGHT} keys, which describes the width and height
273      * of the image. If the image doesn't contain grid (i.e. none of
274      * {@link #KEY_TILE_WIDTH}, {@link #KEY_TILE_HEIGHT},
275      * {@link #KEY_GRID_ROWS}, {@link #KEY_GRID_COLUMNS} are present), the
276      * track will contain a single sample of coded data for the entire image,
277      * and the image width and height should be used to set up the decoder.
278      *
279      * If the image does come with grid, each sample from the track will
280      * contain one tile in the grid, of which the size is described by
281      * {@link #KEY_TILE_WIDTH} and {@link #KEY_TILE_HEIGHT}. This size
282      * (instead of {@link #KEY_WIDTH} and {@link #KEY_HEIGHT}) should be
283      * used to set up the decoder. The track contains {@link #KEY_GRID_ROWS}
284      * by {@link #KEY_GRID_COLUMNS} samples in row-major, top-row first,
285      * left-to-right order. The output image should be reconstructed by
286      * first tiling the decoding results of the tiles in the correct order,
287      * then trimming (before rotation is applied) on the bottom and right
288      * side, if the tiled area is larger than the image width and height.
289      */
290     public static final String MIMETYPE_IMAGE_ANDROID_HEIC = "image/vnd.android.heic";
291 
292     /**
293      * MIME type for AVIF still image data encoded in AV1.
294      *
295      * To decode such an image, {@link MediaCodec} decoder for
296      * {@link #MIMETYPE_VIDEO_AV1} shall be used. The client needs to form
297      * the correct {@link #MediaFormat} based on additional information in
298      * the track format (shown in the next paragraph), and send it to
299      * {@link MediaCodec#configure}.
300      *
301      * The track's MediaFormat will come with {@link #KEY_WIDTH} and
302      * {@link #KEY_HEIGHT} keys, which describes the width and height
303      * of the image. If the image doesn't contain grid (i.e. none of
304      * {@link #KEY_TILE_WIDTH}, {@link #KEY_TILE_HEIGHT},
305      * {@link #KEY_GRID_ROWS}, {@link #KEY_GRID_COLUMNS} are present), the
306      * track will contain a single sample of coded data for the entire image,
307      * and the image width and height should be used to set up the decoder.
308      *
309      * If the image does come with grid, each sample from the track will
310      * contain one tile in the grid, of which the size is described by
311      * {@link #KEY_TILE_WIDTH} and {@link #KEY_TILE_HEIGHT}. This size
312      * (instead of {@link #KEY_WIDTH} and {@link #KEY_HEIGHT}) should be
313      * used to set up the decoder. The track contains {@link #KEY_GRID_ROWS}
314      * by {@link #KEY_GRID_COLUMNS} samples in row-major, top-row first,
315      * left-to-right order. The output image should be reconstructed by
316      * first tiling the decoding results of the tiles in the correct order,
317      * then trimming (before rotation is applied) on the bottom and right
318      * side, if the tiled area is larger than the image width and height.
319      */
320     public static final String MIMETYPE_IMAGE_AVIF = "image/avif";
321 
322     /**
323      * MIME type for WebVTT subtitle data.
324      */
325     public static final String MIMETYPE_TEXT_VTT = "text/vtt";
326 
327     /**
328      * MIME type for SubRip (SRT) container.
329      */
330     public static final String MIMETYPE_TEXT_SUBRIP = "application/x-subrip";
331 
332     /**
333      * MIME type for CEA-608 closed caption data.
334      */
335     public static final String MIMETYPE_TEXT_CEA_608 = "text/cea-608";
336 
337     /**
338      * MIME type for CEA-708 closed caption data.
339      */
340     public static final String MIMETYPE_TEXT_CEA_708 = "text/cea-708";
341 
342     @UnsupportedAppUsage
343     private Map<String, Object> mMap;
344 
345     /**
346      * A key describing the log session ID for MediaCodec. The log session ID is a random 32-byte
347      * hexadecimal string that is used to associate metrics from multiple media codec instances
348      * to the same playback or recording session. The value is created as
349      * {@link android.media.metrics.LogSessionId LogSessionId}. Sessions are created in
350      * {@link android.media.metrics.MediaMetricsManager MediaMetricsManager}.
351      * The associated value is a string.
352      */
353     public static final String LOG_SESSION_ID = "log-session-id";
354 
355     /**
356      * A key describing the mime type of the MediaFormat.
357      * The associated value is a string.
358      */
359     public static final String KEY_MIME = "mime";
360 
361     /**
362      * A key describing the codecs string of the MediaFormat. See RFC 6381 section 3.2 for the
363      * syntax of the value. The value does not hold {@link MediaCodec}-exposed codec names.
364      * The associated value is a string.
365      *
366      * @see MediaParser.TrackData#mediaFormat
367      */
368     public static final String KEY_CODECS_STRING = "codecs-string";
369 
370     /**
371      * An optional key describing the low latency decoding mode. This is an optional parameter
372      * that applies only to decoders. If enabled, the decoder doesn't hold input and output
373      * data more than required by the codec standards.
374      * The associated value is an integer (0 or 1): 1 when low-latency decoding is enabled,
375      * 0 otherwise. The default value is 0.
376      */
377     public static final String KEY_LOW_LATENCY = "low-latency";
378 
379     /**
380      * A key describing the language of the content, using either ISO 639-1
381      * or 639-2/T codes.  The associated value is a string.
382      */
383     public static final String KEY_LANGUAGE = "language";
384 
385     /**
386      * A key describing the closed caption service number. For CEA-608 caption tracks, holds the
387      * channel number. For CEA-708, holds the service number.
388      * The associated value is an int.
389      */
390     public static final String KEY_CAPTION_SERVICE_NUMBER = "caption-service-number";
391 
392     /**
393      * A key describing the sample rate of an audio format.
394      * The associated value is an integer
395      */
396     public static final String KEY_SAMPLE_RATE = "sample-rate";
397 
398     /**
399      * A key describing the number of channels in an audio format.
400      * The associated value is an integer
401      */
402     public static final String KEY_CHANNEL_COUNT = "channel-count";
403 
404     /**
405      * A key describing the width of the content in a video format.
406      * The associated value is an integer
407      */
408     public static final String KEY_WIDTH = "width";
409 
410     /**
411      * A key describing the height of the content in a video format.
412      * The associated value is an integer
413      */
414     public static final String KEY_HEIGHT = "height";
415 
416     /**
417      * A key describing the bottom-coordinate (y) of the crop rectangle.
418      * This is the bottom-most row included in the crop frame,
419      * where row indices start at 0.
420      * Additional information on the crop rectangle semantics can be found at
421      * {@link android.media.MediaCodec}.
422      */
423     public static final String KEY_CROP_BOTTOM = "crop-bottom";
424 
425     /**
426      * A key describing the left-coordinate (x) of the crop rectangle.
427      * This is the left-most column included in the crop frame,
428      * where column indices start at 0.
429      * Additional information on the crop rectangle semantics can be found at
430      * {@link android.media.MediaCodec}.
431      */
432     public static final String KEY_CROP_LEFT = "crop-left";
433 
434     /**
435      * A key describing the right-coordinate (x) of the crop rectangle.
436      * This is the right-most column included in the crop frame,
437      * where column indices start at 0.
438      * Additional information on the crop rectangle semantics can be found at
439      * {@link android.media.MediaCodec}.
440      */
441     public static final String KEY_CROP_RIGHT = "crop-right";
442 
443     /**
444      * A key describing the top-coordinate (y) of the crop rectangle.
445      * This is the top-most row included in the crop frame,
446      * where row indices start at 0.
447      * Additional information on the crop rectangle semantics can be found at
448      * {@link android.media.MediaCodec}.
449      */
450     public static final String KEY_CROP_TOP = "crop-top";
451 
452     /**
453      * A key describing the maximum expected width of the content in a video
454      * decoder format, in case there are resolution changes in the video content.
455      * The associated value is an integer
456      */
457     public static final String KEY_MAX_WIDTH = "max-width";
458 
459     /**
460      * A key describing the maximum expected height of the content in a video
461      * decoder format, in case there are resolution changes in the video content.
462      * The associated value is an integer
463      */
464     public static final String KEY_MAX_HEIGHT = "max-height";
465 
466     /** A key describing the maximum size in bytes of a buffer of data
467      * described by this MediaFormat.
468      * The associated value is an integer
469      */
470     public static final String KEY_MAX_INPUT_SIZE = "max-input-size";
471 
472     /**
473      * A key describing the maximum output buffer size in bytes when using
474      * large buffer mode containing multiple access units.
475      *
476      * When not-set - codec functions with one access-unit per frame.
477      * When set less than the size of two access-units - will make codec
478      * operate in single access-unit per output frame.
479      * When set to a value too big - The component or the framework will
480      * override this value to a reasonable max size not exceeding typical
481      * 10 seconds of data (device dependent) when set to a value larger than
482      * that. The value final value used will be returned in the output format.
483      *
484      * The associated value is an integer
485      *
486      * @see FEATURE_MultipleFrames
487      */
488     @FlaggedApi(FLAG_LARGE_AUDIO_FRAME)
489     public static final String KEY_BUFFER_BATCH_MAX_OUTPUT_SIZE = "buffer-batch-max-output-size";
490 
491     /**
492      * A key describing the threshold output size in bytes when using large buffer
493      * mode containing multiple access units.
494      *
495      * This is an optional parameter.
496      *
497      * If not set - the component can set this to a reasonable value.
498      * If set larger than max size, the components will
499      * clip this setting to maximum buffer batching output size.
500      *
501      * The component will return a partial output buffer if the output buffer reaches or
502      * surpass this limit.
503      *
504      * Threshold size should be always less or equal to KEY_MAX_BUFFER_BATCH_OUTPUT_SIZE.
505      * The final setting of this value as determined by the component will be returned
506      * in the output format
507      *
508      * The associated value is an integer
509      *
510      * @see FEATURE_MultipleFrames
511      */
512     @FlaggedApi(FLAG_LARGE_AUDIO_FRAME)
513     public static final String KEY_BUFFER_BATCH_THRESHOLD_OUTPUT_SIZE =
514             "buffer-batch-threshold-output-size";
515 
516     /**
517      * A key describing the pixel aspect ratio width.
518      * The associated value is an integer
519      */
520     public static final String KEY_PIXEL_ASPECT_RATIO_WIDTH = "sar-width";
521 
522     /**
523      * A key describing the pixel aspect ratio height.
524      * The associated value is an integer
525      */
526     public static final String KEY_PIXEL_ASPECT_RATIO_HEIGHT = "sar-height";
527 
528     /**
529      * A key describing the average bitrate in bits/sec.
530      * The associated value is an integer
531      */
532     public static final String KEY_BIT_RATE = "bitrate";
533 
534     /**
535      * A key describing the hardware AV sync id.
536      * The associated value is an integer
537      *
538      * See android.media.tv.tuner.Tuner#getAvSyncHwId.
539      */
540     public static final String KEY_HARDWARE_AV_SYNC_ID = "hw-av-sync-id";
541 
542     /**
543      * A key describing the max bitrate in bits/sec.
544      * This is usually over a one-second sliding window (e.g. over any window of one second).
545      * The associated value is an integer
546      * @hide
547      */
548     public static final String KEY_MAX_BIT_RATE = "max-bitrate";
549 
550     /**
551      * A key describing the color format of the content in a video format.
552      * Constants are declared in {@link android.media.MediaCodecInfo.CodecCapabilities}.
553      */
554     public static final String KEY_COLOR_FORMAT = "color-format";
555 
556     /**
557      * A key describing the frame rate of a video format in frames/sec.
558      * <p>
559      * The associated value is normally an integer when the value is used by the platform,
560      * but video codecs also accept float configuration values.
561      * Specifically, {@link MediaExtractor#getTrackFormat MediaExtractor} provides an integer
562      * value corresponding to the frame rate information of the track if specified and non-zero.
563      * Otherwise, this key is not present. {@link MediaCodec#configure MediaCodec} accepts both
564      * float and integer values.
565      * <p>
566      * This represents the desired operating frame rate if the
567      * {@link #KEY_OPERATING_RATE} is not present and {@link #KEY_PRIORITY} is {@code 0}
568      * (realtime). Otherwise, this is just informational.
569      * <p>
570      * For video encoders this value corresponds to the intended frame rate (the rate at which
571      * the application intends to send frames to the encoder, as calculated by the buffer
572      * timestamps, and not from the actual real-time rate that the frames are sent to
573      * the encoder). Encoders use this hint for rate control, specifically for the initial
574      * frames, as encoders are expected to support variable frame rate (for rate control) based
575      * on the actual {@link MediaCodec.BufferInfo#presentationTimeUs buffer timestamps} of
576      * subsequent frames.
577      * <p>
578      * This key is not used in the {@code MediaCodec}
579      * {@link MediaCodec#getInputFormat input}/{@link MediaCodec#getOutputFormat output} formats,
580      * nor by {@link MediaMuxer#addTrack MediaMuxer}.
581      */
582     public static final String KEY_FRAME_RATE = "frame-rate";
583 
584     /**
585      * A key describing the width (in pixels) of each tile of the content in a
586      * {@link #MIMETYPE_IMAGE_ANDROID_HEIC} / {@link #MIMETYPE_IMAGE_AVIF} track.
587      * The associated value is an integer.
588      *
589      * Refer to {@link #MIMETYPE_IMAGE_ANDROID_HEIC} / {@link #MIMETYPE_IMAGE_AVIF} on decoding
590      * instructions of such tracks.
591      *
592      * @see #KEY_TILE_HEIGHT
593      * @see #KEY_GRID_ROWS
594      * @see #KEY_GRID_COLUMNS
595      */
596     public static final String KEY_TILE_WIDTH = "tile-width";
597 
598     /**
599      * A key describing the height (in pixels) of each tile of the content in a
600      * {@link #MIMETYPE_IMAGE_ANDROID_HEIC} / {@link #MIMETYPE_IMAGE_AVIF} track.
601      * The associated value is an integer.
602      *
603      * Refer to {@link #MIMETYPE_IMAGE_ANDROID_HEIC} / {@link #MIMETYPE_IMAGE_AVIF} on decoding
604      * instructions of such tracks.
605      *
606      * @see #KEY_TILE_WIDTH
607      * @see #KEY_GRID_ROWS
608      * @see #KEY_GRID_COLUMNS
609      */
610     public static final String KEY_TILE_HEIGHT = "tile-height";
611 
612     /**
613      * A key describing the number of grid rows in the content in a
614      * {@link #MIMETYPE_IMAGE_ANDROID_HEIC} / {@link #MIMETYPE_IMAGE_AVIF} track.
615      * The associated value is an integer.
616      *
617      * Refer to {@link #MIMETYPE_IMAGE_ANDROID_HEIC} / {@link #MIMETYPE_IMAGE_AVIF} on decoding
618      * instructions of such tracks.
619      *
620      * @see #KEY_TILE_WIDTH
621      * @see #KEY_TILE_HEIGHT
622      * @see #KEY_GRID_COLUMNS
623      */
624     public static final String KEY_GRID_ROWS = "grid-rows";
625 
626     /**
627      * A key describing the number of grid columns in the content in a
628      * {@link #MIMETYPE_IMAGE_ANDROID_HEIC} / {@link #MIMETYPE_IMAGE_AVIF} track.
629      * The associated value is an integer.
630      *
631      * Refer to {@link #MIMETYPE_IMAGE_ANDROID_HEIC} / {@link #MIMETYPE_IMAGE_AVIF} on decoding
632      * instructions of such tracks.
633      *
634      * @see #KEY_TILE_WIDTH
635      * @see #KEY_TILE_HEIGHT
636      * @see #KEY_GRID_ROWS
637      */
638     public static final String KEY_GRID_COLUMNS = "grid-cols";
639 
640     /**
641      * A key describing the raw audio sample encoding/format.
642      *
643      * <p>The associated value is an integer, using one of the
644      * {@link AudioFormat}.ENCODING_PCM_ values.</p>
645      *
646      * <p>This is an optional key for audio decoders and encoders specifying the
647      * desired raw audio sample format during {@link MediaCodec#configure
648      * MediaCodec.configure(&hellip;)} call. Use {@link MediaCodec#getInputFormat
649      * MediaCodec.getInput}/{@link MediaCodec#getOutputFormat OutputFormat(&hellip;)}
650      * to confirm the actual format. For the PCM decoder this key specifies both
651      * input and output sample encodings.</p>
652      *
653      * <p>This key is also used by {@link MediaExtractor} to specify the sample
654      * format of audio data, if it is specified.</p>
655      *
656      * <p>If this key is missing, the raw audio sample format is signed 16-bit short.</p>
657      */
658     public static final String KEY_PCM_ENCODING = "pcm-encoding";
659 
660     /**
661      * A key describing the capture rate of a video format in frames/sec.
662      * <p>
663      * When capture rate is different than the frame rate, it means that the
664      * video is acquired at a different rate than the playback, which produces
665      * slow motion or timelapse effect during playback. Application can use the
666      * value of this key to tell the relative speed ratio between capture and
667      * playback rates when the video was recorded.
668      * </p>
669      * <p>
670      * The associated value is an integer or a float.
671      * </p>
672      */
673     public static final String KEY_CAPTURE_RATE = "capture-rate";
674 
675     /**
676      * A key for retrieving the slow-motion marker information associated with a video track.
677      * <p>
678      * The associated value is a ByteBuffer in {@link ByteOrder#BIG_ENDIAN}
679      * (networking order) of the following format:
680      * </p>
681      * <pre class="prettyprint">
682      *     float(32) playbackRate;
683      *     unsigned int(32) numMarkers;
684      *     for (i = 0;i < numMarkers; i++) {
685      *         int(64) timestampUs;
686      *         float(32) speedRatio;
687      *     }</pre>
688      * The meaning of each field is as follows:
689      * <table border="1" width="90%" align="center" cellpadding="5">
690      *     <tbody>
691      *     <tr>
692      *         <td>playbackRate</td>
693      *         <td>The frame rate at which the playback should happen (or the flattened
694      *             clip should be).</td>
695      *     </tr>
696      *     <tr>
697      *         <td>numMarkers</td>
698      *         <td>The number of slow-motion markers that follows.</td>
699      *     </tr>
700      *     <tr>
701      *         <td>timestampUs</td>
702      *         <td>The starting point of a new segment.</td>
703      *     </tr>
704      *     <tr>
705      *         <td>speedRatio</td>
706      *         <td>The playback speed for that segment. The playback speed is a floating
707      *             point number, indicating how fast the time progresses relative to that
708      *             written in the container. (Eg. 4.0 means time goes 4x as fast, which
709      *             makes 30fps become 120fps.)</td>
710      *     </tr>
711      * </table>
712      * <p>
713      * The following constraints apply to the timestampUs of the markers:
714      * </p>
715      * <li>The timestampUs shall be monotonically increasing.</li>
716      * <li>The timestampUs shall fall within the time span of the video track.</li>
717      * <li>The first timestampUs should match that of the first video sample.</li>
718      */
719     public static final String KEY_SLOW_MOTION_MARKERS = "slow-motion-markers";
720 
721     /**
722      * A key describing the frequency of key frames expressed in seconds between key frames.
723      * <p>
724      * This key is used by video encoders.
725      * A negative value means no key frames are requested after the first frame.
726      * A zero value means a stream containing all key frames is requested.
727      * <p class=note>
728      * Most video encoders will convert this value of the number of non-key-frames between
729      * key-frames, using the {@linkplain #KEY_FRAME_RATE frame rate} information; therefore,
730      * if the actual frame rate differs (e.g. input frames are dropped or the frame rate
731      * changes), the <strong>time interval</strong> between key frames will not be the
732      * configured value.
733      * <p>
734      * The associated value is an integer (or float since
735      * {@link android.os.Build.VERSION_CODES#N_MR1}).
736      */
737     public static final String KEY_I_FRAME_INTERVAL = "i-frame-interval";
738 
739     /**
740     * An optional key describing the period of intra refresh in frames. This is an
741     * optional parameter that applies only to video encoders. If encoder supports it
742     * ({@link MediaCodecInfo.CodecCapabilities#FEATURE_IntraRefresh}), the whole
743     * frame is completely refreshed after the specified period. Also for each frame,
744     * a fix subset of macroblocks must be intra coded which leads to more constant bitrate
745     * than inserting a key frame. This key is recommended for video streaming applications
746     * as it provides low-delay and good error-resilience. This key is ignored if the
747     * video encoder does not support the intra refresh feature. Use the output format to
748     * verify that this feature was enabled.
749     * The associated value is an integer.
750     */
751     public static final String KEY_INTRA_REFRESH_PERIOD = "intra-refresh-period";
752 
753     /**
754      * An optional key describing whether encoders prepend headers to sync frames (e.g.
755      * SPS and PPS to IDR frames for H.264). This is an optional parameter that applies only
756      * to video encoders. A video encoder may not support this feature; the component will fail
757      * to configure in that case. For other components, this key is ignored.
758      *
759      * The value is an integer, with 1 indicating to prepend headers to every sync frames,
760      * or 0 otherwise. The default value is 0.
761      */
762     public static final String KEY_PREPEND_HEADER_TO_SYNC_FRAMES = "prepend-sps-pps-to-idr-frames";
763 
764     /**
765      * A key describing the temporal layering schema.  This is an optional parameter
766      * that applies only to video encoders.  Use {@link MediaCodec#getOutputFormat}
767      * after {@link MediaCodec#configure configure} to query if the encoder supports
768      * the desired schema. Supported values are {@code webrtc.vp8.N-layer},
769      * {@code android.generic.N}, {@code android.generic.N+M} and {@code none}, where
770      * {@code N} denotes the total number of non-bidirectional layers (which must be at least 1)
771      * and {@code M} denotes the total number of bidirectional layers (which must be non-negative).
772      * <p class=note>{@code android.generic.*} schemas have been added in {@link
773      * android.os.Build.VERSION_CODES#N_MR1}.
774      * <p>
775      * The encoder may support fewer temporal layers, in which case the output format
776      * will contain the configured schema. If the encoder does not support temporal
777      * layering, the output format will not have an entry with this key.
778      * The associated value is a string.
779      */
780     public static final String KEY_TEMPORAL_LAYERING = "ts-schema";
781 
782     /**
783      * A key describing the stride of the video bytebuffer layout.
784      * Stride (or row increment) is the difference between the index of a pixel
785      * and that of the pixel directly underneath. For YUV 420 formats, the
786      * stride corresponds to the Y plane; the stride of the U and V planes can
787      * be calculated based on the color format, though it is generally undefined
788      * and depends on the device and release.
789      * The associated value is an integer, representing number of bytes.
790      */
791     public static final String KEY_STRIDE = "stride";
792 
793     /**
794      * A key describing the plane height of a multi-planar (YUV) video bytebuffer layout.
795      * Slice height (or plane height/vertical stride) is the number of rows that must be skipped
796      * to get from the top of the Y plane to the top of the U plane in the bytebuffer. In essence
797      * the offset of the U plane is sliceHeight * stride. The height of the U/V planes
798      * can be calculated based on the color format, though it is generally undefined
799      * and depends on the device and release.
800      * The associated value is an integer, representing number of rows.
801      */
802     public static final String KEY_SLICE_HEIGHT = "slice-height";
803 
804     /**
805      * Applies only when configuring a video encoder in "surface-input" mode.
806      * The associated value is a long and gives the time in microseconds
807      * after which the frame previously submitted to the encoder will be
808      * repeated (once) if no new frame became available since.
809      */
810     public static final String KEY_REPEAT_PREVIOUS_FRAME_AFTER
811         = "repeat-previous-frame-after";
812 
813     /**
814      * Instruct the video encoder in "surface-input" mode to drop excessive
815      * frames from the source, so that the input frame rate to the encoder
816      * does not exceed the specified fps.
817      *
818      * The associated value is a float, representing the max frame rate to
819      * feed the encoder at.
820      *
821      */
822     public static final String KEY_MAX_FPS_TO_ENCODER
823         = "max-fps-to-encoder";
824 
825     /**
826      * Instruct the video encoder in "surface-input" mode to limit the gap of
827      * timestamp between any two adjacent frames fed to the encoder to the
828      * specified amount (in micro-second).
829      *
830      * The associated value is a long int. When positive, it represents the max
831      * timestamp gap between two adjacent frames fed to the encoder. When negative,
832      * the absolute value represents a fixed timestamp gap between any two adjacent
833      * frames fed to the encoder. Note that this will also apply even when the
834      * original timestamp goes backward in time. Under normal conditions, such frames
835      * would be dropped and not sent to the encoder.
836      *
837      * The output timestamp will be restored to the original timestamp and will
838      * not be affected.
839      *
840      * This is used in some special scenarios where input frames arrive sparingly
841      * but it's undesirable to allocate more bits to any single frame, or when it's
842      * important to ensure all frames are captured (rather than captured in the
843      * correct order).
844      *
845      */
846     public static final String KEY_MAX_PTS_GAP_TO_ENCODER
847         = "max-pts-gap-to-encoder";
848 
849     /**
850      * If specified when configuring a video encoder that's in "surface-input"
851      * mode, it will instruct the encoder to put the surface source in suspended
852      * state when it's connected. No video frames will be accepted until a resume
853      * operation (see {@link MediaCodec#PARAMETER_KEY_SUSPEND}), optionally with
854      * timestamp specified via {@link MediaCodec#PARAMETER_KEY_SUSPEND_TIME}, is
855      * received.
856      *
857      * The value is an integer, with 1 indicating to create with the surface
858      * source suspended, or 0 otherwise. The default value is 0.
859      *
860      * If this key is not set or set to 0, the surface source will accept buffers
861      * as soon as it's connected to the encoder (although they may not be encoded
862      * immediately). This key can be used when the client wants to prepare the
863      * encoder session in advance, but do not want to accept buffers immediately.
864      */
865     public static final String KEY_CREATE_INPUT_SURFACE_SUSPENDED
866         = "create-input-buffers-suspended";
867 
868     /**
869      * If specified when configuring a video decoder rendering to a surface,
870      * causes the decoder to output "blank", i.e. black frames to the surface
871      * when stopped to clear out any previously displayed contents.
872      * The associated value is an integer of value 1.
873      */
874     public static final String KEY_PUSH_BLANK_BUFFERS_ON_STOP
875         = "push-blank-buffers-on-shutdown";
876 
877     /**
878      * A key describing the duration (in microseconds) of the content.
879      * The associated value is a long.
880      */
881     public static final String KEY_DURATION = "durationUs";
882 
883     /**
884      * A key mapping to a value of 1 if the content is AAC audio and
885      * audio frames are prefixed with an ADTS header.
886      * The associated value is an integer (0 or 1).
887      * This key is only supported when _decoding_ content, it cannot
888      * be used to configure an encoder to emit ADTS output.
889      */
890     public static final String KEY_IS_ADTS = "is-adts";
891 
892     /**
893      * A key describing the channel composition of audio content. This mask
894      * is composed of bits drawn from channel mask definitions in {@link android.media.AudioFormat}.
895      * The associated value is an integer.
896      */
897     public static final String KEY_CHANNEL_MASK = "channel-mask";
898 
899     /**
900      * A key describing the maximum number of channels that can be output by an audio decoder.
901      * By default, the decoder will output the same number of channels as present in the encoded
902      * stream, if supported. Set this value to limit the number of output channels, and use
903      * the downmix information in the stream, if available.
904      * <p>Values larger than the number of channels in the content to decode behave like the number
905      * of channels in the content (if applicable), for instance passing 99 for a 5.1 audio stream
906      * behaves like passing 6.
907      * <p>This key is only used during decoding.
908      */
909     public static final String KEY_MAX_OUTPUT_CHANNEL_COUNT =
910             "max-output-channel-count";
911 
912     /**
913      * A key describing the number of frames to trim from the start of the decoded audio stream.
914      * The associated value is an integer.
915      */
916     public static final String KEY_ENCODER_DELAY = "encoder-delay";
917 
918     /**
919      * A key describing the number of frames to trim from the end of the decoded audio stream.
920      * The associated value is an integer.
921      */
922     public static final String KEY_ENCODER_PADDING = "encoder-padding";
923 
924     /**
925      * A key describing the AAC profile to be used (AAC audio formats only).
926      * Constants are declared in {@link android.media.MediaCodecInfo.CodecProfileLevel}.
927      */
928     public static final String KEY_AAC_PROFILE = "aac-profile";
929 
930     /**
931      * A key describing the AAC SBR mode to be used (AAC audio formats only).
932      * The associated value is an integer and can be set to following values:
933      * <ul>
934      * <li>0 - no SBR should be applied</li>
935      * <li>1 - single rate SBR</li>
936      * <li>2 - double rate SBR</li>
937      * </ul>
938      * Note: If this key is not defined the default SRB mode for the desired AAC profile will
939      * be used.
940      * <p>This key is only used during encoding.
941      */
942     public static final String KEY_AAC_SBR_MODE = "aac-sbr-mode";
943 
944     /**
945      * A key describing the maximum number of channels that can be output by the AAC decoder.
946      * By default, the decoder will output the same number of channels as present in the encoded
947      * stream, if supported. Set this value to limit the number of output channels, and use
948      * the downmix information in the stream, if available.
949      * <p>Values larger than the number of channels in the content to decode behave just
950      * like the actual channel count of the content (e.g. passing 99 for the decoding of 5.1 content
951      * will behave like using 6).
952      * <p>This key is only used during decoding.
953      * @deprecated Use the non-AAC-specific key {@link #KEY_MAX_OUTPUT_CHANNEL_COUNT} instead
954      */
955     public static final String KEY_AAC_MAX_OUTPUT_CHANNEL_COUNT = "aac-max-output-channel_count";
956 
957     /**
958      * A key describing the Target Reference Level (Target Loudness).
959      * <p>For normalizing loudness across program items, a gain is applied to the audio output so
960      * that the output loudness matches the Target Reference Level. The gain is derived as the
961      * difference between the Target Reference Level and the Program Reference Level (Program
962      * Loudness). The latter can be given in the bitstream and indicates the actual loudness value
963      * of the program item.</p>
964      * <p>The Target Reference Level controls loudness normalization for both MPEG-4 DRC and
965      * MPEG-D DRC.
966      * <p>The value is given as an integer value between
967      * 40 and 127, and is calculated as -4 * Target Reference Level in LKFS.
968      * Therefore, it represents the range of -10 to -31.75 LKFS.
969      * <p>For MPEG-4 DRC, a value of -1 switches off loudness normalization and DRC processing.</p>
970      * <p>For MPEG-D DRC, a value of -1 switches off loudness normalization only. For DRC processing
971      * options of MPEG-D DRC, see {@link #KEY_AAC_DRC_EFFECT_TYPE}</p>
972      * <p>The default value on mobile devices is 64 (-16 LKFS).
973      * <p>This key is only used during decoding.
974      */
975     public static final String KEY_AAC_DRC_TARGET_REFERENCE_LEVEL = "aac-target-ref-level";
976 
977     /**
978      * A key describing for selecting the DRC effect type for MPEG-D DRC.
979      * The supported values are defined in ISO/IEC 23003-4:2015 and are described as follows:
980      * <table>
981      * <tr><th>Value</th><th>Effect</th></tr>
982      * <tr><th>-1</th><th>Off</th></tr>
983      * <tr><th>0</th><th>None</th></tr>
984      * <tr><th>1</th><th>Late night</th></tr>
985      * <tr><th>2</th><th>Noisy environment</th></tr>
986      * <tr><th>3</th><th>Limited playback range</th></tr>
987      * <tr><th>4</th><th>Low playback level</th></tr>
988      * <tr><th>5</th><th>Dialog enhancement</th></tr>
989      * <tr><th>6</th><th>General compression</th></tr>
990      * </table>
991      * <p>The value -1 (Off) disables DRC processing, while loudness normalization may still be
992      * active and dependent on {@link #KEY_AAC_DRC_TARGET_REFERENCE_LEVEL}.<br>
993      * The value 0 (None) automatically enables DRC processing if necessary to prevent signal
994      * clipping<br>
995      * The value 6 (General compression) can be used for enabling MPEG-D DRC without particular
996      * DRC effect type request.<br>
997      * The default DRC effect type is 3 ("Limited playback range") on mobile devices.
998      * <p>This key is only used during decoding.
999      */
1000     public static final String KEY_AAC_DRC_EFFECT_TYPE = "aac-drc-effect-type";
1001 
1002     /**
1003      * A key describing the target reference level that was assumed at the encoder for
1004      * calculation of attenuation gains for clipping prevention.
1005      * <p>If it is known, this information can be provided as an integer value between
1006      * 0 and 127, which is calculated as -4 * Encoded Target Level in LKFS.
1007      * If the Encoded Target Level is unknown, the value can be set to -1.
1008      * <p>The default value is -1 (unknown).
1009      * <p>The value is ignored when heavy compression (see {@link #KEY_AAC_DRC_HEAVY_COMPRESSION})
1010      * or MPEG-D DRC is used.
1011      * <p>This key is only used during decoding.
1012      */
1013     public static final String KEY_AAC_ENCODED_TARGET_LEVEL = "aac-encoded-target-level";
1014 
1015     /**
1016      * A key describing the boost factor allowing to adapt the dynamics of the output to the
1017      * actual listening requirements. This relies on DRC gain sequences that can be transmitted in
1018      * the encoded bitstream to be able to reduce the dynamics of the output signal upon request.
1019      * This factor enables the user to select how much of the gains are applied.
1020      * <p>Positive gains (boost) and negative gains (attenuation, see
1021      * {@link #KEY_AAC_DRC_ATTENUATION_FACTOR}) can be controlled separately for a better match
1022      * to different use-cases.
1023      * <p>Typically, attenuation gains are sent for loud signal segments, and boost gains are sent
1024      * for soft signal segments. If the output is listened to in a noisy environment, for example,
1025      * the boost factor is used to enable the positive gains, i.e. to amplify soft signal segments
1026      * beyond the noise floor. But for listening late at night, the attenuation
1027      * factor is used to enable the negative gains, to prevent loud signal from surprising
1028      * the listener. In applications which generally need a low dynamic range, both the boost factor
1029      * and the attenuation factor are used in order to enable all DRC gains.
1030      * <p>In order to prevent clipping, it is also recommended to apply the attenuation gains
1031      * in case of a downmix and/or loudness normalization to high target reference levels.
1032      * <p>Both the boost and the attenuation factor parameters are given as integer values
1033      * between 0 and 127, representing the range of the factor of 0 (i.e. don't apply)
1034      * to 1 (i.e. fully apply boost/attenuation gains respectively).
1035      * <p>The default value is 127 (fully apply boost DRC gains).
1036      * <p>This key is only used during decoding.
1037      */
1038     public static final String KEY_AAC_DRC_BOOST_FACTOR = "aac-drc-boost-level";
1039 
1040     /**
1041      * A key describing the attenuation factor allowing to adapt the dynamics of the output to the
1042      * actual listening requirements.
1043      * See {@link #KEY_AAC_DRC_BOOST_FACTOR} for a description of the role of this attenuation
1044      * factor and the value range.
1045      * <p>The default value is 127 (fully apply attenuation DRC gains).
1046      * <p>This key is only used during decoding.
1047      */
1048     public static final String KEY_AAC_DRC_ATTENUATION_FACTOR = "aac-drc-cut-level";
1049 
1050     /**
1051      * A key describing the selection of the heavy compression profile for MPEG-4 DRC.
1052      * <p>Two separate DRC gain sequences can be transmitted in one bitstream: light compression
1053      * and heavy compression. When selecting the application of the heavy compression, one of
1054      * the sequences is selected:
1055      * <ul>
1056      * <li>0 enables light compression,</li>
1057      * <li>1 enables heavy compression instead.
1058      * </ul>
1059      * Note that heavy compression doesn't offer the features of scaling of DRC gains
1060      * (see {@link #KEY_AAC_DRC_BOOST_FACTOR} and {@link #KEY_AAC_DRC_ATTENUATION_FACTOR} for the
1061      * boost and attenuation factors), and frequency-selective (multiband) DRC.
1062      * Light compression usually contains clipping prevention for stereo downmixing while heavy
1063      * compression, if additionally provided in the bitstream, is usually stronger, and contains
1064      * clipping prevention for stereo and mono downmixing.
1065      * <p>The default is 1 (heavy compression).
1066      * <p>This key is only used during decoding.
1067      */
1068     public static final String KEY_AAC_DRC_HEAVY_COMPRESSION = "aac-drc-heavy-compression";
1069 
1070     /**
1071      * A key to retrieve the output loudness of a decoded bitstream.
1072      * <p>If loudness normalization is active, the value corresponds to the Target Reference Level
1073      * (see {@link #KEY_AAC_DRC_TARGET_REFERENCE_LEVEL}).<br>
1074      * If loudness normalization is not active, the value corresponds to the loudness metadata
1075      * given in the bitstream.
1076      * <p>The value is retrieved with getInteger() and is given as an integer value between 0 and
1077      * 231. It is calculated as -4 * Output Loudness in LKFS. Therefore, it represents the range of
1078      * 0 to -57.75 LKFS.
1079      * <p>A value of -1 indicates that no loudness metadata is present in the bitstream.
1080      * <p>Loudness metadata can originate from MPEG-4 DRC or MPEG-D DRC.
1081      * <p>This key is only used during decoding.
1082      */
1083     public static final String KEY_AAC_DRC_OUTPUT_LOUDNESS = "aac-drc-output-loudness";
1084 
1085     /**
1086      * A key describing the album mode for MPEG-D DRC as defined in ISO/IEC 23003-4.
1087      * <p>The associated value is an integer and can be set to following values:
1088      * <table>
1089      * <tr><th>Value</th><th>Album Mode</th></tr>
1090      * <tr><th>0</th><th>disabled</th></tr>
1091      * <tr><th>1</th><th>enabled</th></tr>
1092      * </table>
1093      * <p>Disabled album mode leads to application of gain sequences for fading in and out, if
1094      * provided in the bitstream. Enabled album mode makes use of dedicated album loudness
1095      * information, if provided in the bitstream.
1096      * <p>The default value is 0 (album mode disabled).
1097      * <p>This key is only used during decoding.
1098      */
1099     public static final String KEY_AAC_DRC_ALBUM_MODE = "aac-drc-album-mode";
1100 
1101     /**
1102      * A key describing the FLAC compression level to be used (FLAC audio format only).
1103      * The associated value is an integer ranging from 0 (fastest, least compression)
1104      * to 8 (slowest, most compression).
1105      */
1106     public static final String KEY_FLAC_COMPRESSION_LEVEL = "flac-compression-level";
1107 
1108     /**
1109      * A key describing the MPEG-H stream profile-level indication.
1110      *
1111      * See ISO_IEC_23008-3;2019 MHADecoderConfigurationRecord mpegh3daProfileLevelIndication.
1112      */
1113     public static final String KEY_MPEGH_PROFILE_LEVEL_INDICATION =
1114             "mpegh-profile-level-indication";
1115 
1116     /**
1117      * A key describing the MPEG-H stream compatible sets.
1118      *
1119      * See FDAmd_2 of ISO_IEC_23008-3;2019 MHAProfileAndLevelCompatibilitySetBox.
1120      */
1121     public static final String KEY_MPEGH_COMPATIBLE_SETS = "mpegh-compatible-sets";
1122 
1123     /**
1124      * A key describing the MPEG-H stream reference channel layout.
1125      *
1126      * See ISO_IEC_23008-3;2019 MHADecoderConfigurationRecord referenceChannelLayout
1127      * and ISO_IEC_23001‐8 ChannelConfiguration value.
1128      */
1129     public static final String KEY_MPEGH_REFERENCE_CHANNEL_LAYOUT =
1130             "mpegh-reference-channel-layout";
1131 
1132     /**
1133      * A key describing the encoding complexity.
1134      * The associated value is an integer.  These values are device and codec specific,
1135      * but lower values generally result in faster and/or less power-hungry encoding.
1136      *
1137      * @see MediaCodecInfo.EncoderCapabilities#getComplexityRange()
1138      */
1139     public static final String KEY_COMPLEXITY = "complexity";
1140 
1141     /**
1142      * A key describing the desired encoding quality.
1143      * The associated value is an integer.  This key is only supported for encoders
1144      * that are configured in constant-quality mode.  These values are device and
1145      * codec specific, but lower values generally result in more efficient
1146      * (smaller-sized) encoding.
1147      *
1148      * @see MediaCodecInfo.EncoderCapabilities#getQualityRange()
1149      */
1150     public static final String KEY_QUALITY = "quality";
1151 
1152     /**
1153      * A key describing the desired codec priority.
1154      * <p>
1155      * The associated value is an integer. Higher value means lower priority.
1156      * <p>
1157      * Currently, only two levels are supported:<br>
1158      * 0: realtime priority - meaning that the codec shall support the given
1159      *    performance configuration (e.g. framerate) at realtime. This should
1160      *    only be used by media playback, capture, and possibly by realtime
1161      *    communication scenarios if best effort performance is not suitable.<br>
1162      * 1: non-realtime priority (best effort).
1163      * <p>
1164      * This is a hint used at codec configuration and resource planning - to understand
1165      * the realtime requirements of the application; however, due to the nature of
1166      * media components, performance is not guaranteed.
1167      *
1168      */
1169     public static final String KEY_PRIORITY = "priority";
1170 
1171     /**
1172      * A key describing the desired operating frame rate for video or sample rate for audio
1173      * that the codec will need to operate at.
1174      * <p>
1175      * The associated value is an integer or a float representing frames-per-second or
1176      * samples-per-second
1177      * <p>
1178      * This is used for cases like high-speed/slow-motion video capture, where the video encoder
1179      * format contains the target playback rate (e.g. 30fps), but the component must be able to
1180      * handle the high operating capture rate (e.g. 240fps).
1181      * <p>
1182      * This rate will be used by codec for resource planning and setting the operating points.
1183      *
1184      */
1185     public static final String KEY_OPERATING_RATE = "operating-rate";
1186 
1187     /**
1188      * A key describing the desired profile to be used by an encoder.
1189      * <p>
1190      * The associated value is an integer.
1191      * Constants are declared in {@link MediaCodecInfo.CodecProfileLevel}.
1192      * This key is used as a hint, and is only supported for codecs
1193      * that specify a profile. When configuring profile, encoder configuration
1194      * may fail if other parameters are not compatible with the desired
1195      * profile or if the desired profile is not supported, but it may also
1196      * fail silently (where the encoder ends up using a different, compatible profile.)
1197      * <p>
1198      * It is recommended that the profile is set for all encoders. For more information, see
1199      * the <i>Encoder Profiles</i> section of the {@link MediaCodec} API reference.
1200      * <p class="note">
1201      * <strong>Note:</strong> Codecs are free to use all the available
1202      * coding tools at the specified profile, but may ultimately choose to not do so.
1203      * <p class="note">
1204      * <strong>Note:</strong> When configuring video encoders, profile (if set) must be
1205      * set together with {@link #KEY_LEVEL level}.
1206      *
1207      * @see MediaCodecInfo.CodecCapabilities#profileLevels
1208      */
1209     public static final String KEY_PROFILE = "profile";
1210 
1211     /**
1212      * A key describing the desired profile to be used by an encoder.
1213      * <p>
1214      * The associated value is an integer.
1215      * Constants are declared in {@link MediaCodecInfo.CodecProfileLevel}.
1216      * This key is used as a further hint when specifying a desired profile,
1217      * and is only supported for codecs that specify a level.
1218      * <p>
1219      * This key is ignored if the {@link #KEY_PROFILE profile} is not specified.
1220      * Otherwise, the value should be a level compatible with the configured encoding
1221      * parameters.
1222      * <p class="note">
1223      * <strong>Note:</strong> This key cannot be used to constrain the encoder's
1224      * output to a maximum encoding level. Encoders are free to target a different
1225      * level if the configured encoding parameters dictate it. Nevertheless,
1226      * encoders shall use (and encode) a level sufficient to decode the generated
1227      * bitstream, though they may exceed the (Video) Buffering Verifier limits for
1228      * that encoded level.
1229      *
1230      * @see MediaCodecInfo.CodecCapabilities#profileLevels
1231      */
1232     public static final String KEY_LEVEL = "level";
1233 
1234     /**
1235     * An optional key describing the desired encoder latency in frames. This is an optional
1236     * parameter that applies only to video encoders. If encoder supports it, it should ouput
1237     * at least one output frame after being queued the specified number of frames. This key
1238     * is ignored if the video encoder does not support the latency feature. Use the output
1239     * format to verify that this feature was enabled and the actual value used by the encoder.
1240     * <p>
1241     * If the key is not specified, the default latency will be implenmentation specific.
1242     * The associated value is an integer.
1243     */
1244     public static final String KEY_LATENCY = "latency";
1245 
1246     /**
1247      * An optional key describing the maximum number of non-display-order coded frames.
1248      * This is an optional parameter that applies only to video encoders. Application should
1249      * check the value for this key in the output format to see if codec will produce
1250      * non-display-order coded frames. If encoder supports it, the output frames' order will be
1251      * different from the display order and each frame's display order could be retrived from
1252      * {@link MediaCodec.BufferInfo#presentationTimeUs}. Before API level 27, application may
1253      * receive non-display-order coded frames even though the application did not request it.
1254      * Note: Application should not rearrange the frames to display order before feeding them
1255      * to {@link MediaMuxer#writeSampleData}.
1256      * <p>
1257      * The default value is 0.
1258      */
1259     public static final String KEY_OUTPUT_REORDER_DEPTH = "output-reorder-depth";
1260 
1261     /**
1262      * A key describing the desired clockwise rotation on an output surface.
1263      * This key is only used when the codec is configured using an output surface.
1264      * The associated value is an integer, representing degrees. Supported values
1265      * are 0, 90, 180 or 270. This is an optional field; if not specified, rotation
1266      * defaults to 0.
1267      *
1268      * @see MediaCodecInfo.CodecCapabilities#profileLevels
1269      */
1270     public static final String KEY_ROTATION = "rotation-degrees";
1271 
1272     /**
1273      * A key describing the desired bitrate mode to be used by an encoder.
1274      * Constants are declared in {@link MediaCodecInfo.EncoderCapabilities}.
1275      *
1276      * @see MediaCodecInfo.EncoderCapabilities#isBitrateModeSupported(int)
1277      */
1278     public static final String KEY_BITRATE_MODE = "bitrate-mode";
1279 
1280     /**
1281      * A key describing the maximum Quantization Parameter allowed for encoding video.
1282      * This key applies to all three video picture types (I, P, and B).
1283      * The value is used directly for picture type I; a per-mime formula is used
1284      * to calculate the value for the remaining picture types.
1285      *
1286      * This calculation can be avoided by directly specifying values for each picture type
1287      * using the type-specific keys {@link #KEY_VIDEO_QP_I_MAX}, {@link #KEY_VIDEO_QP_P_MAX},
1288      * and {@link #KEY_VIDEO_QP_B_MAX}.
1289      *
1290      * The associated value is an integer.
1291      */
1292     public static final String KEY_VIDEO_QP_MAX = "video-qp-max";
1293 
1294     /**
1295      * A key describing the minimum Quantization Parameter allowed for encoding video.
1296      * This key applies to all three video frame types (I, P, and B).
1297      * The value is used directly for picture type I; a per-mime formula is used
1298      * to calculate the value for the remaining picture types.
1299      *
1300      * This calculation can be avoided by directly specifying values for each picture type
1301      * using the type-specific keys {@link #KEY_VIDEO_QP_I_MIN}, {@link #KEY_VIDEO_QP_P_MIN},
1302      * and {@link #KEY_VIDEO_QP_B_MIN}.
1303      *
1304      * The associated value is an integer.
1305      */
1306     public static final String KEY_VIDEO_QP_MIN = "video-qp-min";
1307 
1308     /**
1309      * A key describing the maximum Quantization Parameter allowed for encoding video.
1310      * This value applies to video I-frames.
1311      *
1312      * The associated value is an integer.
1313      */
1314     public static final String KEY_VIDEO_QP_I_MAX = "video-qp-i-max";
1315 
1316     /**
1317      * A key describing the minimum Quantization Parameter allowed for encoding video.
1318      * This value applies to video I-frames.
1319      *
1320      * The associated value is an integer.
1321      */
1322     public static final String KEY_VIDEO_QP_I_MIN = "video-qp-i-min";
1323 
1324     /**
1325      * A key describing the maximum Quantization Parameter allowed for encoding video.
1326      * This value applies to video P-frames.
1327      *
1328      * The associated value is an integer.
1329      */
1330     public static final String KEY_VIDEO_QP_P_MAX = "video-qp-p-max";
1331 
1332     /**
1333      * A key describing the minimum Quantization Parameter allowed for encoding video.
1334      * This value applies to video P-frames.
1335      *
1336      * The associated value is an integer.
1337      */
1338     public static final String KEY_VIDEO_QP_P_MIN = "video-qp-p-min";
1339 
1340     /**
1341      * A key describing the maximum Quantization Parameter allowed for encoding video.
1342      * This value applies to video B-frames.
1343      *
1344      * The associated value is an integer.
1345      */
1346     public static final String KEY_VIDEO_QP_B_MAX = "video-qp-b-max";
1347 
1348     /**
1349      * A key describing the minimum Quantization Parameter allowed for encoding video.
1350      * This value applies to video B-frames.
1351      *
1352      * The associated value is an integer.
1353      */
1354     public static final String KEY_VIDEO_QP_B_MIN = "video-qp-b-min";
1355 
1356     /**
1357      * A key describing the level of encoding statistics information emitted from video encoder.
1358      *
1359      * The associated value is an integer.
1360      */
1361     public static final String KEY_VIDEO_ENCODING_STATISTICS_LEVEL =
1362             "video-encoding-statistics-level";
1363 
1364     /**
1365      * Encoding Statistics Level None.
1366      * Encoder generates no information about Encoding statistics.
1367      */
1368     public static final int VIDEO_ENCODING_STATISTICS_LEVEL_NONE = 0;
1369 
1370     /**
1371      * Encoding Statistics Level 1.
1372      * Encoder generates {@link MediaFormat#KEY_PICTURE_TYPE} and
1373      * {@link MediaFormat#KEY_VIDEO_QP_AVERAGE} for each frame.
1374      */
1375     public static final int VIDEO_ENCODING_STATISTICS_LEVEL_1 = 1;
1376 
1377     /** @hide */
1378     @IntDef({
1379         VIDEO_ENCODING_STATISTICS_LEVEL_NONE,
1380         VIDEO_ENCODING_STATISTICS_LEVEL_1,
1381     })
1382     @Retention(RetentionPolicy.SOURCE)
1383     public @interface VideoEncodingStatisticsLevel {}
1384 
1385     /**
1386      * A key describing the per-frame average block QP (Quantization Parameter).
1387      * This is a part of a video 'Encoding Statistics' export feature.
1388      * This value is emitted from video encoder for a video frame.
1389      * The average value is rounded to the nearest integer value.
1390      *
1391      * The associated value is an integer.
1392      */
1393     public static final String KEY_VIDEO_QP_AVERAGE = "video-qp-average";
1394 
1395     /**
1396      * A key describing the picture type of the encoded frame.
1397      * This is a part of a video 'Encoding Statistics' export feature.
1398      * This value is emitted from video encoder for a video frame.
1399      *
1400      * The associated value is an integer.
1401      */
1402     public static final String KEY_PICTURE_TYPE = "picture-type";
1403 
1404     /** Picture Type is unknown. */
1405     public static final int PICTURE_TYPE_UNKNOWN = 0;
1406 
1407     /** Picture Type is I Frame. */
1408     public static final int PICTURE_TYPE_I = 1;
1409 
1410     /** Picture Type is P Frame. */
1411     public static final int PICTURE_TYPE_P = 2;
1412 
1413     /** Picture Type is B Frame. */
1414     public static final int PICTURE_TYPE_B = 3;
1415 
1416     /** @hide */
1417     @IntDef({
1418         PICTURE_TYPE_UNKNOWN,
1419         PICTURE_TYPE_I,
1420         PICTURE_TYPE_P,
1421         PICTURE_TYPE_B,
1422     })
1423     @Retention(RetentionPolicy.SOURCE)
1424     public @interface PictureType {}
1425 
1426     /**
1427      * A key describing the audio session ID of the AudioTrack associated
1428      * to a tunneled video codec.
1429      * The associated value is an integer.
1430      *
1431      * @see MediaCodecInfo.CodecCapabilities#FEATURE_TunneledPlayback
1432      */
1433     public static final String KEY_AUDIO_SESSION_ID = "audio-session-id";
1434 
1435     /**
1436      * A key describing the audio hardware sync ID of the AudioTrack associated
1437      * to a tunneled video codec. The associated value is an integer.
1438      *
1439      * @hide
1440      *
1441      * @see MediaCodecInfo.CodecCapabilities#FEATURE_TunneledPlayback
1442      * @see AudioManager#getAudioHwSyncForSession
1443      */
1444     public static final String KEY_AUDIO_HW_SYNC = "audio-hw-sync";
1445 
1446     /**
1447      * A key for boolean AUTOSELECT behavior for the track. Tracks with AUTOSELECT=true
1448      * are considered when automatically selecting a track without specific user
1449      * choice, based on the current locale.
1450      * This is currently only used for subtitle tracks, when the user selected
1451      * 'Default' for the captioning locale.
1452      * The associated value is an integer, where non-0 means TRUE.  This is an optional
1453      * field; if not specified, AUTOSELECT defaults to TRUE.
1454      */
1455     public static final String KEY_IS_AUTOSELECT = "is-autoselect";
1456 
1457     /**
1458      * A key for boolean DEFAULT behavior for the track. The track with DEFAULT=true is
1459      * selected in the absence of a specific user choice.
1460      * This is currently used in two scenarios:
1461      * 1) for subtitle tracks, when the user selected 'Default' for the captioning locale.
1462      * 2) for a {@link #MIMETYPE_IMAGE_ANDROID_HEIC} / {@link #MIMETYPE_IMAGE_AVIF} track,
1463      * indicating the image is the primary item in the file.
1464      *
1465      * The associated value is an integer, where non-0 means TRUE.  This is an optional
1466      * field; if not specified, DEFAULT is considered to be FALSE.
1467      */
1468     public static final String KEY_IS_DEFAULT = "is-default";
1469 
1470     /**
1471      * A key for the FORCED field for subtitle tracks. True if it is a
1472      * forced subtitle track.  Forced subtitle tracks are essential for the
1473      * content and are shown even when the user turns off Captions.  They
1474      * are used for example to translate foreign/alien dialogs or signs.
1475      * The associated value is an integer, where non-0 means TRUE.  This is an
1476      * optional field; if not specified, FORCED defaults to FALSE.
1477      */
1478     public static final String KEY_IS_FORCED_SUBTITLE = "is-forced-subtitle";
1479 
1480     /**
1481      * A key describing the number of haptic channels in an audio format.
1482      * The associated value is an integer.
1483      */
1484     public static final String KEY_HAPTIC_CHANNEL_COUNT = "haptic-channel-count";
1485 
1486     /** @hide */
1487     public static final String KEY_IS_TIMED_TEXT = "is-timed-text";
1488 
1489     // The following color aspect values must be in sync with the ones in HardwareAPI.h.
1490     /**
1491      * An optional key describing the color primaries, white point and
1492      * luminance factors for video content.
1493      *
1494      * The associated value is an integer: 0 if unspecified, or one of the
1495      * COLOR_STANDARD_ values.
1496      */
1497     public static final String KEY_COLOR_STANDARD = "color-standard";
1498 
1499     /** BT.709 color chromacity coordinates with KR = 0.2126, KB = 0.0722. */
1500     public static final int COLOR_STANDARD_BT709 = 1;
1501 
1502     /** BT.601 625 color chromacity coordinates with KR = 0.299, KB = 0.114. */
1503     public static final int COLOR_STANDARD_BT601_PAL = 2;
1504 
1505     /** BT.601 525 color chromacity coordinates with KR = 0.299, KB = 0.114. */
1506     public static final int COLOR_STANDARD_BT601_NTSC = 4;
1507 
1508     /** BT.2020 color chromacity coordinates with KR = 0.2627, KB = 0.0593. */
1509     public static final int COLOR_STANDARD_BT2020 = 6;
1510 
1511     /** @hide */
1512     @IntDef({
1513         COLOR_STANDARD_BT709,
1514         COLOR_STANDARD_BT601_PAL,
1515         COLOR_STANDARD_BT601_NTSC,
1516         COLOR_STANDARD_BT2020,
1517     })
1518     @Retention(RetentionPolicy.SOURCE)
1519     public @interface ColorStandard {}
1520 
1521     /**
1522      * An optional key describing the opto-electronic transfer function used
1523      * for the video content.
1524      *
1525      * The associated value is an integer: 0 if unspecified, or one of the
1526      * COLOR_TRANSFER_ values.
1527      */
1528     public static final String KEY_COLOR_TRANSFER = "color-transfer";
1529 
1530     /** Linear transfer characteristic curve. */
1531     public static final int COLOR_TRANSFER_LINEAR = 1;
1532 
1533     /** SMPTE 170M transfer characteristic curve used by BT.601/BT.709/BT.2020. This is the curve
1534      *  used by most non-HDR video content. */
1535     public static final int COLOR_TRANSFER_SDR_VIDEO = 3;
1536 
1537     /** SMPTE ST 2084 transfer function. This is used by some HDR video content. */
1538     public static final int COLOR_TRANSFER_ST2084 = 6;
1539 
1540     /** ARIB STD-B67 hybrid-log-gamma transfer function. This is used by some HDR video content. */
1541     public static final int COLOR_TRANSFER_HLG = 7;
1542 
1543     /** @hide */
1544     @IntDef({
1545         COLOR_TRANSFER_LINEAR,
1546         COLOR_TRANSFER_SDR_VIDEO,
1547         COLOR_TRANSFER_ST2084,
1548         COLOR_TRANSFER_HLG,
1549     })
1550     @Retention(RetentionPolicy.SOURCE)
1551     public @interface ColorTransfer {}
1552 
1553     /**
1554      * An optional key describing the range of the component values of the video content.
1555      *
1556      * The associated value is an integer: 0 if unspecified, or one of the
1557      * COLOR_RANGE_ values.
1558      */
1559     public static final String KEY_COLOR_RANGE = "color-range";
1560 
1561     /** Limited range. Y component values range from 16 to 235 for 8-bit content.
1562      *  Cr, Cy values range from 16 to 240 for 8-bit content.
1563      *  This is the default for video content. */
1564     public static final int COLOR_RANGE_LIMITED = 2;
1565 
1566     /** Full range. Y, Cr and Cb component values range from 0 to 255 for 8-bit content. */
1567     public static final int COLOR_RANGE_FULL = 1;
1568 
1569     /** @hide */
1570     @IntDef({
1571         COLOR_RANGE_LIMITED,
1572         COLOR_RANGE_FULL,
1573     })
1574     @Retention(RetentionPolicy.SOURCE)
1575     public @interface ColorRange {}
1576 
1577     /**
1578      * An optional key describing the static metadata of HDR (high-dynamic-range) video content.
1579      *
1580      * The associated value is a ByteBuffer. This buffer contains the raw contents of the
1581      * Static Metadata Descriptor (including the descriptor ID) of an HDMI Dynamic Range and
1582      * Mastering InfoFrame as defined by CTA-861.3. This key must be provided to video decoders
1583      * for HDR video content unless this information is contained in the bitstream and the video
1584      * decoder supports an HDR-capable profile. This key must be provided to video encoders for
1585      * HDR video content.
1586      */
1587     public static final String KEY_HDR_STATIC_INFO = "hdr-static-info";
1588 
1589     /**
1590      * An optional key describing the HDR10+ metadata of the video content.
1591      *
1592      * The associated value is a ByteBuffer containing HDR10+ metadata conforming to the
1593      * user_data_registered_itu_t_t35() syntax of SEI message for ST 2094-40. This key will
1594      * be present on:
1595      *<p>
1596      * - The formats of output buffers of a decoder configured for HDR10+ profiles (such as
1597      *   {@link MediaCodecInfo.CodecProfileLevel#VP9Profile2HDR10Plus}, {@link
1598      *   MediaCodecInfo.CodecProfileLevel#VP9Profile3HDR10Plus} or {@link
1599      *   MediaCodecInfo.CodecProfileLevel#HEVCProfileMain10HDR10Plus}), or
1600      *<p>
1601      * - The formats of output buffers of an encoder configured for an HDR10+ profiles that
1602      *   uses out-of-band metadata (such as {@link
1603      *   MediaCodecInfo.CodecProfileLevel#VP9Profile2HDR10Plus} or {@link
1604      *   MediaCodecInfo.CodecProfileLevel#VP9Profile3HDR10Plus}).
1605      *
1606      * @see MediaCodec#PARAMETER_KEY_HDR10_PLUS_INFO
1607      */
1608     public static final String KEY_HDR10_PLUS_INFO = "hdr10-plus-info";
1609 
1610     /**
1611      * An optional key describing the opto-electronic transfer function
1612      * requested for the output video content.
1613      *
1614      * The associated value is an integer: 0 if unspecified, or one of the
1615      * COLOR_TRANSFER_ values. When unspecified the component will not touch the
1616      * video content; otherwise the component will tone-map the raw video frame
1617      * to match the requested transfer function.
1618      *
1619      * After configure, component's input format will contain this key to note
1620      * whether the request is supported or not. If the value in the input format
1621      * is the same as the requested value, the request is supported. The value
1622      * is set to 0 if unsupported.
1623      */
1624     public static final String KEY_COLOR_TRANSFER_REQUEST = "color-transfer-request";
1625 
1626     /**
1627      * A key describing a unique ID for the content of a media track.
1628      *
1629      * <p>This key is used by {@link MediaExtractor}. Some extractors provide multiple encodings
1630      * of the same track (e.g. float audio tracks for FLAC and WAV may be expressed as two
1631      * tracks via MediaExtractor: a normal PCM track for backward compatibility, and a float PCM
1632      * track for added fidelity. Similarly, Dolby Vision extractor may provide a baseline SDR
1633      * version of a DV track.) This key can be used to identify which MediaExtractor tracks refer
1634      * to the same underlying content.
1635      * </p>
1636      *
1637      * The associated value is an integer.
1638      */
1639     public static final String KEY_TRACK_ID = "track-id";
1640 
1641     /**
1642      * A key describing the system id of the conditional access system used to scramble
1643      * a media track.
1644      * <p>
1645      * This key is set by {@link MediaExtractor} if the track is scrambled with a conditional
1646      * access system, regardless of the presence of a valid {@link MediaCas} object.
1647      * <p>
1648      * The associated value is an integer.
1649      * @hide
1650      */
1651     public static final String KEY_CA_SYSTEM_ID = "ca-system-id";
1652 
1653     /**
1654      * A key describing the {@link MediaCas.Session} object associated with a media track.
1655      * <p>
1656      * This key is set by {@link MediaExtractor} if the track is scrambled with a conditional
1657      * access system, after it receives a valid {@link MediaCas} object.
1658      * <p>
1659      * The associated value is a ByteBuffer.
1660      * @hide
1661      */
1662     public static final String KEY_CA_SESSION_ID = "ca-session-id";
1663 
1664     /**
1665      * A key describing the private data in the CA_descriptor associated with a media track.
1666      * <p>
1667      * This key is set by {@link MediaExtractor} if the track is scrambled with a conditional
1668      * access system, before it receives a valid {@link MediaCas} object.
1669      * <p>
1670      * The associated value is a ByteBuffer.
1671      * @hide
1672      */
1673     public static final String KEY_CA_PRIVATE_DATA = "ca-private-data";
1674 
1675     /**
1676      * A key describing the maximum number of B frames between I or P frames,
1677      * to be used by a video encoder.
1678      * The associated value is an integer. The default value is 0, which means
1679      * that no B frames are allowed. Note that non-zero value does not guarantee
1680      * B frames; it's up to the encoder to decide.
1681      */
1682     public static final String KEY_MAX_B_FRAMES = "max-bframes";
1683 
1684     /**
1685      * A key for applications to opt out of allowing
1686      * a Surface to discard undisplayed/unconsumed frames
1687      * as means to catch up after falling behind.
1688      * This value is an integer.
1689      * The value 0 indicates the surface is not allowed to drop frames.
1690      * The value 1 indicates the surface is allowed to drop frames.
1691      *
1692      * {@link MediaCodec} describes the semantics.
1693      */
1694     public static final String KEY_ALLOW_FRAME_DROP = "allow-frame-drop";
1695 
1696     /**
1697      * A key describing the desired codec importance for the application.
1698      * <p>
1699      * The associated value is a positive integer including zero.
1700      * Higher value means lesser importance.
1701      * <p>
1702      * The resource manager may use the codec importance, along with other factors
1703      * when reclaiming codecs from an application.
1704      * The specifics of reclaim policy is device dependent, but specifying the codec importance,
1705      * will allow the resource manager to prioritize reclaiming less important codecs
1706      * (assigned higher values) from the (reclaim) requesting application first.
1707      * So, the codec importance is only relevant within the context of that application.
1708      * <p>
1709      * The codec importance can be set:
1710      * <ul>
1711      * <li>through {@link MediaCodec#configure}. </li>
1712      * <li>through {@link MediaCodec#setParameters} if the codec has been configured already,
1713      * which allows the users to change the codec importance multiple times.
1714      * </ul>
1715      * Any change/update in codec importance is guaranteed upon the completion of the function call
1716      * that sets the codec importance. So, in case of concurrent codec operations,
1717      * make sure to wait for the change in codec importance, before using another codec.
1718      * Note that unless specified, by default the codecs will have highest importance (of value 0).
1719      *
1720      */
1721     @FlaggedApi(FLAG_CODEC_IMPORTANCE)
1722     public static final String KEY_IMPORTANCE = "importance";
1723 
1724     /** @hide */
1725     @IntDef(flag = true, prefix = {"FLAG_SECURITY_MODEL_"}, value = {
1726         FLAG_SECURITY_MODEL_SANDBOXED,
1727         FLAG_SECURITY_MODEL_MEMORY_SAFE,
1728         FLAG_SECURITY_MODEL_TRUSTED_CONTENT_ONLY,
1729     })
1730     @Retention(RetentionPolicy.SOURCE)
1731     public @interface SecurityModelFlag {}
1732 
1733     /**
1734      * Flag for {@link MediaCodecInfo#SECURITY_MODEL_SANDBOXED}.
1735      */
1736     @FlaggedApi(FLAG_IN_PROCESS_SW_AUDIO_CODEC)
1737     public static final int FLAG_SECURITY_MODEL_SANDBOXED =
1738             (1 << MediaCodecInfo.SECURITY_MODEL_SANDBOXED);
1739     /**
1740      * Flag for {@link MediaCodecInfo#SECURITY_MODEL_MEMORY_SAFE}.
1741      */
1742     @FlaggedApi(FLAG_IN_PROCESS_SW_AUDIO_CODEC)
1743     public static final int FLAG_SECURITY_MODEL_MEMORY_SAFE =
1744             (1 << MediaCodecInfo.SECURITY_MODEL_MEMORY_SAFE);
1745     /**
1746      * Flag for {@link MediaCodecInfo#SECURITY_MODEL_TRUSTED_CONTENT_ONLY}.
1747      */
1748     @FlaggedApi(FLAG_IN_PROCESS_SW_AUDIO_CODEC)
1749     public static final int FLAG_SECURITY_MODEL_TRUSTED_CONTENT_ONLY =
1750             (1 << MediaCodecInfo.SECURITY_MODEL_TRUSTED_CONTENT_ONLY);
1751 
1752     /**
1753      * A key describing the requested security model as flags.
1754      * <p>
1755      * The associated value is a flag of the following values:
1756      * {@link FLAG_SECURITY_MODEL_SANDBOXED},
1757      * {@link FLAG_SECURITY_MODEL_MEMORY_SAFE},
1758      * {@link FLAG_SECURITY_MODEL_TRUSTED_CONTENT_ONLY}. The default value is
1759      * {@link FLAG_SECURITY_MODEL_SANDBOXED}.
1760      * <p>
1761      * When passed to {@link MediaCodecList#findDecoderForFormat} or
1762      * {@link MediaCodecList#findEncoderForFormat}, MediaCodecList filters
1763      * the security model of the codecs according to this flag value.
1764      * <p>
1765      * When passed to {@link MediaCodec#configure}, MediaCodec verifies
1766      * the security model matches the flag value passed, and throws
1767      * {@link java.lang.IllegalArgumentException} if the model does not match.
1768      * <p>
1769      * @see MediaCodecInfo#getSecurityModel
1770      * @see MediaCodecList#findDecoderForFormat
1771      * @see MediaCodecList#findEncoderForFormat
1772      */
1773     @FlaggedApi(FLAG_IN_PROCESS_SW_AUDIO_CODEC)
1774     public static final String KEY_SECURITY_MODEL = "security-model";
1775 
1776     /**
1777      * QpOffsetRect constitutes the metadata required for encoding a region of interest in an
1778      * image or a video frame. The region of interest is represented by a rectangle. The four
1779      * integer coordinates of the rectangle are stored in fields left, top, right, bottom.
1780      * Note that the right and bottom coordinates are exclusive.
1781      * This is paired with a suggestive qp offset information that is to be used during encoding
1782      * of the blocks belonging to the to the box.
1783      */
1784     @FlaggedApi(FLAG_REGION_OF_INTEREST)
1785     public static final class QpOffsetRect {
1786         private Rect mContour;
1787         private int mQpOffset;
1788 
1789         /**
1790          * Create a new region of interest with the specified coordinates and qpOffset. Note: no
1791          * range checking is performed, so the caller must ensure that left >= 0, left <= right,
1792          * top >= 0 and top <= bottom. Note that the right and bottom coordinates are exclusive.
1793          *
1794          * @param contour  Rectangle specifying the region of interest
1795          * @param qpOffset qpOffset to be used for the blocks in the specified rectangle
1796          */
QpOffsetRect(@onNull Rect contour, int qpOffset)1797         public QpOffsetRect(@NonNull Rect contour, int qpOffset) {
1798             mContour = contour;
1799             mQpOffset = qpOffset;
1800         }
1801 
1802         /**
1803          * Update the region of interest information with the specified coordinates and qpOffset
1804          *
1805          * @param contour  Rectangle specifying the region of interest
1806          * @param qpOffset qpOffset to be used for the blocks in the specified rectangle
1807          */
set(@onNull Rect contour, int qpOffset)1808         public void set(@NonNull Rect contour, int qpOffset) {
1809             mContour = contour;
1810             mQpOffset = qpOffset;
1811         }
1812 
1813         /**
1814          * @return Return a string representation of qpOffsetRect in a compact form.
1815          * Helper function to insert key {@link #PARAMETER_KEY_QP_OFFSET_RECTS} in MediaFormat
1816          */
1817         @NonNull
flattenToString()1818         public String flattenToString() {
1819             return TextUtils.formatSimple("%d,%d-%d,%d=%d;", mContour.top, mContour.left,
1820                         mContour.bottom, mContour.right, mQpOffset);
1821         }
1822 
1823         /**
1824          * @return Return a string representation of qpOffsetRect in a compact form.
1825          * Helper function to insert key {@link #PARAMETER_KEY_QP_OFFSET_RECTS} in MediaFormat
1826          */
1827         @NonNull
flattenToString(@onNull List<QpOffsetRect> qpOffsetRects)1828         public static String flattenToString(@NonNull List<QpOffsetRect> qpOffsetRects) {
1829             StringBuilder builder = new StringBuilder();
1830             for (QpOffsetRect qpOffsetRect : qpOffsetRects) {
1831                 builder.append(qpOffsetRect.flattenToString());
1832             }
1833             return builder.toString();
1834         }
1835     }
1836 
MediaFormat(@onNull Map<String, Object> map)1837     /* package private */ MediaFormat(@NonNull Map<String, Object> map) {
1838         mMap = map;
1839     }
1840 
1841     /**
1842      * Creates an empty MediaFormat
1843      */
MediaFormat()1844     public MediaFormat() {
1845         mMap = new HashMap();
1846     }
1847 
1848     @UnsupportedAppUsage
getMap()1849     /* package private */ Map<String, Object> getMap() {
1850         return mMap;
1851     }
1852 
1853     /**
1854      * Returns true iff a key of the given name exists in the format.
1855      */
containsKey(@onNull String name)1856     public final boolean containsKey(@NonNull String name) {
1857         return mMap.containsKey(name);
1858     }
1859 
1860     /**
1861      * Returns true iff a feature of the given name exists in the format.
1862      */
containsFeature(@onNull String name)1863     public final boolean containsFeature(@NonNull String name) {
1864         return mMap.containsKey(KEY_FEATURE_ + name);
1865     }
1866 
1867     public static final int TYPE_NULL = 0;
1868     public static final int TYPE_INTEGER = 1;
1869     public static final int TYPE_LONG = 2;
1870     public static final int TYPE_FLOAT = 3;
1871     public static final int TYPE_STRING = 4;
1872     public static final int TYPE_BYTE_BUFFER = 5;
1873 
1874     /** @hide */
1875     @IntDef({
1876         TYPE_NULL,
1877         TYPE_INTEGER,
1878         TYPE_LONG,
1879         TYPE_FLOAT,
1880         TYPE_STRING,
1881         TYPE_BYTE_BUFFER
1882     })
1883     @Retention(RetentionPolicy.SOURCE)
1884     public @interface Type {}
1885 
1886     /**
1887      * Returns the value type for a key. If the key does not exist, it returns TYPE_NULL.
1888      */
getValueTypeForKey(@onNull String name)1889     public final @Type int getValueTypeForKey(@NonNull String name) {
1890         Object value = mMap.get(name);
1891         if (value == null) {
1892             return TYPE_NULL;
1893         } else if (value instanceof Integer) {
1894             return TYPE_INTEGER;
1895         } else if (value instanceof Long) {
1896             return TYPE_LONG;
1897         } else if (value instanceof Float) {
1898             return TYPE_FLOAT;
1899         } else if (value instanceof String) {
1900             return TYPE_STRING;
1901         } else if (value instanceof ByteBuffer) {
1902             return TYPE_BYTE_BUFFER;
1903         }
1904         throw new RuntimeException("invalid value for key");
1905     }
1906 
1907     /**
1908      * A key prefix used together with a {@link MediaCodecInfo.CodecCapabilities}
1909      * feature name describing a required or optional feature for a codec capabilities
1910      * query.
1911      * The associated value is an integer, where non-0 value means the feature is
1912      * requested to be present, while 0 value means the feature is requested to be not
1913      * present.
1914      * @see MediaCodecList#findDecoderForFormat
1915      * @see MediaCodecList#findEncoderForFormat
1916      * @see MediaCodecInfo.CodecCapabilities#isFormatSupported
1917      *
1918      * @hide
1919      */
1920     public static final String KEY_FEATURE_ = "feature-";
1921 
1922     /**
1923      * Returns the value of a numeric key. This is provided as a convenience method for keys
1924      * that may take multiple numeric types, such as {@link #KEY_FRAME_RATE}, or {@link
1925      * #KEY_I_FRAME_INTERVAL}.
1926      *
1927      * @return null if the key does not exist or the stored value for the key is null
1928      * @throws ClassCastException if the stored value for the key is ByteBuffer or String
1929      */
getNumber(@onNull String name)1930     public final @Nullable Number getNumber(@NonNull String name) {
1931         return (Number) mMap.get(name);
1932     }
1933 
1934     /**
1935      * Returns the value of a numeric key, or the default value if the key is missing.
1936      *
1937      * @return defaultValue if the key does not exist or the stored value for the key is null
1938      * @throws ClassCastException if the stored value for the key is ByteBuffer or String
1939      */
getNumber(@onNull String name, @NonNull Number defaultValue)1940     public final @NonNull Number getNumber(@NonNull String name, @NonNull Number defaultValue) {
1941         Number ret = getNumber(name);
1942         return ret == null ? defaultValue : ret;
1943     }
1944 
1945     /**
1946      * Returns the value of an integer key.
1947      *
1948      * @throws NullPointerException if the key does not exist or the stored value for the key is
1949      *         null
1950      * @throws ClassCastException if the stored value for the key is long, float, ByteBuffer or
1951      *         String
1952      */
getInteger(@onNull String name)1953     public final int getInteger(@NonNull String name) {
1954         return (int) mMap.get(name);
1955     }
1956 
1957     /**
1958      * Returns the value of an integer key, or the default value if the key is missing.
1959      *
1960      * @return defaultValue if the key does not exist or the stored value for the key is null
1961      * @throws ClassCastException if the stored value for the key is long, float, ByteBuffer or
1962      *         String
1963      */
getInteger(@onNull String name, int defaultValue)1964     public final int getInteger(@NonNull String name, int defaultValue) {
1965         try {
1966             return getInteger(name);
1967         } catch (NullPointerException  e) {
1968             /* no such field or field is null */
1969             return defaultValue;
1970         }
1971     }
1972 
1973     /**
1974      * Returns the value of a long key.
1975      *
1976      * @throws NullPointerException if the key does not exist or the stored value for the key is
1977      *         null
1978      * @throws ClassCastException if the stored value for the key is int, float, ByteBuffer or
1979      *         String
1980      */
getLong(@onNull String name)1981     public final long getLong(@NonNull String name) {
1982         return (long) mMap.get(name);
1983     }
1984 
1985     /**
1986      * Returns the value of a long key, or the default value if the key is missing.
1987      *
1988      * @return defaultValue if the key does not exist or the stored value for the key is null
1989      * @throws ClassCastException if the stored value for the key is int, float, ByteBuffer or
1990      *         String
1991      */
getLong(@onNull String name, long defaultValue)1992     public final long getLong(@NonNull String name, long defaultValue) {
1993         try {
1994             return getLong(name);
1995         } catch (NullPointerException  e) {
1996             /* no such field or field is null */
1997             return defaultValue;
1998         }
1999     }
2000 
2001     /**
2002      * Returns the value of a float key.
2003      *
2004      * @throws NullPointerException if the key does not exist or the stored value for the key is
2005      *         null
2006      * @throws ClassCastException if the stored value for the key is int, long, ByteBuffer or
2007      *         String
2008      */
getFloat(@onNull String name)2009     public final float getFloat(@NonNull String name) {
2010         return (float) mMap.get(name);
2011     }
2012 
2013     /**
2014      * Returns the value of a float key, or the default value if the key is missing.
2015      *
2016      * @return defaultValue if the key does not exist or the stored value for the key is null
2017      * @throws ClassCastException if the stored value for the key is int, long, ByteBuffer or
2018      *         String
2019      */
getFloat(@onNull String name, float defaultValue)2020     public final float getFloat(@NonNull String name, float defaultValue) {
2021         Object value = mMap.get(name);
2022         return value != null ? (float) value : defaultValue;
2023     }
2024 
2025     /**
2026      * Returns the value of a string key.
2027      *
2028      * @return null if the key does not exist or the stored value for the key is null
2029      * @throws ClassCastException if the stored value for the key is int, long, float or ByteBuffer
2030      */
getString(@onNull String name)2031     public final @Nullable String getString(@NonNull String name) {
2032         return (String)mMap.get(name);
2033     }
2034 
2035     /**
2036      * Returns the value of a string key, or the default value if the key is missing.
2037      *
2038      * @return defaultValue if the key does not exist or the stored value for the key is null
2039      * @throws ClassCastException if the stored value for the key is int, long, float or ByteBuffer
2040      */
getString(@onNull String name, @NonNull String defaultValue)2041     public final @NonNull String getString(@NonNull String name, @NonNull String defaultValue) {
2042         String ret = getString(name);
2043         return ret == null ? defaultValue : ret;
2044     }
2045 
2046     /**
2047      * Returns the value of a ByteBuffer key.
2048      *
2049      * @return null if the key does not exist or the stored value for the key is null
2050      * @throws ClassCastException if the stored value for the key is int, long, float or String
2051      */
getByteBuffer(@onNull String name)2052     public final @Nullable ByteBuffer getByteBuffer(@NonNull String name) {
2053         return (ByteBuffer)mMap.get(name);
2054     }
2055 
2056     /**
2057      * Returns the value of a ByteBuffer key, or the default value if the key is missing.
2058      *
2059      * @return defaultValue if the key does not exist or the stored value for the key is null
2060      * @throws ClassCastException if the stored value for the key is int, long, float or String
2061      */
getByteBuffer( @onNull String name, @NonNull ByteBuffer defaultValue)2062     public final @NonNull ByteBuffer getByteBuffer(
2063             @NonNull String name, @NonNull ByteBuffer defaultValue) {
2064         ByteBuffer ret = getByteBuffer(name);
2065         return ret == null ? defaultValue : ret;
2066     }
2067 
2068     /**
2069      * Returns whether a feature is to be enabled ({@code true}) or disabled
2070      * ({@code false}).
2071      *
2072      * @param feature the name of a {@link MediaCodecInfo.CodecCapabilities} feature.
2073      *
2074      * @throws IllegalArgumentException if the feature was neither set to be enabled
2075      *         nor to be disabled.
2076      */
getFeatureEnabled(@onNull String feature)2077     public boolean getFeatureEnabled(@NonNull String feature) {
2078         Integer enabled = (Integer)mMap.get(KEY_FEATURE_ + feature);
2079         if (enabled == null) {
2080             throw new IllegalArgumentException("feature is not specified");
2081         }
2082         return enabled != 0;
2083     }
2084 
2085     /**
2086      * Sets the value of an integer key.
2087      */
setInteger(@onNull String name, int value)2088     public final void setInteger(@NonNull String name, int value) {
2089         mMap.put(name, value);
2090     }
2091 
2092     /**
2093      * Sets the value of a long key.
2094      */
setLong(@onNull String name, long value)2095     public final void setLong(@NonNull String name, long value) {
2096         mMap.put(name, value);
2097     }
2098 
2099     /**
2100      * Sets the value of a float key.
2101      */
setFloat(@onNull String name, float value)2102     public final void setFloat(@NonNull String name, float value) {
2103         mMap.put(name, value);
2104     }
2105 
2106     /**
2107      * Sets the value of a string key.
2108      * <p>
2109      * If value is {@code null}, it sets a null value that behaves similarly to a missing key.
2110      * This could be used prior to API level {@link android os.Build.VERSION_CODES#Q} to effectively
2111      * remove a key.
2112      */
setString(@onNull String name, @Nullable String value)2113     public final void setString(@NonNull String name, @Nullable String value) {
2114         mMap.put(name, value);
2115     }
2116 
2117     /**
2118      * Sets the value of a ByteBuffer key.
2119      * <p>
2120      * If value is {@code null}, it sets a null value that behaves similarly to a missing key.
2121      * This could be used prior to API level {@link android os.Build.VERSION_CODES#Q} to effectively
2122      * remove a key.
2123      */
setByteBuffer(@onNull String name, @Nullable ByteBuffer bytes)2124     public final void setByteBuffer(@NonNull String name, @Nullable ByteBuffer bytes) {
2125         mMap.put(name, bytes);
2126     }
2127 
2128     /**
2129      * Removes a value of a given key if present. Has no effect if the key is not present.
2130      */
removeKey(@onNull String name)2131     public final void removeKey(@NonNull String name) {
2132         // exclude feature mappings
2133         if (!name.startsWith(KEY_FEATURE_)) {
2134             mMap.remove(name);
2135         }
2136     }
2137 
2138     /**
2139      * Removes a given feature setting if present. Has no effect if the feature setting is not
2140      * present.
2141      */
removeFeature(@onNull String name)2142     public final void removeFeature(@NonNull String name) {
2143         mMap.remove(KEY_FEATURE_ + name);
2144     }
2145 
2146     /**
2147      * A Partial set view for a portion of the keys in a MediaFormat object.
2148      *
2149      * This class is needed as we want to return a portion of the actual format keys in getKeys()
2150      * and another portion of the keys in getFeatures(), and still allow the view properties.
2151      */
2152     private abstract class FilteredMappedKeySet extends AbstractSet<String> {
2153         private Set<String> mKeys;
2154 
2155         // Returns true if this set should include this key
keepKey(String key)2156         abstract protected boolean keepKey(String key);
2157 
2158         // Maps a key from the underlying key set into its new value in this key set
mapKeyToItem(String key)2159         abstract protected String mapKeyToItem(String key);
2160 
2161         // Maps a key from this key set into its original value in the underlying key set
mapItemToKey(String item)2162         abstract protected String mapItemToKey(String item);
2163 
FilteredMappedKeySet()2164         public FilteredMappedKeySet() {
2165             mKeys = mMap.keySet();
2166         }
2167 
2168         // speed up contains and remove from abstract implementation (that would iterate
2169         // over each element)
2170         @Override
contains(Object o)2171         public boolean contains(Object o) {
2172             if (o instanceof String) {
2173                 String key = mapItemToKey((String)o);
2174                 return keepKey(key) && mKeys.contains(key);
2175             }
2176             return false;
2177         }
2178 
2179         @Override
remove(Object o)2180         public boolean remove(Object o) {
2181             if (o instanceof String) {
2182                 String key = mapItemToKey((String)o);
2183                 if (keepKey(key) && mKeys.remove(key)) {
2184                     mMap.remove(key);
2185                     return true;
2186                 }
2187             }
2188             return false;
2189         }
2190 
2191         private class KeyIterator implements Iterator<String> {
2192             Iterator<String> mIterator;
2193             String mLast;
2194 
KeyIterator()2195             public KeyIterator() {
2196                 // We must create a copy of the filtered stream, as remove operation has to modify
2197                 // the underlying data structure (mMap), so the iterator's operation is undefined.
2198                 // Use a list as it is likely less memory consuming than the other alternative: set.
2199                 mIterator =
2200                     mKeys.stream().filter(k -> keepKey(k)).collect(Collectors.toList()).iterator();
2201             }
2202 
2203             @Override
hasNext()2204             public boolean hasNext() {
2205                 return mIterator.hasNext();
2206             }
2207 
2208             @Override
next()2209             public String next() {
2210                 mLast = mIterator.next();
2211                 return mapKeyToItem(mLast);
2212             }
2213 
2214             @Override
remove()2215             public void remove() {
2216                 mIterator.remove();
2217                 mMap.remove(mLast);
2218             }
2219         }
2220 
2221         @Override
iterator()2222         public Iterator<String> iterator() {
2223             return new KeyIterator();
2224         }
2225 
2226         @Override
size()2227         public int size() {
2228             return (int) mKeys.stream().filter(this::keepKey).count();
2229         }
2230     }
2231 
2232     /**
2233      * A Partial set view for a portion of the keys in a MediaFormat object for keys that
2234      * don't start with a prefix, such as "feature-"
2235      */
2236     private class UnprefixedKeySet extends FilteredMappedKeySet {
2237         private String mPrefix;
2238 
UnprefixedKeySet(String prefix)2239         public UnprefixedKeySet(String prefix) {
2240             super();
2241             mPrefix = prefix;
2242         }
2243 
keepKey(String key)2244         protected boolean keepKey(String key) {
2245             return !key.startsWith(mPrefix);
2246         }
2247 
mapKeyToItem(String key)2248         protected String mapKeyToItem(String key) {
2249             return key;
2250         }
2251 
mapItemToKey(String item)2252         protected String mapItemToKey(String item) {
2253             return item;
2254         }
2255     }
2256 
2257     /**
2258      * A Partial set view for a portion of the keys in a MediaFormat object for keys that
2259      * start with a prefix, such as "feature-", with the prefix removed
2260      */
2261     private class PrefixedKeySetWithPrefixRemoved extends FilteredMappedKeySet {
2262         private String mPrefix;
2263         private int mPrefixLength;
2264 
PrefixedKeySetWithPrefixRemoved(String prefix)2265         public PrefixedKeySetWithPrefixRemoved(String prefix) {
2266             super();
2267             mPrefix = prefix;
2268             mPrefixLength = prefix.length();
2269         }
2270 
keepKey(String key)2271         protected boolean keepKey(String key) {
2272             return key.startsWith(mPrefix);
2273         }
2274 
mapKeyToItem(String key)2275         protected String mapKeyToItem(String key) {
2276             return key.substring(mPrefixLength);
2277         }
2278 
mapItemToKey(String item)2279         protected String mapItemToKey(String item) {
2280             return mPrefix + item;
2281         }
2282     }
2283 
2284 
2285    /**
2286      * Returns a {@link java.util.Set Set} view of the keys contained in this MediaFormat.
2287      *
2288      * The set is backed by the MediaFormat object, so changes to the format are reflected in the
2289      * set, and vice-versa. If the format is modified while an iteration over the set is in progress
2290      * (except through the iterator's own remove operation), the results of the iteration are
2291      * undefined. The set supports element removal, which removes the corresponding mapping from the
2292      * format, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations.
2293      * It does not support the add or addAll operations.
2294      */
getKeys()2295     public final @NonNull java.util.Set<String> getKeys() {
2296         return new UnprefixedKeySet(KEY_FEATURE_);
2297     }
2298 
2299    /**
2300      * Returns a {@link java.util.Set Set} view of the features contained in this MediaFormat.
2301      *
2302      * The set is backed by the MediaFormat object, so changes to the format are reflected in the
2303      * set, and vice-versa. If the format is modified while an iteration over the set is in progress
2304      * (except through the iterator's own remove operation), the results of the iteration are
2305      * undefined. The set supports element removal, which removes the corresponding mapping from the
2306      * format, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations.
2307      * It does not support the add or addAll operations.
2308      */
getFeatures()2309     public final @NonNull java.util.Set<String> getFeatures() {
2310         return new PrefixedKeySetWithPrefixRemoved(KEY_FEATURE_);
2311     }
2312 
2313     /**
2314      * Create a copy of a media format object.
2315      */
MediaFormat(@onNull MediaFormat other)2316     public MediaFormat(@NonNull MediaFormat other) {
2317         this();
2318         mMap.putAll(other.mMap);
2319     }
2320 
2321     /**
2322      * Sets whether a feature is to be enabled ({@code true}) or disabled
2323      * ({@code false}).
2324      *
2325      * If {@code enabled} is {@code true}, the feature is requested to be present.
2326      * Otherwise, the feature is requested to be not present.
2327      *
2328      * @param feature the name of a {@link MediaCodecInfo.CodecCapabilities} feature.
2329      *
2330      * @see MediaCodecList#findDecoderForFormat
2331      * @see MediaCodecList#findEncoderForFormat
2332      * @see MediaCodecInfo.CodecCapabilities#isFormatSupported
2333      */
setFeatureEnabled(@onNull String feature, boolean enabled)2334     public void setFeatureEnabled(@NonNull String feature, boolean enabled) {
2335         setInteger(KEY_FEATURE_ + feature, enabled ? 1 : 0);
2336     }
2337 
2338     /**
2339      * Creates a minimal audio format.
2340      * @param mime The mime type of the content.
2341      * @param sampleRate The sampling rate of the content.
2342      * @param channelCount The number of audio channels in the content.
2343      */
createAudioFormat( @onNull String mime, int sampleRate, int channelCount)2344     public static final @NonNull MediaFormat createAudioFormat(
2345             @NonNull String mime,
2346             int sampleRate,
2347             int channelCount) {
2348         MediaFormat format = new MediaFormat();
2349         format.setString(KEY_MIME, mime);
2350         format.setInteger(KEY_SAMPLE_RATE, sampleRate);
2351         format.setInteger(KEY_CHANNEL_COUNT, channelCount);
2352 
2353         return format;
2354     }
2355 
2356     /**
2357      * Creates a minimal subtitle format.
2358      * @param mime The mime type of the content.
2359      * @param language The language of the content, using either ISO 639-1 or 639-2/T
2360      *        codes.  Specify null or "und" if language information is only included
2361      *        in the content.  (This will also work if there are multiple language
2362      *        tracks in the content.)
2363      */
createSubtitleFormat( @onNull String mime, String language)2364     public static final @NonNull MediaFormat createSubtitleFormat(
2365             @NonNull String mime,
2366             String language) {
2367         MediaFormat format = new MediaFormat();
2368         format.setString(KEY_MIME, mime);
2369         format.setString(KEY_LANGUAGE, language);
2370 
2371         return format;
2372     }
2373 
2374     /**
2375      * Creates a minimal video format.
2376      * @param mime The mime type of the content.
2377      * @param width The width of the content (in pixels)
2378      * @param height The height of the content (in pixels)
2379      */
createVideoFormat( @onNull String mime, int width, int height)2380     public static final @NonNull MediaFormat createVideoFormat(
2381             @NonNull String mime,
2382             int width,
2383             int height) {
2384         MediaFormat format = new MediaFormat();
2385         format.setString(KEY_MIME, mime);
2386         format.setInteger(KEY_WIDTH, width);
2387         format.setInteger(KEY_HEIGHT, height);
2388 
2389         return format;
2390     }
2391 
2392     @Override
toString()2393     public @NonNull String toString() {
2394         return mMap.toString();
2395     }
2396 }
2397