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