Play a YUV image using SDL

Code

sdl2_demo.c

#include <stdio.h>
#include <SDL2/SDL.h>

/ / Display window size
int WINDOW_W = 640;
int WINDOW_H = 480;

int thread_exit = 0;

int refresh_video(void *opaque)
{
    while(thread_exit == 0)
    {
        SDL_Event event;
        event.type = SDL_USEREVENT + 1;
        SDL_PushEvent(&event);
        SDL_Delay(40);
    }
    printf("exit refresh video thread.\n");
    return 0;
}

int main()
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window *window = SDL_CreateWindow("SDL Demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_W, WINDOW_H, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);

    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

    SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, WINDOW_W, WINDOW_H);

    FILE *fp = fopen("test.iyuv", "rb");
    if (!fp)
    {
        printf("Cannot open the file.\n");
        return -1;
    }

         // The width and height of the image
    const int IMG_W = 640;
    const int IMG_H = 480;

    uint8_t buff[IMG_W * IMG_H * 3 / 2];
    fread(buff, 1, IMG_W * IMG_H * 3 / 2, fp);
    fclose(fp);

    SDL_Thread *thread = SDL_CreateThread(refresh_video, NULL, NULL);

    SDL_Event event;
    SDL_Rect rect;

    while (1)
    {
        SDL_WaitEvent(&event);
        if (event.type == SDL_USEREVENT + 1)
        {
            SDL_UpdateTexture(texture, NULL, buff, IMG_W);

            rect.x = 0;
            rect.y = 0;
            rect.w = WINDOW_W;
            rect.h = WINDOW_H;

            SDL_RenderClear(renderer);
            SDL_RenderCopy(renderer, texture, NULL, &rect);
            SDL_RenderPresent(renderer);

            SDL_Delay(40);
        }
        else if(event.type == SDL_WINDOWEVENT)
        {
            SDL_GetWindowSize(window, &WINDOW_W, &WINDOW_H);
            printf("window resize, w=%d h=%d\n", WINDOW_W, WINDOW_H);
        }
        else if(event.type == SDL_QUIT)
        {
            printf("Exit.\n");
            break;
        }
    }

    return 0;
}

Compile

Create a new build script, such as build.sh

The content is as follows, the installation directory of SDL2 $PREFIX is modified according to the actual situation.

#!/bin/bash
PREFIX=/usr/local
gcc sdl_demo.c -o sdl_demo -I $PREFIX/include -L $PREFIX/lib -lSDL2

Execute the script to compile.

Above code download

reference

  • The simplest video and audio playback example 7: SDL2 playback RGB/YUV

Intelligent Recommendation

SDL (4) SDL YUV playback and PCM playback

Steps to broadcast YUV and PCM data in SDL: 1, play YUV data: In fact, it is constantly updating the texture 1, play PCM data DEMO address Last attachedRaytheon's study materials...

SDL realizes simple YUV player

Overview This article uses the SDL framework to implement a simple YUV player. Operating environment: Windows10, Qt5.13, SDL2.1 Implementation function: space bar controlPause/Resume, ESCdrop out,z、x、...

SDL Development Notes (2): Introduction to Audio Basics, Using SDL to Play Audio

If the article is an original article, it may not be reproduced without permission Original blogger blog address: Original blogger's blog navigation: The blog address of this article: Red Fatty (Red I...

Using the JPEG library to decode JPEG image to YUV

It should be noted that JPEG-6B decoding seems to be compiled with GCC, with g ++ will be wrong, I usually package it into a library, compile it with GCC, then call in C ++....

SDL Play Audio (PCM)

The main reference of the article: PS: The article uses the SDL2 framework. SDL audio playback process The process of SDL playing audio is very simple, divided into the following steps. 1. Initializat...

More Recommendation

SDL Play pcm data

The code is from the video of Li Chao from Mu Ke. I just added a comment  ...

FFMPEG + SDL play class

I have been taking the drum ffmpeg for some time, I feel that the VLC is more intimate.Although I don't know how VLC is used now. In addition to the blog of Thunder, the main reference is still this b...

SDL play PCM audio

Generate PCM audio MP3 intercept -Acodec COPY Coding Format Replication MP3 turn PCM -f mandatory file format-AR channel number -Ar adoption rate Source code demo.c Compilation operation (MINGW enviro...

SDL play PCM

I have recently learned FFMPEG to develop a FFMPEG-based player. Later, FFMPEG could only decode, decode operation, and final SINK needs to be implemented with SDL2, so learn SDL2 first, use SDL2 to p...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top