VideoHelp Forum
+ Reply to Thread
Results 1 to 18 of 18
Thread
  1. Member
    Join Date
    Aug 2009
    Location
    United States
    Search Comp PM
    Hi everyone,

    I am developing a codec as a hobby project and I am an absolute beginner in this field with a signal processing background.
    I am stuck in the reading of a YUV file. This is what I did:

    1. Allocated memory to an pointer of size Height*width for Y frame
    2. 1 Allocated memory to an pointer of size Height*width/4 for U frame and V frame respectively.
    3. I am using fread to read the frame. However what happens is U and V frames are completely filled with the value = 205 and the resulting frame when opened using YUVTools is pink in color.

    I am unable to figure out why "205" is being read.

    Also I was advised to try using int,unsigned char, char pointers for the frames .. but I get the same result.

    Can anyone help me out with this? Thanks a lot in advance ...
    Quote Quote  
  2. What kind of YUV file? YUY2? YV12? What you're describing is YUV with 4:1:1 subsampling (Height*width/4 for U frame and V frame) like in DV video but no decoder I know of outputs that.

    Maybe you could post a few frames of the video.
    Quote Quote  
  3. Member
    Join Date
    Jul 2009
    Location
    Spain
    Search Comp PM
    Originally Posted by jagabo
    What you're describing is YUV with 4:1:1 subsampling (Height*width/4 for U frame and V frame) like in DV video
    YV12 (4:2:0, like MPEG2) also has a size of Height*width/4 for each of the U and V planes.
    It's just interpreted differently as far as chroma placement is concerned.
    Quote Quote  
  4. Member
    Join Date
    Aug 2009
    Location
    United States
    Search Comp PM
    Hi,
    I should have mentioned that. I am using YV12 (4:2:0).

    But what is weird now is that even in the Y frame, roughly the last 200 pixels are fixed at 205 and the entire U and V frame go to 205. As you mentioned I am attaching the first frame of the Football sequence which is what I am using.



    frame1.yuv
    Quote Quote  
  5. Originally Posted by Gavino
    Originally Posted by jagabo
    What you're describing is YUV with 4:1:1 subsampling (Height*width/4 for U frame and V frame) like in DV video
    YV12 (4:2:0, like MPEG2) also has a size of Height*width/4 for each of the U and V planes.
    It's just interpreted differently as far as chroma placement is concerned.
    Yes, the byte size is the same. It's just the way he phrased it made me think he was using 4:2:2.
    Quote Quote  
  6. Originally Posted by krao
    Just so I don't have to spend the whole day guessing, what are the frame dimensions?
    Quote Quote  
  7. Member
    Join Date
    Aug 2009
    Location
    United States
    Search Comp PM
    Hi Jagabo
    It is 176 * 144.

    I was given this by a colleague and the original name is FOOTBALL_176x144_7.5_orig_01.yuv. He mentioned that it is of 4:2:0 format and I am reading Y , then U and then V in that order.

    However as I mentioned before, part of the Y frame and the entire U and V frames are fixed at 205.
    Quote Quote  
  8. Member
    Join Date
    Jul 2009
    Location
    Spain
    Search Comp PM
    Originally Posted by krao
    But what is weird now is that even in the Y frame, roughly the last 200 pixels are fixed at 205 and the entire U and V frame go to 205.
    From the size of the file (38016 bytes), I deduce that your image resolution is 176x144. Is this correct?

    Looking at it with a hex editor, I don't see any sequence of values 205, at least not around the point where the Y plane ends and the U plane begins (offset 25344). But there is a clear change in the range of values at this point (starting 10, 8, 9, 8, ...).

    This suggests there is something wrong with your code to read the file.
    Could you post the relevant part of the code here?
    Quote Quote  
  9. Nevermind I found it 176x144:


    Quick, dirty C code: read.zip
    Quote Quote  
  10. Member
    Join Date
    Aug 2009
    Location
    United States
    Search Comp PM
    Hi,
    I was unable to post anything the whole day due to some Apache server error ! Was there a server problem?

    Anyways here is my code...it is fairly simple..is anything wrong with it?

    Code:
    void ReadYUV()
    {
    	FILE *pInpVideo, *pTestOut;		
    	unsigned char *pInp, *pYFrame, *pUFrame, *pVFrame;
    	int i;
    	int nHeight = 144;
    	int nWidth = 176;
    	int nSize = nHeight*nWidth;
    
    	pYFrame = malloc(sizeof(unsigned char)* nSize);
    	pUFrame = malloc(sizeof(unsigned char)*(nSize/4));
    	pVFrame = malloc(sizeof(unsigned char)*(nSize/4));
    	
    	pInpVideo = fopen ("C:\\Codec\\FOREMAN_176x144_7.5_orig_01.yuv","r") ;
    	pTestOut = fopen (C:\\testout.yuv","w");
    
    
    	if(!pInpVideo)
    		printf("error");
    	
    
    	fread (pYFrame, sizeof(unsigned char), (nSize), pInpVideo);
    	fread (pUFrame, sizeof(unsigned char), (nSize)/4, pInpVideo);
    	fread (pVFrame, sizeof(unsigned char), (nSize)/4, pInpVideo);
    	
    	fwrite(pYFrame, sizeof(unsigned char), (nSize), pTestOut);
    	fwrite(pUFrame, sizeof(unsigned char), (nSize)/4, pTestOut);
    	fwrite(pVFrame, sizeof(unsigned char), (nSize)/4, pTestOut);
    
    	fclose(pTestOut);
    	fclose(pInpVideo);
    
    }
    Quote Quote  
  11. Member
    Join Date
    Aug 2009
    Location
    United States
    Search Comp PM
    Oh wow !! I think the mistake I have been making is using "r" in the fopen statement instead of "rb" ...

    Thanks a lot Jagabo and Gavino for your help !! Hope I learn quickly and help others like you'll did too !!
    Quote Quote  
  12. Yes. Text mode would cause input to stop on any null.
    Quote Quote  
  13. Hi! I try to compose two YUV image (4:2:0, same resolution) in only one image (side by side) but i have some problem with this code because result is four compressed image in one!
    i need yours help!!!


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        FILE *pInpVideo_one, *pInpVideo_two, *pTestOut;
            unsigned char *pYFrame, *pUFrame, *pVFrame, *pYFrame_one, *pUFrame_one, *pVFrame_one, *pYFrame_two, *pUFrame_two, *pVFrame_two;
            int nHeight = 144;
            int nWidth = 176;
            int nSize = nHeight*nWidth;
    
            int nWidth_out = nWidth*2;
            int nSize_out = nHeight*nWidth_out;
    
            pYFrame_one = malloc(sizeof(unsigned char)* nSize);
            pUFrame_one = malloc(sizeof(unsigned char)*(nSize/4)); // un byte per ogni 4 pixel
            pVFrame_one = malloc(sizeof(unsigned char)*(nSize/4)); // un byte per ogni 4 pixel
    
            pYFrame_two = malloc(sizeof(unsigned char)* nSize);
            pUFrame_two = malloc(sizeof(unsigned char)*(nSize/4)); // un byte per ogni 4 pixel
            pVFrame_two = malloc(sizeof(unsigned char)*(nSize/4)); // un byte per ogni 4 pixel
    
    
            pInpVideo_one = fopen ("frame1.yuv","rb") ;
            pInpVideo_two = fopen ("bus.yuv","rb") ;
            pTestOut = fopen ("test_uscita.yuv","wb");
    
    
            if(!pInpVideo_two) printf("error");
    
            fread (pYFrame_one, sizeof(unsigned char), (nSize), pInpVideo_one);
            fread (pUFrame_one, sizeof(unsigned char), (nSize)/4, pInpVideo_one);
            fread (pVFrame_one, sizeof(unsigned char), (nSize)/4, pInpVideo_one);
    
            fread (pYFrame_two, sizeof(unsigned char), (nSize), pInpVideo_two);
            fread (pUFrame_two, sizeof(unsigned char), (nSize)/4, pInpVideo_two);
            fread (pVFrame_two, sizeof(unsigned char), (nSize)/4, pInpVideo_two);
    
    
            fwrite(pYFrame_one, sizeof(unsigned char), (nSize), pTestOut);
            fseek(pTestOut, 0, SEEK_END);
    
            fwrite(pYFrame_two, sizeof(unsigned char), (nSize), pTestOut);
            fseek(pTestOut, 0, SEEK_END);
    
    
            fwrite(pUFrame_one, sizeof(unsigned char), (nSize)/4, pTestOut);
            fseek(pTestOut, 0, SEEK_END);
    
            fwrite(pUFrame_two, sizeof(unsigned char), (nSize)/4, pTestOut);
            fseek(pTestOut, 0, SEEK_END);
    
    
            fwrite(pVFrame_one, sizeof(unsigned char), (nSize)/4, pTestOut);
            fseek(pTestOut, 0, SEEK_END);
    
            fwrite(pVFrame_two, sizeof(unsigned char), (nSize)/4, pTestOut);
            fseek(pTestOut, 0, SEEK_END);
    
            fclose(pTestOut);
            fclose(pInpVideo_one);
            fclose(pInpVideo_two);
    
    
        return 0;
    }
    Quote Quote  
  14. Member
    Join Date
    Jul 2009
    Location
    Spain
    Search Comp PM
    Originally Posted by Streaman View Post
    Hi! I try to compose two YUV image (4:2:0, same resolution) in only one image (side by side) but i have some problem with this code because result is four compressed image in one!
    By simply appending the data (for each plane) for the two images, you are joining them top-to-bottom rather than side-by-side, creating an image of 288x176 instead of 144x352.
    (Obviously, if you try to view this as a 144x352 image, it will look odd.)

    For each plane, you need to write the data row by row in a loop to get them side-by-side.

    (Note also that the variables nWidth_out and nSize_out are unused.)
    Quote Quote  
  15. Thank you for reply. I did not understand very well how I write the data row by row in a loop
    Can you make an example? Thanks
    Quote Quote  
  16. If you want your frames side by side you need to stack your planes side by side, not top/bottom. Ie, you did this (1=frame1, 2=frame2):

    Code:
    11111111
    11111111
    11111111
    11111111
    22222222
    22222222
    22222222
    22222222
    instead of this:

    Code:
    1111111122222222
    1111111122222222
    1111111122222222
    1111111122222222
    You should have done this:

    Code:
    for (int y=0; y<nHeight; y++)
    {
             fwrite(&pYFrame_one[y*nWidth], sizeof(unsigned char), nWidth, pTestOut);
             fwrite(&pYFrame_two[y*nWidth], sizeof(unsigned char), nWidth, pTestOut);
    }
    Repeat for U and V planes -- except they are 1/2 the width and height, obviously.
    Last edited by jagabo; 27th Jan 2011 at 11:32.
    Quote Quote  
  17. It works fine!!!! Thanks a lot!
    I did not understand how to interpret the position of rows in the yuv file.
    Now I will try to build side by side on a yuv video. Let's hope so ...
    Quote Quote  
  18. The goal is getting more difficult!
    I wish I could take the video streams in real time, coded in YUV (4:2:0), by two different cameras. Someone tell me how to do it using C (or C + +)? Work under Linux. I trust in you.
    Thanks a lot!
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!