📄 video_play.cpp
字号:
#include "SDL.h"#include "SDL_video.h"#include <iostream>#include <fstream>#include <string>#include <unistd.h>#include <sys/time.h>#include <sys/types.h>using namespace std;string s1="-w";string s2="-h";string s3="-b";string s4="-f";char *file_name;int width=352;int height=288;int bpp=16;void display_usage(){ cout<<endl<<"Usage: YUV_player [Option] [Value] ..."<<endl; cout<<"\t-f "<<"File Name"<<endl; cout<<"\t-w "<<"Width "<<"Default 352"<<endl; cout<<"\t-h "<<"Height "<<"Default 288"<<endl; cout<<"\t-b "<<"Display Bpp "<<"Default 16"<<endl; cout<<endl<<endl<<"Simple Usage: YUV_player [Filename]"<<endl<<endl;}int parse_args(int argc,char **argv){ cout<<endl<<"A Simple YUV Player using SDL --by John Zhou,Tsinghua Univ--"<<endl<<endl; if (argc==1) { display_usage(); return 1; } if (argc==2) { file_name=argv[1]; } if (argc>2) for (int i=1;i<argc;i++) { if (argv[i][0]=='-') if (argv[i]!=s1 && argv[i]!=s2 && argv[i]!=s3 && argv[i]!=s4) { display_usage(); return 1; } if (argv[i]==s1) if(i<argc-1) { width=atoi(argv[i+1]); if (width>1500 || width<0) { cout <<"!!!Error:Invalid value for width,it must between 0 and 1500"<<endl<<endl; return 1; } } else { display_usage(); return 1; } if (argv[i]==s2) if(i<argc-1) { height=atoi(argv[i+1]); if (height>1200 || height<0) { cout <<"!!!Error:Invalid value for height,it must between 0 and 1200"<<endl<<endl; return 1; } } else { display_usage(); return 1; } if (argv[i]==s3) if(i<argc-1) { int tmp=atoi(argv[i+1]); if (tmp==8 || tmp==16 || tmp==24 || tmp==32) bpp=tmp; else { cout <<"!!!Error:Invalid value for bpp,it must be 8,16,24,32"<<endl<<endl; return 1; } } else { display_usage(); return 1; } if (argv[i]==s4) if(i<argc-1) file_name=argv[i+1]; else { display_usage(); return 1; } } return 0;}int main(int argc,char **argv){ //Parse args if(parse_args(argc,argv)==1) return 1; //Open file Uint8 buf[width*height*2]; ifstream file_in(file_name); if (!file_in) { cout<<"Can't open the file"<<endl<<endl; return 1; } // Initialize the SDL library if( SDL_Init(SDL_INIT_VIDEO) < 0 ) { cout<<"Couldn't initialize SDL:"<<SDL_GetError(); return 1; } // Clean up on exit atexit(SDL_Quit); //Initialize screen SDL_Surface *screen; screen = SDL_SetVideoMode(width,height, bpp, SDL_SWSURFACE); if ( screen == NULL ) { cout<<"Couldn't set video mode: "<<SDL_GetError()<<endl<<endl; return 1; } SDL_Rect *My_Rect = new SDL_Rect(); My_Rect->x=0; My_Rect->y=0; My_Rect->w=width; My_Rect->h=height; //Initialize YUVOverlay SDL_Overlay *My_YUV_Overlay = SDL_CreateYUVOverlay(width,height,SDL_YV12_OVERLAY,screen); if (My_YUV_Overlay == NULL) { cout<<"Can't create YUVOverlay:"<<SDL_GetError()<<endl<<endl; return 1; } My_YUV_Overlay->planes = 3; My_YUV_Overlay->pitches[0]=width; My_YUV_Overlay->pitches[1]=width/2; My_YUV_Overlay->pitches[2]=width/2; //Time control timeval My_Time1,My_Time2,My_Time; My_Time.tv_sec=0; long int time_left; //Main loop while (!file_in.eof()) { gettimeofday(&My_Time1,NULL); file_in.read(buf,width*height+width*height/2); SDL_LockYUVOverlay(My_YUV_Overlay); My_YUV_Overlay->pixels[0]=buf; My_YUV_Overlay->pixels[2]=buf+width*height; My_YUV_Overlay->pixels[1]=buf+width*height+width*height/4; SDL_UnlockYUVOverlay(My_YUV_Overlay); SDL_DisplayYUVOverlay(My_YUV_Overlay,My_Rect); gettimeofday(&My_Time2,NULL); if (My_Time1.tv_sec==My_Time2.tv_sec) time_left=My_Time2.tv_usec-My_Time1.tv_usec; else time_left=My_Time2.tv_usec+1000000-My_Time1.tv_usec; time_left = 40000-time_left; My_Time.tv_usec=time_left; if (time_left > 0) select(0,NULL,NULL,NULL,&My_Time); } //Free resources SDL_FreeYUVOverlay(My_YUV_Overlay); SDL_FreeSurface(screen);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -