简单的GDI+ffmpeg视频播放
简单的GDI+ffmpeg视频播放

链接地址:https://github.com/KaiWenK
少了一个commen文件夹,GITHUB超过100文件无法上传,都是一些ffmpeg的头文件和lib
这里直接贴代码
CMainFramWnd* pMain = (CMainFramWnd*)p; const MediaParam *pmediaParam = pMain->getMediaParam(); unsigned char *out_bufRgb; AVFrame *pFrame = NULL; AVFrame *pFrameYUV = NULL; HDC hdcsrc; HDC hdcdst; pFrame = av_frame_alloc(); pFrameYUV = av_frame_alloc(); const CControlWndUI *pWnd = pMain->GetPlayWnd(); RECT rc; GetWindowRect(pWnd->GetHWND(), &rc); hdcdst = GetDC((HWND)pWnd->GetHWND()); hdcsrc = CreateCompatibleDC(hdcdst); //申请用于存储RGB数据的buffer out_bufRgb = (unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_BGR24 , rc.right - rc.left , rc.bottom - rc.top , 1)); //将out_bufRGB与pFrameYUV->data绑定pFrameYUV->data = out_bufRGB av_image_fill_arrays(pFrameYUV->data , pFrameYUV->linesize, out_bufRgb , AV_PIX_FMT_BGR24, rc.right - rc.left , rc.bottom - rc.top, 1); SwsContext* img_convert_ctx = sws_getContext(pmediaParam->pcodecCtx->width, pmediaParam->pcodecCtx->height , pmediaParam->pcodecCtx->pix_fmt, rc.right - rc.left, rc.bottom - rc.top , AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL); PVOID pbit = NULL; BITMAP bmp = { 0 }; HBITMAP hBitmap = NULL; BITMAPINFO bmpinfo = { 0 }; bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmpinfo.bmiHeader.biWidth = rc.right - rc.left; bmpinfo.bmiHeader.biHeight = -rc.bottom + rc.top; bmpinfo.bmiHeader.biPlanes = 1; bmpinfo.bmiHeader.biBitCount = 24; bmpinfo.bmiHeader.biCompression = BI_RGB; int errcode1 = 0; int errcode2 = 0; while (true) { AVPacket* pkt = pMain->m_MediaParam.pvpq->GetPkt(); errcode1 = avcodec_send_packet(pMain->m_MediaParam.pcodecCtx, pkt); if( errcode1 == 0) { errcode2 = avcodec_receive_frame(pMain->m_MediaParam.pcodecCtx, pFrame); if ( errcode2 == 0 ) { sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize , 0, pmediaParam->pcodecCtx->height, pFrameYUV->data, pFrameYUV->linesize); hBitmap = CreateDIBSection(hdcsrc, &bmpinfo, DIB_RGB_COLORS, &pbit, NULL, 0); GetObject( hBitmap, sizeof(BITMAP), &bmp); memcpy(pbit, out_bufRgb, (rc.right - rc.left)*(rc.bottom-rc.top)*3 ); SelectObject(hdcsrc, hBitmap); BitBlt(hdcdst, 0, 0, rc.right - rc.left, rc.bottom - rc.top, hdcsrc, 0, 0, SRCCOPY); DeleteObject(hBitmap); } else if( AVERROR_EOF == errcode2 ) { break; } } else if (AVERROR_EOF == errcode1) { break; } }效果:
链接地址:https://github.com/KaiWenK
少了一个commen文件夹,GITHUB超过100文件无法上传,都是一些ffmpeg的头文件和lib