天天看点

FFmpeg+SDL视频播放器-图形界面版

作者:音视频流媒体技术

MFC知识

创建MFC工程的方法

• 打开VC++

• 文件->新建->项目->MFC应用程序

• 应用程序类型->基于对话框

• 点击下一步即可

设置控件

• 找到“工具箱”,就可以将相应的控件拖拽至应用程序对话框中

• 常用控件有:Button,Edit Control,Static Text等

• 找到“属性”选项卡

可以在“Caption”属性上修改控件上的文字

可以在“ID”属性上修改控件上的ID(ID是控件的标识,不可重复)

添加消息响应函数

• 双击Button控件,就可以给该控件添加消息响应函数。

• 在菜单栏的“项目->类向导”处,可以添加更多种类的消息响应函数。

MFC最简单的弹出消息框的函数是AfxMessageBox(“HelloWorld”)

FFmpeg+SDL+MFC实现图形界面视频播放器

致命bug:应用新的库,只能显示弹出的窗口,不能显示嵌入的画面

FFmpeg+SDL视频播放器-图形界面版

---------------时间:2022.6.12 bug修复----------------------

FFmpeg+SDL视频播放器-图形界面版

在等待事件中,如果使用SDL_WaitEvent会出现第一次等待的过程,所以在MFC不会播放,如果使用SDL_PollEvent则会继续执行,使其正常显示。

SDL_PollEvent:

函数的作用是,直接查看事件队列,如果事件队列中有事件,直接返回事件,并删除此事件。如果没有事件也直接返回。

SDL_WaitEvent:

事件触发机制。事件发生的时候触发,没有事件的时候阻塞在这里。

FFmpeg+SDL视频播放器-图形界面版

相关学习资料推荐,点击下方链接免费报名,先码住不迷路~】

【免费分享】音视频学习资料包、大厂面试题、技术视频和学习路线图,资料包括(C/C++,Linux,FFmpeg webRTC rtmp hls rtsp ffplay srs 等等)有需要的可以点击加群免费领取~

FFmpeg+SDL视频播放器-图形界面版

下面给出其核心函数:

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博客

继续阅读