MediaCodec判断是否可以采用硬解码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import android.media.MediaCodecList;

import java.util.HashMap;
import java.util.Map;

public class VideoSupportUitl {

private static Map<String, String> codecMap = new HashMap<>();
static {
codecMap.put("h264", "video/avc");
}

public static String findVideoCodecName(String ffcodename){
if(codecMap.containsKey(ffcodename))
{
return codecMap.get(ffcodename);
}
return "";
}

public static boolean isSupportCodec(String ffcodecname){
boolean supportvideo = false;
int count = MediaCodecList.getCodecCount();
for(int i = 0; i < count; i++)
{
String[] tyeps = MediaCodecList.getCodecInfoAt(i).getSupportedTypes();
for(int j = 0; j < tyeps.length; j++)
{
if(tyeps[j].equals(findVideoCodecName(ffcodecname)))
{
supportvideo = true;
break;
}
}
if(supportvideo)
{
break;
}
}
return supportvideo;
}
}

具体的类型对应关系可以查看相关文档,这里在Android源码MediaCodec.createDecoderByType()里面有一些相关的对应支持类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

/**
* Instantiate the preferred decoder supporting input data of the given mime type.
*
* The following is a partial list of defined mime types and their semantics:
* <ul>
* <li>"video/x-vnd.on2.vp8" - VP8 video (i.e. video in .webm)
* <li>"video/x-vnd.on2.vp9" - VP9 video (i.e. video in .webm)
* <li>"video/avc" - H.264/AVC video
* <li>"video/hevc" - H.265/HEVC video
* <li>"video/mp4v-es" - MPEG4 video
* <li>"video/3gpp" - H.263 video
* <li>"audio/3gpp" - AMR narrowband audio
* <li>"audio/amr-wb" - AMR wideband audio
* <li>"audio/mpeg" - MPEG1/2 audio layer III
* <li>"audio/mp4a-latm" - AAC audio (note, this is raw AAC packets, not packaged in LATM!)
* <li>"audio/vorbis" - vorbis audio
* <li>"audio/g711-alaw" - G.711 alaw audio
* <li>"audio/g711-mlaw" - G.711 ulaw audio
* </ul>
*
* <strong>Note:</strong> It is preferred to use {@link MediaCodecList#findDecoderForFormat}
* and {@link #createByCodecName} to ensure that the resulting codec can handle a
* given format.
*
* @param type The mime type of the input data.
* @throws IOException if the codec cannot be created.
* @throws IllegalArgumentException if type is not a valid mime type.
* @throws NullPointerException if type is null.
*/
@NonNull
public static MediaCodec createDecoderByType(@NonNull String type)
throws IOException {
return new MediaCodec(type, true /* nameIsType */, false /* encoder */);
}

ffmpeg里面

1
((const AVCodec*)(avCodecContext->codec))->name;

即可拿到name,然后jni交互调用即可。

-------------The End-------------