FFmpeg+SDL(雷神)-(4)

(4) SDL视频显示
代码记录


#include 

extern "C"
{
#include "sdl/SDL.h"
};

const int bpp=12;

int screen_w=640,screen_h=360;
const int pixel_w=640,pixel_h=360;

unsigned char buffer[pixel_w*pixel_h*bpp/8];

int main(int argc, char* argv[])
{
    if(SDL_Init(SDL_INIT_VIDEO)) {  //初始化SDL系统
        printf( "Could not initialize SDL - %s\n", SDL_GetError()); 
        return -1;
    } 

    SDL_Window *screen; 
    //SDL 2.0 Support for multiple windows
    screen = SDL_CreateWindow("Simplest Video Play SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,//创建窗口SDL_Window
        screen_w, screen_h,SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
    if(!screen) {  
        printf("SDL: could not create window - exiting:%s\n",SDL_GetError());  
        return -1;
    }
    SDL_Renderer* sdlRenderer = SDL_CreateRenderer(screen, -1, 0);  //创建渲染器SDL_Renderer

    Uint32 pixformat=0;
    //IYUV: Y + U + V  (3 planes)
    //YV12: Y + V + U  (3 planes)
    pixformat= SDL_PIXELFORMAT_IYUV;  

    SDL_Texture* sdlTexture = SDL_CreateTexture(sdlRenderer,pixformat, SDL_TEXTUREACCESS_STREAMING,pixel_w,pixel_h);//创建纹理SDL_Texture

    FILE *fp=NULL;
    fp=fopen("sintel_640_360.yuv","rb+");

    if(fp==NULL){
        printf("cannot open this file\n");
        return -1;
    }

    SDL_Rect sdlRect;  

    while(1){
            if (fread(buffer, 1, pixel_w*pixel_h*bpp/8, fp) != pixel_w*pixel_h*bpp/8){
                // Loop
                fseek(fp, 0, SEEK_SET);
                fread(buffer, 1, pixel_w*pixel_h*bpp/8, fp);
            }

            SDL_UpdateTexture( sdlTexture, NULL, buffer, pixel_w);  //设置纹理的数据

            sdlRect.x = 0;  
            sdlRect.y = 0;  
            sdlRect.w = screen_w;  
            sdlRect.h = screen_h;  
            
            SDL_RenderClear( sdlRenderer );   
            SDL_RenderCopy( sdlRenderer, sdlTexture, NULL, &sdlRect);  //将纹理的数据拷贝给渲染器
            SDL_RenderPresent( sdlRenderer );  //显示
            //Delay 40ms
            SDL_Delay(40);//工具函数,用于延时
            
    }
    SDL_Quit();//退出SDL系统
    return 0;
}

你可能感兴趣的:(FFmpeg+SDL(雷神)-(4))