#include "FFmpeg.h"
#include <QDebug>
extern bool h264DecodecStop;
FFmpeg::FFmpeg()
{
pCodecCtx = NULL;
videoStream=-1;
for(int i=0; i <AV_NUM_DATA_POINTERS; i++)
{
pictureData[i] = {0};
}
}
FFmpeg::~FFmpeg()
{
}
int FFmpeg::initial(QString & url)
{
qDebug() << "FFmpeg initial start.";
int ret;
rtspURL=url;
const AVCodec *pCodec;
//初始化
//av_register_all(); //新版的不需要注册
avformat_network_init(); //支持网络流
//打开码流前,设置参数
AVDictionary *optionsDict = NULL;
av_dict_set(&optionsDict, "rtsp_transport", "tcp", 0);
av_dict_set(&optionsDict, "stimeout", "30000000", 0);
pFormatCtx = avformat_alloc_context(); //初始化AVFormatContext, 分配一块内存,保存视频属性信息
ret = avformat_open_input(&pFormatCtx, rtspURL.toStdString().c_str(), NULL, &optionsDict);
char buf[1024] = {0};
if (ret < 0)
{
qDebug() << "Can not open this file";
av_strerror(ret, buf, 1024);
return -1;
}
//查找流信息
if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
{
qDebug() << "Unable to get stream info";
return -1;
}
//获取视频流ID
int i = 0;
videoStream = -1;
for (i = 0; i < pFormatCtx->nb_streams; i++)
{
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStream = i;
break;
}
}
if (videoStream == -1)
{
qDebug() << "Unable to find video stream";
return -1;
}
//AVCodecParameters转AVCodecContext
pCodecCtx = avcodec_alloc_context3(NULL);
avcodec_parameters_to_context(pCodecCtx,pFormatCtx->streams[videoStream]->codecpar);
//查找解码器
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL)
{
qDebug() << "Unsupported codec";
return -1;
}
//打开解码器
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
qDebug() << "Unable to open codec";
return -1;
}
//打印关于输入或输出格式的详细信息
av_dump_format(pFormatCtx, 0, rtspURL.toStdString().c_str(), 0);
//存储解码后AVFrame
pFrame = av_frame_alloc();
height = pCodecCtx->height;
width = pCodecCtx->width;
qDebug() << "FFmpeg initial successfully";
return 0;
}
int FFmpeg::h264Decodec()
{
qDebug() << "h264Decodec start...";
//为packet分配内存
AVPacket * packet = NULL;
packet = (AVPacket *)av_malloc(sizeof(AVPacket));
//循环 获取视频的数据
while (av_read_frame(pFormatCtx, packet) >= 0)
{
//停止读取视频流
if(h264DecodecStop)
{
//进行内存释放
av_packet_unref(packet);
break;
}
//当视频流的ID跟读取到的视频流ID一致的时候
if(packet->stream_index==videoStream)
{
//解码
int re = avcodec_send_packet(pCodecCtx, packet);
av_packet_unref(packet);
if(re!=0)
{
continue;
}
//从解码器中获取解码的输出数据,存入pFrame
re = avcodec_receive_frame(pCodecCtx, pFrame);
if(re!=0)
{
qDebug() << "avcodec_receive_frame failed !";
continue;
}
//初始化img_convert_ctx
struct SwsContext * img_convert_ctx;
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
if (img_convert_ctx == NULL)
{
break;
}
mutex.lock();
char *rgb = new char[width*height*3];
memset(rgb, 0 , width*height*3);
pictureData[0] = (uint8_t *)rgb;
int lines[AV_NUM_DATA_POINTERS] = {0};
lines[0] = width * 3;
//开始转换图像数据
int rs = 0;
rs = sws_scale(img_convert_ctx,
(const uint8_t**) pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
pictureData, lines);
if (img_convert_ctx)
{
sws_freeContext(img_convert_ctx);
img_convert_ctx = NULL;
}
mutex.unlock();
if (rs == -1)
{
qDebug() << "__________Can open to change to des imag_____________e\n";
return -1;
}
}
}
if (packet!=NULL)
{
av_packet_unref (packet);
packet = NULL;
}
if(pFrame!=NULL)
{
av_frame_free(&pFrame);
pFrame = NULL;
}
qDebug() << "h264Decodec end...";
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
RTSPClient.tar.gz (163个子文件)
libavformat.so.59.3.100 2.22MB
libavutil.so.57.0.100 559KB
libswscale.so.6.0.100 358KB
libswresample.so.4.0.100 74KB
libavdevice.so.59.0.100 67KB
libavcodec.so.59.1.101 10.31MB
libavfilter.so.8.0.102 2.94MB
libswresample.so.4 74KB
libavutil.so.57 559KB
libavcodec.so.59 10.31MB
libavformat.so.59 2.22MB
libavdevice.so.59 67KB
libswscale.so.6 358KB
libavfilter.so.8 2.94MB
FFmpeg.cpp 5KB
main.cpp 1KB
video.cpp 1KB
mainwindow.cpp 492B
login.cpp 434B
avcodec.h 110KB
avformat.h 106KB
avfilter.h 40KB
opt.h 36KB
pixfmt.h 35KB
avio.h 31KB
frame.h 29KB
hwcontext.h 24KB
mem.h 23KB
packet.h 22KB
swresample.h 21KB
intreadwrite.h 18KB
common.h 18KB
avdevice.h 18KB
codec.h 17KB
codec_id.h 17KB
avstring.h 15KB
pixdesc.h 15KB
log.h 12KB
hdr_dynamic_metadata.h 12KB
imgutils.h 12KB
buffer.h 12KB
swscale.h 11KB
bsf.h 11KB
channel_layout.h 10KB
samplefmt.h 10KB
avutil.h 9KB
hash.h 8KB
dict.h 8KB
buffersink.h 8KB
spherical.h 8KB
mathematics.h 8KB
timecode.h 8KB
bprint.h 8KB
parseutils.h 7KB
hwcontext_vulkan.h 7KB
codec_par.h 7KB
encryption_info.h 7KB
buffersrc.h 6KB
eval.h 6KB
hwcontext_d3d11va.h 6KB
rational.h 6KB
xvmc.h 6KB
video_enc_params.h 6KB
audio_fifo.h 6KB
fifo.h 6KB
error.h 5KB
tree.h 5KB
defs.h 5KB
stereo3d.h 5KB
film_grain_params.h 5KB
cpu.h 5KB
vdpau.h 5KB
tx.h 5KB
attributes.h 5KB
hwcontext_drm.h 5KB
version.h 4KB
dirac.h 4KB
videotoolbox.h 4KB
mastering_display_metadata.h 4KB
threadmessage.h 4KB
codec_desc.h 4KB
hwcontext_vaapi.h 4KB
qsv.h 4KB
dv_profile.h 4KB
murmur3.h 3KB
display.h 3KB
detection_bbox.h 3KB
mediacodec.h 3KB
downmix_info.h 3KB
crc.h 3KB
version.h 3KB
avfft.h 3KB
hwcontext_opencl.h 3KB
hmac.h 3KB
bswap.h 3KB
d3d11va.h 3KB
xtea.h 3KB
avdct.h 3KB
file.h 3KB
timestamp.h 3KB
共 163 条
- 1
- 2
13jjyao
- 粉丝: 86
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0