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
17package android.media;
18
19/**
20 * Provides information about a given media codec available on the device. You can
21 * iterate through all codecs available by querying {@link MediaCodecList}. For example,
22 * here's how to find an encoder that supports a given MIME type:
23 * <pre>
24 * private static MediaCodecInfo selectCodec(String mimeType) {
25 *     int numCodecs = MediaCodecList.getCodecCount();
26 *     for (int i = 0; i &lt; numCodecs; i++) {
27 *         MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
28 *
29 *         if (!codecInfo.isEncoder()) {
30 *             continue;
31 *         }
32 *
33 *         String[] types = codecInfo.getSupportedTypes();
34 *         for (int j = 0; j &lt; types.length; j++) {
35 *             if (types[j].equalsIgnoreCase(mimeType)) {
36 *                 return codecInfo;
37 *             }
38 *         }
39 *     }
40 *     return null;
41 * }</pre>
42 *
43 */
44public final class MediaCodecInfo {
45    private int mIndex;
46
47    /* package private */ MediaCodecInfo(int index) {
48        mIndex = index;
49    }
50
51    /**
52     * Retrieve the codec name.
53     */
54    public final String getName() {
55        return MediaCodecList.getCodecName(mIndex);
56    }
57
58    /**
59     * Query if the codec is an encoder.
60     */
61    public final boolean isEncoder() {
62        return MediaCodecList.isEncoder(mIndex);
63    }
64
65    /**
66     * Query the media types supported by the codec.
67     */
68    public final String[] getSupportedTypes() {
69        return MediaCodecList.getSupportedTypes(mIndex);
70    }
71
72    /**
73     * Encapsulates the capabilities of a given codec component.
74     * For example, what profile/level combinations it supports and what colorspaces
75     * it is capable of providing the decoded data in, as well as some
76     * codec-type specific capability flags.
77     * <p>You can get an instance for a given {@link MediaCodecInfo} object with
78     * {@link MediaCodecInfo#getCapabilitiesForType getCapabilitiesForType()}, passing a MIME type.
79     */
80    public static final class CodecCapabilities {
81        // Enumerates supported profile/level combinations as defined
82        // by the type of encoded data. These combinations impose restrictions
83        // on video resolution, bitrate... and limit the available encoder tools
84        // such as B-frame support, arithmetic coding...
85        public CodecProfileLevel[] profileLevels;
86
87        // from OMX_COLOR_FORMATTYPE
88        public final static int COLOR_FormatMonochrome              = 1;
89        public final static int COLOR_Format8bitRGB332              = 2;
90        public final static int COLOR_Format12bitRGB444             = 3;
91        public final static int COLOR_Format16bitARGB4444           = 4;
92        public final static int COLOR_Format16bitARGB1555           = 5;
93        public final static int COLOR_Format16bitRGB565             = 6;
94        public final static int COLOR_Format16bitBGR565             = 7;
95        public final static int COLOR_Format18bitRGB666             = 8;
96        public final static int COLOR_Format18bitARGB1665           = 9;
97        public final static int COLOR_Format19bitARGB1666           = 10;
98        public final static int COLOR_Format24bitRGB888             = 11;
99        public final static int COLOR_Format24bitBGR888             = 12;
100        public final static int COLOR_Format24bitARGB1887           = 13;
101        public final static int COLOR_Format25bitARGB1888           = 14;
102        public final static int COLOR_Format32bitBGRA8888           = 15;
103        public final static int COLOR_Format32bitARGB8888           = 16;
104        public final static int COLOR_FormatYUV411Planar            = 17;
105        public final static int COLOR_FormatYUV411PackedPlanar      = 18;
106        public final static int COLOR_FormatYUV420Planar            = 19;
107        public final static int COLOR_FormatYUV420PackedPlanar      = 20;
108        public final static int COLOR_FormatYUV420SemiPlanar        = 21;
109        public final static int COLOR_FormatYUV422Planar            = 22;
110        public final static int COLOR_FormatYUV422PackedPlanar      = 23;
111        public final static int COLOR_FormatYUV422SemiPlanar        = 24;
112        public final static int COLOR_FormatYCbYCr                  = 25;
113        public final static int COLOR_FormatYCrYCb                  = 26;
114        public final static int COLOR_FormatCbYCrY                  = 27;
115        public final static int COLOR_FormatCrYCbY                  = 28;
116        public final static int COLOR_FormatYUV444Interleaved       = 29;
117        public final static int COLOR_FormatRawBayer8bit            = 30;
118        public final static int COLOR_FormatRawBayer10bit           = 31;
119        public final static int COLOR_FormatRawBayer8bitcompressed  = 32;
120        public final static int COLOR_FormatL2                      = 33;
121        public final static int COLOR_FormatL4                      = 34;
122        public final static int COLOR_FormatL8                      = 35;
123        public final static int COLOR_FormatL16                     = 36;
124        public final static int COLOR_FormatL24                     = 37;
125        public final static int COLOR_FormatL32                     = 38;
126        public final static int COLOR_FormatYUV420PackedSemiPlanar  = 39;
127        public final static int COLOR_FormatYUV422PackedSemiPlanar  = 40;
128        public final static int COLOR_Format18BitBGR666             = 41;
129        public final static int COLOR_Format24BitARGB6666           = 42;
130        public final static int COLOR_Format24BitABGR6666           = 43;
131
132        public final static int COLOR_TI_FormatYUV420PackedSemiPlanar = 0x7f000100;
133        // COLOR_FormatSurface indicates that the data will be a GraphicBuffer metadata reference.
134        // In OMX this is called OMX_COLOR_FormatAndroidOpaque.
135        public final static int COLOR_FormatSurface                   = 0x7F000789;
136        public final static int COLOR_QCOM_FormatYUV420SemiPlanar     = 0x7fa30c00;
137
138        /**
139         * Defined in the OpenMAX IL specs, color format values are drawn from
140         * OMX_COLOR_FORMATTYPE.
141         */
142        public int[] colorFormats;
143
144        private final static int FLAG_SupportsAdaptivePlayback       = (1 << 0);
145        private int flags;
146
147        /**
148         * <b>video decoder only</b>: codec supports seamless resolution changes.
149         */
150        public final static String FEATURE_AdaptivePlayback       = "adaptive-playback";
151
152        /**
153         * Query codec feature capabilities.
154         */
155        public final boolean isFeatureSupported(String name) {
156            if (name.equals(FEATURE_AdaptivePlayback)) {
157                return (flags & FLAG_SupportsAdaptivePlayback) != 0;
158            }
159            return false;
160        }
161    };
162
163    /**
164     * Encapsulates the profiles available for a codec component.
165     * <p>You can get a set of {@link MediaCodecInfo.CodecProfileLevel} objects for a given
166     * {@link MediaCodecInfo} object from the
167     * {@link MediaCodecInfo.CodecCapabilities#profileLevels} field.
168     */
169    public static final class CodecProfileLevel {
170        // from OMX_VIDEO_AVCPROFILETYPE
171        public static final int AVCProfileBaseline = 0x01;
172        public static final int AVCProfileMain     = 0x02;
173        public static final int AVCProfileExtended = 0x04;
174        public static final int AVCProfileHigh     = 0x08;
175        public static final int AVCProfileHigh10   = 0x10;
176        public static final int AVCProfileHigh422  = 0x20;
177        public static final int AVCProfileHigh444  = 0x40;
178
179        // from OMX_VIDEO_AVCLEVELTYPE
180        public static final int AVCLevel1       = 0x01;
181        public static final int AVCLevel1b      = 0x02;
182        public static final int AVCLevel11      = 0x04;
183        public static final int AVCLevel12      = 0x08;
184        public static final int AVCLevel13      = 0x10;
185        public static final int AVCLevel2       = 0x20;
186        public static final int AVCLevel21      = 0x40;
187        public static final int AVCLevel22      = 0x80;
188        public static final int AVCLevel3       = 0x100;
189        public static final int AVCLevel31      = 0x200;
190        public static final int AVCLevel32      = 0x400;
191        public static final int AVCLevel4       = 0x800;
192        public static final int AVCLevel41      = 0x1000;
193        public static final int AVCLevel42      = 0x2000;
194        public static final int AVCLevel5       = 0x4000;
195        public static final int AVCLevel51      = 0x8000;
196
197        // from OMX_VIDEO_H263PROFILETYPE
198        public static final int H263ProfileBaseline             = 0x01;
199        public static final int H263ProfileH320Coding           = 0x02;
200        public static final int H263ProfileBackwardCompatible   = 0x04;
201        public static final int H263ProfileISWV2                = 0x08;
202        public static final int H263ProfileISWV3                = 0x10;
203        public static final int H263ProfileHighCompression      = 0x20;
204        public static final int H263ProfileInternet             = 0x40;
205        public static final int H263ProfileInterlace            = 0x80;
206        public static final int H263ProfileHighLatency          = 0x100;
207
208        // from OMX_VIDEO_H263LEVELTYPE
209        public static final int H263Level10      = 0x01;
210        public static final int H263Level20      = 0x02;
211        public static final int H263Level30      = 0x04;
212        public static final int H263Level40      = 0x08;
213        public static final int H263Level45      = 0x10;
214        public static final int H263Level50      = 0x20;
215        public static final int H263Level60      = 0x40;
216        public static final int H263Level70      = 0x80;
217
218        // from OMX_VIDEO_MPEG4PROFILETYPE
219        public static final int MPEG4ProfileSimple              = 0x01;
220        public static final int MPEG4ProfileSimpleScalable      = 0x02;
221        public static final int MPEG4ProfileCore                = 0x04;
222        public static final int MPEG4ProfileMain                = 0x08;
223        public static final int MPEG4ProfileNbit                = 0x10;
224        public static final int MPEG4ProfileScalableTexture     = 0x20;
225        public static final int MPEG4ProfileSimpleFace          = 0x40;
226        public static final int MPEG4ProfileSimpleFBA           = 0x80;
227        public static final int MPEG4ProfileBasicAnimated       = 0x100;
228        public static final int MPEG4ProfileHybrid              = 0x200;
229        public static final int MPEG4ProfileAdvancedRealTime    = 0x400;
230        public static final int MPEG4ProfileCoreScalable        = 0x800;
231        public static final int MPEG4ProfileAdvancedCoding      = 0x1000;
232        public static final int MPEG4ProfileAdvancedCore        = 0x2000;
233        public static final int MPEG4ProfileAdvancedScalable    = 0x4000;
234        public static final int MPEG4ProfileAdvancedSimple      = 0x8000;
235
236        // from OMX_VIDEO_MPEG4LEVELTYPE
237        public static final int MPEG4Level0      = 0x01;
238        public static final int MPEG4Level0b     = 0x02;
239        public static final int MPEG4Level1      = 0x04;
240        public static final int MPEG4Level2      = 0x08;
241        public static final int MPEG4Level3      = 0x10;
242        public static final int MPEG4Level4      = 0x20;
243        public static final int MPEG4Level4a     = 0x40;
244        public static final int MPEG4Level5      = 0x80;
245
246        // from OMX_AUDIO_AACPROFILETYPE
247        public static final int AACObjectMain       = 1;
248        public static final int AACObjectLC         = 2;
249        public static final int AACObjectSSR        = 3;
250        public static final int AACObjectLTP        = 4;
251        public static final int AACObjectHE         = 5;
252        public static final int AACObjectScalable   = 6;
253        public static final int AACObjectERLC       = 17;
254        public static final int AACObjectLD         = 23;
255        public static final int AACObjectHE_PS      = 29;
256        public static final int AACObjectELD        = 39;
257
258        // from OMX_VIDEO_VP8LEVELTYPE
259        public static final int VP8Level_Version0 = 0x01;
260        public static final int VP8Level_Version1 = 0x02;
261        public static final int VP8Level_Version2 = 0x04;
262        public static final int VP8Level_Version3 = 0x08;
263
264        // from OMX_VIDEO_VP8PROFILETYPE
265        public static final int VP8ProfileMain = 0x01;
266
267
268        /**
269         * Defined in the OpenMAX IL specs, depending on the type of media
270         * this can be OMX_VIDEO_AVCPROFILETYPE, OMX_VIDEO_H263PROFILETYPE,
271         * OMX_VIDEO_MPEG4PROFILETYPE or OMX_VIDEO_VP8PROFILETYPE.
272         */
273        public int profile;
274
275        /**
276         * Defined in the OpenMAX IL specs, depending on the type of media
277         * this can be OMX_VIDEO_AVCLEVELTYPE, OMX_VIDEO_H263LEVELTYPE
278         * OMX_VIDEO_MPEG4LEVELTYPE or OMX_VIDEO_VP8LEVELTYPE.
279         */
280        public int level;
281    };
282
283    /**
284     * Enumerates the capabilities of the codec component. Since a single
285     * component can support data of a variety of types, the type has to be
286     * specified to yield a meaningful result.
287     * @param type The MIME type to query
288     */
289    public final CodecCapabilities getCapabilitiesForType(
290            String type) {
291        return MediaCodecList.getCodecCapabilities(mIndex, type);
292    }
293}
294