OpenMax是一个统一的多媒体框架, ffmpeg中支持H264的OpenMax编码, 本文记录如何开启OpenMax编码。
-
ffmpeg版本: 4.0
-
OpenMax头文件版本: 1.2
首先下载ffmepg,然后是OpenMax的头文件, 解压头文件的zip之后得到
头文件拷贝到NDK对应平台的include中,如图
后在configure的时候加入--enable-omx
configure之后, 检查下config.h中, 下面这几个宏是否为1
#define CONFIG_OMX 1 #define CONFIG_H264_OMX_ENCODER 1
configure之后, 如果make报错的话, 检查下OpenMax头文件的版本, OpenMax1.0版本的头文件越少很多东西
最后上传下交叉编译ffmepg的脚本和测试sample
NDK_ROOT=/home/C4/le.zhang/android-ndk-r13b TOOLCHAIN_DIR=$NDK_ROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 SYSROOT=$NDK_ROOT/platforms/android-19/arch-arm PLATFORM=$SYSROOT TOOLCHAIN=$NDK_ROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 PREFIX=./build function build_one { ./configure \ --prefix=$PREFIX \ --target-os=linux \ --disable-doc \ --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \ --arch=arm \ --sysroot=$PLATFORM \ --extra-cflags="-I$PLATFORM/usr/include" \ --cc=$TOOLCHAIN/bin/arm-linux-androideabi-gcc \ --nm=$TOOLCHAIN/bin/arm-linux-androideabi-nm \ --disable-shared \ --enable-runtime-cpudetect \ --enable-gpl \ --enable-small \ --enable-ffplay \ --enable-cross-compile \ --disable-debug \ --enable-static \ --enable-omx \ --disable-asm \ --disable-symver \ --enable-stripping \ --extra-cflags="-Os -fpic $ADDI_CFLAGS" \ --extra-ldflags="$ADDI_LDFLAGS" \ $ADDITIONAL_CONFIGURE_FLAG make clean make -j8 make install $TOOLCHAIN/bin/arm-linux-androideabi-ld \ -rpath-link=$PLATFORM/usr/lib \ -L$PLATFORM/usr/lib \ -L$PREFIX/lib \ -soname libffmpeg.so -shared -nostdlib -Bsymbolic --whole-archive --no-undefined -o \ $PREFIX/libffmpeg.so \ libavcodec/libavcodec.a \ libavfilter/libavfilter.a \ libswresample/libswresample.a \ libavformat/libavformat.a \ libavutil/libavutil.a \ libswscale/libswscale.a \ libavdevice/libavdevice.a \ libpostproc/libpostproc.a \ -lc -lm -lz -ldl -llog --dynamic-linker=/system/bin/linker \ $TOOLCHAIN/lib/gcc/arm-linux-androideabi/4.9.x/libgcc.a } # arm v7vfp CPU=armv7-a OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU " ADDI_CFLAGS="-marm" build_one $TOOLCHAIN/bin/arm-linux-androideabi-strip -s $PREFIX/libffmpeg.so
测试sample
#include "libavcodec/avcodec.h" #include "include/libavformat/avformat.h" int main(int argc, char **argv) { AVFormatContext *pFormatCtx; AVOutputFormat *fmt; AVStream *video_st; const char* out_file = "/sdcard/ds.264"; av_register_all(); //Method1 方法1.组合使用几个函数 pFormatCtx = avformat_alloc_context(); //Guess Format 猜格式 fmt = av_guess_format(NULL, out_file, NULL); pFormatCtx->oformat = fmt; //Method 2 方法2.更加自动化一些 //avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file); //fmt = pFormatCtx->oformat; //Output Format 注意输出路径 if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0) { printf("Failed to open output file! 输出文件打开失败"); return -1; } video_st = avformat_new_stream(pFormatCtx, 0); video_st->time_base.num = 1; video_st->time_base.den = 25; if (video_st==NULL) { return -1; } AVCodecContext *codec = video_st->codec; //pCodecCtx->codec_id =AV_CODEC_ID_HEVC; codec->codec_id = fmt->video_codec; codec->codec_type = AVMEDIA_TYPE_VIDEO; codec->pix_fmt = AV_PIX_FMT_YUV420P; codec->width = 640; codec->height = 360; codec->time_base.num = 1; codec->time_base.den = 25; codec->bit_rate = 400000; codec->gop_size=250; AVCodec *pCodec = avcodec_find_encoder(codec->codec_id); printf("codec name : %s\n", pCodec->name); return 0; }
原创作者:狼爽过羊
原文链接:https://www.jianshu.com/p/61e2c3cbc412
推荐阅读:
说一说我在创建星球这10多天,在星球里干了啥?