Home > other >  Based on the SDL YUV420p player formats
Based on the SDL YUV420p player formats

Time:10-10

#include
#include
#include
#include
640//# define SCREEN_W screen width
272//# define SCREEN_H screen height
640//# define PIXEL_W texture width, pixel
The height of 272//# define PIXEL_H texture, pixel
using namespace std;
Int RefreshVideo (void * data)
{
/* add events to the event queue, every 40 ms to add an event 40 x25=1000 -- -- -- -- -- - */
25 frames per second//SDL_Delay (40);
//SDL_PushEvent (& amp; The event);
Cout<& lt;" RefreshVideo "& lt; }
//SDL playback module
Void SdlThread ()
{
Cout<& lt;" Play thread starts "& lt; Const int the BPP=12;//a YUV420 pixel number of bit
Unsigned char buffer PIXEL_H PIXEL_W * * BPP/8;//used to store from the test. The data read from the yuv
SDL_Rect the rect.//rectangular box, is used to specify the refresh renderer area
The rect. X=0;
The rect. Y=0;
The rect. W=SCREEN_W;
The rect. H=SCREEN_H;

//open the test file
The FILE * fp=fopen ("./test. Yuv ", "rb");
If (fp==NULL) {
Cout<& lt;" Open the test. The yuv failure "& lt; return ;
}
//initialize the SDL
If (SDL_Init (SDL_INIT_VIDEO)) {
SDL_Log (" Unable to initialize SDL: % s ", SDL_GetError ());
return ;
}
//create a window
SDL_Window * window;
The window=SDL_CreateWindow (" Person Network Player, "SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_W SCREEN_H,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
//check whether the form is to create success, if you don't succeed window to NULL
If (window==NULL) {
//if the form didn't create success, the back of the program has no point in holding the execution,
SDL_Log (" Could not create Windows: % s \ n ", SDL_GetError ());
return;
}
//add interface to the window, get window surface
SDL_Surface * surface=SDL_GetWindowSurface (Windows);
//loading BMP picture
SDL_Surface * backgroud=SDL_LoadBMP ("./testyuv. BMP ");
SDL_BlitSurface (backgroud, NULL, surface, NULL);
//refresh window
SDL_UpdateWindowSurface (Windows);

//the third step: SDL_CreateRenderer () : based on the window to create the renderer (Render)
//use the first driver, hardware acceleration
The renderer=SDL_CreateRenderer SDL_Renderer * (window, 1, SDL_RENDERER_PRESENTVSYNC);
If (the renderer==NULL)
{
SDL_Log (" Could not create the renderer: % s \ n ", SDL_GetError ());
return;
}
//step 4: SDL_CreateTexture () : create a Texture (Texture)
Struct SDL_Texture * texture=SDL_CreateTexture (the renderer,
SDL_PIXELFORMAT_IYUV,
SDL_TEXTUREACCESS_STREAMING PIXEL_W, PIXEL_H);
If (texture==NULL)
{
SDL_Log (" Could not create the renderer: % s \ n ", SDL_GetError ());
return;
}
//create a thread
//SDL_Thread * SDL_CreateThread (SDL_ThreadFunction fn, const char * name, void * data);
//typedef int (SDL_ThreadFunction SDLCALL *) (void * data);//SDL_ThreadFunction function pointer type
//void parameters for thread and ginseng
SDL_Thread * refresh_thread=SDL_CreateThread (RefreshVideo, NULL, NULL);

//event capture
SDL_Event event;//create an event object
While (true)
{
SDL_WaitEvent (& amp; The event);//when the event queue is empty the program block, freeing up CPU
//received post-processing events
//press the enter key to start processing

If ((event type==SDL_KEYDOWN) & amp; & (event. Key. Keysym, sym==13))
{
While (true)
{
Cout<& lt;" The Event started "& lt; //read YUV420 data, there needs to be understanding of the internal structure of YUV420
//determine whether finished read
if(! Fread (buffer 1, PIXEL_H PIXEL_W * * BPP/8, fp))
{
//move the file pointer, next time won't be able to play
Fseek (fp, 0, SEEK_SET);
break;
}
//update the texture
SDL_UpdateTexture (texture, NULL, buffer, PIXEL_W);//pass NULL, the entire texture
//update the renderer
SDL_RenderClear (the renderer);//to clear the renderer, eliminate the influence of a frame on the
//copy data from the texture to the renderer
SDL_RenderCopy (the renderer, texture, NULL, & amp; The rect);
//the renderer in the context of data, apply colours to a drawing to the associated form
SDL_RenderPresent (the renderer);
}
}
Else if (event type==SDL_QUIT)
{
break;
}
}
The fclose (fp);//close the file
//release the memory resources
SDL_FreeSurface (backgroud);
SDL_FreeSurface (surface);
SDL_DestroyTexture (texture);
SDL_DestroyRenderer (the renderer);
SDL_DestroyWindow (Windows);
Cout<& lt;" SDLPlayer Exit "& lt; //out
SDL_Quit ();
}
Int main (int arg c, char * argv [])
{
Cout<& lt;" This is the main Function. "& lt; SdlThread ();
return 0;
}

CodePudding user response:

Post for the first time, also don't know the format
  • Related