Android FFmpeg + OpenGL ES YUV Player
xsobi 2024-12-25 16:14 37 浏览
1、FFmpeg解出YUV帧数据
1.1 方法介绍
打开封装格式上下文
1)[avformat.h] avformat_open_input 打开媒体流(本地或者网络资源)得到封装格式上下文AVFormatContext AVFormatContext包含了媒体的基本信息,如时长等
初始化解码器
2)[avformat.h] avformat_find_stream_info 从封装格式上下文中找到所有的媒体数据流AVStream,如音频流、视频流、字幕流等 AVStream中则包含了这些数据流的解码信息,如分辨率、采样率、解码器参数、帧率等等
3)[avcodec.h] avcodec_find_decoder 根据AVStream中的解码器id找到对应的解码器AVCodec,FFmpeg可以指定编译编解码器,因此实际编译的FFmpeg包中可能不包含流所对应的解码器;注意此时解码器还未打开,缺少相关的上下文信息
4)[avcodec.h] avcodec_alloc_context3、avcodec_parameters_to_context avcodec_alloc_context3创建一个空的codec上下文结构体AVCodecContext avcodec_parameters_to_context将AVStream中的codec参数信息导入到创建出来的AVCodecContext中
5)[avcodec.h] avcodec_open2 根据解码器上下文中所描述的解码器参数信息打开指定的解码器
6)[avformat.h] av_read_frame 从媒体上下文中读取一个数据包,这是一个压缩后的数据包AVPacket,需要经过解码器解码才能得到原始的媒体数据
解码
6)[avcodec.h] avcodec_send_packet 将av_read_frame读取到的数据压缩包送入解码器进行解码
7)[avcodec.h] avcodec_receive_frame 从解码去读取一帧媒体原始数据,注意(6)、(7)并不是一一对应的调用关系
重裁剪
8)[swscale.h] sws_getContext 由于我们送显时一般都是固定像素格式或者分辨率的,因此需要对解码出的视频原始数据进行重裁剪,将分辨率或者像素格式统一,这个方法主要是初始化重裁剪相关的上下文
9)[swscale.h] sws_scale 将(7)中的AVFrame视频原始数据进行重裁剪
2、Demo实现
2.1 流程简介
C++音视频学习资料免费获取方法:关注音视频开发T哥,点击「链接」即可免费获取2023年最新C++音视频开发进阶独家免费学习大礼包!
2.2 代码示例
1)打开封装格式上下文
// 1、创建1个空的上下文对象
AVFormatContext* formatCtx = avformat_alloc_context();
// 2、打开指定资源地址的封装格式上下文
avformat_open_input(&formatCtx, sourcePath.c_str(), nullptr, nullptr);
2)找到视频流
// 3、从封装格式中解析出所有的流信息
avformat_find_stream_info(formatCtx, nullptr);
// 4、遍历流找到视频流
AVStream *videoStream = nullptr;
for (int i = 0; i < formatCtx->nb_streams; i++) {
if (formatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = formatCtx->streams[i];
videoStreamIndex = i;
}
}
3)根据流信息打开对应的解码器
// 5、找到解码器,这个时候解码器对象还没有打开,不能用
AVCodec *codec = avcodec_find_decoder(videoStream->codecpar->codec_id);
// 6、为该codec创建1个对应的空上下文
AVCodecContext* codecCtx = avcodec_alloc_context3(codec);
// 7、将指定流中限定的解码器参数输入解码器上下文中
avcodec_parameters_to_context(codecCtx, videoStream->codecpar);
// 8、通过解码器上下文打开解码器
avcodec_open2(codecCtx, codec, NULL);
// 以下为视频相关的信息
// 帧率
// 优先通过 avg_frame_rate 计算帧率,若 avg_frame_rate 为{x, 0} or {0, 1} 那么就使用 r_frame_rate
// av_q2d 就是 avg_frame_rate.num(分子) / avg_frame_rate.den(分母)
if (stream->avg_frame_rate.den == 0 ||
(stream->avg_frame_rate.num == 0 && stream->avg_frame_rate.den == 1)) {
frameRate = av_q2d(stream->r_frame_rate);
} else {
frameRate = av_q2d(stream->avg_frame_rate);
}
// 每一帧的时间(毫秒)
frameMillions = 1000 / frameRate;
4)创建重采样上下文
swsContext = sws_getContext(
decoder->Width(), // 输入源宽度
decoder->Height(), // 输入源高度
decoder->PixelFormat(), // 输入源格式 YUV42 NV12 NV32...
decoder->Width(), // 输出源宽度
decoder->Height(), // 输出源高度
AV_PIX_FMT_YUV420P, // 输出源格式
SWS_BICUBIC, nullptr, nullptr, nullptr);
if (!swsContext) {
return false;
}
// 分配重采样的输出Frame
swsAVFrame = av_frame_alloc();
swsAVFrame->width = decoder->Width();
swsAVFrame->height = decoder->Height();
swsAVFrame->format = AV_PIX_FMT_YUV420P;
// 按照1字节对齐,给AVFrame创建缓冲区,重采样的数据送入此swsAVFrame中
int ret = av_frame_get_buffer(swsAVFrame, 1);
5)从AVFormatContext中读取数据包,放入到缓存队列中
while (1) {
AVPacket *packet = av_packet_alloc();
int ret = av_read_frame(formatCtx, packet);
if (ret == AVERROR_EOF) {
break;
} else if (ret) {
LOGE("%s av_read_frame error", __func__);
av_packet_free(&packet);
av_free(packet);
packet = nullptr;
break;
}
// 读取到一个视频数据包,方璐packet队列中
if (packet->stream_index == videoStreamIndex) {
packetQueue->PushPacket(packet);
}
}
6)从缓存队列中取出AVPacket,送入解码器解码
AVFrame * Decode() {
// 先从缓存中获取,avcodec_receive_frame 与 avcodec_send_packet 不是一一对应的
AVFrame *avFrame = av_frame_alloc();
if (avcodec_receive_frame(codecCtx, av_frame) == 0) {
return avFrame;
}
while (1) {
AVPacket *packet = packetQueue->GetTopOne();
if (!packet) {
av_frame_free(&avFrame);
av_free(avFrame);
avFrame = NULL;
break;
}
// 向解码器发送压缩数据包
int ret = avcodec_send_packet(codecCtx, packet);
if (ret) {
char buffer[128];
av_make_error_string(buffer, 128, ret);
LOGE("%s avcodec_send_packet error %s", __func__, buffer);
av_frame_free(&avFrame);
av_free(avFrame);
avFrame = NULL;
av_packet_free(&packet);
av_free(packet);
break;
}
// 从解码器获取解压缩后的原始包
int code = avcodec_receive_frame(codecCtx, avFrame);
av_packet_free(&packet);
av_free(packet);
if (code == 0) {
break;
} else if (code == AVERROR_EOF) {
av_frame_free(&avFrame);
av_free(avFrame);
avFrame = NULL;
break;
}
}
return avFrame;
}
7)对解出的AVFrame进行重裁剪
sws_scale(swsContext, avFrame->data, avFrame->linesize, 0,
avFrame->height, swsAVFrame->data, swsAVFrame->linesize);
8)将重采样后的AVFrame数据送入OpenGL显示
// 送显示速度控制,按照视频的帧率进行控制,确保流畅播放
long currentMillions = base::CurrentMillions();
long diff = currentMillions - lastMillions;
if (diff >= frameMillions) {
lastMillions = currentMillions;
} else {
long sleepMillions = frameMillions - diff;
sleepMillions = sleepMillions > frameMillions ? frameMillions : sleepMillions;
usleep(sleepMillions * 1000); // usleep 时微秒 1ms = 1000微秒
lastMillions = currentMillions + sleepMillions;
}
// 送入OpenGL显示
(*functor)(swsAVFrame->width, swsAVFrame->height, swsAVFrame->data[0],
swsAVFrame->data[1], swsAVFrame->data[2]);
相关推荐
- 10 非常重要的 Python 列表方法(python的列表怎么用)
-
Python列表是可变的或可更改的数据类型。与不可变或不可更改的字符串数据类型不同,每次我们在列表上使用一个方法时,我们都会影响列表本身,而不是列表的副本。这里有十个非常重要的Python列表方...
- python基础——列表详解(python列表方法有哪几种)
-
一、简述...
- Python数据结构终极对决:List和Dict你站谁?
-
一、开篇暴击:90%新手都踩过的坑(代码示例:一个让程序卡爆的真实案例)#错误示范:用列表做数据检索users=["张三_18","李四_22","王五_25"...]#10000条数...
- Python之列表(list)和字典(Dictionary)嵌套
-
1.列表嵌套字典#案例:多个学生的信息表students=[{"name":"小明","age":18,"score":95},{"name":"小红","...
- 什么是Python列表(python中的列表)
-
列表是一个任意类型的对象的位置相关的有序集合,它没有固定大小,是可变的,与数组类似。列表中的元素没有固定类型的限制,可以由任意对象来构成,是Python语言中一种高频使用的数据类型。...
- Python100天31:如何理解数组(list)、二级数组、series
-
数组List:Excel、pandas、List无论是pandas还是access,亦或是excel,本质上是一个数组的表现形式。Excel是一个二维表格行*列...
- 玩转Python—列表使用教程(python列表讲解)
-
上一讲给大家介绍了Python的列表,今天继续给大家介绍Python中列表的使用。...
- Python编程:集合工具类之Deque及UserString和UserList
-
前言本文继续来盘Python内置集合模块,本期介绍其中的工具类双端队列类(Deque)、用户列表类(UserList)和UserString类的使用。我们还是采用“短平快”的模式——文字+代码,助你多...
- Python新手必看|列表操作全攻略(增删改查+切片+推导式)
-
一、为什么列表是Python的"万能容器"?作为最灵活的序列类型,列表支持:...
- Python 列表方法可视化解释(python漂亮的可视化表)
-
Python列表方法append()在列表末尾添加一个元素。extend()将一个列表(或任何可迭代对象)添加到当前列表的末尾。...
- Python四大数据结构 list,tuple,set,dict 的特点与使用语法
-
python里面有四大数据结构:列表list,元组tuple,集合set,字典dict...
- Python启航:30天编程速成之旅(第10天)- list 数据类型
-
喜欢的条友记得关注、点赞、转发、收藏,你们的支持就是我最大的动力源泉。前期基础教程:...
- python序列之列表详解(python列序类型)
-
Python中除了字符串外,还有另外两种序列:列表和元组,他们都可以包含零个或多个元素,而且并不要求所含元素的类型相同,每个元素都可以是任何Python类型对象。为什么Python要同时设计列表和元组...
- Python编程:List数据类型(list+list python)
-
1.什么是List?...
- python 数据结构之列表(list)简述及演示
-
(一)list列表定义使用中括号[],里面元素可以是任意类型,包括列表本身,也可以是字典、元组等。(二)在Python中,第一个列表元素的索引为0,而不是1。(三)要访问列表的任何元素,都可将...
- 一周热门
- 最近发表
-
- 10 非常重要的 Python 列表方法(python的列表怎么用)
- python基础——列表详解(python列表方法有哪几种)
- Python数据结构终极对决:List和Dict你站谁?
- Python之列表(list)和字典(Dictionary)嵌套
- 什么是Python列表(python中的列表)
- Python100天31:如何理解数组(list)、二级数组、series
- 玩转Python—列表使用教程(python列表讲解)
- Python编程:集合工具类之Deque及UserString和UserList
- Python新手必看|列表操作全攻略(增删改查+切片+推导式)
- Python 列表方法可视化解释(python漂亮的可视化表)
- 标签列表
-
- grid 设置 (58)
- 移位运算 (48)
- not specified (45)
- 导航栏 (58)
- context xml (46)
- scroll (43)
- dedecms模版 (53)
- c 视频教程下载 (33)
- listview排序 (33)
- characterencodingfilter (33)
- getmonth (34)
- label换行 (33)
- android studio 3 0 (34)
- html转js (35)
- 索引的作用 (33)
- checkedlistbox (34)
- xmlhttp (35)
- mysql更改密码 (34)
- 权限777 (33)
- htmlposition (33)
- 学校网站模板 (34)
- textarea换行 (34)
- 轮播 (34)
- asp net三层架构 (38)
- bash (34)