• Advertisement
Sign in to follow this  

SDL 2.0\FFMPEG Swap Window Issue.

This topic is 1820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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]);

                glEnable(GL_TEXTURE_2D);
                glPushMatrix();
                glBindTexture(GL_TEXTURE_2D, VideoTexture);
                glBegin(GL_QUADS);
                glTexCoord2i(0, 0); glVertex2i(0, 0);
                glTexCoord2i(1, 0); glVertex2i(800, 0);
                glTexCoord2i(1, 1); glVertex2i(800, 600);
                glTexCoord2i(0, 1); glVertex2i(0, 600);
                glEnd();
                glPopMatrix();
                glDisable(GL_TEXTURE_2D);
                SDL_GL_SwapWindow(win);
                glDeleteTextures(1, &VideoTexture);
                av_free_packet(&packet);
            }
        }
    }
    else
    {
        av_seek_frame(pFormatCtx, videoStream, 0, AVSEEK_FLAG_BACKWARD);
        SDL_GL_SwapWindow(win);
    }
}

 

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

 

while (!Done)
    {
        while (SDL_PollEvent(&event))
        {
            HandleEvent(event, Done);
        }
        glClear(GL_COLOR_BUFFER_BIT);
        TestVideo.Render(Win);
        //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
Advertisement
Sign in to follow this  

  • Advertisement