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 android.annotation.IntDef; 20 21 import java.lang.annotation.Retention; 22 import java.lang.annotation.RetentionPolicy; 23 import java.nio.ByteBuffer; 24 import java.util.HashMap; 25 import java.util.Map; 26 27 /** 28 * Encapsulates the information describing the format of media data, 29 * be it audio or video. 30 * 31 * The format of the media data is specified as string/value pairs. 32 * 33 * Keys common to all audio/video formats, <b>all keys not marked optional are mandatory</b>: 34 * 35 * <table> 36 * <tr><th>Name</th><th>Value Type</th><th>Description</th></tr> 37 * <tr><td>{@link #KEY_MIME}</td><td>String</td><td>The type of the format.</td></tr> 38 * <tr><td>{@link #KEY_MAX_INPUT_SIZE}</td><td>Integer</td><td>optional, maximum size of a buffer of input data</td></tr> 39 * <tr><td>{@link #KEY_BIT_RATE}</td><td>Integer</td><td><b>encoder-only</b>, desired bitrate in bits/second</td></tr> 40 * </table> 41 * 42 * Video formats have the following keys: 43 * <table> 44 * <tr><th>Name</th><th>Value Type</th><th>Description</th></tr> 45 * <tr><td>{@link #KEY_WIDTH}</td><td>Integer</td><td></td></tr> 46 * <tr><td>{@link #KEY_HEIGHT}</td><td>Integer</td><td></td></tr> 47 * <tr><td>{@link #KEY_COLOR_FORMAT}</td><td>Integer</td><td>set by the user 48 * for encoders, readable in the output format of decoders</b></td></tr> 49 * <tr><td>{@link #KEY_FRAME_RATE}</td><td>Integer or Float</td><td>required for <b>encoders</b>, 50 * optional for <b>decoders</b></td></tr> 51 * <tr><td>{@link #KEY_CAPTURE_RATE}</td><td>Integer</td><td></td></tr> 52 * <tr><td>{@link #KEY_I_FRAME_INTERVAL}</td><td>Integer (or Float)</td><td><b>encoder-only</b>, 53 * time-interval between key frames. 54 * Float support added in {@link android.os.Build.VERSION_CODES#N_MR1}</td></tr> 55 * <tr><td>{@link #KEY_INTRA_REFRESH_PERIOD}</td><td>Integer</td><td><b>encoder-only</b>, optional</td></tr> 56 * <tr><td>{@link #KEY_MAX_WIDTH}</td><td>Integer</td><td><b>decoder-only</b>, optional, max-resolution width</td></tr> 57 * <tr><td>{@link #KEY_MAX_HEIGHT}</td><td>Integer</td><td><b>decoder-only</b>, optional, max-resolution height</td></tr> 58 * <tr><td>{@link #KEY_REPEAT_PREVIOUS_FRAME_AFTER}</td><td>Long</td><td><b>encoder in surface-mode 59 * only</b>, optional</td></tr> 60 * <tr><td>{@link #KEY_PUSH_BLANK_BUFFERS_ON_STOP}</td><td>Integer(1)</td><td><b>decoder rendering 61 * to a surface only</b>, optional</td></tr> 62 * <tr><td>{@link #KEY_TEMPORAL_LAYERING}</td><td>String</td><td><b>encoder only</b>, optional, 63 * temporal-layering schema</td></tr> 64 * </table> 65 * Specify both {@link #KEY_MAX_WIDTH} and {@link #KEY_MAX_HEIGHT} to enable 66 * adaptive playback (seamless resolution change) for a video decoder that 67 * supports it ({@link MediaCodecInfo.CodecCapabilities#FEATURE_AdaptivePlayback}). 68 * The values are used as hints for the codec: they are the maximum expected 69 * resolution to prepare for. Depending on codec support, preparing for larger 70 * maximum resolution may require more memory even if that resolution is never 71 * reached. These fields have no effect for codecs that do not support adaptive 72 * playback.<br /><br /> 73 * 74 * Audio formats have the following keys: 75 * <table> 76 * <tr><th>Name</th><th>Value Type</th><th>Description</th></tr> 77 * <tr><td>{@link #KEY_CHANNEL_COUNT}</td><td>Integer</td><td></td></tr> 78 * <tr><td>{@link #KEY_SAMPLE_RATE}</td><td>Integer</td><td></td></tr> 79 * <tr><td>{@link #KEY_PCM_ENCODING}</td><td>Integer</td><td>optional</td></tr> 80 * <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> 81 * <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> 82 * <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> 83 * <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> 84 * <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> 85 * <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> 86 * <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> 87 * <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> 88 * <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> 89 * <tr><td>{@link #KEY_CHANNEL_MASK}</td><td>Integer</td><td>optional, a mask of audio channel assignments</td></tr> 90 * <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> 91 * </table> 92 * 93 * Subtitle formats have the following keys: 94 * <table> 95 * <tr><td>{@link #KEY_MIME}</td><td>String</td><td>The type of the format.</td></tr> 96 * <tr><td>{@link #KEY_LANGUAGE}</td><td>String</td><td>The language of the content.</td></tr> 97 * </table> 98 */ 99 public final class MediaFormat { 100 public static final String MIMETYPE_VIDEO_VP8 = "video/x-vnd.on2.vp8"; 101 public static final String MIMETYPE_VIDEO_VP9 = "video/x-vnd.on2.vp9"; 102 public static final String MIMETYPE_VIDEO_AVC = "video/avc"; 103 public static final String MIMETYPE_VIDEO_HEVC = "video/hevc"; 104 public static final String MIMETYPE_VIDEO_MPEG4 = "video/mp4v-es"; 105 public static final String MIMETYPE_VIDEO_H263 = "video/3gpp"; 106 public static final String MIMETYPE_VIDEO_MPEG2 = "video/mpeg2"; 107 public static final String MIMETYPE_VIDEO_RAW = "video/raw"; 108 109 public static final String MIMETYPE_AUDIO_AMR_NB = "audio/3gpp"; 110 public static final String MIMETYPE_AUDIO_AMR_WB = "audio/amr-wb"; 111 public static final String MIMETYPE_AUDIO_MPEG = "audio/mpeg"; 112 public static final String MIMETYPE_AUDIO_AAC = "audio/mp4a-latm"; 113 public static final String MIMETYPE_AUDIO_QCELP = "audio/qcelp"; 114 public static final String MIMETYPE_AUDIO_VORBIS = "audio/vorbis"; 115 public static final String MIMETYPE_AUDIO_OPUS = "audio/opus"; 116 public static final String MIMETYPE_AUDIO_G711_ALAW = "audio/g711-alaw"; 117 public static final String MIMETYPE_AUDIO_G711_MLAW = "audio/g711-mlaw"; 118 public static final String MIMETYPE_AUDIO_RAW = "audio/raw"; 119 public static final String MIMETYPE_AUDIO_FLAC = "audio/flac"; 120 public static final String MIMETYPE_AUDIO_MSGSM = "audio/gsm"; 121 public static final String MIMETYPE_AUDIO_AC3 = "audio/ac3"; 122 public static final String MIMETYPE_AUDIO_EAC3 = "audio/eac3"; 123 public static final String MIMETYPE_VIDEO_DOLBY_VISION = "video/dolby-vision"; 124 125 /** 126 * MIME type for WebVTT subtitle data. 127 */ 128 public static final String MIMETYPE_TEXT_VTT = "text/vtt"; 129 130 /** 131 * MIME type for CEA-608 closed caption data. 132 */ 133 public static final String MIMETYPE_TEXT_CEA_608 = "text/cea-608"; 134 135 private Map<String, Object> mMap; 136 137 /** 138 * A key describing the mime type of the MediaFormat. 139 * The associated value is a string. 140 */ 141 public static final String KEY_MIME = "mime"; 142 143 /** 144 * A key describing the language of the content, using either ISO 639-1 145 * or 639-2/T codes. The associated value is a string. 146 */ 147 public static final String KEY_LANGUAGE = "language"; 148 149 /** 150 * A key describing the sample rate of an audio format. 151 * The associated value is an integer 152 */ 153 public static final String KEY_SAMPLE_RATE = "sample-rate"; 154 155 /** 156 * A key describing the number of channels in an audio format. 157 * The associated value is an integer 158 */ 159 public static final String KEY_CHANNEL_COUNT = "channel-count"; 160 161 /** 162 * A key describing the width of the content in a video format. 163 * The associated value is an integer 164 */ 165 public static final String KEY_WIDTH = "width"; 166 167 /** 168 * A key describing the height of the content in a video format. 169 * The associated value is an integer 170 */ 171 public static final String KEY_HEIGHT = "height"; 172 173 /** 174 * A key describing the maximum expected width of the content in a video 175 * decoder format, in case there are resolution changes in the video content. 176 * The associated value is an integer 177 */ 178 public static final String KEY_MAX_WIDTH = "max-width"; 179 180 /** 181 * A key describing the maximum expected height of the content in a video 182 * decoder format, in case there are resolution changes in the video content. 183 * The associated value is an integer 184 */ 185 public static final String KEY_MAX_HEIGHT = "max-height"; 186 187 /** A key describing the maximum size in bytes of a buffer of data 188 * described by this MediaFormat. 189 * The associated value is an integer 190 */ 191 public static final String KEY_MAX_INPUT_SIZE = "max-input-size"; 192 193 /** 194 * A key describing the average bitrate in bits/sec. 195 * The associated value is an integer 196 */ 197 public static final String KEY_BIT_RATE = "bitrate"; 198 199 /** 200 * A key describing the max bitrate in bits/sec. 201 * This is usually over a one-second sliding window (e.g. over any window of one second). 202 * The associated value is an integer 203 * @hide 204 */ 205 public static final String KEY_MAX_BIT_RATE = "max-bitrate"; 206 207 /** 208 * A key describing the color format of the content in a video format. 209 * Constants are declared in {@link android.media.MediaCodecInfo.CodecCapabilities}. 210 */ 211 public static final String KEY_COLOR_FORMAT = "color-format"; 212 213 /** 214 * A key describing the frame rate of a video format in frames/sec. 215 * The associated value is normally an integer when the value is used by the platform, 216 * but video codecs also accept float configuration values. 217 * Specifically, {@link MediaExtractor#getTrackFormat MediaExtractor} provides an integer 218 * value corresponding to the frame rate information of the track if specified and non-zero. 219 * Otherwise, this key is not present. {@link MediaCodec#configure MediaCodec} accepts both 220 * float and integer values. This represents the desired operating frame rate if the 221 * {@link #KEY_OPERATING_RATE} is not present and {@link #KEY_PRIORITY} is {@code 0} 222 * (realtime). For video encoders this value corresponds to the intended frame rate, 223 * although encoders are expected 224 * to support variable frame rate based on {@link MediaCodec.BufferInfo#presentationTimeUs 225 * buffer timestamp}. This key is not used in the {@code MediaCodec} 226 * {@link MediaCodec#getInputFormat input}/{@link MediaCodec#getOutputFormat output} formats, 227 * nor by {@link MediaMuxer#addTrack MediaMuxer}. 228 */ 229 public static final String KEY_FRAME_RATE = "frame-rate"; 230 231 /** 232 * A key describing the raw audio sample encoding/format. 233 * 234 * <p>The associated value is an integer, using one of the 235 * {@link AudioFormat}.ENCODING_PCM_ values.</p> 236 * 237 * <p>This is an optional key for audio decoders and encoders specifying the 238 * desired raw audio sample format during {@link MediaCodec#configure 239 * MediaCodec.configure(…)} call. Use {@link MediaCodec#getInputFormat 240 * MediaCodec.getInput}/{@link MediaCodec#getOutputFormat OutputFormat(…)} 241 * to confirm the actual format. For the PCM decoder this key specifies both 242 * input and output sample encodings.</p> 243 * 244 * <p>This key is also used by {@link MediaExtractor} to specify the sample 245 * format of audio data, if it is specified.</p> 246 * 247 * <p>If this key is missing, the raw audio sample format is signed 16-bit short.</p> 248 */ 249 public static final String KEY_PCM_ENCODING = "pcm-encoding"; 250 251 /** 252 * A key describing the capture rate of a video format in frames/sec. 253 * <p> 254 * When capture rate is different than the frame rate, it means that the 255 * video is acquired at a different rate than the playback, which produces 256 * slow motion or timelapse effect during playback. Application can use the 257 * value of this key to tell the relative speed ratio between capture and 258 * playback rates when the video was recorded. 259 * </p> 260 * <p> 261 * The associated value is an integer or a float. 262 * </p> 263 */ 264 public static final String KEY_CAPTURE_RATE = "capture-rate"; 265 266 /** 267 * A key describing the frequency of key frames expressed in seconds between key frames. 268 * <p> 269 * This key is used by video encoders. 270 * A negative value means no key frames are requested after the first frame. 271 * A zero value means a stream containing all key frames is requested. 272 * <p class=note> 273 * Most video encoders will convert this value of the number of non-key-frames between 274 * key-frames, using the {@linkplain #KEY_FRAME_RATE frame rate} information; therefore, 275 * if the actual frame rate differs (e.g. input frames are dropped or the frame rate 276 * changes), the <strong>time interval</strong> between key frames will not be the 277 * configured value. 278 * <p> 279 * The associated value is an integer (or float since 280 * {@link android.os.Build.VERSION_CODES#N_MR1}). 281 */ 282 public static final String KEY_I_FRAME_INTERVAL = "i-frame-interval"; 283 284 /** 285 * An optional key describing the period of intra refresh in frames. This is an 286 * optional parameter that applies only to video encoders. If encoder supports it 287 * ({@link MediaCodecInfo.CodecCapabilities#FEATURE_IntraRefresh}), the whole 288 * frame is completely refreshed after the specified period. Also for each frame, 289 * a fix subset of macroblocks must be intra coded which leads to more constant bitrate 290 * than inserting a key frame. This key is recommended for video streaming applications 291 * as it provides low-delay and good error-resilience. This key is ignored if the 292 * video encoder does not support the intra refresh feature. Use the output format to 293 * verify that this feature was enabled. 294 * The associated value is an integer. 295 */ 296 public static final String KEY_INTRA_REFRESH_PERIOD = "intra-refresh-period"; 297 298 /** 299 * A key describing the temporal layering schema. This is an optional parameter 300 * that applies only to video encoders. Use {@link MediaCodec#getOutputFormat} 301 * after {@link MediaCodec#configure configure} to query if the encoder supports 302 * the desired schema. Supported values are {@code webrtc.vp8.N-layer}, 303 * {@code android.generic.N}, {@code android.generic.N+M} and {@code none}, where 304 * {@code N} denotes the total number of non-bidirectional layers (which must be at least 1) 305 * and {@code M} denotes the total number of bidirectional layers (which must be non-negative). 306 * <p class=note>{@code android.generic.*} schemas have been added in {@link 307 * android.os.Build.VERSION_CODES#N_MR1}. 308 * <p> 309 * The encoder may support fewer temporal layers, in which case the output format 310 * will contain the configured schema. If the encoder does not support temporal 311 * layering, the output format will not have an entry with this key. 312 * The associated value is a string. 313 */ 314 public static final String KEY_TEMPORAL_LAYERING = "ts-schema"; 315 316 /** 317 * A key describing the stride of the video bytebuffer layout. 318 * Stride (or row increment) is the difference between the index of a pixel 319 * and that of the pixel directly underneath. For YUV 420 formats, the 320 * stride corresponds to the Y plane; the stride of the U and V planes can 321 * be calculated based on the color format, though it is generally undefined 322 * and depends on the device and release. 323 * The associated value is an integer, representing number of bytes. 324 */ 325 public static final String KEY_STRIDE = "stride"; 326 327 /** 328 * A key describing the plane height of a multi-planar (YUV) video bytebuffer layout. 329 * Slice height (or plane height/vertical stride) is the number of rows that must be skipped 330 * to get from the top of the Y plane to the top of the U plane in the bytebuffer. In essence 331 * the offset of the U plane is sliceHeight * stride. The height of the U/V planes 332 * can be calculated based on the color format, though it is generally undefined 333 * and depends on the device and release. 334 * The associated value is an integer, representing number of rows. 335 */ 336 public static final String KEY_SLICE_HEIGHT = "slice-height"; 337 338 /** 339 * Applies only when configuring a video encoder in "surface-input" mode. 340 * The associated value is a long and gives the time in microseconds 341 * after which the frame previously submitted to the encoder will be 342 * repeated (once) if no new frame became available since. 343 */ 344 public static final String KEY_REPEAT_PREVIOUS_FRAME_AFTER 345 = "repeat-previous-frame-after"; 346 347 /** 348 * If specified when configuring a video decoder rendering to a surface, 349 * causes the decoder to output "blank", i.e. black frames to the surface 350 * when stopped to clear out any previously displayed contents. 351 * The associated value is an integer of value 1. 352 */ 353 public static final String KEY_PUSH_BLANK_BUFFERS_ON_STOP 354 = "push-blank-buffers-on-shutdown"; 355 356 /** 357 * A key describing the duration (in microseconds) of the content. 358 * The associated value is a long. 359 */ 360 public static final String KEY_DURATION = "durationUs"; 361 362 /** 363 * A key mapping to a value of 1 if the content is AAC audio and 364 * audio frames are prefixed with an ADTS header. 365 * The associated value is an integer (0 or 1). 366 * This key is only supported when _decoding_ content, it cannot 367 * be used to configure an encoder to emit ADTS output. 368 */ 369 public static final String KEY_IS_ADTS = "is-adts"; 370 371 /** 372 * A key describing the channel composition of audio content. This mask 373 * is composed of bits drawn from channel mask definitions in {@link android.media.AudioFormat}. 374 * The associated value is an integer. 375 */ 376 public static final String KEY_CHANNEL_MASK = "channel-mask"; 377 378 /** 379 * A key describing the AAC profile to be used (AAC audio formats only). 380 * Constants are declared in {@link android.media.MediaCodecInfo.CodecProfileLevel}. 381 */ 382 public static final String KEY_AAC_PROFILE = "aac-profile"; 383 384 /** 385 * A key describing the AAC SBR mode to be used (AAC audio formats only). 386 * The associated value is an integer and can be set to following values: 387 * <ul> 388 * <li>0 - no SBR should be applied</li> 389 * <li>1 - single rate SBR</li> 390 * <li>2 - double rate SBR</li> 391 * </ul> 392 * Note: If this key is not defined the default SRB mode for the desired AAC profile will 393 * be used. 394 * <p>This key is only used during encoding. 395 */ 396 public static final String KEY_AAC_SBR_MODE = "aac-sbr-mode"; 397 398 /** 399 * A key describing the maximum number of channels that can be output by the AAC decoder. 400 * By default, the decoder will output the same number of channels as present in the encoded 401 * stream, if supported. Set this value to limit the number of output channels, and use 402 * the downmix information in the stream, if available. 403 * <p>Values larger than the number of channels in the content to decode are ignored. 404 * <p>This key is only used during decoding. 405 */ 406 public static final String KEY_AAC_MAX_OUTPUT_CHANNEL_COUNT = "aac-max-output-channel_count"; 407 408 /** 409 * A key describing a gain to be applied so that the output loudness matches the 410 * Target Reference Level. This is typically used to normalize loudness across program items. 411 * The gain is derived as the difference between the Target Reference Level and the 412 * Program Reference Level. The latter can be given in the bitstream and indicates the actual 413 * loudness value of the program item. 414 * <p>The value is given as an integer value between 415 * 0 and 127, and is calculated as -0.25 * Target Reference Level in dBFS. 416 * Therefore, it represents the range of Full Scale (0 dBFS) to -31.75 dBFS. 417 * <p>This key is only used during decoding. 418 */ 419 public static final String KEY_AAC_DRC_TARGET_REFERENCE_LEVEL = "aac-target-ref-level"; 420 421 /** 422 * A key describing the target reference level that was assumed at the encoder for 423 * calculation of attenuation gains for clipping prevention. This information can be provided 424 * if it is known, otherwise a worst-case assumption is used. 425 * <p>The value is given as an integer value between 426 * 0 and 127, and is calculated as -0.25 * Target Reference Level in dBFS. 427 * Therefore, it represents the range of Full Scale (0 dBFS) to -31.75 dBFS. 428 * The default value is the worst-case assumption of 127. 429 * <p>The value is ignored when heavy compression is used (see 430 * {@link #KEY_AAC_DRC_HEAVY_COMPRESSION}). 431 * <p>This key is only used during decoding. 432 */ 433 public static final String KEY_AAC_ENCODED_TARGET_LEVEL = "aac-encoded-target-level"; 434 435 /** 436 * A key describing the boost factor allowing to adapt the dynamics of the output to the 437 * actual listening requirements. This relies on DRC gain sequences that can be transmitted in 438 * the encoded bitstream to be able to reduce the dynamics of the output signal upon request. 439 * This factor enables the user to select how much of the gains are applied. 440 * <p>Positive gains (boost) and negative gains (attenuation, see 441 * {@link #KEY_AAC_DRC_ATTENUATION_FACTOR}) can be controlled separately for a better match 442 * to different use-cases. 443 * <p>Typically, attenuation gains are sent for loud signal segments, and boost gains are sent 444 * for soft signal segments. If the output is listened to in a noisy environment, for example, 445 * the boost factor is used to enable the positive gains, i.e. to amplify soft signal segments 446 * beyond the noise floor. But for listening late at night, the attenuation 447 * factor is used to enable the negative gains, to prevent loud signal from surprising 448 * the listener. In applications which generally need a low dynamic range, both the boost factor 449 * and the attenuation factor are used in order to enable all DRC gains. 450 * <p>In order to prevent clipping, it is also recommended to apply the attenuation factors 451 * in case of a downmix and/or loudness normalization to high target reference levels. 452 * <p>Both the boost and the attenuation factor parameters are given as integer values 453 * between 0 and 127, representing the range of the factor of 0 (i.e. don't apply) 454 * to 1 (i.e. fully apply boost/attenuation factors respectively). 455 * <p>This key is only used during decoding. 456 */ 457 public static final String KEY_AAC_DRC_BOOST_FACTOR = "aac-drc-boost-level"; 458 459 /** 460 * A key describing the attenuation factor allowing to adapt the dynamics of the output to the 461 * actual listening requirements. 462 * See {@link #KEY_AAC_DRC_BOOST_FACTOR} for a description of the role of this attenuation 463 * factor and the value range. 464 * <p>This key is only used during decoding. 465 */ 466 public static final String KEY_AAC_DRC_ATTENUATION_FACTOR = "aac-drc-cut-level"; 467 468 /** 469 * A key describing the selection of the heavy compression profile for DRC. 470 * Two separate DRC gain sequences can be transmitted in one bitstream: MPEG-4 DRC light 471 * compression, and DVB-specific heavy compression. When selecting the application of the heavy 472 * compression, one of the sequences is selected: 473 * <ul> 474 * <li>0 enables light compression,</li> 475 * <li>1 enables heavy compression instead. 476 * </ul> 477 * Note that only light compression offers the features of scaling of DRC gains 478 * (see {@link #KEY_AAC_DRC_BOOST_FACTOR} and {@link #KEY_AAC_DRC_ATTENUATION_FACTOR} for the 479 * boost and attenuation factors, and frequency-selective (multiband) DRC. 480 * Light compression usually contains clipping prevention for stereo downmixing while heavy 481 * compression, if additionally provided in the bitstream, is usually stronger, and contains 482 * clipping prevention for stereo and mono downmixing. 483 * <p>The default is light compression. 484 * <p>This key is only used during decoding. 485 */ 486 public static final String KEY_AAC_DRC_HEAVY_COMPRESSION = "aac-drc-heavy-compression"; 487 488 /** 489 * A key describing the FLAC compression level to be used (FLAC audio format only). 490 * The associated value is an integer ranging from 0 (fastest, least compression) 491 * to 8 (slowest, most compression). 492 */ 493 public static final String KEY_FLAC_COMPRESSION_LEVEL = "flac-compression-level"; 494 495 /** 496 * A key describing the encoding complexity. 497 * The associated value is an integer. These values are device and codec specific, 498 * but lower values generally result in faster and/or less power-hungry encoding. 499 * 500 * @see MediaCodecInfo.EncoderCapabilities#getComplexityRange() 501 */ 502 public static final String KEY_COMPLEXITY = "complexity"; 503 504 /** 505 * A key describing the desired encoding quality. 506 * The associated value is an integer. This key is only supported for encoders 507 * that are configured in constant-quality mode. These values are device and 508 * codec specific, but lower values generally result in more efficient 509 * (smaller-sized) encoding. 510 * 511 * @hide 512 * 513 * @see MediaCodecInfo.EncoderCapabilities#getQualityRange() 514 */ 515 public static final String KEY_QUALITY = "quality"; 516 517 /** 518 * A key describing the desired codec priority. 519 * <p> 520 * The associated value is an integer. Higher value means lower priority. 521 * <p> 522 * Currently, only two levels are supported:<br> 523 * 0: realtime priority - meaning that the codec shall support the given 524 * performance configuration (e.g. framerate) at realtime. This should 525 * only be used by media playback, capture, and possibly by realtime 526 * communication scenarios if best effort performance is not suitable.<br> 527 * 1: non-realtime priority (best effort). 528 * <p> 529 * This is a hint used at codec configuration and resource planning - to understand 530 * the realtime requirements of the application; however, due to the nature of 531 * media components, performance is not guaranteed. 532 * 533 */ 534 public static final String KEY_PRIORITY = "priority"; 535 536 /** 537 * A key describing the desired operating frame rate for video or sample rate for audio 538 * that the codec will need to operate at. 539 * <p> 540 * The associated value is an integer or a float representing frames-per-second or 541 * samples-per-second 542 * <p> 543 * This is used for cases like high-speed/slow-motion video capture, where the video encoder 544 * format contains the target playback rate (e.g. 30fps), but the component must be able to 545 * handle the high operating capture rate (e.g. 240fps). 546 * <p> 547 * This rate will be used by codec for resource planning and setting the operating points. 548 * 549 */ 550 public static final String KEY_OPERATING_RATE = "operating-rate"; 551 552 /** 553 * A key describing the desired profile to be used by an encoder. 554 * The associated value is an integer. 555 * Constants are declared in {@link MediaCodecInfo.CodecProfileLevel}. 556 * This key is used as a hint, and is only supported for codecs 557 * that specify a profile. Note: Codecs are free to use all the available 558 * coding tools at the specified profile. 559 * 560 * @see MediaCodecInfo.CodecCapabilities#profileLevels 561 */ 562 public static final String KEY_PROFILE = "profile"; 563 564 /** 565 * A key describing the desired profile to be used by an encoder. 566 * The associated value is an integer. 567 * Constants are declared in {@link MediaCodecInfo.CodecProfileLevel}. 568 * This key is used as a further hint when specifying a desired profile, 569 * and is only supported for codecs that specify a level. 570 * <p> 571 * This key is ignored if the {@link #KEY_PROFILE profile} is not specified. 572 * 573 * @see MediaCodecInfo.CodecCapabilities#profileLevels 574 */ 575 public static final String KEY_LEVEL = "level"; 576 577 /** 578 * A key describing the desired clockwise rotation on an output surface. 579 * This key is only used when the codec is configured using an output surface. 580 * The associated value is an integer, representing degrees. Supported values 581 * are 0, 90, 180 or 270. This is an optional field; if not specified, rotation 582 * defaults to 0. 583 * 584 * @see MediaCodecInfo.CodecCapabilities#profileLevels 585 */ 586 public static final String KEY_ROTATION = "rotation-degrees"; 587 588 /** 589 * A key describing the desired bitrate mode to be used by an encoder. 590 * Constants are declared in {@link MediaCodecInfo.CodecCapabilities}. 591 * 592 * @see MediaCodecInfo.EncoderCapabilities#isBitrateModeSupported(int) 593 */ 594 public static final String KEY_BITRATE_MODE = "bitrate-mode"; 595 596 /** 597 * A key describing the audio session ID of the AudioTrack associated 598 * to a tunneled video codec. 599 * The associated value is an integer. 600 * 601 * @see MediaCodecInfo.CodecCapabilities#FEATURE_TunneledPlayback 602 */ 603 public static final String KEY_AUDIO_SESSION_ID = "audio-session-id"; 604 605 /** 606 * A key for boolean AUTOSELECT behavior for the track. Tracks with AUTOSELECT=true 607 * are considered when automatically selecting a track without specific user 608 * choice, based on the current locale. 609 * This is currently only used for subtitle tracks, when the user selected 610 * 'Default' for the captioning locale. 611 * The associated value is an integer, where non-0 means TRUE. This is an optional 612 * field; if not specified, AUTOSELECT defaults to TRUE. 613 */ 614 public static final String KEY_IS_AUTOSELECT = "is-autoselect"; 615 616 /** 617 * A key for boolean DEFAULT behavior for the track. The track with DEFAULT=true is 618 * selected in the absence of a specific user choice. 619 * This is currently only used for subtitle tracks, when the user selected 620 * 'Default' for the captioning locale. 621 * The associated value is an integer, where non-0 means TRUE. This is an optional 622 * field; if not specified, DEFAULT is considered to be FALSE. 623 */ 624 public static final String KEY_IS_DEFAULT = "is-default"; 625 626 627 /** 628 * A key for the FORCED field for subtitle tracks. True if it is a 629 * forced subtitle track. Forced subtitle tracks are essential for the 630 * content and are shown even when the user turns off Captions. They 631 * are used for example to translate foreign/alien dialogs or signs. 632 * The associated value is an integer, where non-0 means TRUE. This is an 633 * optional field; if not specified, FORCED defaults to FALSE. 634 */ 635 public static final String KEY_IS_FORCED_SUBTITLE = "is-forced-subtitle"; 636 637 /** @hide */ 638 public static final String KEY_IS_TIMED_TEXT = "is-timed-text"; 639 640 // The following color aspect values must be in sync with the ones in HardwareAPI.h. 641 /** 642 * An optional key describing the color primaries, white point and 643 * luminance factors for video content. 644 * 645 * The associated value is an integer: 0 if unspecified, or one of the 646 * COLOR_STANDARD_ values. 647 */ 648 public static final String KEY_COLOR_STANDARD = "color-standard"; 649 650 /** BT.709 color chromacity coordinates with KR = 0.2126, KB = 0.0722. */ 651 public static final int COLOR_STANDARD_BT709 = 1; 652 653 /** BT.601 625 color chromacity coordinates with KR = 0.299, KB = 0.114. */ 654 public static final int COLOR_STANDARD_BT601_PAL = 2; 655 656 /** BT.601 525 color chromacity coordinates with KR = 0.299, KB = 0.114. */ 657 public static final int COLOR_STANDARD_BT601_NTSC = 4; 658 659 /** BT.2020 color chromacity coordinates with KR = 0.2627, KB = 0.0593. */ 660 public static final int COLOR_STANDARD_BT2020 = 6; 661 662 /** @hide */ 663 @IntDef({ 664 COLOR_STANDARD_BT709, 665 COLOR_STANDARD_BT601_PAL, 666 COLOR_STANDARD_BT601_NTSC, 667 COLOR_STANDARD_BT2020, 668 }) 669 @Retention(RetentionPolicy.SOURCE) 670 public @interface ColorStandard {} 671 672 /** 673 * An optional key describing the opto-electronic transfer function used 674 * for the video content. 675 * 676 * The associated value is an integer: 0 if unspecified, or one of the 677 * COLOR_TRANSFER_ values. 678 */ 679 public static final String KEY_COLOR_TRANSFER = "color-transfer"; 680 681 /** Linear transfer characteristic curve. */ 682 public static final int COLOR_TRANSFER_LINEAR = 1; 683 684 /** SMPTE 170M transfer characteristic curve used by BT.601/BT.709/BT.2020. This is the curve 685 * used by most non-HDR video content. */ 686 public static final int COLOR_TRANSFER_SDR_VIDEO = 3; 687 688 /** SMPTE ST 2084 transfer function. This is used by some HDR video content. */ 689 public static final int COLOR_TRANSFER_ST2084 = 6; 690 691 /** ARIB STD-B67 hybrid-log-gamma transfer function. This is used by some HDR video content. */ 692 public static final int COLOR_TRANSFER_HLG = 7; 693 694 /** @hide */ 695 @IntDef({ 696 COLOR_TRANSFER_LINEAR, 697 COLOR_TRANSFER_SDR_VIDEO, 698 COLOR_TRANSFER_ST2084, 699 COLOR_TRANSFER_HLG, 700 }) 701 @Retention(RetentionPolicy.SOURCE) 702 public @interface ColorTransfer {} 703 704 /** 705 * An optional key describing the range of the component values of the video content. 706 * 707 * The associated value is an integer: 0 if unspecified, or one of the 708 * COLOR_RANGE_ values. 709 */ 710 public static final String KEY_COLOR_RANGE = "color-range"; 711 712 /** Limited range. Y component values range from 16 to 235 for 8-bit content. 713 * Cr, Cy values range from 16 to 240 for 8-bit content. 714 * This is the default for video content. */ 715 public static final int COLOR_RANGE_LIMITED = 2; 716 717 /** Full range. Y, Cr and Cb component values range from 0 to 255 for 8-bit content. */ 718 public static final int COLOR_RANGE_FULL = 1; 719 720 /** @hide */ 721 @IntDef({ 722 COLOR_RANGE_LIMITED, 723 COLOR_RANGE_FULL, 724 }) 725 @Retention(RetentionPolicy.SOURCE) 726 public @interface ColorRange {} 727 728 /** 729 * An optional key describing the static metadata of HDR (high-dynamic-range) video content. 730 * 731 * The associated value is a ByteBuffer. This buffer contains the raw contents of the 732 * Static Metadata Descriptor (including the descriptor ID) of an HDMI Dynamic Range and 733 * Mastering InfoFrame as defined by CTA-861.3. This key must be provided to video decoders 734 * for HDR video content unless this information is contained in the bitstream and the video 735 * decoder supports an HDR-capable profile. This key must be provided to video encoders for 736 * HDR video content. 737 */ 738 public static final String KEY_HDR_STATIC_INFO = "hdr-static-info"; 739 740 /** 741 * A key describing a unique ID for the content of a media track. 742 * 743 * <p>This key is used by {@link MediaExtractor}. Some extractors provide multiple encodings 744 * of the same track (e.g. float audio tracks for FLAC and WAV may be expressed as two 745 * tracks via MediaExtractor: a normal PCM track for backward compatibility, and a float PCM 746 * track for added fidelity. Similarly, Dolby Vision extractor may provide a baseline SDR 747 * version of a DV track.) This key can be used to identify which MediaExtractor tracks refer 748 * to the same underlying content. 749 * </p> 750 * 751 * The associated value is an integer. 752 */ 753 public static final String KEY_TRACK_ID = "track-id"; 754 MediaFormat(Map<String, Object> map)755 /* package private */ MediaFormat(Map<String, Object> map) { 756 mMap = map; 757 } 758 759 /** 760 * Creates an empty MediaFormat 761 */ MediaFormat()762 public MediaFormat() { 763 mMap = new HashMap(); 764 } 765 getMap()766 /* package private */ Map<String, Object> getMap() { 767 return mMap; 768 } 769 770 /** 771 * Returns true iff a key of the given name exists in the format. 772 */ containsKey(String name)773 public final boolean containsKey(String name) { 774 return mMap.containsKey(name); 775 } 776 777 /** 778 * A key prefix used together with a {@link MediaCodecInfo.CodecCapabilities} 779 * feature name describing a required or optional feature for a codec capabilities 780 * query. 781 * The associated value is an integer, where non-0 value means the feature is 782 * requested to be present, while 0 value means the feature is requested to be not 783 * present. 784 * @see MediaCodecList#findDecoderForFormat 785 * @see MediaCodecList#findEncoderForFormat 786 * @see MediaCodecInfo.CodecCapabilities#isFormatSupported 787 * 788 * @hide 789 */ 790 public static final String KEY_FEATURE_ = "feature-"; 791 792 /** 793 * Returns the value of an integer key. 794 */ getInteger(String name)795 public final int getInteger(String name) { 796 return ((Integer)mMap.get(name)).intValue(); 797 } 798 799 /** 800 * Returns the value of an integer key, or the default value if the 801 * key is missing or is for another type value. 802 * @hide 803 */ getInteger(String name, int defaultValue)804 public final int getInteger(String name, int defaultValue) { 805 try { 806 return getInteger(name); 807 } 808 catch (NullPointerException e) { /* no such field */ } 809 catch (ClassCastException e) { /* field of different type */ } 810 return defaultValue; 811 } 812 813 /** 814 * Returns the value of a long key. 815 */ getLong(String name)816 public final long getLong(String name) { 817 return ((Long)mMap.get(name)).longValue(); 818 } 819 820 /** 821 * Returns the value of a float key. 822 */ getFloat(String name)823 public final float getFloat(String name) { 824 return ((Float)mMap.get(name)).floatValue(); 825 } 826 827 /** 828 * Returns the value of a string key. 829 */ getString(String name)830 public final String getString(String name) { 831 return (String)mMap.get(name); 832 } 833 834 /** 835 * Returns the value of a ByteBuffer key. 836 */ getByteBuffer(String name)837 public final ByteBuffer getByteBuffer(String name) { 838 return (ByteBuffer)mMap.get(name); 839 } 840 841 /** 842 * Returns whether a feature is to be enabled ({@code true}) or disabled 843 * ({@code false}). 844 * 845 * @param feature the name of a {@link MediaCodecInfo.CodecCapabilities} feature. 846 * 847 * @throws IllegalArgumentException if the feature was neither set to be enabled 848 * nor to be disabled. 849 */ getFeatureEnabled(String feature)850 public boolean getFeatureEnabled(String feature) { 851 Integer enabled = (Integer)mMap.get(KEY_FEATURE_ + feature); 852 if (enabled == null) { 853 throw new IllegalArgumentException("feature is not specified"); 854 } 855 return enabled != 0; 856 } 857 858 /** 859 * Sets the value of an integer key. 860 */ setInteger(String name, int value)861 public final void setInteger(String name, int value) { 862 mMap.put(name, Integer.valueOf(value)); 863 } 864 865 /** 866 * Sets the value of a long key. 867 */ setLong(String name, long value)868 public final void setLong(String name, long value) { 869 mMap.put(name, Long.valueOf(value)); 870 } 871 872 /** 873 * Sets the value of a float key. 874 */ setFloat(String name, float value)875 public final void setFloat(String name, float value) { 876 mMap.put(name, new Float(value)); 877 } 878 879 /** 880 * Sets the value of a string key. 881 */ setString(String name, String value)882 public final void setString(String name, String value) { 883 mMap.put(name, value); 884 } 885 886 /** 887 * Sets the value of a ByteBuffer key. 888 */ setByteBuffer(String name, ByteBuffer bytes)889 public final void setByteBuffer(String name, ByteBuffer bytes) { 890 mMap.put(name, bytes); 891 } 892 893 /** 894 * Sets whether a feature is to be enabled ({@code true}) or disabled 895 * ({@code false}). 896 * 897 * If {@code enabled} is {@code true}, the feature is requested to be present. 898 * Otherwise, the feature is requested to be not present. 899 * 900 * @param feature the name of a {@link MediaCodecInfo.CodecCapabilities} feature. 901 * 902 * @see MediaCodecList#findDecoderForFormat 903 * @see MediaCodecList#findEncoderForFormat 904 * @see MediaCodecInfo.CodecCapabilities#isFormatSupported 905 */ setFeatureEnabled(String feature, boolean enabled)906 public void setFeatureEnabled(String feature, boolean enabled) { 907 setInteger(KEY_FEATURE_ + feature, enabled ? 1 : 0); 908 } 909 910 /** 911 * Creates a minimal audio format. 912 * @param mime The mime type of the content. 913 * @param sampleRate The sampling rate of the content. 914 * @param channelCount The number of audio channels in the content. 915 */ createAudioFormat( String mime, int sampleRate, int channelCount)916 public static final MediaFormat createAudioFormat( 917 String mime, 918 int sampleRate, 919 int channelCount) { 920 MediaFormat format = new MediaFormat(); 921 format.setString(KEY_MIME, mime); 922 format.setInteger(KEY_SAMPLE_RATE, sampleRate); 923 format.setInteger(KEY_CHANNEL_COUNT, channelCount); 924 925 return format; 926 } 927 928 /** 929 * Creates a minimal subtitle format. 930 * @param mime The mime type of the content. 931 * @param language The language of the content, using either ISO 639-1 or 639-2/T 932 * codes. Specify null or "und" if language information is only included 933 * in the content. (This will also work if there are multiple language 934 * tracks in the content.) 935 */ createSubtitleFormat( String mime, String language)936 public static final MediaFormat createSubtitleFormat( 937 String mime, 938 String language) { 939 MediaFormat format = new MediaFormat(); 940 format.setString(KEY_MIME, mime); 941 format.setString(KEY_LANGUAGE, language); 942 943 return format; 944 } 945 946 /** 947 * Creates a minimal video format. 948 * @param mime The mime type of the content. 949 * @param width The width of the content (in pixels) 950 * @param height The height of the content (in pixels) 951 */ createVideoFormat( String mime, int width, int height)952 public static final MediaFormat createVideoFormat( 953 String mime, 954 int width, 955 int height) { 956 MediaFormat format = new MediaFormat(); 957 format.setString(KEY_MIME, mime); 958 format.setInteger(KEY_WIDTH, width); 959 format.setInteger(KEY_HEIGHT, height); 960 961 return format; 962 } 963 964 @Override toString()965 public String toString() { 966 return mMap.toString(); 967 } 968 } 969