YUVJ420P -> RGB24 frame conversion?

For the developers that use FFmpeg in their software.
Post Reply
wcsinx
Posts: 8
Joined: Wed Feb 06, 2013 10:19 pm

YUVJ420P -> RGB24 frame conversion?

Post by wcsinx » Wed Feb 06, 2013 10:38 pm

First of all, my apologies if I'm not posting this in the right place.

I've been trying to get a chunk of code working that in a nutshell connects to a camera pumping out YUVj420p frames, converts them to simple RGB24 buffers, and then forwards that buffer on to another section of code. Well I just can't seem to get the conversion to work. I've scoured the Internet and this forum, and I keep coming up with the same answers which all basically show me the same code which I've posted below.

The problem is the RGB buffer gets screwed up somehow and the image skews to the right. Also the colors get messed up and most of the image is grayscale with sections of color here and there. (and those colors aren't quite right) If there's nothing that jumps out at anyone as omg-this-guy-is-retarded, then I'll post up pics.

Please be gentle. This is my first time using these libraries.

Code: Select all

static void ppm_save(AVFrame* incframe,
                     char *filename)
{
    FILE *f;
    uint8_t  *dst_data[4];
    int xsize,ysize,dst_linesize[4];

    xsize = incframe->width;
    ysize = incframe->height;

    struct SwsContext *sws_ctx;

    sws_ctx = sws_getContext(xsize, 
                             ysize, 
                             video_dec_ctx->pix_fmt, //AV_PIX_FMT_YUVJ420P,
                             xsize, 
                             ysize, 
                             AV_PIX_FMT_RGB24,
                             SWS_BILINEAR, 
                             NULL, 
                             NULL, 
                             NULL);

    int dst_bufsize = 
        av_image_alloc(dst_data,
                       dst_linesize,
                       xsize, 
                       ysize,  
                       AV_PIX_FMT_RGB24,
                       1);

    sws_scale(sws_ctx, 
              incframe->data,
              incframe->linesize, 
              0, 
              ysize, 
              dst_data, 
              dst_linesize);

    sws_freeContext(sws_ctx);

    f=fopen(filename,"w");
 
    fprintf(f,
            "P6\n%d %d\n%d\n",
            xsize,
            ysize,
            255);

    fwrite(dst_data[0],
           1,
           dst_bufsize,
           f);

    fclose(f);
}
As you can see, I'm just trying to do a straight ppm dump of the rgb buffer with no resizing in the conversion. Doing a pgm dump (which is to say only writing out the luminence channel) with much simpler code works fine. The (b&w) image is not distorted in that case.

What am I doing wrong? :?

biskitt
Posts: 9
Joined: Thu Jan 10, 2013 8:51 am

Re: YUVJ420P -> RGB24 frame conversion?

Post by biskitt » Thu Feb 07, 2013 9:02 am

Save your file in binary, in order to write all image data correctly. It should do the trick with:

Code: Select all

f=fopen(filename,"wb");

wcsinx
Posts: 8
Joined: Wed Feb 06, 2013 10:19 pm

Re: YUVJ420P -> RGB24 frame conversion?

Post by wcsinx » Thu Feb 07, 2013 1:34 pm

omg-i-am-retarded :lol:

I'm a little embarrassed that I didn't notice I was writing out an ASCII file.

That did the trick, Biskitt. Thank you!

ramiro
Posts: 157
Joined: Tue May 10, 2011 12:56 am

Re: YUVJ420P -> RGB24 frame conversion?

Post by ramiro » Thu Feb 07, 2013 8:17 pm

it's not writing them in ascii. basically it just changes \n to \r\n (so it adds a byte from time to time). that would explain the shifted image and skewed colors.

wcsinx
Posts: 8
Joined: Wed Feb 06, 2013 10:19 pm

Re: YUVJ420P -> RGB24 frame conversion?

Post by wcsinx » Tue Feb 12, 2013 1:57 pm

true true ... I copied the original PGM writing code from a Linux project then modified it for PPM. And Linux doesn't do the CRLF malarky, so it would have worked fine there. :?

ramiro
Posts: 157
Joined: Tue May 10, 2011 12:56 am

Re: YUVJ420P -> RGB24 frame conversion?

Post by ramiro » Tue Feb 12, 2013 8:46 pm

if you want to know exactly what happens in text mode, download windows nt4 source code from the piratebay and look for the read() and write() functions. write() just changes \n to \r\n and read() changes \r\n to \n and gives EOF on CTRL+Z (which is annoying when you forget to open a gzip file in binary mode because gzip will tell you it's corrupted and you won't know why).

but if you don't like reading leaked source code then don't do this =)

Post Reply
'