Jump to content

  • Log In with Google      Sign In   
  • Create Account


Play video files in SDL?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Link66   Members   -  Reputation: 122

Like
0Likes
Like

Posted 28 September 2005 - 06:31 PM

Hi, I want some help... When I want to play a video file in my SDL code, what format is easiest to use. We are making a simple 2D-game in SDL for Windows and now it's time to do an intro. And we want to use a video file. But what format to use? avi,mpeg,mov,wmv? And does anyone know any example code or a tutorial? Thanks! Link

Sponsor:

#2 Peregrin   Members   -  Reputation: 496

Like
0Likes
Like

Posted 28 September 2005 - 06:50 PM

There's SMPEG, but it seems quite ancient and not under active development.

Jooleem. Get addicted.

#3 samuraicrow   Members   -  Reputation: 325

Like
0Likes
Like

Posted 29 September 2005 - 08:52 AM

SMPEG is the MPEG animation player used by SDLBasic and it seems to be the only one for SDL. I tend to stay away from Windows-only file formats like .WMV since there's no way to port them to another platform without converting the files first. BTW: Why are you limiting yourself to Windows only when SDL will work on practically any operating system?

#4 Drew_Benton   Crossbones+   -  Reputation: 1712

Like
0Likes
Like

Posted 01 October 2005 - 04:32 AM

I'll refer you to my new post here at this thread, Link66.

"But I, being poor, have only my dreams. I have spread my dreams under your feet; tread softly, because you tread on my dreams." - William Butler Yeats

#5 evillive2   Members   -  Reputation: 685

Like
0Likes
Like

Posted 01 October 2005 - 06:10 PM

Once again Drew your attention to the needs of the forums are spot on. Great job. My only suggestion is to make a simple mpeg player that can pause, slow down, rewind and fast forward. Not that the documentation to do so isn't elsewhere but you know how lazy some of us are here and our aversion to using search engines.[wink]

Thanks again :)

#6 Peregrin   Members   -  Reputation: 496

Like
0Likes
Like

Posted 02 October 2005 - 07:01 PM

Nice tutorial, Drew.
I ran into the LGPL FFMPEG project, which unlike SMPEG is under active development and supports a whole bunch of modern codecs.
While there is no direct support for SDL, there is a nice tutorial for the API. The tutorial includes code for decoding video into RGB images, from which you can create SDL surface with SDL_CreateRGBSurfaceFrom.
If I get some free time, I might give it a try.

EDIT: I just found out that the FFMPEG project includes a program called ffplay, which is an SDL based video player. It seems to use SDL_DisplayYUVOverlay to output the video frame to the screen.

Jooleem. Get addicted.

#7 Drew_Benton   Crossbones+   -  Reputation: 1712

Like
0Likes
Like

Posted 03 October 2005 - 12:32 PM

Quote:
Original post by evillive2
Once again Drew your attention to the needs of the forums are spot on. Great job. My only suggestion is to make a simple mpeg player that can pause, slow down, rewind and fast forward. Not that the documentation to do so isn't elsewhere but you know how lazy some of us are here and our aversion to using search engines.[wink]


By popular demand, here is a quick little wrapper that wraps most of all the needed functionality of SMPEG. It's everything in a header file, just so it's easy to work with and modify at first. Undoubtly it's not 100% complete or the best it could be, but it's definitly enough if you want to start easily working with MPEG files in SDL.

SDL_Movie.h

class SDL_Movie
{
private:
// Surface for the main screen
SDL_Surface *screen;

// Surface for the movie
SDL_Surface *movieSurface;

// Holds the movie information
SMPEG_Info movieInfo;

// Load the movie
SMPEG *movie;

// The max we can scale by
int MaxScaleX;
int MaxScaleY;
int MaxScale;

// Locations on screen to draw at
int X, Y;

void DrawIMG(SDL_Surface *img, SDL_Surface *dst, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, dst, &dest);
}

public:
SDL_Movie()
{
MaxScaleX = MaxScaleY = MaxScale = 1;

screen = 0;
movieSurface = 0;
movie = 0;
X = Y = 0;
}

// Free our movie
~SDL_Movie()
{
Stop();
SMPEG_delete( movie );
movie = 0;
SDL_FreeSurface( movieSurface );
movieSurface = 0;
}

void ClearScreen()
{
SDL_FillRect( movieSurface, 0, 0 );
}

// Set's the volume on a scale of 0 - 100
void SetVolume( int vol )
{
SMPEG_setvolume( movie, vol );
}

// Scale the movie by the desired factors
void Scale( int w, int h )
{
// Prevent a divide by 0
if( w == 0 )
w = 1;
if( h == 0 )
h = 1;

SMPEG_scaleXY( movie, w, h );
#ifdef _DEBUG
CheckErrors();
#endif
}

// Scale the movie by the desired factor
void ScaleBy( int factor )
{
// Prevent a divide by 0
if( factor == 0 )
factor = 1;
// Make sure we don't scale larger than the surface size
if( factor > MaxScale )
factor = MaxScale;

SMPEG_scale( movie, factor );
#ifdef _DEBUG
CheckErrors();
#endif
}

// Sets the region of the video to be shown
void SetDisplayRegion( int x, int y, int w, int h )
{
SMPEG_setdisplayregion( movie, x, y, w, h );
#ifdef _DEBUG
CheckErrors();
#endif
}

// Set the position that the movie should be drawn at on the screen
void SetPosition( int x, int y )
{
X = x;
Y = y;
}

// Check for any errors
void CheckErrors()
{
char* error = SMPEG_error( movie );
if( error )
printf( "Error: %s\n", error );
}

// Load the movie
void Load( const char* fileName, SDL_Surface* s, int maxscalex = 1, int maxscaley = 1 )
{
MaxScaleX = maxscalex;
MaxScaleY = maxscaley;

// Limit how much we can scale by
MaxScale = (maxscalex > maxscaley ? maxscaley : maxscalex);

// Assign the screen surface
screen = s;

// Load the movie and store the information about it
movie = SMPEG_new( fileName, &movieInfo, true );

#ifdef _DEBUG
CheckErrors();
#endif

// Create a temporary surface to render the movie to
SDL_Surface *tempSurface2 = SDL_CreateRGBSurface( SDL_SWSURFACE, movieInfo.width * MaxScaleX, movieInfo.height * MaxScaleY, 32, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask );

// Now make a surface optimized for the main screen
movieSurface = SDL_DisplayFormat( tempSurface2 );

// Free the temporary surface
SDL_FreeSurface( tempSurface2 );

// Set the surface to draw to
SMPEG_setdisplay( movie, movieSurface, 0, 0 );

#ifdef _DEBUG
CheckErrors();
#endif

// Set the display region
SMPEG_setdisplayregion( movie, 0, 0, movieInfo.width, movieInfo.height );

#ifdef _DEBUG
CheckErrors();
#endif
}

// Set the looping of hte movie
void SetLoop( int val )
{
SMPEG_loop( movie, val );
#ifdef _DEBUG
CheckErrors();
#endif
}

// Play the movie
void Play()
{
SMPEG_play( movie );

#ifdef _DEBUG
CheckErrors();
#endif
}

// Pause the movie
void Pause()
{
SMPEG_pause( movie );

#ifdef _DEBUG
CheckErrors();
#endif
}

// Stops the movie, but keeps current position
void Stop()
{
SMPEG_stop( movie );

#ifdef _DEBUG
CheckErrors();
#endif
}

// Rewind the movie back to 0:00:00
void Rewind()
{
SMPEG_rewind( movie );

#ifdef _DEBUG
CheckErrors();
#endif
}

// Seek a number of bytes into the movie
void Seek( int bytes )
{
SMPEG_seek( movie, bytes );

#ifdef _DEBUG
CheckErrors();
#endif
}

// Skip a number of seconds
void Skip( float seconds )
{
SMPEG_skip( movie, seconds );

#ifdef _DEBUG
CheckErrors();
#endif
}

// Render some frame of the movie
void RenderFrame( int frame )
{
SMPEG_renderFrame( movie, frame );

#ifdef _DEBUG
CheckErrors();
#endif
}

// Render the final frame of the movie
void RenderFinal()
{
SMPEG_renderFinal( movie, movieSurface, 0, 0 );

#ifdef _DEBUG
CheckErrors();
#endif
}

// Draw the movie surface to the main screen at x, y
void DisplayAt( int x, int y )
{
DrawIMG( movieSurface, screen, x, y );
}

// Draw the movie surface to the main screen at x, y
void Display()
{
DrawIMG( movieSurface, screen, X, Y );
}

// Return the current info for the movie
SMPEG_Info GetInfo()
{
SMPEG_getinfo( movie, &movieInfo );
return movieInfo;
}

// Get the current status of the movie, can be SMPEG_ERROR = -1, SMPEG_STOPPED, SMPEG_PLAYING
SMPEGstatus GetStatus()
{
return SMPEG_status(movie);
}
};



Here is the Main.cpp file that demonstrates using the class, it's very straight forward as well.

// Header file for SDL
#include "SDL.h"

// This is the header file for the library
#include "smpeg.h"

// Link in the needed libraries
#pragma comment( lib, "sdlmain.lib")
#pragma comment( lib, "sdl.lib")
#pragma comment( lib, "smpeg.lib")

#include "SDL_Movie.h"

int main( int argc, char *argv[] )
{
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
return -1;
}
atexit(SDL_Quit);

SDL_Surface* screen = SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE );
if ( screen == NULL )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return -1;
}

// The movie variable
SDL_Movie mov1;

// Load the specified file and draw it to the 'screen' surface
// In this case, we are allowed to scale the X and Y dimensions by 2 maximually
mov1.Load( "demo.mpg", screen, 2, 2 );

// Set the display at
//mov1.SetPosition( 100, 100 );

// Scale the movie by 2
mov1.ScaleBy( 2 );

// Event variable
SDL_Event event;

// Loop flag
int done = 0;

// Key structure
Uint8* keys;

while(done == 0)
{
// Simple event loop
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }

if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}

// Update key states
keys = SDL_GetKeyState( 0 );

// Play
if( keys[SDLK_p] )
{
mov1.Play();
keys[SDLK_p] = 0;
}

// Pause
if( keys[SDLK_a] )
{
mov1.Pause();
keys[SDLK_a] = 0;
}

// Stop
if( keys[SDLK_s] )
{
mov1.Stop();

// If we stop, we want to start from the beginning again
mov1.Rewind();

// Clear the movie surface so it's no longer showing the last frame
mov1.ClearScreen();

keys[SDLK_s] = 0;
}

// Rewind
if( keys[SDLK_r] )
{
mov1.Rewind();
keys[SDLK_r] = 0;
}

// NOTE: DOES NOT WORK
if( keys[SDLK_LEFT] )
{
// Still trying to figure this one out, how to go backwards
mov1.Skip( -.5 );
keys[SDLK_LEFT] = 0;
}

// Skip forward .5 seconds in the movie
if( keys[SDLK_RIGHT] )
{
mov1.Skip( .5 );
keys[SDLK_RIGHT] = 0;
}

// Clear the main screen (might not want to have in a game)
SDL_FillRect( screen, 0, 0 );

// Display the movie at the internal location (setposition)
if( mov1.GetStatus() == SMPEG_PLAYING )
mov1.Display();

// Display the movie at a specified location
//mov1.DisplayAt( 0, 0 );

// Flip the main screen
SDL_Flip( screen );
}

return 0;
}



So basically, if you look though the key inputs, there is play, pause, stop, rewind, seek forward, and no seek backward yet because it doesn't seem to work. Bare minimal to work with this class is as follows:

// Create a movie variable
SDL_Movie mov1;
// Load a movie file, set the surface it should be rendered to
mov1.Load( "demo.mpg", screen );
// Start playback (only needs to be called once, NOT once per frame)
mov1.Play();
// Finally draw the movie (called every frame)
mov1.Display();

And that's a wrap. I will probabally improive upon this a lot more and put it into a second tutorial when I next get time. This is just to get something out there for any interested, feel free to make your modifications as needed [wink]. So if there's any other requests with that project, just let me know!



Peregrin, that's a great find! The main problem I see with using SMPEG now is that it is under GLP, so you do have to release all your soruce code, which for something like E4E, it's not a bad thing, but if you wanted to make a game, then you'd not want to. If you don't beat me to it, I'll also take a look into using that program with SDL as well, for a LPGL alternative with more modern codecs would be great! [smile]

"But I, being poor, have only my dreams. I have spread my dreams under your feet; tread softly, because you tread on my dreams." - William Butler Yeats

#8 evillive2   Members   -  Reputation: 685

Like
0Likes
Like

Posted 03 October 2005 - 03:12 PM

Drew Benton is my idol! Seriously, where do you find the time to be this helpful? I have about 2-3 hours to myself a night and I get to read a bit here on the forums then go to bed so I can function at work the next day. My hat is off to you sir.
Evillive2

#9 andrern2000   Members   -  Reputation: 122

Like
0Likes
Like

Posted 01 October 2009 - 05:39 PM

Xcuse me, Where can I download this SMPEG to play MPEG in SDL?
I need this. I currently work on game SDL development.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS