• SDL2.0 播放YUV


    /*
    * =====================================================================================
    *
    *       Filename: main.cpp 
    *
    *    Description:  
    *
    *        Version:  1.0
    *        Created:  2014年05月10日 14时22分38秒
    *       Revision:  none
    *       Compiler:  gcc
    *
    *         Author:  Wenxian Ni (Hello World~), niwenxianq@qq.com
    *   Organization:  AMS/ICT
    *
    * =====================================================================================
    */
    
    #include <iostream>
    #include <stdlib.h>
    #include <unistd.h>
    #include <SDL2/SDL.h>
    #include <string.h>
    using namespace std;
    
    const int width  = 1920;
    const int height = 1080;
    
    SDL_Window *window = nullptr;
    SDL_Renderer *ren  = nullptr;
    
    SDL_Texture* LoadImage(string file);
    void ApplySurface(int x, int y, SDL_Texture *tex, SDL_Renderer *rend);
    void QuitSdl(SDL_Event event);
    
    int main(int argc, char **argv)
    {
        string filename = "background.bmp";
        SDL_Event        event;
    	SDL_bool         done = SDL_FALSE;
    	Uint32           pixel_format = SDL_PIXELFORMAT_IYUV;
    	int frame_number = 0;
        FILE *fp = fopen("a.yuv","r");
    
        SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    
        //if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
        if (SDL_Init(SDL_INIT_VIDEO) != 0){
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s
    ", SDL_GetError());
            return 3;
        }
    
        SDL_Window *win = SDL_CreateWindow("Hello",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_SHOWN);
        if(win == nullptr)
        {
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s
    ", SDL_GetError());
            return 1;
        }
        ren = SDL_CreateRenderer(win, 0, 0); //渲染器 底层,打底
        if(ren == nullptr)
        {
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s
    ", SDL_GetError());
            return 1; 
        }
    
    
      //  SDL_Texture *tex = LoadImage(filename);
      //  if(tex == nullptr)
      //  {
      //      cout<<"SDL_CreateTextureFromeSurface Error"<<SDL_GetError()<<endl;
      //      return 1;
      //  }
    
      //  SDL_RenderClear(ren);
      //  SDL_RenderCopy(ren, tex, NULL, NULL);
      //  SDL_RenderPresent(ren);
        SDL_Texture *tex = SDL_CreateTexture(ren, pixel_format, SDL_TEXTUREACCESS_STREAMING, width, height);
        int len  = height*width;
        Uint8* buf = (Uint8*)malloc(len*2);
        int iPitch = width * SDL_BYTESPERPIXEL(pixel_format);
            printf("IPath %d
    ",iPitch);
        SDL_Rect rec;
        rec.x = 0;
        rec.y = 0;
        rec.w = width;
        rec.h = height;
    	while(!done)
    	{
            while (SDL_PollEvent(&event)) {
                switch (event.type) {
                case SDL_KEYDOWN:
                    if (event.key.keysym.sym == SDLK_ESCAPE) {
                        done = SDL_TRUE;
                    }
                    break;
                case SDL_QUIT:
                    done = SDL_TRUE;
                    break;
                }
            }
            fread(buf,1,height*width*3/2,fp);
            //printf("lee = %d
    ",lee);
            //    SDL_UpdateYUVTexture(tex,NULL,(const Uint8*)bufY,len,(const Uint8*)bufU,len/4,(const Uint8*)bufV,len/4);
            SDL_UpdateTexture(tex,NULL, buf,width);
            SDL_RenderClear(ren);
            SDL_RenderCopy(ren, tex, NULL, &rec);
            SDL_RenderPresent(ren);
    		frame_number++;
        }
    //  while(1)
    //  {
    //      SDL_WaitEvent(&event);
    //      QuitSdl(event);
    //  }
    SDL_Delay(2000);
    SDL_DestroyTexture(tex);
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    SDL_Quit();
    fclose(fp);
    return 0;
    }
    
    SDL_Texture* LoadImage(string file){
        SDL_Surface *loadedImage = nullptr;
        SDL_Texture *texture = nullptr;
    
        loadedImage = SDL_LoadBMP(file.c_str());
        if (loadedImage != nullptr){
            texture = SDL_CreateTextureFromSurface(ren, loadedImage);
            SDL_FreeSurface(loadedImage);
        }
        else
        std::cout << SDL_GetError() << std::endl;
        return texture;
    }
    void ApplySurface(int x, int y, SDL_Texture *tex, SDL_Renderer *rend){
        SDL_Rect pos;
        pos.x = x;
        pos.y = y;
        SDL_QueryTexture(tex, NULL, NULL, &pos.w, &pos.h);
        SDL_RenderCopy(rend, tex, NULL, &pos);
    }
    
    void QuitSdl(SDL_Event event)
    {
        switch(event.type)
        {
            case SDL_QUIT:
            SDL_Quit();
            break;
        }
        return ;
    }
    
    说明:
    1. SDL2.0 相对于老版,有更新
    2. YV12, IYUV注意区别,如是图片显红,则是此处错误


    每天早上叫醒你的不是闹钟,而是心中的梦~
  • 相关阅读:
    Notepad++快捷键
    NAT123内网映射端口
    Maven 导出依赖Jar,生成source.jar,javadoc.jar
    自定义javascript log方法
    AmazeUI定制
    模板框架
    jquery checkbox全选 获取值
    首页导航点击A连接跳转并添加上背景色,
    Controller中的方法利用ip跨域访问
    Datatable转成List
  • 原文地址:https://www.cnblogs.com/vintion/p/4116890.html
Copyright © 2020-2023  润新知