1、首先按照上一篇博客进行JNI配置,接下来在上一篇博客配置完成的NDKTEST工程基础上进行修改(我的这里用的工程名字是ffmpeg_test);
2、下载ffmpeg for android的so文件,也可以自己编译,这里有别人编译好的下载:
http://download.csdn.net/detail/zhjin8510/8539759
下载后包含我们需要的头文件以及so文件,其so文件是包括libavcodec.so,libavcodec-56.so,libavfilter.so……各个分开的库;
也可以下载:
http://download.csdn.net/detail/huangyifei_1111/9025229
此处的so文件是单个的libffmpeg.so,但未提供头文件,头文件仍需在第一个地址下载或者从ffmpeg源代码里整理出来;
3、将头文件解压到目录下,如图:
4、在main目录下创建jniLibs文件夹,然后在jniLibs中创建armeabi-v7a文件夹,把so文件放在这个目录里,这些so文件成为预编译的so文件(似乎必须创建为这个和平台相关的目录,不然ndk编译能通过,但java层无法从jni创建的so文件链接到预编译的so文件,总之不管从什么目录引入预编译的so文件,但jniLibs/armeabi-v7a下必须有这些so文件)。
5、在java层中的Acitvity中加入函数定义:
public native String GetFFmpegVersion();
public native int H264DecoderInit(int width, int height);
public native int H264DecoderRelease();
public native int H264Decode(byte[] in, int insize, byte[] out);
6、参考http://www.tuicool.com/articles/zMNf63v在jni目录下创建android_FFmpeg.c和android_FFmpeg.h。
android_FFmpeg.c:
//
// Created by Administrator on 2016/1/13.
//
#include <math.h>
//#include “include/libavutil/cpu.h“
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include "libavcodec/version.h"
#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
#include "libavutil/imgutils.h"
#include "libavutil/mathematics.h"
#include "libavutil/samplefmt.h"
#include "libavutil/avutil.h"
#include "android/log.h"
#include "android_FFmpeg.h"
#include "libavformat/avformat.h"
#define LOG_TAG "H264Android.c"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#ifdef __cplusplus
extern "C" {
#endif
//Video
struct AVCodecContext *pAVCodecCtx = NULL;
struct AVCodec *pAVCodec;
struct AVPacket mAVPacket;
struct AVFrame *pAVFrame = NULL;
//Audio
struct AVCodecContext *pAUCodecCtx = NULL;
struct AVCodec *pAUCodec;
struct AVPacket mAUPacket;
struct AVFrame *pAUFrame = NULL;
int iWidth = 0;
int iHeight = 0;
int *colortab = NULL;
int *u_b_tab = NULL;
int *u_g_tab = NULL;
int *v_g_tab = NULL;
int *v_r_tab = NULL;
//short *tmp_pic=NULL;
unsigned int *rgb_2_pix = NULL;
unsigned int *r_2_pix = NULL;
unsigned int *g_2_pix = NULL;
unsigned int *b_2_pix = NULL;
void DeleteYUVTab() {
// av_free(tmp_pic);
av_free(colortab);
av_free(rgb_2_pix);
}
void CreateYUVTab_16() {
int i;
int u, v;
// tmp_pic = (short*)av_malloc(iWidth*iHeight*2); // iWidth * iHeight * 16bits
colortab = (