0

My task: Convert YUV Frame data to jpeg images by ffmpeg. What I have: Data of every plane and linesize for each one;

I tried to create empty AVFrame and fill it's data and linesize with this information, but after encoding it with CODEC_ID_MJPEG codec to images and saving it to files, a havent get correct jpegs.

What I must to do for getting images?

mmmaaak
  • 783
  • 1
  • 13
  • 38
  • Have you tried running an ffmpeg command that actually converts your YUV frames to JPEG? – Patrick.SE Apr 10 '12 at 15:49
  • I cant do it, becouse my YUV data is separated to different files (for each frame there 3 files with Y-data U-data and V-data, linesize of them displayed in their filenames) – mmmaaak Apr 10 '12 at 15:52
  • Hm, I'd write code to merge all three, shouldn't be hard. Cause if you can't test it in the cmd then chances of coding it are lower. – Patrick.SE Apr 10 '12 at 16:01
  • It sounds like you're close to solving this task. Can you post a sample image in order to illustrate the error? Sometimes it's easy to see the problem (U & V planes swapped; linesize off by 1). – Multimedia Mike Apr 11 '12 at 01:25

2 Answers2

0

Maybe you should use jpeglib to do this. Here's my code

FILE *outfile = fopen(path, "wb+");

if (!outfile)
    return 1;
h &= ~15;

struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);

cinfo.image_width = w; 
cinfo.image_height = h;
cinfo.input_components = 3;        
cinfo.in_color_space = JCS_YCbCr;

jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 90, TRUE);
cinfo.dct_method = JDCT_FASTEST; 

cinfo.raw_data_in = TRUE;
cinfo.comp_info[0].h_samp_factor = 2; 
cinfo.comp_info[0].v_samp_factor = 2; 
cinfo.comp_info[1].h_samp_factor = 1; 
cinfo.comp_info[1].v_samp_factor = 1; 
cinfo.comp_info[2].h_samp_factor = 1; 
cinfo.comp_info[2].v_samp_factor = 1; 

int i, j;
JSAMPROW y[16], cb[16], cr[16];
JSAMPARRAY p[3];

jpeg_start_compress(&cinfo, TRUE);
p[0] = y;
p[2] = cb;
p[1] = cr;

for (j = 0; j < cinfo.image_height; j += 16) {
    for (i = 0; i < 16; i++) {
        y[i] = data[0] + line[0]*(i+j);
        cr[i/2] = data[1] + line[1]*((i+j)/2);
        cb[i/2] = data[2] + line[2]*((i+j)/2);
    }
    jpeg_write_raw_data(&cinfo, p, 16);
}

jpeg_finish_compress(&cinfo);
fclose(outfile);
jpeg_destroy_compress(&cinfo);

where data is YUV planar data and line is linesize.

XieRan
  • 1
  • 1
-1

you need to scale your image to get proper JPEG files using `libswscale. one of my answer can help you with this. choose proper PIXEL FORMAT.

Community
  • 1
  • 1
N.Droid
  • 1,982
  • 15
  • 28