Search code examples
sdl-2v4l2

Streaming UYVY video using SDL


I am trying a program to capture image data from a camera using v4l2. I have grabbed 4 frames and stored them in a buffer of continuous memory. The data format of the image in the buffer is UYVY. I need help in knowing the steps to be followed to map the image buffer or copy them to a texture so that I can stream them like a video.

I tried converting the UYVY files into BMP format and stream them using SDL_loadBMP() function but the frame rate is very low.

EDIT: Here's the code for SDL:

The "buf" is the image buffer which consists of bmp image data. I have converted the UYVY files to BMP and passed here. I need help in streaming the UYVY directly.

void video_stream(unsigned char* buf){

    SDL_Surface *image;
    SDL_Surface * screen;
    SDL_Renderer *renderer;
    SDL_Texture* texture;
    SDL_Window *window;
    SDL_RWops* rw;
    SDL_Rect recta;
    SDL_Init(SDL_INIT_EVERYTHING);

    recta.x=0;
    recta.y=0;
    recta.w=s_format.fmt.pix.width;
    recta.h=s_format.fmt.pix.height;
    window=SDL_CreateWindow("Streaming", 0, 0, s_format.fmt.pix.width, s_format.fmt.pix.height, SDL_WINDOW_RESIZABLE);
    rw=SDL_RWFromMem(buf,s_format.fmt.pix.width*s_format.fmt.pix.height*NUMBER_OF_BYTES_PER_PIXEL+OFFSET);
    renderer=SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);

    SDL_RenderSetLogicalSize(renderer,s_format.fmt.pix.width,s_format.fmt.pix.height);
    SDL_SetRenderDrawColor(renderer,0,0,0,50);
    SDL_RenderClear(renderer);

    image=SDL_LoadBMP_RW(rw,1);
    texture=SDL_CreateTextureFromSurface(renderer,image);
    SDL_RenderCopy(renderer,texture,NULL,&recta);
    SDL_RenderPresent(renderer);

    SDL_DestroyRenderer(renderer);
    SDL_FreeSurface(image);
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();


}

Solution

  • SDL_CreateTexture() with SDL_PIXELFORMAT_UYVY. Upload new frames using SDL_UpdateTexture(). Render using SDL_RenderCopy.

    You can use SDL_TEXTUREACCESS_STREAMING and SDL_LockTexture()/SDL_UnlockTexture() to speed up texture updates.