FFmpeg-之音视频解码与音视频同步(二)

//打开解码器,解码器有问题(比如说我们编译FFmpeg的时候没有编译对应类型的解码器)
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
LOGE(“%s”, “解码器无法打开\n”);
return;
}

//输出视频信息
LOGI(“视频的文件格式:%s”, pFormatCtx->iformat->name);
LOGI(“视频时长:%lld”, (pFormatCtx->duration) / (1000 * 1000));
LOGI(“视频的宽高:%d,%d”, pCodecCtx->width, pCodecCtx->height);
LOGI(“解码器的名称:%s”, pCodec->name);

//准备读取
//AVPacket用于存储一帧一帧的压缩数据(H264)
//缓冲区,开辟空间
AVPacket *packet = (AVPacket *) av_malloc(sizeof(AVPacket));

//AVFrame用于存储解码后的像素数据(YUV)
//内存分配
AVFrame *pFrame = av_frame_alloc();
//YUV420
AVFrame *pFrameYUV = av_frame_alloc();
//只有指定了AVFrame的像素格式、画面大小才能真正分配内存
//缓冲区分配内存
uint8_t *out_buffer = (uint8_t *) av_malloc(
avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
//初始化缓冲区
avpicture_fill((AVPicture *) pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width,
pCodecCtx->height);

//用于转码(缩放)的参数,转之前的宽高,转之后的宽高,格式等
struct SwsContext *sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height,
AV_PIX_FMT_YUV420P,
SWS_BICUBIC, NULL, NULL, NULL);
int got_picture, ret;

//输出文件
FILE *fp_yuv = fopen(output, “wb+”);

int frame_count = 0;

//一帧一帧的读取压缩数据
while (av_read_frame(pFormatCtx, packet) >= 0) {
//只要视频压缩数据(根据流的索引位置判断)
if (packet->stream_index == v_stream_idx) {
//解码一帧视频压缩数据,得到视频像素数据
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
if (ret < 0) {
LOGE(“%s”, “解码错误”);
return;
}

//为 0 说明解码完成,非0正在解码
if (got_picture) {
//AVFrame转为像素格式YUV420,宽高
//2 6输入、输出数据
//3 7输入、输出画面一行的数据的大小 AVFrame 转换是一行一行转换的
//4 输入数据第一列要转码的位置 从0开始
//5 输入画面的高度
sws_scale(sws_ctx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
pFrameYUV->data, pFrameYUV->linesize);

//输出到YUV文件
//AVFrame像素帧写入文件
//data解码后的图像像素数据(音频采样数据)
//Y 亮度 UV 色度(压缩了) 人对亮度更加敏感
//U V 个数是Y的1/4
int y_size = pCodecCtx->width * pCodecCtx->height;
fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv);
fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv);
fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv);

frame_count++;
LOGI(“解码第%d帧”, frame_count);
}
}

//释放资源
av_free_packet(packet);
}

fclose(fp_yuv);

av_frame_free(&pFrame);

avcodec_close(pCodecCtx);

avformat_free_context(pFormatCtx);

(*env)->ReleaseStringUTFChars(env, input_, input);
(*env)->ReleaseStringUTFChars(env, output_, output);
//通知 Java 层解码完毕
(*env)->CallStaticVoidMethod(env, type, mid);
}

//使用这两个 Window 相关的头文件需要在 CMake 脚本中引入 android 库
#include <android/native_window_jni.h>
#include <android/native_window.h>
#include “include/yuv/libyuv.h”

JNIEXPORT void JNICALL
Java_com_haohao_ffmpeg_AVUtils_videoRender(JNIEnv *env, jclass type, jstring input_,
jobject surface) {
//需要转码的视频文件(输入的视频文件)
const char *input = (*env)->GetStringUTFChars(env, input_, 0);

//注册所有组件
av_register_all();
//avcodec_register_all();

//封装格式上下文,统领全局的结构体,保存了视频文件封装格式的相关信息
AVFormatContext *pFormatCtx = avformat_alloc_context();

//打开输入视频文件
if (avformat_open_input(&pFormatCtx, input, NULL, NULL) != 0) {
LOGE(“%s”, “无法打开输入视频文件”);
return;
}

//获取视频文件信息,例如得到视频的宽高
//第二个参数是一个字典,表示你需要获取什么信息,比如视频的元数据
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
LOGE(“%s”, “无法获取视频文件信息”);
return;
}

//获取视频流的索引位置
//遍历所有类型的流(音频流、视频流、字幕流),找到视频流
int v_stream_idx = -1;
int i = 0;
//number of streams
for (; i < pFormatCtx->nb_streams; i++) {
//流的类型
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
v_stream_idx = i;
break;
}
}

if (v_stream_idx == -1) {
LOGE(“%s”, “找不到视频流\n”);
return;
}

//获取视频流中的编解码上下文
AVCodecContext *pCodecCtx = pFormatCtx->streams[v_stream_idx]->codec;

//根据编解码上下文中的编码 id 查找对应的解码器
AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);

if (pCodec == NULL) {
LOGE(“%s”, “找不到解码器,或者视频已加密\n”);
return;
}

//打开解码器,解码器有问题(比如说我们编译FFmpeg的时候没有编译对应类型的解码器)
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
LOGE(“%s”, “解码器无法打开\n”);
return;
}

//准备读取
//AVPacket用于存储一帧一帧的压缩数据(H264)
//缓冲区,开辟空间
AVPacket *packet = (AVPacket *) av_malloc(sizeof(AVPacket));

//AVFrame用于存储解码后的像素数据(YUV)
//内存分配
AVFrame *yuv_frame = av_frame_alloc();
AVFrame *rgb_frame = av_frame_alloc();

int got_picture, ret;
int frame_count = 0;

//窗体
ANativeWindow *pWindow = ANativeWindow_fromSurface(env, surface);
//绘制时的缓冲区
ANativeWindow_Buffer out_buffer;

//一帧一帧的读取压缩数据
while (av_read_frame(pFormatCtx, packet) >= 0) {
//只要视频压缩数据(根据流的索引位置判断)
if (packet->stream_index == v_stream_idx) {
//7.解码一帧视频压缩数据,得到视频像素数据
ret = avcodec_decode_video2(pCodecCtx, yuv_frame, &got_picture, packet);
if (ret < 0) {
LOGE(“%s”, “解码错误”);
return;
}

//为0说明解码完成,非0正在解码
if (got_picture) {

//lock window
//设置缓冲区的属性:宽高、像素格式(需要与Java层的格式一致)
ANativeWindow_setBuffersGeometry(pWindow, pCodecCtx->width, pCodecCtx->height,
WINDOW_FORMAT_RGBA_8888);
ANativeWindow_lock(pWindow, &out_buffer, NULL);

//初始化缓冲区
//设置属性,像素格式、宽高
//rgb_frame的缓冲区就是Window的缓冲区,同一个,解锁的时候就会进行绘制
avpicture_fill((AVPicture *) rgb_frame, out_buffer.bits, AV_PIX_FMT_RGBA,
pCodecCtx->width,
pCodecCtx->height);

//YUV格式的数据转换成RGBA 8888格式的数据, FFmpeg 也可以转换,但是存在问题,使用libyuv这个库实现
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],
pCodecCtx->width, pCodecCtx->height);

//3、unlock window
ANativeWindow_unlockAndPost(pWindow);

frame_count++;
LOGI(“解码绘制第%d帧”, frame_count);
}
}

//释放资源
av_free_packet(packet);
}

av_frame_free(&yuv_frame);
avcodec_close(pCodecCtx);
avformat_free_context(pFormatCtx);
(*env)->ReleaseStringUTFChars(env, input_, input);
}

#include “libswresample/swresample.h”

#define MAX_AUDIO_FRME_SIZE 48000 * 4

//音频解码(重采样)
JNIEXPORT void JNICALL
Java_com_haohao_ffmpeg_AVUtils_audioDecode(JNI

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值