Android音视频学习第1章:使用ffmpeg进行视频解码
2017-01-20 12:42
661 查看
FFmpeg解码的流程图如下所示
以下代码实现的是视频解码并写入yuv文件
输入文件路径和输出文件路径
按照ffmpeg的解码流程进行注册
遍历AVStream找到对应的数据索引
FFmpeg解码的数据结构如下所示:根据AVStream获取对应的解码器
接下来要将像素数据转成指定的yuv420p像素格式
使用解码器,将AVPacket转成AVFrame
U和V都是Y的1/4
以下代码是使用ANativeWindow将视频绘制上去
上面用到的库
以下代码实现的是视频解码并写入yuv文件
#include "com_xuemeng_mylive_utils_XuemengPlayer.h" #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <android/native_window.h> #include <android/native_window_jni.h> #include <android/log.h> #define LOGI(FORMAT,...) __android_log_print(ANDROID_LOG_INFO,"xuemeng",FORMAT,##__VA_ARGS__); #define LOGE(FORMAT,...) __android_log_print(ANDROID_LOG_ERROR,"xuemeng",FORMAT,##__VA_ARGS__); #include "libyuv.h" //封装格式 #include "libavformat/avformat.h" //解码 #include "libavcodec/avcodec.h" //缩放 #include "libswscale/swscale.h" JNIEXPORT void JNICALL Java_com_xuemeng_mylive_utils_VideoUtils_decode( JNIEnv *env, jclass jcls, jstring input_jstr, jstring output_jstr) {
输入文件路径和输出文件路径
const char* input_cstr = (*env)->GetStringUTFChars(env, input_jstr, NULL); const char* output_cstr = (*env)->GetStringUTFChars(env, output_jstr, NULL);
按照ffmpeg的解码流程进行注册
//1.注册组件 av_register_all(); //封装格式上下文 AVFormatContext *pFormatCtx = avformat_alloc_context(); //2.打开输入视频文件 if (avformat_open_input(&pFormatCtx, input_cstr, NULL, NULL) != 0) { LOGI("%s", "打开输入视频文件失败"); return; } //3.获取视频信息 if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { LOGI("%s", "获取视频信息失败"); return; }
遍历AVStream找到对应的数据索引
//视频解码,需要找到对应的AVStream所在的pFormatCtx->streams的索引位置 int video_stream_idx = -1; int i = 0; for (; i < pFormatCtx->nb_streams; i++) { //根据类型判断是否是视频流 if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_idx = i; break; } }
FFmpeg解码的数据结构如下所示:根据AVStream获取对应的解码器
//4.获取解码器 //根据索引拿到对应的流,根据流拿到解码器上下文 AVCodecContext *pCodeCtx = pFormatCtx->streams[video_stream_idx]->codec; //再根据上下文拿到编解码id,通过该id拿到解码器 AVCodec *pCodec = avcodec_find_decoder(pCodeCtx->codec_id); if (pCodec == NULL) { LOGI("%s", "无法解码"); return; } //5.打开解码器 if (avcodec_open2(pCodeCtx, pCodec, NULL) < 0) { LOGI("%s", "编码器无法打开"); return; }
接下来要将像素数据转成指定的yuv420p像素格式
//编码数据 AVPacket *packet = av_malloc(sizeof(AVPacket)); //像素数据(解码数据) AVFrame *frame = av_frame_alloc(); ---------- AVFrame *yuvFrame = av_frame_alloc(); //只有指定AVFrame的像素格式,画面大小才能真正分配内存 //缓冲区分配内存 uint8_t *out_buffer = (uint8_t *) av_malloc( avpicture_get_size(AV_PIX_FMT_YUV420P, pCodeCtx->width, pCodeCtx->height)); //设置yuvFrame缓冲区,像素格式 avpicture_fill((AVPicture *) yuvFrame, out_buffer, AV_PIX_FMT_YUV420P,pCodeCtx->width, pCodeCtx->height); //输出文件 FILE *fp_yuv = fopen(output_cstr, "wb"); //用于像素格式转换或者缩放 struct SwsContext *sws_ctx = sws_getContext(pCodeCtx->width,pCodeCtx->height, pCodeCtx->pix_fmt, pCodeCtx->width, pCodeCtx->height, AV_PIX_FMT_YUV420P, SWS_BILINEAR, NULL, NULL,NULL);
使用解码器,将AVPacket转成AVFrame
int len, got_frame, framecount = 0; //6.一帧一帧读取压缩的视频数据AVPacket while (av_read_frame(pFormatCtx, packet) >= 0) { if (packet->stream_index == video_stream_idx) { //解码AVPacket->AVFrame len = avcodec_decode_video2(pCodeCtx, frame, &got_frame, packet); //非0,正在解码 if (got_frame) { //转换为指定YUV420P像素帧 sws_scale(sws_ctx, frame->data, frame->linesize, 0,frame->height, yuvFrame->data, yuvFrame->linesize);
U和V都是Y的1/4
//向YUV文件保存解码之后的帧数据 //AVFrame->YUV //一个像素包含一个Y int y_size = pCodeCtx->width * pCodeCtx->height; //Y fwrite(yuvFrame->data[0], 1, y_size, fp_yuv); //U fwrite(yuvFrame->data[1], 1, y_size / 4, fp_yuv); //V fwrite(yuvFrame->data[2], 1, y_size / 4, fp_yuv); LOGI("解码%d帧", framecount++); } } av_free_packet(packet); }
fclose(fp_yuv); av_frame_free(&frame); avcodec_close(pCodeCtx); avformat_free_context(pFormatCtx); (*env)->ReleaseStringUTFChars(env, input_jstr, input_cstr); (*env)->ReleaseStringUTFChars(env, output_jstr, output_cstr); }
以下代码是使用ANativeWindow将视频绘制上去
#include "com_xuemeng_mylive_utils_XuemengPlayer.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <android/native_window.h>
#include <android/native_window_jni.h>
#include <android/log.h>
#define LOGI(FORMAT,...) __android_log_print(ANDROID_LOG_INFO,"xuemeng",FORMAT,##__VA_ARGS__);
#define LOGE(FORMAT,...) __android_log_print(ANDROID_LOG_ERROR,"xuemeng",FORMAT,##__VA_ARGS__);
#include "libyuv.h"
//封装格式
#include "libavformat/avformat.h"
//解码
#include "libavcodec/avcodec.h"
//缩放
#include "libswscale/swscale.h"
JNIEXPORT void JNICALL Java_com_xuemeng_mylive_utils_XuemengPlayer_render(JNIEnv *env, jobject jobj, jstring input_jstr,
jobject surface) {
const char* input_cstr = (*env)->GetStringUTFChars(env, input_jstr, NULL);
//1.注册组件
av_register_all();
//封装格式上下文
AVFormatContext *pFormatCtx = avformat_alloc_context();
//2.打开输入视频文件
if (avformat_open_input(&pFormatCtx, input_cstr, NULL, NULL) != 0) {
LOGI("%s", "打开输入视频文件失败");
return;
}
//3.获取视频信息
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
LOGI("%s", "获取视频信息失败");
return;
}
//视频解码,需要找到对应的AVStream所在的pFormatCtx->streams的索引位置 int video_stream_idx = -1; int i = 0; for (; i < pFormatCtx->nb_streams; i++) { //根据类型判断是否是视频流 if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_idx = i; break; } }
//4.获取解码器 //根据索引拿到对应的流,根据流拿到解码器上下文 AVCodecContext *pCodeCtx = pFormatCtx->streams[video_stream_idx]->codec; //再根据上下文拿到编解码id,通过该id拿到解码器 AVCodec *pCodec = avcodec_find_decoder(pCodeCtx->codec_id); if (pCodec == NULL) { LOGI("%s", "无法解码"); return; } //5.打开解码器 if (avcodec_open2(pCodeCtx, pCodec, NULL) < 0) { LOGI("%s", "编码器无法打开"); return; }
//编码数据
AVPacket *packet = av_malloc(sizeof(AVPacket));
//像素数据(解码数据)
AVFrame *yuv_frame = av_frame_alloc();
AVFrame *rgb_frame = av_frame_alloc();
//native绘制时的窗体
ANativeWindow* nativeWindow = ANativeWindow_fromSurface(env, surface);
//绘制时的缓冲区
ANativeWindow_Buffer outBuffer;
int len, got_frame, framecount = 0;
//6.一帧一帧读取压缩的视频数据AVPacket
while (av_read_frame(pFormatCtx, packet) >= 0) {
if (packet->stream_index == video_stream_idx) {
//解码AVPacket->AVFrame
len = avcodec_decode_video2(pCodeCtx, yuv_frame, &got_frame, packet);
//非0,正在解码
if (got_frame) {
LOGI("解码%d帧", framecount++);
//lock
//设置缓冲区的属性(宽,高,像素)
ANativeWindow_setBuffersGeometry(nativeWindow, pCodeCtx->width, pCodeCtx->height,
WINDOW_FORMAT_RGBA_8888);
ANativeWindow_lock(nativeWindow, &outBuffer, NULL);
//设置rgb_frame缓冲区,像素格式
//rgb_frame缓冲区与outBuffer.bits是同一块内存
avpicture_fill((AVPicture *) rgb_frame, outBuffer.bits, AV_PIX_FMT_RGBA, pCodeCtx->width,
pCodeCtx->height);
//YUV->RGB 8888
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], rgb_frame->data[0], rgb_frame->linesize[0],
pCodeCtx->width, pCodeCtx->height);
//unlock
ANativeWindow_unlockAndPost(nativeWindow);
usleep(1000 * 16);
}
}
av_free_packet(packet);
}
ANativeWindow_release(nativeWindow);
av_frame_free(&yuv_frame);
avcodec_close(pCodeCtx);
avformat_free_context(pFormatCtx);
(*env)->ReleaseStringUTFChars(env, input_jstr, input_cstr);
}
上面用到的库
以上代码牵扯到的数据结构解释
相关文章推荐
- Android音视频学习第2章:使用ffmpeg进行音频解码
- Android音视频学习第7章:使用OpenSL ES进行音频解码
- Android音视频学习第7章:使用OpenSL ES进行音频解码
- Android平台使用MediaCodec进行H264格式的视频编解码
- 使用FFmpeg对视频进行编解码的一般流程
- 使用ffmpeg进行视频解码以及图像转换
- Android平台上使用SDL官方demo播放视频(使用ffmpeg最新版解码)
- 从零开始学习音视频编程技术(五) 使用FFMPEG解码视频之保存成图片
- 视频学习笔记:Android ffmpeg解码多路h264视频并显示
- Android平台下的FFmpeg的学习之路------(三)视频解码+NDK绘制
- 使用ffmpeg进行视频解码以及图像转换
- 使用ffmpeg进行视频解码以及图像转换
- FFmpeg 学习之 定时器解码两路视频并进行对比<2>
- 视频学习笔记:Android ffmpeg解码多路h264视频并显示
- Android平台上使用SDL官方demo播放视频(使用ffmpeg最新版解码)
- 使用ffmpeg进行音视频编解码时用到的函数介绍
- 使用 ffmpeg 进行网络推流:拉流->解封装->解码->处理原始数据(音频、视频)->编码->编码->推流
- 通过C++/CLI使用FFMPEG库进行视频解码[初步]
- 使用FFMPEG3.4.2版本进行视频的解码为YUV格式
- Android平台上使用SDL官方demo播放视频(使用ffmpeg最新版解码)