Ffmpeg swscaler@0dd9e620:使用了不推荐的像素格式,请确保正确设置了范围;
我正在用FFMpeg解码RTSP视频流。 在显示时(调用Ffmpeg swscaler@0dd9e620:使用了不推荐的像素格式,请确保正确设置了范围;,ffmpeg,swscale,Ffmpeg,Swscale,我正在用FFMpeg解码RTSP视频流。 在显示时(调用cv::imshow(…)),我得到以下异常: [swscaler@0d55e5c0]使用了不推荐的像素格式,请确保使用了 正确设置范围 我正在将像素格式从“AV_PIX_FMT_YUVJ420P”转换为“AV_PIX_FMT_YUV420P”。仍在获取上述异常。感谢您的帮助 int Decodestream() { av_register_all(); avdevice_register_all(); avcode
cv::imshow(…)
),我得到以下异常:
[swscaler@0d55e5c0]使用了不推荐的像素格式,请确保使用了
正确设置范围
我正在将像素格式从“AV_PIX_FMT_YUVJ420P”转换为“AV_PIX_FMT_YUV420P”。仍在获取上述异常。感谢您的帮助
int Decodestream()
{
av_register_all();
avdevice_register_all();
avcodec_register_all();
avformat_network_init();
const char *filenameSrc = "rtsp://192.168.1.67/gnz_media/second";
AVCodecContext *pCodecCtx;
AVFormatContext *pFormatCtx = avformat_alloc_context();
AVCodec * pCodec;
AVFrame *pFrame, *pFrameRGB;
if(avformat_open_input(&pFormatCtx,filenameSrc,NULL,NULL) != 0)
{return -1;}
if(av_find_stream_info(pFormatCtx) < 0)
{return -1;}
av_dump_format(pFormatCtx, 0, filenameSrc, 0);
int videoStream = 1;
for(int i=0; i < pFormatCtx->nb_streams; i++)
{
if(pFormatCtx->streams[i]->codec->coder_type==AVMEDIA_TYPE_VIDEO)
{
videoStream = i;
break;
}
}
if(videoStream == -1) return -1 ;
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec =avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
{return -1;} //codec not found
if(avcodec_open2(pCodecCtx,pCodec,NULL) < 0)
{ return -1;}
pFrame = avcodec_alloc_frame();
pFrameRGB = avcodec_alloc_frame();
uint8_t *buffer;
int numBytes;
AVPixelFormat pFormat;
switch (pFormatCtx->streams[videoStream]->codec->pix_fmt)
{
case AV_PIX_FMT_YUVJ420P : pFormat = AV_PIX_FMT_YUV420P; break;
case AV_PIX_FMT_YUVJ422P : pFormat = AV_PIX_FMT_YUV422P; break;
case AV_PIX_FMT_YUVJ444P : pFormat = AV_PIX_FMT_YUV444P; break;
case AV_PIX_FMT_YUVJ440P : pFormat = AV_PIX_FMT_YUV440P;
default:
pFormat = pFormatCtx->streams[videoStream]->codec->pix_fmt;
break;
}
numBytes = avpicture_get_size(pFormat,pCodecCtx->width,pCodecCtx->height) ;
buffer = (uint8_t *) av_malloc(numBytes*sizeof(uint8_t));
avpicture_fill((AVPicture *) pFrameRGB,buffer,pFormat,pCodecCtx->width,pCodecCtx->height);
int res, frameFinished;
AVPacket packet;
while(res = av_read_frame(pFormatCtx,&packet)>=0)
{
if(packet.stream_index == videoStream){
avcodec_decode_video2(pCodecCtx,pFrame,&frameFinished,&packet);
if(frameFinished){
struct SwsContext * img_convert_ctx;
img_convert_ctx = sws_getCachedContext(NULL,pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL,NULL);
sws_scale(img_convert_ctx, ((AVPicture*)pFrame)->data, ((AVPicture*)pFrame)->linesize, 0, pCodecCtx->height, ((AVPicture *)pFrameRGB)->data, ((AVPicture *)pFrameRGB)->linesize);
cv::Mat img(pFrame->height,pFrame->width,CV_8UC3,pFrameRGB->data[0]);
cv::imshow("display",img);
cvWaitKey(10);
}
}
}
//Memory clean up code goes here
}
int Decodestream()
{
av_寄存器_all();
avdevice_register_all();
avcodec_寄存器_all();
avformat_network_init();
const char*filenameSrc=”rtsp://192.168.1.67/gnz_media/second";
AVCodecContext*pCodecCtx;
AVFormatContext*pFormatCtx=avformat_alloc_context();
AVCodec*pCodec;
AVFrame*pFrame,*pFrameRGB;
if(avformat\u open\u输入(&pFormatCtx,filenameSrc,NULL,NULL)!=0)
{return-1;}
如果(av查找流信息(pFormatCtx)<0)
{return-1;}
av_转储_格式(pFormatCtx,0,filenameSrc,0);
int-videoStream=1;
对于(int i=0;inb_streams;i++)
{
如果(pFormatCtx->streams[i]->codec->coder\u type==AVMEDIA\u type\u VIDEO)
{
视频流=i;
打破
}
}
if(videoStream==-1)返回-1;
pCodecCtx=pFormatCtx->streams[videoStream]->编解码器;
pCodec=avcodec\u find\u解码器(pCodecCtx->codec\u id);
if(pCodec==NULL)
{return-1;}//找不到编解码器
if(avcodec_open2(pCodecCtx,pCodec,NULL)<0)
{return-1;}
pFrame=avcodec_alloc_frame();
pFrameRGB=avcodec_alloc_frame();
uint8_t*缓冲器;
整数单位;
avpixelsformat;
交换机(pFormatCtx->streams[videoStream]->codec->pix_fmt)
{
案例AV_PIX_FMT_YUVJ420P:pFormat=AV_PIX_FMT_YUV420P;中断;
案例AV_PIX_FMT_YUVJ422P:pFormat=AV_PIX_FMT_YUV422P;中断;
案例AV_PIX_FMT_YUVJ444P:pFormat=AV_PIX_FMT_YUV444P;中断;
案例AV_PIX_FMT_YUVJ440P:pFormat=AV_PIX_FMT_YUV440P;
违约:
pFormat=pFormatCtx->streams[videoStream]->codec->pix_fmt;
打破
}
numBytes=avpicture\u get\u size(pFormat,pCodeCtx->width,pCodeCtx->height);
缓冲区=(uint8_t*)av_malloc(numBytes*sizeof(uint8_t));
avpicture_填充((avpicture*)pFrameRGB、缓冲器、pFormat、PCODECTX->宽度、PCODECTX->高度);
int res,frameFinished;
数据包;
而(res=av\u read\u frame(pFormatCtx和packet)>=0)
{
if(packet.stream_index==videoStream){
avcodec_decode_video2(pCodecCtx、pFrame、frameFinished和packet);
如果(框架完成){
结构SwsContext*img\u convert\u ctx;
img_convert_ctx=sws_getCachedContext(NULL,pCodecCtx->width,pCodecCtx->height,pCodecCtx->pix_fmt,pCodecCtx->width,pCodecCtx->height,AV_pix_fmt_BGR24,sws_双三次,NULL,NULL,NULL,NULL);
sws_比例(img_转换_ctx,((AVPicture*)pFrame)->数据,((AVPicture*)pFrame)->线条尺寸,0,pCodecCtx->高度,((AVPicture*)pFrameRGB)->数据,((AVPicture*)pFrameRGB)->线条尺寸;
cv::Mat img(pFrame->高度,pFrame->宽度,cv_8UC3,pFrameRGB->数据[0]);
cv::imshow(“显示”,img);
cvWaitKey(10);
}
}
}
//内存清理代码在这里
}
sws\u getCachedContextcall中的pCodecCtx->pix\u fmt的值是多少?您知道哪行输出错误消息吗?pCodecCtx->pix\u fmt的值是AV\u pix\u fmt\u YUVJ420P。我手动将其更改为AV_PIX_FMT_YUV420P。它仍然在cv::imshow()调用时崩溃。