新增bool ToRGB(const AVFrame *yuv,char *out,int outWidth,int outHeight)方法
MyFFmpeg.h文件:
#pragma once
extern "C"{
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
#include <string>
#include <QMutex>
class MyFFmpeg
{
public:
/*设置成为单件模式*/
static MyFFmpeg *Get()
{
static MyFFmpeg ff;
return &ff;
}
/**
*打开指定路径的视频文件,如果已有打开的视频文件则先关闭
*/
bool Open(const char *path);
//关闭之前打开的视频文件
void Close();
//读取视频帧
AVPacket Read();
//解码功能
AVFrame *Decode(const AVPacket *pkt);
//缩放转换为RGB
bool ToRGB(const AVFrame *yuv,char *out,int outWidth,int outHeight);
/*获取相关错误信息*/
std::string GetError();
/*类析构函数*/
virtual ~MyFFmpeg();
/*视频文件总的毫秒数*/
int totalMs = 0;
int videoStream = 0;
protected:
/*相关错误信息*/
char errorbuf[1024];
//应对多线程访问时的同步锁
QMutex mutex;
AVFormatContext *ac = NULL;
AVFrame *yuv = NULL;
//转换器
SwsContext *cCtx = NULL;
/*设置成为单件模式,所以要把构造函数设置为私有*/
MyFFmpeg();
};
#include "MyFFmpeg.h"
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"swscale.lib")
bool MyFFmpeg::Open(const char *path){
Close();
mutex.lock();
int re = avformat_open_input(&ac, path, 0, 0);
if (re != 0){//打开文件失败
mutex.unlock();
av_strerror(re, errorbuf, sizeof(errorbuf));
return false;
}
//得到视频总时长的毫秒数
totalMs = ((ac->duration / AV_TIME_BASE)*1000);
for (int i = 0; i < ac->nb_streams; i++)
{
AVCodecContext *enc = ac->streams[i]->codec;
if (enc->codec_type == AVMEDIA_TYPE_VIDEO){
videoStream = i;
AVCodec *codec = avcodec_find_decoder(enc->codec_id);
if (!codec){
mutex.unlock();
printf("无法解码此视频文件\n");
return false;
}
int err = avcodec_open2(enc, codec, NULL);
if (err != 0){
mutex.unlock();
char buf[1024] = { 0 };
av_strerror(err, buf, sizeof(buf));
printf(buf);
return false;
}
printf("\n");
printf("成功打开视频编码流\n");
}
}
mutex.unlock();
return true;
}
void MyFFmpeg::Close(){
mutex.lock();
if (ac) avformat_close_input(&ac);
if (yuv) av_frame_free(&yuv);
if (cCtx){
sws_freeContext(cCtx);
cCtx = NULL;
}
mutex.unlock();
}
std::string MyFFmpeg::GetError(){
mutex.lock();
std::string re = this->errorbuf;
mutex.unlock();
return re;
}
AVPacket MyFFmpeg::Read(){
AVPacket pkt;
memset(&pkt, 0, sizeof(AVPacket));
mutex.lock();
if (!ac){
mutex.unlock();
return pkt;
}
int err = av_read_frame(ac, &pkt);
if (err != 0){//读取失败
av_strerror(err, errorbuf, sizeof(errorbuf));
}
mutex.unlock();
return pkt;
}
AVFrame * MyFFmpeg::Decode(const AVPacket *pkt){
mutex.lock();
if (!ac){
mutex.unlock();
return NULL;
}
if (yuv == NULL){
yuv = av_frame_alloc();
}
int re = avcodec_send_packet(ac->streams[pkt->stream_index]->codec, pkt);
if (re != 0){//失败
mutex.unlock();
return NULL;
}
re = avcodec_receive_frame(ac->streams[pkt->stream_index]->codec, yuv);
if (re != 0){//失败
mutex.unlock();
return NULL;
}
mutex.unlock();
return yuv;
}
bool MyFFmpeg::ToRGB(const AVFrame *yuv, char *out, int outWidth, int outHeight){
mutex.lock();
if (!ac){
mutex.unlock();
return false;
}
AVCodecContext *videoCtx = ac->streams[this->videoStream]->codec;
//打开ffmpeg格式转换和缩放器
cCtx = sws_getCachedContext(cCtx, videoCtx->width, videoCtx->height, videoCtx->pix_fmt,
outWidth, outHeight, AV_PIX_FMT_BGRA, SWS_BICUBIC,
NULL, NULL, NULL);
if (!cCtx){
mutex.unlock();
printf("sws_getCachedContext failed!\n");
return false;
}
uint8_t *data[AV_NUM_DATA_POINTERS] = {0};
data[0] = (uint8_t *)out;
int linesize[AV_NUM_DATA_POINTERS] = { 0 };
linesize[0] = outWidth * 4;
int h = sws_scale(cCtx, yuv->data, yuv->linesize, 0, videoCtx->height, data, linesize);
if (h > 0){
printf("(%d)", h);
}
mutex.unlock();
return true;
}
MyFFmpeg::MyFFmpeg()
{
errorbuf[0] = '\0';
av_register_all();
}
MyFFmpeg::~MyFFmpeg()
{
}
main.cpp调用:
#include "myplayer.h"
#include <QtWidgets/QApplication>
#include "MyFFmpeg.h"
int main(int argc, char *argv[])
{
char path[1024] = "test.mp4";
if (MyFFmpeg::Get()->Open(path)){
printf("文件[%s]打开成功",path);
}
else
{
printf("\n文件[%s]打开失败;错误信息:%s", path,MyFFmpeg::Get()->GetError().c_str());
getchar();
return -1;
}
char *rgb = new char[800 * 600 * 4];
for (;;){
AVPacket pkt = MyFFmpeg::Get()->Read();
if (pkt.size == 0){
break;
}
printf("pts = %lld\n",pkt.pts);
if (pkt.stream_index != MyFFmpeg::Get()->videoStream){
av_packet_unref(&pkt);
continue;
}
AVFrame *yuv = MyFFmpeg::Get()->Decode(&pkt);
if (yuv){
printf("解码成功 --- ");
MyFFmpeg::Get()->ToRGB(yuv, rgb, 800, 600);
}
//释放空间
av_packet_unref(&pkt);
}
QApplication a(argc, argv);
MyPlayer w;
w.show();
return a.exec();
}
运行结果: