音视频-SDL播放BMP

BMP格式

BMP是英文Bitmap(位图)的简写,它是Windows操作系统中的标准图像文件格式,能够被多种Windows应用程序所支持。随着Windows操作系统的流行与丰富的Windows应用程序的开发,BMP位图格式理所当然地被广泛应用。这种格式的特点是包含的图像信息较丰富,几乎不进行压缩,但由此导致了它与生俱来的缺点--占用磁盘空间过大。所以,目前BMP在单机上比较流行。

中文名 : 位图图像
外文名 : BMP(Bitmap)
优 点 : 图像信息较丰富,几乎不进行压缩
缺 点 : 占用磁盘空间过大

回顾音视频-SDL2播放PCM和WAV

SDL播放PCM:

1.SDL_Init(SDL_INIT_AUDIO)

从初始化类型

/**
 *  \name SDL_INIT_*
 *
 *  These are the flags which may be passed to SDL_Init().  You should
 *  specify the subsystems which you will be using in your application.
 */
/* @{ */
#define SDL_INIT_TIMER          0x00000001u
#define SDL_INIT_AUDIO          0x00000010u
#define SDL_INIT_VIDEO          0x00000020u  /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
#define SDL_INIT_JOYSTICK       0x00000200u  /**< SDL_INIT_JOYSTICK implies SDL_INIT_EVENTS */
#define SDL_INIT_HAPTIC         0x00001000u
#define SDL_INIT_GAMECONTROLLER 0x00002000u  /**< SDL_INIT_GAMECONTROLLER implies SDL_INIT_JOYSTICK */
#define SDL_INIT_EVENTS         0x00004000u
#define SDL_INIT_SENSOR         0x00008000u
#define SDL_INIT_NOPARACHUTE    0x00100000u  /**< compatibility; this flag is ignored. */
#define SDL_INIT_EVERYTHING ( \
                SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_EVENTS | \
                SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER | SDL_INIT_SENSOR \
            )

类推,得到初始化的是

1.SDL_Init(SDL_INIT_VIDEO)

从图像角度思考

粗略理解,音频是从开始时间第0秒到结束时间第n秒,中间是拉取数据读取到buffer中,传输的播放设备中。
视频图像有点不一样, 要从图形学的角度去看思考。 图像数据在显示器中展示是需要经过GPU的处理和渲染的,也就是需要一点点关于图像相关的了解。

参考自己写的:

LearnOpenGL 入门篇中的LearnOpenGL 一些基本的概念

简单粗暴的理解流程 :
顶点着色器 -> 光栅化 -> 测试与混合 -> 片元着色器 -> buffer缓冲区 -> 显示到屏幕。

简单粗暴的就是:
(顶点着色器)一个是处理, 这些图像在屏幕上显示的坐标位置的问题.
(片元着色器)一个是处理, 这些图像在屏幕上显示的长什么样的问题.
最终显示到我们的显示器上



要显示到显示器上, 需要研究SDL的渲染API, 其实和iOS的drawRect,或者前端开发的Render函数一样。 SDL渲染函数是

/**
 *  \brief Update the screen with rendering performed.
 */
extern DECLSPEC void SDLCALL SDL_RenderPresent(SDL_Renderer * renderer);

PS : 百度一下就知道渲染器的API名字叫什么了

其中,所以需要SDL_Renderer

在SDL_render.h文件中查找对应的API,发现

/**
 *  \brief Create a 2D rendering context for a window.
 *
 *  \param window The window where rendering is displayed.
 *  \param index    The index of the rendering driver to initialize, or -1 to
 *                  initialize the first one supporting the requested flags.
 *  \param flags    ::SDL_RendererFlags.
 *
 *  \return A valid rendering context or NULL if there was an error.
 *
 *  \sa SDL_CreateSoftwareRenderer()
 *  \sa SDL_GetRendererInfo()
 *  \sa SDL_DestroyRenderer()
 */
extern DECLSPEC SDL_Renderer * SDLCALL SDL_CreateRenderer(SDL_Window * window,
                                               int index, Uint32 flags);
这里又需要一个SDL_Window ,还有很多的关于Render的函数

我们显示一张BMP的图片,就应该用到了

/**
 *  \brief Copy a portion of the texture to the current rendering target.
 *  将纹理的一部分复制到当前渲染目标。
 *
 *  \param renderer The renderer which should copy parts of a texture.
 *  \param texture The source texture.
 *  \param srcrect   A pointer to the source rectangle, or NULL for the entire
 *                   texture.
 *  \param dstrect   A pointer to the destination rectangle, or NULL for the
 *                   entire rendering target.
 *
 *  \return 0 on success, or -1 on error
 */
extern DECLSPEC int SDLCALL SDL_RenderCopy(SDL_Renderer * renderer,
                                           SDL_Texture * texture,
                                           const SDL_Rect * srcrect,
                                           const SDL_Rect * dstrect);
这里又需要一个SDL_Texture


SDL_Window 窗口

typedef struct SDL_Window SDL_Window;

创建SDL_CreateWindow

extern DECLSPEC SDL_Window * SDLCALL SDL_CreateWindow(const char *title,
                                                      int x, int y, int w,
                                                      int h, Uint32 flags);


有了这两个就大概能知道渲染需要的东西了

1.一个显示器。 对应的是SDL中的Window
4.渲染到显示器上

都知道了,现在处理的是

2.编写顶点着色器, 也就是SDL的参数配置
3.编写片元着色器, 也就是处理BMP位图数据



在LearnOpenGL 入门篇中要显示图片数据, 我们需要纹理。 这里是一张图片, 也就是一个2D的纹理。

SDL_Texture

纹理创建
/**
 *  \brief Create a texture from an existing surface.
 *
 *  \param renderer The renderer.
 *  \param surface The surface containing pixel data used to fill the texture.
 *
 *  \return The created texture is returned, or NULL on error.
 *
 *  \note The surface is not modified or freed by this function.
 *
 *  \sa SDL_QueryTexture()
 *  \sa SDL_DestroyTexture()
 */
extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTextureFromSurface(SDL_Renderer * renderer, 
                                                                    SDL_Surface * surface);

关于SDL_Surface

/**
 * \brief A collection of pixels used in software blitting.
 *
 * \note  This structure should be treated as read-only, except for \c pixels,
 *        which, if not NULL, contains the raw pixel data for the surface.
 */
typedef struct SDL_Surface
{
    Uint32 flags;               /**< Read-only */
    SDL_PixelFormat *format;    /**< Read-only */
    int w, h;                   /**< Read-only */
    int pitch;                  /**< Read-only */
    void *pixels;               /**< Read-write */

    /** Application data associated with the surface */
    void *userdata;             /**< Read-write */

    /** information needed for surfaces requiring locks */
    int locked;                 /**< Read-only */

    /** list of BlitMap that hold a reference to this surface */
    void *list_blitmap;         /**< Private */

    /** clipping information */
    SDL_Rect clip_rect;         /**< Read-only */

    /** info for fast blit mapping to other surfaces */
    struct SDL_BlitMap *map;    /**< Private */

    /** Reference count -- used when freeing surface */
    int refcount;               /**< Read-mostly */
} SDL_Surface;

查看对应的API就会发现

SDL_LoadBMP

/**
 *  Load a surface from a seekable SDL data stream (memory or file).
 *
 *  If \c freesrc is non-zero, the stream will be closed after being read.
 *
 *  The new surface should be freed with SDL_FreeSurface().
 *
 *  \return the new surface, or NULL if there was an error.
 */
extern DECLSPEC SDL_Surface *SDLCALL SDL_LoadBMP_RW(SDL_RWops * src,
                                                    int freesrc);

/**
 *  Load a surface from a file.
 *
 *  Convenience macro.
 */
#define SDL_LoadBMP(file)   SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)

所以这里就直接通过读取文件路径, 来读取BMP文件了



整理思路:
1.创建一个SDL_Window
2.创建渲染的SDL_Renderer
3.创建纹理SDL_Texture ,并且打开像素数据SDL_Surface
4.纹理SDL_RenderCopy到渲染器上
5.SDL_RenderPresent渲染刷新

代码实现

#include "playthread.h"
#include 
#include 

extern "C" {
#include 
}


// 出错了就执行goto end
#define END(judge, func) \
    if (judge) { \
        qDebug() << #func << "Error" << SDL_GetError(); \
        goto end; \
    }


Playthread::Playthread(QObject *parent) : QThread(parent) {
    // 创建信号连接
    connect(this, &Playthread::finished,
            this, &Playthread::deleteLater);
}

Playthread::~Playthread() {
    // 断开所有的连接
    disconnect();
    // 内存回收之前,正常结束线程
    requestInterruption();
    // 安全退出
    quit();
    wait();
    qDebug() << this << "Playthread 析构(内存被回收)";
}



void Playthread::run() {
    if(SDL_Init(SDL_INIT_VIDEO)) {
        qDebug() << "SDL_Init error : " << SDL_GetError();
        return;
    }

    // 创建一个SDL Window窗口
    SDL_Window *window = nullptr;
    // 像素数据
    SDL_Surface *surface = nullptr;
    // 纹理(直接跟特定驱动程序相关的像素数据)
    SDL_Texture *texture = nullptr;
    // 渲染上下文
    SDL_Renderer *renderer = nullptr;
    //
    int renderCopyRet;

    window = SDL_CreateWindow("SDL播放BMP",
                              100, 100,
                              600, 600,
                              SDL_WINDOW_SHOWN);
    END(!window, SDL_CreateWindow);


    renderer = SDL_CreateRenderer(window, -1,  SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (!renderer) { // 说明开启硬件加速失败
        renderer = SDL_CreateRenderer(window, -1, 0);
    }
    END(!renderer, SDL_CreateRenderer);

    // 处理像素数据
    surface = SDL_LoadBMP("G:/Resource/in.bmp");
    END(!surface, SDL_LoadBMP);

    //  贴图纹理
    texture = SDL_CreateTextureFromSurface( renderer, surface);
    END(!texture, SDL_CreateTextureFromSurface);

    // 复制纹理到渲染目标上
    renderCopyRet = SDL_RenderCopy(renderer, texture, nullptr, nullptr);
    END(renderCopyRet, SDL_RenderCopy);

    // 将此前的所有需要渲染的内容更新到屏幕上
    SDL_RenderPresent(renderer);

    // 延迟3秒退出
    SDL_Delay(3000);

end:


    SDL_DestroyWindow(window);
    SDL_FreeSurface(surface);
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_Quit();
}

void Playthread::setStop(bool stop) {
    _stop = stop;
}

显示正常 (BMP图片自己网上随便找一个吧)

image.png

你可能感兴趣的:(音视频-SDL播放BMP)