MFC creat the dialog and use sdl_createwindowform to set the mfc picturel control,
and use the message WM_SIZE to resize the window.
there is a problem when the window dialg resize by the mouse the decoder thread is stoped at function sdl_renderclear; the code is below here.
int simplest_ffmpeg_player1(LPVOID lpParam)
{
MyVideoDlg1 *dlg=(MyVideoDlg1 *)lpParam;
AVFrame *pFrame;
AVPacket *packet;
int ret, got_picture;
int screen_w,screen_h;
AVFrame *frameold;
double oldpts=0;
num1 = 0;
dlg->video1_continue=0;
int first = 1;
int64_t seek_target = 0;
int thread_back = 0;
SDL_Renderer* sdlRenderer;
SDL_Texture* sdlTexture;
AVFrame *frameout = NULL;
uint8_t *out_buffer;
struct SwsContext *img_convert_ctx;
AVFrame *pFrameYUV;
pFrameYUV=av_frame_alloc();
out_buffer=(uint8_t *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx1->width, pCodecCtx1->height, 1));
av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx1->width, pCodecCtx1->height, 1);
img_convert_ctx = sws_getContext(pCodecCtx1->width, pCodecCtx1->height, pCodecCtx1->pix_fmt,
pCodecCtx1->width, pCodecCtx1->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
frameout = av_frame_alloc();
pFrame=av_frame_alloc();
frameold = av_frame_alloc();
screen_h = pCodecCtx1->height;
screen_w = pCodecCtx1->width;
screen1 = SDL_CreateWindowFrom(dlg->GetDlgItem(IDC_PICTURE1)->GetSafeHwnd());
if(!screen1) {
TRACE("SDL: could not create window - exiting\n");
return -1;
}
sdlRenderer = SDL_CreateRenderer(screen1, -1, 0);
sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,screen_w, screen_h);
packet=(AVPacket *)av_malloc(sizeof(AVPacket));
thread_ontime1 = 1;
thread_exit1 = 0;
thread_seek1 = 0;
thread_over1 = 0;
while(thread_exit1 == 0)
{
if(thread_ontime1 == 1)
{
if(av_read_frame(pFormatCtx1, packet)>=0)
{
if(packet->stream_index==videoindex1)
{
ret = avcodec_send_packet(pCodecCtx1, packet);
if (ret != 0)
{
TRACE("%s/n","error");
return 0;
}
got_picture = avcodec_receive_frame(pCodecCtx1, pFrame);
if(got_picture==0)
{
//TRACE("packet->pts = %f\n",(packet->pts)*av_q2d(pFormatCtx1->streams[videoindex1]->time_base));
//TRACE("11 pFrame->pts = %f\n",(pFrame->pts)*av_q2d(pFormatCtx1->streams[videoindex1]->time_base));
if (thread_over1 == 1)
{
//TRACE("packet->pts = %f\n",(pFrame->pts)*av_q2d(pFormatCtx1->streams[videoindex1]->time_base));
int time_now = (pFrame->pts)*av_q2d(pFormatCtx1->streams[videoindex1]->time_base);
//TRACE("time_now = %f\n",time_now);
if (time_now == TIME_START_BASE)
{
num1=1;
TRACE("a1 %d\n",num1);
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx1->height, pFrameYUV->data, pFrameYUV->linesize);
//Rotate1(pFrame, &frameout);
thread_over1 = 0;
SDL_UpdateTexture(sdlTexture, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0]);
SDL_RenderClear(sdlRenderer);
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
SDL_RenderPresent(sdlRenderer);
thread_ontime1 = 0;
av_packet_unref(packet);
thread_back = 0;
}
continue;
}
if (thread_back == 1)
{
if ((oldpts - (pFrame->pts)*av_q2d(pFormatCtx1->streams[videoindex1]->time_base) <= 0.04)&&(oldpts - (pFrame->pts)*av_q2d(pFormatCtx1->streams[videoindex1]->time_base) >= 0))
{
num1--;
TRACE("b1 %d\n",num1);
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx1->height, pFrameYUV->data, pFrameYUV->linesize);
oldpts = (pFrame->pts)*av_q2d(pFormatCtx1->streams[videoindex1]->time_base);
//Rotate1(pFrame, &frameout);
thread_ontime1 = 0;
SDL_UpdateTexture(sdlTexture, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0]);
SDL_RenderClear(sdlRenderer);
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
SDL_RenderPresent(sdlRenderer);
av_packet_unref(packet);
thread_back = 0;
continue;
}
else
continue;
}
if (packet->pts >= seek_target)
{
num1++;
TRACE("c1 %d\n",num1);
oldpts = (pFrame->pts)*av_q2d(pFormatCtx1->streams[videoindex1]->time_base);
if (first == 1) //第一次解析到第一帧时刷新到屏幕
{
first = 0;
TIME_START_BASE = (int)(packet->pts*av_q2d(pFormatCtx1->streams[videoindex1]->time_base));
TRACE("TIME_START_BASE %d\n",TIME_START_BASE);
thread_ontime1 = 0;
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx1->height, pFrameYUV->data, pFrameYUV->linesize);
//Rotate1(pFrame, &frameout);
SDL_UpdateTexture( sdlTexture, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0]);
SDL_RenderClear( sdlRenderer );
SDL_RenderCopy( sdlRenderer, sdlTexture, NULL, NULL);
SDL_RenderPresent(sdlRenderer );
av_packet_unref(packet);
continue;
}
thread_ontime1 = 0;
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx1->height, pFrameYUV->data, pFrameYUV->linesize);
//Rotate1(pFrame, &frameout);
SDL_UpdateTexture( sdlTexture, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0]);
SDL_RenderClear( sdlRenderer );
SDL_RenderCopy( sdlRenderer, sdlTexture, NULL, NULL);
SDL_RenderPresent(sdlRenderer );
if (dlg->video1_continue == 1)
{
av_packet_unref(packet);
TRACE("11111\n");
dlg->video1_continue = 0;
thread_ontime1 = 1;
continue;
}
}
else
{
av_packet_unref(packet);
//TRACE("1111111111111\n");
continue;
}
}
}
av_packet_unref(packet);
}
else
{
break;
}
}
Sleep(10);
}
dlg->GetDlgItem(IDC_PICTURE1)->ShowWindow(SW_SHOWNORMAL);
av_frame_free(&frameold);
av_frame_free(&frameout);
av_frame_free(&pFrame);
TRACE("simplest_ffmpeg_player1 exit\n");
return 0;
}
this is the ONsi函数
void MyVideoDlg1::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
int xcount = 0;
int ycount = 0;
int xycount = 0;
//xcount = cx / 3;
//ycount = cy / 4;
//xycount = (xcount - ycount) ? ycount : xcount;
// TRACE(“cx = %d, cy = %d\n”, cx, cy);
if (IsAccess_control_position == 1)
{
//GetDlgItem(IDC_PICTURE1)->MoveWindow(Picture1Rect.left+(cx - xycount3)/2 ,Picture1Rect.top,
//xycount3 , xycount4);
GetDlgItem(IDC_PICTURE1)->MoveWindow(Picture1Rect.left ,Picture1Rect.top,
cx , cy);
//TRACE(“cx = %d, cy = %d\n”,xycount3, xycount*4);
Invalidate(TRUE);
}
else
{
Picture1Width = cx;
Picture1Height = cy;
}
}