視頻解碼、像素格式轉換與Native原生繪制
一、視頻解碼
1、FFmpeg 庫簡介
FFmpeg 一共包含8個庫
- avcodec:編解碼(最重要的庫)
- avformat:封裝格式處理
- avfilter:濾鏡特效處理
- avdevice:各自設備的輸入輸出
- avutil:工具庫(大部分庫都需要這個庫的支持)
- postproc:后加工
- swresample:音頻采樣數據格式轉換
- swscale:視頻像素數據格式轉換
2、FFmpeg 解碼的流程圖

FFmpeg 解碼的流程圖
3、FFmpeg 數據格式簡介
- AVFormatContext:封裝格式上下文結構體,也是統(tǒng)領全局的結構體,保存了視頻文件封裝格式相關信息
- AVInputFormat:每種封裝格式(例如FLV、MKV、MP4、AVI)對應一個該結構體。
- AVStream:視頻文件中每個視頻(音頻)流對應一個該結構體
- AVCodeContext:編解碼器上下文結構體,保存了視頻(音頻)編解碼相關信息。
- AVCodec:每種視頻(音頻)編解碼器(例如H.264解碼器)對應一個該結構體
- AVPacket:存儲一幀壓縮編碼數據
- AVFrame:存儲一幀解碼后的像素(采樣)數據
二、像素格式轉換
像素格式轉換就是將yuv420p 的轉成ARGB,可以使用 C/C++ 庫 libyuv 來進行轉換
I420ToARGB
參數按順序如下:
| 參數 | 類型 | 說明 |
|---|---|---|
| src_y | uint8_t* | 來源的frame的 y 幀數據 yuv_frame->data |
| src_stride_y | int | 來源的frame的y大小數據 yuv_frame->linesize |
| src_u | uint8_t* | 來源的frame的 u 幀數據yuv_frame->data |
| src_stride_u | int | 來源的frame的u大小數據 yuv_frame->linesize |
| src_v | uint8_t* | 來源的frame的 v 幀數據yuv_frame->data |
| src_stride_v | int | 來源的frame的v大小數據 yuv_frame->linesize |
| dst_argb | uint8_t* | 轉換后的 rgb 的frame數據 |
| dst_stride_argb | int | 轉換后的 rgb 的frame的大小數據 |
| width | int | 像素數據寬度 |
| height | int | 像素數據高度 |
三、Native 原生繪制
Native 原生繪制是使用ANativeWindow 將surface 和 緩沖區(qū)buffer綁定,進而去更新緩沖區(qū)的數據,并刷新到 surface 就可以實現原生繪制
1、獲取ANativeWindow指針,定義緩沖區(qū)
//Native 繪制
ANativeWindow *nativeWindow = ANativeWindow_fromSurface(env, surface);
//緩沖區(qū)buffer
ANativeWindow_Buffer windowBuffer;
2、設置緩沖區(qū)參數
//設置緩沖區(qū)參數
ANativeWindow_setBuffersGeometry(nativeWindow, avCodecContext->width,
avCodecContext->height, WINDOW_FORMAT_RGBA_8888);
3、刷新數據到緩沖區(qū)
- 對緩沖區(qū)進行加鎖
- 刷新轉換后的數據到緩沖區(qū)
- 對緩沖區(qū)數據進行解鎖
//LOCK
ANativeWindow_lock(nativeWindo&windowBuffer, NULL
avpicture_fill((AVPicture rgba_frame, windowBuffer.bitPIX_FMT_RGBA,
avCodecContext->width,
avCodecContext->height
//fix buffer
I420ToARGB(
yuv_frame->data[0yuv_frame->linesize[0],
yuv_frame->data[2yuv_frame->linesize[2],
yuv_frame->data[1yuv_frame->linesize[1],
rgba_frame->data[0rgba_frame->linesize[0],
avCodecContext->widtavCodecContext->height
//UNLOCK
ANativeWindow_unlockAndPost(natiindow);
4、釋放nativeWindow
ANativeWindow_release(nativeWindow);
四、使用FFmpeg實現native原生繪制,顯示視頻圖像
之前說明了Android Studio 使用 CMake 來配置FFmpeg 的方法,這里就省略項目配置與Java代碼部分,主要來實現C/C++代碼部分,將像素數據會知道 Surface 上
#include "cn_onestravel_ndk_ffmpeg_render_VideoPlayer.h"
#include <android/log.h>
#include <unistd.h>
//編碼
#include "include/libavcodec/avcodec.h"
//封裝格式處理
#include "include/libavformat/avformat.h"
//像素處理
#include "include/libswscale/swscale.h"
#include "include/libavutil/avutil.h"
#include "include/libavutil/frame.h"
#include <android/native_window.h>
#include <android/native_window_jni.h>
#include <libyuv.h>
#include <pthread.h>
#define LOGI(FORMAT, ...) __android_log_print(ANDROID_LOG_INFO,"FFMPEG",FORMAT,##__VA_ARGS__);
#define LOGE(FORMAT, ...) __android_log_print(ANDROID_LOG_ERROR,"FFMPEG",FORMAT,##__VA_ARGS__);
/*
* Class: cn_onestravel_ndk_ffmpeg_render_VideoPlayer
* Method: render
* Signature: (Ljava/lang/String;Landroid/view/Surface;)V
*/
JNIEXPORT void JNICALL Java_cn_onestravel_ndk_ffmpeg_render_VideoPlayer_render
(JNIEnv *env, jclass jcls, jstring jstr_input, jobject surface) {
const char *cstr_input = (*env)->GetStringUTFChars(env, jstr_input, NULL);
//注冊ffmpeg 所有組件
av_register_all();
//封裝格式上下文
AVFormatContext *formatContext = avformat_alloc_context();
//打開輸入視頻文件
if (avformat_open_input(&formatContext, cstr_input, NULL, NULL) != 0) {
LOGE("無法打開視頻文件");
return;
}
//獲取視頻文件信息
if (avformat_find_stream_info(formatContext, NULL) < 0) {
LOGE("獲取視頻文件信息失敗");
return;
}
//獲取視頻流的索引位置
//遍歷所有類型的流(音頻流,視頻流、字幕流)
int i = 0;
int v_stream_index = -1;
for (; i < formatContext->nb_streams; i++) {
if (formatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
v_stream_index = i;
break;
}
}
if (v_stream_index < 0) {
LOGE("%s", "找不到視頻流\n");
return;
}
//獲取視頻流中的編解碼的上下文
AVCodecContext *avCodecContext = formatContext->streams[v_stream_index]->codec;
//根據視頻編解碼上下文的id得到對應的編解碼器
AVCodec *avCodec = avcodec_find_decoder(avCodecContext->codec_id);
if (avCodec == NULL) {
LOGE("未找到解碼器");
return;
}
//打開解碼器
if (avcodec_open2(avCodecContext, avCodec, NULL) < 0) {
LOGE("打開解碼器失敗");
return;
}
//輸出視頻信息
LOGI("視頻文件格式:%s", formatContext->iformat->name);
//formatContext->duration單位為微妙
LOGI("視頻時長:%lld", (formatContext->duration) / 1000000);
LOGI("視頻的寬度和高度 W:%d ,H:%d", avCodecContext->width, avCodecContext->height);
LOGI("視頻解碼器名稱:%s", avCodec->name);
//準備讀取
//AVPacket用于存儲一幀一幀的壓縮數據(H264)
//緩沖區(qū),開辟空間
AVPacket *packet = (AVPacket *) av_malloc(sizeof(AVPacket));
AVFrame *yuv_frame = av_frame_alloc();
AVFrame *yuv_scale_frame = av_frame_alloc();
AVFrame *rgba_frame = av_frame_alloc();
//Native 繪制
ANativeWindow *nativeWindow = ANativeWindow_fromSurface(env, surface);
//緩沖區(qū)buffer
ANativeWindow_Buffer windowBuffer;
int len, got_frame, frame_count = 0;
while (av_read_frame(formatContext, packet) >= 0) {
len = avcodec_decode_video2(avCodecContext, yuv_frame, &got_frame, packet);
//不為0,正在解碼
if (got_frame) {
int i = frame_count++;
LOGI("解碼%d幀", i);
ANativeWindow_setBuffersGeometry(nativeWindow, avCodecContext->width,
avCodecContext->height, WINDOW_FORMAT_RGBA_8888);
//LOCK
ANativeWindow_lock(nativeWindow, &windowBuffer, NULL);
avpicture_fill((AVPicture *) rgba_frame, windowBuffer.bits, PIX_FMT_RGBA,
avCodecContext->width,
avCodecContext->height);
//fix buffer
I420ToARGB(
yuv_frame->data[0], yuv_frame->linesize[0],
yuv_frame->data[2], yuv_frame->linesize[2],
yuv_frame->data[1], yuv_frame->linesize[1],
rgba_frame->data[0], rgba_frame->linesize[0],
avCodecContext->width, avCodecContext->height);
//UNLOCK
ANativeWindow_unlockAndPost(nativeWindow);
// ANativeWindow_release(nativeWindow);
usleep(16 * 1000);
}
av_free_packet(packet);
}
ANativeWindow_release(nativeWindow);
av_frame_free(yuv_frame);
av_frame_free(rgba_frame);
avcodec_close(avCodec);
avcodec_free_context(avCodecContext);
avformat_free_context(formatContext);
(*env)->ReleaseStringUTFChars(env, jstr_input, cstr_input);
}
自定義 VideoView 繼承自 SurfaceView
public class VideoView extends SurfaceView {
public VideoView(Context context) {
super(context);
init();
}
public VideoView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public VideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init(){
//初始化SurfaceView的像素格式
SurfaceHolder holder = getHolder();
holder.setFormat(PixelFormat.RGBA_8888);
}
}