【发布时间】:2013-04-29 14:04:49
【问题描述】:
我使用 C++ 中的 SDL 和 libav 在 Linux 的屏幕上绘制视频。我的大部分视频打开代码都基于this tutorial,但我更改了一些已弃用的功能。我像这样初始化 SDL:
SDL_Init(SDL_INIT_EVERYTHING);
const SDL_VideoInfo * info = SDL_GetVideoInfo();
screen = SDL_SetVideoMode(info->current_w, info->current_h, 0, SDL_SWSURFACE | SDL_FULLSCREEN);
我不打算发布整个代码,因为它非常大,但以下显示了我如何尝试显示视频叠加层。请注意,一些变量是我的 Video 类中的类成员,例如 formatCtx 和 packet。
void Video::GetOverlay(SDL_Overlay * overlay) {
int frameFinished;
while (av_read_frame(formatCtx, &packet) >= 0) {
if (packet.stream_index == videoStream) {
avcodec_decode_video2(codecCtx, frame, &frameFinished, &packet);
if (frameFinished) {
SDL_LockYUVOverlay(overlay);
AVPicture pict;
pict.data[0] = overlay->pixels[0];
pict.data[1] = overlay->pixels[2];
pict.data[2] = overlay->pixels[1];
pict.linesize[0] = overlay->pitches[0];
pict.linesize[1] = overlay->pitches[2];
pict.linesize[2] = overlay->pitches[1];
SwsContext * ctx = sws_getContext (codecCtx->width, codecCtx->height, codecCtx->pix_fmt,
codecCtx->width, codecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
sws_scale(ctx, frame->data, frame->linesize, 0, codecCtx->height, pict.data, pict.linesize);
sws_freeContext(ctx);
SDL_UnlockYUVOverlay(overlay);
++frameIndex;
return;
}
}
av_free_packet(&packet);
}
}
然后在我的主循环中:
SDL_Overlay * overlay = SDL_CreateYUVOverlay(video->GetWidth(), video->GetHeight(), SDL_YV12_OVERLAY, screen);
while (true) {
video->GetOverlay(overlay);
SDL_Rect rect = { 400, 200, video->GetWidth(), video->GetHeight() };
SDL_DisplayYUVOverlay(overlay, &rect);
SDL_Flip(screen);
}
这有效,视频播放,但闪烁很多。就像它试图在每一帧的同一位置绘制图像一样。当我取消对SDL_Flip(screen) 的呼叫时,视频播放正常。太快了,我还没有进行视频超时,但是当我添加一个临时的SDL_Delay(10) 时,它看起来还不错。
但是当我删除SDL_Flip 以显示我的视频时,我无法在屏幕上绘制任何其他内容。 SDL_BlitSurface 和 SDL_FillRect 都无法在屏幕上绘制任何内容。我已经尝试将SDL_DOUBLEBUF 添加到标志中,但这并没有改变这种情况。
如果需要,我可以提供更多代码,但我认为问题出在我发布的代码中,因为其他一切正常(绘制图像或显示没有SDL_Flip 的视频)。
我做错了什么?
【问题讨论】:
-
显然有许多平台或情况(例如 ATI/nVidia 驱动程序设置)会导致 Vsync 被关闭或不被 SDL_Flip 使用;建议您搜索一下“sdl_flip vsync”。你可能会遇到这些事情中的任何一个。