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