Log in

View Full Version : How to store Y, U, V data to a YUV file?


aaronhwf
28th October 2007, 07:46
I want to store the decoded Y,U,V data to a file in YUV(4:2:0) format this week. then I can play it with YUVview.

my C code is as follow:

void writeY(FILE *fp,AVS_BYTE **p,int array_width,int array_height)
{
int i;

AVS_BYTE *buf=NULL;
buf=(AVS_BYTE *)malloc(array_height*array_width*sizeof(AVS_BYTE));
for(i=0;i<array_height;i++)
memcpy(buf+i*array_width,p+i,sizeof(AVS_BYTE)*array_width);

fwrite(buf,sizeof(AVS_BYTE),array_width*array_height,fp);
free(buf);
}

void writeUV(FILE *fp,AVS_BYTE **pU,AVS_BYTE **PV,int array_width,int array_height)
{
int i;

AVS_BYTE *buf=NULL;
buf=(AVS_BYTE *)malloc(array_height*array_width*sizeof(AVS_BYTE)/2);
for(i=0;i<array_height/2;i++)
{
memcpy(buf+i*array_width,pU+i,sizeof(AVS_BYTE)*array_width/2);
memcpy(buf+(2*i+1)*array_width/2,pV+i,sizeof(AVS_BYTE)*array_width/2);
}
fwrite(buf,sizeof(AVS_BYTE),array_width*array_height/2,fp);
free(buf);
}

**********************************
y0 y1 y2.................................................



**********************************
u0 u1........................v0v1......................

**********************************
But when I view the generated YUV file . It's just a sets of some colours.not the picture before Encode.

Do me a favor, thanks in advance

imcold
28th October 2007, 09:27
I've never heard about YUVview, but you can try decoding your file with mplayer.mplayer -demuxer rawvideo -rawvideo w=320:h=240:fps=30:i420 file.yuv
(replace 320/240 with your width/height) If it works, your code is correct (seems to be at first glance) and YUVview is broken/does not support 4:2:0 yuv.

h264visa
28th October 2007, 10:18
your cb/cr sequence is wrong, you should put them in seperate loop.

aaronhwf
28th October 2007, 10:55
Thanks for your reply ,imcold
I'v palyed it with mplayer. Unable to play.
Could you tell me how to store the decoded Y,Uand Vdata to a file in YUV 4:2:0 format or show me some C code for this work?

aaronhwf
28th October 2007, 11:19
I'v put Cr/Cb in seperate loop:

void writeYUV(FILE *fp,AVS_BYTE **p,int array_width,int array_height)
{
int i,j;

AVS_BYTE *buf=NULL;
buf=(AVS_BYTE *)malloc(array_height*array_width*sizeof(AVS_BYTE));
for(i=0;i<array_height;i++)

memcpy(buf+i*array_width,p+i,sizeof(AVS_BYTE)*array_width);

fwrite(buf,sizeof(AVS_BYTE),array_width*array_height,fp);

free(buf);
}

writeYUV(outYUV,&poutY,Info.image_width,Info.image_height);
writeYUV(outYUV,&poutU,Info.image_width,Info/2.image_height/2);
writeYUV(outYUV,&poutV,Info.image_width/2,Info.image_height/2);

but the generated YUV file still unable to open with YUVviewer and mplayer.:confused:

imcold
28th October 2007, 16:16
For example take look at (link removed). Reads one 4:2:0 frame and saves it using your modified function, set filename and image size in code as you wish.
I'd guess the row array passing ( writeYUV(..., &poutU, ...) ) and dereferencing its pointers in memcpy is wrong in your case, providing they doesn't contain trash. If poutY is (AVS_BYTE **p), you shouldn't use & operator, you are already passing a pointer; then you should dereference it in memcpy call (p+i) should be *(p+i) to get some rowptr address.

Einsteinool
12th November 2007, 04:13
Passed by here, leave a mark.
:readfaq:

Guest
12th November 2007, 04:23
@Einsteinool

Stop making useless posts or you'll be suspended.

JohnnyMalaria
12th November 2007, 04:43
There are a number of ways of storing YUV 4:2:0. Do you know which ones YUVview can recognize?

Also, you *may* need to make sure that your arrays have the right stride to ensure that the width in bytes is an exact multiple of 4.