浅析ffmpeg解码流程
【摘要】 使用ffmpeg完成了解码各种格式图片的功能,结合自己实际遇到的问题,分享下关于ffmpeg解码的经验。
使用ffmpeg完成了解码各种格式图片的功能,结合自己实际遇到的问题,分享下关于ffmpeg解码的经验。
1.解码基本流程:
2. 函数调用分析:
//照片的路径
char *FileName = “/…/photo.jpg”;
//解码过程中重要的结构体
AVCodecContext *pCodecCtx = NULL;
AVFrame *pFrame = NULL;
AVCodec *pCodec = NULL;
AVFormatContext *pFormatCtx = NULL;
//初始化
av_register_all();
//打开文件
avformat_open_input(&pFormatCtx, szFileName, NULL, NULL);
//寻找媒体流信息
av_find_stream_info(pFormatCtx);
//for循环遍历所有流,找到视频流
int videoStream = -1;
for(i = 0; i < pFormatCtx->nb_streams; i++) {
if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStream = i;
break;
}
}
//配置视频流对应的解码信息
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
//配置解码器
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
//打开解码器,设置解码参数
avcodec_open(pCodecCtx, pCodec);
// AVFrame申请内存存储解码数据
pFrame = avcodec_alloc_frame();
//解码前的流数据储存在AVPacket中
AVPacket packet;
//初始化AVPacket
av_init_packet(&packet);
//下面两步是解码操作,如果解码视频要做while循环不断读取流数据进行解码
//从pFormatCtx中读取流数去存储到packet中
av_read_frame(pFormatCtx, &packet);
//解码packet中的流数据,返回值decLen为已解码的数据大小,frameFinished标识是否完成一帧的解码,没有完成frameFinished = 0;
decLen = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
//当frameFinished = 1时,完成一帧的解码,可以按需求对数据进行保存
saveframe(pFrame);
//释放资源关闭文件
av_free_packet(&packet);
av_free(pFrame);
avcodec_close(pCodecCtx);
av_close_input_file(pFormatCtx);
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)