MFC知識
建立MFC工程的方法
• 打開VC++
• 檔案->建立->項目->MFC應用程式
• 應用程式類型->基于對話框
• 點選下一步即可
設定控件
• 找到“工具箱”,就可以将相應的控件拖拽至應用程式對話框中
• 常用控件有:Button,Edit Control,Static Text等
• 找到“屬性”頁籤
可以在“Caption”屬性上修改控件上的文字
可以在“ID”屬性上修改控件上的ID(ID是控件的辨別,不可重複)
添加消息響應函數
• 輕按兩下Button控件,就可以給該控件添加消息響應函數。
• 在菜單欄的“項目->類向導”處,可以添加更多種類的消息響應函數。
MFC最簡單的彈出消息框的函數是AfxMessageBox(“HelloWorld”)
FFmpeg+SDL+MFC實作圖形界面視訊播放器
緻命bug:應用新的庫,隻能顯示彈出的視窗,不能顯示嵌入的畫面
---------------時間:2022.6.12 bug修複----------------------
在等待事件中,如果使用SDL_WaitEvent會出現第一次等待的過程,是以在MFC不會播放,如果使用SDL_PollEvent則會繼續執行,使其正常顯示。
SDL_PollEvent:
函數的作用是,直接檢視事件隊列,如果事件隊列中有事件,直接傳回事件,并删除此事件。如果沒有事件也直接傳回。
SDL_WaitEvent:
事件觸發機制。事件發生的時候觸發,沒有事件的時候阻塞在這裡。
相關學習資料推薦,點選下方連結免費報名,先碼住不迷路~】
【免費分享】音視訊學習資料包、大廠面試題、技術視訊和學習路線圖,資料包括(C/C++,Linux,FFmpeg webRTC rtmp hls rtsp ffplay srs 等等)有需要的可以點選加群免費領取~
下面給出其核心函數:
int ffmpegplayer(LPVOID lpParam)
{
AVFormatContext* pFormatCtx;
int i, videoindex;
AVCodecContext* pCodecCtx;
AVCodec* pCodec;
AVFrame* pFrame, * pFrameYUV;
uint8_t* out_buffer;
AVPacket* packet;
int ret, got_picture;
//------------SDL----------------
int screen_w, screen_h;
SDL_Window* screen;
SDL_Renderer* sdlRenderer;
SDL_Texture* sdlTexture;
SDL_Rect sdlRect;
SDL_Thread* video_tid;
SDL_Event event;
struct SwsContext* img_convert_ctx;
//char filepath[] = "屌絲男士.mov";
CSFFPlayerDlg* dlg = (CSFFPlayerDlg*)lpParam;
//-------------------------
char filepath[500] = { 0 };
GetWindowTextA(dlg->m_url, (LPSTR)filepath, 500);
//-------------------------
//av_register_all();
avformat_network_init();
pFormatCtx = avformat_alloc_context();
if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0) {
AfxMessageBox((CString)"Couldn't open input stream.\n");
return -1;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
AfxMessageBox((CString)"Couldn't find stream information.\n");
return -1;
}
videoindex = -1;
for (i = 0; i < pFormatCtx->nb_streams; i++)
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoindex = i;
break;
}
if (videoindex == -1) {
AfxMessageBox((CString)"Didn't find a video stream.\n");
return -1;
}
AVCodecParameters* codecParameters = pFormatCtx->streams[videoindex]->codecpar;
pCodecCtx = avcodec_alloc_context3(nullptr);
avcodec_parameters_to_context(pCodecCtx, codecParameters);
pCodec = (AVCodec*)avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL) {
AfxMessageBox((CString)"Codec not found.\n");
return -1;
}
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
AfxMessageBox((CString)"Could not open codec.\n");
return -1;
}
packet = av_packet_alloc();
pFrame = av_frame_alloc();
pFrameYUV = av_frame_alloc();
out_buffer = (uint8_t*)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1));
av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)) {
AfxMessageBox((CString)"Could not initialize SDL\n");
return -1;
}
//SDL 2.0 Support for multiple windows
screen_w = pCodecCtx->width;
screen_h = pCodecCtx->height;
//顯示在彈出視窗
//screen = SDL_CreateWindow("Simplest ffmpeg player's Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
//screen_w, screen_h,SDL_WINDOW_OPENGL);
//===========================================
//顯示在MFC控件上
screen = SDL_CreateWindowFrom((void*)dlg->GetDlgItem(IDC_SCREEN)->GetSafeHwnd());
//===========================================
if (!screen) {
AfxMessageBox((CString)"SDL: could not create window - exiting\n");
return -1;
}
sdlRenderer = SDL_CreateRenderer(screen, -1, 0);
//IYUV: Y + U + V (3 planes)
//YV12: Y + V + U (3 planes)
sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height);
sdlRect.x = 0;
sdlRect.y = 0;
sdlRect.w = screen_w;
sdlRect.h = screen_h;
packet = (AVPacket*)av_malloc(sizeof(AVPacket));
video_tid = SDL_CreateThread(sfp_refresh_thread, NULL, NULL);
//------------SDL End------------
//Event Loop
for (;;) {
//Wait
// bug :使用SDL_WaitEvent會出現第一次等待的情況,使其無法顯示
// 該函數不會等待,如果有事件就取出,沒有事件會繼續執行下一步
SDL_PollEvent(&event);
// 該函數會等待,如果沒有事件就會一直等待
//SDL_WaitEvent(&event);
if (event.type == SFM_REFRESH_EVENT) {
//------------------------------
if (av_read_frame(pFormatCtx, packet) >= 0) {
if (packet->stream_index == videoindex) {
if (avcodec_send_packet(pCodecCtx, packet) < 0) {
AfxMessageBox((CString)"avcodec_send_packet failed!.\n");
continue;
}
while (1) {
ret = avcodec_receive_frame(pCodecCtx, pFrame);
if (ret != 0)break;
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
//SDL---------------------------
SDL_UpdateTexture(sdlTexture, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0]);
SDL_RenderClear(sdlRenderer);
//SDL_RenderCopy( sdlRenderer, sdlTexture, &sdlRect, &sdlRect );
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
SDL_RenderPresent(sdlRenderer);
//SDL End-----------------------
TRACE("Decode 1 frame\n");
}
}
av_packet_unref(packet);
}
else {
//Exit Thread
thread_exit = 1;
}
}
else if (event.type == SDL_QUIT) {
thread_exit = 1;
}
else if (event.type == SFM_BREAK_EVENT) {
break;
}
}
sws_freeContext(img_convert_ctx);
SDL_DestroyWindow(screen);
SDL_Quit();
//FIX Small Bug
//SDL Hide Window When it finished
dlg->GetDlgItem(IDC_SCREEN)->ShowWindow(SW_SHOWNORMAL);
//--------------
av_frame_free(&pFrameYUV);
av_frame_free(&pFrame);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
return 0;
}
原文 FFmpeg+SDL視訊播放器-圖形界面版_ffmpeg圖形界面_奮鬥吧!騷年!的部落格-CSDN部落格