ffmpeg跨平台开发实现
在cpp类中使用回调函数时,回调函数为类成员必须为静态成员函数
具体介绍如博客 回调函数必须要用static的原因
黑屏没有图像
以为是分辨率或h264解码的问题,
发现一些接口过时的操作,于是采用了对应的ffmpeg-2.0.2版本,但还是黑屏
后来发现是yvu转rgb有问题
发现如下打印,怀疑是swscaler有问题
[swscaler @ 0x7f76d4004de0] bad dst image pointers
发现是swscaler用 this->img_convert_ctx = sws_alloc_context();获取有问题
要采用如下接口,有上下文环境,
this->img_convert_ctx = sws_getContext(this->pCodecCtx->width,this->pCodecCtx->height,this->pCodecCtx->pix_fmt,640,360,AVPixelFormat::AV_PIX_FMT_RGB24,SWS_FAST_BILINEAR,NULL,NULL,NULL);
ffmpeg解码方法:
H264Decoder::H264Decoder()
{
pCodec =NULL;
pCodecCtx =NULL;
pVideoFrame =NULL;
img_convert_ctx =NULL;
pictureWidth =0;
setRecordREsolveState =0;
av_register_all();
avcodec_register_all();
pCodec=avcodec_find_decoder(AV_CODEC_ID_H264);
if(pCodec==NULL){
printf("codec not found!\n");
}
pCodecCtx=avcodec_alloc_context3(pCodec);
if(pCodecCtx == NULL){
printf("alloc codec ctx failed \n");
}
avcodec_open2(pCodecCtx,pCodec,NULL);
pVideoFrame=avcodec_alloc_frame();
avpicture_alloc(&outPicture,PIX_FMT_RGB24,640,360);
}
int H264Decoder::DecoderH264Frames(unsigned char* inputBuffer,int alenth,RGBData_Define *outputRgbBuff)
{
int gotPicPtr = 0;
int result = 0;
av_init_packet(&this->pAvPacket);
this->pAvPacket.data = (unsigned char*)inputBuffer;
this->pAvPacket.size = alenth;
result = avcodec_decode_video2(this->pCodecCtx,this->pVideoFrame,&gotPicPtr,&this->pAvPacket);
printf("decoder result %d\n",result);
if(gotPicPtr ) {
if(this->img_convert_ctx == NULL) {
this->img_convert_ctx = sws_getContext(this->pCodecCtx->width,this->pCodecCtx->height,this->pCodecCtx->pix_fmt,640,360,AVPixelFormat::AV_PIX_FMT_RGB24,SWS_FAST_BILINEAR,NULL,NULL,NULL);
}
printf("decoder frame------------->\n");
sws_scale(img_convert_ctx,(const uint8_t **)this->pVideoFrame->data,this->pVideoFrame->linesize,0,this->pVideoFrame->height,this->outPicture.data ,this->outPicture.linesize);
outputRgbBuff->width = this->pCodecCtx->width;
outputRgbBuff->height = this->pCodecCtx->height;
outputRgbBuff->bufferSize = this->outPicture.linesize[0];
outputRgbBuff->dataBuffer = this->outPicture.data[0];
}else {
}
av_free_packet(&this->pAvPacket);
return 0;
}
ffmepg优化
图像相关的框架
在编程时遇到计算大小的问题
sizeof(“CCTD”) != 4,sizeof(“CCTD”)-1==4
字符串后面会自动加一个‘0’
=
ffmpeg相关的数据结构
SDL