FFmpeg设置OpenMax硬件编码

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

欢迎关注我的微信公众号「码农突围」,分享Python、Java、大数据、机器学习、人工智能等技术,关注码农技术提升•职场突围•思维跃迁,20万+码农成长充电第一站,陪有梦想的你一起成长。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值