首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > 多媒体 >

关于FFMPEG解码H264保存成BMP有关问题!

2013-08-27 
【求助】关于FFMPEG解码H264保存成BMP问题!!!大侠们,谁帮我解决一下我的程序问题,qq292060282。如果加好友帮

【求助】关于FFMPEG解码H264保存成BMP问题!!!
大侠们,谁帮我解决一下我的程序问题,qq292060282。
如果加好友帮助我的都会有分,如果帮我解决了,我可以另外给分!!!谢谢了。
[解决办法]
H264解码后一般就是YV12数据,转成RGB24或RGB32就行了...
[解决办法]
这个我会做,你看下我的博客
[解决办法]
关于FFMPEG解码H264保存成BMP问题,很简单
LPBITMAPINFO lpbmih = new BITMAPINFO; 
lpbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
lpbmih->bmiHeader.biWidth = n_width; 
lpbmih->bmiHeader.biHeight = n_height; 
lpbmih->bmiHeader.biPlanes = 1; 
lpbmih->bmiHeader.biBitCount = iPixel; 
lpbmih->bmiHeader.biCompression = BI_RGB; 
lpbmih->bmiHeader.biSizeImage = (n_width*n_height*iPixel)/8; 
lpbmih->bmiHeader.biXPelsPerMeter = 0; 
lpbmih->bmiHeader.biYPelsPerMeter = 0; 
lpbmih->bmiHeader.biClrUsed = 0; 
lpbmih->bmiHeader.biClrImportant = 0;

        av_register_all();
avcodec_register_all();
AVCodec *codec = avcodec_find_decoder(CODEC_ID_H264);
AVCodecContext *dec = avcodec_alloc_context();
if (avcodec_open(dec, codec) < 0) {
fprintf(stderr, "ERR: open H264 decoder err\n");
exit(-1);
}

AVFrame *pFrameYUV = new AVFrame[1];;  //YUV帧数据avcodec_alloc_frame();
AVFrame *pFrameRGB = new AVFrame[1];  //RGB帧数据 avcodec_alloc_frame();


// h264解压
int got;
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = buffer;
pkt.size = rsize;
int ret = avcodec_decode_video2(dec, pFrameYUV, &got, &pkt);
if (ret > 0 && got) {

// 解码成功
int numBytes=avpicture_get_size(PIX_FMT_BGR24, dec->width,dec->height);
img_width = dec->width;
img_height = dec->height;
img_size = (img_width*img_height*iPixel)/8;
uint8_t *bmp_buffer = (uint8_t *)malloc(img_size*sizeof(uint8_t));


uint8_t *yuv_buffer = (uint8_t *)malloc(img_size*sizeof(uint8_t)/2);
// Assign appropriate parts of buffer to image planes in pFrameRGB

avpicture_fill((AVPicture *)pFrameRGB, bmp_buffer, PIX_FMT_BGR24,img_width, img_height);
 

  // 翻转YUV图像
pFrameYUV->data[0]  += pFrameYUV->linesize[0] * (img_height - 1);
pFrameYUV->linesize[0] *= -1;                   
pFrameYUV->data[1]  += pFrameYUV->linesize[1] * (img_height / 2 - 1);
pFrameYUV->linesize[1] *= -1;
pFrameYUV->data[2]  += pFrameYUV->linesize[2] * (img_height / 2 - 1);
pFrameYUV->linesize[2] *= -1;
//初始化SwsContext
SwsContext * scxt = sws_getContext(img_width,img_height,dec->pix_fmt,img_width,img_height,PIX_FMT_BGR24,SWS_BICUBIC,NULL,NULL,NULL);

  //将YUV转化为RGB
sws_scale(scxt,pFrameYUV->data,pFrameYUV->linesize,0,img_height,pFrameRGB->data,pFrameRGB->linesize);

lpbmih->bmiHeader.biWidth = img_width; 
lpbmih->bmiHeader.biHeight = img_height; 
lpbmih->bmiHeader.biSizeImage = (img_width*img_height*iPixel)/8;
pDC->SetStretchBltMode(HALFTONE);
::StretchDIBits(pDC->GetSafeHdc(),  0, 0,screensize.cx,screensize.cy, 0, 0, lpbmih->bmiHeader.biWidth,lpbmih->bmiHeader.biHeight,pFrameRGB->data[0], (BITMAPINFO*)lpbmih, DIB_RGB_COLORS,SRCCOPY);  

热点排行