一文读懂 Android FFmpeg 视频解码过程与实战分析

本文详细介绍了如何在Android中使用FFmpeg进行视频解码,包括引入FFmpeg库、解码流程、主要接口的使用,以及在实际应用中加入同步锁实现视频播放。文章通过实例展示了从打开视频文件、分配解码上下文到解码每一帧的步骤,并提供了简单的VideoDecoder封装示例,帮助读者理解解码过程并实现自己的解码器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jobject /* this */) {

std::string hello = “Hello from C++”;

return env->NewStringUTF(hello.c_str());

}

  • CMakeLists.txt 是一个构建脚本,目的是配置可以编译出 native-lib 此 so 库的构建信息;

# For more information about using CMake with Android Studio, read the

# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)

# Declares and names the project.

project(“ffmpeg”)

# Creates and names a library, sets it as either STATIC

# or SHARED, and provides the relative paths to its source code.

# You can define multiple libraries, and CMake builds them for you.

# Gradle automatically packages shared libraries with your APK.

# 定义 so 库和头文件所在目录,方便后面使用

set(FFmpeg_lib_dir  C M A K E S O U R C E D I R / . . / j n i L i b s / {CMAKE_SOURCE_DIR}/../jniLibs/ CMAKESOURCEDIR/../jniLibs/{ANDROID_ABI})

set(FFmpeg_head_dir ${CMAKE_SOURCE_DIR}/FFmpeg)

# 添加头文件目录

include_directories(

FFmpeg/include

)

add_library( # Sets the name of the library.

ffmmpeg

# Sets the library as a shared library.

SHARED

# Provides a relative path to your source file(s).

native-lib.cpp

)

# Searches for a specified prebuilt library and stores the path as a

# variable. Because CMake includes system libraries in the search path by

# default, you only need to specify the name of the public NDK library

# you want to add. CMake verifies that the library exists before

# completing its build.

# 添加FFmpeg相关的so库

add_library( avutil

SHARED

IMPORTED )

set_target_properties( avutil

PROPERTIES IMPORTED_LOCATION

${FFmpeg_lib_dir}/libavutil.so )

add_library( swresample

SHARED

IMPORTED )

set_target_properties( swresample

PROPERTIES IMPORTED_LOCATION

${FFmpeg_lib_dir}/libswresample.so )

add_library( avcodec

SHARED

IMPORTED )

set_target_properties( avcodec

PROPERTIES IMPORTED_LOCATION

${FFmpeg_lib_dir}/libavcodec.so )

find_library( # Sets the name of the path variable.

log-lib

# Specifies the name of the NDK library that

# you want CMake to locate.

log)

# Specifies libraries CMake should link to your target library. You

# can link multiple libraries, such as libraries you define in this

# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.

audioffmmpeg

# 把前面添加进来的 FFmpeg.so 库都链接到目标库 native-lib 上

avutil

swresample

avcodec

-landroid

# Links the target library to the log library

# included in the NDK.

${log-lib})

  • 以上的操作就将 FFmpeg 引入 Android 项目。

二、FFmpeg 解码视频的原理和细节


2.1 主要流程

9f7adccbee12a8d8d1370ebf7c3a0e51.png

2.2 基本原理

2.2.1 常用的 ffmpeg 接口

// 1 分配 AVFormatContext

avformat_alloc_context();

// 2 打开文件输入流

avformat_open_input(AVFormatContext **ps, const char *url,

const AVInputFormat *fmt, AVDictionary **options);

// 3 提取输入文件中的数据流信息

avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);

// 4 分配编解码上下文

avcodec_alloc_context3(const AVCodec *codec);

// 5 基于与数据流相关的编解码参数来填充编解码器上下文

avcodec_parameters_to_context(AVCodecContext *codec,

const AVCodecParameters *par);

// 6 查找对应已注册的编解码器

avcodec_find_decoder(enum AVCodecID id);

// 7 打开编解码器

avcodec_open2(AVCodecContext *a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值