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 java.nio.ByteBuffer; 20 import java.util.HashMap; 21 import java.util.Map; 22 23 /** 24 * Encapsulates the information describing the format of media data, 25 * be it audio or video. 26 * 27 * The format of the media data is specified as string/value pairs. 28 * 29 * Keys common to all audio/video formats, <b>all keys not marked optional are mandatory</b>: 30 * 31 * <table> 32 * <tr><th>Name</th><th>Value Type</th><th>Description</th></tr> 33 * <tr><td>{@link #KEY_MIME}</td><td>String</td><td>The type of the format.</td></tr> 34 * <tr><td>{@link #KEY_MAX_INPUT_SIZE}</td><td>Integer</td><td>optional, maximum size of a buffer of input data</td></tr> 35 * <tr><td>{@link #KEY_BIT_RATE}</td><td>Integer</td><td><b>encoder-only</b>, desired bitrate in bits/second</td></tr> 36 * </table> 37 * 38 * Video formats have the following keys: 39 * <table> 40 * <tr><th>Name</th><th>Value Type</th><th>Description</th></tr> 41 * <tr><td>{@link #KEY_WIDTH}</td><td>Integer</td><td></td></tr> 42 * <tr><td>{@link #KEY_HEIGHT}</td><td>Integer</td><td></td></tr> 43 * <tr><td>{@link #KEY_COLOR_FORMAT}</td><td>Integer</td><td>set by the user 44 * for encoders, readable in the output format of decoders</b></td></tr> 45 * <tr><td>{@link #KEY_FRAME_RATE}</td><td>Integer or Float</td><td><b>encoder-only</b></td></tr> 46 * <tr><td>{@link #KEY_I_FRAME_INTERVAL}</td><td>Integer</td><td><b>encoder-only</b></td></tr> 47 * <tr><td>{@link #KEY_MAX_WIDTH}</td><td>Integer</td><td><b>decoder-only</b>, optional, max-resolution width</td></tr> 48 * <tr><td>{@link #KEY_MAX_HEIGHT}</td><td>Integer</td><td><b>decoder-only</b>, optional, max-resolution height</td></tr> 49 * <tr><td>{@link #KEY_REPEAT_PREVIOUS_FRAME_AFTER}</td><td>Long</td><td><b>video encoder in surface-mode only</b></td></tr> 50 * <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> 51 * </table> 52 * Specify both {@link #KEY_MAX_WIDTH} and {@link #KEY_MAX_HEIGHT} to enable 53 * adaptive playback (seamless resolution change) for a video decoder that 54 * supports it ({@link MediaCodecInfo.CodecCapabilities#FEATURE_AdaptivePlayback}). 55 * The values are used as hints for the codec: they are the maximum expected 56 * resolution to prepare for. Depending on codec support, preparing for larger 57 * maximum resolution may require more memory even if that resolution is never 58 * reached. These fields have no effect for codecs that do not support adaptive 59 * playback.<br /><br /> 60 * 61 * Audio formats have the following keys: 62 * <table> 63 * <tr><th>Name</th><th>Value Type</th><th>Description</th></tr> 64 * <tr><td>{@link #KEY_CHANNEL_COUNT}</td><td>Integer</td><td></td></tr> 65 * <tr><td>{@link #KEY_SAMPLE_RATE}</td><td>Integer</td><td></td></tr> 66 * <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> 67 * <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> 68 * <tr><td>{@link #KEY_CHANNEL_MASK}</td><td>Integer</td><td>optional, a mask of audio channel assignments</td></tr> 69 * <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> 70 * </table> 71 * 72 * Subtitle formats have the following keys: 73 * <table> 74 * <tr><td>{@link #KEY_MIME}</td><td>String</td><td>The type of the format.</td></tr> 75 * <tr><td>{@link #KEY_LANGUAGE}</td><td>String</td><td>The language of the content.</td></tr> 76 * </table> 77 */ 78 public final class MediaFormat { 79 private Map<String, Object> mMap; 80 81 /** 82 * A key describing the mime type of the MediaFormat. 83 * The associated value is a string. 84 */ 85 public static final String KEY_MIME = "mime"; 86 87 /** 88 * A key describing the language of the content, using either ISO 639-1 89 * or 639-2/T codes. The associated value is a string. 90 */ 91 public static final String KEY_LANGUAGE = "language"; 92 93 /** 94 * A key describing the sample rate of an audio format. 95 * The associated value is an integer 96 */ 97 public static final String KEY_SAMPLE_RATE = "sample-rate"; 98 99 /** 100 * A key describing the number of channels in an audio format. 101 * The associated value is an integer 102 */ 103 public static final String KEY_CHANNEL_COUNT = "channel-count"; 104 105 /** 106 * A key describing the width of the content in a video format. 107 * The associated value is an integer 108 */ 109 public static final String KEY_WIDTH = "width"; 110 111 /** 112 * A key describing the height of the content in a video format. 113 * The associated value is an integer 114 */ 115 public static final String KEY_HEIGHT = "height"; 116 117 /** 118 * A key describing the maximum expected width of the content in a video 119 * decoder format, in case there are resolution changes in the video content. 120 * The associated value is an integer 121 */ 122 public static final String KEY_MAX_WIDTH = "max-width"; 123 124 /** 125 * A key describing the maximum expected height of the content in a video 126 * decoder format, in case there are resolution changes in the video content. 127 * The associated value is an integer 128 */ 129 public static final String KEY_MAX_HEIGHT = "max-height"; 130 131 /** A key describing the maximum size in bytes of a buffer of data 132 * described by this MediaFormat. 133 * The associated value is an integer 134 */ 135 public static final String KEY_MAX_INPUT_SIZE = "max-input-size"; 136 137 /** 138 * A key describing the bitrate in bits/sec. 139 * The associated value is an integer 140 */ 141 public static final String KEY_BIT_RATE = "bitrate"; 142 143 /** 144 * A key describing the color format of the content in a video format. 145 * Constants are declared in {@link android.media.MediaCodecInfo.CodecCapabilities}. 146 */ 147 public static final String KEY_COLOR_FORMAT = "color-format"; 148 149 /** 150 * A key describing the frame rate of a video format in frames/sec. 151 * The associated value is an integer or a float. 152 */ 153 public static final String KEY_FRAME_RATE = "frame-rate"; 154 155 /** 156 * A key describing the frequency of I frames expressed in secs 157 * between I frames. 158 * The associated value is an integer. 159 */ 160 public static final String KEY_I_FRAME_INTERVAL = "i-frame-interval"; 161 162 /** 163 * @hide 164 */ 165 public static final String KEY_STRIDE = "stride"; 166 /** 167 * @hide 168 */ 169 public static final String KEY_SLICE_HEIGHT = "slice-height"; 170 171 /** 172 * Applies only when configuring a video encoder in "surface-input" mode. 173 * The associated value is a long and gives the time in microseconds 174 * after which the frame previously submitted to the encoder will be 175 * repeated (once) if no new frame became available since. 176 */ 177 public static final String KEY_REPEAT_PREVIOUS_FRAME_AFTER 178 = "repeat-previous-frame-after"; 179 180 /** 181 * If specified when configuring a video decoder rendering to a surface, 182 * causes the decoder to output "blank", i.e. black frames to the surface 183 * when stopped to clear out any previously displayed contents. 184 * The associated value is an integer of value 1. 185 */ 186 public static final String KEY_PUSH_BLANK_BUFFERS_ON_STOP 187 = "push-blank-buffers-on-shutdown"; 188 189 /** 190 * A key describing the duration (in microseconds) of the content. 191 * The associated value is a long. 192 */ 193 public static final String KEY_DURATION = "durationUs"; 194 195 /** 196 * A key mapping to a value of 1 if the content is AAC audio and 197 * audio frames are prefixed with an ADTS header. 198 * The associated value is an integer (0 or 1). 199 * This key is only supported when _decoding_ content, it cannot 200 * be used to configure an encoder to emit ADTS output. 201 */ 202 public static final String KEY_IS_ADTS = "is-adts"; 203 204 /** 205 * A key describing the channel composition of audio content. This mask 206 * is composed of bits drawn from channel mask definitions in {@link android.media.AudioFormat}. 207 * The associated value is an integer. 208 */ 209 public static final String KEY_CHANNEL_MASK = "channel-mask"; 210 211 /** 212 * A key describing the AAC profile to be used (AAC audio formats only). 213 * Constants are declared in {@link android.media.MediaCodecInfo.CodecProfileLevel}. 214 */ 215 public static final String KEY_AAC_PROFILE = "aac-profile"; 216 217 /** 218 * A key describing the FLAC compression level to be used (FLAC audio format only). 219 * The associated value is an integer ranging from 0 (fastest, least compression) 220 * to 8 (slowest, most compression). 221 */ 222 public static final String KEY_FLAC_COMPRESSION_LEVEL = "flac-compression-level"; 223 224 /** 225 * A key for boolean AUTOSELECT behavior for the track. Tracks with AUTOSELECT=true 226 * are considered when automatically selecting a track without specific user 227 * choice, based on the current locale. 228 * This is currently only used for subtitle tracks, when the user selected 229 * 'Default' for the captioning locale. 230 * The associated value is an integer, where non-0 means TRUE. This is an optional 231 * field; if not specified, AUTOSELECT defaults to TRUE. 232 */ 233 public static final String KEY_IS_AUTOSELECT = "is-autoselect"; 234 235 /** 236 * A key for boolean DEFAULT behavior for the track. The track with DEFAULT=true is 237 * selected in the absence of a specific user choice. 238 * This is currently only used for subtitle tracks, when the user selected 239 * 'Default' for the captioning locale. 240 * The associated value is an integer, where non-0 means TRUE. This is an optional 241 * field; if not specified, DEFAULT is considered to be FALSE. 242 */ 243 public static final String KEY_IS_DEFAULT = "is-default"; 244 245 246 /** 247 * A key for the FORCED field for subtitle tracks. True if it is a 248 * forced subtitle track. Forced subtitle tracks are essential for the 249 * content and are shown even when the user turns off Captions. They 250 * are used for example to translate foreign/alien dialogs or signs. 251 * The associated value is an integer, where non-0 means TRUE. This is an 252 * optional field; if not specified, FORCED defaults to FALSE. 253 */ 254 public static final String KEY_IS_FORCED_SUBTITLE = "is-forced-subtitle"; 255 256 /* package private */ MediaFormat(Map<String, Object> map) { 257 mMap = map; 258 } 259 260 /** 261 * Creates an empty MediaFormat 262 */ 263 public MediaFormat() { 264 mMap = new HashMap(); 265 } 266 267 /* package private */ Map<String, Object> getMap() { 268 return mMap; 269 } 270 271 /** 272 * Returns true iff a key of the given name exists in the format. 273 */ 274 public final boolean containsKey(String name) { 275 return mMap.containsKey(name); 276 } 277 278 /** 279 * Returns the value of an integer key. 280 */ 281 public final int getInteger(String name) { 282 return ((Integer)mMap.get(name)).intValue(); 283 } 284 285 /** 286 * Returns the value of an integer key, or the default value if the 287 * key is missing or is for another type value. 288 * @hide 289 */ 290 public final int getInteger(String name, int defaultValue) { 291 try { 292 return getInteger(name); 293 } 294 catch (NullPointerException e) { /* no such field */ } 295 catch (ClassCastException e) { /* field of different type */ } 296 return defaultValue; 297 } 298 299 /** 300 * Returns the value of a long key. 301 */ 302 public final long getLong(String name) { 303 return ((Long)mMap.get(name)).longValue(); 304 } 305 306 /** 307 * Returns the value of a float key. 308 */ 309 public final float getFloat(String name) { 310 return ((Float)mMap.get(name)).floatValue(); 311 } 312 313 /** 314 * Returns the value of a string key. 315 */ 316 public final String getString(String name) { 317 return (String)mMap.get(name); 318 } 319 320 /** 321 * Returns the value of a ByteBuffer key. 322 */ 323 public final ByteBuffer getByteBuffer(String name) { 324 return (ByteBuffer)mMap.get(name); 325 } 326 327 /** 328 * Sets the value of an integer key. 329 */ 330 public final void setInteger(String name, int value) { 331 mMap.put(name, new Integer(value)); 332 } 333 334 /** 335 * Sets the value of a long key. 336 */ 337 public final void setLong(String name, long value) { 338 mMap.put(name, new Long(value)); 339 } 340 341 /** 342 * Sets the value of a float key. 343 */ 344 public final void setFloat(String name, float value) { 345 mMap.put(name, new Float(value)); 346 } 347 348 /** 349 * Sets the value of a string key. 350 */ 351 public final void setString(String name, String value) { 352 mMap.put(name, value); 353 } 354 355 /** 356 * Sets the value of a ByteBuffer key. 357 */ 358 public final void setByteBuffer(String name, ByteBuffer bytes) { 359 mMap.put(name, bytes); 360 } 361 362 /** 363 * Creates a minimal audio format. 364 * @param mime The mime type of the content. 365 * @param sampleRate The sampling rate of the content. 366 * @param channelCount The number of audio channels in the content. 367 */ 368 public static final MediaFormat createAudioFormat( 369 String mime, 370 int sampleRate, 371 int channelCount) { 372 MediaFormat format = new MediaFormat(); 373 format.setString(KEY_MIME, mime); 374 format.setInteger(KEY_SAMPLE_RATE, sampleRate); 375 format.setInteger(KEY_CHANNEL_COUNT, channelCount); 376 377 return format; 378 } 379 380 /** 381 * Creates a minimal subtitle format. 382 * @param mime The mime type of the content. 383 * @param language The language of the content, using either ISO 639-1 or 639-2/T 384 * codes. Specify null or "und" if language information is only included 385 * in the content. (This will also work if there are multiple language 386 * tracks in the content.) 387 */ 388 public static final MediaFormat createSubtitleFormat( 389 String mime, 390 String language) { 391 MediaFormat format = new MediaFormat(); 392 format.setString(KEY_MIME, mime); 393 format.setString(KEY_LANGUAGE, language); 394 395 return format; 396 } 397 398 /** 399 * Creates a minimal video format. 400 * @param mime The mime type of the content. 401 * @param width The width of the content (in pixels) 402 * @param height The height of the content (in pixels) 403 */ 404 public static final MediaFormat createVideoFormat( 405 String mime, 406 int width, 407 int height) { 408 MediaFormat format = new MediaFormat(); 409 format.setString(KEY_MIME, mime); 410 format.setInteger(KEY_WIDTH, width); 411 format.setInteger(KEY_HEIGHT, height); 412 413 return format; 414 } 415 416 @Override 417 public String toString() { 418 return mMap.toString(); 419 } 420 } 421