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