Sign in to follow this  

SDL 2.0\FFMPEG Swap Window Issue.

Recommended Posts

I decided to go back and try to rewrite my old FFMPEG code from C to C++ and SDL 1.2 to SDL 2.0. There was one issue I overlooked and that's whenever I read a frame from a video, I need to call SDL_GL_SwapWindow() as soon as the frame has finished or else I'll have an issue where in between each frame or so, the screen goes black. (or it just lags if I don't call glClear()) I also have to draw the frame immediately after it's read or else the same thing will happen except the screen will clear white instead of black.


The problem is, each video would have their own rendering code inside my Video class, or maybe I would want to render something else that's not a video, well, I would need to call SDL_GL_SwapWindow(), which I can't do or else it will interfere with my video.


Here is my current rendering code:


void Video::Render(SDL_Window* win)
    if (av_read_frame(pFormatCtx, &packet) >= 0)
        if (packet.stream_index == videoStream)
            avcodec_decode_video2(pCodecCtx, pFrame, &FrameFinished, &packet);
            if (FrameFinished)
                sws_scale(swsContext, pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
                glGenTextures(1, &VideoTexture);
                glBindTexture(GL_TEXTURE_2D, VideoTexture);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                glTexImage2D(GL_TEXTURE_2D, 0, 3, pCodecCtx->width, pCodecCtx->height, 0, GL_RGB, GL_UNSIGNED_BYTE, pFrameRGB->data[0]);

                glBindTexture(GL_TEXTURE_2D, VideoTexture);
                glTexCoord2i(0, 0); glVertex2i(0, 0);
                glTexCoord2i(1, 0); glVertex2i(800, 0);
                glTexCoord2i(1, 1); glVertex2i(800, 600);
                glTexCoord2i(0, 1); glVertex2i(0, 600);
                glDeleteTextures(1, &VideoTexture);
        av_seek_frame(pFormatCtx, videoStream, 0, AVSEEK_FLAG_BACKWARD);


And here's my while loop in main() that does the rendering and whatnot:


while (!Done)
        while (SDL_PollEvent(&event))
            HandleEvent(event, Done);
        //This is where I want to call SDL_GL_SwapWindow()


So, what's the problem? Why do I need to draw each frame and call SDL_GL_SwapWindow() immediately after a frame is read? Ideally I should have two to three separate functions, one for reading each frame, one for rendering the read frame, and one for freeing up memory used by each frame. Any help is appreciated.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this