From 6e52fc3832b50fe73b92d27bf2f07434389dcd47 Mon Sep 17 00:00:00 2001 From: xufuji456 <839789740@qq.com> Date: Sat, 21 Aug 2021 22:42:44 +0800 Subject: [PATCH] Add mediacodec to decode h264 --- app/src/main/cpp/media_player.cpp | 2 ++ app/src/main/cpp/video_filter.c | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/src/main/cpp/media_player.cpp b/app/src/main/cpp/media_player.cpp index aab5ced..5cd7be5 100644 --- a/app/src/main/cpp/media_player.cpp +++ b/app/src/main/cpp/media_player.cpp @@ -19,6 +19,7 @@ extern "C" { #include "libswresample/swresample.h" #include "packet_queue.h" #include "ffmpeg_jni_define.h" +#include "include/libavcodec/jni.h" #ifdef __cplusplus } #endif @@ -75,6 +76,7 @@ MediaPlayer* player; jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved){ javaVM = vm; + av_jni_set_java_vm(vm, nullptr);// set JavaVM into FFmpeg return JNI_VERSION_1_6; } diff --git a/app/src/main/cpp/video_filter.c b/app/src/main/cpp/video_filter.c index 5070fb9..70a533f 100644 --- a/app/src/main/cpp/video_filter.c +++ b/app/src/main/cpp/video_filter.c @@ -17,6 +17,7 @@ #include "ffmpeg_jni_define.h" #define TAG "VideoFilter" +#define ENABLE_MEDIACODEC 1 AVFormatContext *pFormatCtx; AVCodecContext *pCodecCtx; @@ -150,7 +151,17 @@ int open_input(JNIEnv *env, const char *file_name, jobject surface) { } pCodecCtx = pFormatCtx->streams[video_stream_index]->codec; - AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id); + AVCodec *pCodec; + if (ENABLE_MEDIACODEC && pCodecCtx->codec_id == AV_CODEC_ID_H264) { + // note: Some devices will bring blurred screen, when using mediacodec + pCodec = avcodec_find_decoder_by_name("h264_mediacodec"); + if (pCodec == NULL) { + pCodec = avcodec_find_decoder(pCodecCtx->codec_id); + } + LOGE(TAG, "codec name=%s", pCodec->name); + } else { + pCodec = avcodec_find_decoder(pCodecCtx->codec_id); + } if (pCodec == NULL) { LOGE(TAG, "couldn't find Codec."); return -1;