1 /*
2  * Copyright (C) 2013 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.video.cts;
18 
19 import android.media.MediaCodec;
20 import android.media.MediaCodecInfo.CodecCapabilities;
21 import android.media.MediaFormat;
22 import android.util.Log;
23 
24 import com.android.compatibility.common.util.MediaUtils;
25 
26 import java.io.IOException;
27 
28 /**
29  * Utility class for getting codec information like bit rate, fps, and etc.
30  * Uses public member variables instead of methods as this code is only for video benchmarking.
31  */
32 public class CodecInfo {
33     /** bit rate in bps */
34     public int mBitRate = 0;
35     /** Frame rate */
36     public int mFps = 0;
37     /** if codec is supporting YUV semiplanar format */
38     public boolean mSupportSemiPlanar = false;
39     /** if codec is supporting YUV planar format */
40     public boolean mSupportPlanar = false;
41     /** if codec is software-based */
42     public boolean mIsSoftware = false;
43 
44     private static final String TAG = "CodecInfo";
45     private static final String VIDEO_AVC = MediaFormat.MIMETYPE_VIDEO_AVC;
46     /**
47      * Check if given codec with given (w,h) is supported.
48      * @param codecName codec name
49      * @param mimeType codec type in mime format like MediaFormat.MIMETYPE_VIDEO_AVC
50      * @param w video width
51      * @param h video height
52      * @return null if the configuration is not supported.
53      */
getSupportedFormatInfo( String codecName, String mimeType, int w, int h, int maxFps)54     public static CodecInfo getSupportedFormatInfo(
55             String codecName, String mimeType, int w, int h, int maxFps) {
56         MediaCodec codec;
57         try {
58             codec = MediaCodec.createByCodecName(codecName);
59         } catch (IOException e) {
60             return null;
61         }
62 
63         CodecCapabilities cap = codec.getCodecInfo().getCapabilitiesForType(mimeType);
64         if (cap.colorFormats.length == 0) {
65             Log.w(TAG, "no supported color format");
66             codec.release();
67             return null;
68         }
69 
70         CodecInfo info = new CodecInfo();
71         for (int color : cap.colorFormats) {
72             if (color == CodecCapabilities.COLOR_FormatYUV420SemiPlanar) {
73                 info.mSupportSemiPlanar = true;
74             }
75             if (color == CodecCapabilities.COLOR_FormatYUV420Planar) {
76                 info.mSupportPlanar = true;
77             }
78         }
79         printIntArray("supported colors", cap.colorFormats);
80 
81         MediaFormat format = MediaFormat.createVideoFormat(mimeType, w, h);
82         MediaUtils.setMaxEncoderFrameAndBitrates(cap.getVideoCapabilities(), format, maxFps);
83         info.mFps = format.getInteger(MediaFormat.KEY_FRAME_RATE);
84         info.mBitRate = format.getInteger(MediaFormat.KEY_BIT_RATE);
85 
86         info.mIsSoftware = !codec.getCodecInfo().isHardwareAccelerated();
87 
88         codec.release();
89         return info;
90     }
91 
92     // for debugging
printIntArray(String msg, int[] data)93     private static void printIntArray(String msg, int[] data) {
94         StringBuilder builder = new StringBuilder();
95         builder.append(msg);
96         builder.append(":");
97         for (int e : data) {
98             builder.append(Integer.toHexString(e));
99             builder.append(",");
100         }
101         builder.deleteCharAt(builder.length() - 1);
102         Log.i(TAG, builder.toString());
103     }
104 }
105