From 1d0dbeb232a7e589cc9ca25405d0f045059c4561 Mon Sep 17 00:00:00 2001 From: Francois-Olivier Devaux Date: Tue, 21 Aug 2007 10:18:40 +0000 Subject: [PATCH] Robustified MJ2 codecs --- mj2/frames_to_mj2.c | 16 ++++++++++------ mj2/mj2.c | 11 ++++++----- mj2/mj2.h | 4 ++-- mj2/mj2_convert.c | 16 ++++++++-------- mj2/mj2_convert.h | 2 +- mj2/mj2_to_frames.c | 2 ++ 6 files changed, 29 insertions(+), 22 deletions(-) diff --git a/mj2/frames_to_mj2.c b/mj2/frames_to_mj2.c index d95fbeb7..a519bda1 100644 --- a/mj2/frames_to_mj2.c +++ b/mj2/frames_to_mj2.c @@ -251,8 +251,7 @@ int main(int argc, char **argv) mj2_parameters.h = 288; // CIF default value mj2_parameters.CbCr_subsampling_dx = 2; // CIF default value mj2_parameters.CbCr_subsampling_dy = 2; // CIF default value - mj2_parameters.frame_rate = 25; - + mj2_parameters.frame_rate = 25; /* configure the event callbacks (not required) setting of each callback is optionnal @@ -275,6 +274,8 @@ int main(int argc, char **argv) sprintf(j2k_parameters->cp_comment,"%s%s", comment, version); } + mj2_parameters.decod_format = 0; + mj2_parameters.cod_format = 0; while (1) { int c = getopt(argc, argv, @@ -610,7 +611,7 @@ int main(int argc, char **argv) /* Error messages */ /* -------------- */ - if (!mj2_parameters.infile || !mj2_parameters.outfile) { + if (!mj2_parameters.cod_format || !mj2_parameters.decod_format) { fprintf(stderr, "Correct usage: mj2_encoder -i yuv-file -o mj2-file (+ options)\n"); return 1; @@ -681,8 +682,8 @@ int main(int argc, char **argv) mj2_setup_encoder(movie, &mj2_parameters); movie->tk[0].num_samples = yuv_num_frames(&movie->tk[0],mj2_parameters.infile); - if (!movie->tk[0].num_samples) { - fprintf(stderr,"Unable to count the number of frames in YUV input file\n"); + if (movie->tk[0].num_samples == -1) { + return 1; } // One sample per chunk @@ -705,6 +706,7 @@ int main(int argc, char **argv) cio_write(cio, MJ2_MDAT, 4); fwrite(buf,cio_tell(cio),1,mj2file); offset = cio_tell(cio); + opj_cio_close(cio); opj_free(buf); for (i = 0; i < movie->num_stk + movie->num_htk + movie->num_vtk; i++) { @@ -727,7 +729,7 @@ int main(int argc, char **argv) for (sampleno = 0; sampleno < numframes; sampleno++) { double init_time = opj_clock(); double elapsed_time; - if (!yuvtoimage(tk, img, sampleno, j2k_parameters, mj2_parameters.infile)) { + if (yuvtoimage(tk, img, sampleno, j2k_parameters, mj2_parameters.infile)) { fprintf(stderr, "Error with frame number %d in YUV file\n", sampleno); return 1; } @@ -751,6 +753,7 @@ int main(int argc, char **argv) len = cio_tell(cio) - 8; cio_seek(cio, 0); cio_write(cio, len+8,4); + opj_cio_close(cio); tk->sample[sampleno].sample_size = len+8; tk->sample[sampleno].offset = offset; tk->chunk[sampleno].offset = offset; // There is one sample per chunk @@ -784,6 +787,7 @@ int main(int argc, char **argv) cio = opj_cio_open(movie->cinfo, buf, (TEMP_BUF+numframes*20)); mj2_write_moov(movie, cio); fwrite(buf,cio_tell(cio),1,mj2file); + opj_free(buf); fprintf(stdout,"Total encoding time: %.2f s for %d frames (%.1f fps)\n", total_time, numframes, (float)numframes/total_time); diff --git a/mj2/mj2.c b/mj2/mj2.c index f4457950..022aa232 100644 --- a/mj2/mj2.c +++ b/mj2/mj2.c @@ -2081,7 +2081,7 @@ void mj2_write_mdhd(mj2_tk_t * tk, opj_cio_t *cio) cio_write(cio, tk->creation_time, 4); /* Creation Time */ time(<ime); /* Time since 1/1/70 */ - modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */ + modification_time = (unsigned int)ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */ cio_write(cio, modification_time, 4); /* Modification Time */ @@ -2224,7 +2224,7 @@ void mj2_write_tkhd(mj2_tk_t * tk, opj_cio_t *cio) cio_write(cio, 3, 4); /* Version=0, flags=3 */ time(<ime); /* Time since 1/1/70 */ - tk->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */ + tk->modification_time = (unsigned int)ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */ cio_write(cio, tk->creation_time, 4); /* Creation Time */ @@ -2416,7 +2416,7 @@ void mj2_write_mvhd(opj_mj2_t * movie, opj_cio_t *cio) cio_write(cio, 0, 4); /* Version = 0, flags = 0 */ time(<ime); /* Time since 1/1/70 */ - movie->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */ + movie->modification_time = (unsigned int)ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */ cio_write(cio, movie->creation_time, 4); /* Creation Time */ @@ -2748,8 +2748,10 @@ void mj2_setup_decoder(opj_mj2_t *movie, mj2_dparameters_t *mj2_parameters) { movie->num_vtk=0; movie->num_stk=0; movie->num_htk=0; + /* setup the J2K decoder parameters */ j2k_setup_decoder(movie->cinfo->j2k_handle, &mj2_parameters->j2k_parameters); + } void mj2_destroy_decompress(opj_mj2_t *movie) { @@ -2757,9 +2759,8 @@ void mj2_destroy_decompress(opj_mj2_t *movie) { int i; mj2_tk_t *tk=NULL; - if (movie->cinfo->j2k_handle) { + if (movie->cinfo->j2k_handle) j2k_destroy_compress(movie->j2k); - } if (movie->num_cl != 0) opj_free(movie->cl); diff --git a/mj2/mj2.h b/mj2/mj2.h index 3eee7148..97cf8ede 100644 --- a/mj2/mj2.h +++ b/mj2/mj2.h @@ -80,13 +80,13 @@ Decompressed format used in parameters YUV = 0 */ -#define YUV_DFMT 0 +#define YUV_DFMT 1 /** Compressed format used in parameters MJ2 = 0 */ -#define MJ2_CFMT 0 +#define MJ2_CFMT 2 /* ----------------------------------------------------------------------- */ diff --git a/mj2/mj2_convert.c b/mj2/mj2_convert.c index 589087a4..8868843a 100644 --- a/mj2/mj2_convert.c +++ b/mj2/mj2_convert.c @@ -46,7 +46,7 @@ int yuv_num_frames(mj2_tk_t * tk, char *infile) f = fopen(infile,"rb"); if (!f) { fprintf(stderr, "failed to open %s for reading\n",infile); - return 1; + return -1; } frame_size = (int) (tk->w * tk->h * (1.0 + (double) 2 / (double) (tk->CbCr_subsampling_dx * tk->CbCr_subsampling_dy))); /* Calculate frame size */ @@ -58,7 +58,7 @@ int yuv_num_frames(mj2_tk_t * tk, char *infile) fprintf(stderr, "YUV does not contains any frame of %d x %d size\n", tk->w, tk->h); - return 0; + return -1; } numimages = end_of_f / frame_size; /* Calculate number of images */ @@ -99,7 +99,7 @@ opj_image_t *mj2_image_create(mj2_tk_t * tk, opj_cparameters_t *parameters) return img; } -bool yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters_t *parameters, char* infile) +char yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters_t *parameters, char* infile) { int i, compno; int offset; @@ -111,8 +111,8 @@ bool yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters yuvfile = fopen(infile,"rb"); if (!yuvfile) { - fprintf(stderr, "failed to open %s for reading\n",parameters->infile); - return false; + fprintf(stderr, "failed to open %s for readings\n",parameters->infile); + return 1; } offset = (int) ((double) (frame_num * tk->w * tk->h) * (1.0 + @@ -125,7 +125,7 @@ bool yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters fprintf(stderr, "Cannot reach frame number %d in yuv file !!\n", frame_num); fclose(yuvfile); - return false; + return 1; } img->x0 = tk->Dim[0]; @@ -140,13 +140,13 @@ bool yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters && !feof(yuvfile); i++) { if (!fread(&img->comps[compno].data[i], 1, 1, yuvfile)) { fprintf(stderr, "Error reading %s file !!\n", infile); - return false; + return 1; } } } fclose(yuvfile); - return true; + return 0; } diff --git a/mj2/mj2_convert.h b/mj2/mj2_convert.h index e5f1f601..78e6c470 100644 --- a/mj2/mj2_convert.h +++ b/mj2/mj2_convert.h @@ -37,7 +37,7 @@ int imagetobmp(opj_image_t * img, char *outfile); opj_image_t *mj2_image_create(mj2_tk_t * tk, opj_cparameters_t *parameters); -bool yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters_t *parameters, char* infile); +char yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters_t *parameters, char* infile); int yuv_num_frames(mj2_tk_t * tk, char *infile); diff --git a/mj2/mj2_to_frames.c b/mj2/mj2_to_frames.c index 69357d31..bf4dfe6b 100644 --- a/mj2/mj2_to_frames.c +++ b/mj2/mj2_to_frames.c @@ -145,6 +145,7 @@ int main(int argc, char *argv[]) { { double init_time = opj_clock(); double elapsed_time; + sample = &track->sample[snum]; if (sample->sample_size-8 > max_codstrm_size) { max_codstrm_size = sample->sample_size-8; @@ -203,6 +204,7 @@ int main(int argc, char *argv[]) { free(frame_codestream); fclose(file); + /* free remaining structures */ if(dinfo) { mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle);