YUV 播放器

//
// Created by ByteDance on 2022/7/14.
//

#include "SDLPlayer.h"
#include "stdio.h"
#include "SDL.h"
#include "SDL_log.h"
#include "SDL_main.h"
#include "SDL_events.h"
#include "SDL_render.h"

#define REFRESH_EVENT (SDL_USEREVENT +1)
#define QUIT_EVENT (SDL_USEREVENT + 2)

int thread_exit = 0;
int refresh_video_timer(void *udata){
    thread_exit =0;
    while(!thread_exit){
        SDL_Event event;
        event.type =REFRESH_EVENT;
        SDL_PushEvent(&event);
        SDL_Delay(40);
    }
    thread_exit =0;
    SDL_Event event;
    event.type = QUIT_EVENT;
    SDL_PushEvent(&event);
    return 0;
}

int main(int argc, char* argv[])
{
    FILE * video_fd = NULL;
    SDL_Event event;
    SDL_Rect rect;
    SDL_Texture * texture;
    Uint32 pixformat = 0;
    SDL_Window *win = NULL;
    SDL_Renderer *renderer = NULL;
    SDL_Thread *timer_thread = NULL;

    int w_width =608, w_height = 368;
    const int video_width =608, video_height = 368;
    Uint8 *video_pos =NULL;
    Uint8 *video_end = NULL;
    unsigned int remain_len =0;
    size_t video_buf_len = 0;
    size_t blank_space_len = 0;
    Uint8 *video_buf = NULL;
    const char* path = "1.yuv";
    const unsigned int yuv_frame_len = video_width * video_height * 3/2;
    unsigned int tmp_yuv_frame_len = yuv_frame_len;
    if(yuv_frame_len & 0xF)
    {
        tmp_yuv_frame_len = (yuv_frame_len & 0xFFF0) +0x10;
    }

    if(SDL_Init(SDL_INIT_VIDEO)){
        fprintf(stderr, "Could not initialize sdl - %s\n", SDL_GetError());
        return -1;
    }
    win = SDL_CreateWindow("YUV Player",
                           SDL_WINDOWPOS_UNDEFINED,
                           SDL_WINDOWPOS_UNDEFINED,
                           w_width, w_height,
                           SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
    if(!win)
    {
        fprintf(stderr, "Failed to create window, %s\n", SDL_GetError());
    }

    renderer = SDL_CreateRenderer(win, -1, 0);
    pixformat = SDL_PIXELFORMAT_IYUV;
    texture = SDL_CreateTexture(renderer, pixformat , SDL_TEXTUREACCESS_STREAMING,
                                video_width, video_height);
    video_buf = (Uint8*) malloc(tmp_yuv_frame_len);
    if(!video_buf) {
        fprintf(stderr, "Failed to alloce yuv frame space \n");
        goto __FAIL;
    }

    video_fd= fopen(path, "r");
    if(!video_fd) {
        fprintf(stderr, "Failed to open yuv file\n");
        goto __FAIL;
    }
    if((video_buf_len = fread(video_buf, 1, yuv_frame_len, video_fd))<=0){
        fprintf(stderr, "Failed to read data from yuv file\n");
        goto __FAIL;
    }
    video_pos = video_buf;

    timer_thread = SDL_CreateThread(refresh_video_timer, NULL, NULL);

    do{
        SDL_WaitEvent(&event);
        if(event.type == REFRESH_EVENT){
            SDL_UpdateTexture(texture, NULL, video_pos, video_width);
            rect.x =0;
            rect.y =0;
            rect.w = w_width;
            rect.h = w_height;
            SDL_RenderCopy(renderer, texture, NULL, &rect);
            SDL_RenderPresent(renderer);
            if(video_buf_len = fread(video_buf, 1, yuv_frame_len, video_fd)<=0) {
                thread_exit =1;
                continue;
            }
        } else if(event.type ==SDL_WINDOWEVENT) {
            thread_exit =1;
        } else if(event.type ==SDL_QUIT){
            thread_exit =1;
        } else if(event.type == QUIT_EVENT){
            break;
        }
    } while (1);
    __FAIL:
        if(video_buf){
            free(video_buf);
        }
        if(video_fd){
            fclose(video_fd);
        }





}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值