# How to display two yuv files at the same time?

**URL:** <https://discourse.libsdl.org/t/how-to-display-two-yuv-files-at-the-same-time/14259>\
**Category:** SDL Development\
**Created:** [January 30, 2007, 2:30am UTC](https://discourse.libsdl.org/t/how-to-display-two-yuv-files-at-the-same-time/14259 "2007-01-30T02:30:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfdf1141\_dfdf1141](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dfdf1141\_dfdf1141](https://discourse.libsdl.org/u/dfdf1141_dfdf1141)\
**Post date:** [January 30, 2007, 2:30am UTC](https://discourse.libsdl.org/t/how-to-display-two-yuv-files-at-the-same-time/14259/1 "2007-01-30T02:30:39Z")

</div>

I writed a simple program to display two yuv files at the same time. but to  
my surprise, it turned out only a window, two files show in the window, it  
messes up. so i search in the mailing list find a similar question, its  
answer is below:

SDL uses XVideo for YUV overlays on the screen, but most cards can’t handle  
more than one application which uses XVideo. You can ether set enironment  
variable SDL\_VIDEO\_YUV\_HWACCEL to 1 or use non-screen surfaces as  
destination for overlays. Notice than anyway you will get some slowdown

but i can not understand what need i do exactly, below goes my code, can you  
do some changes in my code or tell me how to change in details to let it  
show up two windows playing different yuv files at the same time.

where to set the value of SDL\_VIDEO\_YUV\_HWACCEL or how to use non-screen  
surfaces as destination for overlays?

your help will be greatly appreciated.

/\*

- this src is used for testing the v4l and the sdl .
- Beside this,it provides a simple wrap of v4l and sdl .
- You can use it any where,but should keep this header.
- Any suggestion is appreicated.\*
- Tianjin University , China.
- 
- @dfdf1141_dfdf1141  
\*/  
#include \<stdlib.h\>  
#include \<math.h\>  
#include \<malloc.h\>  
#include \< string.h\>  
#include \<SDL/SDL.h\>  
#include \<sys/types.h\>  
#include \<stdio.h\>  
#include \<stdint.h\>

#include \<unistd.h\>  
#include \<signal.h\>

#define WIDTH 352  
#define HEIGHT 288

class CSDLDisplay  
{  
public:  
int init(int width,int height);  
void closeIt();

void display(u\_int8\_t \*ybuf,u\_int8\_t \*ubuf,u\_int8\_t \*vbuf);  
private:  
const SDL\_VideoInfo \*m\_video\_info;  
SDL\_Surface \*m\_screen;  
SDL\_Rect m\_dst\_rect;  
SDL\_Overlay \*m\_image;  
int m\_width;  
int m\_height;  
};

int CSDLDisplay::init(int width, int height)  
{  
if(SDL\_Init(SDL\_INIT\_VIDEO)\<0)  
{  
//ERROR  
return -1;  
}

//SDL\_VIDEO\_YUV\_HWACCEL = 1;  
m\_video\_info = SDL\_GetVideoInfo();  
m\_screen = SDL\_SetVideoMode(width,height,32,SDL\_HWSURFACE|SDL\_ASYNCBLIT);  
m\_width = width;  
m\_height = height;  
m\_dst\_rect.x = 0;  
m\_dst\_rect.y = 0;  
m\_dst\_rect.w = m\_screen-\>w;  
m\_dst\_rect.h = m\_screen-\>h;  
m\_image = SDL\_CreateYUVOverlay(width,height,SDL\_YV12\_OVERLAY,m\_screen);

return 1;  
}

void CSDLDisplay::closeIt()  
{  
SDL\_FreeYUVOverlay(m\_image);  
SDL\_FreeSurface(m\_screen);  
SDL\_Quit();  
}

void CSDLDisplay::display(unsigned char \* ybuf, unsigned char \* ubuf,  
unsigned char \* vbuf)  
{  
int ysize = m\_width\*m\_height;  
int uvsize = ysize/4;

SDL\_LockYUVOverlay(m\_image);  
memcpy(m\_image-\>pixels[0],ybuf,ysize);  
memcpy(m\_image-\>pixels[1],vbuf,uvsize);  
memcpy(m\_image-\>pixels[2],ubuf,uvsize);  
SDL\_DisplayYUVOverlay(m\_image,&m\_dst\_rect);  
SDL\_UnlockYUVOverlay(m\_image);  
}

FILE \* openYUVFile(const char \*yuvFileName)  
{  
FILE \*pFile;  
if(((pFile) = fopen(yuvFileName,“r+”)) == NULL)  
{  
//ERROR  
fprintf(stderr,“Error in open yuv file.\n”);  
return NULL;  
}

return pFile;  
}

int getTotalYUVFrame(FILE \*srcFile,int width,int height)  
{  
int frameTotal = 0;

if(fseek(srcFile,0,SEEK\_END) == 0)  
{  
u\_int64\_t size = ftell(srcFile);  
fseek(srcFile,0,SEEK\_SET);  
frameTotal = (int)(size/(width_height_3/2));  
}  
else  
{  
//ERROR  
fprintf(stderr,“Error in get total yuv frame.\n”);  
return -1;  
}

return frameTotal;  
}

int readYUVFrame(FILE _srcFile,int width,int height,long frameNo,u\_int8\_t  
 yuvData)  
{  
if(fseek(srcFile,frameNowidth_height_3/2,SEEK\_SET)\<0)  
{  
//ERROR  
fprintf(stderr,“Error in read yuv frame.\n”);  
return -1;  
}  
else  
{  
if(fread(yuvData,1,width_height_3/2,srcFile) != width_height\*3/2)  
{  
//ERROR  
fprintf(stderr,“Error in fread yuv frame.\n”);  
return -1;  
}  
}

return 0;  
}

void closeYUVFile(FILE \*srcFile)  
{  
if(srcFile != NULL)  
{  
fclose(srcFile);  
}  
}

int main()  
{  
int width = 352;  
int height = 288;  
int i;  
int ysize = width_height;  
int uvsize = ysize/4;  
//pY1 = (u\_int8\_t_)malloc(ysize);  
//pU1 = (u\_int8\_t\*)malloc(uvsize);  
//pV1 = (u\_int8\_t\*)malloc(uvsize);  
//pY2 = (u\_int8\_t\*)malloc(ysize);  
//pU2 = (u\_int8\_t\*)malloc(uvsize);  
//pV2 = (u\_int8\_t\*)malloc(uvsize);  
u\_int8\_t \*yuvData;  
u\_int8\_t \*yPtr,\*uPtr,\*vPtr;

yuvData = (u\_int8\_t _)malloc(ysize_3/2);

if(yuvData == NULL)  
{  
//ERROR  
fprintf(stderr,“can not malloc for yuv data.\n”);  
//return;  
}

yPtr = yuvData;  
uPtr = yuvData + ysize;  
vPtr = uPtr + uvsize;

u\_int8\_t \*yuvData1;  
u\_int8\_t \*yPtr1,\*uPtr1,\*vPtr1;

yuvData1 = (u\_int8\_t _)malloc(ysize_3/2);

if(yuvData1 == NULL)  
{  
//ERROR  
fprintf(stderr,“can not malloc for yuv data.\n”);  
//return;  
}

yPtr1 = yuvData1;  
uPtr1 = yuvData1 + ysize;  
vPtr1 = uPtr1 + uvsize;

CSDLDisplay displayer1;  
CSDLDisplay displayer2;

displayer1.init(WIDTH, HEIGHT);  
displayer2.init(WIDTH, HEIGHT);  
char strName2[300];  
sprintf(strName2,"/root/mobile\_cif.yuv");  
char strName[300];  
sprintf(strName,"/root/akyio\_cif.yuv");  
FILE \*srcFile =openYUVFile(strName);  
FILE \*srcFile1 =openYUVFile(strName2);  
for (i=0; i\<= 200; i++)  
{  
readYUVFrame(srcFile, width, height, i,yuvData);  
readYUVFrame(srcFile1, width, height, i,yuvData1);  
displayer1.display(yPtr1, uPtr1, vPtr1);  
displayer2.display(yPtr, uPtr, vPtr);  
usleep(1000/30);  
}  
displayer1.closeIt();  
displayer2.closeIt();  
closeYUVFile(srcFile);  
closeYUVFile(srcFile1);  
}

thanks in advance!  
waiting for your reply!  
urgent!!
